OSDN Git Service

4885436f3c1ad63003750d6f5109649808f4b704
[pukiwiki/pukiwiki.git] / plugin / loginform.inc.php
1 <?php
2
3 // PukiWiki - Yet another WikiWikiWeb clone
4 // Copyright (C) 2015 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
6 //
7 // "Login form" plugin
8
9 function plugin_loginform_inline()
10 {
11         $logout_param = '?plugin=basicauthlogout';
12         return '<a href="' . htmlsc(get_script_uri() . $logout_param) . '">Log out</a>';
13 }
14
15 function plugin_loginform_convert()
16 {
17         return '<div>' . plugin_basicauthlogout_inline() . '</div>';
18 }
19
20 function plugin_loginform_action()
21 {
22         global $auth_user, $auth_type, $_loginform_messages;
23         $page = isset($_GET['page']) ? $_GET['page'] : '';
24         $pcmd = isset($_GET['pcmd']) ? $_GET['pcmd'] : '';
25         $url_after_login = isset($_GET['url_after_login']) ? $_GET['url_after_login'] : '';
26         $page_after_login = $page;
27         if (!$url_after_login) {
28                 $page_after_login = $page;
29         }
30         $action_url = get_script_uri() . '?plugin=loginform'
31                 . '&page=' . rawurlencode($page)
32                 . ($url_after_login ? '&url_after_login=' . rawurlencode($url_after_login) : '')
33                 . ($page_after_login ? '&page_after_login=' . rawurlencode($page_after_login) : '');
34         $username = isset($_POST['username']) ? $_POST['username'] : '';
35         $password = isset($_POST['password']) ? $_POST['password'] : '';
36         $isset_user_credential = $username || $password ;
37         if ($username && $password && form_auth($username, $password)) {
38                 // Sign in successfully completed
39                 form_auth_redirect($url_after_login, $page_after_login);
40                 return;
41         }
42         if ($pcmd === 'logout') {
43                 // logout
44                 switch ($auth_type) {
45                         case AUTH_TYPE_BASIC:
46                                 header('WWW-Authenticate: Basic realm="Please cancel to log out"');
47                                 header('HTTP/1.0 401 Unauthorized');
48                                 break;
49                         case AUTH_TYPE_FORM:
50                         case AUTH_TYPE_EXTERNAL:
51                         default:
52                                 $_SESSION = array();
53                                 session_regenerate_id(true); // require: PHP5.1+
54                                 session_destroy();
55                                 break;
56                 }
57                 $auth_user = '';
58                 return array(
59                         'msg' => 'Log out',
60                         'body' => 'Logged out completely<br>'
61                                 . '<a href="'. get_script_uri() . '?' . pagename_urlencode($page) . '">'
62                                 . $page . '</a>'
63                 );
64         } else {
65                 // login
66                 ob_start();
67 ?>
68 <style>
69   .loginformcontainer {
70     text-align: center;
71   }
72   .loginform table {
73     margin-top: 1em;
74         margin-left: auto;
75         margin-right: auto;
76   }
77   .loginform tbody td {
78     padding: .5em;
79   }
80   .loginform .label {
81     text-align: right;
82   }
83   .loginform .login-button-container {
84     text-align: right;
85   }
86   .loginform .loginbutton {
87     margin-top: 1em;
88   }
89   .loginform .errormessage {
90     color: red;
91   }
92 </style>
93 <div class="loginformcontainer">
94 <form name="loginform" class="loginform" action="<?php echo htmlsc($action_url) ?>" method="post">
95 <div>
96 <table style="border:0">
97   <tbody>
98   <tr>
99     <td class="label"><label for="_plugin_loginform_username"><?php echo htmlsc($_loginform_messages['username']) ?></label></td>
100     <td><input type="text" name="username" value="<?php echo htmlsc($username) ?>" id="_plugin_loginform_username"></td>
101   </tr>
102   <tr>
103   <td class="label"><label for="_plugin_loginform_password"><?php echo htmlsc($_loginform_messages['password']) ?></label></td>
104   <td><input type="password" name="password" id="_plugin_loginform_password"></td>
105   </tr>
106 <?php if ($isset_user_credential): ?>
107   <tr>
108     <td></td>
109     <td class="errormessage"><?php echo $_loginform_messages['invalid_username_or_password'] ?></td>
110   </tr>
111 <?php endif ?>
112   <tr>
113     <td></td>
114     <td class="login-button-container"><input type="submit" value="<?php echo htmlsc($_loginform_messages['login']) ?>" class="loginbutton"></td>
115   </tr>
116   </tbody>
117 </table>
118 </div>
119 <div>
120 </div>
121 </form>
122 </div>
123 <script><!--
124 window.addEventListener && window.addEventListener("DOMContentLoaded", function() {
125   var f = window.document.forms.loginform;
126                                 console.log(f);
127                                 console.log(f.username);
128                                 console.log(f.password);
129   if (f && f.username && f.password) {
130     if (f.username.value) {
131      f.password.focus && f.password.focus();
132         } else {
133      f.username.focus && f.username.focus();
134         }
135   }
136 });
137 //-->
138 </script>
139 <?php
140                 $body = ob_get_contents();
141                 ob_end_clean();
142                 return array(
143                         'msg' => $_loginform_messages['login'],
144                         'body' => $body,
145                         );
146         }
147 }