2015-07-06 20:02:21 +00:00
|
|
|
<?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() {
|
2017-12-02 11:07:48 +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;
|
|
|
|
|
2018-06-21 05:12:11 +00:00
|
|
|
if (version_compare(PHP_VERSION, '5.6.0', '<')) {
|
|
|
|
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);
|
2015-08-12 09:16:07 +00:00
|
|
|
|
2019-04-17 05:32:35 +00:00
|
|
|
$enable_share_anything = $this->host->get($this, "enable_share_anything");
|
|
|
|
|
|
|
|
// provide full text services to external code
|
|
|
|
// TODO: option that controls this needs a better caption
|
|
|
|
if ($enable_share_anything)
|
|
|
|
$host->add_hook($host::HOOK_GET_FULL_TEXT, $this);
|
|
|
|
|
2015-08-12 09:16:07 +00:00
|
|
|
$host->add_filter_action($this, "action_inline", __("Inline content"));
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hook_prefs_tab($args) {
|
|
|
|
if ($args != "prefFeeds") return;
|
|
|
|
|
2019-02-22 09:48:02 +00:00
|
|
|
print "<div dojoType='dijit.layout.AccordionPane'
|
2018-12-06 05:56:28 +00:00
|
|
|
title=\"<i class='material-icons'>extension</i> ".__('Readability settings (af_readability)')."\">";
|
2015-07-06 20:02:21 +00:00
|
|
|
|
2018-06-21 05:12:11 +00:00
|
|
|
if (version_compare(PHP_VERSION, '5.6.0', '<')) {
|
|
|
|
print_error("This plugin requires PHP version 5.6.");
|
2019-02-22 09:48:02 +00:00
|
|
|
} else {
|
2018-06-21 05:12:11 +00:00
|
|
|
|
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.");
|
2016-01-29 12:38:05 +00:00
|
|
|
|
2019-02-22 09:48:02 +00:00
|
|
|
print "<form dojoType='dijit.form.Form'>";
|
|
|
|
|
|
|
|
print "<script type='dojo/method' event='onSubmit' args='evt'>
|
2016-01-29 12:38:05 +00:00
|
|
|
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);
|
2016-01-29 12:38:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
//this.reset();
|
|
|
|
}
|
|
|
|
</script>";
|
|
|
|
|
2019-02-22 09:48:02 +00:00
|
|
|
print_hidden("op", "pluginhandler");
|
|
|
|
print_hidden("method", "save");
|
|
|
|
print_hidden("plugin", "af_readability");
|
2016-01-29 12:38:05 +00:00
|
|
|
|
2019-02-22 09:48:02 +00:00
|
|
|
$enable_share_anything = $this->host->get($this, "enable_share_anything");
|
2016-01-29 12:38:05 +00:00
|
|
|
|
2019-02-22 09:48:02 +00:00
|
|
|
print "<fieldset>";
|
|
|
|
print "<label class='checkbox'> ";
|
|
|
|
print_checkbox("enable_share_anything", $enable_share_anything);
|
|
|
|
print " " . __("Use Readability for pages shared via bookmarklet.");
|
|
|
|
print "</label>";
|
|
|
|
print "</fieldset>";
|
2016-01-29 12:38:05 +00:00
|
|
|
|
2019-02-22 09:48:02 +00:00
|
|
|
print print_button("submit", __("Save"), "class='alt-primary'");
|
|
|
|
print "</form>";
|
2016-01-29 12:38:05 +00:00
|
|
|
|
2019-02-22 09:48:02 +00:00
|
|
|
$enabled_feeds = $this->host->get($this, "enabled_feeds");
|
|
|
|
if (!is_array($enabled_feeds)) $enabled_feeds = array();
|
2015-12-05 20:49:54 +00:00
|
|
|
|
2019-02-22 09:48:02 +00:00
|
|
|
$enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
|
|
|
|
$this->host->set($this, "enabled_feeds", $enabled_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) {
|
|
|
|
print "<li><i class='material-icons'>rss_feed</i> <a href='#'
|
2018-12-01 19:39:29 +00:00
|
|
|
onclick='CommonDialogs.editFeed($f)'>".
|
2019-02-22 09:48:02 +00:00
|
|
|
Feeds::getFeedTitle($f) . "</a></li>";
|
|
|
|
}
|
|
|
|
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) {
|
2019-02-22 07:48:56 +00:00
|
|
|
print "<header>".__("Readability")."</header>";
|
|
|
|
print "<section>";
|
2015-07-06 20:02:21 +00:00
|
|
|
|
|
|
|
$enabled_feeds = $this->host->get($this, "enabled_feeds");
|
2015-12-05 21:03:01 +00:00
|
|
|
if (!is_array($enabled_feeds)) $enabled_feeds = array();
|
2015-07-06 20:02:21 +00:00
|
|
|
|
|
|
|
$key = array_search($feed_id, $enabled_feeds);
|
|
|
|
$checked = $key !== FALSE ? "checked" : "";
|
|
|
|
|
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' $checked> ".__('Inline article content')."</label>";
|
2019-02-20 11:37:59 +00:00
|
|
|
|
|
|
|
print "</fieldset>";
|
2015-07-06 20:02:21 +00:00
|
|
|
|
2019-02-22 07:48:56 +00:00
|
|
|
print "</section>";
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hook_prefs_save_feed($feed_id) {
|
|
|
|
$enabled_feeds = $this->host->get($this, "enabled_feeds");
|
|
|
|
if (!is_array($enabled_feeds)) $enabled_feeds = array();
|
|
|
|
|
2017-12-02 11:07:48 +00:00
|
|
|
$enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]);
|
2015-07-06 20:02:21 +00:00
|
|
|
$key = array_search($feed_id, $enabled_feeds);
|
|
|
|
|
|
|
|
if ($enable) {
|
|
|
|
if ($key === FALSE) {
|
|
|
|
array_push($enabled_feeds, $feed_id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($key !== FALSE) {
|
|
|
|
unset($enabled_feeds[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->host->set($this, "enabled_feeds", $enabled_feeds);
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*/
|
2015-08-12 09:16:07 +00:00
|
|
|
function hook_article_filter_action($article, $action) {
|
|
|
|
return $this->process_article($article);
|
|
|
|
}
|
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
|
|
|
|
2018-05-25 11:35:33 +00:00
|
|
|
$tmp = fetch_file_contents([
|
|
|
|
"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
|
|
|
|
2018-12-21 14:50:16 +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-03-21 18:08:02 +00:00
|
|
|
$tmp = preg_replace("/<meta.*?charset.*?\/>/i", "", $tmp);
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function process_article($article) {
|
|
|
|
|
|
|
|
$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
|
|
|
|
$content_test = trim(strip_tags(sanitize($extracted_content)));
|
|
|
|
|
|
|
|
if ($content_test) {
|
2016-01-29 12:38:05 +00:00
|
|
|
$article["content"] = $extracted_content;
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:02:21 +00:00
|
|
|
return $article;
|
2015-08-12 09:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hook_article_filter($article) {
|
|
|
|
|
|
|
|
$enabled_feeds = $this->host->get($this, "enabled_feeds");
|
2015-12-05 21:03:01 +00:00
|
|
|
if (!is_array($enabled_feeds)) return $article;
|
|
|
|
|
2015-08-12 09:16:07 +00:00
|
|
|
$key = array_search($article["feed"]["id"], $enabled_feeds);
|
|
|
|
if ($key === FALSE) return $article;
|
|
|
|
|
|
|
|
return $this->process_article($article);
|
2015-07-06 20:02:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-17 05:32:35 +00:00
|
|
|
function hook_get_full_text($link) {
|
|
|
|
$extracted_content = $this->extract_content($link);
|
|
|
|
|
|
|
|
return trim(strip_tags(sanitize($extracted_content)));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|