Passed
Push — master ( 429a42...439608 )
by Shashi Prakash
29s
created

OpenGraph::verify_image_url()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace shweshi\OpenGraph;
4
5
use DOMDocument;
6
7
class OpenGraph
8
{
9
    public function fetch($url, $allMeta = null)
10
    {
11
        $html = $this->curl_get_contents($url);
12
13
        /**
14
         * parsing starts here:.
15
         */
16
        $doc = new DOMDocument();
17
        @$doc->loadHTML('<?xml encoding="utf-8" ?>'.$html);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for loadHTML(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

17
        /** @scrutinizer ignore-unhandled */ @$doc->loadHTML('<?xml encoding="utf-8" ?>'.$html);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
18
19
        $tags = $doc->getElementsByTagName('meta');
20
        $metadata = [];
21
        foreach ($tags as $tag) {
22
            $metaproperty = ($tag->hasAttribute('property')) ? $tag->getAttribute('property') : $tag->getAttribute('name');
23
            if (!$allMeta && $metaproperty && strpos($tag->getAttribute('property'), 'og:') === 0) {
24
                $key = strtr(substr($metaproperty, 3), '-', '_');
25
                $value = $tag->getAttribute('content');
26
            }
27
            if ($allMeta && $metaproperty) {
28
                $key = (strpos($metaproperty, 'og:') === 0) ? strtr(substr($metaproperty, 3), '-', '_') : $metaproperty;
29
                $value = $tag->getAttribute('content');
30
            }
31
            if (!empty($key)) {
32
                $metadata[$key] = $value;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.
Loading history...
33
            }
34
            /*
35
             * Verify image url
36
             */
37
            if (isset($metadata['image'])) {
38
                $isValidImageUrl = $this->verify_image_url($metadata['image']);
39
                if (!$isValidImageUrl) {
40
                    $metadata['image'] = '';
41
                }
42
            }
43
        }
44
45
        return $metadata;
46
    }
47
48
    protected function curl_get_contents($url)
49
    {
50
        $curl = curl_init($url);
51
        curl_setopt($curl, CURLOPT_FAILONERROR, 1);
0 ignored issues
show
Bug introduced by
It seems like $curl 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

51
        curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_FAILONERROR, 1);
Loading history...
52
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
53
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
54
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
55
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
56
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
57
        curl_setopt($curl, CURLOPT_ENCODING, 'UTF-8');
58
        $response = curl_exec($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl 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

58
        $response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
59
        curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl 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

59
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
60
61
        return $response;
62
    }
63
64
    protected function verify_image_url($url)
65
    {
66
        $headers = get_headers($url);
67
68
        return stripos($headers[0], '200 OK') ? true : false;
69
    }
70
}
71