eddi: CacheRemoteFile Klasse

Beitrag lesen

Moi moin!

* Gibt es in PHP nicht die Möglichkeit, den Status einer HTTP-Abfrage zu ermitteln? Dann könnte die Klasse zusätzlich auf HTTP 304 prüfen.

Gute Idee:

  
class CRF{  
 function CRF($url,$cache='',$life=36){  
  $this->cache =($cache!='') ? $cache : md5($url ).'.cache';  
  $this->content ='';  
  $this->error =array();  
  $this->host =$this->split_url($url);  
  $this->http =array( 'date'=>'',  
     'etag'=>'',  
     'content-md5'=>''  
    );  
  $f=TRUE;  
  
  if(file_exists($this->cache)){  
   clearstatcache();  
   $mtime=filemtime($this->cache);  
   if($mtime && (time()-$life)<$mtime)  
    if($this->get_cache())  
     $f=FALSE;  
  }  
  if($f)  
   if($this->get_new())  
    $this->write();  
 }  
 function split_url($url){  
  if(strpos($url,'://')!==false)  
   list($proto,$host) =explode('://',$url,2);  
  else $host=$url;  
  if(strpos($host,'/')!==false){  
   list($host,$path) =explode('/',$host,2);  
   $path='/'.$path;  
   }  
  else $path='/';  
  if(strpos($host,':')!==false)  
   list($host,$port) =explode(':',$host);  
  if(($proto=strtolower($proto))=='https' && !$port)  
   $port=443;  
  elseif(!$port)  
   $port=80;  
  
  return(array($host,$port,$path,$proto));  
 }  
 function get_cache(){  
  if(is_resource($dat=@fopen($this->cache,'r'))){  
   $this->http['date']  =trim(fgets($dat));  
   $this->http['etag']  =trim(fgets($dat));  
   $this->http['content-md5'] =trim(fgets($dat));  
   $this->content   =fread($dat,filesize($this->cache));  
   fclose($dat);  
   return(TRUE);  
  }  
  else $this->error[]='da is nischt mit Lesen im Käsch';  
  
  return(FALSE);  
 }  
 function get_new(){  
  if(file_exists($this->cache))  
   $this->get_cache();  
  
  $http=array();  
  $http[]='GET '.$this->host[2].' HTTP/1.1';  
  $http[]='Host: '.$this->host[0].($this->host[1]!=80 ? ':'.$this->host[1] : '');  
  
  if($this->http['date'])  $http[]='If-Modified-Since: '. $this->http['date'];  
  if($this->http['etag'])  $http[]='If-None-Match: '. $this->http['etag'];  
  
  $http[]="Connection: close\r\n";  
  
  if(floatval(substr(phpversion(),0,3))>=6.0){  
   if($this->host[3]=='https')  
    if(in_array('ssl',stream_get_transports()))  
     $c=@stream_socket_client('ssl://'.$this->host[0].':'.$this->host[1]);  
    else $c=FALSE;  
   else $c=@stream_socket_client('tcp://'.$this->host[0].':'.$this->host[1]);  
  }  
  else{  
   if($this->host[3]=='https')  
    $c=@fsockopen('ssl://'.$this->host[0],$this->host[1]);  
   else $c=@fsockopen($this->host[0],$this->host[1]);  
  }  
  if(!is_resource($c)){  
   $this->error[]='Keene Verbindung';  
   return(FALSE);  
  }  
  foreach($http as $v)  
   fwrite($c,$v."\r\n");  
  
  $t=substr(fgets($c),9,3);  
  if(!in_array($t,array(200,304))){  
   fclose($c);  
   $this->error[]='Serva will nich und sacht: '.$t;  
   return(FALSE);  
  }  
  elseif($t==304){  
   fclose($c);  
   touch($this->cache);  
   return(TRUE);  
  }  
  while($t!="\r\n"){  
   $t=fgets($c);  
   $x=explode(':',$t,2);  
   $x[0]=strtolower($x[0]);  
   $x[1]=(isset($x[1])) ? trim($x[1]) : '';  
  
   if($x[0]=='date'){  
    if($this->http[$x[0]]==$x[1]){  
     fclose($c);  
     touch($this->cache);  
     return(TRUE);  
    }  
    $this->http[$x[0]]=$x[1];  
   }  
   elseif($x[0]=='etag'){  
    if($this->http[$x[0]]==$x[1]){  
     fclose($c);  
     touch($this->cache);  
     return(TRUE);  
    }  
    $this->http[$x[0]]=$x[1];  
   }  
   elseif($x[0]=='content-md5'){  
    if($this->http[$x[0]]==$x[1]){  
     fclose($c);  
     touch($this->cache);  
     return(TRUE);  
    }  
    $this->http[$x[0]]=$x[1];  
   }  
  }  
  $this->content='';  
  while(!feof($c))  
   $this->content.=fgets($c);  
  
  fclose($c);  
  
  return(TRUE);  
 }  
 function write(){  
  touch($this->cache);  
  if(!is_resource($dat=fopen($this->cache,"w"))){  
   $this->error[] = "Zum Schreiben zu doof ".$this->cache;  
   return(FALSE);  
  }  
  foreach($this->http as $v)  
   fwrite($dat,$v."\n");  
  
  fwrite($dat,$this->content);  
  fclose($dat);  
  return(TRUE);  
 }  
}  

Gruß aus Berlin!
eddi