Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1148 | 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;
1053 daniel-mar 49
 
50
// Root node
51
$dos_ids[''] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT);
754 daniel-mar 52
$parent_oids[''] = '';
1053 daniel-mar 53
$iri[''] = array();
54
$asn1[''] = array();
55
$title[''] = 'OID Root';
56
$description[''] = 'Exported by OIDplus 2.0';
754 daniel-mar 57
 
1053 daniel-mar 58
// Now check all OIDs
754 daniel-mar 59
$res = OIDplus::db()->query("select * from ###objects where id like 'oid:%' order by ".OIDplus::db()->natOrder('id'));
60
while ($row = $res->fetch_object()) {
61
        $oid = substr($row->id, strlen('oid:'));
62
        $parent_oid = substr($row->parent, strlen('oid:'));
1053 daniel-mar 63
 
757 daniel-mar 64
        $dos_ids[$oid] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT);
1053 daniel-mar 65
        fill_asn1($oid, $asn1);
66
        fill_iri($oid, $iri);
1064 daniel-mar 67
        $title[$oid] = vts_utf8_decode($row->title);
68
        $description[$oid] = vts_utf8_decode($row->description);
1053 daniel-mar 69
 
70
        if ((oid_len($oid) > 1) && ($parent_oid == '')) {
71
                do {
72
                        $real_parent = oid_len($oid) > 1 ? oid_up($oid) : '';
73
                        $parent_oids[$oid] = $real_parent;
74
 
75
                        if (isset($dos_ids[$real_parent])) break; // did we already handle this parent node?
76
 
77
                        $dos_ids[$real_parent] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT);
78
                        fill_asn1($real_parent, $asn1); // well-known OIDs?
79
                        fill_iri($real_parent, $iri); // well-known OIDs?
80
                        $title[$real_parent] = '';
81
                        $description[$real_parent] = '';
82
                        $res2 = OIDplus::db()->query("select * from ###objects where id = 'oid:$real_parent'");
83
                        while ($row2 = $res2->fetch_object()) {
1064 daniel-mar 84
                                $title[$real_parent] = vts_utf8_decode($row2->title);
85
                                $description[$real_parent] = vts_utf8_decode($row2->description);
1053 daniel-mar 86
                        }
87
 
88
                        // next
89
                        if ($real_parent == '') break;
90
                        $oid = $real_parent;
91
                } while (true);
754 daniel-mar 92
        } else {
93
                $parent_oids[$oid] = $parent_oid;
94
        }
95
}
96
 
97
$tmp_file = OIDplus::localpath().'userdata/dos_export.zip';
98
 
99
$zip = new ZipArchive();
100
if ($zip->open($tmp_file, ZipArchive::CREATE)!== true) {
101
        throw new OIDplusException("cannot open <$tmp_file>");
102
}
103
 
1130 daniel-mar 104
/**
105
 * @param string $command
106
 * @param string $data
107
 * @return string
108
 */
109
function make_line(string $command, string $data): string {
977 daniel-mar 110
        return $command.$data."\r\n";
111
}
112
 
113
// https://github.com/danielmarschall/oidplus_dos/blob/master/OIDFILE.PAS
1116 daniel-mar 114
const CMD_VERSION         = 'VERS';
115
const CMD_OWN_ID          = 'SELF';
116
const CMD_PARENT          = 'SUPR';
117
const CMD_CHILD           = 'CHLD';
118
const CMD_ASN1_IDENTIFIER = 'ASN1';
119
const CMD_UNICODE_LABEL   = 'UNIL';
120
const CMD_DESCRIPTION     = 'DESC';
977 daniel-mar 121
 
754 daniel-mar 122
foreach ($dos_ids as $oid => $dos_id) {
1130 daniel-mar 123
        $cont = make_line(CMD_VERSION, '2022');
754 daniel-mar 124
 
977 daniel-mar 125
        $cont .= make_line(CMD_OWN_ID, $dos_id.$oid);
754 daniel-mar 126
 
127
        $parent_oid = $parent_oids[$oid];
128
        $parent_id = $dos_ids[$parent_oid];
977 daniel-mar 129
        $cont .= make_line(CMD_PARENT, $parent_id.$parent_oid);
754 daniel-mar 130
 
131
        foreach ($parent_oids as $child_oid => $parent_oid) {
132
                if ($child_oid == '') continue;
133
                if ($parent_oid == $oid) {
134
                        $child_id = $dos_ids[$child_oid];
977 daniel-mar 135
                        $cont .= make_line(CMD_CHILD, $child_id.$child_oid);
754 daniel-mar 136
                }
137
        }
138
 
1053 daniel-mar 139
        foreach ($asn1[$oid] as $name) {
140
                $cont .= make_line(CMD_ASN1_IDENTIFIER, $name);
754 daniel-mar 141
        }
142
 
1053 daniel-mar 143
        foreach ($iri[$oid] as $name) {
144
                $cont .= make_line(CMD_UNICODE_LABEL, $name);
754 daniel-mar 145
        }
146
 
1130 daniel-mar 147
        $desc_ary1 = handleDesc_dos($title[$oid]);
148
        $desc_ary2 = handleDesc_dos($description[$oid]);
1053 daniel-mar 149
        $desc_ary = array_merge($desc_ary1, $desc_ary2);
150
        $prev_line = '';
151
        foreach ($desc_ary as $line_idx => $line) {
152
                if ($line == $prev_line) continue;
153
                if ($line_idx >= 10/*DESCEDIT_LINES*/) break;
154
                $cont .= make_line(CMD_DESCRIPTION, $line);
155
                $prev_line = $line;
754 daniel-mar 156
        }
157
 
158
        //echo "****$dos_id.OID\r\n";
159
        //echo "$cont\r\n";
160
 
161
        $zip->addFromString("$dos_id.OID", $cont);
162
}
163
 
164
$exe_url = 'https://github.com/danielmarschall/oidplus_dos/raw/master/OIDPLUS.EXE';
963 daniel-mar 165
$exe = url_get_contents($exe_url);
166
if (!$exe) {
754 daniel-mar 167
        throw new OIDplusException(_L("Cannot download the binary file from GitHub (%1)", $exe_url));
168
}
169
$zip->addFromString('OIDPLUS.EXE', $exe);
170
 
171
$zip->close();
172
 
173
if (!headers_sent()) {
174
        header('Content-Type: application/zip');
175
        header('Content-Disposition: attachment; filename=oidplus_dos.zip');
176
        readfile($tmp_file);
177
}
178
 
179
unlink($tmp_file);
180
 
181
OIDplus::invoke_shutdown();
991 daniel-mar 182
 
183
# ---
184
 
1130 daniel-mar 185
/**
186
 * @param string $oid
187
 * @param array $asn1
188
 * @return void
189
 * @throws OIDplusException
190
 */
191
function fill_asn1(string $oid, array &$asn1) {
1053 daniel-mar 192
        if (!isset($asn1[$oid])) $asn1[$oid] = array();
193
        $res = OIDplus::db()->query("select * from ###asn1id where oid = 'oid:$oid'");
194
        while ($row = $res->fetch_object()) {
195
                $asn1[$oid][] = $row->name;
196
        }
197
}
198
 
1130 daniel-mar 199
/**
200
 * @param string $oid
201
 * @param array $iri
202
 * @return void
203
 * @throws OIDplusException
204
 */
205
function fill_iri(string $oid, array &$iri) {
1053 daniel-mar 206
        if (!isset($iri[$oid])) $iri[$oid] = array();
207
        $res = OIDplus::db()->query("select * from ###iri where oid = 'oid:$oid'");
208
        while ($row = $res->fetch_object()) {
209
                $iri[$oid][] = $row->name;
210
        }
211
}
212
 
1130 daniel-mar 213
/**
214
 * @param string $desc
215
 * @return array
216
 */
217
function handleDesc_dos(string $desc): array {
991 daniel-mar 218
        $desc = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $desc); // br2nl
219
        $desc = strip_tags($desc);
220
        $desc = str_replace('&nbsp;', ' ', $desc);
221
        $desc = html_entity_decode($desc);
222
        $desc = str_replace("\r", "", $desc);
223
        $desc = str_replace("\n", "  ", $desc);
224
        $desc = str_replace("\t", "  ", $desc);
225
        $desc = trim($desc);
226
        $desc_ary = explode("\r\n", wordwrap($desc, 75, "\r\n", true));
227
        if (implode('',$desc_ary) == '') $desc_ary = array();
228
        return $desc_ary;
229
}