phpGet::check_http()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/** This file is part of KCFinder project
4
 *
5
 * @desc      Helper class for downloading URLs
6
 * @package   KCFinder
7
 * @version   3.12
8
 * @author    Pavel Tzonkov <[email protected]>
9
 * @copyright 2010-2014 KCFinder Project
10
 * @license   http://opensource.org/licenses/GPL-3.0 GPLv3
11
 * @license   http://opensource.org/licenses/LGPL-3.0 LGPLv3
12
 * @link      http://kcfinder.sunhater.com
13
 */
14
15
namespace kcfinder;
16
17
class phpGet
18
{
19
20
    public static $methods    = ['curl', 'fopen', 'http', 'socket'];
21
    public static $urlExpr    = '/^([a-z]+):\/\/((([\p{L}\d\-]+\.)+[\p{L}]{1,4})(\:(\d{1,6}))?(\/.*)*)?$/u';
22
    public static $socketExpr = '/^[A-Z]+\/\d+(\.\d+)\s+\d+\s+OK\s*([a-zA-Z0-9\-]+\:\s*[^\n]*\n)*\s*([a-f0-9]+\r?\n)?(.*)$/s';
23
24
    public static function get($url, $file = null, $method = null)
25
    {
26
        if (true === $file) {
27
            $file = basename($url);
28
        }
29
        if (null !== $file) {
30
            if (is_dir($file)) {
31
                $file = rtrim($file, '/') . '/' . basename($url);
32
            }
33
            $exists = file_exists($file);
34
            if (!@touch($file)) {
35
                return false;
36
            }
37
            if (!$exists) {
38
                @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). 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

38
                /** @scrutinizer ignore-unhandled */ @unlink($file);

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...
39
            }
40
        }
41
42
        if (in_array($method, self::$methods, true)) {
43
            $check = "check_$method";
44
            $get   = "get_$method";
45
            if (self::$check()) {
46
                $content = self::$get($url);
47
            } else {
48
                return false;
49
            }
50
        } else {
51
            foreach (self::$methods as $m) {
52
                $check = "check_$m";
53
                $get   = "get_$m";
54
                if (self::$check()) {
55
                    $content = self::$get($url);
56
                    if (((true !== $method) && ('all' != strtolower($method)))
57
                        || (false !== $content)) {
58
                        break;
59
                    }
60
                }
61
            }
62
            if (!isset($content)) {
63
                return false;
64
            }
65
        }
66
67
        return (null !== $file) ? @file_put_contents($file, $content) : $content;
68
    }
69
70
    public static function get_fopen($url)
71
    {
72
        return @file_get_contents($url);
73
    }
74
75
    public static function get_curl($url)
76
    {
77
        return ((false !== ($curl = @curl_init($url)))
78
                && (@ob_start() || (@curl_close($curl) && false))
0 ignored issues
show
Bug introduced by
Are you sure the usage of curl_close($curl) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
79
                && (@curl_exec($curl) || (@curl_close($curl) && false))
0 ignored issues
show
Bug introduced by
Are you sure the usage of curl_close($curl) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
80
                && ((false !== ($content = @ob_get_clean())) || (@curl_close($curl) && false))
0 ignored issues
show
Bug introduced by
Are you sure the usage of curl_close($curl) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
81
                && (@curl_close($curl) || true)) ? $content : false;
0 ignored issues
show
Bug introduced by
Are you sure the usage of curl_close($curl) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
82
    }
83
84
    public static function get_http($url)
85
    {
86
        return ((false !== ($content = @http_get($url)))
87
                && ((preg_match(self::$socketExpr, $content, $match)
88
                     && false !== ($content = $match[4]))
89
                    || true)) ? $content : false;
90
    }
91
92
    public static function get_socket($url)
93
    {
94
        if (!preg_match(self::$urlExpr, $url, $match)) {
95
            return false;
96
        }
97
98
        $protocol = $match[1];
99
        $host     = $match[3];
100
        $port     = strlen($match[6]) ? $match[6] : 80;
101
        $path     = strlen($match[7]) ? $match[7] : '/';
102
103
        $cmd = "GET $path " . strtoupper($protocol) . "/1.1\r\n" . "Host: $host\r\n" . "Connection: Close\r\n\r\n";
104
105
        if ((false !== ($socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)))
106
            && (false !== @socket_connect($socket, $host, $port))
107
            && (false !== @socket_write($socket, $cmd, strlen($cmd)))
108
            && (false !== ($content = @socket_read($socket, 2048)))) {
109
            do {
110
                $piece   = @socket_read($socket, 2048);
111
                $content .= $piece;
112
            } while ($piece);
113
114
            $content = preg_match(self::$socketExpr, $content, $match) ? $match[4] : false;
115
        }
116
117
        if (isset($socket) && is_resource($socket)) {
118
            @socket_close($socket);
0 ignored issues
show
Bug introduced by
Are you sure the usage of socket_close($socket) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for socket_close(). 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

118
            /** @scrutinizer ignore-unhandled */ @socket_close($socket);

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...
119
        } else {
120
            return false;
121
        }
122
123
        return isset($content) ? $content : false;
124
    }
125
126
    private static function check_fopen()
127
    {
128
        return ini_get('allow_url_fopen')
129
               && function_exists('file_get_contents');
130
    }
131
132
    private static function check_curl()
133
    {
134
        return function_exists('curl_init')
135
               && function_exists('curl_exec')
136
               && function_exists('curl_close')
137
               && function_exists('ob_start')
138
               && function_exists('ob_get_clean');
139
    }
140
141
    private static function check_http()
142
    {
143
        return function_exists('http_get');
144
    }
145
146
    private static function check_socket()
147
    {
148
        return function_exists('socket_create')
149
               && function_exists('socket_connect')
150
               && function_exists('socket_write')
151
               && function_exists('socket_read')
152
               && function_exists('socket_close');
153
    }
154
}
155