|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Suico; |
|
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
|
|
|
* @category Module |
|
17
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
|
18
|
|
|
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
|
19
|
|
|
* @author Marcello Brandão aka Suico, Mamba, LioMJ <https://xoops.org> |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
use WideImage\WideImage; |
|
23
|
|
|
use Xmf\Request; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class Utility |
|
27
|
|
|
*/ |
|
28
|
|
|
class Utility extends Common\SysUtility |
|
29
|
|
|
{ |
|
30
|
|
|
//--------------- Custom module methods ----------------------------- |
|
31
|
|
|
/** |
|
32
|
|
|
* Access the only instance of this class |
|
33
|
|
|
* |
|
34
|
|
|
* @return object |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function getInstance() |
|
37
|
|
|
{ |
|
38
|
|
|
static $instance; |
|
39
|
|
|
if (null === $instance) { |
|
40
|
|
|
$instance = new static(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $instance; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create a unique upload filename |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $folder The folder where the file will be saved |
|
50
|
|
|
* @param $filename |
|
51
|
|
|
* @param bool $trimname |
|
52
|
|
|
* @return string The unique filename to use (with its extension) |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function createUploadName($folder, $filename, $trimname = false) |
|
55
|
|
|
{ |
|
56
|
|
|
$workingfolder = $folder; |
|
57
|
|
|
if ('/' !== \xoops_substr($workingfolder, mb_strlen($workingfolder) - 1, 1)) { |
|
58
|
|
|
$workingfolder .= '/'; |
|
59
|
|
|
} |
|
60
|
|
|
$ext = \basename($filename); |
|
61
|
|
|
$ext = \explode('.', $ext); |
|
62
|
|
|
$ext = '.' . $ext[\count($ext) - 1]; |
|
63
|
|
|
$true = true; |
|
64
|
|
|
while ($true) { |
|
65
|
|
|
$ipbits = \explode('.', $_SERVER['REMOTE_ADDR']); |
|
66
|
|
|
[$usec, $sec] = \explode(' ', \microtime()); |
|
67
|
|
|
$usec *= 65536; |
|
68
|
|
|
$sec = ((int)$sec) & 0xFFFF; |
|
69
|
|
|
if ($trimname) { |
|
70
|
|
|
$uid = \sprintf('%06x%04x%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
|
71
|
|
|
} else { |
|
72
|
|
|
$uid = \sprintf('%08x-%04x-%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
|
73
|
|
|
} |
|
74
|
|
|
if (!\file_exists($workingfolder . $uid . $ext)) { |
|
75
|
|
|
$true = false; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $uid . $ext; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Resize a Picture to some given dimensions (using the wideImage library) |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $src_path Picture's source |
|
86
|
|
|
* @param string $dst_path Picture's destination |
|
87
|
|
|
* @param int $param_width Maximum picture's width |
|
88
|
|
|
* @param int $param_height Maximum picture's height |
|
89
|
|
|
* @param bool $keep_original Do we have to keep the original picture ? |
|
90
|
|
|
* @param string $fit Resize mode (see the wideImage library for more information) |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function resizePicture( |
|
95
|
|
|
$src_path, |
|
96
|
|
|
$dst_path, |
|
97
|
|
|
$param_width, |
|
98
|
|
|
$param_height, |
|
99
|
|
|
$keep_original = false, |
|
100
|
|
|
$fit = 'inside' |
|
101
|
|
|
) { |
|
102
|
|
|
$resize = true; |
|
103
|
|
|
if ($moduleDirNameUpper . '_DONT_RESIZE_IF_SMALLER') { |
|
|
|
|
|
|
104
|
|
|
$pictureDimensions = \getimagesize($src_path); |
|
105
|
|
|
if (\is_array($pictureDimensions)) { |
|
|
|
|
|
|
106
|
|
|
$width = $pictureDimensions[0]; |
|
107
|
|
|
$height = $pictureDimensions[1]; |
|
108
|
|
|
if ($width < $param_width && $height < $param_height) { |
|
109
|
|
|
$resize = false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
$img = WideImage::load($src_path); |
|
114
|
|
|
if ($resize) { |
|
115
|
|
|
$result = $img->resize($param_width, $param_height, $fit); |
|
116
|
|
|
$result->saveToFile($dst_path); |
|
117
|
|
|
} else { |
|
118
|
|
|
@\copy($src_path, $dst_path); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
if (!$keep_original && false === @\unlink($src_path)) { |
|
121
|
|
|
throw new \RuntimeException('The file ' . $src_path . ' could not be deleted.'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $srcPath |
|
129
|
|
|
* @param $destPath |
|
130
|
|
|
* @param $paramWidth |
|
131
|
|
|
* @param $paramHeight |
|
132
|
|
|
* @param bool $keepOriginal |
|
133
|
|
|
* @param string $fit |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function resizeSavePicture( |
|
136
|
|
|
$srcPath, |
|
|
|
|
|
|
137
|
|
|
$destPath, |
|
138
|
|
|
$paramWidth, |
|
|
|
|
|
|
139
|
|
|
$paramHeight, |
|
|
|
|
|
|
140
|
|
|
$keepOriginal = false, |
|
|
|
|
|
|
141
|
|
|
$fit = 'inside' |
|
|
|
|
|
|
142
|
|
|
): void { |
|
143
|
|
|
if ($allowupload) { // L'image |
|
|
|
|
|
|
144
|
|
|
if (Request::hasVar('xoops_upload_file', 'POST')) { |
|
145
|
|
|
$helper = Helper::getInstance(); |
|
146
|
|
|
$fldname = $_FILES[$_POST['xoops_upload_file'][1]]; |
|
147
|
|
|
$fldname = $fldname['name']; |
|
148
|
|
|
if (\xoops_trim('' !== $fldname)) { |
|
149
|
|
|
$destname = self::createUploadName($destPath, $fldname); |
|
150
|
|
|
$permittedTypes = $helper->getConfig('mimetypes'); //['image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png']; |
|
151
|
|
|
$uploader = new \XoopsMediaUploader(XOOPS_ROOT_PATH . '/uploads/news/image', $permittedTypes, $helper->getConfig('maxuploadsize')); |
|
152
|
|
|
$uploader->setTargetFileName($destname); |
|
153
|
|
|
if ($uploader->fetchMedia($_POST['xoops_upload_file'][1])) { |
|
154
|
|
|
if ($uploader->upload()) { |
|
155
|
|
|
$fullPictureName = XOOPS_ROOT_PATH . '/uploads/news/image/' . \basename($destname); |
|
156
|
|
|
$newName = XOOPS_ROOT_PATH . '/uploads/news/image/redim_' . \basename($destname); |
|
157
|
|
|
self::resizePicture($fullPictureName, $newName, $helper->getConfig('maxwidth'), $helper->getConfig('maxheight')); |
|
158
|
|
|
if (\file_exists($newName)) { |
|
159
|
|
|
if (false === @\unlink($fullPictureName)) { |
|
160
|
|
|
throw new \RuntimeException('The file ' . $fullPictureName . ' could not be deleted.'); |
|
161
|
|
|
} |
|
162
|
|
|
\rename($newName, $fullPictureName); |
|
163
|
|
|
} |
|
164
|
|
|
$story->setPicture(\basename($destname)); |
|
|
|
|
|
|
165
|
|
|
} else { |
|
166
|
|
|
echo \_AM_SUICO_UPLOAD_ERROR . ' ' . $uploader->getErrors(); |
|
167
|
|
|
} |
|
168
|
|
|
} else { |
|
169
|
|
|
echo $uploader->getErrors(); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
$story->setPictureinfo(Request::getString('pictureinfo', '', 'POST')); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|