Completed
Pull Request — master (#129)
by
unknown
03:19
created

SEOstats::getCurloptProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace SEOstats;
3
4
use SEOstats\Common\SEOstatsException as E;
5
use SEOstats\Config as Config;
6
use SEOstats\Helper as Helper;
7
use SEOstats\Services as Service;
8
9
/** SEOstats
10
 *  ================================================================================
11
 *  PHP library to request a bunch of SEO-relevant metrics, such as looking up the
12
 *  visibilty of a URL within organic search results, Pagespeed analysis, the
13
 *  Google Toolbar PageRank, Page-Authority, Backlink-Details, Traffic Statistics,
14
 *  social media relevance, comparing competing websites and a lot more.
15
 *  ================================================================================
16
 *  @package     SEOstats
17
 *  @author      Stephan Schmitz <[email protected]>
18
 *  @copyright   Copyright (c) 2010 - present Stephan Schmitz
19
 *  @license     http://eyecatchup.mit-license.org
20
 *  @version     CVS: $Id: SEOstats.php, v2.5.2 Rev 31 2013/08/14 13:57:17 ssc Exp $
21
 *  @link        https://github.com/eyecatchup/SEOstats/
22
 *  ================================================================================
23
 *  LICENSE: Permission is hereby granted, free of charge, to any person obtaining
24
 *  a copy of this software and associated documentation files (the "Software'),
25
 *  to deal in the Software without restriction, including without limitation the
26
 *  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27
 *  copies of the Software, and to permit persons to whom the Software is furnished
28
 *  to do so, subject to the following conditions:
29
 *
30
 *    The above copyright notice and this permission notice shall be included in all
31
 *  copies or substantial portions of the Software.
32
 *
33
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY
37
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
38
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39
 *  ================================================================================
40
 */
41
42
/**
43
 * Check required PHP settings.
44
 */
45
if (!function_exists('curl_init')) {
46
    throw new E('SEOstats requires the PHP CURL extension.');
47
    exit();
48
}
49
50
if (1 == ini_get('safe_mode') || 'on' === strtolower(ini_get('safe_mode'))) {
51
    throw new E('Because some SEOstats functions require the CURLOPT_FOLLOWLOCATION flag, ' .
52
        'you must not run PHP in safe mode! (This flag can not be set in safe mode.)');
53
    exit();
54
}
55
56
/**
57
 * Starting point for the SEOstats library. Example Usage:
58
 *
59
 * <code>
60
 * ...
61
 * $url = 'http://www.domain.tld';
62
 *
63
 * // Get the Google Toolbar PageRank value.
64
 * $result = \SEOstats\Services\Google::getPageRank($url);
65
 *
66
 * // Get the first 100 results for a Google search for 'query string'.
67
 * $result = \SEOstats\Services\Google::getSerps('query string');
68
 *
69
 * // Get the first 500 results for a Google search for 'query string'.
70
 * $result = \SEOstats\Services\Google::getSerps('query string', 500);
71
 *
72
 * // Check the first 500 results for a Google search for 'query string' for
73
 * // occurrences of the given domain name and return an array of matching
74
 * // URL's and their position within the serps.
75
 * $result = \SEOstats\Services\Google::getSerps('query string', 500, $url);
76
 * ...
77
 * </code>
78
 *
79
 */
80
class SEOstats
81
{
82
    const BUILD_NO = Config\Package::VERSION_CODE;
83
84
    protected static $_url,
85
                     $_host,
86
                     $_lastHtml,
87
                     $_lastLoadedUrl,
88
                     $_curlopt_proxy,
89
                     $_curlopt_proxyuserpwd,
90 158
                     $_ua
91
                     = false;
92 158
93
    public function __construct($url = false)
94
    {
95 158
        if (false !== $url) {
96
            self::setUrl($url);
97 1
        }
98
    }
99 1
100
    public function Alexa()
101
    {
102 1
        return new Service\Alexa;
103
    }
104 1
105
    public function Google()
106
    {
107 1
        return new Service\Google;
108
    }
109 1
110
    public function Mozscape()
111
    {
112 1
        return new Service\Mozscape;
113
    }
114 1
115
    public function OpenSiteExplorer()
116
    {
117 1
        return new Service\OpenSiteExplorer;
118
    }
119 1
120
    public function SEMRush()
121
    {
122 1
        return new Service\SemRush;
123
    }
124 1
125
    public function Sistrix()
126
    {
127 1
        return new Service\Sistrix;
128
    }
129 1
130
    public function Social()
131
    {
132 1
        return new Service\Social;
133
    }
134 1
135
    public static function getLastLoadedHtml()
136
    {
137 24
        return self::$_lastHtml;
138
    }
139 24
140
    public static function getLastLoadedUrl()
141
    {
142
        return self::$_lastLoadedUrl;
143
    }
144
145
    /**
146 97
     * Ensure the URL is set, return default otherwise
147
     * @return string
148 97
     */
149 97
    public static function getUrl($url = false)
150
    {
151
        $url = false !== $url ? $url : self::$_url;
152 145
        return $url;
153
    }
154 145
155 144
    public function setUrl($url)
156 144
    {
157 144
        if (false !== Helper\Url::isRfc($url)) {
158
            self::$_url  = $url;
159 1
            self::$_host = Helper\Url::parseHost($url);
160
        }
161
        else {
162 144
            throw new E('Invalid URL!');
163
            exit();
0 ignored issues
show
Unused Code introduced by
die; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
164
        }
165 2
        return true;
166
    }
167 2
168
    public static function getHost($url = false)
169
    {
170 1
        return Helper\Url::parseHost(self::getUrl($url));
171
    }
172 1
        
173
    public static function getDomain($url = false)
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed.

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

Loading history...
174
    {
175
        return 'http://' . self::getHost($url = false);
176
    }
177
178 26
    /**
179 26
     * @return DOMDocument
180 26
     */
181 26
    protected static function _getDOMDocument($html) {
182
        $doc = new \DOMDocument;
183
        @$doc->loadHtml($html);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
184
        return $doc;
185
    }
186
187 26
    /**
188 26
     * @return DOMXPath
189 26
     */
190
    protected static function _getDOMXPath($doc) {
191
        $xpath = new \DOMXPath($doc);
192
        return $xpath;
193
    }
194
195 2
    /**
196 2
     * @return HTML string
197 2
     */
198
    protected static function _getPage($url) {
199
        $url = self::getUrl($url);
200
        if (self::getLastLoadedUrl() == $url) {
201 2
            return self::getLastLoadedHtml();
202 2
        }
203 2
204 2
        $html = Helper\HttpRequest::sendRequest($url);
205 2
        if ($html) {
206
            self::$_lastLoadedUrl = $url;
207
            self::_setHtml($html);
208
            return $html;
209
        }
210
        else {
211
            self::noDataDefaultValue();
0 ignored issues
show
Unused Code introduced by
The call to the method SEOstats\SEOstats::noDataDefaultValue() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
212 3
        }
213
    }
214 3
215 3
    protected static function _setHtml($str)
216
    {
217 60
        self::$_lastHtml = $str;
218
    }
219 60
220
    protected static function noDataDefaultValue()
221
    {
222
        return Config\DefaultSettings::DEFAULT_RETURN_NO_DATA;
223
    }
224
225
    /**
226
     * @return Proxy address
227
     */
228
    public static function getCurloptProxy()
229
    {
230
        return self::$_curlopt_proxy;
231
    }
232
233
    /**
234
     * @param Proxy address $curlopt_proxy
235
     */
236
    public static function setCurloptProxy($curlopt_proxy)
237
    {
238
        self::$_curlopt_proxy = $curlopt_proxy;
239
    }
240
241
    /**
242
     * @return Proxy auth
243
     */
244
    public static function getCurloptProxyuserpwd()
245
    {
246
        return self::$_curlopt_proxyuserpwd;
247
    }
248
249
    /**
250
     * @param Proxy auth $curlopt_proxyuserpwd
251
     */
252
    public static function setCurloptProxyuserpwd($curlopt_proxyuserpwd)
253
    {
254
        self::$_curlopt_proxyuserpwd = $curlopt_proxyuserpwd;
255
    }
256
257
    /**
258
     * @return Useragent string
259
     */
260
    public static function getUserAgent()
261
    {
262
        return self::$_ua;
263
    }
264
265
    /**
266
     * @param Useragent string $ua
267
     */
268
    public static function setUserAgent($ua)
269
    {
270
        self::$_ua = $ua;
271
    }
272
}
273