mirror of
https://github.com/silverstripe/silverstripe-externallinks.git
synced 2024-06-24 13:39:40 +02:00
Running this command for each directory in the `src` folder we generate a valid `.upgrade.yml` which can be used by other modules that may use this one as a dependency to upgrade themselves without issue.
55 lines
1.2 KiB
PHP
55 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace SilverStripe\ExternalLinks\Tasks;
|
|
|
|
use SS_Cache;
|
|
|
|
|
|
/**
|
|
* Check links using curl
|
|
*/
|
|
class CurlLinkChecker implements LinkChecker {
|
|
|
|
/**
|
|
* Return cache
|
|
*
|
|
* @return Zend_Cache_Frontend
|
|
*/
|
|
protected function getCache() {
|
|
return SS_Cache::factory(
|
|
__CLASS__,
|
|
'Output',
|
|
array('automatic_serialization' => true)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Determine the http status code for a given link
|
|
*
|
|
* @param string $href URL to check
|
|
* @return int HTTP status code, or null if not checkable (not a link)
|
|
*/
|
|
public function checkLink($href) {
|
|
// Skip non-external links
|
|
if(!preg_match('/^https?[^:]*:\/\//', $href)) return null;
|
|
|
|
// Check if we have a cached result
|
|
$cacheKey = md5($href);
|
|
$result = $this->getCache()->load($cacheKey);
|
|
if($result !== false) return $result;
|
|
|
|
// No cached result so just request
|
|
$handle = curl_init($href);
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
|
|
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
|
|
curl_setopt($handle, CURLOPT_TIMEOUT, 10);
|
|
curl_exec($handle);
|
|
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
|
|
curl_close($handle);
|
|
|
|
// Cache result
|
|
$this->getCache()->save($httpCode, $cacheKey);
|
|
return $httpCode;
|
|
}
|
|
}
|