Rev 66 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
61 | 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 OIDplusPagePublicFreeOID extends OIDplusPagePlugin { |
||
21 | public function type() { |
||
22 | return 'public'; |
||
23 | } |
||
24 | |||
25 | public function priority() { |
||
26 | return 200; |
||
27 | } |
||
28 | |||
29 | private static function getFreeRootOid() { |
||
30 | return OIDplusOID::parse('oid:'.OIDplus::config()->getValue('freeoid_root_oid')); |
||
31 | } |
||
32 | |||
33 | public function action(&$handled) { |
||
34 | if ($_POST["action"] == "com.viathinksoft.freeoid.request_freeoid") { |
||
35 | $handled = true; |
||
36 | $email = $_POST['email']; |
||
37 | |||
38 | $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($email)."'"); |
||
39 | if (OIDplus::db()->num_rows($res) > 0) { |
||
40 | die('This email address already exists.'); // TODO: actually, the person might have something else (like a DOI) and want to have a FreeOID |
||
41 | } |
||
42 | |||
43 | if (!oiddb_valid_email($email)) { |
||
44 | die('Invalid email address'); |
||
45 | } |
||
46 | |||
47 | if (RECAPTCHA_ENABLED) { |
||
48 | $secret=RECAPTCHA_PRIVATE; |
||
49 | $response=$_POST["captcha"]; |
||
50 | $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}"); |
||
51 | $captcha_success=json_decode($verify); |
||
52 | if ($captcha_success->success==false) { |
||
53 | die('Captcha wrong'); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | $timestamp = time(); |
||
58 | $activate_url = OIDplus::system_url() . '?goto='.urlencode('oidplus:com.viathinksoft.freeoid.activate_freeoid$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp)); |
||
59 | |||
60 | $message = file_get_contents(__DIR__ . '/request_msg.tpl'); |
||
61 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::system_url(), $message); |
||
62 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->systemTitle(), $message); |
||
63 | $message = str_replace('{{ADMIN_EMAIL}}', OIDPLUS_ADMIN_EMAIL, $message); |
||
64 | $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message); |
||
65 | my_mail($email, OIDplus::config()->systemTitle().' - Free OID request', $message, 'daniel-marschall@viathinksoft.de'); |
||
66 | |||
67 | die("OK"); |
||
68 | } |
||
69 | |||
70 | if ($_POST["action"] == "com.viathinksoft.freeoid.activate_freeoid") { |
||
71 | $handled = true; |
||
72 | |||
73 | $password1 = $_POST['password1']; |
||
74 | $password2 = $_POST['password2']; |
||
75 | $email = $_POST['email']; |
||
76 | |||
77 | $ra_name = $_POST['ra_name']; |
||
78 | $url = $_POST['url']; |
||
79 | $title = $_POST['title']; |
||
80 | |||
81 | $auth = $_POST['auth']; |
||
82 | $timestamp = $_POST['timestamp']; |
||
83 | |||
84 | if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) { |
||
85 | die('Invalid auth key'); |
||
86 | } |
||
87 | |||
88 | if ((OIDplus::config()->maxInviteTime() > 0) && (time()-$timestamp > OIDplus::config()->maxInviteTime())) { |
||
89 | die('Invitation expired!'); |
||
90 | } |
||
91 | |||
92 | if ($password1 !== $password2) { |
||
93 | die('Passwords are not equal'); |
||
94 | } |
||
95 | |||
96 | if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) { |
||
97 | die('Password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength()); |
||
98 | } |
||
99 | |||
100 | if (empty($ra_name)) { |
||
101 | die('Please enter your personal name or the name of your group.'); |
||
102 | } |
||
103 | |||
104 | // 1. step: Add the RA to the database |
||
105 | |||
106 | $ra = new OIDplusRA($email); |
||
107 | $ra->register_ra($password1); |
||
108 | $ra->setRaName($ra_name); |
||
109 | |||
110 | // 2. step: Add the new OID to the database |
||
111 | |||
112 | $new_oid = OIDplus::config()->getValue('freeoid_root_oid').'.'.($this->freeoid_max_id()+1); |
||
113 | |||
114 | if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url; |
||
115 | |||
116 | $description = '<p>'.htmlentities($ra_name).'</p>'; |
||
117 | if (!empty($url)) { |
||
118 | $description .= '<p>More information at <a href="'.htmlentities($url).'">'.htmlentities($url).'</a></p>'; |
||
119 | } |
||
120 | |||
121 | if (empty($title)) $title = $ra_name; |
||
122 | |||
123 | if (!OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."objects (id, ra_email, parent, title, description, confidential, created) values ('".OIDplus::db()->real_escape_string('oid:'.$new_oid)."', '".OIDplus::db()->real_escape_string($email)."', '".OIDplus::db()->real_escape_string('oid:'.OIDplus::config()->getValue('freeoid_root_oid'))."', '".OIDplus::db()->real_escape_string($title)."', '".OIDplus::db()->real_escape_string($description)."', 0, now())")) { |
||
124 | $ra->delete(); |
||
125 | die(OIDplus::db()->error()); |
||
126 | } |
||
127 | |||
128 | // Send delegation report email to admin |
||
129 | |||
130 | $message = "OID delegation report\n"; |
||
131 | $message .= "\n"; |
||
132 | $message .= "OID: ".$new_oid."\n";; |
||
133 | $message .= "\n"; |
||
134 | $message .= "RA Name: $ra_name\n"; |
||
135 | $message .= "RA eMail: $email\n"; |
||
136 | $message .= "URL for more information: $url\n"; |
||
137 | $message .= "OID Name: $title\n"; |
||
138 | $message .= "\n"; |
||
139 | $message .= "More details: ".OIDplus::system_url()."?goto=oid:$new_oid\n"; |
||
140 | |||
66 | daniel-mar | 141 | my_mail($email, OIDplus::config()->systemTitle()." - OID $new_oid registered", $message, OIDplus::config()->globalCC()); |
61 | daniel-mar | 142 | |
143 | // Send delegation information to user |
||
144 | |||
145 | $message = file_get_contents(__DIR__ . '/allocated_msg.tpl'); |
||
146 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::system_url(), $message); |
||
147 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->systemTitle(), $message); |
||
148 | $message = str_replace('{{ADMIN_EMAIL}}', OIDPLUS_ADMIN_EMAIL, $message); |
||
149 | $message = str_replace('{{NEW_OID}}', $new_oid, $message); |
||
150 | my_mail($email, OIDplus::config()->systemTitle().' - Free OID allocated', $message, 'daniel-marschall@viathinksoft.de'); |
||
151 | |||
152 | die('OK'); |
||
153 | } |
||
154 | } |
||
155 | |||
75 | daniel-mar | 156 | public function init($html=true) { |
157 | OIDplus::config()->prepareConfigKey('freeoid_root_oid', 'Root-OID of free OID service', '', 0, 1); |
||
61 | daniel-mar | 158 | } |
159 | |||
160 | public function cfgSetValue($name, $value) { |
||
161 | if ($name == 'freeoid_root_oid') { |
||
162 | if (($value != '') && !oid_valid_dotnotation($value,false,false,1)) { |
||
163 | throw new Exception("Please enter a valid OID in dot notation or nothing"); |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | public function gui($id, &$out, &$handled) { |
||
169 | if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid') { |
||
170 | $handled = true; |
||
171 | |||
172 | $out['title'] = 'Register a free OID'; |
||
173 | $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/publicPages/'.basename(__DIR__).'/icon_big.png' : ''; |
||
174 | |||
175 | $highest_id = $this->freeoid_max_id(); |
||
176 | |||
177 | $out['text'] .= '<p>Currently <a href="?goto=oid:'.OIDplus::config()->getValue('freeoid_root_oid').'">'.$highest_id.' free OIDs have been</a> registered. Please enter your email below to receive a free OID.</p>'; |
||
178 | |||
179 | try { |
||
180 | $out['text'] .= ' |
||
181 | <form id="freeOIDForm" onsubmit="return freeOIDFormOnSubmit();"> |
||
182 | E-Mail: <input type="text" id="email" value=""/><br><br>'. |
||
183 | (RECAPTCHA_ENABLED ? '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.RECAPTCHA_PUBLIC.'" }); </script>'. |
||
184 | '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.RECAPTCHA_PUBLIC.'"></div>' : ''). |
||
185 | ' <br> |
||
186 | <input type="submit" value="Request free OID"> |
||
187 | </form>'; |
||
188 | |||
189 | $tos = file_get_contents(__DIR__ . '/tos.html'); |
||
190 | $tos = str_replace('{{ADMIN_EMAIL}}', OIDPLUS_ADMIN_EMAIL, $tos); |
||
191 | $tos = str_replace('{{ROOT_OID}}', OIDplus::config()->getValue('freeoid_root_oid'), $tos); |
||
192 | $tos = str_replace('{{ROOT_OID_ASN1}}', self::getFreeRootOid()->getAsn1Notation(), $tos); |
||
193 | $tos = str_replace('{{ROOT_OID_IRI}}', self::getFreeRootOid()->getIriNotation(), $tos); |
||
194 | $out['text'] .= $tos; |
||
195 | } catch (Exception $e) { |
||
196 | $out['text'] = "Error: ".$e->getMessage(); |
||
197 | } |
||
198 | } else if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid.activate_freeoid') { |
||
199 | $handled = true; |
||
200 | |||
201 | $email = explode('$',$id)[1]; |
||
202 | $timestamp = explode('$',$id)[2]; |
||
203 | $auth = explode('$',$id)[3]; |
||
204 | |||
205 | $out['title'] = 'Activate Free OID'; |
||
206 | $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/publicPages/'.basename(__DIR__).'/icon_big.png' : ''; |
||
207 | |||
208 | $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($email)."'"); |
||
209 | if (OIDplus::db()->num_rows($res) > 0) { |
||
210 | $out['icon'] = 'img/error_big.png'; |
||
211 | $out['text'] = 'This RA is already registered.'; // TODO: actually, the person might have something else (like a DOI) and want to have a FreeOID |
||
212 | } else { |
||
213 | if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) { |
||
214 | $out['icon'] = 'img/error_big.png'; |
||
215 | $out['text'] = 'Invalid authorization. Is the URL OK?'; |
||
216 | } else { |
||
217 | $out['text'] = '<p>eMail-Address: <b>'.$email.'</b></p> |
||
218 | |||
219 | <form id="activateFreeOIDForm" onsubmit="return activateFreeOIDFormOnSubmit();"> |
||
220 | <input type="hidden" id="email" value="'.htmlentities($email).'"/> |
||
221 | <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/> |
||
222 | <input type="hidden" id="auth" value="'.htmlentities($auth).'"/> |
||
223 | |||
224 | Your personal name or the name of your group:<br><input type="text" id="ra_name" value=""/><br><br><!-- TODO: disable autocomplete --> |
||
225 | Title of your OID (usually equal to your name, optional):<br><input type="text" id="title" value=""/><br><br> |
||
226 | URL for more information about your project(s) (optional):<br><input type="text" id="url" value=""/><br><br> |
||
227 | |||
228 | <label class="padding_label">Password:</label><input type="password" id="password1" value=""/><br><br> |
||
229 | <label class="padding_label">Again:</label><input type="password" id="password2" value=""/><br><br> |
||
230 | <input type="submit" value="Register"> |
||
231 | </form>'; |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | public function tree(&$json, $ra_email=null) { |
||
238 | if (file_exists(__DIR__.'/treeicon.png')) { |
||
239 | $tree_icon = 'plugins/publicPages/'.basename(__DIR__).'/treeicon.png'; |
||
240 | } else { |
||
241 | $tree_icon = null; // default icon (folder) |
||
242 | } |
||
243 | |||
244 | $json[] = array( |
||
245 | 'id' => 'oidplus:com.viathinksoft.freeoid', |
||
246 | 'icon' => $tree_icon, |
||
247 | 'text' => 'Register a free OID' |
||
248 | ); |
||
249 | } |
||
250 | |||
251 | # --- |
||
252 | |||
253 | protected function freeoid_max_id() { |
||
254 | $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like '".OIDplus::db()->real_escape_string('oid:'.OIDplus::config()->getValue('freeoid_root_oid').'.%')."' order by ".OIDplus::db()->natOrder('id')); |
||
255 | $highest_id = 0; |
||
256 | while ($row = OIDplus::db()->fetch_array($res)) { |
||
257 | $arc = substr_count(OIDplus::config()->getValue('freeoid_root_oid'), '.')+1; |
||
258 | $highest_id = explode('.',$row['id'])[$arc]; |
||
259 | } |
||
260 | return $highest_id; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | if (OIDplus::config()->getValue('freeoid_root_oid') != '') { |
||
265 | OIDplus::registerPagePlugin(new OIDplusPagePublicFreeOID()); |
||
266 | } |