Subversion Repositories oidplus

Rev

Rev 148 | Go to most recent revision | Blame | 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 OIDplusPagePublicSearch extends OIDplusPagePlugin {
  23.         public function type() {
  24.                 return 'public';
  25.         }
  26.  
  27.         public function priority() {
  28.                 return 300;
  29.         }
  30.  
  31.         public function action(&$handled) {
  32.                 // Nothing
  33.         }
  34.  
  35.         public function init($html=true) {
  36.                 OIDplus::config()->prepareConfigKey('search_min_term_length', 'Minimum length of a search term', '3', 0, 1);
  37.         }
  38.  
  39.         public function cfgSetValue($name, $value) {
  40.                 if ($name == 'search_min_term_length') {
  41.                         if (!is_numeric($value) || ($value < 0)) {
  42.                                 throw new Exception("Please enter a valid value.");
  43.                         }
  44.                 }
  45.         }
  46.  
  47.         public function gui($id, &$out, &$handled) {
  48.                 if (explode('$',$id)[0] == 'oidplus:search') {
  49.                         $handled = true;
  50.  
  51.                         $out['title'] = 'Search';
  52.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
  53.  
  54.                         $out['text'] = '';
  55.  
  56.                         try {
  57.                                 $search_term = isset($_POST['term']) ? $_POST['term'] : '';
  58.                                 $ns = isset($_POST['namespace']) ? $_POST['namespace'] : '';
  59.  
  60.                                 if (!isset($_POST['search'])) {
  61.                                         // Default criteria selection
  62.                                         $_POST['search_title'] = '1';
  63.                                         $_POST['search_asn1id'] = '1';
  64.                                         $_POST['search_iri'] = '1';
  65.                                 }
  66.  
  67.                                 // TODO: make it via AJAX? Reloading the whole page is not good. But attention: Also allow NoScript
  68.                                 $out['text'] .= '<form id="searchForm" action="?goto=oidplus:search" method="POST">
  69.                                                  <input type="hidden" name="search" value="1">
  70.                                                  Search for: <input type="text" id="term" name="term" value="'.htmlentities($search_term).'"><br><br>
  71.                                                  <script>
  72.                                                  function searchNsSelect(ns) {
  73.                                                      document.getElementById("search_options_oid").style.display = (ns == "oid") ? "block" : "none";
  74.                                                      document.getElementById("search_options_object").style.display = (ns == "oidplus:ra") ? "none" : "block";
  75.                                                      document.getElementById("search_options_ra").style.display = (ns == "oidplus:ra") ? "block" : "none";
  76.                                                  }
  77.                                                  $( document ).ready(function() {
  78.                                                      searchNsSelect(document.getElementById("namespace").value);
  79.                                                  });
  80.                                                  </script>
  81.                                                  Search in: <select name="namespace" id="namespace" onchange="searchNsSelect(this.value);"><br><br>';
  82.  
  83.                                 foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
  84.                                         $out['text'] .= '<option value="'.htmlentities($ot::ns()).'"'.(($ns == $ot::ns()) ? ' selected' : '').'>'.htmlentities($ot::objectTypeTitle()).'</option>';
  85.                                 }
  86.                                 $out['text'] .= '<option value="oidplus:ra"'.(($ns == 'oidplus:ra') ? ' selected' : '').'>Registration Authority</option>
  87.                                                  </select><br><br>
  88.                                 <div id="search_options_ra">
  89.                                 <!-- TODO: RA specific selection criterias -->
  90.                                 </div>
  91.                                 <div id="search_options_object">
  92.                                             <input type="checkbox" name="search_title" id="search_title" value="1"'.(isset($_POST["search_title"]) ? ' checked' : '').'> <label for="search_title">Search in field "Title"</label><br>
  93.                                             <input type="checkbox" name="search_description" id="search_description" value="1"'.(isset($_POST["search_description"]) ? ' checked' : '').'> <label for="search_description">Search in field "Description"</label><br>
  94.                                 <div id="search_options_oid">
  95.                                     <input type="checkbox" name="search_asn1id" id="search_asn1id" value="1"'.(isset($_POST["search_asn1id"]) ? ' checked' : '').'> <label for="search_asn1id">Search in field "ASN.1 identifier" (only OIDs)</label><br>
  96.                                     <input type="checkbox" name="search_iri" id="search_iri" value="1"'.(isset($_POST["search_iri"]) ? ' checked' : '').'> <label for="search_iri">Search in field "Unicode label" (only OIDs)</label><br>
  97.                                 </div>
  98.                                 </div>
  99.                                  <br>
  100.  
  101.                                 <input type="submit" value="Search">
  102.                                 </form>';
  103.  
  104.                                 if (isset($_POST['search'])) {
  105.                                         // Note: The SQL collation defines if search is case sensitive or case insensitive
  106.  
  107.                                         $min_length = OIDplus::config()->getValue('search_min_term_length');
  108.  
  109.                                         $search_term = trim($search_term);
  110.  
  111.                                         if (strlen($search_term) == 0) {
  112.                                                 $out['text'] .= '<p><font color="red">Error: You must enter a search term.</font></p>';
  113.                                         } else if (strlen($search_term) < $min_length) {
  114.                                                 $out['text'] .= '<p><font color="red">Error: Search term minimum length is '.$min_length.' characters.</font></p>';
  115.                                         } else {
  116.                                                 if ($ns == 'oidplus:ra') {
  117.                                                         $out['text'] .= '<h2>Search results for RA "'.htmlentities($search_term).'"</h2>';
  118.  
  119.                                                         $sql_where = array(); $prep_where = array();
  120.                                                         $sql_where[] = "email like ?";   $prep_where[] = '%'.$search_term.'%';
  121.                                                         $sql_where[] = "ra_name like ?"; $prep_where[] = '%'.$search_term.'%';
  122.  
  123.                                                         if (count($sql_where) == 0) $sql_where[] = '1=0';
  124.                                                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where (".implode(' or ', $sql_where).")", $prep_where);
  125.  
  126.                                                         $count = 0;
  127.                                                         while ($row = OIDplus::db()->fetch_object($res)) {
  128.                                                                 $email = str_replace('@', '&', $row->email);
  129.                                                                 $out['text'] .= '<p><a '.oidplus_link('oidplus:rainfo$'.str_replace('@','&',$email)).'>'.htmlentities($email).'</a>: <b>'.htmlentities($row->ra_name).'</b></p>';
  130.                                                                 $count++;
  131.                                                         }
  132.                                                         if ($count == 0) {
  133.                                                                 $out['text'] .= '<p>Nothing found</p>';
  134.                                                         }
  135.                                                 } else {
  136.                                                         $out['text'] .= '<h2>Search results for "'.htmlentities($search_term).'" ('.htmlentities($ns).')</h2>';
  137.  
  138.                                                         $sql_where = array(); $prep_where = array();
  139.                                                         $sql_where[] = "id like ?"; $prep_where[] = '%'.$search_term.'%'; // TODO: should we rather do findFitting(), so we can e.g. find GUIDs with different notation?
  140.                                                         if (isset($_POST["search_title"]))       { $sql_where[] = "title like ?";       $prep_where[] = '%'.$search_term.'%'; }
  141.                                                         if (isset($_POST["search_description"])) { $sql_where[] = "description like ?"; $prep_where[] = '%'.$search_term.'%'; }
  142.  
  143.                                                         if (isset($_POST["search_asn1id"])) {
  144.                                                                 $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."asn1id where name like ?", array('%'.$search_term.'%'));
  145.                                                                 while ($row = OIDplus::db()->fetch_object($res)) {
  146.                                                                         $sql_where[] = "id = ?"; $prep_where[] = $row->oid;
  147.                                                                 }
  148.                                                         }
  149.  
  150.                                                         if (isset($_POST["search_iri"])) {
  151.                                                                 $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."iri where name like ?", array('%'.$search_term.'%'));
  152.                                                                 while ($row = OIDplus::db()->fetch_object($res)) {
  153.                                                                         $sql_where[] = "id = ?"; $prep_where[] = $row->oid;
  154.                                                                 }
  155.                                                         }
  156.  
  157.                                                         if (count($sql_where) == 0) $sql_where[] = '1=0';
  158.                                                         array_unshift($prep_where, $ns.':%');
  159.                                                        
  160.                                                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id like ? and (".implode(' or ', $sql_where).")", $prep_where);
  161.  
  162.                                                         $count = 0;
  163.                                                         while ($row = OIDplus::db()->fetch_object($res)) {
  164.                                                                 $out['text'] .= '<p><a '.oidplus_link($row->id).'>'.htmlentities($row->id).'</a>: <b>'.htmlentities($row->title).'</b></p>'; // TODO: also show asn1id; highlight search match?
  165.                                                                 $count++;
  166.                                                         }
  167.                                                         if ($count == 0) {
  168.                                                                 $out['text'] .= '<p>Nothing found</p>';
  169.                                                         }
  170.                                                 }
  171.                                         }
  172.                                 }
  173.                         } catch (Exception $e) {
  174.                                 $out['text'] = "Error: ".$e->getMessage();
  175.                         }
  176.                 }
  177.         }
  178.  
  179.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  180.                 if (file_exists(__DIR__.'/treeicon.png')) {
  181.                         $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
  182.                 } else {
  183.                         $tree_icon = null; // default icon (folder)
  184.                 }
  185.  
  186.                 $json[] = array(
  187.                         'id' => 'oidplus:search',
  188.                         'icon' => $tree_icon,
  189.                         'text' => 'Search'
  190.                 );
  191.  
  192.                 return true;
  193.         }
  194.  
  195.         public function tree_search($request) {
  196.                 return false;
  197.         }
  198. }
  199.  
  200. OIDplus::registerPagePlugin(new OIDplusPagePublicSearch());
  201.