2014-04-02 20:14:20 +00:00
< ? php
###
# @name Album Module
2015-02-01 21:08:37 +00:00
# @copyright 2015 by Tobias Reich
2014-04-02 20:14:20 +00:00
###
if ( ! defined ( 'LYCHEE' )) exit ( 'Error: Direct access is not allowed!' );
2014-04-04 19:27:10 +00:00
class Album extends Module {
2014-04-02 20:14:20 +00:00
private $database = null ;
2014-04-03 16:38:58 +00:00
private $settings = null ;
private $albumIDs = null ;
2014-04-02 20:14:20 +00:00
2014-04-03 16:38:58 +00:00
public function __construct ( $database , $plugins , $settings , $albumIDs ) {
2014-04-02 20:14:20 +00:00
# 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 ) {
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database ));
2014-04-02 20:14:20 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
2014-04-02 20:14:20 +00:00
# Parse
if ( strlen ( $title ) > 50 ) $title = substr ( $title , 0 , 50 );
# Database
2014-04-11 08:38:28 +00:00
$sysstamp = time ();
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " INSERT INTO ? (title, sysstamp, public, visible) VALUES ('?', '?', '?', '?') " , array ( LYCHEE_TABLE_ALBUMS , $title , $sysstamp , $public , $visible ));
2014-08-29 17:27:09 +00:00
$result = $this -> database -> query ( $query );
2014-04-02 20:14:20 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-04-02 20:14:20 +00:00
2014-05-06 19:24:58 +00:00
if ( ! $result ) {
Log :: error ( $this -> database , __METHOD__ , __LINE__ , $this -> database -> error );
return false ;
}
2014-04-02 20:14:20 +00:00
return $this -> database -> insert_id ;
}
2015-03-12 11:57:48 +00:00
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 ;
}
2014-04-04 17:34:12 +00:00
public function get () {
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> settings , $this -> albumIDs ));
2014-04-04 17:34:12 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
2014-04-04 17:34:12 +00:00
# Get album information
2014-04-27 12:40:54 +00:00
switch ( $this -> albumIDs ) {
2014-04-04 17:34:12 +00:00
case 'f' : $return [ 'public' ] = false ;
2014-09-16 20:51:54 +00:00
$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 ));
2014-04-04 17:34:12 +00:00
break ;
case 's' : $return [ 'public' ] = false ;
2014-09-16 20:51:54 +00:00
$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 ));
2014-04-04 17:34:12 +00:00
break ;
2014-06-29 13:40:06 +00:00
case 'r' : $return [ 'public' ] = false ;
2014-09-16 20:51:54 +00:00
$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 ));
2014-06-29 13:40:06 +00:00
break ;
2014-04-04 17:34:12 +00:00
case '0' : $return [ 'public' ] = false ;
2014-09-16 20:51:54 +00:00
$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 ));
2014-04-04 17:34:12 +00:00
break ;
2014-08-30 05:05:26 +00:00
default : $query = Database :: prepare ( $this -> database , " SELECT * FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$albums = $this -> database -> query ( $query );
2014-04-04 17:34:12 +00:00
$return = $albums -> fetch_assoc ();
2014-09-17 21:11:02 +00:00
$return [ 'sysdate' ] = date ( 'd M. Y' , $return [ 'sysstamp' ]);
2015-02-27 20:16:22 +00:00
$return [ 'password' ] = ( $return [ 'password' ] == '' ? '0' : '1' );
2014-09-17 21:11:02 +00:00
$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 ));
2014-04-04 17:34:12 +00:00
break ;
}
# Get photos
$photos = $this -> database -> query ( $query );
$previousPhotoID = '' ;
2014-04-27 12:40:54 +00:00
while ( $photo = $photos -> fetch_assoc ()) {
2014-04-04 17:34:12 +00:00
2015-03-11 23:11:16 +00:00
# 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' ] = '' ;
2014-04-04 17:34:12 +00:00
2015-03-11 23:11:16 +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' ];
# 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
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-04-04 17:34:12 +00:00
return $return ;
}
2014-04-02 20:14:20 +00:00
public function getAll ( $public ) {
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> settings , $public ));
2014-04-02 20:14:20 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
2014-04-02 20:14:20 +00:00
2015-02-27 19:37:24 +00:00
# Initialize return var
$return = array (
'smartalbums' => null ,
'albums' => null ,
'num' => 0
);
2014-04-02 20:14:20 +00:00
# Get SmartAlbums
2015-02-27 19:37:24 +00:00
if ( $public === false ) $return [ 'smartalbums' ] = $this -> getSmartInfo ();
2014-04-02 20:14:20 +00:00
# Albums query
2014-08-30 05:05:26 +00:00
$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 ));
2014-04-02 20:14:20 +00:00
# Execute query
2014-08-29 17:27:09 +00:00
$albums = $this -> database -> query ( $query );
if ( ! $albums ) {
Log :: error ( $database , __METHOD__ , __LINE__ , 'Could not get all albums (' . $database -> error . ')' );
exit ( 'Error: ' . $this -> database -> error );
}
2014-04-02 20:14:20 +00:00
# For each album
while ( $album = $albums -> fetch_assoc ()) {
2015-03-12 11:57:48 +00:00
# Turn data from the database into a front-end friendly format
$album = Album :: prepareData ( $album );
2014-04-02 20:14:20 +00:00
# Thumbs
2015-03-11 23:11:16 +00:00
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 ()) {
2015-03-12 11:57:48 +00:00
$album [ 'thumbs' ][ $k ] = LYCHEE_URL_UPLOADS_THUMB . $thumb -> thumbUrl ;
2015-03-11 23:11:16 +00:00
$k ++ ;
}
2014-04-02 20:14:20 +00:00
}
# Add to return
2015-02-27 19:37:24 +00:00
$return [ 'albums' ][ $album [ 'id' ]] = $album ;
2014-04-02 20:14:20 +00:00
}
# Num of albums
$return [ 'num' ] = $albums -> num_rows ;
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-09-17 21:11:02 +00:00
2014-04-02 20:14:20 +00:00
return $return ;
}
2014-04-04 17:34:12 +00:00
private function getSmartInfo () {
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> settings ));
2014-04-04 17:34:12 +00:00
2015-02-27 19:37:24 +00:00
# Initialize return var
$return = array (
'unsorted' => null ,
'public' => null ,
'starred' => null ,
'recent' => null
);
2015-03-12 11:57:48 +00:00
###
2014-04-04 17:34:12 +00:00
# Unsorted
2015-03-12 11:57:48 +00:00
###
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , 'SELECT thumbUrl FROM ? WHERE album = 0 ' . $this -> settings [ 'sorting' ], array ( LYCHEE_TABLE_PHOTOS ));
2014-08-29 17:27:09 +00:00
$unsorted = $this -> database -> query ( $query );
2014-04-04 17:34:12 +00:00
$i = 0 ;
2015-03-12 11:57:48 +00:00
$return [ 'unsorted' ] = array (
'thumbs' => array (),
'num' => $unsorted -> num_rows
);
2014-04-04 17:34:12 +00:00
while ( $row = $unsorted -> fetch_object ()) {
if ( $i < 3 ) {
2015-03-12 11:57:48 +00:00
$return [ 'unsorted' ][ 'thumbs' ][ $i ] = LYCHEE_URL_UPLOADS_THUMB . $row -> thumbUrl ;
2014-04-04 17:34:12 +00:00
$i ++ ;
} else break ;
}
2015-03-12 11:57:48 +00:00
###
2015-02-27 19:37:24 +00:00
# Starred
2015-03-12 11:57:48 +00:00
###
2015-02-27 19:37:24 +00:00
$query = Database :: prepare ( $this -> database , 'SELECT thumbUrl FROM ? WHERE star = 1 ' . $this -> settings [ 'sorting' ], array ( LYCHEE_TABLE_PHOTOS ));
$starred = $this -> database -> query ( $query );
2014-04-04 17:34:12 +00:00
$i = 0 ;
2015-03-12 11:57:48 +00:00
$return [ 'starred' ] = array (
'thumbs' => array (),
'num' => $starred -> num_rows
);
2015-02-27 19:37:24 +00:00
while ( $row3 = $starred -> fetch_object ()) {
2014-04-04 17:34:12 +00:00
if ( $i < 3 ) {
2015-03-12 11:57:48 +00:00
$return [ 'starred' ][ 'thumbs' ][ $i ] = LYCHEE_URL_UPLOADS_THUMB . $row3 -> thumbUrl ;
2014-04-04 17:34:12 +00:00
$i ++ ;
} else break ;
}
2015-03-12 11:57:48 +00:00
###
2015-02-27 19:37:24 +00:00
# Public
2015-03-12 11:57:48 +00:00
###
2015-02-27 19:37:24 +00:00
$query = Database :: prepare ( $this -> database , 'SELECT thumbUrl FROM ? WHERE public = 1 ' . $this -> settings [ 'sorting' ], array ( LYCHEE_TABLE_PHOTOS ));
$public = $this -> database -> query ( $query );
2014-04-04 17:34:12 +00:00
$i = 0 ;
2015-03-12 11:57:48 +00:00
$return [ 'public' ] = array (
'thumbs' => array (),
'num' => $public -> num_rows
);
2015-02-27 19:37:24 +00:00
while ( $row2 = $public -> fetch_object ()) {
2014-04-04 17:34:12 +00:00
if ( $i < 3 ) {
2015-03-12 11:57:48 +00:00
$return [ 'public' ][ 'thumbs' ][ $i ] = LYCHEE_URL_UPLOADS_THUMB . $row2 -> thumbUrl ;
2014-04-04 17:34:12 +00:00
$i ++ ;
} else break ;
}
2015-03-12 11:57:48 +00:00
###
2014-06-29 13:40:06 +00:00
# Recent
2015-03-12 11:57:48 +00:00
###
2014-08-30 05:05:26 +00:00
$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 ));
2014-08-29 17:27:09 +00:00
$recent = $this -> database -> query ( $query );
2014-06-29 13:40:06 +00:00
$i = 0 ;
2015-03-12 11:57:48 +00:00
$return [ 'recent' ] = array (
'thumbs' => array (),
'num' => $recent -> num_rows
);
2014-06-29 13:40:06 +00:00
while ( $row3 = $recent -> fetch_object ()) {
if ( $i < 3 ) {
2015-03-12 11:57:48 +00:00
$return [ 'recent' ][ 'thumbs' ][ $i ] = LYCHEE_URL_UPLOADS_THUMB . $row3 -> thumbUrl ;
2014-06-29 13:40:06 +00:00
$i ++ ;
} else break ;
}
2015-03-12 11:57:48 +00:00
# Return SmartAlbums
2014-04-04 17:34:12 +00:00
return $return ;
}
public function getArchive () {
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> albumIDs ));
2014-04-04 17:34:12 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
2014-04-04 17:34:12 +00:00
2014-06-18 21:36:00 +00:00
# Illicit chars
$badChars = array_merge (
array_map ( 'chr' , range ( 0 , 31 )),
array ( " < " , " > " , " : " , '"' , " / " , " \\ " , " | " , " ? " , " * " )
);
2014-04-04 17:34:12 +00:00
# Photos query
switch ( $this -> albumIDs ) {
case 's' :
2014-08-30 05:05:26 +00:00
$photos = Database :: prepare ( $this -> database , 'SELECT title, url FROM ? WHERE public = 1' , array ( LYCHEE_TABLE_PHOTOS ));
2014-08-29 17:27:09 +00:00
$zipTitle = 'Public' ;
2014-04-04 17:34:12 +00:00
break ;
case 'f' :
2014-08-30 05:05:26 +00:00
$photos = Database :: prepare ( $this -> database , 'SELECT title, url FROM ? WHERE star = 1' , array ( LYCHEE_TABLE_PHOTOS ));
2014-08-29 17:27:09 +00:00
$zipTitle = 'Starred' ;
2014-04-04 17:34:12 +00:00
break ;
2014-06-29 13:40:06 +00:00
case 'r' :
2014-08-30 05:05:26 +00:00
$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 ));
2014-08-29 17:27:09 +00:00
$zipTitle = 'Recent' ;
2014-06-29 13:40:06 +00:00
break ;
2014-04-04 17:34:12 +00:00
default :
2014-08-30 05:05:26 +00:00
$photos = Database :: prepare ( $this -> database , " SELECT title, url FROM ? WHERE album = '?' " , array ( LYCHEE_TABLE_PHOTOS , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$zipTitle = 'Unsorted' ;
2014-04-04 17:34:12 +00:00
}
2014-04-12 17:55:05 +00:00
# Set title
2014-08-29 17:27:09 +00:00
if ( $this -> albumIDs != 0 && is_numeric ( $this -> albumIDs )) {
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " SELECT title FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$album = $this -> database -> query ( $query );
$zipTitle = $album -> fetch_object () -> title ;
}
2014-06-18 21:36:00 +00:00
2014-08-29 17:27:09 +00:00
# Escape title
2014-06-18 21:36:00 +00:00
$zipTitle = str_replace ( $badChars , '' , $zipTitle );
2014-04-13 12:08:18 +00:00
$filename = LYCHEE_DATA . $zipTitle . '.zip' ;
2014-04-12 17:55:05 +00:00
# Create zip
$zip = new ZipArchive ();
2014-05-06 19:37:21 +00:00
if ( $zip -> open ( $filename , ZIPARCHIVE :: CREATE ) !== TRUE ) {
Log :: error ( $this -> database , __METHOD__ , __LINE__ , 'Could not create ZipArchive' );
return false ;
}
2014-04-12 17:55:05 +00:00
2014-04-04 17:34:12 +00:00
# Execute query
$photos = $this -> database -> query ( $photos );
2014-04-12 16:00:11 +00:00
# Check if album empty
2014-05-06 19:37:21 +00:00
if ( $photos -> num_rows == 0 ) {
Log :: error ( $this -> database , __METHOD__ , __LINE__ , 'Could not create ZipArchive without images' );
return false ;
}
2014-04-12 16:00:11 +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 ()) {
2014-04-12 17:55:05 +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
2014-04-12 17:55:05 +00:00
# Parse title
$photo -> title = str_replace ( $badChars , '' , $photo -> title );
if ( ! isset ( $photo -> title ) || $photo -> title === '' ) $photo -> title = 'Untitled' ;
2014-04-12 16:00:11 +00:00
2014-04-12 17:55:05 +00:00
# Check if readable
if ( !@ is_readable ( $photo -> url )) continue ;
# Get extension of image
2014-05-20 10:44:08 +00:00
$extension = getExtension ( $photo -> url );
2014-04-04 17:34:12 +00:00
2014-04-12 17:55:05 +00:00
# Set title for photo
2014-06-14 21:05:45 +00:00
$zipFileName = $zipTitle . '/' . $photo -> title . $extension ;
2014-04-12 17:55:05 +00:00
# Check for duplicates
if ( ! empty ( $files )) {
$i = 1 ;
while ( in_array ( $zipFileName , $files )) {
# Set new title for photo
2014-05-20 10:44:08 +00:00
$zipFileName = $zipTitle . '/' . $photo -> title . '-' . $i . $extension ;
2014-04-12 17:55:05 +00:00
$i ++ ;
}
}
2014-04-12 16:00:11 +00:00
2014-04-12 17:55:05 +00:00
# Add to array
$files [] = $zipFileName ;
2014-04-12 16:00:11 +00:00
2014-04-12 17:55:05 +00:00
# Add photo to zip
$zip -> addFile ( $photo -> url , $zipFileName );
2014-04-12 16:00:11 +00:00
2014-04-04 17:34:12 +00:00
}
# Finish zip
$zip -> close ();
# Send zip
header ( " Content-Type: application/zip " );
header ( " Content-Disposition: attachment; filename= \" $zipTitle .zip \" " );
2014-04-26 17:37:02 +00:00
header ( " Content-Length: " . filesize ( $filename ));
2014-04-04 17:34:12 +00:00
readfile ( $filename );
# Delete zip
unlink ( $filename );
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-04-04 17:34:12 +00:00
return true ;
}
2014-04-03 16:38:58 +00:00
public function setTitle ( $title = 'Untitled' ) {
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> albumIDs ));
2014-04-03 16:38:58 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
2014-04-03 16:38:58 +00:00
# Parse
if ( strlen ( $title ) > 50 ) $title = substr ( $title , 0 , 50 );
2014-09-17 21:11:02 +00:00
2014-04-03 16:38:58 +00:00
# Execute query
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " UPDATE ? SET title = '?' WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $title , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$result = $this -> database -> query ( $query );
2014-04-03 16:38:58 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-04-03 16:38:58 +00:00
2014-05-06 19:24:58 +00:00
if ( ! $result ) {
Log :: error ( $this -> database , __METHOD__ , __LINE__ , $this -> database -> error );
return false ;
}
2014-04-03 16:38:58 +00:00
return true ;
}
public function setDescription ( $description = '' ) {
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> albumIDs ));
2014-04-03 16:38:58 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
2014-04-03 16:38:58 +00:00
# Parse
2014-08-26 08:15:17 +00:00
$description = htmlentities ( $description , ENT_COMPAT | ENT_HTML401 , 'UTF-8' );
2014-05-06 19:37:21 +00:00
if ( strlen ( $description ) > 1000 ) $description = substr ( $description , 0 , 1000 );
2014-04-03 16:38:58 +00:00
# Execute query
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " UPDATE ? SET description = '?' WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $description , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$result = $this -> database -> query ( $query );
2014-04-03 16:38:58 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-04-03 16:38:58 +00:00
2014-05-06 19:24:58 +00:00
if ( ! $result ) {
Log :: error ( $this -> database , __METHOD__ , __LINE__ , $this -> database -> error );
return false ;
}
2014-04-03 16:38:58 +00:00
return true ;
}
2014-04-05 13:59:31 +00:00
public function getPublic () {
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> albumIDs ));
2014-04-05 13:59:31 +00:00
# Call plugins
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
if ( $this -> albumIDs === '0' || $this -> albumIDs === 's' || $this -> albumIDs === 'f' ) return false ;
# Execute query
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " SELECT public FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$albums = $this -> database -> query ( $query );
2014-04-05 13:59:31 +00:00
$album = $albums -> fetch_object ();
# Call plugins
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
if ( $album -> public == 1 ) return true ;
return false ;
}
2014-08-17 18:22:46 +00:00
public function getDownloadable () {
# Check dependencies
self :: dependencies ( isset ( $this -> database , $this -> albumIDs ));
# Call plugins
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
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
# Execute query
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " SELECT downloadable FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$albums = $this -> database -> query ( $query );
2014-08-17 18:22:46 +00:00
$album = $albums -> fetch_object ();
# Call plugins
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
if ( $album -> downloadable == 1 ) return true ;
return false ;
}
2015-02-28 22:42:13 +00:00
public function setPublic ( $public , $password , $visible , $downloadable ) {
2014-04-04 15:56:08 +00:00
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> albumIDs ));
2014-04-04 15:56:08 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
2014-04-04 15:56:08 +00:00
2015-02-28 22:42:13 +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
2015-02-28 22:42:13 +00:00
# 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 ;
}
2014-08-09 15:57:31 +00:00
2015-02-28 22:42:13 +00:00
# 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 ));
2014-08-29 17:27:09 +00:00
$result = $this -> database -> query ( $query );
2014-05-06 19:24:58 +00:00
if ( ! $result ) {
Log :: error ( $this -> database , __METHOD__ , __LINE__ , $this -> database -> error );
return false ;
}
2014-04-04 15:56:08 +00:00
}
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-04-04 15:56:08 +00:00
# Set password
if ( isset ( $password ) && strlen ( $password ) > 0 ) return $this -> setPassword ( $password );
return true ;
}
2014-07-21 20:38:56 +00:00
private function setPassword ( $password ) {
2014-04-03 16:38:58 +00:00
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> albumIDs ));
2014-04-03 16:38:58 +00:00
# Call plugins
2014-04-05 13:59:31 +00:00
$this -> plugins ( __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 ) {
# Get hashed password
2015-02-08 14:36:13 +00:00
$password = getHashedString ( $password );
2014-04-21 00:19:23 +00:00
2014-04-21 12:18:13 +00:00
# Set hashed password
2014-08-29 17:27:09 +00:00
# Do not prepare $password because it is hashed and save
# Preparing (escaping) the password would destroy the hash
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " 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
# Unset password
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " 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
2015-02-08 14:36:13 +00:00
# Execute query
$result = $this -> database -> query ( $query );
2014-04-03 16:38:58 +00:00
# Call plugins
2014-04-05 13:59:31 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-04-03 16:38:58 +00:00
2014-05-06 19:24:58 +00:00
if ( ! $result ) {
Log :: error ( $this -> database , __METHOD__ , __LINE__ , $this -> database -> error );
return false ;
}
2014-04-03 16:38:58 +00:00
return true ;
}
2014-04-04 17:34:12 +00:00
public function checkPassword ( $password ) {
2014-04-04 15:18:59 +00:00
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> albumIDs ));
2014-04-04 15:18:59 +00:00
2014-04-04 15:56:08 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
2014-04-04 15:18:59 +00:00
# Execute query
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " SELECT password FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$albums = $this -> database -> query ( $query );
2014-04-04 17:34:12 +00:00
$album = $albums -> fetch_object ();
2014-04-04 15:18:59 +00:00
2014-04-04 17:34:12 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-04-04 15:18:59 +00:00
2014-04-04 17:34:12 +00:00
if ( $album -> password == '' ) return true ;
2015-02-08 14:36:13 +00:00
else 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
2014-08-29 17:27:09 +00:00
public function delete () {
2014-04-04 15:18:59 +00:00
2014-04-19 19:07:36 +00:00
# Check dependencies
2014-06-25 12:50:49 +00:00
self :: dependencies ( isset ( $this -> database , $this -> albumIDs ));
2014-04-04 15:18:59 +00:00
2014-04-04 17:34:12 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 0 , func_get_args ());
2014-04-04 15:18:59 +00:00
2014-04-04 17:34:12 +00:00
# Init vars
$error = false ;
2014-04-04 15:18:59 +00:00
2014-04-04 17:34:12 +00:00
# Execute query
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " SELECT id FROM ? WHERE album IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$photos = $this -> database -> query ( $query );
2014-04-04 17:34:12 +00:00
# For each album delete photo
2014-04-05 13:59:31 +00:00
while ( $row = $photos -> fetch_object ()) {
2014-04-11 22:30:26 +00:00
$photo = new Photo ( $this -> database , $this -> plugins , null , $row -> id );
2014-04-05 13:59:31 +00:00
if ( ! $photo -> delete ( $row -> id )) $error = true ;
}
2014-04-04 17:34:12 +00:00
# Delete albums
2014-08-30 05:05:26 +00:00
$query = Database :: prepare ( $this -> database , " DELETE FROM ? WHERE id IN (?) " , array ( LYCHEE_TABLE_ALBUMS , $this -> albumIDs ));
2014-08-29 17:27:09 +00:00
$result = $this -> database -> query ( $query );
2014-04-04 15:18:59 +00:00
2014-04-04 15:56:08 +00:00
# Call plugins
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
2014-04-04 15:56:08 +00:00
2014-05-06 19:37:21 +00:00
if ( $error ) return false ;
if ( ! $result ) {
Log :: error ( $this -> database , __METHOD__ , __LINE__ , $this -> database -> error );
return false ;
}
2014-04-04 15:18:59 +00:00
return true ;
}
2014-04-28 08:17:26 +00:00
}
?>