Subversion Repositories oidplus

Rev

Rev 1140 | Rev 1148 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1140 Rev 1143
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * OIDplus 2.0
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
6
 *
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with 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
9
 * You may obtain a copy of the License at
10
 *
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
17
 * limitations under the License.
18
 */
18
 */
19
 
19
 
20
namespace ViaThinkSoft\OIDplus;
20
namespace ViaThinkSoft\OIDplus;
21
 
21
 
22
// phpcs:disable PSR1.Files.SideEffects
22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
24
// phpcs:enable PSR1.Files.SideEffects
25
 
25
 
26
class OIDplusPagePublicFreeOID extends OIDplusPagePluginPublic {
26
class OIDplusPagePublicFreeOID extends OIDplusPagePluginPublic {
27
 
27
 
28
        /**
28
        /**
29
         * @param bool $with_ns
29
         * @param bool $with_ns
30
         * @return string|null
30
         * @return string|null
31
         * @throws OIDplusException
31
         * @throws OIDplusException
32
         */
32
         */
33
        private static function getFreeRootOid(bool $with_ns)/*: ?string*/ {
33
        private static function getFreeRootOid(bool $with_ns)/*: ?string*/ {
34
                if (!in_array('ViaThinkSoft\OIDplus\OIDplusOid',OIDplus::getEnabledObjectTypes())) {
34
                if (!in_array('ViaThinkSoft\OIDplus\OIDplusOid',OIDplus::getEnabledObjectTypes())) {
35
                        return null;
35
                        return null;
36
                } else {
36
                } else {
37
                        $res = ($with_ns ? 'oid:' : '').OIDplus::config()->getValue('freeoid_root_oid');
37
                        $res = ($with_ns ? 'oid:' : '').OIDplus::config()->getValue('freeoid_root_oid');
38
                        return !empty($res) ? $res : null;
38
                        return !empty($res) ? $res : null;
39
                }
39
                }
40
        }
40
        }
41
 
41
 
42
        /**
42
        /**
43
         * @param string $email
43
         * @param string $email
44
         * @param bool $getId
44
         * @param bool $getId
45
         * @return bool|null
45
         * @return bool|null
46
         * @throws OIDplusException
46
         * @throws OIDplusException
47
         */
47
         */
48
        public static function alreadyHasFreeOid(string $email, bool $getId = false)/*: ?bool*/ {
48
        public static function alreadyHasFreeOid(string $email, bool $getId = false)/*: ?bool*/ {
49
                $res = OIDplus::db()->query("select id from ###objects where ra_email = ? and id like ? order by ".OIDplus::db()->natOrder('id'), array($email, self::getFreeRootOid(true).'.%'));
49
                $res = OIDplus::db()->query("select id from ###objects where ra_email = ? and id like ? order by ".OIDplus::db()->natOrder('id'), array($email, self::getFreeRootOid(true).'.%'));
50
                if ($row = $res->fetch_array()) {
50
                if ($row = $res->fetch_array()) {
51
                        return $getId ? $row['id'] : true;
51
                        return $getId ? $row['id'] : true;
52
                }
52
                }
53
                return $getId ? null : false;
53
                return $getId ? null : false;
54
        }
54
        }
55
 
55
 
56
        /**
56
        /**
57
         * @param string $actionID
57
         * @param string $actionID
58
         * @param array $params
58
         * @param array $params
59
         * @return array|int[]
59
         * @return array
60
         * @throws OIDplusException
60
         * @throws OIDplusException
61
         * @throws OIDplusMailException
61
         * @throws OIDplusMailException
62
         */
62
         */
63
        public function action(string $actionID, array $params): array {
63
        public function action(string $actionID, array $params): array {
64
                if (empty(self::getFreeRootOid(false))) throw new OIDplusException(_L('FreeOID service not available. Please ask your administrator.'));
64
                if (empty(self::getFreeRootOid(false))) throw new OIDplusException(_L('FreeOID service not available. Please ask your administrator.'));
65
 
65
 
66
                if ($actionID == 'request_freeoid') {
66
                if ($actionID == 'request_freeoid') {
67
                        _CheckParamExists($params, 'email');
67
                        _CheckParamExists($params, 'email');
68
                        $email = $params['email'];
68
                        $email = $params['email'];
69
 
69
 
70
                        if ($already_registered_oid = $this->alreadyHasFreeOid($email, true)) {
70
                        if ($already_registered_oid = $this->alreadyHasFreeOid($email, true)) {
71
                                throw new OIDplusException(_L('This email address already has a FreeOID registered (%1)', $already_registered_oid));
71
                                throw new OIDplusException(_L('This email address already has a FreeOID registered (%1)', $already_registered_oid));
72
                        }
72
                        }
73
 
73
 
74
                        if (!OIDplus::mailUtils()->validMailAddress($email)) {
74
                        if (!OIDplus::mailUtils()->validMailAddress($email)) {
75
                                throw new OIDplusException(_L('Invalid email address'));
75
                                throw new OIDplusException(_L('Invalid email address'));
76
                        }
76
                        }
77
 
77
 
78
                        OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha');
78
                        OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha');
79
 
79
 
80
                        $root_oid = self::getFreeRootOid(false);
80
                        $root_oid = self::getFreeRootOid(false);
81
                        OIDplus::logger()->log("[INFO]OID(oid:$root_oid)+RA($email)!", "Requested a free OID for email '$email' to be placed into root '$root_oid'");
81
                        OIDplus::logger()->log("[INFO]OID(oid:$root_oid)+RA($email)!", "Requested a free OID for email '$email' to be placed into root '$root_oid'");
82
 
82
 
83
                        $timestamp = time();
83
                        $timestamp = time();
84
                        $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:com.viathinksoft.freeoid.activate_freeoid$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp));
84
                        $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:com.viathinksoft.freeoid.activate_freeoid$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp));
85
 
85
 
86
                        $message = file_get_contents(__DIR__ . '/request_msg.tpl');
86
                        $message = file_get_contents(__DIR__ . '/request_msg.tpl');
87
                        $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
87
                        $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
88
                        $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
88
                        $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
89
                        $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
89
                        $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
90
                        $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
90
                        $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
91
 
91
 
92
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID request', $message);
92
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID request', $message);
93
 
93
 
94
                        return array("status" => 0);
94
                        return array("status" => 0);
95
 
95
 
96
                } else if ($actionID == 'activate_freeoid') {
96
                } else if ($actionID == 'activate_freeoid') {
97
                        _CheckParamExists($params, 'email');
97
                        _CheckParamExists($params, 'email');
98
                        _CheckParamExists($params, 'auth');
98
                        _CheckParamExists($params, 'auth');
99
                        _CheckParamExists($params, 'timestamp');
99
                        _CheckParamExists($params, 'timestamp');
100
 
100
 
101
                        $email = $params['email'];
101
                        $email = $params['email'];
102
                        $auth = $params['auth'];
102
                        $auth = $params['auth'];
103
                        $timestamp = $params['timestamp'];
103
                        $timestamp = $params['timestamp'];
104
 
104
 
105
                        if (!OIDplus::authUtils()->validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
105
                        if (!OIDplus::authUtils()->validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
106
                                throw new OIDplusException(_L('Invalid auth key'));
106
                                throw new OIDplusException(_L('Invalid auth key'));
107
                        }
107
                        }
108
 
108
 
109
                        if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) {
109
                        if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) {
110
                                throw new OIDplusException(_L('Invitation expired!'));
110
                                throw new OIDplusException(_L('Invitation expired!'));
111
                        }
111
                        }
112
 
112
 
113
                        // 1. step: Check entered data and add the RA to the database
113
                        // 1. step: Check entered data and add the RA to the database
114
 
114
 
115
                        $ra = new OIDplusRA($email);
115
                        $ra = new OIDplusRA($email);
116
                        if (!$ra->existing()) {
116
                        if (!$ra->existing()) {
117
                                _CheckParamExists($params, 'password1');
117
                                _CheckParamExists($params, 'password1');
118
                                _CheckParamExists($params, 'password2');
118
                                _CheckParamExists($params, 'password2');
119
                                _CheckParamExists($params, 'ra_name');
119
                                _CheckParamExists($params, 'ra_name');
120
 
120
 
121
                                $password1 = $params['password1'];
121
                                $password1 = $params['password1'];
122
                                $password2 = $params['password2'];
122
                                $password2 = $params['password2'];
123
                                $ra_name = $params['ra_name'];
123
                                $ra_name = $params['ra_name'];
124
 
124
 
125
                                if ($password1 !== $password2) {
125
                                if ($password1 !== $password2) {
126
                                        throw new OIDplusException(_L('Passwords do not match'));
126
                                        throw new OIDplusException(_L('Passwords do not match'));
127
                                }
127
                                }
128
 
128
 
129
                                if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
129
                                if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
130
                                        $minlen = OIDplus::config()->getValue('ra_min_password_length');
130
                                        $minlen = OIDplus::config()->getValue('ra_min_password_length');
131
                                        throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
131
                                        throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
132
                                }
132
                                }
133
 
133
 
134
                                if (empty($ra_name)) {
134
                                if (empty($ra_name)) {
135
                                        throw new OIDplusException(_L('Please enter your personal name or the name of your group.'));
135
                                        throw new OIDplusException(_L('Please enter your personal name or the name of your group.'));
136
                                }
136
                                }
137
 
137
 
138
                                $ra->register_ra($password1);
138
                                $ra->register_ra($password1);
139
                                $ra->setRaName($ra_name);
139
                                $ra->setRaName($ra_name);
140
                        } else {
140
                        } else {
141
                                // RA already exists (e.g. was logged in using Google OAuth)
141
                                // RA already exists (e.g. was logged in using Google OAuth)
142
                                $ra_name = $ra->raName();
142
                                $ra_name = $ra->raName();
143
                        }
143
                        }
144
 
144
 
145
                        // 2. step: Add the new OID to the database
145
                        // 2. step: Add the new OID to the database
146
 
146
 
147
                        $url = $params['url'] ?? '';
147
                        $url = $params['url'] ?? '';
148
                        $title = $params['title'] ?? '';
148
                        $title = $params['title'] ?? '';
149
 
149
 
150
                        $root_oid = self::getFreeRootOid(false);
150
                        $root_oid = self::getFreeRootOid(false);
151
                        $new_oid = OIDplusOid::parse('oid:'.$root_oid)->appendArcs($this->freeoid_max_id()+1)->nodeId(false);
151
                        $new_oid = OIDplusOid::parse('oid:'.$root_oid)->appendArcs($this->freeoid_max_id()+1)->nodeId(false);
152
 
152
 
153
                        OIDplus::logger()->log("[INFO]OID(oid:$root_oid)+OIDRA(oid:$root_oid)!", "Child OID '$new_oid' added automatically by '$email' (RA Name: '$ra_name')");
153
                        OIDplus::logger()->log("[INFO]OID(oid:$root_oid)+OIDRA(oid:$root_oid)!", "Child OID '$new_oid' added automatically by '$email' (RA Name: '$ra_name')");
154
                        OIDplus::logger()->log("[INFO]OID(oid:$new_oid)+[OK]RA($email)!",            "Free OID '$new_oid' activated (RA Name: '$ra_name')");
154
                        OIDplus::logger()->log("[INFO]OID(oid:$new_oid)+[OK]RA($email)!",            "Free OID '$new_oid' activated (RA Name: '$ra_name')");
155
 
155
 
156
                        if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url;
156
                        if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url;
157
 
157
 
158
                        $description = ''; // '<p>'.htmlentities($ra_name).'</p>';
158
                        $description = ''; // '<p>'.htmlentities($ra_name).'</p>';
159
                        if (!empty($url)) {
159
                        if (!empty($url)) {
160
                                $description .= '<p>'._L('More information at %1','<a href="'.htmlentities($url).'">'.htmlentities($url).'</a>').'</p>';
160
                                $description .= '<p>'._L('More information at %1','<a href="'.htmlentities($url).'">'.htmlentities($url).'</a>').'</p>';
161
                        }
161
                        }
162
 
162
 
163
                        if (empty($title)) $title = $ra_name;
163
                        if (empty($title)) $title = $ra_name;
164
 
164
 
165
                        try {
165
                        try {
166
                                $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:');
166
                                $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:');
167
                                if (strlen($new_oid) > $maxlen) {
167
                                if (strlen($new_oid) > $maxlen) {
168
                                        throw new OIDplusException(_L('The resulting OID %1 is too long (max allowed length: %2)',$new_oid,$maxlen));
168
                                        throw new OIDplusException(_L('The resulting OID %1 is too long (max allowed length: %2)',$new_oid,$maxlen));
169
                                }
169
                                }
170
 
170
 
171
                                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));
171
                                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));
172
                                OIDplusObject::resetObjectInformationCache();
172
                                OIDplusObject::resetObjectInformationCache();
173
                        } catch (\Exception $e) {
173
                        } catch (\Exception $e) {
174
                                $ra->delete();
174
                                $ra->delete();
175
                                throw $e;
175
                                throw $e;
176
                        }
176
                        }
177
 
177
 
178
                        // Send delegation report email to admin
178
                        // Send delegation report email to admin
179
 
179
 
180
                        $message  = "OID delegation report\n";
180
                        $message  = "OID delegation report\n";
181
                        $message .= "\n";
181
                        $message .= "\n";
182
                        $message .= "OID: ".$new_oid."\n";
182
                        $message .= "OID: ".$new_oid."\n";
183
                        $message .= "\n";
183
                        $message .= "\n";
184
                        $message .= "RA Name: $ra_name\n";
184
                        $message .= "RA Name: $ra_name\n";
185
                        $message .= "RA eMail: $email\n";
185
                        $message .= "RA eMail: $email\n";
186
                        $message .= "URL for more information: $url\n";
186
                        $message .= "URL for more information: $url\n";
187
                        $message .= "OID Name: $title\n";
187
                        $message .= "OID Name: $title\n";
188
                        $message .= "\n";
188
                        $message .= "\n";
189
                        $message .= "More details: ".OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL)."?goto=oid%3A$new_oid\n";
189
                        $message .= "More details: ".OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL)."?goto=oid%3A$new_oid\n";
190
 
190
 
191
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title')." - OID $new_oid registered", $message);
191
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title')." - OID $new_oid registered", $message);
192
 
192
 
193
                        // Send delegation information to user
193
                        // Send delegation information to user
194
 
194
 
195
                        $message = file_get_contents(__DIR__ . '/allocated_msg.tpl');
195
                        $message = file_get_contents(__DIR__ . '/allocated_msg.tpl');
196
                        $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
196
                        $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
197
                        $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
197
                        $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
198
                        $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
198
                        $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
199
                        $message = str_replace('{{NEW_OID}}', $new_oid, $message);
199
                        $message = str_replace('{{NEW_OID}}', $new_oid, $message);
200
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID allocated', $message);
200
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID allocated', $message);
201
 
201
 
202
                        return array(
202
                        return array(
203
                                "new_oid" => $new_oid,
203
                                "new_oid" => $new_oid,
204
                                "status" => 0
204
                                "status" => 0
205
                        );
205
                        );
206
                } else {
206
                } else {
207
                        return parent::action($actionID, $params);
207
                        return parent::action($actionID, $params);
208
                }
208
                }
209
        }
209
        }
210
 
210
 
211
        /**
211
        /**
212
         * @param bool $html
212
         * @param bool $html
213
         * @return void
213
         * @return void
214
         * @throws OIDplusException
214
         * @throws OIDplusException
215
         */
215
         */
216
        public function init(bool $html=true) {
216
        public function init(bool $html=true) {
217
                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) {
217
                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) {
218
                        if (($value != '') && !oid_valid_dotnotation($value,false,false,1)) {
218
                        if (($value != '') && !oid_valid_dotnotation($value,false,false,1)) {
219
                                throw new OIDplusException(_L('Please enter a valid OID in dot notation or nothing'));
219
                                throw new OIDplusException(_L('Please enter a valid OID in dot notation or nothing'));
220
                        }
220
                        }
221
                });
221
                });
222
        }
222
        }
223
 
223
 
224
        /**
224
        /**
225
         * @param string $id
225
         * @param string $id
226
         * @param array $out
226
         * @param array $out
227
         * @param bool $handled
227
         * @param bool $handled
228
         * @return void
228
         * @return void
229
         * @throws OIDplusException
229
         * @throws OIDplusException
230
         */
230
         */
231
        public function gui(string $id, array &$out, bool &$handled) {
231
        public function gui(string $id, array &$out, bool &$handled) {
232
                if (empty(self::getFreeRootOid(false))) return;
232
                if (empty(self::getFreeRootOid(false))) return;
233
 
233
 
234
                if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid') {
234
                if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid') {
235
                        $handled = true;
235
                        $handled = true;
236
 
236
 
237
                        $out['title'] = _L('Register a free OID');
237
                        $out['title'] = _L('Register a free OID');
238
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
238
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
239
 
239
 
240
                        // Note: We are using the highest OID instead of the rowcount, because there might be OIDs which could have been deleted in between
240
                        // Note: We are using the highest OID instead of the rowcount, because there might be OIDs which could have been deleted in between
241
                        $highest_id = $this->freeoid_max_id();
241
                        $highest_id = $this->freeoid_max_id();
242
 
242
 
243
                        $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>';
243
                        $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>';
244
 
244
 
245
                        try {
245
                        try {
246
                                $out['text'] .= '
246
                                $out['text'] .= '
247
                                  <form id="freeOIDForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicFreeOID.freeOIDFormOnSubmit();">
247
                                  <form id="freeOIDForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicFreeOID.freeOIDFormOnSubmit();">
248
                                    '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>
248
                                    '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>
249
                                    '.OIDplus::getActiveCaptchaPlugin()->captchaGenerate().'
249
                                    '.OIDplus::getActiveCaptchaPlugin()->captchaGenerate().'
250
                                    <br>
250
                                    <br>
251
                                    <input type="submit" value="'._L('Request a free OID').'">
251
                                    <input type="submit" value="'._L('Request a free OID').'">
252
                                  </form>';
252
                                  </form>';
253
 
253
 
254
                                $obj = OIDplusOid::parse(self::getFreeRootOid(true));
254
                                $obj = OIDplusOid::parse(self::getFreeRootOid(true));
255
 
255
 
256
                                if (file_exists(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html')) {
256
                                if (file_exists(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html')) {
257
                                        $tos = file_get_contents(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html');
257
                                        $tos = file_get_contents(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html');
258
                                } else {
258
                                } else {
259
                                        $tos = file_get_contents(__DIR__ . '/tos.html');
259
                                        $tos = file_get_contents(__DIR__ . '/tos.html');
260
                                }
260
                                }
261
 
261
 
262
                                list($html, $js, $css) = extractHtmlContents($tos);
262
                                list($html, $js, $css) = extractHtmlContents($tos);
263
                                $tos = '';
263
                                $tos = '';
264
                                if (!empty($js))  $tos .= "<script>\n$js\n</script>";
264
                                if (!empty($js))  $tos .= "<script>\n$js\n</script>";
265
                                if (!empty($css)) $tos .= "<style>\n$css\n</style>";
265
                                if (!empty($css)) $tos .= "<style>\n$css\n</style>";
266
                                $tos .= stripHtmlComments($html);
266
                                $tos .= stripHtmlComments($html);
267
 
267
 
268
                                $tos = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $tos);
268
                                $tos = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $tos);
269
                                if ($obj) {
269
                                if ($obj) {
270
                                        $tos = str_replace('{{ROOT_OID}}', $obj->getDotNotation(), $tos);
270
                                        $tos = str_replace('{{ROOT_OID}}', $obj->getDotNotation(), $tos);
271
                                        $tos = str_replace('{{ROOT_OID_ASN1}}', $obj->getAsn1Notation(), $tos);
271
                                        $tos = str_replace('{{ROOT_OID_ASN1}}', $obj->getAsn1Notation(), $tos);
272
                                        $tos = str_replace('{{ROOT_OID_IRI}}', $obj->getIriNotation(), $tos);
272
                                        $tos = str_replace('{{ROOT_OID_IRI}}', $obj->getIriNotation(), $tos);
273
                                }
273
                                }
274
                                $out['text'] .= $tos;
274
                                $out['text'] .= $tos;
275
 
275
 
276
                                if (OIDplus::config()->getValue('freeoid_root_oid') == '1.3.6.1.4.1.37476.9000') {
276
                                if (OIDplus::config()->getValue('freeoid_root_oid') == '1.3.6.1.4.1.37476.9000') {
277
                                        $out['text'] .= '<p>'._L('<b>Note:</b> Since September 2022, owners of FreeOID automatically receive a free ISO-7816 compliant <b>Application Identifier</b> (AID) with the format <code>D2:76:00:01:86:F0:(FreeOID):FF:(PIX)</code> (up to 64 bits application specific PIX, depending on the length of the FreeOID number).');
277
                                        $out['text'] .= '<p>'._L('<b>Note:</b> Since September 2022, owners of FreeOID automatically receive a free ISO-7816 compliant <b>Application Identifier</b> (AID) with the format <code>D2:76:00:01:86:F0:(FreeOID):FF:(PIX)</code> (up to 64 bits application specific PIX, depending on the length of the FreeOID number).');
278
$out['text'] .= ' - <a '.OIDplus::gui()->link('aid:D276000186F1').'>'._L('More information').'</a></p>';
278
$out['text'] .= ' - <a '.OIDplus::gui()->link('aid:D276000186F1').'>'._L('More information').'</a></p>';
279
                                }
279
                                }
280
                        } catch (\Exception $e) {
280
                        } catch (\Exception $e) {
281
                                $out['text'] = _L('Error: %1',$e->getMessage());
281
                                $out['text'] = _L('Error: %1',$e->getMessage());
282
                        }
282
                        }
283
                } else if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid.activate_freeoid') {
283
                } else if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid.activate_freeoid') {
284
                        $handled = true;
284
                        $handled = true;
285
 
285
 
286
                        $email = explode('$',$id)[1];
286
                        $email = explode('$',$id)[1];
287
                        $timestamp = explode('$',$id)[2];
287
                        $timestamp = explode('$',$id)[2];
288
                        $auth = explode('$',$id)[3];
288
                        $auth = explode('$',$id)[3];
289
 
289
 
290
                        $out['title'] = _L('Activate Free OID');
290
                        $out['title'] = _L('Activate Free OID');
291
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
291
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
292
 
292
 
293
                        if ($already_registered_oid = $this->alreadyHasFreeOid($email, true)) {
293
                        if ($already_registered_oid = $this->alreadyHasFreeOid($email, true)) {
294
                                throw new OIDplusException(_L('This email address already has a FreeOID registered (%1)', $already_registered_oid));
294
                                throw new OIDplusException(_L('This email address already has a FreeOID registered (%1)', $already_registered_oid));
295
                        } else {
295
                        } else {
296
                                if (!OIDplus::authUtils()->validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
296
                                if (!OIDplus::authUtils()->validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) {
297
                                        $out['icon'] = 'img/error.png';
297
                                        $out['icon'] = 'img/error.png';
298
                                        $out['text'] = _L('Invalid authorization. Is the URL OK?');
298
                                        $out['text'] = _L('Invalid authorization. Is the URL OK?');
299
                                } else {
299
                                } else {
300
                                        $ra = new OIDplusRA($email);
300
                                        $ra = new OIDplusRA($email);
301
                                        $ra_existing = $ra->existing();
301
                                        $ra_existing = $ra->existing();
302
 
302
 
303
                                        $out['text'] = '<p>'._L('eMail-Address').': <b>'.$email.'</b></p>';
303
                                        $out['text'] = '<p>'._L('eMail-Address').': <b>'.$email.'</b></p>';
304
 
304
 
305
                                        $out['text'] .= '  <form id="activateFreeOIDForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicFreeOID.activateFreeOIDFormOnSubmit();">';
305
                                        $out['text'] .= '  <form id="activateFreeOIDForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicFreeOID.activateFreeOIDFormOnSubmit();">';
306
                                        $out['text'] .= '    <input type="hidden" id="email" value="'.htmlentities($email).'"/>';
306
                                        $out['text'] .= '    <input type="hidden" id="email" value="'.htmlentities($email).'"/>';
307
                                        $out['text'] .= '    <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>';
307
                                        $out['text'] .= '    <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>';
308
                                        $out['text'] .= '    <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>';
308
                                        $out['text'] .= '    <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>';
309
 
309
 
310
                                        if ($ra_existing) {
310
                                        if ($ra_existing) {
311
                                                $out['text'] .= '    '._L('Your personal name or the name of your group').':<br><b>'.htmlentities($ra->raName()).'</b><br><br>';
311
                                                $out['text'] .= '    '._L('Your personal name or the name of your group').':<br><b>'.htmlentities($ra->raName()).'</b><br><br>';
312
                                        } else {
312
                                        } else {
313
                                                $out['text'] .= '    '._L('Your personal name or the name of your group').':<br><input type="text" id="ra_name" value=""/><br><br>'; // TODO: disable autocomplete
313
                                                $out['text'] .= '    '._L('Your personal name or the name of your group').':<br><input type="text" id="ra_name" value=""/><br><br>'; // TODO: disable autocomplete
314
                                        }
314
                                        }
315
                                        $out['text'] .= '    '._L('Title of your OID (usually equal to your name, optional)').':<br><input type="text" id="title" value=""/><br><br>';
315
                                        $out['text'] .= '    '._L('Title of your OID (usually equal to your name, optional)').':<br><input type="text" id="title" value=""/><br><br>';
316
                                        $out['text'] .= '    '._L('URL for more information about your project(s) (optional)').':<br><input type="text" id="url" value=""/><br><br>';
316
                                        $out['text'] .= '    '._L('URL for more information about your project(s) (optional)').':<br><input type="text" id="url" value=""/><br><br>';
317
 
317
 
318
                                        if (!$ra_existing) {
318
                                        if (!$ra_existing) {
319
                                                $out['text'] .= '    <div><label class="padding_label">'._L('Password').':</label><input type="password" id="password1" value=""/></div>';
319
                                                $out['text'] .= '    <div><label class="padding_label">'._L('Password').':</label><input type="password" id="password1" value=""/></div>';
320
                                                $out['text'] .= '    <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>';
320
                                                $out['text'] .= '    <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>';
321
                                        }
321
                                        }
322
                                        $out['text'] .= '    <br><input type="submit" value="'._L('Register').'">';
322
                                        $out['text'] .= '    <br><input type="submit" value="'._L('Register').'">';
323
                                        $out['text'] .= '  </form>';
323
                                        $out['text'] .= '  </form>';
324
                                }
324
                                }
325
                        }
325
                        }
326
                }
326
                }
327
        }
327
        }
328
 
328
 
329
        /**
329
        /**
330
         * @param array $out
330
         * @param array $out
331
         * @return void
331
         * @return void
332
         * @throws OIDplusException
332
         * @throws OIDplusException
333
         */
333
         */
334
        public function publicSitemap(array &$out) {
334
        public function publicSitemap(array &$out) {
335
                if (empty(self::getFreeRootOid(false))) return;
335
                if (empty(self::getFreeRootOid(false))) return;
336
                $out[] = 'oidplus:com.viathinksoft.freeoid';
336
                $out[] = 'oidplus:com.viathinksoft.freeoid';
337
        }
337
        }
338
 
338
 
339
        /**
339
        /**
340
         * @param array $json
340
         * @param array $json
341
         * @param string|null $ra_email
341
         * @param string|null $ra_email
342
         * @param bool $nonjs
342
         * @param bool $nonjs
343
         * @param string $req_goto
343
         * @param string $req_goto
344
         * @return bool
344
         * @return bool
345
         * @throws OIDplusException
345
         * @throws OIDplusException
346
         */
346
         */
347
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
347
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
348
                if (empty(self::getFreeRootOid(false))) return false;
348
                if (empty(self::getFreeRootOid(false))) return false;
349
 
349
 
350
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
350
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
351
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
351
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
352
                } else {
352
                } else {
353
                        $tree_icon = null; // default icon (folder)
353
                        $tree_icon = null; // default icon (folder)
354
                }
354
                }
355
 
355
 
356
                $json[] = array(
356
                $json[] = array(
357
                        'id' => 'oidplus:com.viathinksoft.freeoid',
357
                        'id' => 'oidplus:com.viathinksoft.freeoid',
358
                        'icon' => $tree_icon,
358
                        'icon' => $tree_icon,
359
                        'text' => _L('Register a free OID')
359
                        'text' => _L('Register a free OID')
360
                );
360
                );
361
 
361
 
362
                return true;
362
                return true;
363
        }
363
        }
364
 
364
 
365
        # ---
365
        # ---
366
 
366
 
367
        /**
367
        /**
368
         * @return int
368
         * @return int
369
         * @throws OIDplusException
369
         * @throws OIDplusException
370
         */
370
         */
371
        protected static function freeoid_max_id(): int {
371
        protected static function freeoid_max_id(): int {
372
                $res = OIDplus::db()->query("select id from ###objects where id like ? order by ".OIDplus::db()->natOrder('id'), array(self::getFreeRootOid(true).'.%'));
372
                $res = OIDplus::db()->query("select id from ###objects where id like ? order by ".OIDplus::db()->natOrder('id'), array(self::getFreeRootOid(true).'.%'));
373
                $highest_id = 0;
373
                $highest_id = 0;
374
                while ($row = $res->fetch_array()) {
374
                while ($row = $res->fetch_array()) {
375
                        $arc = substr_count(self::getFreeRootOid(false), '.')+1;
375
                        $arc = substr_count(self::getFreeRootOid(false), '.')+1;
376
                        $highest_id = explode('.',$row['id'])[$arc];
376
                        $highest_id = explode('.',$row['id'])[$arc];
377
                }
377
                }
378
                return $highest_id;
378
                return (int)$highest_id;
379
        }
379
        }
380
 
380
 
381
        /**
381
        /**
382
         * @param string $request
382
         * @param string $request
383
         * @return array|false
383
         * @return array|false
384
         */
384
         */
385
        public function tree_search(string $request) {
385
        public function tree_search(string $request) {
386
                return false;
386
                return false;
387
        }
387
        }
388
}
388
}
389
 
389