Location: PHPKode > projects > Simple Way to Usenet > libs/swun/rpc/rpcbrw_item.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */
class rpcbrw_item {
	
	private $REPO, $rpc, $catid, $path, $title, $engine, $id, $content, $hash, $date;

	public function __construct($rpc, $path, $catid=''){ 
		//echo "New ITEM [$rpc] [$path] [$catid]\n";
		$this->path = $path;
		$this->rpc = $rpc;
		$rpcrepo = hellarpcs::get_destrepo($this->rpc);
		$this->REPO = new file_abstract($rpcrepo);
		$err = $this->REPO->connect();
		if ( CORE::isError($err) ){ errors::broadcast($err); }
		$this->catid = $catid;
	}
	
	public function get_rpc(){ return $this->rpc;}
	
	public function get_catid(){
		if ( isset($this->catid) ){ return $this->catid; } 
		//check for id, if none attempt from path
		return $this->catid;
	}
	
	public function get_title(){
		if ( isset($this->title) ){ return $this->title; }
		$buf = split('/',$this->path);
		$this->title = $buf[count($buf)-1];
		return $this->title;
	}
	
	public function get_engine(){ 
		if ( isset($this->engine) ){ return $this->engine; }
		$this->detect_swunid();
		return $this->engine;		
	}

	public function get_id(){ 
		if ( isset($this->id) ){ return $this->id; }
		$this->detect_swunid();
		return $this->id;
	}
	
	
	private function detect_swunid(){
		$this->get_content();
		if ( count($this->content['file']) == 0 ){ return; }
		foreach($this->content['file'] as $k => &$f ){ //([0-9]?)_
			if ( ereg(".nzb", $f['title']) AND ereg("^[0-9]+_", $f['title']) ){
				$file = $f['title'];
				//echo "\n posof _ in $file => ".strpos($file, '_')."\n";
				$DID = substr($file, 0, strpos($file, '_'));
				$DID = hellarpcs::splitdid($DID);
				$this->engine = $DID['type'];
				$this->id = $DID['id'];
				$this->date = $this->REPO->getmtime($this->path.'/'.$file);
				return;
			}
		}
		$this->engine = '';
		$this->id = '';
		return;
	}
	
	public function get_date(){
		if ( isset($this->date) ){ return $this->date; }
		$this->get_content();
		if ( isset($this->content['file']) ){
			foreach($this->content['file'] as $k => &$f ){
				if ( ereg(".nzb", $f['title']) ){
					$this->date = $this->REPO->getmtime($this->path.'/'.$f['title']);
					return $this->date;		
				}
			}
		}
		$this->date = $this->REPO->getmtime($this->path);
		if ( $this->date == 0 ){ $this->date = time(); }
		return $this->date;
		
	}
	
	public function get_content(){ 
		if ( isset($this->content) ){ return $this->content; }
		$this->content = self::recur_folder_content($this->path, &$this->REPO);
		//print_r($this->content);
		return $this->content;		
	}
	
	private static function recur_folder_content($base, &$REPO){
		$s=0;
		$f=0;
		$d=0;
		while ( FALSE !== ($item = $REPO->readdir($base)) ){
			if ( $REPO->isdir("$base/$item") ){
				$o['dir'][$d] = Array('title'=>$item, 'content'=>self::recur_folder_content("$base/$item", &$REPO) );
				$s += $o['dir'][$d]['content']['size'];
				$f += $o['dir'][$d]['content']['cf'];
				$d += $o['dir'][$d]['content']['cd'];
				$d++;
			} else {
				$o['file'][$f] = Array('title'=>$item, 'size'=>$REPO->getsize("$base/$item") );
				$s += $o['file'][$f]['size'];  
				$f++;
			}
		}
		if ( !isset($o) ){ 
			$o = Array('size'=>0,'dir'=>Array(), 'file'=>Array(),'cf'=>0, 'cd'=>0); 
		} else { 
			$o['size'] = $s;
			$o['cf'] = $f;
			$o['cd'] = $d;
			if ( !isset($o['file']) ){ $o['file'] = Array(); }
			if ( !isset($o['dir']) ){ $o['dir'] = Array(); } 
		}
		return $o;
	}

		
	public function get_hash(){
		if ( isset($this->hash) ){ return $this->hash; }
		$this->hash = md5(serialize($this->get_content()).$this->get_date());
		return $this->hash;
	}
	
}
Return current item: Simple Way to Usenet