2014-04-12 14:51:05 +00:00
< ? php
###
# @name Upload Module
# @author Tobias Reich
# @copyright 2014 by Tobias Reich
###
if ( ! defined ( 'LYCHEE' )) exit ( 'Error: Direct access is not allowed!' );
class Import extends Module {
static function photo ( $database , $plugins , $settings , $path , $albumID = 0 , $description = '' , $tags = '' ) {
$info = getimagesize ( $path );
$size = filesize ( $path );
$photo = new Photo ( $database , $plugins , $settings , null );
$nameFile = array ( array ());
$nameFile [ 0 ][ 'name' ] = $path ;
$nameFile [ 0 ][ 'type' ] = $info [ 'mime' ];
$nameFile [ 0 ][ 'tmp_name' ] = $path ;
$nameFile [ 0 ][ 'error' ] = 0 ;
$nameFile [ 0 ][ 'size' ] = $size ;
if ( ! $photo -> add ( $nameFile , $albumID , $description , $tags )) return false ;
return true ;
}
static function url ( $urls , $albumID = 0 ) {
2014-04-26 19:02:50 +00:00
$error = false ;
2014-04-12 14:51:05 +00:00
# Parse
$urls = str_replace ( ' ' , '%20' , $urls );
$urls = explode ( ',' , $urls );
foreach ( $urls as & $url ) {
2014-04-26 19:02:50 +00:00
if ( @ exif_imagetype ( $url ) === false ) {
$error = true ;
continue ;
}
2014-04-12 14:51:05 +00:00
2014-04-26 19:02:50 +00:00
$pathinfo = pathinfo ( $url );
$filename = $pathinfo [ 'filename' ] . '.' . $pathinfo [ 'extension' ];
$tmp_name = LYCHEE_DATA . $filename ;
2014-04-12 14:51:05 +00:00
2014-04-26 19:02:50 +00:00
if ( @ copy ( $url , $tmp_name ) === false ) $error = true ;
2014-04-12 14:51:05 +00:00
}
2014-04-26 19:02:50 +00:00
$import = Import :: server ( $albumID , LYCHEE_DATA );
if ( $error === false && $import === true ) return true ;
else return false ;
2014-04-12 14:51:05 +00:00
}
2014-07-26 20:14:22 +00:00
/* static function move ( $database , $path ) {
2014-04-12 14:51:05 +00:00
2014-05-13 20:49:23 +00:00
# Determine OS type and set move cmd (Windows untested!)
$myos = substr ( PHP_OS , 0 , 3 );
$myos = strtoupper ( $myos );
2014-05-04 00:15:43 +00:00
2014-05-13 20:49:23 +00:00
if ( $myos === 'WIN' ) $osmv = 'MOVE' ;
else $osmv = 'mv' ;
2014-05-04 00:15:43 +00:00
2014-05-13 20:49:23 +00:00
# Generate tmp dir name by hashing epoch time & random number
2014-05-04 00:15:43 +00:00
$tmpdirname = md5 ( time () . rand ());
2014-05-13 20:49:23 +00:00
# Make temporary directory
2014-05-16 20:47:45 +00:00
if ( @ mkdir ( LYCHEE_DATA . $tmpdirname ) === false ) {
2014-05-13 20:49:23 +00:00
Log :: error ( $database , __METHOD__ , __LINE__ , 'Failed to create temporary directory' );
return false ;
}
2014-05-04 00:15:43 +00:00
2014-05-13 20:49:23 +00:00
# Get list of files and move them to tmpdir
$files = glob ( $path . '*' );
if ( isset ( $files )) {
2014-05-04 00:15:43 +00:00
2014-05-13 20:49:23 +00:00
foreach ( $files as $file ) {
2014-05-04 00:15:43 +00:00
2014-05-17 20:59:28 +00:00
# Prevent unsupported files from being moved
if ( is_dir ( $file ) === false &&@ exif_imagetype ( $file ) === false ) continue ;
2014-05-04 00:15:43 +00:00
2014-05-13 20:49:23 +00:00
$out = '' ;
$ret = '' ;
2014-05-17 05:41:18 +00:00
$file = escapeshellarg ( $file );
$cmd = $osmv . " $file " . LYCHEE_DATA . $tmpdirname ;
@ exec ( $cmd , $out , $ret );
2014-05-04 00:15:43 +00:00
2014-05-13 20:49:23 +00:00
if ( isset ( $ret ) && ( $ret > 0 )) Log :: error ( $database , __METHOD__ , __LINE__ , " Failed to move directory or file ( $ret ): " . $file );
2014-05-04 00:15:43 +00:00
}
2014-05-13 20:49:23 +00:00
}
2014-05-04 00:15:43 +00:00
2014-05-13 20:49:23 +00:00
# If no files could be copied to the temp dir, remove
$files = glob ( LYCHEE_DATA . $tmpdirname . '/*' );
if ( count ( $files ) === 0 ) {
rmdir ( LYCHEE_DATA . $tmpdirname );
Log :: error ( $database , __METHOD__ , __LINE__ , 'Import failed, because files could not be temporary moved to ' . LYCHEE_DATA );
2014-05-04 00:15:43 +00:00
return false ;
}
2014-05-16 20:47:45 +00:00
# Set new path
$path = LYCHEE_DATA . $tmpdirname ;
return $path ;
2014-07-26 20:14:22 +00:00
} */
2014-05-16 20:47:45 +00:00
static function server ( $albumID = 0 , $path , $useTemp = false ) {
global $database , $plugins , $settings ;
2014-07-26 20:14:22 +00:00
# Parse path
2014-05-16 20:47:45 +00:00
if ( ! isset ( $path )) $path = LYCHEE_UPLOADS_IMPORT ;
2014-07-26 20:14:22 +00:00
if ( substr ( $path , - 1 ) === '/' ) $path = substr ( $path , 0 , - 1 );
2014-05-16 20:47:45 +00:00
2014-07-23 19:45:18 +00:00
if ( is_dir ( $path ) === false ) {
Log :: error ( $database , __METHOD__ , __LINE__ , 'Given path is not a directory (' . $path . ')' );
return 'Error: Given path is not a directory!' ;
}
2014-07-26 20:14:22 +00:00
# Skip folders of Lychee
if ( $path === LYCHEE_UPLOADS_BIG || ( $path . '/' ) === LYCHEE_UPLOADS_BIG || $path === LYCHEE_UPLOADS_THUMB || ( $path . '/' ) === LYCHEE_UPLOADS_THUMB ) {
Log :: error ( $database , __METHOD__ , __LINE__ , 'Given path is a reserved path of Lychee (' . $path . ')' );
return 'Error: Given path is a reserved path of Lychee!' ;
}
/* if ( $useTemp === true ) {
2014-05-17 20:48:06 +00:00
$path = Import :: move ( $database , $path );
if ( $path === false ) {
Log :: error ( $database , __METHOD__ , __LINE__ , 'Failed to move import to temporary directory' );
return false ;
}
2014-07-26 20:14:22 +00:00
} */
2014-05-16 20:47:45 +00:00
2014-05-13 20:49:23 +00:00
$error = false ;
2014-04-12 14:51:05 +00:00
$contains [ 'photos' ] = false ;
$contains [ 'albums' ] = false ;
2014-05-13 20:49:23 +00:00
2014-05-16 20:47:45 +00:00
# Get all files
2014-05-13 20:49:23 +00:00
$files = glob ( $path . '/*' );
2014-04-12 14:51:05 +00:00
foreach ( $files as $file ) {
2014-05-13 20:49:23 +00:00
# It is possible to move a file because of directory permissions but
# the file may still be unreadable by the user
if ( ! is_readable ( $file )) {
$error = true ;
Log :: error ( $database , __METHOD__ , __LINE__ , 'Could not read file or directory: ' . $file );
continue ;
}
2014-05-04 00:15:43 +00:00
2014-04-26 17:40:34 +00:00
if ( @ exif_imagetype ( $file ) !== false ) {
2014-04-12 14:51:05 +00:00
# Photo
2014-05-13 20:49:23 +00:00
if ( ! Import :: photo ( $database , $plugins , $settings , $file , $albumID )) {
$error = true ;
Log :: error ( $database , __METHOD__ , __LINE__ , 'Could not import file: ' . $file );
continue ;
}
2014-04-12 14:51:05 +00:00
$contains [ 'photos' ] = true ;
} else if ( is_dir ( $file )) {
# Folder
2014-07-26 20:14:22 +00:00
$name = mysqli_real_escape_string ( $database , basename ( $file ));
$album = new Album ( $database , null , null , null );
$newAlbumID = $album -> add ( '[Import] ' . $name );
$contains [ 'albums' ] = true ;
2014-04-12 14:51:05 +00:00
2014-05-13 20:49:23 +00:00
if ( $newAlbumID === false ) {
$error = true ;
Log :: error ( $database , __METHOD__ , __LINE__ , 'Could not create album in Lychee (' . $newAlbumID . ')' );
continue ;
}
2014-07-26 20:14:22 +00:00
$import = Import :: server ( $newAlbumID , $file . '/' , false );
if ( $import !== true && $import !== 'Notice: Import only contains albums!' ) {
2014-05-16 21:22:41 +00:00
$error = true ;
2014-07-23 19:23:33 +00:00
Log :: error ( $database , __METHOD__ , __LINE__ , 'Could not import folder. Function returned warning' );
2014-05-16 21:22:41 +00:00
continue ;
}
2014-05-13 20:49:23 +00:00
2014-04-12 14:51:05 +00:00
}
}
2014-05-13 20:49:23 +00:00
# Delete tmpdir if import was successful
2014-07-26 20:14:22 +00:00
/* if ( $error === false && $useTemp === true && file_exists ( LYCHEE_DATA . $tmpdirname )) {
2014-05-16 21:22:20 +00:00
if ( @ rmdir ( LYCHEE_DATA . $tmpdirname ) === false ) Log :: error ( $database , __METHOD__ , __LINE__ , 'Could not delete temp-folder (' . LYCHEE_DATA . $tmpdirname . ') after successful import' );
2014-07-26 20:14:22 +00:00
} */
2014-05-13 20:49:23 +00:00
2014-05-04 00:15:43 +00:00
if ( $contains [ 'photos' ] === false && $contains [ 'albums' ] === false ) return 'Warning: Folder empty or no readable files to process!' ;
2014-04-12 14:51:05 +00:00
if ( $contains [ 'photos' ] === false && $contains [ 'albums' ] === true ) return 'Notice: Import only contains albums!' ;
return true ;
}
}
2014-05-04 00:15:43 +00:00
?>