OSDN Git Service

BugTrack2/375 Improve parameter handling in loginform plugin
[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         if ($username && $password && form_auth($username, $password)) {
37                 // Sign in successfully completed
38                 form_auth_redirect($url_after_login, $page_after_login);
39                 return;
40         }
41         if ($pcmd === 'logout') {
42                 // logout
43                 switch ($auth_type) {
44                         case AUTH_TYPE_BASIC:
45                                 header('WWW-Authenticate: Basic realm="Please cancel to log out"');
46                                 header('HTTP/1.0 401 Unauthorized');
47                                 break;
48                         case AUTH_TYPE_FORM:
49                         case AUTH_TYPE_EXTERNAL:
50                         default:
51                                 $_SESSION = array();
52                                 session_regenerate_id(true); // require: PHP5.1+
53                                 session_destroy();
54                                 break;
55                 }
56                 $auth_user = '';
57                 return array(
58                         'msg' => 'Log out',
59                         'body' => 'Logged out completely<br>'
60                                 . '<a href="'. get_script_uri() . '?' . pagename_urlencode($page) . '">'
61                                 . $page . '</a>'
62                 );
63         } else {
64                 // login
65                 $action_url_html = htmlsc($action_url);
66                 $username_html = htmlsc($username);
67                 $username_label_html = htmlsc($_loginform_messages['username']);
68                 $password_label_html = htmlsc($_loginform_messages['password']);
69                 $login_label_html = htmlsc($_loginform_messages['login']);
70                 $body = <<< EOT
71 <style>
72   .loginformcontainer {
73     text-align: center;
74   }
75   .loginform table {
76     margin-top: 1em;
77         margin-left: auto;
78         margin-right: auto;
79   }
80   .loginform tbody td {
81     padding: .5em;
82   }
83   .loginform .label {
84     text-align: right;
85   }
86   .loginform .login-button-container {
87     text-align: right;
88   }
89   .loginform .loginbutton {
90     margin-top: 1em;
91   }
92 </style>
93 <div class="loginformcontainer">
94 <form name="loginform" class="loginform" action="$action_url_html" method="post">
95 <div>
96 <table style="border:0">
97   <tbody>
98   <tr>
99     <td class="label"><label for="_plugin_loginform_username">$username_label_html</label></td>
100     <td><input type="text" name="username" value="$username_html" id="_plugin_loginform_username"></td>
101   </tr>
102   <tr>
103   <td class="label"><label for="_plugin_loginform_password">$password_label_html</label></td>
104   <td><input type="password" name="password" id="_plugin_loginform_password"></td>
105   </tr>
106   <tr>
107     <td></td>
108     <td class="login-button-container"><input type="submit" value="$login_label_html" class="loginbutton"></td>
109   </tr>
110   </tbody>
111 </table>
112 </div>
113 <div>
114 </div>
115 </form>
116 </div>
117 <script><!--
118 window.addEventListener && window.addEventListener("DOMContentLoaded", function() {
119   var f = window.document.forms.loginform;
120                                 console.log(f);
121                                 console.log(f.username);
122                                 console.log(f.password);
123   if (f && f.username && f.password) {
124     if (f.username.value) {
125      f.password.focus && f.password.focus();
126         } else {
127      f.username.focus && f.username.focus();
128         }
129   }
130 });
131 //-->
132 </script>
133 EOT;
134                 return array(
135                         'msg' => $_loginform_messages['login'],
136                         'body' => $body,
137                         );
138         }
139 }