Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Config {
- private $data = Array();
- public $dbHost = 'host';
- public $dbName = 'db';
- public $dbPort = '3306';
- public $dbUser = 'user';
- public $dbPass = 'pass';
- public $dsn = 'mysql:dbname=db;host=host;port=3306;charset=UTF-8';
- public $debug = TRUE;
- public function __construct() {
- try {
- $this->pdo = new PDO($this->dsn, $this->dbUser, $this->dbPass, array(PDO::ATTR_PERSISTENT => true));
- $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- }
- catch (PDOException $e) {
- if ($this->debug) echo $e->getMessage();
- }
- $this->dbCheck();
- if ($this->debug) {
- $this->enableDebug();
- } else {
- $this->disableDebug();
- }
- $this->testObj = new ConfigObject($this->pdo, "testnavn");
- }
- function __get($name) {
- if (($name != "pdo")) {
- if (!isset($this->data[$name])) {
- $this->data[$name]=new ConfigObject($this->pdo, $name);
- }
- }
- return $this->data[$name];
- }
- function __set($name, $value) {
- if ($name != "pdo") {
- $value=new ConfigObject($this->pdo, $name);
- }
- $this->data[$name] = $value;
- }
- function __isset($name) {
- $query = $this->pdo->prepare("SELECT COUNT(*) FROM `configuration` WHERE `key`=:keyname");
- $query->bindValue(":keyname", $name, PDO::PARAM_STR);
- $query->execute();
- $count=$query->fetchColumn();
- $query->closeCursor();
- return ((bool) $count);
- }
- function __unset($name) {
- unset($this->data[$name]);
- $query = $this->pdo->prepare("DELETE FROM `configuration` WHERE `key`=:name");
- $query->bindValue(":name", $name, PDO::PARAM_STR);
- $query->execute();
- $numrows=$query->rowCount();
- return ((bool) $numrows);
- }
- private function dbCheck() {
- $query=$this->pdo->query("SHOW TABLES LIKE 'configuration'");
- $query->execute();
- if ($query->rowCount() == 0) {
- $query->closeCursor();
- $query=$this->pdo->query("CREATE TABLE IF NOT EXISTS `configuration` (".
- "`key` text COLLATE utf8_swedish_ci NOT NULL,".
- "`value` text COLLATE utf8_swedish_ci NOT NULL,".
- "`group` text COLLATE utf8_swedish_ci,".
- "`description` text COLLATE utf8_swedish_ci,".
- "UNIQUE KEY `key` (`key`(16))".
- ") ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;");
- $query->execute();
- }
- $query->closeCursor();
- }
- public function enableDebug() {
- $this->debug=TRUE;
- ini_set('display_errors', 'On');
- error_reporting(2039);
- }
- public function disableDebug() {
- $this->debug=FALSE;
- ini_set('display_errors', 'Off');
- }
- }
- class ConfigObject {
- private $data = Array();
- private $pdo;
- private $name;
- public function __construct($pdo, $name) {
- $this->pdo=$pdo;
- $this->name=$name;
- }
- function __get($property) {
- if ($property == "pdo") return (true);
- $query = $this->pdo->prepare("SELECT `".$property."` FROM `configuration` WHERE `key`=:name");
- $query->bindValue(":name", $this->name, PDO::PARAM_STR);
- $query->execute();
- $obj = $query->fetchObject();
- $this->data[$property]=$obj->$property;
- $query->closeCursor();
- return $this->data[$property];
- }
- function __set($property, $value) {
- $this->data[$property] = $value;
- if (($property == "pdo") || ($property == "name")) return (true);
- $query = $this->pdo->prepare("SELECT COUNT(*) FROM `configuration` WHERE `key`=:name");
- $query->bindValue(":name", $this->name, PDO::PARAM_STR);
- $query->execute();
- $count=$query->fetchColumn();
- $query->closeCursor();
- if ($count == 0) {
- $query = $this->pdo->prepare("INSERT INTO `configuration` (`value`, `key`) VALUES (:value, :name)");
- $query->bindValue(":value", $value, PDO::PARAM_STR);
- $query->bindValue(":name", $this->name, PDO::PARAM_STR);
- $query->execute();
- } else {
- $query = $this->pdo->prepare("UPDATE `configuration` SET `".$property."`=:value WHERE `key`=:name");
- $query->bindValue(":value", $value, PDO::PARAM_STR);
- $query->bindValue(":name", $this->name, PDO::PARAM_STR);
- $query->execute();
- }
- }
- function __isset($name) {
- $query = $this->pdo->prepare("SELECT COUNT(*) FROM `configuration` WHERE `key`=:keyname AND `".$name."` IS NOT NULL");
- $query->bindValue(":keyname", $this->name, PDO::PARAM_STR);
- $query->execute();
- $count=$query->fetchColumn();
- $query->closeCursor();
- return ((bool) $count);
- }
- function __unset($name) {
- unset($this->data[$name]);
- $query = $this->pdo->prepare("UPDATE `configuration` SET `".$name."`=NULL WHERE `key`=:keyname");
- $query->bindValue(":keyname", $this->name, PDO::PARAM_STR);
- $query->execute();
- $numrows=$query->rowCount();
- return ((bool) $numrows);
- }
- public function __tostring() {
- $query = $this->pdo->prepare("SELECT `value` FROM `configuration` WHERE `key`=:name");
- $query->bindValue(":name", $this->name, PDO::PARAM_STR);
- $query->execute();
- $obj = $query->fetchObject();
- $this->data['value']=$obj->value;
- $query->closeCursor();
- return $this->data['value'];
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement