miniblog

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

blog.php (5173B)


      1 <?php
      2 
      3 header('Content-Type: text/html; charset=utf-8');
      4 
      5 ?>
      6 
      7 <form method="post" action="">
      8   <input type="submit" value="Neue Blogeinträge abrufen">
      9 </form>
     10 
     11 <?php
     12 
     13 if	( $_SERVER['REQUEST_METHOD'] == 'POST' )
     14 {
     15 
     16 	if ($dh = opendir('./profiles'))
     17 	{
     18 		while (($file = readdir($dh)) !== false)
     19 		{
     20 			if	( substr($file,-4) == '.ini' )
     21 			{
     22 				$config = parse_ini_file('./profiles/'.$file,true);
     23 				
     24 				if	( !$config['enabled'] )
     25 						continue;
     26 				
     27 				$blogger = new Blogger();
     28 				if	( $config['debug'] ) echo "<h1>Profile: $file</h1>";
     29 				
     30 				$blogger->config = $config;
     31 				$blogger->debug = $config['debug'];
     32 				
     33 				echo "<h2>Step 1: Pulling</h2>";
     34 				$blogger->pull();
     35 				flush();
     36 				echo "<h2>Step 2: CMS</h2>";
     37 				$blogger->pushToCMS();
     38 				flush();
     39 				echo "<h2>Step 3: Networks</h2>";
     40 				$blogger->pushToNetwork();
     41 				flush();
     42 			}
     43 		}
     44 		closedir($dh);
     45 	}
     46 }
     47 
     48 
     49 class Blogger {
     50 
     51 	public $debug = true;
     52 	public $config;
     53 	
     54 	private $blogs = array(); 
     55 	
     56 	public function pull()
     57 	{
     58 		if ($dh = opendir('./source'))
     59 		{
     60 			while (($file = readdir($dh)) !== false)
     61 			{
     62 				if	( substr($file,-4) == '.php' )
     63 				{
     64 					require_once('./source/'.$file);
     65 					$className = substr($file,0,strlen($file)-10);
     66 
     67 					if	( $this->debug )
     68 						echo "<h3>Source-Plugin: ".$className.'</h3>';
     69 
     70 					if	( isset($this->config[strtolower($className)] ))
     71 					{
     72 						$source = new $className;
     73 		
     74 						$source->config = $this->config[strtolower($className)];
     75 						$source->debug    = $this->debug;
     76 
     77 						foreach( $source->pull() as $blog )
     78 						{
     79 							$blog['filename'] = $this->createPageFilename($blog['subject']);
     80 							$d = isset($blog['timestamp'])?$blog['timestamp']:time();
     81 		
     82 							switch( $this->config['urlschema'   ])
     83 							{
     84 								case 'flat':
     85 									$blog['path'] = array();
     86 									break;
     87 								case 'yearly':
     88 									$blog['path'] = array( date('Y',$d) );
     89 									break;
     90 								case 'monthly':
     91 									$blog['path'] = array( date('Y',$d),date('m',$d) );
     92 									break;
     93 								case 'daily':
     94 								default:
     95 									$blog['path'] = array( date('Y',$d),date('m',$d),date('d',$d) );
     96 									break;
     97 							}
     98 							$blog['url'     ] = 'http://'.$this->config['hostname'].'/'.implode('/',$blog['path']).'/'.$blog['filename'];
     99 							$blog['shortUrl'] = $this->createShortUrl($blog['url']);
    100 										
    101 							$this->blogs[] = $blog;
    102 						}
    103 					}
    104 				}
    105 			}
    106 			closedir($dh);
    107 			
    108 			if	( $this->debug )
    109 			{
    110 				echo "<h3>Blogs</h3>";
    111 				echo '<pre>';
    112 				print_r($this->blogs);
    113 				echo '</pre>';
    114 			}
    115 		}
    116 	
    117 	}
    118 	
    119 	public function pushToCMS()
    120 	{
    121 		if ($dh = opendir('./cms'))
    122 		{
    123 			while (($file = readdir($dh)) !== false)
    124 			{
    125 				if	( substr($file,-4) == '.php' )
    126 				{
    127 					require_once('./cms/'.$file);
    128 					$className = substr($file,0,strlen($file)-10);
    129 						
    130 					if	( $this->debug )
    131 						echo "<h3>CMS-Plugin: ".$className.'</h3>';
    132 						
    133 					$cms = new $className;
    134 		
    135 					if	( isset($this->config[strtolower($className)] ))
    136 					{
    137 						$cms->config = $this->config[strtolower($className)];
    138 						
    139 						foreach( $this->blogs as $blog )
    140 						{
    141 							
    142 							$cms->url       = $blog['url'];
    143 							$cms->shortUrl  = $blog['shortUrl'];
    144 							$cms->text      = $blog['text'];
    145 							$cms->subject   = $blog['subject'];
    146 							$cms->filenames = $blog['filenames'];
    147 							$cms->filename  = $blog['filename'];
    148 							$cms->path      = $blog['path'];
    149 							$cms->keywords  = $blog['keywords'];
    150 							$cms->timestamp = $blog['timestamp'];
    151 							$cms->debug     = $this->debug;
    152 							$cms->push();
    153 						}
    154 					}
    155 				}
    156 			}
    157 			closedir($dh);
    158 		}
    159 		
    160 	}
    161 	
    162 	
    163 	public function pushToNetwork()
    164 	{
    165 		if ($dh = opendir('./network'))
    166 		{
    167 			while (($file = readdir($dh)) !== false)
    168 			{
    169 				if	( substr($file,-4) == '.php' )
    170 				{
    171 					require_once('./network/'.$file);
    172 					$className = substr($file,0,strlen($file)-10);
    173 	
    174 					if	( $this->debug )
    175 						echo "<h3>Network-Plugin: ".$className.'</h3>';
    176 	
    177 					if	( isset($this->config[strtolower($className)] ))
    178 					{
    179 						$network = new $className;
    180 		
    181 						$network->config = $this->config[strtolower($className)];
    182 						
    183 						foreach( $this->blogs as $blog )
    184 						{
    185 							$network->url      = $blog['url'];
    186 							$network->shortUrl = $blog['shortUrl'];
    187 							$network->text     = $blog['text'];
    188 							$network->subject  = $blog['subject'];
    189 							$network->debug    = $this->debug;
    190 							$network->push();
    191 						}
    192 					}
    193 				}
    194 			}
    195 			closedir($dh);
    196 		}
    197 	
    198 	}
    199 	
    200 	
    201 	private function createShortUrl( $url )
    202 	{
    203 		$su = file_get_contents('http://l.jdhh.de/?url='.$url);
    204 		$doc = json_decode($su,true);
    205 		
    206 		return 'http://l.jdhh.de/'.$doc['short_url'];
    207 	}
    208 	
    209 	
    210 	private function createPageFilename( $title )
    211 	{
    212 		$title = strtolower(trim($title));
    213 		$title = strtr($title, array( 
    214 			' '  =>  '-',
    215 			'ä'  =>  'ae',
    216 			'Ä'  =>  'ae',
    217 			'ö'  =>  'oe',
    218 			'Ö'  =>  'oe',
    219 			'ü'  =>  'ue',
    220 			'Ü'  =>  'ue',
    221 			'ß'  =>  'ss',
    222 			'_'  =>  '-' ) ); 
    223 
    224 		$gueltig = 'abcdefghijklmnopqrstuvwxyz0123456789-';
    225 		$tmp     = strtr($title, $gueltig, str_repeat('#', strlen($gueltig)));
    226 		$title   = strtr($title, $tmp, str_repeat('-', strlen($tmp)));
    227 		
    228 		return $title;
    229 	}
    230 }
    231 
    232 ?>