Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
file_prefs.inc
1 <?php
18 require_once SQ_LIB_PATH.'/config/prefs.inc';
19 
31 class File_Prefs extends Prefs
32 {
33 
34  var $pref_vars = Array(
35  'SQ_FILE_MAX_SIZE' => Array(
36  'name' => 'max_file_upload',
37  'description' => 'This preference allows you to change the maximum size of files that can be uploaded',
38  'default' => 0,
39  'protected' => FALSE,
40  ),
41  'SQ_FILE_PREUPLOADED_MAX_SIZE' => Array(
42  'name' => 'preuploaded_max_file_upload',
43  'description' => 'This preference allows you to change the maximum size of files that can be created from pre-uploaded ones',
44  'default' => 0,
45  'protected' => FALSE,
46  ),
47  'SQ_FILE_ALLOW_NO_EXTENSION' => Array(
48  'name' => 'allow_file_with_no_extension',
49  'description' => 'This preference allows you to use file with no extension.',
50  'default' => '',
51  'protected' => FALSE,
52  ),
53  'SQ_FILE_ALLOWED_TYPES' => Array(
54  'name' => 'allowed_file_extensions_to_upload',
55  'description' => 'This preference allows you to restrict the allowed types of files to upload',
56  'default' => '',
57  'protected' => FALSE,
58  ),
59  'SQ_FILE_ALLOWED_PREUPLOADED_TYPES' => Array(
60  'name' => 'allowed_file_extentions_for_pre_uploaded_files',
61  'description' => 'This preference allows you to restrict the allowed types of pre-uploaded files',
62  'default' => '',
63  'protected' => FALSE,
64  ),
65 
66  'SQ_FILE_ASSET_SUMMARY' => Array(
67  'name' => 'file_asset_summary',
68  'description' => 'This preference allows user to customize the content for asset_summary_X keyword replacement',
69  'default' => '%file_type^uppercase%, %asset_file_size_in_bytes^divide:1024^round:2% KB',
70  'protected' => FALSE,
71  ),
72  );
73 
74 
79  function File_Prefs($pref_file='')
80  {
81  $this->Prefs($pref_file);
82 
83  }//end constructor
84 
85 
96  function paintBackend(&$o, $have_lock, $pref=NULL)
97  {
98  $is_root = $GLOBALS['SQ_SYSTEM']->userRoot();
99  $is_admin = ($is_root || $GLOBALS['SQ_SYSTEM']->userSystemAdmin());
100 
101 
102  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
103 
104  // work out the max file size that PHP is allowing
105  $ini_size = strtolower(ini_get('upload_max_filesize'));
106  if (substr($ini_size, -1) == 'k') {
107  $ini_size = $ini_size * 1024;
108  } else if (substr($ini_size, -1) == 'm') {
109  $ini_size = $ini_size * 1024 * 1024;
110  }
111 
112  // work out the max post size that PHP is allowing
113  $post_size = strtolower(ini_get('post_max_size'));
114  if (substr($post_size, -1) == 'k') {
115  $post_size = $post_size * 1024;
116  } else if (substr($post_size, -1) == 'm') {
117  $post_size = $post_size * 1024 * 1024;
118  }
119 
120  $mem_limit_size = strtolower(ini_get('memory_limit'));
121  if (substr($mem_limit_size, -1) == 'k') {
122  $mem_limit_size = $mem_limit_size * 1024;
123  } else if (substr($mem_limit_size, -1) == 'm') {
124  $mem_limit_size = $mem_limit_size * 1024 * 1024;
125  }
126  $php_max_file_size = min($ini_size, $post_size, $mem_limit_size);
127 
128  // Max File Size Upload
129  if (is_null($pref) || $pref == 'SQ_FILE_MAX_SIZE') {
130  if (is_null($pref)) {
131  $o->openField(translate( $this->pref_vars['SQ_FILE_MAX_SIZE']['name']));
132  }
133 
134  if ($have_lock && $is_admin) {
135  text_box('prefs[SQ_FILE_MAX_SIZE]', $this->pref_vars['SQ_FILE_MAX_SIZE']['default'], 10);
136  echo $o->note(translate('warn_max_settable_file_size', '<b>'.strtoupper(easy_filesize($php_max_file_size)).'</b>'));
137  if ($ini_size > $post_size) {
138  echo $o->note(translate('warn_max_settable_file_size_warning', '<b>'.strtoupper(easy_filesize($post_size)).'</b>'));
139  }
140  } else {
141  if (!$this->pref_vars['SQ_FILE_MAX_SIZE']['default']) {
142  echo translate('warn_set_max_file_size', '<b>'.strtoupper(easy_filesize($php_max_file_size)).'</b>');
143  } else {
144  $max_file_size = strtolower($this->pref_vars['SQ_FILE_MAX_SIZE']['default']);
145  if (substr($max_file_size, -1) == 'k') {
146  $max_file_size = $max_file_size * 1024;
147  } else if (substr($max_file_size, -1) == 'm') {
148  $max_file_size = $max_file_size * 1024 * 1024;
149  }
150 
151  if ($max_file_size > $php_max_file_size) {
152  echo translate('file_size_above_limit', '<b>'.strtoupper(easy_filesize($max_file_size)).'</b>', '<b>'.strtoupper(easy_filesize($php_max_file_size)).'</b>');
153  } else {
154  echo strtoupper(easy_filesize($max_file_size));
155  }
156  if ($ini_size > $post_size) {
157  echo $o->note(translate('warn_max_settable_file_size_warning', '<b>'.strtoupper(easy_filesize($post_size)).'</b>'));
158  }
159  }
160  }
161  if (is_null($pref)) $o->closeField();
162  }
163 
164  // Max Pre-uploaded File Size
165  if (is_null($pref) || $pref == 'SQ_FILE_PREUPLOADED_MAX_SIZE') {
166  if (is_null($pref)) {
167  $o->openField(translate($this->pref_vars['SQ_FILE_PREUPLOADED_MAX_SIZE']['name']));
168  }
169 
170  if ($have_lock && $is_admin) {
171  text_box('prefs[SQ_FILE_PREUPLOADED_MAX_SIZE]', $this->pref_vars['SQ_FILE_PREUPLOADED_MAX_SIZE']['default'], 10);
172  echo $o->note(translate('warn_preuploaded_max_settable_file_size', '<b>'.strtoupper(easy_filesize($php_max_file_size)).'</b>'));
173  } else {
174  if (!$this->pref_vars['SQ_FILE_PREUPLOADED_MAX_SIZE']['default']) {
175  echo translate('warn_preuploaded_max_settable_file_size', '<b>'.strtoupper(easy_filesize($php_max_file_size)).'</b>');
176  } else {
177  $max_file_size = strtolower($this->pref_vars['SQ_FILE_PREUPLOADED_MAX_SIZE']['default']);
178  if (substr($max_file_size, -1) == 'k') {
179  $max_file_size = $max_file_size * 1024;
180  } else if (substr($max_file_size, -1) == 'm') {
181  $max_file_size = $max_file_size * 1024 * 1024;
182  }
183  echo strtoupper(easy_filesize($max_file_size));
184  }
185  }
186  if (is_null($pref)) $o->closeField();
187  }
188 
189  // Allow No Extension
190  if (is_null($pref) || $pref == 'SQ_FILE_ALLOW_NO_EXTENSION') {
191  if (is_null($pref)) {
192  $o->openField(translate($this->pref_vars['SQ_FILE_ALLOW_NO_EXTENSION']['name']));
193  }
194 
195  $value = $this->pref_vars['SQ_FILE_ALLOW_NO_EXTENSION']['default'];
196  if ($have_lock && $is_admin) {
197  $options = Array(1 => 'Yes', 0 => 'No');
198  combo_box('prefs[SQ_FILE_ALLOW_NO_EXTENSION]', $options, FALSE, $value);
199  } else {
200  echo ($value) ? translate('yes') : translate('no');
201  }
202  if (is_null($pref)) $o->closeField();
203  }
204 
205  // Allowed File Extensions
206  if (is_null($pref) || $pref == 'SQ_FILE_ALLOWED_TYPES') {
207  if (is_null($pref)) {
208  $o->openField(translate($this->pref_vars['SQ_FILE_ALLOWED_TYPES']['name']));
209  }
210 
211  if ($have_lock && $is_admin) {
212  text_box('prefs[SQ_FILE_ALLOWED_TYPES]', $this->pref_vars['SQ_FILE_ALLOWED_TYPES']['default'], 20);
213  echo $o->note(translate('allowed_file_extensions_note'));
214  } else {
215  $value = $this->pref_vars['SQ_FILE_ALLOWED_TYPES']['default'];
216  if (empty($value)) {
217  echo translate('allowed_file_extensions_not_specified');
218  } else {
219  $exts = explode(',', $value);
220  $num_exts = count($exts);
221  if ($num_exts == 1) {
222  $ext_str = array_pop($exts);
223  } else if ($num_exts > 1) {
224  $ext_str = implode(', ', array_slice($exts, 0, -1)).' and '.$exts[$num_exts - 1];
225  }
226  echo translate('allowed_file_extensions_list', (($num_exts > 1) ? 's are ' : ' is ').$ext_str);
227  }
228  }
229  if (is_null($pref)) $o->closeField();
230  }
231 
232  // Allowed File Extensions for Pre-uploaded
233  if (is_null($pref) || $pref == 'SQ_FILE_ALLOWED_PREUPLOADED_TYPES') {
234  if (is_null($pref)) {
235  $o->openField(translate($this->pref_vars['SQ_FILE_ALLOWED_PREUPLOADED_TYPES']['name']));
236  }
237 
238  if ($have_lock && $is_admin) {
239  text_box('prefs[SQ_FILE_ALLOWED_PREUPLOADED_TYPES]', $this->pref_vars['SQ_FILE_ALLOWED_PREUPLOADED_TYPES']['default'], 20);
240  echo $o->note(translate('allowed_file_extensions_note'));
241  } else {
242  $value = $this->pref_vars['SQ_FILE_ALLOWED_PREUPLOADED_TYPES']['default'];
243  if (empty($value)) {
244  echo translate('allowed_file_extensions_not_specified');
245  } else {
246  $exts = explode(',', $value);
247  $num_exts = count($exts);
248  if ($num_exts == 1) {
249  $ext_str = array_pop($exts);
250  } else if ($num_exts > 1) {
251  $ext_str = implode(', ', array_slice($exts, 0, -1)).' and '.$exts[$num_exts - 1];
252  }
253  echo translate('allowed_file_extensions_list', (($num_exts > 1) ? 's are ' : ' is ').$ext_str);
254  }
255  }
256  if (is_null($pref)) $o->closeField();
257  }
258 
259  // File summary defination
260  if (is_null($pref) || $pref == 'SQ_FILE_ASSET_SUMMARY') {
261  if (is_null($pref)) {
262  $o->openField(translate($this->pref_vars['SQ_FILE_ASSET_SUMMARY']['name']));
263  }
264 
265  if ($have_lock && $is_admin) {
266  text_area('prefs[SQ_FILE_ASSET_SUMMARY]', $this->pref_vars['SQ_FILE_ASSET_SUMMARY']['default'], 60, 5);
267  echo $o->note(translate('file_asset_summary_note'));
268  } else {
269  $value = $this->pref_vars['SQ_FILE_ASSET_SUMMARY']['default'];
270  echo $value;
271  }
272  if (is_null($pref)) $o->closeField();
273  }
274 
275  return TRUE;
276 
277  }//end paintBackend()
278 
279 
290  function processBackend(&$o, $have_lock, $pref=NULL)
291  {
292  $is_root = $GLOBALS['SQ_SYSTEM']->userRoot();
293  $is_admin = ($is_root || $GLOBALS['SQ_SYSTEM']->userSystemAdmin());
294  if (!$is_admin) return FALSE;
295 
296  // Max File Size Upload
297  if (is_null($pref) || $pref == 'SQ_FILE_MAX_SIZE') {
298  if (isset($_POST['prefs']['SQ_FILE_MAX_SIZE'])) {
299  $max_size = strtoupper(trim($_POST['prefs']['SQ_FILE_MAX_SIZE']));
300  if (empty($max_size)) $max_size = 0;
301 
302  if (!empty($max_size) && !preg_match('|^[0-9]+[KM]$|', $max_size)) {
303  trigger_localised_error('CORE0002', E_USER_WARNING, $max_size);
304  } else {
305  $this->pref_vars['SQ_FILE_MAX_SIZE']['default'] = $max_size;
306  }
307  }
308  }
309 
310  // Max Pre-uploaded File Size
311  if (is_null($pref) || $pref == 'SQ_FILE_PREUPLOADED_MAX_SIZE') {
312  if (isset($_POST['prefs']['SQ_FILE_PREUPLOADED_MAX_SIZE'])) {
313  $max_size = strtoupper(trim($_POST['prefs']['SQ_FILE_PREUPLOADED_MAX_SIZE']));
314  if (empty($max_size)) $max_size = 0;
315 
316  if (!empty($max_size) && !preg_match('|^[0-9]+[KM]$|', $max_size)) {
317  trigger_localised_error('CORE0002', E_USER_WARNING, $max_size);
318  } else {
319  $this->pref_vars['SQ_FILE_PREUPLOADED_MAX_SIZE']['default'] = $max_size;
320  }
321  }
322  }
323 
324  // Allow No Extension
325  if (is_null($pref) || $pref == 'SQ_FILE_ALLOW_NO_EXTENSION') {
326  if (isset($_POST['prefs']['SQ_FILE_ALLOW_NO_EXTENSION'])) {
327  $allow = strtoupper(trim($_POST['prefs']['SQ_FILE_ALLOW_NO_EXTENSION']));
328  $this->pref_vars['SQ_FILE_ALLOW_NO_EXTENSION']['default'] = $allow;
329  }
330  }
331 
332  // Allowed File Extensions
333  if (is_null($pref) || $pref == 'SQ_FILE_ALLOWED_TYPES') {
334  if (isset($_POST['prefs']['SQ_FILE_ALLOWED_TYPES'])) {
335  $result = $this->_tidyFileTypeString($_POST['prefs']['SQ_FILE_ALLOWED_TYPES']);
336  $this->pref_vars['SQ_FILE_ALLOWED_TYPES']['default'] = $result;
337  }
338  }
339 
340  // Allowed File Extensions for Pre-uploaded
341  if (is_null($pref) || $pref == 'SQ_FILE_ALLOWED_PREUPLOADED_TYPES') {
342  if (isset($_POST['prefs']['SQ_FILE_ALLOWED_PREUPLOADED_TYPES'])) {
343  $result = $this->_tidyFileTypeString($_POST['prefs']['SQ_FILE_ALLOWED_PREUPLOADED_TYPES']);
344  $this->pref_vars['SQ_FILE_ALLOWED_PREUPLOADED_TYPES']['default'] = $result;
345  }
346  }
347 
348  // File summary defination
349  if (is_null($pref) || $pref == 'SQ_FILE_ASSET_SUMMARY') {
350  if (isset($_POST['prefs']['SQ_FILE_ASSET_SUMMARY'])) {
351  $this->pref_vars['SQ_FILE_ASSET_SUMMARY']['default'] = $_POST['prefs']['SQ_FILE_ASSET_SUMMARY'];
352  }
353  }
354 
355  return TRUE;
356 
357  }//end processBackend()
358 
359 
370  function _tidyFileTypeString($str)
371  {
372  $str = str_replace(' ', '', strtolower(trim($str)));
373  if (empty($str)) return '';
374 
375  if (substr($str, -1) == ',') {
376  $str = substr($str, 0, -1);
377  }
378 
379  if (strpos($str, ',') !== FALSE) {
380  $tmp = explode(',', $str);
381  $new_tokens = Array();
382  foreach ($tmp as $token) {
383  if (!empty($token)) $new_tokens[] = $token;
384  }
385  } else {
386  return $str;
387  }
388 
389  if (empty($new_tokens)) {
390  return '';
391  } else {
392  $new_tokens = array_unique($new_tokens);
393  }
394  return implode(',', $new_tokens);
395 
396  }//end _tidyFileTypeString()
397 
398 
408  function mergePrefs($prefs1, $prefs2)
409  {
410  $merged_prefs = Array();
411  $pref_vars = Array(
412  'SQ_FILE_MAX_SIZE',
413  'SQ_FILE_PREUPLOADED_MAX_SIZE',
414  'SQ_FILE_ALLOW_NO_EXTENSION',
415  'SQ_FILE_ALLOWED_TYPES',
416  'SQ_FILE_ALLOWED_PREUPLOADED_TYPES',
417  );
418 
419  foreach ($pref_vars as $var_name) {
420  if (isset($prefs1[$var_name]) && !isset($prefs2[$var_name])) {
421  // this is only set in prefs1
422  $merged_prefs[$var_name] = $prefs1[$var_name];
423  } else if (isset($prefs2[$var_name]) && !isset($prefs1[$var_name])) {
424  // this is only set in prefs2
425  $merged_prefs[$var_name] = $prefs2[$var_name];
426  } else if (isset($prefs1[$var_name]) && isset($prefs2[$var_name])) {
427  // this is set in both prefs
428  switch ($var_name) {
429  case 'SQ_FILE_MAX_SIZE' :
430  case 'SQ_FILE_PREUPLOADED_MAX_SIZE' :
431  // use the maximum allowed file size
432  $pref_1_size = strtolower($prefs1[$var_name]['default']);
433  if (substr($pref_1_size, -1) == 'k') {
434  $pref_1_size = $pref_1_size * 1024;
435  } else if (substr($pref_1_size, -1) == 'm') {
436  $pref_1_size = $pref_1_size * 1024 * 1024;
437  }
438 
439  $pref_2_size = strtolower($prefs2[$var_name]['default']);
440  if (substr($pref_1_size, -1) == 'k') {
441  $pref_1_size = $pref_1_size * 1024;
442  } else if (substr($pref_1_size, -1) == 'm') {
443  $pref_1_size = $pref_1_size * 1024 * 1024;
444  }
445 
446  // Go for smaller size
447  if ($pref_1_size > $pref_2_size) {
448  $merged_prefs[$var_name] = $prefs1[$var_name];
449  } else {
450  $merged_prefs[$var_name] = $prefs2[$var_name];
451  }
452  break;
453  case 'SQ_FILE_ALLOW_NO_EXTENSION' :
454  $pref_1_val = $prefs1[$var_name]['default'];
455  $pref_2_val = $prefs2[$var_name]['default'];
456  $merged_prefs[$var_name]['default'] = ($pref_1_val || $pref_2_val);
457  break;
458  case 'SQ_FILE_ALLOWED_TYPES' :
459  case 'SQ_FILE_ALLOWED_PREUPLOADED_TYPES' :
460  if (is_array($prefs1[$var_name]) && isset($prefs1[$var_name]['default'])) {
461  $pref_1_exts_str = $prefs1[$var_name]['default'];
462  } else {
463  $pref_1_exts_str = $prefs1[$var_name];
464  }
465  if (is_array($prefs2[$var_name]) && isset($prefs2[$var_name]['default'])) {
466  $pref_2_exts_str = $prefs2[$var_name]['default'];
467  } else {
468  $pref_2_exts_str = $prefs2[$var_name];
469  }
470  $pref_merge_str = $pref_1_exts_str.','.$pref_2_exts_str;
471  $merged_prefs[$var_name]['default'] = implode(',', array_unique(explode(',', $pref_merge_str)));
472  break;
473  }//end switch
474 
475  $merged_prefs[$var_name]['name'] = $prefs1[$var_name]['name'];
476  $merged_prefs[$var_name]['description'] = $prefs1[$var_name]['description'];
477 
478  if (isset($prefs2[$var_name]['protected'])) {
479  $merged_prefs[$var_name]['protected'] = ($prefs1[$var_name]['protected'] || $prefs2[$var_name]['protected']);
480  } else {
481  $merged_prefs[$var_name]['protected'] = $prefs1[$var_name]['protected'];
482  }
483  }//end if
484 
485  }//end foreach
486 
487  return $merged_prefs;
488 
489  }//end mergePrefs()
490 
491 
492 }//end class
493 
494 ?>