⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdf.class.php

📁 基于Fase 4 RDF/RSS语法分析器
💻 PHP
📖 第 1 页 / 共 3 页
字号:
        } else {            unset($this->_display_opt);            return false;        }    }        /**    * This Method allows you to define the width of the table that holds the representation of the rdf/rss file.    *    * @access    public	* @author    Stefan Saasen <s@fase4.com>    * @param     int  $width  attribute width in tag <table>	* @see		 _table_width    */                function set_table_width( $width = 400 )    {        $this->_table_width = $width;        return true;    }        /**    * This Method returns an assocative Array with available Options.    *    * The Keys are the Name of the Options to be set.     * The Values are  short Description of available Options.    *    * @access    public	* @author    Stefan Saasen <s@fase4.com>    * @return    array  $options	* @see		 _display_opt    */            function get_Options()    {        $options = array(   "image"=>"If 'image' is set to \"hidden\" no image provided by the RDF Publisher will be displayed.",                            "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",                            "textinput"=>"If set to \"hidden\" no Input Form will be displayed",                            "build"=>"If set to \"hidden\" the Build Date (if provided) of the RDF File will not be displayed",                            "cache_update"=>"If set to \"hidden\" the Update Date/Time of the cached Rdf File will not be displayed");        return $options;    }        /**    * This Method returns the Content of the RDF File in one string. The String actually holds the whole XML Document.    *    * @access    public	* @author    Stefan Saasen <s@fase4.com>    * @param     string $rdf    RDF File (Location)    * @return    string XML Presentation of parsed RDF File	* @see		 _cached_file, _remote_file, _cache_dir, _refresh, _update_cache()    */                        function cache()    {        // checks if the cache directory already exists        // if not, the cache directory will be created        if(!$this->_cache_dir_ok) {            $this->_create_cache_dir();        }        if($this->_use_dynamic_display == true) {            $this->_cached_file = md5("dynamic".$this->_remote_file);            $this->_cache_type = "normal";        } else {            $this->_cached_file = md5($this->_remote_file);            $this->_cache_type = "fast";                    }                $_cache_f = $this->_cache_dir.$this->_cached_file;        if ( (!file_exists($_cache_f)) || (filemtime($_cache_f) < $this->_refresh) || (filesize($_cache_f) == 0)) {        // We have to parse the remote file        $this->_use_cached_file = false;            // --> we want to provide proper Information for Use in             // get_cache_update_time()            clearstatcache();            if($this->_use_dynamic_display == true) {                $_rdf = @implode(" ", $this->_rdf_data()); // -> proxy                if(!$_rdf) {                    $this->_throw_exception( $this->_remote_file." is not available" );                }                $this->_parse_xRDF( $_rdf );                $this->_update_cache( $_rdf );                $data = $this->_output;            } else {                $_rdf = @implode(" ", $this->_rdf_data()); // -> proxy                if(!$_rdf) {                    $this->_throw_exception( $this->_remote_file." is not available" );                }                                $this->_parse_xRDF( $_rdf );                $this->_update_cache( $this->_output );                $data = $this->_output;            }        } else {        // we can use the cached file        $this->_use_cached_file = true;                    if($this->_use_dynamic_display == true) {                $this->_parse_xRDF( implode(" ", file($_cache_f)) );                $data = $this->_output;            } else {                        $data = @implode(" ", file($_cache_f));            }        }        return trim($data);    }   // END cache()    /**    * This Methods creates the Cache Directory if the specified Directory does not exist.    *    * @access    private	* @author    Stefan Saasen <s@fase4.com>    * @param     string $dir Path to Directory.	* @return 	 boolean	* @see		 _cache_dir, _cache_dir_ok    */            function _create_cache_dir()    {        if(!@is_dir($this->_cache_dir)) {            $arr = explode("/", $this->_cache_dir);            $c = count($arr);            if($arr[0]=="") {                $path = "/";            }            for($i = 0;$i<$c;$i++)            {                if($arr[$i]!="") {                    $path .= $arr[$i]."/";                    if(!@is_dir($path)) {                       if(!@mkdir($path, 0777)) {                             $this->_throw_exception("failed to create directory:<b>".$this->_cache_dir."</b>.<br /><br />Exception on Line: ".__LINE__);                        return false;                        }                                        }                }            }            $this->_cache_dir_ok = true;            return true;        } else {            $this->_cache_dir_ok = true;            return true;            }    }   // END _create_cache_dir()        /**    * This Method updates the cached RDF Files and synchronises them with their remote Counterparts.    *    * @access    private	* @author    Stefan Saasen <s@fase4.com>    * @param     string $rdf    RDF File (Location)	* @see		 _cache_dir, _cached_file, _throw_exception()    */                        function _update_cache( $content = "" )    {             $_local = @fopen( $this->_cache_dir.$this->_cached_file, "w" );             if(!$_local) {                 $this->_throw_exception( "Cannot open ".$this->_cache_dir.$this->_cached_file."<br /><br />Exception at Line: ".__LINE__ );                 return false;             }             if(!fwrite( $_local, $content)) {                 $this->_throw_exception( "Cannot write to: ".$this->_cached_file."<br /><br />Exception at Line: ".__LINE__ );                 return false;             }                          fclose( $_local );              return true;    }   // END _update_cache()        /**    * This Method returns the Date/Time of last Cache Update of the actually parsed RDF File.	*    * @access    public	* @author    Stefan Saasen <s@fase4.com>	* @return 	 string Date/Time of last Update	* @see		 _cache_dir, _cached_file    */        function get_cache_update_time()    {               return (file_exists($this->_cache_dir.$this->_cached_file))?date("d.m.Y H:i:s", filemtime($this->_cache_dir.$this->_cached_file)):"Cachemiss";    }   // END get_cache_update_time()    /**    * This Method returns the Type of Cache that was used ('normal' or 'fast')    *    * @access    public	* @author    Stefan Saasen <s@fase4.com>    * @param     string $rdf    RDF File (Location)    * @return    string Displays RDF Content ( using _display() )	* @see		 _remote_file, cache()    */        function get_CacheType()    {        return $this->_cache_type;    }        /**    * Returns true if cached file was used, otherwise false    *    * @access    public	* @author    Stefan Saasen <s@fase4.com>    * @return    array  $options	* @see		 _use_cached_file    */            function is_cachedFile()    {        return $this->_use_cached_file;    }        /**    * This Method deletes all the cached Files.    *    * Please keep in mind to use this method just as a 'manual garbage collection'    * You should cache the rss/rdf files locally to avoid unnecessary traffic.     * (Both for your visitors and the Publisher)	*    * @access    public	* @author    Stefan Saasen <s@fase4.com>	* @see		 _cache_dir    */        function clear_cache()    {        $dir = dir($this->_cache_dir);        while($file=$dir->read()) {            if($file!="." && $file!="..")  {                if(!@unlink($dir->path.$file)) {                    $this->_throw_exception(                     "Unable to unlink ".$dir->path.$file                    ."<br /><br />Exception at Line: ".__LINE__ );                     return false;                }            }        }        $dir->close();        return true;    }   // END clear_cache()	    /**    * Cuts the String $string after $str_len and adds "... "    *    * @access   private    * @param    string  $string String to be shortened    * @param    int     $str_len length of the returned String (overall length including "... ")    * @return   string  Cut String    */    function _cut_string( $string, $str_len = "30" )    {        if(strlen(trim($string))>$str_len) {        $string = substr( trim($string) , 0, $str_len - 4);        $string .= " ...";        }        return $string;    }   // END _cut_string()        /**    * this Method implements simple Garbage Collection    *    * @access    private	* @author    Stefan Saasen <s@fase4.com>	* @see		 _cache_dir, gc_probability, gc_maxlifetime    */                function _garbage_collection()    {        srand((double) microtime() * 1000000);        if (rand(1, 100) <= $this->gc_probability) {            $dir = dir($this->_cache_dir);            while($file=$dir->read()) {                if($file!="." AND $file!=".." AND filemtime($dir->path.$file) <= time() - $this->_refresh )  {                @unlink($dir->path.$file);                }        }        $dir->close();        }   // END if    }        /* ==== Proxy/Auth methods ==== */       /**    * this method sets a proxy server    *    * @access    public    * @param     string $phost Proxy Host    * @param     string $pport Prox Port    * @author    Marco Kraus <marco.kraus@siemens.com>    */    function set_proxy($phost, $pport)    {     $this->_use_proxy = true;     if ($phost != "")        $this->_phost = $phost;     if ($pport != "")        $this->_pport = $pport;    }    /**    * this method sets a proxy server authentification    *    * @access    public    * @param     string $pname Username    * @param     string $ppaswd Password    * @author    Marco Kraus <marco.kraus@siemens.com>    */    function set_proxy_auth( $pname, $ppasswd )    {     $this->_use_proxy_auth = true;     if ($pname != "")        $this->_pname = $pname;     if ($ppasswd != "")        $this->_ppasswd = $ppasswd;    }   /**    * gets _remote_file into an array    *    * needed, cause if you use a proxy, you have to open     * a raw-tcp-socket to get the data    *    * @access    private    * @author    Marco Kraus <Marco.Kraus@siemens.com>    * @return array    * @see _use_proxy, cache()    */    function _rdf_data()    {      if ( $this->_use_proxy == true )      {       // we need a raw socket here to connect to proxy       $fp = fsockopen($this->_phost,$this->_pport);       if (!$fp) {           $this->_throw_exception( $this->_remote_file." is not available with proxy" );       } else {        if ( $this->_use_proxy_auth == true ) {           fputs($fp, "GET ".$this->_remote_file." HTTP/1.0\r\n\r\n");           } else {           fputs($fp, "GET ".$this->_remote_file." HTTP/1.0\r\nProxy-Authorization: Basic ".base64_encode("$this->_pname:$this->_ppasswd") ."\r\n\r\n");           }        }       for ( $i = 0; !feof ($fp) ; $i++)       {          $usable_data[$i] = "";          $usable_data[$i] = fgets($fp,4096);        // PARSE HEADER ---- first line has to be <?xml, second rdf or rss, and third is blank        // strstr did not fit (ask Rasmus why), so we compare each character               if ( ($usable_data[$i][0] == "<" ) &&               ($usable_data[$i][1] == "?" ) &&               ($usable_data[$i][2] == "x" ) &&               ($usable_data[$i][3] == "m" ) &&               ($usable_data[$i][4] == "l" ) ) {                    $usable_data[0] = $usable_data[$i]; // save current field                      $i = 1; // just reset array to start              }        // there seems to be proxystuff after the <?xml....we delete this            if ( (               ($usable_data[$i][0] == "<" ) &&               ($usable_data[$i][1] == "r" ) &&               ($usable_data[$i][2] == "d" ) &&               ($usable_data[$i][3] == "f" ) &&               ($usable_data[$i][4] == ":" )                )               ||               (               ($usable_data[$i][0] == "<" ) &&               ($usable_data[$i][1] == "r" ) &&               ($usable_data[$i][2] == "s" ) &&               ($usable_data[$i][3] == "s" )               )             ) {                $usable_data[1] = $usable_data[$i]; // save current field                $usable_data[2] = "\n";                $i = 2; // just reset array to start          }       }       fclose($fp);       return $usable_data;     } else {        return (file($this->_remote_file));     }   }    // END _rdf_data()}   // END class?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -