123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- <?php
- ###
- # @name Album Module
- # @copyright 2015 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
- self::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();
- $query = Database::prepare($this->database, "INSERT INTO ? (title, sysstamp, public, visible) VALUES ('?', '?', '?', '?')", array(LYCHEE_TABLE_ALBUMS, $title, $sysstamp, $public, $visible));
- $result = $this->database->query($query);
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if (!$result) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- return false;
- }
- return $this->database->insert_id;
- }
- public static function prepareData($data) {
- # This function requires the following album-attributes and turns them
- # into a front-end friendly format: id, title, public, sysstamp, password
- # Note that some attributes remain unchanged
- # Check dependencies
- self::dependencies(isset($data));
- # Init
- $album = null;
- # Set unchanged attributes
- $album['id'] = $data['id'];
- $album['title'] = $data['title'];
- $album['public'] = $data['public'];
- # Parse date
- $album['sysdate'] = date('F Y', $data['sysstamp']);
- # Parse password
- $album['password'] = ($data['password']=='' ? '0' : '1');
- # Set placeholder for thumbs
- $album['thumbs'] = array();
- return $album;
- }
- public function get() {
- # Check dependencies
- self::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'] = '0';
- $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE star = 1 " . $this->settings['sorting'], array(LYCHEE_TABLE_PHOTOS));
- break;
- case 's': $return['public'] = '0';
- $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE public = 1 " . $this->settings['sorting'], array(LYCHEE_TABLE_PHOTOS));
- break;
- case 'r': $return['public'] = '0';
- $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) " . $this->settings['sorting'], array(LYCHEE_TABLE_PHOTOS));
- break;
- case '0': $return['public'] = '0';
- $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = 0 " . $this->settings['sorting'], array(LYCHEE_TABLE_PHOTOS));
- break;
- default: $query = Database::prepare($this->database, "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
- $albums = $this->database->query($query);
- $return = $albums->fetch_assoc();
- $return['sysdate'] = date('d M. Y', $return['sysstamp']);
- $return['password'] = ($return['password']=='' ? '0' : '1');
- $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = '?' " . $this->settings['sorting'], array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
- break;
- }
- # Get photos
- $photos = $this->database->query($query);
- $previousPhotoID = '';
- while ($photo = $photos->fetch_assoc()) {
- # Turn data from the database into a front-end friendly format
- $photo = Photo::prepareData($photo);
- # Set previous and next photoID for navigation purposes
- $photo['previousPhoto'] = $previousPhotoID;
- $photo['nextPhoto'] = '';
- # Set current photoID as nextPhoto of previous photo
- 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
- self::dependencies(isset($this->database, $this->settings, $public));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Initialize return var
- $return = array(
- 'smartalbums' => null,
- 'albums' => null,
- 'num' => 0
- );
- # Get SmartAlbums
- if ($public===false) $return['smartalbums'] = $this->getSmartInfo();
- # Albums query
- $query = Database::prepare($this->database, 'SELECT id, title, public, sysstamp, password FROM ? WHERE public = 1 AND visible <> 0', array(LYCHEE_TABLE_ALBUMS));
- if ($public===false) $query = Database::prepare($this->database, 'SELECT id, title, public, sysstamp, password FROM ?', array(LYCHEE_TABLE_ALBUMS));
- # Execute query
- $albums = $this->database->query($query);
- if (!$albums) {
- Log::error($database, __METHOD__, __LINE__, 'Could not get all albums (' . $database->error . ')');
- exit('Error: ' . $this->database->error);
- }
- # For each album
- while ($album = $albums->fetch_assoc()) {
- # Turn data from the database into a front-end friendly format
- $album = Album::prepareData($album);
- # Thumbs
- if (($public===true&&$album['password']==='0')||
- ($public===false)) {
- # Execute query
- $query = Database::prepare($this->database, "SELECT thumbUrl FROM ? WHERE album = '?' ORDER BY star DESC, " . substr($this->settings['sorting'], 9) . " LIMIT 3", array(LYCHEE_TABLE_PHOTOS, $album['id']));
- $thumbs = $this->database->query($query);
- # For each thumb
- $k = 0;
- while ($thumb = $thumbs->fetch_object()) {
- $album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
- $k++;
- }
- }
- # Add to return
- $return['albums'][$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
- self::dependencies(isset($this->database, $this->settings));
- # Initialize return var
- $return = array(
- 'unsorted' => null,
- 'public' => null,
- 'starred' => null,
- 'recent' => null
- );
- ###
- # Unsorted
- ###
- $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE album = 0 ' . $this->settings['sorting'], array(LYCHEE_TABLE_PHOTOS));
- $unsorted = $this->database->query($query);
- $i = 0;
- $return['unsorted'] = array(
- 'thumbs' => array(),
- 'num' => $unsorted->num_rows
- );
- while($row = $unsorted->fetch_object()) {
- if ($i<3) {
- $return['unsorted']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row->thumbUrl;
- $i++;
- } else break;
- }
- ###
- # Starred
- ###
- $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE star = 1 ' . $this->settings['sorting'], array(LYCHEE_TABLE_PHOTOS));
- $starred = $this->database->query($query);
- $i = 0;
- $return['starred'] = array(
- 'thumbs' => array(),
- 'num' => $starred->num_rows
- );
- while($row3 = $starred->fetch_object()) {
- if ($i<3) {
- $return['starred']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
- $i++;
- } else break;
- }
- ###
- # Public
- ###
- $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE public = 1 ' . $this->settings['sorting'], array(LYCHEE_TABLE_PHOTOS));
- $public = $this->database->query($query);
- $i = 0;
- $return['public'] = array(
- 'thumbs' => array(),
- 'num' => $public->num_rows
- );
- while($row2 = $public->fetch_object()) {
- if ($i<3) {
- $return['public']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row2->thumbUrl;
- $i++;
- } else break;
- }
- ###
- # Recent
- ###
- $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) ' . $this->settings['sorting'], array(LYCHEE_TABLE_PHOTOS));
- $recent = $this->database->query($query);
- $i = 0;
- $return['recent'] = array(
- 'thumbs' => array(),
- 'num' => $recent->num_rows
- );
- while($row3 = $recent->fetch_object()) {
- if ($i<3) {
- $return['recent']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
- $i++;
- } else break;
- }
- # Return SmartAlbums
- return $return;
- }
- public function getArchive() {
- # Check dependencies
- self::dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Illicit chars
- $badChars = array_merge(
- array_map('chr', range(0,31)),
- array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
- );
- # Photos query
- switch($this->albumIDs) {
- case 's':
- $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE public = 1', array(LYCHEE_TABLE_PHOTOS));
- $zipTitle = 'Public';
- break;
- case 'f':
- $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE star = 1', array(LYCHEE_TABLE_PHOTOS));
- $zipTitle = 'Starred';
- break;
- case 'r':
- $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY checksum', array(LYCHEE_TABLE_PHOTOS));
- $zipTitle = 'Recent';
- break;
- default:
- $photos = Database::prepare($this->database, "SELECT title, url FROM ? WHERE album = '?'", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
- $zipTitle = 'Unsorted';
- }
- # Get title from database when album is not a SmartAlbum
- if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) {
- $query = Database::prepare($this->database, "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
- $album = $this->database->query($query);
- # Error in database query
- if (!$album) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- return false;
- }
- # Fetch object
- $album = $album->fetch_object();
- # Photo not found
- if ($album===null) {
- Log::error($this->database, __METHOD__, __LINE__, 'Album not found. Cannot start download.');
- return false;
- }
- # Set title
- $zipTitle = $album->title;
- }
- # Escape title
- $zipTitle = str_replace($badChars, '', $zipTitle);
- $filename = LYCHEE_DATA . $zipTitle . '.zip';
- # Create zip
- $zip = new ZipArchive();
- if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
- Log::error($this->database, __METHOD__, __LINE__, 'Could not create ZipArchive');
- return false;
- }
- # Execute query
- $photos = $this->database->query($photos);
- # Check if album empty
- if ($photos->num_rows==0) {
- Log::error($this->database, __METHOD__, __LINE__, 'Could not create ZipArchive without images');
- return false;
- }
- # Parse each path
- $files = array();
- while ($photo = $photos->fetch_object()) {
- # Parse url
- $photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
- # Parse title
- $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 = getExtension($photo->url);
- # 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
- self::dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Parse
- if (strlen($title)>100) $title = substr($title, 0, 100);
- # Execute query
- $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $title, $this->albumIDs));
- $result = $this->database->query($query);
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if (!$result) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- return false;
- }
- return true;
- }
- public function setDescription($description = '') {
- # Check dependencies
- self::dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Parse
- $description = htmlentities($description, ENT_COMPAT | ENT_HTML401, 'UTF-8');
- if (strlen($description)>1000) $description = substr($description, 0, 1000);
- # Execute query
- $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $description, $this->albumIDs));
- $result = $this->database->query($query);
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if (!$result) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- return false;
- }
- return true;
- }
- public function getPublic() {
- # Check dependencies
- self::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
- $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
- $albums = $this->database->query($query);
- $album = $albums->fetch_object();
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if ($album->public==1) return true;
- return false;
- }
- public function getDownloadable() {
- # Check dependencies
- self::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'||$this->albumIDs==='r') return false;
- # Execute query
- $query = Database::prepare($this->database, "SELECT downloadable FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
- $albums = $this->database->query($query);
- $album = $albums->fetch_object();
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if ($album->downloadable==1) return true;
- return false;
- }
- public function setPublic($public, $password, $visible, $downloadable) {
- # Check dependencies
- self::dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Convert values
- $public = ($public==='1' ? 1 : 0);
- $visible = ($visible==='1' ? 1 : 0);
- $downloadable = ($downloadable==='1' ? 1 : 0);
- # Set public
- $query = Database::prepare($this->database, "UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $public, $visible, $downloadable, $this->albumIDs));
- $result = $this->database->query($query);
- if (!$result) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- return false;
- }
- # Reset permissions for photos
- if ($public===1) {
- $query = Database::prepare($this->database, "UPDATE ? SET public = 0 WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
- $result = $this->database->query($query);
- if (!$result) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- 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;
- }
- private function setPassword($password) {
- # Check dependencies
- self::dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- if (strlen($password)>0) {
- # Get hashed password
- $password = getHashedString($password);
- # Set hashed password
- # Do not prepare $password because it is hashed and save
- # Preparing (escaping) the password would destroy the hash
- $query = Database::prepare($this->database, "UPDATE ? SET password = '$password' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
- } else {
- # Unset password
- $query = Database::prepare($this->database, "UPDATE ? SET password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
- }
- # Execute query
- $result = $this->database->query($query);
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if (!$result) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- return false;
- }
- return true;
- }
- public function checkPassword($password) {
- # Check dependencies
- self::dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Execute query
- $query = Database::prepare($this->database, "SELECT password FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
- $albums = $this->database->query($query);
- $album = $albums->fetch_object();
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if ($album->password=='') return true;
- else if ($album->password===crypt($password, $album->password)) return true;
- return false;
- }
- public function merge() {
- # Check dependencies
- self::dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Convert to array
- $albumIDs = explode(',', $this->albumIDs);
- # Get first albumID
- $albumID = array_splice($albumIDs, 0, 1);
- $albumID = $albumID[0];
- $query = Database::prepare($this->database, "UPDATE ? SET album = ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->albumIDs));
- $result = $this->database->query($query);
- if (!$result) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- return false;
- }
- # $albumIDs contains all IDs without the first albumID
- # Convert to string
- $filteredIDs = implode(',', $albumIDs);
- $query = Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $filteredIDs));
- $result = $this->database->query($query);
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if (!$result) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- return false;
- }
- return true;
- }
- public function delete() {
- # Check dependencies
- self::dependencies(isset($this->database, $this->albumIDs));
- # Call plugins
- $this->plugins(__METHOD__, 0, func_get_args());
- # Init vars
- $error = false;
- # Execute query
- $query = Database::prepare($this->database, "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
- $photos = $this->database->query($query);
- # 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
- $query = Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
- $result = $this->database->query($query);
- # Call plugins
- $this->plugins(__METHOD__, 1, func_get_args());
- if ($error) return false;
- if (!$result) {
- Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
- return false;
- }
- return true;
- }
- }
- ?>
|