Subversion Repositories oidplus

Rev

Rev 496 | 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
511 daniel-mar 5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
61 daniel-mar 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
 
511 daniel-mar 20
if (!defined('INSIDE_OIDPLUS')) die();
21
 
256 daniel-mar 22
class OIDplusPagePublicFreeOID extends OIDplusPagePluginPublic {
61 daniel-mar 23
 
247 daniel-mar 24
        private static function getFreeRootOid($with_ns) {
313 daniel-mar 25
                return ($with_ns ? 'oid:' : '').OIDplus::config()->getValue('freeoid_root_oid');
61 daniel-mar 26
        }
27
 
321 daniel-mar 28
        public function action($actionID, $params) {
360 daniel-mar 29
                if (empty(self::getFreeRootOid(false))) throw new OIDplusException(_L('FreeOID service not available. Please ask your administrator.'));
166 daniel-mar 30
 
321 daniel-mar 31
                if ($actionID == 'request_freeoid') {
32
                        $email = $params['email'];
61 daniel-mar 33
 
261 daniel-mar 34
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
236 daniel-mar 35
                        if ($res->num_rows() > 0) {
360 daniel-mar 36
                                throw new OIDplusException(_L('This email address already exists.')); // TODO: actually, the person might have something else (like a DOI) and want to have a FreeOID
61 daniel-mar 37
                        }
38
 
250 daniel-mar 39
                        if (!OIDplus::mailUtils()->validMailAddress($email)) {
360 daniel-mar 40
                                throw new OIDplusException(_L('Invalid email address'));
61 daniel-mar 41
                        }
42
 
261 daniel-mar 43
                        if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false)) {
44
                                $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', '');
321 daniel-mar 45
                                $response=$params["captcha"];
61 daniel-mar 46
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
47
                                $captcha_success=json_decode($verify);
48
                                if ($captcha_success->success==false) {
360 daniel-mar 49
                                        throw new OIDplusException(_L('CAPTCHA not successfully verified'));
61 daniel-mar 50
                                }
51
                        }
52
 
247 daniel-mar 53
                        $root_oid = self::getFreeRootOid(false);
288 daniel-mar 54
                        OIDplus::logger()->log("[INFO]OID(oid:$root_oid)+RA($email)!", "Requested a free OID for email '$email' to be placed into root '$root_oid'");
115 daniel-mar 55
 
61 daniel-mar 56
                        $timestamp = time();
496 daniel-mar 57
                        $activate_url = OIDplus::webpath(null,false) . '?goto='.urlencode('oidplus:com.viathinksoft.freeoid.activate_freeoid$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp));
61 daniel-mar 58
 
59
                        $message = file_get_contents(__DIR__ . '/request_msg.tpl');
496 daniel-mar 60
                        $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,false), $message);
257 daniel-mar 61
                        $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
76 daniel-mar 62
                        $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
61 daniel-mar 63
                        $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
257 daniel-mar 64
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID request', $message, OIDplus::config()->getValue('global_cc'));
61 daniel-mar 65
 
328 daniel-mar 66
                        return array("status" => 0);
61 daniel-mar 67
 
321 daniel-mar 68
                } else if ($actionID == 'activate_freeoid') {
61 daniel-mar 69
 
321 daniel-mar 70
                        $password1 = $params['password1'];
71
                        $password2 = $params['password2'];
72
                        $email = $params['email'];
61 daniel-mar 73
 
321 daniel-mar 74
                        $ra_name = $params['ra_name'];
75
                        $url = $params['url'];
76
                        $title = $params['title'];
61 daniel-mar 77
 
321 daniel-mar 78
                        $auth = $params['auth'];
79
                        $timestamp = $params['timestamp'];
61 daniel-mar 80
 
81
                        if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
360 daniel-mar 82
                                throw new OIDplusException(_L('Invalid auth key'));
61 daniel-mar 83
                        }
84
 
104 daniel-mar 85
                        if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) {
360 daniel-mar 86
                                throw new OIDplusException(_L('Invitation expired!'));
61 daniel-mar 87
                        }
88
 
89
                        if ($password1 !== $password2) {
360 daniel-mar 90
                                throw new OIDplusException(_L('Passwords do not match'));
61 daniel-mar 91
                        }
92
 
257 daniel-mar 93
                        if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
360 daniel-mar 94
                                $minlen = OIDplus::config()->getValue('ra_min_password_length');
95
                                throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
61 daniel-mar 96
                        }
97
 
98
                        if (empty($ra_name)) {
360 daniel-mar 99
                                throw new OIDplusException(_L('Please enter your personal name or the name of your group.'));
61 daniel-mar 100
                        }
134 daniel-mar 101
 
61 daniel-mar 102
                        // 1. step: Add the RA to the database
103
 
104
                        $ra = new OIDplusRA($email);
105
                        $ra->register_ra($password1);
106
                        $ra->setRaName($ra_name);
107
 
108
                        // 2. step: Add the new OID to the database
109
 
247 daniel-mar 110
                        $root_oid = self::getFreeRootOid(false);
489 daniel-mar 111
                        $new_oid = OIDplusOid::parse('oid:'.$root_oid)->appendArcs($this->freeoid_max_id()+1)->nodeId(false);
61 daniel-mar 112
 
288 daniel-mar 113
                        OIDplus::logger()->log("[INFO]OID(oid:$root_oid)+OIDRA(oid:$root_oid)!", "Child OID '$new_oid' added automatically by '$email' (RA Name: '$ra_name')");
114
                        OIDplus::logger()->log("[INFO]OID(oid:$new_oid)+[OK]RA($email)!",            "Free OID '$new_oid' activated (RA Name: '$ra_name')");
115 daniel-mar 115
 
61 daniel-mar 116
                        if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url;
117
 
134 daniel-mar 118
                        $description = ''; // '<p>'.htmlentities($ra_name).'</p>';
61 daniel-mar 119
                        if (!empty($url)) {
360 daniel-mar 120
                                $description .= '<p>'._L('More information at %1','<a href="'.htmlentities($url).'">'.htmlentities($url).'</a>').'</p>';
61 daniel-mar 121
                        }
122
 
123
                        if (empty($title)) $title = $ra_name;
124
 
237 daniel-mar 125
                        try {
261 daniel-mar 126
                                if ('oid:'.$new_oid > OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')) {
360 daniel-mar 127
                                        $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:');
128
                                        throw new OIDplusException(_L('The resulting OID %1 is too long (max allowed length: %2)',$new_oid,$maxlen));
247 daniel-mar 129
                                }
277 daniel-mar 130
 
264 daniel-mar 131
                                OIDplus::db()->query("insert into ###objects (id, ra_email, parent, title, description, confidential, created) values (?, ?, ?, ?, ?, ?, ".OIDplus::db()->sqlDate().")", array('oid:'.$new_oid, $email, self::getFreeRootOid(true), $title, $description, false));
237 daniel-mar 132
                        } catch (Exception $e) {
61 daniel-mar 133
                                $ra->delete();
237 daniel-mar 134
                                throw $e;
61 daniel-mar 135
                        }
136
 
137
                        // Send delegation report email to admin
138
 
139
                        $message  = "OID delegation report\n";
140
                        $message .= "\n";
141
                        $message .= "OID: ".$new_oid."\n";;
142
                        $message .= "\n";
143
                        $message .= "RA Name: $ra_name\n";
144
                        $message .= "RA eMail: $email\n";
145
                        $message .= "URL for more information: $url\n";
146
                        $message .= "OID Name: $title\n";
147
                        $message .= "\n";
496 daniel-mar 148
                        $message .= "More details: ".OIDplus::webpath(null,false)."?goto=oid:$new_oid\n";
61 daniel-mar 149
 
257 daniel-mar 150
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title')." - OID $new_oid registered", $message, OIDplus::config()->getValue('global_cc'));
61 daniel-mar 151
 
152
                        // Send delegation information to user
153
 
154
                        $message = file_get_contents(__DIR__ . '/allocated_msg.tpl');
496 daniel-mar 155
                        $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,false), $message);
257 daniel-mar 156
                        $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
76 daniel-mar 157
                        $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
61 daniel-mar 158
                        $message = str_replace('{{NEW_OID}}', $new_oid, $message);
257 daniel-mar 159
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID allocated', $message, OIDplus::config()->getValue('global_cc'));
61 daniel-mar 160
 
328 daniel-mar 161
                        return array("status" => 0);
321 daniel-mar 162
                } else {
360 daniel-mar 163
                        throw new OIDplusException(_L('Unknown action ID'));
61 daniel-mar 164
                }
165
        }
166
 
75 daniel-mar 167
        public function init($html=true) {
263 daniel-mar 168
                OIDplus::config()->prepareConfigKey('freeoid_root_oid', 'Root-OID of free OID service (a service where visitors can create their own OID using email verification)', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
61 daniel-mar 169
                        if (($value != '') && !oid_valid_dotnotation($value,false,false,1)) {
360 daniel-mar 170
                                throw new OIDplusException(_L('Please enter a valid OID in dot notation or nothing'));
61 daniel-mar 171
                        }
263 daniel-mar 172
                });
61 daniel-mar 173
        }
174
 
175
        public function gui($id, &$out, &$handled) {
247 daniel-mar 176
                if (empty(self::getFreeRootOid(false))) return;
166 daniel-mar 177
 
61 daniel-mar 178
                if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid') {
179
                        $handled = true;
180
 
360 daniel-mar 181
                        $out['title'] = _L('Register a free OID');
241 daniel-mar 182
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
61 daniel-mar 183
 
184
                        $highest_id = $this->freeoid_max_id();
185
 
360 daniel-mar 186
                        $out['text'] .= '<p>'._L('Currently <a %1>%2 free OIDs have been</a> registered. Please enter your email below to receive a free OID.',OIDplus::gui()->link(self::getFreeRootOid(true)),$highest_id).'</p>';
61 daniel-mar 187
 
188
                        try {
189
                                $out['text'] .= '
428 daniel-mar 190
                                  <form id="freeOIDForm" action="javascript:void(0);" onsubmit="return freeOIDFormOnSubmit();">
360 daniel-mar 191
                                    '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>'.
261 daniel-mar 192
                                 (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) ?
193
                                 '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'" }); </script>'.
194
                                 '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'"></div>' : '').
61 daniel-mar 195
                                ' <br>
360 daniel-mar 196
                                    <input type="submit" value="'._L('Request free OID').'">
61 daniel-mar 197
                                  </form>';
198
 
489 daniel-mar 199
                                $obj = OIDplusOid::parse(self::getFreeRootOid(true));
313 daniel-mar 200
 
360 daniel-mar 201
                                if (file_exists(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html')) {
202
                                        $tos = file_get_contents(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html');
203
                                } else {
204
                                        $tos = file_get_contents(__DIR__ . '/tos.html');
205
                                }
428 daniel-mar 206
 
386 daniel-mar 207
                                list($html, $js, $css) = extractHtmlContents($tos);
208
                                $tos = '';
209
                                if (!empty($js))  $tos .= "<script>\n$js\n</script>";
210
                                if (!empty($css)) $tos .= "<style>\n$css\n</style>";
211
                                $tos .= $html;
428 daniel-mar 212
 
76 daniel-mar 213
                                $tos = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $tos);
313 daniel-mar 214
                                if ($obj) {
215
                                        $tos = str_replace('{{ROOT_OID}}', $obj->getDotNotation(), $tos);
216
                                        $tos = str_replace('{{ROOT_OID_ASN1}}', $obj->getAsn1Notation(), $tos);
217
                                        $tos = str_replace('{{ROOT_OID_IRI}}', $obj->getIriNotation(), $tos);
218
                                }
61 daniel-mar 219
                                $out['text'] .= $tos;
220
                        } catch (Exception $e) {
360 daniel-mar 221
                                $out['text'] = _L('Error: %1',$e->getMessage());
61 daniel-mar 222
                        }
223
                } else if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid.activate_freeoid') {
224
                        $handled = true;
225
 
226
                        $email = explode('$',$id)[1];
227
                        $timestamp = explode('$',$id)[2];
228
                        $auth = explode('$',$id)[3];
229
 
360 daniel-mar 230
                        $out['title'] = _L('Activate Free OID');
241 daniel-mar 231
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
61 daniel-mar 232
 
261 daniel-mar 233
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
236 daniel-mar 234
                        if ($res->num_rows() > 0) {
61 daniel-mar 235
                                $out['icon'] = 'img/error_big.png';
360 daniel-mar 236
                                $out['text'] = _L('This RA is already registered.'); // TODO: actually, the person might have something else (like a DOI) and want to have a FreeOID
61 daniel-mar 237
                        } else {
238
                                if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
239
                                        $out['icon'] = 'img/error_big.png';
360 daniel-mar 240
                                        $out['text'] = _L('Invalid authorization. Is the URL OK?');
61 daniel-mar 241
                                } else {
360 daniel-mar 242
                                        $out['text'] = '<p>'._L('eMail-Address').': <b>'.$email.'</b></p>
61 daniel-mar 243
 
428 daniel-mar 244
                                  <form id="activateFreeOIDForm" action="javascript:void(0);" onsubmit="return activateFreeOIDFormOnSubmit();">
61 daniel-mar 245
                                    <input type="hidden" id="email" value="'.htmlentities($email).'"/>
246
                                    <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
247
                                    <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
248
 
360 daniel-mar 249
                                    '._L('Your personal name or the name of your group').':<br><input type="text" id="ra_name" value=""/><br><br><!-- TODO: disable autocomplete -->
250
                                    '._L('Title of your OID (usually equal to your name, optional)').':<br><input type="text" id="title" value=""/><br><br>
251
                                    '._L('URL for more information about your project(s) (optional)').':<br><input type="text" id="url" value=""/><br><br>
61 daniel-mar 252
 
360 daniel-mar 253
                                    <div><label class="padding_label">'._L('Password').':</label><input type="password" id="password1" value=""/></div>
254
                                    <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>
255
                                    <br><input type="submit" value="'._L('Register').'">
61 daniel-mar 256
                                  </form>';
257
                                }
258
                        }
259
                }
260
        }
261
 
282 daniel-mar 262
        public function publicSitemap(&$out) {
283 daniel-mar 263
                if (empty(self::getFreeRootOid(false))) return;
360 daniel-mar 264
                $out[] = 'oidplus:com.viathinksoft.freeoid';
282 daniel-mar 265
        }
266
 
106 daniel-mar 267
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
247 daniel-mar 268
                if (empty(self::getFreeRootOid(false))) return false;
166 daniel-mar 269
 
61 daniel-mar 270
                if (file_exists(__DIR__.'/treeicon.png')) {
241 daniel-mar 271
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
61 daniel-mar 272
                } else {
273
                        $tree_icon = null; // default icon (folder)
274
                }
275
 
276
                $json[] = array(
277
                        'id' => 'oidplus:com.viathinksoft.freeoid',
278
                        'icon' => $tree_icon,
360 daniel-mar 279
                        'text' => _L('Register a free OID')
61 daniel-mar 280
                );
104 daniel-mar 281
 
282
                return true;
61 daniel-mar 283
        }
284
 
285
        # ---
286
 
247 daniel-mar 287
        protected static function freeoid_max_id() {
261 daniel-mar 288
                $res = OIDplus::db()->query("select id from ###objects where id like ? order by ".OIDplus::db()->natOrder('id'), array(self::getFreeRootOid(true).'.%'));
61 daniel-mar 289
                $highest_id = 0;
236 daniel-mar 290
                while ($row = $res->fetch_array()) {
247 daniel-mar 291
                        $arc = substr_count(self::getFreeRootOid(false), '.')+1;
61 daniel-mar 292
                        $highest_id = explode('.',$row['id'])[$arc];
293
                }
294
                return $highest_id;
295
        }
108 daniel-mar 296
 
297
        public function tree_search($request) {
298
                return false;
299
        }
361 daniel-mar 300
}