Subversion Repositories oidplus

Rev

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