1<?php 2 3class siteexport_debug 4{ 5 private $firstRE = true; 6 7 private $debugLevel = 5; 8 private $debugFile = ''; 9 public $isAJAX = false; 10 11 public $runtimeErrors = ''; 12 13 /** 14 * Debug Level 15 * the level of what should be logged during the proxied session. 16 * To activate the logging, you have to enter a loglevel below 5 (see below) to log 17 * to the screen. If you use the debugFile option the logstream will be rerouted 18 * to this file. 19 * 20 * Default: 5 / No logging 21 * 22 * Available DEBUG Levels: 23 * 5 = off - only socket exceptions will be shown to avoid blank pages 24 * 4 = ERROR - Log errors of the proxy process 25 * 3 = WARN - Log warnings during the proxy process 26 * 2 = INFO - Log information about the ongoing connection process 27 * 1 = DEBUG - detailed log about variable states 28 * 0 = VERBOSE - Additionally logs the reponse body from the server 29 * 30 * @param $level 31 */ 32 public function setDebugLevel($level = 5) 33 { 34 $this->debugLevel = $level; 35 } 36 37 public function debugLevel() 38 { 39 return $this->debugLevel; 40 } 41 42 /** 43 * Set a valid and writeable filename to have the debug information written into a file 44 * Set the debugLevel below 5 to enable the debugging. 45 * 46 * e.g. $CC->debugFile = '/temp/ccproxy.txt'; 47 * e.g. $CC->debugFile = 'C:\temp\ccproxy.txt'; 48 */ 49 public function setDebugFile($file = null) 50 { 51 if (!$file || empty($file)) 52 { 53 $file = null; 54 } 55 56 $this->debugFile = $file; 57 } 58 59 public function firstRE() 60 { 61 return $this->firstRE; 62 } 63 64 /** 65 * print debug info to file if exists 66 */ 67 public function message($info,$var=null,$level=4){ 68 69 $ajaxCanLog = $this->isAJAX && $level == 4; 70 $fh = false; 71 72 if( $this->debugLevel > $level && !$ajaxCanLog ) return; // only log certain Debug Levels 73 74 if ( empty($this->debugFile) ) { 75 $this->runtimeException("DebugFile not properly configured. Make sure, it is set, readable and writable. We suggest to use a file in the DokuWiki's media directory.", true); 76 $this->debugLevel = 5; // shutdown debug 77 } else { 78 $fh = @fopen($this->debugFile, "a+"); 79 if ( !$fh && !$ajaxCanLog ) { 80 $this->runtimeException("Could not create/open/append logfile: '{$this->debugFile}'", true); 81 $this->debugLevel = 5; // shutdown debug 82 return; 83 } 84 } 85 86 switch($level) { 87 case 4: $TYPE = "ERROR"; break; 88 case 3: $TYPE = " WARN"; break; 89 case 2: $TYPE = " INFO"; break; 90 case 1: $TYPE = "DEBUG"; break; 91 default: $TYPE = " NONE"; break; 92 } 93 94 $prepend = "[" . (date('Y-m-d H:i:s') ?: "") . " $TYPE] "; 95 $log = $prepend . str_replace("\n", "\n" . $prepend . "\t", trim($info)) . "\n"; 96 97 if ( $fh !== false ) { 98 fwrite($fh, $log); 99 } 100 if ( $ajaxCanLog ) { 101 if ( !headers_sent() ) { 102 header("HTTP/1.0 500 Internal Server Error", true, 500); 103 header("Status: 500 Internal Server Error", true, 500); 104 } 105 echo $log; 106 } 107 108 if ( !empty($var) ) { 109 110 if ( is_array($var) ) { 111 ob_start(); 112 print_r($var); 113 $content = ob_get_contents(); 114 ob_end_clean(); 115 } else { 116 $content = $var; 117 } 118 119 $log = $prepend . "\t" . str_replace("\n", "\n" . $prepend . "\t", str_replace("\r\n", "\n", trim($content))) . "\n"; 120 if ( $fh ) { 121 fwrite($fh, $log); 122 } 123 if ( $ajaxCanLog ) { 124 echo $log; 125 } 126 } 127 128 if ( $fh ) { 129 fclose($fh); 130 } 131 } 132 133 public function runtimeException($message, $wasDebug=false) { 134 135 if ( empty($message) ) { return; } 136 137 if ( !$this->isAJAX ) { 138 ob_start(); 139 } else if ( !headers_sent() ) { 140 header("HTTP/1.0 500 Internal Server Error", true, 500); 141 header("Status: 500 Internal Server Error", true, 500); 142 } 143 144 if ( !$this->isAJAX ) { 145 if ( $this->firstRE ) { 146 print 'Runtime Error' . "\n"; 147 } 148 149 print '<b>'.$message.'</b><br />' . "\n"; 150 if ( $this->firstRE ) { 151 print '<b>If this error persists, please contact the server administrator.</b><br />' . "\n"; 152 } 153 } else { 154 if ( !$wasDebug ) { 155 $this->message('Runtime Error: ' . $message, null, 4); 156 } else { 157 print 'Runtime Error: ' . $message . "\n"; 158 } 159 } 160 161 $this->firstRE = false; 162 163 if (!$this->isAJAX) { 164 $this->runtimeErrors .= ob_get_contents(); 165 ob_end_clean(); 166 } 167 168 return; 169 } 170} 171