Subversion Repositories oidplus

Rev

Rev 241 | 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
 
222 daniel-mar 27
        public static function getPluginInformation() {
28
                $out = array();
29
                $out['name'] = 'Login';
30
                $out['author'] = 'ViaThinkSoft';
31
                $out['version'] = null;
32
                $out['descriptionHTML'] = null;
33
                return $out;
34
        }
35
 
104 daniel-mar 36
        public function priority() {
37
                return 90;
38
        }
39
 
40
        public function action(&$handled) {
41
                // === RA LOGIN/LOGOUT ===
42
 
107 daniel-mar 43
                if (isset($_POST["action"]) && ($_POST["action"] == "ra_login")) {
104 daniel-mar 44
                        $handled = true;
148 daniel-mar 45
 
119 daniel-mar 46
                        $email = $_POST['email'];
47
                        $ra = new OIDplusRA($email);
148 daniel-mar 48
 
104 daniel-mar 49
                        if (RECAPTCHA_ENABLED) {
50
                                $secret=RECAPTCHA_PRIVATE;
51
                                $response=$_POST["captcha"];
52
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
53
                                $captcha_success=json_decode($verify);
54
                                if ($captcha_success->success==false) {
250 daniel-mar 55
                                        throw new OIDplusException('Captcha wrong');
104 daniel-mar 56
                                }
57
                        }
58
 
59
                        if ($ra->checkPassword($_POST['password'])) {
119 daniel-mar 60
                                OIDplus::logger()->log("RA($email)!", "RA '$email' logged in");
61
                                OIDplus::authUtils()::raLogin($email);
104 daniel-mar 62
 
239 daniel-mar 63
                                if (OIDplus::db()->slang() == 'mssql') {
64
                                        OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."ra set last_login = getdate() where email = ?", array($email));
65
                                } else {
66
                                        // MySQL + PgSQL
67
                                        OIDplus::db()->query("UPDATE ".OIDPLUS_TABLENAME_PREFIX."ra set last_login = now() where email = ?", array($email));
68
                                }
104 daniel-mar 69
 
107 daniel-mar 70
                                echo json_encode(array("status" => 0));
104 daniel-mar 71
                        } else {
250 daniel-mar 72
                                throw new OIDplusException('Wrong password');
104 daniel-mar 73
                        }
74
                }
107 daniel-mar 75
                if (isset($_POST["action"]) && ($_POST["action"] == "ra_logout")) {
104 daniel-mar 76
                        $handled = true;
150 daniel-mar 77
 
119 daniel-mar 78
                        $email = $_POST['email'];
79
 
80
                        OIDplus::logger()->log("RA($email)!", "RA '$email' logged out");
81
                        OIDplus::authUtils()::raLogout($email);
107 daniel-mar 82
                        echo json_encode(array("status" => 0));
104 daniel-mar 83
                }
84
 
85
                // === ADMIN LOGIN/LOGOUT ===
86
 
107 daniel-mar 87
                if (isset($_POST["action"]) && ($_POST["action"] == "admin_login")) {
104 daniel-mar 88
                        $handled = true;
89
 
90
                        if (RECAPTCHA_ENABLED) {
91
                                $secret=RECAPTCHA_PRIVATE;
92
                                $response=$_POST["captcha"];
93
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
94
                                $captcha_success=json_decode($verify);
95
                                if ($captcha_success->success==false) {
250 daniel-mar 96
                                        throw new OIDplusException('Captcha wrong');
104 daniel-mar 97
                                }
98
                        }
99
 
100
                        if (OIDplus::authUtils()::adminCheckPassword($_POST['password'])) {
115 daniel-mar 101
                                OIDplus::logger()->log("A!", "Admin logged in");
104 daniel-mar 102
                                OIDplus::authUtils()::adminLogin();
107 daniel-mar 103
                                echo json_encode(array("status" => 0));
104 daniel-mar 104
                        } else {
250 daniel-mar 105
                                throw new OIDplusException('Wrong password');
104 daniel-mar 106
                        }
107
                }
107 daniel-mar 108
                if (isset($_POST["action"]) && ($_POST["action"] == "admin_logout")) {
104 daniel-mar 109
                        $handled = true;
115 daniel-mar 110
                        OIDplus::logger()->log("A!", "Admin logged out");
104 daniel-mar 111
                        OIDplus::authUtils()::adminLogout();
107 daniel-mar 112
                        echo json_encode(array("status" => 0));
104 daniel-mar 113
                }
114
        }
115
 
116
        public function init($html=true) {
117
        }
118
 
119
        public function cfgSetValue($name, $value) {
120
        }
121
 
122
        public function gui($id, &$out, &$handled) {
123
                if ($id === 'oidplus:login') {
124
                        $handled = true;
125
                        $out['title'] = 'Login';
241 daniel-mar 126
                        $out['icon']  = OIDplus::webpath(__DIR__).'login_big.png';
104 daniel-mar 127
 
128
                        $out['text'] .= '<noscript>';
129
                        $out['text'] .= '<p>You need to enable JavaScript to use the login area.</p>';
130
                        $out['text'] .= '</noscript>';
131
 
219 daniel-mar 132
                        $out['text'] .= '<div id="loginArea" style="visibility: hidden"><div id="loginTab" class="container" style="width:100%;">';
104 daniel-mar 133
                        $out['text'] .= (RECAPTCHA_ENABLED ? '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.RECAPTCHA_PUBLIC.'" }); </script>'.
119 daniel-mar 134
                                                             '<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 135
                        $out['text'] .= '<br>';
219 daniel-mar 136
                        $out['text'] .= '<ul class="nav nav-pills">';
104 daniel-mar 137
                        $out['text'] .= '                       <li class="active">';
119 daniel-mar 138
                        $out['text'] .= '                       <a href="#1a" data-toggle="tab">Login as RA</a>';
104 daniel-mar 139
                        $out['text'] .= '                       </li>';
140
                        $out['text'] .= '                       <li><a href="#2a" data-toggle="tab">Login as administrator</a>';
141
                        $out['text'] .= '                       </li>';
142
                        $out['text'] .= '               </ul>';
143
                        $out['text'] .= '                       <div class="tab-content clearfix">';
144
                        $out['text'] .= '                         <div class="tab-pane active" id="1a">';
145
 
146
                        $out['text'] .= '<h2>Login as RA</h2>';
147
 
148
                        $login_list = OIDplus::authUtils()->loggedInRaList();
149
                        if (count($login_list) > 0) {
115 daniel-mar 150
                                foreach ($login_list as $x) {
151
                                        $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 152
                                }
153
                                $out['text'] .= '<p>If you have more accounts, you can log in with a new account:</p>';
154
                        } else {
155
                                $out['text'] .= '<p>Enter your email address and your password to log in as Registration Authority.</p>';
156
                        }
107 daniel-mar 157
                        $out['text'] .= '<form onsubmit="return raLoginOnSubmit(this);">';
104 daniel-mar 158
                        $out['text'] .= '<input type="hidden" name="action" value="ra_login">';
152 daniel-mar 159
                        $out['text'] .= '<div><label class="padding_label">E-Mail:</label><input type="text" name="email" value="" id="raLoginEMail"></div>';
160
                        $out['text'] .= '<div><label class="padding_label">Password:</label><input type="password" name="password" value="" id="raLoginPassword"></div>';
161
                        $out['text'] .= '<br><input type="submit" value="Login"><br><br>';
104 daniel-mar 162
                        $out['text'] .= '</form>';
250 daniel-mar 163
                        $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:forgot_password').'>Forgot password?</a><br>';
104 daniel-mar 164
 
156 daniel-mar 165
 
166
                        if (OIDplus::config()->getValue('ra_invitation_enabled')) {
167
                                $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>';
168
                        } else {
169
                                $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>';
170
                        }
171
 
104 daniel-mar 172
                        $out['text'] .= '                               </div>';
173
                        $out['text'] .= '                               <div class="tab-pane" id="2a">';
174
 
175
                        if (OIDplus::authUtils()::isAdminLoggedIn()) {
176
                                $out['text'] .= '<h2>Admin login</h2>';
177
                                $out['text'] .= '<p>You are logged in as administrator.</p>';
178
                                $out['text'] .= '<a href="#" onclick="return adminLogout();">Logout</a>';
179
                        } else {
180
                                $out['text'] .= '<h2>Login as administrator</h2>';
107 daniel-mar 181
                                $out['text'] .= '<form onsubmit="return adminLoginOnSubmit(this);">';
104 daniel-mar 182
                                $out['text'] .= '<input type="hidden" name="action" value="admin_login">';
152 daniel-mar 183
                                $out['text'] .= '<div><label class="padding_label">Password:</label><input type="password" name="password" value="" id="adminLoginPassword"></div>';
184
                                $out['text'] .= '<br><input type="submit" value="Login"><br><br>';
104 daniel-mar 185
                                $out['text'] .= '</form>';
250 daniel-mar 186
                                $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:forgot_password_admin').'>Forgot password?</a><br>';
104 daniel-mar 187
                        }
188
 
189
                        $out['text'] .= '                               </div>';
190
                        $out['text'] .= '                       </div>';
191
                        $out['text'] .= '  </div><br>';
192
                        $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. '.
193
                                        '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. '.
219 daniel-mar 194
                                        '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.';
195
                        $privacy_document_file = 'res/OIDplus/privacy_documentation.html';
196
                        if (class_exists('OIDplusPagePublicResources') && file_exists($privacy_document_file)) {
250 daniel-mar 197
                                $out['text'] .= ' <a '.OIDplus::gui()->link('oidplus:resources$'.$privacy_document_file.'$'.OIDplus::authUtils()::makeAuthKey("resources;".$privacy_document_file).'#cookies').'>More information about the cookies used</a>';
219 daniel-mar 198
                        }
199
                        $out['text'] .= '</font></p></div>';
107 daniel-mar 200
 
201
                        $out['text'] .= '<script>document.getElementById("loginArea").style.visibility = "visible";</script>';
104 daniel-mar 202
                }
203
        }
204
 
106 daniel-mar 205
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
104 daniel-mar 206
                $loginChildren = array();
207
 
208
                if (OIDplus::authUtils()::isAdminLoggedIn()) {
209
                        $ra_roots = array();
210
 
211
                        foreach (OIDplus::getPagePlugins('admin') as $plugin) {
212
                                $plugin->tree($ra_roots);
213
                        }
214
 
215
                        $ra_roots[] = array(
216
                                'id'       => 'oidplus:logout$admin',
241 daniel-mar 217
                                'icon'     => OIDplus::webpath(__DIR__).'treeicon_logout.png',
225 daniel-mar 218
                                'conditionalselect' => 'adminLogout()', // defined in oidplus_base.js
104 daniel-mar 219
                                'text'     => 'Log out'
220
                        );
221
                        $loginChildren[] = array(
222
                                'id'       => 'oidplus:dummy$'.md5(rand()),
223
                                'text'     => "Logged in as admin",
241 daniel-mar 224
                                'icon'     => OIDplus::webpath(__DIR__).'treeicon_admin.png',
104 daniel-mar 225
                                'conditionalselect' => 'false', // dummy node that can't be selected
226
                                'state'    => array("opened" => true),
227
                                'children' => $ra_roots
228
                        );
229
                }
230
 
115 daniel-mar 231
                foreach (OIDplus::authUtils()::loggedInRaList() as $ra) {
232
                        $ra_email = $ra->raEmail();
104 daniel-mar 233
                        $ra_roots = array();
234
 
235
                        foreach (OIDplus::getPagePlugins('ra') as $plugin) {
236
                                $plugin->tree($ra_roots, $ra_email);
237
                        }
238
 
239
                        $ra_roots[] = array(
240
                                'id'       => 'oidplus:logout$'.$ra_email,
225 daniel-mar 241
                                'conditionalselect' => 'raLogout('.js_escape($ra_email).')', // defined in oidplus_base.js
241 daniel-mar 242
                                'icon'     => OIDplus::webpath(__DIR__).'treeicon_logout.png',
104 daniel-mar 243
                                'text'     => 'Log out'
244
                        );
245
                        foreach (OIDplusObject::getRaRoots($ra_email) as $loc_root) {
246
                                $ico = $loc_root->getIcon();
247
                                $ra_roots[] = array(
248
                                        'id' => 'oidplus:raroot$'.$loc_root->nodeId(),
249
                                        'text' => 'Jump to RA root '.$loc_root->objectTypeTitleShort().' '.$loc_root->crudShowId(OIDplusObject::parse($loc_root::root())),
110 daniel-mar 250
                                        'conditionalselect' => 'openOidInPanel('.js_escape($loc_root->nodeId()).', true);',
241 daniel-mar 251
                                        'icon' => !is_null($ico) ? $ico : OIDplus::webpath(__DIR__).'treeicon_link.png'
104 daniel-mar 252
                                );
253
                        }
254
                        $ra_email_or_name = (new OIDplusRA($ra_email))->raName();
255
                        if ($ra_email_or_name == '') $ra_email_or_name = $ra_email;
256
                        $loginChildren[] = array(
257
                                'id'       => 'oidplus:dummy$'.md5(rand()),
258
                                'text'     => "Logged in as ".htmlentities($ra_email_or_name),
241 daniel-mar 259
                                'icon'     => OIDplus::webpath(__DIR__).'treeicon_ra.png',
104 daniel-mar 260
                                'conditionalselect' => 'false', // dummy node that can't be selected
261
                                'state'    => array("opened" => true),
262
                                'children' => $ra_roots
263
                        );
264
                }
265
 
266
                $json[] = array(
267
                        'id'       => 'oidplus:login',
241 daniel-mar 268
                        'icon'     => OIDplus::webpath(__DIR__).'treeicon_login.png',
104 daniel-mar 269
                        'text'     => 'Login',
270
                        'state'    => array("opened" => count($loginChildren)>0),
271
                        'children' => $loginChildren
272
                );
273
 
274
                return true;
275
        }
108 daniel-mar 276
 
277
        public function tree_search($request) {
278
                return false;
279
        }
104 daniel-mar 280
}