Subversion Repositories oidplus

Rev

Rev 224 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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