Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
635 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
635 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
635 daniel-mar 26
class OIDplusPagePublicAttachments extends OIDplusPagePluginPublic {
27
 
28
        const DIR_UNLOCK_FILE = 'oidplus_upload.dir';
29
 
30
        private static function checkUploadDir($dir) {
31
                if (!is_dir($dir)) {
32
                        throw new OIDplusException(_L('The attachment directory "%1" is not existing.', $dir));
33
                }
34
 
35
                $realdir = realpath($dir);
36
                if ($realdir === false) {
37
                        throw new OIDplusException(_L('The attachment directory "%1" cannot be resolved (realpath).', $dir));
38
                }
39
 
40
                $unlock_file = $realdir . DIRECTORY_SEPARATOR . self::DIR_UNLOCK_FILE;
41
                if (!file_exists($unlock_file)) {
42
                        throw new OIDplusException(_L('Unlock file "%1" is not existing in attachment directory "%2".', self::DIR_UNLOCK_FILE, $dir));
43
                }
44
 
45
                if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
46
                        // Linux check 1: Check for critical directories
47
                        if (self::isCriticalLinuxDirectory($realdir)) {
48
                                throw new OIDplusException(_L('The attachment directory must not be inside a critical system directory!'));
49
                        }
50
 
51
                        // Linux check 2: Check file owner
52
                        $file_owner_a = fileowner(OIDplus::localpath().'index.php');
53
                        if ($file_owner_a === false) {
54
                                $file_owner_a = -1;
55
                                $file_owner_a_name = '???';
56
                        } else {
720 daniel-mar 57
                                $tmp = function_exists('posix_getpwuid') ? posix_getpwuid($file_owner_a) : false;
635 daniel-mar 58
                                $file_owner_a_name = $tmp !== false ? $tmp['name'] : 'UID '.$file_owner_a;
59
                        }
60
 
61
                        $file_owner_b = fileowner($unlock_file);
62
                        if ($file_owner_b === false) {
63
                                $file_owner_b = -1;
64
                                $file_owner_b_name = '???';
65
                        } else {
720 daniel-mar 66
                                $tmp = function_exists('posix_getpwuid') ? posix_getpwuid($file_owner_b) : false;
635 daniel-mar 67
                                $file_owner_b_name = $tmp !== false ? $tmp['name'] : 'UID '.$file_owner_b;
68
                        }
69
 
70
                        if ($file_owner_a != $file_owner_b) {
71
                                throw new OIDplusException(_L('Owner of unlock file "%1" is wrong. It is "%2", but it should be "%3".', $unlock_file, $file_owner_b_name, $file_owner_a_name));
72
                        }
73
                } else {
74
                        // Windows check 1: Check for critical directories
75
                        if (self::isCriticalWindowsDirectory($realdir)) {
76
                                throw new OIDplusException(_L('The attachment directory must not be inside a critical system directory!'));
77
                        }
78
 
79
                        // Note: We will not query the file owner in Windows systems.
80
                        // It would be possible, however, on Windows systems, the file
81
                        // ownership is rather hidden to the user and the user needs
82
                        // to go into several menus and windows in order to see/change
83
                        // the owner. We don't want to over-complicate it to the Windows admin.
84
                }
85
        }
86
 
87
        private static function isCriticalWindowsDirectory($dir) {
88
                $dir .= '\\';
89
                $windir = isset($_SERVER['SystemRoot']) ? $_SERVER['SystemRoot'].'\\' : 'C:\\Windows\\';
90
                if (stripos($dir,$windir) === 0) return true;
91
                return false;
92
        }
93
 
94
        private static function isCriticalLinuxDirectory($dir) {
95
                if ($dir == '/') return true;
96
                $dir .= '/';
97
                if (strpos($dir,'/bin/') === 0) return true;
98
                if (strpos($dir,'/boot/') === 0) return true;
99
                if (strpos($dir,'/dev/') === 0) return true;
100
                if (strpos($dir,'/etc/') === 0) return true;
101
                if (strpos($dir,'/lib') === 0) return true;
102
                if (strpos($dir,'/opt/') === 0) return true;
103
                if (strpos($dir,'/proc/') === 0) return true;
104
                if (strpos($dir,'/root/') === 0) return true;
105
                if (strpos($dir,'/run/') === 0) return true;
106
                if (strpos($dir,'/sbin/') === 0) return true;
107
                if (strpos($dir,'/sys/') === 0) return true;
108
                if (strpos($dir,'/tmp/') === 0) return true;
109
                if (strpos($dir,'/usr/bin/') === 0) return true;
110
                if (strpos($dir,'/usr/include/') === 0) return true;
111
                if (strpos($dir,'/usr/lib') === 0) return true;
112
                if (strpos($dir,'/usr/sbin/') === 0) return true;
113
                if (strpos($dir,'/usr/src/') === 0) return true;
114
                if (strpos($dir,'/var/cache/') === 0) return true;
115
                if (strpos($dir,'/var/lib') === 0) return true;
116
                if (strpos($dir,'/var/lock/') === 0) return true;
117
                if (strpos($dir,'/var/log/') === 0) return true;
118
                if (strpos($dir,'/var/mail/') === 0) return true;
119
                if (strpos($dir,'/var/opt/') === 0) return true;
120
                if (strpos($dir,'/var/run/') === 0) return true;
121
                if (strpos($dir,'/var/spool/') === 0) return true;
122
                if (strpos($dir,'/var/tmp/') === 0) return true;
123
                return false;
124
        }
125
 
126
        public static function getUploadDir($id=null) {
127
                // Get base path
128
                $cfg = OIDplus::config()->getValue('attachment_upload_dir', '');
129
                $cfg = trim($cfg);
130
                if ($cfg === '') {
131
                        $basepath = OIDplus::localpath() . 'userdata' . DIRECTORY_SEPARATOR . 'attachments';
132
                } else {
133
                        $basepath = $cfg;
134
                }
135
 
136
                try {
137
                        self::checkUploadDir($basepath);
1050 daniel-mar 138
                } catch (\Exception $e) {
635 daniel-mar 139
                        $error = _L('This functionality is not available due to a misconfiguration');
140
                        if (OIDplus::authUtils()->isAdminLoggedIn()) {
141
                                $error .= ': '.$e->getMessage();
142
                        } else {
143
                                $error .= '. '._L('Please notify the system administrator. After they log-in, they can see the reason at this place.');
144
                        }
145
                        throw new OIDplusException($error);
146
                }
147
 
148
                // Get object-specific path
149
                if (!is_null($id)) {
150
                        $obj = OIDplusObject::parse($id);
151
                        if ($obj === null) throw new OIDplusException(_L('Invalid object "%1"',$id));
152
 
153
                        $path_v1 = $basepath . DIRECTORY_SEPARATOR . $obj->getLegacyDirectoryName();
154
                        $path_v1_bug = $basepath . $obj->getLegacyDirectoryName();
155
                        $path_v2 = $basepath . DIRECTORY_SEPARATOR . $obj->getDirectoryName();
156
 
157
                        if (is_dir($path_v1)) return $path_v1; // backwards compatibility
158
                        if (is_dir($path_v1_bug)) return $path_v1_bug; // backwards compatibility
159
                        return $path_v2;
160
                } else {
161
                        return $basepath;
162
                }
163
        }
164
 
165
        private function raMayDelete() {
166
                return OIDplus::config()->getValue('attachments_allow_ra_delete', 0);
167
        }
168
 
169
        private function raMayUpload() {
170
                return OIDplus::config()->getValue('attachments_allow_ra_upload', 0);
171
        }
172
 
173
        public function action($actionID, $params) {
174
 
175
                if ($actionID == 'deleteAttachment') {
176
                        _CheckParamExists($params, 'id');
177
                        $id = $params['id'];
178
                        $obj = OIDplusObject::parse($id);
179
                        if ($obj === null) throw new OIDplusException(_L('Invalid object "%1"',$id));
180
                        if (!$obj->userHasWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA of "%1" to upload an attachment.',$id));
181
 
182
                        if (!OIDplus::authUtils()->isAdminLoggedIn() && !$this->raMayDelete()) {
183
                                throw new OIDplusException(_L('The administrator has disabled deleting attachments by RAs.'));
184
                        }
185
 
186
                        _CheckParamExists($params, 'filename');
187
                        $req_filename = $params['filename'];
188
                        if (strpos($req_filename, '/') !== false) throw new OIDplusException(_L('Illegal file name'));
189
                        if (strpos($req_filename, '\\') !== false) throw new OIDplusException(_L('Illegal file name'));
190
                        if (strpos($req_filename, '..') !== false) throw new OIDplusException(_L('Illegal file name'));
191
                        if (strpos($req_filename, chr(0)) !== false) throw new OIDplusException(_L('Illegal file name'));
192
 
193
                        $uploaddir = self::getUploadDir($id);
194
                        $uploadfile = $uploaddir . DIRECTORY_SEPARATOR . basename($req_filename);
195
 
196
                        if (!file_exists($uploadfile)) throw new OIDplusException(_L('File does not exist'));
197
                        @unlink($uploadfile);
198
                        if (file_exists($uploadfile)) {
199
                                OIDplus::logger()->log("[ERR]OID($id)+[ERR]A!", "Attachment file '".basename($uploadfile)."' could not be deleted from object '$id' (problem with permissions?)");
200
                                $msg = _L('Attachment file "%1" could not be deleted from object "%2" (problem with permissions?)',basename($uploadfile),$id);
201
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
202
                                        throw new OIDplusException($msg);
203
                                } else {
204
                                        throw new OIDplusException($msg.'. '._L('Please contact the system administrator.'));
205
                                }
206
                        } else {
207
                                // If it was the last file, delete the empty directory
719 daniel-mar 208
                                $ary = @glob($uploaddir . DIRECTORY_SEPARATOR . '*');
209
                                if (is_array($ary) && (count($ary) == 0)) @rmdir($uploaddir);
635 daniel-mar 210
                        }
211
 
212
                        OIDplus::logger()->log("[OK]OID($id)+[?INFO/!OK]OIDRA($id)?/[?INFO/!OK]A?", "Deleted attachment '".basename($uploadfile)."' from object '$id'");
213
 
214
                        return array("status" => 0);
215
 
216
                } else if ($actionID == 'uploadAttachment') {
217
                        _CheckParamExists($params, 'id');
218
                        $id = $params['id'];
219
                        $obj = OIDplusObject::parse($id);
220
                        if ($obj === null) throw new OIDplusException(_L('Invalid object "%1"',$id));
221
                        if (!$obj->userHasWriteRights()) throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA of "%1" to upload an attachment.',$id));
222
 
223
                        if (!OIDplus::authUtils()->isAdminLoggedIn() && !$this->raMayUpload()) {
224
                                throw new OIDplusException(_L('The administrator has disabled uploading attachments by RAs.'));
225
                        }
226
 
227
                        if (!isset($_FILES['userfile'])) {
228
                                throw new OIDplusException(_L('Please choose a file.'));
229
                        }
230
 
231
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
836 daniel-mar 232
                                $fname = basename($_FILES['userfile']['name']);
233
 
834 daniel-mar 234
                                // 1. If something is on the blacklist, we always block it, even if it is on the whitelist, too
635 daniel-mar 235
                                $banned = explode(',', OIDplus::config()->getValue('attachments_block_extensions', ''));
236
                                foreach ($banned as $ext) {
237
                                        $ext = trim($ext);
238
                                        if ($ext == '') continue;
836 daniel-mar 239
                                        if (strtolower(substr($fname, -strlen($ext)-1)) == strtolower('.'.$ext)) {
635 daniel-mar 240
                                                throw new OIDplusException(_L('The file extension "%1" is banned by the administrator (it can be uploaded by the administrator though)',$ext));
241
                                        }
242
                                }
834 daniel-mar 243
 
244
                                // 2. Something on the whitelist is always OK
245
                                $allowed = explode(',', OIDplus::config()->getValue('attachments_allow_extensions', ''));
246
                                $is_whitelisted = false;
247
                                foreach ($allowed as $ext) {
248
                                        $ext = trim($ext);
249
                                        if ($ext == '') continue;
836 daniel-mar 250
                                        if (strtolower(substr($fname, -strlen($ext)-1)) == strtolower('.'.$ext)) {
834 daniel-mar 251
                                                $is_whitelisted = true;
252
                                                break;
253
                                        }
254
                                }
255
 
256
                                // 3. For everything that is neither whitelisted, nor blacklisted, the admin can decide if these grey zone is allowed or blocked
257
                                if (!$is_whitelisted) {
258
                                        if (!OIDplus::config()->getValue('attachments_allow_grey_extensions', '1')) {
836 daniel-mar 259
                                                $tmp = explode('.', $fname);
260
                                                $ext = array_pop($tmp);
834 daniel-mar 261
                                                throw new OIDplusException(_L('The file extension "%1" is not on the whitelist (it can be uploaded by the administrator though)',$ext));
262
                                        }
263
                                }
635 daniel-mar 264
                        }
265
 
266
                        $req_filename = $_FILES['userfile']['name'];
267
                        if (strpos($req_filename, '/') !== false) throw new OIDplusException(_L('Illegal file name'));
268
                        if (strpos($req_filename, '\\') !== false) throw new OIDplusException(_L('Illegal file name'));
269
                        if (strpos($req_filename, '..') !== false) throw new OIDplusException(_L('Illegal file name'));
270
                        if (strpos($req_filename, chr(0)) !== false) throw new OIDplusException(_L('Illegal file name'));
271
 
272
                        $uploaddir = self::getUploadDir($id);
273
                        $uploadfile = $uploaddir . DIRECTORY_SEPARATOR . basename($req_filename);
274
 
275
                        if (!is_dir($uploaddir)) {
276
                                @mkdir($uploaddir, 0777, true);
277
                                if (!is_dir($uploaddir)) {
278
                                        OIDplus::logger()->log("[ERR]OID($id)+[ERR]A!", "Upload attachment '".basename($uploadfile)."' to object '$id' failed: Cannot create directory '".basename($uploaddir)."' (problem with permissions?)");
279
                                        $msg = _L('Upload attachment "%1" to object "%2" failed',basename($uploadfile),$id).': '._L('Cannot create directory "%1" (problem with permissions?)',basename($uploaddir));
280
                                        if (OIDplus::authUtils()->isAdminLoggedIn()) {
281
                                                throw new OIDplusException($msg);
282
                                        } else {
283
                                                throw new OIDplusException($msg.'. '._L('Please contact the system administrator.'));
284
                                        }
285
                                }
286
                        }
287
 
288
                        if (!@move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
289
                                OIDplus::logger()->log("[ERR]OID($id)+[ERR]A!", "Upload attachment '".basename($uploadfile)."' to object '$id' failed: Cannot move uploaded file into directory (problem with permissions?)");
290
                                $msg = _L('Upload attachment "%1" to object "%2" failed',basename($uploadfile),$id).': '._L('Cannot move uploaded file into directory (problem with permissions?)');
291
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
292
                                        throw new OIDplusException($msg);
293
                                } else {
294
                                        throw new OIDplusException($msg.'. '._L('Please contact the system administrator.'));
295
                                }
296
                        }
297
 
298
                        OIDplus::logger()->log("[OK]OID($id)+[?INFO/!OK]OIDRA($id)?/[?INFO/!OK]A?", "Uploaded attachment '".basename($uploadfile)."' to object '$id'");
299
 
300
                        return array("status" => 0);
301
                } else {
302
                        throw new OIDplusException(_L('Unknown action ID'));
303
                }
304
        }
305
 
306
        public function init($html=true) {
307
                OIDplus::config()->prepareConfigKey('attachments_block_extensions', 'Block file name extensions being used in file attachments (comma separated)', 'exe,scr,pif,bat,com,vbs,cmd', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
834 daniel-mar 308
                        // TODO: check if a blacklist entry is also on the whitelist (which is not allowed)
635 daniel-mar 309
                });
834 daniel-mar 310
                OIDplus::config()->prepareConfigKey('attachments_allow_extensions', 'Allow (whitelist) file name extensions being used in file attachments (comma separated)', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
311
                        // TODO: check if a whitelist entry is also on the blacklist (which is not allowed)
312
                });
313
                OIDplus::config()->prepareConfigKey('attachments_allow_grey_extensions', 'Should file-extensions which are neither be on the whitelist, nor be at the blacklist, be allowed? (1=Yes, 0=No)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
314
                        if (!is_numeric($value) || ($value < 0) || ($value > 1)) {
315
                                throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
316
                        }
317
                });
635 daniel-mar 318
                OIDplus::config()->prepareConfigKey('attachments_allow_ra_delete', 'Allow that RAs delete file attachments? (0=no, 1=yes)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
319
                        if (!is_numeric($value) || ($value < 0) || ($value > 1)) {
320
                                throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
321
                        }
322
                });
323
                OIDplus::config()->prepareConfigKey('attachments_allow_ra_upload', 'Allow that RAs upload file attachments? (0=no, 1=yes)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
324
                        if (!is_numeric($value) || ($value < 0) || ($value > 1)) {
325
                                throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
326
                        }
327
                });
328
 
329
                $info_txt  = 'Alternative directory for attachments. It must contain a file named "';
330
                $info_txt .= self::DIR_UNLOCK_FILE;
331
                $info_txt .= '"';
332
                if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
333
                        $info_txt .= ' with the same owner as index.php';
334
                }
335
                $info_txt .= '. If this setting is empty, then the userdata directory is used.';
336
                OIDplus::config()->prepareConfigKey('attachment_upload_dir', $info_txt, '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
337
                        if (trim($value) !== '') {
338
                                self::checkUploadDir($value);
339
                        }
340
                });
341
        }
342
 
343
        public function gui($id, &$out, &$handled) {
344
                // Nothing
345
        }
346
 
347
        public function publicSitemap(&$out) {
348
                // Nothing
349
        }
350
 
351
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
352
                return false;
353
        }
354
 
355
        private static function convert_filesize($bytes, $decimals = 2){
356
                $size = array(_L('Bytes'),_L('KiB'),_L('MiB'),_L('GiB'),_L('TiB'),_L('PiB'),_L('EiB'),_L('ZiB'),_L('YiB'));
357
                $factor = floor((strlen($bytes) - 1) / 3);
358
                return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
359
        }
360
 
361
        public function implementsFeature($id) {
362
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.2') return true; // modifyContent
363
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.3') return true; // beforeObject*, afterObject*
364
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.4') return true; // whois*Attributes
365
                return false;
366
        }
367
 
368
        public function modifyContent($id, &$title, &$icon, &$text) {
369
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.2
370
 
371
                $output = '';
372
                $doshow = false;
373
 
374
                try {
375
                        $upload_dir = self::getUploadDir($id);
719 daniel-mar 376
                        $files = @glob($upload_dir . DIRECTORY_SEPARATOR . '*');
635 daniel-mar 377
                        $found_files = false;
378
 
379
                        $obj = OIDplusObject::parse($id);
380
                        if ($obj === null) throw new OIDplusException(_L('Invalid object "%1"',$id));
381
                        $can_upload = OIDplus::authUtils()->isAdminLoggedIn() || ($this->raMayUpload() && $obj->userHasWriteRights());
382
                        $can_delete = OIDplus::authUtils()->isAdminLoggedIn() || ($this->raMayDelete() && $obj->userHasWriteRights());
383
 
384
                        if (OIDplus::authUtils()->isAdminLoggedIn()) {
385
                                $output .= '<p>'._L('Admin info: The directory is %1','<b>'.htmlentities($upload_dir).'</b>').'</p>';
386
                                $doshow = true;
387
                        }
388
 
389
                        $output .= '<div id="fileattachments_table" class="table-responsive">';
390
                        $output .= '<table class="table table-bordered table-striped">';
391
                        $output .= '<tr>';
392
                        $output .= '<th>'._L('Filename').'</th>';
393
                        $output .= '<th>'._L('Size').'</th>';
394
                        $output .= '<th>'._L('File type').'</th>';
395
                        $output .= '<th>'._L('Download').'</th>';
396
                        if ($can_delete) $output .= '<th>'._L('Delete').'</th>';
397
                        $output .= '</tr>';
719 daniel-mar 398
                        if ($files) foreach ($files as $file) {
635 daniel-mar 399
                                if (is_dir($file)) continue;
400
 
401
                                $output .= '<tr>';
402
                                $output .= '<td>'.htmlentities(basename($file)).'</td>';
403
                                $output .= '<td>'.htmlentities(self::convert_filesize(filesize($file), 0)).'</td>';
404
                                $lookup_files = array(
405
                                        OIDplus::localpath().'userdata/attachments/filetypes$'.OIDplus::getCurrentLang().'.conf',
406
                                        OIDplus::localpath().'userdata/attachments/filetypes.conf',
407
                                        OIDplus::localpath().'vendor/danielmarschall/fileformats/filetypes$'.OIDplus::getCurrentLang().'.local', // not recommended
408
                                        OIDplus::localpath().'vendor/danielmarschall/fileformats/filetypes.local', // not recommended
409
                                        OIDplus::localpath().'vendor/danielmarschall/fileformats/filetypes$'.OIDplus::getCurrentLang().'.conf',
410
                                        OIDplus::localpath().'vendor/danielmarschall/fileformats/filetypes.conf'
411
                                );
1050 daniel-mar 412
                                $output .= '<td>'.htmlentities(\VtsFileTypeDetect::getDescription($file, $lookup_files)).'</td>';
635 daniel-mar 413
 
801 daniel-mar 414
                                $output .= '     <td><button type="button" name="download_'.md5($file).'" id="download_'.md5($file).'" class="btn btn-success btn-xs download" onclick="OIDplusPagePublicAttachments.downloadAttachment('.js_escape(OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE)).', current_node,'.js_escape(basename($file)).')">'._L('Download').'</button></td>';
635 daniel-mar 415
                                if ($can_delete) {
416
                                        $output .= '     <td><button type="button" name="delete_'.md5($file).'" id="delete_'.md5($file).'" class="btn btn-danger btn-xs delete" onclick="OIDplusPagePublicAttachments.deleteAttachment(current_node,'.js_escape(basename($file)).')">'._L('Delete').'</button></td>';
417
                                }
418
 
419
                                $output .= '</tr>';
420
                                $doshow = true;
421
                                $found_files = true;
422
                        }
423
 
424
                        if (!$found_files) $output .= '<tr><td colspan="'.($can_delete ? 5 : 4).'"><i>'._L('No attachments').'</i></td></tr>';
425
 
426
                        $output .= '</table></div>';
427
 
428
                        if ($can_upload) {
429
                                $output .= '<form action="javascript:void(0);" onsubmit="return OIDplusPagePublicAttachments.uploadAttachmentOnSubmit(this);" enctype="multipart/form-data" id="uploadAttachmentForm">';
430
                                $output .= '<input type="hidden" name="id" value="'.htmlentities($id).'">';
431
                                $output .= '<div>'._L('Add a file attachment').':<input type="file" name="userfile" value="" id="fileAttachment">';
432
                                $output .= '<br><input type="submit" value="'._L('Upload').'"></div>';
433
                                $output .= '</form>';
434
                                $doshow = true;
435
                        }
1050 daniel-mar 436
                } catch (\Exception $e) {
635 daniel-mar 437
                        $doshow = true;
438
                        $output = '<p>'.$e->getMessage().'</p>';
439
                }
440
 
441
                $output = '<h2>'._L('File attachments').'</h2>' .
442
                          '<div class="container box">' .
443
                          $output .
444
                          '</div>';
445
                if ($doshow) $text .= $output;
446
        }
447
 
448
        public function beforeObjectDelete($id) {} // Interface 1.3.6.1.4.1.37476.2.5.2.3.3
449
        public function afterObjectDelete($id) {
450
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.3
451
                // Delete the attachment folder including all files in it (note: Subfolders are not possible)
452
                $uploaddir = self::getUploadDir($id);
453
                if ($uploaddir != '') {
719 daniel-mar 454
                        $ary = @glob($uploaddir . DIRECTORY_SEPARATOR . '*');
455
                        if ($ary) foreach ($ary as $a) @unlink($a);
635 daniel-mar 456
                        @rmdir($uploaddir);
457
                        if (is_dir($uploaddir)) {
458
                                OIDplus::logger()->log("[WARN]OID($id)+[WARN]A!", "Attachment directory '$uploaddir' could not be deleted during the deletion of the OID");
459
                        }
460
                }
461
        }
462
        public function beforeObjectUpdateSuperior($id, &$params) {} // Interface 1.3.6.1.4.1.37476.2.5.2.3.3
463
        public function afterObjectUpdateSuperior($id, &$params) {} // Interface 1.3.6.1.4.1.37476.2.5.2.3.3
464
        public function beforeObjectUpdateSelf($id, &$params) {} // Interface 1.3.6.1.4.1.37476.2.5.2.3.3
465
        public function afterObjectUpdateSelf($id, &$params) {} // Interface 1.3.6.1.4.1.37476.2.5.2.3.3
466
        public function beforeObjectInsert($id, &$params) {} // Interface 1.3.6.1.4.1.37476.2.5.2.3.3
467
        public function afterObjectInsert($id, &$params) {} // Interface 1.3.6.1.4.1.37476.2.5.2.3.3
468
 
469
        public function tree_search($request) {
470
                return false;
471
        }
472
 
473
        public function whoisObjectAttributes($id, &$out) {
474
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.4
475
 
899 daniel-mar 476
                $xmlns = 'oidplus-attachment-plugin';
477
                $xmlschema = 'urn:oid:1.3.6.1.4.1.37476.2.5.2.4.1.95.1';
478
                $xmlschemauri = OIDplus::webpath(__DIR__.'/attachments.xsd',OIDplus::PATH_ABSOLUTE);
479
 
719 daniel-mar 480
                $files = @glob(self::getUploadDir($id) . DIRECTORY_SEPARATOR . '*');
481
                if ($files) foreach ($files as $file) {
899 daniel-mar 482
                        $url = OIDplus::webpath(__DIR__,OIDplus::PATH_ABSOLUTE).'download.php?id='.urlencode($id).'&filename='.urlencode(basename($file));
483
 
484
                        $out[] = array(
485
                                'xmlns' => $xmlns,
486
                                'xmlschema' => $xmlschema,
487
                                'xmlschemauri' => $xmlschemauri,
488
                                'name' => 'attachment-name',
489
                                'value' => basename($file)
490
                        );
491
 
492
                        $out[] = array(
493
                                'xmlns' => $xmlns,
494
                                'xmlschema' => $xmlschema,
495
                                'xmlschemauri' => $xmlschemauri,
496
                                'name' => 'attachment-url',
497
                                'value' => $url
498
                        );
635 daniel-mar 499
                }
500
 
501
        }
502
        public function whoisRaAttributes($email, &$out) {} // Interface 1.3.6.1.4.1.37476.2.5.2.3.4
503
}