1
|
|
|
<?php namespace XoopsModules\Extcal; |
|
|
|
|
2
|
|
|
/* |
3
|
|
|
* You may not change or alter any portion of this comment or credits |
4
|
|
|
* of supporting developers from this source code or any supporting source code |
5
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
* |
7
|
|
|
* This program is distributed in the hope that it will be useful, |
8
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
14
|
|
|
* @license {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later} |
15
|
|
|
* @package extcal |
16
|
|
|
* @since |
17
|
|
|
* @author XOOPS Development Team, |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.'); |
|
|
|
|
21
|
|
|
|
22
|
|
|
// // require_once __DIR__ . '/ExtcalPersistableObjectHandler.php'; |
23
|
|
|
require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class FileHandler. |
27
|
|
|
*/ |
28
|
|
|
class FileHandler extends ExtcalPersistableObjectHandler |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param $db |
32
|
|
|
*/ |
33
|
|
|
public function __construct(\XoopsDatabase $db) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($db, 'extcal_file', File::class, 'file_id'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $eventId |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function createFile($eventId) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$userId = $GLOBALS['xoopsUser'] ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
46
|
|
|
|
47
|
|
|
$allowedMimeType = []; |
48
|
|
|
$mimeType = include XOOPS_ROOT_PATH . '/include/mimetypes.inc.php'; |
49
|
|
|
foreach ($GLOBALS['xoopsModuleConfig']['allowed_file_extention'] as $fileExt) { |
50
|
|
|
$allowedMimeType[] = $mimeType[$fileExt]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$uploader = new \XoopsMediaUploader(XOOPS_ROOT_PATH . '/uploads/extcal', $allowedMimeType, 3145728); |
54
|
|
|
$uploader->setPrefix($userId . '-' . $eventId . '_'); |
55
|
|
|
if ($uploader->fetchMedia('event_file')) { |
56
|
|
|
if (!$uploader->upload()) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$data = [ |
64
|
|
|
'file_name' => $uploader->getSavedFileName(), |
65
|
|
|
'file_nicename' => $uploader->getMediaName(), |
66
|
|
|
'file_mimetype' => $uploader->getMediaType(), |
67
|
|
|
'file_size' => $_FILES['event_file']['size'], |
68
|
|
|
'file_date' => time(), |
69
|
|
|
'file_approved' => 1, |
70
|
|
|
'event_id' => $eventId, |
71
|
|
|
'uid' => $userId, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
$file = $this->create(); |
75
|
|
|
$file->setVars($data); |
76
|
|
|
|
77
|
|
|
return $this->insert($file); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $file |
82
|
|
|
*/ |
83
|
|
|
public function deleteFile(&$file) |
84
|
|
|
{ |
85
|
|
|
$this->_deleteFile($file); |
86
|
|
|
$this->deleteById($file->getVar('file_id')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $eventId |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getEventFiles($eventId) |
95
|
|
|
{ |
96
|
|
|
$criteria = new \CriteriaCompo(); |
97
|
|
|
$criteria->add( new \Criteria('file_approved', 1)); |
98
|
|
|
$criteria->add( new \Criteria('event_id', $eventId)); |
99
|
|
|
|
100
|
|
|
return $this->getObjects($criteria); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $eventId |
105
|
|
|
*/ |
106
|
|
|
public function updateEventFile($eventId) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$criteria = new \CriteriaCompo(); |
109
|
|
|
$criteria->add( new \Criteria('file_approved', 1)); |
110
|
|
|
$criteria->add( new \Criteria('event_id', $eventId)); |
111
|
|
|
|
112
|
|
|
if (isset($_POST['filetokeep'])) { |
113
|
|
|
if (is_array($_POST['filetokeep'])) { |
114
|
|
|
$count = count($_POST['filetokeep']); |
|
|
|
|
115
|
|
|
$in = '(' . $_POST['filetokeep'][0]; |
116
|
|
|
array_shift($_POST['filetokeep']); |
117
|
|
|
foreach ($_POST['filetokeep'] as $elmt) { |
118
|
|
|
$in .= ',' . $elmt; |
119
|
|
|
} |
120
|
|
|
$in .= ')'; |
121
|
|
|
} else { |
122
|
|
|
$in = '(' . $_POST['filetokeep'] . ')'; |
123
|
|
|
} |
124
|
|
|
$criteria->add( new \Criteria('file_id', $in, 'NOT IN')); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$files = $this->getObjects($criteria); |
128
|
|
|
foreach ($files as $file) { |
129
|
|
|
$this->deleteFile($file); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $fileId |
135
|
|
|
* |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
|
public function getFile($fileId) |
139
|
|
|
{ |
140
|
|
|
return $this->get($fileId); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $files |
145
|
|
|
*/ |
146
|
|
|
public function formatFilesSize(&$files) |
147
|
|
|
{ |
148
|
|
|
for ($i = 0, $iMax = count($files); $i < $iMax; ++$i) { |
149
|
|
|
$this->formatFileSize($files[$i]); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $file |
155
|
|
|
*/ |
156
|
|
|
public function formatFileSize(&$file) |
157
|
|
|
{ |
158
|
|
|
if ($file['file_size'] > 1000) { |
159
|
|
|
$file['formated_file_size'] = round($file['file_size'] / 1000) . 'kb'; |
160
|
|
|
} else { |
161
|
|
|
$file['formated_file_size'] = '1kb'; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $file |
167
|
|
|
*/ |
168
|
|
|
public function _deleteFile(&$file) |
169
|
|
|
{ |
170
|
|
|
if (file_exists(XOOPS_ROOT_PATH . '/uploads/extcal/' . $file->getVar('file_name'))) { |
171
|
|
|
unlink(XOOPS_ROOT_PATH . '/uploads/extcal/' . $file->getVar('file_name')); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.