45 function replace_keywords(&$text, &$replacements)
48 $is_replacements_array = is_array($replacements);
49 if (is_string($text)) {
50 if (!$is_replacements_array)
return '';
51 }
else if (is_array($text)) {
52 if (!$is_replacements_array)
return Array();
57 return _replace_keywords_recursive($text, $replacements);
71 function _replace_keywords_recursive(&$text, &$replacements)
73 if (is_array($text)) {
75 foreach ($text as $key => $value) {
76 $key = _replace_keywords_recursive($key, $replacements);
77 $r[$key] = _replace_keywords_recursive($value, $replacements);
82 foreach ($replacements as $keyword => $replacement) {
83 if ($keyword ==
'')
continue;
84 $text = str_replace(
"%$keyword%", $replacement, $text);
102 function parse_keyword($keyword, &$modifiers = NULL)
104 $modifiers = Array();
105 $parts = explode(
'^', $keyword);
106 $keyword = array_shift($parts);
108 foreach ($parts as &$p) {
109 $args = preg_split(
'/:{1}/', $p, -1);
110 $modifier = array_shift($args);
111 if (!empty($modifier)) {
112 array_push($modifiers, Array(
'modifier' => $modifier,
'args' => $args));
141 function apply_keyword_modifiers(&$text, $modifiers, $replace_keywords=Array())
143 foreach ($modifiers as $m) {
144 $text = apply_keyword_modifier($text, $m, $replace_keywords);
172 function apply_keyword_modifier($text, $modifier, $replace_keywords=Array())
175 switch ($modifier[
'modifier']) {
177 case "replace_keywords":
183 $nested_modifier = array_get_index($modifier[
'args'], 0, NULL);
184 $nested_modifier_args = Array();
186 $kw_replacements = array_get_index($replace_keywords,
'replacements', Array());
187 $assetid = array_get_index($replace_keywords,
'assetid', 0);
188 $call_fns = array_get_index($replace_keywords,
'call_fns', Array(
'getKeywordReplacement'));
190 for ($i = 1; $i < count($modifier[
'args']); $i++){
191 $value = $modifier[
'args'][$i];
192 preg_match_all(
'/\{([a-zA-Z_]{2,}[a-zA-Z_\-0-9\.:]+)\}/', $value, $matches, PREG_PATTERN_ORDER);
193 $keywords = array_unique($matches[1]);
195 $replacements = Array();
196 foreach ($keywords as $keyword){
197 if (isset($kw_replacements[$keyword])){
198 $replacements[$keyword] = $kw_replacements[$keyword];
199 }
else if (!empty($assetid)){
200 if (!isset($asset)) $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
202 foreach ($call_fns as $fn){
203 if (method_exists($asset, $fn)){
204 $asset_replacement = $asset->$fn($keyword);
205 if (!is_null($asset_replacement) && $asset_replacement !=
'%'.$keyword.
'%'){
206 $replacements[$keyword] = $asset_replacement;
216 foreach ($replacements as $keyword => $replacement) {
217 if ($keyword ==
'')
continue;
218 $value = str_replace(
"{".$keyword.
"}", $replacement, $value);
220 $nested_modifier_args[] = $value;
223 return apply_keyword_modifier($text, Array(
'modifier' => $nested_modifier,
'args' => $nested_modifier_args));
226 return urlencode($text);
229 return urldecode($text);
232 return base64_encode($text);
235 return base64_decode($text);
238 return strtolower($text);
241 return strtoupper($text);
247 return addslashes($text);
250 return htmlspecialchars($text, ENT_QUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
253 return strip_tags($text);
257 $max = array_get_index($modifier[
'args'], 0, -1);
259 if (SQ_CONF_DEFAULT_CHARACTER_SET ==
'utf-8' && extension_loaded(
'mbstring'))
260 return mb_substr($text, 0, $max, SQ_CONF_DEFAULT_CHARACTER_SET);
262 return substr($text, 0, $max);
266 $max = array_get_index($modifier[
'args'], 0, NULL);
268 if (is_numeric($max)) {
269 $text = str_replace(
' ',
' ', trim($text));
270 $words = preg_split(
"/[\s,]+/", $text);
272 if ($max >= count($words)) {
275 $words = array_splice($words, 0, $max);
276 return implode(
' ', $words);
283 return str_word_count($text);
286 return strlen($text);
289 return ucfirst($text);
292 return ucwords($text);
298 return preg_replace(
'/\s+/',
'_', $text);
310 if (empty($text))
return $text;
312 $delim = array_get_index($modifier[
'args'], 0,
'');
313 $path = array_get_index($modifier[
'args'], 1, NULL);
316 for ($i = 2; $i < count($modifier[
'args']); $i++) {
317 $path .=
':' . $modifier[
'args'][$i];
322 $xml =
new SimpleXMLElement($text);
325 $namespaces = explode(
'xmlns:', $path);
326 $path = array_shift($namespaces);
327 foreach ($namespaces as $namespace) {
328 $ns = explode(
'=', $namespace);
329 if (count($ns) == 2) {
330 $xml->registerXPathNamespace($ns[0], trim($ns[1]));
334 $res = $xml->xpath($path);
336 return implode($delim, $xml->xpath($path));
338 }
catch (Exception $e) {
340 trigger_error($e->getMessage());
367 if (empty($text))
return $text;
371 $path = implode(
':', $modifier[
'args']);
373 $dom =
new DOMDocument();
374 $dom->loadXML($text);
375 $xpath =
new DOMXPath($dom);
377 $result = $xpath->evaluate($path);
383 return is_scalar($result) ? $result :
'';
389 $text = implode(
':', $modifier[
'args']);
397 $text = implode(
':', $modifier[
'args']);
403 $tag = array_get_index($modifier[
'args'], 0, NULL);
408 return "<$tag>$text</$tag>";
415 $tag = array_get_index($modifier[
'args'], 0, NULL);
420 return "<$tag>$text</$tag>";
427 $text = str_replace(
']]>',
']]]]><![CDATA[>', $text);
428 return "<![CDATA[$text]]>";
432 $text = preg_replace(
'/^<\?xml\s.*\?>/',
'', $text);
453 $plus = array_get_index($modifier[
'args'], 0,
'+');
454 $minus = array_get_index($modifier[
'args'], 1,
'-');
455 $zero = array_get_index($modifier[
'args'], 2,
'0');
459 }
else if ($text < 0) {
475 $precision = array_get_index($modifier[
'args'], 0, 0);
476 return round($text, $precision);
478 case "number_format":
480 $decimals = array_get_index($modifier[
'args'], 0, 0);
481 return number_format($text, $decimals);
490 return _run_arithmetic_keyword_modifier($modifier, $text);
502 return _run_comparison_keyword_modifier($modifier, $text);
513 $format = array_get_index($modifier[
'args'], 0,
'');
514 return date($format, strtotime($text));
517 $toReplace = array_get_index($modifier[
'args'], 0, NULL);
518 $replaceWith = array_get_index($modifier[
'args'], 1, NULL);
520 if (!is_null($toReplace) && !is_null($replaceWith)) {
521 return preg_replace(
'/'.$toReplace.
'/', $replaceWith, $text);
526 $toMatch = array_get_index($modifier[
'args'], 0, NULL);
527 $success = array_get_index($modifier[
'args'], 1, NULL);
528 $failure = array_get_index($modifier[
'args'], 2, NULL);
530 if (!is_null($toMatch) && strpos($toMatch,
'/') !== FALSE) $toMatch = str_replace(
'/',
'\/', $toMatch);
532 if (!is_null($toMatch) && preg_match_all(
'/'.$toMatch.
'/', $text, $matches)) {
533 if (isset($matches[0]) && count($matches[0]) > 0) {
534 return $success ? $success : count($matches[0]);
537 return $failure ? $failure : 0;
540 $asset_keyword = array_get_index($modifier[
'args'], 0,
'');
541 if ($asset_keyword !=
'' && assert_valid_assetid($text,
'', TRUE, FALSE)) {
542 if ($GLOBALS[
'SQ_SYSTEM']->am->assetExists($text)) {
543 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($text);
544 if ($asset->readAccess()) {
545 if (strpos($asset_keyword,
'asset_contents') === 0) {
546 if ($asset_keyword ==
'asset_contents') {
548 $this_paint_layout = $asset->getCurrentPaintLayoutName();
549 $this_url = preg_replace(
'/^.+:\/\//',
'', $asset->getURL());
550 $layout_id = $GLOBALS[
'SQ_SYSTEM']->am->getValueFromURL($this_url, $this_paint_layout);
553 $asset->printBodyWithPaintLayout();
555 $asset->printBodyWithPaintLayout($layout_id);
557 $content = ob_get_contents();
560 }
else if ($asset_keyword ==
'asset_contents_raw') {
563 $content = ob_get_contents();
566 }
else if (strpos($asset_keyword,
'asset_contents_paint_layout_id') === 0) {
567 $layout_id = str_replace(
'asset_contents_paint_layout_id_',
'', $asset_keyword);
570 $asset->printBodyWithPaintLayout();
572 $asset->printBodyWithPaintLayout($layout_id);
574 $content = ob_get_contents();
577 }
else if (0 === strpos ($asset_keyword,
'asset_contents_paint_layout_name')) {
578 $layout_name = str_replace(
'asset_contents_paint_layout_name_',
'', $asset_keyword);
579 $this_url = preg_replace(
'/^.*:\/\//',
'', $asset->getURL());
583 if (strpos($layout_name,
'paint_layout::user::') === FALSE && strpos($layout_name,
'paint_layout::system::frontend') === FALSE) {
584 $layout_name =
'paint_layout::user::'.$layout_name;
586 $layout_id = $GLOBALS[
'SQ_SYSTEM']->am->getValueFromURL($this_url, $layout_name);
589 $asset->printBodyWithPaintLayout();
591 $asset->printBodyWithPaintLayout($layout_id);
593 $content = ob_get_contents();
602 return $asset->getKeywordReplacement($asset_keyword);
607 trigger_localised_error(
'SYS0087', E_USER_WARNING, $text);
612 trigger_localised_error(
'CORE0321', E_USER_WARNING, $text);
620 $xsl_assetid = array_get_index($modifier[
'args'], 0, NULL);
621 if (empty($xsl_assetid))
return $new_text;
622 $xsl_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($xsl_assetid);
623 if (empty($xsl_asset))
return $new_text;
625 $xsl =
new DOMDocument();
626 if ($xsl_asset instanceof
File) {
627 $xsl->load($xsl_asset->data_path.
'/'.$xsl_asset->name);
629 trigger_error(
"XSLT keyword modifier: The asset #$xsl_assetid is not a File asset.");
634 $xslt =
new XSLTProcessor();
635 $xslt->importStylesheet($xsl);
638 $xml =
new DOMDocument();
639 $xml->loadXML($text);
640 $new_text = $xslt->transformToXml($xml);
642 }
catch (Exception $e) {
643 trigger_error($e->getMessage());
649 if (!function_exists(
'json_encode')) {
650 require_once
'Services/JSON.php';
651 $json =
new Services_JSON();
652 $output = $json->encodeUnsafe($text);
653 if (Services_JSON::isError($output)) {
654 trigger_error($output, E_USER_NOTICE);
657 $output = json_encode($text);
658 if (function_exists(
'json_last_error') && (json_last_error() != JSON_ERROR_NONE)) {
659 switch (json_last_error()) {
660 case JSON_ERROR_DEPTH:
661 $error =
'Maximum stack depth exceeded';
663 case JSON_ERROR_STATE_MISMATCH:
664 $error =
'Underflow or the modes mismatch';
666 case JSON_ERROR_CTRL_CHAR:
667 $error =
'Unexpected control character found';
669 case JSON_ERROR_SYNTAX:
670 $error =
'Syntax error, malformed JSON';
672 case JSON_ERROR_UTF8:
673 $error =
'Malformed UTF-8 characters, possibly incorrectly encoded';
676 $error =
'Unknown error';
679 trigger_error($error, E_USER_NOTICE);
685 return trim($output,
'"');
703 function _run_arithmetic_keyword_modifier($modifier, $text)
706 $n = array_get_index($modifier[
'args'], 0, 0);
708 switch ($modifier[
'modifier']) {
730 }
else if ($text ==
'-1') {
750 function _run_comparison_keyword_modifier($modifier, $text)
753 $n = array_get_index($modifier[
'args'], 0, 0);
754 $t = array_get_index($modifier[
'args'], 1,
'1');
755 $f = array_get_index($modifier[
'args'], 2,
'');
758 switch ($modifier[
'modifier']) {
800 function retrieve_keywords_replacements($text, $additional=
'')
802 return extract_keywords($text, $additional);
816 function extract_keywords($text, $additional=
'')
819 $additional = preg_replace(
'|(.)|',
'\\\\\1', $additional);
822 preg_match_all (
'/%([a-zA-Z_]{2,}[a-zA-Z_\-0-9\.:'.$additional.
']+(\^{1}[^%]+)*)%/', $text, $matches, PREG_PATTERN_ORDER);
823 return array_unique($matches[1]);
844 function get_word_counts($text, $split_regexp=
"/\s+/", $word_match_regexp=
"/\S+/", $min_length=1)
847 if (!is_string($text) || !is_string($split_regexp) || !is_string($word_match_regexp) || !is_int($min_length)) {
849 }
else if (!is_int($min_length) || ($min_length < 0)) {
854 $text = remove_silent_chars($text);
856 $words = preg_split($split_regexp, $text, -1, PREG_SPLIT_NO_EMPTY);
860 foreach ($words as $key => $word) {
861 $words[$key] = trim($word);
864 $word_counts = array_count_values($words);
865 foreach ($word_counts as $word => $count) {
866 if (strlen($word) < $min_length || !preg_match($word_match_regexp, $word, $matches)) {
867 unset($word_counts[$word]);
888 function remove_silent_chars($text, $not_indexing=FALSE, $oracle_keyword=FALSE)
891 $number_pattern =
'/(\d{1})[,.](\d{1})/';
892 $text = preg_replace($number_pattern,
'$1$2', $text);
894 if (!$oracle_keyword) {
897 $email_pattern =
'/([^@\s]+)@(([-a-z0-9]+\.)+[a-z]{2,})/e';
898 $text = preg_replace($email_pattern,
'str_replace(".", "", "$1").str_replace(".", "", "$2")', $text);
901 $text = str_replace(
'\'',
'', $text);
904 $silent_chars = Array(
'.',
':',
';',
',',
'!',
'?',
'`',
'!',
'@',
'#',
'$',
'^',
'*',
'-',
'=',
'+',
'{',
'}',
'[',
']',
'\\',
'<',
'>',
'/');
905 if ($not_indexing == FALSE) {
906 $oracle_search_operators = Array(
'(',
')',
'"',
'~',
'&',
'|',
'%');
907 $silent_chars = array_merge($silent_chars, $oracle_search_operators);
909 $text = str_replace(
'_',
'', $text);
913 $text = str_replace($silent_chars,
' ', $text);
928 function escape_double_quotes(&$subject)
930 $subject = str_replace(
'"',
'\"', $subject);
944 function escape_php(&$subject)
947 $subject = str_replace(
'<?',
"<?php echo '<'.'?'; ?>", $subject);
948 $subject = str_replace(
'<%',
"<?php echo '<'.'%'; ?>", $subject);
949 $subject = preg_replace(
956 "<?php echo '<'.'script language=\"php\">'; ?>",
975 function ucwords_no_space($string, $word_separator=
'_')
977 $tmp_word = strtr($string, $word_separator,
' ');
978 $tmp_word = preg_replace(
'/\s+/',
'', ucwords($tmp_word));
997 function make_readable_list($string_array, $end_delimiter=
'and')
999 if (empty($string_array) || !is_array($string_array)) {
1003 $last = array_pop($string_array);
1005 if (empty($string_array))
return $last;
1007 $retval = implode(
', ', $string_array);
1008 $retval .=
' '.$end_delimiter.
' '.$last;
1023 function escape_regex_chars($text)
1025 return preg_replace(
'|([\{\}\(\)\^\$\.\*\?\+\-\[\]\\\])|',
'\\\$1', $text);