Subversion Repositories oidplus

Rev

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