1 | <?php |
---|
2 | defined('BATCH_DOWNLOAD_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | class BatchDownloader |
---|
5 | { |
---|
6 | private $conf; |
---|
7 | private $data; |
---|
8 | private $images; |
---|
9 | |
---|
10 | /** |
---|
11 | * __construct |
---|
12 | * @param: mixed set id (##|'new') |
---|
13 | * @param: array images |
---|
14 | * @param: string set type (calendar,category,flat,tags,search,favorites,most_visited,best_rated,list,recent_pics,collection) |
---|
15 | * @param: mixed set type id (for calendar,category,tags,search,collection) |
---|
16 | * @param: string size to download |
---|
17 | */ |
---|
18 | function __construct($set_id, $images=array(), $type=null, $type_id=null, $size='original') |
---|
19 | { |
---|
20 | global $user, $conf; |
---|
21 | |
---|
22 | $this->conf = $conf['batch_download']; |
---|
23 | $this->data = array( |
---|
24 | 'id' => 0, |
---|
25 | 'user_id' => $user['id'], |
---|
26 | 'date_creation' => '0000-00-00 00:00:00', |
---|
27 | 'type' => null, |
---|
28 | 'type_id' => null, |
---|
29 | 'size' => 'original', |
---|
30 | 'nb_zip' => 0, |
---|
31 | 'last_zip' => 0, |
---|
32 | 'nb_images' => 0, |
---|
33 | 'total_size' => 0, |
---|
34 | 'status' => 'new', |
---|
35 | 'estimated_total_size' => 0, // not in db |
---|
36 | ); |
---|
37 | $this->images = array(); |
---|
38 | |
---|
39 | // load specific set |
---|
40 | if (preg_match('#^[0-9]+$#', $set_id)) |
---|
41 | { |
---|
42 | $query = ' |
---|
43 | SELECT * |
---|
44 | FROM '.BATCH_DOWNLOAD_TSETS.' |
---|
45 | WHERE id = '.$set_id.' |
---|
46 | '.(!is_admin() ? 'AND user_id = '.$this->data['user_id'] : null).' |
---|
47 | ;'; |
---|
48 | $result = pwg_query($query); |
---|
49 | |
---|
50 | if (pwg_db_num_rows($result)) |
---|
51 | { |
---|
52 | $this->data = array_merge( |
---|
53 | $this->data, |
---|
54 | pwg_db_fetch_assoc($result) |
---|
55 | ); |
---|
56 | |
---|
57 | // make sure all pictures of the set exist |
---|
58 | $query = ' |
---|
59 | DELETE FROM '.BATCH_DOWNLOAD_TIMAGES.' |
---|
60 | WHERE image_id NOT IN ( |
---|
61 | SELECT id FROM '.IMAGES_TABLE.' |
---|
62 | ) |
---|
63 | ;'; |
---|
64 | pwg_query($query); |
---|
65 | |
---|
66 | $query = ' |
---|
67 | SELECT * |
---|
68 | FROM '.BATCH_DOWNLOAD_TIMAGES.' |
---|
69 | WHERE set_id = '.$this->data['id'].' |
---|
70 | ;'; |
---|
71 | $this->images = simple_hash_from_query($query, 'image_id', 'zip'); |
---|
72 | |
---|
73 | if ( $this->data['status'] != 'done' and count($this->images) != $this->data['nb_images'] ) |
---|
74 | { |
---|
75 | $this->updateParam('nb_images', count($this->images)); |
---|
76 | } |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | throw new Exception(l10n('Invalid dowload set')); |
---|
81 | } |
---|
82 | } |
---|
83 | // create a new set |
---|
84 | else if ($set_id == 'new') |
---|
85 | { |
---|
86 | if ($size != 'original') |
---|
87 | { |
---|
88 | $types = array_keys(ImageStdParams::get_defined_type_map()); |
---|
89 | if (!in_array($size, $types)) |
---|
90 | { |
---|
91 | throw new Exception(sprintf(l10n('Invalid size %s'), $size)); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | $this->data['type'] = $type; |
---|
96 | $this->data['type_id'] = $type_id; |
---|
97 | $this->data['size'] = $size; |
---|
98 | |
---|
99 | $query = ' |
---|
100 | INSERT INTO '.BATCH_DOWNLOAD_TSETS.'( |
---|
101 | user_id, |
---|
102 | date_creation, |
---|
103 | type, |
---|
104 | type_id, |
---|
105 | size, |
---|
106 | nb_zip, |
---|
107 | last_zip, |
---|
108 | nb_images, |
---|
109 | total_size, |
---|
110 | status |
---|
111 | ) |
---|
112 | VALUES( |
---|
113 | '.$this->data['user_id'].', |
---|
114 | NOW(), |
---|
115 | "'.$this->data['type'].'", |
---|
116 | "'.$this->data['type_id'].'", |
---|
117 | "'.$this->data['size'].'", |
---|
118 | 0, |
---|
119 | 0, |
---|
120 | 0, |
---|
121 | 0, |
---|
122 | "new" |
---|
123 | ) |
---|
124 | ;'; |
---|
125 | pwg_query($query); |
---|
126 | $this->data['id'] = pwg_db_insert_id(); |
---|
127 | |
---|
128 | $date = pwg_query('SELECT FROM_UNIXTIME(NOW());'); |
---|
129 | list($this->data['date_creation']) = pwg_db_fetch_row($date); |
---|
130 | |
---|
131 | if (!empty($images)) |
---|
132 | { |
---|
133 | $this->addImages($images); |
---|
134 | } |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | trigger_error('BatchDownloader::__construct, invalid input parameter', E_USER_ERROR); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * updateParam |
---|
144 | * @param: string param name |
---|
145 | * @param: mixed param value |
---|
146 | */ |
---|
147 | function updateParam($name, $value) |
---|
148 | { |
---|
149 | $this->data[$name] = $value; |
---|
150 | pwg_query('UPDATE '.BATCH_DOWNLOAD_TSETS.' SET '.$name.' = "'.$value.'" WHERE id = '.$this->data['id'].';'); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * getParam |
---|
155 | * @param: string param name |
---|
156 | * @return: mixed param value |
---|
157 | */ |
---|
158 | function getParam($name) |
---|
159 | { |
---|
160 | return $this->data[$name]; |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * getImages |
---|
165 | * @return: array(image_id => zip_idx) |
---|
166 | */ |
---|
167 | function getImages() |
---|
168 | { |
---|
169 | return $this->images; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * isInSet |
---|
174 | * @param: int image id |
---|
175 | * @return: bool |
---|
176 | */ |
---|
177 | function isInSet($image_id) |
---|
178 | { |
---|
179 | return array_key_exists($image_id, $this->images); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * removeImages |
---|
184 | * @param: array image ids |
---|
185 | */ |
---|
186 | function removeImages($image_ids) |
---|
187 | { |
---|
188 | if (empty($image_ids) or !is_array($image_ids)) return; |
---|
189 | |
---|
190 | foreach ($image_ids as $image_id) |
---|
191 | { |
---|
192 | unset($this->images[ $image_id ]); |
---|
193 | } |
---|
194 | |
---|
195 | $query = ' |
---|
196 | DELETE FROM '.BATCH_DOWNLOAD_TIMAGES.' |
---|
197 | WHERE |
---|
198 | set_id = '.$this->data['id'].' |
---|
199 | AND image_id IN('.implode(',', $image_ids).') |
---|
200 | ;'; |
---|
201 | pwg_query($query); |
---|
202 | |
---|
203 | $this->updateParam('nb_images', count($this->images)); |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * addImages |
---|
208 | * @param: array image ids |
---|
209 | */ |
---|
210 | function addImages($image_ids) |
---|
211 | { |
---|
212 | if (empty($image_ids) or !is_array($image_ids)) return; |
---|
213 | |
---|
214 | $query = ' |
---|
215 | SELECT id, file |
---|
216 | FROM '.IMAGES_TABLE.' |
---|
217 | WHERE id IN('.implode(',', array_unique($image_ids)).') |
---|
218 | ;'; |
---|
219 | $images = simple_hash_from_query($query, 'id', 'file'); |
---|
220 | |
---|
221 | $inserts = array(); |
---|
222 | |
---|
223 | foreach ($images as $image_id => $file) |
---|
224 | { |
---|
225 | if ($this->isInSet($image_id)) continue; |
---|
226 | if (!in_array(get_extension($file), $this->conf['allowed_ext'])) continue; |
---|
227 | |
---|
228 | $this->images[ $image_id ] = 0; |
---|
229 | $inserts[] = array( |
---|
230 | 'set_id' => $this->data['id'], |
---|
231 | 'image_id' => $image_id, |
---|
232 | 'zip' => 0, |
---|
233 | ); |
---|
234 | } |
---|
235 | |
---|
236 | if (count($inserts)) |
---|
237 | { |
---|
238 | mass_inserts( |
---|
239 | BATCH_DOWNLOAD_TIMAGES, |
---|
240 | array('set_id','image_id','zip'), |
---|
241 | $inserts |
---|
242 | ); |
---|
243 | } |
---|
244 | |
---|
245 | $this->updateParam('nb_images', count($this->images)); |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * clearImages |
---|
250 | */ |
---|
251 | function clearImages() |
---|
252 | { |
---|
253 | $this->images = array(); |
---|
254 | |
---|
255 | $query = ' |
---|
256 | DELETE FROM '.BATCH_DOWNLOAD_TIMAGES.' |
---|
257 | WHERE set_id = '.$this->data['id'].' |
---|
258 | ;'; |
---|
259 | pwg_query($query); |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * getMissingDerivatives |
---|
264 | * @param: bool update, if true, update IMAGE_SIZES with FS datas |
---|
265 | * @return: array of i.php urls |
---|
266 | */ |
---|
267 | function getMissingDerivatives($update=false) |
---|
268 | { |
---|
269 | if ($this->data['size'] == 'original') |
---|
270 | { |
---|
271 | return array(); |
---|
272 | } |
---|
273 | |
---|
274 | $root_url = get_root_url(); |
---|
275 | $uid = '&b='.time(); |
---|
276 | |
---|
277 | $params = ImageStdParams::get_by_type($this->data['size']); |
---|
278 | $last_mod_time = $params->last_mod_time; |
---|
279 | |
---|
280 | $image_ids = array_keys($this->images); |
---|
281 | $to_update = $urls = $inserts = array(); |
---|
282 | |
---|
283 | global $conf; |
---|
284 | $conf['old_question_mark_in_urls'] = $conf['question_mark_in_urls']; |
---|
285 | $conf['old_php_extension_in_urls'] = $conf['php_extension_in_urls']; |
---|
286 | $conf['old_derivative_url_style'] = $conf['derivative_url_style']; |
---|
287 | $conf['question_mark_in_urls'] = $conf['php_extension_in_urls'] = true; |
---|
288 | $conf['derivative_url_style'] = 2; |
---|
289 | |
---|
290 | // images which we need to update stats |
---|
291 | if ($update) |
---|
292 | { |
---|
293 | $query = ' |
---|
294 | SELECT image_id, filemtime |
---|
295 | FROM '.IMAGE_SIZES_TABLE.' |
---|
296 | WHERE image_id IN('.implode(',', $image_ids).') |
---|
297 | AND type = "'.$this->data['size'].'" |
---|
298 | ;'; |
---|
299 | $registered = array_from_query($query, 'image_id', 'filemtime'); |
---|
300 | |
---|
301 | $to_update = array_filter($registered, create_function('$t', 'return $t<'.$last_mod_time.';')); |
---|
302 | $to_update = array_merge($to_update, array_diff($image_ids, $registered)); |
---|
303 | } |
---|
304 | |
---|
305 | $query = ' |
---|
306 | SELECT id, path, width, height, rotation |
---|
307 | FROM '.IMAGES_TABLE.' |
---|
308 | WHERE id IN('.implode(',', $image_ids).') |
---|
309 | ORDER BY id DESC |
---|
310 | ;'; |
---|
311 | |
---|
312 | $result = pwg_query($query); |
---|
313 | while ($row = pwg_db_fetch_assoc($result)) |
---|
314 | { |
---|
315 | $src_image = new SrcImage($row); // don't give representive_ext |
---|
316 | |
---|
317 | // no-image files |
---|
318 | if ($src_image->is_mimetype()) |
---|
319 | { |
---|
320 | if ($update && in_array($row['id'], $to_update)) |
---|
321 | { |
---|
322 | $inserts[ $row['id'] ] = array( |
---|
323 | 'image_id' => $row['id'], |
---|
324 | 'type' => $this->data['size'], |
---|
325 | 'width' => 0, |
---|
326 | 'height' => 0, |
---|
327 | 'filesize' => filesize(PHPWG_ROOT_PATH.$row['path'])/1024, |
---|
328 | 'filemtime' => filemtime(PHPWG_ROOT_PATH.$row['path']), |
---|
329 | ); |
---|
330 | } |
---|
331 | } |
---|
332 | // images files |
---|
333 | else |
---|
334 | { |
---|
335 | $derivative = new DerivativeImage($this->data['size'], $src_image); |
---|
336 | |
---|
337 | $filemtime = @filemtime($derivative->get_path()); |
---|
338 | $src_mtime = @filemtime(PHPWG_ROOT_PATH.$row['path']); |
---|
339 | if ($src_mtime===false) continue; |
---|
340 | |
---|
341 | if ($filemtime===false || $filemtime<$last_mod_time || $filemtime<$src_mtime) |
---|
342 | { |
---|
343 | $urls[] = $root_url.$derivative->get_url().$uid; |
---|
344 | } |
---|
345 | else if ($update && in_array($row['id'], $to_update)) |
---|
346 | { |
---|
347 | $imagesize = getimagesize($derivative->get_path()); |
---|
348 | |
---|
349 | $inserts[ $row['id'] ] = array( |
---|
350 | 'image_id' => $row['id'], |
---|
351 | 'type' => $this->data['size'], |
---|
352 | 'width' => $imagesize[0], |
---|
353 | 'height' => $imagesize[1], |
---|
354 | 'filesize' => filesize($derivative->get_path())/1024, |
---|
355 | 'filemtime' => $filemtime, |
---|
356 | ); |
---|
357 | } |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | if (!empty($inserts)) |
---|
362 | { |
---|
363 | $query = ' |
---|
364 | DELETE FROM '.IMAGE_SIZES_TABLE.' |
---|
365 | WHERE image_id IN('.implode(',', array_keys($inserts)).') |
---|
366 | ;'; |
---|
367 | pwg_query($query); |
---|
368 | |
---|
369 | mass_inserts( |
---|
370 | IMAGE_SIZES_TABLE, |
---|
371 | array('image_id','type','width','height','filesize','filemtime'), |
---|
372 | $inserts |
---|
373 | ); |
---|
374 | } |
---|
375 | |
---|
376 | $conf['question_mark_in_urls'] = $conf['old_question_mark_in_urls']; |
---|
377 | $conf['php_extension_in_urls'] = $conf['old_php_extension_in_urls']; |
---|
378 | $conf['derivative_url_style'] = $conf['old_derivative_url_style']; |
---|
379 | |
---|
380 | return $urls; |
---|
381 | } |
---|
382 | |
---|
383 | /** |
---|
384 | * deleteLastArchive |
---|
385 | */ |
---|
386 | function deleteLastArchive() |
---|
387 | { |
---|
388 | $zip_path = $this->getArchivePath(); |
---|
389 | if (file_exists($zip_path)) |
---|
390 | { |
---|
391 | unlink($zip_path); |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | /** |
---|
396 | * createNextArchive |
---|
397 | * @param: bool force all elements in one archive |
---|
398 | * @return: string zip path or false |
---|
399 | */ |
---|
400 | function createNextArchive($force_one_archive=false) |
---|
401 | { |
---|
402 | // set already downloaded (we should never be there !) |
---|
403 | if ( $this->data['status'] == 'done' or $this->data['nb_images'] == 0 ) |
---|
404 | { |
---|
405 | trigger_error('BatchDownloader::createNextArchive, the set is empty', E_USER_ERROR); |
---|
406 | } |
---|
407 | |
---|
408 | global $conf; |
---|
409 | |
---|
410 | // first zip |
---|
411 | if ($this->data['last_zip'] == 0) |
---|
412 | { |
---|
413 | $this->updateParam('status', 'download'); |
---|
414 | |
---|
415 | // limit number of elements |
---|
416 | if ($this->data['nb_images'] > $this->conf['max_elements']) |
---|
417 | { |
---|
418 | $images_ids = array_slice(array_keys($this->images), 0, $this->conf['max_elements']); |
---|
419 | $this->clearImages(); |
---|
420 | $this->addImages($images_ids); |
---|
421 | } |
---|
422 | |
---|
423 | $this->getEstimatedArchiveNumber(true); |
---|
424 | |
---|
425 | $this->updateParam('date_creation', date('Y-m-d H:i:s')); |
---|
426 | } |
---|
427 | |
---|
428 | // get next images of the set |
---|
429 | $images_to_add = array(); |
---|
430 | foreach ($this->images as $image_id => $zip_id) |
---|
431 | { |
---|
432 | if ($zip_id != 0) continue; // here are already added images |
---|
433 | array_push($images_to_add, $image_id); |
---|
434 | } |
---|
435 | |
---|
436 | if (count($images_to_add)) |
---|
437 | { |
---|
438 | $query = ' |
---|
439 | SELECT |
---|
440 | id, name, file, path, |
---|
441 | rotation, filesize, width, height |
---|
442 | FROM '.IMAGES_TABLE.' |
---|
443 | WHERE id IN ('.implode(',', $images_to_add).') |
---|
444 | ;'; |
---|
445 | $images_to_add = hash_from_query($query, 'id'); |
---|
446 | |
---|
447 | if ($this->data['size'] != 'original') |
---|
448 | { |
---|
449 | $query = ' |
---|
450 | SELECT image_id, filesize, width, height |
---|
451 | FROM '.IMAGE_SIZES_TABLE.' |
---|
452 | WHERE image_id IN ('.implode(',', array_keys($images_to_add)).') |
---|
453 | AND type = "'.$this->data['size'].'" |
---|
454 | ;'; |
---|
455 | $filesizes = hash_from_query($query, 'image_id'); |
---|
456 | } |
---|
457 | |
---|
458 | // open zip |
---|
459 | $this->updateParam('last_zip', $this->data['last_zip']+1); |
---|
460 | $zip_path = $this->getArchivePath(); |
---|
461 | $zip = new myZip($zip_path, isset($conf['batch_download_force_pclzip'])); |
---|
462 | |
---|
463 | // add images until size limit is reach, or all images are added |
---|
464 | $images_added = array(); |
---|
465 | $total_size = 0; |
---|
466 | foreach ($images_to_add as $row) |
---|
467 | { |
---|
468 | if (!file_exists(PHPWG_ROOT_PATH.$row['path'])) |
---|
469 | { |
---|
470 | $this->removeImages(array($row['id'])); |
---|
471 | continue; |
---|
472 | } |
---|
473 | |
---|
474 | $filename = $row['id'].'_'.stripslashes(get_filename_wo_extension($row['file'])); |
---|
475 | |
---|
476 | if ($this->data['size'] == 'original') |
---|
477 | { |
---|
478 | $filename.= '_'.$row['width'].'x'.$row['height']; |
---|
479 | $zip->addFile(PHPWG_ROOT_PATH.$row['path'], $filename.'.'.get_extension($row['path'])); |
---|
480 | $total_size+= $row['filesize']; |
---|
481 | } |
---|
482 | else |
---|
483 | { |
---|
484 | $src_image = new SrcImage($row); // don't give representive_ext |
---|
485 | |
---|
486 | // no-image files |
---|
487 | if ($src_image->is_mimetype()) |
---|
488 | { |
---|
489 | $zip->addFile(PHPWG_ROOT_PATH.$row['path'], $filename.'.'.get_extension($row['path'])); |
---|
490 | $total_size+= $row['filesize']; |
---|
491 | } |
---|
492 | // images files |
---|
493 | else |
---|
494 | { |
---|
495 | $derivative = new DerivativeImage($this->data['size'], $src_image); |
---|
496 | $path = $derivative->get_path(); |
---|
497 | |
---|
498 | $filename.= '_'.$filesizes[ $row['id'] ]['width'].'x'.$filesizes[ $row['id'] ]['height']; |
---|
499 | $zip->addFile($path, $filename.'.'.get_extension($path)); |
---|
500 | $total_size+= $filesizes[ $row['id'] ]['filesize']; |
---|
501 | } |
---|
502 | } |
---|
503 | |
---|
504 | array_push($images_added, $row['id']); |
---|
505 | $this->images[ $row['id'] ] = $this->data['last_zip']; |
---|
506 | |
---|
507 | if ($total_size >= $this->conf['max_size']*1024 and !$force_one_archive) break; |
---|
508 | } |
---|
509 | |
---|
510 | $this->updateParam('total_size', $this->data['total_size'] + $total_size); |
---|
511 | |
---|
512 | // archive comment |
---|
513 | $comment = 'Generated on '.date('r').' with PHP '.PHP_VERSION.' by Piwigo Batch Downloader.'; |
---|
514 | $comment.= "\n".$conf['gallery_title'].' - '.get_absolute_root_url(); |
---|
515 | if (!empty($conf['batch_download_comment'])) |
---|
516 | { |
---|
517 | $comment.= "\n\n".wordwrap(remove_accents($conf['batch_download_comment']), 60); |
---|
518 | } |
---|
519 | $zip->setArchiveComment($comment); |
---|
520 | |
---|
521 | $zip->close(); |
---|
522 | |
---|
523 | // update database |
---|
524 | $query = ' |
---|
525 | UPDATE '.BATCH_DOWNLOAD_TIMAGES.' |
---|
526 | SET zip = '.$this->data['last_zip'].' |
---|
527 | WHERE |
---|
528 | set_id = '.$this->data['id'].' |
---|
529 | AND image_id IN('.implode(',', $images_added).') |
---|
530 | ;'; |
---|
531 | pwg_query($query); |
---|
532 | |
---|
533 | // all images added ? |
---|
534 | if (count($images_to_add) == count($images_added)) |
---|
535 | { |
---|
536 | $this->updateParam('status', 'done'); |
---|
537 | } |
---|
538 | |
---|
539 | // over estimed |
---|
540 | if ($this->data['status'] == 'done') |
---|
541 | { |
---|
542 | $this->updateParam('nb_zip', $this->data['last_zip']); |
---|
543 | } |
---|
544 | // under estimed |
---|
545 | else if ($this->data['last_zip'] == $this->data['nb_zip']) |
---|
546 | { |
---|
547 | $this->updateParam('nb_zip', $this->data['last_zip']+1); |
---|
548 | } |
---|
549 | |
---|
550 | return $zip_path; |
---|
551 | } |
---|
552 | else |
---|
553 | { |
---|
554 | return false; |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | /** |
---|
559 | * getEstimatedTotalSize |
---|
560 | * @return: int |
---|
561 | */ |
---|
562 | function getEstimatedTotalSize($force=false) |
---|
563 | { |
---|
564 | if ($this->data['status'] == 'done') return $this->data['total_size']; |
---|
565 | if ($this->data['nb_images'] == 0) return 0; |
---|
566 | if ( !empty($this->data['estimated_total_size']) and !$force ) return $this->data['estimated_total_size']; |
---|
567 | |
---|
568 | $image_ids = array_slice(array_keys($this->images), 0, $this->conf['max_elements']); |
---|
569 | |
---|
570 | if ($this->data['size'] == 'original') |
---|
571 | { |
---|
572 | $query = ' |
---|
573 | SELECT SUM(filesize) AS total |
---|
574 | FROM '.IMAGES_TABLE.' |
---|
575 | WHERE id IN ('.implode(',', $image_ids).') |
---|
576 | ;'; |
---|
577 | } |
---|
578 | else |
---|
579 | { |
---|
580 | $query = ' |
---|
581 | SELECT SUM(filesize) AS total |
---|
582 | FROM '.IMAGE_SIZES_TABLE.' |
---|
583 | WHERE image_id IN ('.implode(',', $image_ids).') |
---|
584 | ;'; |
---|
585 | } |
---|
586 | |
---|
587 | list($this->data['estimated_total_size']) = pwg_db_fetch_row(pwg_query($query)); |
---|
588 | return $this->data['estimated_total_size']; |
---|
589 | } |
---|
590 | |
---|
591 | /** |
---|
592 | * getEstimatedArchiveNumber |
---|
593 | * @return: int |
---|
594 | */ |
---|
595 | function getEstimatedArchiveNumber($force=false) |
---|
596 | { |
---|
597 | if ($this->data['status'] == 'done') return $this->data['nb_zip']; |
---|
598 | if ( !empty($this->data['nb_zip']) and !$force ) return $this->data['nb_zip']; |
---|
599 | |
---|
600 | $this->updateParam('nb_zip', ceil( $this->getEstimatedTotalSize($force) / ($this->conf['max_size']*1024) )); |
---|
601 | return $this->data['nb_zip']; |
---|
602 | } |
---|
603 | |
---|
604 | /** |
---|
605 | * getDownloadList |
---|
606 | * @return: string html |
---|
607 | */ |
---|
608 | function getDownloadList($url='') |
---|
609 | { |
---|
610 | if ($this->data['nb_images'] == 0) |
---|
611 | { |
---|
612 | return '<b>'.l10n('No archive').'</b>'; |
---|
613 | } |
---|
614 | |
---|
615 | $root_url = get_root_url(); |
---|
616 | |
---|
617 | $out = ''; |
---|
618 | for ($i=1; $i<=$this->getEstimatedArchiveNumber(); $i++) |
---|
619 | { |
---|
620 | $out.= '<li id="zip-'.$i.'">'; |
---|
621 | |
---|
622 | if ($this->data['status'] == 'done' or $i < $this->data['last_zip']+1) |
---|
623 | { |
---|
624 | $out.= '<img src="'.$root_url.BATCH_DOWNLOAD_PATH.'template/images/drive_error.png"> '.sprintf(l10n('Archive #%d (already downloaded)'), $i); |
---|
625 | } |
---|
626 | else if ($i == $this->data['last_zip']+1) |
---|
627 | { |
---|
628 | $out.= '<a href="'.add_url_params($url, array('set_id'=>$this->data['id'],'zip'=>$i)).'" rel="nofollow" style="font-weight:bold;"' |
---|
629 | .($i!=1 ? ' onClick="return confirm(\''.addslashes(sprintf(l10n('Starting download Archive #%d will destroy Archive #%d, be sure you finish the download. Continue ?'), $i, $i-1)).'\');"' : null). |
---|
630 | '><img src="'.$root_url.BATCH_DOWNLOAD_PATH.'template/images/drive_go.png"> '.sprintf(l10n('Archive #%d (ready)'), $i).'</a>'; |
---|
631 | } |
---|
632 | else |
---|
633 | { |
---|
634 | $out.= '<img src="'.$root_url.BATCH_DOWNLOAD_PATH.'template/images/drive.png"> '.sprintf(l10n('Archive #%d (pending)'), $i); |
---|
635 | } |
---|
636 | |
---|
637 | $out.= '</li>'; |
---|
638 | } |
---|
639 | |
---|
640 | return $out; |
---|
641 | } |
---|
642 | |
---|
643 | /** |
---|
644 | * getArchivePath |
---|
645 | * @param: int archive number |
---|
646 | * @return: string |
---|
647 | */ |
---|
648 | function getArchivePath($i=null) |
---|
649 | { |
---|
650 | if (!file_exists(BATCH_DOWNLOAD_LOCAL . 'u-' .$this->data['user_id']. '/')) |
---|
651 | { |
---|
652 | mkgetdir(BATCH_DOWNLOAD_LOCAL . 'u-' .$this->data['user_id']. '/'); |
---|
653 | } |
---|
654 | |
---|
655 | if ($i === null) $i = $this->data['last_zip']; |
---|
656 | $set = $this->getNames(); |
---|
657 | |
---|
658 | $path = BATCH_DOWNLOAD_LOCAL . 'u-'. $this->data['user_id'] . '/'; |
---|
659 | $path.= !empty($this->conf['archive_prefix']) ? $this->conf['archive_prefix'] . '_' : null; |
---|
660 | $path.= $set['BASENAME'] . '_' . $this->data['size'] . '_'; |
---|
661 | $path.= $this->data['user_id'] . $this->data['id']; |
---|
662 | $path.= '_part' . $i . '.zip'; |
---|
663 | |
---|
664 | return $path; |
---|
665 | } |
---|
666 | |
---|
667 | /** |
---|
668 | * getNames |
---|
669 | * @return: array |
---|
670 | * NAME, set name with HTML |
---|
671 | * sNAME, set name without HTML |
---|
672 | * BASENAME, set filename |
---|
673 | * COMMENT, set comment |
---|
674 | */ |
---|
675 | function getNames() |
---|
676 | { |
---|
677 | switch ($this->data['type']) |
---|
678 | { |
---|
679 | // calendar |
---|
680 | case 'calendar': |
---|
681 | { |
---|
682 | global $conf, $page; |
---|
683 | $old_page = $page; |
---|
684 | |
---|
685 | $fields = array( |
---|
686 | 'created' => l10n('Creation date'), |
---|
687 | 'posted' => l10n('Post date'), |
---|
688 | ); |
---|
689 | |
---|
690 | $chronology = explode('-', $this->data['type_id']); |
---|
691 | $page['chronology_field'] = $chronology[0]; |
---|
692 | $page['chronology_style'] = $chronology[1]; |
---|
693 | $page['chronology_view'] = $chronology[2]; |
---|
694 | $page['chronology_date'] = array_splice($chronology, 3); |
---|
695 | |
---|
696 | if (!class_exists('Calendar')) |
---|
697 | { |
---|
698 | include_once(PHPWG_ROOT_PATH.'include/calendar_'. $page['chronology_style'] .'.class.php'); |
---|
699 | } |
---|
700 | $calendar = new Calendar(); |
---|
701 | $calendar->initialize(''); |
---|
702 | $display_name = strip_tags($calendar->get_display_name()); |
---|
703 | |
---|
704 | $set['NAME'] = l10n('Calendar').': '.$fields[$page['chronology_field']].$display_name; |
---|
705 | $set['sNAME'] = l10n('Calendar').': '.ltrim($display_name, $conf['level_separator']); |
---|
706 | $set['BASENAME'] = 'calendar-'.$page['chronology_field'].'-'.implode('-',$page['chronology_date']); |
---|
707 | |
---|
708 | $page = $old_page; |
---|
709 | break; |
---|
710 | } |
---|
711 | |
---|
712 | // category |
---|
713 | case 'category': |
---|
714 | { |
---|
715 | $category = get_cat_info($this->data['type_id']); |
---|
716 | if ($category == null) |
---|
717 | { |
---|
718 | $set['NAME'] = l10n('Album').': #'.$this->data['type_id'].' (deleted)'; |
---|
719 | $set['BASENAME'] = 'album'.$this->data['type_id']; |
---|
720 | } |
---|
721 | else |
---|
722 | { |
---|
723 | $set['NAME'] = l10n('Album').': '.get_cat_display_name($category['upper_names']); |
---|
724 | $set['sNAME'] = l10n('Album').': '.trigger_event('render_category_name', $category['name']); |
---|
725 | $set['COMMENT'] = trigger_event('render_category_description', $category['comment']); |
---|
726 | |
---|
727 | if (!empty($category['permalink'])) |
---|
728 | { |
---|
729 | $set['BASENAME'] = 'album-'.$category['permalink']; |
---|
730 | } |
---|
731 | else if ( ($name = str2url($category['name'])) != null ) |
---|
732 | { |
---|
733 | $set['BASENAME'] = 'album-'.$name; |
---|
734 | } |
---|
735 | else |
---|
736 | { |
---|
737 | $set['BASENAME'] = 'album'.$this->data['type_id']; |
---|
738 | } |
---|
739 | } |
---|
740 | break; |
---|
741 | } |
---|
742 | |
---|
743 | // flat |
---|
744 | case 'flat': |
---|
745 | { |
---|
746 | $set['NAME'] = l10n('Whole gallery'); |
---|
747 | $set['BASENAME'] = 'all-gallery'; |
---|
748 | break; |
---|
749 | } |
---|
750 | |
---|
751 | // tags |
---|
752 | case 'tags': |
---|
753 | { |
---|
754 | $tags = find_tags(explode(',', $this->data['type_id'])); |
---|
755 | $set['NAME'] = l10n('Tags').': '; |
---|
756 | $set['BASENAME'] = 'tags'; |
---|
757 | |
---|
758 | $first = true; |
---|
759 | foreach ($tags as $tag) |
---|
760 | { |
---|
761 | if ($first) $first = false; |
---|
762 | else $set['NAME'].= ', '; |
---|
763 | $set['NAME'].= |
---|
764 | '<a href="' . make_index_url(array('tags'=>array($tag))) . '">' |
---|
765 | .trigger_event('render_tag_name', $tag['name']) |
---|
766 | .'</a>'; |
---|
767 | $set['BASENAME'].= '-'.$tag['url_name']; |
---|
768 | } |
---|
769 | break; |
---|
770 | } |
---|
771 | |
---|
772 | // search |
---|
773 | case 'search': |
---|
774 | { |
---|
775 | $set['NAME'] = '<a href="'.make_index_url(array('section'=>'search', 'search'=>$this->data['type_id'])).'">'.l10n('Search').'</a>'; |
---|
776 | $set['BASENAME'] = 'search'.$this->data['type_id']; |
---|
777 | break; |
---|
778 | } |
---|
779 | |
---|
780 | // favorites |
---|
781 | case 'favorites': |
---|
782 | { |
---|
783 | $set['NAME'] = '<a href="'.make_index_url(array('section'=>'favorites')).'">'.l10n('Your favorites').'</a>'; |
---|
784 | $set['BASENAME'] = 'favorites'; |
---|
785 | break; |
---|
786 | } |
---|
787 | |
---|
788 | // most_visited |
---|
789 | case 'most_visited': |
---|
790 | { |
---|
791 | $set['NAME'] = '<a href="'.make_index_url(array('section'=>'most_visited')).'">'.l10n('Most visited').'</a>'; |
---|
792 | $set['BASENAME'] = 'most-visited'; |
---|
793 | break; |
---|
794 | } |
---|
795 | |
---|
796 | // best_rated |
---|
797 | case 'best_rated': |
---|
798 | { |
---|
799 | $set['NAME'] = '<a href="'.make_index_url(array('section'=>'best_rated')).'">'.l10n('Best rated').'</a>'; |
---|
800 | $set['BASENAME'] = 'best-rated'; |
---|
801 | break; |
---|
802 | } |
---|
803 | |
---|
804 | // list |
---|
805 | case 'list': |
---|
806 | { |
---|
807 | $set['NAME'] = l10n('Random'); |
---|
808 | $set['BASENAME'] = 'random'; |
---|
809 | break; |
---|
810 | } |
---|
811 | |
---|
812 | // recent_pics |
---|
813 | case 'recent_pics': |
---|
814 | { |
---|
815 | $set['NAME'] = '<a href="'.make_index_url(array('section'=>'recent_pics')).'">'.l10n('Recent photos').'</a>'; |
---|
816 | $set['BASENAME'] = 'recent-pics'; |
---|
817 | break; |
---|
818 | } |
---|
819 | |
---|
820 | // collection |
---|
821 | case 'collection': |
---|
822 | { |
---|
823 | try |
---|
824 | { |
---|
825 | if (!class_exists('UserCollection')) throw new Exception(); |
---|
826 | $UserCollection = new UserCollection($this->data['type_id']); |
---|
827 | $infos = $UserCollection->getCollectionInfo(); |
---|
828 | $set['NAME'] = l10n('Collection').': <a href="'.$infos['U_PUBLIC'].'">'.$UserCollection->getParam('name').'</a>'; |
---|
829 | |
---|
830 | if ( ($name = str2url($UserCollection->getParam('name'))) != null) |
---|
831 | { |
---|
832 | $set['BASENAME'] = 'collection-'.$name; |
---|
833 | } |
---|
834 | else |
---|
835 | { |
---|
836 | $set['BASENAME'] = 'collection'.$this->data['type_id']; |
---|
837 | } |
---|
838 | } |
---|
839 | catch (Exception $e) |
---|
840 | { |
---|
841 | $set['NAME'] = l10n('Collection').': #'.$this->data['type_id'].' (deleted)'; |
---|
842 | $set['BASENAME'] = 'collection'.$this->data['type_id']; |
---|
843 | } |
---|
844 | break; |
---|
845 | } |
---|
846 | } |
---|
847 | |
---|
848 | if (!isset($set['sNAME'])) $set['sNAME'] = strip_tags($set['NAME']); |
---|
849 | if (!isset($set['COMMENT'])) $set['COMMENT'] = null; |
---|
850 | if (!isset($set['BASENAME'])) $set['BASENAME'] = $this->data['type'] . $this->data['type_id']; |
---|
851 | |
---|
852 | return $set; |
---|
853 | } |
---|
854 | |
---|
855 | /** |
---|
856 | * getSetInfo |
---|
857 | * @return: array |
---|
858 | */ |
---|
859 | function getSetInfo() |
---|
860 | { |
---|
861 | $set = array( |
---|
862 | 'NB_IMAGES' => $this->data['nb_images'], |
---|
863 | 'NB_ARCHIVES' => $this->data['status']=='new' ? l10n('Unknown') : $this->getEstimatedArchiveNumber(), |
---|
864 | 'STATUS' => $this->data['status']=='ready' ? 'new' : $this->data['status'], |
---|
865 | 'LAST_ZIP' => $this->data['last_zip'], |
---|
866 | 'TOTAL_SIZE' => $this->data['status']=='new' ? l10n('Unknown') : sprintf(l10n('%d MB'), ceil($this->getEstimatedTotalSize()/1024)), |
---|
867 | 'DATE_CREATION' => format_date($this->data['date_creation'], true), |
---|
868 | 'SIZE' => $this->data['size'], |
---|
869 | ); |
---|
870 | |
---|
871 | if ($this->data['size'] != 'original') |
---|
872 | { |
---|
873 | $params = ImageStdParams::get_by_type($this->data['size']); |
---|
874 | $set['SIZE_INFO'] = $params->sizing->ideal_size[0].' x '.$params->sizing->ideal_size[1]; |
---|
875 | } |
---|
876 | |
---|
877 | return array_merge($set, $this->getNames()); |
---|
878 | } |
---|
879 | |
---|
880 | /** |
---|
881 | * delete |
---|
882 | */ |
---|
883 | function delete() |
---|
884 | { |
---|
885 | $this->deleteLastArchive(); |
---|
886 | $this->clearImages(); |
---|
887 | pwg_query('DELETE FROM '.BATCH_DOWNLOAD_TSETS.' WHERE id = '.$this->data['id'].';'); |
---|
888 | } |
---|
889 | } |
---|
890 | |
---|
891 | |
---|
892 | /** |
---|
893 | * small class implementing basic ZIP creation |
---|
894 | * with ZipArchive or PclZip |
---|
895 | */ |
---|
896 | class myZip |
---|
897 | { |
---|
898 | private $lib; |
---|
899 | private $zip; |
---|
900 | |
---|
901 | function __construct($zip_path, $pclzip=false) |
---|
902 | { |
---|
903 | if ( class_exists('ZipArchive') and !$pclzip ) |
---|
904 | { |
---|
905 | $this->lib = 'zipa'; |
---|
906 | |
---|
907 | $this->zip = new ZipArchive; |
---|
908 | if ($this->zip->open($zip_path, ZipArchive::CREATE) !== true) |
---|
909 | { |
---|
910 | trigger_error('BatchDownloader::createNextArchive, unable to open ZIP archive (ZipArchive)', E_USER_ERROR); |
---|
911 | } |
---|
912 | } |
---|
913 | else |
---|
914 | { |
---|
915 | $this->lib = 'pcl'; |
---|
916 | |
---|
917 | require_once(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php'); |
---|
918 | $this->zip = new PclZip($zip_path); |
---|
919 | |
---|
920 | // create a temporary file for archive creation |
---|
921 | touch(BATCH_DOWNLOAD_LOCAL.'temp.txt'); |
---|
922 | |
---|
923 | if ($this->zip->create(BATCH_DOWNLOAD_LOCAL.'temp.txt', PCLZIP_OPT_REMOVE_ALL_PATH) == 0) |
---|
924 | { |
---|
925 | trigger_error('BatchDownloader::createNextArchive, unable to open ZIP archive (PclZip)', E_USER_ERROR); |
---|
926 | } |
---|
927 | |
---|
928 | unlink(BATCH_DOWNLOAD_LOCAL.'temp.txt'); |
---|
929 | $this->zip->delete(PCLZIP_OPT_BY_NAME, 'temp.txt'); |
---|
930 | } |
---|
931 | } |
---|
932 | |
---|
933 | function addFile($path, $filename) |
---|
934 | { |
---|
935 | if ($this->lib == 'zipa') |
---|
936 | { |
---|
937 | $this->zip->addFile($path, $filename); |
---|
938 | } |
---|
939 | else |
---|
940 | { |
---|
941 | $this->zip->add($path, PCLZIP_OPT_REMOVE_ALL_PATH); |
---|
942 | } |
---|
943 | } |
---|
944 | |
---|
945 | function setArchiveComment($comment) |
---|
946 | { |
---|
947 | if ($this->lib == 'zipa') |
---|
948 | { |
---|
949 | $this->zip->setArchiveComment($comment); |
---|
950 | } |
---|
951 | } |
---|
952 | |
---|
953 | function close() |
---|
954 | { |
---|
955 | if ($this->lib == 'zipa') |
---|
956 | { |
---|
957 | $this->zip->close(); |
---|
958 | } |
---|
959 | } |
---|
960 | } |
---|
961 | |
---|
962 | ?> |
---|