From 6530902d130f74db0ce23ddefc16cbc294c0ba87 Mon Sep 17 00:00:00 2001 From: Tobias Reich Date: Sat, 12 Apr 2014 19:55:05 +0200 Subject: [PATCH] Improved getArchive with file names (#111) --- php/modules/Album.php | 77 ++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 27 deletions(-) diff --git a/php/modules/Album.php b/php/modules/Album.php index 4792c9a..59ed69f 100644 --- a/php/modules/Album.php +++ b/php/modules/Album.php @@ -232,53 +232,76 @@ class Album extends Module { # Photos query switch($this->albumIDs) { case 's': - $photos = "SELECT title, type, url FROM lychee_photos WHERE public = '1';"; + $photos = "SELECT title, url FROM lychee_photos WHERE public = '1';"; $zipTitle = 'Public'; break; case 'f': - $photos = "SELECT title, type, url FROM lychee_photos WHERE star = '1';"; + $photos = "SELECT title, url FROM lychee_photos WHERE star = '1';"; $zipTitle = 'Starred'; break; default: - $photos = "SELECT title, type, url FROM lychee_photos WHERE album = '$this->albumIDs';"; + $photos = "SELECT title, url FROM lychee_photos WHERE album = '$this->albumIDs';"; $zipTitle = 'Unsorted'; } - # Execute query - $photos = $this->database->query($photos); - - # Check if album empty - if ($photos->num_rows==0) return false; - - # Init vars - $files = array(); - $i = 0; - - # Parse each path - while ($photo = $photos->fetch_object()) { - $files[$i] = __DIR__ . '/../../uploads/big/' . $photo->url; - $i++; - } - # Set title $album = $this->database->query("SELECT title FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;"); if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) $zipTitle = $album->fetch_object()->title; $filename = __DIR__ . "/../../data/$zipTitle.zip"; # Create zip - $zip = new ZipArchive(); if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false; - # Add each photo - foreach ($files AS $file) { + # Execute query + $photos = $this->database->query($photos); - if (!@is_readable($file)) continue; + # Check if album empty + if ($photos->num_rows==0) return false; - $photoName = explode('/', $file); - $photoName = array_reverse($photoName); - $photoName = $photoName[0]; - $zip->addFile($file, $zipTitle . '/' . $photoName); + # Parse each path + $files = array(); + while ($photo = $photos->fetch_object()) { + + # Parse url + $photo->url = __DIR__ . '/../../uploads/big/' . $photo->url; + + # Parse title + $badChars = array_merge( + array_map('chr', range(0,31)), + array("<", ">", ":", '"', "/", "\\", "|", "?", "*") + ); + $photo->title = str_replace($badChars, '', $photo->title); + if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled'; + + # Check if readable + if (!@is_readable($photo->url)) continue; + + # Get extension of image + $extension = array_reverse(explode('.', $photo->url)); + $extension = $extension[0]; + + # Set title for photo + $zipFileName = $zipTitle . '/' . $photo->title . '.' . $extension; + + # Check for duplicates + if (!empty($files)) { + $i = 1; + while (in_array($zipFileName, $files)) { + + # Set new title for photo + $zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . '.' . $extension; + + $i++; + + } + } + + # Add to array + $files[] = $zipFileName; + + # Add photo to zip + $zip->addFile($photo->url, $zipFileName); }