rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

update.php (8343B)


      1 <?php
      2 
      3 ###############################
      4 # include files from root dir #
      5 ###############################
      6 $root_1 = realpath($_SERVER["DOCUMENT_ROOT"]);
      7 $currentdir = getcwd();
      8 $root_2 = str_replace($root_1, '', $currentdir);
      9 $root_3 = explode("/", $root_2);
     10 if ($root_3[1] == 'core') {
     11   echo $root_3[1];
     12   $root = realpath($_SERVER["DOCUMENT_ROOT"]);
     13 }else{
     14   $root = $root_1 . '/' . $root_3[1];
     15 }
     16 
     17 /*
     18  * Set the script max execution time
     19  */
     20 ini_set('max_execution_time', 60);
     21 
     22 define('UPDATE_DIR_TEMP', $root . '/temp/');
     23 define('UPDATE_DIR_INSTALL', $root . '/');
     24 
     25 class AutoUpdate {
     26 	/*
     27 	 * Enable logging
     28 	 */
     29 	private $_log = true;
     30 	
     31 	/* 
     32 	 * Log file
     33 	 */
     34 	public $logFile = '.updatelog';
     35 	
     36 	/*
     37 	 * The last error
     38 	 */
     39 	private $_lastError = null;
     40 	
     41 	/*
     42 	 * Current version
     43 	 */
     44 	public $currentVersion = 0;
     45 
     46 	/*
     47 	 * Name of the latest version
     48 	 */
     49 	public $latestVersionName = '';
     50 	
     51 	/*
     52 	 * The latest version
     53 	 */
     54 	public $latestVersion = null;
     55 	
     56 	/*
     57 	 * Url to the latest version of the update
     58 	 */
     59 	public $latestUpdate = null;
     60 	
     61 	/*
     62 	 * Url to the update folder on the server
     63 	 */
     64 	public $updateUrl = 'http://media.nordgedanken.de/rpicms/update/server/';
     65 	
     66 	/*
     67 	 * Version filename on the server
     68 	 */
     69 	public $updateIni = 'update.ini.$this->branch';
     70 
     71 	/*
     72 	 * branch on the server
     73 	 */
     74 	public $branch = 'stable';
     75 	
     76 	/*
     77 	 * Temporary download directory
     78 	 */
     79 	public $tempDir = UPDATE_DIR_TEMP;
     80 	
     81 	/*
     82 	 * Remove temprary directory after installation
     83 	 */
     84 	public $removeTempDir = true;
     85 	
     86 	/*
     87 	 * Install directory
     88 	 */
     89 	public $installDir = UPDATE_DIR_INSTALL;
     90 	
     91 	/*
     92 	 * Create new folders with this privileges
     93 	 */
     94 	public $dirPermissions = 0775;
     95 	
     96 	/*
     97 	 * Update script filename
     98 	 */
     99 	public $updateScriptName = '_upgrade.php';
    100 	
    101 	/*
    102 	 * Create new instance
    103 	 *
    104 	 * @param bool $log Default: false
    105 	 */
    106 	public function __construct($log = false) {
    107 		$this->_log = $log;
    108 	}
    109 	
    110 	/* 
    111 	 * Log a message if logging is enabled
    112 	 *
    113 	 * @param string $message The message
    114 	 *
    115 	 * @return void
    116 	 */
    117 	public function log($message) {
    118 		if ($this->_log) {
    119 			$this->_lastError = $message;
    120 			
    121 			$log = fopen($this->logFile, 'a');
    122 			
    123 			if ($log) {
    124 				$message = date('<Y-m-d H:i:s>').$message."\n";
    125 				fputs($log, $message);
    126 				fclose($log);
    127 			}
    128 			else {
    129 				die('Could not write log file!');
    130 			}
    131 		}
    132 	}
    133 	
    134 	/*
    135 	 * Get the latest error
    136 	 *
    137 	 * @return string Last error
    138 	 */
    139 	public function getLastError() {
    140 		if (!is_null($this->_lastError))
    141 			return $this->_lastError;
    142 		else
    143 			return false;
    144 	}
    145 	
    146 	private function _removeDir($dir) {
    147 		if (is_dir($dir)) {
    148 			$objects = scandir($dir);
    149 			foreach ($objects as $object) {
    150 				if ($object != "." && $object != "..") {
    151 					if (filetype($dir."/".$object) == "dir") 
    152 						$this->_removeDir($dir."/".$object); 
    153 					else 
    154 						unlink($dir."/".$object);
    155 				}
    156 			}
    157 			reset($objects);
    158 			rmdir($dir);
    159 		}
    160 	}
    161 	
    162 	/*
    163 	 * Check for a new version
    164 	 *
    165 	 * @return string The latest version
    166 	 */
    167 	public function checkUpdate() {
    168 		$this->log('Checking for a new update. . .');
    169 		
    170 		$updateFile = $this->updateUrl.'/'.$this->updateIni;
    171 		
    172 		$update = @file_get_contents($updateFile);
    173 		if ($update === false) {
    174 			$this->log('Could not retrieve update file `'.$updateFile.'`!');
    175 			return false;
    176 		}
    177 		else {
    178 			$versions = parse_ini_string($update, true);
    179 			if (is_array($versions)) {
    180 				$keyOld = 0;
    181 				$latest = 0;
    182 				$update = '';
    183 				foreach ($versions as $key => $version) {
    184 						if ($key > $keyOld) {
    185 							$keyOld = $key;
    186 							$latest = $version['version'];
    187 							$update = $version['url'];
    188 						}
    189 				}
    190 				
    191 				$this->log('New version found `'.$latest.'`.');
    192 				$this->latestVersion = $keyOld;
    193 				$this->latestVersionName = $latest;
    194 				$this->latestUpdate = $update;
    195 				
    196 				return $keyOld;
    197 			}
    198 			else {
    199 				$this->log('Unable to parse update file!');
    200 				return false;
    201 			}
    202 		}
    203 	}
    204 	
    205 	/*
    206 	 * Download the update
    207 	 *
    208 	 * @param string $updateUrl Url where to download from
    209 	 * @param string $updateFile Path where to save the download
    210 	 */
    211 	public function downloadUpdate($updateUrl, $updateFile) {
    212 		$this->log('Downloading update...');
    213 		$update = @file_get_contents($updateUrl);
    214 		
    215 		if ($update === false) {
    216 			$this->log('Could not download update `'.$updateUrl.'`!');
    217 			return false;
    218 		}
    219 		
    220 		$handle = fopen($updateFile, 'w');
    221 		
    222 		if (!$handle) {
    223 			$this->log('Could not save update file `'.$updateFile.'`!');
    224 			return false;
    225 		}
    226 		
    227 		if (!fwrite($handle, $update)) {
    228 			$this->log('Could not write to update file `'.$updateFile.'`!');
    229 			return false;
    230 		}
    231 		
    232 		fclose($handle);
    233 		
    234 		return true;
    235 	}
    236 	
    237 	/*
    238 	 * Install update
    239 	 *
    240 	 * @param string $updateFile Path to the update file
    241 	 */
    242 	public function install($updateFile) {
    243 		$zip = zip_open($updateFile);
    244 			
    245 		while ($file = zip_read($zip)) {				
    246 			$filename = zip_entry_name($file);
    247 			$foldername = $this->installDir.dirname($filename);
    248 			
    249 			$this->log('Updating `'.$filename.'`!');
    250 			
    251 			if (!is_dir($foldername)) {
    252 				if (!mkdir($foldername, $this->dirPermissions, true)) {
    253 					$this->log('Could not create folder `'.$foldername.'`!');
    254 				}
    255 			}
    256 			
    257 			$contents = zip_entry_read($file, zip_entry_filesize($file));
    258 			
    259 			//Skip if entry is a directory
    260 			if (substr($filename, -1, 1) == '/')
    261 				continue;
    262 			
    263 			//Write to file
    264 			if (file_exists($this->installDir.$filename)) {
    265 				if (!is_writable($this->installDir.$filename)) {
    266 					$this->log('Could not update `'.$this->installDir.$filename.'`, not writable!');
    267 					return false;
    268 				}
    269 			} else {
    270 				$this->log('The file `'.$this->installDir.$filename.'`, does not exist!');			
    271 				$new_file = fopen($this->installDir.$filename, "w") or $this->log('The file `'.$this->installDir.$filename.'`, could not be created!');
    272 				fclose($new_file);
    273 				$this->log('The file `'.$this->installDir.$filename.'`, was succesfully created.');
    274 			}
    275 			
    276 			$updateHandle = @fopen($this->installDir.$filename, 'w');
    277 			
    278 			if (!$updateHandle) {
    279 				$this->log('Could not update file `'.$this->installDir.$filename.'`!');
    280 				return false;
    281 			}
    282 			
    283 			if (!fwrite($updateHandle, $contents)) {
    284 				$this->log('Could not write to file `'.$this->installDir.$filename.'`!');
    285 				return false;
    286 			}
    287 			
    288 			fclose($updateHandle);
    289 			
    290 			//If file is a update script, include
    291 			if ($filename == $this->updateScriptName) {
    292 				$this->log('Try to include update script `'.$this->installDir.$filename.'`.');
    293 				require($this->installDir.$filename);
    294 				$this->log('Update script `'.$this->installDir.$filename.'` included!');
    295 				unlink($this->installDir.$filename);
    296 			}
    297 		}
    298 		
    299 		zip_close($zip);
    300 		
    301 		if ($this->removeTempDir) {
    302 			$this->log('Temporary directory `'.$this->tempDir.'` deleted.');
    303 			$this->_removeDir($this->tempDir);
    304 		}
    305 		
    306 		$this->log('Update `'.$this->latestVersion.'` installed.');
    307 		
    308 		return true;
    309 	}
    310 	
    311 	
    312 	/*
    313 	 * Update to the latest version
    314 	 */
    315 	public function update() {
    316 		//Check for latest version
    317 		if ((is_null($this->latestVersion)) or (is_null($this->latestUpdate))) {
    318 			$this->checkUpdate();
    319 		}
    320 		
    321 		if ((is_null($this->latestVersion)) or (is_null($this->latestUpdate))) {
    322 			return false;
    323 		}
    324 		
    325 		//Update
    326 		if ($this->latestVersion > $this->currentVersion) {
    327 			$this->log('Updating...');
    328 			
    329 			//Add slash at the end of the path
    330 			if ($this->tempDir[strlen($this->tempDir)-1] != '/');
    331 				$this->tempDir = $this->tempDir.'/';
    332 			
    333 			if ((!is_dir($this->tempDir)) and (!mkdir($this->tempDir, 0777, true))) {
    334 				$this->log('Temporary directory `'.$this->tempDir.'` does not exist and could not be created!');
    335 				return false;
    336 			}
    337 			
    338 			if (!is_writable($this->tempDir)) {
    339 				$this->log('Temporary directory `'.$this->tempDir.'` is not writeable!');
    340 				return false;
    341 			}
    342 			
    343 			$updateFile = $this->tempDir.'/'.$this->latestVersion.'.zip';
    344 			$updateUrl = $this->updateUrl.'/'.$this->branch.'/'.$this->latestVersion.'.zip';
    345 			
    346 			//Download update
    347 			if (!is_file($updateFile)) {
    348 				if (!$this->downloadUpdate($updateUrl, $updateFile)) {
    349 					$this->log('Failed to download update!');
    350 					return false;
    351 				}
    352 				
    353 				$this->log('Latest update downloaded to `'.$updateFile.'`.');
    354 			}
    355 			else {
    356 				$this->log('Latest update already downloaded to `'.$updateFile.'`.');
    357 			}
    358 			
    359 			//Unzip
    360 			return $this->install($updateFile);
    361 		}
    362 		else {
    363 			$this->log('No update available!');
    364 			return false;
    365 		}
    366 	}
    367 }