Subversion Repositories ALCASAR

Rev

Rev 2976 | Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * Basic OS Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI OS class
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.OS.inc.php 699 2012-09-15 11:57:13Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * Basic OS functions for all OS classes
17
 *
18
 * @category  PHP
19
 * @package   PSI OS class
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
abstract class OS implements PSI_Interface_OS
27
{
28
    /**
29
     * object for error handling
30
     *
31
     * @var PSI_Error
32
     */
33
    protected $error;
34
 
35
    /**
36
     * block name
37
     *
38
     * @var string
39
     */
40
    protected $blockname = false;
41
 
42
    /**
43
     * @var System
44
     */
45
    protected $sys;
46
 
47
    /**
48
     * build the global Error object
49
     */
50
    public function __construct($blockname = false)
51
    {
52
        $this->error = PSI_Error::singleton();
53
        $this->sys = new System();
54
        $this->blockname = $blockname;
55
    }
56
 
57
    /**
58
     * get os specific encoding
59
     *
60
     * @see PSI_Interface_OS::getEncoding()
61
     *
62
     * @return string
63
     */
64
    public function getEncoding()
65
    {
66
        return PSI_SYSTEM_CODEPAGE;
67
    }
68
 
69
    /**
70
     * get os specific language
71
     *
72
     * @see PSI_Interface_OS::getLanguage()
73
     *
74
     * @return string
75
     */
76
    public function getLanguage()
77
    {
78
        return PSI_SYSTEM_LANG;
79
    }
80
 
81
    /**
82
     * get block name
83
     *
84
     * @see PSI_Interface_OS::getBlockName()
85
     *
86
     * @return string
87
     */
88
    public function getBlockName()
89
    {
90
        return $this->blockname;
91
    }
92
 
93
    /**
94
     * Number of Users
95
     *
96
     * @return void
97
     */
98
    protected function _users()
99
    {
100
        if (CommonFunctions::executeProgram('who', '', $strBuf, PSI_DEBUG)) {
101
            if (strlen($strBuf) > 0) {
102
                $lines = preg_split('/\n/', $strBuf);
103
                $this->sys->setUsers(count($lines));
104
            }
105
        } elseif (CommonFunctions::executeProgram('uptime', '', $buf, PSI_DEBUG) && preg_match("/,\s+(\d+)\s+user[s]?,/", $buf, $ar_buf)) {
106
        //} elseif (CommonFunctions::executeProgram('uptime', '', $buf) && preg_match("/,\s+(\d+)\s+user[s]?,\s+load average[s]?:\s+(.*),\s+(.*),\s+(.*)$/", $buf, $ar_buf)) {
107
            $this->sys->setUsers($ar_buf[1]);
108
        } else {
109
            $processlist = glob('/proc/*/cmdline', GLOB_NOSORT);
110
            if (is_array($processlist) && (($total = count($processlist)) > 0)) {
111
                $count = 0;
112
                $buf = "";
113
                for ($i = 0; $i < $total; $i++) {
114
                    if (CommonFunctions::rfts($processlist[$i], $buf, 0, 4096, false)) {
115
                        $name = str_replace(chr(0), ' ', trim($buf));
116
                        if (preg_match("/^-/", $name)) {
117
                            $count++;
118
                        }
119
                    }
120
                }
121
                if ($count > 0) {
122
                    $this->sys->setUsers($count);
123
                }
124
            }
125
        }
126
    }
127
 
128
    /**
129
     * IP of the Host
130
     *
131
     * @return void
132
     */
133
    protected function _ip()
134
    {
135
        if (PSI_USE_VHOST === true) {
136
           if ((CommonFunctions::readenv('SERVER_ADDR', $result) || CommonFunctions::readenv('LOCAL_ADDR', $result)) //is server address defined
137
               && !strstr($result, '.') && strstr($result, ':')) { //is IPv6, quick version of preg_match('/\(([[0-9A-Fa-f\:]+)\)/', $result)
138
                $dnsrec = dns_get_record($this->sys->getHostname(), DNS_AAAA);
139
                if (isset($dnsrec[0]['ipv6'])) { //is DNS IPv6 record
140
                    $this->sys->setIp($dnsrec[0]['ipv6']); //from DNS (avoid IPv6 NAT translation)
141
                } else {
142
                    $this->sys->setIp(preg_replace('/^::ffff:/i', '', $result)); //from SERVER_ADDR or LOCAL_ADDR
143
                }
144
            } else {
145
                $this->sys->setIp(gethostbyname($this->sys->getHostname())); //IPv4 only
146
            }
147
        } else {
148
            if (CommonFunctions::readenv('SERVER_ADDR', $result) || CommonFunctions::readenv('LOCAL_ADDR', $result)) {
149
                $this->sys->setIp(preg_replace('/^::ffff:/i', '', $result));
150
            } else {
151
                $this->sys->setIp(gethostbyname($this->sys->getHostname()));
152
            }
153
        }
154
    }
155
 
156
    /**
157
     * get the filled or unfilled (with default values) System object
158
     *
159
     * @see PSI_Interface_OS::getSys()
160
     *
161
     * @return System
162
     */
163
    final public function getSys()
164
    {
165
        $this->build();
166
        if (!$this->blockname || $this->blockname==='vitals') {
167
            $this->_ip();
168
        }
169
 
170
        return $this->sys;
171
    }
172
}