Add correct fallback for older versions and HTTP_Request version 1

This commit is contained in:
Matthias Gutjahr 2014-05-14 23:36:37 +02:00
parent efc06d8dec
commit f09bf70610
3 changed files with 55 additions and 18 deletions

View file

@ -45,7 +45,7 @@ class serendipity_event_lsrstopper extends serendipity_event
$propbag->add('author', 'Matthias Gutjahr');
$propbag->add('version', '0.3');
$propbag->add('requirements', array(
'serendipity' => '2.0',
'serendipity' => '1.6',
'smarty' => '2.6.7',
'php' => '5.2'
));
@ -162,22 +162,7 @@ class serendipity_event_lsrstopper extends serendipity_event
}
$blacklist = $this->readCache();
if ($blacklist === null) {
require_once (defined('S9Y_PEAR_PATH') ? S9Y_PEAR_PATH : S9Y_INCLUDE_PATH . 'bundled-libs/') . 'HTTP/Request2.php';
$req = new HTTP_Request2(
$this->get_config('blacklist_url'),
HTTP_Request2::METHOD_GET,
array('follow_redirects' => true, 'max_redirects' => 3)
);
try {
$response = $req->send();
if (200 == $response->getStatus()) {
$blacklist = $response->getBody();
} else {
return null;
}
} catch (HTTP_Request2_Exception $e) {
return null;
}
$blacklist = $this->fetchRemoteBlacklist();
$this->writeCache($blacklist);
}
return $blacklist;
@ -255,7 +240,7 @@ class serendipity_event_lsrstopper extends serendipity_event
if (!defined('PATH_SMARTY_COMPILE')) {
return '';
}
return $serendipity['serendipityPath'] . '/' . PATH_SMARTY_COMPILE . '/lsrstopper/' . md5($url);
return $serendipity['serendipityPath'] . PATH_SMARTY_COMPILE . '/lsrstopper/' . md5($url);
}
/**
@ -298,4 +283,42 @@ class serendipity_event_lsrstopper extends serendipity_event
{
return filter_var($url, FILTER_VALIDATE_URL);
}
/**
* Fetch blacklist from remote server
*
* @return mixed|null|string
* @throws Exception
*/
protected function fetchRemoteBlacklist() {
$httpDirname = (defined('S9Y_PEAR_PATH') ? S9Y_PEAR_PATH : S9Y_INCLUDE_PATH . 'bundled-libs/') . 'HTTP/';
if (file_exists($httpDirname . 'Request2.php')) {
set_include_path(get_include_path() . PATH_SEPARATOR . $httpDirname . '/..');
require_once $httpDirname . 'Request2.php';
$req = new HTTP_Request2(
$this->get_config('blacklist_url'),
HTTP_Request2::METHOD_GET,
array('follow_redirects' => true, 'max_redirects' => 3)
);
try {
$res = $req->send();
if (200 == $res->getStatus()) {
$blacklist = $res->getBody();
} else {
return null;
}
} catch (HTTP_Request2_Exception $e) {
return null;
}
} else {
// Fallback to old solution
require_once $httpDirname . 'Request.php';
$req = new HTTP_Request($this->get_config('blacklist_url'), array('allowRedirects' => true, 'maxRedirects' => 3));
if (PEAR::isError($req->sendRequest()) || $req->getResponseCode() != '200') {
return null;
}
$blacklist = $req->getResponseBody();
}
return $blacklist;
}
}

View file

@ -25,6 +25,11 @@ class serendipity_event_lsrstopperTest extends PluginTest
*/
protected $eventData;
/**
* @var string
*/
protected $cacheDir;
/**
* Set up
*/
@ -41,6 +46,14 @@ class serendipity_event_lsrstopperTest extends PluginTest
*/
public function tearDown()
{
$cacheDir = S9Y_INCLUDE_PATH . 'plugins/additional_plugins/serendipity_event_lsrstopper/tests/data/lsrstopper';
$cacheFile = $cacheDir . '/' . '91a4de9d7038da7d2af7d375dd4a7df4';
if (file_exists($cacheFile)) {
unlink($cacheFile);
}
if (is_dir($cacheDir)) {
rmdir($cacheDir);
}
parent::tearDown();
}
@ -81,6 +94,7 @@ class serendipity_event_lsrstopperTest extends PluginTest
*/
public function testFrontendDisplay()
{
define('PATH_SMARTY_COMPILE', 'plugins/additional_plugins/serendipity_event_lsrstopper/tests/data');
$this->object->introspect($this->propBag);
$this->object->set_config('blacklist_url', S9Y_INCLUDE_PATH . 'plugins/serendipity_event_lsrstopper/tests/fixtures/blacklist.txt');
$this->object->event_hook('frontend_display', $this->propBag, $this->eventData);