5 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2001 Stefan Saasen |
8 // +----------------------------------------------------------------------+
9 // | The contents of this file are subject to the Mozilla Public License |
10 // | Version 1.1 (the "License"); you may not use this file except in |
11 // | compliance with the License. You may obtain a copy of the License at |
12 // | http://www.mozilla.org/MPL/ |
14 // | Software distributed under the License is distributed on an "AS IS" |
15 // | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
16 // | the License for the specific language governing rights and |
17 // | limitations under the License. |
18 // +----------------------------------------------------------------------+
20 // | Maintainer and initial developer: |
21 // | Stefan Saasen <s@fase4.com> |
23 // | Proxy and authentication methods added by: |
24 // | Marco Kraus <marco.kraus@siemens.com> |
26 // | Decoding of data by htmlentities or utf8_decode added by: |
27 // | Roland Haeder <webmaster@mxchange.org> |
29 // +----------------------------------------------------------------------+
31 // | @link http://www.fase4.com/rdf/ Latest release |
32 // +----------------------------------------------------------------------+
37 * This class offers methods to parse RSS Files
39 * @link http://www.fase4.com/rdf/ Latest release of this class
41 * @copyright Copyright (c) 2001 fase4.com. All rights reserved.
42 * @author Stefan Saasen <s@fase4.com>
43 * @author Roland Haeder <webmaster@mxchange.org>
44 * @version 1.7 ($Date$Revision: 856 $
51 * Word-wrapping mode for description, set it to 0 do disable this feature! Ommits _use_nl2br!
56 var $_word_wrap = '0';
59 * Wether to recode \n -> <br /> or not in description
64 var $_use_nl2br = TRUE;
67 * Sets the decoding mode of the read data (UTF8 scrambles some german umlauts here!)
69 * "htmlentities" - Use the function htmlentities()
70 * "utf8_decode" - Use the function ut8_decode() when you have UTF8 encoded text
71 * <empty> - Use non of both
76 var $_decoding_mode = '';
79 * If $_link_target is set a target='xxx' attribute in each <a /> and <form accept-charset="utf-8" /> html tag will be added
81 * Possible values are "_blank", "_content", "_parent", "_self", "_top"
86 var $_link_target = '_blank';
89 * vars for proxy settings - Prox Host
97 * vars for proxy settings - Prox Port
105 * vars for proxy settings - Prox Username
113 * vars for proxy settings - Prox Password
121 * just a flag for checking if proxy-support should be enabled
122 * set default to false (will be enabled if set_proxy is called)
128 var $_use_proxy = FALSE;
131 * just a flag for checking if proxy-support with authentication
133 * set default to false (will be enabled if set_proxy is called)
139 var $_use_proxy_auth = FALSE;
142 * The time the Files will be cached (in seconds).
147 var $_refresh = 60; // int
150 * The Name of the cached File.
155 var $_cached_file = ''; // String
158 * Indicates whether the cached or the remote file was used.
163 var $_use_cached_file = TRUE;
166 * (fast|normal) depends on _use_dynamic_display(). _use_dynamic_display(TRUE) -> 'normal', otherwise 'fast'
171 var $_cache_type = 'fast';
174 * The Name of the Remote File.
179 var $_remote_file = '';
182 * Path to the Cache Directory.
187 var $_cache_dir = 'cache/'; // String
190 * Indicates whether the Creating of the Cache Directory needs to be done or not.
195 var $_cache_dir_ok = FALSE;
198 * Type of the file to be parsed (RSS or RDF).
200 * The Type depends on the root node
205 var $_type = 'rss'; // string (rss or rdf)
208 * Array of Display Settings.
210 * Specific Parameters can be set to hidden. These are:
211 * image, channel and textinput. If set to "hidden" those elements won't be displayed.
216 var $_display_opt = array(
221 'cache_update' => '',
228 * Defines the width attribute in the table that holds the rdf/rss representation
232 * @see see_table_width()
234 var $_table_width = '100%';
237 * Indicates whether or not to use dynamic Display Settings
242 var $_use_dynamic_display = FALSE;
250 var $_item_count = '0';
258 var $_max_count = FALSE;
261 * Array containing the content of <channel />
266 var $_array_channel = array();
269 * Array containing the content of each <item />
274 var $_array_item = array();
277 * Array containing the content of <textinput />
282 var $_array_textinput = array();
285 * Array containing the content of <image />
290 var $_array_image = array();
293 * Array containing the Channel content. Just For internal XML Parser Purposes.
298 var $_citem = array();
301 * Array containing the Channel Parser Depth. Just For internal XML Parser Purposes.
306 var $_cdepth = array();
309 * Array containing the Channel tags. Just For internal XML Parser Purposes.
314 var $_ctags = array('x');
317 * Array containing the Channel content. Just For internal XML Parser Purposes.
322 var $_item = array(); // Array
325 * Array containing the Channel Parser Depth. Just For internal XML Parser Purposes.
330 var $_depth = array(); // Array
333 * Array containing the tags. Just For internal XML Parser Purposes.
338 var $_tags = array('x'); // Array
341 * Garbage collection: probability in percent
343 * @var integer 0 => never
346 var $gc_probability = 1;
359 var $_parse_mode = '';
368 * Constructor of our Class
370 * This Method checks if the Cache Directory can be found. Otherwise it tries to creat the Cache Directory at the specified Path.
371 * Additionally the Refresh Time is set to a default Value of 1200s (20 min).
374 * @author Stefan Saasen <s@fase4.com>
379 // default Value, to be overwritten in set_refresh()
380 $this->_refresh = (time() - 1200);
381 $this->_clear_cItems();
382 $this->_clear_Items();
386 * This Method starts the parsing of the specified RDF File. The File can be a local or a remote File.
389 * @author Stefan Saasen <s@fase4.com>
390 * @param string $rdf RDF File (Location)
391 * @return string Displays RDF Content (using _display())
392 * @see _remote_file, cache()
394 function parse_RDF($rdf)
396 unset($this->_array_item);
397 $this->_remote_file = $rdf;
398 $this->out .= "<!-- http://www.fase4.com/rdf/ -->";
399 $this->out .= "<table width=\"".$this->_table_width."\">";
400 $this->out .= $this->cache();
401 $this->out .= "</table>";
403 $this->_item_count = '0';
408 * This Method is called when all parsing is finished to use the garbage collection
411 * @author Stefan Saasen <s@fase4.com>
412 * @param string $rdf RDF File (Location)
413 * @return string Displays RDF Content (using _display())
414 * @see _remote_file, cache()
416 function finish($return = FALSE) {
417 $this->out = str_replace('$', '$', $this->out);
424 $this->_garbage_collection();
428 * With this method you can decide whether to use the normal cache and dynamic display Options or to use a static cache.
430 * In the first case the rdf/rss File will be stored locally, in the second case the html output of the specified source will be stored.
431 * In this case you can not modify the display settings.
432 * processing time: (1.4792) --> remote file
433 * processing time: (0.0313) --> using 'normal cache' with display Modification turned on.
434 * processing time: (0.0019) --> using 'fast cache'
437 * @author Stefan Saasen <s@fase4.com>
438 * @param string $rdf RDF File (Location)
439 * @return string Displays RDF Content (using _display())
440 * @see _remote_file, cache()
442 function use_dynamic_display($bool) {
443 $this->_use_dynamic_display = $bool;
448 * This Method avtually parses the XML data.
451 * @author Stefan Saasen <s@fase4.com>
452 * @param string $data RDF File XML Data
453 * @see _clear_Items()
455 function _parse_xRDF($data) {
456 $this->_clear_Items();
457 $xml_parser = xml_parser_create();
458 xml_set_object($xml_parser,$this);
459 xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
460 xml_set_element_handler($xml_parser, '_startElement', '_endElement');
461 xml_set_character_data_handler($xml_parser, '_parseData');
462 if (!xml_parse($xml_parser, trim($data))) {
463 $this->_throw_exception(sprintf("XML error: %s at line %d",
464 xml_error_string(xml_get_error_code($xml_parser)),
465 xml_get_current_line_number($xml_parser)).'<br /><br />Exception in function parse_RDF().');
467 xml_parser_free($xml_parser);
472 * This Methods allows you to set the Refresh Time
475 * @author Stefan Saasen <s@fase4.com>
476 * @param int $seconds time files will be cached (in seconds).
480 function set_refresh($seconds) {
481 $this->_refresh = (time() - $seconds);
485 function set_salt($saltPara) {
486 $this->salt = $saltPara;
491 * This Methods allows you to set the No. of <item>s to display
494 * @param int $int No of max <item>s
495 * @author Stefan Saasen <s@fase4.com>
497 * @see _max_count, _endElement()
499 function set_max_item($int)
501 $this->_max_count = $int;
506 * This Methods allows you to set the Cache Directory
509 * @author Stefan Saasen <s@fase4.com>
510 * @param string $dir Path to Directory.
514 function set_CacheDir($dir)
516 if (substr($dir, -1) != '/') {
519 $this->_cache_dir = $dir;
523 * This Method displays Error Messages and terminates the Execution of the Script
526 * @param string $msg Message to display on failure
527 * @author Stefan Saasen <s@fase4.com>
529 function _throw_exception($msg)
531 $this->out .= "<div style=\"font-family: verdana, helvetica, arial, sans-serif;font-size:11px; color: #6699cc;margin-top:10px;margin-bottom:10px;\" align=\"center\">fase4 RDF Error: ".$msg."</div>";
536 * This Method clears the Array containig the Items.
539 * @author Stefan Saasen <s@fase4.com>
542 function _clear_Items() {
543 $this->_item = array(
550 'lastBuildDate' => '',
556 * This Method clears the Array containig the Channel Items.
559 * @author Stefan Saasen <s@fase4.com>
562 function _clear_cItems() {
563 $this->_citem = array(
570 'managingEditor' => '',
573 'lastBuildDate' => '',
590 * XML Parser Start Element Handler
593 * @author Stefan Saasen <s@fase4.com>
594 * @param mixed $parser a reference to the XML parser calling the handler.
595 * @param string $name contains the name of the element for which this handler is called.
596 * @param string $attrs contains an associative array with the element's attributes (if any).
597 * @see _get_ChannelData(), _clear_Items(), _type, _parse_mode, _depth, _tags, _cdepth, _ctags
599 function _startElement($parser, $name, $attrs) {
600 // We have to determine, which type of xml data we have to parse
601 if ($name == 'rss') {
602 $this->_type = 'rss';
603 } elseif ($name == 'rdf:RDF' || $name == 'rdf') {
604 $this->_type = 'rdf';
608 if ($name == 'channel' && $this->_type != 'rdf') {
609 $this->_parse_mode = 'channel';
610 } elseif (($name == 'item')
611 || ($name == 'image')
612 || ($name == 'textinput')
613 || (($name == 'channel') && ($this->_type != 'rss'))) {
614 if ($this->_parse_mode == 'channel') {
615 $this->_get_ChannelData($parser);
617 $this->_parse_mode = 'all';
620 if (!isset($this->_depth[$this->get_parser_id($parser)])) {
621 $this->_depth[$this->get_parser_id($parser)] = '0';
623 $this->_depth[$this->get_parser_id($parser)]++;
624 array_push($this->_tags, $name);
626 if (!isset($this->_cdepth[$this->get_parser_id($parser)])) {
627 $this->_cdepth[$this->get_parser_id($parser)] = '0';
629 $this->_cdepth[$this->get_parser_id($parser)]++;
630 array_push($this->_ctags, $name);
631 } // END _startElement()
634 * Retrives the Channel Data in <rss> File
637 * @author Stefan Saasen <s@fase4.com>
638 * @param mixed $parser a reference to the XML parser calling the handler.
639 * @see _output, _display_opt, _citem
641 function _get_ChannelData($parser)
643 $this->_citem['link'] = trim($this->_citem['link']);
644 if (($this->_display_opt['sitelink'] == $this->_citem['link']) && (!empty($this->_display_opt['reflink'])) && (!empty($this->_display_opt['refid'])))
646 $this->_citem['link'] .= $this->_display_opt['reflink'].$this->_display_opt['refid'];
649 if (empty($this->_display_opt['channel']) ||
650 $this->_display_opt['channel'] != 'hidden') {
651 $this->_output .= "<tr><td>\n";
652 $this->_output .= "<table border=\"0\" width=\"100%\" class=\"fase4_rdf_meta\" cellspacing=\"5\" cellpadding=\"2\">\n";
653 $this->_output .= "<tr><td class=\"fase4_rdf_main_title\"><div class=\"fase4_rdf_main_title\">".htmlspecialchars($this->_citem['title'])."</div></td></tr>\n";
654 $this->_output .= "<tr><td class=\"fase4_rdf\">".strip_tags($this->_citem['description'], '<a>, <img>')."</td></tr>\n";
655 $this->_output .= "<tr><td> </td></tr>\n";
656 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
657 if (isset($this->_display_opt['build']) && $this->_display_opt['build'] != 'hidden') {
658 if ($this->_citem['lastBuildDate']) { $this->_output .= 'build: '. $this->_citem['lastBuildDate'].'<br />';}
660 if (isset($this->_display_opt['cache_update']) && $this->_display_opt['cache_update'] != 'hidden' && ($_update = $this->get_cache_update_time())) {
661 $this->_output .= 'cache update: '.$_update."<br />\n";
663 $this->_output .= "<a href=\"".$this->_citem['link']."\" ";
664 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
665 $this->_output .= ">".$this->_cut_string($this->_citem['link']) . '</a>';
666 $this->_output .= "</td></tr>\n";
667 $this->_output .= "<tr><td><hr noshade width=\"100%\" size=\"1\"></td></tr>\n";
668 $this->_output .= "</table></td></tr>";
670 $this->_array_channel = array(
671 'title' => $this->_citem['title'],
672 'link' => $this->_citem['link'],
673 'description' => $this->_citem['description'],
674 'lastBuildDate' => $this->_citem['lastBuildDate']);
678 * XML Parser End Element Handler
681 * @author Stefan Saasen <s@fase4.com>
682 * @param mixed $parser a reference to the XML parser calling the handler.
683 * @param string $name contains the name of the element for which this handler is called.
684 * @see _clear_Items(), _type, _parse_mode, _depth, _tags, _cdepth, _ctags, _item, _output, _display_opt
686 function _endElement($parser, $name) {
687 array_pop($this->_tags);
688 $this->_depth[$this->get_parser_id($parser)]--;
689 array_pop($this->_ctags);
690 $this->_cdepth[$this->get_parser_id($parser)]--;
691 $this->_item['link'] = trim($this->_item['link']);
692 if ((!empty($this->_display_opt['refid'])) && (!empty($this->_item['link'])))
694 if (!isInString('refid=', $this->_item['link'])) $this->_item['link'] .= '?refid=' . $this->_display_opt['refid'];
698 if (empty($this->_max_count) || $this->_item_count < $this->_max_count) {
699 if ($this->_item['title'] != $this->_item['description']
700 && $this->_item['description']) {
702 // word-wrapping added by Roland Haeder <webmaster@mxchange.org>
703 if (($this->_word_wrap > 0) && (strlen($this->_item['description']) > $this->_word_wrap))
705 // Switch off _use_nl2br
706 $this->_use_nl2br = FALSE;
707 // First remove all \n
708 $this->_item['description'] = str_replace('\n', ' ', $this->_item['description']);
709 // Wrap with <br />\n
710 $this->_item['description'] = wordwrap($this->_item['description'], $this->_word_wrap, "*<br />\n");
712 elseif (($this->_word_wrap == '0') && (!$this->_use_nl2br))
714 // Strip tags out instead when word-wrap is disabled
715 $this->_item['description'] = strip_tags($this->_item['description'], '<a>, <img>');
718 // nl2br added by Roland Haeder <webmaster@mxchange.org>
719 if ($this->_use_nl2br) $this->_item['description'] = nl2br($this->_item['description']);
721 $this->_output .= "<tr><td class=\"fase4_rdf_title\"><div class=\"fase4_rdf_title\"><a class=\"fase4_rdf_title\" href=\"".$this->_item['link']."\" ";
722 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
723 $this->_output .= ">".strip_tags($this->_item['title'], '<a>, <img>').'</a> ('.$this->_item['pubDate'].")</div></td></tr>\n";
724 $this->_output .= "<tr><td class=\"fase4_rdf\">".$this->_item['description']."</td></tr>\n";
725 // we just display the <hr> if there is a description
726 $this->_output .= "<tr><td><hr noshade=\"noshade\" size=\"1\" /></td></tr>\n";
728 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
729 $this->_output .= "<a href=\"".$this->_item["link"]."\" ";
730 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
731 $this->_output .= ">".$this->_item["title"]."</a></td></tr>\n";
734 $this->_array_item[] = array(
735 'title' => $this->_item['title'],
736 'link' => $this->_item['link'],
737 'description' => $this->_item['description']
740 ++$this->_item_count;
742 $this->_clear_Items();
746 if (isset($this->_display_opt['image']) && ($this->_display_opt['image'] != 'hidden') && $this->_item['url']) {
747 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
748 $this->_output .= "<a href=\"".$this->_item['link']."\" ";
749 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
750 $this->_output .= "><img src=\"".$this->_item['url']."\"";
751 if (isset($this->_item['width']) && isset($this->_item['height'])) {
752 $this->_output .= " width=\"".$this->_item['width']."\" height=\"".$this->_item['height']."\"";
754 $this->_output .= " alt=\"".$this->_item['title']."\" border=\"0\" /></a></td></tr>\n";
756 $this->_array_image[] = array(
757 'url' => $this->_item['url'],
758 'link' => $this->_item['link'],
759 'width' => $this->_item['width'],
760 'height' => $this->_item['height']
762 $this->_clear_Items();
763 } elseif (isset($this->_display_opt['image']) && ($this->_display_opt['image'] == 'hidden')) {
764 $this->_clear_Items();
770 if (isset($this->_display_opt['channel']) && $this->_display_opt['channel'] != 'hidden' && $this->_item['title'] != '') {
771 $this->_output .= "<tr><td>\n";
772 $this->_output .= "<table border=\"0\" width=\"100%\" class=\"fase4_rdf_meta\" cellspacing=\"5\" cellpadding=\"2\">\n";
773 $this->_output .= "<tr><td class=\"fase4_rdf\"><div class=\"fase4_rdf_title\">".htmlspecialchars($this->_item['title'])."</div></td></tr>\n";
774 $this->_output .= "<tr><td class=\"fase4_rdf\">".strip_tags($this->_item['description'], '<a>, <img>')."</td></tr>\n";
775 $this->_output .= "<tr><td> </td></tr>\n";
776 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
777 if ($this->_display_opt['build'] != 'hidden') {
778 if ($this->_item['lastBuildDate']) { $this->_output .= 'build: '. $this->_item['lastBuildDate'].'<br />';}
780 if ($this->_display_opt['cache_update'] != 'hidden' && ($_update = $this->get_cache_update_time())) {
781 $this->_output .= 'cache update: '.$_update."<br />\n";
783 $this->_output .= "<a href=\"".$this->_item['link']."\" ";
784 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
785 $this->_output .= ">".$this->_cut_string($this->_item['link'])."</a>\n";
786 $this->_output .= "</td></tr>\n";
787 $this->_output .= "</table></td></tr>\n";
789 $this->_array_channel = array(
790 'title' => $this->_item['title'],
791 'link' => $this->_item['link'],
792 'description' => $this->_item['description'],
793 'lastBuildDate' => $this->_item['lastBuildDate']
795 $this->_clear_Items();
796 $this->_clear_cItems();
800 if (isset($this->_display_opt['textinput']) && ($this->_display_opt['textinput'] != 'hidden') && $this->_item['name'] && $this->_item['link']) {
801 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
802 $this->_output .= "<form accept-charset=\"UTF-8\" action=\"".$this->_item['link']."\" ";
803 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
804 $this->_output .= "method=\"get\">\n";
805 $this->_output .= "<div class=\"fase4_rdf_title\">".$this->_item['title']."</div>";
806 $this->_output .= strip_tags($this->_item['description'], '<a>, <img>')."<br /><br />\n";
807 $this->_output .= "<input class=\"fase4_rdf_input\" type=\"text\" name=\"".$this->_item['name']."\"> \n";
808 $this->_output .= "<input class=\"fase4_rdf_input\" type=\"submit\" value=\"go\">";
809 $this->_output .= "</form>\n";
810 $this->_output .= "</td></tr>\n";
811 $this->_array_textinput = array(
812 'title' => $this->_item['title'],
813 'name' => $this->_item['name'],
814 'link' => $this->_item['link'],
815 'description' => $this->_item['description']
817 $this->_clear_Items();
818 } elseif (isset($this->_display_opt['textinput']) && ($this->_display_opt['textinput'] == 'hidden')) {
819 $this->_clear_Items();
827 * This Method returns the data from the <channel /> paragraph.
830 * @author Stefan Saasen <s@fase4.com>
832 * @see _array_channel
834 function get_array_channel()
836 return $this->_array_channel;
840 * This Method returns the data from each <item /> paragraph.
843 * @author Stefan Saasen <s@fase4.com>
847 function get_array_item()
849 return $this->_array_item;
853 * This Method returns the data from <textinput />.
856 * @author Stefan Saasen <s@fase4.com>
858 * @see _array_textinput
860 function get_array_textinput()
862 return $this->_array_textinput;
866 * Getter for parser id from resource
869 * @author Roland Haeder <webmaster@mxchange.org>
872 function get_parser_id ($parser) {
877 if (is_resource($parser)) {
878 // Cast the resource into id
887 * This Method returns the data from <image />.
890 * @author Stefan Saasen <s@fase4.com>
894 function get_array_image()
896 return $this->_array_image;
900 * XML Parser Data Handler
903 * @author Stefan Saasen <s@fase4.com>
904 * @param mixed $parser a reference to the XML parser calling the handler.
905 * @param string $text contains the character data as a string.
906 * @see _parse_mode, _item, _tags, _depth, _citem, _ctags, _cdepth
908 function _parseData($parser, $text)
910 // Deocing mode added by Roland Haeder <webmaster@mxchange.org>
911 switch ($this->_decoding_mode)
914 $text = utf8_decode($text);
918 $text = htmlentities($text);
922 $clean = preg_replace("/\s/", "", $text);
924 $text = preg_replace("/^\s+/", "", $text)."\n";
925 if ($this->_parse_mode == 'all') {
926 if (isset($this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]]) &&
927 $this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]]) {
928 $this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]] .= $text;
930 $this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]] = $text;
932 } elseif (isset($this->_parse_mode) && $this->_parse_mode == 'channel') {
933 if (isset($this->_citem[$this->_ctags[$this->_cdepth[$this->get_parser_id($parser)]]])) {
934 $this->_citem[$this->_ctags[$this->_cdepth[$this->get_parser_id($parser)]]] .= $text;
936 $this->_citem[$this->_ctags[$this->_cdepth[$this->get_parser_id($parser)]]] = $text;
943 * This Method allows you to choose if specific Parameters are displayed or not. These are:
944 * image, channel, textinput, build and cache_update. If set to "hidden" those elements won't be displayed.
947 * @author Stefan Saasen <s@fase4.com>
948 * @param array $options
951 function set_Options($options = '')
953 if (is_array($options)) {
954 $this->_display_opt = $options;
957 unset($this->_display_opt);
963 * This Method allows you to define the width of the table that holds the representation of the rdf/rss file.
966 * @author Stefan Saasen <s@fase4.com>
967 * @param int $width attribute width in tag <table>
970 function set_table_width($width = 400)
972 $this->_table_width = $width;
977 * This Method returns an assocative Array with available Options.
979 * The Keys are the Name of the Options to be set.
980 * The Values are short Description of available Options.
983 * @author Stefan Saasen <s@fase4.com>
984 * @return array $options
987 function get_Options() {
989 'image' => "If 'image' is set to \"hidden\" no image provided by the RDF Publisher will be displayed.",
990 'channel' => "If 'channel' is set to \"hidden\" the Channel Meta Data (i.e the Title and the short description regarding the RDF Publisher will not be displayed",
991 'textinput' => "If set to \"hidden\" no Input Form will be displayed",
992 'build' => "If set to \"hidden\" the Build Date (if provided) of the RDF File will not be displayed",
993 'cache_update' => "If set to \"hidden\" the Update Date/Time of the cached Rdf File will not be displayed"
999 * This Method returns the Content of the RDF File in one string. The String actually holds the whole XML Document.
1002 * @author Stefan Saasen <s@fase4.com>
1003 * @param string $rdf RDF File (Location)
1004 * @return string XML Presentation of parsed RDF File
1005 * @see _cached_file, _remote_file, _cache_dir, _refresh, _update_cache()
1009 // checks if the cache directory already exists
1010 // if not, the cache directory will be created
1011 if (!$this->_cache_dir_ok) {
1012 $this->_create_cache_dir();
1014 if ($this->_use_dynamic_display == TRUE) {
1015 $this->_cached_file = md5('dynamic'.$this->salt.$this->_remote_file) . '.cache';
1016 $this->_cache_type = 'normal';
1018 $this->_cached_file = md5($this->salt.$this->_remote_file) . '.cache';
1019 $this->_cache_type = 'fast';
1022 $_cache_f = $this->_cache_dir.$this->_cached_file;
1024 if ((!file_exists($_cache_f)) || (filemtime($_cache_f) < $this->_refresh) || (filesize($_cache_f) == 0)) {
1025 // We have to parse the remote file
1026 $this->_use_cached_file = FALSE;
1027 // --> we want to provide proper Information for Use in
1028 // get_cache_update_time()
1030 if ($this->_use_dynamic_display == TRUE) {
1031 $_rdf = implode(' ', $this->_rdf_data()); // -> proxy
1033 $this->_throw_exception($this->_remote_file.' is not available');
1035 $this->_parse_xRDF($_rdf);
1036 $this->_update_cache($_rdf);
1037 $data = $this->_output;
1039 $_rdf = implode(' ', $this->_rdf_data()); // -> proxy
1041 $this->_throw_exception($this->_remote_file.' is not available');
1043 $this->_parse_xRDF($_rdf);
1044 $this->_update_cache($this->_output);
1045 $data = $this->_output;
1047 } elseif (defined('__SECURITY') && function_exists('readFromFile')) {
1048 // Use readFromFile() from mailer project
1049 $this->_use_cached_file = TRUE;
1050 if ($this->_use_dynamic_display == TRUE) {
1051 $this->_parse_xRDF(readFromFile($_cache_f));
1052 $data = $this->_output;
1054 $data = readFromFile($_cache_f);
1057 // we can use the cached file
1058 $this->_use_cached_file = TRUE;
1059 if ($this->_use_dynamic_display == TRUE) {
1060 $this->_parse_xRDF(implode(' ', file($_cache_f)));
1061 $data = $this->_output;
1063 $data = implode(' ', file($_cache_f));
1070 * This Methods creates the Cache Directory if the specified Directory does not exist.
1073 * @author Stefan Saasen <s@fase4.com>
1074 * @param string $dir Path to Directory.
1076 * @see _cache_dir, _cache_dir_ok
1078 function _create_cache_dir()
1081 if (!is_dir($this->_cache_dir)) {
1082 $arr = explode('/', $this->_cache_dir);
1084 if ($arr[0] == '') {
1087 for($i = '0';$i<$c;$i++) {
1089 $path .= $arr[$i].'/';
1090 if (!is_dir($path)) {
1091 if (!mkdir($path, 0777)) {
1092 $this->_throw_exception("failed to create directory:<b>".$this->_cache_dir."</b>.<br /><br />Exception on Line: ".__LINE__);
1098 $this->_cache_dir_ok = TRUE;
1101 $this->_cache_dir_ok = TRUE;
1104 } // END _create_cache_dir()
1107 * This Method updates the cached RDF Files and synchronises them with their remote Counterparts.
1110 * @author Stefan Saasen <s@fase4.com>
1111 * @param string $rdf RDF File (Location)
1112 * @see _cache_dir, _cached_file, _throw_exception()
1114 function _update_cache($content = '')
1116 if (defined('__SECURITY') && function_exists('writeToFile')) {
1117 // Use mailer-project function
1118 return writeToFile($this->_cache_dir.$this->_cached_file, $content);
1120 $_local = @fopen($this->_cache_dir.$this->_cached_file, 'w');
1122 $this->_throw_exception('Cannot open '.$this->_cached_file.'<br /><br />Exception at Line: '.__LINE__);
1125 if (fwrite($_local, $content) === FALSE) {
1126 $this->_throw_exception('Cannot write to '.$this->_cached_file.'<br /<br />Exeception at Line: '.__LINE__);
1130 @chmod($this->_cache_dir.$this->_cached_file, 0666);
1132 } // END _update_cache()
1135 * This Method returns the Date/Time of last Cache Update of the actually parsed RDF File.
1138 * @author Stefan Saasen <s@fase4.com>
1139 * @return string Date/Time of last Update
1140 * @see _cache_dir, _cached_file
1142 function get_cache_update_time()
1144 return (file_exists($this->_cache_dir.$this->_cached_file))?date('d.m.Y H:i:s', filemtime($this->_cache_dir.$this->_cached_file)):'Cachemiss';
1145 } // END get_cache_update_time()
1148 * This Method returns the Type of Cache that was used ('normal' or 'fast')
1151 * @author Stefan Saasen <s@fase4.com>
1152 * @param string $rdf RDF File (Location)
1153 * @return string Displays RDF Content (using _display())
1154 * @see _remote_file, cache()
1156 function get_CacheType()
1158 return $this->_cache_type;
1162 * Returns true if cached file was used, otherwise false
1165 * @author Stefan Saasen <s@fase4.com>
1166 * @return array $options
1167 * @see _use_cached_file
1169 function is_cachedFile()
1171 return $this->_use_cached_file;
1175 * This Method deletes all the cached Files.
1177 * Please keep in mind to use this method just as a 'manual garbage collection'
1178 * You should cache the rss/rdf files locally to avoid unnecessary traffic.
1179 * (Both for your visitors and the Publisher)
1182 * @author Stefan Saasen <s@fase4.com>
1185 function clear_cache()
1187 $dir = dir($this->_cache_dir);
1188 while($file=$dir->read()) {
1189 // Exclude directories
1190 if (is_file($dir->path.$file) && substr($file, -6, 6) != '.cache' && substr($file, -4, 4) != '.log') {
1191 if ((defined('__SECURITY') && function_exists('removeFile')) && (!removeFile($dir->path.$file))) {
1192 $this->_throw_exception("removeFile() was unable to unlink ".$dir->path.$file."<br />\n<br />\nException at Line: ".__LINE__);
1194 } elseif (!unlink($dir->path.$file)) {
1195 $this->_throw_exception("Unable to unlink ".$dir->path.$file."<br />\n<br />\nException at Line: ".__LINE__);
1202 } // END clear_cache()
1205 * Cuts the String $string after $str_len and adds '... '
1208 * @param string $string String to be shortened
1209 * @param int $str_len length of the returned String (overall length including '... ')
1210 * @return string Cut String
1212 function _cut_string($string, $str_len = '30')
1214 if (strlen(trim($string))>$str_len) {
1215 $string = substr(trim($string) , 0, $str_len - 4);
1219 } // END _cut_string()
1222 * this Method implements simple Garbage Collection
1225 * @author Stefan Saasen <s@fase4.com>
1226 * @see _cache_dir, gc_probability, gc_maxlifetime
1228 function _garbage_collection()
1230 srand((double) microtime() * 1000000);
1231 if (mt_rand(1, 100) <= $this->gc_probability) {
1232 $dir = dir($this->_cache_dir);
1233 while($file=$dir->read()) {
1234 if (is_file($dir->path.$file) && substr($file, -6, 6) != '.cache' && substr($file, -4, 4) != '.log' && filemtime($dir->path.$file) <= time() - $this->_refresh) {
1235 @unlink($dir->path.$file);
1242 /* ==== Proxy/Auth methods ==== */
1245 * this method sets a proxy server
1248 * @param string $phost Proxy Host
1249 * @param string $pport Prox Port
1250 * @author Marco Kraus <marco.kraus@siemens.com>
1252 function set_proxy($phost, $pport)
1254 $this->_use_proxy = TRUE;
1257 $this->_phost = $phost;
1260 $this->_pport = $pport;
1264 * this method sets a proxy server authentification
1267 * @param string $pname Username
1268 * @param string $ppaswd Password
1269 * @author Marco Kraus <marco.kraus@siemens.com>
1271 function set_proxy_auth($pname, $ppasswd)
1273 $this->_use_proxy_auth = TRUE;
1276 $this->_pname = $pname;
1279 $this->_ppasswd = $ppasswd;
1284 * gets _remote_file into an array
1286 * needed, cause if you use a proxy, you have to open
1287 * a raw-tcp-socket to get the data
1290 * @author Marco Kraus <Marco.Kraus@siemens.com>
1292 * @see _use_proxy, cache()
1294 function _rdf_data()
1296 if (defined('__SECURITY') && function_exists('sendGetRequest')) {
1297 // Use mailer-project instead (see http://mxchange.org)
1298 $useable_data = sendGetRequest($this->_remote_file, array(), TRUE);
1299 } elseif ($this->_use_proxy == TRUE) {
1300 // we need a raw socket here to connect to proxy
1301 $fp = fsockopen($this->_phost,$this->_pport);
1304 $this->_throw_exception($this->_remote_file.' is not available with proxy');
1306 if ($this->_use_proxy_auth == TRUE) {
1307 fputs($fp, "GET ".$this->_remote_file." HTTP/1.0\r\nUser-Agent: Fase4 RDF-Reader/1.40 modified by Quix0r\r\n\r\n");
1309 fputs($fp, "GET ".$this->_remote_file." HTTP/1.0\r\nUser-Agent: Fase4 RDF-Reader/1.40 modified by Quix0r\r\nProxy-Authorization: Basic ".base64_encode("$this->_pname:$this->_ppasswd") ."\r\n\r\n");
1314 for ($i = '0'; !feof ($fp) ; $i++)
1316 $usable_data[$i] = "";
1317 $usable_data[$i] = fgets($fp,4096);
1319 // PARSE HEADER ---- first line has to be <?xml, second rdf or rss, and third is blank
1321 // strstr did not fit (ask Rasmus why), so we compare each character
1322 if (($usable_data[$i][0] == '<') &&
1323 ($usable_data[$i][1] == '?') &&
1324 ($usable_data[$i][2] == 'x') &&
1325 ($usable_data[$i][3] == 'm') &&
1326 ($usable_data[$i][4] == 'l')) {
1327 $usable_data[0] = $usable_data[$i]; // save current field
1328 $i = 1; // just reset array to start
1331 // there seems to be proxystuff after the <?xml....we delete this
1333 ($usable_data[$i][0] == '<') &&
1334 ($usable_data[$i][1] == 'r') &&
1335 ($usable_data[$i][2] == 'd') &&
1336 ($usable_data[$i][3] == 'f') &&
1337 ($usable_data[$i][4] == ':')
1341 ($usable_data[$i][0] == '<') &&
1342 ($usable_data[$i][1] == 'r') &&
1343 ($usable_data[$i][2] == 's') &&
1344 ($usable_data[$i][3] == 's')
1348 $usable_data[1] = $usable_data[$i]; // save current field
1349 $usable_data[2] = "\n";
1350 $i = 2; // just reset array to start
1355 return $usable_data;
1357 if (substr($this->_remote_file, 0, 7) != 'http://') {
1358 $this->_throw_exception('Cannot find http:// in '.$this->_remote_file.'!');
1361 // Extract host information
1362 $host = substr($this->_remote_file, 7);
1363 // Extract the GET part
1365 if (strpos($host, '/') > 0) {
1366 $get = substr($host, strpos($host, '/'));
1367 $host = substr($host, 0, strpos($host, '/'));
1371 if (strpos($host, ':') > 0) {
1372 $port = substr($host, (strpos($host, ':') + 1));
1373 $host = substr($host, 0, (strpos($host, ':') - 1));
1376 // Start connection to server
1377 $fp = fsockopen($host, $port);
1379 $this->_throw_exception($this->_remote_file.' is maybe not available.');
1382 // Repare request line
1383 $request = sprintf("GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Fase4 RDF-Reader/1.40 modified by Quix0r\r\n\r\n", $get, $host);
1385 fputs($fp, $request);
1386 $reply = ''; $isContent = FALSE; $dummy = '';
1389 while (!feof($fp)) {
1390 $read = trim(fgets($fp, 4096));
1391 if (substr($read, 0, 5) == '<?xml' || $isContent) {
1401 if ((count($dummy) > 0) && (count($reply) == 0) && (!$isContent)) {
1402 // Transfer content from dummy
1406 //die(htmlentities($reply));
1410 } // END _rdf_data()