Subversion Repositories oidplus

Rev

Rev 360 | 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
511 daniel-mar 5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
61 daniel-mar 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
 
511 daniel-mar 20
if (!defined('INSIDE_OIDPLUS')) die();
21
 
256 daniel-mar 22
class OIDplusPagePublicSearch extends OIDplusPagePluginPublic {
61 daniel-mar 23
 
75 daniel-mar 24
        public function init($html=true) {
263 daniel-mar 25
                OIDplus::config()->prepareConfigKey('search_min_term_length', 'Minimum length of a search term', '3', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
61 daniel-mar 26
                        if (!is_numeric($value) || ($value < 0)) {
360 daniel-mar 27
                                throw new OIDplusException(_L('Please enter a valid value.'));
61 daniel-mar 28
                        }
263 daniel-mar 29
                });
61 daniel-mar 30
        }
31
 
32
        public function gui($id, &$out, &$handled) {
33
                if (explode('$',$id)[0] == 'oidplus:search') {
34
                        $handled = true;
35
 
360 daniel-mar 36
                        $out['title'] = _L('Search');
241 daniel-mar 37
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
61 daniel-mar 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
 
107 daniel-mar 52
                                // TODO: make it via AJAX? Reloading the whole page is not good. But attention: Also allow NoScript
61 daniel-mar 53
                                $out['text'] .= '<form id="searchForm" action="?goto=oidplus:search" method="POST">
54
                                                 <input type="hidden" name="search" value="1">
360 daniel-mar 55
                                                 '._L('Search for').': <input type="text" id="term" name="term" value="'.htmlentities($search_term).'"><br><br>
61 daniel-mar 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>
360 daniel-mar 66
                                                 '._L('Search in').': <select name="namespace" id="namespace" onchange="searchNsSelect(this.value);"><br><br>';
61 daniel-mar 67
 
227 daniel-mar 68
                                foreach (OIDplus::getEnabledObjectTypes() as $ot) {
61 daniel-mar 69
                                        $out['text'] .= '<option value="'.htmlentities($ot::ns()).'"'.(($ns == $ot::ns()) ? ' selected' : '').'>'.htmlentities($ot::objectTypeTitle()).'</option>';
70
                                }
360 daniel-mar 71
                                $out['text'] .= '<option value="oidplus:ra"'.(($ns == 'oidplus:ra') ? ' selected' : '').'>'._L('Registration Authority').'</option>
61 daniel-mar 72
                                                 </select><br><br>
73
                                <div id="search_options_ra">
74
                                <!-- TODO: RA specific selection criterias -->
75
                                </div>
76
                                <div id="search_options_object">
360 daniel-mar 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>
61 daniel-mar 79
                                <div id="search_options_oid">
360 daniel-mar 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>
61 daniel-mar 82
                                </div>
83
                                </div>
84
                                 <br>
85
 
360 daniel-mar 86
                                <input type="submit" value="'._L('Search').'">
61 daniel-mar 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) {
360 daniel-mar 97
                                                $out['text'] .= '<p><font color="red">'._L('Error: You must enter a search term.').'</font></p>';
61 daniel-mar 98
                                        } else if (strlen($search_term) < $min_length) {
360 daniel-mar 99
                                                $out['text'] .= '<p><font color="red">'._L('Error: Search term minimum length is %1 characters.',$min_length).'</font></p>';
61 daniel-mar 100
                                        } else {
101
                                                if ($ns == 'oidplus:ra') {
360 daniel-mar 102
                                                        $out['text'] .= '<h2>'._L('Search results for RA %1',htmlentities($search_term)).'</h2>';
61 daniel-mar 103
 
150 daniel-mar 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.'%';
61 daniel-mar 107
 
150 daniel-mar 108
                                                        if (count($sql_where) == 0) $sql_where[] = '1=0';
261 daniel-mar 109
                                                        $res = OIDplus::db()->query("select * from ###ra where (".implode(' or ', $sql_where).")", $prep_where);
61 daniel-mar 110
 
111
                                                        $count = 0;
236 daniel-mar 112
                                                        while ($row = $res->fetch_object()) {
104 daniel-mar 113
                                                                $email = str_replace('@', '&', $row->email);
250 daniel-mar 114
                                                                $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:rainfo$'.str_replace('@','&',$email)).'>'.htmlentities($email).'</a>: <b>'.htmlentities($row->ra_name).'</b></p>';
61 daniel-mar 115
                                                                $count++;
116
                                                        }
117
                                                        if ($count == 0) {
360 daniel-mar 118
                                                                $out['text'] .= '<p>'._L('Nothing found').'</p>';
61 daniel-mar 119
                                                        }
120
                                                } else {
360 daniel-mar 121
                                                        $out['text'] .= '<h2>'._L('Search results for %1 (%2)',htmlentities($search_term),htmlentities($ns)).'</h2>';
61 daniel-mar 122
 
150 daniel-mar 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.'%'; }
61 daniel-mar 127
 
128
                                                        if (isset($_POST["search_asn1id"])) {
261 daniel-mar 129
                                                                $res = OIDplus::db()->query("select * from ###asn1id where name like ?", array('%'.$search_term.'%'));
236 daniel-mar 130
                                                                while ($row = $res->fetch_object()) {
150 daniel-mar 131
                                                                        $sql_where[] = "id = ?"; $prep_where[] = $row->oid;
61 daniel-mar 132
                                                                }
133
                                                        }
134
 
135
                                                        if (isset($_POST["search_iri"])) {
261 daniel-mar 136
                                                                $res = OIDplus::db()->query("select * from ###iri where name like ?", array('%'.$search_term.'%'));
236 daniel-mar 137
                                                                while ($row = $res->fetch_object()) {
150 daniel-mar 138
                                                                        $sql_where[] = "id = ?"; $prep_where[] = $row->oid;
61 daniel-mar 139
                                                                }
140
                                                        }
141
 
150 daniel-mar 142
                                                        if (count($sql_where) == 0) $sql_where[] = '1=0';
143
                                                        array_unshift($prep_where, $ns.':%');
224 daniel-mar 144
 
261 daniel-mar 145
                                                        $res = OIDplus::db()->query("select * from ###objects where id like ? and (".implode(' or ', $sql_where).")", $prep_where);
61 daniel-mar 146
 
147
                                                        $count = 0;
236 daniel-mar 148
                                                        while ($row = $res->fetch_object()) {
250 daniel-mar 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?
61 daniel-mar 150
                                                                $count++;
151
                                                        }
152
                                                        if ($count == 0) {
360 daniel-mar 153
                                                                $out['text'] .= '<p>'._L('Nothing found').'</p>';
61 daniel-mar 154
                                                        }
155
                                                }
156
                                        }
157
                                }
158
                        } catch (Exception $e) {
360 daniel-mar 159
                                $out['text'] = _L('Error: %1',$e->getMessage());
61 daniel-mar 160
                        }
161
                }
162
        }
163
 
282 daniel-mar 164
        public function publicSitemap(&$out) {
360 daniel-mar 165
                $out[] = 'oidplus:search';
282 daniel-mar 166
        }
167
 
106 daniel-mar 168
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
61 daniel-mar 169
                if (file_exists(__DIR__.'/treeicon.png')) {
241 daniel-mar 170
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
61 daniel-mar 171
                } else {
172
                        $tree_icon = null; // default icon (folder)
173
                }
174
 
175
                $json[] = array(
176
                        'id' => 'oidplus:search',
177
                        'icon' => $tree_icon,
360 daniel-mar 178
                        'text' => _L('Search')
61 daniel-mar 179
                );
104 daniel-mar 180
 
181
                return true;
61 daniel-mar 182
        }
108 daniel-mar 183
 
184
        public function tree_search($request) {
185
                return false;
186
        }
360 daniel-mar 187
}