Issues (36)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/NewsScrapper/Adapters/OpenGraphAdapter.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Zrashwani\NewsScrapper\Adapters;
4
5
use \Symfony\Component\DomCrawler\Crawler;
6
7
/**
8
 * Adapter to extract news base on open graph protocol specifications
9
 * @link http://ogp.me/ open graph meta data specifications
10
 * @author Zeid Rashwani <zrashwani.com>
11
 */
12
class OpenGraphAdapter extends AbstractAdapter
13
{
14
    /**
15
     * extract title information from crawler object
16
     * @param Crawler $crawler
17
     * @return string
18
     */
19
    public function extractTitle(Crawler $crawler)
20
    {
21
        $ret = null;
22
23
        $crawler->filterXPath("//head/meta[@property='og:title']")
24
            ->each(
25
                function(Crawler $node) use (&$ret) {
26
                        $ret = $node->attr('content');
27
                }
28
            );
29
30
        //fallback in case document don't have og:title
31
        if (empty($ret) === true) {
32
            $crawler->filterXPath('//h1')
33
                ->each(
34
                    function(Crawler $node) use (&$ret) {
35
                            $ret = $node->text();
36
                    }
37
                );
38
        }
39
        
40
        if (empty($ret) === true) {
41
            $crawler->filterXPath('//head/title')
42
                ->each(
43
                    function(Crawler $node) use (&$ret) {
44
                            $ret = $node->text();
45
                    }
46
                );
47
        }
48
        
49
        return $ret;
50
    }
51
52
    /**
53
     * extract image url from crawler open graph
54
     * @param Crawler $crawler
55
     * @return string
56
     */
57
    public function extractImage(Crawler $crawler)
58
    {
59
        $ret = null;
60
        $theAdapter = $this;
61
62
        $crawler->filterXPath("//head/meta[@property='og:image']")
63
            ->each(
64
                function(Crawler $node) use (&$ret) {
65
                    if($this->getCheckSmallImage($node->attr('content')) === false){ //not small image size
66
                        $ret = $node->attr('content');
67
                    }
68
                }
69
            );
70
        
71
        if (empty($ret) === true) {            
72
            $crawler->filterXPath('//img')
73
                ->each(                        
74
                    function(Crawler $node) use (&$ret, $theAdapter) {                    
75
                        $img_src = $theAdapter->normalizeLink($node->attr('src'));
76
                        $width_org = $height_org = 0;
77
                    
78
                        $url = pathinfo($img_src);
79
                        list($width, $height) = getimagesize($url['dirname'].'/'.urlencode($url['basename']));
80
81 View Code Duplication
                        if (empty($ret) === false) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
                            $url_ret = pathinfo($ret);
83
                            list($width_org, $height_org) = getimagesize(
84
                                $url_ret['dirname'].
85
                                '/'.urlencode($url_ret['basename'])
86
                            );
87
                        }
88
                        if ($width > $width_org && $height > $height_org
89
                            && $width > 200 && $height > 200 //min size of the image amended
90
                        ) {
91
                            $ret = $img_src;
92
                        }
93
                    }
94
                );
95
        }
96
        
97
        if (empty($ret) === false) {
98
            $ret = $this->normalizeLink($ret);
99
        }
100
        
101
        return $ret;
102
    }
103
104 View Code Duplication
    public function extractDescription(Crawler $crawler)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $ret = null;
107
108
        $crawler->filterXPath("//head/meta[@property='og:description']")
109
            ->each(
110
                function(Crawler $node) use (&$ret) {
111
                        $ret = $node->attr('content');
112
                }
113
            );
114
115
        return $ret;
116
    }
117
118
    /**
119
     * extract keywords out of crawler object
120
     * @param Crawler $crawler
121
     * @return array
122
     */
123 View Code Duplication
    public function extractKeywords(Crawler $crawler)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $ret = array();
126
127
        $crawler->filterXPath("//head/meta[@property='og:keywords']")
128
            ->each(
129
                function(Crawler $node) use (&$ret) {
130
                
131
                        $node_txt = trim($node->attr('content'));
132
                    if (!empty($node_txt)) {
133
                        $ret = explode(',', $node_txt);
134
                        
135
                    }
136
                }
137
            );
138
139
        return $ret;
140
    }
141
142
    public function extractBody(Crawler $crawler)
143
    {
144
        //No body can be extracted from open graph protocol
145
        return null;
146
    }
147
148 View Code Duplication
    public function extractPublishDate(Crawler $crawler)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $date_str = null;
151
152
        $crawler->filterXPath("//head/meta[@property='article:published_time']")
153
            ->each(
154
                function(Crawler $node) use (&$date_str) {
155
                        $date_str = $node->attr('content');
156
                }
157
            );
158
            
159
        if (!is_null($date_str)) {
160
            $ret = new \DateTime($date_str);
161
            return $ret->format(\DateTime::ISO8601);
162
        } else {
163
            return null;
164
        }
165
    }
166
167 View Code Duplication
    public function extractAuthor(Crawler $crawler)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
    {
169
        $ret = null;
170
        $crawler->filterXPath("//head/meta[@property='article:author']")
171
            ->each(
172
                function(Crawler $node) use (&$ret) {
173
                        $ret = $node->attr('content');
174
                }
175
            );
176
                
177
        return $ret;
178
    }
179
    
180
    public function getCheckSmallImage($imageUrl){
181
182
        $url_ret = pathinfo($imageUrl);
183
        list($width_org, $height_org) = getimagesize(
184
            $url_ret['dirname'].'/'.urlencode($url_ret['basename'])
185
        );
186
187
        if($width_org<200 || $height_org < 200){
188
            return true;
189
        }else{
190
            return false;
191
        }
192
    }
193
}
194