123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- <?php
- ###
- # @name Album Module
- # @author Tobias Reich
- # @copyright 2014 by Tobias Reich
- ###
- if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
- class Album extends Module {
- private $database = null;
- private $settings = null;
- private $albumIDs = null;
- public function __construct($database, $plugins, $settings, $albumIDs) {
- # Init vars
- $this->database = $database;
- $this->plugins = $plugins;
- $this->settings = $settings;
- $this->albumIDs = $albumIDs;
- return true;
- }
- public function add($title = 'Untitled', $public = 0, $visible = 1) {
- # Check dependencies
- $this->dependencies(isset($this->database));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Parse
- if (strlen($title)>50) $title = substr($title, 0, 50);
- # Database
- $sysstamp = time();
- $result = $this->database->query("INSERT INTO lychee_albums (title, sysstamp, public, visible) VALUES ('$title', '$sysstamp', '$public', '$visible');");
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if (!$result) return false;
- return $this->database->insert_id;
- }
- public function get() {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->settings, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Get album information
- switch($this->albumIDs) {
- case 'f': $return['public'] = false;
- $query = "SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE star = 1 " . $this->settings['sorting'];
- break;
- case 's': $return['public'] = false;
- $query = "SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE public = 1 " . $this->settings['sorting'];
- break;
- case '0': $return['public'] = false;
- $query = "SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE album = 0 " . $this->settings['sorting'];
- break;
- default: $albums = $this->database->query("SELECT * FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
- $return = $albums->fetch_assoc();
- $return['sysdate'] = date('d M. Y', $return['sysstamp']);
- $return['password'] = ($return['password']=='' ? false : true);
- $query = "SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE album = '$this->albumIDs' " . $this->settings['sorting'];
- break;
- }
- # Get photos
- $photos = $this->database->query($query);
- $previousPhotoID = '';
- while($photo = $photos->fetch_assoc()) {
- # Parse
- $photo['sysdate'] = date('d F Y', substr($photo['id'], 0, -4));
- $photo['previousPhoto'] = $previousPhotoID;
- $photo['nextPhoto'] = '';
- if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id'];
- $previousPhotoID = $photo['id'];
- # Add to return
- $return['content'][$photo['id']] = $photo;
- }
- if ($photos->num_rows===0) {
- # Album empty
- $return['content'] = false;
- } else {
- # Enable next and previous for the first and last photo
- $lastElement = end($return['content']);
- $lastElementId = $lastElement['id'];
- $firstElement = reset($return['content']);
- $firstElementId = $firstElement['id'];
- if ($lastElementId!==$firstElementId) {
- $return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
- $return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
- }
- }
- $return['id'] = $this->albumIDs;
- $return['num'] = $photos->num_rows;
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- return $return;
- }
- public function getAll($public) {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->settings, $public));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Get SmartAlbums
- if ($public===false) $return = $this->getSmartInfo();
- # Albums query
- $query = 'SELECT id, title, public, sysstamp, password FROM lychee_albums WHERE public = 1 AND visible <> 0';
- if ($public===false) $query = 'SELECT id, title, public, sysstamp, password FROM lychee_albums';
- # Execute query
- $albums = $this->database->query($query) OR exit('Error: ' . $this->database->error);
- # For each album
- while ($album = $albums->fetch_assoc()) {
- # Parse info
- $album['sysdate'] = date('F Y', $album['sysstamp']);
- $album['password'] = ($album['password'] != '');
- # Thumbs
- if (($public===true&&$album['password']===false)||($public===false)) {
- # Execute query
- $thumbs = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE album = '" . $album['id'] . "' ORDER BY star DESC, " . substr($this->settings['sorting'], 9) . " LIMIT 3");
- # For each thumb
- $k = 0;
- while ($thumb = $thumbs->fetch_object()) {
- $album["thumb$k"] = $thumb->thumbUrl;
- $k++;
- }
- }
- # Add to return
- $return['content'][$album['id']] = $album;
- }
- # Num of albums
- $return['num'] = $albums->num_rows;
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- return $return;
- }
- private function getSmartInfo() {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->settings));
- # Unsorted
- $unsorted = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE album = 0 " . $this->settings['sorting']);
- $i = 0;
- while($row = $unsorted->fetch_object()) {
- if ($i<3) {
- $return["unsortedThumb$i"] = $row->thumbUrl;
- $i++;
- } else break;
- }
- $return['unsortedNum'] = $unsorted->num_rows;
- # Public
- $public = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE public = 1 " . $this->settings['sorting']);
- $i = 0;
- while($row2 = $public->fetch_object()) {
- if ($i<3) {
- $return["publicThumb$i"] = $row2->thumbUrl;
- $i++;
- } else break;
- }
- $return['publicNum'] = $public->num_rows;
- # Starred
- $starred = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE star = 1 " . $this->settings['sorting']);
- $i = 0;
- while($row3 = $starred->fetch_object()) {
- if ($i<3) {
- $return["starredThumb$i"] = $row3->thumbUrl;
- $i++;
- } else break;
- }
- $return['starredNum'] = $starred->num_rows;
- return $return;
- }
- public function getArchive() {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Photos query
- switch($this->albumIDs) {
- case 's':
- $photos = "SELECT title, url FROM lychee_photos WHERE public = '1';";
- $zipTitle = 'Public';
- break;
- case 'f':
- $photos = "SELECT title, url FROM lychee_photos WHERE star = '1';";
- $zipTitle = 'Starred';
- break;
- default:
- $photos = "SELECT title, url FROM lychee_photos WHERE album = '$this->albumIDs';";
- $zipTitle = 'Unsorted';
- }
- # 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 = LYCHEE_DATA . $zipTitle . '.zip';
- # Create zip
- $zip = new ZipArchive();
- if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false;
- # Execute query
- $photos = $this->database->query($photos);
- # Check if album empty
- if ($photos->num_rows==0) return false;
- # Parse each path
- $files = array();
- while ($photo = $photos->fetch_object()) {
- # Parse url
- $photo->url = LYCHEE_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);
- }
- # Finish zip
- $zip->close();
- # Send zip
- header("Content-Type: application/zip");
- header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
- header("Content-Length: ".filesize($filename));
- readfile($filename);
- # Delete zip
- unlink($filename);
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- return true;
- }
- public function setTitle($title = 'Untitled') {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Parse
- if (strlen($title)>50) $title = substr($title, 0, 50);
- # Execute query
- $result = $this->database->query("UPDATE lychee_albums SET title = '$title' WHERE id IN ($this->albumIDs);");
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if (!$result) return false;
- return true;
- }
- public function setDescription($description = '') {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Parse
- $description = htmlentities($description);
- if (strlen($description)>1000) return false;
- # Execute query
- $result = $this->database->query("UPDATE lychee_albums SET description = '$description' WHERE id IN ($this->albumIDs);");
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if (!$result) return false;
- return true;
- }
- public function getPublic() {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
- # Execute query
- $albums = $this->database->query("SELECT public FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
- $album = $albums->fetch_object();
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if ($album->public==1) return true;
- return false;
- }
- public function setPublic($password) {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Get public
- $albums = $this->database->query("SELECT id, public FROM lychee_albums WHERE id IN ('$this->albumIDs');");
- while ($album = $albums->fetch_object()) {
- # Invert public
- $public = ($album->public=='0' ? 1 : 0);
- # Set public
- $result = $this->database->query("UPDATE lychee_albums SET public = '$public', password = NULL WHERE id = '$album->id';");
- if (!$result) return false;
- # Reset permissions for photos
- if ($public===1) {
- $result = $this->database->query("UPDATE lychee_photos SET public = 0 WHERE album = '$album->id';");
- if (!$result) return false;
- }
- }
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- # Set password
- if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
- return true;
- }
- public function setPassword($password) {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- if (strlen($password)>0) {
- # Get hashed password
- $password = get_hashed_password($password);
- # Set hashed password
- $result = $this->database->query("UPDATE lychee_albums SET password = '$password' WHERE id IN ('$this->albumIDs');");
- } else {
- # Unset password
- $result = $this->database->query("UPDATE lychee_albums SET password = NULL WHERE id IN ('$this->albumIDs');");
- }
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if (!$result) return false;
- return true;
- }
- public function checkPassword($password) {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Execute query
- $albums = $this->database->query("SELECT password FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
- $album = $albums->fetch_object();
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if ($album->password=='') return true;
- else if ($album->password===$password||$album->password===crypt($password, $album->password)) return true;
- return false;
- }
- public function delete($albumIDs) {
- # Check dependencies
- $this->dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Init vars
- $error = false;
- # Execute query
- $photos = $this->database->query("SELECT id FROM lychee_photos WHERE album IN ($albumIDs);");
- # For each album delete photo
- while ($row = $photos->fetch_object()) {
- $photo = new Photo($this->database, $this->plugins, null, $row->id);
- if (!$photo->delete($row->id)) $error = true;
- }
- # Delete albums
- $result = $this->database->query("DELETE FROM lychee_albums WHERE id IN ($albumIDs);");
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if ($error||!$result) return false;
- return true;
- }
- }
|