2
0

debug.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. class debug
  3. {
  4. private static $logs = [];
  5. private static $startTime;
  6. private static $closeTime;
  7. public static function display_errors(){
  8. ini_set('display_errors', 1);
  9. ini_set('display_startup_errors', 1);
  10. error_reporting(E_ALL);
  11. }
  12. private static function typeFile(string $_string){
  13. switch ($_string) {
  14. case 'maintenance':
  15. return ".lock-maintenance";
  16. break;
  17. case 'debug':
  18. return ".active-debug";
  19. break;
  20. default:
  21. return ".active-debug-".$_string;
  22. break;
  23. }
  24. }
  25. public static function addFile(?string $_string = NULL)
  26. {
  27. if($_string != NULL){
  28. $myfile = fopen(DOCUMENT_ROOT . self::typeFile($_string), "w");
  29. fclose($myfile);
  30. }
  31. }
  32. public static function removeFile(?string $_string = NULL)
  33. {
  34. if($_string != NULL){
  35. unlink(DOCUMENT_ROOT . self::typeFile($_string));
  36. }
  37. }
  38. public static function isFile(?string $_string = NULL)
  39. {
  40. return file_exists(DOCUMENT_ROOT . self::typeFile($_string)) ? TRUE : FALSE;
  41. }
  42. public static function includeDebug()
  43. {
  44. if (debug::isFile("debug")) {
  45. echo '<link rel="stylesheet" href="' . cache::getFileWithTime("css/debug.css") . '">';
  46. }
  47. }
  48. public static function dump($var, $label = 'Dump', $echo = true)
  49. {
  50. // Start output buffering
  51. ob_start();
  52. echo "<pre class='debug-dump'>";
  53. echo "<strong>" . $label . ":</strong> ";
  54. // Convert array or object to string
  55. if (is_array($var)) {
  56. print_r($var);
  57. } elseif (is_object($var)) {
  58. echo self::objectToString($var);
  59. } else {
  60. var_dump($var);
  61. }
  62. echo "</pre>";
  63. // Get the contents of the buffer
  64. $output = ob_get_clean();
  65. // If echo is true, print the output
  66. if ($echo) {
  67. echo $output;
  68. } else {
  69. return $output;
  70. }
  71. }
  72. public static function objectToString($object, $_tab = NULL) {
  73. ob_start();
  74. $tab = "&nbsp;&nbsp;&nbsp;&nbsp;" . $_tab;
  75. echo "<span class='debug-console-capsule'>Object (" . get_class($object) . ")\n</span>";
  76. echo $_tab . "<span class='debug-console-capsule'>{\n</span>";
  77. foreach (get_object_vars($object) as $property => $value) {
  78. echo $tab . "<span class='debug-console-capsule'>[<span class='debug-console-key'>$property</span>] => </span>";
  79. if (is_array($value)) {
  80. echo self::arrayToString($value, $tab);
  81. } elseif (is_object($value)) {
  82. echo self::objectToString($value, $tab);
  83. } else {
  84. echo "<span class='debug-console-value'>" . var_export($value, true) . "</span>\n";
  85. }
  86. }
  87. echo $_tab . "<span class='debug-console-capsule'>}\n</span>";
  88. return ob_get_clean();
  89. }
  90. public static function arrayToString($array, $_tab = NULL)
  91. {
  92. ob_start();
  93. $tab = "&nbsp;&nbsp;&nbsp;&nbsp;".$_tab;
  94. echo "<span class='debug-console-capsule'>Array\n</span>";
  95. echo $_tab . "<span class='debug-console-capsule'>(\n</span>";
  96. foreach ($array as $key => $value) {
  97. echo $tab . "<span class='debug-console-capsule'>[<span class='debug-console-key'>$key</span>] => </span>";
  98. if (is_array($value)) {
  99. echo self::arrayToString($value, $tab);
  100. } elseif (is_object($value)) {
  101. echo self::objectToString($value, $tab);
  102. } else {
  103. echo "<span class='debug-console-value'>" . var_export($value, true) . "</span>\n";
  104. }
  105. }
  106. echo $_tab . "<span class='debug-console-capsule'>)\n</span>";
  107. return ob_get_clean();
  108. }
  109. private static function variableToString($var, $label)
  110. {
  111. ob_start();
  112. echo "$label: ";
  113. if (is_array($var) || is_object($var)) {
  114. print_r($var);
  115. } else {
  116. var_dump($var);
  117. }
  118. return ob_get_clean();
  119. }
  120. public static function getTraces(){
  121. $return = "Trace : ";
  122. // Obtenir la trace d'exécution
  123. $backtrace = debug_backtrace();
  124. $nb = count($backtrace)-1;
  125. for ($i=$nb; $i > 0; $i--) {
  126. $return .= ($i != 0) ? "[".$backtrace[$i]["function"]."] " : NULL;
  127. $return .= str_replace(DOCUMENT_ROOT, '', $backtrace[$i]["file"]).":".$backtrace[$i]["line"];
  128. $return .= ($i != 1) ? " >> " : NULL;
  129. }
  130. return $return;
  131. }
  132. public static function print_r(?array $_array = NULL, ?int $_exit = NULL)
  133. {
  134. echo "<div>".debug::getTraces() . "</div>";
  135. if(empty($_array)){
  136. echo "<pre>EMPTY</pre>";
  137. } else {
  138. echo "<pre>";
  139. print_r($_array);
  140. echo "</pre>";
  141. }
  142. ($_exit != NULL) ? exit() : NULL;
  143. }
  144. public static function isHtml(string $_string) : bool
  145. {
  146. $stripped_string = strip_tags($_string);
  147. return $_string !== $stripped_string;
  148. }
  149. public static function generateHtmlContent($htmlContent)
  150. {
  151. $id = md5(microtime());
  152. return <<<HTML
  153. <!DOCTYPE html>
  154. <html>
  155. <head>
  156. <title>Exécution de HTML</title>
  157. <style>
  158. .isolated-iframe {
  159. width: 100%;
  160. height: 300px;
  161. border: none;
  162. }
  163. </style>
  164. </head>
  165. <body>
  166. <iframe id="isolatedIframe-$id" class="isolated-iframe"></iframe>
  167. <script>
  168. var iframe = document.getElementById('isolatedIframe-$id');
  169. var doc = iframe.document || iframe.contentDocument || iframe.contentWindow.document;
  170. doc.open();
  171. doc.write(`{$htmlContent}`);
  172. doc.close();
  173. </script>
  174. </body>
  175. </html>
  176. HTML;
  177. }
  178. public static function log($_message, $_mark = NULL)
  179. {
  180. $mark = "<div class='debug-head-console'>";
  181. $mark .= ($_mark != NULL) ? "<div style='font-weight: bold;'>". $_mark . "</div>" : NULL;
  182. $mark .= self::getTraces();
  183. $mark .= "</div>";
  184. if($_message != NULL) {
  185. if(is_array($_message)){
  186. self::$logs[] = ["data" => $mark ."<div class='debug-console'>" . self::arrayToString($_message) . "</div>", "nl2br" => TRUE];
  187. } elseif(is_object($_message)){
  188. self::$logs[] = ["data" => $mark ."<div class='debug-console'>" . self::objectToString($_message) . "</div>", "nl2br" => TRUE];
  189. } elseif(self::isHtml($_message)){
  190. self::$logs[] = ["data" => $mark ."<div class='debug-console'>" . self::generateHtmlContent($_message) . "</div>", "nl2br" => FALSE];
  191. } elseif($mark != NULL){
  192. self::$logs[] = ["data" => $mark ."<div class='debug-console' style='color:blue;'>" . $_message . "</div>", "nl2br" => TRUE];
  193. } else {
  194. self::$logs[] = ["data" => $_message, "nl2br" => TRUE];
  195. }
  196. }
  197. }
  198. public static function logSession($_message, $_mark = NULL)
  199. {
  200. $mark = "<div class='debug-head-console'>";
  201. $mark .= ($_mark != NULL) ? "<div style='font-weight: bold;'>". $_mark . "</div>" : NULL;
  202. $mark .= self::getTraces();
  203. $mark .= "</div>";
  204. if($_message != NULL) {
  205. if(is_array($_message)){
  206. self::setSession(["data" => $mark ."<div class='debug-console'>" . self::arrayToString($_message) . "</div>", "nl2br" => TRUE]);
  207. } elseif(is_object($_message)){
  208. self::setSession(["data" => $mark ."<div class='debug-console'>" . self::objectToString($_message) . "</div>", "nl2br" => TRUE]);
  209. } elseif(self::isHtml($_message)){
  210. self::setSession(["data" => $mark ."<div class='debug-console'>" . self::generateHtmlContent($_message) . "</div>", "nl2br" => false]);
  211. } elseif($mark != NULL){
  212. self::setSession(["data" => $mark ."<div class='debug-console' style='color:blue;'>" . $_message . "</div>", "nl2br" => TRUE]);
  213. } else {
  214. self::setSession(["data" => $_message, "nl2br" => TRUE]);
  215. }
  216. }
  217. }
  218. public static function renderLogs()
  219. {
  220. echo "<div id='debugger-logs'>";
  221. echo "<div class='debug-renderLogs-header'>";
  222. echo "PHP <span style=\"font-weight: bold;\">". phpversion() . "</span> | MYSQL <span style=\"font-weight: bold;\">" . db::version() . "</span> | " . number_format(self::$closeTime, 4) . " secondes ";
  223. echo "</div>";
  224. echo "<div class=\"form-check form-switch\" style=\"margin-top: -30px;\">
  225. <div>
  226. <input class=\"form-check-input\" type=\"checkbox\" id=\"checkIsSubmit\" " . core::checkboxSelecter(self::isFile("submit"), 0) . " >
  227. <label class=\"form-check-label\" for=\"checkIsSubmit\">Intercepter les <span style=\"font-weight: bold;\">Submit</span></label>
  228. </div>
  229. <div>
  230. <input class=\"form-check-input\" type=\"checkbox\" id=\"checkIsSql\" " . core::checkboxSelecter(self::isFile("sql"), 0) . " >
  231. <label class=\"form-check-label\" for=\"checkIsSql\">Intercepter les <span style=\"font-weight: bold;\">requêtes SQL</span></label>
  232. </div>
  233. <div>
  234. <input class=\"form-check-input\" type=\"checkbox\" id=\"checkIsEmail\" " . core::checkboxSelecter(self::isFile("email"), 0) . " >
  235. <label class=\"form-check-label\" for=\"checkIsEmail\">Intercepter les <span style=\"font-weight: bold;\">Email</span></label>
  236. </div>
  237. <div>
  238. <input class=\"form-check-input\" type=\"checkbox\" id=\"checkSendIsEmail\" " . core::checkboxSelecter(self::isFile("send-email"), 0) . " >
  239. <label class=\"form-check-label\" for=\"checkSendIsEmail\">Afficher le mode debug lors de l'<span style=\"font-weight: bold;\">envoie d'email</span></label>
  240. </div>
  241. </div>";
  242. foreach (self::$logs as $log) {
  243. if($log != NULL){
  244. echo "<div class='debug-renderLogs-print'>";
  245. echo $log["nl2br"] == TRUE ? nl2br($log["data"]) : $log["data"];
  246. echo "</div>";
  247. }
  248. }
  249. if(self::ifSession()){
  250. foreach (self::getSession() as $logSession) {
  251. echo "<div class='debug-renderLogs-print'>";
  252. echo $logSession["nl2br"] == TRUE ? nl2br($logSession["data"]) : $logSession["data"];
  253. echo "</div>";
  254. }
  255. }
  256. echo "</div>";
  257. get::javascript("debug");
  258. }
  259. public static function init()
  260. {
  261. // Register shutdown function to render logs at the end of the script execution
  262. register_shutdown_function(function () {
  263. self::renderLogs();
  264. });
  265. }
  266. public static function startTimer()
  267. {
  268. self::$startTime = microtime(true);
  269. }
  270. public static function endTimer()
  271. {
  272. self::$closeTime = microtime(true) - self::$startTime;
  273. }
  274. public static function getBadge(string $_link,string $_label){
  275. return '<a href="'. $_link .'" target="_blank" class="badge debug-badge">'. $_label .'</a>';
  276. }
  277. public static function printEnvironnement(){
  278. echo (ENVIRONNEMENT != "PROD") ? " [" . ENVIRONNEMENT . "]" : NULL;
  279. }
  280. private static function buildBadge(array $_data){
  281. $color = empty($_data["color"]) ? "black" : $_data["color"];
  282. $backgroundColor = empty($_data["background-color"]) ? "orangered" : $_data["background-color"];
  283. $class = empty($_data["class"]) ? NULL : $_data["class"];
  284. $link = empty($_data["link"]) ? "#" : $_data["link"];
  285. $txt = empty($_data["txt"]) ? NULL : $_data["txt"];
  286. $icon = empty($_data["icon"]) ? NULL : "<i class=\"" . $_data["icon"] . "\"></i> ";
  287. return "<a href=\"" . $link . "\"><span class=\"badge " . $class . "\" style=\"background-color:" . $backgroundColor . "; color:" . $color . "; padding: 3px 5px 2px 5px; margin-right: 5px;\">" . $icon . $txt . "</span></a>";
  288. }
  289. public static function getBadges(){
  290. $return = "";
  291. if(debug::isFile("maintenance")){
  292. $return .= self::buildBadge([
  293. "icon" => "bi bi-exclamation-diamond-fill",
  294. "link" => "/parametres.html#parametres-debug",
  295. "background-color" => "red",
  296. "color" => "white",
  297. "txt" => "MODE MAINTENANCE"
  298. ]);
  299. }
  300. if(debug::isFile("debug")){
  301. $return .= self::buildBadge([
  302. "icon" => "bi bi-bug-fill",
  303. "class" => "toggle-logs",
  304. "link" => "#",
  305. "background-color" => "orangered",
  306. "color" => "white",
  307. "txt" => "MODE DEBUG"
  308. ]);
  309. }
  310. if(self::isFile("sql")){
  311. $return .= self::buildBadge([
  312. "icon" => "bi bi-database-fill",
  313. "class" => "toggle-logs",
  314. "link" => "#",
  315. "background-color" => "orange",
  316. "color" => "black",
  317. "txt" => "DEBUG SQL"
  318. ]);
  319. }
  320. if(self::isFile("email")){
  321. $return .= self::buildBadge([
  322. "icon" => "bi bi-envelope-fill",
  323. "class" => "toggle-logs",
  324. "link" => "#",
  325. "background-color" => "orange",
  326. "color" => "black",
  327. "txt" => "DEBUG EMAIL"
  328. ]);
  329. }
  330. if(self::isFile("send-email")){
  331. $return .= self::buildBadge([
  332. "icon" => "bi bi-envelope-fill",
  333. "class" => "toggle-logs",
  334. "link" => "#",
  335. "background-color" => "orange",
  336. "color" => "black",
  337. "txt" => "DEBUG SEND EMAIL"
  338. ]);
  339. }
  340. if(self::isFile("submit")){
  341. $return .= self::buildBadge([
  342. "icon" => "bi bi-send-fill",
  343. "class" => "toggle-logs",
  344. "link" => "#",
  345. "background-color" => "orange",
  346. "color" => "black",
  347. "txt" => "DEBUG SUBMIT"
  348. ]);
  349. }
  350. return $return;
  351. }
  352. public static function setSession($_data){
  353. $_SESSION["DEBUG"][] = $_data;
  354. }
  355. public static function getSession(){
  356. $return = $_SESSION["DEBUG"];
  357. self::resetSession();
  358. return $return;
  359. }
  360. public static function ifSession(){
  361. return empty($_SESSION["DEBUG"]) ? FALSE : TRUE;
  362. }
  363. public static function resetSession(){
  364. unset($_SESSION["DEBUG"]);
  365. }
  366. }