Subversion Repositories oidplus

Rev

Rev 206 | Rev 228 | 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 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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. class OIDplusGui {
  23.  
  24.         private static $crudCounter = 0;
  25.  
  26.         protected static function objDescription($html) {
  27.                 // We allow HTML, but no hacking
  28.                 $html = anti_xss($html);
  29.  
  30.                 return trim_br($html);
  31.         }
  32.  
  33.         protected static function showCrud($parent='oid:') {
  34.                 $items_total = 0;
  35.                 $items_hidden = 0;
  36.  
  37.                 $objParent = OIDplusObject::parse($parent);
  38.                 $parentNS = $objParent::ns();
  39.  
  40.                 $result = OIDplus::db()->query("select o.*, r.ra_name " .
  41.                                                "from ".OIDPLUS_TABLENAME_PREFIX."objects o " .
  42.                                                "left join ".OIDPLUS_TABLENAME_PREFIX."ra r on r.email = o.ra_email " .
  43.                                                "where parent = ? " .
  44.                                                "order by ".OIDplus::db()->natOrder('id'), array($parent));
  45.                 $rows = array();
  46.                 if ($parentNS == 'oid') {
  47.                         $one_weid_available = $objParent->isWeid(true);
  48.                         while ($row = OIDplus::db()->fetch_object($result)) {
  49.                                 $obj = OIDplusObject::parse($row->id);
  50.                                 $rows[] = array($obj,$row);
  51.                                 if (!$one_weid_available) {
  52.                                         if ($obj->isWeid(true)) $one_weid_available = true;
  53.                                 }
  54.                         }
  55.                 } else {
  56.                         $one_weid_available = false;
  57.                         while ($row = OIDplus::db()->fetch_object($result)) {
  58.                                 $obj = OIDplusObject::parse($row->id);
  59.                                 $rows[] = array($obj,$row);
  60.                         }
  61.                 }
  62.  
  63.                 $output = '';
  64.                 $output .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  65.                 $output .= '<table class="table table-bordered table-striped">';
  66.                 $output .= '    <tr>';
  67.                 $output .= '         <th>ID'.(($parentNS == 'gs1') ? ' (without check digit)' : '').'</th>';
  68.                 if ($parentNS == 'oid') {
  69.                         if ($one_weid_available) $output .= '        <th>WEID</th>';
  70.                         $output .= '         <th>ASN.1 IDs (comma sep.)</th>';
  71.                         $output .= '         <th>IRI IDs (comma sep.)</th>';
  72.                 }
  73.                 $output .= '         <th>RA</th>';
  74.                 $output .= '         <th>Comment</th>';
  75.                 if ($objParent->userHasWriteRights()) {
  76.                         $output .= '         <th>Hide</th>';
  77.                         $output .= '         <th>Update</th>';
  78.                         $output .= '         <th>Delete</th>';
  79.                 }
  80.                 $output .= '         <th>Created</th>';
  81.                 $output .= '         <th>Updated</th>';
  82.                 $output .= '    </tr>';
  83.  
  84.                 foreach ($rows as list($obj,$row)) {
  85.                         $items_total++;
  86.                         if (!$obj->userHasReadRights()) {
  87.                                 $items_hidden++;
  88.                                 continue;
  89.                         }
  90.  
  91.                         $show_id = $obj->crudShowId($objParent);
  92.  
  93.                         $asn1ids = array();
  94.                         $res2 = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ? order by lfd", array($row->id));
  95.                         while ($row2 = OIDplus::db()->fetch_array($res2)) {
  96.                                 $asn1ids[] = $row2['name'];
  97.                         }
  98.  
  99.                         $iris = array();
  100.                         $res2 = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = ? order by lfd", array($row->id));
  101.                         while ($row2 = OIDplus::db()->fetch_array($res2)) {
  102.                                 $iris[] = $row2['name'];
  103.                         }
  104.  
  105.                         $output .= '<tr>';
  106.                         $output .= '     <td><a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.htmlentities($show_id).'</a></td>';
  107.                         if ($objParent->userHasWriteRights()) {
  108.                                 if ($parentNS == 'oid') {
  109.                                         if ($one_weid_available) {
  110.                                                 if ($obj->isWeid(false)) {
  111.                                                         $output .= '    <td>'.$obj->weidArc().'</td>';
  112.                                                 } else {
  113.                                                         $output .= '    <td>n/a</td>';
  114.                                                 }
  115.                                         }
  116.                                         $output .= '     <td><input type="text" id="asn1ids_'.$row->id.'" value="'.implode(', ', $asn1ids).'"></td>';
  117.                                         $output .= '     <td><input type="text" id="iris_'.$row->id.'" value="'.implode(', ', $iris).'"></td>';
  118.                                 }
  119.                                 $output .= '     <td><input type="text" id="ra_email_'.$row->id.'" value="'.htmlentities($row->ra_email).'"></td>';
  120.                                 $output .= '     <td><input type="text" id="comment_'.$row->id.'" value="'.htmlentities($row->comment).'"></td>';
  121.                                 $output .= '     <td><input type="checkbox" id="hide_'.$row->id.'" '.($row->confidential ? 'checked' : '').'></td>';
  122.                                 $output .= '     <td><button type="button" name="update_'.$row->id.'" id="update_'.$row->id.'" class="btn btn-success btn-xs update" onclick="crudActionUpdate('.js_escape($row->id).', '.js_escape($parent).')">Update</button></td>';
  123.                                 $output .= '     <td><button type="button" name="delete_'.$row->id.'" id="delete_'.$row->id.'" class="btn btn-danger btn-xs delete" onclick="crudActionDelete('.js_escape($row->id).', '.js_escape($parent).')">Delete</button></td>';
  124.                                 $output .= '     <td>'.oidplus_formatdate($row->created).'</td>';
  125.                                 $output .= '     <td>'.oidplus_formatdate($row->updated).'</td>';
  126.                         } else {
  127.                                 if ($asn1ids == '') $asn1ids = '<i>(none)</i>';
  128.                                 if ($iris == '') $iris = '<i>(none)</i>';
  129.                                 if ($parentNS == 'oid') {
  130.                                         if ($one_weid_available) {
  131.                                                 if ($obj->isWeid(false)) {
  132.                                                         $output .= '    <td>'.$obj->weidArc().'</td>';
  133.                                                 } else {
  134.                                                         $output .= '    <td>n/a</td>';
  135.                                                 }
  136.                                         }
  137.                                         $asn1ids_ext = array();
  138.                                         foreach ($asn1ids as $asn1id) {
  139.                                                 $asn1ids_ext[] = '<a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.$asn1id.'</a>';
  140.                                         }
  141.                                         $output .= '     <td>'.implode(', ', $asn1ids_ext).'</td>';
  142.                                         $output .= '     <td>'.implode(', ', $iris).'</td>';
  143.                                 }
  144.                                 $output .= '     <td><a '.oidplus_link('oidplus:rainfo$'.str_replace('@','&',$row->ra_email)).'>'.htmlentities(empty($row->ra_name) ? str_replace('@','&',$row->ra_email) : $row->ra_name).'</a></td>';
  145.                                 $output .= '     <td>'.htmlentities($row->comment).'</td>';
  146.                                 $output .= '     <td>'.oidplus_formatdate($row->created).'</td>';
  147.                                 $output .= '     <td>'.oidplus_formatdate($row->updated).'</td>';
  148.                         }
  149.                         $output .= '</tr>';
  150.                 }
  151.  
  152.                 $result = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($parent));
  153.                 $parent_ra_email = OIDplus::db()->num_rows($result) > 0 ? OIDplus::db()->fetch_object($result)->ra_email : '';
  154.  
  155.                 if ($objParent->userHasWriteRights()) {
  156.                         $output .= '<tr>';
  157.                         $prefix = is_null($objParent) ? '' : $objParent->crudInsertPrefix();
  158.                         if ($parentNS == 'oid') {
  159.                                 if ($objParent->isWeid(true)) {
  160.                                         $output .= '     <td>'.$prefix.' <input oninput="frdl_oidid_change()" type="text" id="id" value="" style="width:100%;min-width:100px"></td>'; // TODO: idee classname vergeben, z.B. "OID" und dann mit einem oid-spezifischen css die breite einstellbar machen, somit hat das plugin mehr kontrolle über das aussehen und die mindestbreiten
  161.                                         $output .= '     <td><input type="text" name="weid" id="weid" value="" oninput="frdl_weid_change()"></td>';
  162.                                 } else {
  163.                                         $output .= '     <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:50px"></td>'; // TODO: idee classname vergeben, z.B. "OID" und dann mit einem oid-spezifischen css die breite einstellbar machen, somit hat das plugin mehr kontrolle über das aussehen und die mindestbreiten
  164.                                         if ($one_weid_available) $output .= '     <td></td>'; // WEID-editor not available for root nodes. Do it manually, please
  165.                                 }
  166.                         } else {
  167.                                 $output .= '     <td>'.$prefix.' <input type="text" id="id" value=""></td>';
  168.                         }
  169.                         if ($parentNS == 'oid') $output .= '     <td><input type="text" id="asn1ids" value=""></td>';
  170.                         if ($parentNS == 'oid') $output .= '     <td><input type="text" id="iris" value=""></td>';
  171.                         $output .= '     <td><input type="text" id="ra_email" value="'.htmlentities($parent_ra_email).'"></td>';
  172.                         $output .= '     <td><input type="text" id="comment" value=""></td>';
  173.                         $output .= '     <td><input type="checkbox" id="hide"></td>';
  174.                         $output .= '     <td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs update" onclick="crudActionInsert('.js_escape($parent).')">Insert</button></td>';
  175.                         $output .= '     <td></td>';
  176.                         $output .= '     <td></td>';
  177.                         $output .= '     <td></td>';
  178.                         $output .= '</tr>';
  179.                 } else {
  180.                         if ($items_total-$items_hidden == 0) {
  181.                                 $cols = ($parentNS == 'oid') ? 7 : 5;
  182.                                 if ($one_weid_available) $cols++;
  183.                                 $output .= '<tr><td colspan="'.$cols.'">No items available</td></tr>';
  184.                         }
  185.                 }
  186.  
  187.                 $output .= '</table>';
  188.                 $output .= '</div></div>';
  189.  
  190.                 if ($items_hidden == 1) {
  191.                         $output .= '<p>'.$items_hidden.' item is hidden. Please <a '.oidplus_link('oidplus:login').'>log in</a> to see it.</p>';
  192.                 } else if ($items_hidden > 1) {
  193.                         $output .= '<p>'.$items_hidden.' items are hidden. Please <a '.oidplus_link('oidplus:login').'>log in</a> to see them.</p>';
  194.                 }
  195.  
  196.                 return $output;
  197.         }
  198.  
  199.         // 'quickbars' added 11 July 2019: Disabled because of two problems:
  200.         //                                 1. When you load TinyMCE via AJAX using the left menu, the quickbar is immediately shown, even if TinyMCE does not have the focus
  201.         //                                 2. When you load a page without TinyMCE using the left menu, the quickbar is still visible, although there is no edit
  202.         public static $exclude_tinymce_plugins = array('fullpage', 'bbcode', 'quickbars');
  203.  
  204.         protected static function showMCE($name, $content) {
  205.                 $mce_plugins = array();
  206.                 foreach (glob(__DIR__ . '/../../3p/tinymce/plugins/*') as $m) { // */
  207.                         $mce_plugins[] = basename($m);
  208.                 }
  209.  
  210.                 foreach (self::$exclude_tinymce_plugins as $exclude) {
  211.                         $index = array_search($exclude, $mce_plugins);
  212.                         if ($index !== false) unset($mce_plugins[$index]);
  213.                 }
  214.  
  215.                 $out = '<script>
  216.                                 tinymce.remove("#'.$name.'");
  217.                                 tinymce.init({
  218.                                         selector: "#'.$name.'",
  219.                                         height: 200,
  220.                                         statusbar: false,
  221. //                                      menubar:false,
  222. //                                      toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | fontsizeselect",
  223.                                         toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
  224.                                         plugins: "'.implode(' ', $mce_plugins).'",
  225.                                         mobile: {
  226.                                                 theme: "mobile",
  227.                                                 toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
  228.                                                 plugins: "'.implode(' ', $mce_plugins).'"
  229.                                         }
  230.  
  231.                                 });
  232.                         </script>';
  233.  
  234.                 $content = htmlentities($content); // For some reason, if we want to display the text "<xyz>" in TinyMCE, we need to double-encode things! &lt; will not be accepted, we need &amp;lt; ... why?
  235.  
  236.                 $out .= '<textarea name="'.htmlentities($name).'" id="'.htmlentities($name).'">'.trim($content).'</textarea><br>';
  237.  
  238.                 return $out;
  239.         }
  240.  
  241.         public static function generateContentPage($id) {
  242.                 $out = array();
  243.  
  244.                 $handled = false;
  245.                 $out['title'] = '';
  246.                 $out['icon'] = '';
  247.                 $out['text'] = '';
  248.  
  249.                 // === Plugins ===
  250.  
  251.                 foreach (OIDplus::getPagePlugins('*') as $plugin) {
  252.                         $plugin->gui($id, $out, $handled);
  253.                 }
  254.  
  255.                 // === Everything else (objects) ===
  256.  
  257.                 if (!$handled) {
  258.                         try {
  259.                                 $obj = OIDplusObject::parse($id);
  260.                         } catch (Exception $e) {
  261.                                 $out['title'] = 'Error';
  262.                                 $out['icon'] = 'img/error_big.png';
  263.                                 $out['text'] = htmlentities($e->getMessage());
  264.                                 return $out;
  265.                         }
  266.  
  267.                         if ((!is_null($obj)) && (!$obj->userHasReadRights())) {
  268.                                 $out['title'] = 'Access denied';
  269.                                 $out['icon'] = 'img/error_big.png';
  270.                                 $out['text'] = '<p>Please <a '.oidplus_link('oidplus:login').'>log in</a> to receive information about this object.</p>';
  271.                                 return $out;
  272.                         }
  273.  
  274.                         // ---
  275.  
  276.                         $parent = null;
  277.                         $res = null;
  278.                         $row = null;
  279.                         $matches_any_registered_type = false;
  280.                         foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  281.                                 if ($obj = $ot::parse($id)) {
  282.                                         $matches_any_registered_type = true;
  283.                                         if ($obj->isRoot()) {
  284.                                                 $obj->getContentPage($out['title'], $out['text'], $out['icon']);
  285.                                                 $parent = null; // $obj->getParent();
  286.                                                 break;
  287.                                         } else {
  288.                                                 $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($obj->nodeId()));
  289.                                                 $row = OIDplus::db()->fetch_array($res);
  290.                                                 if (OIDplus::db()->num_rows($res) == 0) {
  291.                                                         http_response_code(404);
  292.                                                         $out['title'] = 'Object not found';
  293.                                                         $out['icon'] = 'img/error_big.png';
  294.                                                         $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
  295.                                                         return $out;
  296.                                                 } else {
  297.                                                         $obj->getContentPage($out['title'], $out['text'], $out['icon']);
  298.                                                         if (empty($out['title'])) $out['title'] = explode(':',$id,2)[1];
  299.                                                         $parent = $obj->getParent();
  300.                                                         break;
  301.                                                 }
  302.                                         }
  303.                                 }
  304.                         }
  305.                         if (!$matches_any_registered_type) {
  306.                                 http_response_code(404);
  307.                                 $out['title'] = 'Object not found';
  308.                                 $out['icon'] = 'img/error_big.png';
  309.                                 $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
  310.                                 return $out;
  311.                         }
  312.  
  313.                         // ---
  314.  
  315.                         if ($parent) {
  316.                                 if ($parent->isRoot()) {
  317.  
  318.                                         $parent_link_text = $parent->objectTypeTitle();
  319.                                         $out['text'] = '<p><a '.oidplus_link($parent->root()).'><img src="img/arrow_back.png" width="16"> Parent node: '.htmlentities($parent_link_text).'</a></p>' . $out['text'];
  320.  
  321.                                 } else {
  322.                                         $res_ = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($parent->nodeId()));
  323.                                         $row_ = OIDplus::db()->fetch_array($res_);
  324.  
  325.                                         $parent_title = $row_['title'];
  326.                                         if (empty($parent_title) && ($parent->ns() == 'oid')) {
  327.                                                 $res_ = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ?", array($parent->nodeId()));
  328.                                                 $row_ = OIDplus::db()->fetch_array($res_);
  329.                                                 $parent_title = $row_['name']; // TODO: multiple ASN1 ids?
  330.                                         }
  331.  
  332.                                         $parent_link_text = empty($parent_title) ? explode(':',$parent->nodeId())[1] : $parent_title.' ('.explode(':',$parent->nodeId())[1].')';
  333.  
  334.                                         $out['text'] = '<p><a '.oidplus_link($parent->nodeId()).'><img src="img/arrow_back.png" width="16"> Parent node: '.htmlentities($parent_link_text).'</a></p>' . $out['text'];
  335.                                 }
  336.                         } else {
  337.                                 $parent_link_text = 'Go back to front page';
  338.                                 $out['text'] = '<p><a '.oidplus_link('oidplus:system').'><img src="img/arrow_back.png" width="16"> '.htmlentities($parent_link_text).'</a></p>' . $out['text'];
  339.                         }
  340.  
  341.                         // ---
  342.  
  343.                         if (!is_null($row) && isset($row['description'])) {
  344.                                 if (empty($row['description'])) {
  345.                                         if (empty($row['title'])) {
  346.                                                 $desc = '<p><i>No description for this object available</i></p>';
  347.                                         } else {
  348.                                                 $desc = $row['title'];
  349.                                         }
  350.                                 } else {
  351.                                         $desc = OIDplusGui::objDescription($row['description']);
  352.                                 }
  353.  
  354.                                 if ($obj->userHasWriteRights()) {
  355.                                         $rand = ++self::$crudCounter;
  356.                                         $desc = '<noscript><p><b>You need to enable JavaScript to edit title or description of this object.</b></p>'.$desc.'</noscript>';
  357.                                         $desc .= '<div class="container box" style="display:none" id="descbox_'.$rand.'">';
  358.                                         $desc .= 'Title: <input type="text" name="title" id="titleedit" value="'.htmlentities($row['title']).'"><br><br>Description:<br>';
  359.                                         $desc .= self::showMCE('description', $row['description']);
  360.                                         $desc .= '<button type="button" name="update_desc" id="update_desc" class="btn btn-success btn-xs update" onclick="updateDesc()">Update description</button>';
  361.                                         $desc .= '</div>';
  362.                                         $desc .= '<script>document.getElementById("descbox_'.$rand.'").style.display = "block";</script>';
  363.                                 }
  364.                         } else {
  365.                                 $desc = '';
  366.                         }
  367.  
  368.                         // ---
  369.  
  370.                         if (strpos($out['text'], '%%DESC%%') !== false)
  371.                                 $out['text'] = str_replace('%%DESC%%',    $desc,                              $out['text']);
  372.                         if (strpos($out['text'], '%%CRUD%%') !== false)
  373.                                 $out['text'] = str_replace('%%CRUD%%',    self::showCrud($id),                $out['text']);
  374.                         if (strpos($out['text'], '%%RA_INFO%%') !== false)
  375.                                 $out['text'] = str_replace('%%RA_INFO%%', OIDplusPagePublicRaInfo::showRaInfo($row['ra_email']), $out['text']);
  376.  
  377.                         $alt_ids = $obj->getAltIds();
  378.                         if (count($alt_ids) > 0) {
  379.                                 $out['text'] .= "<h2>Alternative Identifiers</h2>";
  380.                                 foreach ($alt_ids as list($ns, $aid, $aiddesc)) {
  381.                                         $out['text'] .= "$aiddesc <code>$ns:$aid</code><br>";
  382.                                 }
  383.                         }
  384.  
  385.                         foreach (OIDplus::getPagePlugins('public') as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
  386.                         foreach (OIDplus::getPagePlugins('ra')     as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
  387.                         foreach (OIDplus::getPagePlugins('admin')  as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
  388.                 } else {
  389.                         // Other pages (search, whois, etc.)
  390.                         /*
  391.                         if ($id != 'oidplus:system') {
  392.                                 $parent_link_text = 'Go back to front page';
  393.                                 $out['text'] = '<p><a '.oidplus_link('oidplus:system').'><img src="img/arrow_back.png" width="16"> '.htmlentities($parent_link_text).'</a></p>' . $out['text'];
  394.                         }
  395.                         */
  396.                 }
  397.  
  398.                 return $out;
  399.         }
  400. }
  401.