forked from XOOPS/XoopsCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.php
357 lines (334 loc) · 11.2 KB
/
file.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
use Xoops\Core\Database\Connection;
use Xoops\Core\Kernel\XoopsObject;
use Xoops\Core\Kernel\XoopsObjectHandler;
use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
use Xoops\Core\Kernel\Criteria;
use Xoops\Core\Kernel\CriteriaCompo;
/**
* @copyright The XUUPS Project http://sourceforge.net/projects/xuups/
* @license GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
* @package Publisher
* @since 1.0
* @author trabis <lusopoemas@gmail.com>
* @author The SmartFactory <www.smartfactory.ca>
* @version $Id$
*/
include_once dirname(__DIR__) . '/include/common.php';
// File status
define("_PUBLISHER_STATUS_FILE_NOTSET", -1);
define("_PUBLISHER_STATUS_FILE_ACTIVE", 1);
define("_PUBLISHER_STATUS_FILE_INACTIVE", 2);
class PublisherFile extends XoopsObject
{
/**
* @var Publisher
* @access public
*/
public $publisher = null;
/**
* @param null|int $id
*/
public function __construct($id = null)
{
$this->publisher = Publisher::getInstance();
$this->initVar("fileid", XOBJ_DTYPE_INT, 0, false);
$this->initVar("itemid", XOBJ_DTYPE_INT, null, true);
$this->initVar("name", XOBJ_DTYPE_TXTBOX, null, true, 255);
$this->initVar("description", XOBJ_DTYPE_TXTBOX, null, false, 255);
$this->initVar("filename", XOBJ_DTYPE_TXTBOX, null, true, 255);
$this->initVar("mimetype", XOBJ_DTYPE_TXTBOX, null, true, 64);
$this->initVar("uid", XOBJ_DTYPE_INT, 0, false);
$this->initVar("datesub", XOBJ_DTYPE_INT, null, false);
$this->initVar("status", XOBJ_DTYPE_INT, 1, false);
$this->initVar("notifypub", XOBJ_DTYPE_INT, 0, false);
$this->initVar("counter", XOBJ_DTYPE_INT, null, false);
if (isset($id)) {
$file = $this->publisher->getFileHandler()->get($id);
foreach ($file->vars as $k => $v) {
$this->assignVar($k, $v['value']);
}
}
}
/**
* @param string $post_field
* @param array $allowed_mimetypes
* @param array $errors
*
* @return bool
*/
public function checkUpload($post_field, $allowed_mimetypes = array(), &$errors)
{
$errors = array();
if (!$this->publisher->getMimetypeHandler()->checkMimeTypes($post_field)) {
$errors[] = _CO_PUBLISHER_MESSAGE_WRONG_MIMETYPE;
return false;
}
if (empty($allowed_mimetypes)) {
$allowed_mimetypes = $this->publisher->getMimetypeHandler()->getArrayByType();
}
$maxfilesize = $this->publisher->getConfig('maximum_filesize');
$maxfilewidth = $this->publisher->getConfig('maximum_image_width');
$maxfileheight = $this->publisher->getConfig('maximum_image_height');
$uploader = new XoopsMediaUploader(PublisherUtils::getUploadDir(), $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
if ($uploader->fetchMedia($post_field)) {
return true;
} else {
$errors = array_merge($errors, $uploader->getErrors(false));
return false;
}
}
/**
* @param string $post_field
* @param array $allowed_mimetypes
* @param array $errors
*
* @return bool
*/
public function storeUpload($post_field, $allowed_mimetypes = array(), &$errors)
{
$itemid = $this->getVar('itemid');
if (empty($allowed_mimetypes)) {
$allowed_mimetypes = $this->publisher->getMimetypeHandler()->getArrayByType();
}
$maxfilesize = $this->publisher->getConfig('maximum_filesize');
$maxfilewidth = $this->publisher->getConfig('maximum_image_width');
$maxfileheight = $this->publisher->getConfig('maximum_image_height');
if (!is_dir(PublisherUtils::getUploadDir())) {
mkdir(PublisherUtils::getUploadDir(), 0757);
}
$uploader = new XoopsMediaUploader(PublisherUtils::getUploadDir() . '/', $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
if ($uploader->fetchMedia($post_field)) {
$uploader->setTargetFileName($itemid . "_" . $uploader->getMediaName());
if ($uploader->upload()) {
$this->setVar('filename', $uploader->getSavedFileName());
if ($this->getVar('name') == '') {
$this->setVar('name', $this->getNameFromFilename());
}
$this->setVar('mimetype', $uploader->getMediaType());
return true;
} else {
$errors = array_merge($errors, $uploader->getErrors(false));
return false;
}
} else {
$errors = array_merge($errors, $uploader->getErrors(false));
return false;
}
}
/**
* @param null|array $allowed_mimetypes
* @param bool $force
* @param bool $doupload
*
* @return bool
*/
public function store($allowed_mimetypes = null, $force = true, $doupload = true)
{
if ($this->isNew()) {
$errors = array();
if ($doupload) {
$ret = $this->storeUpload('item_upload_file', $allowed_mimetypes, $errors);
} else {
$ret = true;
}
if (!$ret) {
foreach ($errors as $error) {
$this->setErrors($error);
}
return false;
}
}
return $this->publisher->getFileHandler()->insert($this, $force);
}
/**
* @param string $dateFormat
* @param string $format
*
* @return string
*/
public function datesub($dateFormat = 's', $format = "S")
{
return XoopsLocale::formatTimestamp($this->getVar('datesub', $format), $dateFormat);
}
/**
* @return bool
*/
public function notLoaded()
{
return ($this->getVar('itemid') == 0);
}
/**
* @return string
*/
public function getFileUrl()
{
return PublisherUtils::getUploadDir(false) . $this->getVar('filename');
}
/**
* @return string
*/
public function getFilePath()
{
return PublisherUtils::getUploadDir() . $this->getVar('filename');
}
/**
* @return string
*/
public function getFileLink()
{
return "<a href='" . PUBLISHER_URL . "/visit.php?fileid=" . $this->getVar('fileid') . "'>" . $this->getVar('name') . "</a>";
}
/**
* @return string
*/
public function getItemLink()
{
return "<a href='" . PUBLISHER_URL . "/item.php?itemid=" . $this->getVar('itemid') . "'>" . $this->getVar('name') . "</a>";
}
/**
* Update Counter
*/
public function updateCounter()
{
$this->setVar('counter', $this->getVar('counter') + 1);
$this->store();
}
/**
* @return string
*/
public function displayFlash()
{
return PublisherUtils::displayFlash($this->getFileUrl());
}
/**
* @return string
*/
public function getNameFromFilename()
{
$ret = $this->getVar('filename');
$sep_pos = strpos($ret, '_');
$ret = substr($ret, $sep_pos + 1, strlen($ret) - $sep_pos);
return $ret;
}
}
/**
* Files handler class.
* This class is responsible for providing data access mechanisms to the data source
* of File class objects.
*
* @author marcan <marcan@notrevie.ca>
* @package Publisher
*/
class PublisherFileHandler extends XoopsPersistableObjectHandler
{
/**
* @param null|object $db
*/
public function __construct(Connection $db)
{
parent::__construct($db, "publisher_files", 'PublisherFile', "fileid", "name");
}
/**
* delete a file from the database
*
* @param XoopsObject|PublisherFile $file reference to the file to delete
* @param bool $force
*
* @return bool FALSE if failed.
*/
public function delete(XoopsObject $file, $force = false)
{
$ret = false;
// Delete the actual file
if (is_file($file->getFilePath()) && unlink($file->getFilePath())) {
$ret = parent::delete($file, $force);
}
return $ret;
}
/**
* delete files related to an item from the database
*
* @param object $itemObj reference to the item which files to delete
*
* @return bool
*/
public function deleteItemFiles(&$itemObj)
{
if (strtolower(get_class($itemObj)) !== 'publisheritem') {
return false;
}
$files = $this->getAllFiles($itemObj->getVar('itemid'));
$result = true;
foreach ($files as $file) {
if (!$this->delete($file)) {
$result = false;
}
}
return $result;
}
/**
* retrieve all files
*
* @param int $itemid
* @param int $status
* @param int $limit
* @param int $start
* @param string $sort
* @param string $order
* @param array $category
*
* @return array array of {@link PublisherFile} objects
*/
public function &getAllFiles($itemid = 0, $status = -1, $limit = 0, $start = 0, $sort = 'datesub', $order = 'DESC', $category = array())
{
$this->table_link = $this->db2->prefix('publisher_items');
$this->field_object = 'itemid';
$this->field_link = 'itemid';
$hasStatusCriteria = false;
$criteriaStatus = new CriteriaCompo();
if (is_array($status)) {
$hasStatusCriteria = true;
foreach ($status as $v) {
$criteriaStatus->add(new Criteria('o.status', $v), 'OR');
}
} elseif ($status != -1) {
$hasStatusCriteria = true;
$criteriaStatus->add(new Criteria('o.status', $status), 'OR');
}
$hasCategoryCriteria = false;
$criteriaCategory = new CriteriaCompo();
$category = (array)$category;
if (count($category) > 0 && $category[0] != 0) {
$hasCategoryCriteria = true;
foreach ($category as $cat) {
$criteriaCategory->add(new Criteria('l.categoryid', $cat), 'OR');
}
}
$criteriaItemid = new Criteria('o.itemid', $itemid);
$criteria = new CriteriaCompo();
if ($itemid != 0) {
$criteria->add($criteriaItemid);
}
if ($hasStatusCriteria) {
$criteria->add($criteriaStatus);
}
if ($hasCategoryCriteria) {
$criteria->add($criteriaCategory);
}
$criteria->setSort($sort);
$criteria->setOrder($order);
$criteria->setLimit($limit);
$criteria->setStart($start);
$files = $this->getByLink($criteria, array('o.*'), true);
return $files;
}
}