eokm filter

This commit is contained in:
Shish 2020-08-23 15:40:58 +01:00
parent cb6c9ba987
commit d5993c02f3
3 changed files with 92 additions and 0 deletions

13
ext/eokm/info.php Normal file
View File

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
class EokmInfo extends ExtensionInfo
{
public const KEY = "eokm";
public $key = self::KEY;
public $name = "EOKM Filter";
public $url = self::SHIMMIE_URL;
public $authors = self::SHISH_AUTHOR;
public $license = self::LICENSE_GPLV2;
public $description = "Check uploads against the EOKM blocklist";
}

52
ext/eokm/main.php Normal file
View File

@ -0,0 +1,52 @@
<?php declare(strict_types=1);
class Eokm extends Extension
{
public function get_priority(): int
{
return 40;
} // early, to veto ImageUploadEvent
public function onImageAddition(ImageAdditionEvent $event)
{
global $config;
$username = $config->get_string("eokm_username");
$password = $config->get_string("eokm_password");
if($username && $password) {
$ch = curl_init("https://api.eokmhashdb.nl/v1/check/md5");
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $event->image->hash);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close($ch);
if($return == "false") {
// all ok
}
elseif($return == "true") {
log_warning("eokm", "User tried to upload banned image {$event->image->hash}");
throw new UploadException("Image banned");
}
else {
log_warning("eokm", "Unexpected return from EOKM: $return");
}
}
}
public function onSetupBuilding(SetupBuildingEvent $event)
{
$sb = new SetupBlock("EOKM Filter");
$sb->start_table();
$sb->add_text_option("eokm_username", "Username", true);
$sb->add_text_option("eokm_password", "Password", true);
$sb->end_table();
$event->panel->add_block($sb);
}
}

27
ext/eokm/test.php Normal file
View File

@ -0,0 +1,27 @@
<?php declare(strict_types=1);
class EokmTest extends ShimmiePHPUnitTestCase
{
public function testPass()
{
// no EOKM login details set, so be a no-op
$this->log_in_as_user();
$this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot");
$this->assert_no_text("Image too large");
$this->assert_no_text("Image too small");
$this->assert_no_text("ratio");
}
/*
public function testFail()
{
$this->log_in_as_user();
try {
$this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot");
$this->assertTrue(false, "Invalid-size image was allowed");
} catch (UploadException $e) {
$this->assertEquals("Image too small", $e->getMessage());
}
}
*/
}