2015-07-06 20:02:21 +00:00
|
|
|
<?php
|
2021-02-26 14:35:58 +00:00
|
|
|
require "vendor/autoload.php";
|
|
|
|
|
2018-06-20 11:58:09 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-01-26 08:45:47 +00:00
|
|
|
function flags() {
|
|
|
|
return array("needs_curl" => true);
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:02:21 +00:00
|
|
|
function save() {
|
2021-02-16 15:50:18 +00:00
|
|
|
$enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"] ?? "");
|
2016-01-29 12:38:05 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
|
|
|
|
$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);
|
2020-02-27 04:57:22 +00:00
|
|
|
$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
|
2015-08-12 09:16:07 +00:00
|
|
|
|
2019-05-01 05:12:47 +00:00
|
|
|
// 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);
|
2019-04-17 05:32:35 +00:00
|
|
|
|
2015-08-12 09:16:07 +00:00
|
|
|
$host->add_filter_action($this, "action_inline", __("Inline content"));
|
2020-11-26 10:39:47 +00:00
|
|
|
$host->add_filter_action($this, "action_inline_append", __("Append content"));
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 04:57:22 +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"].")\"
|
2020-02-28 05:03:25 +00:00
|
|
|
style='cursor : pointer' title='".__('Toggle full article text')."'>description</i>";
|
2020-02-27 04:57:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 20:02:21 +00:00
|
|
|
function hook_prefs_tab($args) {
|
|
|
|
if ($args != "prefFeeds") return;
|
|
|
|
|
2021-02-17 09:36:02 +00:00
|
|
|
$enable_share_anything = sql_bool_to_bool($this->host->get($this, "enable_share_anything"));
|
2015-07-06 20:02:21 +00:00
|
|
|
|
2021-02-17 09:36:02 +00:00
|
|
|
?>
|
|
|
|
<div dojoType='dijit.layout.AccordionPane'
|
|
|
|
title="<i class='material-icons'>extension</i> <?= __('Readability settings (af_readability)') ?>">
|
2018-06-21 05:12:11 +00:00
|
|
|
|
2021-02-17 09:36:02 +00:00
|
|
|
<?= format_notice("Enable for specific feeds in the feed editor.") ?>
|
2016-01-29 12:38:05 +00:00
|
|
|
|
2021-02-17 09:36:02 +00:00
|
|
|
<form dojoType='dijit.form.Form'>
|
2019-02-22 09:48:02 +00:00
|
|
|
|
2021-02-17 18:44:21 +00:00
|
|
|
<?= \Controls\pluginhandler_tags($this, "save") ?>
|
2020-11-26 10:39:47 +00:00
|
|
|
|
2021-02-18 09:27:26 +00:00
|
|
|
<script type="dojo/method" event="onSubmit" args="evt">
|
2021-02-17 09:36:02 +00:00
|
|
|
evt.preventDefault();
|
|
|
|
if (this.validate()) {
|
2021-02-18 09:27:26 +00:00
|
|
|
Notify.progress('Saving data...', true);
|
2021-02-19 10:44:56 +00:00
|
|
|
xhr.post("backend.php", this.getValues(), (reply) => {
|
|
|
|
Notify.info(reply);
|
2021-02-18 09:27:26 +00:00
|
|
|
})
|
2021-02-17 09:36:02 +00:00
|
|
|
}
|
2021-02-18 09:27:26 +00:00
|
|
|
</script>
|
2021-02-17 09:36:02 +00:00
|
|
|
|
|
|
|
<fieldset>
|
|
|
|
<label class='checkbox'>
|
|
|
|
<?= \Controls\checkbox_tag("enable_share_anything", $enable_share_anything) ?>
|
|
|
|
<?= __("Provide full-text services to core code (bookmarklets) and other plugins") ?>
|
|
|
|
</label>
|
|
|
|
</fieldset>
|
|
|
|
|
|
|
|
<hr/>
|
|
|
|
|
|
|
|
<?= \Controls\submit_tag(__("Save")) ?>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
/* 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"));
|
|
|
|
|
|
|
|
$this->host->set($this, "enabled_feeds", $enabled_feeds);
|
|
|
|
$this->host->set($this, "append_feeds", $append_feeds);
|
|
|
|
?>
|
|
|
|
|
|
|
|
<?php if (count($enabled_feeds) > 0) { ?>
|
|
|
|
<hr/>
|
|
|
|
<h3><?= __("Currently enabled for (click to edit):") ?></h3>
|
|
|
|
|
|
|
|
<ul class='panel panel-scrollable list list-unstyled'>
|
|
|
|
<?php foreach ($enabled_feeds as $f) { ?>
|
|
|
|
<li>
|
|
|
|
<i class='material-icons'>rss_feed</i>
|
|
|
|
<a href='#' onclick="CommonDialogs.editFeed(<?= $f ?>)">
|
|
|
|
<?= Feeds::_get_title($f) . " " . (in_array($f, $append_feeds) ? __("(append)") : "") ?>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<?php } ?>
|
|
|
|
</ul>
|
|
|
|
<?php } ?>
|
|
|
|
</div>
|
|
|
|
<?php
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hook_prefs_edit_feed($feed_id) {
|
2020-11-26 10:39:47 +00:00
|
|
|
$enabled_feeds = $this->get_stored_array("enabled_feeds");
|
|
|
|
$append_feeds = $this->get_stored_array("append_feeds");
|
2021-02-17 10:55:58 +00:00
|
|
|
?>
|
2015-07-06 20:02:21 +00:00
|
|
|
|
2021-02-17 10:55:58 +00:00
|
|
|
<header><?= __("Readability") ?></header>
|
|
|
|
<section>
|
|
|
|
<fieldset>
|
|
|
|
<label class='checkbox'>
|
|
|
|
<?= \Controls\checkbox_tag("af_readability_enabled", in_array($feed_id, $enabled_feeds)) ?>
|
|
|
|
<?= __('Inline article content') ?>
|
|
|
|
</label>
|
|
|
|
</fieldset>
|
|
|
|
<fieldset>
|
|
|
|
<label class='checkbox'>
|
|
|
|
<?= \Controls\checkbox_tag("af_readability_append", in_array($feed_id, $append_feeds)) ?>
|
|
|
|
<?= __('Append to summary, instead of replacing it') ?>
|
|
|
|
</label>
|
|
|
|
</fieldset>
|
|
|
|
</section>
|
|
|
|
<?php
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hook_prefs_save_feed($feed_id) {
|
2020-11-26 10:39:47 +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
|
|
|
|
2021-02-08 08:46:43 +00:00
|
|
|
$enable = checkbox_to_sql_bool($_POST["af_readability_enabled"] ?? "");
|
|
|
|
$append = checkbox_to_sql_bool($_POST["af_readability_append"] ?? "");
|
2020-11-26 10:39:47 +00:00
|
|
|
|
|
|
|
$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) {
|
2020-11-26 10:39:47 +00:00
|
|
|
if ($enable_key === false) {
|
2015-07-06 20:02:21 +00:00
|
|
|
array_push($enabled_feeds, $feed_id);
|
|
|
|
}
|
|
|
|
} else {
|
2020-11-26 10:39:47 +00:00
|
|
|
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);
|
2020-11-26 10:39:47 +00:00
|
|
|
$this->host->set($this, "append_feeds", $append_feeds);
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 09:16:07 +00:00
|
|
|
function hook_article_filter_action($article, $action) {
|
2020-11-26 10:39:47 +00:00
|
|
|
switch ($action) {
|
|
|
|
case "action_inline":
|
|
|
|
return $this->process_article($article, false);
|
|
|
|
case "action_append":
|
|
|
|
return $this->process_article($article, true);
|
|
|
|
}
|
2015-08-12 09:16:07 +00:00
|
|
|
}
|
2015-07-06 20:02:21 +00:00
|
|
|
|
2016-01-29 12:38:05 +00:00
|
|
|
public function extract_content($url) {
|
2018-12-21 14:50:16 +00:00
|
|
|
|
2018-02-11 21:02:17 +00:00
|
|
|
global $fetch_effective_url;
|
2015-07-08 07:35:19 +00:00
|
|
|
|
2020-09-22 06:04:33 +00:00
|
|
|
$tmp = UrlHelper::fetch([
|
2018-05-25 11:35:33 +00:00
|
|
|
"url" => $url,
|
|
|
|
"http_accept" => "text/*",
|
|
|
|
"type" => "text/html"]);
|
2015-07-06 20:02:21 +00:00
|
|
|
|
2016-08-02 09:25:54 +00:00
|
|
|
if ($tmp && mb_strlen($tmp) < 1024 * 500) {
|
2015-07-07 07:15:08 +00:00
|
|
|
$tmpdoc = new DOMDocument("1.0", "UTF-8");
|
2015-07-08 07:35:19 +00:00
|
|
|
|
2019-05-01 05:12:47 +00:00
|
|
|
if (!@$tmpdoc->loadHTML($tmp))
|
2016-01-29 12:38:05 +00:00
|
|
|
return false;
|
2015-07-07 07:15:08 +00:00
|
|
|
|
2019-03-21 18:08:02 +00:00
|
|
|
// this is the worst hack yet :(
|
2015-07-13 16:24:59 +00:00
|
|
|
if (strtolower($tmpdoc->encoding) != 'utf-8') {
|
2019-06-06 12:18:47 +00:00
|
|
|
$tmp = preg_replace("/<meta.*?charset.*?\/?>/i", "", $tmp);
|
2019-09-12 21:52:40 +00:00
|
|
|
if (empty($tmpdoc->encoding)) {
|
|
|
|
$tmp = mb_convert_encoding($tmp, 'utf-8');
|
|
|
|
} else {
|
|
|
|
$tmp = mb_convert_encoding($tmp, 'utf-8', $tmpdoc->encoding);
|
|
|
|
}
|
2015-07-07 07:15:08 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 11:58:09 +00:00
|
|
|
try {
|
2019-02-21 03:52:15 +00:00
|
|
|
$r = new Readability(new Configuration());
|
|
|
|
|
2018-06-20 11:58:09 +00:00
|
|
|
if ($r->parse($tmp)) {
|
2015-07-06 20:29:00 +00:00
|
|
|
|
2018-06-20 11:58:09 +00:00
|
|
|
$tmpxpath = new DOMXPath($r->getDOMDOcument());
|
|
|
|
$entries = $tmpxpath->query('(//a[@href]|//img[@src])');
|
2015-07-06 20:29:00 +00:00
|
|
|
|
2018-06-20 11:58:09 +00:00
|
|
|
foreach ($entries as $entry) {
|
|
|
|
if ($entry->hasAttribute("href")) {
|
|
|
|
$entry->setAttribute("href",
|
|
|
|
rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href")));
|
2015-07-06 20:29:00 +00:00
|
|
|
|
2018-06-20 11:58:09 +00:00
|
|
|
}
|
2015-07-06 20:29:00 +00:00
|
|
|
|
2018-06-20 11:58:09 +00:00
|
|
|
if ($entry->hasAttribute("src")) {
|
|
|
|
$entry->setAttribute("src",
|
|
|
|
rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src")));
|
2015-07-06 20:29:00 +00:00
|
|
|
|
2018-06-20 11:58:09 +00:00
|
|
|
}
|
2015-07-06 20:29:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 11:58:09 +00:00
|
|
|
return $r->getContent();
|
2015-07-06 20:29:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 15:31:01 +00:00
|
|
|
} catch (Exception $e) {
|
2018-06-20 11:58:09 +00:00
|
|
|
return false;
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 12:38:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-26 10:39:47 +00:00
|
|
|
function process_article($article, $append_mode) {
|
2016-01-29 12:38:05 +00:00
|
|
|
|
|
|
|
$extracted_content = $this->extract_content($article["link"]);
|
|
|
|
|
2018-06-21 05:12:11 +00:00
|
|
|
# let's see if there's anything of value in there
|
2020-09-22 06:04:33 +00:00
|
|
|
$content_test = trim(strip_tags(Sanitizer::sanitize($extracted_content)));
|
2018-06-21 05:12:11 +00:00
|
|
|
|
|
|
|
if ($content_test) {
|
2020-11-26 10:39:47 +00:00
|
|
|
if ($append_mode)
|
|
|
|
$article["content"] .= "<hr/>" . $extracted_content;
|
|
|
|
else
|
|
|
|
$article["content"] = $extracted_content;
|
2016-01-29 12:38:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 20:02:21 +00:00
|
|
|
return $article;
|
2015-08-12 09:16:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 10:39:47 +00:00
|
|
|
private function get_stored_array($name) {
|
|
|
|
$tmp = $this->host->get($this, $name);
|
|
|
|
|
|
|
|
if (!is_array($tmp)) $tmp = [];
|
|
|
|
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2015-08-12 09:16:07 +00:00
|
|
|
function hook_article_filter($article) {
|
|
|
|
|
2020-11-26 10:39:47 +00:00
|
|
|
$enabled_feeds = $this->get_stored_array("enabled_feeds");
|
|
|
|
$append_feeds = $this->get_stored_array("append_feeds");
|
2015-12-05 21:03:01 +00:00
|
|
|
|
2020-11-26 10:39:47 +00:00
|
|
|
$feed_id = $article["feed"]["id"];
|
2015-08-12 09:16:07 +00:00
|
|
|
|
2020-11-26 10:39:47 +00:00
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-26 10:39:47 +00:00
|
|
|
function hook_get_full_text($link) {
|
2019-05-01 05:12:47 +00:00
|
|
|
$enable_share_anything = $this->host->get($this, "enable_share_anything");
|
|
|
|
|
|
|
|
if ($enable_share_anything) {
|
|
|
|
$extracted_content = $this->extract_content($link);
|
2019-04-17 05:32:35 +00:00
|
|
|
|
2019-05-01 05:12:47 +00:00
|
|
|
# let's see if there's anything of value in there
|
2020-09-22 06:04:33 +00:00
|
|
|
$content_test = trim(strip_tags(Sanitizer::sanitize($extracted_content)));
|
2019-05-01 05:12:47 +00:00
|
|
|
|
|
|
|
if ($content_test) {
|
|
|
|
return $extracted_content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-04-17 05:32:35 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 20:02:21 +00:00
|
|
|
function api_version() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function filter_unknown_feeds($enabled_feeds) {
|
|
|
|
$tmp = array();
|
|
|
|
|
2015-12-05 20:49:54 +00:00
|
|
|
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()) {
|
2015-12-05 20:49:54 +00:00
|
|
|
array_push($tmp, $feed);
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2020-02-27 04:57:22 +00:00
|
|
|
function embed() {
|
2021-02-17 18:44:21 +00:00
|
|
|
$article_id = (int) $_REQUEST["id"];
|
2020-02-27 04:57:22 +00:00
|
|
|
|
|
|
|
$sth = $this->pdo->prepare("SELECT link FROM ttrss_entries WHERE id = ?");
|
|
|
|
$sth->execute([$article_id]);
|
|
|
|
|
|
|
|
$ret = [];
|
|
|
|
|
|
|
|
if ($row = $sth->fetch()) {
|
2020-09-22 06:04:33 +00:00
|
|
|
$ret["content"] = Sanitizer::sanitize($this->extract_content($row["link"]));
|
2020-02-27 04:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print json_encode($ret);
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|