Af_Tumblr_1280::api_version()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
class Af_Tumblr_1280 extends Plugin {
3
    private $host;
4
5
    public function about() {
6
        return array(1.0,
7
            "Replace Tumblr pictures and videos with largest size if available (requires CURL)",
8
            "fox");
9
    }
10
11
    public function flags() {
12
        return array("needs_curl" => true);
13
    }
14
15
    public function init($host) {
16
        $this->host = $host;
17
18
        if (function_exists("curl_init")) {
19
            $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
20
        }
21
    }
22
23
    public function hook_article_filter($article) {
24
25
        if (!function_exists("curl_init") || ini_get("open_basedir")) {
26
                    return $article;
27
        }
28
29
        $doc = new DOMDocument();
30
        $doc->loadHTML('<?xml encoding="UTF-8">'.$article["content"]);
31
32
        $found = false;
33
34
        if ($doc) {
0 ignored issues
show
introduced by
$doc is of type DOMDocument, thus it always evaluated to true.
Loading history...
35
            $xpath = new DOMXpath($doc);
36
37
            $images = $xpath->query('(//img[contains(@src, \'media.tumblr.com\')])');
38
39
            foreach ($images as $img) {
40
                $src = $img->getAttribute("src");
41
42
                $test_src = preg_replace("/_\d{3}.(jpg|gif|png)/", "_1280.$1", $src);
43
44
                if ($src != $test_src) {
45
46
                    $ch = curl_init($test_src);
47
                    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
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

47
                    curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_TIMEOUT, 5);
Loading history...
48
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
49
                    curl_setopt($ch, CURLOPT_HEADER, true);
50
                    curl_setopt($ch, CURLOPT_NOBODY, true);
51
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
52
                    curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
53
54
                    @$result = 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

54
                    @$result = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
55
                    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_getinfo() 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

55
                    $http_code = curl_getinfo(/** @scrutinizer ignore-type */ $ch, CURLINFO_HTTP_CODE);
Loading history...
56
57
                    if ($result && $http_code == 200) {
58
                        $img->setAttribute("src", $test_src);
59
                        $found = true;
60
                    }
61
                }
62
            }
63
64
            $video_sources = $xpath->query('//video/source[contains(@src, \'.tumblr.com/video_file\')]');
65
66
            foreach ($video_sources as $source) {
67
                $src = $source->getAttribute("src");
68
69
                $new_src = preg_replace("/\/\d{3}$/", "", $src);
70
71
                if ($src != $new_src) {
72
                    $source->setAttribute("src", $new_src);
73
                    $found = true;
74
                }
75
            }
76
77
            if ($found) {
78
                $doc->removeChild($doc->firstChild); //remove doctype
79
                $article["content"] = $doc->saveHTML();
80
            }
81
        }
82
83
        return $article;
84
85
    }
86
87
88
    public function api_version() {
89
        return 2;
90
    }
91
92
}
93