|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Photo gallery |
|
4
|
|
|
* @category modules |
|
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi |
|
7
|
|
|
* @license MIT License, see license.txt |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace cs\modules\Photo_gallery; |
|
10
|
|
|
use |
|
11
|
|
|
cs\Cache\Prefix, |
|
12
|
|
|
cs\Config, |
|
13
|
|
|
cs\Event, |
|
14
|
|
|
cs\Language, |
|
15
|
|
|
cs\Storage, |
|
16
|
|
|
cs\Text, |
|
17
|
|
|
cs\User, |
|
18
|
|
|
cs\DB\Accessor, |
|
19
|
|
|
cs\Singleton, |
|
20
|
|
|
abeautifulsite\SimpleImage, |
|
21
|
|
|
Exception; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @method static $this instance($check = false) |
|
25
|
|
|
*/ |
|
26
|
|
|
class Photo_gallery { |
|
27
|
|
|
use |
|
28
|
|
|
Accessor, |
|
29
|
|
|
Singleton; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Prefix |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $cache; |
|
35
|
|
|
|
|
36
|
|
|
protected function construct () { |
|
37
|
|
|
$this->cache = new Prefix('Photo_gallery'); |
|
38
|
|
|
$module_data = Config::instance()->module('Photo_gallery'); |
|
39
|
|
|
if (!$module_data->directory_created) { |
|
40
|
|
|
$this->storage()->mkdir('Photo_gallery'); |
|
41
|
|
|
$module_data->directory_created = 1; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
/** |
|
45
|
|
|
* Returns database index |
|
46
|
|
|
* |
|
47
|
|
|
* @return int |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function cdb () { |
|
50
|
|
|
return Config::instance()->module('Photo_gallery')->db('galleries'); |
|
51
|
|
|
} |
|
52
|
|
|
/** |
|
53
|
|
|
* Storage instance |
|
54
|
|
|
* |
|
55
|
|
|
* @return \cs\False_class|Storage\_Abstract |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function storage () { |
|
58
|
|
|
return Storage::instance()->storage( |
|
59
|
|
|
Config::instance()->module('Photo_gallery')->storage('galleries') |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
/** |
|
63
|
|
|
* Get data of specified image |
|
64
|
|
|
* |
|
65
|
|
|
* @param int|int[] $id |
|
66
|
|
|
* |
|
67
|
|
|
* @return array|array[]|false |
|
68
|
|
|
*/ |
|
69
|
|
|
public function get ($id) { |
|
70
|
|
|
if (is_array($id)) { |
|
71
|
|
|
return array_map([$this, 'get'], $id); |
|
72
|
|
|
} |
|
73
|
|
|
$L = Language::instance(); |
|
74
|
|
|
$id = (int)$id; |
|
75
|
|
|
return $this->cache->get("images/$id/$L->clang", function () use ($id) { |
|
76
|
|
|
$data = $this->db()->qf( |
|
77
|
|
|
"SELECT |
|
78
|
|
|
`id`, |
|
79
|
|
|
`gallery`, |
|
80
|
|
|
`user`, |
|
81
|
|
|
`date`, |
|
82
|
|
|
`title`, |
|
83
|
|
|
`description`, |
|
84
|
|
|
`original`, |
|
85
|
|
|
`preview` |
|
86
|
|
|
FROM `[prefix]photo_gallery_images` |
|
87
|
|
|
WHERE `id` = '%s' |
|
88
|
|
|
LIMIT 1", |
|
89
|
|
|
$id |
|
90
|
|
|
); |
|
91
|
|
|
if ($data) { |
|
92
|
|
|
$data['title'] = $this->ml_process($data['title']); |
|
93
|
|
|
$data['description'] = $this->ml_process($data['description']); |
|
94
|
|
|
} |
|
95
|
|
|
return $data; |
|
96
|
|
|
}); |
|
97
|
|
|
} |
|
98
|
|
|
/** |
|
99
|
|
|
* Add new image |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $original Absolute path to original image |
|
102
|
|
|
* @param string $gallery Gallery id |
|
103
|
|
|
* @param string $title |
|
104
|
|
|
* @param string $description |
|
105
|
|
|
* |
|
106
|
|
|
* @return false|int Id of created post on success of <b>false</> on failure |
|
107
|
|
|
*/ |
|
108
|
|
|
public function add ($original, $gallery, $title = '', $description = '') { |
|
109
|
|
|
if (empty($original) || !$gallery) { |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
if (!filter_var($original, FILTER_VALIDATE_URL)) { |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
$gallery = (int)$gallery; |
|
116
|
|
|
if ($this->db_prime()->q( |
|
117
|
|
|
"INSERT INTO `[prefix]photo_gallery_images` |
|
118
|
|
|
( |
|
119
|
|
|
`gallery`, |
|
120
|
|
|
`user`, |
|
121
|
|
|
`date`, |
|
122
|
|
|
`original` |
|
123
|
|
|
) |
|
124
|
|
|
VALUES |
|
125
|
|
|
( |
|
126
|
|
|
'%s', |
|
127
|
|
|
'%s', |
|
128
|
|
|
'%s', |
|
129
|
|
|
'%s' |
|
130
|
|
|
)", |
|
131
|
|
|
$gallery, |
|
132
|
|
|
User::instance()->id, |
|
133
|
|
|
time(), |
|
134
|
|
|
$original |
|
135
|
|
|
)) { |
|
136
|
|
|
$id = $this->db_prime()->id(); |
|
137
|
|
|
if ($this->set($id, $title, $description)) { |
|
138
|
|
|
Event::instance()->fire( |
|
139
|
|
|
'System/upload_files/add_tag', |
|
140
|
|
|
[ |
|
141
|
|
|
'tag' => "Photo_gallery/images/$id", |
|
142
|
|
|
'url' => $original |
|
143
|
|
|
] |
|
144
|
|
|
); |
|
145
|
|
|
$hash = md5(random_bytes(1000)); |
|
146
|
|
|
$tmp_file = TEMP.'/'.User::instance()->id."_$hash"; |
|
147
|
|
|
try { |
|
148
|
|
|
$SimpleImage = new SimpleImage($original); |
|
149
|
|
|
$SimpleImage->thumbnail(256)->save($tmp_file = "$tmp_file.".$SimpleImage->get_original_info()['format']); |
|
150
|
|
|
unset($SimpleImage); |
|
151
|
|
|
} catch (Exception $e) { |
|
152
|
|
|
$this->del($id); |
|
153
|
|
|
trigger_error($e->getMessage(), E_USER_WARNING); |
|
154
|
|
|
return false; |
|
155
|
|
|
} |
|
156
|
|
|
$storage = $this->storage(); |
|
157
|
|
|
if (!$storage->file_exists("Photo_gallery/$gallery")) { |
|
158
|
|
|
$storage->mkdir("Photo_gallery/$gallery"); |
|
159
|
|
|
} |
|
160
|
|
|
$copy_result = $storage->copy($tmp_file, $new_file = "Photo_gallery/$gallery/{$id}_$hash"); |
|
161
|
|
|
unlink($tmp_file); |
|
162
|
|
|
if (!$copy_result) { |
|
163
|
|
|
$this->del($id); |
|
164
|
|
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
$this->db_prime()->q( |
|
167
|
|
|
"UPDATE `[prefix]photo_gallery_images` |
|
168
|
|
|
SET `preview` = '%s' |
|
169
|
|
|
WHERE `id` = '%s'", |
|
170
|
|
|
$storage->url_by_source($new_file), |
|
171
|
|
|
$id |
|
172
|
|
|
); |
|
173
|
|
|
unset( |
|
174
|
|
|
$new_file, |
|
175
|
|
|
$this->cache->{"galleries/$gallery"} |
|
176
|
|
|
); |
|
177
|
|
|
return $id; |
|
178
|
|
|
} else { |
|
179
|
|
|
$this->db_prime()->q( |
|
180
|
|
|
"DELETE FROM `[prefix]photo_gallery_images` |
|
181
|
|
|
WHERE `id` = $id" |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
return false; |
|
186
|
|
|
} |
|
187
|
|
|
/** |
|
188
|
|
|
* Set data of specified image |
|
189
|
|
|
* |
|
190
|
|
|
* @param int $id |
|
191
|
|
|
* @param string $title |
|
192
|
|
|
* @param string $description |
|
193
|
|
|
* |
|
194
|
|
|
* @return bool |
|
195
|
|
|
*/ |
|
196
|
|
|
public function set ($id, $title, $description) { |
|
197
|
|
|
$User = User::instance(); |
|
198
|
|
|
$id = (int)$id; |
|
199
|
|
|
$title = xap(trim($title)); |
|
200
|
|
|
$description = xap(trim($description)); |
|
201
|
|
|
if ($this->db_prime()->q( |
|
202
|
|
|
"UPDATE `[prefix]photo_gallery_images` |
|
203
|
|
|
SET |
|
204
|
|
|
`title` = '%s', |
|
205
|
|
|
`description` = '%s' |
|
206
|
|
|
WHERE |
|
207
|
|
|
`id` = '%s' AND |
|
208
|
|
|
( |
|
209
|
|
|
`user` = '%s' OR |
|
210
|
|
|
%d |
|
211
|
|
|
)", |
|
212
|
|
|
$title ? $this->ml_set('Photo_gallery/images/title', $id, $title) : '', |
|
213
|
|
|
$description ? $this->ml_set('Photo_gallery/images/description', $id, $description) : '', |
|
214
|
|
|
$id, |
|
215
|
|
|
$User->id, |
|
216
|
|
|
(int)$User->admin() |
|
217
|
|
|
)) { |
|
218
|
|
|
unset($this->cache->{"images/$id"}); |
|
219
|
|
|
return true; |
|
220
|
|
|
} |
|
221
|
|
|
return false; |
|
222
|
|
|
} |
|
223
|
|
|
/** |
|
224
|
|
|
* Delete specified image |
|
225
|
|
|
* |
|
226
|
|
|
* @param int $id |
|
227
|
|
|
* |
|
228
|
|
|
* @return bool |
|
229
|
|
|
*/ |
|
230
|
|
|
public function del ($id) { |
|
231
|
|
|
$id = (int)$id; |
|
232
|
|
|
$data = $this->get($id); |
|
233
|
|
|
if ($this->db_prime()->q( |
|
234
|
|
|
"DELETE FROM `[prefix]photo_gallery_images` |
|
235
|
|
|
WHERE `id` = $id" |
|
236
|
|
|
)) { |
|
237
|
|
|
$this->ml_del('Photo_gallery/images/title', $id); |
|
238
|
|
|
$this->ml_del('Photo_gallery/images/description', $id); |
|
239
|
|
|
Event::instance()->fire( |
|
240
|
|
|
'System/upload_files/del_tag', |
|
241
|
|
|
[ |
|
242
|
|
|
'tag' => "Photo_gallery/images/$id" |
|
243
|
|
|
] |
|
244
|
|
|
); |
|
245
|
|
|
if ($data['preview']) { |
|
246
|
|
|
$this->storage()->unlink($this->storage()->source_by_url($data['preview'])); |
|
247
|
|
|
} |
|
248
|
|
|
$Cache = $this->cache; |
|
249
|
|
|
unset( |
|
250
|
|
|
$Cache->{"images/$id"}, |
|
251
|
|
|
$Cache->{"galleries/$data[gallery]"} |
|
252
|
|
|
); |
|
253
|
|
|
return true; |
|
254
|
|
|
} else { |
|
255
|
|
|
return false; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
/** |
|
259
|
|
|
* Get array of galleries in form [<i>path</i> => <i>id</i>] |
|
260
|
|
|
* |
|
261
|
|
|
* @return array|false |
|
262
|
|
|
*/ |
|
263
|
|
|
public function get_galleries_list () { |
|
264
|
|
|
$L = Language::instance(); |
|
265
|
|
|
return $this->cache->get( |
|
266
|
|
|
"galleries/list/$L->clang", |
|
267
|
|
|
function () { |
|
268
|
|
|
$data = []; |
|
269
|
|
|
$galleries = $this->db()->qfas( |
|
270
|
|
|
'SELECT `id` |
|
271
|
|
|
FROM `[prefix]photo_gallery_galleries` |
|
272
|
|
|
WHERE `active` = 1 |
|
273
|
|
|
ORDER BY `order` ASC' |
|
274
|
|
|
); |
|
275
|
|
|
foreach ($galleries ?: [] as $gallery) { |
|
276
|
|
|
$gallery = $this->get_gallery($gallery); |
|
277
|
|
|
if (is_array($gallery)) { |
|
278
|
|
|
$data[$gallery['path']] = $gallery['id']; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
return $data; |
|
282
|
|
|
} |
|
283
|
|
|
); |
|
284
|
|
|
} |
|
285
|
|
|
/** |
|
286
|
|
|
* Get data of specified gallery |
|
287
|
|
|
* |
|
288
|
|
|
* @param int|int[] $id |
|
289
|
|
|
* |
|
290
|
|
|
* @return array|array[]|false |
|
291
|
|
|
*/ |
|
292
|
|
|
public function get_gallery ($id) { |
|
293
|
|
|
if (is_array($id)) { |
|
294
|
|
|
return array_map([$this, 'get_gallery'], $id); |
|
295
|
|
|
} |
|
296
|
|
|
$L = Language::instance(); |
|
297
|
|
|
$id = (int)$id; |
|
298
|
|
|
return $this->cache->get("galleries/$id/$L->clang", function () use ($id) { |
|
299
|
|
|
if ($data = $this->db()->qf( |
|
300
|
|
|
"SELECT |
|
301
|
|
|
`id`, |
|
302
|
|
|
`title`, |
|
303
|
|
|
`path`, |
|
304
|
|
|
`description`, |
|
305
|
|
|
`active`, |
|
306
|
|
|
`preview_image`, |
|
307
|
|
|
( |
|
308
|
|
|
SELECT COUNT(`id`) |
|
309
|
|
|
FROM `[prefix]photo_gallery_images` |
|
310
|
|
|
WHERE `gallery` = '%1\$s' |
|
311
|
|
|
) AS `images` |
|
312
|
|
|
FROM `[prefix]photo_gallery_galleries` |
|
313
|
|
|
WHERE `id` = '%1\$s' |
|
314
|
|
|
LIMIT 1", |
|
315
|
|
|
$id |
|
316
|
|
|
)) { |
|
317
|
|
|
$data['title'] = $this->ml_process($data['title']); |
|
318
|
|
|
$data['path'] = $this->ml_process($data['path']); |
|
319
|
|
|
$data['description'] = $this->ml_process($data['description']); |
|
320
|
|
|
$order = $data['preview_image'] == 'first' ? 'ASC' : 'DESC'; |
|
321
|
|
|
$data['preview'] = $this->db()->qfs( |
|
322
|
|
|
"SELECT `preview` |
|
323
|
|
|
FROM `[prefix]photo_gallery_images` |
|
324
|
|
|
WHERE `gallery` = $data[id] |
|
325
|
|
|
ORDER BY `id` $order" |
|
326
|
|
|
) ?: ''; |
|
327
|
|
|
$data['images'] = $this->db()->qfas( |
|
328
|
|
|
"SELECT `id` |
|
329
|
|
|
FROM `[prefix]photo_gallery_images` |
|
330
|
|
|
WHERE `gallery` = $data[id] |
|
331
|
|
|
ORDER BY `id` $order" |
|
332
|
|
|
) ?: []; |
|
333
|
|
|
} |
|
334
|
|
|
return $data; |
|
335
|
|
|
}); |
|
336
|
|
|
} |
|
337
|
|
|
/** |
|
338
|
|
|
* Add new gallery |
|
339
|
|
|
* |
|
340
|
|
|
* @param string $title |
|
341
|
|
|
* @param string $path |
|
342
|
|
|
* @param string $description |
|
343
|
|
|
* @param int $active |
|
344
|
|
|
* @param string $preview_image |
|
345
|
|
|
* |
|
346
|
|
|
* @return false|int Id of created gallery on success of <b>false</> on failure |
|
347
|
|
|
*/ |
|
348
|
|
|
public function add_gallery ($title, $path, $description, $active, $preview_image) { |
|
349
|
|
|
if ($this->db_prime()->q( |
|
350
|
|
|
"INSERT INTO `[prefix]photo_gallery_galleries` |
|
351
|
|
|
(`active`) |
|
352
|
|
|
VALUES |
|
353
|
|
|
('%s')", |
|
354
|
|
|
(int)(bool)$active |
|
355
|
|
|
)) { |
|
356
|
|
|
$id = $this->db_prime()->id(); |
|
357
|
|
|
$this->set_gallery($id, $title, $path, $description, $active, $preview_image); |
|
358
|
|
|
return $id; |
|
359
|
|
|
} |
|
360
|
|
|
return false; |
|
361
|
|
|
} |
|
362
|
|
|
/** |
|
363
|
|
|
* Set data of specified gallery |
|
364
|
|
|
* |
|
365
|
|
|
* @param int $id |
|
366
|
|
|
* @param string $title |
|
367
|
|
|
* @param string $path |
|
368
|
|
|
* @param string $description |
|
369
|
|
|
* @param int $active |
|
370
|
|
|
* @param string $preview_image |
|
371
|
|
|
* |
|
372
|
|
|
* @return bool |
|
373
|
|
|
*/ |
|
374
|
|
|
public function set_gallery ($id, $title, $path, $description, $active, $preview_image) { |
|
375
|
|
|
$path = path($path ?: $title); |
|
376
|
|
|
$title = xap(trim($title)); |
|
377
|
|
|
$description = xap(trim($description)); |
|
378
|
|
|
$id = (int)$id; |
|
379
|
|
|
if ($this->db_prime()->q( |
|
380
|
|
|
"UPDATE `[prefix]photo_gallery_galleries` |
|
381
|
|
|
SET |
|
382
|
|
|
`title` = '%s', |
|
383
|
|
|
`path` = '%s', |
|
384
|
|
|
`description` = '%s', |
|
385
|
|
|
`active` = '%s', |
|
386
|
|
|
`preview_image` = '%s' |
|
387
|
|
|
WHERE `id` = '%s'", |
|
388
|
|
|
$this->ml_set('Photo_gallery/galleries/title', $id, $title), |
|
389
|
|
|
$this->ml_set('Photo_gallery/galleries/path', $id, $path), |
|
390
|
|
|
$this->ml_set('Photo_gallery/galleries/description', $id, $description), |
|
391
|
|
|
(int)(bool)$active, |
|
392
|
|
|
$preview_image, |
|
393
|
|
|
$id |
|
394
|
|
|
)) { |
|
395
|
|
|
$Cache = $this->cache; |
|
396
|
|
|
unset( |
|
397
|
|
|
$Cache->{"galleries/$id"}, |
|
398
|
|
|
$Cache->{'galleries/list'} |
|
399
|
|
|
); |
|
400
|
|
|
return true; |
|
401
|
|
|
} else { |
|
402
|
|
|
return false; |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
/** |
|
406
|
|
|
* Delete specified gallery |
|
407
|
|
|
* |
|
408
|
|
|
* @param int $id |
|
409
|
|
|
* |
|
410
|
|
|
* @return bool |
|
411
|
|
|
*/ |
|
412
|
|
|
public function del_gallery ($id) { |
|
413
|
|
|
$id = (int)$id; |
|
414
|
|
|
if (!$this->db_prime()->q( |
|
415
|
|
|
"DELETE FROM `[prefix]photo_gallery_galleries` |
|
416
|
|
|
WHERE `id` = '%s'", |
|
417
|
|
|
$id |
|
418
|
|
|
)) { |
|
419
|
|
|
return false; |
|
420
|
|
|
} |
|
421
|
|
|
$this->ml_del('Photo_gallery/galleries/title', $id); |
|
422
|
|
|
$this->ml_del('Photo_gallery/galleries/path', $id); |
|
423
|
|
|
$this->ml_del('Photo_gallery/galleries/description', $id); |
|
424
|
|
|
$Cache = $this->cache; |
|
425
|
|
|
unset( |
|
426
|
|
|
$Cache->{"galleries/$id"}, |
|
427
|
|
|
$Cache->{'galleries/list'} |
|
428
|
|
|
); |
|
429
|
|
|
$images = $this->db()->qfas( |
|
430
|
|
|
"SELECT `id` |
|
431
|
|
|
FROM `[prefix]photo_gallery_images` |
|
432
|
|
|
WHERE `gallery` = '%s'", |
|
433
|
|
|
$id |
|
434
|
|
|
); |
|
435
|
|
|
if ($images) { |
|
436
|
|
|
foreach ((array)$images as $image) { |
|
437
|
|
|
$this->del($image); |
|
438
|
|
|
} |
|
439
|
|
|
} |
|
440
|
|
|
@$this->storage()->rmdir("Photo_gallery/$id"); |
|
441
|
|
|
return true; |
|
442
|
|
|
} |
|
443
|
|
|
private function ml_process ($text) { |
|
444
|
|
|
return Text::instance()->process($this->cdb(), $text); |
|
445
|
|
|
} |
|
446
|
|
|
private function ml_set ($group, $label, $text) { |
|
447
|
|
|
return Text::instance()->set($this->cdb(), $group, $label, $text); |
|
448
|
|
|
} |
|
449
|
|
|
private function ml_del ($group, $label) { |
|
450
|
|
|
return Text::instance()->del($this->cdb(), $group, $label); |
|
451
|
|
|
} |
|
452
|
|
|
} |
|
453
|
|
|
|