Subversion Repositories oidplus

Rev

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