2014-04-02 20:14:20 +00:00
< ? php
2016-01-26 14:31:53 +00:00
namespace Lychee\Modules ;
2014-04-02 20:14:20 +00:00
2016-01-30 22:24:08 +00:00
use ZipArchive ;
2016-01-30 20:33:31 +00:00
final class Album {
2014-04-02 20:14:20 +00:00
2016-01-30 19:22:28 +00:00
private $albumIDs = null ;
2014-04-02 20:14:20 +00:00
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns true when successful .
*/
2016-01-24 21:14:20 +00:00
public function __construct ( $albumIDs ) {
2014-04-02 20:14:20 +00:00
2016-01-30 20:43:57 +00:00
// Init vars
$this -> albumIDs = $albumIDs ;
2014-04-02 20:14:20 +00:00
return true ;
}
2016-02-13 16:32:44 +00:00
/**
2016-03-24 17:41:33 +00:00
* @ return string | false ID of the created album .
2016-02-13 16:32:44 +00:00
*/
Added basic subalbum support.
That is, albums can now contain other albums, which are shown at
the top of the album view. This required some changes to album.js
and the contextMenu.js, because this view contains now both
photos and albums.
The contextMenu on this view has been kept simple by requiring
the user to select either only albums or only photos, but not
a mixture of both.
This feature required a database change, so that the version
has been updated to 3.1.3.
At the moment, album and photo operations (make public, download,
delete, merge) are still "flat", i.e. don't respect the album
hierarchy.
2016-07-28 14:01:36 +00:00
public function add ( $title = 'Untitled' , $parent = 0 ) {
2014-04-02 20:14:20 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-02 20:14:20 +00:00
2016-01-30 20:43:57 +00:00
// Properties
2016-03-12 22:51:33 +00:00
$id = generateID ();
$sysstamp = time ();
$public = 0 ;
$visible = 1 ;
2016-01-24 21:14:20 +00:00
2016-01-30 20:43:57 +00:00
// Database
Added basic subalbum support.
That is, albums can now contain other albums, which are shown at
the top of the album view. This required some changes to album.js
and the contextMenu.js, because this view contains now both
photos and albums.
The contextMenu on this view has been kept simple by requiring
the user to select either only albums or only photos, but not
a mixture of both.
This feature required a database change, so that the version
has been updated to 3.1.3.
At the moment, album and photo operations (make public, download,
delete, merge) are still "flat", i.e. don't respect the album
hierarchy.
2016-07-28 14:01:36 +00:00
$query = Database :: prepare ( Database :: get (), " INSERT INTO ? (id, title, sysstamp, public, visible, parent) VALUES ('?', '?', '?', '?', '?', '?') " , array ( LYCHEE_TABLE_ALBUMS , $id , $title , $sysstamp , $public , $visible , $parent ));
2016-03-12 22:51:33 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-02 20:14:20 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-04-02 20:14:20 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2016-03-12 22:51:33 +00:00
return $id ;
2014-04-02 20:14:20 +00:00
}
2016-02-13 16:32:44 +00:00
/**
* Rurns album - attributes into a front - end friendly format . Note that some attributes remain unchanged .
* @ return array Returns album - attributes in a normalized structure .
*/
2016-01-30 00:06:21 +00:00
public static function prepareData ( array $data ) {
2015-03-12 11:57:48 +00:00
2016-01-30 20:43:57 +00:00
// 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
2015-03-12 11:57:48 +00:00
2016-01-30 20:43:57 +00:00
// Init
2015-03-12 11:57:48 +00:00
$album = null ;
2016-01-30 20:43:57 +00:00
// Set unchanged attributes
$album [ 'id' ] = $data [ 'id' ];
$album [ 'title' ] = $data [ 'title' ];
$album [ 'public' ] = $data [ 'public' ];
2015-03-12 11:57:48 +00:00
2016-01-30 20:43:57 +00:00
// Additional attributes
// Only part of $album when available
if ( isset ( $data [ 'description' ])) $album [ 'description' ] = $data [ 'description' ];
if ( isset ( $data [ 'visible' ])) $album [ 'visible' ] = $data [ 'visible' ];
if ( isset ( $data [ 'downloadable' ])) $album [ 'downloadable' ] = $data [ 'downloadable' ];
2015-12-21 14:01:19 +00:00
2016-01-30 20:43:57 +00:00
// Parse date
2016-03-15 12:28:39 +00:00
$album [ 'sysdate' ] = strftime ( '%B %Y' , $data [ 'sysstamp' ]);
2015-03-12 11:57:48 +00:00
2016-01-30 20:43:57 +00:00
// Parse password
2015-03-12 11:57:48 +00:00
$album [ 'password' ] = ( $data [ 'password' ] == '' ? '0' : '1' );
2016-01-30 20:43:57 +00:00
// Parse thumbs or set default value
2015-12-21 14:01:19 +00:00
$album [ 'thumbs' ] = ( isset ( $data [ 'thumbs' ]) ? explode ( ',' , $data [ 'thumbs' ]) : array ());
2015-03-12 11:57:48 +00:00
Added basic subalbum support.
That is, albums can now contain other albums, which are shown at
the top of the album view. This required some changes to album.js
and the contextMenu.js, because this view contains now both
photos and albums.
The contextMenu on this view has been kept simple by requiring
the user to select either only albums or only photos, but not
a mixture of both.
This feature required a database change, so that the version
has been updated to 3.1.3.
At the moment, album and photo operations (make public, download,
delete, merge) are still "flat", i.e. don't respect the album
hierarchy.
2016-07-28 14:01:36 +00:00
$album [ 'parent' ] = $data [ 'parent' ];
2015-03-12 11:57:48 +00:00
return $album ;
}
2016-02-13 16:32:44 +00:00
/**
* @ return array | false Returns an array of photos and album information or false on failure .
*/
2014-04-04 17:34:12 +00:00
public function get () {
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// Get album information
2014-04-27 12:40:54 +00:00
switch ( $this -> albumIDs ) {
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
case 'f' :
$return [ 'public' ] = '0' ;
2016-07-02 12:53:25 +00:00
$query = Database :: prepare ( Database :: get (), " SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE star = 1 " . Settings :: get ()[ 'sortingPhotos' ], array ( LYCHEE_TABLE_PHOTOS ));
2016-01-30 20:43:57 +00:00
break ;
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
case 's' :
$return [ 'public' ] = '0' ;
2016-07-02 12:53:25 +00:00
$query = Database :: prepare ( Database :: get (), " SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE public = 1 " . Settings :: get ()[ 'sortingPhotos' ], array ( LYCHEE_TABLE_PHOTOS ));
2016-01-30 20:43:57 +00:00
break ;
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
case 'r' :
$return [ 'public' ] = '0' ;
2016-07-02 12:53:25 +00:00
$query = Database :: prepare ( Database :: get (), " SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) " . Settings :: get ()[ 'sortingPhotos' ], array ( LYCHEE_TABLE_PHOTOS ));
2016-01-30 20:43:57 +00:00
break ;
2014-06-29 13:40:06 +00:00
2016-01-30 20:43:57 +00:00
case '0' :
$return [ 'public' ] = '0' ;
2016-07-02 12:53:25 +00:00
$query = Database :: prepare ( Database :: get (), " SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE album = 0 " . Settings :: get ()[ 'sortingPhotos' ], array ( LYCHEE_TABLE_PHOTOS ));
2016-01-30 20:43:57 +00:00
break ;
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
default :
$query = Database :: prepare ( Database :: get (), " SELECT * FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2016-01-31 17:49:31 +00:00
$albums = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2016-01-30 20:43:57 +00:00
$return = $albums -> fetch_assoc ();
$return = Album :: prepareData ( $return );
2016-07-02 12:53:25 +00:00
$query = Database :: prepare ( Database :: get (), " SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE album = '?' " . Settings :: get ()[ 'sortingPhotos' ], array ( LYCHEE_TABLE_PHOTOS , $this -> albumIDs ));
2016-01-30 20:43:57 +00:00
break ;
2014-04-04 17:34:12 +00:00
}
2016-01-30 20:43:57 +00:00
// Get photos
2016-01-31 17:49:31 +00:00
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2016-01-30 20:43:57 +00:00
$previousPhotoID = '' ;
2016-01-31 17:49:31 +00:00
2016-02-13 16:32:44 +00:00
if ( $photos === false ) return false ;
2016-01-31 17:49:31 +00:00
2014-04-27 12:40:54 +00:00
while ( $photo = $photos -> fetch_assoc ()) {
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// Turn data from the database into a front-end friendly format
2015-03-11 23:11:16 +00:00
$photo = Photo :: prepareData ( $photo );
2016-01-30 20:43:57 +00:00
// Set previous and next photoID for navigation purposes
2015-03-11 23:11:16 +00:00
$photo [ 'previousPhoto' ] = $previousPhotoID ;
2016-01-30 20:43:57 +00:00
$photo [ 'nextPhoto' ] = '' ;
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// Set current photoID as nextPhoto of previous photo
2014-04-04 17:34:12 +00:00
if ( $previousPhotoID !== '' ) $return [ 'content' ][ $previousPhotoID ][ 'nextPhoto' ] = $photo [ 'id' ];
$previousPhotoID = $photo [ 'id' ];
2016-01-30 20:43:57 +00:00
// Add to return
2014-04-04 17:34:12 +00:00
$return [ 'content' ][ $photo [ 'id' ]] = $photo ;
}
if ( $photos -> num_rows === 0 ) {
2016-01-30 20:43:57 +00:00
// Album empty
2014-04-04 17:34:12 +00:00
$return [ 'content' ] = false ;
} else {
2016-01-30 20:43:57 +00:00
// Enable next and previous for the first and last photo
$lastElement = end ( $return [ 'content' ]);
$lastElementId = $lastElement [ 'id' ];
$firstElement = reset ( $return [ 'content' ]);
$firstElementId = $firstElement [ 'id' ];
2014-04-04 17:34:12 +00:00
if ( $lastElementId !== $firstElementId ) {
2016-01-30 20:43:57 +00:00
$return [ 'content' ][ $lastElementId ][ 'nextPhoto' ] = $firstElementId ;
$return [ 'content' ][ $firstElementId ][ 'previousPhoto' ] = $lastElementId ;
2014-04-04 17:34:12 +00:00
}
}
2016-01-30 20:43:57 +00:00
$return [ 'id' ] = $this -> albumIDs ;
$return [ 'num' ] = $photos -> num_rows ;
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-04-04 17:34:12 +00:00
return $return ;
}
2016-02-13 16:32:44 +00:00
/**
* Starts a download of an album .
* @ return resource | boolean Sends a ZIP - file or returns false on failure .
*/
2014-04-04 17:34:12 +00:00
public function getArchive () {
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// Photos query
2014-04-04 17:34:12 +00:00
switch ( $this -> albumIDs ) {
case 's' :
2016-01-30 20:43:57 +00:00
$photos = Database :: prepare ( Database :: get (), 'SELECT title, url FROM ? WHERE public = 1' , array ( LYCHEE_TABLE_PHOTOS ));
$zipTitle = 'Public' ;
2014-04-04 17:34:12 +00:00
break ;
case 'f' :
2016-01-30 20:43:57 +00:00
$photos = Database :: prepare ( Database :: get (), 'SELECT title, url FROM ? WHERE star = 1' , array ( LYCHEE_TABLE_PHOTOS ));
$zipTitle = 'Starred' ;
2014-04-04 17:34:12 +00:00
break ;
2014-06-29 13:40:06 +00:00
case 'r' :
2016-01-30 20:43:57 +00:00
$photos = Database :: prepare ( Database :: get (), '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' ;
2014-06-29 13:40:06 +00:00
break ;
2014-04-04 17:34:12 +00:00
default :
2016-01-30 20:43:57 +00:00
$zipTitle = 'Unsorted' ;
2014-04-04 17:34:12 +00:00
2016-07-29 19:47:58 +00:00
// Get title from database when album is not a SmartAlbum
if ( $this -> albumIDs != 0 && is_numeric ( $this -> albumIDs )) {
2015-04-06 16:48:52 +00:00
2016-07-29 19:47:58 +00:00
$query = Database :: prepare ( Database :: get (), " SELECT title FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
$album = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2015-04-06 16:48:52 +00:00
2016-07-29 19:47:58 +00:00
if ( $album === false ) return false ;
2015-04-06 16:48:52 +00:00
2016-07-29 19:47:58 +00:00
// Get album object
$album = $album -> fetch_object ();
2015-04-06 16:48:52 +00:00
2016-07-29 19:47:58 +00:00
// Album not found?
if ( $album === null ) {
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not find specified album' );
return false ;
}
2015-04-06 16:48:52 +00:00
2016-07-29 19:47:58 +00:00
// Set title
$zipTitle = $album -> title ;
2015-04-06 16:48:52 +00:00
2016-07-29 19:47:58 +00:00
}
break ;
2014-08-29 17:27:09 +00:00
}
2014-06-18 21:36:00 +00:00
2016-01-30 20:43:57 +00:00
// Escape title
2016-07-29 19:47:58 +00:00
$zipTitle = $this -> cleanZipName ( $zipTitle );
2014-06-18 21:36:00 +00:00
2014-04-13 12:08:18 +00:00
$filename = LYCHEE_DATA . $zipTitle . '.zip' ;
2014-04-12 17:55:05 +00:00
2016-01-30 20:43:57 +00:00
// Create zip
2014-04-12 17:55:05 +00:00
$zip = new ZipArchive ();
2014-05-06 19:37:21 +00:00
if ( $zip -> open ( $filename , ZIPARCHIVE :: CREATE ) !== TRUE ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not create ZipArchive' );
2014-05-06 19:37:21 +00:00
return false ;
}
2014-04-12 17:55:05 +00:00
2016-07-29 19:47:58 +00:00
// Add photos to zip
switch ( $this -> albumIDs ) {
case 's' :
case 'f' :
case 'r' :
$this -> addPhotosToZip ( $zip , $zipTitle , $photos );
break ;
default :
$this -> addAlbumToZip ( $zip , $zipTitle , $this -> albumIDs );
break ;
}
2016-01-31 17:49:31 +00:00
2016-07-29 19:47:58 +00:00
// Finish zip
$zip -> close ();
2014-04-04 17:34:12 +00:00
2016-07-29 19:47:58 +00:00
// 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
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
return true ;
}
private function cleanZipName ( $name ) {
// Illicit chars
2016-07-31 13:53:34 +00:00
$badChars = array_merge (
2016-07-29 19:47:58 +00:00
array_map ( 'chr' , range ( 0 , 31 )),
array ( " < " , " > " , " : " , '"' , " / " , " \\ " , " | " , " ? " , " * " )
);
return str_replace ( $badChars , '' , $name );
}
private function addAlbumToZip ( $zip , $path , $albumID ) {
// Fetch album title
$photos = Database :: prepare ( Database :: get (), " SELECT title, url FROM ? WHERE album = '?' " , array ( LYCHEE_TABLE_PHOTOS , $albumID ));
$this -> addPhotosToZip ( $zip , $path , $photos );
// Fetch subalbums
$query = Database :: prepare ( Database :: get (), " SELECT id, title FROM ? WHERE parent = '?' " , array ( LYCHEE_TABLE_ALBUMS , $albumID ));
$albums = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
// Add them recursively
while ( $album = $albums -> fetch_assoc ()) {
$this -> addAlbumToZip ( $zip , $path . '/' . $this -> cleanZipName ( $album [ 'title' ]), $album [ 'id' ]);
2014-05-06 19:37:21 +00:00
}
2014-04-12 16:00:11 +00:00
2016-07-29 19:47:58 +00:00
}
private function addPhotosToZip ( $zip , $path , $query ) {
// Execute query
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2016-01-30 20:43:57 +00:00
// Parse each path
2014-04-12 17:55:05 +00:00
$files = array ();
2014-04-04 17:34:12 +00:00
while ( $photo = $photos -> fetch_object ()) {
2016-01-30 20:43:57 +00:00
// Parse url
2014-04-13 12:08:18 +00:00
$photo -> url = LYCHEE_UPLOADS_BIG . $photo -> url ;
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// Parse title
2016-07-29 19:47:58 +00:00
$photo -> title = $this -> cleanZipName ( $photo -> title );
2014-04-12 17:55:05 +00:00
if ( ! isset ( $photo -> title ) || $photo -> title === '' ) $photo -> title = 'Untitled' ;
2014-04-12 16:00:11 +00:00
2016-01-30 20:43:57 +00:00
// Check if readable
2014-04-12 17:55:05 +00:00
if ( !@ is_readable ( $photo -> url )) continue ;
2016-01-30 20:43:57 +00:00
// Get extension of image
2016-03-17 18:21:59 +00:00
$extension = getExtension ( $photo -> url , false );
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// Set title for photo
2016-07-29 19:47:58 +00:00
$zipFileName = $path . '/' . $photo -> title . $extension ;
2014-04-12 17:55:05 +00:00
2016-01-30 20:43:57 +00:00
// Check for duplicates
2014-04-12 17:55:05 +00:00
if ( ! empty ( $files )) {
$i = 1 ;
while ( in_array ( $zipFileName , $files )) {
2016-01-30 20:43:57 +00:00
// Set new title for photo
2016-07-29 19:47:58 +00:00
$zipFileName = $path . '/' . $photo -> title . '-' . $i . $extension ;
2014-04-12 17:55:05 +00:00
$i ++ ;
}
}
2014-04-12 16:00:11 +00:00
2016-01-30 20:43:57 +00:00
// Add to array
2014-04-12 17:55:05 +00:00
$files [] = $zipFileName ;
2014-04-12 16:00:11 +00:00
2016-01-30 20:43:57 +00:00
// Add photo to zip
2014-04-12 17:55:05 +00:00
$zip -> addFile ( $photo -> url , $zipFileName );
2014-04-12 16:00:11 +00:00
2014-04-04 17:34:12 +00:00
}
}
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns true when successful .
*/
2014-04-03 16:38:58 +00:00
public function setTitle ( $title = 'Untitled' ) {
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-04-03 16:38:58 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-03 16:38:58 +00:00
2016-01-30 20:43:57 +00:00
// Execute query
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET title = '?' WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $title , $this -> albumIDs ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-03 16:38:58 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-04-03 16:38:58 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-03 16:38:58 +00:00
return true ;
}
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns true when successful .
*/
2014-04-03 16:38:58 +00:00
public function setDescription ( $description = '' ) {
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-04-03 16:38:58 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-03 16:38:58 +00:00
2016-01-30 20:43:57 +00:00
// Execute query
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET description = '?' WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $description , $this -> albumIDs ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-03 16:38:58 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-04-03 16:38:58 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-03 16:38:58 +00:00
return true ;
}
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns true when the album is public .
*/
2014-04-05 13:59:31 +00:00
public function getPublic () {
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-04-05 13:59:31 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-05 13:59:31 +00:00
if ( $this -> albumIDs === '0' || $this -> albumIDs === 's' || $this -> albumIDs === 'f' ) return false ;
2016-01-30 20:43:57 +00:00
// Execute query
$query = Database :: prepare ( Database :: get (), " SELECT public FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2016-01-31 17:49:31 +00:00
$albums = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $albums === false ) return false ;
// Get album object
$album = $albums -> fetch_object ();
2014-04-05 13:59:31 +00:00
2016-02-13 22:37:25 +00:00
// Album not found?
if ( $album === null ) {
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not find specified album' );
return false ;
}
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-04-05 13:59:31 +00:00
if ( $album -> public == 1 ) return true ;
return false ;
}
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns true when the album is downloadable .
*/
2014-08-17 18:22:46 +00:00
public function getDownloadable () {
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-08-17 18:22:46 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-08-17 18:22:46 +00:00
2014-08-29 17:27:09 +00:00
if ( $this -> albumIDs === '0' || $this -> albumIDs === 's' || $this -> albumIDs === 'f' || $this -> albumIDs === 'r' ) return false ;
2014-08-17 18:22:46 +00:00
2016-01-30 20:43:57 +00:00
// Execute query
$query = Database :: prepare ( Database :: get (), " SELECT downloadable FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2016-01-31 17:49:31 +00:00
$albums = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $albums === false ) return false ;
// Get album object
$album = $albums -> fetch_object ();
2014-08-17 18:22:46 +00:00
2016-02-13 22:37:25 +00:00
// Album not found?
if ( $album === null ) {
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not find specified album' );
return false ;
}
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-08-17 18:22:46 +00:00
if ( $album -> downloadable == 1 ) return true ;
return false ;
}
2016-07-29 19:56:48 +00:00
private function getSubAlbums ( $albumID ) {
$query = Database :: prepare ( Database :: get (), " SELECT id FROM ? WHERE parent = '?' " , array ( LYCHEE_TABLE_ALBUMS , $albumID ));
$albums = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
$ids = array ();
while ( $album = $albums -> fetch_assoc ()) {
$ids = array_merge ( $ids , array ( $album [ 'id' ]), $this -> getSubAlbums ( $album [ 'id' ]));
}
return $ids ;
}
private function addSubAlbumIDs ( $ids ) {
$res = array ();
foreach ( explode ( ',' , $ids ) as $id )
$res = array_merge ( $res , array ( $id ), $this -> getSubAlbums ( $id ));
return implode ( ',' , $res );
}
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns true when successful .
*/
2015-02-28 22:42:13 +00:00
public function setPublic ( $public , $password , $visible , $downloadable ) {
2014-04-04 15:56:08 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-04-04 15:56:08 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-04 15:56:08 +00:00
2016-01-30 20:43:57 +00:00
// Convert values
$public = ( $public === '1' ? 1 : 0 );
$visible = ( $visible === '1' ? 1 : 0 );
$downloadable = ( $downloadable === '1' ? 1 : 0 );
2014-07-21 20:16:30 +00:00
2016-07-29 19:58:40 +00:00
// Get all album ids, including subalbums
$ids = $this -> addSubAlbumIDs ( $this -> albumIDs );
2016-01-30 20:43:57 +00:00
// Set public
2016-07-29 19:58:40 +00:00
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $public , $visible , $downloadable , $ids ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $result === false ) return false ;
2014-08-09 15:57:31 +00:00
2016-01-30 20:43:57 +00:00
// Reset permissions for photos
2015-02-28 22:42:13 +00:00
if ( $public === 1 ) {
2016-01-31 17:49:31 +00:00
2016-07-29 19:58:40 +00:00
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET public = 0 WHERE album IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $ids ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $result === false ) return false ;
2014-04-04 15:56:08 +00:00
}
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-04-04 15:56:08 +00:00
2016-01-30 20:43:57 +00:00
// Set password
2014-04-04 15:56:08 +00:00
if ( isset ( $password ) && strlen ( $password ) > 0 ) return $this -> setPassword ( $password );
return true ;
}
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns true when successful .
*/
2014-07-21 20:38:56 +00:00
private function setPassword ( $password ) {
2014-04-03 16:38:58 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-04-03 16:38:58 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-03 16:38:58 +00:00
2014-04-21 12:18:13 +00:00
if ( strlen ( $password ) > 0 ) {
2016-01-30 20:43:57 +00:00
// Get hashed password
2015-02-08 14:36:13 +00:00
$password = getHashedString ( $password );
2014-04-21 00:19:23 +00:00
2016-01-30 20:43:57 +00:00
// Set hashed password
// Do not prepare $password because it is hashed and save
// Preparing (escaping) the password would destroy the hash
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET password = ' $password ' WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2014-04-21 12:18:13 +00:00
2014-04-21 00:19:23 +00:00
} else {
2014-04-21 12:18:13 +00:00
2016-01-30 20:43:57 +00:00
// Unset password
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET password = NULL WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2014-04-21 12:18:13 +00:00
2014-04-21 00:19:23 +00:00
}
2014-04-03 16:38:58 +00:00
2016-01-30 20:43:57 +00:00
// Execute query
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2015-02-08 14:36:13 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-04-03 16:38:58 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-03 16:38:58 +00:00
return true ;
}
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns when album is public .
*/
2014-04-04 17:34:12 +00:00
public function checkPassword ( $password ) {
2014-04-04 15:18:59 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-04-04 15:18:59 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-04 15:18:59 +00:00
2016-01-30 20:43:57 +00:00
// Execute query
$query = Database :: prepare ( Database :: get (), " SELECT password FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2016-01-31 17:49:31 +00:00
$albums = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $albums === false ) return false ;
// Get album object
$album = $albums -> fetch_object ();
2014-04-04 15:18:59 +00:00
2016-02-13 22:37:25 +00:00
// Album not found?
if ( $album === null ) {
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not find specified album' );
return false ;
}
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-04-04 15:18:59 +00:00
2016-01-31 17:49:31 +00:00
// Check if password is correct
2014-04-04 17:34:12 +00:00
if ( $album -> password == '' ) return true ;
2016-01-31 17:49:31 +00:00
if ( $album -> password === crypt ( $password , $album -> password )) return true ;
2014-04-04 17:34:12 +00:00
return false ;
2014-04-04 15:18:59 +00:00
2014-04-04 17:34:12 +00:00
}
2014-04-04 15:18:59 +00:00
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns true when successful .
*/
2015-05-05 20:06:54 +00:00
public function merge () {
2014-04-04 15:18:59 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2014-04-04 15:18:59 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-04-04 15:18:59 +00:00
2016-01-30 20:43:57 +00:00
// Convert to array
2015-05-05 20:06:54 +00:00
$albumIDs = explode ( ',' , $this -> albumIDs );
2014-04-04 15:18:59 +00:00
2016-01-30 20:43:57 +00:00
// Get first albumID
2015-05-09 08:16:49 +00:00
$albumID = array_splice ( $albumIDs , 0 , 1 );
$albumID = $albumID [ 0 ];
2014-04-04 17:34:12 +00:00
2016-07-29 19:58:29 +00:00
// Move photos
2016-01-30 20:43:57 +00:00
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET album = ? WHERE album IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $albumID , $this -> albumIDs ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-05 13:59:31 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-04 17:34:12 +00:00
2016-01-30 20:43:57 +00:00
// $albumIDs contains all IDs without the first albumID
// Convert to string
2015-05-05 20:06:54 +00:00
$filteredIDs = implode ( ',' , $albumIDs );
2016-07-29 19:58:29 +00:00
// Move subalbums
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET parent = ? WHERE parent IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $albumID , $filteredIDs ));
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $result === false ) return false ;
// Delete other albums
2016-01-30 20:43:57 +00:00
$query = Database :: prepare ( Database :: get (), " DELETE FROM ? WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $filteredIDs ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-04 15:18:59 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2014-04-04 15:56:08 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-04 15:18:59 +00:00
return true ;
}
2016-02-13 16:32:44 +00:00
/**
* @ return boolean Returns true when successful .
*/
2015-05-05 20:06:54 +00:00
public function delete () {
2015-05-05 10:18:27 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> albumIDs ), __METHOD__ );
2015-05-05 10:18:27 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2015-05-05 10:18:27 +00:00
2016-07-29 19:56:48 +00:00
// Get all album ids, including subalbums
$ids = $this -> addSubAlbumIDs ( $this -> albumIDs );
2016-01-30 20:43:57 +00:00
// Init vars
2016-01-31 17:49:31 +00:00
$photoIDs = array ();
2015-05-05 10:18:27 +00:00
2016-01-30 20:43:57 +00:00
// Execute query
2016-07-29 19:56:48 +00:00
$query = Database :: prepare ( Database :: get (), " SELECT id FROM ? WHERE album IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $ids ));
2016-01-31 17:49:31 +00:00
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2015-05-05 10:18:27 +00:00
2016-01-31 17:49:31 +00:00
if ( $photos === false ) return false ;
2015-05-05 20:06:54 +00:00
2016-01-31 17:49:31 +00:00
// Only delete photos when albums contain photos
if ( $photos -> num_rows > 0 ) {
// Add each id to photoIDs
while ( $row = $photos -> fetch_object ()) $photoIDs [] = $row -> id ;
// Convert photoIDs to a string
$photoIDs = implode ( ',' , $photoIDs );
// Delete all photos
$photo = new Photo ( $photoIDs );
if ( $photo -> delete () !== true ) return false ;
2015-05-05 10:18:27 +00:00
}
2016-01-30 20:43:57 +00:00
// Delete albums
2016-07-29 19:56:48 +00:00
$query = Database :: prepare ( Database :: get (), " DELETE FROM ? WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $ids ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2015-05-05 10:18:27 +00:00
2016-01-30 20:43:57 +00:00
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 1 , func_get_args ());
2015-05-05 20:06:54 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2015-05-05 10:18:27 +00:00
return true ;
2015-05-05 20:06:54 +00:00
2015-05-05 10:18:27 +00:00
}
2014-04-28 08:17:26 +00:00
}
2016-01-31 14:53:44 +00:00
?>