Rev 21 | Rev 26 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 21 | Rev 24 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * OID-Info.com API for PHP |
4 | * OID-Info.com API for PHP |
5 | * Copyright 2019-2021 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019-2021 Daniel Marschall, ViaThinkSoft |
6 | * Version 2021-06-19 |
6 | * Version 2021-11-24 |
7 | * |
7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * you may not use this file except in compliance with the License. |
9 | * you may not use this file except in compliance with the License. |
10 | * You may obtain a copy of the License at |
10 | * You may obtain a copy of the License at |
11 | * |
11 | * |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | * |
13 | * |
14 | * Unless required by applicable law or agreed to in writing, software |
14 | * Unless required by applicable law or agreed to in writing, software |
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | * See the License for the specific language governing permissions and |
17 | * See the License for the specific language governing permissions and |
18 | * limitations under the License. |
18 | * limitations under the License. |
19 | */ |
19 | */ |
20 | 20 | ||
21 | // error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED); |
21 | // error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED); |
22 | 22 | ||
23 | if(!defined('STDIN')) define('STDIN', fopen('php://stdin', 'rb')); |
23 | if(!defined('STDIN')) define('STDIN', fopen('php://stdin', 'rb')); |
24 | if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'wb')); |
24 | if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'wb')); |
25 | if(!defined('STDERR')) define('STDERR', fopen('php://stderr', 'wb')); |
25 | if(!defined('STDERR')) define('STDERR', fopen('php://stderr', 'wb')); |
26 | 26 | ||
27 | if (file_exists(__DIR__ . '/oid_utils.inc.phps')) require_once __DIR__ . '/oid_utils.inc.phps'; |
27 | if (file_exists(__DIR__ . '/oid_utils.inc.phps')) require_once __DIR__ . '/oid_utils.inc.phps'; |
28 | if (file_exists(__DIR__ . '/oid_utils.inc.php')) require_once __DIR__ . '/oid_utils.inc.php'; |
28 | if (file_exists(__DIR__ . '/oid_utils.inc.php')) require_once __DIR__ . '/oid_utils.inc.php'; |
29 | if (file_exists(__DIR__ . '/xml_utils.inc.phps')) require_once __DIR__ . '/xml_utils.inc.phps'; |
29 | if (file_exists(__DIR__ . '/xml_utils.inc.phps')) require_once __DIR__ . '/xml_utils.inc.phps'; |
30 | if (file_exists(__DIR__ . '/xml_utils.inc.php')) require_once __DIR__ . '/xml_utils.inc.php'; |
30 | if (file_exists(__DIR__ . '/xml_utils.inc.php')) require_once __DIR__ . '/xml_utils.inc.php'; |
31 | if (file_exists(__DIR__ . '/../includes/oid_utils.inc.php')) require_once __DIR__ . '/../includes/oid_utils.inc.php'; |
31 | if (file_exists(__DIR__ . '/../includes/oid_utils.inc.php')) require_once __DIR__ . '/../includes/oid_utils.inc.php'; |
32 | if (file_exists(__DIR__ . '/../includes/xml_utils.inc.php')) require_once __DIR__ . '/../includes/xml_utils.inc.php'; |
32 | if (file_exists(__DIR__ . '/../includes/xml_utils.inc.php')) require_once __DIR__ . '/../includes/xml_utils.inc.php'; |
33 | if (file_exists(__DIR__ . '/../../includes/oid_utils.inc.php')) require_once __DIR__ . '/../../includes/oid_utils.inc.php'; |
33 | if (file_exists(__DIR__ . '/../../includes/oid_utils.inc.php')) require_once __DIR__ . '/../../includes/oid_utils.inc.php'; |
34 | if (file_exists(__DIR__ . '/../../includes/xml_utils.inc.php')) require_once __DIR__ . '/../../includes/xml_utils.inc.php'; |
34 | if (file_exists(__DIR__ . '/../../includes/xml_utils.inc.php')) require_once __DIR__ . '/../../includes/xml_utils.inc.php'; |
35 | if (file_exists(__DIR__ . '/../../../includes/oid_utils.inc.php')) require_once __DIR__ . '/../../../includes/oid_utils.inc.php'; |
35 | if (file_exists(__DIR__ . '/../../../includes/oid_utils.inc.php')) require_once __DIR__ . '/../../../includes/oid_utils.inc.php'; |
36 | if (file_exists(__DIR__ . '/../../../includes/xml_utils.inc.php')) require_once __DIR__ . '/../../../includes/xml_utils.inc.php'; |
36 | if (file_exists(__DIR__ . '/../../../includes/xml_utils.inc.php')) require_once __DIR__ . '/../../../includes/xml_utils.inc.php'; |
37 | 37 | ||
38 | class OIDInfoException extends Exception { |
38 | class OIDInfoException extends Exception { |
39 | } |
39 | } |
40 | 40 | ||
41 | class OIDInfoAPI { |
41 | class OIDInfoAPI { |
42 | 42 | ||
43 | # --- PART 0: Constants |
43 | # --- PART 0: Constants |
44 | 44 | ||
45 | // First digit of the ping result |
45 | // First digit of the ping result |
46 | // "-" = error |
46 | // "-" = error |
47 | // "0" = OID does not exist |
47 | // "0" = OID does not exist |
48 | // "1" = OID does exist, but is not approved yet |
48 | // "1" = OID does exist, but is not approved yet |
49 | // "2" = OID does exist and is accessible |
49 | // "2" = OID does exist and is accessible |
50 | /*private*/ const PING_IDX_EXISTS = 0; |
50 | /*private*/ const PING_IDX_EXISTS = 0; |
51 | 51 | ||
52 | // Second digit of the ping result |
52 | // Second digit of the ping result |
53 | // "-" = error |
53 | // "-" = error |
54 | // "0" = The OID may not be created |
54 | // "0" = The OID may not be created |
55 | // "1" = OID is not an illegal OID, and none of its ascendant is a leaf and its parent OID is not frozen |
55 | // "1" = OID is not an illegal OID, and none of its ascendant is a leaf and its parent OID is not frozen |
56 | /*private*/ const PING_IDX_MAY_CREATE = 1; |
56 | /*private*/ const PING_IDX_MAY_CREATE = 1; |
57 | 57 | ||
58 | /*private*/ const SOFT_CORRECT_BEHAVIOR_NONE = 0; |
58 | /*private*/ const SOFT_CORRECT_BEHAVIOR_NONE = 0; |
59 | /*private*/ const SOFT_CORRECT_BEHAVIOR_LOWERCASE_BEGINNING = 1; |
59 | /*private*/ const SOFT_CORRECT_BEHAVIOR_LOWERCASE_BEGINNING = 1; |
60 | /*private*/ const SOFT_CORRECT_BEHAVIOR_ALL_POSSIBLE = 2; |
60 | /*private*/ const SOFT_CORRECT_BEHAVIOR_ALL_POSSIBLE = 2; |
61 | 61 | ||
62 | /*public*/ const DEFAULT_ILLEGALITY_RULE_FILE = __DIR__ . '/oid_illegality_rules'; |
62 | /*public*/ const DEFAULT_ILLEGALITY_RULE_FILE = __DIR__ . '/oid_illegality_rules'; |
63 | 63 | ||
64 | # --- Part 1: "Ping API" for checking if OIDs are available or allowed to create |
64 | # --- Part 1: "Ping API" for checking if OIDs are available or allowed to create |
65 | 65 | ||
66 | public $verbosePingProviders = array('https://misc.daniel-marschall.de/oid-repository/ping_oid.php?oid={OID}'); |
66 | public $verbosePingProviders = array('https://misc.daniel-marschall.de/oid-repository/ping_oid.php?oid={OID}'); |
67 | 67 | ||
68 | private $pingCache = array(); |
68 | private $pingCache = array(); |
69 | 69 | ||
70 | public $pingCacheMaxAge = 3600; |
70 | public $pingCacheMaxAge = 3600; |
71 | 71 | ||
72 | public function clearPingCache() { |
72 | public function clearPingCache() { |
73 | $this->pingCache = array(); |
73 | $this->pingCache = array(); |
74 | } |
74 | } |
75 | 75 | ||
76 | public function checkOnlineExists($oid) { |
76 | public function checkOnlineExists($oid) { |
77 | if (!self::strictCheckSyntax($oid)) return false; |
77 | if (!self::strictCheckSyntax($oid)) return false; |
78 | 78 | ||
79 | $pingResult = $this->pingOID($oid); |
79 | $pingResult = $this->pingOID($oid); |
80 | $ret = $pingResult[self::PING_IDX_EXISTS] >= 1; |
80 | $ret = $pingResult[self::PING_IDX_EXISTS] >= 1; |
81 | return $ret; |
81 | return $ret; |
82 | } |
82 | } |
83 | 83 | ||
84 | public function checkOnlineAvailable($oid) { |
84 | public function checkOnlineAvailable($oid) { |
85 | if (!self::strictCheckSyntax($oid)) return false; |
85 | if (!self::strictCheckSyntax($oid)) return false; |
86 | 86 | ||
87 | $pingResult = $this->pingOID($oid); |
87 | $pingResult = $this->pingOID($oid); |
88 | $ret = $pingResult[self::PING_IDX_EXISTS] == 2; |
88 | $ret = $pingResult[self::PING_IDX_EXISTS] == 2; |
89 | return $ret; |
89 | return $ret; |
90 | } |
90 | } |
91 | 91 | ||
92 | public function checkOnlineAllowed($oid) { |
92 | public function checkOnlineAllowed($oid) { |
93 | if (!self::strictCheckSyntax($oid)) return false; |
93 | if (!self::strictCheckSyntax($oid)) return false; |
94 | 94 | ||
95 | $pingResult = $this->pingOID($oid); |
95 | $pingResult = $this->pingOID($oid); |
96 | return $pingResult[self::PING_IDX_MAY_CREATE] == 1; |
96 | return $pingResult[self::PING_IDX_MAY_CREATE] == 1; |
97 | } |
97 | } |
98 | 98 | ||
99 | public function checkOnlineMayCreate($oid) { |
99 | public function checkOnlineMayCreate($oid) { |
100 | if (!self::strictCheckSyntax($oid)) return false; |
100 | if (!self::strictCheckSyntax($oid)) return false; |
101 | 101 | ||
102 | $pingResult = $this->pingOID($oid); |
102 | $pingResult = $this->pingOID($oid); |
103 | 103 | ||
104 | // OID is either illegal, or one of their parents are leaf or frozen |
104 | // OID is either illegal, or one of their parents are leaf or frozen |
105 | # if (!checkOnlineAllowed($oid)) return false; |
105 | # if (!checkOnlineAllowed($oid)) return false; |
106 | if ($pingResult[self::PING_IDX_MAY_CREATE] == 0) return false; |
106 | if ($pingResult[self::PING_IDX_MAY_CREATE] == 0) return false; |
107 | 107 | ||
108 | // The OID exists already, so we don't need to create it again |
108 | // The OID exists already, so we don't need to create it again |
109 | # if ($this->checkOnlineExists($oid)) return false; |
109 | # if ($this->checkOnlineExists($oid)) return false; |
110 | if ($pingResult[self::PING_IDX_EXISTS] >= 1) return false; |
110 | if ($pingResult[self::PING_IDX_EXISTS] >= 1) return false; |
111 | 111 | ||
112 | return true; |
112 | return true; |
113 | } |
113 | } |
114 | 114 | ||
115 | protected function pingOID($oid) { |
115 | protected function pingOID($oid) { |
116 | if (isset($this->pingCache[$oid])) { |
116 | if (isset($this->pingCache[$oid])) { |
117 | $cacheAge = $this->pingCache[$oid][0] - time(); |
117 | $cacheAge = $this->pingCache[$oid][0] - time(); |
118 | if ($cacheAge <= $this->pingCacheMaxAge) { |
118 | if ($cacheAge <= $this->pingCacheMaxAge) { |
119 | return $this->pingCache[$oid][1]; |
119 | return $this->pingCache[$oid][1]; |
120 | } |
120 | } |
121 | } |
121 | } |
122 | 122 | ||
123 | if (count($this->verbosePingProviders) == 0) { |
123 | if (count($this->verbosePingProviders) == 0) { |
124 | throw new OIDInfoException("No verbose ping provider available!"); |
124 | throw new OIDInfoException("No verbose ping provider available!"); |
125 | } |
125 | } |
126 | 126 | ||
127 | $res = false; |
127 | $res = false; |
128 | foreach ($this->verbosePingProviders as $url) { |
128 | foreach ($this->verbosePingProviders as $url) { |
129 | $url = str_replace('{OID}', $oid, $url); |
129 | $url = str_replace('{OID}', $oid, $url); |
130 | $cn = @file_get_contents($url); |
130 | $cn = @file_get_contents($url); |
131 | if ($cn === false) continue; |
131 | if ($cn === false) continue; |
132 | $loc_res = trim($cn); |
132 | $loc_res = trim($cn); |
133 | if (strpos($loc_res, '-') === false) { |
133 | if (strpos($loc_res, '-') === false) { |
134 | $res = $loc_res; |
134 | $res = $loc_res; |
135 | break; |
135 | break; |
136 | } |
136 | } |
137 | } |
137 | } |
138 | if ($res === false) { |
138 | if ($res === false) { |
139 | throw new OIDInfoException("Could not ping OID $oid status!"); |
139 | throw new OIDInfoException("Could not ping OID $oid status!"); |
140 | } |
140 | } |
141 | 141 | ||
142 | // if ($this->pingCacheMaxAge >= 0) { |
142 | // if ($this->pingCacheMaxAge >= 0) { |
143 | $this->pingCache[$oid] = array(time(), $res); |
143 | $this->pingCache[$oid] = array(time(), $res); |
144 | //} |
144 | //} |
145 | 145 | ||
146 | return $res; |
146 | return $res; |
147 | } |
147 | } |
148 | 148 | ||
149 | # --- PART 2: Syntax checking |
149 | # --- PART 2: Syntax checking |
150 | 150 | ||
151 | public static function strictCheckSyntax($oid) { |
151 | public static function strictCheckSyntax($oid) { |
152 | return oid_valid_dotnotation($oid, false, false, 1); |
152 | return oid_valid_dotnotation($oid, false, false, 1); |
153 | } |
153 | } |
154 | 154 | ||
155 | // Returns false if $oid has wrong syntax |
155 | // Returns false if $oid has wrong syntax |
156 | // Return an OID without leading dot or zeroes, if the syntax is acceptable |
156 | // Return an OID without leading dot or zeroes, if the syntax is acceptable |
157 | public static function trySanitizeOID($oid) { |
157 | public static function trySanitizeOID($oid) { |
158 | // Allow leading dots and leading zeroes, but remove then afterwards |
158 | // Allow leading dots and leading zeroes, but remove then afterwards |
159 | $ok = oid_valid_dotnotation($oid, true, true, 1); |
159 | $ok = oid_valid_dotnotation($oid, true, true, 1); |
160 | if ($ok === false) return false; |
160 | if ($ok === false) return false; |
161 | 161 | ||
162 | return sanitizeOID($oid, $oid[0] == '.'); |
162 | return sanitizeOID($oid, $oid[0] == '.'); |
163 | } |
163 | } |
164 | 164 | ||
165 | # --- PART 3: XML file creation |
165 | # --- PART 3: XML file creation |
166 | 166 | ||
167 | protected static function eMailValid($email) { |
167 | protected static function eMailValid($email) { |
168 | # TODO: use isemail project |
168 | # TODO: use isemail project |
169 | 169 | ||
170 | if (empty($email)) return false; |
170 | if (empty($email)) return false; |
171 | 171 | ||
172 | if (strpos($email, '@') === false) return false; |
172 | if (strpos($email, '@') === false) return false; |
173 | 173 | ||
174 | $ary = explode('@', $email, 2); |
174 | $ary = explode('@', $email, 2); |
175 | if (!isset($ary[1])) return false; |
175 | if (!isset($ary[1])) return false; |
176 | if (strpos($ary[1], '.') === false) return false; |
176 | if (strpos($ary[1], '.') === false) return false; |
177 | 177 | ||
178 | return true; |
178 | return true; |
179 | } |
179 | } |
180 | 180 | ||
181 | public function softCorrectEMail($email, $params) { |
181 | public function softCorrectEMail($email, $params) { |
- | 182 | $params = $this->init_params($params); |
|
- | 183 | ||
182 | $email = str_replace(' ', '', $email); |
184 | $email = str_replace(' ', '', $email); |
183 | $email = str_replace('&', '@', $email); |
185 | $email = str_replace('&', '@', $email); |
184 | $email = str_replace('(at)', '@', $email); |
186 | $email = str_replace('(at)', '@', $email); |
185 | $email = str_replace('[at]', '@', $email); |
187 | $email = str_replace('[at]', '@', $email); |
186 | $email = str_replace('(dot)', '.', $email); |
188 | $email = str_replace('(dot)', '.', $email); |
187 | $email = str_replace('[dot]', '.', $email); |
189 | $email = str_replace('[dot]', '.', $email); |
188 | $email = trim($email); |
190 | $email = trim($email); |
189 | 191 | ||
190 | if (!$params['allow_illegal_email'] && !self::eMailValid($email)) { |
192 | if (!$params['allow_illegal_email'] && !self::eMailValid($email)) { |
191 | return ''; |
193 | return ''; |
192 | } |
194 | } |
193 | 195 | ||
194 | return $email; |
196 | return $email; |
195 | } |
197 | } |
196 | 198 | ||
197 | public function softCorrectPhone($phone, $params) { |
199 | public function softCorrectPhone($phone, $params) { |
- | 200 | $params = $this->init_params($params); |
|
- | 201 | ||
198 | // TODO: if no "+", add "+1" , but only if address is in USA |
202 | // TODO: if no "+", add "+1" , but only if address is in USA |
199 | // TODO: or use param to fixate country if it is not known |
203 | // TODO: or use param to fixate country if it is not known |
200 | /* |
204 | /* |
201 | NOTE: with german phone numbers, this will cause trouble, even if we assume "+49" |
205 | NOTE: with german phone numbers, this will cause trouble, even if we assume "+49" |
202 | 06223 / 1234 |
206 | 06223 / 1234 |
203 | shall be |
207 | shall be |
204 | +49 6223 1234 |
208 | +49 6223 1234 |
205 | and not |
209 | and not |
206 | +49 06223 1234 |
210 | +49 06223 1234 |
207 | */ |
211 | */ |
208 | 212 | ||
209 | $phone = str_replace('-', ' ', $phone); |
213 | $phone = str_replace('-', ' ', $phone); |
210 | $phone = str_replace('.', ' ', $phone); |
214 | $phone = str_replace('.', ' ', $phone); |
211 | $phone = str_replace('/', ' ', $phone); |
215 | $phone = str_replace('/', ' ', $phone); |
212 | $phone = str_replace('(', ' ', $phone); |
216 | $phone = str_replace('(', ' ', $phone); |
213 | $phone = str_replace(')', ' ', $phone); |
217 | $phone = str_replace(')', ' ', $phone); |
214 | 218 | ||
215 | // HL7 registry has included this accidently |
219 | // HL7 registry has included this accidently |
216 | $phone = str_replace('"', '', $phone); |
220 | $phone = str_replace('"', '', $phone); |
217 | 221 | ||
218 | $phone = trim($phone); |
222 | $phone = trim($phone); |
219 | 223 | ||
220 | return $phone; |
224 | return $phone; |
221 | } |
225 | } |
222 | 226 | ||
223 | private static function strip_to_xhtml_light($str, $allow_strong_text=false) { |
227 | private static function strip_to_xhtml_light($str, $allow_strong_text=false) { |
224 | // <strong> is allowed in the XSD, but not <b> |
228 | // <strong> is allowed in the XSD, but not <b> |
225 | $str = str_ireplace('<b>', '<strong>', $str); |
229 | $str = str_ireplace('<b>', '<strong>', $str); |
226 | $str = str_ireplace('</b>', '</strong>', $str); |
230 | $str = str_ireplace('</b>', '</strong>', $str); |
227 | 231 | ||
228 | if (!$allow_strong_text) { |
232 | if (!$allow_strong_text) { |
229 | // <strong> is only used for very important things like the word "deprecated". It should therefore not used for anything else |
233 | // <strong> is only used for very important things like the word "deprecated". It should therefore not used for anything else |
230 | $str = str_ireplace('<strong>', '', $str); |
234 | $str = str_ireplace('<strong>', '', $str); |
231 | $str = str_ireplace('</strong>', '', $str); |
235 | $str = str_ireplace('</strong>', '', $str); |
232 | } |
236 | } |
233 | 237 | ||
234 | $str = preg_replace('@<\s*script.+<\s*/script.*>@isU', '', $str); |
238 | $str = preg_replace('@<\s*script.+<\s*/script.*>@isU', '', $str); |
235 | $str = preg_replace('@<\s*style.+<\s*/style.*>@isU', '', $str); |
239 | $str = preg_replace('@<\s*style.+<\s*/style.*>@isU', '', $str); |
236 | 240 | ||
237 | $str = preg_replace_callback( |
241 | $str = preg_replace_callback( |
238 | '@<(\s*/{0,1}\d*)([^\s/>]+)(\s*[^>]*)>@i', |
242 | '@<(\s*/{0,1}\d*)([^\s/>]+)(\s*[^>]*)>@i', |
239 | function ($treffer) { |
243 | function ($treffer) { |
240 | // see http://oid-info.com/xhtml-light.xsd |
244 | // see http://oid-info.com/xhtml-light.xsd |
241 | $whitelist = array('a', 'br', 'code', 'em', 'font', 'img', 'li', 'strong', 'sub', 'sup', 'ul'); |
245 | $whitelist = array('a', 'br', 'code', 'em', 'font', 'img', 'li', 'strong', 'sub', 'sup', 'ul'); |
242 | 246 | ||
243 | $pre = $treffer[1]; |
247 | $pre = $treffer[1]; |
244 | $tag = $treffer[2]; |
248 | $tag = $treffer[2]; |
245 | $attrib = $treffer[3]; |
249 | $attrib = $treffer[3]; |
246 | if (in_array($tag, $whitelist)) { |
250 | if (in_array($tag, $whitelist)) { |
247 | return '<'.$pre.$tag.$attrib.'>'; |
251 | return '<'.$pre.$tag.$attrib.'>'; |
248 | } else { |
252 | } else { |
249 | return ''; |
253 | return ''; |
250 | } |
254 | } |
251 | }, $str); |
255 | }, $str); |
252 | 256 | ||
253 | return $str; |
257 | return $str; |
254 | } |
258 | } |
255 | 259 | ||
256 | const OIDINFO_CORRECT_DESC_OPTIONAL_ENDING_DOT = 0; |
260 | const OIDINFO_CORRECT_DESC_OPTIONAL_ENDING_DOT = 0; |
257 | const OIDINFO_CORRECT_DESC_ENFORCE_ENDING_DOT = 1; |
261 | const OIDINFO_CORRECT_DESC_ENFORCE_ENDING_DOT = 1; |
258 | const OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT = 2; |
262 | const OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT = 2; |
259 | 263 | ||
260 | public function correctDesc($desc, $params, $ending_dot_policy=self::OIDINFO_CORRECT_DESC_OPTIONAL_ENDING_DOT, $enforce_xhtml_light=false) { |
264 | public function correctDesc($desc, $params, $ending_dot_policy=self::OIDINFO_CORRECT_DESC_OPTIONAL_ENDING_DOT, $enforce_xhtml_light=false) { |
- | 265 | $params = $this->init_params($params); |
|
- | 266 | ||
261 | $desc = trim($desc); |
267 | $desc = trim($desc); |
262 | 268 | ||
263 | $desc = preg_replace('@<!\\[CDATA\\[(.+)\\]\\]>@ismU', '\\1', $desc); |
269 | $desc = preg_replace('@<!\\[CDATA\\[(.+)\\]\\]>@ismU', '\\1', $desc); |
264 | 270 | ||
265 | if (substr_count($desc, '>') != substr_count($desc, '<')) { |
271 | if (substr_count($desc, '>') != substr_count($desc, '<')) { |
266 | $params['allow_html'] = false; |
272 | $params['allow_html'] = false; |
267 | } |
273 | } |
268 | 274 | ||
269 | $desc = str_replace("\r", '', $desc); |
275 | $desc = str_replace("\r", '', $desc); |
270 | 276 | ||
271 | if (!$params['allow_html']) { |
277 | if (!$params['allow_html']) { |
272 | // htmlentities_numeric() does this for us |
278 | // htmlentities_numeric() does this for us |
273 | /* |
279 | /* |
274 | $desc = str_replace('&', '&', $desc); |
280 | $desc = str_replace('&', '&', $desc); |
275 | $desc = str_replace('<', '<', $desc); |
281 | $desc = str_replace('<', '<', $desc); |
276 | $desc = str_replace('>', '>', $desc); |
282 | $desc = str_replace('>', '>', $desc); |
277 | $desc = str_replace('"', '"', $desc); |
283 | $desc = str_replace('"', '"', $desc); |
278 | $desc = str_replace("'", ''', $desc); // ' is not HTML. It is XML |
284 | $desc = str_replace("'", ''', $desc); // ' is not HTML. It is XML |
279 | */ |
285 | */ |
280 | 286 | ||
281 | $desc = str_replace("\n", '<br />', $desc); |
287 | $desc = str_replace("\n", '<br />', $desc); |
282 | } else { |
288 | } else { |
283 | // Some problems we had with HL7 registry |
289 | // Some problems we had with HL7 registry |
284 | $desc = preg_replace('@<(/{0,1}(p|i|b|u|ul|li))>@ismU', '<\\1>', $desc); |
290 | $desc = preg_replace('@<(/{0,1}(p|i|b|u|ul|li))>@ismU', '<\\1>', $desc); |
285 | # preg_match_all('@<[^ :\\@]+>@ismU', $desc, $m); |
291 | # preg_match_all('@<[^ :\\@]+>@ismU', $desc, $m); |
286 | # if (count($m[0]) > 0) print_r($m); |
292 | # if (count($m[0]) > 0) print_r($m); |
287 | 293 | ||
288 | $desc = preg_replace('@<i>(.+)<i/>@ismU', '<i>\\1</i>', $desc); |
294 | $desc = preg_replace('@<i>(.+)<i/>@ismU', '<i>\\1</i>', $desc); |
289 | $desc = str_replace('<p><p>', '</p><p>', $desc); |
295 | $desc = str_replace('<p><p>', '</p><p>', $desc); |
290 | 296 | ||
291 | // <p> are not supported by oid-info.com |
297 | // <p> are not supported by oid-info.com |
292 | $desc = str_replace('<p>', '<br /><br />', $desc); |
298 | $desc = str_replace('<p>', '<br /><br />', $desc); |
293 | $desc = str_replace('</p>', '', $desc); |
299 | $desc = str_replace('</p>', '', $desc); |
294 | } |
300 | } |
295 | 301 | ||
296 | // Escape unicode characters as numeric &#...; |
302 | // Escape unicode characters as numeric &#...; |
297 | // The XML 1.0 standard does only has a few entities, but nothing like e.g. € , so we prefer numeric |
303 | // The XML 1.0 standard does only has a few entities, but nothing like e.g. € , so we prefer numeric |
298 | 304 | ||
299 | //$desc = htmlentities_numeric($desc, $params['allow_html']); |
305 | //$desc = htmlentities_numeric($desc, $params['allow_html']); |
300 | if (!$params['allow_html']) $desc = htmlentities($desc); |
306 | if (!$params['allow_html']) $desc = htmlentities($desc); |
301 | $desc = html_named_to_numeric_entities($desc); |
307 | $desc = html_named_to_numeric_entities($desc); |
302 | 308 | ||
303 | // Remove HTML tags which are not allowed |
309 | // Remove HTML tags which are not allowed |
304 | if ($params['allow_html'] && (!$params['ignore_xhtml_light']) && $enforce_xhtml_light) { |
310 | if ($params['allow_html'] && (!$params['ignore_xhtml_light']) && $enforce_xhtml_light) { |
305 | // oid-info.com does only allow a few HTML tags |
311 | // oid-info.com does only allow a few HTML tags |
306 | // see http://oid-info.com/xhtml-light.xsd |
312 | // see http://oid-info.com/xhtml-light.xsd |
307 | $desc = self::strip_to_xhtml_light($desc); |
313 | $desc = self::strip_to_xhtml_light($desc); |
308 | } |
314 | } |
309 | 315 | ||
310 | // Solve some XML problems... |
316 | // Solve some XML problems... |
311 | $desc = preg_replace('@<\s*br\s*>@ismU', '<br/>', $desc); // auto close <br> |
317 | $desc = preg_replace('@<\s*br\s*>@ismU', '<br/>', $desc); // auto close <br> |
312 | $desc = preg_replace('@(href\s*=\s*)(["\'])(.*)&([^#].*)(\2)@ismU', '\1\2\3&\4\5', $desc); // fix "&" inside href-URLs to & |
318 | $desc = preg_replace('@(href\s*=\s*)(["\'])(.*)&([^#].*)(\2)@ismU', '\1\2\3&\4\5', $desc); // fix "&" inside href-URLs to & |
313 | // TODO: what do we do if there are more XHTML errors (e.g. additional open tags) which would make the XML invalid? |
319 | // TODO: what do we do if there are more XHTML errors (e.g. additional open tags) which would make the XML invalid? |
314 | 320 | ||
315 | // "Trim" <br/> |
321 | // "Trim" <br/> |
316 | $count = 0; |
322 | $count = 0; |
317 | do { $desc = preg_replace('@^\s*<\s*br\s*/{0,1}\s*>@isU', '', $desc, -1, $count); } while ($count > 0); // left trim |
323 | do { $desc = preg_replace('@^\s*<\s*br\s*/{0,1}\s*>@isU', '', $desc, -1, $count); } while ($count > 0); // left trim |
318 | do { $desc = preg_replace('@<\s*br\s*/{0,1}\s*>\s*$@isU', '', $desc, -1, $count); } while ($count > 0); // right trim |
324 | do { $desc = preg_replace('@<\s*br\s*/{0,1}\s*>\s*$@isU', '', $desc, -1, $count); } while ($count > 0); // right trim |
319 | 325 | ||
320 | // Correct double-encoded stuff |
326 | // Correct double-encoded stuff |
321 | if (!isset($params['tolerant_htmlentities']) || $params['tolerant_htmlentities']) { |
327 | if (!isset($params['tolerant_htmlentities']) || $params['tolerant_htmlentities']) { |
322 | do { |
328 | do { |
323 | $old_desc = $desc; |
329 | $old_desc = $desc; |
324 | # Full list of entities: https://www.freeformatter.com/html-entities.html |
330 | # Full list of entities: https://www.freeformatter.com/html-entities.html |
325 | # Max: 8 chars ( ϑ ) |
331 | # Max: 8 chars ( ϑ ) |
326 | # Min: 2 chars ( lt,gt,ni,or,ne,le,ge,Mu,Nu,Xi,Pi,mu,nu,xi,pi ) |
332 | # Min: 2 chars ( lt,gt,ni,or,ne,le,ge,Mu,Nu,Xi,Pi,mu,nu,xi,pi ) |
327 | $desc = preg_replace('@(&|&)(#|#)(\d+);@ismU', '&#\3;', $desc); |
333 | $desc = preg_replace('@(&|&)(#|#)(\d+);@ismU', '&#\3;', $desc); |
328 | $desc = preg_replace('@(&|&)([a-zA-Z0-9]{2,8});@ismU', '&\2;', $desc); |
334 | $desc = preg_replace('@(&|&)([a-zA-Z0-9]{2,8});@ismU', '&\2;', $desc); |
329 | } while ($old_desc != $desc); |
335 | } while ($old_desc != $desc); |
330 | } |
336 | } |
331 | 337 | ||
332 | // TODO: use the complete list of oid-info.com |
338 | // TODO: use the complete list of oid-info.com |
333 | // TODO: Make this step optional using $params |
339 | // TODO: Make this step optional using $params |
334 | /* |
340 | /* |
335 | Array |
341 | Array |
336 | ( |
342 | ( |
337 | [0] => Root OID for |
343 | [0] => Root OID for |
338 | [1] => OID for |
344 | [1] => OID for |
339 | [2] => OID identifying |
345 | [2] => OID identifying |
340 | [3] => Top arc for |
346 | [3] => Top arc for |
341 | [4] => Arc for |
347 | [4] => Arc for |
342 | [5] => arc root |
348 | [5] => arc root |
343 | [6] => Node for |
349 | [6] => Node for |
344 | [7] => Leaf node for |
350 | [7] => Leaf node for |
345 | [8] => This OID describes |
351 | [8] => This OID describes |
346 | [9] => [tT]his oid |
352 | [9] => [tT]his oid |
347 | [10] => This arc describes |
353 | [10] => This arc describes |
348 | [11] => This identifies |
354 | [11] => This identifies |
349 | [12] => Identifies a |
355 | [12] => Identifies a |
350 | [13] => [Oo]bject [Ii]dentifier |
356 | [13] => [Oo]bject [Ii]dentifier |
351 | [14] => Identifier for |
357 | [14] => Identifier for |
352 | [15] => This [Ii]dentifier is for |
358 | [15] => This [Ii]dentifier is for |
353 | [16] => Identifiers used by |
359 | [16] => Identifiers used by |
354 | [17] => identifier$ |
360 | [17] => identifier$ |
355 | [18] => This branch |
361 | [18] => This branch |
356 | [19] => Branch for |
362 | [19] => Branch for |
357 | [20] => Child tree for |
363 | [20] => Child tree for |
358 | [21] => Child for |
364 | [21] => Child for |
359 | [22] => Subtree for |
365 | [22] => Subtree for |
360 | [23] => Sub-OID |
366 | [23] => Sub-OID |
361 | [24] => Tree for |
367 | [24] => Tree for |
362 | [25] => Child object |
368 | [25] => Child object |
363 | [26] => Parent OID |
369 | [26] => Parent OID |
364 | [27] => root for |
370 | [27] => root for |
365 | [28] => Assigned for |
371 | [28] => Assigned for |
366 | [29] => Used to identify |
372 | [29] => Used to identify |
367 | [30] => Used in |
373 | [30] => Used in |
368 | [31] => Used for |
374 | [31] => Used for |
369 | [32] => For use by |
375 | [32] => For use by |
370 | [33] => Entry for |
376 | [33] => Entry for |
371 | [34] => This is for |
377 | [34] => This is for |
372 | [35] => ["]?OID["]? |
378 | [35] => ["]?OID["]? |
373 | [36] => ^OID |
379 | [36] => ^OID |
374 | [37] => OID$ |
380 | [37] => OID$ |
375 | [38] => oid |
381 | [38] => oid |
376 | [39] => oid$ |
382 | [39] => oid$ |
377 | [40] => OIDs |
383 | [40] => OIDs |
378 | ) |
384 | ) |
379 | $x = 'Root OID for ; OID for ; OID identifying ; Top arc for ; Arc for ; arc root; Node for ; Leaf node for ; This OID describes ; [tT]his oid ; This arc describes ; This identifies ; Identifies a ; [Oo]bject [Ii]dentifier; Identifier for ; This [Ii]dentifier is for ; Identifiers used by ; identifier$; This branch ; Branch for ; Child tree for ; Child for ; Subtree for ; Sub-OID; Tree for ; Child object; Parent OID; root for ; Assigned for ; Used to identify ; Used in ; Used for ; For use by ; Entry for ; This is for ; ["]?OID["]? ; ^OID ; OID$; oid ; oid$; OIDs'; |
385 | $x = 'Root OID for ; OID for ; OID identifying ; Top arc for ; Arc for ; arc root; Node for ; Leaf node for ; This OID describes ; [tT]his oid ; This arc describes ; This identifies ; Identifies a ; [Oo]bject [Ii]dentifier; Identifier for ; This [Ii]dentifier is for ; Identifiers used by ; identifier$; This branch ; Branch for ; Child tree for ; Child for ; Subtree for ; Sub-OID; Tree for ; Child object; Parent OID; root for ; Assigned for ; Used to identify ; Used in ; Used for ; For use by ; Entry for ; This is for ; ["]?OID["]? ; ^OID ; OID$; oid ; oid$; OIDs'; |
380 | $ary = explode('; ', $x); |
386 | $ary = explode('; ', $x); |
381 | print_r($ary); |
387 | print_r($ary); |
382 | */ |
388 | */ |
383 | $desc = preg_replace("@^Root OID for the @i", '', $desc); |
389 | $desc = preg_replace("@^Root OID for the @i", '', $desc); |
384 | $desc = preg_replace("@^Root OID for @i", '', $desc); |
390 | $desc = preg_replace("@^Root OID for @i", '', $desc); |
385 | $desc = preg_replace("@^OID root for the @i", '', $desc); |
391 | $desc = preg_replace("@^OID root for the @i", '', $desc); |
386 | $desc = preg_replace("@^OID root for @i", '', $desc); |
392 | $desc = preg_replace("@^OID root for @i", '', $desc); |
387 | $desc = preg_replace("@^This OID will be used for @i", '', $desc); |
393 | $desc = preg_replace("@^This OID will be used for @i", '', $desc); |
388 | $desc = preg_replace("@^This will be a generic OID for the @i", '', $desc); |
394 | $desc = preg_replace("@^This will be a generic OID for the @i", '', $desc); |
389 | $desc = preg_replace("@^OID for @i", '', $desc); |
395 | $desc = preg_replace("@^OID for @i", '', $desc); |
390 | $desc = preg_replace("@ Root OID$@i", '', $desc); |
396 | $desc = preg_replace("@ Root OID$@i", '', $desc); |
391 | $desc = preg_replace("@ OID$@i", '', $desc); |
397 | $desc = preg_replace("@ OID$@i", '', $desc); |
392 | $desc = preg_replace("@ OID Namespace$@i", '', $desc); |
398 | $desc = preg_replace("@ OID Namespace$@i", '', $desc); |
393 | $desc = preg_replace("@^OID for @i", '', $desc); |
399 | $desc = preg_replace("@^OID for @i", '', $desc); |
394 | 400 | ||
395 | $desc = rtrim($desc); |
401 | $desc = rtrim($desc); |
396 | if ($ending_dot_policy == self::OIDINFO_CORRECT_DESC_ENFORCE_ENDING_DOT) { |
402 | if ($ending_dot_policy == self::OIDINFO_CORRECT_DESC_ENFORCE_ENDING_DOT) { |
397 | if (($desc != '') && (substr($desc, -1)) != '.') $desc .= '.'; |
403 | if (($desc != '') && (substr($desc, -1)) != '.') $desc .= '.'; |
398 | } else if ($ending_dot_policy == self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT) { |
404 | } else if ($ending_dot_policy == self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT) { |
399 | $desc = preg_replace('@\\.$@', '', $desc); |
405 | $desc = preg_replace('@\\.$@', '', $desc); |
400 | } |
406 | } |
401 | 407 | ||
402 | return $desc; |
408 | return $desc; |
403 | } |
409 | } |
404 | 410 | ||
405 | public function xmlAddHeader($firstName, $lastName, $email) { |
411 | public function xmlAddHeader($firstName, $lastName, $email) { |
406 | // TODO: encode |
412 | // TODO: encode |
407 | 413 | ||
408 | $firstName = htmlentities_numeric($firstName); |
414 | $firstName = htmlentities_numeric($firstName); |
409 | if (empty($firstName)) { |
415 | if (empty($firstName)) { |
410 | throw new OIDInfoException("Please supply a first name"); |
416 | throw new OIDInfoException("Please supply a first name"); |
411 | } |
417 | } |
412 | 418 | ||
413 | $lastName = htmlentities_numeric($lastName); |
419 | $lastName = htmlentities_numeric($lastName); |
414 | if (empty($lastName)) { |
420 | if (empty($lastName)) { |
415 | throw new OIDInfoException("Please supply a last name"); |
421 | throw new OIDInfoException("Please supply a last name"); |
416 | } |
422 | } |
417 | 423 | ||
418 | $email = htmlentities_numeric($email); |
424 | $email = htmlentities_numeric($email); |
419 | if (empty($email)) { |
425 | if (empty($email)) { |
420 | throw new OIDInfoException("Please supply an email address"); |
426 | throw new OIDInfoException("Please supply an email address"); |
421 | } |
427 | } |
422 | 428 | ||
423 | // $out = "<!DOCTYPE oid-database>\n\n"; |
429 | // $out = "<!DOCTYPE oid-database>\n\n"; |
424 | $out = '<?xml version="1.0" encoding="UTF-8" ?>'."\n"; |
430 | $out = '<?xml version="1.0" encoding="UTF-8" ?>'."\n"; |
425 | $out .= '<oid-database xmlns="http://oid-info.com"'."\n"; |
431 | $out .= '<oid-database xmlns="http://oid-info.com"'."\n"; |
426 | $out .= ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n"; |
432 | $out .= ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n"; |
427 | $out .= ' xsi:schemaLocation="http://oid-info.com '."\n"; |
433 | $out .= ' xsi:schemaLocation="http://oid-info.com '."\n"; |
428 | $out .= ' http://oid-info.com/oid.xsd">'."\n"; |
434 | $out .= ' http://oid-info.com/oid.xsd">'."\n"; |
429 | $out .= "\t<submitter>\n"; |
435 | $out .= "\t<submitter>\n"; |
430 | $out .= "\t\t<first-name>$firstName</first-name>\n"; |
436 | $out .= "\t\t<first-name>$firstName</first-name>\n"; |
431 | $out .= "\t\t<last-name>$lastName</last-name>\n"; |
437 | $out .= "\t\t<last-name>$lastName</last-name>\n"; |
432 | $out .= "\t\t<email>$email</email>\n"; |
438 | $out .= "\t\t<email>$email</email>\n"; |
433 | $out .= "\t</submitter>\n"; |
439 | $out .= "\t</submitter>\n"; |
434 | 440 | ||
435 | if (!self::eMailValid($email)) { |
441 | if (!self::eMailValid($email)) { |
436 | throw new OIDInfoException("eMail address '$email' is invalid"); |
442 | throw new OIDInfoException("eMail address '$email' is invalid"); |
437 | } |
443 | } |
438 | 444 | ||
439 | return $out; |
445 | return $out; |
440 | } |
446 | } |
441 | 447 | ||
442 | public function xmlAddFooter() { |
448 | public function xmlAddFooter() { |
443 | return "</oid-database>\n"; |
449 | return "</oid-database>\n"; |
444 | } |
450 | } |
445 | 451 | ||
446 | /* |
452 | /* |
447 | -- CODE TEMPLATE -- |
453 | -- CODE TEMPLATE -- |
448 | 454 | ||
449 | $params['allow_html'] = false; // Allow HTML in <description> and <information> |
455 | $params['allow_html'] = false; // Allow HTML in <description> and <information> |
450 | $params['allow_illegal_email'] = true; // We should allow it, because we don't know if the user has some kind of human-readable anti-spam technique |
456 | $params['allow_illegal_email'] = true; // We should allow it, because we don't know if the user has some kind of human-readable anti-spam technique |
451 | $params['soft_correct_behavior'] = OIDInfoAPI::SOFT_CORRECT_BEHAVIOR_NONE; |
457 | $params['soft_correct_behavior'] = OIDInfoAPI::SOFT_CORRECT_BEHAVIOR_NONE; |
452 | $params['do_online_check'] = false; // Flag to disable this online check, because it generates a lot of traffic and runtime. |
458 | $params['do_online_check'] = false; // Flag to disable this online check, because it generates a lot of traffic and runtime. |
453 | $params['do_illegality_check'] = true; |
459 | $params['do_illegality_check'] = true; |
454 | $params['do_simpleping_check'] = true; |
460 | $params['do_simpleping_check'] = true; |
455 | $params['auto_extract_name'] = ''; |
461 | $params['auto_extract_name'] = ''; |
456 | $params['auto_extract_url'] = ''; |
462 | $params['auto_extract_url'] = ''; |
457 | $params['always_output_comment'] = false; // Also output comment if there was an error (e.g. OID already existing) |
463 | $params['always_output_comment'] = false; // Also output comment if there was an error (e.g. OID already existing) |
458 | $params['creation_allowed_check'] = true; |
464 | $params['creation_allowed_check'] = true; |
459 | $params['tolerant_htmlentities'] = true; |
465 | $params['tolerant_htmlentities'] = true; |
460 | $params['ignore_xhtml_light'] = false; |
466 | $params['ignore_xhtml_light'] = false; |
461 | 467 | ||
462 | $elements['synonymous-identifier'] = ''; // string or array |
468 | $elements['synonymous-identifier'] = ''; // string or array |
463 | $elements['description'] = ''; |
469 | $elements['description'] = ''; |
464 | $elements['information'] = ''; |
470 | $elements['information'] = ''; |
465 | 471 | ||
466 | $elements['first-registrant']['first-name'] = ''; |
472 | $elements['first-registrant']['first-name'] = ''; |
467 | $elements['first-registrant']['last-name'] = ''; |
473 | $elements['first-registrant']['last-name'] = ''; |
468 | $elements['first-registrant']['address'] = ''; |
474 | $elements['first-registrant']['address'] = ''; |
469 | $elements['first-registrant']['email'] = ''; |
475 | $elements['first-registrant']['email'] = ''; |
470 | $elements['first-registrant']['phone'] = ''; |
476 | $elements['first-registrant']['phone'] = ''; |
471 | $elements['first-registrant']['fax'] = ''; |
477 | $elements['first-registrant']['fax'] = ''; |
472 | $elements['first-registrant']['creation-date'] = ''; |
478 | $elements['first-registrant']['creation-date'] = ''; |
473 | 479 | ||
474 | $elements['current-registrant']['first-name'] = ''; |
480 | $elements['current-registrant']['first-name'] = ''; |
475 | $elements['current-registrant']['last-name'] = ''; |
481 | $elements['current-registrant']['last-name'] = ''; |
476 | $elements['current-registrant']['address'] = ''; |
482 | $elements['current-registrant']['address'] = ''; |
477 | $elements['current-registrant']['email'] = ''; |
483 | $elements['current-registrant']['email'] = ''; |
478 | $elements['current-registrant']['phone'] = ''; |
484 | $elements['current-registrant']['phone'] = ''; |
479 | $elements['current-registrant']['fax'] = ''; |
485 | $elements['current-registrant']['fax'] = ''; |
480 | $elements['current-registrant']['modification-date'] = ''; |
486 | $elements['current-registrant']['modification-date'] = ''; |
481 | 487 | ||
482 | $oid = '1.2.3'; |
488 | $oid = '1.2.3'; |
483 | 489 | ||
484 | $comment = 'test'; |
490 | $comment = 'test'; |
485 | 491 | ||
486 | echo $oa->createXMLEntry($oid, $elements, $params, $comment); |
492 | echo $oa->createXMLEntry($oid, $elements, $params, $comment); |
487 | */ |
493 | */ |
488 | public function createXMLEntry($oid, $elements, $params, $comment='') { |
494 | private function init_params($params) { |
489 | // Backward compatibility |
495 | // Backward compatibility |
490 | if (!isset($params['do_csv_check'])) $params['do_simpleping_check'] = true; |
496 | if (!isset($params['do_csv_check'])) $params['do_simpleping_check'] = true; |
491 | 497 | ||
492 | // Set default behavior |
498 | // Set default behavior |
493 | if (!isset($params['allow_html'])) $params['allow_html'] = false; // Allow HTML in <description> and <information> |
499 | if (!isset($params['allow_html'])) $params['allow_html'] = false; // Allow HTML in <description> and <information> |
494 | if (!isset($params['allow_illegal_email'])) $params['allow_illegal_email'] = true; // We should allow it, because we don't know if the user has some kind of human-readable anti-spam technique |
500 | if (!isset($params['allow_illegal_email'])) $params['allow_illegal_email'] = true; // We should allow it, because we don't know if the user has some kind of human-readable anti-spam technique |
495 | if (!isset($params['soft_correct_behavior'])) $params['soft_correct_behavior'] = self::SOFT_CORRECT_BEHAVIOR_NONE; |
501 | if (!isset($params['soft_correct_behavior'])) $params['soft_correct_behavior'] = self::SOFT_CORRECT_BEHAVIOR_NONE; |
496 | if (!isset($params['do_online_check'])) $params['do_online_check'] = false; // Flag to disable this online check, because it generates a lot of traffic and runtime. |
502 | if (!isset($params['do_online_check'])) $params['do_online_check'] = false; // Flag to disable this online check, because it generates a lot of traffic and runtime. |
497 | if (!isset($params['do_illegality_check'])) $params['do_illegality_check'] = true; |
503 | if (!isset($params['do_illegality_check'])) $params['do_illegality_check'] = true; |
498 | if (!isset($params['do_simpleping_check'])) $params['do_simpleping_check'] = true; |
504 | if (!isset($params['do_simpleping_check'])) $params['do_simpleping_check'] = true; |
499 | if (!isset($params['auto_extract_name'])) $params['auto_extract_name'] = ''; |
505 | if (!isset($params['auto_extract_name'])) $params['auto_extract_name'] = ''; |
500 | if (!isset($params['auto_extract_url'])) $params['auto_extract_url'] = ''; |
506 | if (!isset($params['auto_extract_url'])) $params['auto_extract_url'] = ''; |
501 | if (!isset($params['always_output_comment'])) $params['always_output_comment'] = false; // Also output comment if there was an error (e.g. OID already existing) |
507 | if (!isset($params['always_output_comment'])) $params['always_output_comment'] = false; // Also output comment if there was an error (e.g. OID already existing) |
502 | if (!isset($params['creation_allowed_check'])) $params['creation_allowed_check'] = true; |
508 | if (!isset($params['creation_allowed_check'])) $params['creation_allowed_check'] = true; |
503 | if (!isset($params['tolerant_htmlentities'])) $params['tolerant_htmlentities'] = true; |
509 | if (!isset($params['tolerant_htmlentities'])) $params['tolerant_htmlentities'] = true; |
504 | if (!isset($params['ignore_xhtml_light'])) $params['ignore_xhtml_light'] = false; |
510 | if (!isset($params['ignore_xhtml_light'])) $params['ignore_xhtml_light'] = false; |
505 | 511 | ||
- | 512 | return $params; |
|
- | 513 | } |
|
- | 514 | public function createXMLEntry($oid, $elements, $params, $comment='') { |
|
- | 515 | $params = $this->init_params($params); |
|
- | 516 | ||
506 | $out = ''; |
517 | $out = ''; |
507 | if (!empty($comment)) $out .= "\t\t<!-- $comment -->\n"; |
518 | if (!empty($comment)) $out .= "\t\t<!-- $comment -->\n"; |
508 | 519 | ||
509 | if ($params['always_output_comment']) { |
520 | if ($params['always_output_comment']) { |
510 | $err = $out; |
521 | $err = $out; |
511 | } else { |
522 | } else { |
512 | $err = false; |
523 | $err = false; |
513 | } |
524 | } |
514 | 525 | ||
515 | if (isset($elements['dotted_oid'])) { |
526 | if (isset($elements['dotted_oid'])) { |
516 | throw new OIDInfoException("'dotted_oid' in the \$elements array is not supported. Please use the \$oid argument."); |
527 | throw new OIDInfoException("'dotted_oid' in the \$elements array is not supported. Please use the \$oid argument."); |
517 | } |
528 | } |
518 | if (isset($elements['value'])) { |
529 | if (isset($elements['value'])) { |
519 | // TODO: WHAT SHOULD WE DO WITH THAT? |
530 | // TODO: WHAT SHOULD WE DO WITH THAT? |
520 | throw new OIDInfoException("'value' in the \$elements array is currently not supported."); |
531 | throw new OIDInfoException("'value' in the \$elements array is currently not supported."); |
521 | } |
532 | } |
522 | 533 | ||
523 | $bak_oid = $oid; |
534 | $bak_oid = $oid; |
524 | $oid = self::trySanitizeOID($oid); |
535 | $oid = self::trySanitizeOID($oid); |
525 | if ($oid === false) { |
536 | if ($oid === false) { |
526 | fwrite(STDOUT/*STDERR*/,"<!-- ERROR: Ignored '$bak_oid', because it is not a valid OID -->\n"); |
537 | fwrite(STDOUT/*STDERR*/,"<!-- ERROR: Ignored '$bak_oid', because it is not a valid OID -->\n"); |
527 | return $err; |
538 | return $err; |
528 | } |
539 | } |
529 | 540 | ||
530 | if ($params['creation_allowed_check']) { |
541 | if ($params['creation_allowed_check']) { |
531 | if (!$this->oidMayCreate($oid, $params['do_online_check'], $params['do_simpleping_check'], $params['do_illegality_check'])) { |
542 | if (!$this->oidMayCreate($oid, $params['do_online_check'], $params['do_simpleping_check'], $params['do_illegality_check'])) { |
532 | fwrite(STDOUT/*STDERR*/,"<!-- ERROR: Creation of $oid disallowed -->\n"); |
543 | fwrite(STDOUT/*STDERR*/,"<!-- ERROR: Creation of $oid disallowed -->\n"); |
533 | return $err; |
544 | return $err; |
534 | } |
545 | } |
535 | } else { |
546 | } else { |
536 | if ($params['do_illegality_check'] && ($this->illegalOid($oid))) { |
547 | if ($params['do_illegality_check'] && ($this->illegalOid($oid))) { |
537 | fwrite(STDOUT/*STDERR*/,"<!-- ERROR: Creation of $oid disallowed -->\n"); |
548 | fwrite(STDOUT/*STDERR*/,"<!-- ERROR: Creation of $oid disallowed -->\n"); |
538 | return $err; |
549 | return $err; |
539 | } |
550 | } |
540 | } |
551 | } |
541 | 552 | ||
542 | $elements['description'] = $this->correctDesc($elements['description'], $params, self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT, true); |
553 | $elements['description'] = $this->correctDesc($elements['description'], $params, self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT, true); |
543 | $elements['information'] = $this->correctDesc($elements['information'], $params, self::OIDINFO_CORRECT_DESC_ENFORCE_ENDING_DOT, true); |
554 | $elements['information'] = $this->correctDesc($elements['information'], $params, self::OIDINFO_CORRECT_DESC_ENFORCE_ENDING_DOT, true); |
544 | 555 | ||
545 | // Request by O.D. 26 August 2019 |
556 | // Request by O.D. 26 August 2019 |
546 | $elements['description'] = trim($elements['description']); |
557 | $elements['description'] = trim($elements['description']); |
547 | $m = array(); |
558 | $m = array(); |
548 | if (preg_match('@^[a-z]@', $elements['description'], $m)) { |
559 | if (preg_match('@^[a-z]@', $elements['description'], $m)) { |
549 | $ending_dot_policy = self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT; // for description |
560 | $ending_dot_policy = self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT; // for description |
550 | if (($ending_dot_policy != self::OIDINFO_CORRECT_DESC_ENFORCE_ENDING_DOT) && (strpos($elements['description'], ' ') === false)) { // <-- added by DM |
561 | if (($ending_dot_policy != self::OIDINFO_CORRECT_DESC_ENFORCE_ENDING_DOT) && (strpos($elements['description'], ' ') === false)) { // <-- added by DM |
551 | $elements['description'] = '"' . $elements['description'] . '"'; |
562 | $elements['description'] = '"' . $elements['description'] . '"'; |
552 | } |
563 | } |
553 | } |
564 | } |
554 | // End request by O.D. 26. August 2019 |
565 | // End request by O.D. 26. August 2019 |
555 | 566 | ||
556 | if ($params['auto_extract_name'] || $params['auto_extract_url']) { |
567 | if ($params['auto_extract_name'] || $params['auto_extract_url']) { |
557 | if (!empty($elements['information'])) $elements['information'] .= '<br /><br />'; |
568 | if (!empty($elements['information'])) $elements['information'] .= '<br /><br />'; |
558 | if ($params['auto_extract_name'] || $params['auto_extract_url']) { |
569 | if ($params['auto_extract_name'] || $params['auto_extract_url']) { |
559 | $elements['information'] .= 'Automatically extracted from <a href="'.$params['auto_extract_url'].'">'.$params['auto_extract_name'].'</a>.'; |
570 | $elements['information'] .= 'Automatically extracted from <a href="'.$params['auto_extract_url'].'">'.$params['auto_extract_name'].'</a>.'; |
560 | } else if ($params['auto_extract_name']) { |
571 | } else if ($params['auto_extract_name']) { |
561 | $elements['information'] .= 'Automatically extracted from '.$params['auto_extract_name']; |
572 | $elements['information'] .= 'Automatically extracted from '.$params['auto_extract_name']; |
562 | } else if ($params['auto_extract_url']) { |
573 | } else if ($params['auto_extract_url']) { |
563 | $hr_url = $params['auto_extract_url']; |
574 | $hr_url = $params['auto_extract_url']; |
564 | // $hr_url = preg_replace('@^https{0,1}://@ismU', '', $hr_url); |
575 | // $hr_url = preg_replace('@^https{0,1}://@ismU', '', $hr_url); |
565 | $hr_url = preg_replace('@^http://@ismU', '', $hr_url); |
576 | $hr_url = preg_replace('@^http://@ismU', '', $hr_url); |
566 | $elements['information'] .= 'Automatically extracted from <a href="'.$params['auto_extract_url'].'">'.$hr_url.'</a>.'; |
577 | $elements['information'] .= 'Automatically extracted from <a href="'.$params['auto_extract_url'].'">'.$hr_url.'</a>.'; |
567 | } |
578 | } |
568 | } |
579 | } |
569 | 580 | ||
570 | // Validate ASN.1 ID |
581 | // Validate ASN.1 ID |
571 | if (isset($elements['synonymous-identifier'])) { |
582 | if (isset($elements['synonymous-identifier'])) { |
572 | if (!is_array($elements['synonymous-identifier'])) { |
583 | if (!is_array($elements['synonymous-identifier'])) { |
573 | $elements['synonymous-identifier'] = array($elements['synonymous-identifier']); |
584 | $elements['synonymous-identifier'] = array($elements['synonymous-identifier']); |
574 | } |
585 | } |
575 | foreach ($elements['synonymous-identifier'] as &$synid) { |
586 | foreach ($elements['synonymous-identifier'] as &$synid) { |
576 | if ($synid == '') { |
587 | if ($synid == '') { |
577 | $synid = null; |
588 | $synid = null; |
578 | continue; |
589 | continue; |
579 | } |
590 | } |
580 | 591 | ||
581 | $behavior = $params['soft_correct_behavior']; |
592 | $behavior = $params['soft_correct_behavior']; |
582 | 593 | ||
583 | if ($behavior == self::SOFT_CORRECT_BEHAVIOR_NONE) { |
594 | if ($behavior == self::SOFT_CORRECT_BEHAVIOR_NONE) { |
584 | if (!oid_id_is_valid($synid)) $synid = null; |
595 | if (!oid_id_is_valid($synid)) $synid = null; |
585 | } else if ($behavior == self::SOFT_CORRECT_BEHAVIOR_LOWERCASE_BEGINNING) { |
596 | } else if ($behavior == self::SOFT_CORRECT_BEHAVIOR_LOWERCASE_BEGINNING) { |
586 | $synid[0] = strtolower($synid[0]); |
597 | $synid[0] = strtolower($synid[0]); |
587 | if (!oid_id_is_valid($synid)) $synid = null; |
598 | if (!oid_id_is_valid($synid)) $synid = null; |
588 | } else if ($behavior == self::SOFT_CORRECT_BEHAVIOR_ALL_POSSIBLE) { |
599 | } else if ($behavior == self::SOFT_CORRECT_BEHAVIOR_ALL_POSSIBLE) { |
589 | $synid = oid_soft_correct_id($synid); |
600 | $synid = oid_soft_correct_id($synid); |
590 | // if (!oid_id_is_valid($synid)) $synid = null; |
601 | // if (!oid_id_is_valid($synid)) $synid = null; |
591 | } else { |
602 | } else { |
592 | throw new OIDInfoException("Unexpected soft-correction behavior for ASN.1 IDs"); |
603 | throw new OIDInfoException("Unexpected soft-correction behavior for ASN.1 IDs"); |
593 | } |
604 | } |
594 | } |
605 | } |
595 | } |
606 | } |
596 | 607 | ||
597 | // ATTENTION: the XML-generator will always add <dotted-oid> , but what will happen if additionally an |
608 | // ATTENTION: the XML-generator will always add <dotted-oid> , but what will happen if additionally an |
598 | // asn1-path (<value>) is given? (the resulting OIDs might be inconsistent/mismatch) |
609 | // asn1-path (<value>) is given? (the resulting OIDs might be inconsistent/mismatch) |
599 | if (isset($elements['value']) && (!asn1_path_valid($elements['value']))) { |
610 | if (isset($elements['value']) && (!asn1_path_valid($elements['value']))) { |
600 | unset($elements['value']); |
611 | unset($elements['value']); |
601 | } |
612 | } |
602 | 613 | ||
603 | // Validate IRI (currently not supported by oid-info.com, but the tag name is reserved) |
614 | // Validate IRI (currently not supported by oid-info.com, but the tag name is reserved) |
604 | if (isset($elements['iri'])) { |
615 | if (isset($elements['iri'])) { |
605 | if (!is_array($elements['iri'])) { |
616 | if (!is_array($elements['iri'])) { |
606 | $elements['iri'] = array($elements['iri']); |
617 | $elements['iri'] = array($elements['iri']); |
607 | } |
618 | } |
608 | foreach ($elements['iri'] as &$iri) { |
619 | foreach ($elements['iri'] as &$iri) { |
609 | // Do not allow Numeric-only. It would only be valid in an IRI path, but not in a single identifier |
620 | // Do not allow Numeric-only. It would only be valid in an IRI path, but not in a single identifier |
610 | if (!iri_arc_valid($iri, false)) $iri = null; |
621 | if (!iri_arc_valid($iri, false)) $iri = null; |
611 | } |
622 | } |
612 | } |
623 | } |
613 | 624 | ||
614 | if (isset($elements['first-registrant']['phone'])) |
625 | if (isset($elements['first-registrant']['phone'])) |
615 | $elements['first-registrant']['phone'] = $this->softCorrectPhone($elements['first-registrant']['phone'], $params); |
626 | $elements['first-registrant']['phone'] = $this->softCorrectPhone($elements['first-registrant']['phone'], $params); |
616 | 627 | ||
617 | if (isset($elements['current-registrant']['phone'])) |
628 | if (isset($elements['current-registrant']['phone'])) |
618 | $elements['current-registrant']['phone'] = $this->softCorrectPhone($elements['current-registrant']['phone'], $params); |
629 | $elements['current-registrant']['phone'] = $this->softCorrectPhone($elements['current-registrant']['phone'], $params); |
619 | 630 | ||
620 | if (isset($elements['first-registrant']['fax'])) |
631 | if (isset($elements['first-registrant']['fax'])) |
621 | $elements['first-registrant']['fax'] = $this->softCorrectPhone($elements['first-registrant']['fax'], $params); |
632 | $elements['first-registrant']['fax'] = $this->softCorrectPhone($elements['first-registrant']['fax'], $params); |
622 | 633 | ||
623 | if (isset($elements['current-registrant']['fax'])) |
634 | if (isset($elements['current-registrant']['fax'])) |
624 | $elements['current-registrant']['fax'] = $this->softCorrectPhone($elements['current-registrant']['fax'], $params); |
635 | $elements['current-registrant']['fax'] = $this->softCorrectPhone($elements['current-registrant']['fax'], $params); |
625 | 636 | ||
626 | if (isset($elements['first-registrant']['email'])) |
637 | if (isset($elements['first-registrant']['email'])) |
627 | $elements['first-registrant']['email'] = $this->softCorrectEMail($elements['first-registrant']['email'], $params); |
638 | $elements['first-registrant']['email'] = $this->softCorrectEMail($elements['first-registrant']['email'], $params); |
628 | 639 | ||
629 | if (isset($elements['current-registrant']['email'])) |
640 | if (isset($elements['current-registrant']['email'])) |
630 | $elements['current-registrant']['email'] = $this->softCorrectEMail($elements['current-registrant']['email'], $params); |
641 | $elements['current-registrant']['email'] = $this->softCorrectEMail($elements['current-registrant']['email'], $params); |
631 | 642 | ||
632 | // TODO: if name is empty, but address has 1 line, take it as firstname (but remove hyperlink) |
643 | // TODO: if name is empty, but address has 1 line, take it as firstname (but remove hyperlink) |
633 | 644 | ||
634 | $out_loc = ''; |
645 | $out_loc = ''; |
635 | foreach ($elements as $name => $val) { |
646 | foreach ($elements as $name => $val) { |
636 | if (($name == 'first-registrant') || ($name == 'current-registrant')) { |
647 | if (($name == 'first-registrant') || ($name == 'current-registrant')) { |
637 | $out_loc2 = ''; |
648 | $out_loc2 = ''; |
638 | foreach ($val as $name2 => $val2) { |
649 | foreach ($val as $name2 => $val2) { |
639 | if (is_null($val2)) continue; |
650 | if (is_null($val2)) continue; |
640 | if (empty($val2)) continue; |
651 | if (empty($val2)) continue; |
641 | 652 | ||
642 | if (!is_array($val2)) $val2 = array($val2); |
653 | if (!is_array($val2)) $val2 = array($val2); |
643 | 654 | ||
644 | foreach ($val2 as $val3) { |
655 | foreach ($val2 as $val3) { |
645 | // if (is_null($val3)) continue; |
656 | // if (is_null($val3)) continue; |
646 | if (empty($val3)) continue; |
657 | if (empty($val3)) continue; |
647 | 658 | ||
648 | if ($name2 == 'address') { |
659 | if ($name2 == 'address') { |
649 | // $val3 = htmlentities_numeric($val3); |
660 | // $val3 = htmlentities_numeric($val3); |
650 | $val3 = $this->correctDesc($val3, $params, self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT, true); |
661 | $val3 = $this->correctDesc($val3, $params, self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT, true); |
651 | } else { |
662 | } else { |
652 | // $val3 = htmlentities_numeric($val3); |
663 | // $val3 = htmlentities_numeric($val3); |
653 | $val3 = $this->correctDesc($val3, $params, self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT, false); |
664 | $val3 = $this->correctDesc($val3, $params, self::OIDINFO_CORRECT_DESC_DISALLOW_ENDING_DOT, false); |
654 | } |
665 | } |
655 | $out_loc2 .= "\t\t\t<$name2>".$val3."</$name2>\n"; |
666 | $out_loc2 .= "\t\t\t<$name2>".$val3."</$name2>\n"; |
656 | } |
667 | } |
657 | } |
668 | } |
658 | 669 | ||
659 | if (!empty($out_loc2)) { |
670 | if (!empty($out_loc2)) { |
660 | $out_loc .= "\t\t<$name>\n"; |
671 | $out_loc .= "\t\t<$name>\n"; |
661 | $out_loc .= $out_loc2; |
672 | $out_loc .= $out_loc2; |
662 | $out_loc .= "\t\t</$name>\n"; |
673 | $out_loc .= "\t\t</$name>\n"; |
663 | } |
674 | } |
664 | } else { |
675 | } else { |
665 | // if (is_null($val)) continue; |
676 | // if (is_null($val)) continue; |
666 | if (empty($val) && ($name != 'description')) continue; // description is mandatory, according to http://oid-info.com/oid.xsd |
677 | if (empty($val) && ($name != 'description')) continue; // description is mandatory, according to http://oid-info.com/oid.xsd |
667 | 678 | ||
668 | if (!is_array($val)) $val = array($val); |
679 | if (!is_array($val)) $val = array($val); |
669 | 680 | ||
670 | foreach ($val as $val2) { |
681 | foreach ($val as $val2) { |
671 | // if (is_null($val2)) continue; |
682 | // if (is_null($val2)) continue; |
672 | if (empty($val2) && ($name != 'description')) continue; // description is mandatory, according to http://oid-info.com/oid.xsd |
683 | if (empty($val2) && ($name != 'description')) continue; // description is mandatory, according to http://oid-info.com/oid.xsd |
673 | 684 | ||
674 | if (($name != 'description') && ($name != 'information')) { // don't correctDesc description/information, because we already did it above. |
685 | if (($name != 'description') && ($name != 'information')) { // don't correctDesc description/information, because we already did it above. |
675 | // $val2 = htmlentities_numeric($val2); |
686 | // $val2 = htmlentities_numeric($val2); |
676 | $val2 = $this->correctDesc($val2, $params, self::OIDINFO_CORRECT_DESC_OPTIONAL_ENDING_DOT, false); |
687 | $val2 = $this->correctDesc($val2, $params, self::OIDINFO_CORRECT_DESC_OPTIONAL_ENDING_DOT, false); |
677 | } |
688 | } |
678 | $out_loc .= "\t\t<$name>".$val2."</$name>\n"; |
689 | $out_loc .= "\t\t<$name>".$val2."</$name>\n"; |
679 | } |
690 | } |
680 | } |
691 | } |
681 | } |
692 | } |
682 | 693 | ||
683 | if (!empty($out)) { |
694 | if (!empty($out)) { |
684 | $out = "\t<oid>\n"."\t\t".trim($out)."\n"; |
695 | $out = "\t<oid>\n"."\t\t".trim($out)."\n"; |
685 | } else { |
696 | } else { |
686 | $out = "\t<oid>\n"; |
697 | $out = "\t<oid>\n"; |
687 | } |
698 | } |
688 | $out .= "\t\t<dot-notation>$oid</dot-notation>\n"; |
699 | $out .= "\t\t<dot-notation>$oid</dot-notation>\n"; |
689 | $out .= $out_loc; |
700 | $out .= $out_loc; |
690 | $out .= "\t</oid>\n"; |
701 | $out .= "\t</oid>\n"; |
691 | 702 | ||
692 | return $out; |
703 | return $out; |
693 | } |
704 | } |
694 | 705 | ||
695 | # --- PART 4: Offline check if OIDs are illegal |
706 | # --- PART 4: Offline check if OIDs are illegal |
696 | 707 | ||
697 | protected $illegality_rules = array(); |
708 | protected $illegality_rules = array(); |
698 | 709 | ||
699 | public function clearIllegalityRules() { |
710 | public function clearIllegalityRules() { |
700 | $this->illegality_rules = array(); |
711 | $this->illegality_rules = array(); |
701 | } |
712 | } |
702 | 713 | ||
703 | public function loadIllegalityRuleFile($file) { |
714 | public function loadIllegalityRuleFile($file) { |
704 | if (!file_exists($file)) { |
715 | if (!file_exists($file)) { |
705 | throw new OIDInfoException("Error: File '$file' does not exist"); |
716 | throw new OIDInfoException("Error: File '$file' does not exist"); |
706 | } |
717 | } |
707 | 718 | ||
708 | $lines = file($file); |
719 | $lines = file($file); |
709 | 720 | ||
710 | if ($lines === false) { |
721 | if ($lines === false) { |
711 | throw new OIDInfoException("Error: Could not load '$file'"); |
722 | throw new OIDInfoException("Error: Could not load '$file'"); |
712 | } |
723 | } |
713 | 724 | ||
714 | $signature = trim(array_shift($lines)); |
725 | $signature = trim(array_shift($lines)); |
715 | if (($signature != '[1.3.6.1.4.1.37476.3.1.5.1]') && ($signature != '[1.3.6.1.4.1.37476.3.1.5.2]')) { |
726 | if (($signature != '[1.3.6.1.4.1.37476.3.1.5.1]') && ($signature != '[1.3.6.1.4.1.37476.3.1.5.2]')) { |
716 | throw new OIDInfoException("'$file' does not seem to a valid illegality rule file (file format OID does not match. Signature $signature unexpected)"); |
727 | throw new OIDInfoException("'$file' does not seem to a valid illegality rule file (file format OID does not match. Signature $signature unexpected)"); |
717 | } |
728 | } |
718 | 729 | ||
719 | foreach ($lines as $line) { |
730 | foreach ($lines as $line) { |
720 | // Remove comments |
731 | // Remove comments |
721 | $ary = explode('--', $line); |
732 | $ary = explode('--', $line); |
722 | $rule = trim($ary[0]); |
733 | $rule = trim($ary[0]); |
723 | 734 | ||
724 | if ($rule !== '') $this->addIllegalityRule($rule); |
735 | if ($rule !== '') $this->addIllegalityRule($rule); |
725 | } |
736 | } |
726 | } |
737 | } |
727 | 738 | ||
728 | public function addIllegalityRule($rule) { |
739 | public function addIllegalityRule($rule) { |
729 | $test = $rule; |
740 | $test = $rule; |
730 | $test = preg_replace('@\\.\\(!\\d+\\)@ismU', '.0', $test); // added in ver 2 |
741 | $test = preg_replace('@\\.\\(!\\d+\\)@ismU', '.0', $test); // added in ver 2 |
731 | $test = preg_replace('@\\.\\(\\d+\\+\\)@ismU', '.0', $test); |
742 | $test = preg_replace('@\\.\\(\\d+\\+\\)@ismU', '.0', $test); |
732 | $test = preg_replace('@\\.\\(\\d+\\-\\)@ismU', '.0', $test); |
743 | $test = preg_replace('@\\.\\(\\d+\\-\\)@ismU', '.0', $test); |
733 | $test = preg_replace('@\\.\\(\\d+\\-\\d+\\)@ismU', '.0', $test); |
744 | $test = preg_replace('@\\.\\(\\d+\\-\\d+\\)@ismU', '.0', $test); |
734 | $test = preg_replace('@\\.\\*@ismU', '.0', $test); |
745 | $test = preg_replace('@\\.\\*@ismU', '.0', $test); |
735 | 746 | ||
736 | if (!oid_valid_dotnotation($test, false, false, 1)) { |
747 | if (!oid_valid_dotnotation($test, false, false, 1)) { |
737 | throw new OIDInfoException("Illegal illegality rule '$rule'."); |
748 | throw new OIDInfoException("Illegal illegality rule '$rule'."); |
738 | } |
749 | } |
739 | 750 | ||
740 | $this->illegality_rules[] = $rule; |
751 | $this->illegality_rules[] = $rule; |
741 | } |
752 | } |
742 | 753 | ||
743 | private static function bigint_cmp($a, $b) { |
754 | private static function bigint_cmp($a, $b) { |
744 | if (function_exists('bccomp')) { |
755 | if (function_exists('bccomp')) { |
745 | return bccomp($a, $b); |
756 | return bccomp($a, $b); |
746 | } |
757 | } |
747 | 758 | ||
748 | if (function_exists('gmp_cmp')) { |
759 | if (function_exists('gmp_cmp')) { |
749 | return gmp_cmp($a, $b); |
760 | return gmp_cmp($a, $b); |
750 | } |
761 | } |
751 | 762 | ||
752 | if ($a > $b) return 1; |
763 | if ($a > $b) return 1; |
753 | if ($a < $b) return -1; |
764 | if ($a < $b) return -1; |
754 | return 0; |
765 | return 0; |
755 | } |
766 | } |
756 | 767 | ||
757 | public function illegalOID($oid, &$illegal_root='') { |
768 | public function illegalOID($oid, &$illegal_root='') { |
758 | $bak = $oid; |
769 | $bak = $oid; |
759 | $oid = self::trySanitizeOID($oid); |
770 | $oid = self::trySanitizeOID($oid); |
760 | if ($oid === false) { |
771 | if ($oid === false) { |
761 | $illegal_root = $bak; |
772 | $illegal_root = $bak; |
762 | return true; // is illegal |
773 | return true; // is illegal |
763 | } |
774 | } |
764 | 775 | ||
765 | $rules = $this->illegality_rules; |
776 | $rules = $this->illegality_rules; |
766 | 777 | ||
767 | foreach ($rules as $rule) { |
778 | foreach ($rules as $rule) { |
768 | $rule = str_replace(array('(', ')'), '', $rule); |
779 | $rule = str_replace(array('(', ')'), '', $rule); |
769 | 780 | ||
770 | $oarr = explode('.', $oid); |
781 | $oarr = explode('.', $oid); |
771 | $rarr = explode('.', $rule); |
782 | $rarr = explode('.', $rule); |
772 | 783 | ||
773 | if (count($oarr) < count($rarr)) continue; |
784 | if (count($oarr) < count($rarr)) continue; |
774 | 785 | ||
775 | $rulefit = true; |
786 | $rulefit = true; |
776 | 787 | ||
777 | $illrootary = array(); |
788 | $illrootary = array(); |
778 | 789 | ||
779 | $vararcs = 0; |
790 | $vararcs = 0; |
780 | $varsfit = 0; |
791 | $varsfit = 0; |
781 | for ($i=0; $i<count($rarr); $i++) { |
792 | for ($i=0; $i<count($rarr); $i++) { |
782 | $oelem = $oarr[$i]; |
793 | $oelem = $oarr[$i]; |
783 | $relem = $rarr[$i]; |
794 | $relem = $rarr[$i]; |
784 | 795 | ||
785 | $illrootary[] = $oelem; |
796 | $illrootary[] = $oelem; |
786 | 797 | ||
787 | if ($relem == '*') $relem = '0+'; |
798 | if ($relem == '*') $relem = '0+'; |
788 | 799 | ||
789 | $startchar = substr($relem, 0, 1); |
800 | $startchar = substr($relem, 0, 1); |
790 | $endchar = substr($relem, -1, 1); |
801 | $endchar = substr($relem, -1, 1); |
791 | if ($startchar == '!') { // added in ver 2 |
802 | if ($startchar == '!') { // added in ver 2 |
792 | $vararcs++; |
803 | $vararcs++; |
793 | $relem = substr($relem, 1, strlen($relem)-1); // cut away first char |
804 | $relem = substr($relem, 1, strlen($relem)-1); // cut away first char |
794 | $cmp = self::bigint_cmp($oelem, $relem) != 0; |
805 | $cmp = self::bigint_cmp($oelem, $relem) != 0; |
795 | if ($cmp) $varsfit++; |
806 | if ($cmp) $varsfit++; |
796 | } else if ($endchar == '+') { |
807 | } else if ($endchar == '+') { |
797 | $vararcs++; |
808 | $vararcs++; |
798 | $relem = substr($relem, 0, strlen($relem)-1); // cut away last char |
809 | $relem = substr($relem, 0, strlen($relem)-1); // cut away last char |
799 | $cmp = self::bigint_cmp($oelem, $relem) >= 0; |
810 | $cmp = self::bigint_cmp($oelem, $relem) >= 0; |
800 | if ($cmp) $varsfit++; |
811 | if ($cmp) $varsfit++; |
801 | } else if ($endchar == '-') { |
812 | } else if ($endchar == '-') { |
802 | $vararcs++; |
813 | $vararcs++; |
803 | $relem = substr($relem, 0, strlen($relem)-1); // cut away last char |
814 | $relem = substr($relem, 0, strlen($relem)-1); // cut away last char |
804 | $cmp = self::bigint_cmp($oelem, $relem) <= 0; |
815 | $cmp = self::bigint_cmp($oelem, $relem) <= 0; |
805 | if ($cmp) $varsfit++; |
816 | if ($cmp) $varsfit++; |
806 | } else if (strpos($relem, '-') !== false) { |
817 | } else if (strpos($relem, '-') !== false) { |
807 | $vararcs++; |
818 | $vararcs++; |
808 | $limarr = explode('-', $relem); |
819 | $limarr = explode('-', $relem); |
809 | $limmin = $limarr[0]; |
820 | $limmin = $limarr[0]; |
810 | $limmax = $limarr[1]; |
821 | $limmax = $limarr[1]; |
811 | $cmp_min = self::bigint_cmp($oelem, $limmin) >= 0; |
822 | $cmp_min = self::bigint_cmp($oelem, $limmin) >= 0; |
812 | $cmp_max = self::bigint_cmp($oelem, $limmax) <= 0; |
823 | $cmp_max = self::bigint_cmp($oelem, $limmax) <= 0; |
813 | if ($cmp_min && $cmp_max) $varsfit++; |
824 | if ($cmp_min && $cmp_max) $varsfit++; |
814 | } else { |
825 | } else { |
815 | if ($relem != $oelem) { |
826 | if ($relem != $oelem) { |
816 | $rulefit = false; |
827 | $rulefit = false; |
817 | break; |
828 | break; |
818 | } |
829 | } |
819 | } |
830 | } |
820 | } |
831 | } |
821 | 832 | ||
822 | if ($rulefit && ($vararcs == $varsfit)) { |
833 | if ($rulefit && ($vararcs == $varsfit)) { |
823 | $illegal_root = implode('.', $illrootary); |
834 | $illegal_root = implode('.', $illrootary); |
824 | return true; // is illegal |
835 | return true; // is illegal |
825 | } |
836 | } |
826 | } |
837 | } |
827 | 838 | ||
828 | $illegal_root = ''; |
839 | $illegal_root = ''; |
829 | return false; // not illegal |
840 | return false; // not illegal |
830 | } |
841 | } |
831 | 842 | ||
832 | # --- PART 5: Misc functions |
843 | # --- PART 5: Misc functions |
833 | 844 | ||
834 | function __construct() { |
845 | function __construct() { |
835 | if (file_exists(self::DEFAULT_ILLEGALITY_RULE_FILE)) { |
846 | if (file_exists(self::DEFAULT_ILLEGALITY_RULE_FILE)) { |
836 | $this->loadIllegalityRuleFile(self::DEFAULT_ILLEGALITY_RULE_FILE); |
847 | $this->loadIllegalityRuleFile(self::DEFAULT_ILLEGALITY_RULE_FILE); |
837 | } |
848 | } |
838 | } |
849 | } |
839 | 850 | ||
840 | public static function getPublicURL($oid) { |
851 | public static function getPublicURL($oid) { |
841 | return "http://oid-info.com/get/$oid"; |
852 | return "http://oid-info.com/get/$oid"; |
842 | } |
853 | } |
843 | 854 | ||
844 | public function oidExisting($oid, $onlineCheck=true, $useSimplePingProvider=true) { |
855 | public function oidExisting($oid, $onlineCheck=true, $useSimplePingProvider=true) { |
845 | $bak_oid = $oid; |
856 | $bak_oid = $oid; |
846 | $oid = self::trySanitizeOID($oid); |
857 | $oid = self::trySanitizeOID($oid); |
847 | if ($oid === false) { |
858 | if ($oid === false) { |
848 | throw new OIDInfoException("'$bak_oid' is not a valid OID"); |
859 | throw new OIDInfoException("'$bak_oid' is not a valid OID"); |
849 | } |
860 | } |
850 | 861 | ||
851 | $canuseSimplePingProvider = $useSimplePingProvider && $this->simplePingProviderAvailable(); |
862 | $canuseSimplePingProvider = $useSimplePingProvider && $this->simplePingProviderAvailable(); |
852 | if ($canuseSimplePingProvider) { |
863 | if ($canuseSimplePingProvider) { |
853 | if ($this->simplePingProviderCheckOID($oid)) return true; |
864 | if ($this->simplePingProviderCheckOID($oid)) return true; |
854 | } |
865 | } |
855 | if ($onlineCheck) { |
866 | if ($onlineCheck) { |
856 | return $this->checkOnlineExists($oid); |
867 | return $this->checkOnlineExists($oid); |
857 | } |
868 | } |
858 | if ((!$canuseSimplePingProvider) && (!$onlineCheck)) { |
869 | if ((!$canuseSimplePingProvider) && (!$onlineCheck)) { |
859 | throw new OIDInfoException("No simple or verbose checking method chosen/available"); |
870 | throw new OIDInfoException("No simple or verbose checking method chosen/available"); |
860 | } |
871 | } |
861 | return false; |
872 | return false; |
862 | } |
873 | } |
863 | 874 | ||
864 | public function oidMayCreate($oid, $onlineCheck=true, $useSimplePingProvider=true, $illegalityCheck=true) { |
875 | public function oidMayCreate($oid, $onlineCheck=true, $useSimplePingProvider=true, $illegalityCheck=true) { |
865 | $bak_oid = $oid; |
876 | $bak_oid = $oid; |
866 | $oid = self::trySanitizeOID($oid); |
877 | $oid = self::trySanitizeOID($oid); |
867 | if ($oid === false) { |
878 | if ($oid === false) { |
868 | throw new OIDInfoException("'$bak_oid' is not a valid OID"); |
879 | throw new OIDInfoException("'$bak_oid' is not a valid OID"); |
869 | } |
880 | } |
870 | 881 | ||
871 | if ($illegalityCheck && $this->illegalOID($oid)) return false; |
882 | if ($illegalityCheck && $this->illegalOID($oid)) return false; |
872 | 883 | ||
873 | $canuseSimplePingProvider = $useSimplePingProvider && $this->simplePingProviderAvailable(); |
884 | $canuseSimplePingProvider = $useSimplePingProvider && $this->simplePingProviderAvailable(); |
874 | if ($canuseSimplePingProvider) { |
885 | if ($canuseSimplePingProvider) { |
875 | if ($this->simplePingProviderCheckOID($oid)) return false; |
886 | if ($this->simplePingProviderCheckOID($oid)) return false; |
876 | } |
887 | } |
877 | if ($onlineCheck) { |
888 | if ($onlineCheck) { |
878 | return $this->checkOnlineMayCreate($oid); |
889 | return $this->checkOnlineMayCreate($oid); |
879 | } |
890 | } |
880 | if ((!$canuseSimplePingProvider) && (!$onlineCheck)) { |
891 | if ((!$canuseSimplePingProvider) && (!$onlineCheck)) { |
881 | throw new OIDInfoException("No simple or verbose checking method chosen/available"); |
892 | throw new OIDInfoException("No simple or verbose checking method chosen/available"); |
882 | } |
893 | } |
883 | return true; |
894 | return true; |
884 | } |
895 | } |
885 | 896 | ||
886 | # --- PART 6: Simple Ping Providers |
897 | # --- PART 6: Simple Ping Providers |
887 | # TODO: Question ... can't these provider concepts (SPP and VPP) not somehow be combined? |
898 | # TODO: Question ... can't these provider concepts (SPP and VPP) not somehow be combined? |
888 | 899 | ||
889 | protected $simplePingProviders = array(); |
900 | protected $simplePingProviders = array(); |
890 | 901 | ||
891 | public function addSimplePingProvider($addr) { |
902 | public function addSimplePingProvider($addr) { |
892 | if (!isset($this->simplePingProviders[$addr])) { |
903 | if (!isset($this->simplePingProviders[$addr])) { |
893 | if (strtolower(substr($addr, -4, 4)) == '.csv') { |
904 | if (strtolower(substr($addr, -4, 4)) == '.csv') { |
894 | $this->simplePingProviders[$addr] = new CSVSimplePingProvider($addr); |
905 | $this->simplePingProviders[$addr] = new CSVSimplePingProvider($addr); |
895 | } else { |
906 | } else { |
896 | $this->simplePingProviders[$addr] = new OIDSimplePingProvider($addr); |
907 | $this->simplePingProviders[$addr] = new OIDSimplePingProvider($addr); |
897 | // $this->simplePingProviders[$addr]->connect(); |
908 | // $this->simplePingProviders[$addr]->connect(); |
898 | } |
909 | } |
899 | } |
910 | } |
900 | return $this->simplePingProviders[$addr]; |
911 | return $this->simplePingProviders[$addr]; |
901 | } |
912 | } |
902 | 913 | ||
903 | public function removeSimplePingProvider($addr) { |
914 | public function removeSimplePingProvider($addr) { |
904 | $this->simplePingProviders[$addr]->disconnect(); |
915 | $this->simplePingProviders[$addr]->disconnect(); |
905 | unset($this->simplePingProviders[$addr]); |
916 | unset($this->simplePingProviders[$addr]); |
906 | } |
917 | } |
907 | 918 | ||
908 | public function removeAllSimplePingProviders() { |
919 | public function removeAllSimplePingProviders() { |
909 | foreach ($this->simplePingProviders as $addr => $obj) { |
920 | foreach ($this->simplePingProviders as $addr => $obj) { |
910 | $this->removeSimplePingProvider($addr); |
921 | $this->removeSimplePingProvider($addr); |
911 | } |
922 | } |
912 | } |
923 | } |
913 | 924 | ||
914 | public function listSimplePingProviders() { |
925 | public function listSimplePingProviders() { |
915 | $out = array(); |
926 | $out = array(); |
916 | foreach ($this->simplePingProviders as $addr => $obj) { |
927 | foreach ($this->simplePingProviders as $addr => $obj) { |
917 | $out[] = $addr; |
928 | $out[] = $addr; |
918 | } |
929 | } |
919 | return $out; |
930 | return $out; |
920 | } |
931 | } |
921 | 932 | ||
922 | public function simplePingProviderCheckOID($oid) { |
933 | public function simplePingProviderCheckOID($oid) { |
923 | if (!$this->simplePingProviderAvailable()) { |
934 | if (!$this->simplePingProviderAvailable()) { |
924 | throw new OIDInfoException("No simple ping providers available."); |
935 | throw new OIDInfoException("No simple ping providers available."); |
925 | } |
936 | } |
926 | 937 | ||
927 | $one_null = false; |
938 | $one_null = false; |
928 | foreach ($this->simplePingProviders as /*$addr =>*/ $obj) { |
939 | foreach ($this->simplePingProviders as /*$addr =>*/ $obj) { |
929 | $res = $obj->queryOID($oid); |
940 | $res = $obj->queryOID($oid); |
930 | if ($res) return true; |
941 | if ($res) return true; |
931 | if ($res !== false) $one_null = true; |
942 | if ($res !== false) $one_null = true; |
932 | } |
943 | } |
933 | 944 | ||
934 | return $one_null ? null : false; |
945 | return $one_null ? null : false; |
935 | } |
946 | } |
936 | 947 | ||
937 | public function simplePingProviderAvailable() { |
948 | public function simplePingProviderAvailable() { |
938 | return count($this->simplePingProviders) >= 1; |
949 | return count($this->simplePingProviders) >= 1; |
939 | } |
950 | } |
940 | 951 | ||
941 | } |
952 | } |
942 | 953 | ||
943 | interface IOIDSimplePingProvider { |
954 | interface IOIDSimplePingProvider { |
944 | public function queryOID($oid); |
955 | public function queryOID($oid); |
945 | public function disconnect(); |
956 | public function disconnect(); |
946 | public function connect(); |
957 | public function connect(); |
947 | } |
958 | } |
948 | 959 | ||
949 | class CSVSimplePingProvider implements IOIDSimplePingProvider { |
960 | class CSVSimplePingProvider implements IOIDSimplePingProvider { |
950 | protected $csvfile = ''; |
961 | protected $csvfile = ''; |
951 | protected $lines = array(); |
962 | protected $lines = array(); |
952 | protected $filemtime = 0; |
963 | protected $filemtime = 0; |
953 | 964 | ||
954 | public function queryOID($oid) { |
965 | public function queryOID($oid) { |
955 | $this->reloadCSV(); |
966 | $this->reloadCSV(); |
956 | return in_array($oid, $this->lines); |
967 | return in_array($oid, $this->lines); |
957 | } |
968 | } |
958 | 969 | ||
959 | public function disconnect() { |
970 | public function disconnect() { |
960 | // Nothing |
971 | // Nothing |
961 | } |
972 | } |
962 | 973 | ||
963 | public function connect() { |
974 | public function connect() { |
964 | // Nothing |
975 | // Nothing |
965 | } |
976 | } |
966 | 977 | ||
967 | // TODO: This cannot handle big CSVs. We need to introduce the old code of "2016-09-02_old_oidinfo_api_with_csv_reader.zip" here. |
978 | // TODO: This cannot handle big CSVs. We need to introduce the old code of "2016-09-02_old_oidinfo_api_with_csv_reader.zip" here. |
968 | protected function reloadCSV() { |
979 | protected function reloadCSV() { |
969 | if (!file_exists($this->csvfile)) { |
980 | if (!file_exists($this->csvfile)) { |
970 | throw new OIDInfoException("File '".$this->csvfile."' does not exist"); |
981 | throw new OIDInfoException("File '".$this->csvfile."' does not exist"); |
971 | } |
982 | } |
972 | $filemtime = filemtime($this->csvfile); |
983 | $filemtime = filemtime($this->csvfile); |
973 | if ($filemtime != $this->filemtime) { |
984 | if ($filemtime != $this->filemtime) { |
974 | $this->lines = file($this->csvfile); |
985 | $this->lines = file($this->csvfile); |
975 | $this->filemtime = $filemtime; |
986 | $this->filemtime = $filemtime; |
976 | } |
987 | } |
977 | } |
988 | } |
978 | 989 | ||
979 | function __construct($csvfile) { |
990 | function __construct($csvfile) { |
980 | $this->csvfile = $csvfile; |
991 | $this->csvfile = $csvfile; |
981 | $this->reloadCSV(); |
992 | $this->reloadCSV(); |
982 | } |
993 | } |
983 | } |
994 | } |
984 | 995 | ||
985 | 996 | ||
986 | class OIDSimplePingProvider implements IOIDSimplePingProvider { |
997 | class OIDSimplePingProvider implements IOIDSimplePingProvider { |
987 | protected $addr = ''; |
998 | protected $addr = ''; |
988 | protected $connected = false; |
999 | protected $connected = false; |
989 | protected $socket = null; |
1000 | protected $socket = null; |
990 | 1001 | ||
991 | const SPP_MAX_CONNECTION_ATTEMPTS = 3; // TODO: Put into an OIDInfoAPI class...? |
1002 | const SPP_MAX_CONNECTION_ATTEMPTS = 3; // TODO: Put into an OIDInfoAPI class...? |
992 | 1003 | ||
993 | const DEFAULT_PORT = 49500; |
1004 | const DEFAULT_PORT = 49500; |
994 | 1005 | ||
995 | protected function spp_reader_init() { |
1006 | protected function spp_reader_init() { |
996 | $this->spp_reader_uninit(); |
1007 | $this->spp_reader_uninit(); |
997 | 1008 | ||
998 | $ary = explode(':', $this->addr); // TODO: does not work with an IPv6 address |
1009 | $ary = explode(':', $this->addr); // TODO: does not work with an IPv6 address |
999 | $host = $ary[0]; |
1010 | $host = $ary[0]; |
1000 | $service_port = isset($ary[1]) ? $ary[1] : self::DEFAULT_PORT; |
1011 | $service_port = isset($ary[1]) ? $ary[1] : self::DEFAULT_PORT; |
1001 | 1012 | ||
1002 | $is_ip = filter_var($host, FILTER_VALIDATE_IP) !== false; |
1013 | $is_ip = filter_var($host, FILTER_VALIDATE_IP) !== false; |
1003 | if (!$is_ip) { |
1014 | if (!$is_ip) { |
1004 | $address = @gethostbyname($host); |
1015 | $address = @gethostbyname($host); |
1005 | if ($address === $host) { |
1016 | if ($address === $host) { |
1006 | $msg = "gethostbyname() failed.\n"; // TODO: exceptions? (also all "echos" below) |
1017 | $msg = "gethostbyname() failed.\n"; // TODO: exceptions? (also all "echos" below) |
1007 | throw new Exception($msg); |
1018 | throw new Exception($msg); |
1008 | // echo $msg; |
1019 | // echo $msg; |
1009 | // return false; |
1020 | // return false; |
1010 | } |
1021 | } |
1011 | } else { |
1022 | } else { |
1012 | $address = $host; |
1023 | $address = $host; |
1013 | } |
1024 | } |
1014 | 1025 | ||
1015 | $this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
1026 | $this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
1016 | if ($this->socket === false) { |
1027 | if ($this->socket === false) { |
1017 | $msg = "socket_create() failed: " . socket_strerror(socket_last_error()) . "\n"; |
1028 | $msg = "socket_create() failed: " . socket_strerror(socket_last_error()) . "\n"; |
1018 | throw new Exception($msg); |
1029 | throw new Exception($msg); |
1019 | // echo $msg; |
1030 | // echo $msg; |
1020 | // return false; |
1031 | // return false; |
1021 | } |
1032 | } |
1022 | $result = @socket_connect($this->socket, $address, $service_port); |
1033 | $result = @socket_connect($this->socket, $address, $service_port); |
1023 | if ($result === false) { |
1034 | if ($result === false) { |
1024 | $msg = "socket_connect() failed: " . socket_strerror(socket_last_error($this->socket)) . "\n"; |
1035 | $msg = "socket_connect() failed: " . socket_strerror(socket_last_error($this->socket)) . "\n"; |
1025 | throw new Exception($msg); |
1036 | throw new Exception($msg); |
1026 | // echo $msg; |
1037 | // echo $msg; |
1027 | // return false; |
1038 | // return false; |
1028 | } |
1039 | } |
1029 | 1040 | ||
1030 | $this->connected = true; |
1041 | $this->connected = true; |
1031 | } |
1042 | } |
1032 | 1043 | ||
1033 | protected function spp_reader_avail($oid, $failcount=0) { |
1044 | protected function spp_reader_avail($oid, $failcount=0) { |
1034 | $in = "${oid}\n\0"; // PHP's socket_send() does not send a trailing \n . There needs to be something after the \n ... :( |
1045 | $in = "${oid}\n\0"; // PHP's socket_send() does not send a trailing \n . There needs to be something after the \n ... :( |
1035 | 1046 | ||
1036 | if ($failcount >= self::SPP_MAX_CONNECTION_ATTEMPTS) { |
1047 | if ($failcount >= self::SPP_MAX_CONNECTION_ATTEMPTS) { |
1037 | $msg = "Query $oid: CONNECTION TO SIMPLE PING PROVIDER FAILED!\n"; |
1048 | $msg = "Query $oid: CONNECTION TO SIMPLE PING PROVIDER FAILED!\n"; |
1038 | throw new Exception($msg); |
1049 | throw new Exception($msg); |
1039 | // echo $msg; |
1050 | // echo $msg; |
1040 | // return null; |
1051 | // return null; |
1041 | } |
1052 | } |
1042 | 1053 | ||
1043 | if (!$this->connected) { |
1054 | if (!$this->connected) { |
1044 | $this->spp_reader_init(); |
1055 | $this->spp_reader_init(); |
1045 | } |
1056 | } |
1046 | 1057 | ||
1047 | $s = @socket_send($this->socket, $in, strlen($in), 0); |
1058 | $s = @socket_send($this->socket, $in, strlen($in), 0); |
1048 | if ($s != strlen($in)) { |
1059 | if ($s != strlen($in)) { |
1049 | // echo "Query $oid: Sending failed\n"; |
1060 | // echo "Query $oid: Sending failed\n"; |
1050 | $this->spp_reader_init(); |
1061 | $this->spp_reader_init(); |
1051 | if (!$this->socket) return null; |
1062 | if (!$this->socket) return null; |
1052 | return $this->spp_reader_avail($oid, $failcount+1); |
1063 | return $this->spp_reader_avail($oid, $failcount+1); |
1053 | } |
1064 | } |
1054 | 1065 | ||
1055 | $out = @socket_read($this->socket, 2048); |
1066 | $out = @socket_read($this->socket, 2048); |
1056 | if (trim($out) == '1') { |
1067 | if (trim($out) == '1') { |
1057 | return true; |
1068 | return true; |
1058 | } else if (trim($out) == '0') { |
1069 | } else if (trim($out) == '0') { |
1059 | return false; |
1070 | return false; |
1060 | } else { |
1071 | } else { |
1061 | // echo "Query $oid: Receiving failed\n"; |
1072 | // echo "Query $oid: Receiving failed\n"; |
1062 | $this->spp_reader_init(); |
1073 | $this->spp_reader_init(); |
1063 | if (!$this->socket) return null; |
1074 | if (!$this->socket) return null; |
1064 | return $this->spp_reader_avail($oid, $failcount+1); |
1075 | return $this->spp_reader_avail($oid, $failcount+1); |
1065 | } |
1076 | } |
1066 | } |
1077 | } |
1067 | 1078 | ||
1068 | protected function spp_reader_uninit() { |
1079 | protected function spp_reader_uninit() { |
1069 | if (!$this->connected) return; |
1080 | if (!$this->connected) return; |
1070 | @socket_close($this->socket); |
1081 | @socket_close($this->socket); |
1071 | $this->connected = false; |
1082 | $this->connected = false; |
1072 | } |
1083 | } |
1073 | 1084 | ||
1074 | public function queryOID($oid) { |
1085 | public function queryOID($oid) { |
1075 | if (trim($oid) === 'bye') return null; |
1086 | if (trim($oid) === 'bye') return null; |
1076 | return $this->spp_reader_avail($oid); |
1087 | return $this->spp_reader_avail($oid); |
1077 | } |
1088 | } |
1078 | 1089 | ||
1079 | public function disconnect() { |
1090 | public function disconnect() { |
1080 | return $this->spp_reader_uninit(); |
1091 | return $this->spp_reader_uninit(); |
1081 | } |
1092 | } |
1082 | 1093 | ||
1083 | public function connect() { |
1094 | public function connect() { |
1084 | return $this->spp_reader_init(); |
1095 | return $this->spp_reader_init(); |
1085 | } |
1096 | } |
1086 | 1097 | ||
1087 | function __construct($addr='localhost:49500') { |
1098 | function __construct($addr='localhost:49500') { |
1088 | $this->addr = $addr; |
1099 | $this->addr = $addr; |
1089 | } |
1100 | } |
1090 | 1101 | ||
1091 | } |
1102 | } |
1092 | 1103 |