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