43 function sq_error_handler($err_no, $err_msg, $err_file, $err_line)
49 $current_level = error_reporting();
52 if ($current_level === 0)
return;
55 if (($err_no & $current_level) == 0)
return;
57 static $num_errors = 0;
60 $err_msg = strip_tags($err_msg);
62 $terminate = ((E_USER_ERROR | E_ERROR) & $err_no);
65 if (defined(
'E_RECOVERABLE_ERROR') && $err_no == E_RECOVERABLE_ERROR) {
70 $bt = debug_backtrace();
75 while (($real_bt_index < count($bt)-1) && (((FALSE !== strpos(array_get_index($bt[$real_bt_index],
'class'),
'Locale_Manager')) || (FALSE !== strpos(array_get_index($bt[$real_bt_index],
'file'),
'locale_manager'))) || ($real_bt_index === 0 && array_get_index($bt[$real_bt_index],
'function',
'') == __FUNCTION__) && array_get_index($bt[$real_bt_index],
'function',
'') == __FILE__)) {
79 $err_file = (($err_no === E_STRICT) || ($real_bt_index == 0 && !isset($bt[$real_bt_index][
'file']))) ? hide_system_root($bt[
'0'][
'args'][
'2']) : hide_system_root($bt[$real_bt_index][
'file']);
80 $err_line = (($err_no === E_STRICT) || ($real_bt_index == 0 && !isset($bt[$real_bt_index][
'file']))) ? hide_system_root($bt[
'0'][
'args'][
'3']) : hide_system_root($bt[$real_bt_index][
'line']);
81 $err_msg = hide_system_root($err_msg);
86 if ($terminate || (($err_no & ~E_USER_NOTICE) && (SQ_CONF_DEBUG & 2))) {
87 require_once SQ_FUDGE_PATH.
'/dev/dev.inc';
88 $bt_str = hide_system_root(array_contents($bt, 3));
91 $err_name = get_error_name($err_no);
92 $bg_colour = get_error_colour($err_no);
95 if (ini_get(
'log_errors')) {
96 $text_msg = strip_tags(preg_replace(Array(
'/<br\\/?>/i',
'/<p[^>]*>/i'), Array(
"\n",
"\n\n"), $err_msg));
97 log_error($text_msg, $err_no, $err_file, $err_line);
103 while (ob_get_level() > SQ_INIT_OB_LEVEL) {
104 $buffers[] = ob_get_contents();
108 if ((
boolean)SQ_CONF_ERRORS_LOG_TO_SYSLOG === TRUE) {
109 log_to_syslog($err_no, $err_msg, $err_file, $err_line);
112 $type = ($GLOBALS[
'SQ_OUTPUT_TYPE'] ==
'html' && SQ_PHP_CLI) ?
'text' : $GLOBALS[
'SQ_OUTPUT_TYPE'];
115 switch ($GLOBALS[
'SQ_OUTPUT_TYPE']) {
117 $msg =
"$err_msg\nFile: $err_file\nLine:$err_line";
119 $root = simplexml_load_string(
'<xml></xml>');
120 $output = $root->addChild(
'error', $msg);
121 header(
'Content-Type: text/xml');
122 echo $output->asXML();
133 $err_msg = strip_tags(preg_replace(Array(
'/<br\\/?>/i',
'/<p[^>]*>/i'), Array(
"\n",
"\n\n"), $err_msg));
134 $lines = explode(
"\n", $err_msg);
135 $len = 7 + strlen($err_file);
136 $len = max($len, 7 + strlen($err_line));
137 foreach ($lines as $line) {
138 $len = max($len, strlen($line));
141 $str =
'+'.str_repeat(
'-', $len).
"+\n".
142 '| '.$err_name.str_repeat(
' ', $len - 2 - strlen($err_name)).
" |\n".
143 '|'.str_repeat(
'-', $len).
"|\n";
144 if (SQ_CONF_DEBUG & 1) {
145 $str .=
'| File : '.$err_file.str_repeat(
' ', $len - 9 - strlen($err_file)).
" |\n".
146 '| Line : '.$err_line.str_repeat(
' ', $len - 9 - strlen($err_line)).
" |\n".
147 '|'.str_repeat(
'-', $len).
"|\n";
149 foreach ($lines as $line) {
150 $str .=
'| '.$line.str_repeat(
' ', $len - 2 - strlen($line)).
" |\n";
153 $str .=
'+'.str_repeat(
'-', $len).
"+\n";
157 fwrite(STDERR, $str);
171 if (hide_frontend_errors() && !(SQ_IN_BACKEND || SQ_IN_LIMBO)) {
176 if ($err_msg == strip_tags($err_msg)) {
177 $err_msg = nl2br(htmlspecialchars($err_msg));
180 $error_title =
'background-color: '.$bg_colour.
'; font-size: 14px; font-weight: bold; color: #ffffff; font-family: verdana, arial, sans-serif; vertical-align: top;';
181 $error_data =
'background-color: '.$bg_colour.
'; font-size: 12px; color: #ffffff; font-family: verdana, arial, sans-serif; vertical-align: top;';
182 if(empty($buffers)) echo
'</script></table></table></table></table><br/>';
183 if(!(SQ_IN_BACKEND || SQ_IN_LIMBO)) echo
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
185 <table bgcolor=
"#c0c0c0" cellspacing=
"0" border=
"1" cellpadding=
"2" bordercolor=
"#ff0000">
187 <td style=
"<?php echo $error_title; ?>">
188 <?php echo $err_name; ?>
192 <td style=
"<?php echo $error_data; ?>">
194 if (SQ_CONF_DEBUG & (1 | 2)) {
196 <table bgcolor=
"#dddddd" cellpadding=
"2" cellspacing=
"0" border=
"0">
198 if (SQ_CONF_DEBUG & 1) {
201 <td style=
"<?php echo $error_title; ?>" align=
"right">
File:</td>
202 <td style=
"<?php echo $error_data; ?>"><?php echo $err_file; ?></td>
203 <td style=
"<?php echo $error_title; ?>" align=
"right">Line:</td>
204 <td style=
"<?php echo $error_data; ?>"><?php echo $err_line; ?></td>
210 <td style=
"<?php echo $error_title; ?>" align=
"right">Message:</td>
211 <td style=
"<?php echo $error_data; ?>" colspan=
"3"><?php echo $err_msg; ?></td>
215 if ((SQ_CONF_DEBUG & 2) && $bt_str) {
218 <td style=
"<?php echo $error_title; ?>" align=
"right">Backtrace:</td>
219 <td style=
"<?php echo $error_data; ?>" colspan=
"3">
220 <script language=
"JavaScript" type=
"text/javascript">
222 function print_backtrace_<?php echo $num_errors; ?>() {
223 var alert_win = window.open(
'',
'backtrace_window_<?php echo $num_errors; ?>',
'resizable=yes,scrollbars=yes,width=800,height=500');
224 alert_win.document.open();
225 alert_win.document.writeln(
"<html><head><title>MySource Backtrace</title></head><body><pre>");
226 alert_win.document.writeln(
"<?php echo str_replace("\n
", '\\n', addslashes(htmlspecialchars($bt_str))); ?>");
227 alert_win.document.writeln(
"</pre></body></html>");
228 alert_win.document.close();
234 <a href=
"javascript: print_backtrace_<?php echo $num_errors; ?>();" style=
"color: #ffffff;">Show</a>
255 for ($i = count($buffers) - 1; $i >= 0; $i--) {
264 $err_msg = strip_tags(preg_replace(Array(
'/<br\\/?>/i',
'/<p[^>]*>/i'), Array(
"\n",
"\n\n"), $err_msg));
269 $matrix_version =
'N/A';
270 if (defined(
'SQ_SYSTEM_VERSION')) {
271 $matrix_version = SQ_SYSTEM_VERSION;
274 $str =
'*'.$err_name.
"*\n".
275 'File : '.$err_file.
"\n".
276 'Line : '.$err_line.
"\n".
277 'Version : '.$matrix_version.
"\n".
278 'DB Type : '.$db_type.
"\n".
281 "------------------------------------------------------------------\n".
283 if (!empty($GLOBALS[
'SQ_SYSTEM']) && is_object($GLOBALS[
'SQ_SYSTEM']->user)) {
284 $str .=
'User: "'.$GLOBALS[
'SQ_SYSTEM']->user->name.
'" (#'.$GLOBALS[
'SQ_SYSTEM']->user->id.
")\n";
286 $str .= (!empty($_SERVER[
'REMOTE_ADDR']) ?
'IP Address: '.$_SERVER[
'REMOTE_ADDR'].
"\n" :
'');
287 $str .= (!empty($_SERVER[
'HTTP_USER_AGENT']) ?
'User-Agent: '.$_SERVER[
'HTTP_USER_AGENT'].
"\n" :
'');
289 $str .=
"------------------------------------------------------------------\n".
291 SQ_CONF_SYSTEM_ROOT_URLS.
"\n".
292 "------------------------------------------------------------------\n";
294 $str .=
"*Command and Arguments*\n".
295 implode(
' ', $_SERVER[
'argv']).
"\n";
297 $str .=
"*Current URL*\n".
298 current_protocol().
'://'.array_get_index($_SERVER,
'HTTP_HOST',
'NO HOST').array_get_index($_SERVER,
'REQUEST_URI',
'/NO REQUEST URI').
"\n";
302 $str .=
"------------------------------------------------------------------\n".
308 if (SQ_CONF_TECH_EMAIL) {
309 mail(SQ_CONF_TECH_EMAIL,
'['.$err_name.
'] '.SQ_CONF_SYSTEM_NAME, $str,
'From: '.SQ_CONF_TECH_EMAIL.
"\r\n");
327 function sq_exception_handler($e)
329 $nl = SQ_PHP_CLI ?
"\n" :
"<br />";
332 $msg = SQ_PHP_CLI ? $e->getMessage() : htmlspecialchars($e->getMessage(), ENT_QUOTES);
333 $fileline =
"' in ".$e->getFile().
"(".$e->getLine().
"): ";
334 $trace = $e->getTrace();
337 foreach($trace as $key => $val) {
338 $file = $val[
'file'];
339 $func_name = $val[
'function'];
340 $line = $val[
'line'];
342 foreach($val[
'args'] as $arg) {
343 if (is_array($arg)) {
344 $args_str .=
"Array, ";
345 }
else if (is_object($arg)) {
346 $args_str .=
"Object(".get_class($arg).
"), ";
347 }
else if (is_bool($arg)) {
348 $args_str .= ($arg === TRUE ?
"true" :
"false").
", ";
349 }
else if ($arg === null) {
350 $args_str .=
"null, ";
353 if (strlen($arg > 20)) {
354 $arg = substr($arg,0, 20).
"...";
356 $arg = str_replace(
"'",
"\'", $arg);
359 $arg = htmlspecialchars($arg, ENT_QUOTES);
361 $args_str .=
"'".$arg.
"', ";
364 $args_str = rtrim($args_str,
', ' );
367 $backtrace .= $func_name.
"(".$args_str.
")".$nl;
369 $backtrace .=
"#".$key.
" ".$file.
"(".$line.
"): ".$func_name.
"(".$args_str.
")".$nl;
373 $err_msg =
"Fatal error: Uncaught exception '".get_class($e).
"' with message: '".$msg;
375 if (SQ_PHP_CLI || SQ_IN_BACKEND || SQ_IN_LIMBO || !hide_frontend_errors()) {
376 $display_msg = $err_msg;
377 if (SQ_CONF_DEBUG & 1) {
378 $display_msg .= $fileline;
380 if (SQ_CONF_DEBUG & 2) {
381 $display_msg .= $backtrace;
387 if (ini_get(
'log_errors')) {
388 $log_msg = str_replace(
'<br />',
"\n", htmlspecialchars_decode($err_msg.$fileline.$backtrace));
389 log_error($log_msg, E_ERROR);
403 function hide_frontend_errors()
405 $query = array_get_index($_SERVER,
'QUERY_STRING',
'');
408 if (SQ_CONF_ERRORS_HIDE_FRONTEND) {
410 if (isset($GLOBALS[
'SQ_SYSTEM']->user)) {
411 $user = $GLOBALS[
'SQ_SYSTEM']->user;
417 if ((preg_match(
'/SQ_SHOW_ERRORS=1/', $query) != 0) && (!is_null($user) && $user instanceof
System_User)) {
433 function log_to_syslog($error_code, $message, $file, $line)
435 $syslog_code = LOG_NOTICE;
437 switch ($error_code) {
440 $syslog_code = LOG_ERR;
445 $syslog_code = LOG_WARNING;
449 $syslog_code = LOG_NOTICE;
454 if ((SQ_CONF_DEBUG & 1) !== 0) {
455 $message .=
' ('.$file.
':'.$line.
')';
458 syslog($syslog_code, $message);
471 function get_error_name($error_code)
475 if (empty($error_code))
return $name;
478 if (defined(
'E_RECOVERABLE_ERROR') && $error_code == E_RECOVERABLE_ERROR) {
483 }
else if (defined(
'E_STRICT') && $error_code == E_STRICT) {
485 $name =
'PHP 5 Strict Standards';
487 switch ($error_code) {
489 $name = SQ_SYSTEM_SHORT_NAME.
' Error';
491 case E_USER_WARNING :
492 $name = SQ_SYSTEM_SHORT_NAME.
' Warning';
495 $name = SQ_SYSTEM_SHORT_NAME.
' Notice';
501 $name =
'PHP Warning';
504 $name =
'PHP Notice';
507 $name =
'Unknown Error Type - '.$error_code;
524 function get_error_colour($error_code)
528 if (defined(
'E_RECOVERABLE_ERROR') && $error_code == E_RECOVERABLE_ERROR) {
532 }
else if (defined(
'E_STRICT') && $error_code == E_STRICT) {
536 switch ($error_code) {
540 case E_USER_WARNING :
574 function sq_web_path($path=
'base')
576 static $paths = Array();
578 if (isset($paths[$path]))
return $paths[$path];
583 $paths[$path] = (SQ_IN_BACKEND) ?
'..' : ((preg_match(
'/index.php$/', current_url())) ?
'./index.php' :
'.');
588 $paths[$path] = (SQ_IN_BACKEND) ?
'.' : ((preg_match(
'/index.php$/', current_url())) ?
'./index.php' :
'.').
'/'.SQ_CONF_BACKEND_SUFFIX;
593 $paths[$path] = (SQ_IN_LIMBO) ?
'.' : ((preg_match(
'/index.php$/', current_url())) ?
'./index.php' :
'.').
'/'.SQ_CONF_LIMBO_SUFFIX;
598 $paths[$path] = (SQ_IN_LOGIN) ?
'.' : ((preg_match(
'/index.php$/', current_url())) ?
'./index.php' :
'.').
'/'.SQ_CONF_LOGIN_SUFFIX;
602 $root_urls = explode(
"\n", SQ_CONF_SYSTEM_ROOT_URLS);
603 $current_url = strip_url(current_url(FALSE, TRUE));
606 $current_url = preg_replace(
'/\\/'.SQ_CONF_BACKEND_SUFFIX.
'$/',
'', $current_url);
608 $tmp = $root_urls[0];
609 for ($i = 1; $i < count($root_urls); $i++) {
610 if (substr($current_url, 0, strlen($root_urls[$i])) == $root_urls[$i]) {
612 if (substr($tmp, 0, strlen($root_urls[$i])) == $root_urls[$i]) {
614 if (strlen($tmp) < strlen($root_urls[$i])) {
615 $tmp = $root_urls[$i];
618 $tmp = $root_urls[$i];
623 $paths[$path] = current_protocol().
'://'.preg_replace(
'/\/index.php$/',
'', $tmp);
627 $paths[$path] = sq_web_path(
'root_url').
'/__lib';
631 if (SQ_CONF_STATIC_ROOT_URL ==
'') {
632 $paths[$path] = sq_web_path(
'root_url').
'/__data';
635 switch (current_protocol()) {
637 $protocol = (SQ_CONF_STATIC_ROOT_HTTP) ?
'http' :
'https';
640 $protocol = (SQ_CONF_STATIC_ROOT_HTTPS) ?
'https' :
'http';
643 $paths[$path] = $protocol.
'://'.SQ_CONF_STATIC_ROOT_URL;
648 $paths[$path] = sq_web_path(
'root_url').
'/__fudge';
653 return $paths[$path];
667 function sq_root_url($url, $inc_protocol=TRUE)
670 $protocol = ($inc_protocol) ? array_get_index(parse_url($url),
'scheme',
'http').
'://' :
'';
672 $current_url = strip_url($url, TRUE);
673 $root_urls = explode(
"\n", SQ_CONF_SYSTEM_ROOT_URLS);
677 $current_url = preg_replace(
'/\\/'.SQ_CONF_BACKEND_SUFFIX.
'$/',
'', $current_url);
679 $tmp = $root_urls[0];
680 for ($i = 1; $i < count($root_urls); $i++) {
681 if (substr($current_url, 0, strlen($root_urls[$i])) == $root_urls[$i]) {
683 if (substr($tmp, 0, strlen($root_urls[$i])) == $root_urls[$i]) {
685 if (strlen($tmp) < strlen($root_urls[$i])) {
686 $tmp = $root_urls[$i];
689 $tmp = $root_urls[$i];
694 return strip_url($protocol.preg_replace(
'/\/index.php$/',
'', $tmp));
707 function get_asset_hash($assetid)
709 $assetid = trim($assetid);
712 $len = strlen($assetid);
713 for ($i = 0; $i < $len; $i++) {
714 if ((
int) $assetid{$i} != $assetid{$i}) {
715 $hash += ord($assetid{$i});
717 $hash += (int) $assetid{$i};
720 $assetid = (string) $hash;
721 }
while ($hash > SQ_CONF_NUM_DATA_DIRS);
723 while (strlen($hash) != 4) {
740 function asset_data_path_suffix($type_code, $assetid)
742 return 'assets/'.$type_code.
'/'.get_asset_hash($assetid).
'/'.$assetid;
756 function current_url($inc_protocol=TRUE, $strip_backend_suffix=FALSE)
758 if (SQ_PHP_CLI && !isset($_SERVER[
'HTTP_HOST'])) {
762 $host = array_get_index($_SERVER,
'HTTP_HOST', FALSE);
766 $host = array_get_index($_SERVER,
'SERVER_NAME', FALSE);
768 trigger_localised_error(
'SYS0107', E_USER_ERROR);
771 $url = (($inc_protocol) ? current_protocol().
'://' :
'').$host.$_SERVER[
'PHP_SELF'];
774 if ($strip_backend_suffix && (SQ_IN_BACKEND || SQ_IN_LIMBO || SQ_IN_LOGIN)) {
775 $url = preg_replace(
'/'.SQ_CONF_BACKEND_SUFFIX.
'\/?$/',
'', $url);
776 $url = preg_replace(
'/'.SQ_CONF_LIMBO_SUFFIX.
'\/?$/',
'', $url);
777 $url = preg_replace(
'/'.SQ_CONF_LOGIN_SUFFIX.
'\/?$/',
'', $url);
779 $url = preg_replace(
'/'.SQ_CONF_NOCACHE_SUFFIX.
'\/?$/',
'', $url);
780 $url = preg_replace(
'/\/'.SQ_CONF_RECACHE_SUFFIX.
'\/?$/',
'', $url);
781 $url = preg_replace(
'/\/'.SQ_CONF_PERFORMANCE_SUFFIX.
'\/?$/',
'', $url);
782 $url = preg_replace(
'/\/'.SQ_CONF_PERFORMANCE_TIMING_SUFFIX.
'\/?$/',
'', $url);
783 $url = preg_replace(
'/\/'.SQ_CONF_PERFORMANCE_RESULT_SUFFIX.
'\/?$/',
'', $url);
787 if (!defined(
'SQ_CONF_SYSTEM_ROOT_URLS')) {
788 include_once SQ_DATA_PATH.
'/private/conf/main.inc';
790 $root_urls = explode(
"\n", SQ_CONF_SYSTEM_ROOT_URLS);
792 $url = (($inc_protocol) ? current_protocol().
'://' :
'').trim($root_urls[0]).
'/';
808 function clean_url($url)
814 SQ_CONF_LOGIN_SUFFIX,
815 SQ_CONF_LIMBO_SUFFIX,
816 SQ_CONF_BACKEND_SUFFIX,
817 SQ_CONF_NOCACHE_SUFFIX,
818 SQ_CONF_RECACHE_SUFFIX,
819 SQ_CONF_PERFORMANCE_SUFFIX,
822 foreach ($bits as $bit) {
823 $url = preg_replace(
'/\/'.$bit.
'.*/',
'/', $url);
836 function current_protocol()
838 if (is_ssl_accelerated())
return 'https';
839 return (!empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] ==
'on') ?
'https' :
'http';
851 function is_ssl_accelerated()
853 if (SQ_PHP_CLI)
return FALSE;
856 if (SQ_SSLA_ENABLED) {
859 $match_method = SQ_SSLA_MATCH;
860 if ($match_method !==
'header') {
861 $host = array_get_index($_SERVER,
'REMOTE_ADDR');
863 if ($host === NULL) $enabled = FALSE;
864 if (trim($host) !== trim(SQ_SSLA_IP)) {
869 if ($match_method !==
'ip') {
870 if (isset($_SERVER[SQ_SSLA_HEADER_NAME])) {
871 $header_value = $_SERVER[SQ_SSLA_HEADER_NAME];
872 if (trim($header_value) !== trim(SQ_SSLA_HEADER_VALUE)) {
896 function strip_url($url, $strip_protocol=FALSE)
898 preg_match(
'|^([a-z]+://)?(.*)$|', $url, $matches);
899 return (($strip_protocol) ?
'' : $matches[1]).preg_replace(Array(
'|/+$|',
'|//+|'), Array(
'',
'/'), $matches[2]);
914 function get_url_lineage($url, $db_quote = TRUE)
917 $pos = strpos($url,
"/");
922 $urls[] = substr($url, 0, $pos);
924 $pos = strpos($url,
"/", $pos + 1);
948 function replace_query_string_vars($replacements, $base_url=NULL, $query_string=NULL, $remove_sq_vars=FALSE)
950 if (is_null($base_url)) $base_url = current_url();
951 if (is_null($query_string)) {
952 $query_string = array_get_index($_SERVER,
'QUERY_STRING',
'');
955 $replaced_vars = array_keys($replacements);
956 $new_query_string =
'';
960 preg_match_all(
'|&([^=]+)=([^&]+)?|',
'&'.$query_string, $matches);
961 foreach ($matches[1] as $num => $var) {
962 if ($remove_sq_vars && (strlen($var) > 3) && (substr(strtoupper($var), 0, 3) ==
'SQ_'))
continue;
964 if (!in_array($var, $replaced_vars)) {
966 $new_query_string .=
'&'.$var.
'='.$matches[2][$num];
968 $new_value = $replacements[$var];
969 if (!is_null($new_value)) {
970 $new_query_string .=
'&'.$var.
'='.$new_value;
976 foreach ($replacements as $var => $value) {
977 if (in_array($var, $matches[1]))
continue;
978 if (is_null($value))
continue;
979 $new_query_string .=
'&'.$var.
'='.$value;
981 $new_query_string = trim($new_query_string,
'&');
983 return $base_url.
'?'.$new_query_string;
999 function bit_elements($num)
1032 function iso8601_ts($iso8601)
1038 if ($iso8601 ==
'---------- --:--:--') {
1040 }
else if (preg_match(
"/^[0-9\-]{4}-[0-9\-]{2}-[0-9\-]{2}$/", $iso8601)) {
1044 (
int) substr($iso8601,5,2),
1045 (
int) substr($iso8601,8,2),
1046 (
int) substr($iso8601,0,4)
1049 }
else if (preg_match(
'/^[0-9\-]{4}(-[0-9\-]{2}(-[0-9\-]{2}([T| ]{1}[0-9\-]{2}:[0-9\-]{2}(:[0-9\-]{2}(\.[0-9\-]{1,2}){0,1}){0,1}(([\+|-]{1}[0-9\-]{2}:[0-9\-]{2})|Z){0,1}){0,1}){0,1}){0,1}$/', $iso8601)) {
1052 (
int) substr($iso8601,11,2),
1053 (
int) substr($iso8601,14,2),
1054 (
int) substr($iso8601,17,2),
1055 (
int) substr($iso8601,5,2),
1056 (
int) substr($iso8601,8,2),
1057 (
int) substr($iso8601,0,4)
1077 function ts_iso8601($timestamp)
1079 return date(
'Y-m-d H:i:s', $timestamp);
1093 function format_date($timestamp, $format_name)
1095 if ($format_name ===
'relative') {
1096 require_once SQ_FUDGE_PATH.
'/general/datetime.inc';
1097 return easy_short_relative_datetime($timestamp, FALSE);
1098 }
else if ($format_name ===
'relative-dow') {
1099 require_once SQ_FUDGE_PATH.
'/general/datetime.inc';
1100 return easy_short_relative_datetime($timestamp, TRUE);
1102 $date_formats = get_date_formats();
1103 if (isset($date_formats[$format_name])) {
1104 return date($date_formats[$format_name], $timestamp);
1122 function get_date_formats()
1124 $date_formats = Array(
1126 'readable' =>
'd M Y g:ia',
1127 'readabledate' =>
'd M Y',
1128 'readabletime' =>
'g:ia',
1129 'iso8601' =>
'Y-m-d\TH:i:s',
1131 'rfc2822-dateonly' =>
'D, d M Y',
1132 'ical' =>
'Ymd\THis',
1134 'relative-dow' => NULL,
1136 return $date_formats;
1147 function get_date_format_names()
1149 $date_formats = get_date_formats();
1150 foreach ($date_formats as $date_id => $date_format) {
1151 $date_formats[$date_id] = translate(
'date_format_'.$date_id);
1154 return $date_formats;
1172 function array_get_index($array, $index, $default=NULL)
1174 if (!is_array($array))
return $default;
1175 if (isset($array[$index]))
return $array[$index];
1195 function register_implementation($class, $interface)
1197 if (!isset($GLOBALS[
'SQ_IMPLEMENTATION'])) {
1198 $GLOBALS[
'SQ_IMPLEMENTATION'] = Array();
1200 $GLOBALS[
'SQ_IMPLEMENTATION'][$class] = (is_array($interface)) ? $interface : Array($interface);
1214 function implements_interface($class, $interface)
1216 if (is_object($class)) {
1217 return ($class instanceof $interface);
1218 }
else if (!class_exists($class)) {
1219 trigger_localised_error(
'SYS0237', E_USER_ERROR, $class);
1223 $dummy =
new $class();
1224 $ret_val = ($dummy instanceof $interface);
1245 function log_write($data, $logname=SQ_CONF_LOG_FILE_SYSTEM, $level=E_USER_NOTICE, $encode=TRUE)
1247 if (empty($logname) || empty($level)) {
1248 trigger_localised_error(
'SYS0099', E_USER_ERROR);
1253 $current_user = $GLOBALS[
'SQ_SYSTEM']->user;
1257 if (is_null($current_user)) {
1258 $username = SQ_SYSTEM_SHORT_NAME.
' System';
1260 $userid = $current_user->id;
1261 $username = $current_user->name;
1264 $chars_to_escape = Array(
'[',
']',
':',
"\n",
"\r");
1265 $replacements = Array(
'[',
']',
':',
' ',
' ');
1267 $username = str_replace($chars_to_escape, $replacements, $username);
1268 $level_name = strtolower(get_error_name($level));
1271 if (is_string($data)) {
1274 $data = serialize($data);
1279 $data = str_replace(Array(
"\n",
"\r"), Array(
' ',
' '), $data);
1286 $date = date(
'Y-m-d H:i:s');
1287 $log_entry =
"[$date][$userid:$username][$level:$level_name][$flag] $data\n";
1289 if (!$log_entry)
return FALSE;
1291 $logfile = SQ_LOG_PATH.
'/'.$logname.SQ_CONF_LOG_EXTENSION;
1296 if (substr(PHP_OS, 0, 3) !=
'WIN') {
1297 $old_umask = umask(0113);
1300 $handle = fopen($logfile,
'a');
1301 if (!fwrite($handle, $log_entry)) {
1302 trigger_localised_error(
'SYS0051', E_USER_WARNING, $logfile);
1308 if (isset($old_umask)) umask($old_umask);
1329 function log_error($message, $level=E_USER_NOTICE, $file=
'', $line=
'', $log_name=SQ_CONF_LOG_FILE_ERROR)
1331 if (!is_string($message)) {
1332 trigger_localised_error(
'SYS0191', E_USER_WARNING);
1336 if (!is_null($file)) {
1337 $message =
"($file:$line) - $message";
1341 $message = str_replace(
'[SYSTEM_ROOT]',
'', $message);
1343 return log_write($message, $log_name, $level, FALSE);
1356 function hide_system_root($message=
'')
1359 if (SQ_SYSTEM_ROOT !==
'.') {
1360 $message = str_replace(Array(
"\r", SQ_SYSTEM_ROOT), Array(
'',
'[SYSTEM_ROOT]'), $message);
1377 function translate($string)
1379 $args = array_slice(func_get_args(),1);
1380 if (!isset($GLOBALS[
'SQ_SYSTEM']))
return FALSE;
1381 return $GLOBALS[
'SQ_SYSTEM']->lm->getString($string, $args);
1395 function translate_error($code)
1397 $args = array_slice(func_get_args(),1);
1398 if (!isset($GLOBALS[
'SQ_SYSTEM']))
return FALSE;
1399 return $GLOBALS[
'SQ_SYSTEM']->lm->getErrorMessage($code, $args);
1414 function trigger_localised_error($code, $error_level)
1416 $args = array_slice(func_get_args(),2);
1417 if (!isset($GLOBALS[
'SQ_SYSTEM']) || !is_object($GLOBALS[
'SQ_SYSTEM']->lm)) {
1418 trigger_error(
'Error occurred ['.$code.
']'.
"\n".print_r($args, TRUE), $error_level);
1421 return $GLOBALS[
'SQ_SYSTEM']->lm->raiseError($code, $error_level, $args);
1442 function sq_print_icon($path, $width, $height, $alt=
'', $title=NULL, $extras=
'')
1444 echo sq_get_icon($path, $width, $height, $alt, $title, $extras);
1465 function sq_get_icon($path, $width, $height, $alt=
'', $title=NULL, $extras=
'')
1467 if (is_null($title) && !empty($alt)) {
1470 if (is_null($title)) $title =
'';
1471 if (FALSE !== strpos($extras,
'class=')) {
1472 $extras = preg_replace(
'/class="([^"]*)"/',
'class="\\1 sq-icon"', $extras);
1474 $extras .=
' class="sq-icon"';
1477 <img src="'.$path.
'"
1480 height="'.$height.
'"
1501 function get_asset_tag_line($assetid, $screen=
'')
1505 if (strpos($assetid,
':') !== FALSE) {
1506 list($real_assetid, $shadowid) = explode(
':', $assetid, 2);
1507 $asset_info = Array();
1508 $shadow_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
1509 if ($shadow_asset) {
1510 $asset_info[
'type_code'] = $shadow_asset->type();
1511 $asset_info[
'name'] = $shadow_asset->name;
1512 $asset_info[
'short_name'] = $shadow_asset->short_name;
1513 $asset_info[
'status'] = $shadow_asset->status;
1514 $asset_info[
'created'] = $shadow_asset->created;
1515 $asset_info[
'created_userid'] = $shadow_asset->created_userid;
1516 $asset_info[
'updated'] = $shadow_asset->updated;
1517 $asset_info[
'updated_userid'] = $shadow_asset->updated_userid;
1518 $asset_info[
'published'] = $shadow_asset->published;
1519 $asset_info[
'published_userid'] = $shadow_asset->published_userid;
1520 $asset_info[
'status_changed'] = $shadow_asset->status_changed;
1521 $asset_info[
'status_changed_userid'] = $shadow_asset->status_changed_userid;
1524 $asset_info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo(Array($assetid));
1525 if (isset($asset_info[$assetid])) {
1526 $asset_info = $asset_info[$assetid];
1528 $real_assetid = $assetid;
1531 if (empty($asset_info)) {
1533 echo
'<span class="sq-backend-warning">Unknown asset (Id: #'.$assetid.
')</span>';
1534 $contents = ob_get_clean();
1538 $asset_info[
'name'] = htmlspecialchars($asset_info[
'name'], ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
1540 if (empty($screen)) {
1543 $screen_url = $GLOBALS[
'SQ_SYSTEM']->am->getAssetBackendHref(Array($assetid => $screen));
1544 $screen_url = $screen_url[$assetid];
1547 $type_name = $GLOBALS[
'SQ_SYSTEM']->am->getTypeInfo($asset_info[
'type_code'],
'name');
1551 <div
id=
"asset-tag-line-<?php echo $assetid ?>-more-details" style=
"display:none;">
1552 <table cellspacing=
"0">
1554 <td
class=
"sq-backend-table-cell" style=
"background-color: #<?php echo get_status_colour($asset_info['status']) ?>"><strong>Current Status:</strong></td>
1555 <td colspan=
"2" class=
"sq-backend-table-cell" style=
"background-color: #<?php echo get_status_colour($asset_info['status']) ?>"><i><?php echo get_status_description($asset_info[
'status']) ?></i></td>
1558 <td
class=
"sq-backend-table-cell"><strong><?php echo translate(
'asset_type') ?>:</strong></td>
1559 <td colspan=
"2" class=
"sq-backend-table-cell"><?php echo get_asset_type_icon($asset_info[
'type_code']).
' '.$type_name ?></td>
1563 'created' =>
'Asset Created',
1564 'updated' =>
'Last Updated',
1565 'published' =>
'Last Published',
1566 'status_changed' =>
'Status Last Changed',
1569 foreach ($headers as $time_type => $header) {
1571 if (!empty($asset_info[$time_type])) {
1572 $time_str = date(
'jS M Y g:ia', strtotime($asset_info[$time_type]));
1574 if ($asset_info[$time_type.
'_userid'] == 0) {
1575 $user_name =
'System';
1577 $user_name = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo(Array($asset_info[$time_type.
'_userid']), Array(
'asset'), FALSE,
'name');
1578 if (empty($user_name) || !isset($user_name[$asset_info[$time_type.
'_userid']]) || empty($user_name[$asset_info[$time_type.
'_userid']])) {
1579 $user_name =
'<span style="color: red">'.translate(
'unknown_or_deleted_user').
'</span>';
1581 $user_name = htmlspecialchars($user_name[$asset_info[$time_type.
'_userid']], ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
1585 $user_name =
'by '.$user_name;
1587 $time_str = translate(
'never');
1588 $user_name =
' ';
1591 <td
class=
"sq-backend-table-cell" ><strong><?php echo $header ?>:</strong></td>
1592 <td
class=
"sq-backend-table-cell" ><?php echo $time_str ?></td>
1593 <td
class=
"sq-backend-table-cell" ><?php echo $user_name ?></td>
1601 $lineages = $GLOBALS[
'SQ_SYSTEM']->am->getLinkLineages($assetid, 5, NULL,
'name', TRUE);
1606 <div
id=
"asset-tag-line-<?php echo $assetid ?>-lineages" style=
"display:none">
1608 foreach ($lineages as $lineage_key => $lineage) {
1609 if ($lineage[
'link_type'] & SQ_SC_LINK_BACKEND_NAV) {
1610 $lineage_assetids = array_keys($lineage[
'lineage']);
1611 $lineage_assetids[] = str_replace(
'\\',
'\\\\', $assetid);
1613 $lineage_names = $lineage[
'lineage'];
1617 if (count($lineage[
'lineage']) > 3) {
1618 $lineage_names = $lineage[
'lineage'];
1619 array_splice($lineage_names, 2, -1, Array(
'...'));
1622 foreach ($lineage_names as $id => $name){
1623 $lineage_names[$id] = htmlspecialchars($name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
1626 $lineage_names[$assetid] = $asset_info[
'name'];
1627 $lineages_onclick = _asset_locator_onclick($lineage_assetids, TRUE);
1629 <td
class=
"sq-backend-table-cell" ><a href=
"#" onclick=
"<?php echo $lineages_onclick; ?>"><?php echo implode(
' > ', $lineage_names) ?></a></td>
1632 unset($lineages[$lineage_key]);
1638 $lineages_div = ob_get_clean();
1642 if (count($lineages) > 1) {
1644 ?><div
id=
"asset-tag-line-<?php echo $assetid ?>-cancel-icon" style=
"display:none"><?php
1645 sq_print_icon(sq_web_path(
'data').
'/asset_types/bodycopy/images/icons/delete.png', 16, 16, translate(
'cancel'), translate(
'cancel'),
' style="border:none"');
1647 $lineages_onclick =
'tooltip.show(this, document.getElementById(\'asset-tag-line-'.str_replace(
'\\',
'\\\\', $assetid).
'-lineages\').innerHTML, \''.translate(
'lineages').
'\', document.getElementById(\
'asset-tag-line-'.str_replace(
'\\',
'\\\\', $assetid).
'-cancel-icon\').innerHTML)';
1648 $lineages_name = translate(
'show_lineages');
1649 }
else if (count($lineages) == 1) {
1651 $lineage_names = $lineages[0][
'lineage'];
1652 foreach ($lineage_names as $id => $name){
1653 $lineage_names[$id] = htmlspecialchars($name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
1655 $lineage_names[$assetid] = $asset_info[
'name'];
1657 $lineages_onclick = _asset_locator_onclick($lineage_assetids, FALSE);
1658 $lineages_name = translate(
'show_in_asset_map');
1661 echo sq_print_icon(sq_web_path(
'lib').
'/web/images/icons/info.png', 16, 16,
'',
'',
' align="absmiddle" onmouseover="tooltip.show(this, document.getElementById(\'asset-tag-line-'.str_replace(
'\\',
'\\\\', $assetid).
'-more-details\').innerHTML, \'More Info\');" onmouseout="tooltip.hide();"');
1663 if (count($lineages) > 0) {
1664 echo sq_print_icon(sq_web_path(
'lib').
'/web/images/icons/asset_locator.png', 16, 16, $lineages_name, $lineages_name,
' class="clickable" align="absmiddle" onclick="'.$lineages_onclick.
'"');
1669 if (preg_match(
'/^javascript:/i', $screen)) {
1670 $js = preg_replace(
'/^javascript:/i',
'', $screen);
1671 if (substr($js, -1) !=
';') $js .=
';';
1675 echo
'<a href="#" onclick="'.$js.
' return false;">';
1676 }
else if (!empty($screen_url)) {
1677 echo
'<a href="'.$screen_url.
'">';
1679 echo $asset_info[
'name'];
1681 if (!empty($js) || !empty($screen_url)) {
1685 echo
' (Id: #'.$assetid.
') ';
1687 $contents = ob_get_clean();
1703 function _asset_locator_onclick($lineage_assetids, $tooltip_hide)
1714 $onclick =
'if (self.name == \'sq_wysiwyg_popup_main\') {
1716 } else if (self.name == \'hipo_job\') {
1718 } else if ((typeof sq_wysiwyg_dialog != \'undefined\') && (sq_wysiwyg_dialog != null) && (!sq_wysiwyg_dialog.closed)) {
1719 am = sq_wysiwyg_dialog.frames[\'sq_wysiwyg_popup_main\'];
1721 } else if ((typeof opener != \'undefined\') && (opener != null) && (!opener.closed)) {
1722 am = opener.parent.top.frames[\'sq_sidenav\'];
1724 am = parent.top.frames[\'sq_sidenav\'];
1726 am.asset_locator_start(\''.get_asset_lineage_sort_order($lineage_assetids).
'\');
';
1728 if ($tooltip_hide) $onclick .= 'tooltip.hide();
';
1729 $onclick .= 'return false;
';
1732 }//end _asset_locator_onclick()
1744 function get_asset_type_icon($type_code, $extra='')
1746 $type_name = $GLOBALS['SQ_SYSTEM
']->am->getTypeInfo($type_code, 'name
');
1748 return sq_print_icon($GLOBALS['SQ_SYSTEM
']->am->getAssetIconURL($type_code), 16, 16, $type_name, $type_name, ' align=
"absmiddle" '.$extra);
1750 }//end get_asset_type_icon()
1761 function get_asset_status_icon($status)
1763 require_once dirname(__FILE__).'/general_occasional.inc
';
1764 $status_name = get_status_description($status);
1765 $status_colour = get_status_colour($status);
1767 return '<img src=
"'.sq_web_path('lib').'/web/images/blank.gif" width=
"10" height=
"10" alt=
"'.$status_name.'" title=
"'.$status_name.'" align=
"absmiddle" style=
"margin: 2px; border: 1px solid #ccc; background-color: #'.$status_colour.'" />
';
1769 }//end get_asset_status_icon()
1787 function asset_locator_js($asset_lineage=Array(),$map_frame='sq_sidenav
', $new_window=FALSE)
1789 $finder_frame = ($new_window || empty($map_frame)) ? 'parent.top
' : 'parent.frames[
"sq_main"]
';
1790 if (!empty($map_frame)) {
1791 $map_frame = trim('parent.top.frames[\
''.$map_frame.
'\']
', '.
');
1796 $js_code = $map_frame.'.asset_locator_start(\
''.get_asset_lineage_sort_order($asset_lineage).
'\');
';
1799 }//end asset_locator_js()
1811 function get_asset_lineage_sort_order($asset_lineage=Array())
1813 if ($asset_lineage[0] == 1) {
1814 array_shift($asset_lineage);
1817 $asset_count = count($asset_lineage);
1818 $sort_orders = Array();
1819 $link = $GLOBALS['SQ_SYSTEM
']->am->getLinkByAsset(1, $asset_lineage[0]);
1820 if (isset($link['sort_order
'])) {
1821 $sort_orders[0] = $link['sort_order
'];
1824 for ($i = 0; $i < $asset_count-1; $i++) {
1826 // get the link between two assets
1827 if (strpos($asset_lineage[$i],':
') === FALSE) {
1828 $link = $GLOBALS['SQ_SYSTEM
']->am->getLinkByAsset($asset_lineage[$i], $asset_lineage[$i+1]);
1829 if (!empty($link)) {
1830 if (isset($link['sort_order
']) && $link['sort_order
']!='') {
1831 $sort_order = $link['sort_order
'];
1835 $sort_orders[] = $sort_order;
1838 return implode('|
',$asset_lineage).'~
'.implode('|
',$sort_orders);
1840 }//end get_asset_lineage_sort_order()
1849 function get_available_global_keywords()
1852 'globals_user_
' => 'Global Keywords - Current
User',
1853 'globals_get_
' => 'Global Keywords - Get Parameters
',
1854 'globals_post_
' => 'Global Keywords - Post Parameters
',
1855 'globals_session_
' => 'Global Keywords - Session
',
1856 'globals_cookie_
' => 'Global Keywords - Cookie
',
1857 'globals_server_
' => 'Global Keywords - Server
',
1858 'globals_date
' => 'Global Keywords - Current Date and Time
',
1859 'globals_site_
' => 'Global Keywords - Current
Site',
1860 'globals_asset_
' => 'Global Keywords - Front End
Asset',
1861 'globals_snippet_
' => 'Global Keywords - Snippet
',
1862 'globals_icon_
' => 'Global Keywords - Type Icon
',
1867 }//end get_available_global_keywords()
1878 function replace_global_keywords(&$text)
1880 require_once SQ_FUDGE_PATH.'/
general/text.inc
';
1881 for ($c = 0; ($c < 2 && !(stripos($text, '%globals_
') === FALSE)); $c++) {
1882 $keywords = retrieve_keywords_replacements($text);
1883 if (!empty($keywords)) {
1884 $replacements = Array();
1885 foreach ($keywords as $keyword) {
1886 $full_keyword = $keyword;
1887 $keyword = parse_keyword($keyword, $modifiers);
1888 $contextid = extract_context_modifier($modifiers);
1890 if ($contextid !== NULL) {
1891 $GLOBALS['SQ_SYSTEM
']->changeContext($contextid);
1894 if (0 === strpos($keyword, 'globals_
')) {
1895 // Default replacement for global keywords is blank string
1896 $replacements[$full_keyword] = '';
1897 if (0 === strpos($keyword, 'globals_user_
')) {
1898 $user_keyword = 'asset_
'.substr($keyword, 13);
1899 $user_replacement = $GLOBALS['SQ_SYSTEM
']->user->getKeywordReplacement($user_keyword);
1900 if ((string)$user_replacement != "%$user_keyword%") {
1901 $replacements[$full_keyword] = $user_replacement;
1904 if (0 === strpos($keyword, 'globals_get_
')) {
1905 $sub_keyword = substr($keyword, 12);
1906 $replacements[$full_keyword] = clean_keyword_value(array_get_index($_GET, $sub_keyword, ''), $modifiers);
1908 if (0 === strpos($keyword, 'globals_post_
')) {
1909 $sub_keyword = substr($keyword, 13);
1910 $replacements[$full_keyword] = clean_keyword_value(array_get_index($_POST, $sub_keyword, ''), $modifiers);
1912 if (0 === strpos($keyword, 'globals_session_
')) {
1913 $sub_keyword = substr($keyword, 16);
1914 $replacements[$full_keyword] = clean_keyword_value(isset($_SESSION[SQ_SESSION_SANDBOX_INDEX][$sub_keyword]) ? $_SESSION[SQ_SESSION_SANDBOX_INDEX][$sub_keyword] : '', $modifiers);
1916 if ((0 === strpos($keyword, 'globals_cookie_
')) && (0 !== strpos($keyword, 'globals_cookie_SQ_
'))) {
1917 $sub_keyword = substr($keyword, 15);
1918 $replacements[$full_keyword] = clean_keyword_value(array_get_index($_COOKIE, $sub_keyword, ''), $modifiers);
1920 if (0 === strpos($keyword, 'globals_server_
')) {
1921 $sub_keyword = strtoupper(substr($keyword, 15));
1922 if($sub_keyword !== 'HTTP_COOKIE
') {
1923 $replacements[$full_keyword] = array_get_index($_SERVER, $sub_keyword, '');
1924 if (!is_scalar($replacements[$full_keyword])) {
1925 $replacements[$full_keyword] = '';
1929 if (0 === strpos($keyword, 'globals_date
')) {
1930 require_once SQ_FUDGE_PATH.'/
general/datetime.inc
';
1931 $date_keyword = readable_datetime();
1932 $formatting = substr($keyword, 12);
1933 $format = ltrim($formatting, '_
');
1934 if ($format == 'readable
') {
1935 $date_keyword = readable_datetime();
1937 if (!empty($format)) {
1938 $date_keyword = date($format);
1941 $replacements[$full_keyword] = $date_keyword;
1943 if (0 === strpos($keyword, 'globals_icon
')) {
1944 $type = substr($keyword, 12);
1945 $type = ltrim($type, '_
');
1946 $type_icon_replace = '';
1947 $type_icon_url = $GLOBALS['SQ_SYSTEM
']->am->getAssetIconURL($type);
1948 list($base_url, $type_icon_loc) = explode('__data/
', $type_icon_url);
1949 if (file_exists(SQ_DATA_PATH.'/
public/
'.$type_icon_loc)) {
1950 $type_icon_replace = sq_get_icon($type_icon_url, 16, 16, $type);
1952 $replacements[$full_keyword] = $type_icon_replace;
1955 if (0 === strpos($keyword, 'globals_site_
')) {
1956 $site_keyword = 'asset_
'.substr($keyword, 13);
1957 preg_match('|(.*):([0-9]+)$|
',$site_keyword,$matches);
1958 // We must find the site before we can replace these keywords
1960 if(empty($matches[2])) {
1961 if ($GLOBALS['SQ_SYSTEM
']->isGlobalDefineSet('CURRENT_SITE
')) {
1962 $site = $GLOBALS['SQ_SYSTEM
']->getGlobalDefine('CURRENT_SITE
');
1964 if ($GLOBALS['SQ_SYSTEM
']->isGlobalDefineSet('CURRENT_ASSET
')) {
1965 $asset_url = $GLOBALS['SQ_SYSTEM
']->getGlobalDefine('CURRENT_ASSET
')->getURL();
1966 $protocol_pos = strpos($asset_url, ':
1967 if ($protocol_pos !== FALSE) {
1968 $asset_url = substr($asset_url, $protocol_pos + 3);
1971 $asset_url = current_url(FALSE, TRUE);
1973 $asset_lineage = $GLOBALS[
'SQ_SYSTEM']->am->getLineageFromURL(NULL, $asset_url);
1976 foreach ($asset_lineage as $asset_link) {
1977 $asset_type = $asset_link[
'type_code'];
1978 if ($asset_type ==
'site' || $GLOBALS[
'SQ_SYSTEM']->am->isTypeDecendant($asset_type,
'site')) {
1979 $site_assetid = $asset_link[
'assetid'];
1983 if (!empty($site_assetid)) {
1984 $site = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($site_assetid);
1987 if (!is_null($site)) {
1988 $site_replacement = $site->getKeywordReplacement($site_keyword);
1989 if ((
string)$site_replacement !=
"%$site_keyword%") {
1990 $replacements[$full_keyword] = $site_replacement;
1994 $asset_exist = $GLOBALS[
'SQ_SYSTEM']->am->assetExists($matches[2]);
1995 if ($asset_exist === TRUE) {
1996 $site = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($matches[2]);
1998 if (!is_null($site)) {
1999 $site_replacement = $site->getKeywordReplacement($matches[1]);
2000 if ((
string)$site_replacement !=
"%$site_keyword%") {
2001 $replacements[$full_keyword] = $site_replacement;
2008 if (0 === strpos($keyword,
'globals_asset_')) {
2009 $asset_keyword =
'asset_'.substr($keyword, 14);
2010 preg_match(
'|(.*):([0-9]+)$|',$asset_keyword,$matches);
2012 if (empty($matches[2])) {
2013 $asset_replacement =
'%'.$asset_keyword.
'%';
2015 $current_asset = NULL;
2016 if ($GLOBALS[
'SQ_SYSTEM']->isGlobalDefineSet(
'CURRENT_ASSET')) {
2017 $current_asset = $GLOBALS[
'SQ_SYSTEM']->getGlobalDefine(
'CURRENT_ASSET', NULL);
2020 if (isset($_REQUEST[
'limbo_assetid'])) {
2021 $current_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset((
int)$_REQUEST[
'limbo_assetid']);
2023 $current_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAssetFromURL();
2026 $current_asset = $GLOBALS[
'SQ_SYSTEM']->frontend_asset;
2030 if ($current_asset) {
2031 $asset_replacement = $current_asset->getKeywordReplacement($asset_keyword);
2034 if ((
string)$asset_replacement !=
"%$asset_keyword%") {
2035 $replacements[$full_keyword] = $asset_replacement;
2038 $asset_exist = $GLOBALS[
'SQ_SYSTEM']->am->assetExists($matches[2]);
2039 if ($asset_exist === TRUE) {
2040 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($matches[2]);
2041 if ($asset->readAccess()) {
2044 if (0 === strpos($asset_keyword,
'asset_contents')) {
2045 $asset_keyword = preg_replace(
'|:([0-9]+)$|',
'', $asset_keyword);
2046 $this_url = preg_replace(
'/https?:\/\//',
'', $asset->getURL());
2047 if (0 === strpos($asset_keyword,
'asset_contents_raw')) {
2051 $asset->printBody();
2052 $content = ob_get_contents();
2054 $replacements[$full_keyword] = $content;
2056 }
else if ($asset_keyword ==
'asset_contents') {
2059 $this_paint_layout = $asset->getCurrentPaintLayoutName();
2060 $layout_id = $GLOBALS[
'SQ_SYSTEM']->am->getValueFromURL($this_url, $this_paint_layout);
2063 $asset->printBodyWithPaintLayout();
2065 $asset->printBodyWithPaintLayout($layout_id);
2067 $content = ob_get_contents();
2069 $replacements[$full_keyword] = $content;
2071 }
else if (0 === strpos ($asset_keyword,
'asset_contents_paint_layout_id')) {
2073 $layout_id = str_replace(
'asset_contents_paint_layout_id_',
'', $asset_keyword);
2076 $asset->printBodyWithPaintLayout();
2078 $asset->printBodyWithPaintLayout($layout_id);
2080 $content = ob_get_contents();
2082 $replacements[$full_keyword] = $content;
2084 }
else if (0 === strpos ($asset_keyword,
'asset_contents_paint_layout_name')) {
2086 $layout_name = str_replace(
'asset_contents_paint_layout_name_',
'', $asset_keyword);
2090 if (strpos($layout_name,
'paint_layout::user::') === FALSE && strpos($layout_name,
'paint_layout::system::frontend') === FALSE) {
2091 $layout_name =
'paint_layout::user::'.$layout_name;
2093 $layout_id = $GLOBALS[
'SQ_SYSTEM']->am->getValueFromURL($this_url, $layout_name);
2096 $asset->printBodyWithPaintLayout();
2098 $asset->printBodyWithPaintLayout($layout_id);
2100 $content = ob_get_contents();
2102 $replacements[$full_keyword] = $content;
2107 $asset_replacement = $asset->getKeywordReplacement($matches[1]);
2108 if ((
string)$asset_replacement !=
"%$matches[1]%") {
2109 $replacements[$full_keyword] = $asset_replacement;
2115 if (0 === strpos($keyword,
'globals_snippet_')) {
2116 $replacements[$full_keyword] =
'';
2117 if ($GLOBALS[
'SQ_SYSTEM']->am->installed(
'content_type_snippet')) {
2118 $snippet_info = explode(
'_', $keyword);
2119 if (is_numeric($snippet_info[2])) {
2121 $all_snippets = $GLOBALS[
'SQ_SYSTEM']->am->getSnippetKeywords(TRUE);
2122 if (in_array($snippet_info[2], array_keys($all_snippets))) {
2123 $snippet_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($snippet_info[2]);
2124 if (!is_null($snippet_asset)) {
2127 $snippet_asset->printBody();
2128 $replacement = ob_get_contents();
2130 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($snippet_asset);
2131 $replacements[$full_keyword] = $replacement;
2137 if (0 === strpos($keyword,
'globals_random')) {
2138 if ($keyword ==
'globals_random') {
2139 $replacements[$full_keyword] = rand();
2141 $sub_keyword = substr($keyword, 14);
2142 if (preg_match(
'/^_(\d+)_(\d+)$/', $sub_keyword, $matches)) {
2143 $replacements[$full_keyword] = rand((
int)$matches[1], (
int)$matches[2]);
2147 if ($keyword ==
'globals_context') {
2150 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
2151 if (!is_null($contextid)) {
2153 $all_contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
2154 if (isset($all_contexts[$contextid][
'name'])) {
2155 $context_name = $all_contexts[$contextid][
'name'];
2159 $replacements[$full_keyword] = $context_name;
2162 apply_keyword_modifiers($replacements[$full_keyword], $modifiers);
2167 if ($contextid !== NULL) {
2168 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
2172 replace_keywords($text, $replacements);
2202 function extract_context_modifier(&$modifiers, $return_part=
'id')
2205 foreach (array_keys($modifiers) as $modifier_key) {
2206 if ($modifiers[$modifier_key][
'modifier'] ===
'context') {
2207 if (isset($modifiers[$modifier_key][
'args']) === TRUE) {
2208 $context = $modifiers[$modifier_key][
'args'][0];
2211 unset($modifiers[$modifier_key]);
2215 if ($context !== NULL) {
2217 if ($contextid === FALSE) $contextid = NULL;
2222 if ($return_part ===
'name') {
2224 }
else if ($return_part ===
'both') {
2247 function clean_keyword_value($keyword_value, $modifiers)
2250 if (!empty($modifiers)) {
2251 return $keyword_value;
2252 }
else if (!is_scalar($keyword_value)) {
2254 $keyword_value =
'';
2258 $safe_list =
'A-Za-z0-9\.\-\_\%\$\(\)\=\[\] ';
2259 $keyword_value = preg_replace(
'/[^'.$safe_list.
']+/',
'', (
string)$keyword_value);
2262 return $keyword_value;
2284 function quick_hipo($callback_assetid, $callback_function, $items, $settings, $fail_on_error=FALSE)
2286 $hh = $GLOBALS[
'SQ_SYSTEM']->getHipoHerder();
2288 'callback_assetid' => $callback_assetid,
2289 'callback_function' => $callback_function,
2291 'settings' => $settings,
2292 'fail_on_error' => $fail_on_error,
2294 return $hh->queueHipo(
'hipo_job_quick_hipo', $vars);
2309 function do_redirect($url, $message=NULL, $timeout=0)
2311 if ($timeout || $message || headers_sent()) {
2313 if (is_null($message)) {
2314 $message = translate(
'redirecting_you_to', htmlentities($url, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
2320 <p><?php echo $message; ?></p>
2321 <script type=
"text/javascript"><!--
2326 setTimeout(
"self.location = '<?php echo str_replace("'", "\'", $url); ?>'", <?php echo $timeout * 1000; ?>);
2330 self.location = '<?php echo str_replace("'", "\'", $url); ?>';
2342 header(
'Location: '.$url);
2358 function get_class_lower($object)
2360 return strtolower(get_class($object));
2373 function clean_div_attribute($attribute)
2376 $pattern =
'/(?:<\?|\?>)+|\s+/';
2378 $attribute = strtolower(preg_replace($pattern, $replacement, $attribute));
2379 return str_replace(
'"',
'"', $attribute);
2393 function xml_entity_decode($string, $decode_html_entities = TRUE)
2395 if ($decode_html_entities) {
2396 $string = html_entity_decode($string, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
2398 $string = str_replace(
'|',
'|', $string);
2399 $string = str_replace(
''',
"'", $string);
2400 $string = str_replace(
'*',
'*', $string);
2401 $string = str_replace(
'\',
'\\', $string);
2402 $string = str_replace(
'~',
'~', $string);
2403 $string = str_replace(
'`',
'`', $string);
2404 $string = str_replace(
'^',
'^', $string);
2420 function filter_content($attr_value)
2422 $unser_str = @unserialize($attr_value);
2423 if ($unser_str !== FALSE) {
2424 if (is_array($unser_str)){
2426 $success = array_walk($unser_str,
'recursive_content_filter');
2427 if (!$success)
return FALSE;
2428 $attr_value = serialize($unser_str);
2429 }
catch (Exception $e) {
2432 }
else if (is_scalar($unser_str)){
2433 $unser_str = simple_content_filter($unser_str);
2434 if ($unser_str === FALSE)
return FALSE;
2435 $attr_value = serialize($unser_str);
2437 }
else if (is_scalar($attr_value)) {
2438 $attr_value = simple_content_filter($attr_value);
2439 if ($attr_value === FALSE)
return FALSE;
2440 }
else if (is_array($attr_value)) {
2442 $success = array_walk($attr_value,
'recursive_content_filter');
2443 if (!$success)
return FALSE;
2444 }
catch (Exception $e) {
2463 function simple_content_filter($attr_value)
2465 $reg =
"%<\/?script ?[^>]*>%i";
2466 $attr_value = preg_replace($reg,
"", $attr_value);
2467 if ($attr_value === NULL)
return FALSE;
2469 $keywords = extract_keywords($attr_value);
2470 foreach ($keywords as $keyword){
2471 $attr_value = str_ireplace(
"%$keyword%",
"", $attr_value);
2472 if ($attr_value === NULL)
return FALSE;
2475 $attr_value = htmlspecialchars_decode($attr_value, ENT_COMPAT);
2476 $attr_value = htmlspecialchars($attr_value, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
2493 function recursive_content_filter(&$item)
2496 if(is_array($item)){
2497 $success = array_walk($item, __FUNCTION__);
2498 if (!$success)
throw new Exception(
'Content Filtering Failed.');
2500 $unser_item = @unserialize($item);
2501 if ($unser_item !== FALSE) {
2502 if (is_array($unser_item)){
2503 $success = array_walk($unser_item, __FUNCTION__);
2504 if (!$success)
throw new Exception(
'Content Filtering Failed.');
2505 $item = serialize($unser_item);
2506 }
else if (is_scalar($unser_item)){
2507 $clean = simple_content_filter($unser_item);
2508 if ($clean === FALSE)
throw new Exception(
'Content Filtering Failed.');
2509 $item = serialize($clean);
2512 if (!empty($item)) {
2513 $item = simple_content_filter($item);
2514 if ($item === FALSE)
throw new Exception(
'Content Filtering Failed.');
2530 function file_mime_type($filename)
2534 if (class_exists(
'finfo')) {
2535 $finfo =
new finfo(FILEINFO_MIME);
2536 $mime_info = explode(
';', $finfo->file($filename));
2537 $mime_type = $mime_info[0];
2539 }
else if (function_exists(
'mime_content_type')) {
2540 $mime_type = mime_content_type($filename);
2543 $ext_pos = strrpos($filename,
'.');
2544 if ($ext_pos !== FALSE) {
2545 require SQ_FUDGE_PATH.
'/standards_lists/mime_types.inc';
2546 $mime_type = array_get_index($standards_lists_mime_types, substr($filename, $ext_pos + 1),
'');
2572 function fetch_url($url=
'', $options=array(), $headers=array(), $use_proxy=TRUE)
2574 static $_proxies_loaded = FALSE;
2575 static $_redirects = 0;
2578 if (isset($options[
'MAXREDIRS']) === TRUE) {
2579 $maxredirects = $options[
'MAXREDIRS'];
2582 $curl_res = curl_init($url);
2585 if(defined(
'CURLOPT_PROTOCOLS')) {
2586 curl_setopt($curl_res, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FTP | CURLPROTO_FTPS);
2589 if(strpos($url,
':') !== FALSE && !preg_match(
'/^(http)|(ftp)/i', $url)) {
2590 trigger_error(
'protocol not supported:'.$url);
2595 foreach ($options as $option => $value) {
2596 $option = strtolower($option);
2597 if ($option ==
'proxy' && is_array($value) === TRUE) {
2600 if ($option ==
'http_authentication' && is_array($value) === TRUE) {
2603 curl_setopt($curl_res, constant(
'CURLOPT_'.strtoupper($option)), $value);
2606 if (is_array($headers) === FALSE) {
2607 $headers = array($headers);
2610 if (empty($headers) === FALSE) {
2611 curl_setopt($curl_res, CURLOPT_HTTPHEADER, $headers);
2614 if (isset($options[
'http_authentication']) === TRUE) {
2615 $auth = $options[
'http_authentication'];
2616 if (isset($auth[
'username']) === TRUE && isset($auth[
'password']) === TRUE) {
2617 curl_setopt($curl_res, CURLOPT_USERPWD, $auth[
'username'].
':'.$auth[
'password']);
2622 if ($use_proxy === TRUE) {
2623 if ($_proxies_loaded === FALSE) {
2624 $proxy_auth_file = SQ_DATA_PATH.
'/private/conf/proxy_authentication.inc';
2625 if (file_exists($proxy_auth_file) === TRUE) {
2626 require_once $proxy_auth_file;
2627 $_proxies_loaded = TRUE;
2631 if ($_proxies_loaded === TRUE && defined(
'SQ_PA_ENABLED') && SQ_PA_ENABLED) {
2632 $proxy = get_proxy_info_for_url($url);
2633 if (empty($proxy[
'host']) === FALSE) {
2636 if (substr($url, 0, 5) ==
'https') {
2637 curl_setopt($curl_res, CURLOPT_HTTPPROXYTUNNEL, TRUE);
2639 curl_setopt($curl_res, CURLOPT_PROXY, $proxy[
'host']);
2640 if ($proxy[
'port']) {
2641 curl_setopt($curl_res, CURLOPT_PROXYPORT, $proxy[
'port']);
2643 if (isset($options[
'proxy']) === TRUE) {
2644 if (isset($options[
'proxy'][
'user']) === TRUE &&
2645 isset($options[
'proxy'][
'password']) === TRUE) {
2646 curl_setopt($curl_res, CURLOPT_PROXYUSERPWD, $options[
'proxy'][
'user'].
':'.$options[
'proxy'][
'password']);
2649 if ($proxy[
'user'] && $proxy[
'password']) {
2650 curl_setopt($curl_res, CURLOPT_PROXYUSERPWD, $proxy[
'user'].
':'.$proxy[
'password']);
2657 if(!$proxy_set) curl_setopt($curl_res, CURLOPT_PROXY, NULL);
2659 $result = curl_exec($curl_res);
2662 'errornumber' => curl_errno($curl_res),
2663 'errorstring' => curl_error($curl_res),
2664 'response' => $result,
2665 'curlinfo' => curl_getinfo($curl_res),
2670 if (is_resource($result) === FALSE) {
2671 curl_close($curl_res);
2678 preg_match(
'!<meta\\s+([^>]*http-equiv\\s*=\\s*("Refresh"|\'Refresh\'|Refresh)[^>]*)>!is', $result, $matches);
2679 if (empty($matches) === TRUE) {
2684 preg_match(
'!content\\s*=\\s*("[^"]+"|\'[^\']+\'|\\S+)!is', $matches[1], $urlMatches);
2685 if (empty($urlMatches) === TRUE) {
2692 $redirectContent = rtrim($urlMatches[1],
'"');
2693 $redirectContent = rtrim($redirectContent,
"'");
2696 $parts = explode(
';', $redirectContent);
2697 if (isset($parts[1]) === FALSE) {
2702 preg_match(
'/url\\s*=\\s*("[^"]+"|\'[^\']+\'|\\S+)/is', $parts[1], $urlMatches);
2703 if (empty($urlMatches) === TRUE) {
2707 $redirectUrl = rtrim($urlMatches[1],
'"');
2708 $redirectUrl = rtrim($redirectUrl,
"'");
2713 if ($_redirects > $maxredirects || $redirectUrl == $url) {
2714 $info[
'errornumber'] = CURLE_TOO_MANY_REDIRECTS;
2715 $info[
'errorstring'] =
'Too many redirects. When following redirects, libcurl hit the maximum amount.';
2716 $info[
'response'] =
'';
2717 $info[
'curlinfo'] = array();
2721 if (isset($options[
'FOLLOWLOCATION']) === TRUE) {
2723 $info = fetch_url($redirectUrl, $options, $headers, $use_proxy);
2748 function get_proxy_info_for_url($page_url)
2750 $alternatives = SQ_PA_ALTERNATIVES;
2752 if (trim($alternatives) !==
'') {
2753 $alternatives = unserialize($alternatives);
2755 $alternatives = Array();
2761 foreach ($alternatives as $alternative) {
2764 if (empty($alternative))
continue;
2766 $pattern_url = $alternative[
'pattern_url'];
2767 switch ($alternative[
'pattern_type']) {
2769 $match = ($page_url == $pattern_url);
2774 $match = (strpos($page_url, $pattern_url) === 0);
2780 $match = (strpos($page_url, $pattern_url) === (strlen($page_url) - strlen($pattern_url)));
2785 $match = (strpos($page_url, $pattern_url) !== FALSE);
2791 if ($alternative[
'pattern_not']) $match = !$match;
2795 $proxy_host = $alternative[
'proxy_url'];
2796 $proxy_port = $alternative[
'proxy_port'];
2797 $proxy_user = $alternative[
'username'];
2798 $proxy_pass = base64_decode($alternative[
'password']);
2806 $proxy_host = SQ_PA_HOSTNAME;
2807 $proxy_port = SQ_PA_PORT;
2808 $proxy_user = SQ_PA_USERNAME;
2809 $proxy_pass = base64_decode(SQ_PA_PASSWORD);
2813 'host' => $proxy_host,
2814 'port' => $proxy_port,
2815 'user' => $proxy_user,
2816 'password' => $proxy_pass,