Subversion Repositories oidplus

Rev

Rev 1149 | Rev 1201 | 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);
754 daniel-mar 31
 
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';
57
$created[''] = '';
58
$updated[''] = '';
754 daniel-mar 59
 
1053 daniel-mar 60
// Now check all OIDs
1148 daniel-mar 61
$res = OIDplus::db()->query("select * from ###objects where id like 'oid:%'");
1156 daniel-mar 62
$res->naturalSortByField('id');
754 daniel-mar 63
while ($row = $res->fetch_object()) {
64
        $oid = substr($row->id, strlen('oid:'));
65
        $parent_oid = substr($row->parent, strlen('oid:'));
1053 daniel-mar 66
 
757 daniel-mar 67
        $dos_ids[$oid] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT);
1053 daniel-mar 68
        fill_asn1($oid, $asn1);
69
        //fill_iri($oid, $iri);
1064 daniel-mar 70
        $title[$oid] = vts_utf8_decode($row->title);
71
        $description[$oid] = vts_utf8_decode($row->description);
1053 daniel-mar 72
        $created[$oid] = $row->created;
73
        $updated[$oid] = $row->updated;
74
 
75
        if ((oid_len($oid) > 1) && ($parent_oid == '')) {
76
                do {
77
                        $real_parent = oid_len($oid) > 1 ? oid_up($oid) : '';
78
                        $parent_oids[$oid] = $real_parent;
79
 
80
                        if (isset($dos_ids[$real_parent])) break; // did we already handle this parent node?
81
 
82
                        $dos_ids[$real_parent] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT);
83
                        fill_asn1($real_parent, $asn1); // well-known OIDs?
84
                        //fill_iri($real_parent, $iri); // well-known OIDs?
85
                        $title[$real_parent] = '';
86
                        $description[$real_parent] = '';
87
                        $created[$real_parent] = '';
88
                        $updated[$real_parent] = '';
89
                        $res2 = OIDplus::db()->query("select * from ###objects where id = 'oid:$real_parent'");
90
                        while ($row2 = $res2->fetch_object()) {
1064 daniel-mar 91
                                $title[$real_parent] = vts_utf8_decode($row2->title);
92
                                $description[$real_parent] = vts_utf8_decode($row2->description);
1053 daniel-mar 93
                                $created[$real_parent] = $row2->created;
94
                                $updated[$real_parent] = $row2->updated;
95
                        }
96
 
97
                        // next
98
                        if ($real_parent == '') break;
99
                        $oid = $real_parent;
100
                } while (true);
754 daniel-mar 101
        } else {
102
                $parent_oids[$oid] = $parent_oid;
103
        }
104
}
105
 
106
$tmp_file = OIDplus::localpath().'userdata/windows_export.zip';
107
 
108
$zip = new ZipArchive();
109
if ($zip->open($tmp_file, ZipArchive::CREATE)!== true) {
110
        throw new OIDplusException("cannot open <$tmp_file>");
111
}
112
 
113
$cont = '';
114
 
115
foreach ($dos_ids as $oid => $dos_id) {
116
        $cont .= "[OID:$oid]\r\n";
117
 
118
        $i = 1;
119
        foreach ($parent_oids as $child_oid => $parent_oid) {
120
                if ($child_oid == '') continue;
121
                if ($parent_oid == $oid) {
122
                        $cont .= "delegate$i=OID:$child_oid\r\n";
123
                        $i++;
124
                }
125
        }
1064 daniel-mar 126
        $cont .= "delegates=".($i-1)."\r\n";
754 daniel-mar 127
 
128
        if ($oid != '') {
129
                $asnids = array();
1053 daniel-mar 130
                foreach ($asn1[$oid] as $name) {
131
                        $asnids[] = $name;
754 daniel-mar 132
                }
133
                $asnids = implode(',', $asnids);
134
                if ($asnids != '') $cont .= "asn1id=$asnids\r\n";
135
 
136
                /*
137
                $iris = array();
1053 daniel-mar 138
                foreach ($iri[$oid] as $name) {
139
                        $iris[] = $name;
754 daniel-mar 140
                }
141
                $iris = implode(',', $iris);
142
                if ($iris != '') $cont .= "iri=$iris\r\n";
143
                */
144
 
1053 daniel-mar 145
                if ($title[$oid] != '') $cont .= "description=".$title[$oid]."\r\n";
754 daniel-mar 146
 
1053 daniel-mar 147
                if ($updated[$oid] != '') $cont .= "updatedate=".explode(' ',$updated[$oid])[0]."\r\n";
148
                if ($created[$oid] != '') $cont .= "createdate=".explode(' ',$created[$oid])[0]."\r\n";
754 daniel-mar 149
 
1130 daniel-mar 150
                $desc = handleDesc_win($description[$oid]);
1053 daniel-mar 151
                if (trim($desc) != '') {
754 daniel-mar 152
                        $cont .= "information=$dos_id.TXT\r\n";
153
                        $zip->addFromString("DB//$dos_id.TXT", $desc);
154
                }
155
        }
156
}
157
 
158
//echo '<pre>'.$cont.'</pre>';
159
//die();
160
 
161
$settings = array();
162
$settings[] = '[SETTINGS]';
163
$settings[] = 'DATA=DB\\';
164
$zip->addFromString("OIDPLUS.INI", implode("\r\n",$settings)."\r\n");
165
 
166
$zip->addFromString('DB//OID.INI', $cont);
167
 
168
$exe_url = 'https://github.com/danielmarschall/oidplus_win95/raw/master/OIDPLUS.exe';
963 daniel-mar 169
$exe = url_get_contents($exe_url);
1149 daniel-mar 170
if ($exe === false) {
754 daniel-mar 171
        throw new OIDplusException(_L("Cannot download the binary file from GitHub (%1)", $exe_url));
172
}
173
$zip->addFromString('OIDPLS32.EXE', $exe);
174
 
175
$exe_url = 'https://github.com/danielmarschall/oidplus_win311/raw/master/OIDPLUS.exe';
963 daniel-mar 176
$exe = url_get_contents($exe_url);
1149 daniel-mar 177
if ($exe === false) {
754 daniel-mar 178
        throw new OIDplusException(_L("Cannot download the binary file from GitHub (%1)", $exe_url));
179
}
180
$zip->addFromString('OIDPLS16.EXE', $exe);
181
 
182
$zip->close();
183
 
184
if (!headers_sent()) {
185
        header('Content-Type: application/zip');
186
        header('Content-Disposition: attachment; filename=oidplus_windows.zip');
187
        readfile($tmp_file);
188
}
189
 
190
unlink($tmp_file);
191
 
192
OIDplus::invoke_shutdown();
991 daniel-mar 193
 
194
# ---
195
 
1130 daniel-mar 196
/**
197
 * @param string $oid
198
 * @param array $asn1
199
 * @return void
200
 * @throws OIDplusException
201
 */
202
function fill_asn1(string $oid, array &$asn1) {
1053 daniel-mar 203
        if (!isset($asn1[$oid])) $asn1[$oid] = array();
204
        $res = OIDplus::db()->query("select * from ###asn1id where oid = 'oid:$oid'");
205
        while ($row = $res->fetch_object()) {
206
                $asn1[$oid][] = $row->name;
207
        }
208
}
209
 
210
/*
211
function fill_iri($oid, &$iri) {
212
        if (!isset($iri[$oid])) $iri[$oid] = array();
213
        $res = OIDplus::db()->query("select * from ###iri where oid = 'oid:$oid'");
214
        while ($row = $res->fetch_object()) {
215
                $iri[$oid][] = $row->name;
216
        }
217
}
218
*/
219
 
1130 daniel-mar 220
/**
221
 * @param string $desc
222
 * @return string
223
 */
224
function handleDesc_win(string $desc): string {
991 daniel-mar 225
        $desc = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $desc); // br2nl
226
        $desc = strip_tags($desc);
227
        $desc = str_replace('&nbsp;', ' ', $desc);
228
        $desc = html_entity_decode($desc);
229
        $desc = str_replace("\r", "", $desc);
230
        $desc = str_replace("\n", "\r\n", $desc);
1130 daniel-mar 231
        return trim($desc)."\r\n";
991 daniel-mar 232
}