Rev 104 | 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 | |||
20 | class OIDplusPagePublicInvite extends OIDplusPagePlugin { |
||
21 | public function type() { |
||
22 | return 'public'; |
||
23 | } |
||
24 | |||
25 | public function priority() { |
||
26 | return 92; |
||
27 | } |
||
28 | |||
29 | public function action(&$handled) { |
||
30 | if ($_POST["action"] == "invite_ra") { |
||
31 | $handled = true; |
||
32 | $email = $_POST['email']; |
||
33 | |||
34 | if (!oidplus_valid_email($email)) { |
||
35 | die('Invalid email address'); |
||
36 | } |
||
37 | |||
38 | if (RECAPTCHA_ENABLED) { |
||
39 | $secret=RECAPTCHA_PRIVATE; |
||
40 | $response=$_POST["captcha"]; |
||
41 | $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}"); |
||
42 | $captcha_success=json_decode($verify); |
||
43 | if ($captcha_success->success==false) { |
||
44 | die('Captcha wrong'); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | $timestamp = time(); |
||
49 | $activate_url = OIDplus::system_url() . '?goto='.urlencode('oidplus:activate_ra$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('activate_ra;'.$email.';'.$timestamp)); |
||
50 | |||
51 | $message = OIDplus::gui()::getInvitationText($_POST['email']); |
||
52 | $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message); |
||
53 | |||
54 | my_mail($email, OIDplus::config()->systemTitle().' - Invitation', $message, OIDplus::config()->globalCC()); |
||
55 | |||
56 | die("OK"); |
||
57 | } |
||
58 | |||
59 | if ($_POST["action"] == "activate_ra") { |
||
60 | $handled = true; |
||
61 | |||
62 | $password1 = $_POST['password1']; |
||
63 | $password2 = $_POST['password2']; |
||
64 | $email = $_POST['email']; |
||
65 | $auth = $_POST['auth']; |
||
66 | $timestamp = $_POST['timestamp']; |
||
67 | |||
68 | if (!OIDplus::authUtils()::validateAuthKey('activate_ra;'.$email.';'.$timestamp, $auth)) { |
||
69 | die('Invalid auth key'); |
||
70 | } |
||
71 | |||
72 | if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) { |
||
73 | die('Invitation expired!'); |
||
74 | } |
||
75 | |||
76 | if ($password1 !== $password2) { |
||
77 | die('Passwords are not equal'); |
||
78 | } |
||
79 | |||
80 | if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) { |
||
81 | die('Password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength()); |
||
82 | } |
||
83 | |||
84 | $ra = new OIDplusRA($email); |
||
85 | $ra->register_ra($password1); |
||
86 | |||
87 | die('OK'); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public function init($html=true) { |
||
92 | OIDplus::config()->prepareConfigKey('max_ra_invite_time', 'Max RA invite time in seconds (0 = infinite)', '0', 0, 1); |
||
93 | } |
||
94 | |||
95 | public function cfgSetValue($name, $value) { |
||
96 | if ($name == 'max_ra_invite_time') { |
||
97 | if (!is_numeric($value) || ($value < 0)) { |
||
98 | throw new Exception("Please enter a valid value."); |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public function gui($id, &$out, &$handled) { |
||
104 | if (explode('$',$id)[0] == 'oidplus:invite_ra') { |
||
105 | $handled = true; |
||
106 | |||
107 | $email = explode('$',$id)[1]; |
||
108 | $origin = explode('$',$id)[2]; |
||
109 | |||
110 | $out['title'] = 'Invite a Registration Authority'; |
||
111 | $out['icon'] = 'plugins/publicPages/'.basename(__DIR__).'/invite_ra_big.png'; |
||
112 | |||
113 | try { |
||
114 | $cont = self::getInvitationText($email); |
||
115 | |||
116 | $out['text'] .= '<p>You have chosen to invite <b>'.$email.'</b> as an Registration Authority. If you click "Send", the following email will be sent to '.$email.':</p><p><i>'.nl2br(htmlentities($cont)).'</i></p> |
||
117 | <form id="inviteForm" onsubmit="return inviteFormOnSubmit();"> |
||
118 | <input type="hidden" id="email" value="'.htmlentities($email).'"/> |
||
119 | <input type="hidden" id="origin" value="'.htmlentities($origin).'"/>'. |
||
120 | (RECAPTCHA_ENABLED ? '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.RECAPTCHA_PUBLIC.'" }); </script>'. |
||
121 | '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.RECAPTCHA_PUBLIC.'"></div>' : ''). |
||
122 | ' <br> |
||
123 | <input type="submit" value="Send invitation"> |
||
124 | </form>'; |
||
125 | |||
126 | } catch (Exception $e) { |
||
127 | |||
128 | $out['icon'] = 'img/error_big.png'; |
||
129 | $out['text'] = "Error: ".$e->getMessage(); |
||
130 | |||
131 | } |
||
132 | } else if (explode('$',$id)[0] == 'oidplus:activate_ra') { |
||
133 | $handled = true; |
||
134 | |||
135 | $email = explode('$',$id)[1]; |
||
136 | $timestamp = explode('$',$id)[2]; |
||
137 | $auth = explode('$',$id)[3]; |
||
138 | |||
139 | $out['title'] = 'Register as Registration Authority'; |
||
140 | $out['icon'] = 'plugins/publicPages/'.basename(__DIR__).'/activate_ra_big.png'; |
||
141 | |||
142 | $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($email)."'"); |
||
143 | if (OIDplus::db()->num_rows($res) > 0) { |
||
144 | $out['text'] = 'This RA is already registered and does not need to be invited.'; |
||
145 | } else { |
||
146 | if (!OIDplus::authUtils()::validateAuthKey('activate_ra;'.$email.';'.$timestamp, $auth)) { |
||
147 | $out['icon'] = 'img/error_big.png'; |
||
148 | $out['text'] = 'Invalid authorization. Is the URL OK?'; |
||
149 | } else { |
||
150 | // TODO: like in the FreeOID plugin, we could ask here at least for a name for the RA |
||
151 | $out['text'] = '<p>E-Mail-Adress: <b>'.$email.'</b></p> |
||
152 | |||
153 | <form id="activateRaForm" onsubmit="return activateRaFormOnSubmit();"> |
||
154 | <input type="hidden" id="email" value="'.htmlentities($email).'"/> |
||
155 | <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/> |
||
156 | <input type="hidden" id="auth" value="'.htmlentities($auth).'"/> |
||
157 | <label class="padding_label">New password:</label><input type="password" id="password1" value=""/><br><br> |
||
158 | <label class="padding_label">Again:</label><input type="password" id="password2" value=""/><br><br> |
||
159 | <input type="submit" value="Register"> |
||
160 | </form>'; |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
106 | daniel-mar | 166 | public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') { |
104 | daniel-mar | 167 | return false; |
168 | } |
||
169 | |||
170 | private function getInvitationText($email) { |
||
171 | $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($email)."'"); |
||
172 | if (OIDplus::db()->num_rows($res) > 0) { |
||
173 | throw new Exception("This RA is already registered and does not need to be invited."); |
||
174 | } |
||
175 | |||
176 | if (!OIDplus::authUtils()::isAdminLoggedIn()) { |
||
177 | // Check if the RA may invite the user (i.e. the they are the parent of an OID of that person) |
||
178 | $ok = false; |
||
179 | $res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where ra_email = '".OIDplus::db()->real_escape_string($email)."'"); |
||
180 | while ($row = OIDplus::db()->fetch_array($res)) { |
||
181 | $objParent = OIDplusObject::parse($row['parent']); |
||
182 | if (is_null($objParent)) throw new Exception("Type of ".$row['parent']." unknown"); |
||
183 | if ($objParent->userHasWriteRights()) { |
||
184 | $ok = true; |
||
185 | } |
||
186 | } |
||
187 | if (!$ok) { |
||
188 | throw new Exception('You may not invite this RA. Maybe you need to log in again.'); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | $list_of_oids = array(); |
||
193 | $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where ra_email = '".OIDplus::db()->real_escape_string($email)."'"); |
||
194 | while ($row = OIDplus::db()->fetch_array($res)) { |
||
195 | $list_of_oids[] = $row['id']; |
||
196 | } |
||
197 | |||
198 | $message = file_get_contents(__DIR__ . '/../invite_msg.tpl'); |
||
199 | |||
200 | // Resolve stuff |
||
201 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::system_url(), $message); |
||
202 | $message = str_replace('{{OID_LIST}}', implode("\n", $list_of_oids), $message); |
||
203 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
||
204 | $message = str_replace('{{PARTY}}', OIDplus::authUtils()::isAdminLoggedIn() ? 'the system administrator' : 'a superior Registration Authority', $message); |
||
205 | |||
206 | // {{ACTIVATE_URL}} will be resolved in action.php |
||
207 | |||
208 | return $message; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | OIDplus::registerPagePlugin(new OIDplusPagePublicInvite()); |