ttrss/plugins/af_readability/init.php

354 lines
9.3 KiB
PHP
Raw Normal View History

2015-07-06 20:02:21 +00:00
<?php
use andreskrey\Readability\Readability;
use andreskrey\Readability\Configuration;
2015-07-06 20:02:21 +00:00
class Af_Readability extends Plugin {
2017-12-03 07:44:43 +00:00
/* @var PluginHost $host */
2015-07-06 20:02:21 +00:00
private $host;
function about() {
return array(1.0,
"Try to inline article content using Readability",
"fox");
}
function flags() {
return array("needs_curl" => true);
}
2015-07-06 20:02:21 +00:00
function save() {
$enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
$this->host->set($this, "enable_share_anything", $enable_share_anything);
echo __("Data saved.");
2015-07-06 20:02:21 +00:00
}
function init($host)
{
$this->host = $host;
2019-08-21 07:05:25 +00:00
if (version_compare(PHP_VERSION, '7.0.0', '<')) {
user_error("af_readability requires PHP 7.0", E_USER_WARNING);
return;
}
2015-07-06 20:02:21 +00:00
$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
$host->add_hook($host::HOOK_PREFS_TAB, $this);
$host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
$host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
// Note: we have to install the hook even if disabled because init() is being run before plugin data has loaded
// so we can't check for our storage-set options here
$host->add_hook($host::HOOK_GET_FULL_TEXT, $this);
$host->add_filter_action($this, "action_inline", __("Inline content"));
$host->add_filter_action($this, "action_inline_append", __("Append content"));
2015-07-06 20:02:21 +00:00
}
function get_js() {
return file_get_contents(__DIR__ . "/init.js");
}
function hook_article_button($line) {
return "<i class='material-icons' onclick=\"Plugins.Af_Readability.embed(".$line["id"].")\"
style='cursor : pointer' title='".__('Toggle full article text')."'>description</i>";
}
2015-07-06 20:02:21 +00:00
function hook_prefs_tab($args) {
if ($args != "prefFeeds") return;
print "<div dojoType='dijit.layout.AccordionPane'
title=\"<i class='material-icons'>extension</i> ".__('Readability settings (af_readability)')."\">";
2015-07-06 20:02:21 +00:00
2019-08-21 07:05:25 +00:00
if (version_compare(PHP_VERSION, '7.0.0', '<')) {
print_error("This plugin requires PHP 7.0.");
2019-02-22 09:48:02 +00:00
} else {
2019-02-22 09:48:02 +00:00
print "<h2>" . __("Global settings") . "</h2>";
2015-07-06 20:02:21 +00:00
2019-02-22 09:48:02 +00:00
print_notice("Enable for specific feeds in the feed editor.");
2019-02-22 09:48:02 +00:00
print "<form dojoType='dijit.form.Form'>";
print "<script type='dojo/method' event='onSubmit' args='evt'>
evt.preventDefault();
if (this.validate()) {
console.log(dojo.objectToQuery(this.getValues()));
new Ajax.Request('backend.php', {
parameters: dojo.objectToQuery(this.getValues()),
onComplete: function(transport) {
2018-12-02 17:56:30 +00:00
Notify.info(transport.responseText);
}
});
//this.reset();
}
</script>";
2019-02-22 09:48:02 +00:00
print_hidden("op", "pluginhandler");
print_hidden("method", "save");
print_hidden("plugin", "af_readability");
2019-02-22 09:48:02 +00:00
$enable_share_anything = $this->host->get($this, "enable_share_anything");
2019-02-22 09:48:02 +00:00
print "<fieldset>";
print "<label class='checkbox'> ";
print_checkbox("enable_share_anything", $enable_share_anything);
print " " . __("Provide full-text services to core code (bookmarklets) and other plugins");
2019-02-22 09:48:02 +00:00
print "</label>";
print "</fieldset>";
2019-08-21 07:05:25 +00:00
print_button("submit", __("Save"), "class='alt-primary'");
2019-02-22 09:48:02 +00:00
print "</form>";
/* cleanup */
$enabled_feeds = $this->filter_unknown_feeds(
$this->get_stored_array("enabled_feeds"));
$append_feeds = $this->filter_unknown_feeds(
$this->get_stored_array("append_feeds"));
2019-02-22 09:48:02 +00:00
$this->host->set($this, "enabled_feeds", $enabled_feeds);
$this->host->set($this, "append_feeds", $append_feeds);
2015-07-06 20:02:21 +00:00
2019-02-22 09:48:02 +00:00
if (count($enabled_feeds) > 0) {
print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
2015-07-06 20:02:21 +00:00
2019-02-22 09:48:02 +00:00
print "<ul class='panel panel-scrollable list list-unstyled'>";
foreach ($enabled_feeds as $f) {
$is_append = in_array($f, $append_feeds);
2019-02-22 09:48:02 +00:00
print "<li><i class='material-icons'>rss_feed</i> <a href='#'
onclick='CommonDialogs.editFeed($f)'>".
Feeds::getFeedTitle($f) . " " . ($is_append ? __("(append)") : "") . "</a></li>";
2019-02-22 09:48:02 +00:00
}
print "</ul>";
2015-07-06 20:02:21 +00:00
}
2019-02-22 09:48:02 +00:00
2015-07-06 20:02:21 +00:00
}
print "</div>";
}
function hook_prefs_edit_feed($feed_id) {
print "<header>".__("Readability")."</header>";
print "<section>";
2015-07-06 20:02:21 +00:00
$enabled_feeds = $this->get_stored_array("enabled_feeds");
$append_feeds = $this->get_stored_array("append_feeds");
2015-07-06 20:02:21 +00:00
$enable_checked = in_array($feed_id, $enabled_feeds) ? "checked" : "";
$append_checked = in_array($feed_id, $append_feeds) ? "checked" : "";
2015-07-06 20:02:21 +00:00
2019-02-20 11:37:59 +00:00
print "<fieldset>";
2019-02-22 09:48:02 +00:00
print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='af_readability_enabled'
name='af_readability_enabled' $enable_checked>&nbsp;".__('Inline article content')."</label>";
2019-02-20 11:37:59 +00:00
print "</fieldset><fieldset>";
print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='af_readability_append'
name='af_readability_append' $append_checked>&nbsp;".__('Append to summary, instead of replacing it')."</label>";
2015-07-06 20:02:21 +00:00
print "</section>";
2015-07-06 20:02:21 +00:00
}
function hook_prefs_save_feed($feed_id) {
$enabled_feeds = $this->get_stored_array("enabled_feeds");
$append_feeds = $this->get_stored_array("append_feeds");
2015-07-06 20:02:21 +00:00
$enable = checkbox_to_sql_bool($_POST["af_readability_enabled"] ?? "");
$append = checkbox_to_sql_bool($_POST["af_readability_append"] ?? "");
$enable_key = array_search($feed_id, $enabled_feeds);
$append_key = array_search($feed_id, $append_feeds);
2015-07-06 20:02:21 +00:00
if ($enable) {
if ($enable_key === false) {
2015-07-06 20:02:21 +00:00
array_push($enabled_feeds, $feed_id);
}
} else {
if ($enable_key !== false) {
unset($enabled_feeds[$enable_key]);
}
}
if ($append) {
if ($append_key === false) {
array_push($append_feeds, $feed_id);
}
} else {
if ($append_key !== false) {
unset($append_feeds[$append_key]);
2015-07-06 20:02:21 +00:00
}
}
$this->host->set($this, "enabled_feeds", $enabled_feeds);
$this->host->set($this, "append_feeds", $append_feeds);
2015-07-06 20:02:21 +00:00
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
function hook_article_filter_action($article, $action) {
switch ($action) {
case "action_inline":
return $this->process_article($article, false);
case "action_append":
return $this->process_article($article, true);
}
}
2015-07-06 20:02:21 +00:00
public function extract_content($url) {
global $fetch_effective_url;
$tmp = UrlHelper::fetch([
"url" => $url,
"http_accept" => "text/*",
"type" => "text/html"]);
2015-07-06 20:02:21 +00:00
if ($tmp && mb_strlen($tmp) < 1024 * 500) {
$tmpdoc = new DOMDocument("1.0", "UTF-8");
if (!@$tmpdoc->loadHTML($tmp))
return false;
// this is the worst hack yet :(
if (strtolower($tmpdoc->encoding) != 'utf-8') {
$tmp = preg_replace("/<meta.*?charset.*?\/?>/i", "", $tmp);
if (empty($tmpdoc->encoding)) {
$tmp = mb_convert_encoding($tmp, 'utf-8');
} else {
$tmp = mb_convert_encoding($tmp, 'utf-8', $tmpdoc->encoding);
}
}
try {
$r = new Readability(new Configuration());
if ($r->parse($tmp)) {
$tmpxpath = new DOMXPath($r->getDOMDOcument());
$entries = $tmpxpath->query('(//a[@href]|//img[@src])');
foreach ($entries as $entry) {
if ($entry->hasAttribute("href")) {
$entry->setAttribute("href",
rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href")));
}
if ($entry->hasAttribute("src")) {
$entry->setAttribute("src",
rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src")));
}
}
return $r->getContent();
}
} catch (Exception $e) {
return false;
2015-07-06 20:02:21 +00:00
}
}
return false;
}
function process_article($article, $append_mode) {
$extracted_content = $this->extract_content($article["link"]);
# let's see if there's anything of value in there
$content_test = trim(strip_tags(Sanitizer::sanitize($extracted_content)));
if ($content_test) {
if ($append_mode)
$article["content"] .= "<hr/>" . $extracted_content;
else
$article["content"] = $extracted_content;
}
2015-07-06 20:02:21 +00:00
return $article;
}
private function get_stored_array($name) {
$tmp = $this->host->get($this, $name);
if (!is_array($tmp)) $tmp = [];
return $tmp;
}
function hook_article_filter($article) {
$enabled_feeds = $this->get_stored_array("enabled_feeds");
$append_feeds = $this->get_stored_array("append_feeds");
$feed_id = $article["feed"]["id"];
if (!in_array($feed_id, $enabled_feeds))
return $article;
return $this->process_article($article, in_array($feed_id, $append_feeds));
2015-07-06 20:02:21 +00:00
}
function hook_get_full_text($link) {
$enable_share_anything = $this->host->get($this, "enable_share_anything");
if ($enable_share_anything) {
$extracted_content = $this->extract_content($link);
# let's see if there's anything of value in there
$content_test = trim(strip_tags(Sanitizer::sanitize($extracted_content)));
if ($content_test) {
return $extracted_content;
}
}
return false;
}
2015-07-06 20:02:21 +00:00
function api_version() {
return 2;
}
private function filter_unknown_feeds($enabled_feeds) {
$tmp = array();
foreach ($enabled_feeds as $feed) {
2015-07-06 20:02:21 +00:00
2017-12-03 07:44:43 +00:00
$sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
$sth->execute([$feed, $_SESSION['uid']]);
2015-07-06 20:02:21 +00:00
2017-12-03 07:44:43 +00:00
if ($row = $sth->fetch()) {
array_push($tmp, $feed);
2015-07-06 20:02:21 +00:00
}
}
return $tmp;
}
function embed() {
$article_id = (int) $_REQUEST["param"];
$sth = $this->pdo->prepare("SELECT link FROM ttrss_entries WHERE id = ?");
$sth->execute([$article_id]);
$ret = [];
if ($row = $sth->fetch()) {
$ret["content"] = Sanitizer::sanitize($this->extract_content($row["link"]));
}
print json_encode($ret);
}
2015-07-06 20:02:21 +00:00
}