OS::getEncoding()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 <[email protected]>
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
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 <[email protected]>
21
 * @copyright 2009 phpSysInfo
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
23
 * @version   Release: 3.0
24
 * @link      http://phpsysinfo.sourceforge.net
25
 */
26
abstract class OS implements PSI_Interface_OS
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
27
{
28
    /**
29
     * object for error handling
30
     *
31
     * @var Error
32
     */
33
    protected $error;
34
35
    /**
36
     * @var System
37
     */
38
    protected $sys;
39
40
    /**
41
     * build the global Error object
42
     */
43
    public function __construct()
44
    {
45
        $this->error = PSI_Error::singleton();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->error is correct as \PSI_Error::singleton() (which targets PSI_Error::singleton()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
46
        $this->sys = new System();
47
    }
48
49
    /**
50
     * get os specific encoding
51
     *
52
     * @see PSI_Interface_OS::getEncoding()
53
     *
54
     * @return string
55
     */
56
    public function getEncoding()
57
    {
58
        return PSI_SYSTEM_CODEPAGE;
59
    }
60
    /**
61
     * get os specific language
62
     *
63
     * @see PSI_Interface_OS::getLanguage()
64
     *
65
     * @return string
66
     */
67
    public function getLanguage()
68
    {
69
        return PSI_SYSTEM_LANG;
70
    }
71
72
    /**
73
     * Number of Users
74
     *
75
     * @return void
76
     */
77
    protected function _users()
78
    {
79
        if (CommonFunctions::executeProgram('who', '', $strBuf, PSI_DEBUG)) {
80
            if (strlen($strBuf) > 0) {
81
                $lines = preg_split('/\n/', $strBuf);
82
                $this->sys->setUsers(count($lines));
83
            }
84
        } elseif (CommonFunctions::executeProgram('uptime', '', $buf, PSI_DEBUG) && preg_match("/,\s+(\d+)\s+user[s]?,/", $buf, $ar_buf)) {
85
        //} elseif (CommonFunctions::executeProgram('uptime', '', $buf) && preg_match("/,\s+(\d+)\s+user[s]?,\s+load average[s]?:\s+(.*),\s+(.*),\s+(.*)$/", $buf, $ar_buf)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
            $this->sys->setUsers($ar_buf[1]);
87
        } else {
88
            $processlist = glob('/proc/*/cmdline', GLOB_NOSORT);
89
            if (($total = count($processlist)) > 0) {
90
                $count = 0;
91
                $buf = "";
92
                for ($i = 0; $i < $total; $i++) {
93
                    if (CommonFunctions::rfts($processlist[$i], $buf, 0, 4096, false)) {
94
                        $name = str_replace(chr(0), ' ', trim($buf));
95
                        if (preg_match("/^-/", $name)) {
96
                            $count++;
97
                        }
98
                    }
99
                }
100
                if ($count > 0) {
101
                    $this->sys->setUsers($count);
102
                }
103
            }
104
        }
105
    }
106
107
    /**
108
     * IP of the Host
109
     *
110
     * @return void
111
     */
112
    protected function _ip()
113
    {
114
        if (PSI_USE_VHOST === true) {
115
            if ((($result = getenv('SERVER_ADDR')) || ($result = getenv('LOCAL_ADDR'))) //is server address defined
116
               && !strstr($result, '.') && strstr($result, ':')){ //is IPv6, quick version of preg_match('/\(([[0-9A-Fa-f\:]+)\)/', $result)
117
                $dnsrec = dns_get_record($this->sys->getHostname(), DNS_AAAA);
118
                if (isset($dnsrec[0]['ipv6'])) { //is DNS IPv6 record
119
                    $this->sys->setIp($dnsrec[0]['ipv6']); //from DNS (avoid IPv6 NAT translation)
120
                } else {
121
                    $this->sys->setIp(preg_replace('/^::ffff:/i', '', $result)); //from SERVER_ADDR or LOCAL_ADDR
122
                }
123
            } else {
124
                $this->sys->setIp(gethostbyname($this->sys->getHostname())); //IPv4 only
125
            }
126
        } else {
127
            if (($result = getenv('SERVER_ADDR')) || ($result = getenv('LOCAL_ADDR'))) {
128
                $this->sys->setIp(preg_replace('/^::ffff:/i', '', $result));
129
            } else {
130
                $this->sys->setIp(gethostbyname($this->sys->getHostname()));
131
            }
132
        }
133
    }
134
135
    /**
136
     * get the filled or unfilled (with default values) System object
137
     *
138
     * @see PSI_Interface_OS::getSys()
139
     *
140
     * @return System
141
     */
142
    final public function getSys()
143
    {
144
        $this->build();
145
        $this->_ip();
146
147
        return $this->sys;
148
    }
149
}
150