<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program 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 2 of the License, or
	(at your opinion) any later version.
	
	This program 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 this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/
	
class fileUtils {
	//==============================
	//DEFAULT CONFIGURATION
	//==============================
	
	//Overwrite files with same name
	var $overwrite = true;
	
	//Show errors
	var $show_errors = true;
	
	//==============================
	//DO NOT EDIT
	//==============================
	var $error;
	var $write = array('w','w+','a','a+','x','x+');
	var $read = array('r','r+','w+','a+','x+');
	
	//PHP5 Constructor
	function __construct() {
		$this->error = "";
	}
	//PHP4 Constructor
	function fileUtils() {
		$this->error = "";
	}
	
	function readFile($file,$param="r") {
		if ($file!="") {
			if (in_array($param,$this->read)) {
				if (file_exists($file)) {
					if ($handle = @fopen($file,$param)) {
						if (!$content = fread($handle,filesize($file))) {
							$this->error = "Cannot read from the file";
						}
						@fclose($handle);
					} else {
						$this->error = "Cannot create the file handler";
					}
				} else {
					$this->error = "The file does not exist";
				}
			} else {
				$this->error = "Parameter <b>$param</b> not permitted, use: ".implode(",",$this->read);
			}
		} else {
			$this->error = _ERROR_NOFILESELECTED_;
		}
		if ($this->error=="") {
			return $content;
		} else {
			$this->print_error();
		}
	}
	
	function readFileRows($file,$param="r") {
		if ($file!="") {
			if (in_array($param,$this->read)) {
				if (file_exists($file)) {
					if ($handle = @fopen($file,$param)) {
						$content = array();
						while (!feof($handle)) {
							$content[] = fgets($handle, 4096);
						}
						@fclose($handle);
					} else {
						$this->error = "Cannot create the file handler";
					}
				} else {
					$this->error = "The file does not exist";
				}
			} else {
				$this->error = "Parameter <b>$param</b> not permitted, use: ".implode(",",$this->read);
			}
		} else {
			$this->error = _ERROR_NOFILESELECTED_;
		}
		if ($this->error=="") {
			return $content;
		} else {
			$this->print_error();
		}
	}
	
	function writeFile($file,$content,$param="w") {
		if ($file!="") {
			if (in_array($param,$this->write)) {
				$path = explode("/",$file);
				array_pop($path);
				$path = implode("/",$path);
				if (is_writable($path)) {
					if (file_exists($file) && !$this->overwrite) {
						$temp_name = $this->file_name($file);
						$temp_ext = $this->file_ext($file);
						$file = $temp_name."_".$this->random_str(10).".".$temp_ext;
					}
					if ($handle = @fopen($file,$param)) {
						if (!fwrite($handle, $content)) {
							$this->error = "Cannot write into the file";
						}
						@fclose($handle);
					} else {
						$this->error = "Cannot create the file handler";
					}
				} else {
					$this->error = _FOLDER_NOT_WRITABLE_;
				}
			} else {
				$this->error = "Parameter <b>$param</b> not permitted, use: ".implode(",",$this->write);
			}
		} else {
			$this->error = _ERROR_NOFILESELECTED_;
		}
		if ($this->error=="") {
			return true;
		} else {
			$this->print_error();
		}
	}
	
	//Check if the selected path is writable
	function isWritable() {
		if (!is_writable($this->path)) {
			$this->error = _FOLDER_NOT_WRITABLE_;
		}
	}
	
	//Print errors if $show_errors is set to true (default)
	function print_error() {
		if ($this->show_errors) {
			echo "<div style='margin: 2px; padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;'>".$this->error."</div>";
		}
		return false;
	}
	
	//Generate a random string
	function random_str($length) {
		$key = "";
		$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
		for($i=0;$i<$length;$i++) {
			$key .= $pattern{rand(0,35)};
		}
		return $key;
	}
	
	//Return the file name
	function file_name($name) {
		$ext = strrchr($name,'.');
		if ($ext!=false) {
			$name = substr($name,0,-strlen($ext));
		}
		return $name;
	}
	
	//Return the file extension
	function file_ext($name) {
		return strtolower(end(explode('.',$name)));
	}
}

?>