Rev 136 | Rev 183 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | 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 | |||
2 | daniel-mar | 22 | class OIDplusGui { |
23 | |||
24 | private static $crudCounter = 0; |
||
25 | |||
26 | protected static function objDescription($html) { |
||
27 | // We allow HTML, but no hacking |
||
12 | daniel-mar | 28 | $html = anti_xss($html); |
11 | daniel-mar | 29 | |
30 | return trim_br($html); |
||
2 | daniel-mar | 31 | } |
32 | |||
33 | protected static function showCrud($parent='oid:') { |
||
34 | $items_total = 0; |
||
35 | $items_hidden = 0; |
||
36 | |||
37 | $objParent = OIDplusObject::parse($parent); |
||
38 | |||
39 | $output = ''; |
||
40 | $output .= '<div class="container box"><div id="suboid_table" class="table-responsive">'; |
||
41 | $output .= '<table class="table table-bordered table-striped">'; |
||
42 | $output .= ' <tr>'; |
||
43 | $output .= ' <th>ID'.(($objParent::ns() == 'gs1') ? ' (without check digit)' : '').'</th>'; |
||
44 | if ($objParent::ns() == 'oid') $output .= ' <th>ASN.1 IDs (comma sep.)</th>'; |
||
45 | if ($objParent::ns() == 'oid') $output .= ' <th>IRI IDs (comma sep.)</th>'; |
||
46 | $output .= ' <th>RA</th>'; |
||
47 | if ($objParent->userHasWriteRights()) { |
||
48 | $output .= ' <th>Hide</th>'; |
||
49 | $output .= ' <th>Update</th>'; |
||
50 | $output .= ' <th>Delete</th>'; |
||
51 | } |
||
52 | $output .= ' <th>Created</th>'; |
||
53 | $output .= ' <th>Updated</th>'; |
||
54 | $output .= ' </tr>'; |
||
55 | |||
150 | daniel-mar | 56 | $result = OIDplus::db()->query("select o.*, r.ra_name from ".OIDPLUS_TABLENAME_PREFIX."objects o left join ".OIDPLUS_TABLENAME_PREFIX."ra r on r.email = o.ra_email where parent = ? order by ".OIDplus::db()->natOrder('id'), array($parent)); |
2 | daniel-mar | 57 | while ($row = OIDplus::db()->fetch_object($result)) { |
58 | $obj = OIDplusObject::parse($row->id); |
||
59 | |||
60 | $items_total++; |
||
61 | if (!$obj->userHasReadRights()) { |
||
62 | $items_hidden++; |
||
63 | continue; |
||
64 | } |
||
65 | |||
66 | $show_id = $obj->crudShowId($objParent); |
||
67 | |||
68 | $asn1ids = array(); |
||
150 | daniel-mar | 69 | $res2 = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ? order by lfd", array($row->id)); |
2 | daniel-mar | 70 | while ($row2 = OIDplus::db()->fetch_array($res2)) { |
71 | $asn1ids[] = $row2['name']; |
||
72 | } |
||
73 | |||
74 | $iris = array(); |
||
150 | daniel-mar | 75 | $res2 = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = ? order by lfd", array($row->id)); |
2 | daniel-mar | 76 | while ($row2 = OIDplus::db()->fetch_array($res2)) { |
77 | $iris[] = $row2['name']; |
||
78 | } |
||
79 | |||
80 | $output .= '<tr>'; |
||
104 | daniel-mar | 81 | $output .= ' <td><a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.htmlentities($show_id).'</a></td>'; |
2 | daniel-mar | 82 | if ($objParent->userHasWriteRights()) { |
5 | daniel-mar | 83 | if ($obj::ns() == 'oid') { |
84 | $output .= ' <td><input type="text" id="asn1ids_'.$row->id.'" value="'.implode(', ', $asn1ids).'"></td>'; |
||
85 | $output .= ' <td><input type="text" id="iris_'.$row->id.'" value="'.implode(', ', $iris).'"></td>'; |
||
86 | } |
||
2 | daniel-mar | 87 | $output .= ' <td><input type="text" id="ra_email_'.$row->id.'" value="'.$row->ra_email.'"></td>'; |
88 | $output .= ' <td><input type="checkbox" id="hide_'.$row->id.'" '.($row->confidential ? 'checked' : '').'></td>'; |
||
104 | daniel-mar | 89 | $output .= ' <td><button type="button" name="update_'.$row->id.'" id="update_'.$row->id.'" class="btn btn-success btn-xs update" onclick="crudActionUpdate('.js_escape($row->id).', '.js_escape($parent).')">Update</button></td>'; |
90 | $output .= ' <td><button type="button" name="delete_'.$row->id.'" id="delete_'.$row->id.'" class="btn btn-danger btn-xs delete" onclick="crudActionDelete('.js_escape($row->id).', '.js_escape($parent).')">Delete</button></td>'; |
||
91 | $output .= ' <td>'.oidplus_formatdate($row->created).'</td>'; |
||
92 | $output .= ' <td>'.oidplus_formatdate($row->updated).'</td>'; |
||
2 | daniel-mar | 93 | } else { |
94 | if ($asn1ids == '') $asn1ids = '<i>(none)</i>'; |
||
95 | if ($iris == '') $iris = '<i>(none)</i>'; |
||
5 | daniel-mar | 96 | if ($obj::ns() == 'oid') { |
97 | $asn1ids_ext = array(); |
||
98 | foreach ($asn1ids as $asn1id) { |
||
104 | daniel-mar | 99 | $asn1ids_ext[] = '<a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.$asn1id.'</a>'; |
5 | daniel-mar | 100 | } |
101 | $output .= ' <td>'.implode(', ', $asn1ids_ext).'</td>'; |
||
102 | $output .= ' <td>'.implode(', ', $iris).'</td>'; |
||
103 | } |
||
109 | daniel-mar | 104 | $output .= ' <td><a '.oidplus_link('oidplus:rainfo$'.str_replace('@','&',$row->ra_email)).'>'.htmlentities(empty($row->ra_name) ? str_replace('@','&',$row->ra_email) : $row->ra_name).'</a></td>'; |
104 | daniel-mar | 105 | $output .= ' <td>'.oidplus_formatdate($row->created).'</td>'; |
106 | $output .= ' <td>'.oidplus_formatdate($row->updated).'</td>'; |
||
2 | daniel-mar | 107 | } |
108 | $output .= '</tr>'; |
||
109 | } |
||
110 | |||
150 | daniel-mar | 111 | $result = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($parent)); |
2 | daniel-mar | 112 | $parent_ra_email = OIDplus::db()->num_rows($result) > 0 ? OIDplus::db()->fetch_object($result)->ra_email : ''; |
113 | |||
114 | if ($objParent->userHasWriteRights()) { |
||
115 | $output .= '<tr>'; |
||
116 | $prefix = is_null($objParent) ? '' : $objParent->crudInsertPrefix(); |
||
117 | if ($objParent::ns() == 'oid') { |
||
118 | $output .= ' <td>'.$prefix.' <input type="text" id="id" value="" style="width:100%;min-width:50px"></td>'; // TODO: idee classname vergeben, z.B. "OID" und dann mit einem oid-spezifischen css die breite einstellbar machen, somit hat das plugin mehr kontrolle über das aussehen und die mindestbreiten |
||
119 | } else { |
||
120 | $output .= ' <td>'.$prefix.' <input type="text" id="id" value=""></td>'; |
||
121 | } |
||
122 | if ($objParent::ns() == 'oid') $output .= ' <td><input type="text" id="asn1ids" value=""></td>'; |
||
123 | if ($objParent::ns() == 'oid') $output .= ' <td><input type="text" id="iris" value=""></td>'; |
||
124 | $output .= ' <td><input type="text" id="ra_email" value="'.htmlentities($parent_ra_email).'"></td>'; |
||
125 | $output .= ' <td><input type="checkbox" id="hide"></td>'; |
||
104 | daniel-mar | 126 | $output .= ' <td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs update" onclick="crudActionInsert('.js_escape($parent).')">Insert</button></td>'; |
2 | daniel-mar | 127 | $output .= ' <td></td>'; |
128 | $output .= ' <td></td>'; |
||
129 | $output .= ' <td></td>'; |
||
130 | $output .= '</tr>'; |
||
131 | } else { |
||
132 | if ($items_total-$items_hidden == 0) { |
||
133 | $cols = ($objParent::ns() == 'oid') ? 7 : 5; |
||
134 | $output .= '<tr><td colspan="'.$cols.'">No items available</td></tr>'; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | $output .= '</table>'; |
||
139 | $output .= '</div></div>'; |
||
140 | |||
141 | if ($items_hidden == 1) { |
||
107 | daniel-mar | 142 | $output .= '<p>'.$items_hidden.' item is hidden. Please <a '.oidplus_link('oidplus:login').'>log in</a> to see it.</p>'; |
2 | daniel-mar | 143 | } else if ($items_hidden > 1) { |
107 | daniel-mar | 144 | $output .= '<p>'.$items_hidden.' items are hidden. Please <a '.oidplus_link('oidplus:login').'>log in</a> to see them.</p>'; |
2 | daniel-mar | 145 | } |
146 | |||
147 | return $output; |
||
148 | } |
||
149 | |||
136 | daniel-mar | 150 | // 'quickbars' added 11 July 2019: Disabled because of two problems: |
151 | // 1. When you load TinyMCE via AJAX using the left menu, the quickbar is immediately shown, even if TinyMCE does not have the focus |
||
152 | // 2. When you load a page without TinyMCE using the left menu, the quickbar is still visible, although there is no edit |
||
153 | public static $exclude_tinymce_plugins = array('fullpage', 'bbcode', 'quickbars'); |
||
2 | daniel-mar | 154 | |
155 | protected static function showMCE($name, $content) { |
||
156 | $mce_plugins = array(); |
||
4 | daniel-mar | 157 | foreach (glob(__DIR__ . '/../../3p/tinymce/plugins/*') as $m) { // */ |
2 | daniel-mar | 158 | $mce_plugins[] = basename($m); |
159 | } |
||
160 | |||
161 | foreach (self::$exclude_tinymce_plugins as $exclude) { |
||
162 | $index = array_search($exclude, $mce_plugins); |
||
163 | if ($index !== false) unset($mce_plugins[$index]); |
||
164 | } |
||
165 | |||
4 | daniel-mar | 166 | $out = '<script> |
167 | tinymce.remove("#'.$name.'"); |
||
2 | daniel-mar | 168 | tinymce.init({ |
4 | daniel-mar | 169 | selector: "#'.$name.'", |
170 | height: 200, |
||
171 | statusbar: false, |
||
172 | // menubar:false, |
||
173 | // toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | fontsizeselect", |
||
174 | toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect", |
||
123 | daniel-mar | 175 | plugins: "'.implode(' ', $mce_plugins).'", |
176 | mobile: { |
||
177 | theme: "mobile", |
||
178 | toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect", |
||
179 | plugins: "'.implode(' ', $mce_plugins).'" |
||
180 | } |
||
181 | |||
2 | daniel-mar | 182 | }); |
4 | daniel-mar | 183 | </script>'; |
2 | daniel-mar | 184 | |
89 | daniel-mar | 185 | $content = htmlentities($content); // For some reason, if we want to display the text "<xyz>" in TinyMCE, we need to double-encode things! < will not be accepted, we need &lt; ... why? |
186 | |||
4 | daniel-mar | 187 | $out .= '<textarea name="'.htmlentities($name).'" id="'.htmlentities($name).'">'.trim($content).'</textarea><br>'; |
2 | daniel-mar | 188 | |
189 | return $out; |
||
190 | } |
||
191 | |||
192 | public static function generateContentPage($id) { |
||
193 | $out = array(); |
||
194 | |||
195 | $handled = false; |
||
196 | $out['title'] = ''; |
||
32 | daniel-mar | 197 | $out['icon'] = ''; |
2 | daniel-mar | 198 | $out['text'] = ''; |
199 | |||
200 | // === Plugins === |
||
201 | |||
61 | daniel-mar | 202 | foreach (OIDplus::getPagePlugins('*') as $plugin) { |
203 | $plugin->gui($id, $out, $handled); |
||
204 | } |
||
2 | daniel-mar | 205 | |
206 | // === Everything else (objects) === |
||
207 | |||
208 | if (!$handled) { |
||
93 | daniel-mar | 209 | try { |
210 | $obj = OIDplusObject::parse($id); |
||
211 | } catch (Exception $e) { |
||
212 | $out['title'] = 'Error'; |
||
213 | $out['icon'] = 'img/error_big.png'; |
||
214 | $out['text'] = htmlentities($e->getMessage()); |
||
215 | return $out; |
||
216 | } |
||
2 | daniel-mar | 217 | |
218 | if ((!is_null($obj)) && (!$obj->userHasReadRights())) { |
||
219 | $out['title'] = 'Access denied'; |
||
34 | daniel-mar | 220 | $out['icon'] = 'img/error_big.png'; |
107 | daniel-mar | 221 | $out['text'] = '<p>Please <a '.oidplus_link('oidplus:login').'>log in</a> to receive information about this object.</p>'; |
2 | daniel-mar | 222 | return $out; |
223 | } |
||
224 | |||
150 | daniel-mar | 225 | $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($id)); |
2 | daniel-mar | 226 | $row = OIDplus::db()->fetch_array($res); |
227 | |||
228 | if (empty($row['title'])) { |
||
229 | $out['title'] = is_null($obj) ? $id : $obj->defaultTitle(); |
||
230 | } else { |
||
231 | $out['title'] = $row['title']; |
||
232 | } |
||
34 | daniel-mar | 233 | $out['icon'] = 'img/object_big.png'; |
2 | daniel-mar | 234 | |
235 | if (isset($row['description'])) { |
||
92 | daniel-mar | 236 | if (empty($row['description'])) { |
237 | if (empty($row['title'])) { |
||
238 | $desc = '<p><i>No description for this object available</i></p>'; |
||
239 | } else { |
||
240 | $desc = $row['title']; |
||
241 | } |
||
242 | } else { |
||
243 | $desc = OIDplusGui::objDescription($row['description']); |
||
244 | } |
||
245 | |||
2 | daniel-mar | 246 | if ($obj->userHasWriteRights()) { |
247 | $rand = ++self::$crudCounter; |
||
248 | $desc = '<noscript><p><b>You need to enable JavaScript to edit title or description of this object.</b></p>'.$desc.'</noscript>'; |
||
249 | $desc .= '<div class="container box" style="display:none" id="descbox_'.$rand.'">'; |
||
250 | $desc .= 'Title: <input type="text" name="title" id="titleedit" value="'.htmlentities($row['title']).'"><br><br>Description:<br>'; |
||
251 | $desc .= self::showMCE('description', $row['description']); |
||
104 | daniel-mar | 252 | $desc .= '<button type="button" name="update_desc" id="update_desc" class="btn btn-success btn-xs update" onclick="updateDesc()">Update description</button>'; |
2 | daniel-mar | 253 | $desc .= '</div>'; |
254 | $desc .= '<script>document.getElementById("descbox_'.$rand.'").style.display = "block";</script>'; |
||
255 | } |
||
256 | } else { |
||
257 | $desc = ''; |
||
258 | } |
||
259 | |||
98 | daniel-mar | 260 | $parent = null; |
261 | |||
2 | daniel-mar | 262 | $matches_any_registered_type = false; |
61 | daniel-mar | 263 | foreach (OIDplus::getRegisteredObjectTypes() as $ot) { |
2 | daniel-mar | 264 | if ($obj = $ot::parse($id)) { |
265 | $matches_any_registered_type = true; |
||
266 | if ((OIDplus::db()->num_rows($res) == 0) && !$obj->isRoot()){ |
||
267 | http_response_code(404); |
||
268 | $out['title'] = 'Object not found'; |
||
34 | daniel-mar | 269 | $out['icon'] = 'img/error_big.png'; |
2 | daniel-mar | 270 | $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.'; |
271 | return $out; |
||
272 | } else { |
||
34 | daniel-mar | 273 | $obj->getContentPage($out['title'], $out['text'], $out['icon']); |
98 | daniel-mar | 274 | $parent = $obj->getParent(); |
275 | break; |
||
2 | daniel-mar | 276 | } |
277 | } |
||
278 | } |
||
279 | if (!$matches_any_registered_type) { |
||
280 | http_response_code(404); |
||
281 | $out['title'] = 'Object not found'; |
||
34 | daniel-mar | 282 | $out['icon'] = 'img/error_big.png'; |
2 | daniel-mar | 283 | $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.'; |
284 | return $out; |
||
285 | } |
||
286 | |||
103 | daniel-mar | 287 | if ($parent) { |
288 | if ($parent->isRoot()) { |
||
98 | daniel-mar | 289 | |
290 | $parent_link_text = $parent->objectTypeTitle(); |
||
129 | daniel-mar | 291 | $out['text'] = '<p><a '.oidplus_link($parent->root()).'><img src="img/arrow_back.png" width="16"> Parent node: '.htmlentities($parent_link_text).'</a></p>' . $out['text']; |
98 | daniel-mar | 292 | |
293 | } else { |
||
150 | daniel-mar | 294 | $res_ = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($parent->nodeId())); |
98 | daniel-mar | 295 | $row_ = OIDplus::db()->fetch_array($res_); |
296 | |||
297 | $parent_title = $row_['title']; |
||
298 | if (empty($parent_title) && ($parent->ns() == 'oid')) { |
||
150 | daniel-mar | 299 | $res_ = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ?", array($parent->nodeId())); |
98 | daniel-mar | 300 | $row_ = OIDplus::db()->fetch_array($res_); |
301 | $parent_title = $row_['name']; // TODO: multiple ASN1 ids? |
||
302 | } |
||
303 | |||
304 | $parent_link_text = empty($parent_title) ? explode(':',$parent->nodeId())[1] : $parent_title.' ('.explode(':',$parent->nodeId())[1].')'; |
||
305 | |||
129 | daniel-mar | 306 | $out['text'] = '<p><a '.oidplus_link($parent->nodeId()).'><img src="img/arrow_back.png" width="16"> Parent node: '.htmlentities($parent_link_text).'</a></p>' . $out['text']; |
98 | daniel-mar | 307 | } |
103 | daniel-mar | 308 | } else { |
309 | $parent_link_text = 'Go back to front page'; |
||
127 | daniel-mar | 310 | $out['text'] = '<p><a '.oidplus_link('oidplus:system').'><img src="img/arrow_back.png" width="16"> '.htmlentities($parent_link_text).'</a></p>' . $out['text']; |
98 | daniel-mar | 311 | } |
312 | |||
2 | daniel-mar | 313 | if (strpos($out['text'], '%%DESC%%') !== false) |
314 | $out['text'] = str_replace('%%DESC%%', $desc, $out['text']); |
||
315 | if (strpos($out['text'], '%%CRUD%%') !== false) |
||
316 | $out['text'] = str_replace('%%CRUD%%', self::showCrud($id), $out['text']); |
||
317 | if (strpos($out['text'], '%%RA_INFO%%') !== false) |
||
104 | daniel-mar | 318 | $out['text'] = str_replace('%%RA_INFO%%', OIDplusPagePublicRaInfo::showRaInfo($row['ra_email']), $out['text']); |
101 | daniel-mar | 319 | |
320 | foreach (OIDplus::getPagePlugins('public') as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']); |
||
321 | foreach (OIDplus::getPagePlugins('ra') as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']); |
||
322 | foreach (OIDplus::getPagePlugins('admin') as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']); |
||
103 | daniel-mar | 323 | } else { |
120 | daniel-mar | 324 | // Other pages (search, whois, etc.) |
325 | /* |
||
326 | if ($id != 'oidplus:system') { |
||
103 | daniel-mar | 327 | $parent_link_text = 'Go back to front page'; |
127 | daniel-mar | 328 | $out['text'] = '<p><a '.oidplus_link('oidplus:system').'><img src="img/arrow_back.png" width="16"> '.htmlentities($parent_link_text).'</a></p>' . $out['text']; |
103 | daniel-mar | 329 | } |
120 | daniel-mar | 330 | */ |
2 | daniel-mar | 331 | } |
332 | |||
333 | return $out; |
||
334 | } |
||
335 | } |