Rev 1053 | Rev 1116 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1053 | Rev 1064 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * OIDplus 2.0 |
4 | * OIDplus 2.0 |
5 | * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft |
6 | * |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * you may not use this file except in compliance with the License. |
8 | * you may not use this file except in compliance with the License. |
9 | * You may obtain a copy of the License at |
9 | * You may obtain a copy of the License at |
10 | * |
10 | * |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * |
12 | * |
13 | * Unless required by applicable law or agreed to in writing, software |
13 | * Unless required by applicable law or agreed to in writing, software |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
17 | * limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | use ViaThinkSoft\OIDplus\OIDplus; |
20 | use ViaThinkSoft\OIDplus\OIDplus; |
21 | use ViaThinkSoft\OIDplus\OIDplusGui; |
21 | use ViaThinkSoft\OIDplus\OIDplusGui; |
22 | use ViaThinkSoft\OIDplus\OIDplusException; |
22 | use ViaThinkSoft\OIDplus\OIDplusException; |
23 | 23 | ||
24 | header('Content-Type:text/html; charset=UTF-8'); |
24 | header('Content-Type:text/html; charset=UTF-8'); |
25 | 25 | ||
26 | require_once __DIR__ . '/../../../../includes/oidplus.inc.php'; |
26 | require_once __DIR__ . '/../../../../includes/oidplus.inc.php'; |
27 | 27 | ||
28 | set_exception_handler(array(OIDplusGui::class, 'html_exception_handler')); |
28 | set_exception_handler(array(OIDplusGui::class, 'html_exception_handler')); |
29 | 29 | ||
30 | @set_time_limit(0); |
30 | @set_time_limit(0); |
31 | 31 | ||
32 | OIDplus::init(true); |
32 | OIDplus::init(true); |
33 | 33 | ||
34 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPageAdminNostalgia', false)) { |
34 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPageAdminNostalgia', false)) { |
35 | throw new OIDplusException(_L('This plugin was disabled by the system administrator!')); |
35 | throw new OIDplusException(_L('This plugin was disabled by the system administrator!')); |
36 | } |
36 | } |
37 | 37 | ||
38 | if (!OIDplus::authUtils()->isAdminLoggedIn()) { |
38 | if (!OIDplus::authUtils()->isAdminLoggedIn()) { |
39 | throw new OIDplusException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin'))); |
39 | throw new OIDplusException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin'))); |
40 | } |
40 | } |
41 | 41 | ||
42 | if (!class_exists('ZipArchive')) { |
42 | if (!class_exists('ZipArchive')) { |
43 | throw new OIDplusException(_L('The PHP extension "ZipArchive" needs to be installed to create a ZIP archive with an included database. Otherwise, you can just download the plain program without data.')); |
43 | throw new OIDplusException(_L('The PHP extension "ZipArchive" needs to be installed to create a ZIP archive with an included database. Otherwise, you can just download the plain program without data.')); |
44 | } |
44 | } |
45 | 45 | ||
46 | $dos_ids = array(); |
46 | $dos_ids = array(); |
47 | $parent_oids = array(); |
47 | $parent_oids = array(); |
48 | $i = 0; |
48 | $i = 0; |
49 | 49 | ||
50 | // Root node |
50 | // Root node |
51 | $dos_ids[''] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
51 | $dos_ids[''] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
52 | $parent_oids[''] = ''; |
52 | $parent_oids[''] = ''; |
53 | $iri[''] = array(); |
53 | $iri[''] = array(); |
54 | $asn1[''] = array(); |
54 | $asn1[''] = array(); |
55 | $title[''] = 'OID Root'; |
55 | $title[''] = 'OID Root'; |
56 | $description[''] = 'Exported by OIDplus 2.0'; |
56 | $description[''] = 'Exported by OIDplus 2.0'; |
57 | 57 | ||
58 | // Now check all OIDs |
58 | // Now check all OIDs |
59 | $res = OIDplus::db()->query("select * from ###objects where id like 'oid:%' order by ".OIDplus::db()->natOrder('id')); |
59 | $res = OIDplus::db()->query("select * from ###objects where id like 'oid:%' order by ".OIDplus::db()->natOrder('id')); |
60 | while ($row = $res->fetch_object()) { |
60 | while ($row = $res->fetch_object()) { |
61 | $oid = substr($row->id, strlen('oid:')); |
61 | $oid = substr($row->id, strlen('oid:')); |
62 | $parent_oid = substr($row->parent, strlen('oid:')); |
62 | $parent_oid = substr($row->parent, strlen('oid:')); |
63 | 63 | ||
64 | $dos_ids[$oid] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
64 | $dos_ids[$oid] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
65 | fill_asn1($oid, $asn1); |
65 | fill_asn1($oid, $asn1); |
66 | fill_iri($oid, $iri); |
66 | fill_iri($oid, $iri); |
67 | $title[$oid] = $row->title; |
67 | $title[$oid] = vts_utf8_decode($row->title); |
68 | $description[$oid] = $row->description; |
68 | $description[$oid] = vts_utf8_decode($row->description); |
69 | 69 | ||
70 | if ((oid_len($oid) > 1) && ($parent_oid == '')) { |
70 | if ((oid_len($oid) > 1) && ($parent_oid == '')) { |
71 | do { |
71 | do { |
72 | $real_parent = oid_len($oid) > 1 ? oid_up($oid) : ''; |
72 | $real_parent = oid_len($oid) > 1 ? oid_up($oid) : ''; |
73 | $parent_oids[$oid] = $real_parent; |
73 | $parent_oids[$oid] = $real_parent; |
74 | 74 | ||
75 | if (isset($dos_ids[$real_parent])) break; // did we already handle this parent node? |
75 | if (isset($dos_ids[$real_parent])) break; // did we already handle this parent node? |
76 | 76 | ||
77 | $dos_ids[$real_parent] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
77 | $dos_ids[$real_parent] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
78 | fill_asn1($real_parent, $asn1); // well-known OIDs? |
78 | fill_asn1($real_parent, $asn1); // well-known OIDs? |
79 | fill_iri($real_parent, $iri); // well-known OIDs? |
79 | fill_iri($real_parent, $iri); // well-known OIDs? |
80 | $title[$real_parent] = ''; |
80 | $title[$real_parent] = ''; |
81 | $description[$real_parent] = ''; |
81 | $description[$real_parent] = ''; |
82 | $res2 = OIDplus::db()->query("select * from ###objects where id = 'oid:$real_parent'"); |
82 | $res2 = OIDplus::db()->query("select * from ###objects where id = 'oid:$real_parent'"); |
83 | while ($row2 = $res2->fetch_object()) { |
83 | while ($row2 = $res2->fetch_object()) { |
84 | $title[$real_parent] = $row2->title; |
84 | $title[$real_parent] = vts_utf8_decode($row2->title); |
85 | $description[$real_parent] = $row2->description; |
85 | $description[$real_parent] = vts_utf8_decode($row2->description); |
86 | } |
86 | } |
87 | 87 | ||
88 | // next |
88 | // next |
89 | if ($real_parent == '') break; |
89 | if ($real_parent == '') break; |
90 | $oid = $real_parent; |
90 | $oid = $real_parent; |
91 | } while (true); |
91 | } while (true); |
92 | } else { |
92 | } else { |
93 | $parent_oids[$oid] = $parent_oid; |
93 | $parent_oids[$oid] = $parent_oid; |
94 | } |
94 | } |
95 | } |
95 | } |
96 | 96 | ||
97 | $tmp_file = OIDplus::localpath().'userdata/dos_export.zip'; |
97 | $tmp_file = OIDplus::localpath().'userdata/dos_export.zip'; |
98 | 98 | ||
99 | $zip = new ZipArchive(); |
99 | $zip = new ZipArchive(); |
100 | if ($zip->open($tmp_file, ZipArchive::CREATE)!== true) { |
100 | if ($zip->open($tmp_file, ZipArchive::CREATE)!== true) { |
101 | throw new OIDplusException("cannot open <$tmp_file>"); |
101 | throw new OIDplusException("cannot open <$tmp_file>"); |
102 | } |
102 | } |
103 | 103 | ||
104 | function make_line($command, $data) { |
104 | function make_line($command, $data) { |
105 | return $command.$data."\r\n"; |
105 | return $command.$data."\r\n"; |
106 | } |
106 | } |
107 | 107 | ||
108 | // https://github.com/danielmarschall/oidplus_dos/blob/master/OIDFILE.PAS |
108 | // https://github.com/danielmarschall/oidplus_dos/blob/master/OIDFILE.PAS |
109 | define('CMD_VERSION', 'VERS'); |
109 | define('CMD_VERSION', 'VERS'); |
110 | define('CMD_OWN_ID', 'SELF'); |
110 | define('CMD_OWN_ID', 'SELF'); |
111 | define('CMD_PARENT', 'SUPR'); |
111 | define('CMD_PARENT', 'SUPR'); |
112 | define('CMD_CHILD', 'CHLD'); |
112 | define('CMD_CHILD', 'CHLD'); |
113 | define('CMD_ASN1_IDENTIFIER', 'ASN1'); |
113 | define('CMD_ASN1_IDENTIFIER', 'ASN1'); |
114 | define('CMD_UNICODE_LABEL', 'UNIL'); |
114 | define('CMD_UNICODE_LABEL', 'UNIL'); |
115 | define('CMD_DESCRIPTION', 'DESC'); |
115 | define('CMD_DESCRIPTION', 'DESC'); |
116 | 116 | ||
117 | foreach ($dos_ids as $oid => $dos_id) { |
117 | foreach ($dos_ids as $oid => $dos_id) { |
118 | $cont = ''; |
118 | $cont = ''; |
119 | 119 | ||
120 | $cont .= make_line(CMD_VERSION, 2022); |
120 | $cont .= make_line(CMD_VERSION, 2022); |
121 | 121 | ||
122 | $cont .= make_line(CMD_OWN_ID, $dos_id.$oid); |
122 | $cont .= make_line(CMD_OWN_ID, $dos_id.$oid); |
123 | 123 | ||
124 | $parent_oid = $parent_oids[$oid]; |
124 | $parent_oid = $parent_oids[$oid]; |
125 | $parent_id = $dos_ids[$parent_oid]; |
125 | $parent_id = $dos_ids[$parent_oid]; |
126 | $cont .= make_line(CMD_PARENT, $parent_id.$parent_oid); |
126 | $cont .= make_line(CMD_PARENT, $parent_id.$parent_oid); |
127 | 127 | ||
128 | foreach ($parent_oids as $child_oid => $parent_oid) { |
128 | foreach ($parent_oids as $child_oid => $parent_oid) { |
129 | if ($child_oid == '') continue; |
129 | if ($child_oid == '') continue; |
130 | if ($parent_oid == $oid) { |
130 | if ($parent_oid == $oid) { |
131 | $child_id = $dos_ids[$child_oid]; |
131 | $child_id = $dos_ids[$child_oid]; |
132 | $cont .= make_line(CMD_CHILD, $child_id.$child_oid); |
132 | $cont .= make_line(CMD_CHILD, $child_id.$child_oid); |
133 | } |
133 | } |
134 | } |
134 | } |
135 | 135 | ||
136 | foreach ($asn1[$oid] as $name) { |
136 | foreach ($asn1[$oid] as $name) { |
137 | $cont .= make_line(CMD_ASN1_IDENTIFIER, $name); |
137 | $cont .= make_line(CMD_ASN1_IDENTIFIER, $name); |
138 | } |
138 | } |
139 | 139 | ||
140 | foreach ($iri[$oid] as $name) { |
140 | foreach ($iri[$oid] as $name) { |
141 | $cont .= make_line(CMD_UNICODE_LABEL, $name); |
141 | $cont .= make_line(CMD_UNICODE_LABEL, $name); |
142 | } |
142 | } |
143 | 143 | ||
144 | $desc_ary1 = handleDesc($title[$oid]); |
144 | $desc_ary1 = handleDesc($title[$oid]); |
145 | $desc_ary2 = handleDesc($description[$oid]); |
145 | $desc_ary2 = handleDesc($description[$oid]); |
146 | $desc_ary = array_merge($desc_ary1, $desc_ary2); |
146 | $desc_ary = array_merge($desc_ary1, $desc_ary2); |
147 | $prev_line = ''; |
147 | $prev_line = ''; |
148 | foreach ($desc_ary as $line_idx => $line) { |
148 | foreach ($desc_ary as $line_idx => $line) { |
149 | if ($line == $prev_line) continue; |
149 | if ($line == $prev_line) continue; |
150 | if ($line_idx >= 10/*DESCEDIT_LINES*/) break; |
150 | if ($line_idx >= 10/*DESCEDIT_LINES*/) break; |
151 | $cont .= make_line(CMD_DESCRIPTION, $line); |
151 | $cont .= make_line(CMD_DESCRIPTION, $line); |
152 | $prev_line = $line; |
152 | $prev_line = $line; |
153 | } |
153 | } |
154 | 154 | ||
155 | //echo "****$dos_id.OID\r\n"; |
155 | //echo "****$dos_id.OID\r\n"; |
156 | //echo "$cont\r\n"; |
156 | //echo "$cont\r\n"; |
157 | 157 | ||
158 | $zip->addFromString("$dos_id.OID", $cont); |
158 | $zip->addFromString("$dos_id.OID", $cont); |
159 | } |
159 | } |
160 | 160 | ||
161 | $exe_url = 'https://github.com/danielmarschall/oidplus_dos/raw/master/OIDPLUS.EXE'; |
161 | $exe_url = 'https://github.com/danielmarschall/oidplus_dos/raw/master/OIDPLUS.EXE'; |
162 | $exe = url_get_contents($exe_url); |
162 | $exe = url_get_contents($exe_url); |
163 | if (!$exe) { |
163 | if (!$exe) { |
164 | throw new OIDplusException(_L("Cannot download the binary file from GitHub (%1)", $exe_url)); |
164 | throw new OIDplusException(_L("Cannot download the binary file from GitHub (%1)", $exe_url)); |
165 | } |
165 | } |
166 | $zip->addFromString('OIDPLUS.EXE', $exe); |
166 | $zip->addFromString('OIDPLUS.EXE', $exe); |
167 | 167 | ||
168 | $zip->close(); |
168 | $zip->close(); |
169 | 169 | ||
170 | if (!headers_sent()) { |
170 | if (!headers_sent()) { |
171 | header('Content-Type: application/zip'); |
171 | header('Content-Type: application/zip'); |
172 | header('Content-Disposition: attachment; filename=oidplus_dos.zip'); |
172 | header('Content-Disposition: attachment; filename=oidplus_dos.zip'); |
173 | readfile($tmp_file); |
173 | readfile($tmp_file); |
174 | } |
174 | } |
175 | 175 | ||
176 | unlink($tmp_file); |
176 | unlink($tmp_file); |
177 | 177 | ||
178 | OIDplus::invoke_shutdown(); |
178 | OIDplus::invoke_shutdown(); |
179 | 179 | ||
180 | # --- |
180 | # --- |
181 | 181 | ||
182 | function fill_asn1($oid, &$asn1) { |
182 | function fill_asn1($oid, &$asn1) { |
183 | if (!isset($asn1[$oid])) $asn1[$oid] = array(); |
183 | if (!isset($asn1[$oid])) $asn1[$oid] = array(); |
184 | $res = OIDplus::db()->query("select * from ###asn1id where oid = 'oid:$oid'"); |
184 | $res = OIDplus::db()->query("select * from ###asn1id where oid = 'oid:$oid'"); |
185 | while ($row = $res->fetch_object()) { |
185 | while ($row = $res->fetch_object()) { |
186 | $asn1[$oid][] = $row->name; |
186 | $asn1[$oid][] = $row->name; |
187 | } |
187 | } |
188 | } |
188 | } |
189 | 189 | ||
190 | function fill_iri($oid, &$iri) { |
190 | function fill_iri($oid, &$iri) { |
191 | if (!isset($iri[$oid])) $iri[$oid] = array(); |
191 | if (!isset($iri[$oid])) $iri[$oid] = array(); |
192 | $res = OIDplus::db()->query("select * from ###iri where oid = 'oid:$oid'"); |
192 | $res = OIDplus::db()->query("select * from ###iri where oid = 'oid:$oid'"); |
193 | while ($row = $res->fetch_object()) { |
193 | while ($row = $res->fetch_object()) { |
194 | $iri[$oid][] = $row->name; |
194 | $iri[$oid][] = $row->name; |
195 | } |
195 | } |
196 | } |
196 | } |
197 | 197 | ||
198 | function handleDesc($desc) { |
198 | function handleDesc($desc) { |
199 | $desc = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $desc); // br2nl |
199 | $desc = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $desc); // br2nl |
200 | $desc = strip_tags($desc); |
200 | $desc = strip_tags($desc); |
201 | $desc = str_replace(' ', ' ', $desc); |
201 | $desc = str_replace(' ', ' ', $desc); |
202 | $desc = html_entity_decode($desc); |
202 | $desc = html_entity_decode($desc); |
203 | $desc = str_replace("\r", "", $desc); |
203 | $desc = str_replace("\r", "", $desc); |
204 | $desc = str_replace("\n", " ", $desc); |
204 | $desc = str_replace("\n", " ", $desc); |
205 | $desc = str_replace("\t", " ", $desc); |
205 | $desc = str_replace("\t", " ", $desc); |
206 | $desc = trim($desc); |
206 | $desc = trim($desc); |
207 | $desc_ary = explode("\r\n", wordwrap($desc, 75, "\r\n", true)); |
207 | $desc_ary = explode("\r\n", wordwrap($desc, 75, "\r\n", true)); |
208 | if (implode('',$desc_ary) == '') $desc_ary = array(); |
208 | if (implode('',$desc_ary) == '') $desc_ary = array(); |
209 | return $desc_ary; |
209 | return $desc_ary; |
210 | } |
210 | } |
211 | 211 |