2015-07-06 20:02:21 +00:00
|
|
|
<?php
|
|
|
|
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;
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$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;
|
|
|
|
|
2017-02-10 11:57:25 +00:00
|
|
|
print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">";
|
2015-07-06 20:02:21 +00:00
|
|
|
|
|
|
|
print_notice("Enable the plugin for specific feeds in the feed editor.");
|
|
|
|
|
2016-01-29 12:38:05 +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) {
|
|
|
|
notify_info(transport.responseText);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//this.reset();
|
|
|
|
}
|
|
|
|
</script>";
|
|
|
|
|
2017-02-10 11:36:21 +00:00
|
|
|
print_hidden("op", "pluginhandler");
|
|
|
|
print_hidden("method", "save");
|
|
|
|
print_hidden("plugin", "af_readability");
|
2016-01-29 12:38:05 +00:00
|
|
|
|
|
|
|
$enable_share_anything = $this->host->get($this, "enable_share_anything");
|
|
|
|
|
2017-02-10 11:57:25 +00:00
|
|
|
print_checkbox("enable_share_anything", $enable_share_anything);
|
|
|
|
print " <label for=\"enable_share_anything\">" . __("Use Readability for pages shared via bookmarklet.") . "</label>";
|
2016-01-29 12:38:05 +00:00
|
|
|
|
2017-02-10 11:57:25 +00:00
|
|
|
print "<p>"; print_button("submit", __("Save"));
|
2016-01-29 12:38:05 +00:00
|
|
|
print "</form>";
|
|
|
|
|
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-12-05 20:49:54 +00:00
|
|
|
|
|
|
|
$enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
|
2015-07-06 20:02:21 +00:00
|
|
|
$this->host->set($this, "enabled_feeds", $enabled_feeds);
|
|
|
|
|
|
|
|
if (count($enabled_feeds) > 0) {
|
|
|
|
print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
|
|
|
|
|
|
|
|
print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
|
|
|
|
foreach ($enabled_feeds as $f) {
|
|
|
|
print "<li>" .
|
|
|
|
"<img src='images/pub_set.png'
|
|
|
|
style='vertical-align : middle'> <a href='#'
|
|
|
|
onclick='editFeed($f)'>".
|
move the following to Feeds:
+ static function catchup_feed($feed, $cat_view, $owner_uid = false, $mode = 'all', $search = false) {
+ static function getFeedArticles($feed, $is_cat = false, $unread_only = false,
+ static function subscribe_to_feed($url, $cat_id = 0,
+ static function getFeedIcon($id) {
+ static function getFeedTitle($id, $cat = false) {
+ static function getCategoryUnread($cat, $owner_uid = false) {
+ static function getCategoryChildrenUnread($cat, $owner_uid = false) {
2017-05-04 11:50:56 +00:00
|
|
|
Feeds::getFeedTitle($f) . "</a></li>";
|
2015-07-06 20:02:21 +00:00
|
|
|
}
|
|
|
|
print "</ul>";
|
|
|
|
}
|
|
|
|
|
|
|
|
print "</div>";
|
|
|
|
}
|
|
|
|
|
|
|
|
function hook_prefs_edit_feed($feed_id) {
|
|
|
|
print "<div class=\"dlgSec\">".__("Readability")."</div>";
|
|
|
|
print "<div class=\"dlgSecCont\">";
|
|
|
|
|
|
|
|
$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" : "";
|
|
|
|
|
|
|
|
print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
|
|
|
|
name=\"af_readability_enabled\"
|
|
|
|
$checked> <label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
|
|
|
|
|
|
|
|
print "</div>";
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-07-08 10:44:45 +00:00
|
|
|
if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
|
2015-07-06 20:02:21 +00:00
|
|
|
|
2015-11-20 10:34:52 +00:00
|
|
|
if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) {
|
|
|
|
|
2016-01-29 12:38:05 +00:00
|
|
|
$ch = curl_init($url);
|
2015-11-20 10:34:52 +00:00
|
|
|
|
2015-07-08 07:35:19 +00:00
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true);
|
2015-11-20 10:34:52 +00:00
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
2015-07-08 07:35:19 +00:00
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
@curl_exec($ch);
|
2015-07-08 07:35:19 +00:00
|
|
|
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
|
|
|
|
|
|
|
if (strpos($content_type, "text/html") === FALSE)
|
2016-01-29 12:38:05 +00:00
|
|
|
return false;
|
2015-07-08 07:35:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 12:38:05 +00:00
|
|
|
$tmp = fetch_file_contents($url);
|
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
|
|
|
|
2017-01-28 11:24:48 +00:00
|
|
|
if (!$tmpdoc->loadHTML('<?xml encoding="utf-8" ?>\n' . $tmp))
|
2016-01-29 12:38:05 +00:00
|
|
|
return false;
|
2015-07-07 07:15:08 +00:00
|
|
|
|
2015-07-13 16:24:59 +00:00
|
|
|
if (strtolower($tmpdoc->encoding) != 'utf-8') {
|
2015-07-07 07:15:08 +00:00
|
|
|
$tmpxpath = new DOMXPath($tmpdoc);
|
|
|
|
|
|
|
|
foreach ($tmpxpath->query("//meta") as $elem) {
|
|
|
|
$elem->parentNode->removeChild($elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tmp = $tmpdoc->saveHTML();
|
|
|
|
}
|
|
|
|
|
2016-01-29 12:38:05 +00:00
|
|
|
$r = new Readability($tmp, $url);
|
2015-07-06 20:02:21 +00:00
|
|
|
|
|
|
|
if ($r->init()) {
|
2015-07-06 20:29:00 +00:00
|
|
|
$tmpxpath = new DOMXPath($r->dom);
|
|
|
|
|
|
|
|
$entries = $tmpxpath->query('(//a[@href]|//img[@src])');
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
if ($entry->hasAttribute("href")) {
|
|
|
|
$entry->setAttribute("href",
|
2016-01-29 12:38:05 +00:00
|
|
|
rewrite_relative_url($url, $entry->getAttribute("href")));
|
2015-07-06 20:29:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($entry->hasAttribute("src")) {
|
|
|
|
$entry->setAttribute("src",
|
2016-01-29 12:38:05 +00:00
|
|
|
rewrite_relative_url($url, $entry->getAttribute("src")));
|
2015-07-06 20:29:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-29 12:38:05 +00:00
|
|
|
return $r->articleContent->innerHTML;
|
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"]);
|
|
|
|
|
|
|
|
if ($extracted_content) {
|
|
|
|
$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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|