SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | class Config { | |
4 | ||
5 | private $data = Array(); | |
6 | - | public $dbUser = 'username'; |
6 | + | |
7 | - | public $dbPass = 'password'; |
7 | + | public $dbHost = 'host'; |
8 | - | public $dsn = 'mysql:dbname=;host=hostname;port=3306;charset=UTF-8'; |
8 | + | public $dbName = 'db'; |
9 | public $dbPort = '3306'; | |
10 | ||
11 | public $dbUser = 'user'; | |
12 | public $dbPass = 'pass'; | |
13 | ||
14 | public $dsn = 'mysql:dbname=db;host=host;port=3306;charset=UTF-8'; | |
15 | ||
16 | public $debug = TRUE; | |
17 | ||
18 | public function __construct() { | |
19 | try { | |
20 | $this->pdo = new PDO($this->dsn, $this->dbUser, $this->dbPass, array(PDO::ATTR_PERSISTENT => true)); | |
21 | $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
22 | $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
23 | } | |
24 | catch (PDOException $e) { | |
25 | if ($this->debug) echo $e->getMessage(); | |
26 | } | |
27 | $this->dbCheck(); | |
28 | if ($this->debug) { | |
29 | $this->enableDebug(); | |
30 | } else { | |
31 | $this->disableDebug(); | |
32 | } | |
33 | $this->testObj = new ConfigObject($this->pdo, "testnavn"); | |
34 | } | |
35 | ||
36 | function __get($name) { | |
37 | if (($name != "pdo")) { | |
38 | if (!isset($this->data[$name])) { | |
39 | $this->data[$name]=new ConfigObject($this->pdo, $name); | |
40 | } | |
41 | } | |
42 | return $this->data[$name]; | |
43 | } | |
44 | ||
45 | function __set($name, $value) { | |
46 | - | $query = $this->pdo->prepare("SELECT COUNT (*) FROM `configuration` WHERE `key`=:name"); |
46 | + | |
47 | $value=new ConfigObject($this->pdo, $name); | |
48 | } | |
49 | $this->data[$name] = $value; | |
50 | } | |
51 | ||
52 | function __isset($name) { | |
53 | $query = $this->pdo->prepare("SELECT COUNT(*) FROM `configuration` WHERE `key`=:keyname"); | |
54 | $query->bindValue(":keyname", $name, PDO::PARAM_STR); | |
55 | $query->execute(); | |
56 | $count=$query->fetchColumn(); | |
57 | $query->closeCursor(); | |
58 | return ((bool) $count); | |
59 | } | |
60 | ||
61 | function __unset($name) { | |
62 | unset($this->data[$name]); | |
63 | $query = $this->pdo->prepare("DELETE FROM `configuration` WHERE `key`=:name"); | |
64 | $query->bindValue(":name", $name, PDO::PARAM_STR); | |
65 | $query->execute(); | |
66 | $numrows=$query->rowCount(); | |
67 | return ((bool) $numrows); | |
68 | } | |
69 | ||
70 | private function dbCheck() { | |
71 | $query=$this->pdo->query("SHOW TABLES LIKE 'configuration'"); | |
72 | $query->execute(); | |
73 | if ($query->rowCount() == 0) { | |
74 | $query->closeCursor(); | |
75 | $query=$this->pdo->query("CREATE TABLE IF NOT EXISTS `configuration` (". | |
76 | "`key` text COLLATE utf8_swedish_ci NOT NULL,". | |
77 | "`value` text COLLATE utf8_swedish_ci NOT NULL,". | |
78 | "`group` text COLLATE utf8_swedish_ci,". | |
79 | "`description` text COLLATE utf8_swedish_ci,". | |
80 | "UNIQUE KEY `key` (`key`(16))". | |
81 | ") ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;"); | |
82 | $query->execute(); | |
83 | } | |
84 | $query->closeCursor(); | |
85 | } | |
86 | ||
87 | public function enableDebug() { | |
88 | $this->debug=TRUE; | |
89 | ini_set('display_errors', 'On'); | |
90 | error_reporting(2039); | |
91 | } | |
92 | ||
93 | public function disableDebug() { | |
94 | $this->debug=FALSE; | |
95 | ini_set('display_errors', 'Off'); | |
96 | } | |
97 | } | |
98 | ||
99 | class ConfigObject { | |
100 | private $data = Array(); | |
101 | private $pdo; | |
102 | private $name; | |
103 | ||
104 | public function __construct($pdo, $name) { | |
105 | $this->pdo=$pdo; | |
106 | $this->name=$name; | |
107 | } | |
108 | ||
109 | function __get($property) { | |
110 | if ($property == "pdo") return (true); | |
111 | $query = $this->pdo->prepare("SELECT `".$property."` FROM `configuration` WHERE `key`=:name"); | |
112 | $query->bindValue(":name", $this->name, PDO::PARAM_STR); | |
113 | $query->execute(); | |
114 | $obj = $query->fetchObject(); | |
115 | $this->data[$property]=$obj->$property; | |
116 | $query->closeCursor(); | |
117 | return $this->data[$property]; | |
118 | } | |
119 | ||
120 | function __set($property, $value) { | |
121 | $this->data[$property] = $value; | |
122 | if (($property == "pdo") || ($property == "name")) return (true); | |
123 | $query = $this->pdo->prepare("SELECT COUNT(*) FROM `configuration` WHERE `key`=:name"); | |
124 | $query->bindValue(":name", $this->name, PDO::PARAM_STR); | |
125 | $query->execute(); | |
126 | $count=$query->fetchColumn(); | |
127 | $query->closeCursor(); | |
128 | if ($count == 0) { | |
129 | $query = $this->pdo->prepare("INSERT INTO `configuration` (`value`, `key`) VALUES (:value, :name)"); | |
130 | $query->bindValue(":value", $value, PDO::PARAM_STR); | |
131 | $query->bindValue(":name", $this->name, PDO::PARAM_STR); | |
132 | $query->execute(); | |
133 | } else { | |
134 | $query = $this->pdo->prepare("UPDATE `configuration` SET `".$property."`=:value WHERE `key`=:name"); | |
135 | $query->bindValue(":value", $value, PDO::PARAM_STR); | |
136 | $query->bindValue(":name", $this->name, PDO::PARAM_STR); | |
137 | $query->execute(); | |
138 | } | |
139 | } | |
140 | ||
141 | function __isset($name) { | |
142 | $query = $this->pdo->prepare("SELECT COUNT(*) FROM `configuration` WHERE `key`=:keyname AND `".$name."` IS NOT NULL"); | |
143 | $query->bindValue(":keyname", $this->name, PDO::PARAM_STR); | |
144 | $query->execute(); | |
145 | $count=$query->fetchColumn(); | |
146 | $query->closeCursor(); | |
147 | return ((bool) $count); | |
148 | } | |
149 | ||
150 | function __unset($name) { | |
151 | unset($this->data[$name]); | |
152 | $query = $this->pdo->prepare("UPDATE `configuration` SET `".$name."`=NULL WHERE `key`=:keyname"); | |
153 | $query->bindValue(":keyname", $this->name, PDO::PARAM_STR); | |
154 | $query->execute(); | |
155 | $numrows=$query->rowCount(); | |
156 | return ((bool) $numrows); | |
157 | } | |
158 | ||
159 | public function __tostring() { | |
160 | $query = $this->pdo->prepare("SELECT `value` FROM `configuration` WHERE `key`=:name"); | |
161 | $query->bindValue(":name", $this->name, PDO::PARAM_STR); | |
162 | $query->execute(); | |
163 | $obj = $query->fetchObject(); | |
164 | $this->data['value']=$obj->value; | |
165 | $query->closeCursor(); | |
166 | return $this->data['value']; | |
167 | } | |
168 | } | |
169 | ?> |