Subversion Repositories oidplus

Rev

Rev 224 | 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
 
40
        private static function getFreeRootOid() {
41
                return OIDplusOID::parse('oid:'.OIDplus::config()->getValue('freeoid_root_oid'));
42
        }
43
 
44
        public function action(&$handled) {
166 daniel-mar 45
                if (empty(OIDplus::config()->getValue('freeoid_root_oid'))) return;
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));
61 daniel-mar 52
                        if (OIDplus::db()->num_rows($res) > 0) {
107 daniel-mar 53
                                die(json_encode(array("error" => '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)) {
107 daniel-mar 57
                                die(json_encode(array("error" => '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) {
107 daniel-mar 66
                                        die(json_encode(array("error" => 'Captcha wrong')));
61 daniel-mar 67
                                }
68
                        }
69
 
115 daniel-mar 70
                        $root_oid = OIDplus::config()->getValue('freeoid_root_oid');
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)) {
107 daniel-mar 101
                                die(json_encode(array("error" => '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'))) {
107 daniel-mar 105
                                die(json_encode(array("error" => 'Invitation expired!')));
61 daniel-mar 106
                        }
107
 
108
                        if ($password1 !== $password2) {
107 daniel-mar 109
                                die(json_encode(array("error" => 'Passwords are not equal')));
61 daniel-mar 110
                        }
111
 
112
                        if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) {
107 daniel-mar 113
                                die(json_encode(array("error" => 'Password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength())));
61 daniel-mar 114
                        }
115
 
116
                        if (empty($ra_name)) {
107 daniel-mar 117
                                die(json_encode(array("error" => '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
 
115 daniel-mar 128
                        $root_oid = OIDplus::config()->getValue('freeoid_root_oid');
129
                        $new_oid = $root_oid.'.'.($this->freeoid_max_id()+1);
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
 
150 daniel-mar 143
                        if (!OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."objects (id, ra_email, parent, title, description, confidential, created) values (?, ?, ?, ?, ?, 0, now())", array('oid:'.$new_oid, $email, 'oid:'.OIDplus::config()->getValue('freeoid_root_oid'), $title, $description))) {
61 daniel-mar 144
                                $ra->delete();
107 daniel-mar 145
                                die(json_encode(array("error" => OIDplus::db()->error())));
61 daniel-mar 146
                        }
147
 
148
                        // Send delegation report email to admin
149
 
150
                        $message  = "OID delegation report\n";
151
                        $message .= "\n";
152
                        $message .= "OID: ".$new_oid."\n";;
153
                        $message .= "\n";
154
                        $message .= "RA Name: $ra_name\n";
155
                        $message .= "RA eMail: $email\n";
156
                        $message .= "URL for more information: $url\n";
157
                        $message .= "OID Name: $title\n";
158
                        $message .= "\n";
227 daniel-mar 159
                        $message .= "More details: ".OIDplus::getSystemUrl()."?goto=oid:$new_oid\n";
61 daniel-mar 160
 
66 daniel-mar 161
                        my_mail($email, OIDplus::config()->systemTitle()." - OID $new_oid registered", $message, OIDplus::config()->globalCC());
61 daniel-mar 162
 
163
                        // Send delegation information to user
164
 
165
                        $message = file_get_contents(__DIR__ . '/allocated_msg.tpl');
227 daniel-mar 166
                        $message = str_replace('{{SYSTEM_URL}}', OIDplus::getSystemUrl(), $message);
61 daniel-mar 167
                        $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->systemTitle(), $message);
76 daniel-mar 168
                        $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
61 daniel-mar 169
                        $message = str_replace('{{NEW_OID}}', $new_oid, $message);
115 daniel-mar 170
                        my_mail($email, OIDplus::config()->systemTitle().' - Free OID allocated', $message, OIDplus::config()->globalCC());
61 daniel-mar 171
 
107 daniel-mar 172
                        echo json_encode(array("status" => 0));
61 daniel-mar 173
                }
174
        }
175
 
75 daniel-mar 176
        public function init($html=true) {
166 daniel-mar 177
                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 178
        }
179
 
180
        public function cfgSetValue($name, $value) {
181
                if ($name == 'freeoid_root_oid') {
182
                        if (($value != '') && !oid_valid_dotnotation($value,false,false,1)) {
183
                                throw new Exception("Please enter a valid OID in dot notation or nothing");
184
                        }
185
                }
186
        }
187
 
188
        public function gui($id, &$out, &$handled) {
166 daniel-mar 189
                if (empty(OIDplus::config()->getValue('freeoid_root_oid'))) return;
190
 
61 daniel-mar 191
                if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid') {
192
                        $handled = true;
193
 
194
                        $out['title'] = 'Register a free OID';
148 daniel-mar 195
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
61 daniel-mar 196
 
197
                        $highest_id = $this->freeoid_max_id();
198
 
107 daniel-mar 199
                        $out['text'] .= '<p>Currently <a '.oidplus_link('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>';
61 daniel-mar 200
 
201
                        try {
202
                                $out['text'] .= '
203
                                  <form id="freeOIDForm" onsubmit="return freeOIDFormOnSubmit();">
204
                                    E-Mail: <input type="text" id="email" value=""/><br><br>'.
205
                                 (RECAPTCHA_ENABLED ? '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.RECAPTCHA_PUBLIC.'" }); </script>'.
206
                                                   '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.RECAPTCHA_PUBLIC.'"></div>' : '').
207
                                ' <br>
208
                                    <input type="submit" value="Request free OID">
209
                                  </form>';
210
 
211
                                $tos = file_get_contents(__DIR__ . '/tos.html');
76 daniel-mar 212
                                $tos = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $tos);
61 daniel-mar 213
                                $tos = str_replace('{{ROOT_OID}}', OIDplus::config()->getValue('freeoid_root_oid'), $tos);
214
                                $tos = str_replace('{{ROOT_OID_ASN1}}', self::getFreeRootOid()->getAsn1Notation(), $tos);
215
                                $tos = str_replace('{{ROOT_OID_IRI}}', self::getFreeRootOid()->getIriNotation(), $tos);
216
                                $out['text'] .= $tos;
217
                        } catch (Exception $e) {
218
                                $out['text'] = "Error: ".$e->getMessage();
219
                        }
220
                } else if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid.activate_freeoid') {
221
                        $handled = true;
222
 
223
                        $email = explode('$',$id)[1];
224
                        $timestamp = explode('$',$id)[2];
225
                        $auth = explode('$',$id)[3];
226
 
227
                        $out['title'] = 'Activate Free OID';
148 daniel-mar 228
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
61 daniel-mar 229
 
150 daniel-mar 230
                        $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($email));
61 daniel-mar 231
                        if (OIDplus::db()->num_rows($res) > 0) {
232
                                $out['icon'] = 'img/error_big.png';
233
                                $out['text'] = 'This RA is already registered.'; // TODO: actually, the person might have something else (like a DOI) and want to have a FreeOID
234
                        } else {
235
                                if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
236
                                        $out['icon'] = 'img/error_big.png';
237
                                        $out['text'] = 'Invalid authorization. Is the URL OK?';
238
                                } else {
239
                                        $out['text'] = '<p>eMail-Address: <b>'.$email.'</b></p>
240
 
241
                                  <form id="activateFreeOIDForm" onsubmit="return activateFreeOIDFormOnSubmit();">
242
                                    <input type="hidden" id="email" value="'.htmlentities($email).'"/>
243
                                    <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
244
                                    <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
245
 
246
                                    Your personal name or the name of your group:<br><input type="text" id="ra_name" value=""/><br><br><!-- TODO: disable autocomplete -->
247
                                    Title of your OID (usually equal to your name, optional):<br><input type="text" id="title" value=""/><br><br>
248
                                    URL for more information about your project(s) (optional):<br><input type="text" id="url" value=""/><br><br>
249
 
152 daniel-mar 250
                                    <div><label class="padding_label">Password:</label><input type="password" id="password1" value=""/></div>
153 daniel-mar 251
                                    <div><label class="padding_label">Repeat:</label><input type="password" id="password2" value=""/></div>
152 daniel-mar 252
                                    <br><input type="submit" value="Register">
61 daniel-mar 253
                                  </form>';
254
                                }
255
                        }
256
                }
257
        }
258
 
106 daniel-mar 259
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
166 daniel-mar 260
                if (empty(OIDplus::config()->getValue('freeoid_root_oid'))) return false;
261
 
61 daniel-mar 262
                if (file_exists(__DIR__.'/treeicon.png')) {
148 daniel-mar 263
                        $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
61 daniel-mar 264
                } else {
265
                        $tree_icon = null; // default icon (folder)
266
                }
267
 
268
                $json[] = array(
269
                        'id' => 'oidplus:com.viathinksoft.freeoid',
270
                        'icon' => $tree_icon,
271
                        'text' => 'Register a free OID'
272
                );
104 daniel-mar 273
 
274
                return true;
61 daniel-mar 275
        }
276
 
277
        # ---
278
 
279
        protected function freeoid_max_id() {
150 daniel-mar 280
                $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where id like ? order by ".OIDplus::db()->natOrder('id'), array('oid:'.OIDplus::config()->getValue('freeoid_root_oid').'.%'));
61 daniel-mar 281
                $highest_id = 0;
282
                while ($row = OIDplus::db()->fetch_array($res)) {
283
                        $arc = substr_count(OIDplus::config()->getValue('freeoid_root_oid'), '.')+1;
284
                        $highest_id = explode('.',$row['id'])[$arc];
285
                }
286
                return $highest_id;
287
        }
108 daniel-mar 288
 
289
        public function tree_search($request) {
290
                return false;
291
        }
61 daniel-mar 292
}