Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class confRead {
- private $_handler = null;
- private $_file = null;
- private $_contents = null;
- function __construct($file) {
- if($file == null && $this->_file != null) {
- print "Error: No File Was Defined to load.";
- }
- else {
- $this->_file = $file;
- $file = explode(".",$this->_file);
- if($file[1] == "conf") {
- if(!file_exists($this->_file)) {
- print "Error: File Specified Was not found.";
- }
- else {
- $this->openFile();
- }
- }
- else {
- print "Error: Wrong File Extension!";
- }
- }
- }
- private function openFile() {
- if($this->_handler == null) {
- $this->_handler = fopen($this->_file,"r");
- if(!$this->_handler) {
- print "Error: Couldn't Open File {$this->_file}";
- }
- else {
- return 5;
- }
- }
- else {
- print "Error: Please Close existing File.";
- }
- }
- function getValue($item,$defval=false) {
- if(!$this->_handler || $this->_handler == null) {
- print "Error: No File is Open, Please Open one.";
- }
- else {
- do {
- $this->_contents .= fgets($this->_handler,2056);
- }
- while(!feof($this->_handler));
- $item2 = explode($item." = ",$this->_contents);
- $item2 = explode(";",$item2[1]);
- $item2 = $item2[0];
- $this->closeFile();
- $this->_handler = null;
- if($defval != false && $item2 == "") {
- return $defval;
- }
- else {
- return $item2;
- }
- }
- }
- function setValue($item,$value) {
- if($this->_handler == null && $this->_contents != null) {
- if(preg_match('/'.$item.' = (.+);/',$this->_contents)) {
- $this->_contents = preg_replace('/'.$item.' = (.+);/',$item.' = '.$value.';',$this->_contents);
- $this->_handler = fopen($this->_file,"w");
- if($this->_handler) {
- $upd = fwrite($this->_handler,$this->_contents);
- if(!$upd) {
- print "Error: File Failed to Update";
- } else {
- print "File Writen";
- }
- }
- }
- }
- else {
- print "Error: File Not Open or Contents Was Null";
- }
- }
- function closeFile() {
- if($this->_handler) {
- fclose($this->_handler);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement