Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
general_occasional.inc
1 <?php
36 function link_type_name($type)
37 {
44  static $_link_type_name_cache = array();
45  if (isset($_link_type_name_cache[$type])) {
46  return $_link_type_name_cache[$type];
47  }
48 
49  $names = get_bit_names('SQ_LINK_', $type);
50 
51  if (is_array($names)) {
52  $str = '';
53  for ($i = 0; $i < count($names); $i++) {
54  $str .= $names[$i];
55  if ($i < count($names) - 2) {
56  $str .= ', ';
57  } else if ($i < count($names) - 1) {
58  $str .= ' or ';
59  }
60  }
61  $_link_type_name_cache[$type] = $str;
62  return $str;
63  } else {
64  $_link_type_name_cache[$type] = $names;
65  return $names;
66  }
67 
68 }//end link_type_name()
69 
70 
77 function get_link_type_names()
78 {
79  static $_get_link_type_names_cache = array();
80  if (!empty($_get_link_type_names_cache)) {
81  return $_get_link_type_names_cache;
82  }
83  $link_types = get_constant_values('SQ_LINK_');
84 
85  foreach ($link_types as $value) {
86  $type_names[$value] = link_type_name($value);
87  }
88 
89  $_get_link_type_names_cache = $type_names;
90 
91  return $type_names;
92 
93 }//end get_link_type_names()
94 
95 
104 function permission_type_name($type)
105 {
106  if (isset($GLOBALS['SQ_PERM_TYPE_NAME'][$type]) === FALSE) {
107  $name = get_bit_names('SQ_PERMISSION_', $type, FALSE, TRUE);
108  if (is_array($name)) return 'Unknown';
109  $GLOBALS['SQ_PERM_TYPE_NAME'][$type] = ucwords(strtolower($name));
110  }
111 
112  return $GLOBALS['SQ_PERM_TYPE_NAME'][$type];
113 
114 }//end permission_type_name()
115 
116 
131 function get_bit_names($prefix, $bit_value, $force_array=FALSE, $exact_value=FALSE)
132 {
133  if ($exact_value) {
134  $bit_values = Array($bit_value);
135  } else {
136  $bit_values = bit_elements($bit_value);
137  }
138 
144  static $_cache = array();
145  $hash = $prefix.'.'.$bit_value.'.'.(int)$force_array.'.'.(int)$exact_value;
146  if (isset($_cache[$hash]) === TRUE) {
147  return $_cache[$hash];
148  }
149 
150  // We only care about the ones we've defined ourselves.
151  static $_userConstants = array();
152  if (empty($_userConstants) === TRUE) {
153  $constants = get_defined_constants(TRUE);
154  $_userConstants = $constants['user'];
155  }
156 
157  $names = Array();
158 
159  foreach ($_userConstants as $name => $value) {
160  if (preg_match('/^'.$prefix.'(.*)$/', $name, $matches) && in_array($value, $bit_values)) {
161  $names[] = $matches[1];
162  }
163  }
164 
165  if ($force_array || count($names) > 1) {
166  $_cache[$hash] = $names;
167  return $names;
168  } else if (count($names)) {
169  $_cache[$hash] = $names[0];
170  return $names[0];
171  }
172 
173  return '';
174 
175 }//end get_bit_names()
176 
177 
186 function get_constant_values($prefix)
187 {
188  // Check our local cache first.
189  // The names are based on defined variables
190  // and can't change, so we can cache them nicely.
191  static $_cache = array();
192  if (isset($_cache[$prefix]) === TRUE) {
193  return $_cache[$prefix];
194  }
195 
196  // We only care about the ones we've defined ourselves.
197  static $_userConstants = array();
198  if (empty($_userConstants) === TRUE) {
199  $constants = get_defined_constants(TRUE);
200  $_userConstants = $constants['user'];
201  }
202 
203  $names = Array();
204  foreach ($_userConstants as $name => $value) {
205  if (preg_match('/^'.$prefix.'(.*)$/', $name, $matches)) {
206  $names[$name] = $value;
207  }
208  }
209 
210  $_cache[$prefix] = $names;
211 
212  return $names;
213 
214 }//end get_constant_values()
215 
216 
225 function get_status_description($status)
226 {
227  if (isset($GLOBALS['SQ_STATUS_DESC_NAME'][$status]) === FALSE) {
228  // work out the name of our status file
229  $status_code = get_bit_names('SQ_STATUS_', $status);
230  $status_code = 'asset_status_'.strtolower($status_code);
231 
232  require_once SQ_INCLUDE_PATH.'/asset_status/'.$status_code.'.inc';
233  if (isset($this)) {
234  $status_object = new $status_code($this);
235  } else {
236  $status_object = new $status_code(NULL);
237  }
238 
239  $GLOBALS['SQ_STATUS_DESC_NAME'][$status] = $status_object->getDescription();
240  }
241 
242  return $GLOBALS['SQ_STATUS_DESC_NAME'][$status];
243 
244 }//end get_status_description()
245 
246 
255 function get_status_value($description)
256 {
257  foreach (get_constant_values('SQ_STATUS_') as $status_value) {
258  if (strtolower(get_status_description($status_value)) == strtolower($description)) {
259  return $status_value;
260  }
261  }//end foreach
262 
263 }//end get_status_value()
264 
265 
274 function get_status_colour($status)
275 {
276  switch ($status) {
277  case SQ_STATUS_ARCHIVED :
278  $colour = 'C2B0A0';
279  break;
280  case SQ_STATUS_UNDER_CONSTRUCTION :
281  $colour = 'AACCDD';
282  break;
283  case SQ_STATUS_PENDING_APPROVAL :
284  $colour = 'DCD2E6';
285  break;
286  case SQ_STATUS_APPROVED :
287  $colour = 'F4D425';
288  break;
289  case SQ_STATUS_LIVE :
290  $colour = 'DBF18A';
291  break;
292  case SQ_STATUS_LIVE_APPROVAL :
293  $colour = '50D000';
294  break;
295  case SQ_STATUS_EDITING :
296  $colour = 'F25C86';
297  break;
298  case SQ_STATUS_EDITING_APPROVAL :
299  $colour = 'CC7CC7';
300  break;
301  case SQ_STATUS_EDITING_APPROVED :
302  $colour = 'FF9A00';
303  break;
304  default :
305  $colour = 'FF0000';
306  break;
307  }//end switch
308 
309  return $colour;
310 
311 }//end get_status_colour()
312 
313 
320 function get_status_names()
321 {
322  $res = Array();
323  $statuses = get_constant_values('SQ_STATUS_');
324  foreach ($statuses as $name => $value) {
325  $res[$value] = ucwords(strtolower(str_replace(Array('SQ_STATUS_', '_'), Array('', ' '), $name)));
326  }
327 
328  return $res;
329 
330 }//end get_status_names()
331 
332 
345 function asset_link_treeid_convert($value, $encode)
346 {
347  // these characters are chosen so as not to clash with any SQL control symbols - ie . * ^ \ [ ] { } ( ) < > ? & $ _
348  static $digits = Array(
349  0 => '0', 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7',
350  8 => '8', 9 => '9', 10 => '=', 11 => 'A', 12 => 'B', 13 => 'C', 14 => 'D', 15 => 'E',
351  16 => 'F', 17 => 'G', 18 => 'H', 19 => 'I', 20 => 'J', 21 => 'K', 22 => 'L', 23 => 'M',
352  24 => 'N', 25 => 'O', 26 => 'P', 27 => 'Q', 28 => 'R', 29 => 'S', 30 => 'T', 31 => 'U',
353  32 => 'V', 33 => 'W', 34 => 'X', 35 => 'Y', 36 => 'Z', 37 => 'a', 38 => 'b', 39 => 'c',
354  40 => 'd', 41 => 'e', 42 => 'f', 43 => 'g', 44 => 'h', 45 => 'i', 46 => 'j', 47 => 'k',
355  48 => 'l', 49 => 'm', 50 => 'n', 51 => 'o', 52 => 'p', 53 => 'q', 54 => 'r', 55 => 's',
356  56 => 't', 57 => 'u', 58 => 'v', 59 => 'w', 60 => 'x', 61 => 'y', 62 => 'z', 63 => '~',
357  );
358 
359  // encode value ?
360  if ($encode) {
361  $str = '';
362  $i = (int) $value;
363  while ($i != 0) {
364  $str = $digits[$i % SQ_CONF_ASSET_TREE_BASE].$str;
365  $i = floor($i / SQ_CONF_ASSET_TREE_BASE);
366  }
367  if (strlen($str) > SQ_CONF_ASSET_TREE_SIZE) {
368  trigger_localised_error('SYS0213', E_USER_WARNING, (int) $value, SQ_CONF_ASSET_TREE_SIZE, (pow(SQ_CONF_ASSET_TREE_BASE, SQ_CONF_ASSET_TREE_SIZE) - 1));
369  return NULL;
370  } else {
371  return str_pad($str, SQ_CONF_ASSET_TREE_SIZE, $digits[0], STR_PAD_LEFT);
372  }
373  } else {
374  // decode value
375  $num = 0;
376  $str = (string) $value;
377  $len = strlen($str);
378  $x = pow(SQ_CONF_ASSET_TREE_BASE, $len - 1);
379  for ($i = 0; $i < $len; $i++) {
380  if (($d = array_search($str{$i}, $digits)) === FALSE) {
381  return NULL;
382  }
383  $num += $x * $d;
384  $x = $x / SQ_CONF_ASSET_TREE_BASE;
385  }
386  return (int) $num;
387 
388  }// end if
389 
390 }//end asset_link_treeid_convert()
391 
392 
405 function make_valid_web_paths($paths, $fallback_path='')
406 {
407  $valid_paths = Array();
408 
409  // get replacement array if config settings are true
410  $lang_char_map = Array();
411  if (SQ_CONF_USE_LANG_CHAR_MAP) {
412  $lang_char_map = getLangCharMap();
413  }
414 
415  foreach ($paths as $path) {
416  $path = trim($path);
417  // replace language characters if replacement array is specified
418  if (!empty($lang_char_map)) {
419  $path = str_replace(array_keys($lang_char_map), array_values($lang_char_map), $path);
420  }
421 
422  // get rid of paths that are the same as the backend suffix
423  if ($path == SQ_CONF_BACKEND_SUFFIX || $path == SQ_CONF_LIMBO_SUFFIX || $path == SQ_CONF_LOGIN_SUFFIX || $path == SQ_CONF_NOCACHE_SUFFIX) {
424  trigger_localised_error('SYS0114', E_USER_WARNING, $path);
425  continue;
426  }
427 
428  // get rid of paths that start with the two underscores
429  if (preg_match('/^__/', $path)) {
430  trigger_localised_error('SYS0115', E_USER_WARNING);
431  continue;
432  }
433 
434  // no ampersands in web paths
435  $path = str_replace('&amp;', SQ_CONF_WEB_PATH_SEPARATOR.translate('web_path_ampersand_replacement').SQ_CONF_WEB_PATH_SEPARATOR, $path);
436  $path = preg_replace('|&[^;]+;|', '', $path);
437  $path = preg_replace('|&#[0-9]+;|', '', $path);
438  $path = str_replace('&', SQ_CONF_WEB_PATH_SEPARATOR.translate('web_path_ampersand_replacement').SQ_CONF_WEB_PATH_SEPARATOR, $path);
439 
440  // no spaces in web paths
441  $path = preg_replace('/\\s+/', SQ_CONF_WEB_PATH_SEPARATOR, $path);
442 
443  // no parentheses
444  $path = preg_replace('/[\(\)\[\]]/', '', $path);
445 
446  // replace accents, handle utf-8 input
447  if (function_exists('mb_convert_encoding')) {
448  $path = mb_convert_encoding($path, 'HTML-ENTITIES', SQ_CONF_DEFAULT_CHARACTER_SET);
449  } else {
450  $path = htmlentities($path, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
451  }
452 
453  // replace accented characters
454  $path = preg_replace('/&([a-zA-Z])(ring|uml|acute|grave|circ|tilde);/', '$1', $path);
455 
456  // and some more entities
457  $path = preg_replace('|&[^;]+;|', '', $path);
458 
459  // taken (in part) from info here -> http://www.w3.org/Addressing/URL/5_URI_BNF.html
460  $path = preg_replace('/[^a-zA-Z0-9\-$_@.!*~(),]/', '', $path);
461 
462  // Bug Fix #3991 Run time error if SQ_CONF_WEB_PATH_SEPARATOR is empty
463  // replace more than one web path separator with just one
464  if (SQ_CONF_WEB_PATH_SEPARATOR != '') $path = preg_replace('/'.SQ_CONF_WEB_PATH_SEPARATOR.'+/', SQ_CONF_WEB_PATH_SEPARATOR, $path);
465 
466  // ignore blanks
467  if ($path !== '' && !in_array($path, $valid_paths)) {
468  $valid_paths[] = $path;
469  }
470 
471  }//end foreach
472 
473  // Fallback: only if there is no valid path at all AND at least one non blank path has been provided
474  if (empty($valid_paths) && strlen(trim(join('', $paths)))) {
475  $valid_paths = make_valid_web_paths(Array($fallback_path));
476  }
477 
478  return $valid_paths;
479 
480 }//end make_valid_web_paths()
481 
482 
503 function implode_sql($query_components)
504 {
505  $where_joiner = trim(strtoupper(array_get_index($query_components, 'where_joiner', '')));
506  if ($where_joiner != 'OR') $where_joiner = 'AND';
507 
508  $select = 'SELECT '.implode(', ', $query_components['select']);
509  $from = 'FROM '.implode(', ', $query_components['from']);
510  if (isset($query_components['join'])) {
511  $from .= ' '.implode("\n", $query_components['join']);
512  }
513  $where = 'WHERE ('.implode(")\n\t".$where_joiner.' (', $query_components['where']).')';
514 
515  $sql = "$select\n$from\n$where";
516 
517  if (!empty($query_components['group_by'])) {
518  $sql .= "\nGROUP BY ".implode(', ', $query_components['group_by']);
519  }
520  if (!empty($query_components['having'])) {
521  $sql .= "\nHAVING ".implode(' ', $query_components['having']);
522  }
523  if (!empty($query_components['order_by'])) {
524  $sql .= "\nORDER BY ".implode(', ', $query_components['order_by']);
525  }
526  return $sql;
527 
528 }//end implode_sql()
529 
530 
542 function addProxyInfo(&$request_parameters, &$default_headers, $page_url)
543 {
544  require_once(SQ_DATA_PATH.'/private/conf/proxy_authentication.inc');
545  if (SQ_PA_ENABLED) {
546  $proxy = get_proxy_info_for_url($page_url);
547 
548  // Make the host/port values null unless a proxy host has been set. Ensures
549  // a proxy is NOT used if the host field is blank
550  $request_parameters['proxy_host'] = ($proxy['host'] === '') ? NULL : $proxy['host'];
551  $request_parameters['proxy_port'] = ($proxy['host'] === '') ? NULL : $proxy['port'];
552  $default_headers['Proxy-Authorization'] = 'Basic '.base64_encode($proxy['user'].':'.$proxy['password']);
553  }
554 
555 }//end addProxyInfo()
556 
557 
572 function openSocketConnection($url_parts, &$errno, &$errstr, $force_fsockopen=FALSE, $timeout=60)
573 {
574  return open_socket_connection($url_parts, $errno, $errstr, $force_fsockopen, $timeout);
575 
576 }//end openSocketConnection()
577 
578 
591 function open_socket_connection($url_parts, &$errno, &$errstr, $force_fsockopen=FALSE, $timeout=60)
592 {
593  include_once(SQ_DATA_PATH.'/private/conf/proxy_authentication.inc');
594  $page_url = '';
595  if (is_array($url_parts)) {
596  if (!empty($url_parts['query']) && $url_parts['query'][0] != '?') {
597  $url_parts['query'] = '?'.$url_parts['query'];
598  }
599  if (!empty($url_parts['path']) && $url_parts['path'][0] != '/') {
600  $url_parts['path'] = '/'.$url_parths['path'];
601  }
602  $page_url = $url_parts['scheme'].'://'.$url_parts['host'].':'.$url_parts['port'].$url_parts['path'].array_get_index($url_parts, 'query', '');
603  } else {
604  $page_url = $url_parts;
605  }
606 
607  if (SQ_PA_ENABLED) {
608  $proxy = get_proxy_info_for_url($page_url);
609 
610  $con = @fsockopen($proxy['host'], $proxy['port'], $errno, $errstr);
611  if (!$con) return FALSE;
612 
613  $matches = array();
614  preg_match('@^(?:http://)?([^/]+)@i', $page_url, $matches);
615  $host = $matches[1];
616 
617  fputs($con, "GET $page_url HTTP/1.0\r\n");
618  fputs($con, "Host: ".$host."\r\n");
619  if( !empty($proxy['user']) && !empty($proxy['password']) ){
620  fputs($con, 'Proxy-Authorization: Basic '.base64_encode($proxy['user'].':'.$proxy['password'])."\r\n");
621  }
622  fputs($con, "\r\n"); // empty row to tell remote headers are complete
623 
624  } else {
625  if ($force_fsockopen && is_array($url_parts)) {
626  $con = @fsockopen($url_parts['host'], $url_parts['port'], $errno, $errstr, $timeout);
627  } else {
628  $con = @fopen($page_url, 'r');
629  }//end if
630  }
631 
632  return $con;
633 
634 }//end open_socket_connection()
635 
636 
645 function getLangCharMap($lang_code='')
646 {
647  $character_map = Array();
648  if (empty($lang_code)) {
649  $lang_code = SQ_CONF_DEFAULT_FRONTEND_LANGUAGE;
650  }
651 
652  include SQ_DATA_PATH.'/private/conf/lang_char_map.inc';
653 
654  if (isset($lang_char_map[$lang_code]) && is_array($lang_char_map[$lang_code])) {
655  $character_map = $lang_char_map[$lang_code];
656  }
657 
658  return $character_map;
659 
660 }//end getLangCharMap()
661 
662 
671 function parse_self_links($content)
672 {
673  // get the current frontend asset url
674  $url = str_replace("/", "\/", $GLOBALS['SQ_SYSTEM']->frontend_asset->getURL());
675  $web_path = substr($url, strrpos($url, "/") + 1);
676 
677  // preg for every occurence of full url or just web path
678  $href_pattern = "/<a [^<]*href=\"(".$url.")\/?\"[^>]*>(.*?)<\/a>/msi";
679  $web_path_pattern = "/<a [^<]*href=\"(\.{0,2}\/{1})?".$web_path."\/?\"[^>]*>(.*?)<\/a>/msi";
680  $content = preg_replace(Array($href_pattern, $web_path_pattern), '$2', $content);
681 
682  return $content;
683 
684 }
685 
686 
695 function sizeInBytes($size)
696 {
697  $size = strtolower(str_replace(' ', '', $size));
698  preg_match('/^([0-9]+)([a-z]+)$/', $size, $matches);
699 
700  $value = (int) array_get_index($matches, 1, $size);
701  $unit_type = array_get_index($matches, 2, '');
702 
703  switch($unit_type) {
704  case 'g':
705  case 'gb':
706  $value = $value * 1024 * 1024 * 1024;
707  break;
708 
709  case 'm':
710  case 'mb':
711  $value = $value * 1024 * 1024;
712  break;
713 
714  case 'k':
715  case 'kb':
716  $value = $value * 1024;
717  break;
718  }
719 
720  return $value;
721 
722 }//end sizeInBytes()
723 
724 ?>