Subversion Repositories oidplus

Rev

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