Subversion Repositories oidplus

Rev

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