Rev 1130 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
557 | daniel-mar | 1 | #!/usr/bin/env php |
61 | daniel-mar | 2 | <?php |
3 | |||
511 | daniel-mar | 4 | /* |
5 | * OIDplus 2.0 |
||
6 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
||
7 | * |
||
8 | * Licensed under the Apache License, Version 2.0 (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 |
||
11 | * |
||
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
13 | * |
||
14 | * Unless required by applicable law or agreed to in writing, software |
||
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
17 | * See the License for the specific language governing permissions and |
||
18 | * limitations under the License. |
||
19 | */ |
||
20 | |||
1162 | daniel-mar | 21 | use ViaThinkSoft\OIDplus\OIDplus; |
22 | |||
97 | daniel-mar | 23 | require_once __DIR__ . '/../includes/oidplus.inc.php'; |
61 | daniel-mar | 24 | |
25 | OIDplus::init(false); |
||
261 | daniel-mar | 26 | if (!OIDplus::baseConfig()->exists('OIDINFO_API_URL')) { |
61 | daniel-mar | 27 | die("OIDINFO_API_URL not available (API is currently not public)\n"); |
28 | } |
||
29 | |||
1116 | daniel-mar | 30 | const VERBOSE = false; |
31 | const DEFAULT_EMAIL = 'oidra@viathinksoft.de'; |
||
32 | const ALL_OID_LIST = '/home/oidplus/all_oids.txt'; // contains all OIDs (non-public file) |
||
33 | const DESIRED_ROOT = '1.3.6.1.4.1.37476'; |
||
61 | daniel-mar | 34 | |
35 | # --- |
||
36 | |||
37 | exec("cat ".escapeshellarg(ALL_OID_LIST)." | sort | grep '^".DESIRED_ROOT."'", $out, $ec); |
||
38 | |||
39 | foreach ($out as $oid) { |
||
40 | $oid = trim($oid); |
||
41 | if ($oid == '') continue; |
||
42 | |||
43 | check_oid($oid); |
||
44 | } |
||
45 | |||
1130 | daniel-mar | 46 | /** |
47 | * @param string $oid |
||
48 | * @return void |
||
49 | */ |
||
50 | function check_oid(string $oid) { |
||
61 | daniel-mar | 51 | |
261 | daniel-mar | 52 | $res = OIDplus::db()->query("select * from ###objects where id = ?", array("oid:$oid")); |
236 | daniel-mar | 53 | $ok = $res->num_rows() > 0; |
61 | daniel-mar | 54 | |
55 | if (!$ok) { |
||
56 | echo "Not existing: $oid : Adding!\n"; |
||
57 | add_oid($oid); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | echo "OK\n"; |
||
62 | |||
63 | # --- |
||
64 | |||
1130 | daniel-mar | 65 | /** |
66 | * @param string $oid |
||
67 | * @param string $root |
||
68 | * @return void |
||
69 | */ |
||
70 | function add_oid(string $oid, string $root=DESIRED_ROOT) { |
||
61 | daniel-mar | 71 | if (VERBOSE) echo "Adding $oid ...\n"; |
72 | $data = ft_get_oid_data($oid); |
||
73 | |||
74 | if (!isset($data['oid'])) { |
||
75 | echo "Warning: Cannot gain information about OID $oid\n"; |
||
76 | |||
77 | $parent = ($oid == $root) ? '' : oid_up($oid); |
||
78 | $title = '(tbd)'; |
||
79 | $description = '(tbd)'; |
||
80 | $ra_email = ''; |
||
81 | if ((DEFAULT_EMAIL != '') && ($ra_email == '')) $ra_email = DEFAULT_EMAIL; |
||
82 | |||
261 | daniel-mar | 83 | sql_execute("insert into ###objects (id, parent, title, description, ra_email) values (?, ?, ?, ?, ?)", array("oid:$oid", "oid:$parent", $title, $description, $ra_email)); |
61 | daniel-mar | 84 | |
85 | return; |
||
86 | } |
||
87 | |||
88 | if (!isset($data['oid']['identifier'])) $data['oid']['identifier'] = array(); |
||
89 | foreach ($data['oid']['identifier'] as $identifier) { |
||
261 | daniel-mar | 90 | sql_execute("insert into ###asn1id (oid, name) values (?, ?)", array("oid:$oid", $identifier)); |
61 | daniel-mar | 91 | } |
92 | |||
93 | if (!isset($data['oid']['unicode-label'])) $data['oid']['unicode-label'] = array(); |
||
94 | foreach ($data['oid']['unicode-label'] as $identifier) { |
||
261 | daniel-mar | 95 | sql_execute("insert into ###iri (oid, name) values (?, ?)", array("oid:$oid", $identifier)); |
61 | daniel-mar | 96 | } |
97 | |||
98 | $parent = ($oid == $root) ? '' : oid_up($oid); |
||
99 | |||
100 | $title = strip_tags(@$data['oid']['description']); |
||
101 | $title = html_entity_decode($title, ENT_COMPAT, 'UTF-8'); |
||
102 | |||
103 | $description = '<strong>' . trim(@$data['oid']['description']) . '</strong><br><br>' . nl2br(trim(@$data['oid']['information'])); |
||
104 | $description = str_replace(array("\r", "\n"), '', $description); |
||
105 | $description = str_ireplace('<br />', '<br>', $description); |
||
106 | $description = str_ireplace('<br/>', '<br>', $description); |
||
107 | $description = '<p>' . str_ireplace('<br><br>', '</p><p>', $description) . '</p>'; |
||
108 | |||
109 | $ra_email = @$data['oid']['current_registrant']['email']; |
||
110 | if ($ra_email == '') $ra_email = @$data['oid']['first_registrant']['email']; |
||
111 | |||
261 | daniel-mar | 112 | $resx = OIDplus::db()->query("select ra_email from ###objects where id = ?", array("oid:$parent")); |
236 | daniel-mar | 113 | if ($rowx = $resx->fetch_array()) $ra_email = $rowx['ra_email']; |
61 | daniel-mar | 114 | |
115 | $created = @$data['oid']['first_registrant']['creation-date']; |
||
116 | $updated = @$data['oid']['last-modification']; |
||
117 | |||
261 | daniel-mar | 118 | sql_execute("insert into ###objects (id, parent, title, description, ra_email, created, updated) values (?, ?, ?, ?, ?, ?, ?)", |
150 | daniel-mar | 119 | array("oid:$oid", "oid:$parent", $title, $description, $ra_email, $created, $updated)); |
61 | daniel-mar | 120 | } |
121 | |||
1130 | daniel-mar | 122 | /** |
123 | * @param string $oid |
||
124 | * @return false|array |
||
125 | */ |
||
126 | function ft_get_oid_data(string $oid) { |
||
261 | daniel-mar | 127 | $url = OIDplus::baseConfig()->getValue('OIDINFO_API_URL') . '&oid='.urlencode($oid); |
61 | daniel-mar | 128 | $cont_json = @file_get_contents($url); |
129 | if (!$cont_json) { |
||
130 | sleep(5); |
||
131 | $cont_json = @file_get_contents($url); |
||
132 | if (!$cont_json) return false; |
||
133 | } |
||
1130 | daniel-mar | 134 | return json_decode($cont_json,true); |
61 | daniel-mar | 135 | } |
136 | |||
1130 | daniel-mar | 137 | /** |
138 | * @param string $sql |
||
139 | * @param array|null $prep |
||
140 | * @return void |
||
141 | */ |
||
142 | function sql_execute(string $sql, array $prep=null) { |
||
61 | daniel-mar | 143 | # echo "$sql\n"; |
237 | daniel-mar | 144 | try { |
145 | OIDplus::db()->query($sql, $prep); |
||
146 | } catch (Exception $e) { |
||
147 | echo "Warning: " . $e->getMessage() . "\n"; |
||
148 | } |
||
61 | daniel-mar | 149 | } |