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
75 var $_decoding_mode = 'utf8_decode';
78 * If $_link_target is set a target='xxx' attribute in each <a /> and <form accept-charset="utf-8" /> html tag will be added
80 * Possible values are "_blank", "_content", "_parent", "_self", "_top"
85 var $_link_target = '_blank';
88 * vars for proxy settings - Prox Host
96 * vars for proxy settings - Prox Port
104 * vars for proxy settings - Prox Username
112 * vars for proxy settings - Prox Password
120 * just a flag for checking if proxy-support should be enabled
121 * set default to false (will be enabled if set_proxy is called)
127 var $_use_proxy = false;
130 * just a flag for checking if proxy-support with authentication
132 * set default to false (will be enabled if set_proxy is called)
138 var $_use_proxy_auth = false;
141 * The time the Files will be cached (in seconds).
146 var $_refresh = 60; // int
149 * The Name of the cached File.
154 var $_cached_file = ''; // String
157 * Indicates whether the cached or the remote file was used.
162 var $_use_cached_file = true;
165 * (fast|normal) depends on _use_dynamic_display(). _use_dynamic_display( TRUE ) -> 'normal', otherwise 'fast'
170 var $_cache_type = 'fast';
173 * The Name of the Remote File.
178 var $_remote_file = '';
181 * Path to the Cache Directory.
186 var $_cache_dir = 'cache/'; // String
189 * Indicates whether the Creating of the Cache Directory needs to be done or not.
194 var $_cache_dir_ok = false;
197 * Type of the file to be parsed (RSS or RDF).
199 * The Type depends on the root node
204 var $_type = 'rss'; // string (rss or rdf)
207 * Array of Display Settings.
209 * Specific Parameters can be set to hidden. These are:
210 * image, channel and textinput. If set to "hidden" those elements won't be displayed.
215 var $_display_opt = array(
220 'cache_update' => '',
227 * Defines the width attribute in the table that holds the rdf/rss representation
231 * @see see_table_width()
233 var $_table_width = '100%';
236 * Indicates whether or not to use dynamic Display Settings
241 var $_use_dynamic_display = false;
249 var $_item_count = '0';
257 var $_max_count = false;
260 * Array containing the content of <channel />
265 var $_array_channel = array();
268 * Array containing the content of each <item />
273 var $_array_item = array();
276 * Array containing the content of <textinput />
281 var $_array_textinput = array();
284 * Array containing the content of <image />
289 var $_array_image = array();
292 * Array containing the Channel content. Just For internal XML Parser Purposes.
297 var $_citem = array();
300 * Array containing the Channel Parser Depth. Just For internal XML Parser Purposes.
305 var $_cdepth = array();
308 * Array containing the Channel tags. Just For internal XML Parser Purposes.
313 var $_ctags = array( 'x' );
316 * Array containing the Channel content. Just For internal XML Parser Purposes.
321 var $_item = array(); // Array
324 * Array containing the Channel Parser Depth. Just For internal XML Parser Purposes.
329 var $_depth = array(); // Array
332 * Array containing the tags. Just For internal XML Parser Purposes.
337 var $_tags = array( 'x' ); // Array
340 * Garbage collection: probability in percent
342 * @var integer 0 => never
345 var $gc_probability = 1;
358 var $_parse_mode = '';
367 * Constructor of our Class
369 * This Method checks if the Cache Directory can be found. Otherwise it tries to creat the Cache Directory at the specified Path.
370 * Additionally the Refresh Time is set to a default Value of 1200s (20 min).
373 * @author Stefan Saasen <s@fase4.com>
378 // default Value, to be overwritten in set_refresh()
379 $this->_refresh = (time() - 1200);
380 $this->_clear_cItems();
381 $this->_clear_Items();
385 * This Method starts the parsing of the specified RDF File. The File can be a local or a remote File.
388 * @author Stefan Saasen <s@fase4.com>
389 * @param string $rdf RDF File (Location)
390 * @return string Displays RDF Content ( using _display() )
391 * @see _remote_file, cache()
393 function parse_RDF( $rdf )
395 unset($this->_array_item);
396 $this->_remote_file = $rdf;
397 $this->out .= "<!-- http://www.fase4.com/rdf/ -->";
398 $this->out .= "<table width=\"".$this->_table_width."\">";
399 $this->out .= $this->cache();
400 $this->out .= "</table>";
402 $this->_item_count = '0';
407 * This Method is called when all parsing is finished to use the garbage collection
410 * @author Stefan Saasen <s@fase4.com>
411 * @param string $rdf RDF File (Location)
412 * @return string Displays RDF Content ( using _display() )
413 * @see _remote_file, cache()
415 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 )
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 )
458 $this->_clear_Items();
459 $xml_parser = xml_parser_create();
460 xml_set_object($xml_parser,$this);
461 xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
462 xml_set_element_handler($xml_parser, '_startElement', '_endElement');
463 xml_set_character_data_handler($xml_parser, '_parseData');
464 if (!xml_parse($xml_parser, trim($data))) {
465 $this->_throw_exception(sprintf("XML error: %s at line %d",
466 xml_error_string(xml_get_error_code($xml_parser)),
467 xml_get_current_line_number($xml_parser)).'<br /><br />Exception in function parse_RDF().');
469 xml_parser_free($xml_parser);
474 * This Methods allows you to set the Refresh Time
477 * @author Stefan Saasen <s@fase4.com>
478 * @param int $seconds time files will be cached (in seconds).
482 function set_refresh( $seconds )
484 $this->_refresh = (time() - $seconds);
488 function set_salt( $saltPara )
490 $this->salt = $saltPara;
495 * This Methods allows you to set the No. of <item>s to display
498 * @param int $int No of max <item>s
499 * @author Stefan Saasen <s@fase4.com>
501 * @see _max_count, _endElement()
503 function set_max_item( $int )
505 $this->_max_count = $int;
510 * This Methods allows you to set the Cache Directory
513 * @author Stefan Saasen <s@fase4.com>
514 * @param string $dir Path to Directory.
518 function set_CacheDir( $dir )
520 if(substr($dir, -1) != '/') {
523 $this->_cache_dir = $dir;
527 * This Method displays Error Messages and terminates the Execution of the Script
530 * @param string $msg Message to display on failure
531 * @author Stefan Saasen <s@fase4.com>
533 function _throw_exception( $msg )
535 $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>";
540 * This Method clears the Array containig the Items.
543 * @author Stefan Saasen <s@fase4.com>
546 function _clear_Items( ) {
547 $this->_item = array(
554 'lastBuildDate' => '',
560 * This Method clears the Array containig the Channel Items.
563 * @author Stefan Saasen <s@fase4.com>
566 function _clear_cItems( ) {
567 $this->_citem = array(
574 'managingEditor' => '',
577 'lastBuildDate' => '',
594 * XML Parser Start Element Handler
597 * @author Stefan Saasen <s@fase4.com>
598 * @param mixed $parser a reference to the XML parser calling the handler.
599 * @param string $name contains the name of the element for which this handler is called.
600 * @param string $attrs contains an associative array with the element's attributes (if any).
601 * @see _get_ChannelData(), _clear_Items(), _type, _parse_mode, _depth, _tags, _cdepth, _ctags
603 function _startElement($parser, $name, $attrs) {
604 // We have to determine, which type of xml data we have to parse
606 $this->_type = 'rss';
607 } elseif($name == 'rdf:RDF' OR $name == 'rdf') {
608 $this->_type = 'rdf';
612 if ( $name == 'channel' AND $this->_type != 'rdf' ) {
613 $this->_parse_mode = 'channel';
614 } elseif ( ($name=='item')
616 ||($name=='textinput')
617 ||(($name=='channel') && ($this->_type != 'rss')) ) {
618 if($this->_parse_mode=='channel') {
619 $this->_get_ChannelData( $parser );
621 $this->_parse_mode = 'all';
624 if( !isset( $this->_depth[$this->get_parser_id($parser)] ) ) {
625 $this->_depth[$this->get_parser_id($parser)] = '0';
627 $this->_depth[$this->get_parser_id($parser)]++;
628 array_push($this->_tags, $name);
630 if( !isset( $this->_cdepth[$this->get_parser_id($parser)] ) ) {
631 $this->_cdepth[$this->get_parser_id($parser)] = '0';
633 $this->_cdepth[$this->get_parser_id($parser)]++;
634 array_push($this->_ctags, $name);
635 } // END _startElement()
638 * Retrives the Channel Data in <rss> File
641 * @author Stefan Saasen <s@fase4.com>
642 * @param mixed $parser a reference to the XML parser calling the handler.
643 * @see _output, _display_opt, _citem
645 function _get_ChannelData( $parser )
647 $this->_citem['link'] = trim($this->_citem['link']);
648 if (($this->_display_opt['sitelink'] == $this->_citem['link']) && (!empty($this->_display_opt['reflink'])) && (!empty($this->_display_opt['refid'])))
650 $this->_citem['link'] .= $this->_display_opt['reflink'].$this->_display_opt['refid'];
653 if( empty($this->_display_opt['channel']) OR
654 $this->_display_opt['channel'] != 'hidden') {
655 $this->_output .= "<tr><td>\n";
656 $this->_output .= "<table border=\"0\" width=\"100%\" class=\"fase4_rdf_meta\" cellspacing=\"5\" cellpadding=\"2\">\n";
657 $this->_output .= "<tr><td class=\"fase4_rdf_main_title\"><div class=\"fase4_rdf_main_title\">".htmlspecialchars($this->_citem['title'])."</div></td></tr>\n";
658 $this->_output .= "<tr><td class=\"fase4_rdf\">".strip_tags($this->_citem['description'], '<a>, <img>')."</td></tr>\n";
659 $this->_output .= "<tr><td> </td></tr>\n";
660 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
661 if(isset($this->_display_opt['build']) && $this->_display_opt['build'] != 'hidden') {
662 if($this->_citem['lastBuildDate']){$this->_output .= 'build: '. $this->_citem['lastBuildDate'].'<br />';}
664 if(isset($this->_display_opt['cache_update']) && $this->_display_opt['cache_update'] != 'hidden' && ( $_update = $this->get_cache_update_time()) ) {
665 $this->_output .= 'cache update: '.$_update."<br />\n";
667 $this->_output .= "<a href=\"".$this->_citem['link']."\" ";
668 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
669 $this->_output .= ">".$this->_cut_string($this->_citem['link']) . '</a>';
670 $this->_output .= "</td></tr>\n";
671 $this->_output .= "<tr><td><hr noshade width=\"100%\" size=\"1\"></td></tr>\n";
672 $this->_output .= "</table></td></tr>";
674 $this->_array_channel = array(
675 'title' => $this->_citem['title'],
676 'link' => $this->_citem['link'],
677 'description' => $this->_citem['description'],
678 'lastBuildDate' => $this->_citem['lastBuildDate']);
682 * XML Parser End Element Handler
685 * @author Stefan Saasen <s@fase4.com>
686 * @param mixed $parser a reference to the XML parser calling the handler.
687 * @param string $name contains the name of the element for which this handler is called.
688 * @see _clear_Items(), _type, _parse_mode, _depth, _tags, _cdepth, _ctags, _item, _output, _display_opt
690 function _endElement($parser, $name) {
691 array_pop($this->_tags);
692 $this->_depth[$this->get_parser_id($parser)]--;
693 array_pop($this->_ctags);
694 $this->_cdepth[$this->get_parser_id($parser)]--;
695 $this->_item['link'] = trim($this->_item['link']);
696 if ((!empty($this->_display_opt['refid'])) && (!empty($this->_item['link'])))
698 if (!isInString('refid=', $this->_item['link'])) $this->_item['link'] .= '?refid=' . $this->_display_opt['refid'];
702 if(empty($this->_max_count) OR $this->_item_count < $this->_max_count) {
703 if($this->_item['title'] != $this->_item['description']
704 AND $this->_item['description']) {
706 // word-wrapping added by Roland Haeder <webmaster@mxchange.org>
707 if (($this->_word_wrap > 0) && (strlen($this->_item['description']) > $this->_word_wrap))
709 // Switch off _use_nl2br
710 $this->_use_nl2br = false;
711 // First remove all \n
712 $this->_item['description'] = str_replace('\n', ' ', $this->_item['description']);
713 // Wrap with <br />\n
714 $this->_item['description'] = wordwrap($this->_item['description'], $this->_word_wrap, "*<br />\n");
716 elseif (($this->_word_wrap == '0') && (!$this->_use_nl2br))
718 // Strip tags out instead when word-wrap is disabled
719 $this->_item['description'] = strip_tags($this->_item['description'], '<a>, <img>');
722 // nl2br added by Roland Haeder <webmaster@mxchange.org>
723 if ($this->_use_nl2br) $this->_item['description'] = nl2br($this->_item['description']);
725 $this->_output .= "<tr><td class=\"fase4_rdf_title\"><div class=\"fase4_rdf_title\"><a class=\"fase4_rdf_title\" href=\"".$this->_item['link']."\" ";
726 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
727 $this->_output .= ">".strip_tags($this->_item['title'], '<a>, <img>').'</a> ('.$this->_item['pubDate'].")</div></td></tr>\n";
728 $this->_output .= "<tr><td class=\"fase4_rdf\">".$this->_item['description']."</td></tr>\n";
729 // we just display the <hr> if there is a description
730 $this->_output .= "<tr><td><hr noshade=\"noshade\" size=\"1\" /></td></tr>\n";
732 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
733 $this->_output .= "<a href=\"".$this->_item["link"]."\" ";
734 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
735 $this->_output .= ">".$this->_item["title"]."</a></td></tr>\n";
738 $this->_array_item[] = array(
739 'title' => $this->_item['title'],
740 'link' => $this->_item['link'],
741 'description' => $this->_item['description']
744 ++$this->_item_count;
746 $this->_clear_Items();
750 if(isset($this->_display_opt['image']) && ($this->_display_opt['image'] != 'hidden') && $this->_item['url']) {
751 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
752 $this->_output .= "<a href=\"".$this->_item['link']."\" ";
753 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
754 $this->_output .= "><img src=\"".$this->_item['url']."\"";
755 if(isset($this->_item['width']) && isset($this->_item['height'])) {
756 $this->_output .= " width=\"".$this->_item['width']."\" height=\"".$this->_item['height']."\"";
758 $this->_output .= " alt=\"".$this->_item['title']."\" border=\"0\" /></a></td></tr>\n";
760 $this->_array_image[] = array(
761 'url' => $this->_item['url'],
762 'link' => $this->_item['link'],
763 'width' => $this->_item['width'],
764 'height' => $this->_item['height']
766 $this->_clear_Items();
767 } elseif( isset($this->_display_opt['image'] ) && ($this->_display_opt['image'] == 'hidden') ) {
768 $this->_clear_Items();
774 if(isset($this->_display_opt['channel']) AND $this->_display_opt['channel'] != 'hidden' AND $this->_item['title'] != '') {
775 $this->_output .= "<tr><td>\n";
776 $this->_output .= "<table border=\"0\" width=\"100%\" class=\"fase4_rdf_meta\" cellspacing=\"5\" cellpadding=\"2\">\n";
777 $this->_output .= "<tr><td class=\"fase4_rdf\"><div class=\"fase4_rdf_title\">".htmlspecialchars($this->_item['title'])."</div></td></tr>\n";
778 $this->_output .= "<tr><td class=\"fase4_rdf\">".strip_tags($this->_item['description'], '<a>, <img>')."</td></tr>\n";
779 $this->_output .= "<tr><td> </td></tr>\n";
780 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
781 if($this->_display_opt['build'] != 'hidden') {
782 if($this->_item['lastBuildDate']){$this->_output .= 'build: '. $this->_item['lastBuildDate'].'<br />';}
784 if($this->_display_opt['cache_update'] != 'hidden' && ( $_update = $this->get_cache_update_time()) ) {
785 $this->_output .= 'cache update: '.$_update."<br />\n";
787 $this->_output .= "<a href=\"".$this->_item['link']."\" ";
788 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
789 $this->_output .= ">".$this->_cut_string($this->_item['link'])."</a>\n";
790 $this->_output .= "</td></tr>\n";
791 $this->_output .= "</table></td></tr>\n";
793 $this->_array_channel = array(
794 'title' => $this->_item['title'],
795 'link' => $this->_item['link'],
796 'description' => $this->_item['description'],
797 'lastBuildDate' => $this->_item['lastBuildDate']
799 $this->_clear_Items();
800 $this->_clear_cItems();
804 if(isset($this->_display_opt['textinput']) && ($this->_display_opt['textinput'] != 'hidden') && $this->_item['name'] && $this->_item['link']) {
805 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
806 $this->_output .= "<form accept-charset=\"utf-8\" action=\"".$this->_item['link']."\" ";
807 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
808 $this->_output .= "method=\"get\">\n";
809 $this->_output .= "<div class=\"fase4_rdf_title\">".$this->_item['title']."</div>";
810 $this->_output .= strip_tags($this->_item['description'], '<a>, <img>')."<br /><br />\n";
811 $this->_output .= "<input class=\"fase4_rdf_input\" type=\"text\" name=\"".$this->_item['name']."\"> \n";
812 $this->_output .= "<input class=\"fase4_rdf_input\" type=\"submit\" value=\"go\">";
813 $this->_output .= "</form>\n";
814 $this->_output .= "</td></tr>\n";
815 $this->_array_textinput = array(
816 'title' => $this->_item['title'],
817 'name' => $this->_item['name'],
818 'link' => $this->_item['link'],
819 'description' => $this->_item['description']
821 $this->_clear_Items();
822 } elseif( isset($this->_display_opt['textinput']) && ($this->_display_opt['textinput'] == 'hidden') ) {
823 $this->_clear_Items();
831 * This Method returns the data from the <channel /> paragraph.
834 * @author Stefan Saasen <s@fase4.com>
836 * @see _array_channel
838 function get_array_channel( )
840 return $this->_array_channel;
844 * This Method returns the data from each <item /> paragraph.
847 * @author Stefan Saasen <s@fase4.com>
851 function get_array_item( )
853 return $this->_array_item;
857 * This Method returns the data from <textinput />.
860 * @author Stefan Saasen <s@fase4.com>
862 * @see _array_textinput
864 function get_array_textinput( )
866 return $this->_array_textinput;
870 * Getter for parser id from resource
873 * @author Roland Haeder <webmaster@mxchange.org>
876 function get_parser_id ($parser) {
881 if (is_resource($parser)) {
882 // Cast the resource into id
891 * This Method returns the data from <image />.
894 * @author Stefan Saasen <s@fase4.com>
898 function get_array_image( )
900 return $this->_array_image;
904 * XML Parser Data Handler
907 * @author Stefan Saasen <s@fase4.com>
908 * @param mixed $parser a reference to the XML parser calling the handler.
909 * @param string $text contains the character data as a string.
910 * @see _parse_mode, _item, _tags, _depth, _citem, _ctags, _cdepth
912 function _parseData($parser, $text)
914 // Deocing mode added by Roland Haeder <webmaster@mxchange.org>
915 switch ($this->_decoding_mode)
918 $text = utf8_decode($text);
922 $text = htmlentities($text);
926 $clean = preg_replace("/\s/", "", $text);
928 $text = preg_replace("/^\s+/", "", $text)."\n";
929 if($this->_parse_mode == 'all') {
930 if ( isset($this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]]) &&
931 $this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]] ) {
932 $this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]] .= $text;
934 $this->_item[$this->_tags[$this->_depth[$this->get_parser_id($parser)]]] = $text;
936 } elseif (isset($this->_parse_mode) && $this->_parse_mode == 'channel') {
937 if ( isset($this->_citem[$this->_ctags[$this->_cdepth[$this->get_parser_id($parser)]]]) ) {
938 $this->_citem[$this->_ctags[$this->_cdepth[$this->get_parser_id($parser)]]] .= $text;
940 $this->_citem[$this->_ctags[$this->_cdepth[$this->get_parser_id($parser)]]] = $text;
947 * This Method allows you to choose if specific Parameters are displayed or not. These are:
948 * image, channel, textinput, build and cache_update. If set to "hidden" those elements won't be displayed.
951 * @author Stefan Saasen <s@fase4.com>
952 * @param array $options
955 function set_Options( $options = '' )
957 if(is_array( $options )) {
958 $this->_display_opt = $options;
961 unset($this->_display_opt);
967 * This Method allows you to define the width of the table that holds the representation of the rdf/rss file.
970 * @author Stefan Saasen <s@fase4.com>
971 * @param int $width attribute width in tag <table>
974 function set_table_width( $width = 400 )
976 $this->_table_width = $width;
981 * This Method returns an assocative Array with available Options.
983 * The Keys are the Name of the Options to be set.
984 * The Values are short Description of available Options.
987 * @author Stefan Saasen <s@fase4.com>
988 * @return array $options
991 function get_Options() {
993 'image' => "If 'image' is set to \"hidden\" no image provided by the RDF Publisher will be displayed.",
994 '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",
995 'textinput' => "If set to \"hidden\" no Input Form will be displayed",
996 'build' => "If set to \"hidden\" the Build Date (if provided) of the RDF File will not be displayed",
997 'cache_update' => "If set to \"hidden\" the Update Date/Time of the cached Rdf File will not be displayed"
1003 * This Method returns the Content of the RDF File in one string. The String actually holds the whole XML Document.
1006 * @author Stefan Saasen <s@fase4.com>
1007 * @param string $rdf RDF File (Location)
1008 * @return string XML Presentation of parsed RDF File
1009 * @see _cached_file, _remote_file, _cache_dir, _refresh, _update_cache()
1013 // checks if the cache directory already exists
1014 // if not, the cache directory will be created
1015 if(!$this->_cache_dir_ok) {
1016 $this->_create_cache_dir();
1018 if($this->_use_dynamic_display == true) {
1019 $this->_cached_file = md5('dynamic'.$this->salt.$this->_remote_file) . '.cache';
1020 $this->_cache_type = 'normal';
1022 $this->_cached_file = md5($this->salt.$this->_remote_file) . '.cache';
1023 $this->_cache_type = 'fast';
1026 $_cache_f = $this->_cache_dir.$this->_cached_file;
1028 if ( (!file_exists($_cache_f)) || (filemtime($_cache_f) < $this->_refresh) || (filesize($_cache_f) == 0)) {
1029 // We have to parse the remote file
1030 $this->_use_cached_file = false;
1031 // --> we want to provide proper Information for Use in
1032 // get_cache_update_time()
1034 if($this->_use_dynamic_display == true) {
1035 $_rdf = @implode(' ', $this->_rdf_data()); // -> proxy
1037 $this->_throw_exception( $this->_remote_file.' is not available' );
1039 $this->_parse_xRDF( $_rdf );
1040 $this->_update_cache( $_rdf );
1041 $data = $this->_output;
1043 $_rdf = @implode(' ', $this->_rdf_data()); // -> proxy
1045 $this->_throw_exception( $this->_remote_file.' is not available' );
1047 $this->_parse_xRDF( $_rdf );
1048 $this->_update_cache( $this->_output );
1049 $data = $this->_output;
1052 // we can use the cached file
1053 $this->_use_cached_file = true;
1054 if($this->_use_dynamic_display == true) {
1055 $this->_parse_xRDF( implode(' ', file($_cache_f)) );
1056 $data = $this->_output;
1058 $data = @implode(' ', file($_cache_f));
1065 * This Methods creates the Cache Directory if the specified Directory does not exist.
1068 * @author Stefan Saasen <s@fase4.com>
1069 * @param string $dir Path to Directory.
1071 * @see _cache_dir, _cache_dir_ok
1073 function _create_cache_dir()
1076 if(!@is_dir($this->_cache_dir)) {
1077 $arr = explode('/', $this->_cache_dir);
1082 for($i = '0';$i<$c;$i++)
1085 $path .= $arr[$i].'/';
1086 if(!@is_dir($path)) {
1087 if(!@mkdir($path, 0777)) {
1088 $this->_throw_exception("failed to create directory:<b>".$this->_cache_dir."</b>.<br /><br />Exception on Line: ".__LINE__);
1094 $this->_cache_dir_ok = true;
1097 $this->_cache_dir_ok = true;
1100 } // END _create_cache_dir()
1103 * This Method updates the cached RDF Files and synchronises them with their remote Counterparts.
1106 * @author Stefan Saasen <s@fase4.com>
1107 * @param string $rdf RDF File (Location)
1108 * @see _cache_dir, _cached_file, _throw_exception()
1110 function _update_cache( $content = '' )
1112 $_local = @fopen( $this->_cache_dir.$this->_cached_file, 'w' );
1114 $this->_throw_exception( 'Cannot open '.$this->_cached_file.'<br /><br />Exception at Line: '.__LINE__ );
1117 if (fwrite( $_local, $content) === false) {
1118 $this->_throw_exception( 'Cannot write to '.$this->_cached_file.'<br /<br />Exeception at Line: '.__LINE__);
1122 @chmod( $this->_cache_dir.$this->_cached_file, 0666);
1124 } // END _update_cache()
1127 * This Method returns the Date/Time of last Cache Update of the actually parsed RDF File.
1130 * @author Stefan Saasen <s@fase4.com>
1131 * @return string Date/Time of last Update
1132 * @see _cache_dir, _cached_file
1134 function get_cache_update_time()
1136 return (file_exists($this->_cache_dir.$this->_cached_file))?date('d.m.Y H:i:s', filemtime($this->_cache_dir.$this->_cached_file)):'Cachemiss';
1137 } // END get_cache_update_time()
1140 * This Method returns the Type of Cache that was used ('normal' or 'fast')
1143 * @author Stefan Saasen <s@fase4.com>
1144 * @param string $rdf RDF File (Location)
1145 * @return string Displays RDF Content ( using _display() )
1146 * @see _remote_file, cache()
1148 function get_CacheType()
1150 return $this->_cache_type;
1154 * Returns true if cached file was used, otherwise false
1157 * @author Stefan Saasen <s@fase4.com>
1158 * @return array $options
1159 * @see _use_cached_file
1161 function is_cachedFile()
1163 return $this->_use_cached_file;
1167 * This Method deletes all the cached Files.
1169 * Please keep in mind to use this method just as a 'manual garbage collection'
1170 * You should cache the rss/rdf files locally to avoid unnecessary traffic.
1171 * (Both for your visitors and the Publisher)
1174 * @author Stefan Saasen <s@fase4.com>
1177 function clear_cache()
1179 $dir = dir($this->_cache_dir);
1180 while($file=$dir->read()) {
1181 // Exclude directories
1182 if (is_file($dir->path.$file) && substr($file, -6, 6) != '.cache' && substr($file, -4, 4) != '.log') {
1183 if(!@unlink($dir->path.$file)) {
1184 $this->_throw_exception("Unable to unlink ".$dir->path.$file."<br />\n<br />\nException at Line: ".__LINE__ );
1191 } // END clear_cache()
1194 * Cuts the String $string after $str_len and adds '... '
1197 * @param string $string String to be shortened
1198 * @param int $str_len length of the returned String (overall length including '... ')
1199 * @return string Cut String
1201 function _cut_string( $string, $str_len = '30' )
1203 if(strlen(trim($string))>$str_len) {
1204 $string = substr( trim($string) , 0, $str_len - 4);
1208 } // END _cut_string()
1211 * this Method implements simple Garbage Collection
1214 * @author Stefan Saasen <s@fase4.com>
1215 * @see _cache_dir, gc_probability, gc_maxlifetime
1217 function _garbage_collection()
1219 srand((double) microtime() * 1000000);
1220 if (mt_rand(1, 100) <= $this->gc_probability) {
1221 $dir = dir($this->_cache_dir);
1222 while($file=$dir->read()) {
1223 if (is_file($dir->path.$file) && substr($file, -6, 6) != '.cache' && substr($file, -4, 4) != '.log' && filemtime($dir->path.$file) <= time() - $this->_refresh ) {
1224 @unlink($dir->path.$file);
1231 /* ==== Proxy/Auth methods ==== */
1234 * this method sets a proxy server
1237 * @param string $phost Proxy Host
1238 * @param string $pport Prox Port
1239 * @author Marco Kraus <marco.kraus@siemens.com>
1241 function set_proxy($phost, $pport)
1243 $this->_use_proxy = true;
1246 $this->_phost = $phost;
1249 $this->_pport = $pport;
1253 * this method sets a proxy server authentification
1256 * @param string $pname Username
1257 * @param string $ppaswd Password
1258 * @author Marco Kraus <marco.kraus@siemens.com>
1260 function set_proxy_auth( $pname, $ppasswd )
1262 $this->_use_proxy_auth = true;
1265 $this->_pname = $pname;
1268 $this->_ppasswd = $ppasswd;
1273 * gets _remote_file into an array
1275 * needed, cause if you use a proxy, you have to open
1276 * a raw-tcp-socket to get the data
1279 * @author Marco Kraus <Marco.Kraus@siemens.com>
1281 * @see _use_proxy, cache()
1283 function _rdf_data()
1285 if ( $this->_use_proxy == true )
1287 // we need a raw socket here to connect to proxy
1288 $fp = fsockopen($this->_phost,$this->_pport);
1291 $this->_throw_exception( $this->_remote_file.' is not available with proxy' );
1293 if ( $this->_use_proxy_auth == true ) {
1294 fputs($fp, "GET ".$this->_remote_file." HTTP/1.0\r\nUser-Agent: Fase4 RDF-Reader/1.40 modified by Quix0r\r\n\r\n");
1296 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");
1301 for ( $i = '0'; !feof ($fp) ; $i++)
1303 $usable_data[$i] = "";
1304 $usable_data[$i] = fgets($fp,4096);
1306 // PARSE HEADER ---- first line has to be <?xml, second rdf or rss, and third is blank
1308 // strstr did not fit (ask Rasmus why), so we compare each character
1309 if ( ($usable_data[$i][0] == '<' ) &&
1310 ($usable_data[$i][1] == '?' ) &&
1311 ($usable_data[$i][2] == 'x' ) &&
1312 ($usable_data[$i][3] == 'm' ) &&
1313 ($usable_data[$i][4] == 'l' ) ) {
1314 $usable_data[0] = $usable_data[$i]; // save current field
1315 $i = 1; // just reset array to start
1318 // there seems to be proxystuff after the <?xml....we delete this
1320 ($usable_data[$i][0] == '<' ) &&
1321 ($usable_data[$i][1] == 'r' ) &&
1322 ($usable_data[$i][2] == 'd' ) &&
1323 ($usable_data[$i][3] == 'f' ) &&
1324 ($usable_data[$i][4] == ':' )
1328 ($usable_data[$i][0] == '<' ) &&
1329 ($usable_data[$i][1] == 'r' ) &&
1330 ($usable_data[$i][2] == 's' ) &&
1331 ($usable_data[$i][3] == 's' )
1335 $usable_data[1] = $usable_data[$i]; // save current field
1336 $usable_data[2] = "\n";
1337 $i = 2; // just reset array to start
1342 return $usable_data;
1344 if (substr($this->_remote_file, 0, 7) != 'http://') {
1345 $this->_throw_exception( 'Cannot find http:// in '.$this->_remote_file.'!' );
1348 // Extract host information
1349 $host = substr($this->_remote_file, 7);
1350 // Extract the GET part
1352 if (strpos($host, '/') > 0) {
1353 $get = substr($host, strpos($host, '/'));
1354 $host = substr($host, 0, strpos($host, '/'));
1358 if (strpos($host, ':') > 0) {
1359 $port = substr($host, (strpos($host, ':') + 1));
1360 $host = substr($host, 0, (strpos($host, ':') - 1));
1363 // Start connection to server
1364 $fp = fsockopen($host, $port);
1366 $this->_throw_exception( $this->_remote_file.' is maybe not available.' );
1369 // Repare request line
1370 $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);
1372 fputs($fp, $request);
1373 $reply = ''; $isContent = false; $dummy = '';
1376 while ( !feof($fp) ) {
1377 $read = trim(fgets($fp, 4096));
1378 if (substr($read, 0, 5) == '<?xml' || $isContent) {
1388 if ((count($dummy) > 0) && (count($reply) == 0) && (!$isContent)) {
1389 // Transfer content from dummy
1393 //die(htmlentities($reply));
1397 } // END _rdf_data()