api.sqldebug.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Shows SQL debug icon if SQL queries log is available
  4. *
  5. * @return string
  6. */
  7. function web_SqlDebugIconShow() {
  8. $result = '';
  9. if (SQL_DEBUG) {
  10. if (cfr('ROOT')) {
  11. $backUrl = '';
  12. if (!empty($_SERVER['REQUEST_URI'])) {
  13. $backUrl = '&back=' . base64_encode($_SERVER['REQUEST_URI']);
  14. }
  15. $controls = wf_Link('?module=sqldebug' . $backUrl, wf_img('skins/log_icon_small.png') . ' ' . __('All SQL queries log'), true, 'ubButton');
  16. $queryLogData = web_SqlDebugBufferRender(zb_GetSqlDebugBuffer());
  17. $dataToRender = $controls . $queryLogData;
  18. $result = wf_modal(wf_img_sized('skins/sqldebug.png', __('SQL queries debug'), 20), __('SQL queries debug'), $dataToRender, '', '900', '500');
  19. }
  20. }
  21. return ($result);
  22. }
  23. /**
  24. * Returns content of current run SQL debug buffer
  25. *
  26. * @global array $mysqlDebugBuffer
  27. *
  28. * @return array
  29. */
  30. function zb_GetSqlDebugBuffer() {
  31. global $mysqlDebugBuffer;
  32. return($mysqlDebugBuffer);
  33. }
  34. /**
  35. * Renders current run SQL debug buffer
  36. *
  37. * @param array $bufferData
  38. *
  39. * @return string
  40. */
  41. function web_SqlDebugBufferRender($bufferData) {
  42. $result = '';
  43. $messages = new UbillingMessageHelper();
  44. $result .= $messages->getStyledMessage(__('Current run SQL queries'), 'success');
  45. if (!empty($bufferData)) {
  46. foreach ($bufferData as $io => $each) {
  47. $result .= $messages->getStyledMessage(nl2br($each), 'info');
  48. }
  49. $result .= $messages->getStyledMessage(__('SQL queries executed') . ': ' . sizeof($bufferData), 'success');
  50. } else {
  51. $result .= $messages->getStyledMessage(__('Nothing to show'), 'warning');
  52. }
  53. return($result);
  54. }
  55. /**
  56. * Renders SQL queries log
  57. *
  58. * @return string
  59. */
  60. function web_SqlDebugLogParse() {
  61. $result = '';
  62. $messages = new UbillingMessageHelper();
  63. if (file_exists(SQL_DEBUG_LOG)) {
  64. $rawData = file_get_contents(SQL_DEBUG_LOG);
  65. if (!empty($rawData)) {
  66. $logLines = explode(SQL_DEBUG_QUERY_EOL, $rawData);
  67. if (!empty($logLines)) {
  68. $result .= $messages->getStyledMessage(__('All SQL queries log'), 'success');
  69. $logLines = array_reverse($logLines);
  70. foreach ($logLines as $io => $eachLine) {
  71. $lineToRender = str_replace(SQL_DEBUG_QUERY_EOL, '', $eachLine);
  72. $lineToRender = trim($lineToRender);
  73. if (!empty($lineToRender)) {
  74. $lineToRender = nl2br($lineToRender);
  75. $result .= $messages->getStyledMessage($lineToRender, 'info');
  76. }
  77. }
  78. }
  79. } else {
  80. $result .= $messages->getStyledMessage(__('Nothing to show'), 'warning');
  81. }
  82. } else {
  83. $result .= $messages->getStyledMessage(__('SQL queries log not exists'), 'error');
  84. }
  85. return($result);
  86. }
  87. /**
  88. * Flushes SQL debug log file
  89. *
  90. * @return void
  91. */
  92. function zb_SqlDebugLogFlush() {
  93. if (file_exists(SQL_DEBUG_LOG)) {
  94. if (is_writable(SQL_DEBUG_LOG)) {
  95. file_put_contents(SQL_DEBUG_LOG, '');
  96. }
  97. }
  98. }