Import.php 5.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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) {

33
34
		$error = false;

35
36
37
38
39
40
		# Parse
		$urls = str_replace(' ', '%20', $urls);
		$urls = explode(',', $urls);

		foreach ($urls as &$url) {

41
42
43
44
			if (@exif_imagetype($url)===false) {
				$error = true;
				continue;
			}
45

46
47
48
			$pathinfo	= pathinfo($url);
			$filename	= $pathinfo['filename'] . '.' . $pathinfo['extension'];
			$tmp_name	= LYCHEE_DATA . $filename;
49

50
			if (@copy($url, $tmp_name)===false) $error = true;
51
52
53

		}

54
55
56
57
		$import = Import::server($albumID, LYCHEE_DATA);

		if ($error===false&&$import===true) return true;
		else return false;
58
59
60

	}

61
	/*static function move($database, $path) {
62

63
64
65
		# Determine OS type and set move cmd (Windows untested!)
		$myos = substr(PHP_OS,0,3);
		$myos = strtoupper($myos);
66

67
68
		if ($myos==='WIN') $osmv = 'MOVE';
		else $osmv = 'mv';
69

70
		# Generate tmp dir name by hashing epoch time & random number
71
72
		$tmpdirname = md5(time() . rand());

73
		# Make temporary directory
74
		if (@mkdir(LYCHEE_DATA . $tmpdirname)===false) {
75
76
77
			Log::error($database, __METHOD__, __LINE__, 'Failed to create temporary directory');
			return false;
		}
78

79
80
81
		# Get list of files and move them to tmpdir
		$files = glob($path . '*');
		if (isset($files)) {
82

83
			foreach ($files as $file) {
84

85
86
				# Prevent unsupported files from being moved
				if (is_dir($file)===false&&@exif_imagetype($file)===false) continue;
87

88
89
				$out = '';
				$ret = '';
90
91
92
93
				$file = escapeshellarg($file);
				$cmd = $osmv . " $file " . LYCHEE_DATA . $tmpdirname;

				@exec($cmd, $out, $ret);
94

95
				if (isset($ret)&&($ret>0)) Log::error($database, __METHOD__, __LINE__, "Failed to move directory or file ($ret):" . $file);
96
97
98

			}

99
		}
100

101
102
103
104
105
		# 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);
106
107
108
			return false;
		}

109
110
111
112
113
		# Set new path
		$path = LYCHEE_DATA . $tmpdirname;

		return $path;

114
	}*/
115
116
117
118
119

	static function server($albumID = 0, $path, $useTemp = false) {

		global $database, $plugins, $settings;

120
		# Parse path
121
		if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
122
		if (substr($path, -1)==='/') $path = substr($path, 0, -1);
123

124
125
126
127
128
		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!';
		}

129
130
131
132
133
134
135
		# 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) {
136
137
138
139
140
			$path = Import::move($database, $path);
			if ($path===false) {
				Log::error($database, __METHOD__, __LINE__, 'Failed to move import to temporary directory');
				return false;
			}
141
		}*/
142

143
		$error				= false;
144
145
		$contains['photos']	= false;
		$contains['albums']	= false;
146

147
		# Get all files
148
		$files = glob($path . '/*');
149
150
151

		foreach ($files as $file) {

152
153
154
155
156
157
158
			# 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;
			}
159

Tobias Reich's avatar
Tobias Reich committed
160
			if (@exif_imagetype($file)!==false) {
161
162
163

				# Photo

164
165
166
167
168
				if (!Import::photo($database, $plugins, $settings, $file, $albumID)) {
					$error = true;
					Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file);
					continue;
				}
169
170
171
172
173
174
				$contains['photos'] = true;

			} else if (is_dir($file)) {

				# Folder

175
176
177
178
				$name				= mysqli_real_escape_string($database, basename($file));
				$album				= new Album($database, null, null, null);
				$newAlbumID			= $album->add('[Import] ' . $name);
				$contains['albums']	= true;
179

180
181
182
183
184
185
				if ($newAlbumID===false) {
					$error = true;
					Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
					continue;
				}

186
187
188
				$import = Import::server($newAlbumID, $file . '/', false);

				if ($import!==true&&$import!=='Notice: Import only contains albums!') {
Tobias Reich's avatar
Tobias Reich committed
189
					$error = true;
Tobias Reich's avatar
Tobias Reich committed
190
					Log::error($database, __METHOD__, __LINE__, 'Could not import folder. Function returned warning');
Tobias Reich's avatar
Tobias Reich committed
191
192
					continue;
				}
193

194
195
196
197
			}

		}

198
		# Delete tmpdir if import was successful
199
		/*if ($error===false&&$useTemp===true&&file_exists(LYCHEE_DATA . $tmpdirname)) {
200
			if (@rmdir(LYCHEE_DATA . $tmpdirname)===false) Log::error($database, __METHOD__, __LINE__, 'Could not delete temp-folder (' . LYCHEE_DATA . $tmpdirname . ') after successful import');
201
		}*/
202

203
		if ($contains['photos']===false&&$contains['albums']===false)	return 'Warning: Folder empty or no readable files to process!';
204
205
206
207
208
209
210
		if ($contains['photos']===false&&$contains['albums']===true)	return 'Notice: Import only contains albums!';
		return true;

	}

}

211
?>