xref: /plugin/siteexport/inc/debug.php (revision 6553d16b1e1a8511ac380f713515281b64eb8590)
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        if ( !$this->isAJAX ) {
136            ob_start();
137        } else if ( !headers_sent() ) {
138			header("HTTP/1.0 500 Internal Server Error", true, 500);
139			header("Status: 500 Internal Server Error", true, 500);
140        }
141
142        if ( !$this->isAJAX ) {
143	        if ( $this->firstRE ) {
144	            print 'Runtime Error' . "\n";
145	        }
146
147            print '<b>'.$message.'</b><br />' . "\n";
148            if ( $this->firstRE ) {
149                print '<b>If this error persists, please contact the server administrator.</b><br />' . "\n";
150            }
151        } else {
152            if ( !$wasDebug ) {
153                $this->message('Runtime Error: ' . $message, null, 4);
154            } else {
155	            print 'Runtime Error: ' . $message . "\n";
156            }
157        }
158
159        $this->firstRE = false;
160
161        if ( !$this->isAJAX ) {
162            $this->runtimeErrors .= ob_get_contents();
163            ob_end_clean();
164        }
165
166        return;
167    }
168}
169
170?>