Rev 991 | Rev 1053 | 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; |
||
23 | |||
754 | daniel-mar | 24 | header('Content-Type:text/html; charset=UTF-8'); |
25 | |||
26 | require_once __DIR__ . '/../../../../includes/oidplus.inc.php'; |
||
27 | |||
1050 | daniel-mar | 28 | set_exception_handler(array(OIDplusGui::class, 'html_exception_handler')); |
754 | daniel-mar | 29 | |
757 | daniel-mar | 30 | @set_time_limit(0); |
31 | |||
754 | daniel-mar | 32 | OIDplus::init(true); |
33 | |||
1050 | daniel-mar | 34 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPageAdminNostalgia', false)) { |
754 | daniel-mar | 35 | throw new OIDplusException(_L('This plugin was disabled by the system administrator!')); |
36 | } |
||
37 | |||
756 | daniel-mar | 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'))); |
||
40 | } |
||
41 | |||
755 | daniel-mar | 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.')); |
||
44 | } |
||
45 | |||
754 | daniel-mar | 46 | $dos_ids = array(); |
47 | $parent_oids = array(); |
||
48 | $i = 0; |
||
49 | $dos_ids[''] = '00000000'; |
||
50 | $parent_oids[''] = ''; |
||
51 | |||
757 | daniel-mar | 52 | $dos_ids[''] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
754 | daniel-mar | 53 | $res = OIDplus::db()->query("select * from ###objects where id like 'oid:%' order by ".OIDplus::db()->natOrder('id')); |
54 | while ($row = $res->fetch_object()) { |
||
55 | $oid = substr($row->id, strlen('oid:')); |
||
56 | $parent_oid = substr($row->parent, strlen('oid:')); |
||
757 | daniel-mar | 57 | $dos_ids[$oid] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT); |
754 | daniel-mar | 58 | if ($parent_oid == '') { |
59 | $parent_oids[$oid] = ''; |
||
60 | } else { |
||
61 | $parent_oids[$oid] = $parent_oid; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | $tmp_file = OIDplus::localpath().'userdata/dos_export.zip'; |
||
66 | |||
67 | $zip = new ZipArchive(); |
||
68 | if ($zip->open($tmp_file, ZipArchive::CREATE)!== true) { |
||
69 | throw new OIDplusException("cannot open <$tmp_file>"); |
||
70 | } |
||
71 | |||
977 | daniel-mar | 72 | function make_line($command, $data) { |
73 | return $command.$data."\r\n"; |
||
74 | } |
||
75 | |||
76 | // https://github.com/danielmarschall/oidplus_dos/blob/master/OIDFILE.PAS |
||
77 | define('CMD_VERSION', 'VERS'); |
||
78 | define('CMD_OWN_ID', 'SELF'); |
||
79 | define('CMD_PARENT', 'SUPR'); |
||
80 | define('CMD_CHILD', 'CHLD'); |
||
81 | define('CMD_ASN1_IDENTIFIER', 'ASN1'); |
||
82 | define('CMD_UNICODE_LABEL', 'UNIL'); |
||
83 | define('CMD_DESCRIPTION', 'DESC'); |
||
84 | |||
754 | daniel-mar | 85 | foreach ($dos_ids as $oid => $dos_id) { |
86 | $cont = ''; |
||
87 | |||
977 | daniel-mar | 88 | $cont .= make_line(CMD_VERSION, 2022); |
754 | daniel-mar | 89 | |
977 | daniel-mar | 90 | $cont .= make_line(CMD_OWN_ID, $dos_id.$oid); |
754 | daniel-mar | 91 | |
92 | $parent_oid = $parent_oids[$oid]; |
||
93 | $parent_id = $dos_ids[$parent_oid]; |
||
977 | daniel-mar | 94 | $cont .= make_line(CMD_PARENT, $parent_id.$parent_oid); |
754 | daniel-mar | 95 | |
96 | foreach ($parent_oids as $child_oid => $parent_oid) { |
||
97 | if ($child_oid == '') continue; |
||
98 | if ($parent_oid == $oid) { |
||
99 | $child_id = $dos_ids[$child_oid]; |
||
977 | daniel-mar | 100 | $cont .= make_line(CMD_CHILD, $child_id.$child_oid); |
754 | daniel-mar | 101 | } |
102 | } |
||
103 | |||
104 | $res = OIDplus::db()->query("select * from ###asn1id where oid = 'oid:$oid'"); |
||
105 | while ($row = $res->fetch_object()) { |
||
106 | $asn1 = $row->name; |
||
977 | daniel-mar | 107 | $cont .= make_line(CMD_ASN1_IDENTIFIER, $asn1); |
754 | daniel-mar | 108 | } |
109 | |||
110 | $res = OIDplus::db()->query("select * from ###iri where oid = 'oid:$oid'"); |
||
111 | while ($row = $res->fetch_object()) { |
||
112 | $iri = $row->name; |
||
977 | daniel-mar | 113 | $cont .= make_line(CMD_UNICODE_LABEL, $iri); |
754 | daniel-mar | 114 | } |
115 | |||
116 | if ($oid == '') { |
||
778 | daniel-mar | 117 | // TODO: Split our OIDplus root OIDs into the real OID tree (1, 1.3, 1.3.6, ...) |
977 | daniel-mar | 118 | $cont .= make_line(CMD_DESCRIPTION, 'Here, you can find the root OIDs'); |
754 | daniel-mar | 119 | } else { |
120 | $res = OIDplus::db()->query("select * from ###objects where id = 'oid:$oid';"); |
||
121 | $row = $res->fetch_object(); |
||
991 | daniel-mar | 122 | $desc_ary1 = handleDesc($row->title); |
123 | $desc_ary2 = handleDesc($row->description); |
||
754 | daniel-mar | 124 | $desc_ary = array_merge($desc_ary1, $desc_ary2); |
991 | daniel-mar | 125 | $prev_line = ''; |
754 | daniel-mar | 126 | foreach ($desc_ary as $line_idx => $line) { |
991 | daniel-mar | 127 | if ($line == $prev_line) continue; |
754 | daniel-mar | 128 | if ($line_idx >= 10/*DESCEDIT_LINES*/) break; |
977 | daniel-mar | 129 | $cont .= make_line(CMD_DESCRIPTION, $line); |
991 | daniel-mar | 130 | $prev_line = $line; |
754 | daniel-mar | 131 | } |
132 | } |
||
133 | |||
134 | //echo "****$dos_id.OID\r\n"; |
||
135 | //echo "$cont\r\n"; |
||
136 | |||
137 | $zip->addFromString("$dos_id.OID", $cont); |
||
138 | } |
||
139 | |||
140 | $exe_url = 'https://github.com/danielmarschall/oidplus_dos/raw/master/OIDPLUS.EXE'; |
||
963 | daniel-mar | 141 | $exe = url_get_contents($exe_url); |
142 | if (!$exe) { |
||
754 | daniel-mar | 143 | throw new OIDplusException(_L("Cannot download the binary file from GitHub (%1)", $exe_url)); |
144 | } |
||
145 | $zip->addFromString('OIDPLUS.EXE', $exe); |
||
146 | |||
147 | $zip->close(); |
||
148 | |||
149 | if (!headers_sent()) { |
||
150 | header('Content-Type: application/zip'); |
||
151 | header('Content-Disposition: attachment; filename=oidplus_dos.zip'); |
||
152 | readfile($tmp_file); |
||
153 | } |
||
154 | |||
155 | unlink($tmp_file); |
||
156 | |||
157 | OIDplus::invoke_shutdown(); |
||
991 | daniel-mar | 158 | |
159 | # --- |
||
160 | |||
161 | function handleDesc($desc) { |
||
162 | $desc = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $desc); // br2nl |
||
163 | $desc = strip_tags($desc); |
||
164 | $desc = str_replace(' ', ' ', $desc); |
||
165 | $desc = html_entity_decode($desc); |
||
166 | $desc = str_replace("\r", "", $desc); |
||
167 | $desc = str_replace("\n", " ", $desc); |
||
168 | $desc = str_replace("\t", " ", $desc); |
||
169 | $desc = trim($desc); |
||
170 | $desc_ary = explode("\r\n", wordwrap($desc, 75, "\r\n", true)); |
||
171 | if (implode('',$desc_ary) == '') $desc_ary = array(); |
||
172 | return $desc_ary; |
||
173 | } |