Issues (1482)

src/service/SystemService.php (3 issues)

1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
15
// +----------------------------------------------------------------------
16
17
namespace DtApp\ThinkLibrary\service;
18
19
use DtApp\ThinkLibrary\Service;
20
use Exception;
21
22
/**
23
 * 系统服务
24
 * Class SystemService
25
 * @package DtApp\ThinkLibrary\service
26
 */
27
class SystemService extends Service
28
{
29
    /**
30
     * 生成最短URL地址
31
     * @param string $url 路由地址
32
     * @param array $vars PATH 变量
33
     * @param boolean|string $suffix 后缀
34
     * @param boolean|string $domain 域名
35
     * @param boolean|string $fillSuffix 补上后缀
36
     * @return string
37
     */
38
    public function uri($url = '', array $vars = [], $suffix = true, $domain = false, $fillSuffix = false): string
39
    {
40
        $default_app = config('app.default_app', 'index');
41
        $default_action = config('route.default_action', 'index');
42
        $default_controller = config('route.default_controller', 'Index');
43
        $url_html_suffix = config('route.url_html_suffix', 'html');
44
        $pathinfo_depr = config('route.pathinfo_depr', '/');
45
        $url_common_param = config('route.url_common_param', true);
46
        if (empty($url)) {
47
            $url = "{$default_app}/{$default_action}/{$default_controller}";
48
        }
49
        if (empty($suffix) && !empty($fillSuffix)) {
50
            if (empty($url_common_param)) {
51
                $location = $this->app->route->buildUrl($url, $vars)->suffix($suffix)->domain($domain)->build();
52
            } else {
53
                $location = $this->app->route->buildUrl($url, [])->suffix($suffix)->domain($domain)->build();
54
            }
55
            if (empty($vars)) {
56
                $location = substr($location . ($pathinfo_depr) . $this->arr_to_str($vars, $pathinfo_depr), 0, -1) . ".{$url_html_suffix}";
57
            } else {
58
                $location .= ($pathinfo_depr) . $this->arr_to_str($vars, $pathinfo_depr) . ".{$url_html_suffix}";
59
            }
60
        } else {
61
            $location = $this->app->route->buildUrl($url, $vars)->suffix($suffix)->domain($domain)->build();
62
        }
63
        return $location;
64
    }
65
66
    /**
67
     * 二维数组转化为字符串,中间用,隔开
68
     * @param $arr
69
     * @param string $glue
70
     * @return false|string
71
     */
72
    private function arr_to_str($arr, $glue = "/")
73
    {
74
        $t = '';
75
        foreach ($arr as $k => $v) {
76
            $t .= $k . $glue . $v . $glue;
77
        }
78
        $t = substr($t, 0, -1); // 利用字符串截取函数消除最后一个
79
        return $t;
80
    }
81
82
    /**
83
     * @var array
84
     */
85
    private $result = [];
86
87
    /**
88
     * 第一个mac地址
89
     * @var
90
     */
91
    private $macAddr;
92
93
    /**
94
     * 获取电脑MAC地址
95
     * @return mixed
96
     */
97
    public function mac()
98
    {
99
        switch (strtolower(PHP_OS)) {
100
            case "solaris":
101
            case "aix":
102
            case 'unix':
103
                break;
104
            case "linux":
105
                $this->getLinux();
106
                break;
107
            default:
108
                $this->getWindows();
109
                break;
110
        }
111
        $tem = array();
112
        foreach ($this->result as $val) {
113
            if (preg_match("/[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f]/i", $val, $tem)) {
114
                $this->macAddr = $tem[0];//多个网卡时,会返回第一个网卡的mac地址,一般够用。
115
                break;
116
                //$this->macAddrs[] = $temp_array[0];//返回所有的mac地址
117
            }
118
        }
119
        unset($temp_array);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $temp_array seems to be never defined.
Loading history...
120
        return $this->macAddr;
121
    }
122
123
    /**
124
     * Linux系统
125
     * @return array
126
     */
127
    private function getLinux()
128
    {
129
        @exec("ifconfig -a", $this->result);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for exec(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

129
        /** @scrutinizer ignore-unhandled */ @exec("ifconfig -a", $this->result);

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...
130
        return $this->result;
131
    }
132
133
    /**
134
     * Windows系统
135
     * @return array
136
     */
137
    private function getWindows(): array
138
    {
139
        @exec("ipconfig /all", $this->result);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for exec(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

139
        /** @scrutinizer ignore-unhandled */ @exec("ipconfig /all", $this->result);

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...
140
        if ($this->result) {
141
            return $this->result;
142
        }
143
144
        $ipconfig = $_SERVER["WINDIR"] . "\system32\ipconfig.exe";
145
        if (is_file($ipconfig)) {
146
            @exec($ipconfig . " /all", $this->result);
147
            return $this->result;
148
        }
149
150
        @exec($_SERVER["WINDIR"] . "\system\ipconfig.exe /all", $this->result);
151
        return $this->result;
152
    }
153
154
    /**
155
     * 获取Linux服务器IP
156
     * @return string
157
     */
158
    public function linuxIp()
159
    {
160
        try {
161
            $ip_cmd = "ifconfig eth0 | sed -n '/inet addr/p' | awk '{print $2}' | awk -F ':' '{print $2}'";
162
            return trim(exec($ip_cmd));
163
        } catch (Exception $e) {
164
            return "0.0.0.0";
165
        }
166
    }
167
}
168