Subversion Repositories oidplus

Rev

Rev 1116 | 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 - 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. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusPagePublicSearch extends OIDplusPagePluginPublic {
  27.  
  28.         /**
  29.          * @param bool $html
  30.          * @return void
  31.          * @throws OIDplusException
  32.          */
  33.         public function init(bool $html=true) {
  34.                 OIDplus::config()->prepareConfigKey('search_min_term_length', 'Minimum length of a search term', '3', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  35.                         if (!is_numeric($value) || ($value < 0)) {
  36.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  37.                         }
  38.                 });
  39.         }
  40.  
  41.         /**
  42.          * @param array $params
  43.          * @param bool $is_searching
  44.          * @return void
  45.          */
  46.         private function prepareSearchParams(array &$params, bool $is_searching) {
  47.                 $params['term'] = isset($params['term']) ? trim($params['term']) : '';
  48.                 $params['namespace'] = isset($params['namespace']) ? trim($params['namespace']) : '';
  49.  
  50.                 // Default criteria selection:
  51.                 if ($is_searching) {
  52.                         $params['search_title'] = isset($params['search_title']) && $params['search_title'];
  53.                         $params['search_description'] = isset($params['search_description']) && $params['search_description'];
  54.                         $params['search_asn1id'] = isset($params['search_asn1id']) && $params['search_asn1id'];
  55.                         $params['search_iri'] = isset($params['search_iri']) && $params['search_iri'];
  56.                 } else {
  57.                         $params['search_title'] = true;
  58.                         $params['search_description'] = false;
  59.                         $params['search_asn1id'] = true;
  60.                         $params['search_iri'] = true;
  61.                 }
  62.         }
  63.  
  64.         /**
  65.          * @param string $html
  66.          * @param string $term
  67.          * @return string
  68.          */
  69.         private function highlight_match(string $html, string $term): string {
  70.                 return str_replace(htmlentities($term), '<font color="red">'.htmlentities($term).'</font>', $html);
  71.         }
  72.  
  73.         /**
  74.          * @param array $params
  75.          * @return string
  76.          * @throws OIDplusException
  77.          */
  78.         private function doSearch(array $params): string {
  79.                 $output = '';
  80.  
  81.                 // Note: The SQL collation defines if search is case sensitive or case insensitive
  82.  
  83.                 $min_length = OIDplus::config()->getValue('search_min_term_length');
  84.  
  85.                 $this->prepareSearchParams($params, true);
  86.  
  87.                 if (strlen($params['term']) == 0) {
  88.                         $output .= '<p><font color="red">'._L('Error: You must enter a search term.').'</font></p>';
  89.                 } else if (strlen($params['term']) < $min_length) {
  90.                         $output .= '<p><font color="red">'._L('Error: Search term minimum length is %1 characters.',$min_length).'</font></p>';
  91.                 } else {
  92.                         // TODO: case insensitive comparison (or should we leave that to the DBMS?)
  93.  
  94.                         if ($params['namespace'] == 'oidplus:ra') {
  95.                                 $output .= '<h2>'._L('Search results for RA %1','<font color="red">'.htmlentities($params['term']).'</font>').'</h2>';
  96.  
  97.                                 $sql_where = array(); $prep_where = array();
  98.                                 $sql_where[] = "email like ?";   $prep_where[] = '%'.$params['term'].'%';
  99.                                 $sql_where[] = "ra_name like ?"; $prep_where[] = '%'.$params['term'].'%';
  100.  
  101.                                 if (count($sql_where) == 0) $sql_where[] = '1=0';
  102.                                 $res = OIDplus::db()->query("select * from ###ra where (".implode(' or ', $sql_where).")", $prep_where);
  103.  
  104.                                 $count = 0;
  105.                                 while ($row = $res->fetch_object()) {
  106.                                         $email = str_replace('@', '&', $row->email);
  107.                                         $output .= '<p><a '.OIDplus::gui()->link('oidplus:rainfo$'.str_replace('@','&',$email)).'>'.$this->highlight_match(htmlentities($email),$params['term']).'</a>: <b>'.$this->highlight_match(htmlentities($row->ra_name),$params['term']).'</b></p>';
  108.                                         $count++;
  109.                                 }
  110.                                 if ($count == 0) {
  111.                                         $output .= '<p>'._L('Nothing found').'</p>';
  112.                                 }
  113.                         } else {
  114.                                 $output .= '<h2>'._L('Search results for %1 (%2)','<font color="red">'.htmlentities($params['term']).'</font>',htmlentities($params['namespace'])).'</h2>';
  115.  
  116.                                 $sql_where = array(); $prep_where = array();
  117.                                 $sql_where[] = "id like ?"; $prep_where[] = '%'.$params['term'].'%'; // TODO: should we rather do findFitting(), so we can e.g. find GUIDs with different notation?
  118.                                 if ($params["search_title"])       { $sql_where[] = "title like ?";       $prep_where[] = '%'.$params['term'].'%'; }
  119.                                 if ($params["search_description"]) { $sql_where[] = "description like ?"; $prep_where[] = '%'.$params['term'].'%'; }
  120.  
  121.                                 if ($params["search_asn1id"]) {
  122.                                         $res = OIDplus::db()->query("select * from ###asn1id where name like ?", array('%'.$params['term'].'%'));
  123.                                         while ($row = $res->fetch_object()) {
  124.                                                 $sql_where[] = "id = ?"; $prep_where[] = $row->oid;
  125.                                         }
  126.                                 }
  127.  
  128.                                 if ($params["search_iri"]) {
  129.                                         $res = OIDplus::db()->query("select * from ###iri where name like ?", array('%'.$params['term'].'%'));
  130.                                         while ($row = $res->fetch_object()) {
  131.                                                 $sql_where[] = "id = ?"; $prep_where[] = $row->oid;
  132.                                         }
  133.                                 }
  134.  
  135.                                 if (count($sql_where) == 0) $sql_where[] = '1=0';
  136.                                 array_unshift($prep_where, $params['namespace'].':%');
  137.  
  138.                                 $res = OIDplus::db()->query("select * from ###objects where id like ? and (".implode(' or ', $sql_where).")", $prep_where);
  139.  
  140.                                 $count = 0;
  141.                                 while ($row = $res->fetch_object()) {
  142.                                         $output .= '<p><a '.OIDplus::gui()->link($row->id).'>'.$this->highlight_match(htmlentities($row->id),$params['term']).'</a>';
  143.  
  144.                                         $asn1ids = array();
  145.                                         $res2 = OIDplus::db()->query("select name from ###asn1id where oid = ?", array($row->id));
  146.                                         while ($row2 = $res2->fetch_object()) {
  147.                                                 $asn1ids[] = $row2->name;
  148.                                         }
  149.                                         if (count($asn1ids) > 0) {
  150.                                                 $asn1ids = implode(', ', $asn1ids);
  151.                                                 $output .= ' ('.$this->highlight_match(htmlentities($asn1ids),$params['term']).')';
  152.                                         }
  153.  
  154.                                         if (htmlentities($row->title) != '') $output .= ': <b>'.$this->highlight_match(htmlentities($row->title),$params['term']).'</b></p>';
  155.                                         $count++;
  156.                                 }
  157.                                 if ($count == 0) {
  158.                                         $output .= '<p>'._L('Nothing found').'</p>';
  159.                                 }
  160.                         }
  161.                 }
  162.  
  163.                 return $output;
  164.         }
  165.  
  166.         /**
  167.          * @param string $actionID
  168.          * @param array $params
  169.          * @return array
  170.          * @throws OIDplusException
  171.          */
  172.         public function action(string $actionID, array $params): array {
  173.  
  174.                 if ($actionID == 'search') {
  175.                         // Search with JavaScript/AJAX
  176.                         $ret = $this->doSearch($params);
  177.                         return array("status" => 0, "output" => $ret);
  178.                 } else {
  179.                         return parent::action($actionID, $params);
  180.                 }
  181.  
  182.         }
  183.  
  184.         /**
  185.          * @param string $id
  186.          * @param array $out
  187.          * @param bool $handled
  188.          * @return void
  189.          * @throws OIDplusException
  190.          */
  191.         public function gui(string $id, array &$out, bool &$handled) {
  192.                 if (explode('$',$id)[0] == 'oidplus:search') {
  193.                         $handled = true;
  194.  
  195.                         $out['title'] = _L('Search');
  196.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  197.  
  198.                         $out['text'] = '';
  199.  
  200.                         try {
  201.                                 $params = $_POST;
  202.  
  203.                                 $this->prepareSearchParams($params, isset($params['search']));
  204.  
  205.                                 $out['text'] .= '<form id="searchForm" action="?goto=oidplus%3Asearch" method="POST">
  206.                                                  <input type="hidden" name="search" value="1">
  207.                                                  '._L('Search for').': <input type="text" id="term" name="term" value="'.htmlentities($params['term']).'"><br><br>
  208.                                                  <script>
  209.                                                  function searchNsSelect(ns) {
  210.                                                      $("#search_options_oid")[0].style.display = (ns == "oid") ? "block" : "none";
  211.                                                      $("#search_options_object")[0].style.display = (ns == "oidplus:ra") ? "none" : "block";
  212.                                                      $("#search_options_ra")[0].style.display = (ns == "oidplus:ra") ? "block" : "none";
  213.                                                  }
  214.                                                  $( document ).ready(function() {
  215.                                                      searchNsSelect($("#namespace")[0].value);
  216.                                                  });
  217.                                                  </script>
  218.                                                  '._L('Search in').': <select name="namespace" id="namespace" onchange="searchNsSelect(this.value);"><br><br>';
  219.  
  220.                                 foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  221.                                         $out['text'] .= '<option value="'.htmlentities($ot::ns()).'"'.(($params['namespace'] == $ot::ns()) ? ' selected' : '').'>'.htmlentities($ot::objectTypeTitle()).'</option>';
  222.                                 }
  223.                                 $out['text'] .= '<option value="oidplus:ra"'.(($params['namespace'] == 'oidplus:ra') ? ' selected' : '').'>'._L('Registration Authority').'</option>
  224.                                                  </select><br><br>
  225.                                 <div id="search_options_ra">
  226.                                 <!-- TODO: RA specific selection criterias -->
  227.                                 </div>
  228.                                 <div id="search_options_object">
  229.                                             <input type="checkbox" name="search_title" id="search_title" value="1"'.($params["search_title"] ? ' checked' : '').'> <label for="search_title">'._L('Search in field "Title"').'</label><br>
  230.                                             <input type="checkbox" name="search_description" id="search_description" value="1"'.($params["search_description"] ? ' checked' : '').'> <label for="search_description">'._L('Search in field "Description"').'</label><br>
  231.                                 <div id="search_options_oid">
  232.                                     <input type="checkbox" name="search_asn1id" id="search_asn1id" value="1"'.($params["search_asn1id"] ? ' checked' : '').'> <label for="search_asn1id">'._L('Search in field "ASN.1 identifier" (only OIDs)').'</label><br>
  233.                                     <input type="checkbox" name="search_iri" id="search_iri" value="1"'.($params["search_iri"] ? ' checked' : '').'> <label for="search_iri">'._L('Search in field "Unicode label" (only OIDs)').'</label><br>
  234.                                 </div>
  235.                                 </div>
  236.                                  <br>
  237.  
  238.                                 <input type="submit" value="'._L('Search').'" onclick="return OIDplusPagePublicSearch.search_button_click()">
  239.                                 </form>';
  240.  
  241.                                 $out['text'] .= '<div id="search_output">'; // will be filled with either AJAX or staticly (HTML form submit)
  242.                                 if (isset($params['search'])) {
  243.                                         // Search with NoJS/HTML
  244.                                         $out['text'] .= $this->doSearch($params);
  245.                                 }
  246.                                 $out['text'] .= '</div>';
  247.                         } catch (\Exception $e) {
  248.                                 $out['text'] = _L('Error: %1',$e->getMessage());
  249.                         }
  250.                 }
  251.         }
  252.  
  253.         /**
  254.          * @param array $out
  255.          * @return void
  256.          */
  257.         public function publicSitemap(array &$out) {
  258.                 $out[] = 'oidplus:search';
  259.         }
  260.  
  261.         /**
  262.          * @param array $json
  263.          * @param string|null $ra_email
  264.          * @param bool $nonjs
  265.          * @param string $req_goto
  266.          * @return bool
  267.          * @throws OIDplusException
  268.          */
  269.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  270.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  271.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  272.                 } else {
  273.                         $tree_icon = null; // default icon (folder)
  274.                 }
  275.  
  276.                 $json[] = array(
  277.                         'id' => 'oidplus:search',
  278.                         'icon' => $tree_icon,
  279.                         'text' => _L('Search')
  280.                 );
  281.  
  282.                 return true;
  283.         }
  284.  
  285.         /**
  286.          * @param string $request
  287.          * @return array|false
  288.          */
  289.         public function tree_search(string $request) {
  290.                 return false;
  291.         }
  292. }
  293.