@@ -19,396 +19,396 @@ |
||
19 | 19 | */ |
20 | 20 | class Db extends Generic implements Db_Interface |
21 | 21 | { |
22 | - /* public: this is an api revision, not a CVS revision. */ |
|
23 | - public $type = 'pgsql'; |
|
24 | - public $port = ''; |
|
25 | - public $defaultPort = '5432'; |
|
26 | - |
|
27 | - |
|
28 | - /** |
|
29 | - * adds if not blank |
|
30 | - * |
|
31 | - * @param string $add the value to set |
|
32 | - * @param string $me the key/field to set the value for |
|
33 | - * @param false|string $quote optional indicate the value needs quoted |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - public function ifadd($add, $me, $quote = false) |
|
37 | - { |
|
38 | - if ('' != $add) { |
|
39 | - return ' '.$me.($quote === false ? '' : $quote).$add.($quote === false ? '' : $quote); |
|
40 | - } |
|
41 | - return ''; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @param $string |
|
46 | - * @return string |
|
47 | - */ |
|
48 | - public function real_escape($string = '') |
|
49 | - { |
|
50 | - return $this->escape($string); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * alias function of select_db, changes the database we are working with. |
|
55 | - * |
|
56 | - * @param string $database the name of the database to use |
|
57 | - * @return void |
|
58 | - */ |
|
59 | - public function useDb($database) |
|
60 | - { |
|
61 | - $this->selectDb($database); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * changes the database we are working with. |
|
66 | - * |
|
67 | - * @param string $database the name of the database to use |
|
68 | - * @return void |
|
69 | - */ |
|
70 | - public function selectDb($database) |
|
71 | - { |
|
72 | - /*if ($database != $this->database) { |
|
22 | + /* public: this is an api revision, not a CVS revision. */ |
|
23 | + public $type = 'pgsql'; |
|
24 | + public $port = ''; |
|
25 | + public $defaultPort = '5432'; |
|
26 | + |
|
27 | + |
|
28 | + /** |
|
29 | + * adds if not blank |
|
30 | + * |
|
31 | + * @param string $add the value to set |
|
32 | + * @param string $me the key/field to set the value for |
|
33 | + * @param false|string $quote optional indicate the value needs quoted |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + public function ifadd($add, $me, $quote = false) |
|
37 | + { |
|
38 | + if ('' != $add) { |
|
39 | + return ' '.$me.($quote === false ? '' : $quote).$add.($quote === false ? '' : $quote); |
|
40 | + } |
|
41 | + return ''; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @param $string |
|
46 | + * @return string |
|
47 | + */ |
|
48 | + public function real_escape($string = '') |
|
49 | + { |
|
50 | + return $this->escape($string); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * alias function of select_db, changes the database we are working with. |
|
55 | + * |
|
56 | + * @param string $database the name of the database to use |
|
57 | + * @return void |
|
58 | + */ |
|
59 | + public function useDb($database) |
|
60 | + { |
|
61 | + $this->selectDb($database); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * changes the database we are working with. |
|
66 | + * |
|
67 | + * @param string $database the name of the database to use |
|
68 | + * @return void |
|
69 | + */ |
|
70 | + public function selectDb($database) |
|
71 | + { |
|
72 | + /*if ($database != $this->database) { |
|
73 | 73 | $this->database = $database; |
74 | 74 | $this->linkId = null; |
75 | 75 | $this->connect(); |
76 | 76 | }*/ |
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Db::connect() |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function connect() |
|
84 | - { |
|
85 | - if (0 == $this->linkId) { |
|
86 | - $connectString = trim($this->ifadd($this->host, 'host='). |
|
87 | - $this->ifadd($this->port, 'port='). |
|
88 | - $this->ifadd($this->database, 'dbname='). |
|
89 | - $this->ifadd($this->user, 'user='). |
|
90 | - $this->ifadd($this->password, 'password=', "'")); |
|
91 | - $this->linkId = pg_connect($connectString); |
|
92 | - if (!$this->linkId) { |
|
93 | - $this->halt('Link-ID == FALSE, connect failed'); |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - /* This only affects systems not using persistent connections */ |
|
99 | - |
|
100 | - /** |
|
101 | - * Db::disconnect() |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public function disconnect() |
|
105 | - { |
|
106 | - return @pg_close($this->linkId); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Db::queryReturn() |
|
111 | - * |
|
112 | - * Sends an SQL query to the server like the normal query() command but iterates through |
|
113 | - * any rows and returns the row or rows immediately or FALSE on error |
|
114 | - * |
|
115 | - * @param mixed $query SQL Query to be used |
|
116 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
117 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
118 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
119 | - */ |
|
120 | - public function queryReturn($query, $line = '', $file = '') |
|
121 | - { |
|
122 | - $this->query($query, $line, $file); |
|
123 | - if ($this->num_rows() == 0) { |
|
124 | - return false; |
|
125 | - } elseif ($this->num_rows() == 1) { |
|
126 | - $this->next_record(MYSQL_ASSOC); |
|
127 | - return $this->Record; |
|
128 | - } else { |
|
129 | - $out = []; |
|
130 | - while ($this->next_record(MYSQL_ASSOC)) { |
|
131 | - $out[] = $this->Record; |
|
132 | - } |
|
133 | - return $out; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * db:qr() |
|
139 | - * |
|
140 | - * alias of queryReturn() |
|
141 | - * |
|
142 | - * @param mixed $query SQL Query to be used |
|
143 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
144 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
145 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
146 | - */ |
|
147 | - public function qr($query, $line = '', $file = '') |
|
148 | - { |
|
149 | - return $this->queryReturn($query, $line, $file); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Db::query() |
|
154 | - * |
|
155 | - * Sends an SQL query to the database |
|
156 | - * |
|
157 | - * @param mixed $queryString |
|
158 | - * @param string $line |
|
159 | - * @param string $file |
|
160 | - * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
161 | - */ |
|
162 | - public function query($queryString, $line = '', $file = '') |
|
163 | - { |
|
164 | - /* No empty queries, please, since PHP4 chokes on them. */ |
|
165 | - /* The empty query string is passed on from the constructor, |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Db::connect() |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function connect() |
|
84 | + { |
|
85 | + if (0 == $this->linkId) { |
|
86 | + $connectString = trim($this->ifadd($this->host, 'host='). |
|
87 | + $this->ifadd($this->port, 'port='). |
|
88 | + $this->ifadd($this->database, 'dbname='). |
|
89 | + $this->ifadd($this->user, 'user='). |
|
90 | + $this->ifadd($this->password, 'password=', "'")); |
|
91 | + $this->linkId = pg_connect($connectString); |
|
92 | + if (!$this->linkId) { |
|
93 | + $this->halt('Link-ID == FALSE, connect failed'); |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + /* This only affects systems not using persistent connections */ |
|
99 | + |
|
100 | + /** |
|
101 | + * Db::disconnect() |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public function disconnect() |
|
105 | + { |
|
106 | + return @pg_close($this->linkId); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Db::queryReturn() |
|
111 | + * |
|
112 | + * Sends an SQL query to the server like the normal query() command but iterates through |
|
113 | + * any rows and returns the row or rows immediately or FALSE on error |
|
114 | + * |
|
115 | + * @param mixed $query SQL Query to be used |
|
116 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
117 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
118 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
119 | + */ |
|
120 | + public function queryReturn($query, $line = '', $file = '') |
|
121 | + { |
|
122 | + $this->query($query, $line, $file); |
|
123 | + if ($this->num_rows() == 0) { |
|
124 | + return false; |
|
125 | + } elseif ($this->num_rows() == 1) { |
|
126 | + $this->next_record(MYSQL_ASSOC); |
|
127 | + return $this->Record; |
|
128 | + } else { |
|
129 | + $out = []; |
|
130 | + while ($this->next_record(MYSQL_ASSOC)) { |
|
131 | + $out[] = $this->Record; |
|
132 | + } |
|
133 | + return $out; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * db:qr() |
|
139 | + * |
|
140 | + * alias of queryReturn() |
|
141 | + * |
|
142 | + * @param mixed $query SQL Query to be used |
|
143 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
144 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
145 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
146 | + */ |
|
147 | + public function qr($query, $line = '', $file = '') |
|
148 | + { |
|
149 | + return $this->queryReturn($query, $line, $file); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Db::query() |
|
154 | + * |
|
155 | + * Sends an SQL query to the database |
|
156 | + * |
|
157 | + * @param mixed $queryString |
|
158 | + * @param string $line |
|
159 | + * @param string $file |
|
160 | + * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
161 | + */ |
|
162 | + public function query($queryString, $line = '', $file = '') |
|
163 | + { |
|
164 | + /* No empty queries, please, since PHP4 chokes on them. */ |
|
165 | + /* The empty query string is passed on from the constructor, |
|
166 | 166 | * when calling the class without a query, e.g. in situations |
167 | 167 | * like these: '$db = new db_Subclass;' |
168 | 168 | */ |
169 | - if ($queryString == '') { |
|
170 | - return 0; |
|
171 | - } |
|
172 | - |
|
173 | - $this->connect(); |
|
174 | - |
|
175 | - /* printf("<br>Debug: query = %s<br>\n", $queryString); */ |
|
176 | - |
|
177 | - $this->queryId = @pg_exec($this->linkId, $queryString); |
|
178 | - $this->Row = 0; |
|
179 | - |
|
180 | - $this->Error = pg_errormessage($this->linkId); |
|
181 | - $this->Errno = ($this->Error == '') ? 0 : 1; |
|
182 | - if (!$this->queryId) { |
|
183 | - $this->halt('Invalid SQL: '.$queryString, $line, $file); |
|
184 | - } |
|
185 | - |
|
186 | - return $this->queryId; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Db::free() |
|
191 | - * |
|
192 | - * @return void |
|
193 | - */ |
|
194 | - public function free() |
|
195 | - { |
|
196 | - @pg_freeresult($this->queryId); |
|
197 | - $this->queryId = 0; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Db::next_record() |
|
202 | - * @param mixed $resultType |
|
203 | - * @return bool |
|
204 | - */ |
|
205 | - public function next_record($resultType = PGSQL_BOTH) |
|
206 | - { |
|
207 | - $this->Record = @pg_fetch_array($this->queryId, $this->Row++, $resultType); |
|
208 | - |
|
209 | - $this->Error = pg_errormessage($this->linkId); |
|
210 | - $this->Errno = ($this->Error == '') ? 0 : 1; |
|
211 | - |
|
212 | - $stat = is_array($this->Record); |
|
213 | - if (!$stat && $this->autoFree) { |
|
214 | - pg_freeresult($this->queryId); |
|
215 | - $this->queryId = 0; |
|
216 | - } |
|
217 | - return $stat; |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * Db::seek() |
|
222 | - * |
|
223 | - * @param mixed $pos |
|
224 | - * @return void |
|
225 | - */ |
|
226 | - public function seek($pos) |
|
227 | - { |
|
228 | - $this->Row = $pos; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Db::transactionBegin() |
|
233 | - * |
|
234 | - * @return mixed |
|
235 | - */ |
|
236 | - public function transactionBegin() |
|
237 | - { |
|
238 | - return $this->query('begin'); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Db::transactionCommit() |
|
243 | - * @return bool|mixed |
|
244 | - */ |
|
245 | - public function transactionCommit() |
|
246 | - { |
|
247 | - if (!$this->Errno) { |
|
248 | - return pg_exec($this->linkId, 'commit'); |
|
249 | - } else { |
|
250 | - return false; |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Db::transactionAbort() |
|
256 | - * @return mixed |
|
257 | - */ |
|
258 | - public function transactionAbort() |
|
259 | - { |
|
260 | - return pg_exec($this->linkId, 'rollback'); |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Db::getLastInsertId() |
|
265 | - * @param mixed $table |
|
266 | - * @param mixed $field |
|
267 | - * @return int |
|
268 | - */ |
|
269 | - public function getLastInsertId($table, $field) |
|
270 | - { |
|
271 | - /* This will get the last insert ID created on the current connection. Should only be called |
|
169 | + if ($queryString == '') { |
|
170 | + return 0; |
|
171 | + } |
|
172 | + |
|
173 | + $this->connect(); |
|
174 | + |
|
175 | + /* printf("<br>Debug: query = %s<br>\n", $queryString); */ |
|
176 | + |
|
177 | + $this->queryId = @pg_exec($this->linkId, $queryString); |
|
178 | + $this->Row = 0; |
|
179 | + |
|
180 | + $this->Error = pg_errormessage($this->linkId); |
|
181 | + $this->Errno = ($this->Error == '') ? 0 : 1; |
|
182 | + if (!$this->queryId) { |
|
183 | + $this->halt('Invalid SQL: '.$queryString, $line, $file); |
|
184 | + } |
|
185 | + |
|
186 | + return $this->queryId; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Db::free() |
|
191 | + * |
|
192 | + * @return void |
|
193 | + */ |
|
194 | + public function free() |
|
195 | + { |
|
196 | + @pg_freeresult($this->queryId); |
|
197 | + $this->queryId = 0; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Db::next_record() |
|
202 | + * @param mixed $resultType |
|
203 | + * @return bool |
|
204 | + */ |
|
205 | + public function next_record($resultType = PGSQL_BOTH) |
|
206 | + { |
|
207 | + $this->Record = @pg_fetch_array($this->queryId, $this->Row++, $resultType); |
|
208 | + |
|
209 | + $this->Error = pg_errormessage($this->linkId); |
|
210 | + $this->Errno = ($this->Error == '') ? 0 : 1; |
|
211 | + |
|
212 | + $stat = is_array($this->Record); |
|
213 | + if (!$stat && $this->autoFree) { |
|
214 | + pg_freeresult($this->queryId); |
|
215 | + $this->queryId = 0; |
|
216 | + } |
|
217 | + return $stat; |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * Db::seek() |
|
222 | + * |
|
223 | + * @param mixed $pos |
|
224 | + * @return void |
|
225 | + */ |
|
226 | + public function seek($pos) |
|
227 | + { |
|
228 | + $this->Row = $pos; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Db::transactionBegin() |
|
233 | + * |
|
234 | + * @return mixed |
|
235 | + */ |
|
236 | + public function transactionBegin() |
|
237 | + { |
|
238 | + return $this->query('begin'); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Db::transactionCommit() |
|
243 | + * @return bool|mixed |
|
244 | + */ |
|
245 | + public function transactionCommit() |
|
246 | + { |
|
247 | + if (!$this->Errno) { |
|
248 | + return pg_exec($this->linkId, 'commit'); |
|
249 | + } else { |
|
250 | + return false; |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Db::transactionAbort() |
|
256 | + * @return mixed |
|
257 | + */ |
|
258 | + public function transactionAbort() |
|
259 | + { |
|
260 | + return pg_exec($this->linkId, 'rollback'); |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Db::getLastInsertId() |
|
265 | + * @param mixed $table |
|
266 | + * @param mixed $field |
|
267 | + * @return int |
|
268 | + */ |
|
269 | + public function getLastInsertId($table, $field) |
|
270 | + { |
|
271 | + /* This will get the last insert ID created on the current connection. Should only be called |
|
272 | 272 | * after an insert query is run on a table that has an auto incrementing field. Of note, table |
273 | 273 | * and field are required because pgsql returns the last inserted OID, which is unique across |
274 | 274 | * an entire installation. These params allow us to retrieve the sequenced field without adding |
275 | 275 | * conditional code to the apps. |
276 | 276 | */ |
277 | - if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
278 | - return -1; |
|
279 | - } |
|
280 | - |
|
281 | - $oid = pg_getlastoid($this->queryId); |
|
282 | - if ($oid == -1) { |
|
283 | - return -1; |
|
284 | - } |
|
285 | - |
|
286 | - $result = @pg_exec($this->linkId, "select $field from $table where oid=$oid"); |
|
287 | - if (!$result) { |
|
288 | - return -1; |
|
289 | - } |
|
290 | - |
|
291 | - $Record = @pg_fetch_array($result, 0); |
|
292 | - @pg_freeresult($result); |
|
293 | - if (!is_array($Record)) /* OID not found? */ |
|
294 | - { |
|
295 | - return -1; |
|
296 | - } |
|
297 | - |
|
298 | - return $Record[0]; |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Db::lock() |
|
303 | - * @param mixed $table |
|
304 | - * @param string $mode |
|
305 | - * @return int|mixed |
|
306 | - */ |
|
307 | - public function lock($table, $mode = 'write') |
|
308 | - { |
|
309 | - $result = $this->transactionBegin(); |
|
310 | - |
|
311 | - if ($mode == 'write') { |
|
312 | - if (is_array($table)) { |
|
313 | - foreach ($table as $t) { |
|
314 | - $result = pg_exec($this->linkId, 'lock table '.$t[1].' in share mode'); |
|
315 | - } |
|
316 | - } else { |
|
317 | - $result = pg_exec($this->linkId, 'lock table '.$table.' in share mode'); |
|
318 | - } |
|
319 | - } else { |
|
320 | - $result = 1; |
|
321 | - } |
|
322 | - |
|
323 | - return $result; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Db::unlock() |
|
328 | - * @return bool|mixed |
|
329 | - */ |
|
330 | - public function unlock() |
|
331 | - { |
|
332 | - return $this->transactionCommit(); |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Db::affectedRows() |
|
337 | - * @return void |
|
338 | - */ |
|
339 | - public function affectedRows() |
|
340 | - { |
|
341 | - return pg_cmdtuples($this->queryId); |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Db::num_rows() |
|
346 | - * @return int |
|
347 | - */ |
|
348 | - public function num_rows() |
|
349 | - { |
|
350 | - return pg_numrows($this->queryId); |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Db::num_fields() |
|
355 | - * @return int |
|
356 | - */ |
|
357 | - public function num_fields() |
|
358 | - { |
|
359 | - return pg_numfields($this->queryId); |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * @param mixed $msg |
|
364 | - * @param string $line |
|
365 | - * @param string $file |
|
366 | - * @return mixed|void |
|
367 | - */ |
|
368 | - public function haltmsg($msg, $line = '', $file = '') |
|
369 | - { |
|
370 | - $this->log("Database error: $msg", $line, $file, 'error'); |
|
371 | - if ($this->Errno != '0' || $this->Error != '()') { |
|
372 | - $this->log('PostgreSQL Error: '.pg_last_error($this->linkId), $line, $file, 'error'); |
|
373 | - } |
|
374 | - $this->logBackTrace($msg, $line, $file); |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Db::tableNames() |
|
379 | - * |
|
380 | - * @return array |
|
381 | - */ |
|
382 | - public function tableNames() |
|
383 | - { |
|
384 | - $return = []; |
|
385 | - $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'"); |
|
386 | - $i = 0; |
|
387 | - while ($this->next_record()) { |
|
388 | - $return[$i]['table_name'] = $this->f(0); |
|
389 | - $return[$i]['tablespace_name'] = $this->database; |
|
390 | - $return[$i]['database'] = $this->database; |
|
391 | - ++$i; |
|
392 | - } |
|
393 | - return $return; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Db::indexNames() |
|
398 | - * |
|
399 | - * @return array |
|
400 | - */ |
|
401 | - public function indexNames() |
|
402 | - { |
|
403 | - $return = []; |
|
404 | - $this->query("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relkind ='i' ORDER BY relname"); |
|
405 | - $i = 0; |
|
406 | - while ($this->next_record()) { |
|
407 | - $return[$i]['index_name'] = $this->f(0); |
|
408 | - $return[$i]['tablespace_name'] = $this->database; |
|
409 | - $return[$i]['database'] = $this->database; |
|
410 | - ++$i; |
|
411 | - } |
|
412 | - return $return; |
|
413 | - } |
|
277 | + if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
278 | + return -1; |
|
279 | + } |
|
280 | + |
|
281 | + $oid = pg_getlastoid($this->queryId); |
|
282 | + if ($oid == -1) { |
|
283 | + return -1; |
|
284 | + } |
|
285 | + |
|
286 | + $result = @pg_exec($this->linkId, "select $field from $table where oid=$oid"); |
|
287 | + if (!$result) { |
|
288 | + return -1; |
|
289 | + } |
|
290 | + |
|
291 | + $Record = @pg_fetch_array($result, 0); |
|
292 | + @pg_freeresult($result); |
|
293 | + if (!is_array($Record)) /* OID not found? */ |
|
294 | + { |
|
295 | + return -1; |
|
296 | + } |
|
297 | + |
|
298 | + return $Record[0]; |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Db::lock() |
|
303 | + * @param mixed $table |
|
304 | + * @param string $mode |
|
305 | + * @return int|mixed |
|
306 | + */ |
|
307 | + public function lock($table, $mode = 'write') |
|
308 | + { |
|
309 | + $result = $this->transactionBegin(); |
|
310 | + |
|
311 | + if ($mode == 'write') { |
|
312 | + if (is_array($table)) { |
|
313 | + foreach ($table as $t) { |
|
314 | + $result = pg_exec($this->linkId, 'lock table '.$t[1].' in share mode'); |
|
315 | + } |
|
316 | + } else { |
|
317 | + $result = pg_exec($this->linkId, 'lock table '.$table.' in share mode'); |
|
318 | + } |
|
319 | + } else { |
|
320 | + $result = 1; |
|
321 | + } |
|
322 | + |
|
323 | + return $result; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Db::unlock() |
|
328 | + * @return bool|mixed |
|
329 | + */ |
|
330 | + public function unlock() |
|
331 | + { |
|
332 | + return $this->transactionCommit(); |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Db::affectedRows() |
|
337 | + * @return void |
|
338 | + */ |
|
339 | + public function affectedRows() |
|
340 | + { |
|
341 | + return pg_cmdtuples($this->queryId); |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Db::num_rows() |
|
346 | + * @return int |
|
347 | + */ |
|
348 | + public function num_rows() |
|
349 | + { |
|
350 | + return pg_numrows($this->queryId); |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Db::num_fields() |
|
355 | + * @return int |
|
356 | + */ |
|
357 | + public function num_fields() |
|
358 | + { |
|
359 | + return pg_numfields($this->queryId); |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * @param mixed $msg |
|
364 | + * @param string $line |
|
365 | + * @param string $file |
|
366 | + * @return mixed|void |
|
367 | + */ |
|
368 | + public function haltmsg($msg, $line = '', $file = '') |
|
369 | + { |
|
370 | + $this->log("Database error: $msg", $line, $file, 'error'); |
|
371 | + if ($this->Errno != '0' || $this->Error != '()') { |
|
372 | + $this->log('PostgreSQL Error: '.pg_last_error($this->linkId), $line, $file, 'error'); |
|
373 | + } |
|
374 | + $this->logBackTrace($msg, $line, $file); |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Db::tableNames() |
|
379 | + * |
|
380 | + * @return array |
|
381 | + */ |
|
382 | + public function tableNames() |
|
383 | + { |
|
384 | + $return = []; |
|
385 | + $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'"); |
|
386 | + $i = 0; |
|
387 | + while ($this->next_record()) { |
|
388 | + $return[$i]['table_name'] = $this->f(0); |
|
389 | + $return[$i]['tablespace_name'] = $this->database; |
|
390 | + $return[$i]['database'] = $this->database; |
|
391 | + ++$i; |
|
392 | + } |
|
393 | + return $return; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Db::indexNames() |
|
398 | + * |
|
399 | + * @return array |
|
400 | + */ |
|
401 | + public function indexNames() |
|
402 | + { |
|
403 | + $return = []; |
|
404 | + $this->query("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relkind ='i' ORDER BY relname"); |
|
405 | + $i = 0; |
|
406 | + while ($this->next_record()) { |
|
407 | + $return[$i]['index_name'] = $this->f(0); |
|
408 | + $return[$i]['tablespace_name'] = $this->database; |
|
409 | + $return[$i]['database'] = $this->database; |
|
410 | + ++$i; |
|
411 | + } |
|
412 | + return $return; |
|
413 | + } |
|
414 | 414 | } |
@@ -17,8 +17,7 @@ discard block |
||
17 | 17 | * |
18 | 18 | * @access public |
19 | 19 | */ |
20 | -class Db extends Generic implements Db_Interface |
|
21 | -{ |
|
20 | +class Db extends Generic implements Db_Interface { |
|
22 | 21 | /* public: this is an api revision, not a CVS revision. */ |
23 | 22 | public $type = 'pgsql'; |
24 | 23 | public $port = ''; |
@@ -33,8 +32,7 @@ discard block |
||
33 | 32 | * @param false|string $quote optional indicate the value needs quoted |
34 | 33 | * @return string |
35 | 34 | */ |
36 | - public function ifadd($add, $me, $quote = false) |
|
37 | - { |
|
35 | + public function ifadd($add, $me, $quote = false) { |
|
38 | 36 | if ('' != $add) { |
39 | 37 | return ' '.$me.($quote === false ? '' : $quote).$add.($quote === false ? '' : $quote); |
40 | 38 | } |
@@ -45,8 +43,7 @@ discard block |
||
45 | 43 | * @param $string |
46 | 44 | * @return string |
47 | 45 | */ |
48 | - public function real_escape($string = '') |
|
49 | - { |
|
46 | + public function real_escape($string = '') { |
|
50 | 47 | return $this->escape($string); |
51 | 48 | } |
52 | 49 | |
@@ -56,8 +53,7 @@ discard block |
||
56 | 53 | * @param string $database the name of the database to use |
57 | 54 | * @return void |
58 | 55 | */ |
59 | - public function useDb($database) |
|
60 | - { |
|
56 | + public function useDb($database) { |
|
61 | 57 | $this->selectDb($database); |
62 | 58 | } |
63 | 59 | |
@@ -67,8 +63,7 @@ discard block |
||
67 | 63 | * @param string $database the name of the database to use |
68 | 64 | * @return void |
69 | 65 | */ |
70 | - public function selectDb($database) |
|
71 | - { |
|
66 | + public function selectDb($database) { |
|
72 | 67 | /*if ($database != $this->database) { |
73 | 68 | $this->database = $database; |
74 | 69 | $this->linkId = null; |
@@ -80,8 +75,7 @@ discard block |
||
80 | 75 | * Db::connect() |
81 | 76 | * @return void |
82 | 77 | */ |
83 | - public function connect() |
|
84 | - { |
|
78 | + public function connect() { |
|
85 | 79 | if (0 == $this->linkId) { |
86 | 80 | $connectString = trim($this->ifadd($this->host, 'host='). |
87 | 81 | $this->ifadd($this->port, 'port='). |
@@ -101,8 +95,7 @@ discard block |
||
101 | 95 | * Db::disconnect() |
102 | 96 | * @return bool |
103 | 97 | */ |
104 | - public function disconnect() |
|
105 | - { |
|
98 | + public function disconnect() { |
|
106 | 99 | return @pg_close($this->linkId); |
107 | 100 | } |
108 | 101 | |
@@ -117,8 +110,7 @@ discard block |
||
117 | 110 | * @param string $file optionally pass __FILE__ calling the query for logging |
118 | 111 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
119 | 112 | */ |
120 | - public function queryReturn($query, $line = '', $file = '') |
|
121 | - { |
|
113 | + public function queryReturn($query, $line = '', $file = '') { |
|
122 | 114 | $this->query($query, $line, $file); |
123 | 115 | if ($this->num_rows() == 0) { |
124 | 116 | return false; |
@@ -144,8 +136,7 @@ discard block |
||
144 | 136 | * @param string $file optionally pass __FILE__ calling the query for logging |
145 | 137 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
146 | 138 | */ |
147 | - public function qr($query, $line = '', $file = '') |
|
148 | - { |
|
139 | + public function qr($query, $line = '', $file = '') { |
|
149 | 140 | return $this->queryReturn($query, $line, $file); |
150 | 141 | } |
151 | 142 | |
@@ -159,8 +150,7 @@ discard block |
||
159 | 150 | * @param string $file |
160 | 151 | * @return mixed 0 if no query or query id handler, safe to ignore this return |
161 | 152 | */ |
162 | - public function query($queryString, $line = '', $file = '') |
|
163 | - { |
|
153 | + public function query($queryString, $line = '', $file = '') { |
|
164 | 154 | /* No empty queries, please, since PHP4 chokes on them. */ |
165 | 155 | /* The empty query string is passed on from the constructor, |
166 | 156 | * when calling the class without a query, e.g. in situations |
@@ -191,8 +181,7 @@ discard block |
||
191 | 181 | * |
192 | 182 | * @return void |
193 | 183 | */ |
194 | - public function free() |
|
195 | - { |
|
184 | + public function free() { |
|
196 | 185 | @pg_freeresult($this->queryId); |
197 | 186 | $this->queryId = 0; |
198 | 187 | } |
@@ -202,8 +191,7 @@ discard block |
||
202 | 191 | * @param mixed $resultType |
203 | 192 | * @return bool |
204 | 193 | */ |
205 | - public function next_record($resultType = PGSQL_BOTH) |
|
206 | - { |
|
194 | + public function next_record($resultType = PGSQL_BOTH) { |
|
207 | 195 | $this->Record = @pg_fetch_array($this->queryId, $this->Row++, $resultType); |
208 | 196 | |
209 | 197 | $this->Error = pg_errormessage($this->linkId); |
@@ -223,8 +211,7 @@ discard block |
||
223 | 211 | * @param mixed $pos |
224 | 212 | * @return void |
225 | 213 | */ |
226 | - public function seek($pos) |
|
227 | - { |
|
214 | + public function seek($pos) { |
|
228 | 215 | $this->Row = $pos; |
229 | 216 | } |
230 | 217 | |
@@ -233,8 +220,7 @@ discard block |
||
233 | 220 | * |
234 | 221 | * @return mixed |
235 | 222 | */ |
236 | - public function transactionBegin() |
|
237 | - { |
|
223 | + public function transactionBegin() { |
|
238 | 224 | return $this->query('begin'); |
239 | 225 | } |
240 | 226 | |
@@ -242,8 +228,7 @@ discard block |
||
242 | 228 | * Db::transactionCommit() |
243 | 229 | * @return bool|mixed |
244 | 230 | */ |
245 | - public function transactionCommit() |
|
246 | - { |
|
231 | + public function transactionCommit() { |
|
247 | 232 | if (!$this->Errno) { |
248 | 233 | return pg_exec($this->linkId, 'commit'); |
249 | 234 | } else { |
@@ -255,8 +240,7 @@ discard block |
||
255 | 240 | * Db::transactionAbort() |
256 | 241 | * @return mixed |
257 | 242 | */ |
258 | - public function transactionAbort() |
|
259 | - { |
|
243 | + public function transactionAbort() { |
|
260 | 244 | return pg_exec($this->linkId, 'rollback'); |
261 | 245 | } |
262 | 246 | |
@@ -266,8 +250,7 @@ discard block |
||
266 | 250 | * @param mixed $field |
267 | 251 | * @return int |
268 | 252 | */ |
269 | - public function getLastInsertId($table, $field) |
|
270 | - { |
|
253 | + public function getLastInsertId($table, $field) { |
|
271 | 254 | /* This will get the last insert ID created on the current connection. Should only be called |
272 | 255 | * after an insert query is run on a table that has an auto incrementing field. Of note, table |
273 | 256 | * and field are required because pgsql returns the last inserted OID, which is unique across |
@@ -304,8 +287,7 @@ discard block |
||
304 | 287 | * @param string $mode |
305 | 288 | * @return int|mixed |
306 | 289 | */ |
307 | - public function lock($table, $mode = 'write') |
|
308 | - { |
|
290 | + public function lock($table, $mode = 'write') { |
|
309 | 291 | $result = $this->transactionBegin(); |
310 | 292 | |
311 | 293 | if ($mode == 'write') { |
@@ -327,8 +309,7 @@ discard block |
||
327 | 309 | * Db::unlock() |
328 | 310 | * @return bool|mixed |
329 | 311 | */ |
330 | - public function unlock() |
|
331 | - { |
|
312 | + public function unlock() { |
|
332 | 313 | return $this->transactionCommit(); |
333 | 314 | } |
334 | 315 | |
@@ -336,8 +317,7 @@ discard block |
||
336 | 317 | * Db::affectedRows() |
337 | 318 | * @return void |
338 | 319 | */ |
339 | - public function affectedRows() |
|
340 | - { |
|
320 | + public function affectedRows() { |
|
341 | 321 | return pg_cmdtuples($this->queryId); |
342 | 322 | } |
343 | 323 | |
@@ -345,8 +325,7 @@ discard block |
||
345 | 325 | * Db::num_rows() |
346 | 326 | * @return int |
347 | 327 | */ |
348 | - public function num_rows() |
|
349 | - { |
|
328 | + public function num_rows() { |
|
350 | 329 | return pg_numrows($this->queryId); |
351 | 330 | } |
352 | 331 | |
@@ -354,8 +333,7 @@ discard block |
||
354 | 333 | * Db::num_fields() |
355 | 334 | * @return int |
356 | 335 | */ |
357 | - public function num_fields() |
|
358 | - { |
|
336 | + public function num_fields() { |
|
359 | 337 | return pg_numfields($this->queryId); |
360 | 338 | } |
361 | 339 | |
@@ -365,8 +343,7 @@ discard block |
||
365 | 343 | * @param string $file |
366 | 344 | * @return mixed|void |
367 | 345 | */ |
368 | - public function haltmsg($msg, $line = '', $file = '') |
|
369 | - { |
|
346 | + public function haltmsg($msg, $line = '', $file = '') { |
|
370 | 347 | $this->log("Database error: $msg", $line, $file, 'error'); |
371 | 348 | if ($this->Errno != '0' || $this->Error != '()') { |
372 | 349 | $this->log('PostgreSQL Error: '.pg_last_error($this->linkId), $line, $file, 'error'); |
@@ -379,8 +356,7 @@ discard block |
||
379 | 356 | * |
380 | 357 | * @return array |
381 | 358 | */ |
382 | - public function tableNames() |
|
383 | - { |
|
359 | + public function tableNames() { |
|
384 | 360 | $return = []; |
385 | 361 | $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'"); |
386 | 362 | $i = 0; |
@@ -398,8 +374,7 @@ discard block |
||
398 | 374 | * |
399 | 375 | * @return array |
400 | 376 | */ |
401 | - public function indexNames() |
|
402 | - { |
|
377 | + public function indexNames() { |
|
403 | 378 | $return = []; |
404 | 379 | $this->query("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relkind ='i' ORDER BY relname"); |
405 | 380 | $i = 0; |
@@ -1,12 +1,12 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | - /** |
|
4 | - * Generic SQL Driver Related Functionality |
|
5 | - * @author Joe Huss <[email protected]> |
|
6 | - * @copyright 2019 |
|
7 | - * @package MyAdmin |
|
8 | - * @category SQL |
|
9 | - */ |
|
3 | + /** |
|
4 | + * Generic SQL Driver Related Functionality |
|
5 | + * @author Joe Huss <[email protected]> |
|
6 | + * @copyright 2019 |
|
7 | + * @package MyAdmin |
|
8 | + * @category SQL |
|
9 | + */ |
|
10 | 10 | |
11 | 11 | namespace MyDb; |
12 | 12 | |
@@ -17,184 +17,184 @@ discard block |
||
17 | 17 | */ |
18 | 18 | class Loader |
19 | 19 | { |
20 | - /* public: connection parameters */ |
|
21 | - public $Type = 'mysqli'; |
|
22 | - public $host = 'localhost'; |
|
23 | - public $database = ''; |
|
24 | - public $user = ''; |
|
25 | - public $password = ''; |
|
26 | - |
|
27 | - /* public: configuration parameters */ |
|
28 | - public $autoStripslashes = false; |
|
29 | - public $Debug = 0; // Set to 1 for debugging messages. |
|
30 | - public $haltOnError = 'yes'; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning) |
|
31 | - |
|
32 | - /* public: result array and current row number */ |
|
33 | - public $Record = []; |
|
34 | - public $Row; |
|
35 | - |
|
36 | - /* public: current error number and error text */ |
|
37 | - public $Errno = 0; |
|
38 | - public $Error = ''; |
|
39 | - |
|
40 | - public $type; |
|
41 | - |
|
42 | - /* private: link and query handles */ |
|
43 | - public $linkId = 0; |
|
44 | - public $queryId = 0; |
|
45 | - |
|
46 | - public $characterSet = ''; |
|
47 | - public $collation = ''; |
|
48 | - |
|
49 | - /** |
|
50 | - * Constructs the db handler, can optionally specify connection parameters |
|
51 | - * |
|
52 | - * @param string $Type Optional The database type mysql/mysqli/pdo/adodb/pgsql |
|
53 | - * @param string $database Optional The database name |
|
54 | - * @param string $user Optional The username to connect with |
|
55 | - * @param string $password Optional The password to use |
|
56 | - * @param string $host Optional The hostname where the server is, or default to localhost |
|
57 | - * @param string $query Optional query to perform immediately |
|
58 | - */ |
|
59 | - public function __construct($Type = '', $database = '', $user = '', $password = '', $host = 'localhost', $query = '') |
|
60 | - { |
|
61 | - $this->Type = $Type; |
|
62 | - if (!defined('db')) { |
|
63 | - switch ($this->Type) { |
|
64 | - case 'mysqli': |
|
65 | - include_once 'class.db_mysqli.inc.php'; |
|
66 | - break; |
|
67 | - case 'mysql': |
|
68 | - include_once 'class.db_mysql.inc.php'; |
|
69 | - break; |
|
70 | - case 'adodb': |
|
71 | - include_once 'class.db_adodb.inc.php'; |
|
72 | - break; |
|
73 | - case 'mdb2': |
|
74 | - include_once 'class.db_mdb2.inc.php'; |
|
75 | - break; |
|
76 | - case 'pdo': |
|
77 | - include_once 'class.db_pdo.inc.php'; |
|
78 | - break; |
|
79 | - case 'pgsql': |
|
80 | - include_once 'class.db_pgsql.inc.php'; |
|
81 | - break; |
|
82 | - default: |
|
83 | - $this->log('Could not find DB class '.$this->Type, __LINE__, __FILE__); |
|
84 | - break; |
|
85 | - } |
|
86 | - } |
|
87 | - $this->database = $database; |
|
88 | - $this->user = $user; |
|
89 | - $this->password = $password; |
|
90 | - $this->host = $host; |
|
91 | - if ($query != '') { |
|
92 | - $this->query($query); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param $message |
|
98 | - * @param string $line |
|
99 | - * @param string $file |
|
100 | - */ |
|
101 | - public function log($message, $line = '', $file = '') |
|
102 | - { |
|
103 | - error_log($message); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return int |
|
108 | - */ |
|
109 | - public function linkId() |
|
110 | - { |
|
111 | - return $this->linkId; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return int |
|
116 | - */ |
|
117 | - public function queryId() |
|
118 | - { |
|
119 | - return $this->queryId; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @param $str |
|
124 | - * @return string |
|
125 | - */ |
|
126 | - public function dbAddslashes($str) |
|
127 | - { |
|
128 | - if (!isset($str) || $str == '') { |
|
129 | - return ''; |
|
130 | - } |
|
131 | - |
|
132 | - return addslashes($str); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * db:qr() |
|
137 | - * |
|
138 | - * alias of queryReturn() |
|
139 | - * |
|
140 | - * @param mixed $query SQL Query to be used |
|
141 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
142 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
143 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
144 | - */ |
|
145 | - public function qr($query, $line = '', $file = '') |
|
146 | - { |
|
147 | - return $this->queryReturn($query, $line, $file); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * error handling |
|
152 | - * |
|
153 | - * @param mixed $msg |
|
154 | - * @param string $line |
|
155 | - * @param string $file |
|
156 | - * @return void |
|
157 | - */ |
|
158 | - public function halt($msg, $line = '', $file = '') |
|
159 | - { |
|
160 | - $this->unlock(false); |
|
161 | - |
|
162 | - if ($this->haltOnError == 'no') { |
|
163 | - return; |
|
164 | - } |
|
165 | - $this->haltmsg($msg); |
|
166 | - |
|
167 | - if ($file) { |
|
168 | - error_log("File: $file"); |
|
169 | - } |
|
170 | - if ($line) { |
|
171 | - error_log("Line: $line"); |
|
172 | - } |
|
173 | - if ($this->haltOnError != 'report') { |
|
174 | - echo '<p><b>Session halted.</b>'; |
|
175 | - // FIXME! Add check for error levels |
|
176 | - if (isset($GLOBALS['tf'])) { |
|
177 | - $GLOBALS['tf']->terminate(); |
|
178 | - } |
|
179 | - } |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * @param $msg |
|
184 | - */ |
|
185 | - public function haltmsg($msg) |
|
186 | - { |
|
187 | - $this->log("Database error: $msg", __LINE__, __FILE__); |
|
188 | - if ($this->Errno != '0' || $this->Error != '()') { |
|
189 | - $this->log('SQL Error: '.$this->Errno.' ('.$this->Error.')', __LINE__, __FILE__); |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @return array |
|
195 | - */ |
|
196 | - public function indexNames() |
|
197 | - { |
|
198 | - return []; |
|
199 | - } |
|
20 | + /* public: connection parameters */ |
|
21 | + public $Type = 'mysqli'; |
|
22 | + public $host = 'localhost'; |
|
23 | + public $database = ''; |
|
24 | + public $user = ''; |
|
25 | + public $password = ''; |
|
26 | + |
|
27 | + /* public: configuration parameters */ |
|
28 | + public $autoStripslashes = false; |
|
29 | + public $Debug = 0; // Set to 1 for debugging messages. |
|
30 | + public $haltOnError = 'yes'; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning) |
|
31 | + |
|
32 | + /* public: result array and current row number */ |
|
33 | + public $Record = []; |
|
34 | + public $Row; |
|
35 | + |
|
36 | + /* public: current error number and error text */ |
|
37 | + public $Errno = 0; |
|
38 | + public $Error = ''; |
|
39 | + |
|
40 | + public $type; |
|
41 | + |
|
42 | + /* private: link and query handles */ |
|
43 | + public $linkId = 0; |
|
44 | + public $queryId = 0; |
|
45 | + |
|
46 | + public $characterSet = ''; |
|
47 | + public $collation = ''; |
|
48 | + |
|
49 | + /** |
|
50 | + * Constructs the db handler, can optionally specify connection parameters |
|
51 | + * |
|
52 | + * @param string $Type Optional The database type mysql/mysqli/pdo/adodb/pgsql |
|
53 | + * @param string $database Optional The database name |
|
54 | + * @param string $user Optional The username to connect with |
|
55 | + * @param string $password Optional The password to use |
|
56 | + * @param string $host Optional The hostname where the server is, or default to localhost |
|
57 | + * @param string $query Optional query to perform immediately |
|
58 | + */ |
|
59 | + public function __construct($Type = '', $database = '', $user = '', $password = '', $host = 'localhost', $query = '') |
|
60 | + { |
|
61 | + $this->Type = $Type; |
|
62 | + if (!defined('db')) { |
|
63 | + switch ($this->Type) { |
|
64 | + case 'mysqli': |
|
65 | + include_once 'class.db_mysqli.inc.php'; |
|
66 | + break; |
|
67 | + case 'mysql': |
|
68 | + include_once 'class.db_mysql.inc.php'; |
|
69 | + break; |
|
70 | + case 'adodb': |
|
71 | + include_once 'class.db_adodb.inc.php'; |
|
72 | + break; |
|
73 | + case 'mdb2': |
|
74 | + include_once 'class.db_mdb2.inc.php'; |
|
75 | + break; |
|
76 | + case 'pdo': |
|
77 | + include_once 'class.db_pdo.inc.php'; |
|
78 | + break; |
|
79 | + case 'pgsql': |
|
80 | + include_once 'class.db_pgsql.inc.php'; |
|
81 | + break; |
|
82 | + default: |
|
83 | + $this->log('Could not find DB class '.$this->Type, __LINE__, __FILE__); |
|
84 | + break; |
|
85 | + } |
|
86 | + } |
|
87 | + $this->database = $database; |
|
88 | + $this->user = $user; |
|
89 | + $this->password = $password; |
|
90 | + $this->host = $host; |
|
91 | + if ($query != '') { |
|
92 | + $this->query($query); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param $message |
|
98 | + * @param string $line |
|
99 | + * @param string $file |
|
100 | + */ |
|
101 | + public function log($message, $line = '', $file = '') |
|
102 | + { |
|
103 | + error_log($message); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return int |
|
108 | + */ |
|
109 | + public function linkId() |
|
110 | + { |
|
111 | + return $this->linkId; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return int |
|
116 | + */ |
|
117 | + public function queryId() |
|
118 | + { |
|
119 | + return $this->queryId; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @param $str |
|
124 | + * @return string |
|
125 | + */ |
|
126 | + public function dbAddslashes($str) |
|
127 | + { |
|
128 | + if (!isset($str) || $str == '') { |
|
129 | + return ''; |
|
130 | + } |
|
131 | + |
|
132 | + return addslashes($str); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * db:qr() |
|
137 | + * |
|
138 | + * alias of queryReturn() |
|
139 | + * |
|
140 | + * @param mixed $query SQL Query to be used |
|
141 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
142 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
143 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
144 | + */ |
|
145 | + public function qr($query, $line = '', $file = '') |
|
146 | + { |
|
147 | + return $this->queryReturn($query, $line, $file); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * error handling |
|
152 | + * |
|
153 | + * @param mixed $msg |
|
154 | + * @param string $line |
|
155 | + * @param string $file |
|
156 | + * @return void |
|
157 | + */ |
|
158 | + public function halt($msg, $line = '', $file = '') |
|
159 | + { |
|
160 | + $this->unlock(false); |
|
161 | + |
|
162 | + if ($this->haltOnError == 'no') { |
|
163 | + return; |
|
164 | + } |
|
165 | + $this->haltmsg($msg); |
|
166 | + |
|
167 | + if ($file) { |
|
168 | + error_log("File: $file"); |
|
169 | + } |
|
170 | + if ($line) { |
|
171 | + error_log("Line: $line"); |
|
172 | + } |
|
173 | + if ($this->haltOnError != 'report') { |
|
174 | + echo '<p><b>Session halted.</b>'; |
|
175 | + // FIXME! Add check for error levels |
|
176 | + if (isset($GLOBALS['tf'])) { |
|
177 | + $GLOBALS['tf']->terminate(); |
|
178 | + } |
|
179 | + } |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * @param $msg |
|
184 | + */ |
|
185 | + public function haltmsg($msg) |
|
186 | + { |
|
187 | + $this->log("Database error: $msg", __LINE__, __FILE__); |
|
188 | + if ($this->Errno != '0' || $this->Error != '()') { |
|
189 | + $this->log('SQL Error: '.$this->Errno.' ('.$this->Error.')', __LINE__, __FILE__); |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @return array |
|
195 | + */ |
|
196 | + public function indexNames() |
|
197 | + { |
|
198 | + return []; |
|
199 | + } |
|
200 | 200 | } |
@@ -15,8 +15,7 @@ discard block |
||
15 | 15 | * |
16 | 16 | * @package MyDb |
17 | 17 | */ |
18 | -class Loader |
|
19 | -{ |
|
18 | +class Loader { |
|
20 | 19 | /* public: connection parameters */ |
21 | 20 | public $Type = 'mysqli'; |
22 | 21 | public $host = 'localhost'; |
@@ -56,8 +55,7 @@ discard block |
||
56 | 55 | * @param string $host Optional The hostname where the server is, or default to localhost |
57 | 56 | * @param string $query Optional query to perform immediately |
58 | 57 | */ |
59 | - public function __construct($Type = '', $database = '', $user = '', $password = '', $host = 'localhost', $query = '') |
|
60 | - { |
|
58 | + public function __construct($Type = '', $database = '', $user = '', $password = '', $host = 'localhost', $query = '') { |
|
61 | 59 | $this->Type = $Type; |
62 | 60 | if (!defined('db')) { |
63 | 61 | switch ($this->Type) { |
@@ -98,24 +96,21 @@ discard block |
||
98 | 96 | * @param string $line |
99 | 97 | * @param string $file |
100 | 98 | */ |
101 | - public function log($message, $line = '', $file = '') |
|
102 | - { |
|
99 | + public function log($message, $line = '', $file = '') { |
|
103 | 100 | error_log($message); |
104 | 101 | } |
105 | 102 | |
106 | 103 | /** |
107 | 104 | * @return int |
108 | 105 | */ |
109 | - public function linkId() |
|
110 | - { |
|
106 | + public function linkId() { |
|
111 | 107 | return $this->linkId; |
112 | 108 | } |
113 | 109 | |
114 | 110 | /** |
115 | 111 | * @return int |
116 | 112 | */ |
117 | - public function queryId() |
|
118 | - { |
|
113 | + public function queryId() { |
|
119 | 114 | return $this->queryId; |
120 | 115 | } |
121 | 116 | |
@@ -123,8 +118,7 @@ discard block |
||
123 | 118 | * @param $str |
124 | 119 | * @return string |
125 | 120 | */ |
126 | - public function dbAddslashes($str) |
|
127 | - { |
|
121 | + public function dbAddslashes($str) { |
|
128 | 122 | if (!isset($str) || $str == '') { |
129 | 123 | return ''; |
130 | 124 | } |
@@ -142,8 +136,7 @@ discard block |
||
142 | 136 | * @param string $file optionally pass __FILE__ calling the query for logging |
143 | 137 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
144 | 138 | */ |
145 | - public function qr($query, $line = '', $file = '') |
|
146 | - { |
|
139 | + public function qr($query, $line = '', $file = '') { |
|
147 | 140 | return $this->queryReturn($query, $line, $file); |
148 | 141 | } |
149 | 142 | |
@@ -155,8 +148,7 @@ discard block |
||
155 | 148 | * @param string $file |
156 | 149 | * @return void |
157 | 150 | */ |
158 | - public function halt($msg, $line = '', $file = '') |
|
159 | - { |
|
151 | + public function halt($msg, $line = '', $file = '') { |
|
160 | 152 | $this->unlock(false); |
161 | 153 | |
162 | 154 | if ($this->haltOnError == 'no') { |
@@ -182,8 +174,7 @@ discard block |
||
182 | 174 | /** |
183 | 175 | * @param $msg |
184 | 176 | */ |
185 | - public function haltmsg($msg) |
|
186 | - { |
|
177 | + public function haltmsg($msg) { |
|
187 | 178 | $this->log("Database error: $msg", __LINE__, __FILE__); |
188 | 179 | if ($this->Errno != '0' || $this->Error != '()') { |
189 | 180 | $this->log('SQL Error: '.$this->Errno.' ('.$this->Error.')', __LINE__, __FILE__); |
@@ -193,8 +184,7 @@ discard block |
||
193 | 184 | /** |
194 | 185 | * @return array |
195 | 186 | */ |
196 | - public function indexNames() |
|
197 | - { |
|
187 | + public function indexNames() { |
|
198 | 188 | return []; |
199 | 189 | } |
200 | 190 | } |
@@ -20,81 +20,81 @@ |
||
20 | 20 | */ |
21 | 21 | class Db extends MysqliDb implements Db_Interface |
22 | 22 | { |
23 | - public $host = 'localhost'; |
|
24 | - public $user = 'pdns'; |
|
25 | - public $password = ''; |
|
26 | - public $database = 'pdns'; |
|
27 | - public $type = 'mdb2'; |
|
28 | - public $error = false; |
|
29 | - public $message = ''; |
|
23 | + public $host = 'localhost'; |
|
24 | + public $user = 'pdns'; |
|
25 | + public $password = ''; |
|
26 | + public $database = 'pdns'; |
|
27 | + public $type = 'mdb2'; |
|
28 | + public $error = false; |
|
29 | + public $message = ''; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Db::quote() |
|
33 | - * @param string $text |
|
34 | - * @param string $type |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function quote($text = '', $type = 'text') |
|
38 | - { |
|
39 | - switch ($type) { |
|
40 | - case 'text': |
|
41 | - return "'".$this->escape($text)."'"; |
|
42 | - break; |
|
43 | - case 'integer': |
|
44 | - return (int) $text; |
|
45 | - break; |
|
46 | - default: |
|
47 | - return $text; |
|
48 | - break; |
|
49 | - } |
|
50 | - } |
|
31 | + /** |
|
32 | + * Db::quote() |
|
33 | + * @param string $text |
|
34 | + * @param string $type |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function quote($text = '', $type = 'text') |
|
38 | + { |
|
39 | + switch ($type) { |
|
40 | + case 'text': |
|
41 | + return "'".$this->escape($text)."'"; |
|
42 | + break; |
|
43 | + case 'integer': |
|
44 | + return (int) $text; |
|
45 | + break; |
|
46 | + default: |
|
47 | + return $text; |
|
48 | + break; |
|
49 | + } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Db::queryOne() |
|
54 | - * |
|
55 | - * @param mixed $query |
|
56 | - * @param string $line |
|
57 | - * @param string $file |
|
58 | - * @return bool |
|
59 | - */ |
|
60 | - public function queryOne($query, $line = '', $file = '') |
|
61 | - { |
|
62 | - $this->query($query, $line, $file); |
|
63 | - if ($this->num_rows() > 0) { |
|
64 | - $this->next_record(); |
|
65 | - return $this->f(0); |
|
66 | - } else { |
|
67 | - return 0; |
|
68 | - } |
|
69 | - } |
|
52 | + /** |
|
53 | + * Db::queryOne() |
|
54 | + * |
|
55 | + * @param mixed $query |
|
56 | + * @param string $line |
|
57 | + * @param string $file |
|
58 | + * @return bool |
|
59 | + */ |
|
60 | + public function queryOne($query, $line = '', $file = '') |
|
61 | + { |
|
62 | + $this->query($query, $line, $file); |
|
63 | + if ($this->num_rows() > 0) { |
|
64 | + $this->next_record(); |
|
65 | + return $this->f(0); |
|
66 | + } else { |
|
67 | + return 0; |
|
68 | + } |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Db::queryRow() |
|
73 | - * |
|
74 | - * @param mixed $query |
|
75 | - * @param string $line |
|
76 | - * @param string $file |
|
77 | - * @return array|bool |
|
78 | - */ |
|
79 | - public function queryRow($query, $line = '', $file = '') |
|
80 | - { |
|
81 | - $this->query($query, $line, $file); |
|
82 | - if ($this->num_rows() > 0) { |
|
83 | - $this->next_record(); |
|
84 | - return $this->Record; |
|
85 | - } else { |
|
86 | - return 0; |
|
87 | - } |
|
88 | - } |
|
71 | + /** |
|
72 | + * Db::queryRow() |
|
73 | + * |
|
74 | + * @param mixed $query |
|
75 | + * @param string $line |
|
76 | + * @param string $file |
|
77 | + * @return array|bool |
|
78 | + */ |
|
79 | + public function queryRow($query, $line = '', $file = '') |
|
80 | + { |
|
81 | + $this->query($query, $line, $file); |
|
82 | + if ($this->num_rows() > 0) { |
|
83 | + $this->next_record(); |
|
84 | + return $this->Record; |
|
85 | + } else { |
|
86 | + return 0; |
|
87 | + } |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Db::lastInsertId() |
|
92 | - * @param mixed $table |
|
93 | - * @param mixed $field |
|
94 | - * @return int |
|
95 | - */ |
|
96 | - public function lastInsertId($table, $field) |
|
97 | - { |
|
98 | - return $this->getLastInsertId($table, $field); |
|
99 | - } |
|
90 | + /** |
|
91 | + * Db::lastInsertId() |
|
92 | + * @param mixed $table |
|
93 | + * @param mixed $field |
|
94 | + * @return int |
|
95 | + */ |
|
96 | + public function lastInsertId($table, $field) |
|
97 | + { |
|
98 | + return $this->getLastInsertId($table, $field); |
|
99 | + } |
|
100 | 100 | } |
@@ -18,8 +18,7 @@ discard block |
||
18 | 18 | * |
19 | 19 | * @access public |
20 | 20 | */ |
21 | -class Db extends MysqliDb implements Db_Interface |
|
22 | -{ |
|
21 | +class Db extends MysqliDb implements Db_Interface { |
|
23 | 22 | public $host = 'localhost'; |
24 | 23 | public $user = 'pdns'; |
25 | 24 | public $password = ''; |
@@ -34,8 +33,7 @@ discard block |
||
34 | 33 | * @param string $type |
35 | 34 | * @return string |
36 | 35 | */ |
37 | - public function quote($text = '', $type = 'text') |
|
38 | - { |
|
36 | + public function quote($text = '', $type = 'text') { |
|
39 | 37 | switch ($type) { |
40 | 38 | case 'text': |
41 | 39 | return "'".$this->escape($text)."'"; |
@@ -57,8 +55,7 @@ discard block |
||
57 | 55 | * @param string $file |
58 | 56 | * @return bool |
59 | 57 | */ |
60 | - public function queryOne($query, $line = '', $file = '') |
|
61 | - { |
|
58 | + public function queryOne($query, $line = '', $file = '') { |
|
62 | 59 | $this->query($query, $line, $file); |
63 | 60 | if ($this->num_rows() > 0) { |
64 | 61 | $this->next_record(); |
@@ -76,8 +73,7 @@ discard block |
||
76 | 73 | * @param string $file |
77 | 74 | * @return array|bool |
78 | 75 | */ |
79 | - public function queryRow($query, $line = '', $file = '') |
|
80 | - { |
|
76 | + public function queryRow($query, $line = '', $file = '') { |
|
81 | 77 | $this->query($query, $line, $file); |
82 | 78 | if ($this->num_rows() > 0) { |
83 | 79 | $this->next_record(); |
@@ -93,8 +89,7 @@ discard block |
||
93 | 89 | * @param mixed $field |
94 | 90 | * @return int |
95 | 91 | */ |
96 | - public function lastInsertId($table, $field) |
|
97 | - { |
|
92 | + public function lastInsertId($table, $field) { |
|
98 | 93 | return $this->getLastInsertId($table, $field); |
99 | 94 | } |
100 | 95 | } |
@@ -17,8 +17,7 @@ discard block |
||
17 | 17 | * |
18 | 18 | * @access public |
19 | 19 | */ |
20 | -class Db extends Generic implements Db_Interface |
|
21 | -{ |
|
20 | +class Db extends Generic implements Db_Interface { |
|
22 | 21 | /** |
23 | 22 | * @var string |
24 | 23 | */ |
@@ -30,8 +29,7 @@ discard block |
||
30 | 29 | * @param string $database the name of the database to use |
31 | 30 | * @return void |
32 | 31 | */ |
33 | - public function useDb($database) |
|
34 | - { |
|
32 | + public function useDb($database) { |
|
35 | 33 | $this->selectDb($database); |
36 | 34 | } |
37 | 35 | |
@@ -41,8 +39,7 @@ discard block |
||
41 | 39 | * @param string $database the name of the database to use |
42 | 40 | * @return void |
43 | 41 | */ |
44 | - public function selectDb($database) |
|
45 | - { |
|
42 | + public function selectDb($database) { |
|
46 | 43 | $this->connect(); |
47 | 44 | mysqli_select_db($this->linkId, $database); |
48 | 45 | } |
@@ -57,8 +54,7 @@ discard block |
||
57 | 54 | * @param string $password |
58 | 55 | * @return int|\mysqli |
59 | 56 | */ |
60 | - public function connect($database = '', $host = '', $user = '', $password = '', $port = '') |
|
61 | - { |
|
57 | + public function connect($database = '', $host = '', $user = '', $password = '', $port = '') { |
|
62 | 58 | /* Handle defaults */ |
63 | 59 | if ($database == '') { |
64 | 60 | $database = $this->database; |
@@ -105,8 +101,7 @@ discard block |
||
105 | 101 | * Db::disconnect() |
106 | 102 | * @return bool |
107 | 103 | */ |
108 | - public function disconnect() |
|
109 | - { |
|
104 | + public function disconnect() { |
|
110 | 105 | $return = !is_int($this->linkId) && method_exists($this->linkId, 'close') ? $this->linkId->close() : false; |
111 | 106 | $this->linkId = 0; |
112 | 107 | return $return; |
@@ -116,8 +111,7 @@ discard block |
||
116 | 111 | * @param $string |
117 | 112 | * @return string |
118 | 113 | */ |
119 | - public function real_escape($string = '') |
|
120 | - { |
|
114 | + public function real_escape($string = '') { |
|
121 | 115 | if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
122 | 116 | return $this->escape($string); |
123 | 117 | } |
@@ -128,8 +122,7 @@ discard block |
||
128 | 122 | * discard the query result |
129 | 123 | * @return void |
130 | 124 | */ |
131 | - public function free() |
|
132 | - { |
|
125 | + public function free() { |
|
133 | 126 | if (is_resource($this->queryId)) { |
134 | 127 | @mysqli_free_result($this->queryId); |
135 | 128 | } |
@@ -147,8 +140,7 @@ discard block |
||
147 | 140 | * @param string $file optionally pass __FILE__ calling the query for logging |
148 | 141 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
149 | 142 | */ |
150 | - public function queryReturn($query, $line = '', $file = '') |
|
151 | - { |
|
143 | + public function queryReturn($query, $line = '', $file = '') { |
|
152 | 144 | $this->query($query, $line, $file); |
153 | 145 | if ($this->num_rows() == 0) { |
154 | 146 | return false; |
@@ -174,8 +166,7 @@ discard block |
||
174 | 166 | * @param string $file optionally pass __FILE__ calling the query for logging |
175 | 167 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
176 | 168 | */ |
177 | - public function qr($query, $line = '', $file = '') |
|
178 | - { |
|
169 | + public function qr($query, $line = '', $file = '') { |
|
179 | 170 | return $this->queryReturn($query, $line, $file); |
180 | 171 | } |
181 | 172 | |
@@ -187,8 +178,7 @@ discard block |
||
187 | 178 | * @param string $line |
188 | 179 | * @param string $file |
189 | 180 | */ |
190 | - public function prepare($query, $line = '', $file = '') |
|
191 | - { |
|
181 | + public function prepare($query, $line = '', $file = '') { |
|
192 | 182 | if (!$this->connect()) { |
193 | 183 | return 0; |
194 | 184 | } |
@@ -210,8 +200,7 @@ discard block |
||
210 | 200 | * @param string $file |
211 | 201 | * @return mixed 0 if no query or query id handler, safe to ignore this return |
212 | 202 | */ |
213 | - public function query($queryString, $line = '', $file = '') |
|
214 | - { |
|
203 | + public function query($queryString, $line = '', $file = '') { |
|
215 | 204 | /* No empty queries, please, since PHP4 chokes on them. */ |
216 | 205 | /* The empty query string is passed on from the constructor, |
217 | 206 | * when calling the class without a query, e.g. in situations |
@@ -289,8 +278,7 @@ discard block |
||
289 | 278 | /** |
290 | 279 | * @return array|null|object |
291 | 280 | */ |
292 | - public function fetchObject() |
|
293 | - { |
|
281 | + public function fetchObject() { |
|
294 | 282 | $this->Record = @mysqli_fetch_object($this->queryId); |
295 | 283 | return $this->Record; |
296 | 284 | } |
@@ -303,8 +291,7 @@ discard block |
||
303 | 291 | * @param mixed $resultType |
304 | 292 | * @return bool |
305 | 293 | */ |
306 | - public function next_record($resultType = MYSQLI_BOTH) |
|
307 | - { |
|
294 | + public function next_record($resultType = MYSQLI_BOTH) { |
|
308 | 295 | if ($this->queryId === false) { |
309 | 296 | $this->haltmsg('next_record called with no query pending.'); |
310 | 297 | return 0; |
@@ -328,8 +315,7 @@ discard block |
||
328 | 315 | * @param integer $pos the row numbe starting at 0 to switch to |
329 | 316 | * @return bool whetherit was successfu or not. |
330 | 317 | */ |
331 | - public function seek($pos = 0) |
|
332 | - { |
|
318 | + public function seek($pos = 0) { |
|
333 | 319 | $status = @mysqli_data_seek($this->queryId, $pos); |
334 | 320 | if ($status) { |
335 | 321 | $this->Row = $pos; |
@@ -349,8 +335,7 @@ discard block |
||
349 | 335 | * |
350 | 336 | * @return bool |
351 | 337 | */ |
352 | - public function transactionBegin() |
|
353 | - { |
|
338 | + public function transactionBegin() { |
|
354 | 339 | if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
355 | 340 | return true; |
356 | 341 | } |
@@ -365,8 +350,7 @@ discard block |
||
365 | 350 | * |
366 | 351 | * @return bool |
367 | 352 | */ |
368 | - public function transactionCommit() |
|
369 | - { |
|
353 | + public function transactionCommit() { |
|
370 | 354 | if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
371 | 355 | return true; |
372 | 356 | } |
@@ -378,8 +362,7 @@ discard block |
||
378 | 362 | * |
379 | 363 | * @return bool |
380 | 364 | */ |
381 | - public function transactionAbort() |
|
382 | - { |
|
365 | + public function transactionAbort() { |
|
383 | 366 | if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
384 | 367 | return true; |
385 | 368 | } |
@@ -395,8 +378,7 @@ discard block |
||
395 | 378 | * @param string $field |
396 | 379 | * @return int|string |
397 | 380 | */ |
398 | - public function getLastInsertId($table, $field) |
|
399 | - { |
|
381 | + public function getLastInsertId($table, $field) { |
|
400 | 382 | if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
401 | 383 | return -1; |
402 | 384 | } |
@@ -412,8 +394,7 @@ discard block |
||
412 | 394 | * @param string $mode |
413 | 395 | * @return bool|int|\mysqli_result |
414 | 396 | */ |
415 | - public function lock($table, $mode = 'write') |
|
416 | - { |
|
397 | + public function lock($table, $mode = 'write') { |
|
417 | 398 | $this->connect(); |
418 | 399 | $query = 'lock tables '; |
419 | 400 | if (is_array($table)) { |
@@ -441,8 +422,7 @@ discard block |
||
441 | 422 | * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
442 | 423 | * @return bool|int|\mysqli_result |
443 | 424 | */ |
444 | - public function unlock($haltOnError = true) |
|
445 | - { |
|
425 | + public function unlock($haltOnError = true) { |
|
446 | 426 | $this->connect(); |
447 | 427 | |
448 | 428 | $res = @mysqli_query($this->linkId, 'unlock tables'); |
@@ -459,8 +439,7 @@ discard block |
||
459 | 439 | * Db::affectedRows() |
460 | 440 | * @return int |
461 | 441 | */ |
462 | - public function affectedRows() |
|
463 | - { |
|
442 | + public function affectedRows() { |
|
464 | 443 | return @mysqli_affected_rows($this->linkId); |
465 | 444 | } |
466 | 445 | |
@@ -468,8 +447,7 @@ discard block |
||
468 | 447 | * Db::num_rows() |
469 | 448 | * @return int |
470 | 449 | */ |
471 | - public function num_rows() |
|
472 | - { |
|
450 | + public function num_rows() { |
|
473 | 451 | return @mysqli_num_rows($this->queryId); |
474 | 452 | } |
475 | 453 | |
@@ -477,8 +455,7 @@ discard block |
||
477 | 455 | * Db::num_fields() |
478 | 456 | * @return int |
479 | 457 | */ |
480 | - public function num_fields() |
|
481 | - { |
|
458 | + public function num_fields() { |
|
482 | 459 | return @mysqli_num_fields($this->queryId); |
483 | 460 | } |
484 | 461 | |
@@ -487,8 +464,7 @@ discard block |
||
487 | 464 | * |
488 | 465 | * @return array |
489 | 466 | */ |
490 | - public function tableNames() |
|
491 | - { |
|
467 | + public function tableNames() { |
|
492 | 468 | $return = []; |
493 | 469 | $this->query('SHOW TABLES'); |
494 | 470 | $i = 0; |
@@ -88,7 +88,7 @@ |
||
88 | 88 | //error_log("real_connect($host, $user, $password, $database, $port)"); |
89 | 89 | $this->linkId = mysqli_init(); |
90 | 90 | $this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
91 | - if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : NULL)) { |
|
91 | + if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : null)) { |
|
92 | 92 | $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
93 | 93 | return 0; |
94 | 94 | } |
@@ -19,503 +19,503 @@ |
||
19 | 19 | */ |
20 | 20 | class Db extends Generic implements Db_Interface |
21 | 21 | { |
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - public $type = 'mysqli'; |
|
26 | - |
|
27 | - /** |
|
28 | - * alias function of select_db, changes the database we are working with. |
|
29 | - * |
|
30 | - * @param string $database the name of the database to use |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - public function useDb($database) |
|
34 | - { |
|
35 | - $this->selectDb($database); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * changes the database we are working with. |
|
40 | - * |
|
41 | - * @param string $database the name of the database to use |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function selectDb($database) |
|
45 | - { |
|
46 | - $this->connect(); |
|
47 | - mysqli_select_db($this->linkId, $database); |
|
48 | - } |
|
49 | - |
|
50 | - /* public: connection management */ |
|
51 | - |
|
52 | - /** |
|
53 | - * Db::connect() |
|
54 | - * @param string $database |
|
55 | - * @param string $host |
|
56 | - * @param string $user |
|
57 | - * @param string $password |
|
58 | - * @return int|\mysqli |
|
59 | - */ |
|
60 | - public function connect($database = '', $host = '', $user = '', $password = '', $port = '') |
|
61 | - { |
|
62 | - /* Handle defaults */ |
|
63 | - if ($database == '') { |
|
64 | - $database = $this->database; |
|
65 | - } |
|
66 | - if ($host == '') { |
|
67 | - $host = $this->host; |
|
68 | - } |
|
69 | - if ($user == '') { |
|
70 | - $user = $this->user; |
|
71 | - } |
|
72 | - if ($password == '') { |
|
73 | - $password = $this->password; |
|
74 | - } |
|
75 | - if ($port == '') { |
|
76 | - $port = $this->port; |
|
77 | - } |
|
78 | - /* establish connection, select database */ |
|
79 | - if (!is_object($this->linkId)) { |
|
80 | - $this->connectionAttempt++; |
|
81 | - if ($this->connectionAttempt >= $this->maxConnectErrors - 1) { |
|
82 | - error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}"); |
|
83 | - } |
|
84 | - if ($this->connectionAttempt >= $this->maxConnectErrors) { |
|
85 | - $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
86 | - return 0; |
|
87 | - } |
|
88 | - //error_log("real_connect($host, $user, $password, $database, $port)"); |
|
89 | - $this->linkId = mysqli_init(); |
|
90 | - $this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
|
91 | - if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : NULL)) { |
|
92 | - $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
93 | - return 0; |
|
94 | - } |
|
95 | - $this->linkId->set_charset($this->characterSet); |
|
96 | - if ($this->linkId->connect_errno) { |
|
97 | - $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
98 | - return 0; |
|
99 | - } |
|
100 | - } |
|
101 | - return $this->linkId; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Db::disconnect() |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - public function disconnect() |
|
109 | - { |
|
110 | - $return = !is_int($this->linkId) && method_exists($this->linkId, 'close') ? $this->linkId->close() : false; |
|
111 | - $this->linkId = 0; |
|
112 | - return $return; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @param $string |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - public function real_escape($string = '') |
|
120 | - { |
|
121 | - if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
122 | - return $this->escape($string); |
|
123 | - } |
|
124 | - return mysqli_real_escape_string($this->linkId, $string); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * discard the query result |
|
129 | - * @return void |
|
130 | - */ |
|
131 | - public function free() |
|
132 | - { |
|
133 | - if (is_resource($this->queryId)) { |
|
134 | - @mysqli_free_result($this->queryId); |
|
135 | - } |
|
136 | - $this->queryId = 0; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Db::queryReturn() |
|
141 | - * |
|
142 | - * Sends an SQL query to the server like the normal query() command but iterates through |
|
143 | - * any rows and returns the row or rows immediately or FALSE on error |
|
144 | - * |
|
145 | - * @param mixed $query SQL Query to be used |
|
146 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
147 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
148 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
149 | - */ |
|
150 | - public function queryReturn($query, $line = '', $file = '') |
|
151 | - { |
|
152 | - $this->query($query, $line, $file); |
|
153 | - if ($this->num_rows() == 0) { |
|
154 | - return false; |
|
155 | - } elseif ($this->num_rows() == 1) { |
|
156 | - $this->next_record(MYSQLI_ASSOC); |
|
157 | - return $this->Record; |
|
158 | - } else { |
|
159 | - $out = []; |
|
160 | - while ($this->next_record(MYSQLI_ASSOC)) { |
|
161 | - $out[] = $this->Record; |
|
162 | - } |
|
163 | - return $out; |
|
164 | - } |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * db:qr() |
|
169 | - * |
|
170 | - * alias of queryReturn() |
|
171 | - * |
|
172 | - * @param mixed $query SQL Query to be used |
|
173 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
174 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
175 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
176 | - */ |
|
177 | - public function qr($query, $line = '', $file = '') |
|
178 | - { |
|
179 | - return $this->queryReturn($query, $line, $file); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * creates a prepaired statement from query |
|
184 | - * |
|
185 | - * @param string $query sql query like INSERT INTO table (col) VALUES (?) or SELECT * from table WHERE col1 = ? and col2 = ? or UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? |
|
186 | - * @return int|\MyDb\Mysqli\mysqli_stmt |
|
187 | - * @param string $line |
|
188 | - * @param string $file |
|
189 | - */ |
|
190 | - public function prepare($query, $line = '', $file = '') |
|
191 | - { |
|
192 | - if (!$this->connect()) { |
|
193 | - return 0; |
|
194 | - } |
|
195 | - $haltPrev = $this->haltOnError; |
|
196 | - $this->haltOnError = 'no'; |
|
197 | - $start = microtime(true); |
|
198 | - $prepare = mysqli_prepare($this->linkId, $query); |
|
199 | - if (!isset($GLOBALS['disable_db_queries'])) { |
|
200 | - $this->addLog($query, microtime(true) - $start, $line, $file); |
|
201 | - } |
|
202 | - return $prepare; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Db::query() |
|
207 | - * |
|
208 | - * Sends an SQL query to the database |
|
209 | - * |
|
210 | - * @param mixed $queryString |
|
211 | - * @param string $line |
|
212 | - * @param string $file |
|
213 | - * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
214 | - */ |
|
215 | - public function query($queryString, $line = '', $file = '') |
|
216 | - { |
|
217 | - /* No empty queries, please, since PHP4 chokes on them. */ |
|
218 | - /* The empty query string is passed on from the constructor, |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + public $type = 'mysqli'; |
|
26 | + |
|
27 | + /** |
|
28 | + * alias function of select_db, changes the database we are working with. |
|
29 | + * |
|
30 | + * @param string $database the name of the database to use |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + public function useDb($database) |
|
34 | + { |
|
35 | + $this->selectDb($database); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * changes the database we are working with. |
|
40 | + * |
|
41 | + * @param string $database the name of the database to use |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function selectDb($database) |
|
45 | + { |
|
46 | + $this->connect(); |
|
47 | + mysqli_select_db($this->linkId, $database); |
|
48 | + } |
|
49 | + |
|
50 | + /* public: connection management */ |
|
51 | + |
|
52 | + /** |
|
53 | + * Db::connect() |
|
54 | + * @param string $database |
|
55 | + * @param string $host |
|
56 | + * @param string $user |
|
57 | + * @param string $password |
|
58 | + * @return int|\mysqli |
|
59 | + */ |
|
60 | + public function connect($database = '', $host = '', $user = '', $password = '', $port = '') |
|
61 | + { |
|
62 | + /* Handle defaults */ |
|
63 | + if ($database == '') { |
|
64 | + $database = $this->database; |
|
65 | + } |
|
66 | + if ($host == '') { |
|
67 | + $host = $this->host; |
|
68 | + } |
|
69 | + if ($user == '') { |
|
70 | + $user = $this->user; |
|
71 | + } |
|
72 | + if ($password == '') { |
|
73 | + $password = $this->password; |
|
74 | + } |
|
75 | + if ($port == '') { |
|
76 | + $port = $this->port; |
|
77 | + } |
|
78 | + /* establish connection, select database */ |
|
79 | + if (!is_object($this->linkId)) { |
|
80 | + $this->connectionAttempt++; |
|
81 | + if ($this->connectionAttempt >= $this->maxConnectErrors - 1) { |
|
82 | + error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}"); |
|
83 | + } |
|
84 | + if ($this->connectionAttempt >= $this->maxConnectErrors) { |
|
85 | + $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
86 | + return 0; |
|
87 | + } |
|
88 | + //error_log("real_connect($host, $user, $password, $database, $port)"); |
|
89 | + $this->linkId = mysqli_init(); |
|
90 | + $this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
|
91 | + if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : NULL)) { |
|
92 | + $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
93 | + return 0; |
|
94 | + } |
|
95 | + $this->linkId->set_charset($this->characterSet); |
|
96 | + if ($this->linkId->connect_errno) { |
|
97 | + $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
98 | + return 0; |
|
99 | + } |
|
100 | + } |
|
101 | + return $this->linkId; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Db::disconnect() |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + public function disconnect() |
|
109 | + { |
|
110 | + $return = !is_int($this->linkId) && method_exists($this->linkId, 'close') ? $this->linkId->close() : false; |
|
111 | + $this->linkId = 0; |
|
112 | + return $return; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @param $string |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + public function real_escape($string = '') |
|
120 | + { |
|
121 | + if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
122 | + return $this->escape($string); |
|
123 | + } |
|
124 | + return mysqli_real_escape_string($this->linkId, $string); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * discard the query result |
|
129 | + * @return void |
|
130 | + */ |
|
131 | + public function free() |
|
132 | + { |
|
133 | + if (is_resource($this->queryId)) { |
|
134 | + @mysqli_free_result($this->queryId); |
|
135 | + } |
|
136 | + $this->queryId = 0; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Db::queryReturn() |
|
141 | + * |
|
142 | + * Sends an SQL query to the server like the normal query() command but iterates through |
|
143 | + * any rows and returns the row or rows immediately or FALSE on error |
|
144 | + * |
|
145 | + * @param mixed $query SQL Query to be used |
|
146 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
147 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
148 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
149 | + */ |
|
150 | + public function queryReturn($query, $line = '', $file = '') |
|
151 | + { |
|
152 | + $this->query($query, $line, $file); |
|
153 | + if ($this->num_rows() == 0) { |
|
154 | + return false; |
|
155 | + } elseif ($this->num_rows() == 1) { |
|
156 | + $this->next_record(MYSQLI_ASSOC); |
|
157 | + return $this->Record; |
|
158 | + } else { |
|
159 | + $out = []; |
|
160 | + while ($this->next_record(MYSQLI_ASSOC)) { |
|
161 | + $out[] = $this->Record; |
|
162 | + } |
|
163 | + return $out; |
|
164 | + } |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * db:qr() |
|
169 | + * |
|
170 | + * alias of queryReturn() |
|
171 | + * |
|
172 | + * @param mixed $query SQL Query to be used |
|
173 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
174 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
175 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
176 | + */ |
|
177 | + public function qr($query, $line = '', $file = '') |
|
178 | + { |
|
179 | + return $this->queryReturn($query, $line, $file); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * creates a prepaired statement from query |
|
184 | + * |
|
185 | + * @param string $query sql query like INSERT INTO table (col) VALUES (?) or SELECT * from table WHERE col1 = ? and col2 = ? or UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? |
|
186 | + * @return int|\MyDb\Mysqli\mysqli_stmt |
|
187 | + * @param string $line |
|
188 | + * @param string $file |
|
189 | + */ |
|
190 | + public function prepare($query, $line = '', $file = '') |
|
191 | + { |
|
192 | + if (!$this->connect()) { |
|
193 | + return 0; |
|
194 | + } |
|
195 | + $haltPrev = $this->haltOnError; |
|
196 | + $this->haltOnError = 'no'; |
|
197 | + $start = microtime(true); |
|
198 | + $prepare = mysqli_prepare($this->linkId, $query); |
|
199 | + if (!isset($GLOBALS['disable_db_queries'])) { |
|
200 | + $this->addLog($query, microtime(true) - $start, $line, $file); |
|
201 | + } |
|
202 | + return $prepare; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Db::query() |
|
207 | + * |
|
208 | + * Sends an SQL query to the database |
|
209 | + * |
|
210 | + * @param mixed $queryString |
|
211 | + * @param string $line |
|
212 | + * @param string $file |
|
213 | + * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
214 | + */ |
|
215 | + public function query($queryString, $line = '', $file = '') |
|
216 | + { |
|
217 | + /* No empty queries, please, since PHP4 chokes on them. */ |
|
218 | + /* The empty query string is passed on from the constructor, |
|
219 | 219 | * when calling the class without a query, e.g. in situations |
220 | 220 | * like these: '$db = new db_Subclass;' |
221 | 221 | */ |
222 | - if ($queryString == '') { |
|
223 | - return 0; |
|
224 | - } |
|
225 | - if (!$this->connect()) { |
|
226 | - return 0; |
|
227 | - /* we already complained in connect() about that. */ |
|
228 | - } |
|
229 | - $haltPrev = $this->haltOnError; |
|
230 | - $this->haltOnError = 'no'; |
|
231 | - // New query, discard previous result. |
|
232 | - if (is_resource($this->queryId)) { |
|
233 | - $this->free(); |
|
234 | - } |
|
235 | - if ($this->Debug) { |
|
236 | - printf("Debug: query = %s<br>\n", $queryString); |
|
237 | - } |
|
238 | - if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
239 | - $this->log($queryString, $line, $file); |
|
240 | - } |
|
241 | - $tries = 2; |
|
242 | - $try = 0; |
|
243 | - $this->queryId = false; |
|
244 | - while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) { |
|
245 | - $try++; |
|
246 | - if ($try > 1) { |
|
247 | - @mysqli_close($this->linkId); |
|
248 | - $this->linkId = 0; |
|
249 | - } |
|
250 | - $start = microtime(true); |
|
251 | - $onlyRollback = true; |
|
252 | - $fails = -1; |
|
253 | - while ($fails < 30 && (null === $this->queryId || $this->queryId === false)) { |
|
254 | - $this->connect(); |
|
255 | - $fails++; |
|
256 | - try { |
|
257 | - $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
|
258 | - if (in_array((int)@mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
259 | - //error_log("got ".@mysqli_errno($this->linkId)." sql error fails {$fails} on query {$queryString} from {$line}:{$file}"); |
|
260 | - usleep(250000); // 0.25 second |
|
261 | - } else { |
|
262 | - $onlyRollback = false; |
|
263 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
264 | - $tries = 0; |
|
265 | - } |
|
266 | - break; |
|
267 | - } |
|
268 | - } catch (\mysqli_sql_exception $e) { |
|
269 | - if (in_array((int)$e->getCode(), [1213, 2006, 3101, 1180])) { |
|
270 | - //error_log("got ".$e->getCode()." sql error fails {$fails}"); |
|
271 | - usleep(250000); // 0.25 second |
|
272 | - } else { |
|
273 | - error_log('Got mysqli_sql_exception code '.$e->getCode().' error '.$e->getMessage().' on query '.$queryString.' from '.$line.':'.$file); |
|
274 | - $onlyRollback = false; |
|
275 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
276 | - $tries = 0; |
|
277 | - } |
|
278 | - break; |
|
279 | - } |
|
280 | - } |
|
281 | - } |
|
282 | - if (!isset($GLOBALS['disable_db_queries'])) { |
|
283 | - $this->addLog($queryString, microtime(true) - $start, $line, $file); |
|
284 | - } |
|
285 | - $this->Row = 0; |
|
286 | - $this->Errno = @mysqli_errno($this->linkId); |
|
287 | - $this->Error = @mysqli_error($this->linkId); |
|
288 | - if ($try == 1 && (null === $this->queryId || $this->queryId === false)) { |
|
289 | - //$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
290 | - } |
|
291 | - } |
|
292 | - $this->haltOnError = $haltPrev; |
|
293 | - if ($onlyRollback === true && false === $this->queryId) { |
|
294 | - error_log('Got MySQLi 3101 Rollback Error '.$fails.' Times, Giving Up on '.$queryString.' from '.$line.':'.$file.' on '.__LINE__.':'.__FILE__); |
|
295 | - } |
|
296 | - if (null === $this->queryId || $this->queryId === false) { |
|
297 | - $this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
298 | - $this->halt('', $line, $file); |
|
299 | - } |
|
300 | - |
|
301 | - // Will return nada if it fails. That's fine. |
|
302 | - return $this->queryId; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * @return array|null|object |
|
307 | - */ |
|
308 | - public function fetchObject() |
|
309 | - { |
|
310 | - $this->Record = @mysqli_fetch_object($this->queryId); |
|
311 | - return $this->Record; |
|
312 | - } |
|
313 | - |
|
314 | - /* public: walk result set */ |
|
315 | - |
|
316 | - /** |
|
317 | - * Db::next_record() |
|
318 | - * |
|
319 | - * @param mixed $resultType |
|
320 | - * @return bool |
|
321 | - */ |
|
322 | - public function next_record($resultType = MYSQLI_BOTH) |
|
323 | - { |
|
324 | - if ($this->queryId === false) { |
|
325 | - $this->haltmsg('next_record called with no query pending.'); |
|
326 | - return 0; |
|
327 | - } |
|
328 | - |
|
329 | - $this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
330 | - ++$this->Row; |
|
331 | - $this->Errno = mysqli_errno($this->linkId); |
|
332 | - $this->Error = mysqli_error($this->linkId); |
|
333 | - |
|
334 | - $stat = is_array($this->Record); |
|
335 | - if (!$stat && $this->autoFree && is_resource($this->queryId)) { |
|
336 | - $this->free(); |
|
337 | - } |
|
338 | - return $stat; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * switch to position in result set |
|
343 | - * |
|
344 | - * @param integer $pos the row numbe starting at 0 to switch to |
|
345 | - * @return bool whetherit was successfu or not. |
|
346 | - */ |
|
347 | - public function seek($pos = 0) |
|
348 | - { |
|
349 | - $status = @mysqli_data_seek($this->queryId, $pos); |
|
350 | - if ($status) { |
|
351 | - $this->Row = $pos; |
|
352 | - } else { |
|
353 | - $this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__); |
|
354 | - /* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */ |
|
355 | - $rows = $this->num_rows(); |
|
356 | - @mysqli_data_seek($this->queryId, $rows); |
|
357 | - $this->Row = $rows; |
|
358 | - return false; |
|
359 | - } |
|
360 | - return true; |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Initiates a transaction |
|
365 | - * |
|
366 | - * @return bool |
|
367 | - */ |
|
368 | - public function transactionBegin() |
|
369 | - { |
|
370 | - if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
|
371 | - return true; |
|
372 | - } |
|
373 | - if (!$this->connect()) { |
|
374 | - return 0; |
|
375 | - } |
|
376 | - return mysqli_begin_transaction($this->linkId); |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * Commits a transaction |
|
381 | - * |
|
382 | - * @return bool |
|
383 | - */ |
|
384 | - public function transactionCommit() |
|
385 | - { |
|
386 | - if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
387 | - return true; |
|
388 | - } |
|
389 | - return mysqli_commit($this->linkId); |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Rolls back a transaction |
|
394 | - * |
|
395 | - * @return bool |
|
396 | - */ |
|
397 | - public function transactionAbort() |
|
398 | - { |
|
399 | - if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
400 | - return true; |
|
401 | - } |
|
402 | - return mysqli_rollback($this->linkId); |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * This will get the last insert ID created on the current connection. Should only be called after an insert query is |
|
407 | - * run on a table that has an auto incrementing field. $table and $field are required, but unused here since it's |
|
408 | - * unnecessary for mysql. For compatibility with pgsql, the params must be supplied. |
|
409 | - * |
|
410 | - * @param string $table |
|
411 | - * @param string $field |
|
412 | - * @return int|string |
|
413 | - */ |
|
414 | - public function getLastInsertId($table, $field) |
|
415 | - { |
|
416 | - if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
417 | - return -1; |
|
418 | - } |
|
419 | - |
|
420 | - return @mysqli_insert_id($this->linkId); |
|
421 | - } |
|
422 | - |
|
423 | - /* public: table locking */ |
|
424 | - |
|
425 | - /** |
|
426 | - * Db::lock() |
|
427 | - * @param mixed $table |
|
428 | - * @param string $mode |
|
429 | - * @return bool|int|\mysqli_result |
|
430 | - */ |
|
431 | - public function lock($table, $mode = 'write') |
|
432 | - { |
|
433 | - $this->connect(); |
|
434 | - $query = 'lock tables '; |
|
435 | - if (is_array($table)) { |
|
436 | - foreach ($table as $key => $value) { |
|
437 | - if ($key == 'read' && $key != 0) { |
|
438 | - $query .= "$value read, "; |
|
439 | - } else { |
|
440 | - $query .= "$value $mode, "; |
|
441 | - } |
|
442 | - } |
|
443 | - $query = mb_substr($query, 0, -2); |
|
444 | - } else { |
|
445 | - $query .= "$table $mode"; |
|
446 | - } |
|
447 | - $res = @mysqli_query($this->linkId, $query); |
|
448 | - if (!$res) { |
|
449 | - $this->halt("lock($table, $mode) failed."); |
|
450 | - return 0; |
|
451 | - } |
|
452 | - return $res; |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Db::unlock() |
|
457 | - * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
|
458 | - * @return bool|int|\mysqli_result |
|
459 | - */ |
|
460 | - public function unlock($haltOnError = true) |
|
461 | - { |
|
462 | - $this->connect(); |
|
463 | - |
|
464 | - $res = @mysqli_query($this->linkId, 'unlock tables'); |
|
465 | - if ($haltOnError === true && !$res) { |
|
466 | - $this->halt('unlock() failed.'); |
|
467 | - return 0; |
|
468 | - } |
|
469 | - return $res; |
|
470 | - } |
|
471 | - |
|
472 | - /* public: evaluate the result (size, width) */ |
|
473 | - |
|
474 | - /** |
|
475 | - * Db::affectedRows() |
|
476 | - * @return int |
|
477 | - */ |
|
478 | - public function affectedRows() |
|
479 | - { |
|
480 | - return @mysqli_affected_rows($this->linkId); |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Db::num_rows() |
|
485 | - * @return int |
|
486 | - */ |
|
487 | - public function num_rows() |
|
488 | - { |
|
489 | - return @mysqli_num_rows($this->queryId); |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Db::num_fields() |
|
494 | - * @return int |
|
495 | - */ |
|
496 | - public function num_fields() |
|
497 | - { |
|
498 | - return @mysqli_num_fields($this->queryId); |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * gets an array of the table names in teh current datase |
|
503 | - * |
|
504 | - * @return array |
|
505 | - */ |
|
506 | - public function tableNames() |
|
507 | - { |
|
508 | - $return = []; |
|
509 | - $this->query('SHOW TABLES'); |
|
510 | - $i = 0; |
|
511 | - while ($info = $this->queryId->fetch_row()) { |
|
512 | - $return[$i]['table_name'] = $info[0]; |
|
513 | - $return[$i]['tablespace_name'] = $this->database; |
|
514 | - $return[$i]['database'] = $this->database; |
|
515 | - ++$i; |
|
516 | - } |
|
517 | - return $return; |
|
518 | - } |
|
222 | + if ($queryString == '') { |
|
223 | + return 0; |
|
224 | + } |
|
225 | + if (!$this->connect()) { |
|
226 | + return 0; |
|
227 | + /* we already complained in connect() about that. */ |
|
228 | + } |
|
229 | + $haltPrev = $this->haltOnError; |
|
230 | + $this->haltOnError = 'no'; |
|
231 | + // New query, discard previous result. |
|
232 | + if (is_resource($this->queryId)) { |
|
233 | + $this->free(); |
|
234 | + } |
|
235 | + if ($this->Debug) { |
|
236 | + printf("Debug: query = %s<br>\n", $queryString); |
|
237 | + } |
|
238 | + if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
239 | + $this->log($queryString, $line, $file); |
|
240 | + } |
|
241 | + $tries = 2; |
|
242 | + $try = 0; |
|
243 | + $this->queryId = false; |
|
244 | + while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) { |
|
245 | + $try++; |
|
246 | + if ($try > 1) { |
|
247 | + @mysqli_close($this->linkId); |
|
248 | + $this->linkId = 0; |
|
249 | + } |
|
250 | + $start = microtime(true); |
|
251 | + $onlyRollback = true; |
|
252 | + $fails = -1; |
|
253 | + while ($fails < 30 && (null === $this->queryId || $this->queryId === false)) { |
|
254 | + $this->connect(); |
|
255 | + $fails++; |
|
256 | + try { |
|
257 | + $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
|
258 | + if (in_array((int)@mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
259 | + //error_log("got ".@mysqli_errno($this->linkId)." sql error fails {$fails} on query {$queryString} from {$line}:{$file}"); |
|
260 | + usleep(250000); // 0.25 second |
|
261 | + } else { |
|
262 | + $onlyRollback = false; |
|
263 | + if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
264 | + $tries = 0; |
|
265 | + } |
|
266 | + break; |
|
267 | + } |
|
268 | + } catch (\mysqli_sql_exception $e) { |
|
269 | + if (in_array((int)$e->getCode(), [1213, 2006, 3101, 1180])) { |
|
270 | + //error_log("got ".$e->getCode()." sql error fails {$fails}"); |
|
271 | + usleep(250000); // 0.25 second |
|
272 | + } else { |
|
273 | + error_log('Got mysqli_sql_exception code '.$e->getCode().' error '.$e->getMessage().' on query '.$queryString.' from '.$line.':'.$file); |
|
274 | + $onlyRollback = false; |
|
275 | + if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
276 | + $tries = 0; |
|
277 | + } |
|
278 | + break; |
|
279 | + } |
|
280 | + } |
|
281 | + } |
|
282 | + if (!isset($GLOBALS['disable_db_queries'])) { |
|
283 | + $this->addLog($queryString, microtime(true) - $start, $line, $file); |
|
284 | + } |
|
285 | + $this->Row = 0; |
|
286 | + $this->Errno = @mysqli_errno($this->linkId); |
|
287 | + $this->Error = @mysqli_error($this->linkId); |
|
288 | + if ($try == 1 && (null === $this->queryId || $this->queryId === false)) { |
|
289 | + //$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
290 | + } |
|
291 | + } |
|
292 | + $this->haltOnError = $haltPrev; |
|
293 | + if ($onlyRollback === true && false === $this->queryId) { |
|
294 | + error_log('Got MySQLi 3101 Rollback Error '.$fails.' Times, Giving Up on '.$queryString.' from '.$line.':'.$file.' on '.__LINE__.':'.__FILE__); |
|
295 | + } |
|
296 | + if (null === $this->queryId || $this->queryId === false) { |
|
297 | + $this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
298 | + $this->halt('', $line, $file); |
|
299 | + } |
|
300 | + |
|
301 | + // Will return nada if it fails. That's fine. |
|
302 | + return $this->queryId; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * @return array|null|object |
|
307 | + */ |
|
308 | + public function fetchObject() |
|
309 | + { |
|
310 | + $this->Record = @mysqli_fetch_object($this->queryId); |
|
311 | + return $this->Record; |
|
312 | + } |
|
313 | + |
|
314 | + /* public: walk result set */ |
|
315 | + |
|
316 | + /** |
|
317 | + * Db::next_record() |
|
318 | + * |
|
319 | + * @param mixed $resultType |
|
320 | + * @return bool |
|
321 | + */ |
|
322 | + public function next_record($resultType = MYSQLI_BOTH) |
|
323 | + { |
|
324 | + if ($this->queryId === false) { |
|
325 | + $this->haltmsg('next_record called with no query pending.'); |
|
326 | + return 0; |
|
327 | + } |
|
328 | + |
|
329 | + $this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
330 | + ++$this->Row; |
|
331 | + $this->Errno = mysqli_errno($this->linkId); |
|
332 | + $this->Error = mysqli_error($this->linkId); |
|
333 | + |
|
334 | + $stat = is_array($this->Record); |
|
335 | + if (!$stat && $this->autoFree && is_resource($this->queryId)) { |
|
336 | + $this->free(); |
|
337 | + } |
|
338 | + return $stat; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * switch to position in result set |
|
343 | + * |
|
344 | + * @param integer $pos the row numbe starting at 0 to switch to |
|
345 | + * @return bool whetherit was successfu or not. |
|
346 | + */ |
|
347 | + public function seek($pos = 0) |
|
348 | + { |
|
349 | + $status = @mysqli_data_seek($this->queryId, $pos); |
|
350 | + if ($status) { |
|
351 | + $this->Row = $pos; |
|
352 | + } else { |
|
353 | + $this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__); |
|
354 | + /* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */ |
|
355 | + $rows = $this->num_rows(); |
|
356 | + @mysqli_data_seek($this->queryId, $rows); |
|
357 | + $this->Row = $rows; |
|
358 | + return false; |
|
359 | + } |
|
360 | + return true; |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Initiates a transaction |
|
365 | + * |
|
366 | + * @return bool |
|
367 | + */ |
|
368 | + public function transactionBegin() |
|
369 | + { |
|
370 | + if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
|
371 | + return true; |
|
372 | + } |
|
373 | + if (!$this->connect()) { |
|
374 | + return 0; |
|
375 | + } |
|
376 | + return mysqli_begin_transaction($this->linkId); |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * Commits a transaction |
|
381 | + * |
|
382 | + * @return bool |
|
383 | + */ |
|
384 | + public function transactionCommit() |
|
385 | + { |
|
386 | + if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
387 | + return true; |
|
388 | + } |
|
389 | + return mysqli_commit($this->linkId); |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Rolls back a transaction |
|
394 | + * |
|
395 | + * @return bool |
|
396 | + */ |
|
397 | + public function transactionAbort() |
|
398 | + { |
|
399 | + if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
400 | + return true; |
|
401 | + } |
|
402 | + return mysqli_rollback($this->linkId); |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * This will get the last insert ID created on the current connection. Should only be called after an insert query is |
|
407 | + * run on a table that has an auto incrementing field. $table and $field are required, but unused here since it's |
|
408 | + * unnecessary for mysql. For compatibility with pgsql, the params must be supplied. |
|
409 | + * |
|
410 | + * @param string $table |
|
411 | + * @param string $field |
|
412 | + * @return int|string |
|
413 | + */ |
|
414 | + public function getLastInsertId($table, $field) |
|
415 | + { |
|
416 | + if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
417 | + return -1; |
|
418 | + } |
|
419 | + |
|
420 | + return @mysqli_insert_id($this->linkId); |
|
421 | + } |
|
422 | + |
|
423 | + /* public: table locking */ |
|
424 | + |
|
425 | + /** |
|
426 | + * Db::lock() |
|
427 | + * @param mixed $table |
|
428 | + * @param string $mode |
|
429 | + * @return bool|int|\mysqli_result |
|
430 | + */ |
|
431 | + public function lock($table, $mode = 'write') |
|
432 | + { |
|
433 | + $this->connect(); |
|
434 | + $query = 'lock tables '; |
|
435 | + if (is_array($table)) { |
|
436 | + foreach ($table as $key => $value) { |
|
437 | + if ($key == 'read' && $key != 0) { |
|
438 | + $query .= "$value read, "; |
|
439 | + } else { |
|
440 | + $query .= "$value $mode, "; |
|
441 | + } |
|
442 | + } |
|
443 | + $query = mb_substr($query, 0, -2); |
|
444 | + } else { |
|
445 | + $query .= "$table $mode"; |
|
446 | + } |
|
447 | + $res = @mysqli_query($this->linkId, $query); |
|
448 | + if (!$res) { |
|
449 | + $this->halt("lock($table, $mode) failed."); |
|
450 | + return 0; |
|
451 | + } |
|
452 | + return $res; |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Db::unlock() |
|
457 | + * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
|
458 | + * @return bool|int|\mysqli_result |
|
459 | + */ |
|
460 | + public function unlock($haltOnError = true) |
|
461 | + { |
|
462 | + $this->connect(); |
|
463 | + |
|
464 | + $res = @mysqli_query($this->linkId, 'unlock tables'); |
|
465 | + if ($haltOnError === true && !$res) { |
|
466 | + $this->halt('unlock() failed.'); |
|
467 | + return 0; |
|
468 | + } |
|
469 | + return $res; |
|
470 | + } |
|
471 | + |
|
472 | + /* public: evaluate the result (size, width) */ |
|
473 | + |
|
474 | + /** |
|
475 | + * Db::affectedRows() |
|
476 | + * @return int |
|
477 | + */ |
|
478 | + public function affectedRows() |
|
479 | + { |
|
480 | + return @mysqli_affected_rows($this->linkId); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Db::num_rows() |
|
485 | + * @return int |
|
486 | + */ |
|
487 | + public function num_rows() |
|
488 | + { |
|
489 | + return @mysqli_num_rows($this->queryId); |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Db::num_fields() |
|
494 | + * @return int |
|
495 | + */ |
|
496 | + public function num_fields() |
|
497 | + { |
|
498 | + return @mysqli_num_fields($this->queryId); |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * gets an array of the table names in teh current datase |
|
503 | + * |
|
504 | + * @return array |
|
505 | + */ |
|
506 | + public function tableNames() |
|
507 | + { |
|
508 | + $return = []; |
|
509 | + $this->query('SHOW TABLES'); |
|
510 | + $i = 0; |
|
511 | + while ($info = $this->queryId->fetch_row()) { |
|
512 | + $return[$i]['table_name'] = $info[0]; |
|
513 | + $return[$i]['tablespace_name'] = $this->database; |
|
514 | + $return[$i]['database'] = $this->database; |
|
515 | + ++$i; |
|
516 | + } |
|
517 | + return $return; |
|
518 | + } |
|
519 | 519 | } |
520 | 520 | |
521 | 521 | /** |
@@ -255,24 +255,24 @@ |
||
255 | 255 | $fails++; |
256 | 256 | try { |
257 | 257 | $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
258 | - if (in_array((int)@mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
258 | + if (in_array((int) @mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
259 | 259 | //error_log("got ".@mysqli_errno($this->linkId)." sql error fails {$fails} on query {$queryString} from {$line}:{$file}"); |
260 | 260 | usleep(250000); // 0.25 second |
261 | 261 | } else { |
262 | 262 | $onlyRollback = false; |
263 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
263 | + if (in_array((int) @mysqli_errno($this->linkId), [1064])) { |
|
264 | 264 | $tries = 0; |
265 | 265 | } |
266 | 266 | break; |
267 | 267 | } |
268 | 268 | } catch (\mysqli_sql_exception $e) { |
269 | - if (in_array((int)$e->getCode(), [1213, 2006, 3101, 1180])) { |
|
269 | + if (in_array((int) $e->getCode(), [1213, 2006, 3101, 1180])) { |
|
270 | 270 | //error_log("got ".$e->getCode()." sql error fails {$fails}"); |
271 | 271 | usleep(250000); // 0.25 second |
272 | 272 | } else { |
273 | 273 | error_log('Got mysqli_sql_exception code '.$e->getCode().' error '.$e->getMessage().' on query '.$queryString.' from '.$line.':'.$file); |
274 | 274 | $onlyRollback = false; |
275 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
275 | + if (in_array((int) @mysqli_errno($this->linkId), [1064])) { |
|
276 | 276 | $tries = 0; |
277 | 277 | } |
278 | 278 | break; |
@@ -16,57 +16,57 @@ |
||
16 | 16 | */ |
17 | 17 | interface Db_Interface |
18 | 18 | { |
19 | - /** |
|
20 | - * Db_Interface constructor. |
|
21 | - * |
|
22 | - * @param string $database |
|
23 | - * @param string $user |
|
24 | - * @param string $password |
|
25 | - * @param string $host |
|
26 | - * @param string $query |
|
27 | - * @param string $port |
|
28 | - */ |
|
29 | - public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = ''); |
|
19 | + /** |
|
20 | + * Db_Interface constructor. |
|
21 | + * |
|
22 | + * @param string $database |
|
23 | + * @param string $user |
|
24 | + * @param string $password |
|
25 | + * @param string $host |
|
26 | + * @param string $query |
|
27 | + * @param string $port |
|
28 | + */ |
|
29 | + public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = ''); |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param $message |
|
33 | - * @param string $line |
|
34 | - * @param string $file |
|
35 | - * @return mixed |
|
36 | - */ |
|
37 | - public function log($message, $line = '', $file = ''); |
|
31 | + /** |
|
32 | + * @param $message |
|
33 | + * @param string $line |
|
34 | + * @param string $file |
|
35 | + * @return mixed |
|
36 | + */ |
|
37 | + public function log($message, $line = '', $file = ''); |
|
38 | 38 | |
39 | - public function linkId(); |
|
39 | + public function linkId(); |
|
40 | 40 | |
41 | - public function queryId(); |
|
41 | + public function queryId(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param $str |
|
45 | - * @return mixed |
|
46 | - */ |
|
47 | - public function dbAddslashes($str); |
|
43 | + /** |
|
44 | + * @param $str |
|
45 | + * @return mixed |
|
46 | + */ |
|
47 | + public function dbAddslashes($str); |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param $query |
|
51 | - * @param string $line |
|
52 | - * @param string $file |
|
53 | - * @return mixed |
|
54 | - */ |
|
55 | - public function qr($query, $line = '', $file = ''); |
|
49 | + /** |
|
50 | + * @param $query |
|
51 | + * @param string $line |
|
52 | + * @param string $file |
|
53 | + * @return mixed |
|
54 | + */ |
|
55 | + public function qr($query, $line = '', $file = ''); |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param $msg |
|
59 | - * @param string $line |
|
60 | - * @param string $file |
|
61 | - * @return mixed |
|
62 | - */ |
|
63 | - public function halt($msg, $line = '', $file = ''); |
|
57 | + /** |
|
58 | + * @param $msg |
|
59 | + * @param string $line |
|
60 | + * @param string $file |
|
61 | + * @return mixed |
|
62 | + */ |
|
63 | + public function halt($msg, $line = '', $file = ''); |
|
64 | 64 | |
65 | - /** |
|
66 | - * @param $msg |
|
67 | - * @return mixed |
|
68 | - */ |
|
69 | - public function haltmsg($msg); |
|
65 | + /** |
|
66 | + * @param $msg |
|
67 | + * @return mixed |
|
68 | + */ |
|
69 | + public function haltmsg($msg); |
|
70 | 70 | |
71 | - public function indexNames(); |
|
71 | + public function indexNames(); |
|
72 | 72 | } |
@@ -14,8 +14,7 @@ |
||
14 | 14 | * |
15 | 15 | * @package MyDb |
16 | 16 | */ |
17 | -interface Db_Interface |
|
18 | -{ |
|
17 | +interface Db_Interface { |
|
19 | 18 | /** |
20 | 19 | * Db_Interface constructor. |
21 | 20 | * |
@@ -19,258 +19,258 @@ discard block |
||
19 | 19 | */ |
20 | 20 | class Db extends Generic implements Db_Interface |
21 | 21 | { |
22 | - public $driver = 'mysql'; |
|
23 | - public $Rows = []; |
|
24 | - public $type = 'adodb'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Db::connect() |
|
28 | - * @param string $database |
|
29 | - * @param string $host |
|
30 | - * @param string $user |
|
31 | - * @param string $password |
|
32 | - * @param string $driver |
|
33 | - * @return bool|\the |
|
34 | - */ |
|
35 | - public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
36 | - { |
|
37 | - /* Handle defaults */ |
|
38 | - if ('' == $database) { |
|
39 | - $database = $this->database; |
|
40 | - } |
|
41 | - if ('' == $host) { |
|
42 | - $host = $this->host; |
|
43 | - } |
|
44 | - if ('' == $user) { |
|
45 | - $user = $this->user; |
|
46 | - } |
|
47 | - if ('' == $password) { |
|
48 | - $password = $this->password; |
|
49 | - } |
|
50 | - if ('' == $driver) { |
|
51 | - $driver = $this->driver; |
|
52 | - } |
|
53 | - /* establish connection, select database */ |
|
54 | - if ($this->linkId === false) { |
|
55 | - $this->linkId = NewADOConnection($driver); |
|
56 | - $this->linkId->Connect($host, $user, $password, $database); |
|
57 | - } |
|
58 | - return $this->linkId; |
|
59 | - } |
|
60 | - |
|
61 | - /* This only affects systems not using persistent connections */ |
|
62 | - |
|
63 | - /** |
|
64 | - * Db::disconnect() |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public function disconnect() |
|
68 | - { |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param $string |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function real_escape($string = '') |
|
76 | - { |
|
77 | - return escapeshellarg($string); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * discard the query result |
|
82 | - * @return void |
|
83 | - */ |
|
84 | - public function free() |
|
85 | - { |
|
86 | - // @mysql_free_result($this->queryId); |
|
87 | - // $this->queryId = 0; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Db::queryReturn() |
|
92 | - * |
|
93 | - * Sends an SQL query to the server like the normal query() command but iterates through |
|
94 | - * any rows and returns the row or rows immediately or FALSE on error |
|
95 | - * |
|
96 | - * @param mixed $query SQL Query to be used |
|
97 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
98 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
99 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
100 | - */ |
|
101 | - public function queryReturn($query, $line = '', $file = '') |
|
102 | - { |
|
103 | - $this->query($query, $line, $file); |
|
104 | - if ($this->num_rows() == 0) { |
|
105 | - return false; |
|
106 | - } elseif ($this->num_rows() == 1) { |
|
107 | - $this->next_record(MYSQL_ASSOC); |
|
108 | - return $this->Record; |
|
109 | - } else { |
|
110 | - $out = []; |
|
111 | - while ($this->next_record(MYSQL_ASSOC)) { |
|
112 | - $out[] = $this->Record; |
|
113 | - } |
|
114 | - return $out; |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * db:qr() |
|
120 | - * |
|
121 | - * alias of queryReturn() |
|
122 | - * |
|
123 | - * @param mixed $query SQL Query to be used |
|
124 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
125 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
126 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
127 | - */ |
|
128 | - public function qr($query, $line = '', $file = '') |
|
129 | - { |
|
130 | - return $this->queryReturn($query, $line, $file); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Db::query() |
|
135 | - * |
|
136 | - * Sends an SQL query to the database |
|
137 | - * |
|
138 | - * @param mixed $queryString |
|
139 | - * @param string $line |
|
140 | - * @param string $file |
|
141 | - * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
142 | - */ |
|
143 | - public function query($queryString, $line = '', $file = '') |
|
144 | - { |
|
145 | - /* No empty queries, please, since PHP4 chokes on them. */ |
|
146 | - /* The empty query string is passed on from the constructor, |
|
22 | + public $driver = 'mysql'; |
|
23 | + public $Rows = []; |
|
24 | + public $type = 'adodb'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Db::connect() |
|
28 | + * @param string $database |
|
29 | + * @param string $host |
|
30 | + * @param string $user |
|
31 | + * @param string $password |
|
32 | + * @param string $driver |
|
33 | + * @return bool|\the |
|
34 | + */ |
|
35 | + public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
36 | + { |
|
37 | + /* Handle defaults */ |
|
38 | + if ('' == $database) { |
|
39 | + $database = $this->database; |
|
40 | + } |
|
41 | + if ('' == $host) { |
|
42 | + $host = $this->host; |
|
43 | + } |
|
44 | + if ('' == $user) { |
|
45 | + $user = $this->user; |
|
46 | + } |
|
47 | + if ('' == $password) { |
|
48 | + $password = $this->password; |
|
49 | + } |
|
50 | + if ('' == $driver) { |
|
51 | + $driver = $this->driver; |
|
52 | + } |
|
53 | + /* establish connection, select database */ |
|
54 | + if ($this->linkId === false) { |
|
55 | + $this->linkId = NewADOConnection($driver); |
|
56 | + $this->linkId->Connect($host, $user, $password, $database); |
|
57 | + } |
|
58 | + return $this->linkId; |
|
59 | + } |
|
60 | + |
|
61 | + /* This only affects systems not using persistent connections */ |
|
62 | + |
|
63 | + /** |
|
64 | + * Db::disconnect() |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public function disconnect() |
|
68 | + { |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param $string |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function real_escape($string = '') |
|
76 | + { |
|
77 | + return escapeshellarg($string); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * discard the query result |
|
82 | + * @return void |
|
83 | + */ |
|
84 | + public function free() |
|
85 | + { |
|
86 | + // @mysql_free_result($this->queryId); |
|
87 | + // $this->queryId = 0; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Db::queryReturn() |
|
92 | + * |
|
93 | + * Sends an SQL query to the server like the normal query() command but iterates through |
|
94 | + * any rows and returns the row or rows immediately or FALSE on error |
|
95 | + * |
|
96 | + * @param mixed $query SQL Query to be used |
|
97 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
98 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
99 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
100 | + */ |
|
101 | + public function queryReturn($query, $line = '', $file = '') |
|
102 | + { |
|
103 | + $this->query($query, $line, $file); |
|
104 | + if ($this->num_rows() == 0) { |
|
105 | + return false; |
|
106 | + } elseif ($this->num_rows() == 1) { |
|
107 | + $this->next_record(MYSQL_ASSOC); |
|
108 | + return $this->Record; |
|
109 | + } else { |
|
110 | + $out = []; |
|
111 | + while ($this->next_record(MYSQL_ASSOC)) { |
|
112 | + $out[] = $this->Record; |
|
113 | + } |
|
114 | + return $out; |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * db:qr() |
|
120 | + * |
|
121 | + * alias of queryReturn() |
|
122 | + * |
|
123 | + * @param mixed $query SQL Query to be used |
|
124 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
125 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
126 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
127 | + */ |
|
128 | + public function qr($query, $line = '', $file = '') |
|
129 | + { |
|
130 | + return $this->queryReturn($query, $line, $file); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Db::query() |
|
135 | + * |
|
136 | + * Sends an SQL query to the database |
|
137 | + * |
|
138 | + * @param mixed $queryString |
|
139 | + * @param string $line |
|
140 | + * @param string $file |
|
141 | + * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
142 | + */ |
|
143 | + public function query($queryString, $line = '', $file = '') |
|
144 | + { |
|
145 | + /* No empty queries, please, since PHP4 chokes on them. */ |
|
146 | + /* The empty query string is passed on from the constructor, |
|
147 | 147 | * when calling the class without a query, e.g. in situations |
148 | 148 | * like these: '$db = new db_Subclass;' |
149 | 149 | */ |
150 | - if ($queryString == '') { |
|
151 | - return 0; |
|
152 | - } |
|
153 | - if (!$this->connect()) { |
|
154 | - return 0; |
|
155 | - /* we already complained in connect() about that. */ |
|
156 | - } |
|
157 | - |
|
158 | - // New query, discard previous result. |
|
159 | - if ($this->queryId !== false) { |
|
160 | - $this->free(); |
|
161 | - } |
|
162 | - |
|
163 | - if ($this->Debug) { |
|
164 | - printf("Debug: query = %s<br>\n", $queryString); |
|
165 | - } |
|
166 | - if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
167 | - $this->log($queryString, $line, $file); |
|
168 | - } |
|
169 | - |
|
170 | - try { |
|
171 | - $this->queryId = $this->linkId->Execute($queryString); |
|
172 | - } catch (exception $e) { |
|
173 | - $this->emailError($queryString, $e, $line, $file); |
|
174 | - } |
|
175 | - $this->log("ADOdb Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__); |
|
176 | - $this->Row = 0; |
|
177 | - |
|
178 | - // Will return nada if it fails. That's fine. |
|
179 | - return $this->queryId; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Db::next_record() |
|
184 | - * @param mixed $resultType |
|
185 | - * @return bool |
|
186 | - */ |
|
187 | - public function next_record($resultType = MYSQL_ASSOC) |
|
188 | - { |
|
189 | - if (!$this->queryId) { |
|
190 | - $this->halt('next_record called with no query pending.'); |
|
191 | - return 0; |
|
192 | - } |
|
193 | - ++$this->Row; |
|
194 | - $this->Record = $this->queryId->FetchRow(); |
|
195 | - $stat = is_array($this->Record); |
|
196 | - if (!$stat && $this->autoFree) { |
|
197 | - $this->free(); |
|
198 | - } |
|
199 | - return $stat; |
|
200 | - } |
|
201 | - |
|
202 | - /* public: position in result set */ |
|
203 | - |
|
204 | - /** |
|
205 | - * Db::seek() |
|
206 | - * @param integer $pos |
|
207 | - * @return int |
|
208 | - */ |
|
209 | - public function seek($pos = 0) |
|
210 | - { |
|
211 | - if (isset($this->Rows[$pos])) { |
|
212 | - $this->Row = $pos; |
|
213 | - } else { |
|
214 | - $this->halt("seek($pos) failed: result has ".count($this->Rows).' rows'); |
|
215 | - /* half assed attempt to save the day, |
|
150 | + if ($queryString == '') { |
|
151 | + return 0; |
|
152 | + } |
|
153 | + if (!$this->connect()) { |
|
154 | + return 0; |
|
155 | + /* we already complained in connect() about that. */ |
|
156 | + } |
|
157 | + |
|
158 | + // New query, discard previous result. |
|
159 | + if ($this->queryId !== false) { |
|
160 | + $this->free(); |
|
161 | + } |
|
162 | + |
|
163 | + if ($this->Debug) { |
|
164 | + printf("Debug: query = %s<br>\n", $queryString); |
|
165 | + } |
|
166 | + if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
167 | + $this->log($queryString, $line, $file); |
|
168 | + } |
|
169 | + |
|
170 | + try { |
|
171 | + $this->queryId = $this->linkId->Execute($queryString); |
|
172 | + } catch (exception $e) { |
|
173 | + $this->emailError($queryString, $e, $line, $file); |
|
174 | + } |
|
175 | + $this->log("ADOdb Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__); |
|
176 | + $this->Row = 0; |
|
177 | + |
|
178 | + // Will return nada if it fails. That's fine. |
|
179 | + return $this->queryId; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Db::next_record() |
|
184 | + * @param mixed $resultType |
|
185 | + * @return bool |
|
186 | + */ |
|
187 | + public function next_record($resultType = MYSQL_ASSOC) |
|
188 | + { |
|
189 | + if (!$this->queryId) { |
|
190 | + $this->halt('next_record called with no query pending.'); |
|
191 | + return 0; |
|
192 | + } |
|
193 | + ++$this->Row; |
|
194 | + $this->Record = $this->queryId->FetchRow(); |
|
195 | + $stat = is_array($this->Record); |
|
196 | + if (!$stat && $this->autoFree) { |
|
197 | + $this->free(); |
|
198 | + } |
|
199 | + return $stat; |
|
200 | + } |
|
201 | + |
|
202 | + /* public: position in result set */ |
|
203 | + |
|
204 | + /** |
|
205 | + * Db::seek() |
|
206 | + * @param integer $pos |
|
207 | + * @return int |
|
208 | + */ |
|
209 | + public function seek($pos = 0) |
|
210 | + { |
|
211 | + if (isset($this->Rows[$pos])) { |
|
212 | + $this->Row = $pos; |
|
213 | + } else { |
|
214 | + $this->halt("seek($pos) failed: result has ".count($this->Rows).' rows'); |
|
215 | + /* half assed attempt to save the day, |
|
216 | 216 | * but do not consider this documented or even |
217 | 217 | * desirable behaviour. |
218 | 218 | */ |
219 | - return 0; |
|
220 | - } |
|
221 | - return 1; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Db::transactionBegin() |
|
226 | - * @return bool |
|
227 | - */ |
|
228 | - public function transactionBegin() |
|
229 | - { |
|
230 | - return true; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Db::transactionCommit() |
|
235 | - * @return bool |
|
236 | - */ |
|
237 | - public function transactionCommit() |
|
238 | - { |
|
239 | - return true; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Db::transactionAbort() |
|
244 | - * @return bool |
|
245 | - */ |
|
246 | - public function transactionAbort() |
|
247 | - { |
|
248 | - return true; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Db::getLastInsertId() |
|
253 | - * |
|
254 | - * @param mixed $table |
|
255 | - * @param mixed $field |
|
256 | - * @return mixed |
|
257 | - */ |
|
258 | - public function getLastInsertId($table, $field) |
|
259 | - { |
|
260 | - return $this->linkId->Insert_ID($table, $field); |
|
261 | - } |
|
262 | - |
|
263 | - /* public: table locking */ |
|
264 | - |
|
265 | - /** |
|
266 | - * Db::lock() |
|
267 | - * @param mixed $table |
|
268 | - * @param string $mode |
|
269 | - * @return void |
|
270 | - */ |
|
271 | - public function lock($table, $mode = 'write') |
|
272 | - { |
|
273 | - /* $this->connect(); |
|
219 | + return 0; |
|
220 | + } |
|
221 | + return 1; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Db::transactionBegin() |
|
226 | + * @return bool |
|
227 | + */ |
|
228 | + public function transactionBegin() |
|
229 | + { |
|
230 | + return true; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Db::transactionCommit() |
|
235 | + * @return bool |
|
236 | + */ |
|
237 | + public function transactionCommit() |
|
238 | + { |
|
239 | + return true; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Db::transactionAbort() |
|
244 | + * @return bool |
|
245 | + */ |
|
246 | + public function transactionAbort() |
|
247 | + { |
|
248 | + return true; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Db::getLastInsertId() |
|
253 | + * |
|
254 | + * @param mixed $table |
|
255 | + * @param mixed $field |
|
256 | + * @return mixed |
|
257 | + */ |
|
258 | + public function getLastInsertId($table, $field) |
|
259 | + { |
|
260 | + return $this->linkId->Insert_ID($table, $field); |
|
261 | + } |
|
262 | + |
|
263 | + /* public: table locking */ |
|
264 | + |
|
265 | + /** |
|
266 | + * Db::lock() |
|
267 | + * @param mixed $table |
|
268 | + * @param string $mode |
|
269 | + * @return void |
|
270 | + */ |
|
271 | + public function lock($table, $mode = 'write') |
|
272 | + { |
|
273 | + /* $this->connect(); |
|
274 | 274 | |
275 | 275 | * $query = "lock tables "; |
276 | 276 | * if (is_array($table)) |
@@ -300,15 +300,15 @@ discard block |
||
300 | 300 | * } |
301 | 301 | * return $res; |
302 | 302 | */ |
303 | - } |
|
303 | + } |
|
304 | 304 | |
305 | - /** |
|
306 | - * Db::unlock() |
|
307 | - * @return void |
|
308 | - */ |
|
309 | - public function unlock() |
|
310 | - { |
|
311 | - /* $this->connect(); |
|
305 | + /** |
|
306 | + * Db::unlock() |
|
307 | + * @return void |
|
308 | + */ |
|
309 | + public function unlock() |
|
310 | + { |
|
311 | + /* $this->connect(); |
|
312 | 312 | |
313 | 313 | * $res = @mysql_query("unlock tables"); |
314 | 314 | * if (!$res) |
@@ -318,72 +318,72 @@ discard block |
||
318 | 318 | * } |
319 | 319 | * return $res; |
320 | 320 | */ |
321 | - } |
|
322 | - |
|
323 | - /* public: evaluate the result (size, width) */ |
|
324 | - |
|
325 | - /** |
|
326 | - * Db::affectedRows() |
|
327 | - * |
|
328 | - * @return mixed |
|
329 | - */ |
|
330 | - public function affectedRows() |
|
331 | - { |
|
332 | - return @$this->linkId->Affected_Rows(); |
|
333 | - // return @$this->queryId->rowCount(); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Db::num_rows() |
|
338 | - * |
|
339 | - * @return mixed |
|
340 | - */ |
|
341 | - public function num_rows() |
|
342 | - { |
|
343 | - return $this->queryId->NumRows(); |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Db::num_fields() |
|
348 | - * |
|
349 | - * @return mixed |
|
350 | - */ |
|
351 | - public function num_fields() |
|
352 | - { |
|
353 | - return $this->queryId->NumCols(); |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * @param mixed $msg |
|
358 | - * @param string $line |
|
359 | - * @param string $file |
|
360 | - * @return mixed|void |
|
361 | - */ |
|
362 | - public function haltmsg($msg, $line = '', $file = '') |
|
363 | - { |
|
364 | - $this->log("Database error: $msg", $line, $file, 'error'); |
|
365 | - if ($this->linkId->ErrorNo() != '0' && $this->linkId->ErrorMsg() != '') { |
|
366 | - $this->log('ADOdb SQL Error: '.$this->linkId->ErrorMsg(), $line, $file, 'error'); |
|
367 | - } |
|
368 | - $this->logBackTrace($msg, $line, $file); |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Db::tableNames() |
|
373 | - * |
|
374 | - * @return array |
|
375 | - */ |
|
376 | - public function tableNames() |
|
377 | - { |
|
378 | - $return = []; |
|
379 | - $this->query('SHOW TABLES'); |
|
380 | - $i = 0; |
|
381 | - while ($info = $this->queryId->FetchRow()) { |
|
382 | - $return[$i]['table_name'] = $info[0]; |
|
383 | - $return[$i]['tablespace_name'] = $this->database; |
|
384 | - $return[$i]['database'] = $this->database; |
|
385 | - ++$i; |
|
386 | - } |
|
387 | - return $return; |
|
388 | - } |
|
321 | + } |
|
322 | + |
|
323 | + /* public: evaluate the result (size, width) */ |
|
324 | + |
|
325 | + /** |
|
326 | + * Db::affectedRows() |
|
327 | + * |
|
328 | + * @return mixed |
|
329 | + */ |
|
330 | + public function affectedRows() |
|
331 | + { |
|
332 | + return @$this->linkId->Affected_Rows(); |
|
333 | + // return @$this->queryId->rowCount(); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Db::num_rows() |
|
338 | + * |
|
339 | + * @return mixed |
|
340 | + */ |
|
341 | + public function num_rows() |
|
342 | + { |
|
343 | + return $this->queryId->NumRows(); |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Db::num_fields() |
|
348 | + * |
|
349 | + * @return mixed |
|
350 | + */ |
|
351 | + public function num_fields() |
|
352 | + { |
|
353 | + return $this->queryId->NumCols(); |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * @param mixed $msg |
|
358 | + * @param string $line |
|
359 | + * @param string $file |
|
360 | + * @return mixed|void |
|
361 | + */ |
|
362 | + public function haltmsg($msg, $line = '', $file = '') |
|
363 | + { |
|
364 | + $this->log("Database error: $msg", $line, $file, 'error'); |
|
365 | + if ($this->linkId->ErrorNo() != '0' && $this->linkId->ErrorMsg() != '') { |
|
366 | + $this->log('ADOdb SQL Error: '.$this->linkId->ErrorMsg(), $line, $file, 'error'); |
|
367 | + } |
|
368 | + $this->logBackTrace($msg, $line, $file); |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Db::tableNames() |
|
373 | + * |
|
374 | + * @return array |
|
375 | + */ |
|
376 | + public function tableNames() |
|
377 | + { |
|
378 | + $return = []; |
|
379 | + $this->query('SHOW TABLES'); |
|
380 | + $i = 0; |
|
381 | + while ($info = $this->queryId->FetchRow()) { |
|
382 | + $return[$i]['table_name'] = $info[0]; |
|
383 | + $return[$i]['tablespace_name'] = $this->database; |
|
384 | + $return[$i]['database'] = $this->database; |
|
385 | + ++$i; |
|
386 | + } |
|
387 | + return $return; |
|
388 | + } |
|
389 | 389 | } |
@@ -17,8 +17,7 @@ discard block |
||
17 | 17 | * |
18 | 18 | * @access public |
19 | 19 | */ |
20 | -class Db extends Generic implements Db_Interface |
|
21 | -{ |
|
20 | +class Db extends Generic implements Db_Interface { |
|
22 | 21 | public $driver = 'mysql'; |
23 | 22 | public $Rows = []; |
24 | 23 | public $type = 'adodb'; |
@@ -32,8 +31,7 @@ discard block |
||
32 | 31 | * @param string $driver |
33 | 32 | * @return bool|\the |
34 | 33 | */ |
35 | - public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
36 | - { |
|
34 | + public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') { |
|
37 | 35 | /* Handle defaults */ |
38 | 36 | if ('' == $database) { |
39 | 37 | $database = $this->database; |
@@ -64,16 +62,14 @@ discard block |
||
64 | 62 | * Db::disconnect() |
65 | 63 | * @return void |
66 | 64 | */ |
67 | - public function disconnect() |
|
68 | - { |
|
65 | + public function disconnect() { |
|
69 | 66 | } |
70 | 67 | |
71 | 68 | /** |
72 | 69 | * @param $string |
73 | 70 | * @return string |
74 | 71 | */ |
75 | - public function real_escape($string = '') |
|
76 | - { |
|
72 | + public function real_escape($string = '') { |
|
77 | 73 | return escapeshellarg($string); |
78 | 74 | } |
79 | 75 | |
@@ -81,8 +77,7 @@ discard block |
||
81 | 77 | * discard the query result |
82 | 78 | * @return void |
83 | 79 | */ |
84 | - public function free() |
|
85 | - { |
|
80 | + public function free() { |
|
86 | 81 | // @mysql_free_result($this->queryId); |
87 | 82 | // $this->queryId = 0; |
88 | 83 | } |
@@ -98,8 +93,7 @@ discard block |
||
98 | 93 | * @param string $file optionally pass __FILE__ calling the query for logging |
99 | 94 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
100 | 95 | */ |
101 | - public function queryReturn($query, $line = '', $file = '') |
|
102 | - { |
|
96 | + public function queryReturn($query, $line = '', $file = '') { |
|
103 | 97 | $this->query($query, $line, $file); |
104 | 98 | if ($this->num_rows() == 0) { |
105 | 99 | return false; |
@@ -125,8 +119,7 @@ discard block |
||
125 | 119 | * @param string $file optionally pass __FILE__ calling the query for logging |
126 | 120 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
127 | 121 | */ |
128 | - public function qr($query, $line = '', $file = '') |
|
129 | - { |
|
122 | + public function qr($query, $line = '', $file = '') { |
|
130 | 123 | return $this->queryReturn($query, $line, $file); |
131 | 124 | } |
132 | 125 | |
@@ -140,8 +133,7 @@ discard block |
||
140 | 133 | * @param string $file |
141 | 134 | * @return mixed 0 if no query or query id handler, safe to ignore this return |
142 | 135 | */ |
143 | - public function query($queryString, $line = '', $file = '') |
|
144 | - { |
|
136 | + public function query($queryString, $line = '', $file = '') { |
|
145 | 137 | /* No empty queries, please, since PHP4 chokes on them. */ |
146 | 138 | /* The empty query string is passed on from the constructor, |
147 | 139 | * when calling the class without a query, e.g. in situations |
@@ -184,8 +176,7 @@ discard block |
||
184 | 176 | * @param mixed $resultType |
185 | 177 | * @return bool |
186 | 178 | */ |
187 | - public function next_record($resultType = MYSQL_ASSOC) |
|
188 | - { |
|
179 | + public function next_record($resultType = MYSQL_ASSOC) { |
|
189 | 180 | if (!$this->queryId) { |
190 | 181 | $this->halt('next_record called with no query pending.'); |
191 | 182 | return 0; |
@@ -206,8 +197,7 @@ discard block |
||
206 | 197 | * @param integer $pos |
207 | 198 | * @return int |
208 | 199 | */ |
209 | - public function seek($pos = 0) |
|
210 | - { |
|
200 | + public function seek($pos = 0) { |
|
211 | 201 | if (isset($this->Rows[$pos])) { |
212 | 202 | $this->Row = $pos; |
213 | 203 | } else { |
@@ -225,8 +215,7 @@ discard block |
||
225 | 215 | * Db::transactionBegin() |
226 | 216 | * @return bool |
227 | 217 | */ |
228 | - public function transactionBegin() |
|
229 | - { |
|
218 | + public function transactionBegin() { |
|
230 | 219 | return true; |
231 | 220 | } |
232 | 221 | |
@@ -234,8 +223,7 @@ discard block |
||
234 | 223 | * Db::transactionCommit() |
235 | 224 | * @return bool |
236 | 225 | */ |
237 | - public function transactionCommit() |
|
238 | - { |
|
226 | + public function transactionCommit() { |
|
239 | 227 | return true; |
240 | 228 | } |
241 | 229 | |
@@ -243,8 +231,7 @@ discard block |
||
243 | 231 | * Db::transactionAbort() |
244 | 232 | * @return bool |
245 | 233 | */ |
246 | - public function transactionAbort() |
|
247 | - { |
|
234 | + public function transactionAbort() { |
|
248 | 235 | return true; |
249 | 236 | } |
250 | 237 | |
@@ -255,8 +242,7 @@ discard block |
||
255 | 242 | * @param mixed $field |
256 | 243 | * @return mixed |
257 | 244 | */ |
258 | - public function getLastInsertId($table, $field) |
|
259 | - { |
|
245 | + public function getLastInsertId($table, $field) { |
|
260 | 246 | return $this->linkId->Insert_ID($table, $field); |
261 | 247 | } |
262 | 248 | |
@@ -268,8 +254,7 @@ discard block |
||
268 | 254 | * @param string $mode |
269 | 255 | * @return void |
270 | 256 | */ |
271 | - public function lock($table, $mode = 'write') |
|
272 | - { |
|
257 | + public function lock($table, $mode = 'write') { |
|
273 | 258 | /* $this->connect(); |
274 | 259 | |
275 | 260 | * $query = "lock tables "; |
@@ -306,8 +291,7 @@ discard block |
||
306 | 291 | * Db::unlock() |
307 | 292 | * @return void |
308 | 293 | */ |
309 | - public function unlock() |
|
310 | - { |
|
294 | + public function unlock() { |
|
311 | 295 | /* $this->connect(); |
312 | 296 | |
313 | 297 | * $res = @mysql_query("unlock tables"); |
@@ -327,8 +311,7 @@ discard block |
||
327 | 311 | * |
328 | 312 | * @return mixed |
329 | 313 | */ |
330 | - public function affectedRows() |
|
331 | - { |
|
314 | + public function affectedRows() { |
|
332 | 315 | return @$this->linkId->Affected_Rows(); |
333 | 316 | // return @$this->queryId->rowCount(); |
334 | 317 | } |
@@ -338,8 +321,7 @@ discard block |
||
338 | 321 | * |
339 | 322 | * @return mixed |
340 | 323 | */ |
341 | - public function num_rows() |
|
342 | - { |
|
324 | + public function num_rows() { |
|
343 | 325 | return $this->queryId->NumRows(); |
344 | 326 | } |
345 | 327 | |
@@ -348,8 +330,7 @@ discard block |
||
348 | 330 | * |
349 | 331 | * @return mixed |
350 | 332 | */ |
351 | - public function num_fields() |
|
352 | - { |
|
333 | + public function num_fields() { |
|
353 | 334 | return $this->queryId->NumCols(); |
354 | 335 | } |
355 | 336 | |
@@ -359,8 +340,7 @@ discard block |
||
359 | 340 | * @param string $file |
360 | 341 | * @return mixed|void |
361 | 342 | */ |
362 | - public function haltmsg($msg, $line = '', $file = '') |
|
363 | - { |
|
343 | + public function haltmsg($msg, $line = '', $file = '') { |
|
364 | 344 | $this->log("Database error: $msg", $line, $file, 'error'); |
365 | 345 | if ($this->linkId->ErrorNo() != '0' && $this->linkId->ErrorMsg() != '') { |
366 | 346 | $this->log('ADOdb SQL Error: '.$this->linkId->ErrorMsg(), $line, $file, 'error'); |
@@ -373,8 +353,7 @@ discard block |
||
373 | 353 | * |
374 | 354 | * @return array |
375 | 355 | */ |
376 | - public function tableNames() |
|
377 | - { |
|
356 | + public function tableNames() { |
|
378 | 357 | $return = []; |
379 | 358 | $this->query('SHOW TABLES'); |
380 | 359 | $i = 0; |
@@ -20,256 +20,256 @@ discard block |
||
20 | 20 | */ |
21 | 21 | class Db extends Generic implements Db_Interface |
22 | 22 | { |
23 | - /* public: connection parameters */ |
|
24 | - public $driver = 'mysql'; |
|
25 | - public $Rows = []; |
|
26 | - /* public: this is an api revision, not a CVS revision. */ |
|
27 | - public $type = 'pdo'; |
|
28 | - |
|
29 | - /** |
|
30 | - * changes the database we are working with. |
|
31 | - * |
|
32 | - * @param string $database the name of the database to use |
|
33 | - * @return void |
|
34 | - */ |
|
35 | - public function selectDb($database) |
|
36 | - { |
|
37 | - $dSN = "{$this->driver}:dbname={$database};host={$this->host}"; |
|
38 | - if ($this->characterSet != '') { |
|
39 | - $dSN .= ';charset='.$this->characterSet; |
|
40 | - } |
|
41 | - $this->linkId = new PDO($dSN, $this->user, $this->password); |
|
42 | - $this->database = $database; |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * alias function of select_db, changes the database we are working with. |
|
48 | - * |
|
49 | - * @param string $database the name of the database to use |
|
50 | - * @return void |
|
51 | - */ |
|
52 | - public function useDb($database) |
|
53 | - { |
|
54 | - $this->selectDb($database); |
|
55 | - } |
|
56 | - |
|
57 | - /* public: connection management */ |
|
58 | - |
|
59 | - /** |
|
60 | - * Db::connect() |
|
61 | - * @param string $database |
|
62 | - * @param string $host |
|
63 | - * @param string $user |
|
64 | - * @param string $password |
|
65 | - * @param string $driver |
|
66 | - * @return bool|int|PDO |
|
67 | - */ |
|
68 | - public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
69 | - { |
|
70 | - /* Handle defaults */ |
|
71 | - if ('' == $database) { |
|
72 | - $database = $this->database; |
|
73 | - } |
|
74 | - if ('' == $host) { |
|
75 | - $host = $this->host; |
|
76 | - } |
|
77 | - if ('' == $user) { |
|
78 | - $user = $this->user; |
|
79 | - } |
|
80 | - if ('' == $password) { |
|
81 | - $password = $this->password; |
|
82 | - } |
|
83 | - if ('' == $driver) { |
|
84 | - $driver = $this->driver; |
|
85 | - } |
|
86 | - /* establish connection, select database */ |
|
87 | - $dSN = "{$driver}:dbname={$database};host={$host}"; |
|
88 | - if ($this->characterSet != '') { |
|
89 | - $dSN .= ';charset='.$this->characterSet; |
|
90 | - } |
|
91 | - if ($this->linkId === false) { |
|
92 | - try { |
|
93 | - $this->linkId = new PDO($dSN, $user, $password); |
|
94 | - } catch (\PDOException $e) { |
|
95 | - $this->halt('Connection Failed '.$e->getMessage()); |
|
96 | - return 0; |
|
97 | - } |
|
98 | - } |
|
99 | - return $this->linkId; |
|
100 | - } |
|
101 | - |
|
102 | - /* This only affects systems not using persistent connections */ |
|
103 | - |
|
104 | - /** |
|
105 | - * Db::disconnect() |
|
106 | - * @return void |
|
107 | - */ |
|
108 | - public function disconnect() |
|
109 | - { |
|
110 | - } |
|
111 | - |
|
112 | - /* public: discard the query result */ |
|
113 | - |
|
114 | - /** |
|
115 | - * Db::free() |
|
116 | - * @return void |
|
117 | - */ |
|
118 | - public function free() |
|
119 | - { |
|
120 | - // @mysql_free_result($this->queryId); |
|
121 | - // $this->queryId = 0; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Db::query() |
|
126 | - * |
|
127 | - * Sends an SQL query to the database |
|
128 | - * |
|
129 | - * @param mixed $queryString |
|
130 | - * @param string $line |
|
131 | - * @param string $file |
|
132 | - * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
133 | - */ |
|
134 | - public function query($queryString, $line = '', $file = '') |
|
135 | - { |
|
136 | - /* No empty queries, please, since PHP4 chokes on them. */ |
|
137 | - /* The empty query string is passed on from the constructor, |
|
23 | + /* public: connection parameters */ |
|
24 | + public $driver = 'mysql'; |
|
25 | + public $Rows = []; |
|
26 | + /* public: this is an api revision, not a CVS revision. */ |
|
27 | + public $type = 'pdo'; |
|
28 | + |
|
29 | + /** |
|
30 | + * changes the database we are working with. |
|
31 | + * |
|
32 | + * @param string $database the name of the database to use |
|
33 | + * @return void |
|
34 | + */ |
|
35 | + public function selectDb($database) |
|
36 | + { |
|
37 | + $dSN = "{$this->driver}:dbname={$database};host={$this->host}"; |
|
38 | + if ($this->characterSet != '') { |
|
39 | + $dSN .= ';charset='.$this->characterSet; |
|
40 | + } |
|
41 | + $this->linkId = new PDO($dSN, $this->user, $this->password); |
|
42 | + $this->database = $database; |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * alias function of select_db, changes the database we are working with. |
|
48 | + * |
|
49 | + * @param string $database the name of the database to use |
|
50 | + * @return void |
|
51 | + */ |
|
52 | + public function useDb($database) |
|
53 | + { |
|
54 | + $this->selectDb($database); |
|
55 | + } |
|
56 | + |
|
57 | + /* public: connection management */ |
|
58 | + |
|
59 | + /** |
|
60 | + * Db::connect() |
|
61 | + * @param string $database |
|
62 | + * @param string $host |
|
63 | + * @param string $user |
|
64 | + * @param string $password |
|
65 | + * @param string $driver |
|
66 | + * @return bool|int|PDO |
|
67 | + */ |
|
68 | + public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
69 | + { |
|
70 | + /* Handle defaults */ |
|
71 | + if ('' == $database) { |
|
72 | + $database = $this->database; |
|
73 | + } |
|
74 | + if ('' == $host) { |
|
75 | + $host = $this->host; |
|
76 | + } |
|
77 | + if ('' == $user) { |
|
78 | + $user = $this->user; |
|
79 | + } |
|
80 | + if ('' == $password) { |
|
81 | + $password = $this->password; |
|
82 | + } |
|
83 | + if ('' == $driver) { |
|
84 | + $driver = $this->driver; |
|
85 | + } |
|
86 | + /* establish connection, select database */ |
|
87 | + $dSN = "{$driver}:dbname={$database};host={$host}"; |
|
88 | + if ($this->characterSet != '') { |
|
89 | + $dSN .= ';charset='.$this->characterSet; |
|
90 | + } |
|
91 | + if ($this->linkId === false) { |
|
92 | + try { |
|
93 | + $this->linkId = new PDO($dSN, $user, $password); |
|
94 | + } catch (\PDOException $e) { |
|
95 | + $this->halt('Connection Failed '.$e->getMessage()); |
|
96 | + return 0; |
|
97 | + } |
|
98 | + } |
|
99 | + return $this->linkId; |
|
100 | + } |
|
101 | + |
|
102 | + /* This only affects systems not using persistent connections */ |
|
103 | + |
|
104 | + /** |
|
105 | + * Db::disconnect() |
|
106 | + * @return void |
|
107 | + */ |
|
108 | + public function disconnect() |
|
109 | + { |
|
110 | + } |
|
111 | + |
|
112 | + /* public: discard the query result */ |
|
113 | + |
|
114 | + /** |
|
115 | + * Db::free() |
|
116 | + * @return void |
|
117 | + */ |
|
118 | + public function free() |
|
119 | + { |
|
120 | + // @mysql_free_result($this->queryId); |
|
121 | + // $this->queryId = 0; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Db::query() |
|
126 | + * |
|
127 | + * Sends an SQL query to the database |
|
128 | + * |
|
129 | + * @param mixed $queryString |
|
130 | + * @param string $line |
|
131 | + * @param string $file |
|
132 | + * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
133 | + */ |
|
134 | + public function query($queryString, $line = '', $file = '') |
|
135 | + { |
|
136 | + /* No empty queries, please, since PHP4 chokes on them. */ |
|
137 | + /* The empty query string is passed on from the constructor, |
|
138 | 138 | * when calling the class without a query, e.g. in situations |
139 | 139 | * like these: '$db = new db_Subclass;' |
140 | 140 | */ |
141 | - if ($queryString == '') { |
|
142 | - return 0; |
|
143 | - } |
|
144 | - if (!$this->connect()) { |
|
145 | - return 0; |
|
146 | - /* we already complained in connect() about that. */ |
|
147 | - } |
|
148 | - // New query, discard previous result. |
|
149 | - if ($this->queryId !== false) { |
|
150 | - $this->free(); |
|
151 | - } |
|
152 | - |
|
153 | - if ($this->Debug) { |
|
154 | - printf("Debug: query = %s<br>\n", $queryString); |
|
155 | - } |
|
156 | - if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
157 | - $this->log($queryString, $line, $file); |
|
158 | - } |
|
159 | - |
|
160 | - $this->queryId = $this->linkId->prepare($queryString); |
|
161 | - $success = $this->queryId->execute(); |
|
162 | - $this->Rows = $this->queryId->fetchAll(); |
|
163 | - //$this->log("PDO Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__); |
|
164 | - $this->Row = -1; |
|
165 | - if ($success === false) { |
|
166 | - $this->emailError($queryString, json_encode($this->queryId->errorInfo(), JSON_PRETTY_PRINT), $line, $file); |
|
167 | - } |
|
168 | - |
|
169 | - // Will return nada if it fails. That's fine. |
|
170 | - return $this->queryId; |
|
171 | - } |
|
172 | - |
|
173 | - /* public: walk result set */ |
|
174 | - |
|
175 | - /** |
|
176 | - * Db::next_record() |
|
177 | - * @param mixed $resultType |
|
178 | - * @return bool |
|
179 | - */ |
|
180 | - public function next_record($resultType = MYSQLI_ASSOC) |
|
181 | - { |
|
182 | - // PDO result types so far seem to be +1 |
|
183 | - ++$resultType; |
|
184 | - if (!$this->queryId) { |
|
185 | - $this->halt('next_record called with no query pending.'); |
|
186 | - return 0; |
|
187 | - } |
|
188 | - |
|
189 | - ++$this->Row; |
|
190 | - $this->Record = $this->Rows[$this->Row]; |
|
191 | - |
|
192 | - $stat = is_array($this->Record); |
|
193 | - if (!$stat && $this->autoFree) { |
|
194 | - $this->free(); |
|
195 | - } |
|
196 | - return $stat; |
|
197 | - } |
|
198 | - |
|
199 | - /* public: position in result set */ |
|
200 | - |
|
201 | - /** |
|
202 | - * Db::seek() |
|
203 | - * @param integer $pos |
|
204 | - * @return int |
|
205 | - */ |
|
206 | - public function seek($pos = 0) |
|
207 | - { |
|
208 | - if (isset($this->Rows[$pos])) { |
|
209 | - $this->Row = $pos; |
|
210 | - } else { |
|
211 | - $this->halt("seek($pos) failed: result has ".count($this->Rows).' rows'); |
|
212 | - /* half assed attempt to save the day, |
|
141 | + if ($queryString == '') { |
|
142 | + return 0; |
|
143 | + } |
|
144 | + if (!$this->connect()) { |
|
145 | + return 0; |
|
146 | + /* we already complained in connect() about that. */ |
|
147 | + } |
|
148 | + // New query, discard previous result. |
|
149 | + if ($this->queryId !== false) { |
|
150 | + $this->free(); |
|
151 | + } |
|
152 | + |
|
153 | + if ($this->Debug) { |
|
154 | + printf("Debug: query = %s<br>\n", $queryString); |
|
155 | + } |
|
156 | + if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
157 | + $this->log($queryString, $line, $file); |
|
158 | + } |
|
159 | + |
|
160 | + $this->queryId = $this->linkId->prepare($queryString); |
|
161 | + $success = $this->queryId->execute(); |
|
162 | + $this->Rows = $this->queryId->fetchAll(); |
|
163 | + //$this->log("PDO Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__); |
|
164 | + $this->Row = -1; |
|
165 | + if ($success === false) { |
|
166 | + $this->emailError($queryString, json_encode($this->queryId->errorInfo(), JSON_PRETTY_PRINT), $line, $file); |
|
167 | + } |
|
168 | + |
|
169 | + // Will return nada if it fails. That's fine. |
|
170 | + return $this->queryId; |
|
171 | + } |
|
172 | + |
|
173 | + /* public: walk result set */ |
|
174 | + |
|
175 | + /** |
|
176 | + * Db::next_record() |
|
177 | + * @param mixed $resultType |
|
178 | + * @return bool |
|
179 | + */ |
|
180 | + public function next_record($resultType = MYSQLI_ASSOC) |
|
181 | + { |
|
182 | + // PDO result types so far seem to be +1 |
|
183 | + ++$resultType; |
|
184 | + if (!$this->queryId) { |
|
185 | + $this->halt('next_record called with no query pending.'); |
|
186 | + return 0; |
|
187 | + } |
|
188 | + |
|
189 | + ++$this->Row; |
|
190 | + $this->Record = $this->Rows[$this->Row]; |
|
191 | + |
|
192 | + $stat = is_array($this->Record); |
|
193 | + if (!$stat && $this->autoFree) { |
|
194 | + $this->free(); |
|
195 | + } |
|
196 | + return $stat; |
|
197 | + } |
|
198 | + |
|
199 | + /* public: position in result set */ |
|
200 | + |
|
201 | + /** |
|
202 | + * Db::seek() |
|
203 | + * @param integer $pos |
|
204 | + * @return int |
|
205 | + */ |
|
206 | + public function seek($pos = 0) |
|
207 | + { |
|
208 | + if (isset($this->Rows[$pos])) { |
|
209 | + $this->Row = $pos; |
|
210 | + } else { |
|
211 | + $this->halt("seek($pos) failed: result has ".count($this->Rows).' rows'); |
|
212 | + /* half assed attempt to save the day, |
|
213 | 213 | * but do not consider this documented or even |
214 | 214 | * desirable behaviour. |
215 | 215 | */ |
216 | - return 0; |
|
217 | - } |
|
218 | - return 1; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Initiates a transaction |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function transactionBegin() |
|
226 | - { |
|
227 | - return $this->linkId->beginTransaction(); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Commits a transaction |
|
232 | - * @return bool |
|
233 | - */ |
|
234 | - public function transactionCommit() |
|
235 | - { |
|
236 | - return $this->linkId->commit(); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * Rolls back a transaction |
|
241 | - * @return bool |
|
242 | - */ |
|
243 | - public function transactionAbort() |
|
244 | - { |
|
245 | - return $this->linkId->rollBack(); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Db::getLastInsertId() |
|
250 | - * @param mixed $table |
|
251 | - * @param mixed $field |
|
252 | - * @return int |
|
253 | - */ |
|
254 | - public function getLastInsertId($table, $field) |
|
255 | - { |
|
256 | - if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
257 | - return -1; |
|
258 | - } |
|
259 | - return $this->linkId->lastInsertId(); |
|
260 | - } |
|
261 | - |
|
262 | - /* public: table locking */ |
|
263 | - |
|
264 | - /** |
|
265 | - * Db::lock() |
|
266 | - * @param mixed $table |
|
267 | - * @param string $mode |
|
268 | - * @return void |
|
269 | - */ |
|
270 | - public function lock($table, $mode = 'write') |
|
271 | - { |
|
272 | - /* $this->connect(); |
|
216 | + return 0; |
|
217 | + } |
|
218 | + return 1; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Initiates a transaction |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function transactionBegin() |
|
226 | + { |
|
227 | + return $this->linkId->beginTransaction(); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Commits a transaction |
|
232 | + * @return bool |
|
233 | + */ |
|
234 | + public function transactionCommit() |
|
235 | + { |
|
236 | + return $this->linkId->commit(); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * Rolls back a transaction |
|
241 | + * @return bool |
|
242 | + */ |
|
243 | + public function transactionAbort() |
|
244 | + { |
|
245 | + return $this->linkId->rollBack(); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Db::getLastInsertId() |
|
250 | + * @param mixed $table |
|
251 | + * @param mixed $field |
|
252 | + * @return int |
|
253 | + */ |
|
254 | + public function getLastInsertId($table, $field) |
|
255 | + { |
|
256 | + if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
257 | + return -1; |
|
258 | + } |
|
259 | + return $this->linkId->lastInsertId(); |
|
260 | + } |
|
261 | + |
|
262 | + /* public: table locking */ |
|
263 | + |
|
264 | + /** |
|
265 | + * Db::lock() |
|
266 | + * @param mixed $table |
|
267 | + * @param string $mode |
|
268 | + * @return void |
|
269 | + */ |
|
270 | + public function lock($table, $mode = 'write') |
|
271 | + { |
|
272 | + /* $this->connect(); |
|
273 | 273 | |
274 | 274 | * $query = "lock tables "; |
275 | 275 | * if (is_array($table)) |
@@ -299,15 +299,15 @@ discard block |
||
299 | 299 | * } |
300 | 300 | * return $res; |
301 | 301 | */ |
302 | - } |
|
302 | + } |
|
303 | 303 | |
304 | - /** |
|
305 | - * Db::unlock() |
|
306 | - * @return void |
|
307 | - */ |
|
308 | - public function unlock() |
|
309 | - { |
|
310 | - /* $this->connect(); |
|
304 | + /** |
|
305 | + * Db::unlock() |
|
306 | + * @return void |
|
307 | + */ |
|
308 | + public function unlock() |
|
309 | + { |
|
310 | + /* $this->connect(); |
|
311 | 311 | |
312 | 312 | * $res = @mysql_query("unlock tables"); |
313 | 313 | * if (!$res) |
@@ -317,68 +317,68 @@ discard block |
||
317 | 317 | * } |
318 | 318 | * return $res; |
319 | 319 | */ |
320 | - } |
|
321 | - |
|
322 | - /* public: evaluate the result (size, width) */ |
|
323 | - |
|
324 | - /** |
|
325 | - * Db::affectedRows() |
|
326 | - * |
|
327 | - * @return mixed |
|
328 | - */ |
|
329 | - public function affectedRows() |
|
330 | - { |
|
331 | - return @$this->queryId->rowCount(); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Db::num_rows() |
|
336 | - * @return int |
|
337 | - */ |
|
338 | - public function num_rows() |
|
339 | - { |
|
340 | - return count($this->Rows); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Db::num_fields() |
|
345 | - * @return int |
|
346 | - */ |
|
347 | - public function num_fields() |
|
348 | - { |
|
349 | - $keys = array_keys($this->Rows); |
|
350 | - return count($this->Rows[$keys[0]]); |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * @param mixed $msg |
|
355 | - * @param string $line |
|
356 | - * @param string $file |
|
357 | - * @return mixed|void |
|
358 | - */ |
|
359 | - public function haltmsg($msg, $line = '', $file = '') |
|
360 | - { |
|
361 | - $this->log("Database error: $msg", $line, $file, 'error'); |
|
362 | - if ($this->Errno != '0' || $this->Error != '()') { |
|
363 | - $this->log('PDO MySQL Error: '.json_encode($this->linkId->errorInfo()), $line, $file, 'error'); |
|
364 | - } |
|
365 | - $this->logBackTrace($msg, $line, $file); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Db::tableNames() |
|
370 | - * |
|
371 | - * @return array |
|
372 | - */ |
|
373 | - public function tableNames() |
|
374 | - { |
|
375 | - $return = []; |
|
376 | - $this->query('SHOW TABLES'); |
|
377 | - foreach ($this->Rows as $i => $info) { |
|
378 | - $return[$i]['table_name'] = $info[0]; |
|
379 | - $return[$i]['tablespace_name'] = $this->database; |
|
380 | - $return[$i]['database'] = $this->database; |
|
381 | - } |
|
382 | - return $return; |
|
383 | - } |
|
320 | + } |
|
321 | + |
|
322 | + /* public: evaluate the result (size, width) */ |
|
323 | + |
|
324 | + /** |
|
325 | + * Db::affectedRows() |
|
326 | + * |
|
327 | + * @return mixed |
|
328 | + */ |
|
329 | + public function affectedRows() |
|
330 | + { |
|
331 | + return @$this->queryId->rowCount(); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Db::num_rows() |
|
336 | + * @return int |
|
337 | + */ |
|
338 | + public function num_rows() |
|
339 | + { |
|
340 | + return count($this->Rows); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Db::num_fields() |
|
345 | + * @return int |
|
346 | + */ |
|
347 | + public function num_fields() |
|
348 | + { |
|
349 | + $keys = array_keys($this->Rows); |
|
350 | + return count($this->Rows[$keys[0]]); |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * @param mixed $msg |
|
355 | + * @param string $line |
|
356 | + * @param string $file |
|
357 | + * @return mixed|void |
|
358 | + */ |
|
359 | + public function haltmsg($msg, $line = '', $file = '') |
|
360 | + { |
|
361 | + $this->log("Database error: $msg", $line, $file, 'error'); |
|
362 | + if ($this->Errno != '0' || $this->Error != '()') { |
|
363 | + $this->log('PDO MySQL Error: '.json_encode($this->linkId->errorInfo()), $line, $file, 'error'); |
|
364 | + } |
|
365 | + $this->logBackTrace($msg, $line, $file); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Db::tableNames() |
|
370 | + * |
|
371 | + * @return array |
|
372 | + */ |
|
373 | + public function tableNames() |
|
374 | + { |
|
375 | + $return = []; |
|
376 | + $this->query('SHOW TABLES'); |
|
377 | + foreach ($this->Rows as $i => $info) { |
|
378 | + $return[$i]['table_name'] = $info[0]; |
|
379 | + $return[$i]['tablespace_name'] = $this->database; |
|
380 | + $return[$i]['database'] = $this->database; |
|
381 | + } |
|
382 | + return $return; |
|
383 | + } |
|
384 | 384 | } |
@@ -18,8 +18,7 @@ discard block |
||
18 | 18 | * |
19 | 19 | * @access public |
20 | 20 | */ |
21 | -class Db extends Generic implements Db_Interface |
|
22 | -{ |
|
21 | +class Db extends Generic implements Db_Interface { |
|
23 | 22 | /* public: connection parameters */ |
24 | 23 | public $driver = 'mysql'; |
25 | 24 | public $Rows = []; |
@@ -32,8 +31,7 @@ discard block |
||
32 | 31 | * @param string $database the name of the database to use |
33 | 32 | * @return void |
34 | 33 | */ |
35 | - public function selectDb($database) |
|
36 | - { |
|
34 | + public function selectDb($database) { |
|
37 | 35 | $dSN = "{$this->driver}:dbname={$database};host={$this->host}"; |
38 | 36 | if ($this->characterSet != '') { |
39 | 37 | $dSN .= ';charset='.$this->characterSet; |
@@ -49,8 +47,7 @@ discard block |
||
49 | 47 | * @param string $database the name of the database to use |
50 | 48 | * @return void |
51 | 49 | */ |
52 | - public function useDb($database) |
|
53 | - { |
|
50 | + public function useDb($database) { |
|
54 | 51 | $this->selectDb($database); |
55 | 52 | } |
56 | 53 | |
@@ -65,8 +62,7 @@ discard block |
||
65 | 62 | * @param string $driver |
66 | 63 | * @return bool|int|PDO |
67 | 64 | */ |
68 | - public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
69 | - { |
|
65 | + public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') { |
|
70 | 66 | /* Handle defaults */ |
71 | 67 | if ('' == $database) { |
72 | 68 | $database = $this->database; |
@@ -105,8 +101,7 @@ discard block |
||
105 | 101 | * Db::disconnect() |
106 | 102 | * @return void |
107 | 103 | */ |
108 | - public function disconnect() |
|
109 | - { |
|
104 | + public function disconnect() { |
|
110 | 105 | } |
111 | 106 | |
112 | 107 | /* public: discard the query result */ |
@@ -115,8 +110,7 @@ discard block |
||
115 | 110 | * Db::free() |
116 | 111 | * @return void |
117 | 112 | */ |
118 | - public function free() |
|
119 | - { |
|
113 | + public function free() { |
|
120 | 114 | // @mysql_free_result($this->queryId); |
121 | 115 | // $this->queryId = 0; |
122 | 116 | } |
@@ -131,8 +125,7 @@ discard block |
||
131 | 125 | * @param string $file |
132 | 126 | * @return mixed 0 if no query or query id handler, safe to ignore this return |
133 | 127 | */ |
134 | - public function query($queryString, $line = '', $file = '') |
|
135 | - { |
|
128 | + public function query($queryString, $line = '', $file = '') { |
|
136 | 129 | /* No empty queries, please, since PHP4 chokes on them. */ |
137 | 130 | /* The empty query string is passed on from the constructor, |
138 | 131 | * when calling the class without a query, e.g. in situations |
@@ -177,8 +170,7 @@ discard block |
||
177 | 170 | * @param mixed $resultType |
178 | 171 | * @return bool |
179 | 172 | */ |
180 | - public function next_record($resultType = MYSQLI_ASSOC) |
|
181 | - { |
|
173 | + public function next_record($resultType = MYSQLI_ASSOC) { |
|
182 | 174 | // PDO result types so far seem to be +1 |
183 | 175 | ++$resultType; |
184 | 176 | if (!$this->queryId) { |
@@ -203,8 +195,7 @@ discard block |
||
203 | 195 | * @param integer $pos |
204 | 196 | * @return int |
205 | 197 | */ |
206 | - public function seek($pos = 0) |
|
207 | - { |
|
198 | + public function seek($pos = 0) { |
|
208 | 199 | if (isset($this->Rows[$pos])) { |
209 | 200 | $this->Row = $pos; |
210 | 201 | } else { |
@@ -222,8 +213,7 @@ discard block |
||
222 | 213 | * Initiates a transaction |
223 | 214 | * @return bool |
224 | 215 | */ |
225 | - public function transactionBegin() |
|
226 | - { |
|
216 | + public function transactionBegin() { |
|
227 | 217 | return $this->linkId->beginTransaction(); |
228 | 218 | } |
229 | 219 | |
@@ -231,8 +221,7 @@ discard block |
||
231 | 221 | * Commits a transaction |
232 | 222 | * @return bool |
233 | 223 | */ |
234 | - public function transactionCommit() |
|
235 | - { |
|
224 | + public function transactionCommit() { |
|
236 | 225 | return $this->linkId->commit(); |
237 | 226 | } |
238 | 227 | |
@@ -240,8 +229,7 @@ discard block |
||
240 | 229 | * Rolls back a transaction |
241 | 230 | * @return bool |
242 | 231 | */ |
243 | - public function transactionAbort() |
|
244 | - { |
|
232 | + public function transactionAbort() { |
|
245 | 233 | return $this->linkId->rollBack(); |
246 | 234 | } |
247 | 235 | |
@@ -251,8 +239,7 @@ discard block |
||
251 | 239 | * @param mixed $field |
252 | 240 | * @return int |
253 | 241 | */ |
254 | - public function getLastInsertId($table, $field) |
|
255 | - { |
|
242 | + public function getLastInsertId($table, $field) { |
|
256 | 243 | if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
257 | 244 | return -1; |
258 | 245 | } |
@@ -267,8 +254,7 @@ discard block |
||
267 | 254 | * @param string $mode |
268 | 255 | * @return void |
269 | 256 | */ |
270 | - public function lock($table, $mode = 'write') |
|
271 | - { |
|
257 | + public function lock($table, $mode = 'write') { |
|
272 | 258 | /* $this->connect(); |
273 | 259 | |
274 | 260 | * $query = "lock tables "; |
@@ -305,8 +291,7 @@ discard block |
||
305 | 291 | * Db::unlock() |
306 | 292 | * @return void |
307 | 293 | */ |
308 | - public function unlock() |
|
309 | - { |
|
294 | + public function unlock() { |
|
310 | 295 | /* $this->connect(); |
311 | 296 | |
312 | 297 | * $res = @mysql_query("unlock tables"); |
@@ -326,8 +311,7 @@ discard block |
||
326 | 311 | * |
327 | 312 | * @return mixed |
328 | 313 | */ |
329 | - public function affectedRows() |
|
330 | - { |
|
314 | + public function affectedRows() { |
|
331 | 315 | return @$this->queryId->rowCount(); |
332 | 316 | } |
333 | 317 | |
@@ -335,8 +319,7 @@ discard block |
||
335 | 319 | * Db::num_rows() |
336 | 320 | * @return int |
337 | 321 | */ |
338 | - public function num_rows() |
|
339 | - { |
|
322 | + public function num_rows() { |
|
340 | 323 | return count($this->Rows); |
341 | 324 | } |
342 | 325 | |
@@ -344,8 +327,7 @@ discard block |
||
344 | 327 | * Db::num_fields() |
345 | 328 | * @return int |
346 | 329 | */ |
347 | - public function num_fields() |
|
348 | - { |
|
330 | + public function num_fields() { |
|
349 | 331 | $keys = array_keys($this->Rows); |
350 | 332 | return count($this->Rows[$keys[0]]); |
351 | 333 | } |
@@ -356,8 +338,7 @@ discard block |
||
356 | 338 | * @param string $file |
357 | 339 | * @return mixed|void |
358 | 340 | */ |
359 | - public function haltmsg($msg, $line = '', $file = '') |
|
360 | - { |
|
341 | + public function haltmsg($msg, $line = '', $file = '') { |
|
361 | 342 | $this->log("Database error: $msg", $line, $file, 'error'); |
362 | 343 | if ($this->Errno != '0' || $this->Error != '()') { |
363 | 344 | $this->log('PDO MySQL Error: '.json_encode($this->linkId->errorInfo()), $line, $file, 'error'); |
@@ -370,8 +351,7 @@ discard block |
||
370 | 351 | * |
371 | 352 | * @return array |
372 | 353 | */ |
373 | - public function tableNames() |
|
374 | - { |
|
354 | + public function tableNames() { |
|
375 | 355 | $return = []; |
376 | 356 | $this->query('SHOW TABLES'); |
377 | 357 | foreach ($this->Rows as $i => $info) { |
@@ -14,385 +14,385 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class Generic |
16 | 16 | { |
17 | - /* public: connection parameters */ |
|
18 | - public $host = 'localhost'; |
|
19 | - public $database = ''; |
|
20 | - public $user = ''; |
|
21 | - public $password = ''; |
|
22 | - public $port = ''; |
|
23 | - |
|
24 | - /* public: configuration parameters */ |
|
25 | - public $autoStripslashes = false; |
|
26 | - public $Debug = 0; // Set to 1 for debugging messages. |
|
27 | - public $haltOnError = 'yes'; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning) |
|
28 | - |
|
29 | - public $maxConnectErrors = 5; |
|
30 | - public $connectionAttempt = 0; |
|
31 | - public $maxMatches = 10000000; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var int |
|
35 | - */ |
|
36 | - public $autoFree = 0; // Set to 1 for automatic mysql_free_result() |
|
37 | - |
|
38 | - /* public: result array and current row number */ |
|
39 | - public $Record = []; |
|
40 | - public $Row; |
|
41 | - |
|
42 | - /* public: current error number and error text */ |
|
43 | - public $Errno = 0; |
|
44 | - public $Error = ''; |
|
45 | - |
|
46 | - /* public: this is an api revision, not a CVS revision. */ |
|
47 | - public $type = 'generic'; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var int|object |
|
51 | - */ |
|
52 | - public $linkId = 0; |
|
53 | - public $queryId = 0; |
|
54 | - |
|
55 | - public $characterSet = 'utf8mb4'; |
|
56 | - public $collation = 'utf8mb4_unicode_ci'; |
|
57 | - |
|
58 | - /** |
|
59 | - * Logged queries. |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - protected $log = []; |
|
63 | - |
|
64 | - /** |
|
65 | - * Constructs the db handler, can optionally specify connection parameters |
|
66 | - * |
|
67 | - * @param string $database Optional The database name |
|
68 | - * @param string $user Optional The username to connect with |
|
69 | - * @param string $password Optional The password to use |
|
70 | - * @param string $host Optional The hostname where the server is, or default to localhost |
|
71 | - * @param string $query Optional query to perform immediately |
|
72 | - * @param string $port optional port for the connection |
|
73 | - */ |
|
74 | - public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = '') |
|
75 | - { |
|
76 | - $this->database = $database; |
|
77 | - $this->user = $user; |
|
78 | - $this->password = $password; |
|
79 | - $this->host = $host; |
|
80 | - $this->port = $port; |
|
81 | - if ($query != '') { |
|
82 | - $this->query($query); |
|
83 | - } |
|
84 | - $this->connection_atttempt = 0; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @param string $message |
|
89 | - * @param string $line |
|
90 | - * @param string $file |
|
91 | - * @return void |
|
92 | - */ |
|
93 | - public function log($message, $line = '', $file = '', $level = 'info') |
|
94 | - { |
|
95 | - error_log($message); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @return int|object |
|
100 | - */ |
|
101 | - public function linkId() |
|
102 | - { |
|
103 | - return $this->linkId; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return int|object |
|
108 | - */ |
|
109 | - public function queryId() |
|
110 | - { |
|
111 | - return $this->queryId; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param $string |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - public function real_escape($string = '') |
|
119 | - { |
|
120 | - if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
121 | - return $this->escape($string); |
|
122 | - } |
|
123 | - return mysqli_real_escape_string($this->linkId, $string); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param $string |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - public function escape($string = '') |
|
131 | - { |
|
132 | - //if (function_exists('mysql_escape_string')) |
|
133 | - //return mysql_escape_string($string); |
|
134 | - return str_replace(['\\', "\0", "\n", "\r", "'", '"', "\x1a"], ['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'], $string); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param mixed $str |
|
139 | - * @return string |
|
140 | - */ |
|
141 | - public function dbAddslashes($str = '') |
|
142 | - { |
|
143 | - if (!isset($str) || $str == '') { |
|
144 | - return ''; |
|
145 | - } |
|
146 | - return addslashes($str); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Db::toTimestamp() |
|
151 | - * @param mixed $epoch |
|
152 | - * @return bool|string |
|
153 | - */ |
|
154 | - public function toTimestamp($epoch) |
|
155 | - { |
|
156 | - return date('Y-m-d H:i:s', is_float($epoch) ? intval($epoch) : $epoch); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Db::fromTimestamp() |
|
161 | - * @param mixed $timestamp |
|
162 | - * @return bool|int|mixed |
|
163 | - */ |
|
164 | - public function fromTimestamp($timestamp) |
|
165 | - { |
|
166 | - if (preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $timestamp, $parts)) { |
|
167 | - $time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
|
168 | - } elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) { |
|
169 | - $time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
|
170 | - } elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) { |
|
171 | - $time = mktime(1, 1, 1, $parts[2], $parts[3], $parts[1]); |
|
172 | - } elseif (is_numeric($timestamp) && $timestamp >= 943938000) { |
|
173 | - $time = $timestamp; |
|
174 | - } else { |
|
175 | - $this->log('Cannot Match Timestamp from '.$timestamp, __LINE__, __FILE__); |
|
176 | - $time = false; |
|
177 | - } |
|
178 | - return $time; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * perform a query with limited result set |
|
183 | - * |
|
184 | - * @param string $queryString |
|
185 | - * @param string|int $numRows |
|
186 | - * @param int $offset |
|
187 | - * @param string|int $line |
|
188 | - * @param string $file |
|
189 | - * @return mixed |
|
190 | - */ |
|
191 | - public function limitQuery($queryString, $numRows = '', $offset = 0, $line = '', $file = '') |
|
192 | - { |
|
193 | - if (!$numRows) { |
|
194 | - $numRows = $this->maxMatches; |
|
195 | - } |
|
196 | - if ($offset == 0) { |
|
197 | - $queryString .= ' LIMIT '.(int) $numRows; |
|
198 | - } else { |
|
199 | - $queryString .= ' LIMIT '.(int) $offset.','.(int) $numRows; |
|
200 | - } |
|
201 | - |
|
202 | - if ($this->Debug) { |
|
203 | - printf("Debug: limitQuery = %s<br>offset=%d, num_rows=%d<br>\n", $queryString, $offset, $numRows); |
|
204 | - } |
|
205 | - |
|
206 | - return $this->query($queryString, $line, $file); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * db:qr() |
|
211 | - * |
|
212 | - * alias of queryReturn() |
|
213 | - * |
|
214 | - * @param mixed $query SQL Query to be used |
|
215 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
216 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
217 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
218 | - */ |
|
219 | - public function qr($query, $line = '', $file = '') |
|
220 | - { |
|
221 | - return $this->queryReturn($query, $line, $file); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * gets a field |
|
226 | - * |
|
227 | - * @param mixed $name |
|
228 | - * @param string $stripSlashes |
|
229 | - * @return string |
|
230 | - */ |
|
231 | - public function f($name, $stripSlashes = '') |
|
232 | - { |
|
233 | - if (is_null($this->Record)) { |
|
234 | - return null; |
|
235 | - } elseif ($stripSlashes || ($this->autoStripslashes && !$stripSlashes)) { |
|
236 | - return stripslashes($this->Record[$name]); |
|
237 | - } else { |
|
238 | - return $this->Record[$name]; |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * error handling |
|
244 | - * |
|
245 | - * @param mixed $msg |
|
246 | - * @param string $line |
|
247 | - * @param string $file |
|
248 | - * @return void |
|
249 | - */ |
|
250 | - public function halt($msg, $line = '', $file = '') |
|
251 | - { |
|
252 | - $this->unlock(false); |
|
253 | - /* Just in case there is a table currently locked */ |
|
254 | - |
|
255 | - //$this->Error = @$this->linkId->error; |
|
256 | - //$this->Errno = @$this->linkId->errno; |
|
257 | - if ($this->haltOnError == 'no') { |
|
258 | - return true; |
|
259 | - } |
|
260 | - if ($msg != '') { |
|
261 | - $this->haltmsg($msg, $line, $file); |
|
262 | - } |
|
263 | - if ($this->haltOnError != 'report') { |
|
264 | - echo '<p><b>Session halted.</b>'; |
|
265 | - //if (isset($GLOBALS['tf'])) |
|
266 | - //$GLOBALS['tf']->terminate(); |
|
267 | - die(); |
|
268 | - } |
|
269 | - return true; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * @param mixed $msg |
|
274 | - * @param string $line |
|
275 | - * @param string $file |
|
276 | - * @return mixed|void |
|
277 | - */ |
|
278 | - public function logBackTrace($msg, $line = '', $file = '') |
|
279 | - { |
|
280 | - $backtrace = (function_exists('debug_backtrace') ? debug_backtrace() : []); |
|
281 | - $this->log( |
|
282 | - ('' !== getenv('REQUEST_URI') ? ' '.getenv('REQUEST_URI') : ''). |
|
283 | - ((isset($_POST) && count($_POST)) ? ' POST='.json_encode($_POST) : ''). |
|
284 | - ((isset($_GET) && count($_GET)) ? ' GET='.json_encode($_GET) : ''). |
|
285 | - ((isset($_FILES) && count($_FILES)) ? ' FILES='.json_encode($_FILES) : ''). |
|
286 | - ('' !== getenv('HTTP_USER_AGENT') ? ' AGENT="'.getenv('HTTP_USER_AGENT').'"' : ''). |
|
287 | - (isset($_SERVER['REQUEST_METHOD']) ? ' METHOD="'.$_SERVER['REQUEST_METHOD'].'"'. |
|
288 | - ($_SERVER['REQUEST_METHOD'] === 'POST' ? ' POST="'.json_encode($_POST).'"' : '') : ''), |
|
289 | - $line, |
|
290 | - $file, |
|
291 | - 'error' |
|
292 | - ); |
|
293 | - for ($level = 1, $levelMax = count($backtrace); $level < $levelMax; $level++) { |
|
294 | - $message = (isset($backtrace[$level]['file']) ? 'File: '.$backtrace[$level]['file'] : ''). |
|
295 | - (isset($backtrace[$level]['line']) ? ' Line: '.$backtrace[$level]['line'] : ''). |
|
296 | - ' Function: '.(isset($backtrace[$level] ['class']) ? '(class '.$backtrace[$level] ['class'].') ' : ''). |
|
297 | - (isset($backtrace[$level] ['type']) ? $backtrace[$level] ['type'].' ' : ''). |
|
298 | - $backtrace[$level] ['function'].'('; |
|
299 | - if (isset($backtrace[$level] ['args'])) { |
|
300 | - for ($argument = 0, $argumentMax = count($backtrace[$level]['args']); $argument < $argumentMax; $argument++) { |
|
301 | - $message .= ($argument > 0 ? ', ' : ''). |
|
302 | - (is_object($backtrace[$level]['args'][$argument]) ? 'class '.get_class($backtrace[$level]['args'][$argument]) : json_encode($backtrace[$level]['args'][$argument])); |
|
303 | - } |
|
304 | - } |
|
305 | - $message .= ')'; |
|
306 | - $this->log($message, $line, $file, 'error'); |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - public function emailError($queryString, $error, $line, $file) |
|
311 | - { |
|
312 | - $subject = php_uname('n').' MySQLi Error '.$queryString; |
|
313 | - if (class_exists('\\TFSmarty')) { |
|
314 | - $smarty = new \TFSmarty(); |
|
315 | - $smarty->assign([ |
|
316 | - 'type' => $this->type, |
|
317 | - 'queryString' => $queryString, |
|
318 | - 'error' => $error, |
|
319 | - 'line' => $line, |
|
320 | - 'file' => $file, |
|
321 | - 'request' => $_REQUEST, |
|
322 | - 'server' => $_SERVER, |
|
323 | - ]); |
|
324 | - if (isset($GLOBALS['tf'])) { |
|
325 | - $smarty->assign('account_id', $GLOBALS['tf']->session->account_id); |
|
326 | - } |
|
327 | - $email = $smarty->fetch('email/admin/sql_error.tpl'); |
|
328 | - (new \MyAdmin\Mail())->adminMail($subject, $email, '[email protected]', ''); |
|
329 | - (new \MyAdmin\Mail())->adminMail($subject, $email, '[email protected]', ''); |
|
330 | - } |
|
331 | - $this->haltmsg('Invalid SQL: '.$queryString, $line, $file); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @param mixed $msg |
|
336 | - * @param string $line |
|
337 | - * @param string $file |
|
338 | - * @return mixed|void |
|
339 | - */ |
|
340 | - public function haltmsg($msg, $line = '', $file = '') |
|
341 | - { |
|
342 | - $email = "DB Error {$msg} {$file}:{$line}"; |
|
343 | - if (class_exists('\\MyAdmin\Mail')) { |
|
344 | - \MyAdmin\Mail::failsafeMail($email, $email, ['[email protected]', '[email protected]']); |
|
345 | - return; |
|
346 | - } |
|
347 | - $this->log("Database error: $msg", $line, $file, 'error'); |
|
348 | - if ($this->Errno != '0' || !in_array($this->Error, ['', '()'])) { |
|
349 | - $sqlstate = mysqli_sqlstate($this->linkId); |
|
350 | - $this->log("MySQLi SQLState: {$sqlstate}. Error: ".$this->Errno.' ('.$this->Error.')', $line, $file, 'error'); |
|
351 | - } |
|
352 | - $this->logBackTrace($msg, $line, $file); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * @return array |
|
357 | - */ |
|
358 | - public function indexNames() |
|
359 | - { |
|
360 | - return []; |
|
361 | - } |
|
362 | - |
|
363 | - |
|
364 | - /** |
|
365 | - * Add query to logged queries. |
|
366 | - * @param string $statement |
|
367 | - * @param float $time Elapsed seconds with microseconds |
|
368 | - * @param string|int $line Line Number |
|
369 | - * @param string $file File Name |
|
370 | - */ |
|
371 | - public function addLog($statement, $time, $line = '', $file = '') |
|
372 | - { |
|
373 | - $query = [ |
|
374 | - 'statement' => $statement, |
|
375 | - 'time' => $time * 1000 |
|
376 | - ]; |
|
377 | - if ($line != '') { |
|
378 | - $query['line'] = $line; |
|
379 | - } |
|
380 | - if ($file != '') { |
|
381 | - $query['file'] = $file; |
|
382 | - } |
|
383 | - if (!isset($GLOBALS['db_queries'])) { |
|
384 | - $GLOBALS['db_queries'] = []; |
|
385 | - } |
|
386 | - $GLOBALS['db_queries'][] = $query; |
|
387 | - array_push($this->log, $query); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Return logged queries. |
|
392 | - * @return array Logged queries |
|
393 | - */ |
|
394 | - public function getLog() |
|
395 | - { |
|
396 | - return $this->log; |
|
397 | - } |
|
17 | + /* public: connection parameters */ |
|
18 | + public $host = 'localhost'; |
|
19 | + public $database = ''; |
|
20 | + public $user = ''; |
|
21 | + public $password = ''; |
|
22 | + public $port = ''; |
|
23 | + |
|
24 | + /* public: configuration parameters */ |
|
25 | + public $autoStripslashes = false; |
|
26 | + public $Debug = 0; // Set to 1 for debugging messages. |
|
27 | + public $haltOnError = 'yes'; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning) |
|
28 | + |
|
29 | + public $maxConnectErrors = 5; |
|
30 | + public $connectionAttempt = 0; |
|
31 | + public $maxMatches = 10000000; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var int |
|
35 | + */ |
|
36 | + public $autoFree = 0; // Set to 1 for automatic mysql_free_result() |
|
37 | + |
|
38 | + /* public: result array and current row number */ |
|
39 | + public $Record = []; |
|
40 | + public $Row; |
|
41 | + |
|
42 | + /* public: current error number and error text */ |
|
43 | + public $Errno = 0; |
|
44 | + public $Error = ''; |
|
45 | + |
|
46 | + /* public: this is an api revision, not a CVS revision. */ |
|
47 | + public $type = 'generic'; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var int|object |
|
51 | + */ |
|
52 | + public $linkId = 0; |
|
53 | + public $queryId = 0; |
|
54 | + |
|
55 | + public $characterSet = 'utf8mb4'; |
|
56 | + public $collation = 'utf8mb4_unicode_ci'; |
|
57 | + |
|
58 | + /** |
|
59 | + * Logged queries. |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + protected $log = []; |
|
63 | + |
|
64 | + /** |
|
65 | + * Constructs the db handler, can optionally specify connection parameters |
|
66 | + * |
|
67 | + * @param string $database Optional The database name |
|
68 | + * @param string $user Optional The username to connect with |
|
69 | + * @param string $password Optional The password to use |
|
70 | + * @param string $host Optional The hostname where the server is, or default to localhost |
|
71 | + * @param string $query Optional query to perform immediately |
|
72 | + * @param string $port optional port for the connection |
|
73 | + */ |
|
74 | + public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = '') |
|
75 | + { |
|
76 | + $this->database = $database; |
|
77 | + $this->user = $user; |
|
78 | + $this->password = $password; |
|
79 | + $this->host = $host; |
|
80 | + $this->port = $port; |
|
81 | + if ($query != '') { |
|
82 | + $this->query($query); |
|
83 | + } |
|
84 | + $this->connection_atttempt = 0; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @param string $message |
|
89 | + * @param string $line |
|
90 | + * @param string $file |
|
91 | + * @return void |
|
92 | + */ |
|
93 | + public function log($message, $line = '', $file = '', $level = 'info') |
|
94 | + { |
|
95 | + error_log($message); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @return int|object |
|
100 | + */ |
|
101 | + public function linkId() |
|
102 | + { |
|
103 | + return $this->linkId; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return int|object |
|
108 | + */ |
|
109 | + public function queryId() |
|
110 | + { |
|
111 | + return $this->queryId; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param $string |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + public function real_escape($string = '') |
|
119 | + { |
|
120 | + if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
121 | + return $this->escape($string); |
|
122 | + } |
|
123 | + return mysqli_real_escape_string($this->linkId, $string); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param $string |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + public function escape($string = '') |
|
131 | + { |
|
132 | + //if (function_exists('mysql_escape_string')) |
|
133 | + //return mysql_escape_string($string); |
|
134 | + return str_replace(['\\', "\0", "\n", "\r", "'", '"', "\x1a"], ['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'], $string); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param mixed $str |
|
139 | + * @return string |
|
140 | + */ |
|
141 | + public function dbAddslashes($str = '') |
|
142 | + { |
|
143 | + if (!isset($str) || $str == '') { |
|
144 | + return ''; |
|
145 | + } |
|
146 | + return addslashes($str); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Db::toTimestamp() |
|
151 | + * @param mixed $epoch |
|
152 | + * @return bool|string |
|
153 | + */ |
|
154 | + public function toTimestamp($epoch) |
|
155 | + { |
|
156 | + return date('Y-m-d H:i:s', is_float($epoch) ? intval($epoch) : $epoch); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Db::fromTimestamp() |
|
161 | + * @param mixed $timestamp |
|
162 | + * @return bool|int|mixed |
|
163 | + */ |
|
164 | + public function fromTimestamp($timestamp) |
|
165 | + { |
|
166 | + if (preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $timestamp, $parts)) { |
|
167 | + $time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
|
168 | + } elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) { |
|
169 | + $time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
|
170 | + } elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) { |
|
171 | + $time = mktime(1, 1, 1, $parts[2], $parts[3], $parts[1]); |
|
172 | + } elseif (is_numeric($timestamp) && $timestamp >= 943938000) { |
|
173 | + $time = $timestamp; |
|
174 | + } else { |
|
175 | + $this->log('Cannot Match Timestamp from '.$timestamp, __LINE__, __FILE__); |
|
176 | + $time = false; |
|
177 | + } |
|
178 | + return $time; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * perform a query with limited result set |
|
183 | + * |
|
184 | + * @param string $queryString |
|
185 | + * @param string|int $numRows |
|
186 | + * @param int $offset |
|
187 | + * @param string|int $line |
|
188 | + * @param string $file |
|
189 | + * @return mixed |
|
190 | + */ |
|
191 | + public function limitQuery($queryString, $numRows = '', $offset = 0, $line = '', $file = '') |
|
192 | + { |
|
193 | + if (!$numRows) { |
|
194 | + $numRows = $this->maxMatches; |
|
195 | + } |
|
196 | + if ($offset == 0) { |
|
197 | + $queryString .= ' LIMIT '.(int) $numRows; |
|
198 | + } else { |
|
199 | + $queryString .= ' LIMIT '.(int) $offset.','.(int) $numRows; |
|
200 | + } |
|
201 | + |
|
202 | + if ($this->Debug) { |
|
203 | + printf("Debug: limitQuery = %s<br>offset=%d, num_rows=%d<br>\n", $queryString, $offset, $numRows); |
|
204 | + } |
|
205 | + |
|
206 | + return $this->query($queryString, $line, $file); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * db:qr() |
|
211 | + * |
|
212 | + * alias of queryReturn() |
|
213 | + * |
|
214 | + * @param mixed $query SQL Query to be used |
|
215 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
216 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
217 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
218 | + */ |
|
219 | + public function qr($query, $line = '', $file = '') |
|
220 | + { |
|
221 | + return $this->queryReturn($query, $line, $file); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * gets a field |
|
226 | + * |
|
227 | + * @param mixed $name |
|
228 | + * @param string $stripSlashes |
|
229 | + * @return string |
|
230 | + */ |
|
231 | + public function f($name, $stripSlashes = '') |
|
232 | + { |
|
233 | + if (is_null($this->Record)) { |
|
234 | + return null; |
|
235 | + } elseif ($stripSlashes || ($this->autoStripslashes && !$stripSlashes)) { |
|
236 | + return stripslashes($this->Record[$name]); |
|
237 | + } else { |
|
238 | + return $this->Record[$name]; |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * error handling |
|
244 | + * |
|
245 | + * @param mixed $msg |
|
246 | + * @param string $line |
|
247 | + * @param string $file |
|
248 | + * @return void |
|
249 | + */ |
|
250 | + public function halt($msg, $line = '', $file = '') |
|
251 | + { |
|
252 | + $this->unlock(false); |
|
253 | + /* Just in case there is a table currently locked */ |
|
254 | + |
|
255 | + //$this->Error = @$this->linkId->error; |
|
256 | + //$this->Errno = @$this->linkId->errno; |
|
257 | + if ($this->haltOnError == 'no') { |
|
258 | + return true; |
|
259 | + } |
|
260 | + if ($msg != '') { |
|
261 | + $this->haltmsg($msg, $line, $file); |
|
262 | + } |
|
263 | + if ($this->haltOnError != 'report') { |
|
264 | + echo '<p><b>Session halted.</b>'; |
|
265 | + //if (isset($GLOBALS['tf'])) |
|
266 | + //$GLOBALS['tf']->terminate(); |
|
267 | + die(); |
|
268 | + } |
|
269 | + return true; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * @param mixed $msg |
|
274 | + * @param string $line |
|
275 | + * @param string $file |
|
276 | + * @return mixed|void |
|
277 | + */ |
|
278 | + public function logBackTrace($msg, $line = '', $file = '') |
|
279 | + { |
|
280 | + $backtrace = (function_exists('debug_backtrace') ? debug_backtrace() : []); |
|
281 | + $this->log( |
|
282 | + ('' !== getenv('REQUEST_URI') ? ' '.getenv('REQUEST_URI') : ''). |
|
283 | + ((isset($_POST) && count($_POST)) ? ' POST='.json_encode($_POST) : ''). |
|
284 | + ((isset($_GET) && count($_GET)) ? ' GET='.json_encode($_GET) : ''). |
|
285 | + ((isset($_FILES) && count($_FILES)) ? ' FILES='.json_encode($_FILES) : ''). |
|
286 | + ('' !== getenv('HTTP_USER_AGENT') ? ' AGENT="'.getenv('HTTP_USER_AGENT').'"' : ''). |
|
287 | + (isset($_SERVER['REQUEST_METHOD']) ? ' METHOD="'.$_SERVER['REQUEST_METHOD'].'"'. |
|
288 | + ($_SERVER['REQUEST_METHOD'] === 'POST' ? ' POST="'.json_encode($_POST).'"' : '') : ''), |
|
289 | + $line, |
|
290 | + $file, |
|
291 | + 'error' |
|
292 | + ); |
|
293 | + for ($level = 1, $levelMax = count($backtrace); $level < $levelMax; $level++) { |
|
294 | + $message = (isset($backtrace[$level]['file']) ? 'File: '.$backtrace[$level]['file'] : ''). |
|
295 | + (isset($backtrace[$level]['line']) ? ' Line: '.$backtrace[$level]['line'] : ''). |
|
296 | + ' Function: '.(isset($backtrace[$level] ['class']) ? '(class '.$backtrace[$level] ['class'].') ' : ''). |
|
297 | + (isset($backtrace[$level] ['type']) ? $backtrace[$level] ['type'].' ' : ''). |
|
298 | + $backtrace[$level] ['function'].'('; |
|
299 | + if (isset($backtrace[$level] ['args'])) { |
|
300 | + for ($argument = 0, $argumentMax = count($backtrace[$level]['args']); $argument < $argumentMax; $argument++) { |
|
301 | + $message .= ($argument > 0 ? ', ' : ''). |
|
302 | + (is_object($backtrace[$level]['args'][$argument]) ? 'class '.get_class($backtrace[$level]['args'][$argument]) : json_encode($backtrace[$level]['args'][$argument])); |
|
303 | + } |
|
304 | + } |
|
305 | + $message .= ')'; |
|
306 | + $this->log($message, $line, $file, 'error'); |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + public function emailError($queryString, $error, $line, $file) |
|
311 | + { |
|
312 | + $subject = php_uname('n').' MySQLi Error '.$queryString; |
|
313 | + if (class_exists('\\TFSmarty')) { |
|
314 | + $smarty = new \TFSmarty(); |
|
315 | + $smarty->assign([ |
|
316 | + 'type' => $this->type, |
|
317 | + 'queryString' => $queryString, |
|
318 | + 'error' => $error, |
|
319 | + 'line' => $line, |
|
320 | + 'file' => $file, |
|
321 | + 'request' => $_REQUEST, |
|
322 | + 'server' => $_SERVER, |
|
323 | + ]); |
|
324 | + if (isset($GLOBALS['tf'])) { |
|
325 | + $smarty->assign('account_id', $GLOBALS['tf']->session->account_id); |
|
326 | + } |
|
327 | + $email = $smarty->fetch('email/admin/sql_error.tpl'); |
|
328 | + (new \MyAdmin\Mail())->adminMail($subject, $email, '[email protected]', ''); |
|
329 | + (new \MyAdmin\Mail())->adminMail($subject, $email, '[email protected]', ''); |
|
330 | + } |
|
331 | + $this->haltmsg('Invalid SQL: '.$queryString, $line, $file); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @param mixed $msg |
|
336 | + * @param string $line |
|
337 | + * @param string $file |
|
338 | + * @return mixed|void |
|
339 | + */ |
|
340 | + public function haltmsg($msg, $line = '', $file = '') |
|
341 | + { |
|
342 | + $email = "DB Error {$msg} {$file}:{$line}"; |
|
343 | + if (class_exists('\\MyAdmin\Mail')) { |
|
344 | + \MyAdmin\Mail::failsafeMail($email, $email, ['[email protected]', '[email protected]']); |
|
345 | + return; |
|
346 | + } |
|
347 | + $this->log("Database error: $msg", $line, $file, 'error'); |
|
348 | + if ($this->Errno != '0' || !in_array($this->Error, ['', '()'])) { |
|
349 | + $sqlstate = mysqli_sqlstate($this->linkId); |
|
350 | + $this->log("MySQLi SQLState: {$sqlstate}. Error: ".$this->Errno.' ('.$this->Error.')', $line, $file, 'error'); |
|
351 | + } |
|
352 | + $this->logBackTrace($msg, $line, $file); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * @return array |
|
357 | + */ |
|
358 | + public function indexNames() |
|
359 | + { |
|
360 | + return []; |
|
361 | + } |
|
362 | + |
|
363 | + |
|
364 | + /** |
|
365 | + * Add query to logged queries. |
|
366 | + * @param string $statement |
|
367 | + * @param float $time Elapsed seconds with microseconds |
|
368 | + * @param string|int $line Line Number |
|
369 | + * @param string $file File Name |
|
370 | + */ |
|
371 | + public function addLog($statement, $time, $line = '', $file = '') |
|
372 | + { |
|
373 | + $query = [ |
|
374 | + 'statement' => $statement, |
|
375 | + 'time' => $time * 1000 |
|
376 | + ]; |
|
377 | + if ($line != '') { |
|
378 | + $query['line'] = $line; |
|
379 | + } |
|
380 | + if ($file != '') { |
|
381 | + $query['file'] = $file; |
|
382 | + } |
|
383 | + if (!isset($GLOBALS['db_queries'])) { |
|
384 | + $GLOBALS['db_queries'] = []; |
|
385 | + } |
|
386 | + $GLOBALS['db_queries'][] = $query; |
|
387 | + array_push($this->log, $query); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Return logged queries. |
|
392 | + * @return array Logged queries |
|
393 | + */ |
|
394 | + public function getLog() |
|
395 | + { |
|
396 | + return $this->log; |
|
397 | + } |
|
398 | 398 | } |
@@ -12,8 +12,7 @@ discard block |
||
12 | 12 | /** |
13 | 13 | * Class Generic |
14 | 14 | */ |
15 | -abstract class Generic |
|
16 | -{ |
|
15 | +abstract class Generic { |
|
17 | 16 | /* public: connection parameters */ |
18 | 17 | public $host = 'localhost'; |
19 | 18 | public $database = ''; |
@@ -71,8 +70,7 @@ discard block |
||
71 | 70 | * @param string $query Optional query to perform immediately |
72 | 71 | * @param string $port optional port for the connection |
73 | 72 | */ |
74 | - public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = '') |
|
75 | - { |
|
73 | + public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = '') { |
|
76 | 74 | $this->database = $database; |
77 | 75 | $this->user = $user; |
78 | 76 | $this->password = $password; |
@@ -90,24 +88,21 @@ discard block |
||
90 | 88 | * @param string $file |
91 | 89 | * @return void |
92 | 90 | */ |
93 | - public function log($message, $line = '', $file = '', $level = 'info') |
|
94 | - { |
|
91 | + public function log($message, $line = '', $file = '', $level = 'info') { |
|
95 | 92 | error_log($message); |
96 | 93 | } |
97 | 94 | |
98 | 95 | /** |
99 | 96 | * @return int|object |
100 | 97 | */ |
101 | - public function linkId() |
|
102 | - { |
|
98 | + public function linkId() { |
|
103 | 99 | return $this->linkId; |
104 | 100 | } |
105 | 101 | |
106 | 102 | /** |
107 | 103 | * @return int|object |
108 | 104 | */ |
109 | - public function queryId() |
|
110 | - { |
|
105 | + public function queryId() { |
|
111 | 106 | return $this->queryId; |
112 | 107 | } |
113 | 108 | |
@@ -115,8 +110,7 @@ discard block |
||
115 | 110 | * @param $string |
116 | 111 | * @return string |
117 | 112 | */ |
118 | - public function real_escape($string = '') |
|
119 | - { |
|
113 | + public function real_escape($string = '') { |
|
120 | 114 | if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
121 | 115 | return $this->escape($string); |
122 | 116 | } |
@@ -127,8 +121,7 @@ discard block |
||
127 | 121 | * @param $string |
128 | 122 | * @return string |
129 | 123 | */ |
130 | - public function escape($string = '') |
|
131 | - { |
|
124 | + public function escape($string = '') { |
|
132 | 125 | //if (function_exists('mysql_escape_string')) |
133 | 126 | //return mysql_escape_string($string); |
134 | 127 | return str_replace(['\\', "\0", "\n", "\r", "'", '"', "\x1a"], ['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'], $string); |
@@ -138,8 +131,7 @@ discard block |
||
138 | 131 | * @param mixed $str |
139 | 132 | * @return string |
140 | 133 | */ |
141 | - public function dbAddslashes($str = '') |
|
142 | - { |
|
134 | + public function dbAddslashes($str = '') { |
|
143 | 135 | if (!isset($str) || $str == '') { |
144 | 136 | return ''; |
145 | 137 | } |
@@ -151,8 +143,7 @@ discard block |
||
151 | 143 | * @param mixed $epoch |
152 | 144 | * @return bool|string |
153 | 145 | */ |
154 | - public function toTimestamp($epoch) |
|
155 | - { |
|
146 | + public function toTimestamp($epoch) { |
|
156 | 147 | return date('Y-m-d H:i:s', is_float($epoch) ? intval($epoch) : $epoch); |
157 | 148 | } |
158 | 149 | |
@@ -161,8 +152,7 @@ discard block |
||
161 | 152 | * @param mixed $timestamp |
162 | 153 | * @return bool|int|mixed |
163 | 154 | */ |
164 | - public function fromTimestamp($timestamp) |
|
165 | - { |
|
155 | + public function fromTimestamp($timestamp) { |
|
166 | 156 | if (preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $timestamp, $parts)) { |
167 | 157 | $time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
168 | 158 | } elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) { |
@@ -188,8 +178,7 @@ discard block |
||
188 | 178 | * @param string $file |
189 | 179 | * @return mixed |
190 | 180 | */ |
191 | - public function limitQuery($queryString, $numRows = '', $offset = 0, $line = '', $file = '') |
|
192 | - { |
|
181 | + public function limitQuery($queryString, $numRows = '', $offset = 0, $line = '', $file = '') { |
|
193 | 182 | if (!$numRows) { |
194 | 183 | $numRows = $this->maxMatches; |
195 | 184 | } |
@@ -216,8 +205,7 @@ discard block |
||
216 | 205 | * @param string $file optionally pass __FILE__ calling the query for logging |
217 | 206 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
218 | 207 | */ |
219 | - public function qr($query, $line = '', $file = '') |
|
220 | - { |
|
208 | + public function qr($query, $line = '', $file = '') { |
|
221 | 209 | return $this->queryReturn($query, $line, $file); |
222 | 210 | } |
223 | 211 | |
@@ -228,8 +216,7 @@ discard block |
||
228 | 216 | * @param string $stripSlashes |
229 | 217 | * @return string |
230 | 218 | */ |
231 | - public function f($name, $stripSlashes = '') |
|
232 | - { |
|
219 | + public function f($name, $stripSlashes = '') { |
|
233 | 220 | if (is_null($this->Record)) { |
234 | 221 | return null; |
235 | 222 | } elseif ($stripSlashes || ($this->autoStripslashes && !$stripSlashes)) { |
@@ -247,8 +234,7 @@ discard block |
||
247 | 234 | * @param string $file |
248 | 235 | * @return void |
249 | 236 | */ |
250 | - public function halt($msg, $line = '', $file = '') |
|
251 | - { |
|
237 | + public function halt($msg, $line = '', $file = '') { |
|
252 | 238 | $this->unlock(false); |
253 | 239 | /* Just in case there is a table currently locked */ |
254 | 240 | |
@@ -275,8 +261,7 @@ discard block |
||
275 | 261 | * @param string $file |
276 | 262 | * @return mixed|void |
277 | 263 | */ |
278 | - public function logBackTrace($msg, $line = '', $file = '') |
|
279 | - { |
|
264 | + public function logBackTrace($msg, $line = '', $file = '') { |
|
280 | 265 | $backtrace = (function_exists('debug_backtrace') ? debug_backtrace() : []); |
281 | 266 | $this->log( |
282 | 267 | ('' !== getenv('REQUEST_URI') ? ' '.getenv('REQUEST_URI') : ''). |
@@ -307,8 +292,7 @@ discard block |
||
307 | 292 | } |
308 | 293 | } |
309 | 294 | |
310 | - public function emailError($queryString, $error, $line, $file) |
|
311 | - { |
|
295 | + public function emailError($queryString, $error, $line, $file) { |
|
312 | 296 | $subject = php_uname('n').' MySQLi Error '.$queryString; |
313 | 297 | if (class_exists('\\TFSmarty')) { |
314 | 298 | $smarty = new \TFSmarty(); |
@@ -337,8 +321,7 @@ discard block |
||
337 | 321 | * @param string $file |
338 | 322 | * @return mixed|void |
339 | 323 | */ |
340 | - public function haltmsg($msg, $line = '', $file = '') |
|
341 | - { |
|
324 | + public function haltmsg($msg, $line = '', $file = '') { |
|
342 | 325 | $email = "DB Error {$msg} {$file}:{$line}"; |
343 | 326 | if (class_exists('\\MyAdmin\Mail')) { |
344 | 327 | \MyAdmin\Mail::failsafeMail($email, $email, ['[email protected]', '[email protected]']); |
@@ -355,8 +338,7 @@ discard block |
||
355 | 338 | /** |
356 | 339 | * @return array |
357 | 340 | */ |
358 | - public function indexNames() |
|
359 | - { |
|
341 | + public function indexNames() { |
|
360 | 342 | return []; |
361 | 343 | } |
362 | 344 | |
@@ -368,8 +350,7 @@ discard block |
||
368 | 350 | * @param string|int $line Line Number |
369 | 351 | * @param string $file File Name |
370 | 352 | */ |
371 | - public function addLog($statement, $time, $line = '', $file = '') |
|
372 | - { |
|
353 | + public function addLog($statement, $time, $line = '', $file = '') { |
|
373 | 354 | $query = [ |
374 | 355 | 'statement' => $statement, |
375 | 356 | 'time' => $time * 1000 |
@@ -391,8 +372,7 @@ discard block |
||
391 | 372 | * Return logged queries. |
392 | 373 | * @return array Logged queries |
393 | 374 | */ |
394 | - public function getLog() |
|
395 | - { |
|
375 | + public function getLog() { |
|
396 | 376 | return $this->log; |
397 | 377 | } |
398 | 378 | } |