Subversion Repositories oidplus

Rev

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