Subversion Repositories oidplus

Rev

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