File: 0.01.1a/core/Configuration.php (View as HTML)

  1: <?php 
  2: /* -------------------------------------------------------------
  3: This file is part of FreeDESK
  4: 
  5: FreeDESK is (C) Copyright 2012 David Cutting
  6: 
  7: FreeDESK is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeDESK is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeDESK.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freedesk/
 21: -------------------------------------------------------------- */
 22: 
 23: /**
 24:  * Main System-Wide Configuration Class (uses sysconfig table)
 25: **/
 26: class Configuration
 27: {
 28: 	/**
 29: 	 * Configuration items
 30: 	**/
 31: 	private $items = array();
 32: 	/**
 33: 	 * FreeDESK instance
 34: 	**/
 35: 	private $DESK = null;
 36: 	
 37: 	/**
 38: 	 * Constructor
 39: 	**/
 40: 	function Configuration(&$freeDESK)
 41: 	{
 42: 		$this->DESK = &$freeDESK;
 43: 		$this->DESK->PluginManager->Register(new Plugin(
 44: 			"System-Wide Configuration Engine","0.01","Core" ));
 45: 	}
 46: 	
 47: 	/**
 48: 	 * Load Configuration
 49: 	 * @return bool Returns a true on success or a false on failure
 50: 	**/
 51: 	function Load()
 52: 	{
 53: 		$q = "SELECT * FROM ".$this->DESK->Database->Table("sysconfig");
 54: 		$r = $this->DESK->Database->Query($q);
 55: 		while ($i = $this->DESK->Database->FetchAssoc($r))
 56: 		{
 57: 			$this->items[$i['sc_option']] = $i['sc_value'];
 58: 		}
 59: 		$error = $this->DESK->Database->Error();
 60: 		$this->DESK->Database->Free($r);
 61: 		
 62: 		if (!$error) return true;
 63: 		return false;
 64: 	}
 65: 	
 66: 	/**
 67: 	 * Get a configuration item
 68: 	 * @param string $name Configuration option
 69: 	 * @param string $default Default return if not found (optional, default "")
 70: 	 * @return string Option value or default if not found
 71: 	**/
 72: 	function Get($name, $default="")
 73: 	{
 74: 		if (!isset($this->items[$name]))
 75: 			return $default;
 76: 		else
 77: 			return $this->items[$name];
 78: 	}
 79: 	
 80: 	/**
 81: 	 * Get all configuration items
 82: 	 * @return array Array of all CIs
 83: 	**/
 84: 	function GetAll()
 85: 	{
 86: 		return $this->items;
 87: 	}
 88: 	
 89: 	/**
 90: 	 * Set a configuration item (no security check in here)
 91: 	 * @param string $name Configuration option
 92: 	 * @param string $value Option value
 93: 	 * @param bool $persist Persistant (saved to database), optional default=true
 94: 	 * @return bool Returns flag to show if successfully set (or database error)
 95: 	**/
 96: 	function Set($name, $value, $persist=true)
 97: 	{
 98: 		$items[$name]=$value;
 99: 		
100: 		if ($persist)
101: 		{
102: 			$q="SELECT * FROM ".$this->DESK->Database->Table("sysconfig")." WHERE ";
103: 			$q.=$this->DESK->Database->Field("sc_option")."=\"".$this->DESK->Database->Safe($name)."\"";
104: 			$r=$this->DESK->Database->Query($q);
105: 			$count=$this->DESK->Database->NumRows($r);
106: 			$this->DESK->Database->Free($r);
107: 			if ($count>0)
108: 			{
109: 				// Already exists in database
110: 				$q="UPDATE ".$this->DESK->Database->Table("sysconfig")." SET ";
111: 				$q.=$this->DESK->Database->Field("sc_value")."=\"";
112: 				$q.=$this->DESK->Database->Safe($value)."\" WHERE ".$this->DESK->Database->Field("sc_option")."=";
113: 				$q.="\"".$this->DESK->Database->Safe($name)."\"";
114: 			}
115: 			else
116: 			{
117: 				// New Entry
118: 				$q="INSERT INTO ".$this->DESK->Database->Table("sysconfig")."(".$this->DESK->Database->Field("sc_option").",";
119: 				$q.=$this->DESK->Database->Field("sc_value").") VALUES(\"".$this->DESK->Database->Safe($name)."\",";
120: 				$q.="\"".$this->DESK->Database->Safe($value)."\")";
121: 			}
122: 			$this->DESK->Database->Query($q);
123: 			if ($this->DESK->Database->Error()) return false;
124: 		}
125: 		return true;
126: 	}
127: 	
128: 	/**
129: 	 * Delete a configuration item (no security check in here)
130: 	 * @param string $name Configuration option
131: 	**/
132: 	function Delete($name)
133: 	{
134: 		$q="DELETE FROM ".$this->DESK->Database->Table("sysconfig")." WHERE ".$this->DESK->Database->Field("sc_option")."=\"";
135: 		$q.=$this->DESK->Database->Safe($name)."\"";
136: 		$this->DESK->Database->Query($q);
137: 		unset($this->items[$name]);
138: 	}
139: 	
140: 
141: }
142: 
143: ?>
144: