2014-01-22 10:12:51 +00:00
< ? php
/**
2014-02-17 16:01:46 +00:00
* @ name Album Module
* @ author Philipp Maurer
* @ author Tobias Reich
* @ copyright 2014 by Philipp Maurer , Tobias Reich
2014-01-22 10:12:51 +00:00
*/
if ( ! defined ( 'LYCHEE' )) exit ( 'Error: Direct access is not allowed!' );
function addAlbum ( $title ) {
global $database ;
2014-02-17 16:01:46 +00:00
if ( strlen ( $title ) < 1 || strlen ( $title ) > 50 ) return false ;
$sysdate = date ( " d.m.Y " );
$result = $database -> query ( " INSERT INTO lychee_albums (title, sysdate) VALUES (' $title ', ' $sysdate '); " );
if ( ! $result ) return false ;
return $database -> insert_id ;
2014-01-22 10:12:51 +00:00
}
function getAlbums ( $public ) {
global $database , $settings ;
2014-02-17 16:01:46 +00:00
// Smart Albums
if ( ! $public ) $return = getSmartInfo ();
// Albums
if ( $public ) $query = " SELECT id, title, public, sysdate, password FROM lychee_albums WHERE public = 1 " ;
else $query = " SELECT id, title, public, sysdate, password FROM lychee_albums " ;
$result = $database -> query ( $query ) OR exit ( " Error: $result <br> " . $database -> error );
$i = 0 ;
while ( $row = $result -> fetch_object ()) {
// Info
$return [ " content " ][ $row -> id ][ 'id' ] = $row -> id ;
$return [ " content " ][ $row -> id ][ 'title' ] = $row -> title ;
$return [ " content " ][ $row -> id ][ 'public' ] = $row -> public ;
$return [ " content " ][ $row -> id ][ 'sysdate' ] = date ( 'F Y' , strtotime ( $row -> sysdate ));
// Password
if ( $row -> password == " " ) $return [ " content " ][ $row -> id ][ 'password' ] = false ;
else $return [ " content " ][ $row -> id ][ 'password' ] = true ;
// Thumbs
if (( $public && $row -> password == " " ) || ( ! $public )) {
$albumID = $row -> id ;
$result2 = $database -> query ( " SELECT thumbUrl FROM lychee_photos WHERE album = ' $albumID ' ORDER BY star DESC, " . substr ( $settings [ 'sorting' ], 9 ) . " LIMIT 0, 3 " );
$k = 0 ;
while ( $row2 = $result2 -> fetch_object ()){
$return [ " content " ][ $row -> id ][ " thumb $k " ] = $row2 -> thumbUrl ;
$k ++ ;
}
if ( ! isset ( $return [ " content " ][ $row -> id ][ " thumb0 " ])) $return [ " content " ][ $row -> id ][ " thumb0 " ] = " " ;
if ( ! isset ( $return [ " content " ][ $row -> id ][ " thumb1 " ])) $return [ " content " ][ $row -> id ][ " thumb1 " ] = " " ;
if ( ! isset ( $return [ " content " ][ $row -> id ][ " thumb2 " ])) $return [ " content " ][ $row -> id ][ " thumb2 " ] = " " ;
}
// Album count
$i ++ ;
}
$return [ " num " ] = $i ;
return $return ;
2014-01-22 10:12:51 +00:00
}
function getSmartInfo () {
global $database , $settings ;
// Unsorted
2014-02-17 16:01:46 +00:00
$result = $database -> query ( " SELECT thumbUrl FROM lychee_photos WHERE album = 0 " . $settings [ 'sorting' ]);
$i = 0 ;
while ( $row = $result -> fetch_object ()) {
if ( $i < 3 ) $return [ " unsortedThumb $i " ] = $row -> thumbUrl ;
$i ++ ;
}
$return [ 'unsortedNum' ] = $i ;
2014-01-22 10:12:51 +00:00
// Public
2014-02-17 16:01:46 +00:00
$result2 = $database -> query ( " SELECT thumbUrl FROM lychee_photos WHERE public = 1 " . $settings [ 'sorting' ]);
$i = 0 ;
while ( $row2 = $result2 -> fetch_object ()) {
if ( $i < 3 ) $return [ " publicThumb $i " ] = $row2 -> thumbUrl ;
$i ++ ;
}
$return [ 'publicNum' ] = $i ;
2014-01-22 10:12:51 +00:00
// Starred
2014-02-17 16:01:46 +00:00
$result3 = $database -> query ( " SELECT thumbUrl FROM lychee_photos WHERE star = 1 " . $settings [ 'sorting' ]);
$i = 0 ;
while ( $row3 = $result3 -> fetch_object ()) {
if ( $i < 3 ) $return [ " starredThumb $i " ] = $row3 -> thumbUrl ;
$i ++ ;
}
$return [ 'starredNum' ] = $i ;
2014-01-22 10:12:51 +00:00
2014-02-17 16:01:46 +00:00
return $return ;
2014-01-22 10:12:51 +00:00
}
function getAlbum ( $albumID ) {
global $database , $settings ;
// Get album information
switch ( $albumID ) {
2014-02-17 16:01:46 +00:00
case " f " : $return [ 'public' ] = false ;
$query = " SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE star = 1 " . $settings [ 'sorting' ];
break ;
2014-01-22 10:12:51 +00:00
2014-02-17 16:01:46 +00:00
case " s " : $return [ 'public' ] = false ;
$query = " SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE public = 1 " . $settings [ 'sorting' ];
break ;
2014-01-22 10:12:51 +00:00
2014-02-17 16:01:46 +00:00
case " 0 " : $return [ 'public' ] = false ;
$query = " SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE album = 0 " . $settings [ 'sorting' ];
2014-01-26 14:16:59 +00:00
break ;
2014-01-22 10:12:51 +00:00
2014-02-17 16:01:46 +00:00
default : $result = $database -> query ( " SELECT * FROM lychee_albums WHERE id = ' $albumID '; " );
$row = $result -> fetch_object ();
$return [ 'title' ] = $row -> title ;
$return [ 'description' ] = $row -> description ;
$return [ 'sysdate' ] = date ( 'd M. Y' , strtotime ( $row -> sysdate ));
$return [ 'public' ] = $row -> public ;
$return [ 'password' ] = ( $row -> password == " " ? false : true );
$query = " SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE album = ' $albumID ' " . $settings [ 'sorting' ];
break ;
2014-01-22 10:12:51 +00:00
}
// Get photos
2014-02-09 21:30:16 +00:00
$result = $database -> query ( $query );
$previousPhotoID = " " ;
$i = 0 ;
2014-01-22 10:12:51 +00:00
while ( $row = $result -> fetch_array ()) {
2014-02-17 16:01:46 +00:00
$return [ 'content' ][ $row [ 'id' ]][ 'id' ] = $row [ 'id' ];
$return [ 'content' ][ $row [ 'id' ]][ 'title' ] = $row [ 'title' ];
$return [ 'content' ][ $row [ 'id' ]][ 'sysdate' ] = date ( 'd F Y' , strtotime ( $row [ 'sysdate' ]));
$return [ 'content' ][ $row [ 'id' ]][ 'public' ] = $row [ 'public' ];
$return [ 'content' ][ $row [ 'id' ]][ 'star' ] = $row [ 'star' ];
$return [ 'content' ][ $row [ 'id' ]][ 'tags' ] = $row [ 'tags' ];
$return [ 'content' ][ $row [ 'id' ]][ 'album' ] = $row [ 'album' ];
$return [ 'content' ][ $row [ 'id' ]][ 'thumbUrl' ] = $row [ 'thumbUrl' ];
2014-02-09 21:30:16 +00:00
2014-02-17 16:01:46 +00:00
$return [ 'content' ][ $row [ 'id' ]][ 'previousPhoto' ] = $previousPhotoID ;
$return [ 'content' ][ $row [ 'id' ]][ 'nextPhoto' ] = " " ;
if ( $previousPhotoID != " " ) $return [ 'content' ][ $previousPhotoID ][ 'nextPhoto' ] = $row [ 'id' ];
2014-01-22 10:12:51 +00:00
2014-02-17 16:01:46 +00:00
$previousPhotoID = $row [ 'id' ];
$i ++ ;
2014-01-22 10:12:51 +00:00
}
if ( $i == 0 ) {
// Empty album
$return [ 'content' ] = false ;
} else {
// Enable next and previous for the first and last photo
2014-02-09 21:30:16 +00:00
$lastElement = end ( $return [ 'content' ]);
$lastElementId = $lastElement [ 'id' ];
$firstElement = reset ( $return [ 'content' ]);
$firstElementId = $firstElement [ 'id' ];
2014-01-22 10:12:51 +00:00
if ( $lastElementId !== $firstElementId ) {
2014-02-09 21:30:16 +00:00
$return [ 'content' ][ $lastElementId ][ 'nextPhoto' ] = $firstElementId ;
$return [ 'content' ][ $firstElementId ][ 'previousPhoto' ] = $lastElementId ;
2014-01-22 10:12:51 +00:00
}
}
2014-02-09 21:30:16 +00:00
$return [ 'id' ] = $albumID ;
$return [ 'num' ] = $i ;
2014-01-22 10:12:51 +00:00
return $return ;
}
2014-01-31 20:22:25 +00:00
function setAlbumTitle ( $albumIDs , $title ) {
2014-01-22 10:12:51 +00:00
global $database ;
2014-02-17 16:01:46 +00:00
if ( strlen ( $title ) < 1 || strlen ( $title ) > 50 ) return false ;
$result = $database -> query ( " UPDATE lychee_albums SET title = ' $title ' WHERE id IN ( $albumIDs ); " );
if ( ! $result ) return false ;
return true ;
2014-01-22 10:12:51 +00:00
}
function setAlbumDescription ( $albumID , $description ) {
global $database ;
2014-02-17 16:01:46 +00:00
2014-01-22 10:12:51 +00:00
$description = htmlentities ( $description );
2014-02-02 22:56:20 +00:00
if ( strlen ( $description ) > 1000 ) return false ;
2014-01-31 20:22:25 +00:00
$result = $database -> query ( " UPDATE lychee_albums SET description = ' $description ' WHERE id = ' $albumID '; " );
2014-02-17 16:01:46 +00:00
2014-01-31 20:22:25 +00:00
if ( ! $result ) return false ;
return true ;
2014-01-22 10:12:51 +00:00
}
2014-01-31 20:22:25 +00:00
function deleteAlbum ( $albumIDs ) {
2014-01-22 10:12:51 +00:00
global $database ;
2014-02-17 16:01:46 +00:00
2014-02-09 21:30:16 +00:00
$error = false ;
$result = $database -> query ( " SELECT id FROM lychee_photos WHERE album IN ( $albumIDs ); " );
2014-02-17 16:01:46 +00:00
2014-01-31 20:22:25 +00:00
// Delete photos
while ( $row = $result -> fetch_object ())
if ( ! deletePhoto ( $row -> id )) $error = true ;
2014-02-17 16:01:46 +00:00
2014-01-31 20:22:25 +00:00
// Delete album
$result = $database -> query ( " DELETE FROM lychee_albums WHERE id IN ( $albumIDs ); " );
2014-02-17 16:01:46 +00:00
2014-01-31 20:22:25 +00:00
if ( $error ||! $result ) return false ;
return true ;
2014-01-22 10:12:51 +00:00
}
function getAlbumArchive ( $albumID ) {
global $database ;
2014-02-17 16:01:46 +00:00
2014-02-07 23:08:18 +00:00
switch ( $albumID ) {
2014-02-17 16:01:46 +00:00
case 's' :
$query = " SELECT url FROM lychee_photos WHERE public = '1'; " ;
$zipTitle = " Public " ;
break ;
case 'f' :
$query = " SELECT url FROM lychee_photos WHERE star = '1'; " ;
$zipTitle = " Starred " ;
break ;
default :
$query = " SELECT url FROM lychee_photos WHERE album = ' $albumID '; " ;
$zipTitle = " Unsorted " ;
2014-02-07 23:08:18 +00:00
}
2014-02-17 16:01:46 +00:00
2014-02-09 21:30:16 +00:00
$zip = new ZipArchive ();
$result = $database -> query ( $query );
$files = array ();
$i = 0 ;
2014-02-17 16:01:46 +00:00
2014-02-07 23:08:18 +00:00
while ( $row = $result -> fetch_object ()) {
2014-02-17 16:01:46 +00:00
$files [ $i ] = " ../uploads/big/ " . $row -> url ;
$i ++ ;
2014-02-07 23:08:18 +00:00
}
2014-02-17 16:01:46 +00:00
2014-02-10 17:04:34 +00:00
$result = $database -> query ( " SELECT title FROM lychee_albums WHERE id = ' $albumID ' LIMIT 1; " );
2014-02-07 23:08:18 +00:00
$row = $result -> fetch_object ();
if ( $albumID != 0 && is_numeric ( $albumID )) $zipTitle = $row -> title ;
$filename = " ../data/ $zipTitle .zip " ;
2014-02-17 16:01:46 +00:00
2014-02-07 23:08:18 +00:00
if ( $zip -> open ( $filename , ZIPARCHIVE :: CREATE ) !== TRUE ) {
2014-02-17 16:01:46 +00:00
return false ;
2014-02-07 23:08:18 +00:00
}
2014-02-17 16:01:46 +00:00
2014-02-07 23:08:18 +00:00
foreach ( $files AS $zipFile ) {
2014-02-17 16:01:46 +00:00
$newFile = explode ( " / " , $zipFile );
$newFile = array_reverse ( $newFile );
$zip -> addFile ( $zipFile , $zipTitle . " / " . $newFile [ 0 ]);
2014-02-07 23:08:18 +00:00
}
2014-02-17 16:01:46 +00:00
2014-02-07 23:08:18 +00:00
$zip -> close ();
2014-02-17 16:01:46 +00:00
2014-02-07 23:08:18 +00:00
header ( " Content-Type: application/zip " );
header ( " Content-Disposition: attachment; filename= \" $zipTitle .zip \" " );
header ( " Content-Length: " . filesize ( $filename ));
readfile ( $filename );
unlink ( $filename );
2014-02-17 16:01:46 +00:00
2014-02-07 23:08:18 +00:00
return true ;
2014-01-22 10:12:51 +00:00
}
function setAlbumPublic ( $albumID , $password ) {
global $database ;
2014-02-09 21:30:16 +00:00
$result = $database -> query ( " SELECT public FROM lychee_albums WHERE id = ' $albumID '; " );
$row = $result -> fetch_object ();
2014-02-11 11:36:06 +00:00
$public = ( $row -> public == '0' ? 1 : 0 );
2014-02-17 16:01:46 +00:00
2014-01-22 10:12:51 +00:00
$result = $database -> query ( " UPDATE lychee_albums SET public = ' $public ', password = NULL WHERE id = ' $albumID '; " );
if ( ! $result ) return false ;
2014-02-17 16:01:46 +00:00
2014-01-22 10:12:51 +00:00
if ( $public == 1 ) {
$result = $database -> query ( " UPDATE lychee_photos SET public = 0 WHERE album = ' $albumID '; " );
if ( ! $result ) return false ;
}
if ( strlen ( $password ) > 0 ) return setAlbumPassword ( $albumID , $password );
2014-01-31 20:22:25 +00:00
return true ;
2014-01-22 10:12:51 +00:00
}
function setAlbumPassword ( $albumID , $password ) {
global $database ;
$result = $database -> query ( " UPDATE lychee_albums SET password = ' $password ' WHERE id = ' $albumID '; " );
if ( ! $result ) return false ;
return true ;
}
function checkAlbumPassword ( $albumID , $password ) {
global $database ;
2014-02-09 21:30:16 +00:00
$result = $database -> query ( " SELECT password FROM lychee_albums WHERE id = ' $albumID '; " );
$row = $result -> fetch_object ();
2014-02-17 16:01:46 +00:00
2014-01-22 10:12:51 +00:00
if ( $row -> password == " " ) return true ;
else if ( $row -> password == $password ) return true ;
2014-01-31 20:22:25 +00:00
return false ;
2014-01-22 10:12:51 +00:00
}
function isAlbumPublic ( $albumID ) {
global $database ;
2014-02-17 16:01:46 +00:00
2014-02-09 21:30:16 +00:00
$result = $database -> query ( " SELECT public FROM lychee_albums WHERE id = ' $albumID '; " );
$row = $result -> fetch_object ();
2014-01-22 10:12:51 +00:00
2014-02-14 17:58:21 +00:00
if ( $albumID === '0' || $albumID === 's' || $albumID === 'f' ) return false ;
2014-01-22 10:12:51 +00:00
if ( $row -> public == 1 ) return true ;
2014-01-31 20:22:25 +00:00
return false ;
2014-01-22 10:12:51 +00:00
}
?>