Location: PHPKode > projects > Simple Way to Usenet > libs/swun/nzb/nzb.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 nzb {
		
	public static function isnzb($file){
		$dom = new DOMDocument();
		if ( FALSE === $dom->Load($file) ){ return FALSE; }
		return $dom->validate();
	}
	
	public static function parse($file){
		if ( FALSE === self::isnzb($file) ){ return FALSE; }
		$XML = new XMLReader();
		$XML->open( $file );
		$XML->setParserProperty(XMLReader::VALIDATE, FALSE);
		$XML->setParserProperty(XMLReader::LOADDTD,  FALSE);
		while( $XML->read() ) {
			if ( $XML->name == 'file' && $XML->nodeType == 1 ){
				$buf['poster'] 	= $XML->getAttribute('poster');
				$buf['date'] 	= $XML->getAttribute('date');
				$buf['subject']	= $XML->getAttribute('subject');
				//we got file header info, now need to fetch groups
				while( $XML->read() ) {
					if ( $XML->name == 'group' && $XML->nodeType == 1 ){
						$XML->read();
						$buf['groups'][] = $XML->value;
					} elseif( $XML->name == 'groups' && $XML->nodeType == 15 ){	break; }
				}
				//now we got the file groups, lets fetch the file segments
				while( $XML->read() ) {
					if ( $XML->name == 'segment' && $XML->nodeType == 1 ){
						$seg['bytes'] = $XML->getAttribute('bytes');
						$seg['number'] = $XML->getAttribute('number');
						$XML->read();
						$seg['segment'] = $XML->value;
						$buf['segments'][] = $seg;
						unset($seg);
					} elseif( $XML->name == 'segments' && $XML->nodeType == 15 ){	break; }
				}
				$ar[] = $buf;
				unset($buf);
			}
		}
		$XML->close();
		if ( !isset($ar) ){ return FALSE; }
		return $ar;
	}
	
	public static function getsize($file){
		if ( FALSE === self::isnzb($file) ){ return FALSE; }
		$XML = new XMLReader();
		if ( FALSE === $XML->open( $file ) ){ return FALSE; }
		$XML->setParserProperty(XMLReader::LOADDTD,  FALSE);
		$XML->setParserProperty(XMLReader::VALIDATE, FALSE);
		$tot=0;
		while( $XML->read() ) {
			if ( $XML->name == 'segment' && $XML->nodeType == 1 ){
				$tot += $XML->getAttribute('bytes');
			}
		}
		$XML->close();
		return $tot;
	}
	
	public static function count_files($file){
		if ( FALSE === self::isnzb($file) ){ return FALSE; }
		$XML = new XMLReader();
		if ( FALSE === $XML->open( $file ) ){ return FALSE; }
		$XML->setParserProperty(XMLReader::LOADDTD,  FALSE);
		$XML->setParserProperty(XMLReader::VALIDATE, FALSE);
		$tot=0;
		while( $XML->read() ) {
			if ( $XML->name == 'file' && $XML->nodeType == 1 ){ $tot++; }
		}
		$XML->close();
		return $tot;		
	}
	
	public static function count_segments($file){
		if ( FALSE === self::isnzb($file) ){ return FALSE; }
		$XML = new XMLReader();
		if ( FALSE === $XML->open( $file ) ){ return FALSE; }
		$XML->setParserProperty(XMLReader::LOADDTD,  FALSE);
		$XML->setParserProperty(XMLReader::VALIDATE, FALSE);
		$tot=0;
		while( $XML->read() ) {
			if ( $XML->name == 'segment' && $XML->nodeType == 1 ){ $tot++; }
		}
		$XML->close();
		return $tot;
	}
	
	public static function count_filesegments($file, $subject){
		if ( FALSE === self::isnzb($file) ){ return FALSE; }
		$XML = new XMLReader();
		if ( FALSE === $XML->open( $file ) ){ return FALSE; }
		$XML->setParserProperty(XMLReader::LOADDTD,  FALSE);
		$XML->setParserProperty(XMLReader::VALIDATE, FALSE);
		$tot=0;
		while( $XML->read() ) {
			if ( $XML->name == 'file' && $XML->nodeType == 1 ){
				if ( $XML->getAttribute('subject') == $subject ){
					while( $XML->read() ) {
						if ( $XML->name == 'segment' && $XML->nodeType == 1 ){ $tot++; 
						} elseif( $XML->name == 'segments' && $XML->nodeType == 15 ){	break; }
					}
					if ( $tot !== 0 ){ break; }
				}
			}
		}
		$XML->close();
		return $tot;
	}
	
	public static function gen_frominfo($infos){
		if ( ! CORE::isphpModLoaded('XMLWriter') ){
			if ( ! CORE::LoadphpMod('XMLWriter') ){ return FALSE; }
		}
		$xw = new XMLWriter();
	    $xw->openMemory();
  		$xw->startDocument('1.0','UTF-8');
  		$xw->startElement('nzb');
  		$xw->writeAttribute('xmlns', 'http://newzbin.com/DTD/2003/nzb');
  		if ( !is_array($infos['files']) ){ return FALSE; }
  		foreach($infos['files'] as $k => $file ){
	  		$xw->startElement('file');
  			$xw->writeAttribute('poster', $file['poster']);
	  		$xw->writeAttribute('date', $file['date']);
	  		$xw->writeAttribute('subject', $file['subject']);
			$xw->startElement('groups');
			if ( is_array($file['groups']) ){
				foreach($infos['groups'] as $k => $group){ $xw->writeElement('group', $group); }
			} elseif ( ereg('', $infos['groups']) ){
				$xw->writeElement('group', $infos['groups']);
			} else {
				$xw->writeElement('group', 'alt.binaries.unknow');
			}
			$xw->endElement('groups');
			$xw->startElement('segments');
	  		if ( is_array($file['segments']) ){
				foreach($infos['segments'] as $k => $segment){ 
					$xw->startElement('segment');
					$xw->writeAttribute('bytes', $segment['bytes']);
					$xw->writeAttribute('number', $k);
					$xw->writeCData($segment['unid']);
					$xw->endElement('segment');
				}
	  		} else {
	  			$xw->startElement('segment');
				$xw->writeAttribute('bytes', 0);
				$xw->writeAttribute('number', 1);
				$xw->writeCData('unknow');
				$xw->endElement('segment');
	  		}
			$xw->endElement('segments');
			$xw->endElement('file');
  		}
		$xw->endElement('nzb');
		return $xw->outputMemory(FALSE);
	}

	public static function gen_fromid($id){
		//TODO, bcause less mem needed.
		return FALSE;	
	}
}
Return current item: Simple Way to Usenet