1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Publisher; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
You may not change or alter any portion of this comment or credits |
7
|
|
|
of supporting developers from this source code or any supporting source code |
8
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
9
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, |
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
17
|
|
|
* @license https://www.fsf.org/copyleft/gpl.html GNU public license |
18
|
|
|
* @since 1.0 |
19
|
|
|
* @author trabis <[email protected]> |
20
|
|
|
* @author The SmartFactory <www.smartfactory.ca> |
21
|
|
|
*/ |
22
|
|
|
require_once \dirname(__DIR__) . '/include/common.php'; |
23
|
|
|
|
24
|
|
|
// File status |
25
|
|
|
//define("_PUBLISHER_STATUS_FILE_NOTSET", -1); |
26
|
|
|
//define("_PUBLISHER_STATUS_FILE_ACTIVE", 1); |
27
|
|
|
//define("_PUBLISHER_STATUS_FILE_INACTIVE", 2); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class File |
31
|
|
|
*/ |
32
|
|
|
class File extends \XoopsObject |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Helper |
36
|
|
|
*/ |
37
|
|
|
public $helper; |
38
|
|
|
/** @var \XoopsMySQLDatabase */ |
39
|
|
|
public $db; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param null|int $id |
43
|
|
|
*/ |
44
|
|
|
public function __construct($id = null) |
45
|
|
|
{ |
46
|
|
|
/** @var Helper $this- >helper */ |
47
|
|
|
$this->helper = Helper::getInstance(); |
48
|
|
|
/** @var \XoopsMySQLDatabase $db */ |
49
|
|
|
$this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
50
|
|
|
|
51
|
|
|
$this->initVar('fileid', \XOBJ_DTYPE_INT, 0, false); |
52
|
|
|
$this->initVar('itemid', \XOBJ_DTYPE_INT, null, true); |
53
|
|
|
$this->initVar('name', \XOBJ_DTYPE_TXTBOX, null, true, 255); |
54
|
|
|
$this->initVar('description', \XOBJ_DTYPE_TXTBOX, null, false, 255); |
55
|
|
|
$this->initVar('filename', \XOBJ_DTYPE_TXTBOX, null, true, 255); |
56
|
|
|
$this->initVar('mimetype', \XOBJ_DTYPE_TXTBOX, null, true, 64); |
57
|
|
|
$this->initVar('uid', \XOBJ_DTYPE_INT, 0, false); |
58
|
|
|
$this->initVar('datesub', \XOBJ_DTYPE_INT, null, false); |
59
|
|
|
$this->initVar('status', \XOBJ_DTYPE_INT, 1, false); |
60
|
|
|
$this->initVar('notifypub', \XOBJ_DTYPE_INT, 0, false); |
61
|
|
|
$this->initVar('counter', \XOBJ_DTYPE_INT, null, false); |
62
|
|
|
if (null !== $id) { |
63
|
|
|
$file = $this->helper->getHandler('File') |
64
|
|
|
->get($id); |
65
|
|
|
foreach ($file->vars as $k => $v) { |
66
|
|
|
$this->assignVar($k, $v['value']); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $method |
73
|
|
|
* @param array $args |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function __call($method, $args) |
78
|
|
|
{ |
79
|
|
|
$arg = $args[0] ?? ''; |
80
|
|
|
|
81
|
|
|
return $this->getVar($method, $arg); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $postField |
86
|
|
|
* @param array $allowedMimetypes |
87
|
|
|
* @param array $errors |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function checkUpload($postField, $allowedMimetypes, &$errors) |
92
|
|
|
{ |
93
|
|
|
/** @var MimetypeHandler $mimetypeHandler */ |
94
|
|
|
$mimetypeHandler = $this->helper->getHandler('Mimetype'); |
95
|
|
|
$errors = []; |
96
|
|
|
if (!$mimetypeHandler->checkMimeTypes($postField)) { |
97
|
|
|
$errors[] = \_CO_PUBLISHER_MESSAGE_WRONG_MIMETYPE; |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
if (0 === \count($allowedMimetypes)) { |
102
|
|
|
$allowedMimetypes = $mimetypeHandler->getArrayByType(); |
103
|
|
|
} |
104
|
|
|
$maxfilesize = $this->helper->getConfig('maximum_filesize'); |
105
|
|
|
$maxfilewidth = $this->helper->getConfig('maximum_image_width'); |
106
|
|
|
$maxfileheight = $this->helper->getConfig('maximum_image_height'); |
107
|
|
|
\xoops_load('XoopsMediaUploader'); |
108
|
|
|
$uploader = new \XoopsMediaUploader(Utility::getUploadDir(), $allowedMimetypes, $maxfilesize, $maxfilewidth, $maxfileheight); |
109
|
|
|
if ($uploader->fetchMedia($postField)) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
$errors = \array_merge($errors, $uploader->getErrors(false)); |
113
|
|
|
|
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $postField |
119
|
|
|
* @param array $allowedMimetypes |
120
|
|
|
* @param array $errors |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function storeUpload($postField, $allowedMimetypes, &$errors) |
125
|
|
|
{ |
126
|
|
|
/** @var MimetypeHandler $mimetypeHandler */ |
127
|
|
|
$mimetypeHandler = $this->helper->getHandler('Mimetype'); |
128
|
|
|
$itemId = $this->getVar('itemid'); |
129
|
|
|
if (0 === \count($allowedMimetypes)) { |
130
|
|
|
$allowedMimetypes = $mimetypeHandler->getArrayByType(); |
131
|
|
|
} |
132
|
|
|
$maxfilesize = $this->helper->getConfig('maximum_filesize'); |
133
|
|
|
$maxfilewidth = $this->helper->getConfig('maximum_image_width'); |
134
|
|
|
$maxfileheight = $this->helper->getConfig('maximum_image_height'); |
135
|
|
|
if (!\is_dir(Utility::getUploadDir())) { |
136
|
|
|
if (!\mkdir($concurrentDirectory = Utility::getUploadDir(), 0757) && !\is_dir($concurrentDirectory)) { |
137
|
|
|
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $concurrentDirectory)); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
\xoops_load('XoopsMediaUploader'); |
141
|
|
|
$uploader = new \XoopsMediaUploader(Utility::getUploadDir() . '/', $allowedMimetypes, $maxfilesize, $maxfilewidth, $maxfileheight); |
142
|
|
|
if ($uploader->fetchMedia($postField)) { |
143
|
|
|
$uploader->setTargetFileName($itemId . '_' . $uploader->getMediaName()); |
144
|
|
|
if ($uploader->upload()) { |
145
|
|
|
$this->setVar('filename', $uploader->getSavedFileName()); |
146
|
|
|
if ('' == $this->getVar('name')) { |
147
|
|
|
$this->setVar('name', $this->getNameFromFilename()); |
148
|
|
|
} |
149
|
|
|
$this->setVar('mimetype', $uploader->getMediaType()); |
150
|
|
|
|
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
$errors = \array_merge($errors, $uploader->getErrors(false)); |
154
|
|
|
|
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
$errors = \array_merge($errors, $uploader->getErrors(false)); |
158
|
|
|
|
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param null|array $allowedMimetypes |
164
|
|
|
* @param bool $force |
165
|
|
|
* @param bool $doupload |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function store($allowedMimetypes = null, $force = true, $doupload = true) |
170
|
|
|
{ |
171
|
|
|
if ($this->isNew()) { |
172
|
|
|
$errors = []; |
173
|
|
|
$ret = true; |
174
|
|
|
if ($doupload) { |
175
|
|
|
$ret = $this->storeUpload('item_upload_file', $allowedMimetypes, $errors); |
176
|
|
|
} |
177
|
|
|
if (!$ret) { |
178
|
|
|
foreach ($errors as $error) { |
179
|
|
|
$this->setErrors($error); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this->helper->getHandler('File') |
187
|
|
|
->insert($this, $force); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $dateFormat |
192
|
|
|
* @param string $format |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getDatesub($dateFormat = 's', $format = 'S') |
197
|
|
|
{ |
198
|
|
|
//mb xoops_load('XoopsLocal'); |
199
|
|
|
//mb return XoopsLocal::formatTimestamp($this->getVar('datesub', $format), $dateFormat); |
200
|
|
|
return \formatTimestamp($this->getVar('datesub', $format), $dateFormat); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public function notLoaded() |
207
|
|
|
{ |
208
|
|
|
return (0 === $this->getVar('itemid')); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getFileUrl() |
215
|
|
|
{ |
216
|
|
|
return Utility::getUploadDir(false) . $this->filename(); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
public function getFilePath() |
223
|
|
|
{ |
224
|
|
|
return Utility::getUploadDir() . $this->filename(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function getFileLink() |
231
|
|
|
{ |
232
|
|
|
return "<a href='" . PUBLISHER_URL . '/visit.php?fileid=' . $this->fileid() . "'>" . $this->name() . '</a>'; |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
public function getItemLink() |
239
|
|
|
{ |
240
|
|
|
return "<a href='" . PUBLISHER_URL . '/item.php?itemid=' . $this->itemid() . "'>" . $this->name() . '</a>'; |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Update Counter |
245
|
|
|
*/ |
246
|
|
|
public function updateCounter(): void |
247
|
|
|
{ |
248
|
|
|
$this->setVar('counter', $this->counter() + 1); |
|
|
|
|
249
|
|
|
$this->store(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public function displayFlash() |
256
|
|
|
{ |
257
|
|
|
// if (!defined('MYTEXTSANITIZER_EXTENDED_MEDIA')) { |
258
|
|
|
// require_once PUBLISHER_ROOT_PATH . '/include/media.textsanitizer.php'; |
259
|
|
|
// } |
260
|
|
|
$mediaTs = MyTextSanitizerExtension::getInstance(); |
261
|
|
|
|
262
|
|
|
return $mediaTs->displayFlash($this->getFileUrl()); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function getNameFromFilename() |
269
|
|
|
{ |
270
|
|
|
$ret = $this->filename(); |
271
|
|
|
$sepPos = \mb_strpos($ret, '_'); |
272
|
|
|
$ret = \mb_substr($ret, $sepPos + 1); |
273
|
|
|
|
274
|
|
|
return $ret; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return Form\FileForm |
279
|
|
|
*/ |
280
|
|
|
public function getForm() |
281
|
|
|
{ |
282
|
|
|
$form = new Form\FileForm($this); |
283
|
|
|
|
284
|
|
|
return $form; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|