Subversion Repositories ALCASAR

Rev

Rev 3037 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 3037 Rev 3100
1
<?php
1
<?php
2
/**
2
/**
3
 * PSI_Error class
3
 * PSI_Error class
4
 *
4
 *
5
 * PHP version 5
5
 * PHP version 5
6
 *
6
 *
7
 * @category  PHP
7
 * @category  PHP
8
 * @package   PSI_Error
8
 * @package   PSI_Error
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
10
 * @copyright 2009 phpSysInfo
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12
 * @version   SVN: $Id: class.Error.inc.php 569 2012-04-16 06:08:18Z namiltd $
12
 * @version   SVN: $Id: class.Error.inc.php 569 2012-04-16 06:08:18Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
14
 */
15
 /**
15
 /**
16
 * class for the error handling in phpsysinfo
16
 * class for the error handling in phpsysinfo
17
 *
17
 *
18
 * @category  PHP
18
 * @category  PHP
19
 * @package   PSI_Error
19
 * @package   PSI_Error
20
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
20
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
21
 * @copyright 2009 phpSysInfo
21
 * @copyright 2009 phpSysInfo
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
23
 * @version   Release: 3.0
23
 * @version   Release: 3.0
24
 * @link      http://phpsysinfo.sourceforge.net
24
 * @link      http://phpsysinfo.sourceforge.net
25
 */
25
 */
26
class PSI_Error
26
class PSI_Error
27
{
27
{
28
    /**
28
    /**
29
     * holds the instance of this class
29
     * holds the instance of this class
30
     *
30
     *
31
     * @static
31
     * @static
32
     * @var PSI_Error
32
     * @var PSI_Error
33
     */
33
     */
34
    private static $_instance;
34
    private static $_instance;
35
 
35
 
36
    /**
36
    /**
37
     * holds the error messages
37
     * holds the error messages
38
     *
38
     *
39
     * @var array
39
     * @var array
40
     */
40
     */
41
    private $_arrErrorList = array();
41
    private $_arrErrorList = array();
42
 
42
 
43
    /**
43
    /**
44
     * current number ob errors
44
     * current number ob errors
45
     *
45
     *
46
     * @var integer
46
     * @var integer
47
     */
47
     */
48
    private $_errors = 0;
48
    private $_errors = 0;
49
 
49
 
50
    /**
50
    /**
51
     * initalize some used vars
51
     * initalize some used vars
52
     */
52
     */
53
    private function __construct()
53
    private function __construct()
54
    {
54
    {
55
        $this->_errors = 0;
55
        $this->_errors = 0;
56
        $this->_arrErrorList = array();
56
        $this->_arrErrorList = array();
57
    }
57
    }
58
 
58
 
59
    /**
59
    /**
60
     * Singleton function
60
     * Singleton function
61
     *
61
     *
62
     * @return PSI_Error instance of the class
62
     * @return PSI_Error instance of the class
63
     */
63
     */
64
    public static function singleton()
64
    public static function singleton()
65
    {
65
    {
66
        if (!isset(self::$_instance)) {
66
        if (!isset(self::$_instance)) {
67
            $c = __CLASS__;
67
            $c = __CLASS__;
68
            self::$_instance = new $c;
68
            self::$_instance = new $c;
69
        }
69
        }
70
 
70
 
71
        return self::$_instance;
71
        return self::$_instance;
72
    }
72
    }
73
 
73
 
74
    /**
74
    /**
75
     * triggers an error when somebody tries to clone the object
75
     * triggers an error when somebody tries to clone the object
76
     *
76
     *
77
     * @return void
77
     * @return void
78
     */
78
     */
79
    public function __clone()
79
    public function __clone()
80
    {
80
    {
81
        trigger_error("Can't be cloned", E_USER_ERROR);
81
        trigger_error("Can't be cloned", E_USER_ERROR);
82
    }
82
    }
83
 
83
 
84
    /**
84
    /**
85
     * adds an phpsysinfo error to the internal list
85
     * adds an phpsysinfo error to the internal list
86
     *
86
     *
87
     * @param string $strCommand Command, which cause the Error
87
     * @param string $strCommand Command, which cause the Error
88
     * @param string $strMessage additional Message, to describe the Error
88
     * @param string $strMessage additional Message, to describe the Error
89
     *
89
     *
90
     * @return void
90
     * @return void
91
     */
91
     */
92
    public function addError($strCommand, $strMessage)
92
    public function addError($strCommand, $strMessage)
93
    {
93
    {
94
        $this->_addError($strCommand, $this->_trace($strMessage));
94
        $this->_addError($strCommand, $this->_trace($strMessage));
95
    }
95
    }
96
 
96
 
97
    /**
97
    /**
98
     * adds an error to the internal list
98
     * adds an error to the internal list
99
     *
99
     *
100
     * @param string $strCommand Command, which cause the Error
100
     * @param string $strCommand Command, which cause the Error
101
     * @param string $strMessage message, that describe the Error
101
     * @param string $strMessage message, that describe the Error
102
     *
102
     *
103
     * @return void
103
     * @return void
104
     */
104
     */
105
    private function _addError($strCommand, $strMessage)
105
    private function _addError($strCommand, $strMessage)
106
    {
106
    {
107
        $index = count($this->_arrErrorList) + 1;
107
        $index = count($this->_arrErrorList) + 1;
108
        $this->_arrErrorList[$index]['command'] = $strCommand;
108
        $this->_arrErrorList[$index]['command'] = $strCommand;
109
        $this->_arrErrorList[$index]['message'] = $strMessage;
109
        $this->_arrErrorList[$index]['message'] = $strMessage;
110
        $this->_errors++;
110
        $this->_errors++;
111
    }
111
    }
112
 
112
 
113
    /**
113
    /**
114
     * add a config error to the internal list
114
     * add a config error to the internal list
115
     *
115
     *
116
     * @param string $strCommand Command, which cause the Error
116
     * @param string $strCommand Command, which cause the Error
117
     * @param string $strMessage additional Message, to describe the Error
117
     * @param string $strMessage additional Message, to describe the Error
118
     *
118
     *
119
     * @return void
119
     * @return void
120
     */
120
     */
121
    public function addConfigError($strCommand, $strMessage)
121
    public function addConfigError($strCommand, $strMessage)
122
    {
122
    {
123
        $this->_addError($strCommand, "Wrong Value in phpsysinfo.ini for ".$strMessage);
123
        $this->_addError($strCommand, "Wrong Value in phpsysinfo.ini for ".$strMessage);
124
    }
124
    }
125
 
125
 
126
    /**
126
    /**
127
     * add a php error to the internal list
127
     * add a php error to the internal list
128
     *
128
     *
129
     * @param string $strCommand Command, which cause the Error
129
     * @param string $strCommand Command, which cause the Error
130
     * @param string $strMessage additional Message, to describe the Error
130
     * @param string $strMessage additional Message, to describe the Error
131
     *
131
     *
132
     * @return void
132
     * @return void
133
     */
133
     */
134
    public function addPhpError($strCommand, $strMessage)
134
    public function addPhpError($strCommand, $strMessage)
135
    {
135
    {
136
        $this->_addError($strCommand, "PHP throws a error\n".$strMessage);
136
        $this->_addError($strCommand, "PHP throws a error\n".$strMessage);
137
    }
137
    }
138
 
138
 
139
    /**
139
    /**
140
     * adds a waraning to the internal list
140
     * adds a waraning to the internal list
141
     *
141
     *
142
     * @param string $strMessage Warning message to display
142
     * @param string $strMessage Warning message to display
143
     *
143
     *
144
     * @return void
144
     * @return void
145
     */
145
     */
146
    public function addWarning($strMessage)
146
    public function addWarning($strMessage)
147
    {
147
    {
148
        $index = count($this->_arrErrorList) + 1;
148
        $index = count($this->_arrErrorList) + 1;
149
        $this->_arrErrorList[$index]['command'] = "WARN";
149
        $this->_arrErrorList[$index]['command'] = "WARN";
150
        $this->_arrErrorList[$index]['message'] = $strMessage;
150
        $this->_arrErrorList[$index]['message'] = $strMessage;
151
    }
151
    }
152
 
152
 
153
    /**
153
    /**
154
     * converts the internal error and warning list to a XML file
154
     * converts the internal error and warning list to a XML file
155
     *
155
     *
156
     * @return void
156
     * @return void
157
     */
157
     */
158
    public function errorsAsXML()
158
    public function errorsAsXML()
159
    {
159
    {
160
        $dom = new DOMDocument('1.0', 'UTF-8');
160
        $dom = new DOMDocument('1.0', 'UTF-8');
161
        $root = $dom->createElement("phpsysinfo");
161
        $root = $dom->createElement("phpsysinfo");
162
        $dom->appendChild($root);
162
        $dom->appendChild($root);
163
        $xml = new SimpleXMLExtended(simplexml_import_dom($dom), 'UTF-8');
163
        $xml = new SimpleXMLExtended(simplexml_import_dom($dom), 'UTF-8');
164
        $generation = $xml->addChild('Generation');
164
        $generation = $xml->addChild('Generation');
165
        $generation->addAttribute('version', PSI_VERSION_STRING);
165
        $generation->addAttribute('version', PSI_VERSION_STRING);
166
        $generation->addAttribute('timestamp', time());
166
        $generation->addAttribute('timestamp', time());
167
        $xmlerr = $xml->addChild("Errors");
167
        $xmlerr = $xml->addChild("Errors");
168
        foreach ($this->_arrErrorList as $arrLine) {
168
        foreach ($this->_arrErrorList as $arrLine) {
169
//            $error = $xmlerr->addCData('Error', $arrLine['message']);
169
//            $error = $xmlerr->addCData('Error', $arrLine['message']);
170
            $error = $xmlerr->addChild('Error');
170
            $error = $xmlerr->addChild('Error');
171
            $error->addAttribute('Message', $arrLine['message']);
171
            $error->addAttribute('Message', $arrLine['message']);
172
            $error->addAttribute('Function', $arrLine['command']);
172
            $error->addAttribute('Function', $arrLine['command']);
173
        }
173
        }
174
        header('Cache-Control: no-cache, must-revalidate');
174
        header('Cache-Control: no-cache, must-revalidate');
175
        header('Content-Type: text/xml');
175
        header('Content-Type: text/xml');
176
        echo $xml->getSimpleXmlElement()->asXML();
176
        echo $xml->getSimpleXmlElement()->asXML();
177
        exit();
177
        exit();
178
    }
178
    }
179
    /**
179
    /**
180
     * add the errors to an existing xml document
180
     * add the errors to an existing xml document
181
     *
181
     *
182
     * @param String $encoding encoding
182
     * @param String $encoding encoding
183
     *
183
     *
184
     * @return SimpleXmlElement
184
     * @return SimpleXmlElement
185
     */
185
     */
186
    public function errorsAddToXML($encoding)
186
    public function errorsAddToXML($encoding)
187
    {
187
    {
188
        $dom = new DOMDocument('1.0', 'UTF-8');
188
        $dom = new DOMDocument('1.0', 'UTF-8');
189
        $root = $dom->createElement("Errors");
189
        $root = $dom->createElement("Errors");
190
        $dom->appendChild($root);
190
        $dom->appendChild($root);
191
        $xml = simplexml_import_dom($dom);
191
        $xml = simplexml_import_dom($dom);
192
        $xmlerr = new SimpleXMLExtended($xml, $encoding);
192
        $xmlerr = new SimpleXMLExtended($xml, $encoding);
193
        foreach ($this->_arrErrorList as $arrLine) {
193
        foreach ($this->_arrErrorList as $arrLine) {
194
//            $error = $xmlerr->addCData('Error', $arrLine['message']);
194
//            $error = $xmlerr->addCData('Error', $arrLine['message']);
195
            $error = $xmlerr->addChild('Error');
195
            $error = $xmlerr->addChild('Error');
196
            $error->addAttribute('Message', $arrLine['message']);
196
            $error->addAttribute('Message', $arrLine['message']);
197
            $error->addAttribute('Function', $arrLine['command']);
197
            $error->addAttribute('Function', $arrLine['command']);
198
        }
198
        }
199
 
199
 
200
        return $xmlerr->getSimpleXmlElement();
200
        return $xmlerr->getSimpleXmlElement();
201
    }
201
    }
202
    /**
202
    /**
203
     * check if errors exists
203
     * check if errors exists
204
     *
204
     *
205
     * @return boolean true if are errors logged, false if not
205
     * @return boolean true if are errors logged, false if not
206
     */
206
     */
207
    public function errorsExist()
207
    public function errorsExist()
208
    {
208
    {
209
        if ($this->_errors > 0) {
209
        if ($this->_errors > 0) {
210
            return true;
210
            return true;
211
        } else {
211
        } else {
212
            return false;
212
            return false;
213
        }
213
        }
214
    }
214
    }
215
    /**
215
    /**
216
     * generate a function backtrace for error diagnostic, function is genearally based on code submitted in the php reference page
216
     * generate a function backtrace for error diagnostic, function is genearally based on code submitted in the php reference page
217
     *
217
     *
218
     * @param string $strMessage additional message to display
218
     * @param string $strMessage additional message to display
219
     *
219
     *
220
     * @return string formatted string of the backtrace
220
     * @return string formatted string of the backtrace
221
     */
221
     */
222
    private function _trace($strMessage)
222
    private function _trace($strMessage)
223
    {
223
    {
224
        $arrTrace = array_reverse(debug_backtrace());
224
        $arrTrace = array_reverse(debug_backtrace());
225
        $strFunc = '';
225
        $strFunc = '';
226
        $strBacktrace = htmlspecialchars($strMessage)."\n\n";
226
        $strBacktrace = htmlspecialchars($strMessage)."\n\n";
227
        foreach ($arrTrace as $val) {
227
        foreach ($arrTrace as $val) {
228
            // avoid the last line, which says the error is from the error class
228
            // avoid the last line, which says the error is from the error class
229
            if ($val == $arrTrace[count($arrTrace) - 1]) {
229
            if ($val == $arrTrace[count($arrTrace) - 1]) {
230
                break;
230
                break;
231
            }
231
            }
232
            if (isset($val['file'])) {
232
            if (isset($val['file'])) {
233
                $strBacktrace .= str_replace(PSI_APP_ROOT, ".", $val['file']).' on line '.$val['line'];
233
                $strBacktrace .= str_replace(PSI_APP_ROOT, ".", $val['file']).' on line '.$val['line'];
234
            }
234
            }
235
            if ($strFunc) {
235
            if ($strFunc) {
236
                $strBacktrace .= ' in function '.$strFunc;
236
                $strBacktrace .= ' in function '.$strFunc;
237
            }
237
            }
238
            if ($val['function'] == 'include' || $val['function'] == 'require' || $val['function'] == 'include_once' || $val['function'] == 'require_once') {
238
            if ($val['function'] == 'include' || $val['function'] == 'require' || $val['function'] == 'include_once' || $val['function'] == 'require_once') {
239
                $strFunc = '';
239
                $strFunc = '';
240
            } else {
240
            } else {
241
                $strFunc = $val['function'].'(';
241
                $strFunc = $val['function'].'(';
242
                if (isset($val['args'][0])) {
242
                if (isset($val['args'][0])) {
-
 
243
                    if (($val['function'] == 'executeProgram') && ($val['args'][0] == 'sshpass')
-
 
244
                       && isset($val['args'][1]) && preg_match('/"([^"]+)"$/', $val['args'][1], $tmpout)) {
-
 
245
                        $val['args'][1] = 'ssh: '. $tmpout[1];
-
 
246
                    }
243
                    $strFunc .= ' ';
247
                    $strFunc .= ' ';
244
                    $strComma = '';
248
                    $strComma = '';
245
                    foreach ($val['args'] as $valArgs) {
249
                    foreach ($val['args'] as $valArgs) {
246
                        $strFunc .= $strComma.$this->_printVar($valArgs);
250
                        $strFunc .= $strComma.$this->_printVar($valArgs);
247
                        $strComma = ', ';
251
                        $strComma = ', ';
248
                    }
252
                    }
249
                    $strFunc .= ' ';
253
                    $strFunc .= ' ';
250
                }
254
                }
251
                $strFunc .= ')';
255
                $strFunc .= ')';
252
            }
256
            }
253
            $strBacktrace .= "\n";
257
            $strBacktrace .= "\n";
254
        }
258
        }
255
 
259
 
256
        return $strBacktrace;
260
        return $strBacktrace;
257
    }
261
    }
258
    /**
262
    /**
259
     * convert some special vars into better readable output
263
     * convert some special vars into better readable output
260
     *
264
     *
261
     * @param mixed $var value, which should be formatted
265
     * @param mixed $var value, which should be formatted
262
     *
266
     *
263
     * @return string formatted string
267
     * @return string formatted string
264
     */
268
     */
265
    private function _printVar($var)
269
    private function _printVar($var)
266
    {
270
    {
267
        if (is_string($var)) {
271
        if (is_string($var)) {
268
            $search = array("\x00", "\x0a", "\x0d", "\x1a", "\x09");
272
            $search = array("\x00", "\x0a", "\x0d", "\x1a", "\x09");
269
            $replace = array('\0', '\n', '\r', '\Z', '\t');
273
            $replace = array('\0', '\n', '\r', '\Z', '\t');
270
 
274
 
271
            return ('"'.str_replace($search, $replace, $var).'"');
275
            return ('"'.str_replace($search, $replace, $var).'"');
272
        } elseif (is_bool($var)) {
276
        } elseif (is_bool($var)) {
273
            if ($var) {
277
            if ($var) {
274
                return ('true');
278
                return ('true');
275
            } else {
279
            } else {
276
                return ('false');
280
                return ('false');
277
            }
281
            }
278
        } elseif (is_array($var)) {
282
        } elseif (is_array($var)) {
279
            $strResult = 'array( ';
283
            $strResult = 'array( ';
280
            $strComma = '';
284
            $strComma = '';
281
            foreach ($var as $key=>$val) {
285
            foreach ($var as $key=>$val) {
282
                $strResult .= $strComma.$this->_printVar($key).' => '.$this->_printVar($val);
286
                $strResult .= $strComma.$this->_printVar($key).' => '.$this->_printVar($val);
283
                $strComma = ', ';
287
                $strComma = ', ';
284
            }
288
            }
285
            $strResult .= ' )';
289
            $strResult .= ' )';
286
 
290
 
287
            return ($strResult);
291
            return ($strResult);
288
        }
292
        }
289
        // anything else, just let php try to print it
293
        // anything else, just let php try to print it
290
        return (var_export($var, true));
294
        return (var_export($var, true));
291
    }
295
    }
292
}
296
}
293
 
297