xExtension-Readable/extension.php

41 lines
1 KiB
PHP

<?php
class Readable2Extension extends Minz_Extension
{
private $mercHost;
private $blacklist;
public function init()
{
$this->registerHook('entry_before_insert', array($this, 'fetchStuff'));
$this->mercHost = getenv('MERCURY_API_URL');
$this->blacklist = explode(";", getenv('MERCURY_BLACKLIST'));
}
public function fetchStuff($entry)
{
if (!$this->mercHost) return $entry;
$entryUrl = $entry->link();
$entryHost = parse_url($entryUrl)["host"];
if (!empty($entryHost) && in_array($entryHost, $this->blacklist)) return $entry;
$host = $this->mercHost . "/parser?url=" . $entryUrl;
$c = curl_init($host);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($c);
$c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
if ($c_status !== 200) {
return $entry;
}
$val = json_decode($result, true);
if (empty($val) || empty($val["content"]) || empty(trim(strip_tags($val["content"])))) {
return $entry;
}
$entry->_content($val["content"]);
return $entry;
}
}