Subversion Repositories ALCASAR

Rev

Rev 2770 | Rev 3037 | Go to most recent revision | 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) {
43
                    $arrParams[$cp-1] = trim($arrParams[$cp-1],'"'); // remove last "
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
            }
120
        } elseif (CommonFunctions::rfts("/etc/mtab", $mount)) {
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
        }
134
        if (CommonFunctions::executeProgram('df', '-k '.$df_param, $df, PSI_DEBUG) && ($df!=="")) {
135
            $df = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
136
            if ($get_inodes && PSI_SHOW_INODES) {
137
                if (CommonFunctions::executeProgram('df', '-i '.$df_param, $df2, PSI_DEBUG)) {
138
                    $df2 = preg_split("/\n/", $df2, -1, PREG_SPLIT_NO_EMPTY);
139
                    // Store inode use% in an associative array (df_inodes) for later use
140
                    foreach ($df2 as $df2_line) {
141
                        if (preg_match("/^(\S+).*\s([0-9]+)%/", $df2_line, $inode_buf)) {
142
                            $df_inodes[$inode_buf[1]] = $inode_buf[2];
143
                        }
144
                    }
145
                }
146
            }
147
            foreach ($df as $df_line) {
148
                $df_buf1 = preg_split("/(\%\s)/", $df_line, 3);
149
                if (count($df_buf1) < 2) {
150
                    continue;
151
                }
152
                if (preg_match("/(.*)(\s+)(([0-9]+)(\s+)([0-9]+)(\s+)([\-0-9]+)(\s+)([0-9]+)$)/", $df_buf1[0], $df_buf2)) {
153
                    if (count($df_buf1) == 3) {
154
                        $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[2]);
155
                    } else {
156
                        $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[1]);
157
                    }
158
                    if (count($df_buf) == 6) {
159
                        $df_buf[5] = trim($df_buf[5]);
160
                        $dev = new DiskDevice();
161
                        $dev->setName(trim($df_buf[0]));
162
                        if ($df_buf[2] < 0) {
163
                            $dev->setTotal($df_buf[3] * 1024);
164
                            $dev->setUsed($df_buf[3] * 1024);
165
                        } else {
166
                            $dev->setTotal($df_buf[1] * 1024);
167
                            $dev->setUsed($df_buf[2] * 1024);
168
                            if ($df_buf[3]>0) {
169
                                $dev->setFree($df_buf[3] * 1024);
170
                            }
171
                        }
172
                        if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($df_buf[5]);
173
 
174
                        $notwas = true;
175
                        if (isset($mount_parm)) {
176
                            foreach ($mount_parm as $mount_param) { //name and mountpoint find
177
                                if (($mount_param['name']===trim($df_buf[0])) && ($mount_param['mountpoint']===$df_buf[5])) {
178
                                    $dev->setFsType($mount_param['fstype']);
179
                                    if (PSI_SHOW_MOUNT_OPTION && (trim($mount_param['options'])!=="")) {
180
                                        if (PSI_SHOW_MOUNT_CREDENTIALS) {
181
                                            $dev->setOptions($mount_param['options']);
182
                                        } else {
183
                                            $mpo=$mount_param['options'];
184
 
185
                                            $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
186
                                            $mpo=preg_replace('/,guest,/i', ',', $mpo);
187
 
188
                                            $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
189
                                            $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
190
 
191
                                            $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
192
                                            $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
193
 
194
                                            $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
195
                                            $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
196
 
197
                                            $dev->setOptions($mpo);
198
                                        }
199
                                    }
200
                                    $notwas = false;
201
                                    break;
202
                                }
203
                            }
204
                            if ($notwas) foreach ($mount_parm as $mount_param) { //mountpoint find
205
                                if ($mount_param['mountpoint']===$df_buf[5]) {
206
                                    $dev->setFsType($mount_param['fstype']);
207
                                    if (PSI_SHOW_MOUNT_OPTION && (trim($mount_param['options'])!=="")) {
208
                                        if (PSI_SHOW_MOUNT_CREDENTIALS) {
209
                                            $dev->setOptions($mount_param['options']);
210
                                        } else {
211
                                            $mpo=$mount_param['options'];
212
 
213
                                            $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
214
                                            $mpo=preg_replace('/,guest,/i', ',', $mpo);
215
 
216
                                            $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
217
                                            $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
218
 
219
                                            $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
220
                                            $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
221
 
222
                                            $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
223
                                            $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
224
 
225
                                            $dev->setOptions($mpo);
226
                                        }
227
                                    }
228
                                    $notwas = false;
229
                                    break;
230
                                }
231
                            }
232
                        }
233
 
234
                        if ($notwas) {
235
                            $dev->setFsType('unknown');
236
                        }
237
 
238
                        if ($get_inodes && PSI_SHOW_INODES && isset($df_inodes[trim($df_buf[0])])) {
239
                            $dev->setPercentInodesUsed($df_inodes[trim($df_buf[0])]);
240
                        }
241
                        $arrResult[] = $dev;
242
                    }
243
                }
244
            }
245
        } else {
246
            if (isset($mount_parm)) {
247
                foreach ($mount_parm as $mount_param) {
248
                    if (is_dir($mount_param['mountpoint'])) {
249
                        $total = disk_total_space($mount_param['mountpoint']);
250
                        if (($mount_param['fstype'] != 'none') && ($total > 0)) {
251
                            $dev = new DiskDevice();
252
                            $dev->setName($mount_param['name']);
253
                            $dev->setFsType($mount_param['fstype']);
254
 
255
                            if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($mount_param['mountpoint']);
256
 
257
                            $dev->setTotal($total);
258
                            $free = disk_free_space($mount_param['mountpoint']);
259
                            if ($free > 0) {
260
                                $dev->setFree($free);
261
                            } else {
262
                                $free = 0;
263
                            }
264
                            if ($total > $free) $dev->setUsed($total - $free);
265
 
266
                            if (PSI_SHOW_MOUNT_OPTION) {
267
                                if (PSI_SHOW_MOUNT_CREDENTIALS) {
268
                                    $dev->setOptions($mount_param['options']);
269
                                } else {
270
                                    $mpo=$mount_param['options'];
271
 
272
                                    $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
273
                                    $mpo=preg_replace('/,guest,/i', ',', $mpo);
274
 
275
                                    $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
276
                                    $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
277
 
278
                                    $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
279
                                    $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
280
 
281
                                    $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
282
                                    $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
283
 
284
                                    $dev->setOptions($mpo);
285
                                }
286
                            }
287
                            $arrResult[] = $dev;
288
                        }
289
                    }
290
                }
291
            }
292
        }
293
 
294
        return $arrResult;
295
    }
296
}