Rev 1156 | Rev 1266 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
754 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
5 | * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft |
||
6 | * |
||
7 | * Licensed under the Apache License, Version 2.0 (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 |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | use ViaThinkSoft\OIDplus\OIDplus; |
21 | use ViaThinkSoft\OIDplus\OIDplusGui; |
||
22 | use ViaThinkSoft\OIDplus\OIDplusException; |
||
1201 | daniel-mar | 23 | use ViaThinkSoft\OIDplus\OIDplusHtmlException; |
1050 | daniel-mar | 24 | |
754 | daniel-mar | 25 | header('Content-Type:text/html; charset=UTF-8'); |
26 | |||
27 | require_once __DIR__ . '/../../../../includes/oidplus.inc.php'; |
||
28 | |||
1050 | daniel-mar | 29 | set_exception_handler(array(OIDplusGui::class, 'html_exception_handler')); |
754 | daniel-mar | 30 | |
757 | daniel-mar | 31 | @set_time_limit(0); |
754 | daniel-mar | 32 | |
33 | OIDplus::init(true); |
||
34 | |||
1050 | daniel-mar | 35 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPageAdminNostalgia', false)) { |
754 | daniel-mar | 36 | throw new OIDplusException(_L('This plugin was disabled by the system administrator!')); |
37 | } |
||
38 | |||
756 | daniel-mar | 39 | if (!OIDplus::authUtils()->isAdminLoggedIn()) { |
1201 | daniel-mar | 40 | throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin'))); |
756 | daniel-mar | 41 | } |
42 | |||
755 | daniel-mar | 43 | if (!class_exists('ZipArchive')) { |
44 | 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.')); |
||
45 | } |
||
46 | |||
754 | daniel-mar | 47 | $dos_ids = array(); |
48 | $parent_oids = array(); |
||
49 | $i = 0; |
||
1053 | daniel-mar | 50 | |
51 | // Root node |
||
52 | $dos_ids[''] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
||
754 | daniel-mar | 53 | $parent_oids[''] = ''; |
1053 | daniel-mar | 54 | $iri[''] = array(); |
55 | $asn1[''] = array(); |
||
56 | $title[''] = 'OID Root'; |
||
57 | $description[''] = 'Exported by OIDplus 2.0'; |
||
58 | $created[''] = ''; |
||
59 | $updated[''] = ''; |
||
754 | daniel-mar | 60 | |
1053 | daniel-mar | 61 | // Now check all OIDs |
1148 | daniel-mar | 62 | $res = OIDplus::db()->query("select * from ###objects where id like 'oid:%'"); |
1156 | daniel-mar | 63 | $res->naturalSortByField('id'); |
754 | daniel-mar | 64 | while ($row = $res->fetch_object()) { |
65 | $oid = substr($row->id, strlen('oid:')); |
||
66 | $parent_oid = substr($row->parent, strlen('oid:')); |
||
1053 | daniel-mar | 67 | |
757 | daniel-mar | 68 | $dos_ids[$oid] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
1053 | daniel-mar | 69 | fill_asn1($oid, $asn1); |
70 | //fill_iri($oid, $iri); |
||
1064 | daniel-mar | 71 | $title[$oid] = vts_utf8_decode($row->title); |
72 | $description[$oid] = vts_utf8_decode($row->description); |
||
1053 | daniel-mar | 73 | $created[$oid] = $row->created; |
74 | $updated[$oid] = $row->updated; |
||
75 | |||
76 | if ((oid_len($oid) > 1) && ($parent_oid == '')) { |
||
77 | do { |
||
78 | $real_parent = oid_len($oid) > 1 ? oid_up($oid) : ''; |
||
79 | $parent_oids[$oid] = $real_parent; |
||
80 | |||
81 | if (isset($dos_ids[$real_parent])) break; // did we already handle this parent node? |
||
82 | |||
83 | $dos_ids[$real_parent] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
||
84 | fill_asn1($real_parent, $asn1); // well-known OIDs? |
||
85 | //fill_iri($real_parent, $iri); // well-known OIDs? |
||
86 | $title[$real_parent] = ''; |
||
87 | $description[$real_parent] = ''; |
||
88 | $created[$real_parent] = ''; |
||
89 | $updated[$real_parent] = ''; |
||
90 | $res2 = OIDplus::db()->query("select * from ###objects where id = 'oid:$real_parent'"); |
||
91 | while ($row2 = $res2->fetch_object()) { |
||
1064 | daniel-mar | 92 | $title[$real_parent] = vts_utf8_decode($row2->title); |
93 | $description[$real_parent] = vts_utf8_decode($row2->description); |
||
1053 | daniel-mar | 94 | $created[$real_parent] = $row2->created; |
95 | $updated[$real_parent] = $row2->updated; |
||
96 | } |
||
97 | |||
98 | // next |
||
99 | if ($real_parent == '') break; |
||
100 | $oid = $real_parent; |
||
101 | } while (true); |
||
754 | daniel-mar | 102 | } else { |
103 | $parent_oids[$oid] = $parent_oid; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $tmp_file = OIDplus::localpath().'userdata/windows_export.zip'; |
||
108 | |||
109 | $zip = new ZipArchive(); |
||
110 | if ($zip->open($tmp_file, ZipArchive::CREATE)!== true) { |
||
1201 | daniel-mar | 111 | throw new OIDplusException(_L("Cannot open file %1", $tmp_file)); |
754 | daniel-mar | 112 | } |
113 | |||
114 | $cont = ''; |
||
115 | |||
116 | foreach ($dos_ids as $oid => $dos_id) { |
||
117 | $cont .= "[OID:$oid]\r\n"; |
||
118 | |||
119 | $i = 1; |
||
120 | foreach ($parent_oids as $child_oid => $parent_oid) { |
||
121 | if ($child_oid == '') continue; |
||
122 | if ($parent_oid == $oid) { |
||
123 | $cont .= "delegate$i=OID:$child_oid\r\n"; |
||
124 | $i++; |
||
125 | } |
||
126 | } |
||
1064 | daniel-mar | 127 | $cont .= "delegates=".($i-1)."\r\n"; |
754 | daniel-mar | 128 | |
129 | if ($oid != '') { |
||
130 | $asnids = array(); |
||
1053 | daniel-mar | 131 | foreach ($asn1[$oid] as $name) { |
132 | $asnids[] = $name; |
||
754 | daniel-mar | 133 | } |
134 | $asnids = implode(',', $asnids); |
||
135 | if ($asnids != '') $cont .= "asn1id=$asnids\r\n"; |
||
136 | |||
137 | /* |
||
138 | $iris = array(); |
||
1053 | daniel-mar | 139 | foreach ($iri[$oid] as $name) { |
140 | $iris[] = $name; |
||
754 | daniel-mar | 141 | } |
142 | $iris = implode(',', $iris); |
||
143 | if ($iris != '') $cont .= "iri=$iris\r\n"; |
||
144 | */ |
||
145 | |||
1053 | daniel-mar | 146 | if ($title[$oid] != '') $cont .= "description=".$title[$oid]."\r\n"; |
754 | daniel-mar | 147 | |
1053 | daniel-mar | 148 | if ($updated[$oid] != '') $cont .= "updatedate=".explode(' ',$updated[$oid])[0]."\r\n"; |
149 | if ($created[$oid] != '') $cont .= "createdate=".explode(' ',$created[$oid])[0]."\r\n"; |
||
754 | daniel-mar | 150 | |
1130 | daniel-mar | 151 | $desc = handleDesc_win($description[$oid]); |
1053 | daniel-mar | 152 | if (trim($desc) != '') { |
754 | daniel-mar | 153 | $cont .= "information=$dos_id.TXT\r\n"; |
154 | $zip->addFromString("DB//$dos_id.TXT", $desc); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | //echo '<pre>'.$cont.'</pre>'; |
||
160 | //die(); |
||
161 | |||
162 | $settings = array(); |
||
163 | $settings[] = '[SETTINGS]'; |
||
164 | $settings[] = 'DATA=DB\\'; |
||
165 | $zip->addFromString("OIDPLUS.INI", implode("\r\n",$settings)."\r\n"); |
||
166 | |||
167 | $zip->addFromString('DB//OID.INI', $cont); |
||
168 | |||
169 | $exe_url = 'https://github.com/danielmarschall/oidplus_win95/raw/master/OIDPLUS.exe'; |
||
963 | daniel-mar | 170 | $exe = url_get_contents($exe_url); |
1149 | daniel-mar | 171 | if ($exe === false) { |
754 | daniel-mar | 172 | throw new OIDplusException(_L("Cannot download the binary file from GitHub (%1)", $exe_url)); |
173 | } |
||
174 | $zip->addFromString('OIDPLS32.EXE', $exe); |
||
175 | |||
176 | $exe_url = 'https://github.com/danielmarschall/oidplus_win311/raw/master/OIDPLUS.exe'; |
||
963 | daniel-mar | 177 | $exe = url_get_contents($exe_url); |
1149 | daniel-mar | 178 | if ($exe === false) { |
754 | daniel-mar | 179 | throw new OIDplusException(_L("Cannot download the binary file from GitHub (%1)", $exe_url)); |
180 | } |
||
181 | $zip->addFromString('OIDPLS16.EXE', $exe); |
||
182 | |||
183 | $zip->close(); |
||
184 | |||
185 | if (!headers_sent()) { |
||
186 | header('Content-Type: application/zip'); |
||
187 | header('Content-Disposition: attachment; filename=oidplus_windows.zip'); |
||
188 | readfile($tmp_file); |
||
189 | } |
||
190 | |||
191 | unlink($tmp_file); |
||
192 | |||
193 | OIDplus::invoke_shutdown(); |
||
991 | daniel-mar | 194 | |
195 | # --- |
||
196 | |||
1130 | daniel-mar | 197 | /** |
198 | * @param string $oid |
||
199 | * @param array $asn1 |
||
200 | * @return void |
||
201 | * @throws OIDplusException |
||
202 | */ |
||
203 | function fill_asn1(string $oid, array &$asn1) { |
||
1053 | daniel-mar | 204 | if (!isset($asn1[$oid])) $asn1[$oid] = array(); |
205 | $res = OIDplus::db()->query("select * from ###asn1id where oid = 'oid:$oid'"); |
||
206 | while ($row = $res->fetch_object()) { |
||
207 | $asn1[$oid][] = $row->name; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /* |
||
212 | function fill_iri($oid, &$iri) { |
||
213 | if (!isset($iri[$oid])) $iri[$oid] = array(); |
||
214 | $res = OIDplus::db()->query("select * from ###iri where oid = 'oid:$oid'"); |
||
215 | while ($row = $res->fetch_object()) { |
||
216 | $iri[$oid][] = $row->name; |
||
217 | } |
||
218 | } |
||
219 | */ |
||
220 | |||
1130 | daniel-mar | 221 | /** |
222 | * @param string $desc |
||
223 | * @return string |
||
224 | */ |
||
225 | function handleDesc_win(string $desc): string { |
||
991 | daniel-mar | 226 | $desc = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $desc); // br2nl |
227 | $desc = strip_tags($desc); |
||
228 | $desc = str_replace(' ', ' ', $desc); |
||
229 | $desc = html_entity_decode($desc); |
||
230 | $desc = str_replace("\r", "", $desc); |
||
231 | $desc = str_replace("\n", "\r\n", $desc); |
||
1130 | daniel-mar | 232 | return trim($desc)."\r\n"; |
991 | daniel-mar | 233 | } |