Passed
Pull Request — master (#1252)
by Keal
02:21
created

get_server_ip()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 12
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel\Support;
13
14
/*
15
 * helpers.
16
 *
17
 * @author overtrue <[email protected]>
18
 */
19
20
/**
21
 * Generate a signature.
22
 *
23
 * @param array  $attributes
24
 * @param string $key
25
 * @param string $encryptMethod
26
 *
27
 * @return string
28
 */
29
function generate_sign(array $attributes, $key, $encryptMethod = 'md5')
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
30
{
31
    ksort($attributes);
32
33
    $attributes['key'] = $key;
34
35
    return strtoupper(call_user_func_array($encryptMethod, [urldecode(http_build_query($attributes))]));
36
}
37
38
/**
39
 * Get client ip.
40
 *
41
 * @return string
42
 */
43
function get_client_ip()
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
44
{
45
    if (!empty($_SERVER['REMOTE_ADDR'])) {
46
        $ip = $_SERVER['REMOTE_ADDR'];
47
    } else {
48
        // for php-cli(phpunit etc.)
49
        $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
50
    }
51
52
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
53
}
54
55
/**
56
 * Get current server ip.
57
 *
58
 * @return string
59
 */
60
function get_server_ip()
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
61
{
62
    if (!empty($_SERVER['SERVER_ADDR'])) {
63
        $ip = $_SERVER['SERVER_ADDR'];
64
    } elseif (!empty($_SERVER['SERVER_NAME'])) {
65
        $ip = gethostbyname($_SERVER['SERVER_NAME']);
66
    } else {
67
        // for php-cli(phpunit etc.)
68
        $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
69
    }
70
71
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
72
}
73
74
/**
75
 * Return current url.
76
 *
77
 * @return string
78
 */
79
function current_url()
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
80
{
81
    $protocol = 'http://';
82
83
    if ((!empty($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS']) || ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? 'http') === 'https') {
84
        $protocol = 'https://';
85
    }
86
87
    return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
88
}
89
90
/**
91
 * Return random string.
92
 *
93
 * @param string $length
94
 *
95
 * @return string
96
 */
97
function str_random($length)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
98
{
99
    return Str::random($length);
0 ignored issues
show
Bug introduced by
$length of type string is incompatible with the type integer expected by parameter $length of EasyWeChat\Kernel\Support\Str::random(). ( Ignorable by Annotation )

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

99
    return Str::random(/** @scrutinizer ignore-type */ $length);
Loading history...
100
}
101
102
/**
103
 * @param string $content
104
 * @param string $publicKey
105
 *
106
 * @return string
107
 */
108
function rsa_public_encrypt($content, $publicKey)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
109
{
110
    $encrypted = '';
111
    openssl_public_encrypt($content, $encrypted, openssl_pkey_get_public($publicKey), OPENSSL_PKCS1_OAEP_PADDING);
112
113
    return base64_encode($encrypted);
114
}
115