1: <?php
2:
3: /**
4: * SystemFineImUploadHandler class to work with ajaxfineupload.php endpoint
5: * to facilitate uploads for the system image manager
6: *
7: * Do not use or reference this directly from your client-side code.
8: * Instead, this should be required via the endpoint.php or endpoint-cors.php
9: * file(s).
10: *
11: * @license MIT License (MIT)
12: * @copyright Copyright (c) 2015-present, Widen Enterprises, Inc.
13: * @link https://github.com/FineUploader/php-traditional-server
14: *
15: * The MIT License (MIT)
16: *
17: * Copyright (c) 2015-present, Widen Enterprises, Inc.
18: *
19: * Permission is hereby granted, free of charge, to any person obtaining a copy
20: * of this software and associated documentation files (the "Software"), to deal
21: * in the Software without restriction, including without limitation the rights
22: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23: * copies of the Software, and to permit persons to whom the Software is
24: * furnished to do so, subject to the following conditions:
25: *
26: * The above copyright notice and this permission notice shall be included in all
27: * copies or substantial portions of the Software.
28: *
29: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35: * SOFTWARE.
36: */
37:
38: class SystemFineImUploadHandler extends SystemFineUploadHandler
39: {
40: /**
41: * XoopsFineImUploadHandler constructor.
42: * @param stdClass $claims claims passed in JWT header
43: */
44: public function __construct(\stdClass $claims)
45: {
46: parent::__construct($claims);
47: $this->allowedMimeTypes = array('image/gif', 'image/jpeg', 'image/png');
48: $this->allowedExtensions = array('gif', 'jpeg', 'jpg', 'png');
49: }
50:
51: protected function storeUploadedFile($target, $mimeType, $uuid)
52: {
53: /** @var XoopsImagecategoryHandler */
54: $imgcatHandler = xoops_getHandler('imagecategory');
55: $imgcat = $imgcatHandler->get($this->claims->cat);
56:
57: $pathParts = pathinfo($this->getName());
58:
59: $imageName = uniqid('img', true) . '.' . strtolower($pathParts['extension']);
60: $imageNicename = str_replace(array('_','-'), ' ', $pathParts['filename']);
61: $imagePath = XOOPS_ROOT_PATH . '/uploads/images/' . $imageName;
62:
63: $fbinary = null;
64: if ($imgcat->getVar('imgcat_storetype') === 'db') {
65: $fbinary = file_get_contents($_FILES[$this->inputName]['tmp_name']);
66: } else {
67: if (false === move_uploaded_file($_FILES[$this->inputName]['tmp_name'], $imagePath)) {
68: return false;
69: }
70: }
71:
72: /** @var XoopsImageHandler $imageHandler */
73: $imageHandler = xoops_getHandler('image');
74: $image = $imageHandler->create();
75:
76: $image->setVar('image_nicename', $imageNicename);
77: $image->setVar('image_mimetype', $mimeType);
78: $image->setVar('image_created', time());
79: $image->setVar('image_display', 1);
80: $image->setVar('image_weight', 0);
81: $image->setVar('imgcat_id', $this->claims->cat);
82: if ($imgcat->getVar('imgcat_storetype') === 'db') {
83: $image->setVar('image_body', $fbinary, true);
84: } else {
85: $image->setVar('image_name', 'images/' . $imageName);
86: }
87: if (!$imageHandler->insert($image)) {
88: return array(
89: 'error' => sprintf(_FAILSAVEIMG, $image->getVar('image_nicename'))
90: );
91: }
92: return array('success'=> true, "uuid" => $uuid);
93: }
94: }
95: