Passed
Push — master ( 609799...41987d )
by John
04:18
created

CrawlerBase   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_url() 0 11 1
A pcrawler_process_info() 0 6 1
B process_and_get_image() 0 41 6
A insert_problem() 0 4 1
A __construct() 0 2 1
A update_problem() 0 4 1
A cmp() 0 3 2
1
<?php
2
3
namespace App\Babel\Crawler;
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\Crawler\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
    public function process_and_get_image($ori, $path, $baseurl, $space_deli, $cookie)
53
    {
54
        $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...
55
        $para["base"]=$baseurl;
56
        $para["trans"]=!$space_deli;
57
        $para["cookie"]=$cookie;
58
59
        if ($space_deli) {
60
            $reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\' >]*)[^>]*>/si";
61
        } else {
62
            $reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\'>]*)[^>]*>/si";
63
        }
64
65
        return preg_replace_callback($reg, function($matches) use ($para) {
66
            global $config;
67
            $url=trim($matches[1]);
68
            if (stripos($url, "http://")===false && stripos($url, "https://")===false) {
69
                if ($para["trans"]) {
70
                    $url=str_replace(" ", "%20", $url);
71
                }
72
                $url=$para["base"].$url;
73
            }
74
            $name=basename($url);
75
            $name="images/".strtr($name, ":", "_");
76
            $result=str_replace(trim($matches[1]), "online_Judges/spoj/images/".strtr(basename($url), ":", "_"), $matches[0]);
77
            $ch=curl_init();
78
            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

78
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
Loading history...
79
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
80
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
81
            if ($para["cookie"]!="") {
82
                curl_setopt($ch, CURLOPT_COOKIEFILE, $para["cookie"]);
83
            }
84
            curl_setopt($ch, CURLOPT_HEADER, 0);
85
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
86
            $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

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

87
            curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
88
            $fp=fopen($name, "wb");
89
            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

89
            fwrite(/** @scrutinizer ignore-type */ $fp, $content);
Loading history...
90
            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

90
            fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
91
            return $result;
92
        }, $ori);
93
    }
94
95
    public function pcrawler_process_info($path, $baseurl, $space_deli=true, $cookie="")
96
    {
97
        $this->pro["description"]=$this->process_and_get_image($this->pro["description"], $path, $baseurl, $space_deli, $cookie);
98
        $this->pro["input"]=$this->process_and_get_image($this->pro["input"], $path, $baseurl, $space_deli, $cookie);
99
        $this->pro["output"]=$this->process_and_get_image($this->pro["output"], $path, $baseurl, $space_deli, $cookie);
100
        $this->pro["note"]=$this->process_and_get_image($this->pro["note"], $path, $baseurl, $space_deli, $cookie);
101
    }
102
103
    public function get_url($url)
104
    {
105
        $ch=curl_init();
106
        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

106
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
Loading history...
107
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
108
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
109
        curl_setopt($ch, CURLOPT_HEADER, 0);
110
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
111
        $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

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

112
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
113
        return $content;
114
    }
115
116
    public function insert_problem($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

116
    public function insert_problem(/** @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...
117
    {
118
        $problemModel=new ProblemModel();
119
        return $problemModel->insertProblem($this->pro);
120
    }
121
122
    public function update_problem($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

122
    public function update_problem(/** @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...
123
    {
124
        $problemModel=new ProblemModel();
125
        return $problemModel->updateProblem($this->pro);
126
    }
127
}
128