aboutsummaryrefslogtreecommitdiffstats
path: root/include/environment.php
blob: 47ad241a78d7df1e8864d4572a3702c50de2a606 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
 * @file include/environment.php
 * @brief Functions related to system/environment tasks.
 *
 * This file contains some functions to check the environment/system.
 */

/**
 * @brief Get some upload related limits from php.ini.
 *
 * This function returns values from php.ini like \b post_max_size,
 * \b max_file_uploads, \b upload_max_filesize.
 *
 * @return array associative array
 *   * \e int \b post_max_size the maximum size of a complete POST in bytes
 *   * \e int \b upload_max_filesize the maximum size of one file in bytes
 *   * \e int \b max_file_uploads maximum number of files in one POST
 *   * \e int \b max_upload_filesize min(post_max_size, upload_max_filesize)
 */
function getPhpiniUploadLimits() {
	$ret = array();

	// max size of the complete POST
	$ret['post_max_size'] = phpiniSizeToBytes(ini_get('post_max_size'));
	// max size of one file
	$ret['upload_max_filesize'] = phpiniSizeToBytes(ini_get('upload_max_filesize'));
	// catch a configuration error where post_max_size < upload_max_filesize
	$ret['max_upload_filesize'] = min(
			$ret['post_max_size'],
			$ret['upload_max_filesize']
			);
	// maximum number of files in one POST
	$ret['max_file_uploads'] = intval(ini_get('max_file_uploads'));

	return $ret;
}

/**
 * @brief Parses php_ini size settings to bytes.
 *
 * This function parses common size setting from php.ini files to bytes.
 * e.g. post_max_size = 8M ==> 8388608
 *
 * \note This method does not recognise other human readable formats like
 * 8MB, etc.
 *
 * @todo Make this function more universal useable. MB, T, etc.
 *
 * @param string $val value from php.ini e.g. 2M, 8M
 * @return int size in bytes
 */
function phpiniSizeToBytes($val) {
	$val = trim($val);
	$unit = strtolower($val[strlen($val)-1]);
	switch($unit) {
		case 'g':
			$val *= 1024;
		case 'm':
			$val *= 1024;
		case 'k':
			$val *= 1024;
	}

	return (int)$val;
}