<commit_before><?php

// $Header: /cvsroot/tikiwiki/tiki/tiki-upload_image.php,v 1.42 2007-03-06 19:29:52 sylvieg Exp $

// Copyright (c) 2002-2007, <NAME>, <NAME>, <NAME>, et. al.
// 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.

// Initialization
$section = 'galleries';
require_once ('tiki-setup.php');

include_once ('lib/imagegals/imagegallib.php');

if ($feature_galleries != 'y') {
	$smarty->assign('msg', tra("This feature is disabled").": feature_galleries");

	$smarty->display("error.tpl");
	die;
}

// Now check permissions to access this page
if ($tiki_p_upload_images != 'y' and !$tikilib->user_has_perm_on_object($user,$_REQUEST["galleryId"],"image gallery","tiki_p_upload_images") ) {
	$smarty->assign('msg', tra("Permission denied you cannot upload images"));

	$smarty->display("error.tpl");
	die;
}

$foo = parse_url($_SERVER["REQUEST_URI"]);
$foo1 = str_replace("tiki-upload_image", "tiki-browse_image", $foo["path"]);
$foo2 = str_replace("tiki-upload_image", "show_image", $foo["path"]);
$smarty->assign('url_browse', $tikilib->httpPrefix(). $foo1);
$smarty->assign('url_show', $tikilib->httpPrefix(). $foo2);

$smarty->assign('show', 'n');
unset($data);
// Process an upload here
if (isset($_REQUEST["upload"])) {
	check_ticket('upload-image');
	// Check here if it is an upload or an URL
	$smarty->assign('individual', 'n');

	if ($userlib->object_has_one_permission($_REQUEST["galleryId"], 'image gallery')) {
		$smarty->assign('individual', 'y');

		if ($tiki_p_admin != 'y') {
			// Now get all the permissions that are set for this type of permissions 'image gallery'
			$perms = $userlib->get_permissions(0, -1, 'permName_desc', '', 'image galleries');

			foreach ($perms["data"] as $perm) {
				$permName = $perm["permName"];

				if ($userlib->object_has_permission($user, $_REQUEST["galleryId"], 'image gallery', $permName)) {
					$$permName = 'y';

					$smarty->assign("$permName", 'y');
				} else {
					$$permName = 'n';

					$smarty->assign("$permName", 'n');
				}
			}
		}
	}

	if ($tiki_p_admin_galleries == 'y') {
		$tiki_p_view_image_gallery = 'y';

		$tiki_p_upload_images = 'y';
		$tiki_p_create_galleries = 'y';
	}

	if ($tiki_p_upload_images != 'y') {
		$smarty->assign('msg', tra("Permission denied you cannot upload images"));

		$smarty->display("error.tpl");
		die;
	}

	$gal_info = $imagegallib->get_gallery($_REQUEST["galleryId"]);

	if ($gal_info["thumbSizeX"] == 0)
		$gal_info["thumbSizeX"] = 80;

	if ($gal_info["thumbSizeY"] == 0)
		$gal_info["thumbSizeY"] = 80;

	// Check the user to be admin or owner or the gallery is public
	if ($tiki_p_admin_galleries != 'y' && (!$user || $user != $gal_info["user"]) && $gal_info["public"] != 'y') {
		$smarty->assign('msg', tra("Permission denied you can upload images but not to this gallery"));

		$smarty->display("error.tpl");
		die;
	}

	$error_msg = '';

	if (!empty($_REQUEST["url"])) {
		// check URL. avoid uploading local files!
		if(!preg_match('#http[s]?://#i',$_REQUEST["url"])) {
			$_REQUEST["url"]='http://'.$_REQUEST["url"];
		}

		$data=$tikilib->httprequest($_REQUEST["url"]);
		if($data) {
		// Get the image from a URL
		if (@getimagesize($_REQUEST["url"])) { // that's not nice. reads the image twice.
						       // I'll have to add some functionality in imagegalslib
						       // remember me if i forget that. redflo
			$url_info = parse_url($_REQUEST["url"]);
			$pinfo = pathinfo($url_info["path"]);
			$type = "image/" . $pinfo["extension"];
			$filename = $pinfo["basename"];
			$size = strlen($data);
		} else {
			$error_msg = tra("Cannot get image from URL");
		}
		} else {
			$error_msg = tra("That is not an image (or you have php < 4.0.5)");
		}
	} else {
		// We process here file uploads
		if (isset($_FILES['userfile1']) && !empty($_FILES['userfile1']['name'])) {
			if (is_uploaded_file($_FILES['userfile1']['tmp_name'])) {
			if (!empty($gal_match_regex)) {
				if (!preg_match("/$gal_match_regex/", $_FILES['userfile1']['name'], $reqs)) {
					$smarty->assign('msg', tra('Invalid imagename (using filters for filenames)'));

					$smarty->display("error.tpl");
					die;
				}
			}

			if (!empty($gal_nmatch_regex)) {
				if (preg_match("/$gal_nmatch_regex/", $_FILES['userfile1']['name'], $reqs)) {
					$smarty->assign('msg', tra('Invalid imagename (using filters for filenames)'));

					$smarty->display("error.tpl");
					die;
				}
			}

			$type = $_FILES['userfile1']['type'];
			$size = $_FILES['userfile1']['size'];
			$filename = $_FILES['userfile1']['name'];

			// Check for a zip file.....
			// Fixed by Flo
			if (substr($filename, strlen($filename) - 3) == 'zip') {
				if ($tiki_p_batch_upload_images == 'y') {
					if ($imagegallib->process_batch_image_upload($_REQUEST["galleryId"], $_FILES['userfile1']['tmp_name'], $user)
						== 0) {
						$smarty->assign('msg', tra('Error processing zipped image package'));

						$smarty->display("error.tpl");
						die;
					}

					header ("location: tiki-browse_gallery.php?galleryId=" . $_REQUEST["galleryId"]);
					die();
				} else {
					$smarty->assign('msg', tra('No permission to upload zipped image packages'));

					$smarty->display("error.tpl");
					die;
				}
			}

			$file_name = $_FILES['userfile1']['name'];	
			$file_tmp_name = $_FILES['userfile1']['tmp_name'];
			$tmp_dest = $tmpDir . '/' . $file_name.'.tmp'; // add .tmp to not overwrite existing files (like index.php)
			if (!move_uploaded_file($file_tmp_name, $tmp_dest)) {
				$smarty->assign('msg', tra('Errors detected'));
				$smarty->display("error.tpl");
				die();
			}
			 
			$fp = fopen($tmp_dest, "rb");
			$data = fread($fp, filesize($tmp_dest));
			fclose ($fp);
			$imginfo = @getimagesize($tmp_dest);
			unlink($tmp_dest);
			if (!$data || !$imginfo) { // Not in Image format
				$error_msg = tra('The uploaded file ist not recognized as a image');
	        }
		} else {
			$error_msg = tra("cannot process upload");<commit_msg>[MOD]img: better error message<commit_after><?php

// $Header: /cvsroot/tikiwiki/tiki/tiki-upload_image.php,v 1.43 2007-05-01 16:28:18 sylvieg Exp $

// Copyright (c) 2002-2007, <NAME>, <NAME>, <NAME>, et. al.
// 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.

// Initialization
$section = 'galleries';
require_once ('tiki-setup.php');

include_once ('lib/imagegals/imagegallib.php');

if ($feature_galleries != 'y') {
	$smarty->assign('msg', tra("This feature is disabled").": feature_galleries");

	$smarty->display("error.tpl");
	die;
}

// Now check permissions to access this page
if ($tiki_p_upload_images != 'y' and !$tikilib->user_has_perm_on_object($user,$_REQUEST["galleryId"],"image gallery","tiki_p_upload_images") ) {
	$smarty->assign('msg', tra("Permission denied you cannot upload images"));

	$smarty->display("error.tpl");
	die;
}

$foo = parse_url($_SERVER["REQUEST_URI"]);
$foo1 = str_replace("tiki-upload_image", "tiki-browse_image", $foo["path"]);
$foo2 = str_replace("tiki-upload_image", "show_image", $foo["path"]);
$smarty->assign('url_browse', $tikilib->httpPrefix(). $foo1);
$smarty->assign('url_show', $tikilib->httpPrefix(). $foo2);

$smarty->assign('show', 'n');
unset($data);
// Process an upload here
if (isset($_REQUEST["upload"])) {
	check_ticket('upload-image');
	// Check here if it is an upload or an URL
	$smarty->assign('individual', 'n');

	if ($userlib->object_has_one_permission($_REQUEST["galleryId"], 'image gallery')) {
		$smarty->assign('individual', 'y');

		if ($tiki_p_admin != 'y') {
			// Now get all the permissions that are set for this type of permissions 'image gallery'
			$perms = $userlib->get_permissions(0, -1, 'permName_desc', '', 'image galleries');

			foreach ($perms["data"] as $perm) {
				$permName = $perm["permName"];

				if ($userlib->object_has_permission($user, $_REQUEST["galleryId"], 'image gallery', $permName)) {
					$$permName = 'y';

					$smarty->assign("$permName", 'y');
				} else {
					$$permName = 'n';

					$smarty->assign("$permName", 'n');
				}
			}
		}
	}

	if ($tiki_p_admin_galleries == 'y') {
		$tiki_p_view_image_gallery = 'y';

		$tiki_p_upload_images = 'y';
		$tiki_p_create_galleries = 'y';
	}

	if ($tiki_p_upload_images != 'y') {
		$smarty->assign('msg', tra("Permission denied you cannot upload images"));

		$smarty->display("error.tpl");
		die;
	}

	$gal_info = $imagegallib->get_gallery($_REQUEST["galleryId"]);

	if ($gal_info["thumbSizeX"] == 0)
		$gal_info["thumbSizeX"] = 80;

	if ($gal_info["thumbSizeY"] == 0)
		$gal_info["thumbSizeY"] = 80;

	// Check the user to be admin or owner or the gallery is public
	if ($tiki_p_admin_galleries != 'y' && (!$user || $user != $gal_info["user"]) && $gal_info["public"] != 'y') {
		$smarty->assign('msg', tra("Permission denied you can upload images but not to this gallery"));

		$smarty->display("error.tpl");
		die;
	}

	$error_msg = '';

	if (!empty($_REQUEST["url"])) {
		// check URL. avoid uploading local files!
		if(!preg_match('#http[s]?://#i',$_REQUEST["url"])) {
			$_REQUEST["url"]='http://'.$_REQUEST["url"];
		}

		$data=$tikilib->httprequest($_REQUEST["url"]);
		if($data) {
		// Get the image from a URL
		if (@getimagesize($_REQUEST["url"])) { // that's not nice. reads the image twice.
						       // I'll have to add some functionality in imagegalslib
						       // remember me if i forget that. redflo
			$url_info = parse_url($_REQUEST["url"]);
			$pinfo = pathinfo($url_info["path"]);
			$type = "image/" . $pinfo["extension"];
			$filename = $pinfo["basename"];
			$size = strlen($data);
		} else {
			$error_msg = tra("Cannot get image from URL");
		}
		} else {
			$error_msg = tra("That is not an image (or you have php < 4.0.5)");
		}
	} else {
		// We process here file uploads
		if (isset($_FILES['userfile1']) && !empty($_FILES['userfile1']['name'])) {
			if (is_uploaded_file($_FILES['userfile1']['tmp_name'])) {
			if (!empty($gal_match_regex)) {
				if (!preg_match("/$gal_match_regex/", $_FILES['userfile1']['name'], $reqs)) {
					$smarty->assign('msg', tra('Invalid imagename (using filters for filenames)'));

					$smarty->display("error.tpl");
					die;
				}
			}

			if (!empty($gal_nmatch_regex)) {
				if (preg_match("/$gal_nmatch_regex/", $_FILES['userfile1']['name'], $reqs)) {
					$smarty->assign('msg', tra('Invalid imagename (using filters for filenames)'));

					$smarty->display("error.tpl");
					die;
				}
			}

			$type = $_FILES['userfile1']['type'];
			$size = $_FILES['userfile1']['size'];
			$filename = $_FILES['userfile1']['name'];

			// Check for a zip file.....
			// Fixed by Flo
			if (substr($filename, strlen($filename) - 3) == 'zip') {
				if ($tiki_p_batch_upload_images == 'y') {
					if ($imagegallib->process_batch_image_upload($_REQUEST["galleryId"], $_FILES['userfile1']['tmp_name'], $user)
						== 0) {
						$smarty->assign('msg', tra('Error processing zipped image package'));

						$smarty->display("error.tpl");
						die;
					}

					header ("location: tiki-browse_gallery.php?galleryId=" . $_REQUEST["galleryId"]);
					die();
				} else {
					$smarty->assign('msg', tra('No permission to upload zipped image packages'));

					$smarty->display("error.tpl");
					die;
				}
			}

			$file_name = $_FILES['userfile1']['name'];	
			$file_tmp_name = $_FILES['userfile1']['tmp_name'];
			$tmp_dest = $tmpDir . '/' . $file_name.'.tmp'; // add .tmp to not overwrite existing files (like index.php)
			if (!move_uploaded_file($file_tmp_name, $tmp_dest)) {
				$smarty->assign('msg', tra('Errors detected'));
				$smarty->display("error.tpl");
				die();
			}
			 
			$fp = fopen($tmp_dest, "rb");
			$data = fread($fp, filesize($tmp_dest));
			fclose ($fp);
			$imginfo = @getimagesize($tmp_dest);
			unlink($tmp_dest);
			if (!$data || !$imginfo) { // Not in Image format
				$error_msg = tra('The uploaded file ist not recognized as a image');
	        }
		} else {
			$error_msg = $tikilib->uploaded_file_error($_FILES['userfile1']['error']);
{'fraction_non_alphanumeric': 0.2011517893870835, 'fraction_numerical': 0.008802961744138214, 'mean_word_length': 4.139957716701903, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4401908', 'n_tokens_mistral': 4885, 'n_tokens_neox': 4197, 'n_words': 924}