Subversion Repositories oidplus

Rev

Rev 328 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 328 Rev 360
Line 22... Line 22...
22
        private static function getFreeRootOid($with_ns) {
22
        private static function getFreeRootOid($with_ns) {
23
                return ($with_ns ? 'oid:' : '').OIDplus::config()->getValue('freeoid_root_oid');
23
                return ($with_ns ? 'oid:' : '').OIDplus::config()->getValue('freeoid_root_oid');
24
        }
24
        }
25
 
25
 
26
        public function action($actionID, $params) {
26
        public function action($actionID, $params) {
27
                if (empty(self::getFreeRootOid(false))) throw new OIDplusException("FreeOID service not available. Please ask your administrator.");
27
                if (empty(self::getFreeRootOid(false))) throw new OIDplusException(_L('FreeOID service not available. Please ask your administrator.'));
28
 
28
 
29
                if ($actionID == 'request_freeoid') {
29
                if ($actionID == 'request_freeoid') {
30
                        $email = $params['email'];
30
                        $email = $params['email'];
31
 
31
 
32
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
32
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
33
                        if ($res->num_rows() > 0) {
33
                        if ($res->num_rows() > 0) {
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
34
                                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
35
                        }
35
                        }
36
 
36
 
37
                        if (!OIDplus::mailUtils()->validMailAddress($email)) {
37
                        if (!OIDplus::mailUtils()->validMailAddress($email)) {
38
                                throw new OIDplusException('Invalid email address');
38
                                throw new OIDplusException(_L('Invalid email address'));
39
                        }
39
                        }
40
 
40
 
41
                        if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false)) {
41
                        if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false)) {
42
                                $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', '');
42
                                $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', '');
43
                                $response=$params["captcha"];
43
                                $response=$params["captcha"];
44
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
44
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
45
                                $captcha_success=json_decode($verify);
45
                                $captcha_success=json_decode($verify);
46
                                if ($captcha_success->success==false) {
46
                                if ($captcha_success->success==false) {
47
                                        throw new OIDplusException('Captcha wrong');
47
                                        throw new OIDplusException(_L('CAPTCHA not successfully verified'));
48
                                }
48
                                }
49
                        }
49
                        }
50
 
50
 
51
                        $root_oid = self::getFreeRootOid(false);
51
                        $root_oid = self::getFreeRootOid(false);
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'");
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'");
Line 75... Line 75...
75
 
75
 
76
                        $auth = $params['auth'];
76
                        $auth = $params['auth'];
77
                        $timestamp = $params['timestamp'];
77
                        $timestamp = $params['timestamp'];
78
 
78
 
79
                        if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
79
                        if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
80
                                throw new OIDplusException('Invalid auth key');
80
                                throw new OIDplusException(_L('Invalid auth key'));
81
                        }
81
                        }
82
 
82
 
83
                        if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) {
83
                        if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) {
84
                                throw new OIDplusException('Invitation expired!');
84
                                throw new OIDplusException(_L('Invitation expired!'));
85
                        }
85
                        }
86
 
86
 
87
                        if ($password1 !== $password2) {
87
                        if ($password1 !== $password2) {
88
                                throw new OIDplusException('Passwords are not equal');
88
                                throw new OIDplusException(_L('Passwords do not match'));
89
                        }
89
                        }
90
 
90
 
91
                        if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
91
                        if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
-
 
92
                                $minlen = 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'));
93
                                throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
93
                        }
94
                        }
94
 
95
 
95
                        if (empty($ra_name)) {
96
                        if (empty($ra_name)) {
96
                                throw new OIDplusException('Please enter your personal name or the name of your group.');
97
                                throw new OIDplusException(_L('Please enter your personal name or the name of your group.'));
97
                        }
98
                        }
98
 
99
 
99
                        // 1. step: Add the RA to the database
100
                        // 1. step: Add the RA to the database
100
 
101
 
101
                        $ra = new OIDplusRA($email);
102
                        $ra = new OIDplusRA($email);
Line 112... Line 113...
112
 
113
 
113
                        if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url;
114
                        if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url;
114
 
115
 
115
                        $description = ''; // '<p>'.htmlentities($ra_name).'</p>';
116
                        $description = ''; // '<p>'.htmlentities($ra_name).'</p>';
116
                        if (!empty($url)) {
117
                        if (!empty($url)) {
117
                                $description .= '<p>More information at <a href="'.htmlentities($url).'">'.htmlentities($url).'</a></p>';
118
                                $description .= '<p>'._L('More information at %1','<a href="'.htmlentities($url).'">'.htmlentities($url).'</a>').'</p>';
118
                        }
119
                        }
119
 
120
 
120
                        if (empty($title)) $title = $ra_name;
121
                        if (empty($title)) $title = $ra_name;
121
 
122
 
122
                        try {
123
                        try {
123
                                if ('oid:'.$new_oid > OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')) {
124
                                if ('oid:'.$new_oid > OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')) {
-
 
125
                                        $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:');
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:')).")");
126
                                        throw new OIDplusException(_L('The resulting OID %1 is too long (max allowed length: %2)',$new_oid,$maxlen));
125
                                }
127
                                }
126
 
128
 
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));
129
                                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));
128
                        } catch (Exception $e) {
130
                        } catch (Exception $e) {
129
                                $ra->delete();
131
                                $ra->delete();
Line 154... Line 156...
154
                        $message = str_replace('{{NEW_OID}}', $new_oid, $message);
156
                        $message = str_replace('{{NEW_OID}}', $new_oid, $message);
155
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID allocated', $message, OIDplus::config()->getValue('global_cc'));
157
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID allocated', $message, OIDplus::config()->getValue('global_cc'));
156
 
158
 
157
                        return array("status" => 0);
159
                        return array("status" => 0);
158
                } else {
160
                } else {
159
                        throw new OIDplusException("Unknown action ID");
161
                        throw new OIDplusException(_L('Unknown action ID'));
160
                }
162
                }
161
        }
163
        }
162
 
164
 
163
        public function init($html=true) {
165
        public function init($html=true) {
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) {
166
                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) {
165
                        if (($value != '') && !oid_valid_dotnotation($value,false,false,1)) {
167
                        if (($value != '') && !oid_valid_dotnotation($value,false,false,1)) {
166
                                throw new OIDplusException("Please enter a valid OID in dot notation or nothing");
168
                                throw new OIDplusException(_L('Please enter a valid OID in dot notation or nothing'));
167
                        }
169
                        }
168
                });
170
                });
169
        }
171
        }
170
 
172
 
171
        public function gui($id, &$out, &$handled) {
173
        public function gui($id, &$out, &$handled) {
172
                if (empty(self::getFreeRootOid(false))) return;
174
                if (empty(self::getFreeRootOid(false))) return;
173
 
175
 
174
                if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid') {
176
                if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid') {
175
                        $handled = true;
177
                        $handled = true;
176
 
178
 
177
                        $out['title'] = 'Register a free OID';
179
                        $out['title'] = _L('Register a free OID');
178
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
180
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
179
 
181
 
180
                        $highest_id = $this->freeoid_max_id();
182
                        $highest_id = $this->freeoid_max_id();
181
 
183
 
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>';
184
                        $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>';
183
 
185
 
184
                        try {
186
                        try {
185
                                $out['text'] .= '
187
                                $out['text'] .= '
186
                                  <form id="freeOIDForm" onsubmit="return freeOIDFormOnSubmit();">
188
                                  <form id="freeOIDForm" onsubmit="return freeOIDFormOnSubmit();">
187
                                    E-Mail: <input type="text" id="email" value=""/><br><br>'.
189
                                    '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>'.
188
                                 (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) ?
190
                                 (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) ?
189
                                 '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'" }); </script>'.
191
                                 '<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>' : '').
192
                                 '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'"></div>' : '').
191
                                ' <br>
193
                                ' <br>
192
                                    <input type="submit" value="Request free OID">
194
                                    <input type="submit" value="'._L('Request free OID').'">
193
                                  </form>';
195
                                  </form>';
194
 
196
 
195
                                $obj = OIDplusOID::parse(self::getFreeRootOid(true));
197
                                $obj = OIDplusOID::parse(self::getFreeRootOid(true));
196
 
198
 
-
 
199
                                if (file_exists(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html')) {
-
 
200
                                        $tos = file_get_contents(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html');
-
 
201
                                } else {
197
                                $tos = file_get_contents(__DIR__ . '/tos.html');
202
                                        $tos = file_get_contents(__DIR__ . '/tos.html');
-
 
203
                                }
198
                                $tos = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $tos);
204
                                $tos = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $tos);
199
                                if ($obj) {
205
                                if ($obj) {
200
                                        $tos = str_replace('{{ROOT_OID}}', $obj->getDotNotation(), $tos);
206
                                        $tos = str_replace('{{ROOT_OID}}', $obj->getDotNotation(), $tos);
201
                                        $tos = str_replace('{{ROOT_OID_ASN1}}', $obj->getAsn1Notation(), $tos);
207
                                        $tos = str_replace('{{ROOT_OID_ASN1}}', $obj->getAsn1Notation(), $tos);
202
                                        $tos = str_replace('{{ROOT_OID_IRI}}', $obj->getIriNotation(), $tos);
208
                                        $tos = str_replace('{{ROOT_OID_IRI}}', $obj->getIriNotation(), $tos);
203
                                }
209
                                }
204
                                $out['text'] .= $tos;
210
                                $out['text'] .= $tos;
205
                        } catch (Exception $e) {
211
                        } catch (Exception $e) {
206
                                $out['text'] = "Error: ".$e->getMessage();
212
                                $out['text'] = _L('Error: %1',$e->getMessage());
207
                        }
213
                        }
208
                } else if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid.activate_freeoid') {
214
                } else if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid.activate_freeoid') {
209
                        $handled = true;
215
                        $handled = true;
210
 
216
 
211
                        $email = explode('$',$id)[1];
217
                        $email = explode('$',$id)[1];
212
                        $timestamp = explode('$',$id)[2];
218
                        $timestamp = explode('$',$id)[2];
213
                        $auth = explode('$',$id)[3];
219
                        $auth = explode('$',$id)[3];
214
 
220
 
215
                        $out['title'] = 'Activate Free OID';
221
                        $out['title'] = _L('Activate Free OID');
216
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
222
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
217
 
223
 
218
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
224
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
219
                        if ($res->num_rows() > 0) {
225
                        if ($res->num_rows() > 0) {
220
                                $out['icon'] = 'img/error_big.png';
226
                                $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
227
                                $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
222
                        } else {
228
                        } else {
223
                                if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
229
                                if (!OIDplus::authUtils()::validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
224
                                        $out['icon'] = 'img/error_big.png';
230
                                        $out['icon'] = 'img/error_big.png';
225
                                        $out['text'] = 'Invalid authorization. Is the URL OK?';
231
                                        $out['text'] = _L('Invalid authorization. Is the URL OK?');
226
                                } else {
232
                                } else {
227
                                        $out['text'] = '<p>eMail-Address: <b>'.$email.'</b></p>
233
                                        $out['text'] = '<p>'._L('eMail-Address').': <b>'.$email.'</b></p>
228
 
234
 
229
                                  <form id="activateFreeOIDForm" onsubmit="return activateFreeOIDFormOnSubmit();">
235
                                  <form id="activateFreeOIDForm" onsubmit="return activateFreeOIDFormOnSubmit();">
230
                                    <input type="hidden" id="email" value="'.htmlentities($email).'"/>
236
                                    <input type="hidden" id="email" value="'.htmlentities($email).'"/>
231
                                    <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
237
                                    <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
232
                                    <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
238
                                    <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
233
 
239
 
234
                                    Your personal name or the name of your group:<br><input type="text" id="ra_name" value=""/><br><br><!-- TODO: disable autocomplete -->
240
                                    '._L('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>
241
                                    '._L('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>
242
                                    '._L('URL for more information about your project(s) (optional)').':<br><input type="text" id="url" value=""/><br><br>
237
 
243
 
238
                                    <div><label class="padding_label">Password:</label><input type="password" id="password1" value=""/></div>
244
                                    <div><label class="padding_label">'._L('Password').':</label><input type="password" id="password1" value=""/></div>
239
                                    <div><label class="padding_label">Repeat:</label><input type="password" id="password2" value=""/></div>
245
                                    <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>
240
                                    <br><input type="submit" value="Register">
246
                                    <br><input type="submit" value="'._L('Register').'">
241
                                  </form>';
247
                                  </form>';
242
                                }
248
                                }
243
                        }
249
                        }
244
                }
250
                }
245
        }
251
        }
246
 
252
 
247
        public function publicSitemap(&$out) {
253
        public function publicSitemap(&$out) {
248
                if (empty(self::getFreeRootOid(false))) return;
254
                if (empty(self::getFreeRootOid(false))) return;
249
                $out[] = OIDplus::getSystemUrl().'?goto='.urlencode('oidplus:com.viathinksoft.freeoid');
255
                $out[] = 'oidplus:com.viathinksoft.freeoid';
250
        }
256
        }
251
 
257
 
252
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
258
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
253
                if (empty(self::getFreeRootOid(false))) return false;
259
                if (empty(self::getFreeRootOid(false))) return false;
254
 
260
 
Line 259... Line 265...
259
                }
265
                }
260
 
266
 
261
                $json[] = array(
267
                $json[] = array(
262
                        'id' => 'oidplus:com.viathinksoft.freeoid',
268
                        'id' => 'oidplus:com.viathinksoft.freeoid',
263
                        'icon' => $tree_icon,
269
                        'icon' => $tree_icon,
264
                        'text' => 'Register a free OID'
270
                        'text' => _L('Register a free OID')
265
                );
271
                );
266
 
272
 
267
                return true;
273
                return true;
268
        }
274
        }
269
 
275