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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static function server($albumID = 0, $path) {
|
|
|
|
|
2014-04-13 12:08:18 +00:00
|
|
|
if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
|
2014-04-12 14:51:05 +00:00
|
|
|
|
2014-05-04 00:15:43 +00:00
|
|
|
//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;
|
2014-04-12 14:51:05 +00:00
|
|
|
|
|
|
|
$contains['photos'] = false;
|
|
|
|
$contains['albums'] = false;
|
2014-05-04 00:15:43 +00:00
|
|
|
|
|
|
|
$path = LYCHEE_DATA . "$tmpdirname";
|
|
|
|
$files = glob($path . '/*');
|
2014-04-12 14:51:05 +00:00
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
2014-05-04 00:15:43 +00:00
|
|
|
//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; }
|
|
|
|
|
2014-04-26 17:40:34 +00:00
|
|
|
if (@exif_imagetype($file)!==false) {
|
2014-04-12 14:51:05 +00:00
|
|
|
|
|
|
|
# Photo
|
|
|
|
|
|
|
|
if (!Import::photo($database, $plugins, $settings, $file, $albumID)) return false;
|
|
|
|
$contains['photos'] = true;
|
|
|
|
|
|
|
|
} else if (is_dir($file)) {
|
|
|
|
|
|
|
|
# Folder
|
|
|
|
|
|
|
|
$name = mysqli_real_escape_string($database, basename($file));
|
|
|
|
$album = new Album($database, null, null, null);
|
|
|
|
$newAlbumID = $album->add('[Import] ' . $name);
|
|
|
|
|
|
|
|
if ($newAlbumID!==false) Import::server($newAlbumID, $file . '/');
|
|
|
|
$contains['albums'] = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
?>
|