index.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. if ($system->getAuthEnabled()) {
  3. if (cfr('ROOT')) {
  4. /**
  5. * Minimalistic development/debugging console implementation
  6. */
  7. class DevConsole {
  8. /**
  9. * System message helper instance.
  10. *
  11. * @var object
  12. */
  13. public $messages = '';
  14. /**
  15. * Some static routes etc
  16. */
  17. const URL_ME = '?module=devconsole';
  18. const ROUTE_PHPCON = 'phpconsole';
  19. const PROUTE_QUERY = 'devsqlquery';
  20. const PROUTE_CODE = 'devphpcode';
  21. public function __construct() {
  22. $this->initMessages();
  23. }
  24. /**
  25. * Inits system message helper
  26. *
  27. * @return void
  28. */
  29. protected function initMessages() {
  30. $this->messages = new UbillingMessageHelper();
  31. }
  32. /**
  33. * Renders module controls
  34. *
  35. * @return string
  36. */
  37. public function panel() {
  38. $result = '';
  39. $result .= wf_Link(self::URL_ME, wf_img('skins/icon_restoredb.png') . ' ' . __('SQL console'), false, 'ubButton') . ' ';
  40. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_PHPCON . '=true', wf_img('skins/icon_php.png') . ' ' . __('PHP console'), false, 'ubButton') . ' ';
  41. return ($result);
  42. }
  43. /**
  44. * Renders SQL console form
  45. *
  46. * @return string
  47. */
  48. public function renderSqlConsole() {
  49. $result = '';
  50. $inputs = wf_TextArea(self::PROUTE_QUERY, '', '', true, '80x10');
  51. $inputs .= wf_Submit(__('Run SQL query'));
  52. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  53. return ($result);
  54. }
  55. /**
  56. * Renders PHP console form
  57. *
  58. * @return string
  59. */
  60. public function renderPhpConsole() {
  61. $result = '';
  62. $inputs = wf_TextArea(self::PROUTE_CODE, '', '', true, '80x10');
  63. $inputs .= wf_Submit(__('Run PHP code'));
  64. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  65. return ($result);
  66. }
  67. /**
  68. * Runs some database query
  69. *
  70. * @param string $query
  71. *
  72. * @return string
  73. */
  74. public function runQuery($query) {
  75. global $loginDB;
  76. $result = '';
  77. $query_result = array();
  78. $stripquery = substr($query, 0, 70) . '..';
  79. log_register('SQLCONSOLE ' . $stripquery);
  80. ob_start();
  81. if (!empty($query)) {
  82. if (!extension_loaded('mysql')) {
  83. mysqli_report(0);
  84. $resultRaw = mysqli_query($loginDB, $query);
  85. } else {
  86. $resultRaw = mysql_query($query);
  87. }
  88. if ($resultRaw === false) {
  89. ob_end_clean();
  90. $result .= $this->messages->getStyledMessage(__('Wrong query') . ': ' . $query, 'error');
  91. } else {
  92. if (!extension_loaded('mysql')) {
  93. mysqli_report(0);
  94. if ($resultRaw !== true) {
  95. while (@$row = mysqli_fetch_assoc($resultRaw)) {
  96. $query_result[] = $row;
  97. }
  98. }
  99. } else {
  100. while (@$row = mysql_fetch_assoc($resultRaw)) {
  101. $query_result[] = $row;
  102. }
  103. }
  104. $sqlDebugData = ob_get_contents();
  105. ob_end_clean();
  106. log_register('SQLCONSOLE QUERYDONE');
  107. }
  108. $result .= $this->messages->getStyledMessage(__('Query executed successfully') . ': ' . $query, 'success');
  109. $result .= wf_delimiter(0);
  110. if (!empty($query_result)) {
  111. $result .= wf_tag('pre');
  112. $result .= var_export($query_result, true);
  113. $result .= wf_tag('pre', true);
  114. } else {
  115. $result .= $this->messages->getStyledMessage(__('Query returned empty result'), 'info');
  116. }
  117. } else {
  118. $result .= $this->messages->getStyledMessage(__('Empty query'), 'error');
  119. }
  120. return ($result);
  121. }
  122. }
  123. $console = new DevConsole();
  124. //rendering controls
  125. show_window(__('Developers console'), $console->panel());
  126. //rendering interfaces
  127. if (ubRouting::checkGet($console::ROUTE_PHPCON)) {
  128. show_window(__('PHP'), $console->renderPhpConsole());
  129. } else {
  130. show_window(__('SQL'), $console->renderSqlConsole());
  131. }
  132. //performing SQL queries
  133. if (ubRouting::checkPost($console::PROUTE_QUERY)) {
  134. show_window(__('SQL query result'), $console->runQuery(ubRouting::post($console::PROUTE_QUERY)));
  135. }
  136. ///Or executing PHP code right here
  137. if (ubRouting::checkPost($console::PROUTE_CODE)) {
  138. //executing code directly here because variables/objects visibily is broken inside methods or functions
  139. $code = ubRouting::post($console::PROUTE_CODE);
  140. $phpCodeExecResult = '';
  141. $code = trim($code);
  142. if (!empty($code)) {
  143. $phpCodeExecResult .= $console->messages->getStyledMessage(__('Running this code'), 'info') . wf_delimiter(0);
  144. $phpCodeExecResult .= highlight_string('<?php' . "\n" . $code . "\n" . '?>', true);
  145. //executing it
  146. $stripcode = substr($code, 0, 70) . '..';
  147. log_register('DEVCONSOLE ' . $stripcode);
  148. ob_start();
  149. try {
  150. eval($code);
  151. } catch (ParseError $e) {
  152. show_error(__('Error') . ':' . $e);
  153. }
  154. $debugData = ob_get_contents();
  155. ob_end_clean();
  156. if (!empty($debugData)) {
  157. $phpCodeExecResult .= $console->messages->getStyledMessage(__('Console debug data'), 'warning') . wf_delimiter(0);
  158. $phpCodeExecResult .= wf_tag('pre') . $debugData . wf_tag('pre', true);
  159. } else {
  160. $phpCodeExecResult .= $console->messages->getStyledMessage(__('Console debug data is empty'), 'success') . wf_delimiter(0);
  161. }
  162. log_register('DEVCONSOLE DONE');
  163. } else {
  164. $phpCodeExecResult .= $console->messages->getStyledMessage(__('Empty code part received'), 'error');
  165. }
  166. show_window(__('PHP code execution result'), $phpCodeExecResult);
  167. }
  168. } else {
  169. show_error(__('Access denied'));
  170. }
  171. } else {
  172. show_error(__('Authorization engine disabled'));
  173. }