Subversion Repositories oidplus

Rev

Rev 1321 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 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.  
  20. use ViaThinkSoft\OIDplus\OIDplus;
  21. use ViaThinkSoft\OIDplus\OIDplusGui;
  22. use ViaThinkSoft\OIDplus\OIDplusException;
  23. use ViaThinkSoft\OIDplus\OIDplusHtmlException;
  24.  
  25. header('Content-Type:text/html; charset=UTF-8');
  26.  
  27. require_once __DIR__ . '/../../../../includes/oidplus.inc.php';
  28.  
  29. set_exception_handler(array(OIDplusGui::class, 'html_exception_handler'));
  30.  
  31. @set_time_limit(0);
  32.  
  33. OIDplus::init(true);
  34.  
  35. if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplusPageAdminNostalgia', false)) {
  36.         throw new OIDplusException(_L('This plugin was disabled by the system administrator!'));
  37. }
  38.  
  39. if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  40.         throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), null, 401);
  41. }
  42.  
  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.  
  47. $dos_ids = array();
  48. $parent_oids = array();
  49. $i = 0;
  50.  
  51. // Root node
  52. $dos_ids[''] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT);
  53. $parent_oids[''] = '';
  54. $iri[''] = array();
  55. $asn1[''] = array();
  56. $title[''] = 'OID Root';
  57. $description[''] = 'Exported by OIDplus 2.0';
  58. $created[''] = '';
  59. $updated[''] = '';
  60.  
  61. // Now check all OIDs
  62. $res = OIDplus::db()->query("select * from ###objects where id like 'oid:%'");
  63. $res->naturalSortByField('id');
  64. while ($row = $res->fetch_object()) {
  65.         $oid = substr($row->id, strlen('oid:'));
  66.         $parent_oid = substr($row->parent, strlen('oid:'));
  67.  
  68.         $dos_ids[$oid] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT);
  69.         fill_asn1($oid, $asn1);
  70.         //fill_iri($oid, $iri);
  71.         $title[$oid] = vts_utf8_decode($row->title);
  72.         $description[$oid] = vts_utf8_decode($row->description);
  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()) {
  92.                                 $title[$real_parent] = vts_utf8_decode($row2->title);
  93.                                 $description[$real_parent] = vts_utf8_decode($row2->description);
  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);
  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) {
  111.         throw new OIDplusException(_L("Cannot open file %1", $tmp_file));
  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.         }
  127.         $cont .= "delegates=".($i-1)."\r\n";
  128.  
  129.         if ($oid != '') {
  130.                 $asnids = array();
  131.                 foreach ($asn1[$oid] as $name) {
  132.                         $asnids[] = $name;
  133.                 }
  134.                 $asnids = implode(',', $asnids);
  135.                 if ($asnids != '') $cont .= "asn1id=$asnids\r\n";
  136.  
  137.                 /*
  138.                 $iris = array();
  139.                 foreach ($iri[$oid] as $name) {
  140.                         $iris[] = $name;
  141.                 }
  142.                 $iris = implode(',', $iris);
  143.                 if ($iris != '') $cont .= "iri=$iris\r\n";
  144.                 */
  145.  
  146.                 if ($title[$oid] != '') $cont .= "description=".$title[$oid]."\r\n";
  147.  
  148.                 if ($updated[$oid] != '') $cont .= "updatedate=".explode(' ',$updated[$oid])[0]."\r\n";
  149.                 if ($created[$oid] != '') $cont .= "createdate=".explode(' ',$created[$oid])[0]."\r\n";
  150.  
  151.                 $desc = handleDesc_win($description[$oid]);
  152.                 if (trim($desc) != '') {
  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';
  170. $exe = url_get_contents($exe_url);
  171. if ($exe === false) {
  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';
  177. $exe = url_get_contents($exe_url);
  178. if ($exe === false) {
  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();
  194.  
  195. # ---
  196.  
  197. /**
  198.  * @param string $oid
  199.  * @param array $asn1
  200.  * @return void
  201.  * @throws OIDplusException
  202.  */
  203. function fill_asn1(string $oid, array &$asn1) {
  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.  
  221. /**
  222.  * @param string $desc
  223.  * @return string
  224.  */
  225. function handleDesc_win(string $desc): string {
  226.         $desc = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $desc); // br2nl
  227.         $desc = strip_tags($desc);
  228.         $desc = str_replace('&nbsp;', ' ', $desc);
  229.         $desc = html_entity_decode($desc);
  230.         $desc = str_replace("\r", "", $desc);
  231.         $desc = str_replace("\n", "\r\n", $desc);
  232.         return trim($desc)."\r\n";
  233. }
  234.