2014-04-05 14:02:53 +00:00
< ? php
2016-01-26 14:31:53 +00:00
namespace Lychee\Modules ;
2014-04-05 14:02:53 +00:00
2016-01-30 22:24:08 +00:00
use ZipArchive ;
2016-01-30 20:33:31 +00:00
final class Photo {
2014-04-05 14:02:53 +00:00
2016-01-24 21:14:20 +00:00
private $photoIDs = null ;
2014-04-05 14:02:53 +00:00
2015-04-13 19:09:28 +00:00
public static $validTypes = array (
2014-04-25 08:13:43 +00:00
IMAGETYPE_JPEG ,
IMAGETYPE_GIF ,
IMAGETYPE_PNG
2014-05-23 13:47:48 +00:00
);
2016-01-19 10:03:28 +00:00
2015-04-13 19:09:28 +00:00
public static $validExtensions = array (
2014-04-25 08:13:43 +00:00
'.jpg' ,
'.jpeg' ,
'.png' ,
'.gif'
2014-05-23 13:47:48 +00:00
);
2014-04-25 08:13:43 +00:00
2016-01-24 21:14:20 +00:00
public function __construct ( $photoIDs ) {
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Init vars
$this -> photoIDs = $photoIDs ;
2014-04-05 14:02:53 +00:00
return true ;
}
2016-01-30 00:06:21 +00:00
public function add ( array $files , $albumID = 0 , $description = '' , $tags = '' , $returnOnError = false ) {
2015-06-28 21:09:41 +00:00
2016-01-30 20:43:57 +00:00
// Use $returnOnError if you want to handle errors by your own
// e.g. when calling this functions inside an if-condition
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Check permissions
2014-11-06 21:25:27 +00:00
if ( hasPermissions ( LYCHEE_UPLOADS ) === false ||
hasPermissions ( LYCHEE_UPLOADS_BIG ) === false ||
2014-12-06 13:29:17 +00:00
hasPermissions ( LYCHEE_UPLOADS_THUMB ) === false ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'An upload-folder is missing or not readable and writable' );
2014-11-06 21:25:27 +00:00
exit ( 'Error: An upload-folder is missing or not readable and writable!' );
2014-05-16 21:24:11 +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-11 22:30:26 +00:00
switch ( $albumID ) {
case 's' :
2016-01-30 20:43:57 +00:00
// s for public (share)
$public = 1 ;
$star = 0 ;
$albumID = 0 ;
2014-04-11 22:30:26 +00:00
break ;
case 'f' :
2016-01-30 20:43:57 +00:00
// f for starred (fav)
$star = 1 ;
$public = 0 ;
$albumID = 0 ;
2014-04-11 22:30:26 +00:00
break ;
2014-06-29 13:40:06 +00:00
case 'r' :
2016-01-30 20:43:57 +00:00
// r for recent
$public = 0 ;
$star = 0 ;
$albumID = 0 ;
2014-06-29 13:40:06 +00:00
break ;
2014-04-11 22:30:26 +00:00
default :
2016-01-30 20:43:57 +00:00
$star = 0 ;
$public = 0 ;
2014-04-11 22:30:26 +00:00
break ;
}
foreach ( $files as $file ) {
2016-01-30 20:43:57 +00:00
// Check if file exceeds the upload_max_filesize directive
2015-08-29 19:55:32 +00:00
if ( $file [ 'error' ] === UPLOAD_ERR_INI_SIZE ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'The uploaded file exceeds the upload_max_filesize directive in php.ini' );
2015-08-29 19:55:32 +00:00
if ( $returnOnError === true ) return false ;
exit ( 'Error: The uploaded file exceeds the upload_max_filesize directive in php.ini!' );
}
2016-01-30 20:43:57 +00:00
// Check if file was only partially uploaded
2015-08-29 19:55:32 +00:00
if ( $file [ 'error' ] === UPLOAD_ERR_PARTIAL ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'The uploaded file was only partially uploaded' );
2015-08-29 19:55:32 +00:00
if ( $returnOnError === true ) return false ;
exit ( 'Error: The uploaded file was only partially uploaded!' );
}
2016-01-30 20:43:57 +00:00
// Check if writing file to disk failed
2015-08-29 19:55:32 +00:00
if ( $file [ 'error' ] === UPLOAD_ERR_CANT_WRITE ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Failed to write photo to disk' );
2015-08-29 19:55:32 +00:00
if ( $returnOnError === true ) return false ;
exit ( 'Error: Failed to write photo to disk!' );
}
2016-01-30 20:43:57 +00:00
// Check if a extension stopped the file upload
2015-08-29 19:55:32 +00:00
if ( $file [ 'error' ] === UPLOAD_ERR_EXTENSION ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'A PHP extension stopped the file upload' );
2015-08-29 19:55:32 +00:00
if ( $returnOnError === true ) return false ;
exit ( 'Error: A PHP extension stopped the file upload!' );
}
2016-01-30 20:43:57 +00:00
// Check if the upload was successful
2015-08-29 19:55:32 +00:00
if ( $file [ 'error' ] !== UPLOAD_ERR_OK ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Upload contains an error (' . $file [ 'error' ] . ')' );
2015-08-29 19:55:32 +00:00
if ( $returnOnError === true ) return false ;
exit ( 'Error: Upload failed!' );
}
2016-01-30 20:43:57 +00:00
// Verify extension
2014-05-20 10:44:08 +00:00
$extension = getExtension ( $file [ 'name' ]);
2016-01-19 10:03:28 +00:00
if ( ! in_array ( strtolower ( $extension ), self :: $validExtensions , true )) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Photo format not supported' );
2015-06-28 21:09:41 +00:00
if ( $returnOnError === true ) return false ;
2015-04-13 19:09:28 +00:00
exit ( 'Error: Photo format not supported!' );
}
2014-04-25 08:13:43 +00:00
2016-01-30 20:43:57 +00:00
// Verify image
2014-04-25 08:13:43 +00:00
$type = @ exif_imagetype ( $file [ 'tmp_name' ]);
2016-01-19 10:03:28 +00:00
if ( ! in_array ( $type , self :: $validTypes , true )) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Photo type not supported' );
2015-06-28 21:09:41 +00:00
if ( $returnOnError === true ) return false ;
2015-04-13 19:09:28 +00:00
exit ( 'Error: Photo type not supported!' );
}
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Generate id
2014-04-11 22:30:26 +00:00
$id = str_replace ( '.' , '' , microtime ( true ));
while ( strlen ( $id ) < 14 ) $id .= 0 ;
2016-01-30 20:43:57 +00:00
// Set paths
$tmp_name = $file [ 'tmp_name' ];
$photo_name = md5 ( $id ) . $extension ;
$path = LYCHEE_UPLOADS_BIG . $photo_name ;
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Calculate checksum
2014-08-22 20:02:58 +00:00
$checksum = sha1_file ( $tmp_name );
2014-08-27 19:17:05 +00:00
if ( $checksum === false ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not calculate checksum for photo' );
2015-06-28 21:09:41 +00:00
if ( $returnOnError === true ) return false ;
2014-08-27 19:17:05 +00:00
exit ( 'Error: Could not calculate checksum for photo!' );
}
2014-08-22 20:02:58 +00:00
2016-01-30 20:43:57 +00:00
// Check if image exists based on checksum
2014-08-22 20:02:58 +00:00
if ( $checksum === false ) {
2016-01-30 20:43:57 +00:00
$checksum = '' ;
$exists = false ;
2014-08-22 20:02:58 +00:00
2014-04-11 22:30:26 +00:00
} else {
2014-08-22 20:02:58 +00:00
2014-08-22 20:14:50 +00:00
$exists = $this -> exists ( $checksum );
if ( $exists !== false ) {
2016-01-30 20:43:57 +00:00
$photo_name = $exists [ 'photo_name' ];
$path = $exists [ 'path' ];
$path_thumb = $exists [ 'path_thumb' ];
$medium = ( $exists [ 'medium' ] === '1' ? 1 : 0 );
$exists = true ;
2014-05-08 18:50:18 +00:00
}
2014-08-22 20:02:58 +00:00
2014-04-11 22:30:26 +00:00
}
2014-08-22 20:02:58 +00:00
if ( $exists === false ) {
2016-01-30 20:43:57 +00:00
// Import if not uploaded via web
2014-08-22 20:02:58 +00:00
if ( ! is_uploaded_file ( $tmp_name )) {
if ( !@ copy ( $tmp_name , $path )) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not copy photo to uploads' );
2015-06-28 21:09:41 +00:00
if ( $returnOnError === true ) return false ;
2014-08-22 20:02:58 +00:00
exit ( 'Error: Could not copy photo to uploads!' );
} else @ unlink ( $tmp_name );
} else {
if ( !@ move_uploaded_file ( $tmp_name , $path )) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not move photo to uploads' );
2015-06-28 21:09:41 +00:00
if ( $returnOnError === true ) return false ;
2014-08-22 20:02:58 +00:00
exit ( 'Error: Could not move photo to uploads!' );
}
}
2015-06-27 19:03:27 +00:00
} else {
2016-01-30 20:43:57 +00:00
// Photo already exists
// Check if the user wants to skip duplicates
2016-01-24 21:14:20 +00:00
if ( Settings :: get ()[ 'skipDuplicates' ] === '1' ) {
2016-01-31 17:49:31 +00:00
Log :: notice ( Database :: get (), __METHOD__ , __LINE__ , 'Skipped upload of existing photo because skipDuplicates is activated' );
2015-06-28 21:09:41 +00:00
if ( $returnOnError === true ) return false ;
2015-06-27 19:03:27 +00:00
exit ( 'Warning: This photo has been skipped because it\'s already in your library.' );
}
2014-08-22 20:02:58 +00:00
}
2014-07-20 14:45:30 +00:00
2016-01-30 20:43:57 +00:00
// Read infos
2014-04-11 22:30:26 +00:00
$info = $this -> getInfo ( $path );
2016-01-30 20:43:57 +00:00
// Use title of file if IPTC title missing
2014-08-29 19:16:10 +00:00
if ( $info [ 'title' ] === '' ) $info [ 'title' ] = substr ( basename ( $file [ 'name' ], $extension ), 0 , 30 );
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Use description parameter if set
2014-04-11 22:30:26 +00:00
if ( $description === '' ) $description = $info [ 'description' ];
2014-08-22 20:02:58 +00:00
if ( $exists === false ) {
2016-01-30 20:43:57 +00:00
// Set orientation based on EXIF data
2014-10-18 14:57:09 +00:00
if ( $file [ 'type' ] === 'image/jpeg' && isset ( $info [ 'orientation' ]) && $info [ 'orientation' ] !== '' ) {
$adjustFile = $this -> adjustFile ( $path , $info );
if ( $adjustFile !== false ) $info = $adjustFile ;
2016-01-31 17:49:31 +00:00
else Log :: notice ( Database :: get (), __METHOD__ , __LINE__ , 'Skipped adjustment of photo (' . $info [ 'title' ] . ')' );
2014-08-22 20:02:58 +00:00
}
2016-01-30 20:43:57 +00:00
// Set original date
2014-10-08 20:55:36 +00:00
if ( $info [ 'takestamp' ] !== '' && $info [ 'takestamp' ] !== 0 ) @ touch ( $path , $info [ 'takestamp' ]);
2014-08-22 20:02:58 +00:00
2016-01-30 20:43:57 +00:00
// Create Thumb
2014-10-18 14:57:09 +00:00
if ( ! $this -> createThumb ( $path , $photo_name , $info [ 'type' ], $info [ 'width' ], $info [ 'height' ])) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not create thumbnail for photo' );
2015-06-28 21:09:41 +00:00
if ( $returnOnError === true ) return false ;
2014-08-22 20:02:58 +00:00
exit ( 'Error: Could not create thumbnail for photo!' );
}
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Create Medium
2015-01-22 20:19:55 +00:00
if ( $this -> createMedium ( $path , $photo_name , $info [ 'width' ], $info [ 'height' ])) $medium = 1 ;
else $medium = 0 ;
2014-10-10 22:52:15 +00:00
2016-01-30 20:43:57 +00:00
// Set thumb url
2014-08-22 20:02:58 +00:00
$path_thumb = md5 ( $id ) . '.jpeg' ;
2014-04-13 10:10:49 +00:00
2014-05-08 18:50:18 +00:00
}
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Save to DB
$values = array ( LYCHEE_TABLE_PHOTOS , $id , $info [ 'title' ], $photo_name , $description , $tags , $info [ 'type' ], $info [ 'width' ], $info [ 'height' ], $info [ 'size' ], $info [ 'iso' ], $info [ 'aperture' ], $info [ 'make' ], $info [ 'model' ], $info [ 'shutter' ], $info [ 'focal' ], $info [ 'takestamp' ], $path_thumb , $albumID , $public , $star , $checksum , $medium );
$query = Database :: prepare ( Database :: get (), " INSERT INTO ? (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum, medium) VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?') " , $values );
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-11 22:30:26 +00:00
2016-01-31 14:44:54 +00:00
if ( $result === false ) {
2015-06-28 21:09:41 +00:00
if ( $returnOnError === true ) return false ;
2014-05-08 18:50:18 +00:00
exit ( 'Error: Could not save photo in database!' );
}
2014-04-11 22:30:26 +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-11 22:30:26 +00:00
return true ;
}
2014-08-22 20:54:33 +00:00
private function exists ( $checksum , $photoID = null ) {
2014-08-22 20:14:50 +00:00
2016-01-30 20:43:57 +00:00
// Exclude $photoID from select when $photoID is set
2016-01-24 21:14:20 +00:00
if ( isset ( $photoID )) $query = Database :: prepare ( Database :: get (), " SELECT id, url, thumbUrl, medium FROM ? WHERE checksum = '?' AND id <> '?' LIMIT 1 " , array ( LYCHEE_TABLE_PHOTOS , $checksum , $photoID ));
else $query = Database :: prepare ( Database :: get (), " SELECT id, url, thumbUrl, medium FROM ? WHERE checksum = '?' LIMIT 1 " , array ( LYCHEE_TABLE_PHOTOS , $checksum ));
2014-08-22 20:54:33 +00:00
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-08-22 20:14:50 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-08-22 20:14:50 +00:00
if ( $result -> num_rows === 1 ) {
2014-08-22 20:34:59 +00:00
$result = $result -> fetch_object ();
2014-08-22 20:14:50 +00:00
$return = array (
2016-01-30 20:43:57 +00:00
'photo_name' => $result -> url ,
'path' => LYCHEE_UPLOADS_BIG . $result -> url ,
'path_thumb' => $result -> thumbUrl ,
'medium' => $result -> medium
2014-08-22 20:14:50 +00:00
);
return $return ;
}
return false ;
}
2014-10-18 14:57:09 +00:00
private function createThumb ( $url , $filename , $type , $width , $height ) {
2014-04-11 22:30:26 +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-11 22:30:26 +00:00
2016-02-01 08:44:23 +00:00
// Quality of thumbnails
$thumbQuality = 90 ;
2016-01-30 20:43:57 +00:00
// Size of the thumbnail
$newWidth = 200 ;
$newHeight = 200 ;
2014-10-18 14:57:09 +00:00
2016-01-30 20:43:57 +00:00
$photoName = explode ( '.' , $filename );
$newUrl = LYCHEE_UPLOADS_THUMB . $photoName [ 0 ] . '.jpeg' ;
$newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName [ 0 ] . '@2x.jpeg' ;
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Create thumbnails with Imagick
2016-01-24 21:14:20 +00:00
if ( extension_loaded ( 'imagick' ) && Settings :: get ()[ 'imagick' ] === '1' ) {
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Read image
2014-04-11 22:30:26 +00:00
$thumb = new Imagick ();
$thumb -> readImage ( $url );
2016-02-01 08:44:23 +00:00
$thumb -> setImageCompressionQuality ( $thumbQuality );
2014-04-11 22:30:26 +00:00
$thumb -> setImageFormat ( 'jpeg' );
2016-01-30 20:43:57 +00:00
// Copy image for 2nd thumb version
2014-04-11 22:30:26 +00:00
$thumb2x = clone $thumb ;
2016-01-30 20:43:57 +00:00
// Create 1st version
2014-10-18 14:57:09 +00:00
$thumb -> cropThumbnailImage ( $newWidth , $newHeight );
2014-04-11 22:30:26 +00:00
$thumb -> writeImage ( $newUrl );
2014-04-19 15:32:33 +00:00
$thumb -> clear ();
$thumb -> destroy ();
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Create 2nd version
2014-10-18 14:57:09 +00:00
$thumb2x -> cropThumbnailImage ( $newWidth * 2 , $newHeight * 2 );
2014-04-11 22:30:26 +00:00
$thumb2x -> writeImage ( $newUrl2x );
$thumb2x -> clear ();
$thumb2x -> destroy ();
} else {
2016-01-30 20:43:57 +00:00
// Create image
$thumb = imagecreatetruecolor ( $newWidth , $newHeight );
$thumb2x = imagecreatetruecolor ( $newWidth * 2 , $newHeight * 2 );
2014-10-10 22:52:15 +00:00
2016-01-30 20:43:57 +00:00
// Set position
2014-10-18 14:57:09 +00:00
if ( $width < $height ) {
2016-01-30 20:43:57 +00:00
$newSize = $width ;
$startWidth = 0 ;
$startHeight = $height / 2 - $width / 2 ;
2014-04-11 22:30:26 +00:00
} else {
2016-01-30 20:43:57 +00:00
$newSize = $height ;
$startWidth = $width / 2 - $height / 2 ;
$startHeight = 0 ;
2014-04-11 22:30:26 +00:00
}
2016-01-30 20:43:57 +00:00
// Create new image
2014-10-18 14:57:09 +00:00
switch ( $type ) {
2016-01-30 20:43:57 +00:00
case 'image/jpeg' : $sourceImg = imagecreatefromjpeg ( $url ); break ;
case 'image/png' : $sourceImg = imagecreatefrompng ( $url ); break ;
case 'image/gif' : $sourceImg = imagecreatefromgif ( $url ); break ;
2016-01-31 17:49:31 +00:00
default : Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Type of photo is not supported' );
2016-01-30 20:43:57 +00:00
return false ;
break ;
2014-04-11 22:30:26 +00:00
}
2016-01-30 20:43:57 +00:00
// Create thumb
2016-01-26 14:31:53 +00:00
fastImageCopyResampled ( $thumb , $sourceImg , 0 , 0 , $startWidth , $startHeight , $newWidth , $newHeight , $newSize , $newSize );
2016-02-01 08:44:23 +00:00
imagejpeg ( $thumb , $newUrl , $thumbQuality );
2014-04-19 15:32:33 +00:00
imagedestroy ( $thumb );
2016-01-30 20:43:57 +00:00
// Create retina thumb
2016-01-26 14:31:53 +00:00
fastImageCopyResampled ( $thumb2x , $sourceImg , 0 , 0 , $startWidth , $startHeight , $newWidth * 2 , $newHeight * 2 , $newSize , $newSize );
2016-02-01 08:44:23 +00:00
imagejpeg ( $thumb2x , $newUrl2x , $thumbQuality );
2014-04-19 15:32:33 +00:00
imagedestroy ( $thumb2x );
2016-01-30 20:43:57 +00:00
// Free memory
2014-04-19 15:32:33 +00:00
imagedestroy ( $sourceImg );
2014-04-11 22:30:26 +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-11 22:30:26 +00:00
return true ;
}
2014-10-11 14:09:10 +00:00
private function createMedium ( $url , $filename , $width , $height ) {
2014-10-10 22:52:15 +00:00
2016-01-30 20:43:57 +00:00
// Function creates a smaller version of a photo when its size is bigger than a preset size
// Excepts the following:
// (string) $url = Path to the photo-file
// (string) $filename = Name of the photo-file
// (int) $width = Width of the photo
// (int) $height = Height of the photo
// Returns the following
// (boolean) true = Success
// (boolean) false = Failure
// Call plugins
2016-01-29 23:27:50 +00:00
Plugins :: get () -> activate ( __METHOD__ , 0 , func_get_args ());
2014-10-10 22:52:15 +00:00
2016-01-30 20:43:57 +00:00
// Set to true when creation of medium-photo failed
2014-12-06 13:29:17 +00:00
$error = false ;
2016-01-30 20:43:57 +00:00
// Size of the medium-photo
// When changing these values,
// also change the size detection in the front-end
$newWidth = 1920 ;
$newHeight = 1080 ;
2014-10-11 14:09:10 +00:00
2016-01-30 20:43:57 +00:00
// Check permissions
2014-12-06 13:29:17 +00:00
if ( hasPermissions ( LYCHEE_UPLOADS_MEDIUM ) === false ) {
2016-01-30 20:43:57 +00:00
// Permissions are missing
2016-01-31 17:49:31 +00:00
Log :: notice ( Database :: get (), __METHOD__ , __LINE__ , 'Skipped creation of medium-photo, because uploads/medium/ is missing or not readable and writable.' );
2014-12-06 13:29:17 +00:00
$error = true ;
}
2016-01-30 20:43:57 +00:00
// Is photo big enough?
// Is medium activated?
// Is Imagick installed and activated?
2014-12-06 13:29:17 +00:00
if (( $error === false ) &&
( $width > $newWidth || $height > $newHeight ) &&
2016-01-24 21:14:20 +00:00
( Settings :: get ()[ 'medium' ] === '1' ) &&
( extension_loaded ( 'imagick' ) && Settings :: get ()[ 'imagick' ] === '1' )) {
2014-10-10 22:52:15 +00:00
$newUrl = LYCHEE_UPLOADS_MEDIUM . $filename ;
2016-01-30 20:43:57 +00:00
// Read image
2014-10-10 22:52:15 +00:00
$medium = new Imagick ();
$medium -> readImage ( $url );
2014-12-06 13:29:17 +00:00
2016-01-30 20:43:57 +00:00
// Adjust image
2014-10-10 22:52:15 +00:00
$medium -> scaleImage ( $newWidth , $newHeight , true );
2014-12-06 13:29:17 +00:00
2016-01-30 20:43:57 +00:00
// Save image
2014-12-06 13:29:17 +00:00
try { $medium -> writeImage ( $newUrl ); }
catch ( ImagickException $err ) {
2016-02-06 22:23:10 +00:00
Log :: notice ( Database :: get (), __METHOD__ , __LINE__ , 'Could not save medium-photo (' . $err -> getMessage () . ')' );
2014-12-06 13:29:17 +00:00
$error = true ;
}
2014-10-10 22:52:15 +00:00
$medium -> clear ();
$medium -> destroy ();
} else {
2016-01-30 20:43:57 +00:00
// Photo too small or
// Medium is deactivated or
// Imagick not installed
2014-10-10 22:52:15 +00:00
$error = true ;
}
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-10-10 22:52:15 +00:00
if ( $error === true ) return false ;
return true ;
}
2016-01-30 00:06:21 +00:00
public function adjustFile ( $path , array $info ) {
2014-04-12 12:15:21 +00:00
2016-01-30 20:43:57 +00:00
// Function rotates and flips a photo based on its EXIF orientation
// Excepts the following:
// (string) $path = Path to the photo-file
// (array) $info = ['orientation', 'width', 'height']
// Returns the following
// (array) $info = ['orientation', 'width', 'height'] = Success
// (boolean) false = Failure
2015-04-06 17:08: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-12 12:15:21 +00:00
2014-10-18 14:57:09 +00:00
$swapSize = false ;
2016-01-24 21:14:20 +00:00
if ( extension_loaded ( 'imagick' ) && Settings :: get ()[ 'imagick' ] === '1' ) {
2014-04-12 12:15:21 +00:00
switch ( $info [ 'orientation' ]) {
case 3 :
$rotateImage = 180 ;
break ;
case 6 :
2016-01-30 20:43:57 +00:00
$rotateImage = 90 ;
$swapSize = true ;
2014-04-12 12:15:21 +00:00
break ;
case 8 :
2016-01-30 20:43:57 +00:00
$rotateImage = 270 ;
$swapSize = true ;
2014-10-18 14:57:09 +00:00
break ;
default :
return false ;
2014-04-12 12:15:21 +00:00
break ;
}
2014-04-19 15:32:33 +00:00
if ( $rotateImage !== 0 ) {
2014-04-12 12:15:21 +00:00
$image = new Imagick ();
$image -> readImage ( $path );
$image -> rotateImage ( new ImagickPixel (), $rotateImage );
2014-10-18 14:57:09 +00:00
$image -> setImageOrientation ( 1 );
2014-04-12 12:15:21 +00:00
$image -> writeImage ( $path );
$image -> clear ();
$image -> destroy ();
}
} else {
2016-01-30 20:43:57 +00:00
$newWidth = $info [ 'width' ];
$newHeight = $info [ 'height' ];
$sourceImg = imagecreatefromjpeg ( $path );
2014-04-12 12:15:21 +00:00
switch ( $info [ 'orientation' ]) {
case 2 :
2016-01-30 20:43:57 +00:00
// mirror
// not yet implemented
2014-10-18 14:57:09 +00:00
return false ;
2014-04-12 12:15:21 +00:00
break ;
case 3 :
2016-01-30 20:43:57 +00:00
$sourceImg = imagerotate ( $sourceImg , - 180 , 0 );
2014-04-12 12:15:21 +00:00
break ;
case 4 :
2016-01-30 20:43:57 +00:00
// rotate 180 and mirror
// not yet implemented
2014-10-18 14:57:09 +00:00
return false ;
2014-04-12 12:15:21 +00:00
break ;
case 5 :
2016-01-30 20:43:57 +00:00
// rotate 90 and mirror
// not yet implemented
2014-10-18 14:57:09 +00:00
return false ;
2014-04-12 12:15:21 +00:00
break ;
case 6 :
2016-01-30 20:43:57 +00:00
$sourceImg = imagerotate ( $sourceImg , - 90 , 0 );
$newWidth = $info [ 'height' ];
$newHeight = $info [ 'width' ];
$swapSize = true ;
2014-04-12 12:15:21 +00:00
break ;
case 7 :
2016-01-30 20:43:57 +00:00
// rotate -90 and mirror
// not yet implemented
2014-10-18 14:57:09 +00:00
return false ;
2014-04-12 12:15:21 +00:00
break ;
case 8 :
2016-01-30 20:43:57 +00:00
$sourceImg = imagerotate ( $sourceImg , 90 , 0 );
$newWidth = $info [ 'height' ];
$newHeight = $info [ 'width' ];
$swapSize = true ;
2014-04-12 12:15:21 +00:00
break ;
2014-10-18 14:57:09 +00:00
default :
return false ;
break ;
2014-04-12 12:15:21 +00:00
2014-10-18 14:57:09 +00:00
}
2014-04-19 15:32:33 +00:00
2016-01-30 20:43:57 +00:00
// Recreate photo
2014-10-18 14:57:09 +00:00
$newSourceImg = imagecreatetruecolor ( $newWidth , $newHeight );
imagecopyresampled ( $newSourceImg , $sourceImg , 0 , 0 , 0 , 0 , $newWidth , $newHeight , $newWidth , $newHeight );
imagejpeg ( $newSourceImg , $path , 100 );
2014-04-19 15:32:33 +00:00
2016-01-30 20:43:57 +00:00
// Free memory
2014-10-18 14:57:09 +00:00
imagedestroy ( $sourceImg );
imagedestroy ( $newSourceImg );
2014-04-12 12:15:21 +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-12 12:15:21 +00:00
2016-01-30 20:43:57 +00:00
// SwapSize should be true when the image has been rotated
// Return new dimensions in this case
2014-10-18 14:57:09 +00:00
if ( $swapSize === true ) {
2016-01-30 20:43:57 +00:00
$swapSize = $info [ 'width' ];
$info [ 'width' ] = $info [ 'height' ];
$info [ 'height' ] = $swapSize ;
2014-10-18 14:57:09 +00:00
}
return $info ;
2014-04-12 12:15:21 +00:00
}
2016-01-30 00:06:21 +00:00
public static function prepareData ( array $data ) {
2015-03-11 23:11:16 +00:00
2016-01-30 20:43:57 +00:00
// Function turns photo-attributes into a front-end friendly format. Note that some attributes remain unchanged.
// Excepts the following:
// (array) $data = ['id', 'title', 'tags', 'public', 'star', 'album', 'thumbUrl', 'takestamp', 'url']
// Returns the following:
// (array) $photo
2015-03-11 23:11:16 +00:00
2016-01-30 20:43:57 +00:00
// Init
2015-03-11 23:11:16 +00:00
$photo = null ;
2016-01-30 20:43:57 +00:00
// Set unchanged attributes
$photo [ 'id' ] = $data [ 'id' ];
$photo [ 'title' ] = $data [ 'title' ];
$photo [ 'tags' ] = $data [ 'tags' ];
$photo [ 'public' ] = $data [ 'public' ];
$photo [ 'star' ] = $data [ 'star' ];
$photo [ 'album' ] = $data [ 'album' ];
2015-03-11 23:11:16 +00:00
2016-01-30 20:43:57 +00:00
// Parse urls
$photo [ 'thumbUrl' ] = LYCHEE_URL_UPLOADS_THUMB . $data [ 'thumbUrl' ];
$photo [ 'url' ] = LYCHEE_URL_UPLOADS_BIG . $data [ 'url' ];
2015-03-11 23:11:16 +00:00
2016-01-30 20:43:57 +00:00
// Use takestamp as sysdate when possible
2015-03-11 23:11:16 +00:00
if ( isset ( $data [ 'takestamp' ]) && $data [ 'takestamp' ] !== '0' ) {
2016-01-30 20:43:57 +00:00
// Use takestamp
$photo [ 'cameraDate' ] = '1' ;
$photo [ 'sysdate' ] = date ( 'd F Y' , $data [ 'takestamp' ]);
2015-03-11 23:11:16 +00:00
} else {
2016-01-30 20:43:57 +00:00
// Use sysstamp from the id
$photo [ 'cameraDate' ] = '0' ;
$photo [ 'sysdate' ] = date ( 'd F Y' , substr ( $data [ 'id' ], 0 , - 4 ));
2015-03-11 23:11:16 +00:00
}
return $photo ;
}
2014-04-05 14:02:53 +00:00
public function get ( $albumID ) {
2016-01-30 20:43:57 +00:00
// Functions returns data of a photo
// Excepts the following:
// (string) $albumID = Album which is currently visible to the user
// Returns the following:
// (array) $photo
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Get photo
$query = Database :: prepare ( Database :: get (), " SELECT * FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_PHOTOS , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $photos === false ) exit ( 'Error: Could not get photo from database!' );
// Get photo object
$photo = $photos -> fetch_assoc ();
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Parse photo
2014-04-05 14:22:46 +00:00
$photo [ 'sysdate' ] = date ( 'd M. Y' , substr ( $photo [ 'id' ], 0 , - 4 ));
2014-04-13 10:35:59 +00:00
if ( strlen ( $photo [ 'takestamp' ]) > 1 ) $photo [ 'takedate' ] = date ( 'd M. Y' , $photo [ 'takestamp' ]);
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Parse medium
if ( $photo [ 'medium' ] === '1' ) $photo [ 'medium' ] = LYCHEE_URL_UPLOADS_MEDIUM . $photo [ 'url' ];
else $photo [ 'medium' ] = '' ;
2014-10-11 14:09:10 +00:00
2016-01-30 20:43:57 +00:00
// Parse paths
$photo [ 'url' ] = LYCHEE_URL_UPLOADS_BIG . $photo [ 'url' ];
$photo [ 'thumbUrl' ] = LYCHEE_URL_UPLOADS_THUMB . $photo [ 'thumbUrl' ];
2014-07-12 18:12:38 +00:00
2014-04-05 14:02:53 +00:00
if ( $albumID != 'false' ) {
2016-01-30 20:43:57 +00:00
// Only show photo as public when parent album is public
// Check if parent album is not 'Unsorted'
2015-04-06 16:48:52 +00:00
if ( $photo [ 'album' ] !== '0' ) {
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Get album
$query = Database :: prepare ( Database :: get (), " SELECT public FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_ALBUMS , $photo [ 'album' ]));
2016-01-31 17:49:31 +00:00
$albums = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $albums === false ) exit ( 'Error: Could not get album of photo from database!' );
// Get album object
$album = $albums -> fetch_assoc ();
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Parse album
2015-04-06 16:48:52 +00:00
$photo [ 'public' ] = ( $album [ 'public' ] === '1' ? '2' : $photo [ 'public' ]);
2014-04-05 14:02:53 +00:00
}
2016-01-30 20:43:57 +00:00
$photo [ 'original_album' ] = $photo [ 'album' ];
$photo [ 'album' ] = $albumID ;
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2014-04-05 14:02:53 +00:00
return $photo ;
}
2014-08-26 18:26:14 +00:00
public function getInfo ( $url ) {
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Functions returns information and metadata of a photo
// Excepts the following:
// (string) $url = Path to photo-file
// Returns the following:
// (array) $return
2015-04-06 16:48:52 +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-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
$iptcArray = array ();
$info = getimagesize ( $url , $iptcArray );
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// General information
$return [ 'type' ] = $info [ 'mime' ];
$return [ 'width' ] = $info [ 0 ];
$return [ 'height' ] = $info [ 1 ];
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// Size
2014-04-11 22:30:26 +00:00
$size = filesize ( $url ) / 1024 ;
if ( $size >= 1024 ) $return [ 'size' ] = round ( $size / 1024 , 1 ) . ' MB' ;
else $return [ 'size' ] = round ( $size , 1 ) . ' KB' ;
2016-01-30 20:43:57 +00:00
// IPTC Metadata Fallback
$return [ 'title' ] = '' ;
$return [ 'description' ] = '' ;
2014-04-11 22:30:26 +00:00
2016-01-30 20:43:57 +00:00
// IPTC Metadata
2014-04-11 22:30:26 +00:00
if ( isset ( $iptcArray [ 'APP13' ])) {
$iptcInfo = iptcparse ( $iptcArray [ 'APP13' ]);
if ( is_array ( $iptcInfo )) {
$temp = @ $iptcInfo [ '2#105' ][ 0 ];
if ( isset ( $temp ) && strlen ( $temp ) > 0 ) $return [ 'title' ] = $temp ;
$temp = @ $iptcInfo [ '2#120' ][ 0 ];
if ( isset ( $temp ) && strlen ( $temp ) > 0 ) $return [ 'description' ] = $temp ;
2014-08-26 14:07:59 +00:00
$temp = @ $iptcInfo [ '2#005' ][ 0 ];
if ( isset ( $temp ) && strlen ( $temp ) > 0 && $return [ 'title' ] === '' ) $return [ 'title' ] = $temp ;
2014-04-11 22:30:26 +00:00
}
}
2016-01-30 20:43:57 +00:00
// EXIF Metadata Fallback
$return [ 'orientation' ] = '' ;
$return [ 'iso' ] = '' ;
$return [ 'aperture' ] = '' ;
$return [ 'make' ] = '' ;
$return [ 'model' ] = '' ;
$return [ 'shutter' ] = '' ;
$return [ 'focal' ] = '' ;
$return [ 'takestamp' ] = 0 ;
// Read EXIF
2014-04-11 22:30:26 +00:00
if ( $info [ 'mime' ] == 'image/jpeg' ) $exif = @ exif_read_data ( $url , 'EXIF' , 0 );
else $exif = false ;
2016-01-30 20:43:57 +00:00
// EXIF Metadata
2014-04-11 22:30:26 +00:00
if ( $exif !== false ) {
if ( isset ( $exif [ 'Orientation' ])) $return [ 'orientation' ] = $exif [ 'Orientation' ];
else if ( isset ( $exif [ 'IFD0' ][ 'Orientation' ])) $return [ 'orientation' ] = $exif [ 'IFD0' ][ 'Orientation' ];
$temp = @ $exif [ 'ISOSpeedRatings' ];
if ( isset ( $temp )) $return [ 'iso' ] = $temp ;
$temp = @ $exif [ 'COMPUTED' ][ 'ApertureFNumber' ];
if ( isset ( $temp )) $return [ 'aperture' ] = $temp ;
$temp = @ $exif [ 'Make' ];
2014-04-25 14:36:31 +00:00
if ( isset ( $temp )) $return [ 'make' ] = trim ( $temp );
2014-04-11 22:30:26 +00:00
$temp = @ $exif [ 'Model' ];
2014-04-25 14:36:31 +00:00
if ( isset ( $temp )) $return [ 'model' ] = trim ( $temp );
2014-04-11 22:30:26 +00:00
$temp = @ $exif [ 'ExposureTime' ];
2014-12-19 20:39:43 +00:00
if ( isset ( $temp )) $return [ 'shutter' ] = $exif [ 'ExposureTime' ] . ' s' ;
2014-04-11 22:30:26 +00:00
$temp = @ $exif [ 'FocalLength' ];
2014-12-21 21:50:56 +00:00
if ( isset ( $temp )) {
if ( strpos ( $temp , '/' ) !== FALSE ) {
$temp = explode ( '/' , $temp , 2 );
$temp = $temp [ 0 ] / $temp [ 1 ];
$temp = round ( $temp , 1 );
$return [ 'focal' ] = $temp . ' mm' ;
}
$return [ 'focal' ] = $temp . ' mm' ;
}
2014-04-11 22:30:26 +00:00
$temp = @ $exif [ 'DateTimeOriginal' ];
if ( isset ( $temp )) $return [ 'takestamp' ] = strtotime ( $temp );
}
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-11 22:30:26 +00:00
return $return ;
}
2014-04-05 14:02:53 +00:00
public function getArchive () {
2016-01-30 20:43:57 +00:00
// Functions starts a download of a photo
// Returns the following:
// (boolean + output) true = Success
// (boolean) false = Failure
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Get photo
$query = Database :: prepare ( Database :: get (), " SELECT title, url FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_PHOTOS , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-05 14:02:53 +00:00
2016-01-31 17:49:31 +00:00
if ( $photos === false ) exit ( 'Error: Could not get photo from database!' );
2015-04-06 16:48:52 +00:00
2016-01-31 14:44:54 +00:00
// Get photo object
$photo = $photos -> fetch_object ();
2016-01-30 20:43:57 +00:00
// Photo not found
2015-04-06 16:48:52 +00:00
if ( $photo === null ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not find specified photo' );
exit ( 'Error: Could not find specified photo!' );
2015-04-06 16:48:52 +00:00
}
2016-01-30 20:43:57 +00:00
// Get extension
2014-05-20 10:44:08 +00:00
$extension = getExtension ( $photo -> url );
2014-05-08 18:50:18 +00:00
if ( $extension === false ) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Invalid photo extension' );
2014-05-08 18:50:18 +00:00
return false ;
}
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Illicit chars
$badChars = array_merge (
array_map ( 'chr' , range ( 0 , 31 )),
array ( " < " , " > " , " : " , '"' , " / " , " \\ " , " | " , " ? " , " * " )
);
2014-08-30 17:18:09 +00:00
2016-01-30 20:43:57 +00:00
// Parse title
2014-04-05 14:02:53 +00:00
if ( $photo -> title == '' ) $photo -> title = 'Untitled' ;
2016-01-30 20:43:57 +00:00
// Escape title
2014-08-30 18:03:38 +00:00
$photo -> title = str_replace ( $badChars , '' , $photo -> title );
2014-08-30 17:18:09 +00:00
2016-01-30 20:43:57 +00:00
// Set headers
2014-04-05 14:02:53 +00:00
header ( " Content-Type: application/octet-stream " );
2014-05-20 10:31:48 +00:00
header ( " Content-Disposition: attachment; filename= \" " . $photo -> title . $extension . " \" " );
2014-04-13 12:08:18 +00:00
header ( " Content-Length: " . filesize ( LYCHEE_UPLOADS_BIG . $photo -> url ));
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Send file
2014-04-13 12:08:18 +00:00
readfile ( LYCHEE_UPLOADS_BIG . $photo -> url );
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2014-04-05 14:02:53 +00:00
return true ;
}
2014-04-25 08:13:43 +00:00
public function setTitle ( $title ) {
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Functions sets the title of a photo
// Excepts the following:
// (string) $title = Title with a maximum length of 50 chars
// Returns the following:
// (boolean) true = Success
// (boolean) false = Failure
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Set title
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET title = '?' WHERE id IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $title , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-05 14:02:53 +00:00
return true ;
}
2014-04-25 08:13:43 +00:00
public function setDescription ( $description ) {
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Functions sets the description of a photo
// Excepts the following:
// (string) $description = Description with a maximum length of 1000 chars
// Returns the following:
// (boolean) true = Success
// (boolean) false = Failure
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Set description
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET description = '?' WHERE id IN ('?') " , array ( LYCHEE_TABLE_PHOTOS , $description , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-05 14:02:53 +00:00
return true ;
}
public function setStar () {
2016-01-30 20:43:57 +00:00
// Functions stars a photo
// Returns the following:
// (boolean) true = Success
// (boolean) false = Failure
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Init vars
$error = false ;
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Get photos
$query = Database :: prepare ( Database :: get (), " SELECT id, star FROM ? WHERE id IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $photos === false ) return false ;
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// For each photo
2014-04-05 14:02:53 +00:00
while ( $photo = $photos -> fetch_object ()) {
2016-01-30 20:43:57 +00:00
// Invert star
2014-04-05 14:02:53 +00:00
$star = ( $photo -> star == 0 ? 1 : 0 );
2016-01-30 20:43:57 +00:00
// Set star
2016-01-31 14:44:54 +00:00
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET star = '?' WHERE id = '?' " , array ( LYCHEE_TABLE_PHOTOS , $star , $photo -> id ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2016-01-31 14:44:54 +00:00
if ( $result === false ) $error = true ;
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2016-01-31 17:49:31 +00:00
if ( $error === true ) return false ;
2014-04-05 14:02:53 +00:00
return true ;
}
2014-04-25 08:13:43 +00:00
public function getPublic ( $password ) {
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Functions checks if photo or parent album is public
// Returns the following:
// (int) 0 = Photo private and parent album private
// (int) 1 = Album public, but password incorrect
// (int) 2 = Photo public or album public and password correct
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Get photo
$query = Database :: prepare ( Database :: get (), " SELECT public, album FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_PHOTOS , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $photos === false ) return 0 ;
// Get photo object
$photo = $photos -> fetch_object ();
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Check if public
2015-04-06 16:48:52 +00:00
if ( $photo -> public === '1' ) {
2016-01-30 20:43:57 +00:00
// Photo public
2015-04-06 16:48:52 +00:00
return 2 ;
} else {
2016-01-30 20:43:57 +00:00
// Check if album public
$album = new Album ( $photo -> album );
$agP = $album -> getPublic ();
$acP = $album -> checkPassword ( $password );
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Album public and password correct
2015-04-06 16:48:52 +00:00
if ( $agP === true && $acP === true ) return 2 ;
2016-01-30 20:43:57 +00:00
// Album public, but password incorrect
2015-04-06 16:48:52 +00:00
if ( $agP === true && $acP === false ) return 1 ;
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Photo private
2015-04-06 16:48:52 +00:00
return 0 ;
2014-04-05 14:02:53 +00:00
}
public function setPublic () {
2016-01-30 20:43:57 +00:00
// Functions toggles the public property of a photo
// Returns the following:
// (boolean) true = Success
// (boolean) false = Failure
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Get public
$query = Database :: prepare ( Database :: get (), " SELECT public FROM ? WHERE id = '?' LIMIT 1 " , array ( LYCHEE_TABLE_PHOTOS , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
if ( $photos === false ) return false ;
// Get photo object
$photo = $photos -> fetch_object ();
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Invert public
2014-04-05 14:02:53 +00:00
$public = ( $photo -> public == 0 ? 1 : 0 );
2016-01-30 20:43:57 +00:00
// Set public
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET public = '?' WHERE id = '?' " , array ( LYCHEE_TABLE_PHOTOS , $public , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-05 14:02:53 +00:00
return true ;
}
function setAlbum ( $albumID ) {
2016-01-30 20:43:57 +00:00
// Functions sets the parent album of a photo
// Returns the following:
// (boolean) true = Success
// (boolean) false = Failure
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Set album
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET album = '?' WHERE id IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $albumID , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-05 14:02:53 +00:00
return true ;
}
public function setTags ( $tags ) {
2016-01-30 20:43:57 +00:00
// Functions sets the tags of a photo
// Excepts the following:
// (string) $tags = Comma separated list of tags with a maximum length of 1000 chars
// Returns the following:
// (boolean) true = Success
// (boolean) false = Failure
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-30 20:43:57 +00:00
// Parse tags
2014-04-05 14:02:53 +00:00
$tags = preg_replace ( '/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/' , ',' , $tags );
2014-04-18 19:27:52 +00:00
$tags = preg_replace ( '/,$|^,|(\ ){0,}$/' , '' , $tags );
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Set tags
$query = Database :: prepare ( Database :: get (), " UPDATE ? SET tags = '?' WHERE id IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $tags , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) return false ;
2014-04-05 14:02:53 +00:00
return true ;
}
2014-08-22 21:31:09 +00:00
public function duplicate () {
2016-01-30 20:43:57 +00:00
// Functions duplicates a photo
// Returns the following:
// (boolean) true = Success
// (boolean) false = Failure
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-08-22 21:31:09 +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-22 21:31:09 +00:00
2016-01-31 17:49:31 +00:00
// Init vars
$error = false ;
2016-01-30 20:43:57 +00:00
// Get photos
$query = Database :: prepare ( Database :: get (), " SELECT id, checksum FROM ? WHERE id IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2016-01-31 14:44:54 +00:00
2016-01-31 17:49:31 +00:00
if ( $photos === false ) return false ;
2014-08-22 21:31:09 +00:00
2016-01-30 20:43:57 +00:00
// For each photo
2014-08-22 21:31:09 +00:00
while ( $photo = $photos -> fetch_object ()) {
2016-01-30 20:43:57 +00:00
// Generate id
2014-08-22 21:31:09 +00:00
$id = str_replace ( '.' , '' , microtime ( true ));
while ( strlen ( $id ) < 14 ) $id .= 0 ;
2016-01-30 20:43:57 +00:00
// Duplicate entry
2016-01-31 14:44:54 +00:00
$values = array ( LYCHEE_TABLE_PHOTOS , $id , LYCHEE_TABLE_PHOTOS , $photo -> id );
$query = Database :: prepare ( Database :: get (), " INSERT INTO ? (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum) SELECT '?' AS id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum FROM ? WHERE id = '?' " , $values );
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2016-01-31 14:44:54 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) $error = true ;
2014-08-22 21:31:09 +00:00
}
2016-01-31 17:49:31 +00:00
if ( $error === true ) return false ;
2014-08-22 21:31:09 +00:00
return true ;
}
2014-04-05 14:02:53 +00:00
public function delete () {
2016-01-30 20:43:57 +00:00
// Functions deletes a photo with all its data and files
// Returns the following:
// (boolean) true = Success
// (boolean) false = Failure
2015-04-06 16:48:52 +00:00
2016-01-30 20:43:57 +00:00
// Check dependencies
2016-01-30 20:33:31 +00:00
Validator :: required ( isset ( $this -> photoIDs ), __METHOD__ );
2014-04-05 14:02:53 +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 14:25:49 +00:00
2016-01-31 17:49:31 +00:00
// Init vars
$error = false ;
2016-01-30 20:43:57 +00:00
// Get photos
$query = Database :: prepare ( Database :: get (), " SELECT id, url, thumbUrl, checksum FROM ? WHERE id IN (?) " , array ( LYCHEE_TABLE_PHOTOS , $this -> photoIDs ));
2016-01-31 17:49:31 +00:00
$photos = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2016-01-31 14:44:54 +00:00
2016-01-31 17:49:31 +00:00
if ( $photos === false ) return false ;
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// For each photo
2014-04-05 14:02:53 +00:00
while ( $photo = $photos -> fetch_object ()) {
2016-01-30 20:43:57 +00:00
// Check if other photos are referring to this images
// If so, only delete the db entry
2014-08-22 20:54:33 +00:00
if ( $this -> exists ( $photo -> checksum , $photo -> id ) === false ) {
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Get retina thumb url
2014-08-22 20:54:33 +00:00
$thumbUrl2x = explode ( " . " , $photo -> thumbUrl );
$thumbUrl2x = $thumbUrl2x [ 0 ] . '@2x.' . $thumbUrl2x [ 1 ];
2014-05-08 18:50:18 +00:00
2016-01-30 20:43:57 +00:00
// Delete big
2014-08-22 20:54:33 +00:00
if ( file_exists ( LYCHEE_UPLOADS_BIG . $photo -> url ) &&! unlink ( LYCHEE_UPLOADS_BIG . $photo -> url )) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not delete photo in uploads/big/' );
$error = true ;
2014-08-22 20:54:33 +00:00
}
2016-01-30 20:43:57 +00:00
// Delete medium
2014-10-11 14:09:10 +00:00
if ( file_exists ( LYCHEE_UPLOADS_MEDIUM . $photo -> url ) &&! unlink ( LYCHEE_UPLOADS_MEDIUM . $photo -> url )) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not delete photo in uploads/medium/' );
$error = true ;
2014-10-11 14:09:10 +00:00
}
2016-01-30 20:43:57 +00:00
// Delete thumb
2014-08-22 20:54:33 +00:00
if ( file_exists ( LYCHEE_UPLOADS_THUMB . $photo -> thumbUrl ) &&! unlink ( LYCHEE_UPLOADS_THUMB . $photo -> thumbUrl )) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not delete photo in uploads/thumb/' );
$error = true ;
2014-08-22 20:54:33 +00:00
}
2016-01-30 20:43:57 +00:00
// Delete thumb@2x
if ( file_exists ( LYCHEE_UPLOADS_THUMB . $thumbUrl2x ) &&! unlink ( LYCHEE_UPLOADS_THUMB . $thumbUrl2x )) {
2016-01-31 17:49:31 +00:00
Log :: error ( Database :: get (), __METHOD__ , __LINE__ , 'Could not delete high-res photo in uploads/thumb/' );
$error = true ;
2014-08-22 20:54:33 +00:00
}
2014-05-08 18:50:18 +00:00
}
2014-04-05 14:02:53 +00:00
2016-01-30 20:43:57 +00:00
// Delete db entry
$query = Database :: prepare ( Database :: get (), " DELETE FROM ? WHERE id = '?' " , array ( LYCHEE_TABLE_PHOTOS , $photo -> id ));
2016-01-31 17:49:31 +00:00
$result = Database :: execute ( Database :: get (), $query , __METHOD__ , __LINE__ );
2016-01-31 14:44:54 +00:00
2016-01-31 17:49:31 +00:00
if ( $result === false ) $error = true ;
2014-04-05 14:02:53 +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-05 14:25:49 +00:00
2016-01-31 17:49:31 +00:00
if ( $error === true ) return false ;
2014-04-05 14:02:53 +00:00
return true ;
}
}
2016-01-31 14:53:44 +00:00
?>