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