Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_file_size_edit_fns.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question_rule/form_question_rule_edit_fns.inc';
19 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
20 
33 {
34 
35 
41  {
43 
44  }//end constructor
45 
46 
47  function paintRule(&$asset, &$o, $prefix, $rule_data, $write_access)
48  {
49  $operator_list = $asset->getOperators();
50  $multiplier_map = Array(
51  '' => 'bytes',
52  'KB' => 'KB',
53  'MB' => 'MB',
54  'GB' => 'GB',
55  );
56 
57  if (!$write_access) {
58  $o->openField(translate('core_form_rule_details'));
59  echo translate('core_form_rule_file_size_edit_'.array_get_index($rule_data, 'operator', '<='), array_get_index($rule_data, 'value', '0').' '.$multiplier_map[array_get_index($rule_data, 'multiplier', '')]);
60  $o->closeField();
61 
62  } else {
63  $o->openField(translate('operator'));
64  // list of operators
65  combo_box($prefix.'[operator]', $operator_list, false, array_get_index($rule_data, 'operator', '<='));
66  $o->closeField();
67 
68  $o->openField(translate('file_size'));
69  // give a text box to write the comparison value in
70  int_text_box($prefix.'[value]', array_get_index($rule_data, 'value', ''), false, 3);
71  combo_box($prefix.'[multiplier]', $multiplier_map, false, array_get_index($rule_data, 'multiplier', ''));
72  $o->closeField();
73 
74  $o->sectionNote(translate('core_form_rule_file_size_config_note', strtoupper(easy_filesize($this->_maxUploadSize()))));
75  }
76 
77  return $write_access;
78 
79  }//end paintRule()
80 
81 
88  function processRule(&$asset, &$o, $prefix, &$rule_data)
89  {
90  if (!isset($_POST[$prefix])) return false;
91 
92  $multipliers = Array(0 => '', 1 => 'KB', 2 => 'MB', 3 => 'GB');
93 
94  $operator = array_get_index($_POST[$prefix], 'operator', null);
95  $comparison = array_get_index($_POST[$prefix], 'value', null);
96  $multiplier = strtoupper(trim(array_get_index($_POST[$prefix], 'multiplier', '')));
97 
98  // expand the comparison size and check whether it is above maximum
99  // upload size
100  $comparison_size = (float)$comparison * pow(2, 10 * array_search($multiplier, $multipliers));
101 
102  if ($comparison_size > $this->_maxUploadSize()) {
103  // something like "File size exceeds system-imposed limit of xx MB, resetting to this limit"
104  // TRANSLATE: CMS0003 => CORE0234
105  trigger_localised_error('CMS0003', E_USER_NOTICE, easy_filesize($this->_maxUploadSize()));
106  $comparison_size = $this->_maxUploadSize();
107  }
108 
109  // then fold back whatever the comparison size is now and store
110  $final_multi = 0;
111  while (($comparison_size >= 1024) && ($final_multi < 3)) {
112  $comparison_size /= 1024;
113  $final_multi++;
114  }
115 
116  $rule_data['operator'] = $operator;
117  $rule_data['value'] = $comparison_size;
118  $rule_data['multiplier'] = $multipliers[$final_multi];
119 
120  return true;
121 
122  }//end processRule()
123 
124 
131  function _maxUploadSize()
132  {
133  $upload_size = strtolower(ini_get('upload_max_filesize'));
134 
135  // allow users to specify abbreviated forms of file size (kB, MB, GB)
136  // the conversion used is 1024 to the kByte, rather than the decimal 1000
137  if (substr($upload_size,-1) == 'k') {
138  $upload_size = floatval($upload_size) * 1024;
139  } else if (substr($upload_size,-1) == 'm') {
140  $upload_size = floatval($upload_size) * 1024 * 1024;
141  }
142 
143  $post_size = strtolower(ini_get('post_max_size'));
144 
145  // allow users to specify abbreviated forms of file size (kB, MB, GB)
146  // the conversion used is 1024 to the kByte, rather than the decimal 1000
147  if (substr($post_size,-1) == 'k') {
148  $post_size = floatval($post_size) * 1024;
149  } else if (substr($post_size,-1) == 'm') {
150  $post_size = floatval($post_size) * 1024 * 1024;
151  }
152 
153  $mem_limit_size = strtolower(ini_get('memory_limit'));
154 
155  // allow users to specify abbreviated forms of file size (kB, MB, GB)
156  // the conversion used is 1024 to the kByte, rather than the decimal 1000
157  if (substr($mem_limit_size,-1) == 'k') {
158  $mem_limit_size = floatval($mem_limit_size) * 1024;
159  } else if (substr($mem_limit_size,-1) == 'm') {
160  $mem_limit_size = floatval($mem_limit_size) * 1024 * 1024;
161  }
162 
163  return min($upload_size, $post_size, $mem_limit_size);
164  }//end _maxUploadSize()
165 
166 
167 }//end class
168 ?>