diff options
| author | s1n <[email protected]> | 2020-03-28 10:36:41 -0700 |
|---|---|---|
| committer | s1n <[email protected]> | 2020-03-28 10:36:41 -0700 |
| commit | 25b7d2aab61ae6421398d3abae5da6ffe590333d (patch) | |
| tree | 611985ec78bb2d94099c9fd5dd687f5c9cee6f3e /includes/Admin | |
| parent | Initial commit (diff) | |
| download | crack.cf-backup-master.tar.xz crack.cf-backup-master.zip | |
Diffstat (limited to 'includes/Admin')
| -rw-r--r-- | includes/Admin/Logger.php | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/includes/Admin/Logger.php b/includes/Admin/Logger.php new file mode 100644 index 0000000..ebcb102 --- /dev/null +++ b/includes/Admin/Logger.php @@ -0,0 +1,104 @@ +<?php + +/** + * YOURLS simple logger + * + * @since 1.7.3 + */ + +namespace YOURLS\Admin; + +use YOURLS\Database\YDB; + +class Logger extends \Aura\Sql\Profiler { + + /** + * the YDB instance + * @var YDB + */ + protected $ydb; + + /** + * Debug log where messages are stored + * @var array + */ + protected $debug_log = array(); + + public function __construct(YDB $ydb) { + $this->ydb = $ydb; + /** + * @todo Extend this to allow calling an external logger + */ + } + + /** + * @param string $message + */ + public function log($message) { + yourls_do_action('debug_log', $message); + $this->debug_log[] = $message; + } + + public function get_log() { + return $this->debug_log; + } + + /** + * Extends \Aura\Sql\Profiler::addProfile() to log queries in our YOURLS logger + * + * @since 1.7.3 + * @param float $duration The query duration. + * @param string $function The PDO method that made the entry. + * @param string $statement The SQL query statement. + * @param array $bind_values The values bound to the statement. + * @return void + */ + public function addProfile($duration, $function, $statement, array $bind_values = array() ) { + parent::addProfile($duration, $function, $statement, $bind_values); + + if($function == 'connect') { + $this->log( sprintf('SQL: CONNECT (%s s)', number_format($duration, 5)) ); + return; + } + + // If we are emulating prepare, don't log 'prepare' statement, as they are not actual queries sent to the server + // See YDB::get_queries() for details + if ($this->ydb->get_emulate_state() && $function !== 'prepare') { + $this->log( sprintf('SQL: %s (%s s)', $this->pretty_format($statement, $bind_values), number_format($duration, 5) ) ); + } + } + + /** + * Format PDO statement with bind/values replacement + * + * This replaces PDO binds such as 'key_name = :name' with corresponding array values, eg array('name'=>'some value') + * This is merely a cosmetic replacement to allow for readability: the result WILL NOT be valid SQL! (eg no proper quotes) + * + * @since 1.7.3 + * @param string $statement SQL query with PDO style named placeholders + * @param array $values Optional array of values corresponding to placeholders + * @return string Readable SQL query with placeholders replaced + */ + public function pretty_format($statement, array $values = array() ) { + if (!$values) { + return $statement; + } + + return preg_replace_callback( + '/:([^\s;)]*)/', + + /** + * @param string $matches + */ + function ($matches) use ($values) { + $replacement = isset( $values[$matches[1]] ) ? $values[$matches[1]] : ''; + if(is_array($replacement)) { + $replacement = implode(",", $replacement); + } + return "'$replacement'"; + }, + $statement + ); + } + +} |