lychee/php/modules/Import.php

181 lines
5.1 KiB
PHP
Raw Normal View History

<?php
###
# @name Upload Module
2015-02-01 21:08:37 +00:00
# @copyright 2015 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 = '') {
# No need to validate photo type and extension in this function.
# $photo->add will take care of it.
$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) {
global $database, $plugins, $settings;
$error = false;
# Parse
$urls = str_replace(' ', '%20', $urls);
$urls = explode(',', $urls);
foreach ($urls as &$url) {
# Validate photo type and extension even when Import::photo (=> $photo->add) will do the same.
# This prevents us from downloading invalid photos.
# Verify extension
$extension = getExtension($url);
if (!in_array(strtolower($extension), Photo::$validExtensions, true)) {
$error = true;
continue;
}
# Verify image
$type = @exif_imagetype($url);
if (!in_array($type, Photo::$validTypes, true)) {
$error = true;
continue;
}
$pathinfo = pathinfo($url);
$filename = $pathinfo['filename'] . '.' . $pathinfo['extension'];
$tmp_name = LYCHEE_DATA . $filename;
if (@copy($url, $tmp_name)===false) {
$error = true;
Log::error($database, __METHOD__, __LINE__, 'Could not copy file (' . $tmp_name . ') to temp-folder (' . $tmp_name . ')');
continue;
}
# Import photo
if (!Import::photo($database, $plugins, $settings, $tmp_name, $albumID)) {
$error = true;
Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $tmp_name);
continue;
}
}
if ($error===false) return true;
return false;
}
2015-01-23 20:41:32 +00:00
static function server($albumID = 0, $path) {
global $database, $plugins, $settings;
2014-07-26 20:14:22 +00:00
# Parse path
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-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
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) {
Log::error($database, __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
return 'Error: Given path is a reserved path of Lychee!';
2014-07-26 20:14:22 +00:00
}
$error = false;
$contains['photos'] = false;
$contains['albums'] = false;
# Invoke plugins directly, as instance method not valid here
# Note that updated albumId and path explicitly passed, rather
# than using func_get_args() which will only return original ones
$plugins->activate(__METHOD__ . ":before", array($albumID, $path));
# Get all files
$files = glob($path . '/*');
foreach ($files as $file) {
# 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-04-26 17:40:34 +00:00
if (@exif_imagetype($file)!==false) {
# Photo
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)) {
# 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;
if ($newAlbumID===false) {
$error = true;
Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
continue;
}
2015-01-23 20:41:32 +00:00
$import = Import::server($newAlbumID, $file . '/');
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;
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;
}
}
}
# Invoke plugins directly, as instance method not valid here
# Note that updated albumId and path explicitly passed, rather
# than using func_get_args() which will only return original ones
$plugins->activate(__METHOD__ . ":after", array($albumID, $path));
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;
}
}
?>