miniblog

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

File.class.php (3547B)


      1 <?php
      2 
      3 class File
      4 {
      5 	public $debug = false;
      6 	public $config = array();
      7 	
      8 	private $text;
      9 	private $html;
     10 	private $filenames;
     11 	
     12 	public function pull()
     13 	{
     14 		$entrys = array();
     15 
     16 		$dh = opendir('./blogs');
     17 		
     18 		while (($file = readdir($dh)) !== false)
     19 		{
     20 			$file = './blogs/'.$file;
     21 			if	( is_dir($file) && is_file($file.'/OK') && !is_file($file.'/PROCESSED'))
     22 			{
     23 				$filenames = array();
     24 				
     25 				if	( file_exists($file.'/image') )
     26 				{
     27 					$imagename = file_get_contents($file.'/image');
     28 					$tmpfilename = tempnam('/tmp','blog-').$imagename;
     29 					copy( $file.'/'.$imagename,$tmpfilename);
     30 					$filenames[] = array('name'=>$imagename,'filename'=>$tmpfilename);
     31 				}
     32 				
     33 				$entrys[] = array(
     34 					'filenames'=> $filenames,
     35 					'keywords' => array(),
     36 					'timestamp' => filectime($file),
     37 					'subject'  => file_get_contents($file.'/subject'),
     38 					'text'     => file_get_contents($file.'/text'   )
     39 				);
     40 				
     41 				// als "verarbeitet" markieren...
     42 				$processed = fopen($file.'/PROCESSED','w');
     43 				fwrite($processed,'');
     44 				fclose($processed);
     45 				
     46 				// und verschieben...
     47 				if	( !is_dir('./blogs/archive'))
     48 				{
     49 					mkdir('./blogs/archive');
     50 				}
     51 				rename($file,'blogs/archive/'.basename($file));
     52 				
     53 			}
     54 		}
     55 
     56 		
     57 		return $entrys;
     58 	}
     59 	
     60 	
     61 	
     62 	private function getpart($mbox,$mid,$p,$partno)
     63 	{
     64 		// $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
     65 		
     66 		// DECODE DATA
     67 		$data = ($partno) ?
     68 		imap_fetchbody($mbox,$mid,$partno):  // multipart
     69 		imap_body($mbox,$mid);  // simple
     70 	
     71 		// Any part may be encoded, even plain text messages, so check everything.
     72 		if	($p->encoding==4)
     73 			$data = quoted_printable_decode($data);
     74 		elseif ($p->encoding==3)
     75 		$data = base64_decode($data);
     76 	
     77 		// PARAMETERS
     78 		// get all parameters, like charset, filenames of attachments, etc.
     79 		$params = array();
     80 		if ($p->parameters)
     81 			foreach ($p->parameters as $x)
     82 			$params[strtolower($x->attribute)] = $x->value;
     83 		if ($p->dparameters)
     84 			foreach ($p->dparameters as $x)
     85 			$params[strtolower($x->attribute)] = $x->value;
     86 	
     87 		// ATTACHMENT
     88 		// Any part with a filename is an attachment,
     89 		// so an attached text file (type 0) is not mistaken as the message.
     90 		if ($params['filename'] || $params['name']) {
     91 			// filename may be given as 'Filename' or 'Name' or both
     92 			$filename = ($params['filename'])? $params['filename'] : $params['name'];
     93 			// filename may be encoded, so see imap_mime_header_decode()
     94 			$fname = tempnam(null,'blog-file-');
     95 			$file = fopen($fname,'w');
     96 			fwrite($file,$data);
     97 			fclose($file);
     98 			chmod($fname,0644);
     99 	
    100 			$this->filenames[] = array('filename'=>$fname,'name'=>$filename);
    101 		}
    102 	
    103 		// TEXT
    104 		if ($p->type==0 && $data) {
    105 			// Messages may be split in different parts because of inline attachments,
    106 			// so append parts together with blank row.
    107 			if (strtolower($p->subtype)=='plain')
    108 				$this->text.= trim($data) ."\n\n";
    109 			else
    110 				$this->html.= $data ."<br><br>";
    111 			$charset = $params['charset'];  // assume all parts are same charset
    112 		}
    113 	
    114 		// EMBEDDED MESSAGE
    115 		// Many bounce notifications embed the original message as type 2,
    116 		// but AOL uses type 1 (multipart), which is not handled here.
    117 		// There are no PHP functions to parse embedded messages,
    118 		// so this just appends the raw source to the main message.
    119 		elseif ($p->type==2 && $data) {
    120 			$this->text.= $data."\n\n";
    121 		}
    122 	
    123 		// SUBPART RECURSION
    124 		if ($p->parts) {
    125 			foreach ($p->parts as $partno0=>$p2)
    126 				$this->getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1));  // 1.2, 1.2.1, etc.
    127 		}
    128 	}
    129 	
    130 }
    131 
    132 ?>