Subversion Repositories oidplus

Rev

Rev 360 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

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