Passed
Push — master ( efc233...7383db )
by Ferry
04:51
created

CurlHelper::userAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 8/13/2019
6
 * Time: 10:33 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\helpers;
10
11
12
class CurlHelper
13
{
14
15
    private $url;
16
    private $headers;
17
    private $data;
18
    private $type;
19
    private $timeout;
20
    private $user_agent;
21
    private $referer;
22
    private $cookie;
23
24
    public function __construct($url, $type = "POST")
25
    {
26
        $this->url = $url;
27
        $this->type = $type;
28
        $this->timeout = 30;
29
        $this->user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13';
30
        $this->referer = url("/");
31
        $this->cookie = false;
32
    }
33
34
    public function cookie($enable) {
35
        $this->cookie = $enable;
36
    }
37
38
    public function referer($referer) {
39
        $this->referer = $referer;
40
    }
41
42
    public function userAgent($user_agent) {
43
        $this->user_agent = $user_agent;
44
    }
45
46
    public function timeout($seconds) {
47
        $this->timeout = $seconds;
48
    }
49
50
    public function headers(array $headers) {
51
        $newHeaders = [];
52
        foreach($headers as $key=>$val) {
53
            $newHeaders[] = $key.": ".$val;
54
        }
55
        $this->headers = $newHeaders;
56
    }
57
58
    public function data(array $data) {
59
        $this->data = $data;
60
    }
61
62
    public function send() {
63
        $ch = curl_init($this->url);
64
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->type);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

64
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_CUSTOMREQUEST, $this->type);
Loading history...
65
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
66
        curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
67
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
68
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
69
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->data);
70
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
71
        curl_setopt($ch,CURLOPT_USERAGENT,$this->user_agent);
72
        curl_setopt($ch, CURLOPT_REFERER, $this->referer);
73
        if($this->cookie === true) {
74
            $cookie_dir = storage_path("cookies");
75
            if(!file_exists($cookie_dir)) mkdir($cookie_dir);
76
            $cookie_file = $cookie_dir."/".md5(request()->ip()).".txt";
77
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
78
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
79
        }
80
81
        $response = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

81
        $response = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
82
        $err = curl_error($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

82
        $err = curl_error(/** @scrutinizer ignore-type */ $ch);
Loading history...
83
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

83
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
84
85
        if($err) {
86
            return $err;
87
        }else {
88
            return $response;
89
        }
90
    }
91
}