Rev 704 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 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 | private function prepareSearchParams(&$params, $is_searching) { |
||
33 | $params['term'] = isset($params['term']) ? trim($params['term']) : ''; |
||
34 | $params['namespace'] = isset($params['namespace']) ? trim($params['namespace']) : ''; |
||
35 | |||
36 | // Default criteria selection: |
||
37 | if ($is_searching) { |
||
38 | $params['search_title'] = isset($params['search_title']) && $params['search_title']; |
||
39 | $params['search_description'] = isset($params['search_description']) && $params['search_description']; |
||
40 | $params['search_asn1id'] = isset($params['search_asn1id']) && $params['search_asn1id']; |
||
41 | $params['search_iri'] = isset($params['search_iri']) && $params['search_iri']; |
||
42 | } else { |
||
43 | $params['search_title'] = true; |
||
44 | $params['search_description'] = false; |
||
45 | $params['search_asn1id'] = true; |
||
46 | $params['search_iri'] = true; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | private function doSearch($params) { |
||
51 | $output = ''; |
||
52 | |||
53 | // Note: The SQL collation defines if search is case sensitive or case insensitive |
||
54 | |||
55 | $min_length = OIDplus::config()->getValue('search_min_term_length'); |
||
56 | |||
57 | $this->prepareSearchParams($params, true); |
||
58 | |||
59 | if (strlen($params['term']) == 0) { |
||
60 | $output .= '<p><font color="red">'._L('Error: You must enter a search term.').'</font></p>'; |
||
61 | } else if (strlen($params['term']) < $min_length) { |
||
62 | $output .= '<p><font color="red">'._L('Error: Search term minimum length is %1 characters.',$min_length).'</font></p>'; |
||
63 | } else { |
||
64 | // TODO: case insensitive comparison (or should we leave that to the DBMS?) |
||
65 | |||
66 | if ($params['namespace'] == 'oidplus:ra') { |
||
67 | $output .= '<h2>'._L('Search results for RA %1',htmlentities($params['term'])).'</h2>'; |
||
68 | |||
69 | $sql_where = array(); $prep_where = array(); |
||
70 | $sql_where[] = "email like ?"; $prep_where[] = '%'.$params['term'].'%'; |
||
71 | $sql_where[] = "ra_name like ?"; $prep_where[] = '%'.$params['term'].'%'; |
||
72 | |||
73 | if (count($sql_where) == 0) $sql_where[] = '1=0'; |
||
74 | $res = OIDplus::db()->query("select * from ###ra where (".implode(' or ', $sql_where).")", $prep_where); |
||
75 | |||
76 | $count = 0; |
||
77 | while ($row = $res->fetch_object()) { |
||
78 | $email = str_replace('@', '&', $row->email); |
||
79 | $output .= '<p><a '.OIDplus::gui()->link('oidplus:rainfo$'.str_replace('@','&',$email)).'>'.htmlentities($email).'</a>: <b>'.htmlentities($row->ra_name).'</b></p>'; |
||
80 | $count++; |
||
81 | } |
||
82 | if ($count == 0) { |
||
83 | $output .= '<p>'._L('Nothing found').'</p>'; |
||
84 | } |
||
85 | } else { |
||
86 | $output .= '<h2>'._L('Search results for %1 (%2)',htmlentities($params['term']),htmlentities($params['namespace'])).'</h2>'; |
||
87 | |||
88 | $sql_where = array(); $prep_where = array(); |
||
89 | $sql_where[] = "id like ?"; $prep_where[] = '%'.$params['term'].'%'; // TODO: should we rather do findFitting(), so we can e.g. find GUIDs with different notation? |
||
90 | if ($params["search_title"]) { $sql_where[] = "title like ?"; $prep_where[] = '%'.$params['term'].'%'; } |
||
91 | if ($params["search_description"]) { $sql_where[] = "description like ?"; $prep_where[] = '%'.$params['term'].'%'; } |
||
92 | |||
93 | if ($params["search_asn1id"]) { |
||
94 | $res = OIDplus::db()->query("select * from ###asn1id where name like ?", array('%'.$params['term'].'%')); |
||
95 | while ($row = $res->fetch_object()) { |
||
96 | $sql_where[] = "id = ?"; $prep_where[] = $row->oid; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | if ($params["search_iri"]) { |
||
101 | $res = OIDplus::db()->query("select * from ###iri where name like ?", array('%'.$params['term'].'%')); |
||
102 | while ($row = $res->fetch_object()) { |
||
103 | $sql_where[] = "id = ?"; $prep_where[] = $row->oid; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | if (count($sql_where) == 0) $sql_where[] = '1=0'; |
||
108 | array_unshift($prep_where, $params['namespace'].':%'); |
||
109 | |||
110 | $res = OIDplus::db()->query("select * from ###objects where id like ? and (".implode(' or ', $sql_where).")", $prep_where); |
||
111 | |||
112 | $count = 0; |
||
113 | while ($row = $res->fetch_object()) { |
||
114 | $output .= '<p><a '.OIDplus::gui()->link($row->id).'>'.htmlentities($row->id).'</a>'; |
||
115 | |||
116 | $asn1ids = array(); |
||
117 | $res2 = OIDplus::db()->query("select name from ###asn1id where oid = ?", array($row->id)); |
||
118 | while ($row2 = $res2->fetch_object()) { |
||
119 | $asn1ids[] = $row2->name; |
||
120 | } |
||
121 | if (count($asn1ids) > 0) { |
||
122 | $asn1ids = implode(', ', $asn1ids); |
||
123 | $output .= ' ('.$asn1ids.')'; |
||
124 | } |
||
125 | |||
126 | if (htmlentities($row->title) != '') $output .= ': <b>'.htmlentities($row->title).'</b></p>'; |
||
127 | $count++; |
||
128 | } |
||
129 | if ($count == 0) { |
||
130 | $output .= '<p>'._L('Nothing found').'</p>'; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | // Highlight exact match |
||
135 | //$output = str_ireplace($params['term'], '<font color="red">'.$params['term'].'</font>', $output); |
||
136 | $output = preg_replace('@('.preg_quote($params['term'],'@').')@i', '<font color="red">\\1</font>', $output); // this variant keeps the case of the search term |
||
137 | } |
||
138 | |||
139 | return $output; |
||
140 | } |
||
141 | |||
142 | public function action($actionID, $params) { |
||
143 | |||
144 | if ($actionID == 'search') { |
||
145 | // Search with JavaScript/AJAX |
||
146 | $ret = $this->doSearch($params); |
||
147 | return array("status" => 0, "output" => $ret); |
||
148 | } |
||
149 | |||
150 | } |
||
151 | |||
152 | public function gui($id, &$out, &$handled) { |
||
153 | if (explode('$',$id)[0] == 'oidplus:search') { |
||
154 | $handled = true; |
||
155 | |||
156 | $out['title'] = _L('Search'); |
||
157 | $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : ''; |
||
158 | |||
159 | $out['text'] = ''; |
||
160 | |||
161 | try { |
||
162 | $params = $_POST; |
||
163 | |||
164 | $this->prepareSearchParams($params, isset($params['search'])); |
||
165 | |||
166 | $out['text'] .= '<form id="searchForm" action="?goto=oidplus:search" method="POST"> |
||
167 | <input type="hidden" name="search" value="1"> |
||
168 | '._L('Search for').': <input type="text" id="term" name="term" value="'.htmlentities($params['term']).'"><br><br> |
||
169 | <script> |
||
170 | function searchNsSelect(ns) { |
||
171 | $("#search_options_oid")[0].style.display = (ns == "oid") ? "block" : "none"; |
||
172 | $("#search_options_object")[0].style.display = (ns == "oidplus:ra") ? "none" : "block"; |
||
173 | $("#search_options_ra")[0].style.display = (ns == "oidplus:ra") ? "block" : "none"; |
||
174 | } |
||
175 | $( document ).ready(function() { |
||
176 | searchNsSelect($("#namespace")[0].value); |
||
177 | }); |
||
178 | </script> |
||
179 | '._L('Search in').': <select name="namespace" id="namespace" onchange="searchNsSelect(this.value);"><br><br>'; |
||
180 | |||
181 | foreach (OIDplus::getEnabledObjectTypes() as $ot) { |
||
182 | $out['text'] .= '<option value="'.htmlentities($ot::ns()).'"'.(($params['namespace'] == $ot::ns()) ? ' selected' : '').'>'.htmlentities($ot::objectTypeTitle()).'</option>'; |
||
183 | } |
||
184 | $out['text'] .= '<option value="oidplus:ra"'.(($params['namespace'] == 'oidplus:ra') ? ' selected' : '').'>'._L('Registration Authority').'</option> |
||
185 | </select><br><br> |
||
186 | <div id="search_options_ra"> |
||
187 | <!-- TODO: RA specific selection criterias --> |
||
188 | </div> |
||
189 | <div id="search_options_object"> |
||
190 | <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> |
||
191 | <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> |
||
192 | <div id="search_options_oid"> |
||
193 | <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> |
||
194 | <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> |
||
195 | </div> |
||
196 | </div> |
||
197 | <br> |
||
198 | |||
199 | <input type="submit" value="'._L('Search').'" onclick="return OIDplusPagePublicSearch.search_button_click()"> |
||
200 | </form>'; |
||
201 | |||
202 | $out['text'] .= '<div id="search_output">'; // will be filled with either AJAX or staticly (HTML form submit) |
||
203 | if (isset($params['search'])) { |
||
204 | // Search with NoJS/HTML |
||
205 | $out['text'] .= $this->doSearch($params); |
||
206 | } |
||
207 | $out['text'] .= '</div>'; |
||
208 | } catch (Exception $e) { |
||
209 | $out['text'] = _L('Error: %1',$e->getMessage()); |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | |||
214 | public function publicSitemap(&$out) { |
||
215 | $out[] = 'oidplus:search'; |
||
216 | } |
||
217 | |||
218 | public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') { |
||
219 | if (file_exists(__DIR__.'/treeicon.png')) { |
||
220 | $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png'; |
||
221 | } else { |
||
222 | $tree_icon = null; // default icon (folder) |
||
223 | } |
||
224 | |||
225 | $json[] = array( |
||
226 | 'id' => 'oidplus:search', |
||
227 | 'icon' => $tree_icon, |
||
228 | 'text' => _L('Search') |
||
229 | ); |
||
230 | |||
231 | return true; |
||
232 | } |
||
233 | |||
234 | public function tree_search($request) { |
||
235 | return false; |
||
236 | } |
||
237 | } |