Subversion Repositories oidplus

Rev

Rev 152 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
104 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
112 daniel-mar 20
if (!defined('IN_OIDPLUS')) die();
21
 
104 daniel-mar 22
class OIDplusPagePublicLogin extends OIDplusPagePlugin {
23
        public function type() {
24
                return 'public';
25
        }
26
 
27
        public function priority() {
28
                return 90;
29
        }
30
 
31
        public function action(&$handled) {
32
                // === RA LOGIN/LOGOUT ===
33
 
107 daniel-mar 34
                if (isset($_POST["action"]) && ($_POST["action"] == "ra_login")) {
104 daniel-mar 35
                        $handled = true;
148 daniel-mar 36
 
119 daniel-mar 37
                        $email = $_POST['email'];
38
                        $ra = new OIDplusRA($email);
148 daniel-mar 39
 
104 daniel-mar 40
                        if (RECAPTCHA_ENABLED) {
41
                                $secret=RECAPTCHA_PRIVATE;
42
                                $response=$_POST["captcha"];
43
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
44
                                $captcha_success=json_decode($verify);
45
                                if ($captcha_success->success==false) {
107 daniel-mar 46
                                        die(json_encode(array("error" => 'Captcha wrong')));
104 daniel-mar 47
                                }
48
                        }
49
 
50
                        if ($ra->checkPassword($_POST['password'])) {
119 daniel-mar 51
                                OIDplus::logger()->log("RA($email)!", "RA '$email' logged in");
52
                                OIDplus::authUtils()::raLogin($email);
104 daniel-mar 53
 
150 daniel-mar 54
                                if (!OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."ra set last_login = now() where email = ?", array($email))) {
107 daniel-mar 55
                                        die(json_encode(array("error" => OIDplus::db()->error())));
104 daniel-mar 56
                                }
57
 
107 daniel-mar 58
                                echo json_encode(array("status" => 0));
104 daniel-mar 59
                        } else {
107 daniel-mar 60
                                die(json_encode(array("error" => "Wrong password")));
104 daniel-mar 61
                        }
62
                }
107 daniel-mar 63
                if (isset($_POST["action"]) && ($_POST["action"] == "ra_logout")) {
104 daniel-mar 64
                        $handled = true;
150 daniel-mar 65
 
119 daniel-mar 66
                        $email = $_POST['email'];
67
 
68
                        OIDplus::logger()->log("RA($email)!", "RA '$email' logged out");
69
                        OIDplus::authUtils()::raLogout($email);
107 daniel-mar 70
                        echo json_encode(array("status" => 0));
104 daniel-mar 71
                }
72
 
73
                // === ADMIN LOGIN/LOGOUT ===
74
 
107 daniel-mar 75
                if (isset($_POST["action"]) && ($_POST["action"] == "admin_login")) {
104 daniel-mar 76
                        $handled = true;
77
 
78
                        if (RECAPTCHA_ENABLED) {
79
                                $secret=RECAPTCHA_PRIVATE;
80
                                $response=$_POST["captcha"];
81
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
82
                                $captcha_success=json_decode($verify);
83
                                if ($captcha_success->success==false) {
107 daniel-mar 84
                                        die(json_encode(array("error" => 'Captcha wrong')));
104 daniel-mar 85
                                }
86
                        }
87
 
88
                        if (OIDplus::authUtils()::adminCheckPassword($_POST['password'])) {
115 daniel-mar 89
                                OIDplus::logger()->log("A!", "Admin logged in");
104 daniel-mar 90
                                OIDplus::authUtils()::adminLogin();
107 daniel-mar 91
                                echo json_encode(array("status" => 0));
104 daniel-mar 92
                        } else {
107 daniel-mar 93
                                die(json_encode(array("error" => "Wrong password")));
104 daniel-mar 94
                        }
95
                }
107 daniel-mar 96
                if (isset($_POST["action"]) && ($_POST["action"] == "admin_logout")) {
104 daniel-mar 97
                        $handled = true;
115 daniel-mar 98
                        OIDplus::logger()->log("A!", "Admin logged out");
104 daniel-mar 99
                        OIDplus::authUtils()::adminLogout();
107 daniel-mar 100
                        echo json_encode(array("status" => 0));
104 daniel-mar 101
                }
102
        }
103
 
104
        public function init($html=true) {
105
        }
106
 
107
        public function cfgSetValue($name, $value) {
108
        }
109
 
110
        public function gui($id, &$out, &$handled) {
111
                if ($id === 'oidplus:login') {
112
                        $handled = true;
113
                        $out['title'] = 'Login';
148 daniel-mar 114
                        $out['icon']  = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/login_big.png';
104 daniel-mar 115
 
116
                        $out['text'] .= '<noscript>';
117
                        $out['text'] .= '<p>You need to enable JavaScript to use the login area.</p>';
118
                        $out['text'] .= '</noscript>';
119
 
120
                        $out['text'] .= (RECAPTCHA_ENABLED ? '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.RECAPTCHA_PUBLIC.'" }); </script>'.
119 daniel-mar 121
                                                             '<p>Before logging in, please solve the following CAPTCHA</p><div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.RECAPTCHA_PUBLIC.'"></div>' : '');
104 daniel-mar 122
 
123
 
124
                        $out['text'] .= '<br>';
125
 
126
                        $out['text'] .= '<div id="loginArea" style="visibility: hidden"><div id="loginTab" class="container" style="width:100%;">';
127
                        $out['text'] .= '<ul  class="nav nav-pills">';
128
                        $out['text'] .= '                       <li class="active">';
119 daniel-mar 129
                        $out['text'] .= '                       <a href="#1a" data-toggle="tab">Login as RA</a>';
104 daniel-mar 130
                        $out['text'] .= '                       </li>';
131
                        $out['text'] .= '                       <li><a href="#2a" data-toggle="tab">Login as administrator</a>';
132
                        $out['text'] .= '                       </li>';
133
                        $out['text'] .= '               </ul>';
134
                        $out['text'] .= '                       <div class="tab-content clearfix">';
135
                        $out['text'] .= '                         <div class="tab-pane active" id="1a">';
136
 
137
                        $out['text'] .= '<h2>Login as RA</h2>';
138
 
139
                        $login_list = OIDplus::authUtils()->loggedInRaList();
140
                        if (count($login_list) > 0) {
115 daniel-mar 141
                                foreach ($login_list as $x) {
142
                                        $out['text'] .= '<p>You are logged in as <b>'.$x->raEmail().'</b> (<a href="#" onclick="return raLogout('.js_escape($x->raEmail()).');">Logout</a>)</p>';
104 daniel-mar 143
                                }
144
                                $out['text'] .= '<p>If you have more accounts, you can log in with a new account:</p>';
145
                        } else {
146
                                $out['text'] .= '<p>Enter your email address and your password to log in as Registration Authority.</p>';
147
                        }
107 daniel-mar 148
                        $out['text'] .= '<form onsubmit="return raLoginOnSubmit(this);">';
104 daniel-mar 149
                        $out['text'] .= '<input type="hidden" name="action" value="ra_login">';
152 daniel-mar 150
                        $out['text'] .= '<div><label class="padding_label">E-Mail:</label><input type="text" name="email" value="" id="raLoginEMail"></div>';
151
                        $out['text'] .= '<div><label class="padding_label">Password:</label><input type="password" name="password" value="" id="raLoginPassword"></div>';
152
                        $out['text'] .= '<br><input type="submit" value="Login"><br><br>';
104 daniel-mar 153
                        $out['text'] .= '</form>';
107 daniel-mar 154
                        $out['text'] .= '<p><a '.oidplus_link('oidplus:forgot_password').'>Forgot password?</a><br>';
104 daniel-mar 155
 
156 daniel-mar 156
 
157
                        if (OIDplus::config()->getValue('ra_invitation_enabled')) {
158
                                $out['text'] .= '<abbr title="To receive login data, the superior RA needs to send you an invitation. After creating or updating your OID, the system will ask them if they want to send you an invitation. If they accept, you will receive an email with an activation link. Alternatively, the system admin can create your account manually in the administrator control panel.">How to register?</abbr></p>';
159
                        } else {
160
                                $out['text'] .= '<abbr title="Since invitations are disabled at this OIDplus installation, the system administrator needs to create your account manually in the administrator control panel.">How to register?</abbr></p>';
161
                        }
162
 
104 daniel-mar 163
                        $out['text'] .= '                               </div>';
164
                        $out['text'] .= '                               <div class="tab-pane" id="2a">';
165
 
166
                        if (OIDplus::authUtils()::isAdminLoggedIn()) {
167
                                $out['text'] .= '<h2>Admin login</h2>';
168
                                $out['text'] .= '<p>You are logged in as administrator.</p>';
169
                                $out['text'] .= '<a href="#" onclick="return adminLogout();">Logout</a>';
170
                        } else {
171
                                $out['text'] .= '<h2>Login as administrator</h2>';
107 daniel-mar 172
                                $out['text'] .= '<form onsubmit="return adminLoginOnSubmit(this);">';
104 daniel-mar 173
                                $out['text'] .= '<input type="hidden" name="action" value="admin_login">';
152 daniel-mar 174
                                $out['text'] .= '<div><label class="padding_label">Password:</label><input type="password" name="password" value="" id="adminLoginPassword"></div>';
175
                                $out['text'] .= '<br><input type="submit" value="Login"><br><br>';
104 daniel-mar 176
                                $out['text'] .= '</form>';
149 daniel-mar 177
                                $out['text'] .= '<p><a '.oidplus_link('oidplus:forgot_password_admin').'>Forgot password?</a><br>';
104 daniel-mar 178
                        }
179
 
180
                        $out['text'] .= '                               </div>';
181
                        $out['text'] .= '                       </div>';
182
                        $out['text'] .= '  </div><br>';
183
                        $out['text'] .= '<p><font size="-1"><i>Privacy information</i>: By using the login functionality, you are accepting that a "session cookie" is temporarily stored in your browser. '.
184
                                        'The session cookie is a small text file that is sent to this website every time you visit it, to identify you as an already logged in user. '.
185
                                        'It does not track any of your online activities outside OIDplus. The cookie will be destroyed when you log out or after an inactivity of '.ceil(SESSION_LIFETIME/60).' minutes.</font></p></div>';
107 daniel-mar 186
 
187
                        $out['text'] .= '<script>document.getElementById("loginArea").style.visibility = "visible";</script>';
104 daniel-mar 188
                }
189
        }
190
 
106 daniel-mar 191
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
104 daniel-mar 192
                $loginChildren = array();
193
 
194
                if (OIDplus::authUtils()::isAdminLoggedIn()) {
195
                        $ra_roots = array();
196
 
197
                        foreach (OIDplus::getPagePlugins('admin') as $plugin) {
198
                                $plugin->tree($ra_roots);
199
                        }
200
 
201
                        $ra_roots[] = array(
202
                                'id'       => 'oidplus:logout$admin',
148 daniel-mar 203
                                'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_logout.png',
104 daniel-mar 204
                                'conditionalselect' => 'adminLogout()', // defined in oidplus.js
205
                                'text'     => 'Log out'
206
                        );
207
                        $loginChildren[] = array(
208
                                'id'       => 'oidplus:dummy$'.md5(rand()),
209
                                'text'     => "Logged in as admin",
148 daniel-mar 210
                                'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_admin.png',
104 daniel-mar 211
                                'conditionalselect' => 'false', // dummy node that can't be selected
212
                                'state'    => array("opened" => true),
213
                                'children' => $ra_roots
214
                        );
215
                }
216
 
115 daniel-mar 217
                foreach (OIDplus::authUtils()::loggedInRaList() as $ra) {
218
                        $ra_email = $ra->raEmail();
104 daniel-mar 219
                        $ra_roots = array();
220
 
221
                        foreach (OIDplus::getPagePlugins('ra') as $plugin) {
222
                                $plugin->tree($ra_roots, $ra_email);
223
                        }
224
 
225
                        $ra_roots[] = array(
226
                                'id'       => 'oidplus:logout$'.$ra_email,
116 daniel-mar 227
                                'conditionalselect' => 'raLogout('.js_escape($ra_email).')', // defined in oidplus.js
148 daniel-mar 228
                                'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_logout.png',
104 daniel-mar 229
                                'text'     => 'Log out'
230
                        );
231
                        foreach (OIDplusObject::getRaRoots($ra_email) as $loc_root) {
232
                                $ico = $loc_root->getIcon();
233
                                $ra_roots[] = array(
234
                                        'id' => 'oidplus:raroot$'.$loc_root->nodeId(),
235
                                        'text' => 'Jump to RA root '.$loc_root->objectTypeTitleShort().' '.$loc_root->crudShowId(OIDplusObject::parse($loc_root::root())),
110 daniel-mar 236
                                        'conditionalselect' => 'openOidInPanel('.js_escape($loc_root->nodeId()).', true);',
148 daniel-mar 237
                                        'icon' => !is_null($ico) ? $ico : 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_link.png'
104 daniel-mar 238
                                );
239
                        }
240
                        $ra_email_or_name = (new OIDplusRA($ra_email))->raName();
241
                        if ($ra_email_or_name == '') $ra_email_or_name = $ra_email;
242
                        $loginChildren[] = array(
243
                                'id'       => 'oidplus:dummy$'.md5(rand()),
244
                                'text'     => "Logged in as ".htmlentities($ra_email_or_name),
148 daniel-mar 245
                                'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_ra.png',
104 daniel-mar 246
                                'conditionalselect' => 'false', // dummy node that can't be selected
247
                                'state'    => array("opened" => true),
248
                                'children' => $ra_roots
249
                        );
250
                }
251
 
252
                $json[] = array(
253
                        'id'       => 'oidplus:login',
148 daniel-mar 254
                        'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_login.png',
104 daniel-mar 255
                        'text'     => 'Login',
256
                        'state'    => array("opened" => count($loginChildren)>0),
257
                        'children' => $loginChildren
258
                );
259
 
260
                return true;
261
        }
108 daniel-mar 262
 
263
        public function tree_search($request) {
264
                return false;
265
        }
104 daniel-mar 266
}
267
 
268
OIDplus::registerPagePlugin(new OIDplusPagePublicLogin());