Fix DB_MySQL_PDO::escape_binary().
[bugdar.git] / framework / db_mysql_pdo.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Abstract Database Layer
24 * db.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * PDO MySQL Driver
31 *
32 * This class wraps PDO-MySQL in the old DB_Abstract ISSO API.
33 *
34 * Constants:
35 * ISSO_SHOW_QUERIES_LIVE - Show queries in page output as they are sent
36 *
37 * @author Blue Static
38 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
39 * @version $Revision$
40 * @package ISSO
41 *
42 */
43 class DB_MySQL_PDO
44 {
45 /**
46 * Framework registry object
47 * @var object
48 * @access protected
49 */
50 var $registry = null;
51
52 /**
53 * Determines whether or not errors should be shown
54 * @var bool
55 * @access public
56 */
57 var $showerrors = true;
58
59 /**
60 * Current error number
61 * @var integer
62 * @access protected
63 */
64 var $errnum = 0;
65
66 /**
67 * Description of current error
68 * @var string
69 * @access protected
70 */
71 var $errstr = '';
72
73 /**
74 * Currend open MySQL connexion
75 * @var resource
76 * @access protected
77 */
78 var $dblink = null;
79
80 /**
81 * Current query ID
82 * @var integer
83 * @access protected
84 */
85 var $result = null;
86
87 /**
88 * Current query string
89 * @var string
90 * @access protected
91 */
92 var $querystr = '';
93
94 /**
95 * History of all executed queryies
96 * @var array
97 * @access protected
98 */
99 var $history = array();
100
101 // ###################################################################
102 /**
103 * Constructor
104 */
105 function __construct(&$registry)
106 {
107 $this->registry =& $registry;
108 }
109
110 // ###################################################################
111 /**
112 * Connect to a the specified database
113 *
114 * @access public
115 *
116 * @param string Server name
117 * @param string User name
118 * @param string Password
119 * @param string Database name
120 * @param bool Use p-connect?
121 *
122 * @return bool Result of connect
123 */
124 function connect($server, $user, $password, $database, $pconnect)
125 {
126 define('ISSO_DB_LAYER', 'db_mysql_pdo');
127 if ($this->dblink == false)
128 {
129 $this->dblink = new PDO("mysql:dbname=$database;host=$server", $user, $password);
130 if ($this->dblink == false)
131 {
132 $this->error('DB-Link == false, cannot connect');
133 return false;
134 }
135 return true;
136 }
137 }
138
139 // ###################################################################
140 /**
141 * Send a query to the open database link
142 *
143 * @access public
144 *
145 * @param string Query string
146 *
147 * @return integer Result
148 */
149 function query($string)
150 {
151 $time = microtime();
152
153 $this->querystr = $string;
154 $this->result = $this->dblink->query($string);
155
156 if (!$this->result)
157 {
158 $this->error('Invalid SQL query');
159 }
160
161 $this->history[] = $history = array('query' => $string, 'time' => ($this->registry->is_loaded('functions') ? $this->registry->modules['functions']->fetch_microtime_diff($time) : 0), 'trace' => $this->registry->format_debug_trace(debug_backtrace()));
162
163 if (defined('ISSO_SHOW_QUERIES_LIVE'))
164 {
165 if (constant('ISSO_SHOW_QUERIES_LIVE'))
166 {
167 print($this->construct_query_debug($history));
168 }
169 }
170
171 return $this->result;
172 }
173
174 // ###################################################################
175 /**
176 * Escape a string (depending on character set, if supported)
177 *
178 * @access public
179 *
180 * @param string String to be escaped
181 *
182 * @return string Escaped string
183 */
184 function escape_string($string)
185 {
186 $q = $this->dblink->quote($string, PDO::PARAM_STR);
187 // This will put quotes arround the string, which is incompatible
188 // with how queries are written in Bugdar. Remove them here.
189 return substr($q, 1, -1);
190 }
191
192 // ###################################################################
193 /**
194 * Escapes a binary string for insertion into the database
195 *
196 * @access public
197 *
198 * @param string Unescaped data
199 *
200 * @return string Escaped binary data
201 */
202 function escape_binary($binary)
203 {
204 return $this->escape_string($binary);
205 }
206
207 // ###################################################################
208 /**
209 * Fetch the query result as an array
210 *
211 * @access public
212 *
213 * @param integer Result
214 * @param bool Return an associative array?
215 *
216 * @return array A row of the query result
217 */
218 function fetch_array($result, $assoc = true)
219 {
220 $style = $assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM;
221 return $result->fetch($style);
222 }
223
224 // ###################################################################
225 /**
226 * Fetch the query result as an object
227 *
228 * @access public
229 *
230 * @param integer Result
231 *
232 * @return object An object with the query result
233 */
234 function fetch_object($result)
235 {
236 return $result->fetch(PDO::FETCH_OBJ);
237 }
238
239 // ###################################################################
240 /**
241 * Send a query and return the first row of the results
242 *
243 * @access public
244 *
245 * @param string Query string
246 * @param string Result return function (in the database layer)
247 *
248 * @return mixed Results in variable formats
249 */
250 function query_first($string, $callback = 'fetch_array')
251 {
252 $resource = $this->query($string);
253 if ($resource)
254 {
255 $return = $this->$callback($resource);
256 $this->free_result($resource);
257 return $return;
258 }
259 else
260 {
261 return false;
262 }
263 }
264
265 // ###################################################################
266 /**
267 * Free the current query result
268 *
269 * @access public
270 *
271 * @param integer Result
272 */
273 function free_result($result)
274 {
275 $result->closeCursor();
276 $this->result = null;
277 $this->querystr = '';
278 }
279
280 // ###################################################################
281 /**
282 * Fetch the unique ID of the record just inserted
283 *
284 * @access public
285 *
286 * @return integer Insert-ID
287 */
288 function insert_id()
289 {
290 return $this->dblink->lastInsertId();
291 }
292
293 // ###################################################################
294 /**
295 * Fetch the number of rows in the result
296 *
297 * @access public
298 *
299 * @param integer Result
300 *
301 * @return integer Number of rows
302 */
303 function num_rows($result)
304 {
305 return $result->rowCount();
306 }
307
308 // ###################################################################
309 /**
310 * Fetch the number of rows affected by the query
311 *
312 * @access public
313 *
314 * @param integer Result
315 *
316 * @return integer Number of affected rows
317 */
318 function affected_rows($result)
319 {
320 return $result->rowCount();
321 }
322
323 // ###################################################################
324 /**
325 * Constructs a table of query information output that is used in some
326 * other modules to display a list of queries. This merely formats
327 * a DB->history array entry nicely
328 *
329 * @access public
330 *
331 * @param array An entry from DB->history
332 *
333 * @return string A formatted table block
334 */
335 function construct_query_debug($query)
336 {
337 $block = "<strong>Query:</strong>\n\n<div>" . $this->registry->entity_encode($query['query']) . "</div>\n";
338 $block .= "<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
339 $block .= "<strong>Time:</strong> $query[time]<br />\n\t\t<br />\n\t\t";
340 $block .= "<strong>Backtrace:</strong>\n\t\t<div>" . implode("<br />\n", $query['trace']) . "</div>\n\t</td>\n</tr>";
341
342 return $this->registry->message('Query Debug', $block, 1, true, false, 0);
343 }
344
345 // ###################################################################
346 /**
347 * Error wrapper for ISSO->message()
348 *
349 * @access protected
350 *
351 * @param string User defined error message
352 */
353 function error($message)
354 {
355 if ($this->showerrors)
356 {
357 if ($this->dblink)
358 {
359 $err = $this->dblink->errorInfo();
360 $this->errnum = $err[0];
361 $this->errstr = $err[1] . ': ' . $err[2];
362 }
363
364 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
365
366 $message_prepped = "<blockquote>\n<p>";
367 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr) ."</pre>\n<br />";
368 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum . "</span>\n<br />";
369 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr . "</span>\n<br />";
370 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
371 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
372 $message_prepped .= "\n</p>\n</blockquote>";
373
374 $this->registry->message('Database Error in `<em>' . $this->registry->application . '</em>`', $message_prepped, 3);
375 exit;
376 }
377 }
378 }
379