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 * +----------------------------------------------------------------------+
38 * This class offers methods to parse RSS Files
40 * @link http://www.fase4.com/rdf/ Latest release of this class
42 * @copyright Copyright (c) 2001 fase4.com. All rights reserved.
43 * @author Stefan Saasen <s@fase4.com>
44 * @author Roland Haeder <webmaster@mxchange.org>
45 * @version 1.7 ($Date$Revision: 856 $
52 * Word-wrapping mode for description, set it to 0 do disable this feature! Ommits _use_nl2br!
57 var $_word_wrap = '0';
60 * Wether to recode \n -> <br /> or not in description
65 var $_use_nl2br = TRUE;
68 * Sets the decoding mode of the read data (UTF8 scrambles some german umlauts here!)
70 * "htmlentities" - Use the function htmlentities()
71 * "utf8_decode" - Use the function ut8_decode() when you have UTF8 encoded text
72 * <empty> - Use non of both
77 var $_decoding_mode = '';
80 * If $_link_target is set a target='xxx' attribute in each <a /> and <form accept-charset="utf-8" /> html tag will be added
82 * Possible values are "_blank", "_content", "_parent", "_self", "_top"
87 var $_link_target = '_blank';
90 * vars for proxy settings - Prox Host
98 * vars for proxy settings - Prox Port
106 * vars for proxy settings - Prox Username
114 * vars for proxy settings - Prox Password
122 * just a flag for checking if proxy-support should be enabled
123 * set default to false (will be enabled if set_proxy is called)
129 var $_use_proxy = FALSE;
132 * just a flag for checking if proxy-support with authentication
134 * set default to false (will be enabled if set_proxy is called)
140 var $_use_proxy_auth = FALSE;
143 * The time the Files will be cached (in seconds).
148 var $_refresh = 60; // int
151 * The Name of the cached File.
156 var $_cached_file = ''; // String
159 * Indicates whether the cached or the remote file was used.
164 var $_use_cached_file = TRUE;
167 * (fast|normal) depends on _use_dynamic_display(). _use_dynamic_display(TRUE) -> 'normal', otherwise 'fast'
172 var $_cache_type = 'fast';
175 * The Name of the Remote File.
180 var $_remote_file = '';
183 * Path to the Cache Directory.
188 var $_cache_dir = 'cache/'; // String
191 * Indicates whether the Creating of the Cache Directory needs to be done or not.
196 var $_cache_dir_ok = FALSE;
199 * Type of the file to be parsed (RSS or RDF).
201 * The Type depends on the root node
206 var $_type = 'rss'; // string (rss or rdf)
209 * Array of Display Settings.
211 * Specific Parameters can be set to hidden. These are:
212 * image, channel and textinput. If set to "hidden" those elements won't be displayed.
217 var $_display_opt = array(
222 'cache_update' => '',
229 * Defines the width attribute in the table that holds the rdf/rss representation
233 * @see see_table_width()
235 var $_table_width = '100%';
238 * Indicates whether or not to use dynamic Display Settings
243 var $_use_dynamic_display = FALSE;
251 var $_item_count = '0';
259 var $_max_count = FALSE;
262 * Array containing the content of <channel />
267 var $_array_channel = array();
270 * Array containing the content of each <item />
275 var $_array_item = array();
278 * Array containing the content of <textinput />
283 var $_array_textinput = array();
286 * Array containing the content of <image />
291 var $_array_image = array();
294 * Array containing the Channel content. Just For internal XML Parser Purposes.
299 var $_citem = array();
302 * Array containing the Channel Parser Depth. Just For internal XML Parser Purposes.
307 var $_cdepth = array();
310 * Array containing the Channel tags. Just For internal XML Parser Purposes.
315 var $_ctags = array('x');
318 * Array containing the Channel content. Just For internal XML Parser Purposes.
323 var $_item = array(); // Array
326 * Array containing the Channel Parser Depth. Just For internal XML Parser Purposes.
331 var $_depth = array(); // Array
334 * Array containing the tags. Just For internal XML Parser Purposes.
339 var $_tags = array('x'); // Array
342 * Garbage collection: probability in percent
344 * @var integer 0 => never
347 var $gc_probability = 1;
360 var $_parse_mode = '';
369 * Constructor of our Class
371 * This Method checks if the Cache Directory can be found. Otherwise it tries to creat the Cache Directory at the specified Path.
372 * Additionally the Refresh Time is set to a default Value of 1200s (20 min).
375 * @author Stefan Saasen <s@fase4.com>
380 // default Value, to be overwritten in set_refresh()
381 $this->_refresh = (time() - 1200);
382 $this->_clear_cItems();
383 $this->_clear_Items();
387 * This Method starts the parsing of the specified RDF File. The File can be a local or a remote File.
390 * @author Stefan Saasen <s@fase4.com>
391 * @param string $rdf RDF File (Location)
392 * @return string Displays RDF Content (using _display())
393 * @see _remote_file, cache()
395 function parse_RDF($rdf)
397 unset($this->_array_item);
398 $this->_remote_file = $rdf;
399 $this->out .= "<!-- http://www.fase4.com/rdf/ -->";
400 $this->out .= "<table width=\"".$this->_table_width."\">";
401 $this->out .= $this->cache();
402 $this->out .= "</table>";
404 $this->_item_count = '0';
409 * This Method is called when all parsing is finished to use the garbage collection
412 * @author Stefan Saasen <s@fase4.com>
413 * @param string $rdf RDF File (Location)
414 * @return string Displays RDF Content (using _display())
415 * @see _remote_file, cache()
417 function finish($return = FALSE) {
418 $this->out = str_replace('$', '$', $this->out);
425 $this->_garbage_collection();
429 * With this method you can decide whether to use the normal cache and dynamic display Options or to use a static cache.
431 * 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.
432 * In this case you can not modify the display settings.
433 * processing time: (1.4792) --> remote file
434 * processing time: (0.0313) --> using 'normal cache' with display Modification turned on.
435 * processing time: (0.0019) --> using 'fast cache'
438 * @author Stefan Saasen <s@fase4.com>
439 * @param string $rdf RDF File (Location)
440 * @return string Displays RDF Content (using _display())
441 * @see _remote_file, cache()
443 function use_dynamic_display($bool) {
444 $this->_use_dynamic_display = $bool;
449 * This Method avtually parses the XML data.
452 * @author Stefan Saasen <s@fase4.com>
453 * @param string $data RDF File XML Data
454 * @see _clear_Items()
456 function _parse_xRDF($data) {
457 $this->_clear_Items();
458 $xml_parser = xml_parser_create();
459 xml_set_object($xml_parser,$this);
460 xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
461 xml_set_element_handler($xml_parser, '_startElement', '_endElement');
462 xml_set_character_data_handler($xml_parser, '_parseData');
463 if (!xml_parse($xml_parser, trim($data))) {
464 $this->_throw_exception(sprintf("XML error: %s at line %d",
465 xml_error_string(xml_get_error_code($xml_parser)),
466 xml_get_current_line_number($xml_parser)).'<br /><br />Exception in function parse_RDF().');
468 xml_parser_free($xml_parser);
473 * This Methods allows you to set the Refresh Time
476 * @author Stefan Saasen <s@fase4.com>
477 * @param int $seconds time files will be cached (in seconds).
481 function set_refresh($seconds) {
482 $this->_refresh = (time() - $seconds);
486 function set_salt($saltPara) {
487 $this->salt = $saltPara;
492 * This Methods allows you to set the No. of <item>s to display
495 * @param int $int No of max <item>s
496 * @author Stefan Saasen <s@fase4.com>
498 * @see _max_count, _endElement()
500 function set_max_item($int)
502 $this->_max_count = $int;
507 * This Methods allows you to set the Cache Directory
510 * @author Stefan Saasen <s@fase4.com>
511 * @param string $dir Path to Directory.
515 function set_CacheDir($dir)
517 if (substr($dir, -1) != '/') {
520 $this->_cache_dir = $dir;
524 * This Method displays Error Messages and terminates the Execution of the Script
527 * @param string $msg Message to display on failure
528 * @author Stefan Saasen <s@fase4.com>
530 function _throw_exception($msg)
532 $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>";
537 * This Method clears the Array containig the Items.
540 * @author Stefan Saasen <s@fase4.com>
543 function _clear_Items() {
544 $this->_item = array(
551 'lastBuildDate' => '',
557 * This Method clears the Array containig the Channel Items.
560 * @author Stefan Saasen <s@fase4.com>
563 function _clear_cItems() {
564 $this->_citem = array(
571 'managingEditor' => '',
574 'lastBuildDate' => '',
591 * XML Parser Start Element Handler
594 * @author Stefan Saasen <s@fase4.com>
595 * @param mixed $parser a reference to the XML parser calling the handler.
596 * @param string $name contains the name of the element for which this handler is called.
597 * @param string $attrs contains an associative array with the element's attributes (if any).
598 * @see _get_ChannelData(), _clear_Items(), _type, _parse_mode, _depth, _tags, _cdepth, _ctags
600 function _startElement($parser, $name, $attrs) {
601 // We have to determine, which type of xml data we have to parse
602 if ($name == 'rss') {
603 $this->_type = 'rss';
604 } elseif ($name == 'rdf:RDF' || $name == 'rdf') {
605 $this->_type = 'rdf';
609 if ($name == 'channel' && $this->_type != 'rdf') {
610 $this->_parse_mode = 'channel';
611 } elseif (($name == 'item')
612 || ($name == 'image')
613 || ($name == 'textinput')
614 || (($name == 'channel') && ($this->_type != 'rss'))) {
615 if ($this->_parse_mode == 'channel') {
616 $this->_get_ChannelData($parser);
618 $this->_parse_mode = 'all';
621 if (!isset($this->_depth[$this->get_parser_id($parser)])) {
622 $this->_depth[$this->get_parser_id($parser)] = '0';
624 $this->_depth[$this->get_parser_id($parser)]++;
625 array_push($this->_tags, $name);
627 if (!isset($this->_cdepth[$this->get_parser_id($parser)])) {
628 $this->_cdepth[$this->get_parser_id($parser)] = '0';
630 $this->_cdepth[$this->get_parser_id($parser)]++;
631 array_push($this->_ctags, $name);
632 } // END _startElement()
635 * Retrives the Channel Data in <rss> File
638 * @author Stefan Saasen <s@fase4.com>
639 * @param mixed $parser a reference to the XML parser calling the handler.
640 * @see _output, _display_opt, _citem
642 function _get_ChannelData($parser)
644 $this->_citem['link'] = trim($this->_citem['link']);
645 if (($this->_display_opt['sitelink'] == $this->_citem['link']) && (!empty($this->_display_opt['reflink'])) && (!empty($this->_display_opt['refid'])))
647 $this->_citem['link'] .= $this->_display_opt['reflink'].$this->_display_opt['refid'];
650 if (empty($this->_display_opt['channel']) ||
651 $this->_display_opt['channel'] != 'hidden') {
652 $this->_output .= "<tr><td>\n";
653 $this->_output .= "<table border=\"0\" width=\"100%\" class=\"fase4_rdf_meta\" cellspacing=\"5\" cellpadding=\"2\">\n";
654 $this->_output .= "<tr><td class=\"fase4_rdf_main_title\"><div class=\"fase4_rdf_main_title\">".htmlspecialchars($this->_citem['title'])."</div></td></tr>\n";
655 $this->_output .= "<tr><td class=\"fase4_rdf\">".strip_tags($this->_citem['description'], '<a>, <img>')."</td></tr>\n";
656 $this->_output .= "<tr><td> </td></tr>\n";
657 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
658 if (isset($this->_display_opt['build']) && $this->_display_opt['build'] != 'hidden') {
659 if ($this->_citem['lastBuildDate']) { $this->_output .= 'build: '. $this->_citem['lastBuildDate'].'<br />';}
661 if (isset($this->_display_opt['cache_update']) && $this->_display_opt['cache_update'] != 'hidden' && ($_update = $this->get_cache_update_time())) {
662 $this->_output .= 'cache update: '.$_update."<br />\n";
664 $this->_output .= "<a href=\"".$this->_citem['link']."\" ";
665 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
666 $this->_output .= ">".$this->_cut_string($this->_citem['link']) . '</a>';
667 $this->_output .= "</td></tr>\n";
668 $this->_output .= "<tr><td><hr noshade width=\"100%\" size=\"1\"></td></tr>\n";
669 $this->_output .= "</table></td></tr>";
671 $this->_array_channel = array(
672 'title' => $this->_citem['title'],
673 'link' => $this->_citem['link'],
674 'description' => $this->_citem['description'],
675 'lastBuildDate' => $this->_citem['lastBuildDate']);
679 * XML Parser End Element Handler
682 * @author Stefan Saasen <s@fase4.com>
683 * @param mixed $parser a reference to the XML parser calling the handler.
684 * @param string $name contains the name of the element for which this handler is called.
685 * @see _clear_Items(), _type, _parse_mode, _depth, _tags, _cdepth, _ctags, _item, _output, _display_opt
687 function _endElement($parser, $name) {
688 array_pop($this->_tags);
689 $this->_depth[$this->get_parser_id($parser)]--;
690 array_pop($this->_ctags);
691 $this->_cdepth[$this->get_parser_id($parser)]--;
692 $this->_item['link'] = trim($this->_item['link']);
693 if ((!empty($this->_display_opt['refid'])) && (!empty($this->_item['link'])))
695 if (!isInString('refid=', $this->_item['link'])) $this->_item['link'] .= '?refid=' . $this->_display_opt['refid'];
699 if (empty($this->_max_count) || $this->_item_count < $this->_max_count) {
700 if ($this->_item['title'] != $this->_item['description']
701 && $this->_item['description']) {
703 // word-wrapping added by Roland Haeder <webmaster@mxchange.org>
704 if (($this->_word_wrap > 0) && (strlen($this->_item['description']) > $this->_word_wrap))
706 // Switch off _use_nl2br
707 $this->_use_nl2br = FALSE;
708 // First remove all \n
709 $this->_item['description'] = str_replace('\n', ' ', $this->_item['description']);
710 // Wrap with <br />\n
711 $this->_item['description'] = wordwrap($this->_item['description'], $this->_word_wrap, "*<br />\n");
713 elseif (($this->_word_wrap == '0') && (!$this->_use_nl2br))
715 // Strip tags out instead when word-wrap is disabled
716 $this->_item['description'] = strip_tags($this->_item['description'], '<a>, <img>');
719 // nl2br added by Roland Haeder <webmaster@mxchange.org>
720 if ($this->_use_nl2br) $this->_item['description'] = nl2br($this->_item['description']);
722 $this->_output .= "<tr><td class=\"fase4_rdf_title\"><div class=\"fase4_rdf_title\"><a class=\"fase4_rdf_title\" href=\"".$this->_item['link']."\" ";
723 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
724 $this->_output .= ">".strip_tags($this->_item['title'], '<a>, <img>').'</a> ('.$this->_item['pubDate'].")</div></td></tr>\n";
725 $this->_output .= "<tr><td class=\"fase4_rdf\">".$this->_item['description']."</td></tr>\n";
726 // we just display the <hr> if there is a description
727 $this->_output .= "<tr><td><hr noshade=\"noshade\" size=\"1\" /></td></tr>\n";
729 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
730 $this->_output .= "<a href=\"".$this->_item["link"]."\" ";
731 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
732 $this->_output .= ">".$this->_item["title"]."</a></td></tr>\n";
735 $this->_array_item[] = array(
736 'title' => $this->_item['title'],
737 'link' => $this->_item['link'],
738 'description' => $this->_item['description']
741 ++$this->_item_count;
743 $this->_clear_Items();
747 if (isset($this->_display_opt['image']) && ($this->_display_opt['image'] != 'hidden') && $this->_item['url']) {
748 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
749 $this->_output .= "<a href=\"".$this->_item['link']."\" ";
750 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
751 $this->_output .= "><img src=\"".$this->_item['url']."\"";
752 if (isset($this->_item['width']) && isset($this->_item['height'])) {
753 $this->_output .= " width=\"".$this->_item['width']."\" height=\"".$this->_item['height']."\"";
755 $this->_output .= " alt=\"".$this->_item['title']."\" border=\"0\" /></a></td></tr>\n";
757 $this->_array_image[] = array(
758 'url' => $this->_item['url'],
759 'link' => $this->_item['link'],
760 'width' => $this->_item['width'],
761 'height' => $this->_item['height']
763 $this->_clear_Items();
764 } elseif (isset($this->_display_opt['image']) && ($this->_display_opt['image'] == 'hidden')) {
765 $this->_clear_Items();
771 if (isset($this->_display_opt['channel']) && $this->_display_opt['channel'] != 'hidden' && $this->_item['title'] != '') {
772 $this->_output .= "<tr><td>\n";
773 $this->_output .= "<table border=\"0\" width=\"100%\" class=\"fase4_rdf_meta\" cellspacing=\"5\" cellpadding=\"2\">\n";
774 $this->_output .= "<tr><td class=\"fase4_rdf\"><div class=\"fase4_rdf_title\">".htmlspecialchars($this->_item['title'])."</div></td></tr>\n";
775 $this->_output .= "<tr><td class=\"fase4_rdf\">".strip_tags($this->_item['description'], '<a>, <img>')."</td></tr>\n";
776 $this->_output .= "<tr><td> </td></tr>\n";
777 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
778 if ($this->_display_opt['build'] != 'hidden') {
779 if ($this->_item['lastBuildDate']) { $this->_output .= 'build: '. $this->_item['lastBuildDate'].'<br />';}
781 if ($this->_display_opt['cache_update'] != 'hidden' && ($_update = $this->get_cache_update_time())) {
782 $this->_output .= 'cache update: '.$_update."<br />\n";
784 $this->_output .= "<a href=\"".$this->_item['link']."\" ";
785 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
786 $this->_output .= ">".$this->_cut_string($this->_item['link'])."</a>\n";
787 $this->_output .= "</td></tr>\n";
788 $this->_output .= "</table></td></tr>\n";
790 $this->_array_channel = array(
791 'title' => $this->_item['title'],
792 'link' => $this->_item['link'],
793 'description' => $this->_item['description'],
794 'lastBuildDate' => $this->_item['lastBuildDate']
796 $this->_clear_Items();
797 $this->_clear_cItems();
801 if (isset($this->_display_opt['textinput']) && ($this->_display_opt['textinput'] != 'hidden') && $this->_item['name'] && $this->_item['link']) {
802 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
803 $this->_output .= "<form accept-charset=\"UTF-8\" action=\"".$this->_item['link']."\" ";
804 if (isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
805 $this->_output .= "method=\"get\">\n";
806 $this->_output .= "<div class=\"fase4_rdf_title\">".$this->_item['title']."</div>";
807 $this->_output .= strip_tags($this->_item['description'], '<a>, <img>')."<br /><br />\n";
808 $this->_output .= "<input class=\"fase4_rdf_input\" type=\"text\" name=\"".$this->_item['name']."\"> \n";
809 $this->_output .= "<input class=\"fase4_rdf_input\" type=\"submit\" value=\"go\">";
810 $this->_output .= "</form>\n";
811 $this->_output .= "</td></tr>\n";
812 $this->_array_textinput = array(
813 'title' => $this->_item['title'],
814 'name' => $this->_item['name'],
815 'link' => $this->_item['link'],
816 'description' => $this->_item['description']
818 $this->_clear_Items();
819 } elseif (isset($this->_display_opt['textinput']) && ($this->_display_opt['textinput'] == 'hidden')) {
820 $this->_clear_Items();
828 * This Method returns the data from the <channel /> paragraph.
831 * @author Stefan Saasen <s@fase4.com>
833 * @see _array_channel
835 function get_array_channel()
837 return $this->_array_channel;
841 * This Method returns the data from each <item /> paragraph.
844 * @author Stefan Saasen <s@fase4.com>
848 function get_array_item()
850 return $this->_array_item;
854 * This Method returns the data from <textinput />.
857 * @author Stefan Saasen <s@fase4.com>
859 * @see _array_textinput
861 function get_array_textinput()
863 return $this->_array_textinput;
867 * Getter for parser id from resource
870 * @author Roland Haeder <webmaster@mxchange.org>
873 function get_parser_id ($parser) {
878 if (is_resource($parser)) {
879 // Cast the resource into id
888 * This Method returns the data from <image />.
891 * @author Stefan Saasen <s@fase4.com>
895 function get_array_image()
897 return $this->_array_image;
901 * XML Parser Data Handler
904 * @author Stefan Saasen <s@fase4.com>
905 * @param mixed $parser a reference to the XML parser calling the handler.
906 * @param string $text contains the character data as a string.
907 * @see _parse_mode, _item, _tags, _depth, _citem, _ctags, _cdepth
909 function _parseData($parser, $text)
911 // Deocing mode added by Roland Haeder <webmaster@mxchange.org>
912 switch ($this->_decoding_mode)
915 $text = utf8_decode($text);
919 $text = htmlentities($text);
923 $clean = preg_replace("/\s/", "", $text);
925 $text = preg_replace("/^\s+/", "", $text)."\n";
926 if ($this->_parse_mode == 'all') {
927 if (isset($this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]]) &&
928 $this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]]) {
929 $this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]] .= $text;
931 $this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]] = $text;
933 } elseif (isset($this->_parse_mode) && $this->_parse_mode == 'channel') {
934 if (isset($this->_citem[$this->_ctags[$this->_cdepth[$this->get_parser_id($parser)]]])) {
935 $this->_citem[$this->_ctags[$this->_cdepth[$this->get_parser_id($parser)]]] .= $text;
937 $this->_citem[$this->_ctags[$this->_cdepth[$this->get_parser_id($parser)]]] = $text;
944 * This Method allows you to choose if specific Parameters are displayed or not. These are:
945 * image, channel, textinput, build and cache_update. If set to "hidden" those elements won't be displayed.
948 * @author Stefan Saasen <s@fase4.com>
949 * @param array $options
952 function set_Options($options = '')
954 if (is_array($options)) {
955 $this->_display_opt = $options;
958 unset($this->_display_opt);
964 * This Method allows you to define the width of the table that holds the representation of the rdf/rss file.
967 * @author Stefan Saasen <s@fase4.com>
968 * @param int $width attribute width in tag <table>
971 function set_table_width($width = 400)
973 $this->_table_width = $width;
978 * This Method returns an assocative Array with available Options.
980 * The Keys are the Name of the Options to be set.
981 * The Values are short Description of available Options.
984 * @author Stefan Saasen <s@fase4.com>
985 * @return array $options
988 function get_Options() {
990 'image' => "If 'image' is set to \"hidden\" no image provided by the RDF Publisher will be displayed.",
991 '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",
992 'textinput' => "If set to \"hidden\" no Input Form will be displayed",
993 'build' => "If set to \"hidden\" the Build Date (if provided) of the RDF File will not be displayed",
994 'cache_update' => "If set to \"hidden\" the Update Date/Time of the cached Rdf File will not be displayed"
1000 * This Method returns the Content of the RDF File in one string. The String actually holds the whole XML Document.
1003 * @author Stefan Saasen <s@fase4.com>
1004 * @param string $rdf RDF File (Location)
1005 * @return string XML Presentation of parsed RDF File
1006 * @see _cached_file, _remote_file, _cache_dir, _refresh, _update_cache()
1010 // checks if the cache directory already exists
1011 // if not, the cache directory will be created
1012 if (!$this->_cache_dir_ok) {
1013 $this->_create_cache_dir();
1015 if ($this->_use_dynamic_display == TRUE) {
1016 $this->_cached_file = md5('dynamic'.$this->salt.$this->_remote_file) . '.cache';
1017 $this->_cache_type = 'normal';
1019 $this->_cached_file = md5($this->salt.$this->_remote_file) . '.cache';
1020 $this->_cache_type = 'fast';
1023 $_cache_f = $this->_cache_dir.$this->_cached_file;
1025 if ((!file_exists($_cache_f)) || (filemtime($_cache_f) < $this->_refresh) || (filesize($_cache_f) == 0)) {
1026 // We have to parse the remote file
1027 $this->_use_cached_file = FALSE;
1028 // --> we want to provide proper Information for Use in
1029 // get_cache_update_time()
1031 if ($this->_use_dynamic_display == TRUE) {
1032 $_rdf = implode(' ', $this->_rdf_data()); // -> proxy
1034 $this->_throw_exception($this->_remote_file.' is not available');
1036 $this->_parse_xRDF($_rdf);
1037 $this->_update_cache($_rdf);
1038 $data = $this->_output;
1040 $_rdf = implode(' ', $this->_rdf_data()); // -> proxy
1042 $this->_throw_exception($this->_remote_file.' is not available');
1044 $this->_parse_xRDF($_rdf);
1045 $this->_update_cache($this->_output);
1046 $data = $this->_output;
1048 } elseif (defined('__SECURITY') && function_exists('readFromFile')) {
1049 // Use readFromFile() from mailer project
1050 $this->_use_cached_file = TRUE;
1051 if ($this->_use_dynamic_display == TRUE) {
1052 $this->_parse_xRDF(readFromFile($_cache_f));
1053 $data = $this->_output;
1055 $data = readFromFile($_cache_f);
1058 // we can use the cached file
1059 $this->_use_cached_file = TRUE;
1060 if ($this->_use_dynamic_display == TRUE) {
1061 $this->_parse_xRDF(implode(' ', file($_cache_f)));
1062 $data = $this->_output;
1064 $data = implode(' ', file($_cache_f));
1071 * This Methods creates the Cache Directory if the specified Directory does not exist.
1074 * @author Stefan Saasen <s@fase4.com>
1075 * @param string $dir Path to Directory.
1077 * @see _cache_dir, _cache_dir_ok
1079 function _create_cache_dir()
1082 if (!is_dir($this->_cache_dir)) {
1083 $arr = explode('/', $this->_cache_dir);
1085 if ($arr[0] == '') {
1088 for($i = '0';$i<$c;$i++) {
1090 $path .= $arr[$i].'/';
1091 if (!is_dir($path)) {
1092 if (!mkdir($path, 0777)) {
1093 $this->_throw_exception("failed to create directory:<b>".$this->_cache_dir."</b>.<br /><br />Exception on Line: ".__LINE__);
1099 $this->_cache_dir_ok = TRUE;
1102 $this->_cache_dir_ok = TRUE;
1105 } // END _create_cache_dir()
1108 * This Method updates the cached RDF Files and synchronises them with their remote Counterparts.
1111 * @author Stefan Saasen <s@fase4.com>
1112 * @param string $rdf RDF File (Location)
1113 * @see _cache_dir, _cached_file, _throw_exception()
1115 function _update_cache($content = '')
1117 if (defined('__SECURITY') && function_exists('writeToFile')) {
1118 // Use mailer-project function
1119 return writeToFile($this->_cache_dir.$this->_cached_file, $content);
1121 $_local = @fopen($this->_cache_dir.$this->_cached_file, 'w');
1123 $this->_throw_exception('Cannot open '.$this->_cached_file.'<br /><br />Exception at Line: '.__LINE__);
1126 if (fwrite($_local, $content) === FALSE) {
1127 $this->_throw_exception('Cannot write to '.$this->_cached_file.'<br /<br />Exeception at Line: '.__LINE__);
1131 @chmod($this->_cache_dir.$this->_cached_file, 0666);
1133 } // END _update_cache()
1136 * This Method returns the Date/Time of last Cache Update of the actually parsed RDF File.
1139 * @author Stefan Saasen <s@fase4.com>
1140 * @return string Date/Time of last Update
1141 * @see _cache_dir, _cached_file
1143 function get_cache_update_time()
1145 return (file_exists($this->_cache_dir.$this->_cached_file))?date('d.m.Y H:i:s', filemtime($this->_cache_dir.$this->_cached_file)):'Cachemiss';
1146 } // END get_cache_update_time()
1149 * This Method returns the Type of Cache that was used ('normal' or 'fast')
1152 * @author Stefan Saasen <s@fase4.com>
1153 * @param string $rdf RDF File (Location)
1154 * @return string Displays RDF Content (using _display())
1155 * @see _remote_file, cache()
1157 function get_CacheType()
1159 return $this->_cache_type;
1163 * Returns true if cached file was used, otherwise false
1166 * @author Stefan Saasen <s@fase4.com>
1167 * @return array $options
1168 * @see _use_cached_file
1170 function is_cachedFile()
1172 return $this->_use_cached_file;
1176 * This Method deletes all the cached Files.
1178 * Please keep in mind to use this method just as a 'manual garbage collection'
1179 * You should cache the rss/rdf files locally to avoid unnecessary traffic.
1180 * (Both for your visitors and the Publisher)
1183 * @author Stefan Saasen <s@fase4.com>
1186 function clear_cache()
1188 $dir = dir($this->_cache_dir);
1189 while($file=$dir->read()) {
1190 // Exclude directories
1191 if (is_file($dir->path.$file) && substr($file, -6, 6) != '.cache' && substr($file, -4, 4) != '.log') {
1192 if ((defined('__SECURITY') && function_exists('removeFile')) && (!removeFile($dir->path.$file))) {
1193 $this->_throw_exception("removeFile() was unable to unlink ".$dir->path.$file."<br />\n<br />\nException at Line: ".__LINE__);
1195 } elseif (!unlink($dir->path.$file)) {
1196 $this->_throw_exception("Unable to unlink ".$dir->path.$file."<br />\n<br />\nException at Line: ".__LINE__);
1203 } // END clear_cache()
1206 * Cuts the String $string after $str_len and adds '... '
1209 * @param string $string String to be shortened
1210 * @param int $str_len length of the returned String (overall length including '... ')
1211 * @return string Cut String
1213 function _cut_string($string, $str_len = '30')
1215 if (strlen(trim($string))>$str_len) {
1216 $string = substr(trim($string) , 0, $str_len - 4);
1220 } // END _cut_string()
1223 * this Method implements simple Garbage Collection
1226 * @author Stefan Saasen <s@fase4.com>
1227 * @see _cache_dir, gc_probability, gc_maxlifetime
1229 function _garbage_collection()
1231 srand((double) microtime() * 1000000);
1232 if (mt_rand(1, 100) <= $this->gc_probability) {
1233 $dir = dir($this->_cache_dir);
1234 while($file=$dir->read()) {
1235 if (is_file($dir->path.$file) && substr($file, -6, 6) != '.cache' && substr($file, -4, 4) != '.log' && filemtime($dir->path.$file) <= time() - $this->_refresh) {
1236 @unlink($dir->path.$file);
1243 /* ==== Proxy/Auth methods ==== */
1246 * this method sets a proxy server
1249 * @param string $phost Proxy Host
1250 * @param string $pport Prox Port
1251 * @author Marco Kraus <marco.kraus@siemens.com>
1253 function set_proxy($phost, $pport)
1255 $this->_use_proxy = TRUE;
1258 $this->_phost = $phost;
1261 $this->_pport = $pport;
1265 * this method sets a proxy server authentification
1268 * @param string $pname Username
1269 * @param string $ppaswd Password
1270 * @author Marco Kraus <marco.kraus@siemens.com>
1272 function set_proxy_auth($pname, $ppasswd)
1274 $this->_use_proxy_auth = TRUE;
1277 $this->_pname = $pname;
1280 $this->_ppasswd = $ppasswd;
1285 * gets _remote_file into an array
1287 * needed, cause if you use a proxy, you have to open
1288 * a raw-tcp-socket to get the data
1291 * @author Marco Kraus <Marco.Kraus@siemens.com>
1293 * @see _use_proxy, cache()
1295 function _rdf_data()
1297 if (defined('__SECURITY') && function_exists('sendHttpGetRequest')) {
1298 // Use mailer-project instead (see http://mxchange.org)
1299 return sendHttpGetRequest($this->_remote_file, array(), TRUE);
1300 } elseif ($this->_use_proxy == TRUE) {
1301 // we need a raw socket here to connect to proxy
1302 $fp = fsockopen($this->_phost,$this->_pport);
1305 $this->_throw_exception($this->_remote_file.' is not available with proxy');
1307 if ($this->_use_proxy_auth == TRUE) {
1308 fputs($fp, "GET ".$this->_remote_file." HTTP/1.0\r\nUser-Agent: Fase4 RDF-Reader/1.40 modified by Quix0r\r\n\r\n");
1310 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");
1315 for ($i = '0'; !feof ($fp) ; $i++)
1317 $usable_data[$i] = "";
1318 $usable_data[$i] = fgets($fp,4096);
1320 // PARSE HEADER ---- first line has to be <?xml, second rdf or rss, and third is blank
1322 // strstr did not fit (ask Rasmus why), so we compare each character
1323 if (($usable_data[$i][0] == '<') &&
1324 ($usable_data[$i][1] == '?') &&
1325 ($usable_data[$i][2] == 'x') &&
1326 ($usable_data[$i][3] == 'm') &&
1327 ($usable_data[$i][4] == 'l')) {
1328 $usable_data[0] = $usable_data[$i]; // save current field
1329 $i = 1; // just reset array to start
1332 // there seems to be proxystuff after the <?xml....we delete this
1334 ($usable_data[$i][0] == '<') &&
1335 ($usable_data[$i][1] == 'r') &&
1336 ($usable_data[$i][2] == 'd') &&
1337 ($usable_data[$i][3] == 'f') &&
1338 ($usable_data[$i][4] == ':')
1342 ($usable_data[$i][0] == '<') &&
1343 ($usable_data[$i][1] == 'r') &&
1344 ($usable_data[$i][2] == 's') &&
1345 ($usable_data[$i][3] == 's')
1349 $usable_data[1] = $usable_data[$i]; // save current field
1350 $usable_data[2] = "\n";
1351 $i = 2; // just reset array to start
1356 return $usable_data;
1358 if (substr($this->_remote_file, 0, 7) != 'http://') {
1359 $this->_throw_exception('Cannot find http:// in '.$this->_remote_file.'!');
1362 // Extract host information
1363 $host = substr($this->_remote_file, 7);
1364 // Extract the GET part
1366 if (strpos($host, '/') > 0) {
1367 $get = substr($host, strpos($host, '/'));
1368 $host = substr($host, 0, strpos($host, '/'));
1372 if (strpos($host, ':') > 0) {
1373 $port = substr($host, (strpos($host, ':') + 1));
1374 $host = substr($host, 0, (strpos($host, ':') - 1));
1377 // Start connection to server
1378 $fp = fsockopen($host, $port);
1380 $this->_throw_exception($this->_remote_file.' is maybe not available.');
1383 // Repare request line
1384 $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);
1386 fputs($fp, $request);
1387 $reply = ''; $isContent = FALSE; $dummy = '';
1390 while (!feof($fp)) {
1391 $read = trim(fgets($fp, 4096));
1392 if (substr($read, 0, 5) == '<?xml' || $isContent) {
1402 if ((count($dummy) > 0) && (count($reply) == 0) && (!$isContent)) {
1403 // Transfer content from dummy
1407 //die(htmlentities($reply));
1411 } // END _rdf_data()