sanitize: allow <hr> elements

af_redditimgur: optionally add original content using Readability php implementation
This commit is contained in:
Andrew Dolgov 2015-07-06 22:44:43 +03:00
parent 3b9ca4e6cc
commit b90c4468fc
4 changed files with 1254 additions and 1 deletions

View File

@ -957,7 +957,7 @@
'caption', 'cite', 'center', 'code', 'col', 'colgroup', 'caption', 'cite', 'center', 'code', 'col', 'colgroup',
'data', 'dd', 'del', 'details', 'div', 'dl', 'font', 'data', 'dd', 'del', 'details', 'div', 'dl', 'font',
'dt', 'em', 'footer', 'figure', 'figcaption', 'dt', 'em', 'footer', 'figure', 'figcaption',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'html', 'i', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'html', 'hr', 'i',
'img', 'ins', 'kbd', 'li', 'main', 'mark', 'nav', 'noscript', 'img', 'ins', 'kbd', 'li', 'main', 'mark', 'nav', 'noscript',
'ol', 'p', 'pre', 'q', 'ruby', 'rp', 'rt', 's', 'samp', 'section', 'ol', 'p', 'pre', 'q', 'ruby', 'rp', 'rt', 's', 'samp', 'section',
'small', 'source', 'span', 'strike', 'strong', 'sub', 'summary', 'small', 'source', 'span', 'strike', 'strong', 'sub', 'summary',

View File

@ -0,0 +1,110 @@
<?php
/**
* JavaScript-like HTML DOM Element
*
* This class extends PHP's DOMElement to allow
* users to get and set the innerHTML property of
* HTML elements in the same way it's done in
* JavaScript.
*
* Example usage:
* @code
* require_once 'JSLikeHTMLElement.php';
* header('Content-Type: text/plain');
* $doc = new DOMDocument();
* $doc->registerNodeClass('DOMElement', 'JSLikeHTMLElement');
* $doc->loadHTML('<div><p>Para 1</p><p>Para 2</p></div>');
* $elem = $doc->getElementsByTagName('div')->item(0);
*
* // print innerHTML
* echo $elem->innerHTML; // prints '<p>Para 1</p><p>Para 2</p>'
* echo "\n\n";
*
* // set innerHTML
* $elem->innerHTML = '<a href="http://fivefilters.org">FiveFilters.org</a>';
* echo $elem->innerHTML; // prints '<a href="http://fivefilters.org">FiveFilters.org</a>'
* echo "\n\n";
*
* // print document (with our changes)
* echo $doc->saveXML();
* @endcode
*
* @author Keyvan Minoukadeh - http://www.keyvan.net - keyvan@keyvan.net
* @see http://fivefilters.org (the project this was written for)
*/
class JSLikeHTMLElement extends DOMElement
{
/**
* Used for setting innerHTML like it's done in JavaScript:
* @code
* $div->innerHTML = '<h2>Chapter 2</h2><p>The story begins...</p>';
* @endcode
*/
public function __set($name, $value) {
if ($name == 'innerHTML') {
// first, empty the element
for ($x=$this->childNodes->length-1; $x>=0; $x--) {
$this->removeChild($this->childNodes->item($x));
}
// $value holds our new inner HTML
if ($value != '') {
$f = $this->ownerDocument->createDocumentFragment();
// appendXML() expects well-formed markup (XHTML)
$result = @$f->appendXML($value); // @ to suppress PHP warnings
if ($result) {
if ($f->hasChildNodes()) $this->appendChild($f);
} else {
// $value is probably ill-formed
$f = new DOMDocument();
$value = mb_convert_encoding($value, 'HTML-ENTITIES', 'UTF-8');
// Using <htmlfragment> will generate a warning, but so will bad HTML
// (and by this point, bad HTML is what we've got).
// We use it (and suppress the warning) because an HTML fragment will
// be wrapped around <html><body> tags which we don't really want to keep.
// Note: despite the warning, if loadHTML succeeds it will return true.
$result = @$f->loadHTML('<htmlfragment>'.$value.'</htmlfragment>');
if ($result) {
$import = $f->getElementsByTagName('htmlfragment')->item(0);
foreach ($import->childNodes as $child) {
$importedNode = $this->ownerDocument->importNode($child, true);
$this->appendChild($importedNode);
}
} else {
// oh well, we tried, we really did. :(
// this element is now empty
}
}
}
} else {
$trace = debug_backtrace();
trigger_error('Undefined property via __set(): '.$name.' in '.$trace[0]['file'].' on line '.$trace[0]['line'], E_USER_NOTICE);
}
}
/**
* Used for getting innerHTML like it's done in JavaScript:
* @code
* $string = $div->innerHTML;
* @endcode
*/
public function __get($name)
{
if ($name == 'innerHTML') {
$inner = '';
foreach ($this->childNodes as $child) {
$inner .= $this->ownerDocument->saveXML($child);
}
return $inner;
}
$trace = debug_backtrace();
trigger_error('Undefined property via __get(): '.$name.' in '.$trace[0]['file'].' on line '.$trace[0]['line'], E_USER_NOTICE);
return null;
}
public function __toString()
{
return '['.$this->tagName.']';
}
}
?>

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,61 @@ class Af_RedditImgur extends Plugin {
$this->host = $host; $this->host = $host;
$host->add_hook($host::HOOK_ARTICLE_FILTER, $this); $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
$host->add_hook($host::HOOK_PREFS_TAB, $this);
}
function hook_prefs_tab($args) {
if ($args != "prefFeeds") return;
print "<div id=\"af_redditimgur_prefs\" dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_redditimgur settings')."\">";
$enable_readability = $this->host->get($this, "enable_readability");
$enable_readability_checked = $enable_readability ? "checked" : "";
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>";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_redditimgur\">";
print "<h3>" . __("Global settings") . "</h3>";
print_notice("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
print "<p/>";
print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\"
$enable_readability_checked name=\"enable_readability\">&nbsp;";
print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
__("Save")."</button>";
print "</form>";
print "</div>";
}
function save() {
$enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
$this->host->set($this, "enable_readability", $enable_readability);
echo __("Configuration saved");
} }
function hook_article_filter($article) { function hook_article_filter($article) {
@ -199,6 +254,27 @@ class Af_RedditImgur extends Plugin {
} }
} }
if (!$found && $this->host->get($this, "enable_readability") && mb_strlen(strip_tags($article["content"])) <= 150) {
require_once(__DIR__ . "/classes/Readability.php");
$content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
if ($content_link) {
$tmp = fetch_file_contents($content_link->getAttribute("href"));
if ($tmp) {
$r = new Readability($tmp, $content_link->getAttribute("href"));
if ($r->init()) {
$article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
}
}
}
}
$node = $doc->getElementsByTagName('body')->item(0); $node = $doc->getElementsByTagName('body')->item(0);
if ($node && $found) { if ($node && $found) {