2014-04-02 20:14:20 +00:00
< ? php
###
# @name Album Module
# @author Tobias Reich
# @copyright 2014 by Tobias Reich
###
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
$this -> 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 ();
$result = $this -> database -> query ( " INSERT INTO lychee_albums (title, sysstamp, public, visible) VALUES (' $title ', ' $sysstamp ', ' $public ', ' $visible '); " );
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 ;
}
2014-04-04 17:34:12 +00:00
public function get () {
2014-04-19 19:07:36 +00:00
# Check dependencies
$this -> 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-04-05 14:22:46 +00:00
$query = " SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE star = 1 " . $this -> settings [ 'sorting' ];
2014-04-04 17:34:12 +00:00
break ;
case 's' : $return [ 'public' ] = false ;
2014-04-05 14:22:46 +00:00
$query = " SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE public = 1 " . $this -> settings [ 'sorting' ];
2014-04-04 17:34:12 +00:00
break ;
case '0' : $return [ 'public' ] = false ;
2014-04-05 14:22:46 +00:00
$query = " SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE album = 0 " . $this -> settings [ 'sorting' ];
2014-04-04 17:34:12 +00:00
break ;
default : $albums = $this -> database -> query ( " SELECT * FROM lychee_albums WHERE id = ' $this->albumIDs ' LIMIT 1; " );
$return = $albums -> fetch_assoc ();
2014-04-11 08:38:28 +00:00
$return [ 'sysdate' ] = date ( 'd M. Y' , $return [ 'sysstamp' ]);
2014-04-04 17:34:12 +00:00
$return [ 'password' ] = ( $return [ 'password' ] == '' ? false : true );
2014-04-05 14:22:46 +00:00
$query = " SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE album = ' $this->albumIDs ' " . $this -> settings [ 'sorting' ];
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
# Parse
2014-04-05 14:22:46 +00:00
$photo [ 'sysdate' ] = date ( 'd F Y' , substr ( $photo [ 'id' ], 0 , - 4 ));
2014-04-04 17:34:12 +00:00
$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
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
$this -> 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
# Get SmartAlbums
2014-04-04 17:34:12 +00:00
if ( $public === false ) $return = $this -> getSmartInfo ();
2014-04-02 20:14:20 +00:00
# Albums query
2014-04-11 08:38:28 +00:00
$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' ;
2014-04-02 20:14:20 +00:00
# Execute query
$albums = $this -> database -> query ( $query ) OR exit ( 'Error: ' . $this -> database -> error );
# For each album
while ( $album = $albums -> fetch_assoc ()) {
# Parse info
2014-04-11 08:38:28 +00:00
$album [ 'sysdate' ] = date ( 'F Y' , $album [ 'sysstamp' ]);
2014-04-02 20:14:20 +00:00
$album [ 'password' ] = ( $album [ 'password' ] != '' );
# Thumbs
if (( $public === true && $album [ 'password' ] === false ) || ( $public === false )) {
# Execute query
2014-04-04 21:17:54 +00:00
$thumbs = $this -> database -> query ( " SELECT thumbUrl FROM lychee_photos WHERE album = ' " . $album [ 'id' ] . " ' ORDER BY star DESC, " . substr ( $this -> settings [ 'sorting' ], 9 ) . " LIMIT 3 " );
2014-04-02 20:14:20 +00:00
# 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
2014-04-04 18:11:45 +00:00
$this -> plugins ( __METHOD__ , 1 , func_get_args ());
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
$this -> dependencies ( isset ( $this -> database , $this -> settings ));
2014-04-04 17:34:12 +00:00
# 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 () {
2014-04-19 19:07:36 +00:00
# Check dependencies
$this -> 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
# Photos query
switch ( $this -> albumIDs ) {
case 's' :
2014-04-12 17:55:05 +00:00
$photos = " SELECT title, url FROM lychee_photos WHERE public = '1'; " ;
2014-04-04 17:34:12 +00:00
$zipTitle = 'Public' ;
break ;
case 'f' :
2014-04-12 17:55:05 +00:00
$photos = " SELECT title, url FROM lychee_photos WHERE star = '1'; " ;
2014-04-04 17:34:12 +00:00
$zipTitle = 'Starred' ;
break ;
default :
2014-04-12 17:55:05 +00:00
$photos = " SELECT title, url FROM lychee_photos WHERE album = ' $this->albumIDs '; " ;
2014-04-04 17:34:12 +00:00
$zipTitle = 'Unsorted' ;
}
2014-04-12 17:55:05 +00:00
# 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 ;
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 ();
if ( $zip -> open ( $filename , ZIPARCHIVE :: CREATE ) !== TRUE ) return false ;
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
if ( $photos -> num_rows == 0 ) return false ;
# 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
$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' ;
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
$extension = array_reverse ( explode ( '.' , $photo -> url ));
$extension = $extension [ 0 ];
2014-04-04 17:34:12 +00:00
2014-04-12 17:55:05 +00:00
# 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 ++ ;
}
}
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
$this -> 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 );
# Execute query
$result = $this -> database -> query ( " UPDATE lychee_albums SET title = ' $title ' WHERE id IN ( $this->albumIDs ); " );
# 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
$this -> 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
$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
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
$this -> 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
$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 ;
}
2014-04-04 15:56:08 +00:00
public function setPublic ( $password ) {
2014-04-19 19:07:36 +00:00
# Check dependencies
$this -> 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
# 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 '; " );
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
# Reset permissions for photos
if ( $public === 1 ) {
$result = $this -> database -> query ( " UPDATE lychee_photos SET public = 0 WHERE album = ' $album->id '; " );
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-04-04 17:34:12 +00:00
public function setPassword ( $password ) {
2014-04-03 16:38:58 +00:00
2014-04-19 19:07:36 +00:00
# Check dependencies
$this -> 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
2014-04-21 00:19:23 +00:00
$password = get_hashed_password ( $password );
2014-04-21 12:18:13 +00:00
# Set hashed password
2014-04-21 00:19:23 +00:00
$result = $this -> database -> query ( " UPDATE lychee_albums SET password = ' $password ' WHERE id IN (' $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-04-21 00:19:23 +00:00
$result = $this -> database -> query ( " UPDATE lychee_albums SET password = NULL WHERE id IN (' $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
# 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
$this -> 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-04-04 17:34:12 +00:00
$albums = $this -> database -> query ( " SELECT password FROM lychee_albums WHERE id = ' $this->albumIDs ' LIMIT 1; " );
$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 ;
2014-04-21 00:19:23 +00:00
else if ( $album -> password === $password || $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-04-04 17:34:12 +00:00
public function delete ( $albumIDs ) {
2014-04-04 15:18:59 +00:00
2014-04-19 19:07:36 +00:00
# Check dependencies
$this -> 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-04-05 13:59:31 +00:00
$photos = $this -> database -> query ( " SELECT id FROM lychee_photos WHERE album IN ( $albumIDs ); " );
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
$result = $this -> database -> query ( " DELETE FROM lychee_albums WHERE id IN ( $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__ , 1 , func_get_args ());
2014-04-04 15:56:08 +00:00
2014-04-04 17:34:12 +00:00
if ( $error ||! $result ) return false ;
2014-04-04 15:18:59 +00:00
return true ;
}
2014-04-28 08:17:26 +00:00
}
?>