diff --git a/SEOstats/Config/ApiKeys.php b/SEOstats/Config/ApiKeys.php index b5cd27d8..23dc1855 100644 --- a/SEOstats/Config/ApiKeys.php +++ b/SEOstats/Config/ApiKeys.php @@ -9,27 +9,14 @@ * @copyright Copyright (c) 2010 - present Stephan Schmitz * @license http://eyecatchup.mit-license.org/ MIT License * @updated 2013/12/11 - */ - -/** * Client API keys - * @package SEOstats */ -interface ApiKeys +class ApiKeys extends ConfigAbstract { - // To acquire an API key, visit Google's APIs Console here: - // https://code.google.com/apis/console - // In the Services pane, activate the "PageSpeed Insights API" (not the service!). - // Next, go to the API Access pane. The API key is near the bottom of that pane, - // in the section titled "Simple API Access.". - const GOOGLE_SIMPLE_API_ACCESS_KEY = ''; - - // To acquire a Mozscape (f.k.a. SEOmoz) API key, visit: - // https://moz.com/products/api/keys - const MOZSCAPE_ACCESS_ID = ''; - const MOZSCAPE_SECRET_KEY = ''; - - // To acquire a SISTRIX API key, visit: - // http://www.sistrix.de - const SISTRIX_API_ACCESS_KEY = ''; + protected static $config = [ + 'GOOGLE_SIMPLE_API_ACCESS_KEY' => '', + 'MOZSCAPE_ACCESS_ID' => '', + 'MOZSCAPE_SECRET_KEY' => '', + 'SISTRIX_API_ACCESS_KEY' => '', + ]; } diff --git a/SEOstats/Config/ConfigAbstract.php b/SEOstats/Config/ConfigAbstract.php new file mode 100644 index 00000000..11630089 --- /dev/null +++ b/SEOstats/Config/ConfigAbstract.php @@ -0,0 +1,30 @@ + $value) { + static::set($key, $config[$key]); + } + } + + public static function set($name, $value) + { + if (isset(static::$config[$name])) { + static::$config[$name] = $value; + } + } + + public static function get($name) + { + if (!isset(static::$config[$name])) { + throw new \Exception("Value for $name does not exist"); + } + return static::$config[$name]; + } +} diff --git a/SEOstats/Config/Configurable.php b/SEOstats/Config/Configurable.php new file mode 100644 index 00000000..9b6af72d --- /dev/null +++ b/SEOstats/Config/Configurable.php @@ -0,0 +1,8 @@ + 'n.a.', + // The default top level domain ending to use to query Google. + 'GOOGLE_TLD' => 'com', + // The HTTP header value for the 'Accept-Language' attribute. + // + // Note: Google search results, doesn't matter which tld you request, vary depending on + // the value sent for the HTTP header attribute 'Accept-Language'! Eg: I am from Germany. + // Even if I use the "ncr" (no country redirect) request parameter, all search results + // that I get in response to a query on google.com will be localized to German, because + // my browser sends an Accept-Language header value of 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3'. + // On the other side, if I change my browser settings to send a value of 'en-us;q=0.8,en;q=0.3', + // all my searches on google.de (the german Google page) will be localized English. + // Thus, if you want to get the same results that you see when you search Google from + // your browser, you must not only set the @const GOOGLE_TLD to your country specifiy TLD, + // but also set the value below to be the same used by your browser! + 'HTTP_HEADER_ACCEPT_LANGUAGE' => 'en-us;q=0.8,en;q=0.3', + // For curl instances: Whether to allow Google to store cookies, or not. + 'ALLOW_GOOGLE_COOKIES' => 0, + // Choose the local SEMRush database to use. + // Valid values are: + // au - Google.com.au (Australia) + // br - Google.com.br (Brazil) + // ca - Google.ca (Canada) + // de - Google.de (Germany) + // es - Google.es (Spain) + // fr - Google.fr (France) + // it - Google.it (Italy) + // ru - Google.ru (Russia) + // uk - Google.co.uk (United Kingdom) + // us - Google.com (United States) + 'SEMRUSH_DB' => 0, + // Choose the local SISTRIX database to use. + // Valid values are: + // de – Germany + // at – Austria + // ch – Switzerland + // us – USA + // uk – England + // es – Spain + // fr – France + // it – Italy + 'SISTRIX_DB' => 0 + ]; } diff --git a/SEOstats/Config/Services.php b/SEOstats/Config/Services.php index c1ef9141..6d7178f2 100644 --- a/SEOstats/Config/Services.php +++ b/SEOstats/Config/Services.php @@ -35,7 +35,7 @@ interface Services const SEMRUSH_WIDGET_URL = 'http://widget.semrush.com/widget.php?action=report&type=%s&db=%s&domain=%s'; // Mozscape (f.k.a. Seomoz) Link metrics API Endpoint. - const MOZSCAPE_API_URL = 'http://lsapi.seomoz.com/linkscape/url-metrics/%s?Cols=%s&AccessID=%s&Expires=%s&Signature=%s'; + const MOZSCAPE_API_URL = 'http://lsapi.seomoz.com/linkscape/url-metrics/%s?Cols=103079233536&AccessID=%s&Expires=%s&Signature=%s'; // Google Websearch API Endpoint. const GOOGLE_APISEARCH_URL = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=%s&q=%s'; diff --git a/SEOstats/Helper/HttpRequest.php b/SEOstats/Helper/HttpRequest.php index 0f8ba3d4..dc5ea121 100644 --- a/SEOstats/Helper/HttpRequest.php +++ b/SEOstats/Helper/HttpRequest.php @@ -15,26 +15,26 @@ class HttpRequest { /** * HTTP GET/POST request with curl. - * @access public - * @param String $url The Request URL - * @param Array $postData Optional: POST data array to be send. - * @return Mixed On success, returns the response string. + * @access public + * @param String $url The Request URL + * @param Array $postData Optional: POST data array to be send. + * @return Mixed On success, returns the response string. * Else, the the HTTP status code received * in reponse to the request. */ public static function sendRequest($url, $postData = false, $postJson = false) { $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats', - \SEOstats\SEOstats::BUILD_NO); + \SEOstats\SEOstats::BUILD_NO); $ch = curl_init($url); curl_setopt_array($ch, array( - CURLOPT_USERAGENT => $ua, - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_CONNECTTIMEOUT => 30, - CURLOPT_FOLLOWLOCATION => 1, - CURLOPT_MAXREDIRS => 2, - CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_USERAGENT => $ua, + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CONNECTTIMEOUT => 30, + CURLOPT_FOLLOWLOCATION => 1, + CURLOPT_MAXREDIRS => 2, + CURLOPT_SSL_VERIFYPEER => 0, )); if (false !== $postData) { @@ -52,7 +52,9 @@ public static function sendRequest($url, $postData = false, $postJson = false) $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); - + if (429 == (int)$httpCode) { + throw new TooManyRequestsException("To many requests"); + } return (200 == (int)$httpCode) ? $response : false; } @@ -60,24 +62,24 @@ public static function sendRequest($url, $postData = false, $postJson = false) * HTTP HEAD request with curl. * * @access private - * @param String $a The request URL + * @param String $a The request URL * @return Integer Returns the HTTP status code received in * response to a GET request of the input URL. */ public static function getHttpCode($url) { $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats', - \SEOstats\SEOstats::BUILD_NO); + \SEOstats\SEOstats::BUILD_NO); $ch = curl_init($url); curl_setopt_array($ch, array( - CURLOPT_USERAGENT => $ua, - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_CONNECTTIMEOUT => 10, - CURLOPT_FOLLOWLOCATION => 1, - CURLOPT_MAXREDIRS => 2, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_NOBODY => 1, + CURLOPT_USERAGENT => $ua, + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CONNECTTIMEOUT => 10, + CURLOPT_FOLLOWLOCATION => 1, + CURLOPT_MAXREDIRS => 2, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_NOBODY => 1, )); curl_exec($ch); @@ -90,19 +92,19 @@ public static function getHttpCode($url) public function getFile($url, $file) { $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats', - \SEOstats\SEOstats::BUILD_NO); + \SEOstats\SEOstats::BUILD_NO); $fp = fopen("$file", 'w'); $ch = curl_init($url); curl_setopt_array($ch, array( - CURLOPT_USERAGENT => $ua, - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_CONNECTTIMEOUT => 30, - CURLOPT_FOLLOWLOCATION => 1, - CURLOPT_MAXREDIRS => 2, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_FILE => $fp, + CURLOPT_USERAGENT => $ua, + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CONNECTTIMEOUT => 30, + CURLOPT_FOLLOWLOCATION => 1, + CURLOPT_MAXREDIRS => 2, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_FILE => $fp, )); curl_exec($ch); diff --git a/SEOstats/Helper/TooManyRequestsException.php b/SEOstats/Helper/TooManyRequestsException.php new file mode 100644 index 00000000..1b47a384 --- /dev/null +++ b/SEOstats/Helper/TooManyRequestsException.php @@ -0,0 +1,8 @@ + - * @copyright Copyright (c) 2010 - present Stephan Schmitz - * @license http://eyecatchup.mit-license.org - * @version CVS: $Id: SEOstats.php, v2.5.2 Rev 31 2013/08/14 13:57:17 ssc Exp $ - * @link https://github.com/eyecatchup/SEOstats/ + * @package SEOstats + * @author Stephan Schmitz + * @copyright Copyright (c) 2010 - present Stephan Schmitz + * @license http://eyecatchup.mit-license.org + * @version CVS: $Id: SEOstats.php, v2.5.2 Rev 31 2013/08/14 13:57:17 ssc Exp $ + * @link https://github.com/eyecatchup/SEOstats/ * ================================================================================ * LICENSE: Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the "Software'), @@ -82,10 +82,10 @@ class SEOstats const BUILD_NO = Config\Package::VERSION_CODE; protected static $_url, - $_host, - $_lastHtml, - $_lastLoadedUrl - = false; + $_host, + $_lastHtml, + $_lastLoadedUrl + = false; public function __construct($url = false) { @@ -152,10 +152,9 @@ public static function getUrl($url = false) public function setUrl($url) { if (false !== Helper\Url::isRfc($url)) { - self::$_url = $url; + self::$_url = $url; self::$_host = Helper\Url::parseHost($url); - } - else { + } else { throw new E('Invalid URL!'); exit(); } @@ -166,7 +165,7 @@ public static function getHost($url = false) { return Helper\Url::parseHost(self::getUrl($url)); } - + public static function getDomain($url = false) { return 'http://' . self::getHost($url = false); @@ -175,7 +174,8 @@ public static function getDomain($url = false) /** * @return DOMDocument */ - protected static function _getDOMDocument($html) { + protected static function _getDOMDocument($html) + { $doc = new \DOMDocument; @$doc->loadHtml($html); return $doc; @@ -184,7 +184,8 @@ protected static function _getDOMDocument($html) { /** * @return DOMXPath */ - protected static function _getDOMXPath($doc) { + protected static function _getDOMXPath($doc) + { $xpath = new \DOMXPath($doc); return $xpath; } @@ -192,7 +193,8 @@ protected static function _getDOMXPath($doc) { /** * @return HTML string */ - protected static function _getPage($url) { + protected static function _getPage($url) + { $url = self::getUrl($url); if (self::getLastLoadedUrl() == $url) { return self::getLastLoadedHtml(); @@ -203,9 +205,9 @@ protected static function _getPage($url) { self::$_lastLoadedUrl = $url; self::_setHtml($html); return $html; - } - else { - self::noDataDefaultValue(); + } else { + return false; + // return self::noDataDefaultValue(); } } @@ -214,8 +216,8 @@ protected static function _setHtml($str) self::$_lastHtml = $str; } - protected static function noDataDefaultValue() + public static function noDataDefaultValue() { - return Config\DefaultSettings::DEFAULT_RETURN_NO_DATA; + return Config\DefaultSettings::get('DEFAULT_RETURN_NO_DATA'); } } diff --git a/SEOstats/Services/Google.php b/SEOstats/Services/Google.php index fcdc4bcc..e50b216f 100644 --- a/SEOstats/Services/Google.php +++ b/SEOstats/Services/Google.php @@ -21,14 +21,14 @@ class Google extends SEOstats /** * Gets the Google Pagerank * - * @param string $url String, containing the query URL. - * @return integer Returns the Google PageRank. + * @param string $url String, containing the query URL. + * @return integer Returns the Google PageRank. */ public static function getPageRank($url = false) { // Composer autoloads classes out of the SEOstats namespace. // The custom autolader, however, does not. So we need to include it first. - if(!class_exists('\GTB_PageRank')) { + if (!class_exists('\GTB_PageRank')) { require_once realpath(__DIR__ . '/3rdparty/GTB_PageRank.php'); } @@ -41,12 +41,12 @@ public static function getPageRank($url = false) /** * Returns the total amount of results for a Google 'site:'-search for the object URL. * - * @param string $url String, containing the query URL. - * @return integer Returns the total site-search result count. + * @param string $url String, containing the query URL. + * @return integer Returns the total site-search result count. */ public static function getSiteindexTotal($url = false) { - $url = parent::getUrl($url); + $url = parent::getUrl($url); $query = urlencode("site:{$url}"); return self::getSearchResultsTotal($query); @@ -55,12 +55,12 @@ public static function getSiteindexTotal($url = false) /** * Returns the total amount of results for a Google 'link:'-search for the object URL. * - * @param string $url String, containing the query URL. - * @return integer Returns the total link-search result count. + * @param string $url String, containing the query URL. + * @return integer Returns the total link-search result count. */ public static function getBacklinksTotal($url = false) { - $url = parent::getUrl($url); + $url = parent::getUrl($url); $query = urlencode("link:{$url}"); return self::getSearchResultsTotal($query); @@ -70,8 +70,8 @@ public static function getBacklinksTotal($url = false) * Returns total amount of results for any Google search, * requesting the deprecated Websearch API. * - * @param string $url String, containing the query URL. - * @return integer Returns the total search result count. + * @param string $url String, containing the query URL. + * @return integer Returns the total search result count. */ public static function getSearchResultsTotal($url = false) { @@ -82,21 +82,20 @@ public static function getSearchResultsTotal($url = false) $obj = Helper\Json::decode($ret); return !isset($obj->responseData->cursor->estimatedResultCount) - ? parent::noDataDefaultValue() - : intval($obj->responseData->cursor->estimatedResultCount); + ? parent::noDataDefaultValue() + : intval($obj->responseData->cursor->estimatedResultCount); } public static function getPagespeedAnalysis($url = false, $strategy = 'desktop') { - if ('' == Config\ApiKeys::GOOGLE_SIMPLE_API_ACCESS_KEY) { + if ('' == Config\ApiKeys::get('GOOGLE_SIMPLE_API_ACCESS_KEY')) { throw new E('In order to use the PageSpeed API, you must obtain and set an API key first (see SEOstats\Config\ApiKeys.php).'); - exit(0); } $url = parent::getUrl($url); $url = sprintf(Config\Services::GOOGLE_PAGESPEED_URL, - $url, $strategy, Config\ApiKeys::GOOGLE_SIMPLE_API_ACCESS_KEY); + $url, $strategy, Config\ApiKeys::get('GOOGLE_SIMPLE_API_ACCESS_KEY')); $ret = static::_getPage($url); @@ -112,8 +111,7 @@ public static function getPagespeedScore($url = false, $strategy = 'desktop') if (!isset($ret->score)) { return !isset($ret->ruleGroups->SPEED->score) || !$ret->ruleGroups->SPEED->score ? parent::noDataDefaultValue() : intval($ret->ruleGroups->SPEED->score); - } - else { + } else { return $ret->score; } } @@ -121,11 +119,11 @@ public static function getPagespeedScore($url = false, $strategy = 'desktop') /** * Returns array, containing detailed results for any Google search. * - * @param string $query String, containing the search query. - * @param string $tld String, containing the desired Google top level domain. + * @param string $query String, containing the search query. + * @param string $tld String, containing the desired Google top level domain. * @return array Returns array, containing the keys 'URL', 'Title' and 'Description'. */ - public static function getSerps($query, $maxResults=100, $domain=false) + public static function getSerps($query, $maxResults = 100, $domain = false) { return Google\Search::getSerps($query, $maxResults, $domain); } diff --git a/SEOstats/Services/Google/Search.php b/SEOstats/Services/Google/Search.php index c0c788d8..614db900 100644 --- a/SEOstats/Services/Google/Search.php +++ b/SEOstats/Services/Google/Search.php @@ -22,23 +22,23 @@ class Search extends SEOstats /** * Returns array, containing detailed results for any Google search. * - * @param string $query String, containing the search query. - * @param string $tld String, containing the desired Google top level domain. + * @param string $query String, containing the search query. + * @param string $tld String, containing the desired Google top level domain. * @return array Returns array, containing the keys 'URL', 'Title' and 'Description'. */ - public static function getSerps($query, $maxResults=100, $domain=false) + public static function getSerps($query, $maxResults = 100, $domain = false) { $q = rawurlencode($query); - $maxPage = ceil(($maxResults/10)-1); + $maxPage = ceil(($maxResults / 10) - 1); $result = new Helper\ArrayHandle (); $pages = 1; $delay = 0; $domainRexExp = static::getDomainFilter($domain); - for ($start=0; $start<$pages; $start++) { + for ($start = 0; $start < $pages; $start++) { - $haveNextPage = static::makeRequest ($start, $q, $result, $domainRexExp); + $haveNextPage = static::makeRequest($start, $q, $result, $domainRexExp); if (!$haveNextPage) { $pages -= 1; } else { @@ -55,12 +55,12 @@ public static function getSerps($query, $maxResults=100, $domain=false) return $result->toArray(); } - protected static function makeRequest ($start, $query, $result, $domainRexExp) + protected static function makeRequest($start, $query, $result, $domainRexExp) { $ref = static::getReference($start, $query); $nextSerp = static::getNextSerp($start, $query); - $curledSerp = utf8_decode( static::gCurl($nextSerp, $ref) ); + $curledSerp = utf8_decode(static::gCurl($nextSerp, $ref)); static::guardNoCaptcha($curledSerp); @@ -74,7 +74,7 @@ protected static function makeRequest ($start, $query, $result, $domainRexExp) static::parseResults($matches, $domainRexExp, $start * 10, $result); - if ( preg_match('#id="?pnnext"?#', $curledSerp) ) { + if (preg_match('#id="?pnnext"?#', $curledSerp)) { // Found 'Next'-link on currect page return true; } @@ -83,28 +83,28 @@ protected static function makeRequest ($start, $query, $result, $domainRexExp) return false; } - protected static function getReference ($start, $query) + protected static function getReference($start, $query) { return 0 == $start ? 'ncr' : sprintf('search?q=%s&hl=en&prmd=imvns&start=%s0&sa=N', $query, $start); } - protected static function getDomainFilter ($domain) + protected static function getDomainFilter($domain) { return $domain ? "#^(https?://)?[^/]*{$domain}#i" : false; } - protected static function getNextSerp ($start, $query) + protected static function getNextSerp($start, $query) { return 0 == $start ? sprintf('search?q=%s&filter=0', $query) : sprintf('search?q=%s&filter=0&start=%s0', $query, $start); } - protected static function guardNoCaptcha ($response) + protected static function guardNoCaptcha($response) { if (preg_match("#answer[=|/]86640#i", $response)) { print('Please read: https://support.google.com/websearch/answer/86640'); @@ -112,7 +112,7 @@ protected static function guardNoCaptcha ($response) } } - protected static function parseResults ($matches, $domainRexExp, $start, $result) + protected static function parseResults($matches, $domainRexExp, $start, $result) { $c = 0; @@ -121,7 +121,7 @@ protected static function parseResults ($matches, $domainRexExp, $start, $result $c++; $resCnt = $start + $c; - if (! $domainRexExp) { + if (!$domainRexExp) { $result->setElement($resCnt, array( 'url' => $match[1], 'headline' => trim(strip_tags($match[2])) @@ -141,7 +141,7 @@ protected static function parseLink($link) $isValidLink = preg_match('#]*href=[\'"]?([^\'" ]+)[\'"]?[^>]*>(.*?)#', $link, $match); // is valid and not webmaster link - return ( !$isValidLink || self::isAGoogleWebmasterLink($match[1]) ) + return (!$isValidLink || self::isAGoogleWebmasterLink($match[1])) ? false : $match; } @@ -151,9 +151,12 @@ protected static function isAGoogleWebmasterLink($url) return preg_match('#^https?://www.google.com/(?:intl/.+/)?webmasters#', $url); } - protected static function gCurl($path, $ref, $useCookie = Config\DefaultSettings::ALLOW_GOOGLE_COOKIES) + protected static function gCurl($path, $ref, $useCookie = null) { - $url = sprintf('https://www.google.%s/', Config\DefaultSettings::GOOGLE_TLD); + if (!$useCookie) { + $useCookie = Config\DefaultSettings::get('ALLOW_GOOGLE_COOKIES'); + } + $url = sprintf('https://www.google.%s/', Config\DefaultSettings::get('GOOGLE_TLD')); $referer = $ref == '' ? $url : $ref; $url .= $path; @@ -163,13 +166,13 @@ protected static function gCurl($path, $ref, $useCookie = Config\DefaultSettings } $header = array( - 'Host: www.google.' . Config\DefaultSettings::GOOGLE_TLD, + 'Host: www.google.' . Config\DefaultSettings::get('GOOGLE_TLD'), 'Connection: keep-alive', 'Cache-Control: max-age=0', 'User-Agent: ' . $ua, 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Referer: ' . $referer, - 'Accept-Language: ' . Config\DefaultSettings::HTTP_HEADER_ACCEPT_LANGUAGE, + 'Accept-Language: ' . Config\DefaultSettings::get('HTTP_HEADER_ACCEPT_LANGUAGE'), 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' ); @@ -187,6 +190,6 @@ protected static function gCurl($path, $ref, $useCookie = Config\DefaultSettings $result = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); - return ($info['http_code']!=200) ? false : $result; + return ($info['http_code'] != 200) ? false : $result; } } diff --git a/SEOstats/Services/Mozscape.php b/SEOstats/Services/Mozscape.php index 9a7d4dc5..302a64d5 100644 --- a/SEOstats/Services/Mozscape.php +++ b/SEOstats/Services/Mozscape.php @@ -18,11 +18,14 @@ class Mozscape extends SEOstats { + + protected static $lastLoadedDomain; + protected static $lastLoadedPage; // A normalized 100-point score representing the likelihood // of the URL to rank well in search engine results. public static function getPageAuthority($url = false) { - $data = static::getCols('34359738368', $url); + $data = static::getCols($url); return (parent::noDataDefaultValue() == $data) ? $data : $data['upa']; } @@ -31,7 +34,7 @@ public static function getPageAuthority($url = false) // of the domain of the URL to rank well in search engine results. public static function getDomainAuthority($url = false) { - $data = static::getCols('68719476736', Helper\Url::parseHost($url)); + $data = static::getCols(Helper\Url::parseHost($url)); return (parent::noDataDefaultValue() == $data) ? $data : $data['pda']; } @@ -40,7 +43,7 @@ public static function getDomainAuthority($url = false) // http://apiwiki.moz.com/glossary#equity public static function getEquityLinkCount($url = false) { - $data = static::getCols('2048', $url); + $data = static::getCols($url); return (parent::noDataDefaultValue() == $data) ? $data : $data['uid']; } @@ -48,7 +51,7 @@ public static function getEquityLinkCount($url = false) // The number of links (equity or nonequity or not, internal or external) to the URL. public static function getLinkCount($url = false) { - $data = static::getCols('2048', $url); + $data = static::getCols($url); return (parent::noDataDefaultValue() == $data) ? $data : $data['uid']; } @@ -56,7 +59,7 @@ public static function getLinkCount($url = false) // The normalized 10-point MozRank score of the URL. public static function getMozRank($url = false) { - $data = static::getCols('16384', $url); + $data = static::getCols($url); return (parent::noDataDefaultValue() == $data) ? $data : $data['umrp']; } @@ -64,7 +67,7 @@ public static function getMozRank($url = false) // The raw MozRank score of the URL. public static function getMozRankRaw($url = false) { - $data = static::getCols('16384', $url); + $data = static::getCols($url); return (parent::noDataDefaultValue() == $data) ? $data : number_format($data['umrr'], 16); } @@ -75,37 +78,67 @@ public static function getMozRankRaw($url = false) * @access public * @param cols string The bit flags you want returned. * @param url string The URL to get metrics for. + * @return mixed */ - public static function getCols($cols, $url = false) + public static function getCols($url = false) { - if ('' == Config\ApiKeys::MOZSCAPE_ACCESS_ID || - '' == Config\ApiKeys::MOZSCAPE_SECRET_KEY) { + if ('' == Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') || + '' == Config\ApiKeys::get('MOZSCAPE_SECRET_KEY') + ) { throw new E('In order to use the Mozscape API, you must obtain and set an API key first (see SEOstats\Config\ApiKeys.php).'); - exit(0); } - $expires = time() + 300; + $verbose = getenv('SEOSTATS_VERBOSE'); + $host = Helper\Url::parseHost(parent::getUrl($url)); + if ($verbose) { + print "[SEOSTATS] Request for url $url\n"; + print "[SEOSTATS] Request host $host\n"; + } + if (static::$lastLoadedDomain == $host) { + if ($verbose) { + print "[SEOSTATS] Return value:\n"; + print_r(static::$lastLoadedPage); + print "\n"; + } + return static::$lastLoadedPage; + } + + $expires = time() + 300; $apiEndpoint = sprintf(Config\Services::MOZSCAPE_API_URL, - urlencode(Helper\Url::parseHost(parent::getUrl($url))), - $cols, - Config\ApiKeys::MOZSCAPE_ACCESS_ID, + urlencode($host), + Config\ApiKeys::get('MOZSCAPE_ACCESS_ID'), $expires, urlencode(self::_getUrlSafeSignature($expires)) ); - + if ($verbose) { + print "[SEOSTATS] Endpoint url:\n"; + print_r($apiEndpoint); + print "\n"; + } $ret = static::_getPage($apiEndpoint); - - return (!$ret || empty($ret) || '{}' == (string)$ret) - ? parent::noDataDefaultValue() - : Helper\Json::decode($ret, true); + if ($verbose) { + print "[SEOSTATS] Raw responce:\n"; + print_r($ret); + print "\n"; + } + static::$lastLoadedDomain = $host; + static::$lastLoadedPage = (!$ret || empty($ret) || '{}' == (string)$ret) + ? parent::noDataDefaultValue() + : Helper\Json::decode($ret, true); + if ($verbose) { + print "[SEOSTATS] Return value:\n"; + print_r(static::$lastLoadedPage); + print "\n"; + } + return static::$lastLoadedPage; } private static function _getUrlSafeSignature($expires) { - $data = Config\ApiKeys::MOZSCAPE_ACCESS_ID . "\n{$expires}"; - $sig = self::_hmacsha1($data, Config\ApiKeys::MOZSCAPE_SECRET_KEY); + $data = Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') . "\n{$expires}"; + $sig = self::_hmacsha1($data, Config\ApiKeys::get('MOZSCAPE_SECRET_KEY')); return base64_encode($sig); } @@ -124,17 +157,17 @@ private static function _hmacsha1($data, $key) private static function _hmacsha1Rebuild($data, $key) { $blocksize = 64; - $hashfunc = 'sha1'; + $hashfunc = 'sha1'; if (strlen($key) > $blocksize) { $key = pack('H*', $hashfunc($key)); } - $key = str_pad($key, $blocksize, chr(0x00)); + $key = str_pad($key, $blocksize, chr(0x00)); $ipad = str_repeat(chr(0x36), $blocksize); $opad = str_repeat(chr(0x5c), $blocksize); - $hmac = pack('H*', $hashfunc(($key^$opad) . - pack('H*', $hashfunc(($key^$ipad) . $data)))); + $hmac = pack('H*', $hashfunc(($key ^ $opad) . + pack('H*', $hashfunc(($key ^ $ipad) . $data)))); return $hmac; } } diff --git a/SEOstats/Services/SemRush.php b/SEOstats/Services/SemRush.php index e84fe7a6..96b7dbc1 100644 --- a/SEOstats/Services/SemRush.php +++ b/SEOstats/Services/SemRush.php @@ -130,7 +130,7 @@ protected static function getSemRushDatabase($db) { return false !== $db ? $db - : Config\DefaultSettings::SEMRUSH_DB; + : Config\DefaultSettings::get('SEMRUSH_DB'); } protected static function guardDomainIsValid($domain) @@ -168,7 +168,7 @@ protected static function guardValidArgsForGetDomainGraph($reportType, $width, $ protected static function getBackendData($url, $db, $reportType) { - $db = false !== $db ? $db : Config\DefaultSettings::SEMRUSH_DB; + $db = false !== $db ? $db : Config\DefaultSettings::get('SEMRUSH_DB'); $dataUrl = self::getBackendUrl($url, $db, $reportType); $data = self::getApiData($dataUrl); @@ -202,7 +202,7 @@ protected static function getWidgetUrl($url, $db, $reportType) protected static function getWidgetData($url, $db, $reportType, $valueKey) { - $db = false !== $db ? $db : Config\DefaultSettings::SEMRUSH_DB; + $db = false !== $db ? $db : Config\DefaultSettings::get('SEMRUSH_DB'); $dataUrl = self::getWidgetUrl($url, $db, $reportType); $data = self::getApiData($dataUrl); diff --git a/SEOstats/Services/Sistrix.php b/SEOstats/Services/Sistrix.php index e6f2d5ae..0cf4318f 100644 --- a/SEOstats/Services/Sistrix.php +++ b/SEOstats/Services/Sistrix.php @@ -42,8 +42,8 @@ public static function getDBs() */ public static function getVisibilityIndex($url = false) { - $url = parent::getUrl($url); - $domain = Helper\Url::parseHost($url); + $url = parent::getUrl($url); + $domain = Helper\Url::parseHost($url); $dataUrl = sprintf(Config\Services::SISTRIX_VI_URL, urlencode($domain)); $html = static::_getPage($dataUrl); @@ -69,11 +69,11 @@ public static function getVisibilityIndexByApi($url = false, $db = false) $domain = static::getDomainFromUrl($url); $database = static::getValidDatabase($db); - $dataUrl = sprintf(Config\Services::SISTRIX_API_VI_URL, Config\ApiKeys::SISTRIX_API_ACCESS_KEY, urlencode($domain), $database); + $dataUrl = sprintf(Config\Services::SISTRIX_API_VI_URL, Config\ApiKeys::get('SISTRIX_API_ACCESS_KEY'), urlencode($domain), $database); $json = static::_getPage($dataUrl); - if(empty($json)) { + if (empty($json)) { return parent::noDataDefaultValue(); } @@ -88,10 +88,10 @@ public static function getApiCredits() { self::guardApiKey(); - $dataUrl = sprintf(Config\Services::SISTRIX_API_CREDITS_URL, Config\ApiKeys::SISTRIX_API_ACCESS_KEY); + $dataUrl = sprintf(Config\Services::SISTRIX_API_CREDITS_URL, Config\ApiKeys::get('SISTRIX_API_ACCESS_KEY')); $json = static::_getPage($dataUrl); - if(empty($json)) { + if (empty($json)) { return parent::noDataDefaultValue(); } @@ -109,15 +109,15 @@ public static function checkApiCredits() protected static function guardApiKey() { - if(!static::hasApiKey()) { + if (!static::hasApiKey()) { self::exc('In order to use the SISTRIX API, you must obtain and set an ' . - 'API key first (see SEOstats\Config\ApiKeys.php).' . PHP_EOL); + 'API key first (see SEOstats\Config\ApiKeys.php).' . PHP_EOL); } } protected static function hasApiKey() { - if ('' == Config\ApiKeys::SISTRIX_API_ACCESS_KEY) { + if ('' == Config\ApiKeys::get('SISTRIX_API_ACCESS_KEY')) { return false; } @@ -126,8 +126,8 @@ protected static function hasApiKey() protected static function guardApiCredits() { - if(!static::checkApiCredits()) { - self::exc('Not enough API credits.'.PHP_EOL); + if (!static::checkApiCredits()) { + self::exc('Not enough API credits.' . PHP_EOL); } } @@ -138,8 +138,8 @@ private static function checkDatabase($db) protected static function getDomainFromUrl($url) { - $url = parent::getUrl($url); - $domain = Helper\Url::parseHost($url); + $url = parent::getUrl($url); + $domain = Helper\Url::parseHost($url); static::guardDomainIsValid($domain); return $domain; @@ -147,7 +147,7 @@ protected static function getDomainFromUrl($url) protected static function getValidDatabase($db) { - $db = ($db == false) ? Config\DefaultSettings::SISTRIX_DB : $db; + $db = ($db == false) ? Config\DefaultSettings::get('SISTRIX_DB') : $db; $database = self::checkDatabase($db); static::guardDatabaseIsValid($database); @@ -173,7 +173,7 @@ private static function exc($err) { $e = ($err == 'db') ? "Invalid database. Choose one of: " . - substr( implode(", ", self::getDBs()), 0, -2) + substr(implode(", ", self::getDBs()), 0, -2) : $err; throw new E($e); }