18 require_once SQ_CORE_PACKAGE_PATH.
'/../include/asset.inc';
19 require_once SQ_CORE_PACKAGE_PATH.
'/data_source/data_source/data_source.inc';
20 require_once SQ_LIB_PATH.
'/html_form/html_form.inc';
40 protected $_tree_level = 0;
48 protected $_record_tree_level = 0;
56 protected $_non_empty_record = FALSE;
64 protected $_result = Array();
73 protected $_inside_relevant_tag = FALSE;
81 protected $_inside_root_tag = FALSE;
89 protected $_current_tag_name =
'';
97 protected $_current_attributes = Array();
105 protected $_record_set_index = 0;
114 protected $_new_teg = FALSE;
132 parent::__construct($assetid);
147 $allowed_link[
'asset'][
'card'] =
'M';
148 $allowed_link[
'asset'][
'exclusive'] = FALSE;
150 $links[SQ_LINK_TYPE_1] = $allowed_link;
151 $links[SQ_LINK_TYPE_2] = $allowed_link;
152 $links[SQ_LINK_TYPE_3] = $allowed_link;
153 $links[SQ_LINK_NOTICE] = $allowed_link;
168 $GLOBALS[
'SQ_SYSTEM']->pm->startTimer($this);
176 if (!isset($this->_tmp[$hash])) {
178 $result = parent::getResultSet($hash);
180 if ($result !== FALSE) {
181 $this->_tmp[$hash] = $result;
183 $this->_tmp[$hash] = $this->
getItems();
184 if ($this->_tmp[$hash] !== FALSE) {
185 parent::setResultSet($this->_tmp[$hash], $hash);
190 $GLOBALS[
'SQ_SYSTEM']->pm->stopTimer($this);
191 return (!empty($url)) ? $this->_tmp[$hash] : Array();
221 if (isset($this->_tmp[
'urlcontents'][$url]) === TRUE) {
222 return $this->_tmp[
'urlcontents'][$url];
226 'RETURNTRANSFER' => 1,
227 'USERAGENT' => SQ_SYSTEM_LONG_NAME,
230 $responseDetails = fetch_url($url, $options);
233 if ($responseDetails[
'errornumber'] != 0) {
234 trigger_localised_error(
'CMS0063', E_USER_WARNING, $url, $responseDetails[
'errorstring']);
245 $response = $responseDetails[
'response'];
247 $this->_tmp[
'urlcontents'][$url] = $response;
262 $url = html_entity_decode($this->_tmp[
'url_string']);
280 $url = html_entity_decode(isset($this->_tmp[
'url_string']) ? $this->_tmp[
'url_string'] : $this->
attr(
'url'));
298 if (!empty($content)) {
304 if(!empty($required_tags)) {
305 foreach($required_tags as $index => $required_tag) {
306 $pattern =
'|(<'.$required_tag.
')([\s]+[^>]*)/>|i';
307 $replace =
"<".$required_tag.
"$2>\n</".$required_tag.
">";
308 $content = preg_replace($pattern, $replace, $content);
310 $pattern =
'|(<'.$required_tag.
')([\s]+[^>]*)></'.$required_tag.
'>|i';
311 $replace =
"<".$required_tag.
"$2>\n</".$required_tag.
">";
313 $content = preg_replace($pattern, $replace, $content);
318 $xml_parser = xml_parser_create();
320 xml_parser_set_option ($xml_parser, XML_OPTION_CASE_FOLDING, 0);
321 xml_set_character_data_handler ($xml_parser, Array(&$this,
'characterData') );
322 xml_set_element_handler($xml_parser, Array(&$this,
'startElement'), Array(&$this,
'endElement'));
324 if (!xml_parse($xml_parser, $content, TRUE)) {
325 trigger_localised_error(
'CORE0258', E_USER_WARNING, xml_error_string(xml_get_error_code($xml_parser)).
', line '.xml_get_current_line_number($xml_parser).
', column '.xml_get_current_column_number($xml_parser).
' (byte '.xml_get_current_byte_index($xml_parser).
')', htmlentities($content, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
328 xml_parser_free($xml_parser);
329 return $this->_result;
347 $root_tag = $this->
attr(
'root_tag');
349 if (!$this->_inside_root_tag) {
350 if (empty($root_tag)) {
351 $this->_inside_root_tag = TRUE;
352 }
else if ($name === $root_tag) {
353 $this->_inside_root_tag = TRUE;
355 $this->_inside_root_tag = FALSE;
359 if (empty($root_tag)) {
361 $this->_inside_root_tag = TRUE;
362 }
else if ($name === $root_tag) {
364 $this->_inside_root_tag = FALSE;
384 $this->_tree_level++;
387 foreach($this->_complex_elements as $element => $val) {
388 if ($this->_complex_elements[$element] >= 0) $this->_complex_elements[$element]++;
390 if (!isset($this->_complex_elements[$name])) {
391 $this->_complex_elements[$name] = 0;
398 if ($this->_inside_root_tag) {
400 for ($i=0; $i<count($required_tags); $i++) {
401 if ($name === $required_tags[$i]) {
402 $this->_inside_relevant_tag = TRUE;
403 $this->_current_tag_name = $name;
404 $this->_new_tag = TRUE;
407 $this->_record_tree_level = $this->_tree_level;
410 $this->_non_empty_record = TRUE;
413 $this->_current_attributes = $attributes;
415 $this->_current_attributes = FALSE;
423 if ($this->_non_empty_record && ($this->_tree_level < $this->_record_tree_level || ($this->_tree_level == $this->_record_tree_level && $this->_complex_elements[$this->_current_tag_name] == -1))) {
424 $this->_record_set_index++;
427 $this->_non_empty_record = FALSE;
447 if ($this->_inside_relevant_tag) {
448 if (!isset($this->_result[$this->_record_set_index][$this->_current_tag_name])) {
449 $this->_result[$this->_record_set_index][$this->_current_tag_name] = $data;
453 else if ($this->_new_tag) {
454 $this->_result[$this->_record_set_index][$this->_current_tag_name] .= $this->
attr(
'data_delimiter') . $data;
461 $this->_result[$this->_record_set_index][$this->_current_tag_name] .= $data;
466 if (!empty($required_attributes) && $this->_current_attributes) {
467 foreach ($required_attributes as $tag_name => $attr_name) {
468 if ($this->_current_tag_name === $tag_name) {
469 foreach ($this->_current_attributes as $name => $value) {
470 for ($i=0; $i<count($attr_name); $i++) {
471 if ($attr_name[$i] === $name) {
472 $this->_result[$this->_record_set_index][$this->_current_tag_name.
':'.$attr_name[$i]] = $value;
481 $this->_new_tag = FALSE;
502 if ($this->_complex_elements[$name] >= 0) {
503 $this->_complex_elements[$name] = $this->_complex_elements[$name] > 0 ? -1 : -2;
507 $this->_tree_level--;
509 $this->_inside_relevant_tag = FALSE;
522 return unserialize(html_entity_decode($this->
attr(
'tags')));
535 return unserialize(html_entity_decode($this->
attr(
'tag_attributes')));
548 $url = $this->
attr(
'url');
549 $keyword_wrapper =
'%%';
550 $keyword_pattern =
'('.$keyword_wrapper.
'([a-zA-Z_\-0-9\.]+)'.$keyword_wrapper.
')';
553 preg_match_all (
'/'.$keyword_pattern.
'/', $url, $matches, PREG_PATTERN_ORDER);
554 if (empty($matches[1])) {
555 $this->_tmp[
'url_string'] = $url;
556 return $this->_tmp[
'url_string'];
559 $raw_keywords =& $matches[1];
560 $keywords =& $matches[2];
562 foreach ($keywords as $keyword) {
564 $replacements[] = str_replace(
'$',
'\$', $value);
565 $patterns[] =
'/('.$keyword_wrapper.$keyword.$keyword_wrapper.
')/';
568 $url = preg_replace($patterns, $replacements, $url);
570 $this->_tmp[
'url_string'] = $url;
571 return $this->_tmp[
'url_string'];
584 return isset($this->_tmp[
'url_string']) ? $this->_tmp[
'url_string'] :
'';
599 $dynamic_vars = $this->
attr(
'dynamic_vars');
600 if (!isset($dynamic_vars[$variable_name]))
return '';
603 $value = $parameter_map->getParameterValue($variable_name);
607 return array_get_index($dynamic_vars, $variable_name,
'');
610 return urlencode($value);
625 $cdata_tags = unserialize(html_entity_decode($this->
attr(
'cdata_tags')));
626 if (empty($cdata_tags))
return $content;
627 $cdata_tags = array_unique($cdata_tags);
629 foreach($cdata_tags as $cdata_tag) {
630 if (!$cdata_tag)
continue;
633 $replace_pattern =
"/(<$cdata_tag(\s[^>]*?)*?(?<!\/)>)(?!<\!\[CDATA\[)(.*?)(?<!\]\]>)(<\/$cdata_tag\s*>)/ms";
634 $replacement =
"$1<![CDATA[$3]]>$4";
635 $content = preg_replace($replace_pattern, $replacement, $content);
651 $xsl_file_id = $this->
attr(
'xslt_file');
654 $xsl_file = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($xsl_file_id);
656 if (!is_null($xsl_file) && class_exists(
'XSLTProcessor')) {
674 $xsl_file_id = $this->
attr(
'xslt_file');
677 if (!empty($feed_url) && $xsl_file_id) {
678 $xsl_file = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($xsl_file_id);
680 if (!is_null($xsl_file)) {
682 $xml =
new DOMDocument();
684 $xsl =
new DOMDocument();
685 $xsl->loadXML($xsl_file->getContent());
687 $parser =
new XSLTProcessor();
688 $parser->importStylesheet($xsl);
690 $content = $parser->transformToXML($xml);