Skip to content

Instantly share code, notes, and snippets.

@jgeboski
Last active December 18, 2021 19:33
Show Gist options
  • Save jgeboski/304c2bcd406afe037174 to your computer and use it in GitHub Desktop.
Save jgeboski/304c2bcd406afe037174 to your computer and use it in GitHub Desktop.
Tiny-Tiny-RSS plugin which removes images from the content of articles
<?php
/*
* Copyright 2014 James Geboski <jgeboski@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* This Tiny-Tiny-RSS plugin removes images from the content of
* articles, and strips out any left over elements related to the
* images.
*
* Installing:
* 1. Move this file to <tt-rss-root>/plugins/no_images/init.php
* 2. Enable via the prefernces interface
*/
class No_Images extends Plugin
{
private $host;
public function about()
{
return array(1.0, 'Remove embedded images', 'jgeboski', false);
}
public function api_version()
{
return 2;
}
public function init($host)
{
$this->host = $host;
$host->add_hook($host::HOOK_FORMAT_ENCLOSURES, $this);
$host->add_hook($host::HOOK_SANITIZE, $this);
}
public function hook_format_enclosures($rv, $result, $id,
$always_display_enclosures,
$article_content, $hide_images)
{
/* Disable attachments (no sane MIME calls in PHP) */
return array('', array());
}
public function hook_sanitize($doc, $site_url, $allowed_elements,
$disallowed_attributes, $article_id)
{
$xpath = new DOMXpath($doc);
$nodes = $xpath->query('//img');
$full = array('img');
foreach ($nodes as $node) {
$ops = array(
'nextSibling' => NULL,
'previousSibling' => NULL,
'parentNode' => NULL
);
foreach ($ops as $op => $n) {
$ops[$op] = $this->getElement($node, $op);
}
$node->parentNode->removeChild($node);
foreach ($ops as $op => $n) {
while (($n != NULL) && $this->isElementEmpty($n, $full)) {
$s = $this->getElement($n, $op);
$n->parentNode->removeChild($n);
$n = $s;
}
}
}
return array($doc, $allowed_elements, $disallowed_attributes);
}
private function isElementEmpty($node, $full = array())
{
if ($node->nodeType != XML_ELEMENT_NODE) {
return false;
}
$name = strtolower($node->nodeName);
if (in_array($name, $full)) {
return false;
}
foreach ($node->childNodes as $c) {
if ($node->nodeType != XML_ELEMENT_NODE) {
continue;
}
if (!$this->isElementEmpty($c, $full)) {
return false;
}
}
$val = html_entity_decode($node->nodeValue);
return preg_match('/^\s*$/', $val) == 1;
}
private function getElement($node, $op)
{
for ($n = $node->$op; $n != NULL; $n = $n->$op) {
if ($n->nodeType == XML_ELEMENT_NODE) {
return $n;
}
}
return NULL;
}
}
?>
@earthling-slavlius
Copy link

Hi,
May you please add possibility to activate this for a certain feed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment