Subversion Repositories oidplus

Rev

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

Rev 1283 Rev 1293
Line 53... Line 53...
53
                }
53
                }
54
                return $getId ? null : false;
54
                return $getId ? null : false;
55
        }
55
        }
56
 
56
 
57
        /**
57
        /**
58
         * @param string $actionID
-
 
59
         * @param array $params
58
         * @param array $params
60
         * @return array
59
         * @return array
61
         * @throws OIDplusException
60
         * @throws OIDplusException
62
         * @throws OIDplusMailException
61
         * @throws OIDplusMailException
63
         */
62
         */
64
        public function action(string $actionID, array $params): array {
63
        private function action_Request(array $params): array {
65
                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.'));
66
 
65
 
67
                if ($actionID == 'request_freeoid') {
-
 
68
                        _CheckParamExists($params, 'email');
66
                _CheckParamExists($params, 'email');
69
                        $email = $params['email'];
67
                $email = $params['email'];
70
 
68
 
71
                        if ($already_registered_oid = $this->alreadyHasFreeOid($email, true)) {
69
                if ($already_registered_oid = $this->alreadyHasFreeOid($email, true)) {
72
                                throw new OIDplusHtmlException(_L('This email address already has a FreeOID registered (%1)', '<a '.OIDplus::gui()->link($already_registered_oid).'>'.htmlentities($already_registered_oid).'</a>'));
70
                        throw new OIDplusHtmlException(_L('This email address already has a FreeOID registered (%1)', '<a '.OIDplus::gui()->link($already_registered_oid).'>'.htmlentities($already_registered_oid).'</a>'));
73
                        }
71
                }
74
 
72
 
75
                        if (!OIDplus::mailUtils()->validMailAddress($email)) {
73
                if (!OIDplus::mailUtils()->validMailAddress($email)) {
76
                                throw new OIDplusException(_L('Invalid email address'));
74
                        throw new OIDplusException(_L('Invalid email address'));
77
                        }
75
                }
78
 
76
 
79
                        OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha');
77
                OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha');
80
 
78
 
81
                        $root_oid = self::getFreeRootOid(false);
79
                $root_oid = self::getFreeRootOid(false);
82
                        OIDplus::logger()->log("V2:[INFO]OID(oid:%1)+RA(%2)", "Requested a free OID for email '%2' to be placed into root '%1'", $root_oid, $email);
80
                OIDplus::logger()->log("V2:[INFO]OID(oid:%1)+RA(%2)", "Requested a free OID for email '%2' to be placed into root '%1'", $root_oid, $email);
83
 
81
 
84
                        $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:com.viathinksoft.freeoid.activate_freeoid$'.$email.'$'.OIDplus::authUtils()->makeAuthKey(['40c87e20-f4fb-11ed-86ca-3c4a92df8582',$email]));
82
                $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:com.viathinksoft.freeoid.activate_freeoid$'.$email.'$'.OIDplus::authUtils()->makeAuthKey(['40c87e20-f4fb-11ed-86ca-3c4a92df8582',$email]));
85
 
83
 
86
                        $message = file_get_contents(__DIR__ . '/request_msg.tpl');
84
                $message = file_get_contents(__DIR__ . '/request_msg.tpl');
87
                        $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
85
                $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);
86
                $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
89
                        $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
87
                $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
90
                        $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
88
                $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
91
 
89
 
92
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID request', $message);
90
                OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID request', $message);
93
 
91
 
94
                        return array("status" => 0);
92
                return array("status" => 0);
-
 
93
        }
95
 
94
 
-
 
95
        /**
-
 
96
         * @param array $params
-
 
97
         * @return array
-
 
98
         * @throws OIDplusException
-
 
99
         * @throws OIDplusMailException
-
 
100
         */
96
                } else if ($actionID == 'activate_freeoid') {
101
        private function action_Activate(array $params): array {
-
 
102
                if (empty(self::getFreeRootOid(false))) throw new OIDplusException(_L('FreeOID service not available. Please ask your administrator.'));
-
 
103
 
97
                        _CheckParamExists($params, 'email');
104
                _CheckParamExists($params, 'email');
98
                        _CheckParamExists($params, 'auth');
105
                _CheckParamExists($params, 'auth');
-
 
106
 
-
 
107
                $email = $params['email'];
-
 
108
                $auth = $params['auth'];
-
 
109
 
-
 
110
                if (!OIDplus::authUtils()->validateAuthKey(['40c87e20-f4fb-11ed-86ca-3c4a92df8582',$email], $auth, OIDplus::config()->getValue('max_ra_invite_time', -1))) {
-
 
111
                        throw new OIDplusException(_L('Invalid or expired authentication key'));
-
 
112
                }
-
 
113
 
-
 
114
                // 1. step: Check entered data and add the RA to the database
99
 
115
 
100
                        $email = $params['email'];
116
                $ra = new OIDplusRA($email);
-
 
117
                if (!$ra->existing()) {
-
 
118
                        _CheckParamExists($params, 'password1');
-
 
119
                        _CheckParamExists($params, 'password2');
101
                        $auth = $params['auth'];
120
                        _CheckParamExists($params, 'ra_name');
102
 
121
 
-
 
122
                        $password1 = $params['password1'];
-
 
123
                        $password2 = $params['password2'];
-
 
124
                        $ra_name = $params['ra_name'];
-
 
125
 
103
                        if (!OIDplus::authUtils()->validateAuthKey(['40c87e20-f4fb-11ed-86ca-3c4a92df8582',$email], $auth, OIDplus::config()->getValue('max_ra_invite_time', -1))) {
126
                        if ($password1 !== $password2) {
104
                                throw new OIDplusException(_L('Invalid or expired authentication key'));
127
                                throw new OIDplusException(_L('Passwords do not match'));
105
                        }
128
                        }
106
 
129
 
-
 
130
                        if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
107
                        // 1. step: Check entered data and add the RA to the database
131
                                $minlen = OIDplus::config()->getValue('ra_min_password_length');
-
 
132
                                throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
-
 
133
                        }
108
 
134
 
109
                        $ra = new OIDplusRA($email);
-
 
110
                        if (!$ra->existing()) {
-
 
111
                                _CheckParamExists($params, 'password1');
-
 
112
                                _CheckParamExists($params, 'password2');
-
 
113
                                _CheckParamExists($params, 'ra_name');
-
 
114
 
-
 
115
                                $password1 = $params['password1'];
-
 
116
                                $password2 = $params['password2'];
-
 
117
                                $ra_name = $params['ra_name'];
-
 
118
 
-
 
119
                                if ($password1 !== $password2) {
-
 
120
                                        throw new OIDplusException(_L('Passwords do not match'));
-
 
121
                                }
-
 
122
 
-
 
123
                                if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
-
 
124
                                        $minlen = OIDplus::config()->getValue('ra_min_password_length');
-
 
125
                                        throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
-
 
126
                                }
-
 
127
 
-
 
128
                                if (empty($ra_name)) {
135
                        if (empty($ra_name)) {
129
                                        throw new OIDplusException(_L('Please enter your personal name or the name of your group.'));
136
                                throw new OIDplusException(_L('Please enter your personal name or the name of your group.'));
130
                                }
-
 
131
 
-
 
132
                                $ra->register_ra($password1);
-
 
133
                                $ra->setRaName($ra_name);
-
 
134
                        } else {
-
 
135
                                // RA already exists (e.g. was logged in using Google OAuth)
-
 
136
                                $ra_name = $ra->raName();
-
 
137
                        }
137
                        }
138
 
138
 
-
 
139
                        $ra->register_ra($password1);
-
 
140
                        $ra->setRaName($ra_name);
-
 
141
                } else {
139
                        // 2. step: Add the new OID to the database
142
                        // RA already exists (e.g. was logged in using Google OAuth)
-
 
143
                        $ra_name = $ra->raName();
-
 
144
                }
140
 
145
 
141
                        $url = $params['url'] ?? '';
-
 
142
                        $title = $params['title'] ?? '';
146
                // 2. step: Add the new OID to the database
143
 
147
 
144
                        $root_oid = self::getFreeRootOid(false);
148
                $url = $params['url'] ?? '';
145
                        $new_oid = OIDplusOid::parse('oid:'.$root_oid)->appendArcs($this->freeoid_max_id()+1)->nodeId(false);
149
                $title = $params['title'] ?? '';
146
 
150
 
147
                        OIDplus::logger()->log("V2:[INFO]OID(oid:%2)+OIDRA(oid:%2)", "Child OID '%1' added automatically by '%3' (RA Name: '%4')", $new_oid, $root_oid, $email, $ra_name);
151
                $root_oid = self::getFreeRootOid(false);
148
                        OIDplus::logger()->log("V2:[INFO]OID(oid:%1)+[OK]RA(%3)",    "Free OID '%1' activated (RA Name: '%4')",                    $new_oid, $root_oid, $email, $ra_name);
152
                $new_oid = OIDplusOid::parse('oid:'.$root_oid)->appendArcs($this->freeoid_max_id()+1)->nodeId(false);
149
 
153
 
150
                        if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url;
154
                OIDplus::logger()->log("V2:[INFO]OID(oid:%2)+OIDRA(oid:%2)", "Child OID '%1' added automatically by '%3' (RA Name: '%4')", $new_oid, $root_oid, $email, $ra_name);
-
 
155
                OIDplus::logger()->log("V2:[INFO]OID(oid:%1)+[OK]RA(%3)",    "Free OID '%1' activated (RA Name: '%4')",                    $new_oid, $root_oid, $email, $ra_name);
151
 
156
 
152
                        $description = ''; // '<p>'.htmlentities($ra_name).'</p>';
157
                if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url;
153
                        if (!empty($url)) {
-
 
154
                                $description .= '<p>'._L('More information at %1','<a href="'.htmlentities($url).'">'.htmlentities($url).'</a>').'</p>';
-
 
155
                        }
-
 
156
 
158
 
-
 
159
                $description = ''; // '<p>'.htmlentities($ra_name).'</p>';
157
                        if (empty($title)) $title = $ra_name;
160
                if (!empty($url)) {
-
 
161
                        $description .= '<p>'._L('More information at %1','<a href="'.htmlentities($url).'">'.htmlentities($url).'</a>').'</p>';
-
 
162
                }
158
 
163
 
-
 
164
                if (empty($title)) $title = $ra_name;
-
 
165
 
159
                        try {
166
                try {
160
                                $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:');
167
                        $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:');
161
                                if (strlen($new_oid) > $maxlen) {
168
                        if (strlen($new_oid) > $maxlen) {
162
                                        throw new OIDplusException(_L('The resulting OID %1 is too long (max allowed length: %2)',$new_oid,$maxlen));
169
                                throw new OIDplusException(_L('The resulting OID %1 is too long (max allowed length: %2)',$new_oid,$maxlen));
163
                                }
-
 
164
 
-
 
165
                                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));
-
 
166
                                OIDplusObject::resetObjectInformationCache();
-
 
167
                        } catch (\Exception $e) {
-
 
168
                                $ra->delete();
-
 
169
                                throw $e;
-
 
170
                        }
170
                        }
171
 
171
 
-
 
172
                        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));
-
 
173
                        OIDplusObject::resetObjectInformationCache();
-
 
174
                } catch (\Exception $e) {
-
 
175
                        $ra->delete();
-
 
176
                        throw $e;
-
 
177
                }
-
 
178
 
172
                        // Send delegation report email to admin
179
                // Send delegation report email to admin
-
 
180
 
-
 
181
                $message  = "OID delegation report\n";
-
 
182
                $message .= "\n";
-
 
183
                $message .= "OID: ".$new_oid."\n";
-
 
184
                $message .= "\n";
-
 
185
                $message .= "RA Name: $ra_name\n";
-
 
186
                $message .= "RA eMail: $email\n";
-
 
187
                $message .= "URL for more information: $url\n";
-
 
188
                $message .= "OID Name: $title\n";
-
 
189
                $message .= "\n";
-
 
190
                $message .= "More details: ".OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL)."?goto=oid%3A$new_oid\n";
-
 
191
 
-
 
192
                OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title')." - OID $new_oid registered", $message);
-
 
193
 
-
 
194
                // Send delegation information to user
-
 
195
 
-
 
196
                $message = file_get_contents(__DIR__ . '/allocated_msg.tpl');
-
 
197
                $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
-
 
198
                $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
-
 
199
                $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
-
 
200
                $message = str_replace('{{NEW_OID}}', $new_oid, $message);
-
 
201
                OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID allocated', $message);
-
 
202
 
-
 
203
                return array(
-
 
204
                        "new_oid" => $new_oid,
-
 
205
                        "status" => 0
-
 
206
                );
-
 
207
        }
173
 
208
 
174
                        $message  = "OID delegation report\n";
-
 
175
                        $message .= "\n";
209
        /**
176
                        $message .= "OID: ".$new_oid."\n";
210
         * @param string $actionID
177
                        $message .= "\n";
211
         * @param array $params
178
                        $message .= "RA Name: $ra_name\n";
212
         * @return array
179
                        $message .= "RA eMail: $email\n";
213
         * @throws OIDplusException
180
                        $message .= "URL for more information: $url\n";
-
 
181
                        $message .= "OID Name: $title\n";
214
         * @throws OIDplusMailException
182
                        $message .= "\n";
-
 
183
                        $message .= "More details: ".OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL)."?goto=oid%3A$new_oid\n";
-
 
184
 
215
         */
185
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title')." - OID $new_oid registered", $message);
216
        public function action(string $actionID, array $params): array {
186
 
-
 
187
                        // Send delegation information to user
217
                if ($actionID == 'request_freeoid') {
188
 
-
 
189
                        $message = file_get_contents(__DIR__ . '/allocated_msg.tpl');
218
                        return $this->action_Request($params);
190
                        $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
-
 
191
                        $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
-
 
192
                        $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
-
 
193
                        $message = str_replace('{{NEW_OID}}', $new_oid, $message);
219
                } else if ($actionID == 'activate_freeoid') {
194
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID allocated', $message);
-
 
195
 
-
 
196
                        return array(
220
                        return $this->action_Activate($params);
197
                                "new_oid" => $new_oid,
-
 
198
                                "status" => 0
-
 
199
                        );
-
 
200
                } else {
221
                } else {
201
                        return parent::action($actionID, $params);
222
                        return parent::action($actionID, $params);
202
                }
223
                }
203
        }
224
        }
204
 
225