Location: PHPKode > projects > WaypointHR > waypointhr/conf/installation_write.php
<?php
/*  ================================================================================================
	WaypointHR
	www.waypointhr.com
	Copyright 2009 HR-Fundamentals ltd.
----------------------------------------------------------------------------------------------------
	This file is part of WaypointHR.
	
	WaypointHR 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 3 of
	the License, or (at your option) any later version.
	
	WaypointHR 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.
	
	You should have received a copy of the GNU General Public License along with WaypointHR.
	If not, see <http://www.gnu.org/licenses/>.
----------------------------------------------------------------------------------------------------
	CONFIGURATION WRITER
	$Revision: 537 $
	$LastChangedDate: 2009-07-20 02:27:32 +0100 (Mon, 20 Jul 2009) $
================================================================================================= */

class config_write {
	var $settings_filename_prefix;
	var $settings_filename_suffix;
	
	function config_write() {
		$this->settings_filename_prefix=constant('application_directory').'/conf/installation_settings';
		$this->settings_filename_suffix='.php';		
	}
	function backup_exists() {
		if(file_exists($this->settings_filename_prefix.'_backup'.$this->settings_filename_suffix)) {
			return true;
		} else {
			return false;
		}
	}
	function backup_revert() {
		if(file_exists($this->settings_filename_prefix.'_backup'.$this->settings_filename_suffix)) {
			// Create a temp copy of the current config (just in case the revert fails),
			// ensuring any existing temp copy is deleted first.
			if(file_exists($this->settings_filename_prefix.'_temp'.$this->settings_filename_suffix)) {
				unlink($this->settings_filename_prefix.'_temp'.$this->settings_filename_suffix);
			}
			rename($this->settings_filename_prefix.$this->settings_filename_suffix,$this->settings_filename_prefix.'_temp'.$this->settings_filename_suffix);

			$result=rename($this->settings_filename_prefix.'_backup'.$this->settings_filename_suffix,$this->settings_filename_prefix.$this->settings_filename_suffix);
			if($result) {
				unlink($this->settings_filename_prefix.'_temp'.$this->settings_filename_suffix);
				unlink($this->settings_filename_prefix.'_backup'.$this->settings_filename_suffix);
				return true;
			} else{
				rename($this->settings_filename_prefix.'_temp'.$this->settings_filename_suffix,$this->settings_filename_prefix.$this->settings_filename_suffix);
				return false;
			}
		} else {
			rename($this->settings_filename_prefix.'_temp'.$this->settings_filename_suffix,$this->settings_filename_prefix.$this->settings_filename_suffix);
			return false;
		}		
	}
	function write_exec($settings,$defaults_from_template=true) {
		global $global_config;
		$output='Changes: ';

		$settings_template=@file_get_contents(constant('application_directory').'/conf/installation_template.php');
		if(preg_match_all('/##([\-0-9A-Z_ ]+):([\/\-0-9.A-Z!£%^&*#@\[\]?,._ ]+)##/i',$settings_template,$matches,PREG_SET_ORDER)>0) {
			foreach($matches as $match) {
				if(is_array($settings)&&count($settings)>0&&array_key_exists($match[1],$settings)&&$settings[$match[1]]!==null) {
					$new_setting=$settings[$match[1]];
				} else {
					if($defaults_from_template===true) {
						// Use the default provided in the template file
						$new_setting=$match[2];
					} else {
						// Take 'new setting' from existing configuration file
						if(strlen($match[1])>9&&substr($match[1],0,9)==='database_') {
							$parameter=substr($match[1],9);
							$new_setting=$global_config->get_database_setting($parameter);
						} else {
							if(defined($match[1])) {
								$new_setting=constant($match[1]);
							} else {
								$new_setting=$match[2];
							}
						}
					}
				}
				
				if(is_bool($new_setting)) {
					if($new_setting) {
						$new_setting='true';
					} else {
						$new_setting='false';
					}
				}
				$settings_template=str_replace($match[0],$new_setting,$settings_template);
			}
		}
		
		if(file_exists($this->settings_filename_prefix.$this->settings_filename_suffix)) {
			// Create backup of existing settings
			if(file_exists($this->settings_filename_prefix.'_backup'.$this->settings_filename_suffix)) {
				// Delete existing backup
				$result=unlink($this->settings_filename_prefix.'_backup'.$this->settings_filename_suffix);
				if($result) {
					//echo 'deleted.';
				} else {
					//echo 'delete failure.';		
				}
			}
			
			$result=rename($this->settings_filename_prefix.$this->settings_filename_suffix,$this->settings_filename_prefix.'_backup'.$this->settings_filename_suffix);
			if($result) {
				//echo 'renamed';
			} else {
				//echo 'rename failure';		
			}
		}

		$settings_filename=$this->settings_filename_prefix.$this->settings_filename_suffix;
		
		if(function_exists('file_put_contents')) {
			file_put_contents($settings_filename,$settings_template);
		} else {
			$this->file_put_contents_php4($settings_filename,$settings_template);
		}

		$output=htmlspecialchars($settings_template, ENT_COMPAT, 'UTF-8');
		return '<hr>'.$output.'<hr>';
	}
    function file_put_contents_php4($filename, $content) {
        $file_handle=@fopen($filename, 'w');
        if(!$file_handle) {
            return false;
        } else {
            $byte_count=fwrite($file_handle, $content);
            fclose($file_handle);
            return $byte_count;
        }
    }
}

?>
Return current item: WaypointHR