Subversion Repositories oidplus

Rev

Rev 123 | Rev 129 | 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
 
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 = '".OIDplus::db()->real_escape_string($parent)."' order by ".OIDplus::db()->natOrder('id'));
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();
69
                        $res2 = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = '".OIDplus::db()->real_escape_string($row->id)."' order by lfd");
70
                        while ($row2 = OIDplus::db()->fetch_array($res2)) {
71
                                $asn1ids[] = $row2['name'];
72
                        }
73
 
74
                        $iris = array();
75
                        $res2 = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = '".OIDplus::db()->real_escape_string($row->id)."' order by lfd");
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
 
111
                $result = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($parent)."'");
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
 
150
        public static $exclude_tinymce_plugins = array('fullpage', 'bbcode');
151
 
152
        protected static function showMCE($name, $content) {
153
                $mce_plugins = array();
4 daniel-mar 154
                foreach (glob(__DIR__ . '/../../3p/tinymce/plugins/*') as $m) { // */
2 daniel-mar 155
                        $mce_plugins[] = basename($m);
156
                }
157
 
158
                foreach (self::$exclude_tinymce_plugins as $exclude) {
159
                        $index = array_search($exclude, $mce_plugins);
160
                        if ($index !== false) unset($mce_plugins[$index]);
161
                }
162
 
4 daniel-mar 163
                $out = '<script>
164
                                tinymce.remove("#'.$name.'");
2 daniel-mar 165
                                tinymce.init({
4 daniel-mar 166
                                        selector: "#'.$name.'",
167
                                        height: 200,
168
                                        statusbar: false,
169
//                                      menubar:false,
170
//                                      toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | fontsizeselect",
171
                                        toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
123 daniel-mar 172
                                        plugins: "'.implode(' ', $mce_plugins).'",
173
                                        mobile: {
174
                                                theme: "mobile",
175
                                                toolbar: "undo redo | styleselect | bold italic underline forecolor | bullist numlist | outdent indent | table | fontsizeselect",
176
                                                plugins: "'.implode(' ', $mce_plugins).'"
177
                                        }
178
 
2 daniel-mar 179
                                });
4 daniel-mar 180
                        </script>';
2 daniel-mar 181
 
89 daniel-mar 182
                $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?
183
 
4 daniel-mar 184
                $out .= '<textarea name="'.htmlentities($name).'" id="'.htmlentities($name).'">'.trim($content).'</textarea><br>';
2 daniel-mar 185
 
186
                return $out;
187
        }
188
 
189
        public static function generateContentPage($id) {
190
                $out = array();
191
 
192
                $handled = false;
193
                $out['title'] = '';
32 daniel-mar 194
                $out['icon'] = '';
2 daniel-mar 195
                $out['text'] = '';
196
 
197
                // === Plugins ===
198
 
61 daniel-mar 199
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
200
                        $plugin->gui($id, $out, $handled);
201
                }
2 daniel-mar 202
 
203
                // === Everything else (objects) ===
204
 
205
                if (!$handled) {
93 daniel-mar 206
                        try {
207
                                $obj = OIDplusObject::parse($id);
208
                        } catch (Exception $e) {
209
                                $out['title'] = 'Error';
210
                                $out['icon'] = 'img/error_big.png';
211
                                $out['text'] = htmlentities($e->getMessage());
212
                                return $out;
213
                        }
2 daniel-mar 214
 
215
                        if ((!is_null($obj)) && (!$obj->userHasReadRights())) {
216
                                $out['title'] = 'Access denied';
34 daniel-mar 217
                                $out['icon'] = 'img/error_big.png';
107 daniel-mar 218
                                $out['text'] = '<p>Please <a '.oidplus_link('oidplus:login').'>log in</a> to receive information about this object.</p>';
2 daniel-mar 219
                                return $out;
220
                        }
221
 
222
                        $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($id)."'");
223
                        $row = OIDplus::db()->fetch_array($res);
224
 
225
                        if (empty($row['title'])) {
226
                                $out['title'] = is_null($obj) ? $id : $obj->defaultTitle();
227
                        } else {
228
                                $out['title'] = $row['title'];
229
                        }
34 daniel-mar 230
                        $out['icon'] = 'img/object_big.png';
2 daniel-mar 231
 
232
                        if (isset($row['description'])) {
92 daniel-mar 233
                                if (empty($row['description'])) {
234
                                        if (empty($row['title'])) {
235
                                                $desc = '<p><i>No description for this object available</i></p>';
236
                                        } else {
237
                                                $desc = $row['title'];
238
                                        }
239
                                } else {
240
                                        $desc = OIDplusGui::objDescription($row['description']);
241
                                }
242
 
2 daniel-mar 243
                                if ($obj->userHasWriteRights()) {
244
                                        $rand = ++self::$crudCounter;
245
                                        $desc = '<noscript><p><b>You need to enable JavaScript to edit title or description of this object.</b></p>'.$desc.'</noscript>';
246
                                        $desc .= '<div class="container box" style="display:none" id="descbox_'.$rand.'">';
247
                                        $desc .= 'Title: <input type="text" name="title" id="titleedit" value="'.htmlentities($row['title']).'"><br><br>Description:<br>';
248
                                        $desc .= self::showMCE('description', $row['description']);
104 daniel-mar 249
                                        $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 250
                                        $desc .= '</div>';
251
                                        $desc .= '<script>document.getElementById("descbox_'.$rand.'").style.display = "block";</script>';
252
                                }
253
                        } else {
254
                                $desc = '';
255
                        }
256
 
98 daniel-mar 257
                        $parent = null;
258
 
2 daniel-mar 259
                        $matches_any_registered_type = false;
61 daniel-mar 260
                        foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
2 daniel-mar 261
                                if ($obj = $ot::parse($id)) {
262
                                        $matches_any_registered_type = true;
263
                                        if ((OIDplus::db()->num_rows($res) == 0) && !$obj->isRoot()){
264
                                                http_response_code(404);
265
                                                $out['title'] = 'Object not found';
34 daniel-mar 266
                                                $out['icon'] = 'img/error_big.png';
2 daniel-mar 267
                                                $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
268
                                                return $out;
269
                                        } else {
34 daniel-mar 270
                                                $obj->getContentPage($out['title'], $out['text'], $out['icon']);
98 daniel-mar 271
                                                $parent = $obj->getParent();
272
                                                break;
2 daniel-mar 273
                                        }
274
                                }
275
                        }
276
                        if (!$matches_any_registered_type) {
277
                                http_response_code(404);
278
                                $out['title'] = 'Object not found';
34 daniel-mar 279
                                $out['icon'] = 'img/error_big.png';
2 daniel-mar 280
                                $out['text'] = 'The object <code>'.htmlentities($id).'</code> was not found in this database.';
281
                                return $out;
282
                        }
283
 
103 daniel-mar 284
                        if ($parent) {
285
                                if ($parent->isRoot()) {
98 daniel-mar 286
 
287
                                        $parent_link_text = $parent->objectTypeTitle();
127 daniel-mar 288
                                        $out['text'] = '<p><img src="img/arrow_back.png" width="16"> Parent node: <a '.oidplus_link($parent->root()).'>'.htmlentities($parent_link_text).'</a></p>' . $out['text'];
98 daniel-mar 289
 
290
                                } else {
291
                                        $res_ = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($parent->nodeId())."'");
292
                                        $row_ = OIDplus::db()->fetch_array($res_);
293
 
294
                                        $parent_title = $row_['title'];
295
                                        if (empty($parent_title) && ($parent->ns() == 'oid')) {
296
                                                $res_ = OIDplus::db()->query("select name from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = '".OIDplus::db()->real_escape_string($parent->nodeId())."'");
297
                                                $row_ = OIDplus::db()->fetch_array($res_);
298
                                                $parent_title = $row_['name']; // TODO: multiple ASN1 ids?
299
                                        }
300
 
301
                                        $parent_link_text = empty($parent_title) ? explode(':',$parent->nodeId())[1] : $parent_title.' ('.explode(':',$parent->nodeId())[1].')';
302
 
127 daniel-mar 303
                                        $out['text'] = '<p><img src="img/arrow_back.png" width="16"> Parent node: <a '.oidplus_link($parent->nodeId()).'>'.htmlentities($parent_link_text).'</a></p>' . $out['text'];
98 daniel-mar 304
                                }
103 daniel-mar 305
                        } else {
306
                                $parent_link_text = 'Go back to front page';
127 daniel-mar 307
                                $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 308
                        }
309
 
2 daniel-mar 310
                        if (strpos($out['text'], '%%DESC%%') !== false)
311
                                $out['text'] = str_replace('%%DESC%%',    $desc,                              $out['text']);
312
                        if (strpos($out['text'], '%%CRUD%%') !== false)
313
                                $out['text'] = str_replace('%%CRUD%%',    self::showCrud($id),                $out['text']);
314
                        if (strpos($out['text'], '%%RA_INFO%%') !== false)
104 daniel-mar 315
                                $out['text'] = str_replace('%%RA_INFO%%', OIDplusPagePublicRaInfo::showRaInfo($row['ra_email']), $out['text']);
101 daniel-mar 316
 
317
                        foreach (OIDplus::getPagePlugins('public') as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
318
                        foreach (OIDplus::getPagePlugins('ra')     as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
319
                        foreach (OIDplus::getPagePlugins('admin')  as $plugin) $plugin->modifyContent($id, $out['title'], $out['icon'], $out['text']);
103 daniel-mar 320
                } else {
120 daniel-mar 321
                        // Other pages (search, whois, etc.)
322
                        /*
323
                        if ($id != 'oidplus:system') {
103 daniel-mar 324
                                $parent_link_text = 'Go back to front page';
127 daniel-mar 325
                                $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 326
                        }
120 daniel-mar 327
                        */
2 daniel-mar 328
                }
329
 
330
                return $out;
331
        }
332
}