Subversion Repositories oidplus

Rev

Rev 149 | 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">';
150
                        $out['text'] .= '<label class="padding_label">E-Mail:</label><input type="text" name="email" value="" id="raLoginEMail"><br>';
151
                        $out['text'] .= '<label class="padding_label">Password:</label><input type="password" name="password" value="" id="raLoginPassword"><br><br>';
152
                        $out['text'] .= '<input type="submit" value="Login"><br><br>';
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
                        $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.">How to register?</abbr></p>';
156
 
157
                        $out['text'] .= '                               </div>';
158
                        $out['text'] .= '                               <div class="tab-pane" id="2a">';
159
 
160
                        if (OIDplus::authUtils()::isAdminLoggedIn()) {
161
                                $out['text'] .= '<h2>Admin login</h2>';
162
                                $out['text'] .= '<p>You are logged in as administrator.</p>';
163
                                $out['text'] .= '<a href="#" onclick="return adminLogout();">Logout</a>';
164
                        } else {
165
                                $out['text'] .= '<h2>Login as administrator</h2>';
107 daniel-mar 166
                                $out['text'] .= '<form onsubmit="return adminLoginOnSubmit(this);">';
104 daniel-mar 167
                                $out['text'] .= '<input type="hidden" name="action" value="admin_login">';
168
                                $out['text'] .= '<label class="padding_label">Password:</label><input type="password" name="password" value="" id="adminLoginPassword"><br><br>';
169
                                $out['text'] .= '<input type="submit" value="Login"><br><br>';
170
                                $out['text'] .= '</form>';
149 daniel-mar 171
                                $out['text'] .= '<p><a '.oidplus_link('oidplus:forgot_password_admin').'>Forgot password?</a><br>';
104 daniel-mar 172
                        }
173
 
174
                        $out['text'] .= '                               </div>';
175
                        $out['text'] .= '                       </div>';
176
                        $out['text'] .= '  </div><br>';
177
                        $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. '.
178
                                        '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. '.
179
                                        '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 180
 
181
                        $out['text'] .= '<script>document.getElementById("loginArea").style.visibility = "visible";</script>';
104 daniel-mar 182
                }
183
        }
184
 
106 daniel-mar 185
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
104 daniel-mar 186
                $loginChildren = array();
187
 
188
                if (OIDplus::authUtils()::isAdminLoggedIn()) {
189
                        $ra_roots = array();
190
 
191
                        foreach (OIDplus::getPagePlugins('admin') as $plugin) {
192
                                $plugin->tree($ra_roots);
193
                        }
194
 
195
                        $ra_roots[] = array(
196
                                'id'       => 'oidplus:logout$admin',
148 daniel-mar 197
                                'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_logout.png',
104 daniel-mar 198
                                'conditionalselect' => 'adminLogout()', // defined in oidplus.js
199
                                'text'     => 'Log out'
200
                        );
201
                        $loginChildren[] = array(
202
                                'id'       => 'oidplus:dummy$'.md5(rand()),
203
                                'text'     => "Logged in as admin",
148 daniel-mar 204
                                'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_admin.png',
104 daniel-mar 205
                                'conditionalselect' => 'false', // dummy node that can't be selected
206
                                'state'    => array("opened" => true),
207
                                'children' => $ra_roots
208
                        );
209
                }
210
 
115 daniel-mar 211
                foreach (OIDplus::authUtils()::loggedInRaList() as $ra) {
212
                        $ra_email = $ra->raEmail();
104 daniel-mar 213
                        $ra_roots = array();
214
 
215
                        foreach (OIDplus::getPagePlugins('ra') as $plugin) {
216
                                $plugin->tree($ra_roots, $ra_email);
217
                        }
218
 
219
                        $ra_roots[] = array(
220
                                'id'       => 'oidplus:logout$'.$ra_email,
116 daniel-mar 221
                                'conditionalselect' => 'raLogout('.js_escape($ra_email).')', // defined in oidplus.js
148 daniel-mar 222
                                'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_logout.png',
104 daniel-mar 223
                                'text'     => 'Log out'
224
                        );
225
                        foreach (OIDplusObject::getRaRoots($ra_email) as $loc_root) {
226
                                $ico = $loc_root->getIcon();
227
                                $ra_roots[] = array(
228
                                        'id' => 'oidplus:raroot$'.$loc_root->nodeId(),
229
                                        'text' => 'Jump to RA root '.$loc_root->objectTypeTitleShort().' '.$loc_root->crudShowId(OIDplusObject::parse($loc_root::root())),
110 daniel-mar 230
                                        'conditionalselect' => 'openOidInPanel('.js_escape($loc_root->nodeId()).', true);',
148 daniel-mar 231
                                        'icon' => !is_null($ico) ? $ico : 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_link.png'
104 daniel-mar 232
                                );
233
                        }
234
                        $ra_email_or_name = (new OIDplusRA($ra_email))->raName();
235
                        if ($ra_email_or_name == '') $ra_email_or_name = $ra_email;
236
                        $loginChildren[] = array(
237
                                'id'       => 'oidplus:dummy$'.md5(rand()),
238
                                'text'     => "Logged in as ".htmlentities($ra_email_or_name),
148 daniel-mar 239
                                'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_ra.png',
104 daniel-mar 240
                                'conditionalselect' => 'false', // dummy node that can't be selected
241
                                'state'    => array("opened" => true),
242
                                'children' => $ra_roots
243
                        );
244
                }
245
 
246
                $json[] = array(
247
                        'id'       => 'oidplus:login',
148 daniel-mar 248
                        'icon'     => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon_login.png',
104 daniel-mar 249
                        'text'     => 'Login',
250
                        'state'    => array("opened" => count($loginChildren)>0),
251
                        'children' => $loginChildren
252
                );
253
 
254
                return true;
255
        }
108 daniel-mar 256
 
257
        public function tree_search($request) {
258
                return false;
259
        }
104 daniel-mar 260
}
261
 
262
OIDplus::registerPagePlugin(new OIDplusPagePublicLogin());