2 /* $Id: rdf.class.php,v 1.40 2003/07/06 20:33:58 sts Exp $ */
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 * @version 1.7 ($Date: 2003/07/06 20:33:58 $) $Revision: 1.40 $
50 * Word-wrapping mode for description, set it to 0 do disable this feature! Ommits _use_nl2br!
58 * Wether to recode \n -> <br /> or not in description
63 var $_use_nl2br = true;
66 * Sets the decoding mode of the read data (UTF8 scrambles some german umlauts here!)
68 * "htmlentities" - Use the function htmlentities()
69 * "utf8_decode" - Use the function ut8_decode() when you have UTF8 encoded text
74 var $_decoding_mode = "utf8_decode";
77 * If $_link_target is set a target='xxx' attribute in each <a /> and <form /> html tag will be added
79 * Possible values are "_blank", "_content", "_parent", "_self", "_top"
84 var $_link_target = "_blank";
87 * vars for proxy settings - Prox Host
95 * vars for proxy settings - Prox Port
103 * vars for proxy settings - Prox Username
111 * vars for proxy settings - Prox Password
119 * just a flag for checking if proxy-support should be enabled
120 * set default to false (will be enabled if set_proxy is called)
126 var $_use_proxy = false;
129 * just a flag for checking if proxy-support with authentication
131 * set default to false (will be enabled if set_proxy is called)
137 var $_use_proxy_auth = false;
140 * The time the Files will be cached (in seconds).
145 var $_refresh = 60; // int
148 * The Name of the cached File.
153 var $_cached_file = ""; // String
156 * Indicates whether the cached or the remote file was used.
161 var $_use_cached_file = true;
164 * (fast|normal) depends on _use_dynamic_display(). _use_dynamic_display( TRUE ) -> 'normal', otherwise 'fast'
169 var $_cache_type = "fast";
172 * The Name of the Remote File.
177 var $_remote_file = "";
180 * Path to the Cache Directory.
185 var $_cache_dir = "cache/"; // String
188 * Indicates whether the Creating of the Cache Directory needs to be done or not.
193 var $_cache_dir_ok = false;
196 * Type of the file to be parsed (RSS or RDF).
198 * The Type depends on the root node
203 var $_type = "rss"; // string (rss or rdf)
206 * Array of Display Settings.
208 * Specific Parameters can be set to hidden. These are:
209 * image, channel and textinput. If set to "hidden" those elements won't be displayed.
214 var $_display_opt = array(
219 'cache_update' => "",
226 * Defines the width attribute in the table that holds the rdf/rss representation
230 * @see see_table_width()
232 var $_table_width = "100%";
235 * Indicates whether or not to use dynamic Display Settings
240 var $_use_dynamic_display = false;
248 var $_item_count = 0;
256 var $_max_count = false;
259 * Array containing the content of <channel />
264 var $_array_channel = array();
267 * Array containing the content of each <item />
272 var $_array_item = array();
275 * Array containing the content of <textinput />
280 var $_array_textinput = array();
283 * Array containing the content of <image />
288 var $_array_image = array();
291 * Array containing the Channel content. Just For internal XML Parser Purposes.
296 var $_citem = array();
299 * Array containing the Channel Parser Depth. Just For internal XML Parser Purposes.
304 var $_cdepth = array();
307 * Array containing the Channel tags. Just For internal XML Parser Purposes.
312 var $_ctags = array( "x" );
315 * Array containing the Channel content. Just For internal XML Parser Purposes.
320 var $_item = array(); // Array
323 * Array containing the Channel Parser Depth. Just For internal XML Parser Purposes.
328 var $_depth = array(); // Array
331 * Array containing the tags. Just For internal XML Parser Purposes.
336 var $_tags = array( "x" ); // Array
339 * Garbage collection: probability in percent
341 * @var integer 0 => never
344 var $gc_probability = 1;
357 var $_parse_mode = "";
366 * Constructor of our Class
368 * This Method checks if the Cache Directory can be found. Otherwise it tries to creat the Cache Directory at the specified Path.
369 * Additionally the Refresh Time is set to a default Value of 1200s (20 min).
372 * @author Stefan Saasen <s@fase4.com>
377 // default Value, to be overwritten in set_refresh()
378 $this->_refresh = (time() - 1200);
379 $this->_clear_cItems();
380 $this->_clear_Items();
384 * This Method starts the parsing of the specified RDF File. The File can be a local or a remote File.
387 * @author Stefan Saasen <s@fase4.com>
388 * @param string $rdf RDF File (Location)
389 * @return string Displays RDF Content ( using _display() )
390 * @see _remote_file, cache()
392 function parse_RDF( $rdf )
394 unset($this->_array_item);
395 $this->_remote_file = $rdf;
396 $this->out .= "<!-- http://www.fase4.com/rdf/ -->";
397 $this->out .= "<table width=\"".$this->_table_width."\">";
398 $this->out .= $this->cache();
399 $this->out .= "</table>";
401 $this->_item_count = 0;
406 * This Method is called when all parsing is finished to use the garbage collection
409 * @author Stefan Saasen <s@fase4.com>
410 * @param string $rdf RDF File (Location)
411 * @return string Displays RDF Content ( using _display() )
412 * @see _remote_file, cache()
414 function finish($return = false)
422 $this->_garbage_collection();
426 * With this method you can decide whether to use the normal cache and dynamic display Options or to use a static cache.
428 * 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.
429 * In this case you can not modify the display settings.
430 * processing time: ( 1.4792) --> remote file
431 * processing time: ( 0.0313) --> using 'normal cache' with display Modification turned on.
432 * processing time: ( 0.0019) --> using 'fast cache'
435 * @author Stefan Saasen <s@fase4.com>
436 * @param string $rdf RDF File (Location)
437 * @return string Displays RDF Content ( using _display() )
438 * @see _remote_file, cache()
440 function use_dynamic_display( $bool )
442 $this->_use_dynamic_display = $bool;
447 * This Method avtually parses the XML data.
450 * @author Stefan Saasen <s@fase4.com>
451 * @param string $data RDF File XML Data
452 * @see _clear_Items()
454 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 )
482 $this->_refresh = (time() - $seconds);
486 function set_salt( $saltPara )
488 $this->salt = $saltPara;
493 * This Methods allows you to set the No. of <item>s to display
496 * @param int $int No of max <item>s
497 * @author Stefan Saasen <s@fase4.com>
499 * @see _max_count, _endElement()
501 function set_max_item( $int )
503 $this->_max_count = $int;
508 * This Methods allows you to set the Cache Directory
511 * @author Stefan Saasen <s@fase4.com>
512 * @param string $dir Path to Directory.
516 function set_CacheDir( $dir )
518 if(substr($dir, -1) != "/") {
521 $this->_cache_dir = $dir;
525 * This Method displays Error Messages and terminates the Execution of the Script
528 * @param string $msg Message to display on failure
529 * @author Stefan Saasen <s@fase4.com>
531 function _throw_exception( $msg )
533 $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>";
538 * This Method clears the Array containig the Items.
541 * @author Stefan Saasen <s@fase4.com>
544 function _clear_Items( ) {
545 $this->_item = array(
558 * This Method clears the Array containig the Channel Items.
561 * @author Stefan Saasen <s@fase4.com>
564 function _clear_cItems( ) {
565 $this->_citem = array(
572 'managingEditor'=>"",
592 * XML Parser Start Element Handler
595 * @author Stefan Saasen <s@fase4.com>
596 * @param mixed $parser a reference to the XML parser calling the handler.
597 * @param string $name contains the name of the element for which this handler is called.
598 * @param string $attrs contains an associative array with the element's attributes (if any).
599 * @see _get_ChannelData(), _clear_Items(), _type, _parse_mode, _depth, _tags, _cdepth, _ctags
601 function _startElement($parser, $name, $attrs) {
602 // We have to determine, which type of xml data we have to parse
604 $this->_type = "rss";
605 } elseif($name == "rdf:RDF" OR $name == "rdf") {
606 $this->_type = "rdf";
610 if ( $name == "channel" AND $this->_type != "rdf" ) {
611 $this->_parse_mode = "channel";
612 } elseif ( ($name=="item")
614 ||($name=="textinput")
615 ||(($name=="channel") && ($this->_type != "rss")) ) {
616 if($this->_parse_mode=="channel") {
617 $this->_get_ChannelData( $parser );
619 $this->_parse_mode = "all";
622 if( !isset( $this->_depth[$parser] ) ) {
623 $this->_depth[$parser] = 0;
625 $this->_depth[$parser]++;
626 array_push($this->_tags, $name);
628 if( !isset( $this->_cdepth[$parser] ) ) {
629 $this->_cdepth[$parser] = 0;
631 $this->_cdepth[$parser]++;
632 array_push($this->_ctags, $name);
633 } // END _startElement()
636 * Retrives the Channel Data in <rss> File
639 * @author Stefan Saasen <s@fase4.com>
640 * @param mixed $parser a reference to the XML parser calling the handler.
641 * @see _output, _display_opt, _citem
643 function _get_ChannelData( $parser )
645 $this->_citem["link"] = trim($this->_citem["link"]);
646 if (($this->_display_opt["sitelink"] == $this->_citem["link"]) && (!empty($this->_display_opt["reflink"])) && (!empty($this->_display_opt["refid"])))
648 $this->_citem["link"] .= $this->_display_opt["reflink"].$this->_display_opt["refid"];
651 if( empty($this->_display_opt["channel"]) OR
652 $this->_display_opt["channel"] != "hidden") {
653 $this->_output .= "<tr><td>\n";
654 $this->_output .= "<table border=\"0\" width=\"100%\" class=\"fase4_rdf_meta\" cellspacing=\"5\" cellpadding=\"2\">\n";
655 $this->_output .= "<tr><td class=\"fase4_rdf_main_title\"><div class=\"fase4_rdf_main_title\">".htmlspecialchars($this->_citem["title"])."</div></td></tr>\n";
656 $this->_output .= "<tr><td class=\"fase4_rdf\">".strip_tags($this->_citem["description"], "<a>, <img>")."</td></tr>\n";
657 $this->_output .= "<tr><td> </td></tr>\n";
658 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
659 if(isset($this->_display_opt["build"]) && $this->_display_opt["build"] != "hidden") {
660 if($this->_citem["lastBuildDate"]){$this->_output .= "build: ". $this->_citem["lastBuildDate"]."<br />";}
662 if(isset($this->_display_opt["cache_update"]) && $this->_display_opt["cache_update"] != "hidden" && ( $_update = $this->get_cache_update_time()) ) {
663 $this->_output .= "cache update: ".$_update."<br />\n";
665 $this->_output .= "<a href=\"".$this->_citem["link"]."\" ";
666 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
667 $this->_output .= ">".$this->_cut_string($this->_citem["link"])."</a>";
668 $this->_output .= "</td></tr>\n";
669 $this->_output .= "<tr><td><hr noshade width=\"100%\" size=\"1\"></td></tr>\n";
670 $this->_output .= "</table></td></tr>";
672 $this->_array_channel = array( "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[$parser]--;
690 array_pop($this->_ctags);
691 $this->_cdepth[$parser]--;
692 $this->_item["link"] = trim($this->_item["link"]);
693 if ((!empty($this->_display_opt["refid"])) && (!empty($this->_item["link"])))
695 if (!ereg("refid=", $this->_item["link"])) $this->_item["link"] .= "?refid=" . $this->_display_opt["refid"];
699 if(empty($this->_max_count) OR $this->_item_count < $this->_max_count) {
700 if($this->_item["title"] != $this->_item["description"]
701 AND $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";
734 $this->_array_item[] = array( "title"=>$this->_item["title"],
735 "link"=>$this->_item["link"],
736 "description"=>$this->_item["description"]);
737 ++$this->_item_count;
739 $this->_clear_Items();
742 if(isset($this->_display_opt["image"]) && ($this->_display_opt["image"] != "hidden") && $this->_item["url"]) {
743 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
744 $this->_output .= "<a href=\"".$this->_item["link"]."\" ";
745 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
746 $this->_output .= "><img src=\"".$this->_item["url"]."\"";
747 if(isset($this->_item["width"]) && isset($this->_item["height"])) {
748 $this->_output .= " width=\"".$this->_item["width"]."\" height=\"".$this->_item["height"]."\"";
750 $this->_output .= " alt=\"".$this->_item["title"]."\" border=\"0\" /></a></td></tr>\n";
752 $this->_array_image[] = array( "url"=>$this->_item["url"],
753 "link"=>$this->_item["link"],
754 "width"=>$this->_item["width"],
755 "height"=>$this->_item["height"]);
756 $this->_clear_Items();
757 } elseif( isset($this->_display_opt["image"] ) && ($this->_display_opt["image"] == "hidden") ) {
758 $this->_clear_Items();
763 if(isset($this->_display_opt["channel"]) AND $this->_display_opt["channel"] != "hidden" AND $this->_item["title"] != '') {
764 $this->_output .= "<tr><td>\n";
765 $this->_output .= '<table border="0" width="100%" class="fase4_rdf_meta" cellspacing="5" cellpadding="2">'."\n";
766 $this->_output .= "<tr><td class=\"fase4_rdf\"><div class=\"fase4_rdf_title\">".htmlspecialchars($this->_item["title"])."</div></td></tr>\n";
767 $this->_output .= "<tr><td class=\"fase4_rdf\">".strip_tags($this->_item["description"], "<a>, <img>")."</td></tr>\n";
768 $this->_output .= "<tr><td> </td></tr>\n";
769 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
770 if($this->_display_opt["build"] != "hidden") {
771 if($this->_item["lastBuildDate"]){$this->_output .= "build: ". $this->_item["lastBuildDate"]."<br />";}
773 if($this->_display_opt["cache_update"] != "hidden" && ( $_update = $this->get_cache_update_time()) ) {
774 $this->_output .= "cache update: ".$_update."<br />\n";
776 $this->_output .= "<a href=\"".$this->_item["link"]."\" ";
777 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
778 $this->_output .= ">".$this->_cut_string($this->_item["link"])."</a>\n";
779 $this->_output .= "</td></tr>\n";
780 $this->_output .= "</table></td></tr>\n";
782 $this->_array_channel = array( "title"=>$this->_item["title"],
783 "link"=>$this->_item["link"],
784 "description"=>$this->_item["description"],
785 "lastBuildDate"=>$this->_item["lastBuildDate"]);
786 $this->_clear_Items();
787 $this->_clear_cItems();
790 if(isset($this->_display_opt["textinput"]) && ($this->_display_opt["textinput"] != "hidden") && $this->_item["name"] && $this->_item["link"]) {
791 $this->_output .= "<tr><td class=\"fase4_rdf\">\n";
792 $this->_output .= "<form action=\"".$this->_item["link"]."\" ";
793 if(isset($this->_link_target)) { $this->_output .= "target=\"".$this->_link_target."\" "; }
794 $this->_output .= "method=\"get\">\n";
795 $this->_output .= "<div class=\"fase4_rdf_title\">".$this->_item["title"]."</div>";
796 $this->_output .= strip_tags($this->_item["description"], "<a>, <img>")."<br><br>\n";
797 $this->_output .= "<input class=\"fase4_rdf_input\" type=\"text\" name=\"".$this->_item["name"]."\"> \n";
798 $this->_output .= "<input class=\"fase4_rdf_input\" type=\"submit\" value=\"go\">";
799 $this->_output .= "</form>\n";
800 $this->_output .= "</td></tr>\n";
801 $this->_array_textinput = array( "title"=>$this->_item["title"],
802 "name"=>$this->_item["name"],
803 "link"=>$this->_item["link"],
804 "description"=>$this->_item["description"]);
805 $this->_clear_Items();
806 } elseif( isset($this->_display_opt["textinput"]) && ($this->_display_opt["textinput"] == "hidden") ) {
807 $this->_clear_Items();
815 * This Method returns the data from the <channel /> paragraph.
818 * @author Stefan Saasen <s@fase4.com>
820 * @see _array_channel
822 function get_array_channel( )
824 return $this->_array_channel;
828 * This Method returns the data from each <item /> paragraph.
831 * @author Stefan Saasen <s@fase4.com>
835 function get_array_item( )
837 return $this->_array_item;
841 * This Method returns the data from <textinput />.
844 * @author Stefan Saasen <s@fase4.com>
846 * @see _array_textinput
848 function get_array_textinput( )
850 return $this->_array_textinput;
854 * This Method returns the data from <image />.
857 * @author Stefan Saasen <s@fase4.com>
861 function get_array_image( )
863 return $this->_array_image;
867 * XML Parser Data Handler
870 * @author Stefan Saasen <s@fase4.com>
871 * @param mixed $parser a reference to the XML parser calling the handler.
872 * @param string $text contains the character data as a string.
873 * @see _parse_mode, _item, _tags, _depth, _citem, _ctags, _cdepth
875 function _parseData($parser, $text)
877 // Deocing mode added by Roland Haeder <webmaster@mxchange.org>
878 switch ($this->_decoding_mode)
881 $text = utf8_decode($text);
885 $text = htmlentities($text);
889 $clean = preg_replace("/\s/", "", $text);
891 $text = preg_replace("/^\s+/", "", $text)."\n";
892 if($this->_parse_mode == "all") {
893 if ( isset($this->_item[$this->_tags[$this->_depth[$parser]]]) &&
894 $this->_item[$this->_tags[$this->_depth[$parser]]] ) {
895 $this->_item[$this->_tags[$this->_depth[$parser]]] .= $text;
897 $this->_item[$this->_tags[$this->_depth[$parser]]] = $text;
899 } elseif (isset($this->_parse_mode) && $this->_parse_mode == "channel") {
900 if ( isset($this->_citem[$this->_ctags[$this->_cdepth[$parser]]]) ) {
901 $this->_citem[$this->_ctags[$this->_cdepth[$parser]]] .= $text;
903 $this->_citem[$this->_ctags[$this->_cdepth[$parser]]] = $text;
910 * This Method allows you to choose if specific Parameters are displayed or not. These are:
911 * image, channel, textinput, build and cache_update. If set to "hidden" those elements won't be displayed.
914 * @author Stefan Saasen <s@fase4.com>
915 * @param array $options
918 function set_Options( $options = "" )
920 if(is_array( $options )) {
921 $this->_display_opt = $options;
924 unset($this->_display_opt);
930 * This Method allows you to define the width of the table that holds the representation of the rdf/rss file.
933 * @author Stefan Saasen <s@fase4.com>
934 * @param int $width attribute width in tag <table>
937 function set_table_width( $width = 400 )
939 $this->_table_width = $width;
944 * This Method returns an assocative Array with available Options.
946 * The Keys are the Name of the Options to be set.
947 * The Values are short Description of available Options.
950 * @author Stefan Saasen <s@fase4.com>
951 * @return array $options
954 function get_Options()
956 $options = array( "image"=>"If 'image' is set to \"hidden\" no image provided by the RDF Publisher will be displayed.",
957 "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",
958 "textinput"=>"If set to \"hidden\" no Input Form will be displayed",
959 "build"=>"If set to \"hidden\" the Build Date (if provided) of the RDF File will not be displayed",
960 "cache_update"=>"If set to \"hidden\" the Update Date/Time of the cached Rdf File will not be displayed");
965 * This Method returns the Content of the RDF File in one string. The String actually holds the whole XML Document.
968 * @author Stefan Saasen <s@fase4.com>
969 * @param string $rdf RDF File (Location)
970 * @return string XML Presentation of parsed RDF File
971 * @see _cached_file, _remote_file, _cache_dir, _refresh, _update_cache()
975 // checks if the cache directory already exists
976 // if not, the cache directory will be created
977 if(!$this->_cache_dir_ok) {
978 $this->_create_cache_dir();
980 if($this->_use_dynamic_display == true) {
981 $this->_cached_file = md5("dynamic".$this->salt.$this->_remote_file);
982 $this->_cache_type = "normal";
984 $this->_cached_file = md5($this->salt.$this->_remote_file);
985 $this->_cache_type = "fast";
988 $_cache_f = $this->_cache_dir.$this->_cached_file;
990 if ( (!file_exists($_cache_f)) || (filemtime($_cache_f) < $this->_refresh) || (filesize($_cache_f) == 0)) {
991 // We have to parse the remote file
992 $this->_use_cached_file = false;
993 // --> we want to provide proper Information for Use in
994 // get_cache_update_time()
996 if($this->_use_dynamic_display == true) {
997 $_rdf = @implode(" ", $this->_rdf_data()); // -> proxy
999 $this->_throw_exception( $this->_remote_file." is not available" );
1001 $this->_parse_xRDF( $_rdf );
1002 $this->_update_cache( $_rdf );
1003 $data = $this->_output;
1005 $_rdf = @implode(" ", $this->_rdf_data()); // -> proxy
1007 $this->_throw_exception( $this->_remote_file." is not available" );
1009 $this->_parse_xRDF( $_rdf );
1010 $this->_update_cache( $this->_output );
1011 $data = $this->_output;
1014 // we can use the cached file
1015 $this->_use_cached_file = true;
1016 if($this->_use_dynamic_display == true) {
1017 $this->_parse_xRDF( implode(" ", file($_cache_f)) );
1018 $data = $this->_output;
1020 $data = @implode(" ", file($_cache_f));
1027 * This Methods creates the Cache Directory if the specified Directory does not exist.
1030 * @author Stefan Saasen <s@fase4.com>
1031 * @param string $dir Path to Directory.
1033 * @see _cache_dir, _cache_dir_ok
1035 function _create_cache_dir()
1038 if(!@is_dir($this->_cache_dir)) {
1039 $arr = explode("/", $this->_cache_dir);
1044 for($i = 0;$i<$c;$i++)
1047 $path .= $arr[$i]."/";
1048 if(!@is_dir($path)) {
1049 if(!@mkdir($path, 0777)) {
1050 $this->_throw_exception("failed to create directory:<b>".$this->_cache_dir."</b>.<br /><br />Exception on Line: ".__LINE__);
1056 $this->_cache_dir_ok = true;
1059 $this->_cache_dir_ok = true;
1062 } // END _create_cache_dir()
1065 * This Method updates the cached RDF Files and synchronises them with their remote Counterparts.
1068 * @author Stefan Saasen <s@fase4.com>
1069 * @param string $rdf RDF File (Location)
1070 * @see _cache_dir, _cached_file, _throw_exception()
1072 function _update_cache( $content = "" )
1074 $_local = @fopen( $this->_cache_dir.$this->_cached_file, 'w' );
1076 $this->_throw_exception( "Cannot open ".$this->_cached_file."<br /><br />Exception at Line: ".__LINE__ );
1079 if (fwrite( $_local, $content) === false) {
1080 $this->_throw_exception( "Cannot write to ".$this->_cached_file."<br /<br />Exeception at Line: ".__LINE__);
1084 @chmod( $this->_cache_dir.$this->_cached_file, 0666);
1086 } // END _update_cache()
1089 * This Method returns the Date/Time of last Cache Update of the actually parsed RDF File.
1092 * @author Stefan Saasen <s@fase4.com>
1093 * @return string Date/Time of last Update
1094 * @see _cache_dir, _cached_file
1096 function get_cache_update_time()
1098 return (file_exists($this->_cache_dir.$this->_cached_file))?date("d.m.Y H:i:s", filemtime($this->_cache_dir.$this->_cached_file)):"Cachemiss";
1099 } // END get_cache_update_time()
1102 * This Method returns the Type of Cache that was used ('normal' or 'fast')
1105 * @author Stefan Saasen <s@fase4.com>
1106 * @param string $rdf RDF File (Location)
1107 * @return string Displays RDF Content ( using _display() )
1108 * @see _remote_file, cache()
1110 function get_CacheType()
1112 return $this->_cache_type;
1116 * Returns true if cached file was used, otherwise false
1119 * @author Stefan Saasen <s@fase4.com>
1120 * @return array $options
1121 * @see _use_cached_file
1123 function is_cachedFile()
1125 return $this->_use_cached_file;
1129 * This Method deletes all the cached Files.
1131 * Please keep in mind to use this method just as a 'manual garbage collection'
1132 * You should cache the rss/rdf files locally to avoid unnecessary traffic.
1133 * (Both for your visitors and the Publisher)
1136 * @author Stefan Saasen <s@fase4.com>
1139 function clear_cache()
1141 $dir = dir($this->_cache_dir);
1142 while($file=$dir->read()) {
1143 if($file!="." && $file!="..") {
1144 if(!@unlink($dir->path.$file)) {
1145 $this->_throw_exception(
1146 "Unable to unlink ".$dir->path.$file
1147 ."<br /><br />Exception at Line: ".__LINE__ );
1154 } // END clear_cache()
1157 * Cuts the String $string after $str_len and adds "... "
1160 * @param string $string String to be shortened
1161 * @param int $str_len length of the returned String (overall length including "... ")
1162 * @return string Cut String
1164 function _cut_string( $string, $str_len = "30" )
1166 if(strlen(trim($string))>$str_len) {
1167 $string = substr( trim($string) , 0, $str_len - 4);
1171 } // END _cut_string()
1174 * this Method implements simple Garbage Collection
1177 * @author Stefan Saasen <s@fase4.com>
1178 * @see _cache_dir, gc_probability, gc_maxlifetime
1180 function _garbage_collection()
1182 srand((double) microtime() * 1000000);
1183 if (rand(1, 100) <= $this->gc_probability) {
1184 $dir = dir($this->_cache_dir);
1185 while($file=$dir->read()) {
1186 if($file!="." AND $file!=".." AND filemtime($dir->path.$file) <= time() - $this->_refresh ) {
1187 @unlink($dir->path.$file);
1194 /* ==== Proxy/Auth methods ==== */
1197 * this method sets a proxy server
1200 * @param string $phost Proxy Host
1201 * @param string $pport Prox Port
1202 * @author Marco Kraus <marco.kraus@siemens.com>
1204 function set_proxy($phost, $pport)
1206 $this->_use_proxy = true;
1209 $this->_phost = $phost;
1212 $this->_pport = $pport;
1216 * this method sets a proxy server authentification
1219 * @param string $pname Username
1220 * @param string $ppaswd Password
1221 * @author Marco Kraus <marco.kraus@siemens.com>
1223 function set_proxy_auth( $pname, $ppasswd )
1225 $this->_use_proxy_auth = true;
1228 $this->_pname = $pname;
1231 $this->_ppasswd = $ppasswd;
1236 * gets _remote_file into an array
1238 * needed, cause if you use a proxy, you have to open
1239 * a raw-tcp-socket to get the data
1242 * @author Marco Kraus <Marco.Kraus@siemens.com>
1244 * @see _use_proxy, cache()
1246 function _rdf_data()
1248 if ( $this->_use_proxy == true )
1250 // we need a raw socket here to connect to proxy
1251 $fp = fsockopen($this->_phost,$this->_pport);
1254 $this->_throw_exception( $this->_remote_file." is not available with proxy" );
1256 if ( $this->_use_proxy_auth == true ) {
1257 fputs($fp, "GET ".$this->_remote_file." HTTP/1.0\r\nUser-Agent: Fase4 RDF-Reader/1.40 modified by Quix0r\r\n\r\n");
1259 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");
1264 for ( $i = 0; !feof ($fp) ; $i++)
1266 $usable_data[$i] = "";
1267 $usable_data[$i] = fgets($fp,4096);
1269 // PARSE HEADER ---- first line has to be <?xml, second rdf or rss, and third is blank
1271 // strstr did not fit (ask Rasmus why), so we compare each character
1272 if ( ($usable_data[$i][0] == "<" ) &&
1273 ($usable_data[$i][1] == "?" ) &&
1274 ($usable_data[$i][2] == "x" ) &&
1275 ($usable_data[$i][3] == "m" ) &&
1276 ($usable_data[$i][4] == "l" ) ) {
1277 $usable_data[0] = $usable_data[$i]; // save current field
1278 $i = 1; // just reset array to start
1281 // there seems to be proxystuff after the <?xml....we delete this
1283 ($usable_data[$i][0] == "<" ) &&
1284 ($usable_data[$i][1] == "r" ) &&
1285 ($usable_data[$i][2] == "d" ) &&
1286 ($usable_data[$i][3] == "f" ) &&
1287 ($usable_data[$i][4] == ":" )
1291 ($usable_data[$i][0] == "<" ) &&
1292 ($usable_data[$i][1] == "r" ) &&
1293 ($usable_data[$i][2] == "s" ) &&
1294 ($usable_data[$i][3] == "s" )
1298 $usable_data[1] = $usable_data[$i]; // save current field
1299 $usable_data[2] = "\n";
1300 $i = 2; // just reset array to start
1305 return $usable_data;
1307 if (substr($this->_remote_file, 0, 7) != "http://") {
1308 $this->_throw_exception( "Cannot find http:// in ".$this->_remote_file."!" );
1311 // Extract host information
1312 $host = substr($this->_remote_file, 7);
1313 // Extract the GET part
1315 if (strpos($host, "/") > 0) {
1316 $get = substr($host, strpos($host, "/"));
1317 $host = substr($host, 0, strpos($host, "/"));
1321 if (strpos($host, ":") > 0) {
1322 $port = substr($host, (strpos($host, ":") + 1));
1323 $host = substr($host, 0, (strpos($host, ":") - 1));
1326 // Start connection to server
1327 $fp = fsockopen($host, $port);
1329 $this->_throw_exception( $this->_remote_file." is maybe not available." );
1332 // Repare request line
1333 $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);
1335 fputs($fp, $request);
1336 $reply = ""; $isContent = false; $dummy = "";
1338 while ( !feof($fp) ) {
1339 $read = trim(fgets($fp, 4096));
1340 if (substr($read, 0, 5) == "<?xml" || $isContent) {
1350 if ((count($dummy) > 0) && (count($reply) == 0) && (!$isContent)) {
1351 // Transfer content from dummy
1355 //die(htmlentities($reply));
1359 } // END _rdf_data()