Passed
Pull Request — master (#182)
by John
04:01
created

Curl   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 81
dl 0
loc 114
rs 10
c 4
b 0
f 1
wmc 27

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
F post_data() 0 42 13
B login() 0 34 7
B grab_page() 0 27 6
1
<?php
2
namespace App\Babel\Submit;
3
4
use App\Models\SubmissionModel;
5
use Exception;
6
7
interface CurlInterface {
8
    function login($all_data);
9
    function grab_page($all_data);
10
    function post_data($all_data);
11
}
12
class Curl
13
{
14
    public function __construct()
15
    {
16
        //
17
    }
18
19
    protected function login($all_data)
20
    {
21
        if(isset($all_data["url"]))    $url = $all_data["url"];       else throw new Exception("url is not exist in all_data");
22
        if(isset($all_data["data"]))   $data = $all_data["data"];     else throw new Exception("data is not exist in all_data");
23
        if(isset($all_data["oj"]))     $oj = $all_data["oj"];         else throw new Exception("oj is not exist in all_data");
24
        if(isset($all_data["ret"]))    $ret = $all_data["ret"];       else $ret = 'false';
25
        if(isset($all_data["handle"])) $handle = $all_data["handle"]; else $handle = "default";
26
27
        $datapost=curl_init();
28
        $headers=array("Expect:");
29
        $handle=urlencode($handle);
30
31
        curl_setopt($datapost, CURLOPT_CAINFO, babel_path("Cookies/cacert.pem"));
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

31
        curl_setopt(/** @scrutinizer ignore-type */ $datapost, CURLOPT_CAINFO, babel_path("Cookies/cacert.pem"));
Loading history...
32
        curl_setopt($datapost, CURLOPT_URL, $url);
33
        curl_setopt($datapost, CURLOPT_HEADER, true); //
34
        curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers); //
35
        curl_setopt($datapost, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
36
        curl_setopt($datapost, CURLOPT_POST, true);
37
38
        curl_setopt($datapost, CURLOPT_RETURNTRANSFER, $ret);
39
        curl_setopt($datapost, CURLOPT_FOLLOWLOCATION, true);
40
41
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
42
        curl_setopt($datapost, CURLOPT_COOKIEFILE, babel_path("Cookies/{$oj}_{$handle}.cookie"));
43
        curl_setopt($datapost, CURLOPT_COOKIEJAR, babel_path("Cookies/{$oj}_{$handle}.cookie"));
44
        ob_start();
45
        $response=curl_exec($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

45
        $response=curl_exec(/** @scrutinizer ignore-type */ $datapost);
Loading history...
46
        if (curl_errno($datapost)) {
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_errno() 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

46
        if (curl_errno(/** @scrutinizer ignore-type */ $datapost)) {
Loading history...
47
            die(curl_error($datapost));
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

47
            die(curl_error(/** @scrutinizer ignore-type */ $datapost));
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
48
        }
49
        ob_end_clean();
50
        curl_close($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

50
        curl_close(/** @scrutinizer ignore-type */ $datapost);
Loading history...
51
        unset($datapost);
52
        return $response;
53
    }
54
55
    protected function grab_page($all_data)
56
    {
57
        if(isset($all_data["site"]))    $site = $all_data["site"];          else throw new Exception("site is not exist in all_data");
58
        if(isset($all_data["oj"]))      $oj = $all_data["oj"];              else throw new Exception("oj is not exist in all_data");
59
        if(isset($all_data["headers"])) $headers = $all_data["headers"];    else $headers = [];
60
        if(isset($all_data["handle"]))  $handle = $all_data["handle"];      else $handle = "default";
61
62
        $handle=urlencode($handle);
63
64
        $ch=curl_init();
65
        curl_setopt($ch, CURLOPT_CAINFO, babel_path("Cookies/cacert.pem"));
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

65
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_CAINFO, babel_path("Cookies/cacert.pem"));
Loading history...
66
        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
67
        // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
68
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
69
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
70
        curl_setopt($ch, CURLOPT_COOKIEFILE, babel_path("Cookies/{$oj}_{$handle}.cookie"));
71
        curl_setopt($ch, CURLOPT_COOKIEJAR, babel_path("Cookies/{$oj}_{$handle}.cookie"));
72
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
73
        curl_setopt($ch, CURLOPT_URL, $site);
74
        ob_start();
75
        $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

75
        $response=curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
76
        if (curl_errno($ch)) {
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_errno() 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

76
        if (curl_errno(/** @scrutinizer ignore-type */ $ch)) {
Loading history...
77
            die(curl_error($ch));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
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

77
            die(curl_error(/** @scrutinizer ignore-type */ $ch));
Loading history...
78
        }
79
        ob_end_clean();
80
        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

80
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
81
        return $response;
82
    }
83
84
    protected function post_data($all_data)
85
    {
86
        if(isset($all_data["site"]))         $site = $all_data["site"];                 else throw new Exception("site is not exist in all_data");
87
        if(isset($all_data["data"]))         $data = $all_data["data"];                 else throw new Exception("data is not exist in all_data");
88
        if(isset($all_data["oj"]))           $oj = $all_data["oj"];                     else throw new Exception("oj is not exist in all_data");
89
        if(isset($all_data["ret"]))          $ret = $all_data["ret"];                   else $ret = false;
90
        if(isset($all_data["follow"]))       $follow = $all_data["follow"];             else $follow = true;
91
        if(isset($all_data["returnHeader"])) $returnHeader = $all_data["returnHeader"]; else $returnHeader = true;
92
        if(isset($all_data["postJson"]))     $postJson = $all_data["postJson"];         else $postJson = false;
93
        if(isset($all_data["extraHeaders"])) $extraHeaders = $all_data["extraHeaders"]; else $extraHeaders = [];
94
        if(isset($all_data["handle"]))       $handle = $all_data["handle"];             else $handle = "default";
95
96
        $handle=urlencode($handle);
97
98
        $datapost=curl_init();
99
        $headers=array("Expect:");
100
        if ($postJson) {
101
            $data=$data ? json_encode($data) : '{}';
102
            array_push($headers, 'Content-Type: application/json', 'Content-Length: '.strlen($data));
103
        }
104
        curl_setopt($datapost, CURLOPT_CAINFO, babel_path("Cookies/cacert.pem"));
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

104
        curl_setopt(/** @scrutinizer ignore-type */ $datapost, CURLOPT_CAINFO, babel_path("Cookies/cacert.pem"));
Loading history...
105
        curl_setopt($datapost, CURLOPT_URL, $site);
106
        curl_setopt($datapost, CURLOPT_HEADER, $returnHeader);
107
        curl_setopt($datapost, CURLOPT_HTTPHEADER, array_merge($headers, $extraHeaders));
108
        curl_setopt($datapost, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
109
        curl_setopt($datapost, CURLOPT_POST, true);
110
111
        curl_setopt($datapost, CURLOPT_RETURNTRANSFER, $ret);
112
        curl_setopt($datapost, CURLOPT_FOLLOWLOCATION, $follow);
113
114
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
115
        curl_setopt($datapost, CURLOPT_COOKIEFILE, babel_path("Cookies/{$oj}_{$handle}.cookie"));
116
        curl_setopt($datapost, CURLOPT_COOKIEJAR, babel_path("Cookies/{$oj}_{$handle}.cookie"));
117
        ob_start();
118
        $response=curl_exec($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

118
        $response=curl_exec(/** @scrutinizer ignore-type */ $datapost);
Loading history...
119
        if (curl_errno($datapost)) {
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_errno() 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

119
        if (curl_errno(/** @scrutinizer ignore-type */ $datapost)) {
Loading history...
120
            die(curl_error($datapost));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug introduced by
It seems like $datapost 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

120
            die(curl_error(/** @scrutinizer ignore-type */ $datapost));
Loading history...
121
        }
122
        ob_end_clean();
123
        curl_close($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

123
        curl_close(/** @scrutinizer ignore-type */ $datapost);
Loading history...
124
        unset($datapost);
125
        return $response;
126
    }
127
}
128