Subversion Repositories oidplus

Rev

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