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