Subversion Repositories oidplus

Rev

Rev 1149 | Rev 1201 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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