get_server_ip()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 12
rs 9.6111
1
<?php
2
3
namespace Freyo\ApiGateway\Kernel\Support;
4
5
/**
6
 * Generate a signature.
7
 *
8
 * @param array  $attributes
9
 * @param string $key
10
 *
11
 * @return string
12
 */
13
function generate_sign(array $headers, $secretKey)
14
{
15
    $headers = array_map(function ($key, $value) {
16
        return strtolower($key) . ': ' . $value;
17
    }, array_keys($headers), $headers);
18
19
    $sourceString = implode("\n", $headers);
20
21
    return base64_encode(hash_hmac('sha1', $sourceString, $secretKey, true));
22
}
23
24
/**
25
 * Get client ip.
26
 *
27
 * @return string
28
 */
29
function get_client_ip()
30
{
31
    if (!empty($_SERVER['REMOTE_ADDR'])) {
32
        $ip = $_SERVER['REMOTE_ADDR'];
33
    } else {
34
        // for php-cli(phpunit etc.)
35
        $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
36
    }
37
38
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
39
}
40
41
/**
42
 * Get current server ip.
43
 *
44
 * @return string
45
 */
46
function get_server_ip()
47
{
48
    if (!empty($_SERVER['SERVER_ADDR'])) {
49
        $ip = $_SERVER['SERVER_ADDR'];
50
    } elseif (!empty($_SERVER['SERVER_NAME'])) {
51
        $ip = gethostbyname($_SERVER['SERVER_NAME']);
52
    } else {
53
        // for php-cli(phpunit etc.)
54
        $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
55
    }
56
57
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
58
}
59
60
/**
61
 * Return current url.
62
 *
63
 * @return string
64
 */
65
function current_url()
66
{
67
    $protocol = 'http://';
68
69
    if ((!empty($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS']) ||
70
        (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) ? $_SERVER['HTTP_X_FORWARDED_PROTO'] : 'http') === 'https') {
71
        $protocol = 'https://';
72
    }
73
74
    return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
75
}
76
77
/**
78
 * Return random string.
79
 *
80
 * @param string $length
81
 *
82
 * @return string
83
 */
84
function str_random($length)
85
{
86
    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 Freyo\ApiGateway\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

86
    return Str::random(/** @scrutinizer ignore-type */ $length);
Loading history...
87
}
88
89
/**
90
 * @param string $content
91
 * @param string $publicKey
92
 *
93
 * @return string
94
 */
95
function rsa_public_encrypt($content, $publicKey)
96
{
97
    $encrypted = '';
98
    openssl_public_encrypt($content, $encrypted, openssl_pkey_get_public($publicKey), OPENSSL_PKCS1_OAEP_PADDING);
99
100
    return base64_encode($encrypted);
101
}
102