tiki-batch_upload.php 9.29 KB
Newer Older
1
<?php
2
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
3 4 5
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6
// $Id$
changi67's avatar
changi67 committed
7

8
$section = 'galleries';
9
require_once ('tiki-setup.php');
10
$categlib = TikiLib::lib('categ');
lphuberdeau's avatar
lphuberdeau committed
11
$imagegallib = TikiLib::lib('imagegal');
12 13
$access->check_feature(array('feature_galleries', 'feature_gal_batch'));

14
// Now check permissions to access this page
15 16
$access->check_permission('tiki_p_batch_upload_image_dir');

17
// check directory path
18
if (!isset($prefs['gal_batch_dir']) or !is_dir($prefs['gal_batch_dir'])) {
19
	$msg = tra("Incorrect directory chosen for batch upload of images.") . "<br />";
20
	if ($tiki_p_admin == 'y') {
21
		$msg.= tra("Please setup that dir on ") . '<a href="tiki-admin.php?page=gal">' . tra('Image Galleries Configuration Panel') . '</a>.';
22 23 24
	} else {
		$msg.= tra("Please contact the website administrator.");
	}
25
	$smarty->assign('msg', $msg);
26 27 28
	$smarty->display("error.tpl");
	die;
} else {
29
	$imgdir = $prefs['gal_batch_dir'];
30
}
31
$smarty->assign('imgdir', $imgdir);
32
$a_img = $imgstring = $feedback = array();
33
$a_path = array();
34 35 36 37 38 39
$allowed_types = array(
	'.png',
	'.jpg',
	'.jpeg',
	'.gif'
); // list of filetypes you want to show
40
// recursively get all images from all subdirectories
41 42 43 44
/**
 * @param $sub
 */
function getDirContent($sub)
45
{
46
	$smarty = TikiLib::lib('smarty');
47 48 49
	global $allowed_types;
	global $a_img;
	global $a_path;
50
	global $imgdir;
sylvieg's avatar
sylvieg committed
51
	$allimg = array();
52 53
	$tmp = $imgdir;
	if ($sub <> "") $tmp.= '/' . $sub;
54
	if (!@($dimg = opendir($tmp))) {
55 56
		$msg = tra("Invalid directory name");
		$smarty->assign('msg', $msg);
57 58 59
		$smarty->display("error.tpl");
		die;
	}
60 61
	while ((false !== ($imgf = readdir($dimg)))) {
		if ($imgf != "." && $imgf != ".." && substr($imgf, 0, 1) != ".") {
62 63 64 65
			$allimg[] = $imgf;
		}
	}
	sort($allimg);
66
	foreach ($allimg as $imgfile) {
67
		if (is_dir($tmp . "/" . $imgfile)) {
68 69
			if ((substr($sub, -1) <> "/") && (substr($sub, -1) <> "\\")) {
				$sub.= '/';
70
			}
71
			getDirContent($sub . $imgfile);
72
		} elseif (in_array(strtolower(substr($imgfile, -(strlen($imgfile) - strrpos($imgfile, ".")))), $allowed_types)) {
73 74 75
			$a_img[] = $imgfile;
			$a_path[] = $sub;
		}
76
	}
77
	closedir($dimg);
78
}
79
// build a complete list of all images on filesystem including all necessary image info
80 81
function buildImageList() 
{
82 83
	global $a_img;
	global $a_path;
84
	global $imgdir;
85
	global $imgstring;
86 87 88 89
	getDirContent('');
	$totimg = count($a_img); // total image number
	$totalsize = 0;
	// build image data array
90
	for ($x = 0; $x < $totimg; $x++) {
91
		// get root dir
92
		while (substr($imgdir, -1) == '/') $imgdir = substr($imgdir, 0, -1);
93 94
		$tmp = $imgdir;
		// add any subdir names
95
		if ($a_path[$x] <> "") $tmp.= $a_path[$x];
96
		// get image information
97 98 99 100 101 102 103 104 105
		$size = @getimagesize($tmp . '/' . $a_img[$x]);
		$filesize = @filesize($tmp . '/' . $a_img[$x]);
		$halfwidth = ceil($size[0] / 2);
		$halfheight = ceil($size[1] / 2);
		$imgstring[$x][0] = $a_img[$x];
		if ($a_path[$x] <> "") $imgstring[$x][0] = $a_path[$x] . '/' . $a_img[$x];
		$imgstring[$x][1] = $size[0];
		$imgstring[$x][2] = $size[1];
		$imgstring[$x][3] = $filesize;
106
		// type is string after last dot
107 108 109
		$tmp = strtolower(substr($a_img[$x], -(strlen($a_img[$x]) - 1 - strrpos($a_img[$x], "."))));
		if ($tmp == "jpeg") $tmp = "jpg";
		$imgstring[$x][4] = $tmp;
110 111
		$totalsize+= $filesize;
	}
112
	$smarty = TikiLib::lib('smarty');
113 114 115
	$smarty->assign('totimg', $totimg);
	$smarty->assign('totalsize', $totalsize);
	$smarty->assign('imgstring', $imgstring);
116 117
}
if (isset($_REQUEST["batch_upload"]) and isset($_REQUEST['imgs']) and is_array($_REQUEST['imgs'])) {
118 119 120
	// default is: image names from request
	$imgArray = $_REQUEST['imgs'];
	$totimgs = count($imgArray);
121 122 123
	// if ALL is given, get all the images from the filesystem (stored in $a_img[] already)
	if ($totimgs == 1) {
		if ($imgArray[0] == "ALL") {
124 125 126 127
			getDirContent('');
			$imgArray = $a_img;
			$imgPathArray = $a_path;
			$totimgs = count($imgArray);
128 129 130
		}
	}
	// for subdirToSubgal we need all existing sub galleries for the current gallery
131
	$subgals = array();
132 133 134
	if (isset($_REQUEST["subdirTosubgal"])) {
		$subgals = $imagegallib->get_subgalleries(0, 9999, "name_asc", '', $_REQUEST["galleryId"]);
	}
135 136 137 138
	// cycle through all images to upload
	for ($x = 0; $x < $totimgs; $x++) {
		if ($imgPathArray[$x] <> "") {
			$imgPathArray[$x].= '/';
139 140
		} else {
			// if there is a path in image name, move it to the path array
141 142 143
			if (strrpos($imgArray[$x], "/") > 0) {
				$imgPathArray[$x] = substr($imgArray[$x], 0, strrpos($imgArray[$x], "/") + 1);
				$imgArray[$x] = substr($imgArray[$x], strrpos($imgArray[$x], "/") + 1);
144 145
			}
		}
146
		$filepath = $imgdir . $imgPathArray[$x] . $imgArray[$x];
147 148 149
		$size = @getimagesize($filepath);
		$filesize = @filesize($filepath);
		// type is string after last dot
150 151 152 153
		$type = strtolower(substr($imgArray[$x], -(strlen($imgArray[$x]) - 1 - strrpos($imgArray[$x], "."))));
		if ($type == "jpeg") $type = "jpg";
		$data = '';
		$fp = @fopen($filepath, 'r');
154
		if (!$fp) {
155
			$feedback[] = "!!!" . sprintf(tra('Could not read image %s.'), $filepath);
156
		} else {
157
			while (!feof($fp)) {
158
				$data.= @fread($fp, 1024);
159
			}
160
			@fclose($fp);
161
			// replace \ with /
162 163 164
			$imgArray[$x] = strtr($imgArray[$x], "\\", "/");
			$imgPathArray[$x] = strtr($imgPathArray[$x], "\\", "/");
			// get path, maybe needed as subgallery name
165 166 167
			$tmppath = $imgPathArray[$x];
			if (substr($tmppath, 0, 1) == "/") $tmppath = substr($tmppath, 1, 999);
			if (substr($tmppath, -1) == "/") $tmppath = substr($tmppath, 0, -1);
168
			// fix image name:
169
			$tmpName = '/' . $imgArray[$x];
170
			// remove possible path from filename
171
			$tmpName = substr($tmpName, strrpos($tmpName, "/") + 1, 999);
172
			// save filename without path
173
			$imgArray[$x] = $tmpName;
174 175
			// remove extension from name field
			if (isset($_REQUEST["removeExt"])) {
176
				$tmpName = substr($tmpName, 0, strrpos($tmpName, "."));
177
			}
178
			// check which gallery to upload to
179 180 181 182
			$tmpGalId = $_REQUEST["galleryId"];
			if (isset($_REQUEST["subdirToSubgal"])) {
				// get parent gallery data
				$parent = @$imagegallib->get_gallery($_REQUEST["galleryId"]);
183
				if ($tmppath <> "") {
184 185
					$tmpGalName = $tmppath;
					// get last subdir 'last' from 'some/path/last'
186 187
					if (strpos($tmpGalName, "/") > 0) $tmpGalName = substr($tmpGalName, strrpos($tmpGalName, "/") + 1, 999);
					$tmpGalId = @$imagegallib->replace_gallery(0, $tmpGalName, '', '', $user, $parent["maxRows"], $parent["rowImages"], $parent["thumbSizeX"], $parent["thumbSizeY"], $parent["public"], $parent["visible"], $parent['sortorder'], $parent['sortdirection'], $parent['galleryimage'], $_REQUEST["galleryId"], $parent['showname'], $parent['showimageid'], $parent['showdescription'], $parent['showcreated'], $parent['showuser'], $parent['showhits'], $parent['showxysize'], $parent['showfilesize'], $parent['showfilename'], $parent['defaultscale']);
188 189
					if ($tmpGalId == 0) $tmpGalId = $_REQUEST["galleryId"];
				}
190 191
			}
			// if subToDesc is set, set description:
192 193
			if (isset($_REQUEST["subToDesc"])) $tmpDesc = $tmppath;
			else $tmpDesc = '';
194
			// add image to gallery
195
			$imageId = $imagegallib->insert_image($tmpGalId, $tmpName, $tmpDesc, $imgArray[$x], $type, $data, $filesize, $size[0], $size[1], $user, '', '');
196
			if (!$imageId) {
197
				$feedback[] = "!!!" . sprintf(tra('Image %s upload failed.'), $imgArray[$x]);
198
			} else {
199
				$feedback[] = sprintf(tra('Image %s uploaded successfully.'), $imgArray[$x]);
200
				if (@unlink($filepath)) {
201
					$feedback[] = sprintf(tra('Image %s removed from Batch directory.'), $imgArray[$x]);
202
				} else {
203
					$feedback[] = "!!! " . sprintf(tra('Impossible to remove image %s from Batch directory.'), $imgArray[$x]);
204
				}
205 206 207 208
			}
		}
	}
}
209 210
$a_img = array();
$a_path = array();
211
buildImageList();
212 213 214
$smarty->assign('feedback', $feedback);
if (isset($_REQUEST["galleryId"])) {
	$smarty->assign_by_ref('galleryId', $_REQUEST["galleryId"]);
215
	$smarty->assign('permAddGallery', 'n');
216
	if ($tiki_p_admin_galleries == 'y' || $userlib->object_has_permission($user, $_REQUEST["galleryId"], 'image gallery', 'tiki_p_create_galleries')) {
217 218
		$smarty->assign('permAddGallery', 'y');
	}
219 220 221 222 223 224 225 226 227 228 229 230 231
} else {
	$smarty->assign('galleryId', '');
}
if ($tiki_p_admin_galleries != 'y') {
	$galleries = $imagegallib->list_visible_galleries(0, -1, 'lastModif_desc', $user, '');
} else {
	$galleries = $imagegallib->list_galleries(0, -1, 'lastModif_desc', $user, '');
}
$temp_max = count($galleries["data"]);
for ($i = 0; $i < $temp_max; $i++) {
	if ($userlib->object_has_one_permission($galleries["data"][$i]["galleryId"], 'image gallery')) {
		$galleries["data"][$i]["individual"] = 'y';
		$galleries["data"][$i]["individual_tiki_p_batch_upload_image_dir"] = 'n';
232
		if ($tiki_p_admin == 'y' || $userlib->object_has_permission($user, $galleries["data"][$i]["galleryId"], 'image gallery', 'tiki_p_batch_upload_image_dir') || $userlib->object_has_permission($user, $galleries["data"][$i]["galleryId"], 'image gallery', 'tiki_p_admin_galleries')) {
233 234 235 236 237 238 239 240
			$galleries["data"][$i]["individual_tiki_p_batch_upload_image_dir"] = 'y';
		}
	} else {
		$galleries["data"][$i]["individual"] = 'n';
	}
}
$smarty->assign_by_ref('galleries', $galleries["data"]);
include_once ('tiki-section_options.php');
241 242
// disallow robots to index page:
$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
243 244 245
// Display the template
$smarty->assign('mid', 'tiki-batch_upload.tpl');
$smarty->display("tiki.tpl");