|
1
|
|
|
<?php declare(strict_types=1); |
|
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 https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later} |
|
15
|
|
|
* @since |
|
16
|
|
|
* @author XOOPS Development Team |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Xmf\Request; |
|
20
|
|
|
use XoopsModules\Publisher\Helper; |
|
21
|
|
|
use XoopsModules\Publisher\Resizer; |
|
22
|
|
|
use XoopsModules\Publisher\Utility; |
|
23
|
|
|
|
|
24
|
|
|
error_reporting(0); |
|
25
|
|
|
require \dirname(__DIR__, 3) . '/mainfile.php'; |
|
26
|
|
|
require_once __DIR__ . '/common.php'; |
|
27
|
|
|
|
|
28
|
|
|
$GLOBALS['xoopsLogger']->activated = false; |
|
29
|
|
|
$helper = Helper::getInstance(); |
|
30
|
|
|
$helper->loadLanguage('common'); |
|
31
|
|
|
|
|
32
|
|
|
if (is_object($GLOBALS['xoopsUser'])) { |
|
33
|
|
|
$group = $GLOBALS['xoopsUser']->getGroups(); |
|
34
|
|
|
} else { |
|
35
|
|
|
$group = [XOOPS_GROUP_ANONYMOUS]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$filename = basename($_FILES['publisher_upload_file']['name']); |
|
39
|
|
|
$imageNiceName = Request::getString('image_nicename', '', 'POST'); |
|
40
|
|
|
if ('' === $imageNiceName || _CO_PUBLISHER_IMAGE_NICENAME == $imageNiceName) { |
|
41
|
|
|
$imageNiceName = $filename; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$imgcatId = Request::getInt('imgcat_id', 0, 'POST'); |
|
45
|
|
|
|
|
46
|
|
|
$imgcatHandler = xoops_getHandler('imagecategory'); |
|
47
|
|
|
$imgcat = $imgcatHandler->get($imgcatId); |
|
48
|
|
|
|
|
49
|
|
|
$error = false; |
|
50
|
|
|
if (is_object($imgcat)) { |
|
51
|
|
|
/** @var XoopsGroupPermHandler $imgcatpermHandler */ |
|
52
|
|
|
$imgcatpermHandler = xoops_getHandler('groupperm'); |
|
53
|
|
|
if (is_object($GLOBALS['xoopsUser'])) { |
|
54
|
|
|
if (!$imgcatpermHandler->checkRight('imgcat_write', $imgcatId, $GLOBALS['xoopsUser']->getGroups())) { |
|
55
|
|
|
$error = _CO_PUBLISHER_IMAGE_CAT_NONE; |
|
56
|
|
|
} |
|
57
|
|
|
} elseif (!$imgcatpermHandler->checkRight('imgcat_write', $imgcatId, XOOPS_GROUP_ANONYMOUS)) { |
|
58
|
|
|
$error = _CO_PUBLISHER_IMAGE_CAT_NOPERM; |
|
59
|
|
|
} |
|
60
|
|
|
} else { |
|
61
|
|
|
$error = _CO_PUBLISHER_IMAGE_CAT_NONE; |
|
62
|
|
|
} |
|
63
|
|
|
$image = ''; |
|
64
|
|
|
if (false === $error) { |
|
65
|
|
|
xoops_load('XoopsMediaUploader'); |
|
66
|
|
|
// upload image according to module preferences and resize later to max size of selected image cat |
|
67
|
|
|
$uploader = new \XoopsMediaUploader(XOOPS_UPLOAD_PATH . '/images', ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png'], $helper->getConfig('maximum_filesize'), $helper->getConfig('maximum_image_width'), $helper->getConfig('maximum_image_height')); |
|
68
|
|
|
$uploader->setPrefix('img'); |
|
69
|
|
|
if ($uploader->fetchMedia('publisher_upload_file')) { |
|
70
|
|
|
if ($uploader->upload()) { |
|
71
|
|
|
/** @var \XoopsImageHandler $imageHandler */ |
|
72
|
|
|
$imageHandler = xoops_getHandler('image'); |
|
73
|
|
|
/** @var \XoopsImage $image */ |
|
74
|
|
|
$image = $imageHandler->create(); |
|
75
|
|
|
$savedFilename = $uploader->getSavedFileName(); |
|
76
|
|
|
$imageMimetype = $uploader->getMediaType(); |
|
77
|
|
|
$image->setVar('image_name', 'images/' . $savedFilename); |
|
78
|
|
|
$image->setVar('image_nicename', $imageNiceName); |
|
79
|
|
|
$image->setVar('image_mimetype', $imageMimetype); |
|
80
|
|
|
$image->setVar('image_created', time()); |
|
81
|
|
|
$image->setVar('image_display', 1); |
|
82
|
|
|
$image->setVar('image_weight', 0); |
|
83
|
|
|
$image->setVar('imgcat_id', $imgcatId); |
|
84
|
|
|
if ('db' === $imgcat->getVar('imgcat_storetype')) { |
|
85
|
|
|
$fp = @fopen($uploader->getSavedDestination(), 'rb'); |
|
86
|
|
|
$fbinary = @fread($fp, filesize($uploader->getSavedDestination())); |
|
|
|
|
|
|
87
|
|
|
@fclose($fp); |
|
|
|
|
|
|
88
|
|
|
$image->setVar('image_body', $fbinary, true); |
|
89
|
|
|
if (file_exists($uploader->getSavedDestination())) { |
|
90
|
|
|
unlink($uploader->getSavedDestination()); |
|
91
|
|
|
} |
|
92
|
|
|
} else { |
|
93
|
|
|
$maxwidth = $imgcat->getVar('imgcat_maxwidth'); |
|
94
|
|
|
$maxheight = $imgcat->getVar('imgcat_maxheight'); |
|
95
|
|
|
$imgHandler = new Resizer(); |
|
96
|
|
|
$imgHandler->sourceFile = $uploader->getSavedDestination(); |
|
97
|
|
|
$imgHandler->endFile = $uploader->getSavedDestination(); |
|
98
|
|
|
$imgHandler->imageMimetype = $imageMimetype; |
|
99
|
|
|
$imgHandler->maxWidth = $maxwidth; |
|
100
|
|
|
$imgHandler->maxHeight = $maxheight; |
|
101
|
|
|
$result = $imgHandler->resizeImage(); |
|
102
|
|
|
} |
|
103
|
|
|
if (!$imageHandler->insert($image)) { |
|
104
|
|
|
$error = sprintf(_FAILSAVEIMG, $image->getVar('image_nicename')); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} else { |
|
107
|
|
|
$error = implode('<br>', $uploader->getErrors(false)); |
|
108
|
|
|
} |
|
109
|
|
|
} else { |
|
110
|
|
|
$error = sprintf(_FAILFETCHIMG, 0) . '<br>' . implode('<br>', $uploader->getErrors(false)); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$arr = ['success', $image->getVar('image_name'), Utility::convertCharset($image->getVar('image_nicename'))]; |
|
|
|
|
|
|
115
|
|
|
if (false !== $error) { |
|
116
|
|
|
$arr = ['error', Utility::convertCharset($error)]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
echo json_encode($arr); |
|
120
|
|
|
|