Skip to content

Commit

Permalink
Properly working AJAX debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
stefangabos committed Dec 30, 2024
1 parent a3cde33 commit 0095d1a
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 240 deletions.
74 changes: 57 additions & 17 deletions Zebra_Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,46 @@ class Zebra_Database {
public $debug = true;

/**
* Enables logging of queries done through AJAX requests.
* Enables logging of queries run through AJAX requests.
*
* When set to `TRUE` all AJAX requests sent by your page will be captured and checked for logs generated by
* `Zebra Database`.
* The property's value needs to be set to the path where the `update-debug-info.php` file would be accessible via
* a `GET` `AJAX` request.
*
* > Note that when this is enabled HTML code will be appended to the result of the AJAX requests!<br>Therefore,
* depending on your use-case, having this enabled could break some of your page's functionality.
* Can be both a relative or an absolute path.
*
* <code>
* $debug_ajax = './update-debug-info.php';
* </code>
*
* > You need to move the `update-debug-info.php` file from the library's root folder to location accessible by an
* AJAX GET request. Additionally, you can rename the file to whatever suits your needs - or use whatever
* technique you want, as long as the file is publicly accessible and its content is unchanged.
*
* How this works:
*
* - when this feature is enabled, the library's JavaScript creates a hook that appends a custom {@link https://developer.mozilla.org/en-US/docs/Glossary/Request_header request header}
* to **all** subsequent AJAX requests; the header, named `zdb_log_id`, will contain a 12-character long random
* numerical value; this value serves both as a flag indicating that the library's instances in these requests
* should generate debug information to be relayed back to the "main" script, and as an identifier for the
* temporary file holding the debug information (read on)
*
* - AJAX requests using the library will use this value to write debug information to a temporary file named
* `zdb-log-[numeric value from request header]` in the system's temporary folder (as returned by {@link sys_get_temp_dir() sys_get_temp_dir});
*
* - for all terminating AJAX request, the library's JavaScript will make an additional AJAX call to the `update-debug-info.php`
* file (at the path specified by {@link debug_ajax}); this file will check the system's temporary folder for the
* corresponding temporary file, as described above; if the file exists, it will fetch the debug information from it,
* delete the temporary file, and inject the debug information into the debug console
*
* > Note that this will work only if the {@link $debug} property is set to `TRUE` both in the "main" file as well as in the file that is being called through AJAX.
*
* > This needs to be set only for the "main" script; setting it for the files called through AJAX has no effect.
*
* Default is `FALSE`
*
* @since 2.11.0
*
* @var boolean
* @var string
*/
public $debug_ajax = false;

Expand Down Expand Up @@ -5092,12 +5119,31 @@ function _show_debugging_console() {

}

// if this was an AJAX request add results to the debugging console
// if this was an AJAX request
if ($is_ajax) {

foreach ($blocks as $k => $v)
if (in_array($k, array('errors', 'successful-queries', 'unsuccessful-queries')) && $v['generated'] !== '')
echo '<div class="zdc-' . $k . '-ajax" style="display:none">' . $v['generated'] . '</div class="zdc-end">';
// if debugging AJAX requests is enabled
if (isset($_SERVER['HTTP_ZDB_LOG_ID']) && preg_match('/^[0-9]{12}$/', $_SERVER['HTTP_ZDB_LOG_ID'])) {

$output = '';

foreach ($blocks as $k => $v)
if (in_array($k, array('errors', 'successful-queries', 'unsuccessful-queries')) && $v['generated'] !== '')
$output .= '<div class="zdc-' . $k . '-ajax" style="display:none">' . $v['generated'] . '</div class="zdc-end">';

// minimize output
$output = preg_replace(array(
// remove blank lines
'/[\r\n]+\s*[\r\n]+/',
// remove spaces used for indentation
'/^\s+/m',
), array("\r\n", ''), $output);

// save log info to the system's temp folder
$filename = sys_get_temp_dir() . '/zdb-log-' . $_SERVER['HTTP_ZDB_LOG_ID'];
file_put_contents($filename, $output);

}

// if *not* AJAX request
} else {
Expand Down Expand Up @@ -5199,7 +5245,7 @@ function _show_debugging_console() {
$path = rtrim(preg_replace('/\\\/', '/', '//' . $_SERVER['HTTP_HOST'] . substr(dirname(__FILE__), strlen(realpath($_SERVER['DOCUMENT_ROOT'])))), '/');

// link the required javascript
$output = '<script type="text/javascript" src="' . $path . '/public/javascript/zebra_database.min.js"></script>' . $output;
$output = '<script type="text/javascript" src="' . $path . '/public/javascript/zebra_database.src.js"></script>' . (is_string($this->debug_ajax) && $this->debug_ajax !== '' ? '<script>window.zdb_log_path = "' . $this->debug_ajax . '"</script>' : '') . $output;

// link the required css file
$output = '<link rel="stylesheet" href="' . $path . '/public/css/default/zebra_database.min.css" type="text/css">' . $output;
Expand Down Expand Up @@ -5375,12 +5421,6 @@ private function _is_debugging_enabled() {
// debugging is enabled but needs to be logged instead of being shown on the screen
(is_array($this->debug) && empty(array_filter($this->debug, function($value) { return !(is_bool($value) || $value === 0 || $value === 1); })))

// AND
) && (

// not an AJAX request, or an AJAX request and debugging AJAX requests is ON
!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || $this->debug_ajax

// AND
) && (

Expand Down
Loading

0 comments on commit 0095d1a

Please sign in to comment.