Completed
Push — master ( c365cd...202f3c )
by Tom
05:19
created

OperatingSystem::isRoot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
/*
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Util;
9
10
/**
11
 * Class OperatingSystem
12
 *
13
 * @package N98\Util
14
 */
15
class OperatingSystem
16
{
17
    /**
18
     * @var int
19
     */
20
    const UID_ROOT = 0;
21
22
    /**
23
     * Returns true if operating system is
24
     * based on GNU linux.
25
     *
26
     * @return boolean
27
     */
28
    public static function isLinux()
29
    {
30
        return (bool) stristr(PHP_OS, 'linux');
31
    }
32
33
    /**
34
     * Returns true if operating system is
35
     * based on Microsoft Windows.
36
     *
37
     * @return boolean
38
     */
39
    public static function isWindows()
40
    {
41
        return strtolower(substr(PHP_OS, 0, 3)) === 'win';
42
    }
43
44
    /**
45
     * Returns true if operating system is
46
     * based on novell netware.
47
     *
48
     * @return boolean
49
     */
50
    public static function isNetware()
51
    {
52
        return (bool) stristr(PHP_OS, 'netware');
53
    }
54
55
    /**
56
     * Returns true if operating system is
57
     * based on apple MacOS.
58
     *
59
     * @return boolean
60
     */
61
    public static function isMacOs()
62
    {
63
        return stristr(PHP_OS, 'darwin') || stristr(PHP_OS, 'mac');
64
    }
65
66
    /**
67
     * @param string $program
68
     * @return bool
69
     */
70
    public static function isProgramInstalled($program)
71
    {
72
        if (self::isWindows()) {
73
            return WindowsSystem::isProgramInstalled($program);
74
        }
75
76
        $out = null;
77
        $return = null;
78
        @exec('which ' . $program, $out, $return);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
79
80
        return $return === 0;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public static function getHomeDir()
87
    {
88
        if (self::isWindows()) {
89
            return getenv('USERPROFILE');
90
        }
91
92
        return getenv('HOME');
93
    }
94
95
    /**
96
     * Test for Root UID on a POSIX system if posix_getuid() is available.
97
     *
98
     * Returns false negatives if posix_getuid() is not available.
99
     *
100
     * @return bool
101
     */
102
    public static function isRoot()
103
    {
104
        return function_exists('posix_getuid') && posix_getuid() === self::UID_ROOT;
105
    }
106
107
    /**
108
     * get current working directory
109
     *
110
     * @return string the current working directory on success, or false on failure.
111
     */
112
    public static function getCwd()
113
    {
114
        return getcwd();
115
    }
116
117
    /**
118
     * Retrieve path to php binary
119
     *
120
     * @return string
121
     */
122
    public static function getPhpBinary()
123
    {
124
        // PHP_BINARY (>= php 5.4)
125
        if (defined('PHP_BINARY')) {
126
            return PHP_BINARY;
127
        }
128
        if (self::isWindows()) {
129
            return 'php';
130
        }
131
        return '/usr/bin/env php';
132
    }
133
}
134