2014-04-12 14:51:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
###
|
|
|
|
# @name Upload Module
|
2015-02-01 21:08:37 +00:00
|
|
|
# @copyright 2015 by Tobias Reich
|
2014-04-12 14:51:05 +00:00
|
|
|
###
|
|
|
|
|
|
|
|
if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|
|
|
|
|
|
|
class Import extends Module {
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
private $database = null;
|
|
|
|
private $settings = null;
|
|
|
|
private $albumIDs = null;
|
|
|
|
|
|
|
|
public function __construct($database, $plugins, $settings) {
|
|
|
|
|
|
|
|
# Init vars
|
|
|
|
$this->database = $database;
|
|
|
|
$this->plugins = $plugins;
|
|
|
|
$this->settings = $settings;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function photo($path, $albumID = 0, $description = '', $tags = '') {
|
|
|
|
|
|
|
|
# Check dependencies
|
|
|
|
self::dependencies(isset($this->database, $this->plugins, $this->settings, $path));
|
2014-04-12 14:51:05 +00:00
|
|
|
|
2015-04-13 19:09:28 +00:00
|
|
|
# No need to validate photo type and extension in this function.
|
|
|
|
# $photo->add will take care of it.
|
|
|
|
|
2014-04-12 14:51:05 +00:00
|
|
|
$info = getimagesize($path);
|
|
|
|
$size = filesize($path);
|
2015-06-29 07:45:03 +00:00
|
|
|
$photo = new Photo($this->database, $this->plugins, $this->settings, null);
|
2014-04-12 14:51:05 +00:00
|
|
|
|
|
|
|
$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;
|
2015-08-29 19:55:32 +00:00
|
|
|
$nameFile[0]['error'] = UPLOAD_ERR_OK;
|
2014-04-12 14:51:05 +00:00
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
if (!$photo->add($nameFile, $albumID, $description, $tags, true)) return false;
|
2014-04-12 14:51:05 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
public function url($urls, $albumID = 0) {
|
|
|
|
|
|
|
|
# Check dependencies
|
|
|
|
self::dependencies(isset($this->database, $urls));
|
2014-04-12 14:51:05 +00:00
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
# Call plugins
|
|
|
|
$this->plugins(__METHOD__, 0, func_get_args());
|
2015-06-28 16:58:17 +00:00
|
|
|
|
2014-04-26 19:02:50 +00:00
|
|
|
$error = false;
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
# Parse URLs
|
2014-04-12 14:51:05 +00:00
|
|
|
$urls = str_replace(' ', '%20', $urls);
|
|
|
|
$urls = explode(',', $urls);
|
|
|
|
|
|
|
|
foreach ($urls as &$url) {
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
# Validate photo type and extension even when $this->photo (=> $photo->add) will do the same.
|
2015-06-28 16:58:17 +00:00
|
|
|
# This prevents us from downloading invalid photos.
|
|
|
|
|
2015-04-13 19:09:28 +00:00
|
|
|
# Verify extension
|
|
|
|
$extension = getExtension($url);
|
|
|
|
if (!in_array(strtolower($extension), Photo::$validExtensions, true)) {
|
|
|
|
$error = true;
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->database, __METHOD__, __LINE__, 'Photo format not supported (' . $url . ')');
|
2015-04-13 19:09:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Verify image
|
|
|
|
$type = @exif_imagetype($url);
|
|
|
|
if (!in_array($type, Photo::$validTypes, true)) {
|
2014-04-26 19:02:50 +00:00
|
|
|
$error = true;
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->database, __METHOD__, __LINE__, 'Photo type not supported (' . $url . ')');
|
2014-04-26 19:02:50 +00:00
|
|
|
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
|
|
|
|
2015-06-28 16:58:17 +00:00
|
|
|
if (@copy($url, $tmp_name)===false) {
|
|
|
|
$error = true;
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->database, __METHOD__, __LINE__, 'Could not copy file (' . $tmp_name . ') to temp-folder (' . $tmp_name . ')');
|
2015-06-28 16:58:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Import photo
|
2015-06-29 07:45:03 +00:00
|
|
|
if (!$this->photo($tmp_name, $albumID)) {
|
2015-06-28 16:58:17 +00:00
|
|
|
$error = true;
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->database, __METHOD__, __LINE__, 'Could not import file: ' . $tmp_name);
|
2015-06-28 16:58:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-12 14:51:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
# Call plugins
|
|
|
|
$this->plugins(__METHOD__, 1, func_get_args());
|
|
|
|
|
2015-06-28 16:58:17 +00:00
|
|
|
if ($error===false) return true;
|
|
|
|
return false;
|
2014-04-12 14:51:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
public function server($path, $albumID = 0) {
|
2014-05-16 20:47:45 +00:00
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
# Check dependencies
|
|
|
|
self::dependencies(isset($this->database, $this->plugins, $this->settings));
|
2014-05-16 20:47:45 +00:00
|
|
|
|
2014-07-26 20:14:22 +00:00
|
|
|
# Parse path
|
2015-06-29 07:45:03 +00:00
|
|
|
if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
|
|
|
|
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) {
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->database, __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
|
2014-07-23 19:45:18 +00:00
|
|
|
return 'Error: Given path is not a directory!';
|
|
|
|
}
|
|
|
|
|
2014-07-26 20:14:22 +00:00
|
|
|
# Skip folders of Lychee
|
2015-01-23 20:52:19 +00:00
|
|
|
if ($path===LYCHEE_UPLOADS_BIG||($path . '/')===LYCHEE_UPLOADS_BIG||
|
|
|
|
$path===LYCHEE_UPLOADS_MEDIUM||($path . '/')===LYCHEE_UPLOADS_MEDIUM||
|
|
|
|
$path===LYCHEE_UPLOADS_THUMB||($path . '/')===LYCHEE_UPLOADS_THUMB) {
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->database, __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
|
2015-01-23 20:52:19 +00:00
|
|
|
return 'Error: Given path is a reserved path of Lychee!';
|
2014-07-26 20:14:22 +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
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
# Call plugins
|
2015-02-24 22:15:58 +00:00
|
|
|
# Note that updated albumId and path explicitly passed, rather
|
|
|
|
# than using func_get_args() which will only return original ones
|
2015-06-29 07:45:03 +00:00
|
|
|
$this->plugins(__METHOD__, 0, array($albumID, $path));
|
2015-02-21 08:52:33 +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;
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
|
2014-05-13 20:49:23 +00:00
|
|
|
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
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
$contains['photos'] = true;
|
|
|
|
|
|
|
|
if (!$this->photo($file, $albumID)) {
|
2014-05-13 20:49:23 +00:00
|
|
|
$error = true;
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->database, __METHOD__, __LINE__, 'Could not import file: ' . $file);
|
2014-05-13 20:49:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-12 14:51:05 +00:00
|
|
|
|
|
|
|
} else if (is_dir($file)) {
|
|
|
|
|
|
|
|
# Folder
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
$album = new Album($this->database, $this->plugins, $this->settings, null);
|
|
|
|
$newAlbumID = $album->add('[Import] ' . basename($file));
|
2014-07-26 20:14:22 +00:00
|
|
|
$contains['albums'] = true;
|
2014-04-12 14:51:05 +00:00
|
|
|
|
2014-05-13 20:49:23 +00:00
|
|
|
if ($newAlbumID===false) {
|
|
|
|
$error = true;
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
|
2014-05-13 20:49:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
$import = $this->server($file . '/', $newAlbumID);
|
2014-07-26 20:14:22 +00:00
|
|
|
|
|
|
|
if ($import!==true&&$import!=='Notice: Import only contains albums!') {
|
2014-05-16 21:22:41 +00:00
|
|
|
$error = true;
|
2015-06-29 07:45:03 +00:00
|
|
|
Log::error($this->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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
# Call plugins
|
2015-02-24 22:15:58 +00:00
|
|
|
# Note that updated albumId and path explicitly passed, rather
|
|
|
|
# than using func_get_args() which will only return original ones
|
2015-06-29 07:45:03 +00:00
|
|
|
$this->plugins(__METHOD__, 1, array($albumID, $path));
|
2015-02-21 08:52:33 +00:00
|
|
|
|
2015-06-29 07:45:03 +00:00
|
|
|
# The following returns will be caught in the front-end
|
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!';
|
2015-06-29 07:45:03 +00:00
|
|
|
if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contained albums!';
|
|
|
|
|
|
|
|
if ($error===true) return false;
|
2014-04-12 14:51:05 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-04 00:15:43 +00:00
|
|
|
?>
|