Subversion Repositories ALCASAR

Rev

Rev 3100 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * parser Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
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
12
 * @version   SVN: $Id: class.Parser.inc.php 604 2012-07-10 07:31:34Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * parser class with common used parsing metods
17
 *
18
 * @category  PHP
19
 * @package   PSI
20
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
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
23
 * @version   Release: 3.0
24
 * @link      http://phpsysinfo.sourceforge.net
25
 */
26
class Parser
27
{
28
    /**
29
     * parsing the output of lspci command
30
     *
31
     * @param  bool  $debug
32
     * @return array
33
     */
34
    public static function lspci($debug = PSI_DEBUG)
35
    {
36
        $arrResults = array();
2976 rexy 37
        if (CommonFunctions::executeProgram("lspci", (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS)?"-m":"", $strBuf, $debug)) {
2770 rexy 38
            $arrLines = preg_split("/\n/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
39
            foreach ($arrLines as $strLine) {
40
                $dev = new HWDevice();
2976 rexy 41
                $arrParams = preg_split('/(\"? ")|(\" (?=-))/', trim($strLine));
42
                if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS && ($cp = count($arrParams)) >= 6) {
3037 rexy 43
                    $arrParams[$cp-1] = trim($arrParams[$cp-1], '"'); // remove last "
2976 rexy 44
                    $dev->setName($arrParams[1].': '.$arrParams[2].' '.$arrParams[3]);
45
                    if (preg_match('/^-/', $arrParams[4])) {
46
                        if (($arrParams[5] !== "") && !preg_match('/^Unknown vendor/', $arrParams[5])) {
47
                            $dev->setManufacturer(trim($arrParams[5]));
48
                        }
49
                        if (($arrParams[6] !== "") && !preg_match('/^Device /', $arrParams[6])) {
50
                            $dev->setProduct(trim($arrParams[6]));
51
                        }
52
                    } else {
53
                        if (($arrParams[4] !== "") && !preg_match('/^Unknown vendor/', $arrParams[4])) {
54
                            $dev->setManufacturer(trim($arrParams[4]));
55
                        }
56
                        if (($arrParams[5] !== "") && !preg_match('/^Device /', $arrParams[5])) {
57
                            $dev->setProduct(trim($arrParams[5]));
58
                        }
59
                    }
60
                } else {
61
                    $strLine=trim(preg_replace('/(")|( -\S+)/', '', $strLine));
62
                    $arrParams = preg_split('/ /', trim($strLine), 2);
63
                    if (count($arrParams) == 2)
64
                        $strName = preg_replace('/\(rev\s[^\)]+\)/', '', $arrParams[1]);
65
                    else
66
                       $strName = "unknown";
67
                    $dev->setName($strName);
68
                }
2770 rexy 69
                $arrResults[] = $dev;
70
            }
71
        }
72
 
73
        return $arrResults;
74
    }
75
 
76
    /**
77
     * parsing the output of df command
78
     *
79
     * @param string $df_param   additional parameter for df command
80
     * @param bool   $get_inodes
81
     *
82
     * @return array
83
     */
84
    public static function df($df_param = "", $get_inodes = true)
85
    {
86
        $arrResult = array();
87
        if (CommonFunctions::executeProgram('mount', '', $mount, PSI_DEBUG)) {
88
            $mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY);
89
            foreach ($mount as $mount_line) {
90
                if (preg_match("/(\S+) on ([\S ]+) type (.*) \((.*)\)/", $mount_line, $mount_buf)) {
91
                    $parm = array();
92
                    $parm['mountpoint'] = trim($mount_buf[2]);
93
                    $parm['fstype'] = $mount_buf[3];
94
                    $parm['name'] = $mount_buf[1];
95
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4];
96
                    $mount_parm[] = $parm;
97
                } elseif (preg_match("/(\S+) is (.*) mounted on (\S+) \(type (.*)\)/", $mount_line, $mount_buf)) {
98
                    $parm = array();
99
                    $parm['mountpoint'] = trim($mount_buf[3]);
100
                    $parm['fstype'] = $mount_buf[4];
101
                    $parm['name'] = $mount_buf[1];
102
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[2];
103
                    $mount_parm[] = $parm;
104
                } elseif (preg_match("/(\S+) (.*) on (\S+) \((.*)\)/", $mount_line, $mount_buf)) {
105
                    $parm = array();
106
                    $parm['mountpoint'] = trim($mount_buf[3]);
107
                    $parm['fstype'] = $mount_buf[2];
108
                    $parm['name'] = $mount_buf[1];
109
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4];
110
                    $mount_parm[] = $parm;
111
                } elseif (preg_match("/(\S+) on ([\S ]+) \((\S+)(,\s(.*))?\)/", $mount_line, $mount_buf)) {
112
                    $parm = array();
113
                    $parm['mountpoint'] = trim($mount_buf[2]);
114
                    $parm['fstype'] = $mount_buf[3];
115
                    $parm['name'] = $mount_buf[1];
116
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = isset($mount_buf[5]) ? $mount_buf[5] : '';
117
                    $mount_parm[] = $parm;
118
                }
119
            }
3100 rexy 120
        } elseif (CommonFunctions::rfts(((PSI_ROOT_FILESYSTEM === '')||(PSI_OS !== 'Linux'))?"/etc/mtab":"/proc/1/mounts", $mount)) {
2770 rexy 121
            $mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY);
122
            foreach ($mount as $mount_line) {
123
                if (preg_match("/(\S+) (\S+) (\S+) (\S+) ([0-9]+) ([0-9]+)/", $mount_line, $mount_buf)) {
124
                    $parm = array();
125
                    $mount_point = preg_replace("/\\\\040/i", ' ', $mount_buf[2]); //space as \040
126
                    $parm['mountpoint'] = $mount_point;
127
                    $parm['fstype'] = $mount_buf[3];
128
                    $parm['name'] = $mount_buf[1];
129
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4];
130
                    $mount_parm[] = $parm;
131
                }
132
            }
133
        }
3179 rexy 134
        $df = "";
135
        CommonFunctions::executeProgram('df', '-k '.$df_param, $df, PSI_DEBUG);
136
        if ($df!=="") {
2770 rexy 137
            $df = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
138
            if ($get_inodes && PSI_SHOW_INODES) {
139
                if (CommonFunctions::executeProgram('df', '-i '.$df_param, $df2, PSI_DEBUG)) {
140
                    $df2 = preg_split("/\n/", $df2, -1, PREG_SPLIT_NO_EMPTY);
141
                    // Store inode use% in an associative array (df_inodes) for later use
142
                    foreach ($df2 as $df2_line) {
143
                        if (preg_match("/^(\S+).*\s([0-9]+)%/", $df2_line, $inode_buf)) {
144
                            $df_inodes[$inode_buf[1]] = $inode_buf[2];
145
                        }
146
                    }
147
                }
148
            }
149
            foreach ($df as $df_line) {
150
                $df_buf1 = preg_split("/(\%\s)/", $df_line, 3);
151
                if (count($df_buf1) < 2) {
152
                    continue;
153
                }
154
                if (preg_match("/(.*)(\s+)(([0-9]+)(\s+)([0-9]+)(\s+)([\-0-9]+)(\s+)([0-9]+)$)/", $df_buf1[0], $df_buf2)) {
155
                    if (count($df_buf1) == 3) {
156
                        $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[2]);
157
                    } else {
158
                        $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[1]);
159
                    }
160
                    if (count($df_buf) == 6) {
161
                        $df_buf[5] = trim($df_buf[5]);
162
                        $dev = new DiskDevice();
163
                        $dev->setName(trim($df_buf[0]));
164
                        if ($df_buf[2] < 0) {
165
                            $dev->setTotal($df_buf[3] * 1024);
166
                            $dev->setUsed($df_buf[3] * 1024);
167
                        } else {
168
                            $dev->setTotal($df_buf[1] * 1024);
169
                            $dev->setUsed($df_buf[2] * 1024);
170
                            if ($df_buf[3]>0) {
171
                                $dev->setFree($df_buf[3] * 1024);
172
                            }
173
                        }
174
                        if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($df_buf[5]);
175
 
176
                        $notwas = true;
177
                        if (isset($mount_parm)) {
178
                            foreach ($mount_parm as $mount_param) { //name and mountpoint find
179
                                if (($mount_param['name']===trim($df_buf[0])) && ($mount_param['mountpoint']===$df_buf[5])) {
180
                                    $dev->setFsType($mount_param['fstype']);
181
                                    if (PSI_SHOW_MOUNT_OPTION && (trim($mount_param['options'])!=="")) {
182
                                        if (PSI_SHOW_MOUNT_CREDENTIALS) {
183
                                            $dev->setOptions($mount_param['options']);
184
                                        } else {
185
                                            $mpo=$mount_param['options'];
186
 
187
                                            $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
188
                                            $mpo=preg_replace('/,guest,/i', ',', $mpo);
189
 
190
                                            $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
191
                                            $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
192
 
193
                                            $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
194
                                            $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
195
 
196
                                            $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
197
                                            $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
198
 
199
                                            $dev->setOptions($mpo);
200
                                        }
201
                                    }
202
                                    $notwas = false;
203
                                    break;
204
                                }
205
                            }
206
                            if ($notwas) foreach ($mount_parm as $mount_param) { //mountpoint find
207
                                if ($mount_param['mountpoint']===$df_buf[5]) {
208
                                    $dev->setFsType($mount_param['fstype']);
209
                                    if (PSI_SHOW_MOUNT_OPTION && (trim($mount_param['options'])!=="")) {
210
                                        if (PSI_SHOW_MOUNT_CREDENTIALS) {
211
                                            $dev->setOptions($mount_param['options']);
212
                                        } else {
213
                                            $mpo=$mount_param['options'];
214
 
215
                                            $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
216
                                            $mpo=preg_replace('/,guest,/i', ',', $mpo);
217
 
218
                                            $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
219
                                            $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
220
 
221
                                            $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
222
                                            $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
223
 
224
                                            $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
225
                                            $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
226
 
227
                                            $dev->setOptions($mpo);
228
                                        }
229
                                    }
230
                                    $notwas = false;
231
                                    break;
232
                                }
233
                            }
234
                        }
235
 
236
                        if ($notwas) {
237
                            $dev->setFsType('unknown');
238
                        }
239
 
240
                        if ($get_inodes && PSI_SHOW_INODES && isset($df_inodes[trim($df_buf[0])])) {
241
                            $dev->setPercentInodesUsed($df_inodes[trim($df_buf[0])]);
242
                        }
243
                        $arrResult[] = $dev;
244
                    }
245
                }
246
            }
247
        } else {
248
            if (isset($mount_parm)) {
249
                foreach ($mount_parm as $mount_param) {
250
                    if (is_dir($mount_param['mountpoint'])) {
251
                        $total = disk_total_space($mount_param['mountpoint']);
252
                        if (($mount_param['fstype'] != 'none') && ($total > 0)) {
253
                            $dev = new DiskDevice();
254
                            $dev->setName($mount_param['name']);
255
                            $dev->setFsType($mount_param['fstype']);
256
 
257
                            if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($mount_param['mountpoint']);
258
 
259
                            $dev->setTotal($total);
260
                            $free = disk_free_space($mount_param['mountpoint']);
261
                            if ($free > 0) {
262
                                $dev->setFree($free);
263
                            } else {
264
                                $free = 0;
265
                            }
266
                            if ($total > $free) $dev->setUsed($total - $free);
267
 
268
                            if (PSI_SHOW_MOUNT_OPTION) {
269
                                if (PSI_SHOW_MOUNT_CREDENTIALS) {
270
                                    $dev->setOptions($mount_param['options']);
271
                                } else {
272
                                    $mpo=$mount_param['options'];
273
 
274
                                    $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
275
                                    $mpo=preg_replace('/,guest,/i', ',', $mpo);
276
 
277
                                    $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
278
                                    $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
279
 
280
                                    $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
281
                                    $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
282
 
283
                                    $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
284
                                    $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
285
 
286
                                    $dev->setOptions($mpo);
287
                                }
288
                            }
289
                            $arrResult[] = $dev;
290
                        }
291
                    }
292
                }
293
            }
294
        }
295
 
296
        return $arrResult;
297
    }
298
}