1 | <?php |
||
20 | class YDB extends ExtendedPdo { |
||
21 | |||
22 | /** |
||
23 | * Debug mode, default false |
||
24 | * @var bool |
||
25 | */ |
||
26 | protected $debug = false; |
||
27 | |||
28 | /** |
||
29 | * Page context (ie "infos", "bookmark", "plugins"...) |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $context = ''; |
||
33 | |||
34 | /** |
||
35 | * Information related to a short URL keyword (eg timestamp, long URL, ...) |
||
36 | * |
||
37 | * @var array |
||
38 | * |
||
39 | */ |
||
40 | protected $infos = array(); |
||
41 | |||
42 | /** |
||
43 | * Is YOURLS installed and ready to run? |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $installed = false; |
||
47 | |||
48 | /** |
||
49 | * Options |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $option = array(); |
||
53 | |||
54 | /** |
||
55 | * Plugin admin pages informations |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $plugin_pages = array(); |
||
59 | |||
60 | /** |
||
61 | * Plugin informations |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $plugins = array(); |
||
65 | |||
66 | /** |
||
67 | * Class constructor |
||
68 | * |
||
69 | * Don't forget to end with a call to the parent constructor |
||
70 | * |
||
71 | * @since 1.7.3 |
||
72 | * @param string $dsn The data source name |
||
73 | * @param string $user The username |
||
74 | * @param string $pass The password |
||
75 | * @param array $options Driver-specific options |
||
76 | * @param array $attributes Attributes to set after a connection |
||
77 | */ |
||
78 | public function __construct($dsn, $user, $pass, $options, $attributes) { |
||
79 | parent::__construct($dsn, $user, $pass, $options, $attributes); |
||
80 | |||
81 | // Log query infos |
||
82 | $this->start_profiler(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Initiate real connection to DB server |
||
87 | * |
||
88 | * This is to check that the server is running and/or the config is OK |
||
89 | * |
||
90 | * @since 1.7.3 |
||
91 | * @return void |
||
92 | */ |
||
93 | public function connect_to_DB() { |
||
94 | try { |
||
95 | $this->connect(); |
||
96 | } catch ( \Exception $e ) { |
||
97 | // Use any /user/db_error.php file |
||
98 | if( file_exists( YOURLS_USERDIR . '/db_error.php' ) ) { |
||
99 | include_once( YOURLS_USERDIR . '/db_error.php' ); |
||
100 | die(); |
||
101 | } |
||
102 | |||
103 | $message = yourls__( 'Incorrect DB config, or could not connect to DB' ); |
||
104 | $message .= '<br/>' . get_class($e) .': ' . $e->getMessage(); |
||
105 | |||
106 | yourls_die( yourls__( $message ), yourls__( 'Fatal error' ), 503 ); |
||
107 | die(); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Start a Message Logger |
||
113 | * |
||
114 | * @since 1.7.3 |
||
115 | * @see \YOURLS\Admin\Logger |
||
116 | * @see \Aura\Sql\Profiler |
||
117 | * @return void |
||
118 | */ |
||
119 | public function start_profiler() { |
||
122 | |||
123 | /** |
||
124 | * @param string $context |
||
125 | */ |
||
126 | public function set_html_context($context) { |
||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | */ |
||
133 | public function get_html_context() { |
||
136 | |||
137 | // Options low level functions, see \YOURLS\Database\Options |
||
138 | |||
139 | /** |
||
140 | * @param string $name |
||
141 | * @param mixed $value |
||
142 | */ |
||
143 | public function set_option($name, $value) { |
||
146 | |||
147 | /** |
||
148 | * @param string $name |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function has_option($name) { |
||
154 | |||
155 | /** |
||
156 | * @param string $name |
||
157 | * @return string |
||
158 | */ |
||
159 | public function get_option($name) { |
||
162 | |||
163 | /** |
||
164 | * @param string $name |
||
165 | */ |
||
166 | public function delete_option($name) { |
||
169 | |||
170 | |||
171 | // Infos (related to keyword) low level functions |
||
172 | |||
173 | /** |
||
174 | * @param string $keyword |
||
175 | * @param mixed $infos |
||
176 | */ |
||
177 | public function set_infos($keyword, $infos) { |
||
180 | |||
181 | /** |
||
182 | * @param string $keyword |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function has_infos($keyword) { |
||
188 | |||
189 | /** |
||
190 | * @param string $keyword |
||
191 | * @return array |
||
192 | */ |
||
193 | public function get_infos($keyword) { |
||
196 | |||
197 | /** |
||
198 | * @param string $keyword |
||
199 | */ |
||
200 | public function delete_infos($keyword) { |
||
203 | |||
204 | /** |
||
205 | * @todo: infos & options are working the same way here. Abstract this. |
||
206 | */ |
||
207 | |||
208 | |||
209 | // Plugin low level functions, see functions-plugins.php |
||
210 | |||
211 | /** |
||
212 | * @return array |
||
213 | */ |
||
214 | public function get_plugins() { |
||
217 | |||
218 | /** |
||
219 | * @param array $plugins |
||
220 | */ |
||
221 | public function set_plugins(array $plugins) { |
||
224 | |||
225 | /** |
||
226 | * @param string $plugin plugin filename |
||
227 | */ |
||
228 | public function add_plugin($plugin) { |
||
231 | |||
232 | /** |
||
233 | * @param string $plugin plugin filename |
||
234 | */ |
||
235 | public function remove_plugin($plugin) { |
||
238 | |||
239 | |||
240 | // Plugin Pages low level functions, see functions-plugins.php |
||
241 | |||
242 | /** |
||
243 | * @return array |
||
244 | */ |
||
245 | public function get_plugin_pages() { |
||
248 | |||
249 | /** |
||
250 | * @param array $pages |
||
251 | */ |
||
252 | public function set_plugin_pages(array $pages) { |
||
255 | |||
256 | /** |
||
257 | * @param string $slug |
||
258 | * @param string $title |
||
259 | * @param callable $function |
||
260 | */ |
||
261 | public function add_plugin_page($slug, $title, $function) { |
||
268 | |||
269 | /** |
||
270 | * @param string $slug |
||
271 | */ |
||
272 | public function remove_plugin_page($slug) { |
||
275 | |||
276 | |||
277 | /** |
||
278 | * Return count of SQL queries performed |
||
279 | * |
||
280 | * @since 1.7.3 |
||
281 | * @return int |
||
282 | */ |
||
283 | public function get_num_queries() { |
||
286 | |||
287 | /** |
||
288 | * Return SQL queries performed |
||
289 | * |
||
290 | * Aura\Sql\Profiler logs every PDO command issued. But depending on PDO::ATTR_EMULATE_PREPARES, some are |
||
291 | * actually sent to the mysql server or not : |
||
292 | * - if PDO::ATTR_EMULATE_PREPARES is true, prepare() statements are not sent to the server and are performed |
||
293 | * internally, so they are removed from the logger |
||
294 | * - if PDO::ATTR_EMULATE_PREPARES is false, prepare() statements are actually performed by the mysql server, |
||
295 | * and count as an actual query |
||
296 | * |
||
297 | * Resulting array is something like: |
||
298 | * array ( |
||
299 | * 0 => array ( |
||
300 | * 'duration' => 1.0010569095611572265625, |
||
301 | * 'function' => 'connect', |
||
302 | * 'statement' => NULL, |
||
303 | * 'bind_values' => array (), |
||
304 | * 'trace' => ...back trace..., |
||
305 | * ), |
||
306 | * // key index might not be sequential if 'prepare' function are filtered out |
||
307 | * 2 => array ( |
||
308 | * 'duration' => 0.000999927520751953125, |
||
309 | * 'function' => 'perform', |
||
310 | * 'statement' => 'SELECT option_value FROM yourls_options WHERE option_name = :option_name LIMIT 1', |
||
311 | * 'bind_values' => array ( 'option_name' => 'test_option' ), |
||
312 | * 'trace' => ...back trace..., |
||
313 | * ), |
||
314 | * ); |
||
315 | * |
||
316 | * @since 1.7.3 |
||
317 | * @return array |
||
318 | */ |
||
319 | public function get_queries() { |
||
329 | |||
330 | /** |
||
331 | * Set YOURLS installed state |
||
332 | * |
||
333 | * @since 1.7.3 |
||
334 | * @param bool $bool |
||
335 | * @return void |
||
336 | */ |
||
337 | public function set_installed($bool) { |
||
340 | |||
341 | /** |
||
342 | * Get YOURLS installed state |
||
343 | * |
||
344 | * @since 1.7.3 |
||
345 | * @return bool |
||
346 | */ |
||
347 | public function is_installed() { |
||
350 | |||
351 | /** |
||
352 | * Return standardized DB version |
||
353 | * |
||
354 | * The regex removes everything that's not a number at the start of the string, or remove anything that's not a number and what |
||
355 | * follows after that. |
||
356 | * 'omgmysql-5.5-ubuntu-4.20' => '5.5' |
||
357 | * 'mysql5.5-ubuntu-4.20' => '5.5' |
||
358 | * '5.5-ubuntu-4.20' => '5.5' |
||
359 | * '5.5-beta2' => '5.5' |
||
360 | * '5.5' => '5.5' |
||
361 | * |
||
362 | * @since 1.7.3 |
||
363 | * @return string |
||
364 | */ |
||
365 | public function mysql_version() { |
||
369 | |||
370 | /** |
||
371 | * Deprecated properties since 1.7.3, unused in 3rd party plugins as far as I know |
||
372 | * |
||
373 | * $ydb->DB_driver |
||
374 | * $ydb->captured_errors |
||
375 | * $ydb->dbh |
||
376 | * $ydb->result |
||
377 | * $ydb->rows_affected |
||
378 | * $ydb->show_errors |
||
379 | */ |
||
380 | |||
381 | /** |
||
382 | * Deprecated functions since 1.7.3 |
||
383 | */ |
||
384 | |||
385 | // @codeCoverageIgnoreStart |
||
386 | |||
387 | public function escape($string) { |
||
392 | |||
393 | public function get_col($query) { |
||
398 | |||
399 | public function get_results($query) { |
||
405 | |||
406 | public function get_row($query) { |
||
412 | |||
413 | public function get_var($query) { |
||
418 | |||
419 | public function query($query) { |
||
424 | // @codeCoverageIgnoreEnd |
||
425 | } |
||
426 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.