<commit_before><?php
// (c) Copyright 2002-2009 by authors of the Tiki Wiki/CMS/Groupware Project
// 
// 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.
// $Id: /cvsroot/tikiwiki/tiki/tiki-upload_file.php,v 1.65.2.4 2008-03-11 15:17:54 nyloth Exp $
$section = 'file_galleries';
require_once ('tiki-setup.php');
if ($prefs['feature_categories'] == 'y') {
	include_once ('lib/categories/categlib.php');
}
if ($prefs['feature_file_galleries'] != 'y') {
	$smarty->assign('msg', tra("This feature is disabled") . ": feature_file_galleries");
	$smarty->display("error.tpl");
	die;
}
include_once ('lib/filegals/filegallib.php');
if ($prefs['feature_groupalert'] == 'y') {
	include_once ('lib/groupalert/groupalertlib.php');
}
include ('lib/filegals/max_upload_size.php');
@ini_set('max_execution_time', 0); //will not work in safe_mode is on
$auto_query_args = array('galleryId', 'fileId');
function print_progress($msg) {
	global $prefs;
	if ($prefs['javascript_enabled'] == 'y') {
		echo $msg;
		ob_flush();
	}
}
function print_msg($msg, $id) {
	global $prefs;
	if ($prefs['javascript_enabled'] == 'y') {
		echo "<script type='text/javascript'><!--//--><![CDATA[//><!--\n";
		echo "parent.progress('$id','" . htmlentities($msg, ENT_QUOTES, "UTF-8") . "')\n";
		echo "//--><!]]></script>\n";
		ob_flush();
	}
}
if (!empty($_REQUEST['fileId'])) {
	if (!($fileInfo = $filegallib->get_file_info($_REQUEST['fileId']))) {
		$smarty->assign('msg', tra("Incorrect param"));
		$smarty->display('error.tpl');
		die;
	}
	if (!empty($_REQUEST['galleryId']) && !is_array($_REQUEST['galleryId'])) {
		$_REQUEST['galleryId'] = array($_REQUEST['galleryId']);
	}
	if (empty($_REQUEST['galleryId'][0])) {
		$_REQUEST['galleryId'][0] = $fileInfo['galleryId'];
	} elseif ($_REQUEST['galleryId'][0] != $fileInfo['galleryId']) {
		$smarty->assign('msg', tra("Could not find the file requested"));
		$smarty->display('error.tpl');
		die;
	}
} elseif (isset($_REQUEST['galleryId']) && !is_array($_REQUEST['galleryId'])) {
	$_REQUEST['galleryId'] = array($_REQUEST['galleryId']);
}
if (isset($_REQUEST['galleryId'][0])) {
	$gal_info = $tikilib->get_file_gallery((int)$_REQUEST['galleryId'][0]);
	$tikilib->get_perm_object($_REQUEST['galleryId'][0], 'file gallery', $gal_info, true);
}
if (empty($_REQUEST['fileId']) && $tiki_p_upload_files != 'y' && $tiki_p_admin_file_galleries != 'y') {
	$smarty->assign('errortype', 401);
	$smarty->assign('msg', tra("Permission denied"));
	$smarty->display('error.tpl');
	die;
}
if (isset($_REQUEST['galleryId'][1])) {
	foreach($_REQUEST['galleryId'] as $i => $gal) {
		if (!$i) continue;
		$perms = $tikilib->get_perm_object($_REQUEST['galleryId'][$key], 'file gallery', $gal_info, false);
		if ($perm['tiki_p_upload_files'] != 'y') {
			$smarty->assign('errortype', 401);
			$smarty->assign('msg', tra("Permission denied"));
			$smarty->display('error.tpl');
			die;
		}
	}
}
if (!empty($_REQUEST['fileId'])) {
	if (!empty($fileInfo['lockedby']) && $fileInfo['lockedby'] != $user && $tiki_p_admin_file_galleries != 'y') { // if locked must be the locker
		$smarty->assign('msg', tra(sprintf('The file is locked by %s', $fileInfo['lockedby'])));
		$smarty->display('error.tpl');
		die;
	}
	if (!((!empty($user) && ($user == $fileInfo['user'] || $user == $fileInfo['lockedby'])) || $tiki_p_edit_gallery_file == 'y')) { // must be the owner or the locker or have the perms
		$smarty->assign('errortype', 401);
		$smarty->assign('msg', tra("Permission denied you can edit this file"));
		$smarty->display('error.tpl');
		die;
	}
	if (isset($_REQUEST['lockedby']) && $fileInfo['lockedby'] != $_REQUEST['lockedby']) {
		if (empty($fileInfo['lockedby'])) {
			$smarty->assign('msg', tra(sprintf('The file has been unlocked meanwhile')));
		} else {
			$smarty->assign('msg', tra(sprintf('The file is locked by %s', $fileInfo['lockedby'])));
		}
		$smarty->display('error.tpl');
		die;
	}
	if ($gal_info['lockable'] == 'y' && empty($fileInfo['lockedby']) && $tiki_p_admin_file_galleries != 'y') {
		$smarty->assign('msg', tra('You must lock the file before editing it'));
		$smarty->display('error.tpl');
		die;
	}
}
$foo = parse_url($_SERVER["REQUEST_URI"]);
$foo1 = str_replace("tiki-upload_file", "tiki-download_file", $foo["path"]);
$smarty->assign('url_browse', $tikilib->httpPrefix() . $foo1);
$url_browse = $tikilib->httpPrefix() . $foo1;
// create direct download path for podcasts
$podcast_url = str_replace("tiki-upload_file.php", "", $foo["path"]);
$podcast_url = $tikilib->httpPrefix() . $podcast_url . $prefs['fgal_podcast_dir'];
if (!isset($_REQUEST["description"])) $_REQUEST["description"] = '';
if (!isset($_REQUEST['author'])) $_REQUEST['author'] = '';
if (isset($_REQUEST['hit_limit'])) $_REQUEST['hit_limit'] = (int)$_REQUEST['hit_limit'];
else $_REQUEST['hit_limit'] = 0;
$smarty->assign('show', 'n');
if (!empty($_REQUEST['galleryId'][0]) && $prefs['feature_groupalert'] == 'y') {
	$groupforalert = $groupalertlib->GetGroup('file gallery', (int)$_REQUEST['galleryId'][0]);
	if ($groupforalert != '') {
		$showeachuser = $groupalertlib->GetShowEachUser('file gallery', (int)$_REQUEST['galleryId'][0], $groupforalert);
		$listusertoalert = $userlib->get_users(0, -1, 'login_asc', '', '', false, $groupforalert, '');
		$smarty->assign_by_ref('listusertoalert', $listusertoalert['data']);
	}
	$smarty->assign_by_ref('groupforalert', $groupforalert);
	$smarty->assign_by_ref('showeachuser', $showeachuser);
}
if (isset($_REQUEST['fileId'])) $editFileId = $_REQUEST['fileId'];
$editFile = false;
if (!empty($editFileId)) {
	if (!empty($_REQUEST['name'][0])) $fileInfo['name'] = $_REQUEST['name'][0];
	if (!empty($_REQUEST['description'][0])) $fileInfo['description'] = $_REQUEST['description'][0];
	if (!empty($_REQUEST['user'][0])) $fileInfo['user'] = $_REQUEST['user'][0];
	if (!empty($_REQUEST['author'][0])) $fileInfo['author'] = $_REQUEST['author'][0];
	$smarty->assign_by_ref('fileInfo', $fileInfo);
	$smarty->assign('editFileId', $editFileId);
	$editFile = true;
}
if (!empty($_REQUEST['galleryId'][0])) {
	//$gal_info = $tikilib->get_file_gallery((int)$_REQUEST["galleryId"]);
	$smarty->assign_by_ref('gal_info', $gal_info);
	$podCastGallery = $filegallib->isPodCastGallery((int)$_REQUEST["galleryId"][0], $gal_info);
}
// Process an upload here
if (isset($_REQUEST["upload"])) {
	check_ticket('upload-file');
	//print_progress('<script type="text/javascript" src="lib/tiki-js.js"></script>');
	$error_msg = '';
	$errors = array();
	$uploads = array();
	$batch_job = false;
	$didFileReplace = false;
	foreach($_FILES["userfile"]["error"] as $key => $error) {
		if ($prefs['javascript_enabled'] == 'y') {
			print_progress('<?xml version="1.0" encoding="UTF-8"?>');
		}
		$formId = $_REQUEST['formId'];
		$smarty->assign("FormId", $_REQUEST['formId']);
		if (empty($_REQUEST['galleryId'][$key])) continue;
		if (!isset($_REQUEST['comment'][$key])) $_REQUEST['comment'][$key] = '';
		// We process here file uploads
		if (!empty($_FILES["userfile"]["name"][$key])) {
			// Were there any problems with the upload?  If so, report here.
			if (!is_uploaded_file($_FILES["userfile"]["tmp_name"][$key])) {
				$errors[] = $_FILES['userfile']['name'][$key] . ': ' . tra('Upload was not successful') . ': ' . $tikilib->uploaded_file_error($error);
				continue;
			}
			// Check the name
			if (!empty($prefs['fgal_match_regex'])) {
				if (!preg_match('/' . $prefs['fgal_match_regex'] . '/', $_FILES["userfile"]['name'][$key])) {
					$errors[] = tra('Invalid filename (using filters for filenames)') . ': ' . $_FILES["userfile"]["name"][$key];
					continue;
				}
			}
			if (!empty($prefs['fgal_nmatch_regex'])) {
				if (preg_match('/' . $prefs['fgal_nmatch_regex'] . '/', $_FILES["userfile"]["name"][$key])) {
					$errors[] = tra('Invalid filename (using filters for filenames)') . ': ' . $_FILES["userfile"]["name"][$key];
					continue;
				}
			}
			$name = $_FILES["userfile"]["name"][$key];
			if (isset($_REQUEST["isbatch"][$key]) && $_REQUEST["isbatch"][$key] == 'on' && strtolower(substr($name, strlen($name) - 3)) == 'zip') {
				if ($tiki_p_batch_upload_files == 'y') {
					$filegallib->process_batch_file_upload($_REQUEST["galleryId"][$key], $_FILES["userfile"]['tmp_name'][$key], $user, $_REQUEST["description"][$key]);
					$batch_job = true;
					$batch_job_galleryId = $_REQUEST["galleryId"][$key];
					print_msg(tra('Batch file processed') . " $name", $formId);
					continue;
				} else {
					$errors[] = tra('No permission to upload zipped file packages');
					continue;
				}
			}
			$file_name = $_FILES["userfile"]["name"][$key];
			$file_tmp_name = $_FILES["userfile"]["tmp_name"][$key];
			$tmp_dest = $prefs['tmpDir'] . "/" . $file_name . ".tmp";
			if (!move_uploaded_file($file_tmp_name, $tmp_dest)) {
				$errors[] = tra('Errors detected');
				continue;
			}
			$fp = fopen($tmp_dest, "rb");
			if (!$fp) {
				$errors[] = tra('Cannot read file:') . ' ' . $tmp_dest;
			}
			$data = '';
			$fhash = '';
			if (($prefs['fgal_use_db'] == 'n') || ($podCastGallery)) {
				$fhash = md5($name = $_FILES["userfile"]['name'][$key]);
				$extension = '';
				// for podcast galleries add the extension so the
				// file can be called directly if name is known,
				if ($podCastGallery) {
					$path_parts = pathinfo($_FILES["userfile"]['name'][$key]);
					if (in_array(strtolower($path_parts["extension"]), array("m4a", "mp3", "mov", "mp4", "m4v", "pdf", "flv"))) {
						$extension = "." . strtolower($path_parts["extension"]);
					}
					$savedir = $prefs['fgal_podcast_dir'];
				} else {
					$savedir = $prefs['fgal_use_dir'];
				}
				do {
					$fhash = md5(uniqid($fhash));
				}
				while (file_exists($savedir . $fhash . $extension));
				@$fw = fopen($savedir . $fhash . $extension, "wb");
				if (!$fw) {
					$errors[] = tra('Cannot write to this file:') . $savedir . $fhash;
				}
			}
			while (!feof($fp)) {
				if (($prefs['fgal_use_db'] == 'y') && (!$podCastGallery)) {
					$data.= fread($fp, 8192 * 16);
				} else {
					if (($data = fread($fp, 8192 * 16)) === false) {
						$errors[] = tra('Cannot read the file:') . ' ' . $tmp_dest;
					}
					fwrite($fw, $data);
				}
			}
			fclose($fp);
			// remove file after copying it to the right location or database
			@unlink($tmp_dest);
			if (($prefs['fgal_use_db'] == 'n') || ($podCastGallery)) {
				fclose($fw);
				$data = '';
			}
			$size = $_FILES["userfile"]['size'][$key];
			$name = stripslashes($_FILES["userfile"]['name'][$key]);
			$type = $_FILES["userfile"]['type'][$key];
			if (preg_match('/.flv$/', $name)) {
				$type = "video/x-flv";
			}
			if (count($errors)) {
				continue;
			}
			if (!$size) {
				$errors[] = tra('Warning: Empty file:') . '  ' . $name . '. ' . tra('Please re-upload your file');
			}
			if (($prefs['fgal_use_db'] == 'y') && (!$podCastGallery)) {
				if (!isset($data) || strlen($data) < 1) {
					$errors[] = tra('Warning: Empty file:') . ' ' . $name . '. ' . tra('Please re-upload your file');
				}
			}
			if (!isset($_REQUEST['name'][$key])) $_REQUEST['name'][$key] = $name;
			if (empty($_REQUEST['user'][$key])) $_REQUEST['user'][$key] = $user;
			$fileInfo['filename'] = $file_name;
			if (isset($data)) {
				if ($editFile) {
					$didFileReplace = true;
					$fileId = $filegallib->replace_file($editFileId, $_REQUEST["name"][$key], $_REQUEST["description"][$key], $name, $data, $size, $type, $_REQUEST['user'][$key], $fhash . $extension, $_REQUEST['comment'][$key], $gal_info, $didFileReplace, $_REQUEST['author'][$key], $fileInfo['lastModif'], $fileInfo['lockedby']);
					if ($prefs['fgal_limit_hits_per_file'] == 'y') {
						$filegallib->set_download_limit($editFileId, $_REQUEST['hit_limit'][$key]);
					}
				} else {
					$fileId = $filegallib->insert_file($_REQUEST["galleryId"][$key], $_REQUEST["name"][$key], $_REQUEST["description"][$key], $name, $data, $size, $type, $_REQUEST['user'][$key], $fhash . $extension, '', $_REQUEST['author'][$key]);<commit_msg>[FIX] Init some vars (didn't fix my stored in database problem, but may help others)<commit_after><?php
// (c) Copyright 2002-2009 by authors of the Tiki Wiki/CMS/Groupware Project
// 
// 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.
// $Id: /cvsroot/tikiwiki/tiki/tiki-upload_file.php,v 1.65.2.4 2008-03-11 15:17:54 nyloth Exp $
$section = 'file_galleries';
require_once ('tiki-setup.php');
if ($prefs['feature_categories'] == 'y') {
	include_once ('lib/categories/categlib.php');
}
if ($prefs['feature_file_galleries'] != 'y') {
	$smarty->assign('msg', tra("This feature is disabled") . ": feature_file_galleries");
	$smarty->display("error.tpl");
	die;
}
include_once ('lib/filegals/filegallib.php');
if ($prefs['feature_groupalert'] == 'y') {
	include_once ('lib/groupalert/groupalertlib.php');
}
include ('lib/filegals/max_upload_size.php');
@ini_set('max_execution_time', 0); //will not work in safe_mode is on
$auto_query_args = array('galleryId', 'fileId');
function print_progress($msg) {
	global $prefs;
	if ($prefs['javascript_enabled'] == 'y') {
		echo $msg;
		ob_flush();
	}
}
function print_msg($msg, $id) {
	global $prefs;
	if ($prefs['javascript_enabled'] == 'y') {
		echo "<script type='text/javascript'><!--//--><![CDATA[//><!--\n";
		echo "parent.progress('$id','" . htmlentities($msg, ENT_QUOTES, "UTF-8") . "')\n";
		echo "//--><!]]></script>\n";
		ob_flush();
	}
}
if (!empty($_REQUEST['fileId'])) {
	if (!($fileInfo = $filegallib->get_file_info($_REQUEST['fileId']))) {
		$smarty->assign('msg', tra("Incorrect param"));
		$smarty->display('error.tpl');
		die;
	}
	if (!empty($_REQUEST['galleryId']) && !is_array($_REQUEST['galleryId'])) {
		$_REQUEST['galleryId'] = array($_REQUEST['galleryId']);
	}
	if (empty($_REQUEST['galleryId'][0])) {
		$_REQUEST['galleryId'][0] = $fileInfo['galleryId'];
	} elseif ($_REQUEST['galleryId'][0] != $fileInfo['galleryId']) {
		$smarty->assign('msg', tra("Could not find the file requested"));
		$smarty->display('error.tpl');
		die;
	}
} elseif (isset($_REQUEST['galleryId']) && !is_array($_REQUEST['galleryId'])) {
	$_REQUEST['galleryId'] = array($_REQUEST['galleryId']);
}
if (isset($_REQUEST['galleryId'][0])) {
	$gal_info = $tikilib->get_file_gallery((int)$_REQUEST['galleryId'][0]);
	$tikilib->get_perm_object($_REQUEST['galleryId'][0], 'file gallery', $gal_info, true);
}
if (empty($_REQUEST['fileId']) && $tiki_p_upload_files != 'y' && $tiki_p_admin_file_galleries != 'y') {
	$smarty->assign('errortype', 401);
	$smarty->assign('msg', tra("Permission denied"));
	$smarty->display('error.tpl');
	die;
}
if (isset($_REQUEST['galleryId'][1])) {
	foreach($_REQUEST['galleryId'] as $i => $gal) {
		if (!$i) continue;
		$perms = $tikilib->get_perm_object($_REQUEST['galleryId'][$key], 'file gallery', $gal_info, false);
		if ($perm['tiki_p_upload_files'] != 'y') {
			$smarty->assign('errortype', 401);
			$smarty->assign('msg', tra("Permission denied"));
			$smarty->display('error.tpl');
			die;
		}
	}
}
if (!empty($_REQUEST['fileId'])) {
	if (!empty($fileInfo['lockedby']) && $fileInfo['lockedby'] != $user && $tiki_p_admin_file_galleries != 'y') { // if locked must be the locker
		$smarty->assign('msg', tra(sprintf('The file is locked by %s', $fileInfo['lockedby'])));
		$smarty->display('error.tpl');
		die;
	}
	if (!((!empty($user) && ($user == $fileInfo['user'] || $user == $fileInfo['lockedby'])) || $tiki_p_edit_gallery_file == 'y')) { // must be the owner or the locker or have the perms
		$smarty->assign('errortype', 401);
		$smarty->assign('msg', tra("Permission denied you can edit this file"));
		$smarty->display('error.tpl');
		die;
	}
	if (isset($_REQUEST['lockedby']) && $fileInfo['lockedby'] != $_REQUEST['lockedby']) {
		if (empty($fileInfo['lockedby'])) {
			$smarty->assign('msg', tra(sprintf('The file has been unlocked meanwhile')));
		} else {
			$smarty->assign('msg', tra(sprintf('The file is locked by %s', $fileInfo['lockedby'])));
		}
		$smarty->display('error.tpl');
		die;
	}
	if ($gal_info['lockable'] == 'y' && empty($fileInfo['lockedby']) && $tiki_p_admin_file_galleries != 'y') {
		$smarty->assign('msg', tra('You must lock the file before editing it'));
		$smarty->display('error.tpl');
		die;
	}
}
$foo = parse_url($_SERVER["REQUEST_URI"]);
$foo1 = str_replace("tiki-upload_file", "tiki-download_file", $foo["path"]);
$smarty->assign('url_browse', $tikilib->httpPrefix() . $foo1);
$url_browse = $tikilib->httpPrefix() . $foo1;
// create direct download path for podcasts
$podcast_url = str_replace("tiki-upload_file.php", "", $foo["path"]);
$podcast_url = $tikilib->httpPrefix() . $podcast_url . $prefs['fgal_podcast_dir'];
if (!isset($_REQUEST["description"])) $_REQUEST["description"] = '';
if (!isset($_REQUEST['author'])) $_REQUEST['author'] = '';
if (isset($_REQUEST['hit_limit'])) $_REQUEST['hit_limit'] = (int)$_REQUEST['hit_limit'];
else $_REQUEST['hit_limit'] = 0;
$smarty->assign('show', 'n');
if (!empty($_REQUEST['galleryId'][0]) && $prefs['feature_groupalert'] == 'y') {
	$groupforalert = $groupalertlib->GetGroup('file gallery', (int)$_REQUEST['galleryId'][0]);
	if ($groupforalert != '') {
		$showeachuser = $groupalertlib->GetShowEachUser('file gallery', (int)$_REQUEST['galleryId'][0], $groupforalert);
		$listusertoalert = $userlib->get_users(0, -1, 'login_asc', '', '', false, $groupforalert, '');
		$smarty->assign_by_ref('listusertoalert', $listusertoalert['data']);
	}
	$smarty->assign_by_ref('groupforalert', $groupforalert);
	$smarty->assign_by_ref('showeachuser', $showeachuser);
}
if (isset($_REQUEST['fileId'])) $editFileId = $_REQUEST['fileId'];
$editFile = false;
if (!empty($editFileId)) {
	if (!empty($_REQUEST['name'][0])) $fileInfo['name'] = $_REQUEST['name'][0];
	if (!empty($_REQUEST['description'][0])) $fileInfo['description'] = $_REQUEST['description'][0];
	if (!empty($_REQUEST['user'][0])) $fileInfo['user'] = $_REQUEST['user'][0];
	if (!empty($_REQUEST['author'][0])) $fileInfo['author'] = $_REQUEST['author'][0];
	$smarty->assign_by_ref('fileInfo', $fileInfo);
	$smarty->assign('editFileId', $editFileId);
	$editFile = true;
}
if (!empty($_REQUEST['galleryId'][0])) {
	//$gal_info = $tikilib->get_file_gallery((int)$_REQUEST["galleryId"]);
	$smarty->assign_by_ref('gal_info', $gal_info);
	$podCastGallery = $filegallib->isPodCastGallery((int)$_REQUEST["galleryId"][0], $gal_info);
}
// Process an upload here
if (isset($_REQUEST["upload"])) {
	check_ticket('upload-file');
	//print_progress('<script type="text/javascript" src="lib/tiki-js.js"></script>');
	$error_msg = '';
	$errors = array();
	$uploads = array();
	$batch_job = false;
	$didFileReplace = false;
	foreach($_FILES["userfile"]["error"] as $key => $error) {
		if ($prefs['javascript_enabled'] == 'y') {
			print_progress('<?xml version="1.0" encoding="UTF-8"?>');
		}
		$formId = $_REQUEST['formId'];
		$smarty->assign("FormId", $_REQUEST['formId']);
		if (empty($_REQUEST['galleryId'][$key])) continue;
		if (!isset($_REQUEST['comment'][$key])) $_REQUEST['comment'][$key] = '';
		// We process here file uploads
		if (!empty($_FILES["userfile"]["name"][$key])) {
			// Were there any problems with the upload?  If so, report here.
			if (!is_uploaded_file($_FILES["userfile"]["tmp_name"][$key])) {
				$errors[] = $_FILES['userfile']['name'][$key] . ': ' . tra('Upload was not successful') . ': ' . $tikilib->uploaded_file_error($error);
				continue;
			}
			// Check the name
			if (!empty($prefs['fgal_match_regex'])) {
				if (!preg_match('/' . $prefs['fgal_match_regex'] . '/', $_FILES["userfile"]['name'][$key])) {
					$errors[] = tra('Invalid filename (using filters for filenames)') . ': ' . $_FILES["userfile"]["name"][$key];
					continue;
				}
			}
			if (!empty($prefs['fgal_nmatch_regex'])) {
				if (preg_match('/' . $prefs['fgal_nmatch_regex'] . '/', $_FILES["userfile"]["name"][$key])) {
					$errors[] = tra('Invalid filename (using filters for filenames)') . ': ' . $_FILES["userfile"]["name"][$key];
					continue;
				}
			}
			$name = $_FILES["userfile"]["name"][$key];
			if (isset($_REQUEST["isbatch"][$key]) && $_REQUEST["isbatch"][$key] == 'on' && strtolower(substr($name, strlen($name) - 3)) == 'zip') {
				if ($tiki_p_batch_upload_files == 'y') {
					$filegallib->process_batch_file_upload($_REQUEST["galleryId"][$key], $_FILES["userfile"]['tmp_name'][$key], $user, $_REQUEST["description"][$key]);
					$batch_job = true;
					$batch_job_galleryId = $_REQUEST["galleryId"][$key];
					print_msg(tra('Batch file processed') . " $name", $formId);
					continue;
				} else {
					$errors[] = tra('No permission to upload zipped file packages');
					continue;
				}
			}
			$file_name = $_FILES["userfile"]["name"][$key];
			$file_tmp_name = $_FILES["userfile"]["tmp_name"][$key];
			$tmp_dest = $prefs['tmpDir'] . "/" . $file_name . ".tmp";
			if (!move_uploaded_file($file_tmp_name, $tmp_dest)) {
				$errors[] = tra('Errors detected');
				continue;
			}
			$fp = fopen($tmp_dest, "rb");
			if (!$fp) {
				$errors[] = tra('Cannot read file:') . ' ' . $tmp_dest;
			}
			$data = '';
			$fhash = '';
			$extension = '';
			if (($prefs['fgal_use_db'] == 'n') || ($podCastGallery)) {
				$fhash = md5($name = $_FILES["userfile"]['name'][$key]);
				$extension = '';
				// for podcast galleries add the extension so the
				// file can be called directly if name is known,
				if ($podCastGallery) {
					$path_parts = pathinfo($_FILES["userfile"]['name'][$key]);
					if (in_array(strtolower($path_parts["extension"]), array("m4a", "mp3", "mov", "mp4", "m4v", "pdf", "flv"))) {
						$extension = "." . strtolower($path_parts["extension"]);
					}
					$savedir = $prefs['fgal_podcast_dir'];
				} else {
					$savedir = $prefs['fgal_use_dir'];
				}
				do {
					$fhash = md5(uniqid($fhash));
				}
				while (file_exists($savedir . $fhash . $extension));
				@$fw = fopen($savedir . $fhash . $extension, "wb");
				if (!$fw) {
					$errors[] = tra('Cannot write to this file:') . $savedir . $fhash;
				}
			}
			while (!feof($fp)) {
				if (($prefs['fgal_use_db'] == 'y') && (!$podCastGallery)) {
					$data.= fread($fp, 8192 * 16);
				} else {
					if (($data = fread($fp, 8192 * 16)) === false) {
						$errors[] = tra('Cannot read the file:') . ' ' . $tmp_dest;
					}
					fwrite($fw, $data);
				}
			}
			fclose($fp);
			// remove file after copying it to the right location or database
			@unlink($tmp_dest);
			if (($prefs['fgal_use_db'] == 'n') || ($podCastGallery)) {
				fclose($fw);
				$data = '';
			}
			$size = $_FILES["userfile"]['size'][$key];
			$name = stripslashes($_FILES["userfile"]['name'][$key]);
			$type = $_FILES["userfile"]['type'][$key];
			if (preg_match('/.flv$/', $name)) {
				$type = "video/x-flv";
			}
			if (count($errors)) {
				continue;
			}
			if (!$size) {
				$errors[] = tra('Warning: Empty file:') . '  ' . $name . '. ' . tra('Please re-upload your file');
			}
			if (($prefs['fgal_use_db'] == 'y') && (!$podCastGallery)) {
				if (!isset($data) || strlen($data) < 1) {
					$errors[] = tra('Warning: Empty file:') . ' ' . $name . '. ' . tra('Please re-upload your file');
				}
			}
			if (empty($_REQUEST['name'][$key])) $_REQUEST['name'][$key] = $name;
			if (empty($_REQUEST['user'][$key])) $_REQUEST['user'][$key] = $user;
			if (!isset($_REQUEST['description'][$key])) $_REQUEST['description'][$key] = '';
			if (empty($_REQUEST['author'][$key])) $_REQUEST['author'][$key] = $user;
			
			$fileInfo['filename'] = $file_name;
			if (isset($data)) {
				if ($editFile) {
					$didFileReplace = true;
					$fileId = $filegallib->replace_file($editFileId, $_REQUEST["name"][$key], $_REQUEST["description"][$key], $name, $data, $size, $type, $_REQUEST['user'][$key], $fhash . $extension, $_REQUEST['comment'][$key], $gal_info, $didFileReplace, $_REQUEST['author'][$key], $fileInfo['lastModif'], $fileInfo['lockedby']);
					if ($prefs['fgal_limit_hits_per_file'] == 'y') {
						$filegallib->set_download_limit($editFileId, $_REQUEST['hit_limit'][$key]);
					}
				} else {
					$fileId = $filegallib->insert_file($_REQUEST["galleryId"][$key], $_REQUEST["name"][$key], $_REQUEST["description"][$key], $name, $data, $size, $type, $_REQUEST['user'][$key], $fhash . $extension, '', $_REQUEST['author'][$key]);
{'fraction_non_alphanumeric': 0.23827484421121678, 'fraction_numerical': 0.007133486388979994, 'mean_word_length': 5.180136812769192, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 2, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4699301', 'n_tokens_mistral': 9625, 'n_tokens_neox': 8719, 'n_words': 1555}