Subversion Repositories oidplus

Rev

Rev 206 | Rev 228 | 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);
206 daniel-mar 38
                $parentNS = $objParent::ns();
2 daniel-mar 39
 
206 daniel-mar 40
                $result = OIDplus::db()->query("select o.*, r.ra_name " .
41
                                               "from ".OIDPLUS_TABLENAME_PREFIX."objects o " .
42
                                               "left join ".OIDPLUS_TABLENAME_PREFIX."ra r on r.email = o.ra_email " .
43
                                               "where parent = ? " .
44
                                               "order by ".OIDplus::db()->natOrder('id'), array($parent));
45
                $rows = array();
46
                if ($parentNS == 'oid') {
47
                        $one_weid_available = $objParent->isWeid(true);
48
                        while ($row = OIDplus::db()->fetch_object($result)) {
49
                                $obj = OIDplusObject::parse($row->id);
50
                                $rows[] = array($obj,$row);
51
                                if (!$one_weid_available) {
52
                                        if ($obj->isWeid(true)) $one_weid_available = true;
53
                                }
54
                        }
55
                } else {
56
                        $one_weid_available = false;
57
                        while ($row = OIDplus::db()->fetch_object($result)) {
58
                                $obj = OIDplusObject::parse($row->id);
59
                                $rows[] = array($obj,$row);
60
                        }
61
                }
62
 
2 daniel-mar 63
                $output = '';
64
                $output .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
65
                $output .= '<table class="table table-bordered table-striped">';
66
                $output .= '    <tr>';
206 daniel-mar 67
                $output .= '         <th>ID'.(($parentNS == 'gs1') ? ' (without check digit)' : '').'</th>';
68
                if ($parentNS == 'oid') {
69
                        if ($one_weid_available) $output .= '        <th>WEID</th>';
196 daniel-mar 70
                        $output .= '         <th>ASN.1 IDs (comma sep.)</th>';
71
                        $output .= '         <th>IRI IDs (comma sep.)</th>';
72
                }
2 daniel-mar 73
                $output .= '         <th>RA</th>';
204 daniel-mar 74
                $output .= '         <th>Comment</th>';
2 daniel-mar 75
                if ($objParent->userHasWriteRights()) {
76
                        $output .= '         <th>Hide</th>';
77
                        $output .= '         <th>Update</th>';
78
                        $output .= '         <th>Delete</th>';
79
                }
80
                $output .= '         <th>Created</th>';
81
                $output .= '         <th>Updated</th>';
82
                $output .= '    </tr>';
83
 
206 daniel-mar 84
                foreach ($rows as list($obj,$row)) {
2 daniel-mar 85
                        $items_total++;
86
                        if (!$obj->userHasReadRights()) {
87
                                $items_hidden++;
88
                                continue;
89
                        }
90
 
91
                        $show_id = $obj->crudShowId($objParent);
92
 
93
                        $asn1ids = array();
150 daniel-mar 94
                        $res2 = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ? order by lfd", array($row->id));
2 daniel-mar 95
                        while ($row2 = OIDplus::db()->fetch_array($res2)) {
96
                                $asn1ids[] = $row2['name'];
97
                        }
98
 
99
                        $iris = array();
150 daniel-mar 100
                        $res2 = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = ? order by lfd", array($row->id));
2 daniel-mar 101
                        while ($row2 = OIDplus::db()->fetch_array($res2)) {
102
                                $iris[] = $row2['name'];
103
                        }
104
 
105
                        $output .= '<tr>';
104 daniel-mar 106
                        $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 107
                        if ($objParent->userHasWriteRights()) {
206 daniel-mar 108
                                if ($parentNS == 'oid') {
109
                                        if ($one_weid_available) {
110
                                                if ($obj->isWeid(false)) {
111
                                                        $output .= '    <td>'.$obj->weidArc().'</td>';
112
                                                } else {
113
                                                        $output .= '    <td>n/a</td>';
114
                                                }
196 daniel-mar 115
                                        }
5 daniel-mar 116
                                        $output .= '     <td><input type="text" id="asn1ids_'.$row->id.'" value="'.implode(', ', $asn1ids).'"></td>';
117
                                        $output .= '     <td><input type="text" id="iris_'.$row->id.'" value="'.implode(', ', $iris).'"></td>';
118
                                }
204 daniel-mar 119
                                $output .= '     <td><input type="text" id="ra_email_'.$row->id.'" value="'.htmlentities($row->ra_email).'"></td>';
120
                                $output .= '     <td><input type="text" id="comment_'.$row->id.'" value="'.htmlentities($row->comment).'"></td>';
2 daniel-mar 121
                                $output .= '     <td><input type="checkbox" id="hide_'.$row->id.'" '.($row->confidential ? 'checked' : '').'></td>';
104 daniel-mar 122
                                $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>';
123
                                $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>';
124
                                $output .= '     <td>'.oidplus_formatdate($row->created).'</td>';
125
                                $output .= '     <td>'.oidplus_formatdate($row->updated).'</td>';
2 daniel-mar 126
                        } else {
127
                                if ($asn1ids == '') $asn1ids = '<i>(none)</i>';
128
                                if ($iris == '') $iris = '<i>(none)</i>';
206 daniel-mar 129
                                if ($parentNS == 'oid') {
130
                                        if ($one_weid_available) {
131
                                                if ($obj->isWeid(false)) {
132
                                                        $output .= '    <td>'.$obj->weidArc().'</td>';
133
                                                } else {
134
                                                        $output .= '    <td>n/a</td>';
135
                                                }
196 daniel-mar 136
                                        }
5 daniel-mar 137
                                        $asn1ids_ext = array();
138
                                        foreach ($asn1ids as $asn1id) {
104 daniel-mar 139
                                                $asn1ids_ext[] = '<a href="?goto='.urlencode($row->id).'" onclick="openAndSelectNode('.js_escape($row->id).', '.js_escape($parent).'); return false;">'.$asn1id.'</a>';
5 daniel-mar 140
                                        }
141
                                        $output .= '     <td>'.implode(', ', $asn1ids_ext).'</td>';
142
                                        $output .= '     <td>'.implode(', ', $iris).'</td>';
143
                                }
109 daniel-mar 144
                                $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>';
204 daniel-mar 145
                                $output .= '     <td>'.htmlentities($row->comment).'</td>';
104 daniel-mar 146
                                $output .= '     <td>'.oidplus_formatdate($row->created).'</td>';
147
                                $output .= '     <td>'.oidplus_formatdate($row->updated).'</td>';
2 daniel-mar 148
                        }
149
                        $output .= '</tr>';
150
                }
151
 
150 daniel-mar 152
                $result = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($parent));
2 daniel-mar 153
                $parent_ra_email = OIDplus::db()->num_rows($result) > 0 ? OIDplus::db()->fetch_object($result)->ra_email : '';
154
 
155
                if ($objParent->userHasWriteRights()) {
156
                        $output .= '<tr>';
157
                        $prefix = is_null($objParent) ? '' : $objParent->crudInsertPrefix();
206 daniel-mar 158
                        if ($parentNS == 'oid') {
199 daniel-mar 159
                                if ($objParent->isWeid(true)) {
160
                                        $output .= '     <td>'.$prefix.' <input oninput="frdl_oidid_change()" type="text" id="id" value="" style="width:100%;min-width:100px"></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
161
                                        $output .= '     <td><input type="text" name="weid" id="weid" value="" oninput="frdl_weid_change()"></td>';
162
                                } else {
163
                                        $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
206 daniel-mar 164
                                        if ($one_weid_available) $output .= '     <td></td>'; // WEID-editor not available for root nodes. Do it manually, please
199 daniel-mar 165
                                }
2 daniel-mar 166
                        } else {
167
                                $output .= '     <td>'.$prefix.' <input type="text" id="id" value=""></td>';
168
                        }
206 daniel-mar 169
                        if ($parentNS == 'oid') $output .= '     <td><input type="text" id="asn1ids" value=""></td>';
170
                        if ($parentNS == 'oid') $output .= '     <td><input type="text" id="iris" value=""></td>';
2 daniel-mar 171
                        $output .= '     <td><input type="text" id="ra_email" value="'.htmlentities($parent_ra_email).'"></td>';
204 daniel-mar 172
                        $output .= '     <td><input type="text" id="comment" value=""></td>';
2 daniel-mar 173
                        $output .= '     <td><input type="checkbox" id="hide"></td>';
104 daniel-mar 174
                        $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 175
                        $output .= '     <td></td>';
176
                        $output .= '     <td></td>';
177
                        $output .= '     <td></td>';
178
                        $output .= '</tr>';
179
                } else {
180
                        if ($items_total-$items_hidden == 0) {
206 daniel-mar 181
                                $cols = ($parentNS == 'oid') ? 7 : 5;
182
                                if ($one_weid_available) $cols++;
2 daniel-mar 183
                                $output .= '<tr><td colspan="'.$cols.'">No items available</td></tr>';
184
                        }
185
                }
186
 
187
                $output .= '</table>';
188
                $output .= '</div></div>';
189
 
190
                if ($items_hidden == 1) {
107 daniel-mar 191
                        $output .= '<p>'.$items_hidden.' item is hidden. Please <a '.oidplus_link('oidplus:login').'>log in</a> to see it.</p>';
2 daniel-mar 192
                } else if ($items_hidden > 1) {
107 daniel-mar 193
                        $output .= '<p>'.$items_hidden.' items are hidden. Please <a '.oidplus_link('oidplus:login').'>log in</a> to see them.</p>';
2 daniel-mar 194
                }
195
 
196
                return $output;
197
        }
198
 
136 daniel-mar 199
        // 'quickbars' added 11 July 2019: Disabled because of two problems:
200
        //                                 1. When you load TinyMCE via AJAX using the left menu, the quickbar is immediately shown, even if TinyMCE does not have the focus
201
        //                                 2. When you load a page without TinyMCE using the left menu, the quickbar is still visible, although there is no edit
202
        public static $exclude_tinymce_plugins = array('fullpage', 'bbcode', 'quickbars');
2 daniel-mar 203
 
204
        protected static function showMCE($name, $content) {
205
                $mce_plugins = array();
4 daniel-mar 206
                foreach (glob(__DIR__ . '/../../3p/tinymce/plugins/*') as $m) { // */
2 daniel-mar 207
                        $mce_plugins[] = basename($m);
208
                }
209
 
210
                foreach (self::$exclude_tinymce_plugins as $exclude) {
211
                        $index = array_search($exclude, $mce_plugins);
212
                        if ($index !== false) unset($mce_plugins[$index]);
213
                }
214
 
4 daniel-mar 215
                $out = '<script>
216
                                tinymce.remove("#'.$name.'");
2 daniel-mar 217
                                tinymce.init({
4 daniel-mar 218
                                        selector: "#'.$name.'",
219
                                        height: 200,
220
                                        statusbar: false,
221
//                                      menubar:false,
222
//                                      toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | fontsizeselect",
223
                                        toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
123 daniel-mar 224
                                        plugins: "'.implode(' ', $mce_plugins).'",
225
                                        mobile: {
226
                                                theme: "mobile",
227
                                                toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
228
                                                plugins: "'.implode(' ', $mce_plugins).'"
229
                                        }
230
 
2 daniel-mar 231
                                });
4 daniel-mar 232
                        </script>';
2 daniel-mar 233
 
89 daniel-mar 234
                $content = htmlentities($content); // For some reason, if we want to display the text "<xyz>" in TinyMCE, we need to double-encode things! &lt; will not be accepted, we need &amp;lt; ... why?
235
 
4 daniel-mar 236
                $out .= '<textarea name="'.htmlentities($name).'" id="'.htmlentities($name).'">'.trim($content).'</textarea><br>';
2 daniel-mar 237
 
238
                return $out;
239
        }
240
 
241
        public static function generateContentPage($id) {
242
                $out = array();
243
 
244
                $handled = false;
245
                $out['title'] = '';
32 daniel-mar 246
                $out['icon'] = '';
2 daniel-mar 247
                $out['text'] = '';
248
 
249
                // === Plugins ===
250
 
61 daniel-mar 251
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
252
                        $plugin->gui($id, $out, $handled);
253
                }
2 daniel-mar 254
 
255
                // === Everything else (objects) ===
256
 
257
                if (!$handled) {
93 daniel-mar 258
                        try {
259
                                $obj = OIDplusObject::parse($id);
260
                        } catch (Exception $e) {
261
                                $out['title'] = 'Error';
262
                                $out['icon'] = 'img/error_big.png';
263
                                $out['text'] = htmlentities($e->getMessage());
264
                                return $out;
265
                        }
2 daniel-mar 266
 
267
                        if ((!is_null($obj)) && (!$obj->userHasReadRights())) {
268
                                $out['title'] = 'Access denied';
34 daniel-mar 269
                                $out['icon'] = 'img/error_big.png';
107 daniel-mar 270
                                $out['text'] = '<p>Please <a '.oidplus_link('oidplus:login').'>log in</a> to receive information about this object.</p>';
2 daniel-mar 271
                                return $out;
272
                        }
273
 
183 daniel-mar 274
                        // ---
2 daniel-mar 275
 
98 daniel-mar 276
                        $parent = null;
183 daniel-mar 277
                        $res = null;
278
                        $row = null;
2 daniel-mar 279
                        $matches_any_registered_type = false;
227 daniel-mar 280
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
2 daniel-mar 281
                                if ($obj = $ot::parse($id)) {
282
                                        $matches_any_registered_type = true;
183 daniel-mar 283
                                        if ($obj->isRoot()) {
34 daniel-mar 284
                                                $obj->getContentPage($out['title'], $out['text'], $out['icon']);
183 daniel-mar 285
                                                $parent = null; // $obj->getParent();
98 daniel-mar 286
                                                break;
183 daniel-mar 287
                                        } else {
288
                                                $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($obj->nodeId()));
289
                                                $row = OIDplus::db()->fetch_array($res);
290
                                                if (OIDplus::db()->num_rows($res) == 0) {
291
                                                        http_response_code(404);
292
                                                        $out['title'] = 'Object not found';
293
                                                        $out['icon'] = 'img/error_big.png';
294
                                                        $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
295
                                                        return $out;
296
                                                } else {
297
                                                        $obj->getContentPage($out['title'], $out['text'], $out['icon']);
227 daniel-mar 298
                                                        if (empty($out['title'])) $out['title'] = explode(':',$id,2)[1];
183 daniel-mar 299
                                                        $parent = $obj->getParent();
300
                                                        break;
301
                                                }
2 daniel-mar 302
                                        }
303
                                }
304
                        }
305
                        if (!$matches_any_registered_type) {
306
                                http_response_code(404);
307
                                $out['title'] = 'Object not found';
34 daniel-mar 308
                                $out['icon'] = 'img/error_big.png';
2 daniel-mar 309
                                $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
310
                                return $out;
311
                        }
312
 
183 daniel-mar 313
                        // ---
314
 
103 daniel-mar 315
                        if ($parent) {
316
                                if ($parent->isRoot()) {
98 daniel-mar 317
 
318
                                        $parent_link_text = $parent->objectTypeTitle();
129 daniel-mar 319
                                        $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 320
 
321
                                } else {
150 daniel-mar 322
                                        $res_ = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($parent->nodeId()));
98 daniel-mar 323
                                        $row_ = OIDplus::db()->fetch_array($res_);
324
 
325
                                        $parent_title = $row_['title'];
326
                                        if (empty($parent_title) && ($parent->ns() == 'oid')) {
150 daniel-mar 327
                                                $res_ = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = ?", array($parent->nodeId()));
98 daniel-mar 328
                                                $row_ = OIDplus::db()->fetch_array($res_);
329
                                                $parent_title = $row_['name']; // TODO: multiple ASN1 ids?
330
                                        }
331
 
332
                                        $parent_link_text = empty($parent_title) ? explode(':',$parent->nodeId())[1] : $parent_title.' ('.explode(':',$parent->nodeId())[1].')';
333
 
129 daniel-mar 334
                                        $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 335
                                }
103 daniel-mar 336
                        } else {
337
                                $parent_link_text = 'Go back to front page';
127 daniel-mar 338
                                $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 339
                        }
340
 
183 daniel-mar 341
                        // ---
342
 
343
                        if (!is_null($row) && isset($row['description'])) {
344
                                if (empty($row['description'])) {
345
                                        if (empty($row['title'])) {
346
                                                $desc = '<p><i>No description for this object available</i></p>';
347
                                        } else {
348
                                                $desc = $row['title'];
349
                                        }
350
                                } else {
351
                                        $desc = OIDplusGui::objDescription($row['description']);
352
                                }
353
 
354
                                if ($obj->userHasWriteRights()) {
355
                                        $rand = ++self::$crudCounter;
356
                                        $desc = '<noscript><p><b>You need to enable JavaScript to edit title or description of this object.</b></p>'.$desc.'</noscript>';
357
                                        $desc .= '<div class="container box" style="display:none" id="descbox_'.$rand.'">';
358
                                        $desc .= 'Title: <input type="text" name="title" id="titleedit" value="'.htmlentities($row['title']).'"><br><br>Description:<br>';
359
                                        $desc .= self::showMCE('description', $row['description']);
360
                                        $desc .= '<button type="button" name="update_desc" id="update_desc" class="btn btn-success btn-xs update" onclick="updateDesc()">Update description</button>';
361
                                        $desc .= '</div>';
362
                                        $desc .= '<script>document.getElementById("descbox_'.$rand.'").style.display = "block";</script>';
363
                                }
364
                        } else {
365
                                $desc = '';
366
                        }
367
 
368
                        // ---
369
 
2 daniel-mar 370
                        if (strpos($out['text'], '%%DESC%%') !== false)
371
                                $out['text'] = str_replace('%%DESC%%',    $desc,                              $out['text']);
372
                        if (strpos($out['text'], '%%CRUD%%') !== false)
373
                                $out['text'] = str_replace('%%CRUD%%',    self::showCrud($id),                $out['text']);
374
                        if (strpos($out['text'], '%%RA_INFO%%') !== false)
104 daniel-mar 375
                                $out['text'] = str_replace('%%RA_INFO%%', OIDplusPagePublicRaInfo::showRaInfo($row['ra_email']), $out['text']);
101 daniel-mar 376
 
193 daniel-mar 377
                        $alt_ids = $obj->getAltIds();
378
                        if (count($alt_ids) > 0) {
379
                                $out['text'] .= "<h2>Alternative Identifiers</h2>";
380
                                foreach ($alt_ids as list($ns, $aid, $aiddesc)) {
381
                                        $out['text'] .= "$aiddesc <code>$ns:$aid</code><br>";
382
                                }
383
                        }
384
 
101 daniel-mar 385
                        foreach (OIDplus::getPagePlugins('public') as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
386
                        foreach (OIDplus::getPagePlugins('ra')     as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
387
                        foreach (OIDplus::getPagePlugins('admin')  as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
103 daniel-mar 388
                } else {
120 daniel-mar 389
                        // Other pages (search, whois, etc.)
390
                        /*
391
                        if ($id != 'oidplus:system') {
103 daniel-mar 392
                                $parent_link_text = 'Go back to front page';
127 daniel-mar 393
                                $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 394
                        }
120 daniel-mar 395
                        */
2 daniel-mar 396
                }
397
 
398
                return $out;
399
        }
400
}