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//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
10	header("location: index.php");
11	exit;
12}
13
14// Try to detect a file max size due to upload or memory limits
15// (to do an insert into the database, the data has to be put in memory by PHP)
16$smarty = TikiLib::lib('smarty');
17$tikilib = TikiLib::lib('tiki');
18@$max_upload_size = $tikilib->return_bytes(ini_get('upload_max_filesize'));
19@$post_max_size = $tikilib->return_bytes(ini_get('post_max_size'));
20@$max_file_uploads = ini_get('max_file_uploads');
21$max_upload_size_comment = tra("This is the value of your server's PHP '%s' setting");
22
23if ($post_max_size > 0 && ( $post_max_size < $max_upload_size || $max_upload_size == 0 )) {
24	$max_upload_size = $post_max_size;
25	$max_upload_size_comment = sprintf($max_upload_size_comment, 'post_max_size');
26} else {
27	$max_upload_size_comment = sprintf($max_upload_size_comment, 'upload_max_filesize');
28}
29
30// Get memory limit
31@$memory_limit_ini = ini_get('memory_limit');
32$memory_limit = $tikilib->return_bytes($memory_limit_ini);
33
34// Try to detect current memory usage or set it arbitrary to 10MB
35@$current_memory_usage = function_exists('memory_get_usage') ? (int)memory_get_usage(true) : 10 * 1024 * 1024;
36
37// TODO: this is quite deprecated code and possibly quite wrong - we no longer use ADODB and string escaping, so check what is the real limit!
38if ($prefs['fgal_use_db'] == 'y' && (empty($podCastGallery) || ! $podCastGallery)) {
39	if ($memory_limit > 0) {
40		// Estimate available memory for file upload.
41		// The result is divided by 3, because the file has to be stored twice in memory :
42		//    one copy when reading the file, and two other modified copies in ADODB when adding quotes to the query variables
43		//    ( due to functions like mysqli_real_escape_string that takes 200% more memory)
44		// We also reduce of a memory size of 3 MB (which is an approximation too) that may be necessary for other tasks to work
45		//
46		$remaining_memory = max(0, ((int)($memory_limit - $current_memory_usage) / 3) - (3 * 1024 * 1024));
47
48		if ($max_upload_size > $remaining_memory) {
49			$max_upload_size = $remaining_memory;
50			$max_upload_size_comment = tra('This is an approximation based on your server memory limit:') . ' ' . $memory_limit_ini;
51		}
52	}
53}
54
55$smarty->assign("max_upload_size_comment", $max_upload_size_comment);
56$smarty->assign("max_upload_size", "$max_upload_size");
57$smarty->assign("max_file_uploads", "$max_file_uploads");
58