From b321e2d97069195b55d26abdee9d4581568b4013 Mon Sep 17 00:00:00 2001 From: djdallmann Date: Sun, 4 May 2014 00:15:43 +0000 Subject: [PATCH 1/3] Updated to include better error handling and to use the new error class. --- php/modules/Import.php | 84 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/php/modules/Import.php b/php/modules/Import.php index b4bbb25..359ea6c 100644 --- a/php/modules/Import.php +++ b/php/modules/Import.php @@ -62,14 +62,90 @@ class Import extends Module { if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT; - global $database, $plugins, $settings; + //determine OS type and set move cmd (Windows untested!) + $myos = strtoupper( substr(PHP_OS,0,3) ); + + if ( $myos === "WIN" ) { $osmv = 'MOVE'; } else { $osmv = 'mv'; } + + //generate tmp dir name by hashing epoch time & random number + $tmpdirname = md5(time() . rand()); + + global $database; + + if ( isset($tmpdirname) ){ + + //make temporary directory + if (@mkdir(LYCHEE_DATA . "$tmpdirname")!==false) { + + //get list of folders and move them to tmpdir + $folders = glob($path . '*', GLOB_ONLYDIR); + + if ( isset($folders) ) { + + foreach ($folders as $folder) { + + $out = ''; + $ret = ''; + + @exec("$osmv " . $folder . ' ' . LYCHEE_DATA . $tmpdirname, $out, $ret); + + if ( isset($ret) && ($ret > 0) ) Log::error($database, __METHOD__, __LINE__, "Failed to move directory or file ($ret):" . $folder); + + } + + } + + //get list of files and move them to tmpdir + $files = glob($path . '*'); + + if ( isset($files) ) { + + foreach ($files as $file) { + + //prevent logging second error for directories that could not be moved. + if ( is_dir($file) ) continue; + + $out = ''; + $ret = ''; + + @exec("$osmv " . $file . ' ' . LYCHEE_DATA . $tmpdirname, $out, $ret); + if ( isset($ret) && ($ret > 0) ) Log::error($database, __METHOD__, __LINE__, "Failed to move directory or file ($ret):" . $file); + + } + + } + + //If no files could be copied to the temp dir, remove. + if ( count( glob(LYCHEE_DATA . "$tmpdirname/*") ) == 0 ) { rmdir(LYCHEE_DATA . "$tmpdirname"); return false; } + + } else { + + Log::error($database, __METHOD__, __LINE__, 'Failed to create temporary directory'); + return false; + + } + + } else { + + Log::error($database, __METHOD__, __LINE__, 'Failed to generate temporary directory name'); + return false; + + } + + global $plugins, $settings; - $files = glob($path . '*'); $contains['photos'] = false; $contains['albums'] = false; + + $path = LYCHEE_DATA . "$tmpdirname"; + $files = glob($path . '/*'); foreach ($files as $file) { + //It's possible to move a file because of directory permissions but + //the file may still be unreadable by the user + if (!is_readable($file) ) { Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file); continue; } + if (@exif_imagetype($file)!==false) { # Photo @@ -92,7 +168,7 @@ class Import extends Module { } - if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty!'; + if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!'; if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contains albums!'; return true; @@ -100,4 +176,4 @@ class Import extends Module { } -?> \ No newline at end of file +?> From 83a7aa196cfccd976f699c229eee4c6493bad48d Mon Sep 17 00:00:00 2001 From: Tobias Reich Date: Tue, 13 May 2014 22:10:04 +0200 Subject: [PATCH 2/3] Fixed logging --- php/modules/Log.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/modules/Log.php b/php/modules/Log.php index 4856736..00f06b8 100644 --- a/php/modules/Log.php +++ b/php/modules/Log.php @@ -37,7 +37,7 @@ class Log extends Module { $sysstamp = time(); # Escape - $type = mysqli_real_escape_string($type, $function); + $type = mysqli_real_escape_string($database, $type); $function = mysqli_real_escape_string($database, $function); $line = mysqli_real_escape_string($database, $line); $text = mysqli_real_escape_string($database, $text); From 0167507604e5d432bff39579ce580e0520a1c038 Mon Sep 17 00:00:00 2001 From: Tobias Reich Date: Tue, 13 May 2014 22:49:23 +0200 Subject: [PATCH 3/3] Code adjustments and improvemts for #151 --- php/modules/Import.php | 122 +++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 65 deletions(-) diff --git a/php/modules/Import.php b/php/modules/Import.php index 359ea6c..7584156 100644 --- a/php/modules/Import.php +++ b/php/modules/Import.php @@ -60,97 +60,79 @@ class Import extends Module { static function server($albumID = 0, $path) { + global $database, $plugins, $settings; + if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT; - //determine OS type and set move cmd (Windows untested!) - $myos = strtoupper( substr(PHP_OS,0,3) ); + # Determine OS type and set move cmd (Windows untested!) + $myos = substr(PHP_OS,0,3); + $myos = strtoupper($myos); - if ( $myos === "WIN" ) { $osmv = 'MOVE'; } else { $osmv = 'mv'; } + if ($myos==='WIN') $osmv = 'MOVE'; + else $osmv = 'mv'; - //generate tmp dir name by hashing epoch time & random number + # Generate tmp dir name by hashing epoch time & random number $tmpdirname = md5(time() . rand()); - global $database; + # Make temporary directory + if (@mkdir(LYCHEE_DATA . $tmpdirname)===false) { + Log::error($database, __METHOD__, __LINE__, 'Failed to create temporary directory'); + return false; + } - if ( isset($tmpdirname) ){ + # Get list of files and move them to tmpdir + $files = glob($path . '*'); + if (isset($files)) { - //make temporary directory - if (@mkdir(LYCHEE_DATA . "$tmpdirname")!==false) { + foreach ($files as $file) { - //get list of folders and move them to tmpdir - $folders = glob($path . '*', GLOB_ONLYDIR); + # Prevent index.html from being moved + if (basename($file)==='index.html') continue; - if ( isset($folders) ) { + $out = ''; + $ret = ''; - foreach ($folders as $folder) { - - $out = ''; - $ret = ''; - - @exec("$osmv " . $folder . ' ' . LYCHEE_DATA . $tmpdirname, $out, $ret); - - if ( isset($ret) && ($ret > 0) ) Log::error($database, __METHOD__, __LINE__, "Failed to move directory or file ($ret):" . $folder); - - } - - } - - //get list of files and move them to tmpdir - $files = glob($path . '*'); - - if ( isset($files) ) { - - foreach ($files as $file) { - - //prevent logging second error for directories that could not be moved. - if ( is_dir($file) ) continue; - - $out = ''; - $ret = ''; - - @exec("$osmv " . $file . ' ' . LYCHEE_DATA . $tmpdirname, $out, $ret); - if ( isset($ret) && ($ret > 0) ) Log::error($database, __METHOD__, __LINE__, "Failed to move directory or file ($ret):" . $file); - - } - - } - - //If no files could be copied to the temp dir, remove. - if ( count( glob(LYCHEE_DATA . "$tmpdirname/*") ) == 0 ) { rmdir(LYCHEE_DATA . "$tmpdirname"); return false; } - - } else { - - Log::error($database, __METHOD__, __LINE__, 'Failed to create temporary directory'); - return false; + @exec($osmv . ' ' . $file . ' ' . LYCHEE_DATA . $tmpdirname, $out, $ret); + if (isset($ret)&&($ret>0)) Log::error($database, __METHOD__, __LINE__, "Failed to move directory or file ($ret):" . $file); } - } else { - - Log::error($database, __METHOD__, __LINE__, 'Failed to generate temporary directory name'); - return false; - } - global $plugins, $settings; + # 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); + return false; + } + $error = false; $contains['photos'] = false; $contains['albums'] = false; - - $path = LYCHEE_DATA . "$tmpdirname"; - $files = glob($path . '/*'); + + $path = LYCHEE_DATA . $tmpdirname; + $files = glob($path . '/*'); foreach ($files as $file) { - //It's possible to move a file because of directory permissions but - //the file may still be unreadable by the user - if (!is_readable($file) ) { Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file); continue; } + # 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; + } if (@exif_imagetype($file)!==false) { # Photo - if (!Import::photo($database, $plugins, $settings, $file, $albumID)) return false; + if (!Import::photo($database, $plugins, $settings, $file, $albumID)) { + $error = true; + Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file); + continue; + } $contains['photos'] = true; } else if (is_dir($file)) { @@ -161,13 +143,23 @@ class Import extends Module { $album = new Album($database, null, null, null); $newAlbumID = $album->add('[Import] ' . $name); - if ($newAlbumID!==false) Import::server($newAlbumID, $file . '/'); + if ($newAlbumID===false) { + $error = true; + Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')'); + continue; + } + + Import::server($newAlbumID, $file . '/'); + $contains['albums'] = true; } } + # Delete tmpdir if import was successful + if ($error===false) rmdir(LYCHEE_DATA . $tmpdirname); + if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!'; if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contains albums!'; return true;