Passed
Branch feature/babel (9a8402)
by John
03:52
created

CrawlerBase::insertProblem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Babel\Crawl;
4
5
use App\Models\ProblemModel;
6
use KubAT\PhpSimple\HtmlDomParser;
7
use Auth;
8
9
class CrawlerBase
10
{
11
    public $pro=[
12
        'file'=> 0,
13
        'pcode'=>'',
14
        'solved_count'=>'',
15
        'time_limit'=>'',
16
        'memory_limit'=>'',
17
        'title'=>'',
18
        'OJ'=>'',
19
        'description'=>'',
20
        'input'=>'',
21
        'output'=>'',
22
        'note'=>'',
23
        'input_type'=>'',
24
        'output_type'=>'',
25
        'contest_id'=>'',
26
        'index_id'=>'',
27
        'origin'=>'',
28
        'source'=>'',
29
        'sample'=>[],
30
        'markdown'=>0,
31
        'tot_score'=>1,
32
        'partial'=>0,
33
        'special_compiler'=>null,
34
    ];
35
36
    public $data=null;
37
38
    /**
39
     * Initial
40
     *
41
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Babel\Crawl\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
42
     */
43
    public function __construct()
44
    {
45
    }
46
47
    public static function cmp($a, $b)
48
    {
49
        return ($a[1]>$b[1]) ?-1 : 1;
50
    }
51
52
    private function _cacheImage($data)
53
    {
54
        if(!isset($data["ori"]) || !isset($data["path"]) || !isset($data["baseurl"]) || !isset($data["space_deli"]) || !isset($data["cookie"])){
55
            throw new Exception("data is not completely exist in cacheImage");
0 ignored issues
show
Bug introduced by
The type App\Babel\Crawl\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
56
        }
57
        $ori = $data["ori"];
58
        $path = $data["path"];
59
        $baseurl = $data["baseurl"];
60
        $space_deli = $data["space_deli"];
61
        $cookie = $data["cookie"];
62
63
        $para["path"]=$path;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$para was never initialized. Although not strictly required by PHP, it is generally a good practice to add $para = array(); before regardless.
Loading history...
64
        $para["base"]=$baseurl;
65
        $para["trans"]=!$space_deli;
66
        $para["cookie"]=$cookie;
67
68
        if ($space_deli) {
69
            $reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\' >]*)[^>]*>/si";
70
        } else {
71
            $reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\'>]*)[^>]*>/si";
72
        }
73
74
        return preg_replace_callback($reg, function($matches) use ($para) {
75
            global $config;
76
            $url=trim($matches[1]);
77
            if (stripos($url, "http://")===false && stripos($url, "https://")===false) {
78
                if ($para["trans"]) {
79
                    $url=str_replace(" ", "%20", $url);
80
                }
81
                $url=$para["base"].$url;
82
            }
83
            $name=basename($url);
84
            $name="images/".strtr($name, ":", "_");
85
            $result=str_replace(trim($matches[1]), "online_Judges/spoj/images/".strtr(basename($url), ":", "_"), $matches[0]);
86
            $ch=curl_init();
87
            curl_setopt($ch, CURLOPT_URL, $url);
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

87
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
Loading history...
88
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
89
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
90
            if ($para["cookie"]!="") {
91
                curl_setopt($ch, CURLOPT_COOKIEFILE, $para["cookie"]);
92
            }
93
            curl_setopt($ch, CURLOPT_HEADER, 0);
94
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
95
            $content=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

95
            $content=curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
96
            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

96
            curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
97
            $fp=fopen($name, "wb");
98
            fwrite($fp, $content);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fwrite() 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

98
            fwrite(/** @scrutinizer ignore-type */ $fp, $content);
Loading history...
99
            fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() 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

99
            fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
100
            return $result;
101
        }, $ori);
102
    }
103
104
    public function procInfo($data)
105
    {
106
        if(isset($data["path"]))       $path = $data["path"];             else throw new Exception("path is not exist in data");
107
        if(isset($data["baseurl"]))    $baseurl = $data["baseurl"];       else throw new Exception("baseurl is not exist in data");
108
        if(isset($data["space_deli"])) $space_deli = $data["space_deli"]; else $space_deli = true;
109
        if(isset($data["cookie"]))     $cookie = $data["cookie"];         else $cookie = "";
110
111
        $this->pro["description"]=$this->_cacheImage($this->pro["description"], $path, $baseurl, $space_deli, $cookie);
0 ignored issues
show
Unused Code introduced by
The call to App\Babel\Crawl\CrawlerBase::_cacheImage() has too many arguments starting with $path. ( Ignorable by Annotation )

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

111
        /** @scrutinizer ignore-call */ 
112
        $this->pro["description"]=$this->_cacheImage($this->pro["description"], $path, $baseurl, $space_deli, $cookie);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
112
        $this->pro["input"]=$this->_cacheImage($this->pro["input"], $path, $baseurl, $space_deli, $cookie);
113
        $this->pro["output"]=$this->_cacheImage($this->pro["output"], $path, $baseurl, $space_deli, $cookie);
114
        $this->pro["note"]=$this->_cacheImage($this->pro["note"], $path, $baseurl, $space_deli, $cookie);
115
    }
116
117
    public function getUrl($url)
118
    {
119
        $ch=curl_init();
120
        curl_setopt($ch, CURLOPT_URL, $url);
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

120
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
Loading history...
121
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
122
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
123
        curl_setopt($ch, CURLOPT_HEADER, 0);
124
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
125
        $content=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

125
        $content=curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
126
        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

126
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
127
        return $content;
128
    }
129
130
    public function insertProblem($oid=2)
0 ignored issues
show
Unused Code introduced by
The parameter $oid is not used and could be removed. ( Ignorable by Annotation )

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

130
    public function insertProblem(/** @scrutinizer ignore-unused */ $oid=2)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        $problemModel=new ProblemModel();
133
        return $problemModel->insertProblem($this->pro);
134
    }
135
136
    public function updateProblem($oid=2)
0 ignored issues
show
Unused Code introduced by
The parameter $oid is not used and could be removed. ( Ignorable by Annotation )

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

136
    public function updateProblem(/** @scrutinizer ignore-unused */ $oid=2)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        $problemModel=new ProblemModel();
139
        return $problemModel->updateProblem($this->pro);
140
    }
141
}
142