Subversion Repositories oidplus

Rev

Rev 778 | Rev 977 | 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. header('Content-Type:text/html; charset=UTF-8');
  21.  
  22. require_once __DIR__ . '/../../../../includes/oidplus.inc.php';
  23.  
  24. set_exception_handler(array('OIDplusGui', 'html_exception_handler'));
  25.  
  26. @set_time_limit(0);
  27.  
  28. OIDplus::init(true);
  29.  
  30. if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_OIDplusPageAdminNostalgia', false)) {
  31.         throw new OIDplusException(_L('This plugin was disabled by the system administrator!'));
  32. }
  33.  
  34. if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  35.         throw new OIDplusException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')));
  36. }
  37.  
  38. if (!class_exists('ZipArchive')) {
  39.         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.'));
  40. }
  41.  
  42. $dos_ids = array();
  43. $parent_oids = array();
  44. $i = 0;
  45. $dos_ids[''] = '00000000';
  46. $parent_oids[''] = '';
  47.  
  48. $dos_ids[''] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT);
  49. $res = OIDplus::db()->query("select * from ###objects where id like 'oid:%' order by ".OIDplus::db()->natOrder('id'));
  50. while ($row = $res->fetch_object()) {
  51.         $oid = substr($row->id, strlen('oid:'));
  52.         $parent_oid = substr($row->parent, strlen('oid:'));
  53.         $dos_ids[$oid] = str_pad(strval($i++), 8, '0', STR_PAD_LEFT);
  54.         if ($parent_oid == '') {
  55.                 $parent_oids[$oid] = '';
  56.         } else {
  57.                 $parent_oids[$oid] = $parent_oid;
  58.         }
  59. }
  60.  
  61. $tmp_file = OIDplus::localpath().'userdata/dos_export.zip';
  62.  
  63. $zip = new ZipArchive();
  64. if ($zip->open($tmp_file, ZipArchive::CREATE)!== true) {
  65.         throw new OIDplusException("cannot open <$tmp_file>");
  66. }
  67.  
  68. foreach ($dos_ids as $oid => $dos_id) {
  69.         $cont = '';
  70.  
  71.         $cont .= "VERS2022\r\n";
  72.  
  73.         $cont .= "SELF$dos_id$oid\r\n";
  74.  
  75.         $parent_oid = $parent_oids[$oid];
  76.         $parent_id = $dos_ids[$parent_oid];
  77.         $cont .= "SUPR$parent_id$parent_oid\r\n";
  78.  
  79.         foreach ($parent_oids as $child_oid => $parent_oid) {
  80.                 if ($child_oid == '') continue;
  81.                 if ($parent_oid == $oid) {
  82.                         $child_id = $dos_ids[$child_oid];
  83.                         $cont .= "CHLD$child_id$child_oid\r\n";
  84.                 }
  85.         }
  86.  
  87.         $res = OIDplus::db()->query("select * from ###asn1id where oid = 'oid:$oid'");
  88.         while ($row = $res->fetch_object()) {
  89.                 $asn1 = $row->name;
  90.                 $cont .= "ASN1$asn1\r\n";
  91.         }
  92.  
  93.         $res = OIDplus::db()->query("select * from ###iri where oid = 'oid:$oid'");
  94.         while ($row = $res->fetch_object()) {
  95.                 $iri = $row->name;
  96.                 $cont .= "UNIL$iri\r\n";
  97.         }
  98.  
  99.         if ($oid == '') {
  100.                 // TODO: Split our OIDplus root OIDs into the real OID tree (1, 1.3, 1.3.6, ...)
  101.                 $cont .= "DESCHere, you can find the root OIDs.\r\n";
  102.         } else {
  103.                 $res = OIDplus::db()->query("select * from ###objects where id = 'oid:$oid';");
  104.                 $row = $res->fetch_object();
  105.                 $desc = trim(trim(strip_tags($row->description)));
  106.                 $desc = str_replace("\r", "", $desc);
  107.                 $desc = str_replace("\n", "  ", $desc);
  108.                 $desc_ary1 = explode("\r\n", wordwrap($desc, 80/*TREEVIEW_WIDTH*/, "\r\n", true));
  109.                 $desc_ary2 = explode("\r\n", wordwrap($row->title, 80/*TREEVIEW_WIDTH*/, "\r\n", true));
  110.                 if (implode('',$desc_ary1) == '') $desc_ary1 = array();
  111.                 if (implode('',$desc_ary2) == '') $desc_ary2 = array();
  112.                 $desc_ary = array_merge($desc_ary1, $desc_ary2);
  113.                 foreach ($desc_ary as $line_idx => $line) {
  114.                         if ($line_idx >= 10/*DESCEDIT_LINES*/) break;
  115.                         $cont .= "DESC$line\r\n";
  116.                 }
  117.         }
  118.  
  119.         //echo "****$dos_id.OID\r\n";
  120.         //echo "$cont\r\n";
  121.  
  122.         $zip->addFromString("$dos_id.OID", $cont);
  123. }
  124.  
  125. $exe_url = 'https://github.com/danielmarschall/oidplus_dos/raw/master/OIDPLUS.EXE';
  126. $exe = url_get_contents($exe_url);
  127. if (!$exe) {
  128.         throw new OIDplusException(_L("Cannot download the binary file from GitHub (%1)", $exe_url));
  129. }
  130. $zip->addFromString('OIDPLUS.EXE', $exe);
  131.  
  132. $zip->close();
  133.  
  134. if (!headers_sent()) {
  135.         header('Content-Type: application/zip');
  136.         header('Content-Disposition: attachment; filename=oidplus_dos.zip');
  137.         readfile($tmp_file);
  138. }
  139.  
  140. unlink($tmp_file);
  141.  
  142. OIDplus::invoke_shutdown();
  143.