OSDN Git Service

BugTrack/2213 Set Absolute URI or Root relative path
[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                         case AUTH_TYPE_SAML:
52                         default:
53                                 $_SESSION = array();
54                                 session_regenerate_id(true); // require: PHP5.1+
55                                 session_destroy();
56                                 break;
57                 }
58                 $auth_user = '';
59                 return array(
60                         'msg' => 'Log out',
61                         'body' => 'Logged out completely<br>'
62                                 . '<a href="'. get_script_uri() . '?' . pagename_urlencode($page) . '">'
63                                 . $page . '</a>'
64                 );
65         } else {
66                 // login
67                 ob_start();
68 ?>
69 <style>
70   .loginformcontainer {
71     text-align: center;
72   }
73   .loginform table {
74     margin-top: 1em;
75         margin-left: auto;
76         margin-right: auto;
77   }
78   .loginform tbody td {
79     padding: .5em;
80   }
81   .loginform .label {
82     text-align: right;
83   }
84   .loginform .login-button-container {
85     text-align: right;
86   }
87   .loginform .loginbutton {
88     margin-top: 1em;
89   }
90   .loginform .errormessage {
91     color: red;
92   }
93 </style>
94 <div class="loginformcontainer">
95 <form name="loginform" class="loginform" action="<?php echo htmlsc($action_url) ?>" method="post">
96 <div>
97 <table style="border:0">
98   <tbody>
99   <tr>
100     <td class="label"><label for="_plugin_loginform_username"><?php echo htmlsc($_loginform_messages['username']) ?></label></td>
101     <td><input type="text" name="username" value="<?php echo htmlsc($username) ?>" id="_plugin_loginform_username"></td>
102   </tr>
103   <tr>
104   <td class="label"><label for="_plugin_loginform_password"><?php echo htmlsc($_loginform_messages['password']) ?></label></td>
105   <td><input type="password" name="password" id="_plugin_loginform_password"></td>
106   </tr>
107 <?php if ($isset_user_credential): ?>
108   <tr>
109     <td></td>
110     <td class="errormessage"><?php echo $_loginform_messages['invalid_username_or_password'] ?></td>
111   </tr>
112 <?php endif ?>
113   <tr>
114     <td></td>
115     <td class="login-button-container"><input type="submit" value="<?php echo htmlsc($_loginform_messages['login']) ?>" class="loginbutton"></td>
116   </tr>
117   </tbody>
118 </table>
119 </div>
120 <div>
121 </div>
122 </form>
123 </div>
124 <script><!--
125 window.addEventListener && window.addEventListener("DOMContentLoaded", function() {
126   var f = window.document.forms.loginform;
127                                 console.log(f);
128                                 console.log(f.username);
129                                 console.log(f.password);
130   if (f && f.username && f.password) {
131     if (f.username.value) {
132      f.password.focus && f.password.focus();
133         } else {
134      f.username.focus && f.username.focus();
135         }
136   }
137 });
138 //-->
139 </script>
140 <?php
141                 $body = ob_get_contents();
142                 ob_end_clean();
143                 return array(
144                         'msg' => $_loginform_messages['login'],
145                         'body' => $body,
146                         );
147         }
148 }