OSDN Git Service

BugTrack/2525 Support make_link
[pukiwiki/pukiwiki.git] / plugin / loginform.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // Copyright 2015-2021 PukiWiki Development Team
4 // License: GPL v2 or (at your option) any later version
5 //
6 // "Login form" plugin
7
8 function plugin_loginform_inline()
9 {
10         $logout_param = '?plugin=basicauthlogout';
11         return '<a href="' . htmlsc(get_base_uri() . $logout_param) . '">Log out</a>';
12 }
13
14 function plugin_loginform_convert()
15 {
16         return '<div>' . plugin_loginform_inline() . '</div>';
17 }
18
19 function plugin_loginform_action()
20 {
21         global $auth_user, $auth_type, $_loginform_messages;
22         $page = isset($_GET['page']) ? $_GET['page'] : '';
23         $pcmd = isset($_GET['pcmd']) ? $_GET['pcmd'] : '';
24         $url_after_login = isset($_GET['url_after_login']) ? $_GET['url_after_login'] : '';
25         $page_after_login = $page;
26         if (!$url_after_login) {
27                 $page_after_login = $page;
28         }
29         $action_url = get_base_uri() . '?plugin=loginform'
30                 . '&page=' . rawurlencode($page)
31                 . ($url_after_login ? '&url_after_login=' . rawurlencode($url_after_login) : '')
32                 . ($page_after_login ? '&page_after_login=' . rawurlencode($page_after_login) : '');
33         $username = isset($_POST['username']) ? $_POST['username'] : '';
34         $password = isset($_POST['password']) ? $_POST['password'] : '';
35         $isset_user_credential = $username || $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                 exit; // or 'return FALSE;' - Don't double check for FORM_AUTH
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                         case AUTH_TYPE_SAML:
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_page_uri($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   if (f && f.username && f.password) {
127     if (f.username.value) {
128      f.password.focus && f.password.focus();
129         } else {
130      f.username.focus && f.username.focus();
131         }
132   }
133 });
134 //-->
135 </script>
136 <?php
137                 $body = ob_get_contents();
138                 ob_end_clean();
139                 return array(
140                         'msg' => $_loginform_messages['login'],
141                         'body' => $body,
142                         );
143         }
144 }