2014-08-07 03:56:15 +02:00
|
|
|
<?php
|
|
|
|
|
2017-11-22 02:26:15 +01:00
|
|
|
namespace SilverStripe\ExternalLinks\Tasks;
|
|
|
|
|
2017-11-26 23:19:58 +01:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
2018-02-21 22:51:59 +01:00
|
|
|
use SilverStripe\Core\Config\Configurable;
|
2017-11-27 03:14:16 +01:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-11-22 02:26:15 +01:00
|
|
|
|
2014-08-07 03:56:15 +02:00
|
|
|
/**
|
|
|
|
* Check links using curl
|
|
|
|
*/
|
2017-11-23 00:56:44 +01:00
|
|
|
class CurlLinkChecker implements LinkChecker
|
|
|
|
{
|
2018-02-21 22:51:59 +01:00
|
|
|
use Configurable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we want to follow redirects a 301 http code for example
|
|
|
|
* Set via YAML file
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $follow_location = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we want to bypass the cache
|
|
|
|
* Set via YAML file
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $bypass_cache = false;
|
2017-11-23 00:56:44 +01:00
|
|
|
|
2020-07-07 00:25:25 +02:00
|
|
|
/**
|
|
|
|
* Allow to pass custom header to be in CURL request
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $headers = [];
|
|
|
|
|
2017-11-23 00:56:44 +01:00
|
|
|
/**
|
|
|
|
* Return cache
|
|
|
|
*
|
2017-11-29 00:14:39 +01:00
|
|
|
* @return CacheInterface
|
2017-11-23 00:56:44 +01:00
|
|
|
*/
|
|
|
|
protected function getCache()
|
|
|
|
{
|
2017-11-26 23:19:58 +01:00
|
|
|
return Injector::inst()->get(CacheInterface::class . '.CurlLinkChecker');
|
2017-11-23 00:56:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2022-04-13 00:29:57 +02:00
|
|
|
if (!preg_match('/^https?[^:]*:\/\//', $href ?? '')) {
|
2017-11-23 00:56:44 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-04-13 00:29:57 +02:00
|
|
|
$cacheKey = md5($href ?? '');
|
2018-02-21 22:51:59 +01:00
|
|
|
if (!$this->config()->get('bypass_cache')) {
|
|
|
|
// Check if we have a cached result
|
2018-04-06 00:54:28 +02:00
|
|
|
$result = $this->getCache()->get($cacheKey, false);
|
2018-02-21 22:58:16 +01:00
|
|
|
if ($result !== false) {
|
|
|
|
return $result;
|
|
|
|
}
|
2018-02-21 22:51:59 +01:00
|
|
|
}
|
|
|
|
|
2017-11-23 00:56:44 +01:00
|
|
|
// No cached result so just request
|
|
|
|
$handle = curl_init($href);
|
|
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
|
2020-07-07 00:25:25 +02:00
|
|
|
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
|
|
|
|
curl_setopt($handle, CURLOPT_TIMEOUT, 10);
|
2018-02-21 22:51:59 +01:00
|
|
|
if ($this->config()->get('follow_location')) {
|
2018-02-21 22:58:16 +01:00
|
|
|
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
|
2018-02-21 22:51:59 +01:00
|
|
|
}
|
2020-07-07 00:25:25 +02:00
|
|
|
|
|
|
|
// Add headers
|
|
|
|
$headers = (array) $this->config()->get('headers');
|
|
|
|
if (!empty($headers)) {
|
|
|
|
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve http code
|
2017-11-23 00:56:44 +01:00
|
|
|
curl_exec($handle);
|
|
|
|
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($handle);
|
|
|
|
|
2018-02-21 22:51:59 +01:00
|
|
|
if (!$this->config()->get('bypass_cache')) {
|
|
|
|
// Cache result
|
2018-04-06 00:54:28 +02:00
|
|
|
$this->getCache()->set($cacheKey, $httpCode);
|
2018-02-21 22:51:59 +01:00
|
|
|
}
|
2020-07-07 00:25:25 +02:00
|
|
|
|
2017-11-23 00:56:44 +01:00
|
|
|
return $httpCode;
|
|
|
|
}
|
2014-08-07 03:56:15 +02:00
|
|
|
}
|