All pastes #638403 Raw Edit

Miscellany

public php v1 · immutable
#638403 ·published 2007-07-29 20:12 UTC
rendered paste body
// Load settingsrequire_once('./config.php');// Errorsdefine("INPUT_FILE_OPEN_ERROR", 1);define("INPUT_FILE_READ_ERROR", 2);class ch_data {	public $level = 0;	public $string;}class Chameleon {	private $nr_rep;   // elements to be parsed	private $nr_done;  // elements parsed	private $static_file; // static filename	private $initialized; // object must be initialized before use	private $data = 0;     // static file data	private $ddata;	private $stfile_handle = 0;	private $replace_pos;	private $replaceable_data;		public function __construct($filename = null) {		$this->static_file = $filename;		$this->initialized = false;		if ($filename !== null)			$this->load($filename);		$this->replace_pos = Array();		$this->replaceable_data = Array();	}	public function load($filename) {		$this->initialized = false;				if ($this->stfile_handle !== 0) {			fclose($this->stfile_handle);			$this->data = 0;		}				$real_filename = $this->calculate_filename($filename);		$stfile_handle = @fopen($real_filename, 'rb');		if ($stfile_handle == FALSE) {			throw new Exception("Error opening file: $real_filename.", 			                    INPUT_FILE_OPEN_ERROR);		}			$contents = @fread($stfile_handle, filesize($real_filename));		if ($contents == FALSE) {			fclose($stfile_handle);			throw new Exception("Error reading from file: $real_filename",			                    INPUT_FILE_READ_ERROR);		}		$this->data = $contents;		$this->ddata = $this->data;				$this->init();	}	public function update($name, $value) {			}	public function isparsed() {		if ($this->nr_done == $this->nr_rep)			return true;		return false;	}	public function submit() {		return $this->ddata;	}	public function __destruct() {		if ($this->stfile_handle !== 0)			fclose($this->stfile_handle);	}		private function calculate_filename($filename) {		$workname = $filename;		$firstchar = substr($workname, 0, 1);				if (($firstchar == '.') || ($firstchar == '/'))			return $workname;				$workname = STATIC_DIR."/".$workname;		return $workname;	}		private function init() {		$old_pos = 0;		$i = 0;				while (true) {			$new_pos = strpos($this->data, CHAMELEON, $old_pos + 1);			if ($new_pos < $old_pos)				break;			$this->replace_pos[$i] = $new_pos;			$old_pos = $new_pos;						$new_pos = strpos($this->data, ")", $old_pos + 1);			$tmp_string = substr($this->data, 			                     $old_pos + strlen(CHAMELEON) + 1, 			                     $new_pos - ($old_pos + strlen(CHAMELEON) + 1));			$this->replaceable_data[$i]->string = $tmp_string;						$i++;		}				print_r($this->replace_pos);				$this->nr_rep = $i;		$this->initialized = true;	}}?>