(Original Author) * Wil Clouser * * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ // Include config file $root = dirname(dirname(__FILE__)); require_once("{$root}/site/app/config/config.php"); require_once("{$root}/site/app/config/constants.php"); /** * Database class for services */ class Database { var $write; // Writable database var $read; // Read-only database /** * Connect to databases */ function Database($write_config=array(), $read_config=array()) { if (empty($write_config)) { $write_config = array( 'host' => DB_HOST, 'user' => DB_USER, 'pass' => DB_PASS, 'name' => DB_NAME ); } if (empty($read_config)) { $read_config = array( 'host' => SHADOW_DB_HOST, 'user' => SHADOW_DB_USER, 'pass' => SHADOW_DB_PASS, 'name' => SHADOW_DB_NAME ); } $this->connectWrite($write_config['host'], $write_config['user'], $write_config['pass'], $write_config['name']); $this->connectRead($read_config['host'], $read_config['user'], $read_config['pass'], $read_config['name']); } /** * Connects to read-only database */ function connectRead($host, $username, $password, $database) { $this->read = mysql_connect($host, $username, $password) or die('Could not connect: '.mysql_error()); mysql_select_db($database, $this->read) or die("Could not select read-only database {$database}"); } /** * Connects to writable database */ function connectWrite($host, $username, $password, $database) { $this->write = mysql_connect($host, $username, $password) or die('Could not connect: '.mysql_error()); mysql_select_db($database, $this->write) or die("Could not select writable database {$database}"); } /** * Performs query using read-only database by default * * @param str $qry the query to execute * @param bool $useWrite whether to use the writable database */ function query($qry, $useWrite = false) { if (!$result = mysql_query($qry, ($useWrite ? $this->write : $this->read))) { trigger_error('MySQL Error '.mysql_errno().': '.mysql_error()."\nQuery was: [".$qry.']', E_USER_NOTICE); return false; } return $result; } /** * Sets the stats_updating config value to prevent the Stats Dashboard from * showing inaccurate data while a script is running. */ function lockStats() { if ($this->query("UPDATE `config` SET `value`='1' WHERE `key`='stats_updating'", true)) return true; else return false; } /** * Rests the stats_updating config value to make the Stats Dashboard function * again */ function unlockStats() { if ($this->query("UPDATE `config` SET `value`='0' WHERE `key`='stats_updating'", true)) return true; else return false; } /** * Close database connections */ function close() { mysql_close($this->write); mysql_close($this->read); } } ?>