miniblog

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/miniblog.git
Log | Files | Refs

FileQueue.class.php (1879B)


      1 <?php
      2 
      3 require('Queue.class.php');
      4 
      5 define('QUEUE_TYPE_MAIL',1);
      6 define('QUEUE_TYPE_FILE_JSON',2);
      7 define('QUEUE_TYPE_FILE_SERIALIZE',3);
      8 
      9 class FileQueue extends Queue
     10 {
     11 	public $type;
     12 	public $directory;
     13 	
     14 	public function Queue($directory)
     15 	{
     16 		if (!is_dir($directory))
     17 			throw new Exception('Queue-Directory does not exist: '.$directory);
     18 		
     19 		$this->directory = $directory;
     20 	}
     21 	
     22 	function push( $entry )
     23 	{
     24 		if (!is_dir($this->directory))
     25 			throw new Exception('Queue-Directory does not exist: '.$this->directory);
     26 		
     27 		$entryDirName = $this->directory.'/'.time().'-'.rand(10000000,99999999);
     28 		mkdir($entryDirName);
     29 
     30 
     31 		mkdir($entryDirName.'/files');
     32 		$files = array();
     33 		foreach( $entry->files as $file)
     34 		{
     35 			if	( !is_file($file) )
     36 				throw new Exception('file does not exist: '.$file);
     37 			$files[] = $file;
     38 			copy($file,$entryDirName.'/files/'.basename($file));
     39 		}
     40 		
     41 		$value = array('value'=>$entry->value,'files'=>$files,'time'=>time(),'user'=>get_current_user());
     42 		$file = fopen($entryDirName.'/value','w');
     43 		fwrite($file,json_encode($value));
     44 		fclose($file);
     45 	}
     46 	
     47 	
     48 	public function pull()
     49 	{
     50 		if (!is_dir($this->directory))
     51 			throw new Exception('Queue-Directory does not exist: '.$this->directory);
     52 		
     53 		// Öffnen eines bekannten Verzeichnisses und danach seinen Inhalt einlesen
     54 		if ($dh = opendir($this->directory)) {
     55 			while (($file = readdir($dh)) !== false && is_dir($file) && substr($file,0,1) != '.')
     56 			{
     57 				$files[] = $file;
     58 			}
     59 			closedir($dh);
     60 			
     61 			if	( empty($files))
     62 				return null;
     63 			
     64 			$entryName = $files[0];
     65 			rename($this->directory.'/'.$entryName,$this->directory.'/.pull-in-progress-'.$entryName);
     66 			
     67 			$entry = new QueueEntry();
     68 			$value = json_decode(file_get_contents($this->directory.'/.pull-in-progress-'.$entryName.'/value'));
     69 			$entry->value = $value['value'];
     70 			$entry->files = $value['files'];
     71 		}
     72 	}
     73 }
     74 ?>