1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* RooCMS - Open Source Free Content Managment System
|
4
|
|
|
* @copyright © 2010-2020 alexandr Belov aka alex Roosso. All rights reserved.
|
5
|
|
|
* @author alex Roosso <[email protected]>
|
6
|
|
|
* @link http://www.roocms.com
|
7
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html
|
8
|
|
|
*
|
9
|
|
|
* You should have received a copy of the GNU General Public License v3
|
10
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/
|
11
|
|
|
*/
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
//#########################################################
|
15
|
|
|
// Anti Hack
|
16
|
|
|
//---------------------------------------------------------
|
17
|
|
|
if(!defined('RooCMS')) {
|
18
|
|
|
die('Access Denied');
|
19
|
|
|
}
|
20
|
|
|
//#########################################################
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/**
|
24
|
|
|
* Class Images
|
25
|
|
|
*/
|
26
|
|
|
class Images extends GD {
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* Function for upload image
|
31
|
|
|
*
|
32
|
|
|
* @param string $file - title vars in array $_FILES
|
33
|
|
|
* @param string $prefix - prefix for filename
|
34
|
|
|
* @param array $thumbsize - array(width,height) - thumbnail size
|
35
|
|
|
* @param bool $watermark - on/off watermark
|
36
|
|
|
* @param bool $modify - on/off modify for resize and create thumbnail
|
37
|
|
|
* @param bool $noresize - on/off resize for unmodify image
|
38
|
|
|
* @param string $fname - special filename
|
39
|
|
|
* @param string $path - path for image folder
|
40
|
|
|
*
|
41
|
|
|
* @return false|array - filenames array or false if images dont upload
|
42
|
|
|
*/
|
43
|
|
|
public function upload_image($file, $prefix="", array $thumbsize=[], $watermark=true, $modify=true, $noresize=false, $fname="", $path=_UPLOADIMAGES) {
|
44
|
|
|
return $this->upload_post_image($file, $prefix, $thumbsize, $watermark, $modify, $noresize, $fname, $path);
|
45
|
|
|
}
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* Upload images with $_POST
|
50
|
|
|
*
|
51
|
|
|
* @param string $file - title vars in array $_FILES
|
52
|
|
|
* @param string $prefix - prefix for filename
|
53
|
|
|
* @param array $thumbsize - array(width,height) - thumbnail size
|
54
|
|
|
* @param bool $watermark - on/off watermark
|
55
|
|
|
* @param bool $modify - on/off modify for resize and create thumbnail
|
56
|
|
|
* @param bool $noresize - on/off resize for unmodify image
|
57
|
|
|
* @param string $fname - special filename
|
58
|
|
|
* @param string $path - path for image folder
|
59
|
|
|
*
|
60
|
|
|
* @return false|array - filenames array or false if images dont upload
|
61
|
|
|
*/
|
62
|
|
|
protected function upload_post_image($file, $prefix="", array $thumbsize=[], $watermark=true, $modify=true, $noresize=false, $fname="", $path=_UPLOADIMAGES) {
|
63
|
|
|
|
64
|
|
|
global $config, $files;
|
65
|
|
|
|
66
|
|
|
# If lie
|
67
|
|
|
if(!isset($_FILES[$file])) {
|
68
|
|
|
return false;
|
69
|
|
|
}
|
70
|
|
|
|
71
|
|
|
# output array
|
72
|
|
|
$images = [];
|
73
|
|
|
|
74
|
|
|
# array for allowed file extension
|
75
|
|
|
static $allow_exts = [];
|
76
|
|
|
if(empty($allow_exts)) {
|
77
|
|
|
$allow_exts = $this->get_allow_images();
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
# Set thumbnail size
|
81
|
|
|
$this->set_mod_sizes($thumbsize);
|
82
|
|
|
|
83
|
|
|
# handle $_FILES
|
84
|
|
|
$upfiles = [];
|
85
|
|
|
if(!is_array($_FILES[$file]['tmp_name'])) {
|
86
|
|
|
foreach($_FILES[$file] AS $k=>$v) {
|
87
|
|
|
$upfiles[$file][$k][$file] = $v;
|
88
|
|
|
}
|
89
|
|
|
}
|
90
|
|
|
else {
|
91
|
|
|
$upfiles[$file] = $_FILES[$file];
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
# proceed to processing
|
95
|
|
|
foreach($upfiles[$file]['tmp_name'] AS $key=>$value) {
|
96
|
|
|
if(isset($upfiles[$file]['tmp_name'][$key]) && $upfiles[$file]['error'][$key] == 0) {
|
97
|
|
|
|
98
|
|
|
$upload = false;
|
99
|
|
|
|
100
|
|
|
# lets work
|
101
|
|
|
if(array_key_exists($upfiles[$file]['type'][$key], $allow_exts)) {
|
102
|
|
|
|
103
|
|
|
# file extension
|
104
|
|
|
$ext = $allow_exts[$upfiles[$file]['type'][$key]];
|
105
|
|
|
|
106
|
|
|
# Set file name if set handly naming
|
107
|
|
|
if($fname != "") {
|
108
|
|
|
$upfiles[$file]['name'][$key] = $fname.".".$ext;
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
# Create file name
|
112
|
|
|
$filename = $files->create_filename($upfiles[$file]['name'][$key], $prefix, "", $path);
|
113
|
|
|
|
114
|
|
|
# filename pofix for "modify"/"nomodify" images
|
115
|
|
|
$filename_pofix = "";
|
116
|
|
|
if($modify) {
|
117
|
|
|
$filename_pofix = "_original";
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
# save image on disk
|
121
|
|
|
copy($upfiles[$file]['tmp_name'][$key], $path."/".$filename.$filename_pofix.".".$ext);
|
122
|
|
|
|
123
|
|
|
# if uploading was successful and file exists
|
124
|
|
|
$upload = is_file($path."/".$filename.$filename_pofix.".".$ext);
|
125
|
|
|
}
|
126
|
|
|
|
127
|
|
|
# if uploading was successful
|
128
|
|
|
if($upload) {
|
129
|
|
|
# convert jpgtowebp
|
130
|
|
|
if($config->gd_convert_jpg_to_webp) {
|
131
|
|
|
$ext = $this->convert_jpgtowebp($filename, $ext, $path);
|
|
|
|
|
132
|
|
|
}
|
133
|
|
|
|
134
|
|
|
$this->modify_image($filename, $ext, $path, $watermark, $modify, $noresize);
|
135
|
|
|
}
|
136
|
|
|
else {
|
137
|
|
|
# TODO: Обработчик если загрузка не удалась =)
|
138
|
|
|
$filename = false;
|
139
|
|
|
}
|
140
|
|
|
}
|
141
|
|
|
else {
|
142
|
|
|
# TODO: вписать сообщение об ошибке.
|
143
|
|
|
# TODO: впрочем ещё надо и обработчик ошибок написать.
|
144
|
|
|
$filename = false;
|
145
|
|
|
}
|
146
|
|
|
|
147
|
|
|
if($filename) {
|
148
|
|
|
$images[] = $filename.".".$ext;
|
149
|
|
|
}
|
150
|
|
|
}
|
151
|
|
|
|
152
|
|
|
# return filenames array
|
153
|
|
|
return (count($images) > 0) ? $images : false ;
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/**
|
158
|
|
|
* Load images
|
159
|
|
|
*
|
160
|
|
|
* @param string $cond - image link condition
|
161
|
|
|
* @param int $from - start position for image load
|
162
|
|
|
* @param int $limit - limit for uploading
|
163
|
|
|
*
|
164
|
|
|
* @return array $data - data array.
|
165
|
|
|
*/
|
166
|
|
|
public function load_images($cond, $from = 0, $limit = 0) {
|
167
|
|
|
|
168
|
|
|
global $db;
|
169
|
|
|
|
170
|
|
|
$data = [];
|
171
|
|
|
|
172
|
|
|
$l = ($limit != 0) ? "LIMIT {$from},{$limit}" : "" ;
|
173
|
|
|
|
174
|
|
|
$q = $db->query("SELECT id, filename, fileext, sort, alt FROM ".IMAGES_TABLE." WHERE attachedto='{$cond}' ORDER BY sort ASC ".$l);
|
175
|
|
|
while($image = $db->fetch_assoc($q)) {
|
176
|
|
|
$image['original'] = $image['filename']."_original.".$image['fileext'];
|
177
|
|
|
$image['resize'] = $image['filename']."_resize.".$image['fileext'];
|
178
|
|
|
$image['thumb'] = $image['filename']."_thumb.".$image['fileext'];
|
179
|
|
|
|
180
|
|
|
$data[] = $image;
|
181
|
|
|
}
|
182
|
|
|
|
183
|
|
|
return $data;
|
184
|
|
|
}
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/**
|
188
|
|
|
* Upload image information to DB
|
189
|
|
|
*
|
190
|
|
|
* @param string $filename - filename without $pofix
|
191
|
|
|
* @param mixed $attached - file parent
|
192
|
|
|
* @param string $alt - alt-text
|
193
|
|
|
*/
|
194
|
|
|
public function insert_images($filename, $attached, $alt="") {
|
195
|
|
|
|
196
|
|
|
global $db, $logger;
|
197
|
|
|
|
198
|
|
|
$image = pathinfo($filename);
|
199
|
|
|
|
200
|
|
|
$db->query("INSERT INTO ".IMAGES_TABLE." (attachedto, filename, fileext, alt)
|
201
|
|
|
VALUES ('".$attached."', '".$image['filename']."', '".$image['extension']."', '".$alt."')");
|
202
|
|
|
|
203
|
|
|
# log
|
204
|
|
|
$logger->log("Изображение ".basename($filename)." успешно загружено на сервер");
|
205
|
|
|
}
|
206
|
|
|
|
207
|
|
|
|
208
|
|
|
/**
|
209
|
|
|
* Update image info in DB
|
210
|
|
|
*
|
211
|
|
|
* @param mixed $attachedto - attached link image
|
212
|
|
|
* @param int $id - image id
|
213
|
|
|
*/
|
214
|
|
|
public function update_images_info($attachedto, $id) {
|
215
|
|
|
|
216
|
|
|
global $db, $post, $img, $parse;
|
217
|
|
|
|
218
|
|
|
if(isset($post->sort) || isset($post->alt)) {
|
219
|
|
|
$sortimg = $img->load_images($attachedto."=".$id);
|
220
|
|
|
foreach($sortimg AS $v) {
|
221
|
|
|
|
222
|
|
|
$cond = [];
|
223
|
|
|
|
224
|
|
|
if(isset($post->sort[$v['id']]) && $post->sort[$v['id']] != $v['sort']) {
|
225
|
|
|
$cond[] = "sort='".$post->sort[$v['id']]."'";
|
226
|
|
|
}
|
227
|
|
|
|
228
|
|
|
if(!isset($post->alt[$v['id']])) {
|
229
|
|
|
$post->alt[$v['id']] = "";
|
230
|
|
|
}
|
231
|
|
|
|
232
|
|
|
if($post->alt[$v['id']] != $v['alt']) {
|
233
|
|
|
$cond[] = "alt='".$post->alt[$v['id']]."'";
|
234
|
|
|
}
|
235
|
|
|
|
236
|
|
|
if(!empty($cond)) {
|
237
|
|
|
# db query
|
238
|
|
|
$db->query("UPDATE ".IMAGES_TABLE." SET ".implode(", ", $cond)." WHERE id='".$v['id']."'");
|
239
|
|
|
}
|
240
|
|
|
}
|
241
|
|
|
}
|
242
|
|
|
}
|
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/**
|
246
|
|
|
* Delete image
|
247
|
|
|
*
|
248
|
|
|
* @param int|string $image - id or attachedto
|
249
|
|
|
* @param boolean $clwhere - type $image param
|
250
|
|
|
* false for id or attachedto
|
251
|
|
|
* true for another condition
|
252
|
|
|
*/
|
253
|
|
|
public function remove_images($image, $clwhere=false) {
|
254
|
|
|
|
255
|
|
|
global $db;
|
256
|
|
|
|
257
|
|
|
if(is_numeric($image) || is_integer($image)) {
|
258
|
|
|
$cond = " id='".$image."' ";
|
259
|
|
|
}
|
260
|
|
|
else {
|
261
|
|
|
$cond = " attachedto='".$image."' ";
|
262
|
|
|
}
|
263
|
|
|
|
264
|
|
|
if($clwhere) {
|
265
|
|
|
$cond = $image;
|
266
|
|
|
}
|
267
|
|
|
|
268
|
|
|
$q = $db->query("SELECT id, filename, fileext FROM ".IMAGES_TABLE." WHERE ".$cond);
|
269
|
|
|
while($row = $db->fetch_assoc($q)) {
|
270
|
|
|
if(!empty($row)) {
|
271
|
|
|
$original = $row['filename']."_original.".$row['fileext'];
|
272
|
|
|
$resize = $row['filename']."_resize.".$row['fileext'];
|
273
|
|
|
$thumb = $row['filename']."_thumb.".$row['fileext'];
|
274
|
|
|
|
275
|
|
|
# delete unique name
|
276
|
|
|
//$this->erase_image(_UPLOADIMAGES."/".$row['filename'].".".$row['fileext']);
|
277
|
|
|
|
278
|
|
|
# delete original
|
279
|
|
|
$this->erase_image(_UPLOADIMAGES."/".$original);
|
280
|
|
|
|
281
|
|
|
# delete resize
|
282
|
|
|
$this->erase_image(_UPLOADIMAGES."/".$resize);
|
283
|
|
|
|
284
|
|
|
# delete thumb
|
285
|
|
|
$this->erase_image(_UPLOADIMAGES."/".$thumb);
|
286
|
|
|
}
|
287
|
|
|
}
|
288
|
|
|
|
289
|
|
|
$db->query("DELETE FROM ".IMAGES_TABLE." WHERE ".$cond);
|
290
|
|
|
}
|
291
|
|
|
|
292
|
|
|
|
293
|
|
|
/**
|
294
|
|
|
* Erase image file
|
295
|
|
|
*
|
296
|
|
|
* @param $image
|
297
|
|
|
*/
|
298
|
|
|
public function erase_image($image) {
|
299
|
|
|
|
300
|
|
|
global $logger;
|
301
|
|
|
|
302
|
|
|
if(is_file($image)) {
|
303
|
|
|
unlink($image);
|
304
|
|
|
$logger->log("Изображение ".basename($image)." удалено");
|
305
|
|
|
}
|
306
|
|
|
else {
|
307
|
|
|
$logger->log("Не удалось найти изображение ".basename($image), "error");
|
308
|
|
|
}
|
309
|
|
|
}
|
310
|
|
|
|
311
|
|
|
|
312
|
|
|
/**
|
313
|
|
|
* This function checks the input parameters of width and height for generating small images.
|
314
|
|
|
*/
|
315
|
|
|
public function check_post_thumb_parametrs() {
|
316
|
|
|
|
317
|
|
|
global $post;
|
318
|
|
|
|
319
|
|
|
if(!isset($post->thumb_img_width)) {
|
320
|
|
|
$post->thumb_img_width = 0;
|
321
|
|
|
}
|
322
|
|
|
if(!isset($post->thumb_img_height)) {
|
323
|
|
|
$post->thumb_img_height = 0;
|
324
|
|
|
}
|
325
|
|
|
|
326
|
|
|
$post->thumb_img_width = round($post->thumb_img_width);
|
327
|
|
|
$post->thumb_img_height = round($post->thumb_img_height);
|
328
|
|
|
|
329
|
|
|
if($post->thumb_img_width < 16) {
|
330
|
|
|
$post->thumb_img_width = 0;
|
331
|
|
|
}
|
332
|
|
|
if($post->thumb_img_height < 16) {
|
333
|
|
|
$post->thumb_img_height = 0;
|
334
|
|
|
}
|
335
|
|
|
}
|
336
|
|
|
|
337
|
|
|
|
338
|
|
|
/**
|
339
|
|
|
* This function create an array of valid image extensions allowed for upload to server.
|
340
|
|
|
*
|
341
|
|
|
* @return mixed
|
342
|
|
|
*/
|
343
|
|
|
public function get_allow_images() {
|
344
|
|
|
$imagetype = [];
|
345
|
|
|
require _LIB."/mimetype.php";
|
346
|
|
|
|
347
|
|
|
$allow_exts = [];
|
348
|
|
|
foreach($imagetype AS $itype) {
|
349
|
|
|
$allow_exts[$itype['mime_type']] = $itype['ext'];
|
350
|
|
|
}
|
351
|
|
|
|
352
|
|
|
return $allow_exts;
|
353
|
|
|
}
|
354
|
|
|
}
|
355
|
|
|
|