Subversion Repositories oidplus

Rev

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