Commit bf8899ee authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) refactor rss.widget.php, use latest SimplePie

(cherry picked from commit e5c48cf6)
parent e25e9f10
<?php <?php
/* /*
Copyright (C) 2014 Deciso B.V. Copyright (C) 2014-2016 Deciso B.V.
Copyright (C) 2009 Scott Ullrich Copyright (C) 2009 Scott Ullrich
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
...@@ -30,8 +30,18 @@ $nocsrf = true; ...@@ -30,8 +30,18 @@ $nocsrf = true;
require_once("guiconfig.inc"); require_once("guiconfig.inc");
require_once("pfsense-utils.inc"); require_once("pfsense-utils.inc");
require_once("simplepie/autoloader.php");
require_once("simplepie/idn/idna_convert.class.php");
if ($_POST['rssfeed']) { function textLimit($string, $length, $replacer = '...')
{
if (strlen($string) > $length) {
return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;
}
return $string;
}
if (!empty($_POST['rssfeed'])) {
$config['widgets']['rssfeed'] = str_replace("\n", ",", htmlspecialchars($_POST['rssfeed'], ENT_QUOTES | ENT_HTML401)); $config['widgets']['rssfeed'] = str_replace("\n", ",", htmlspecialchars($_POST['rssfeed'], ENT_QUOTES | ENT_HTML401));
$config['widgets']['rssmaxitems'] = str_replace("\n", ",", htmlspecialchars($_POST['rssmaxitems'], ENT_QUOTES | ENT_HTML401)); $config['widgets']['rssmaxitems'] = str_replace("\n", ",", htmlspecialchars($_POST['rssmaxitems'], ENT_QUOTES | ENT_HTML401));
$config['widgets']['rsswidgetheight'] = htmlspecialchars($_POST['rsswidgetheight'], ENT_QUOTES | ENT_HTML401); $config['widgets']['rsswidgetheight'] = htmlspecialchars($_POST['rsswidgetheight'], ENT_QUOTES | ENT_HTML401);
...@@ -41,45 +51,33 @@ if ($_POST['rssfeed']) { ...@@ -41,45 +51,33 @@ if ($_POST['rssfeed']) {
} }
// Use saved feed and max items // Use saved feed and max items
if ($config['widgets']['rssfeed']) { if (!empty($config['widgets']['rssfeed'])) {
$rss_feed_s = explode(",", $config['widgets']['rssfeed']); $rss_feed_s = explode(",", $config['widgets']['rssfeed']);
} $textarea_txt = str_replace(",", "\n", $config['widgets']['rssfeed']);
} else {
if ($config['widgets']['rssmaxitems']) { // Set a default feed if none exists
$max_items = $config['widgets']['rssmaxitems'];
}
if (is_numeric($config['widgets']['rsswidgetheight'])) {
$rsswidgetheight = $config['widgets']['rsswidgetheight'];
}
if (is_numeric($config['widgets']['rsswidgettextlength'])) {
$rsswidgettextlength = $config['widgets']['rsswidgettextlength'];
}
// Set a default feed if none exists
if (!$rss_feed_s) {
$rss_feed_s = "https://opnsense.org/feed/"; $rss_feed_s = "https://opnsense.org/feed/";
$config['widgets']['rssfeed'] = "https://opnsense.org/feed/"; $config['widgets']['rssfeed'] = "https://opnsense.org/feed/";
$textarea_txt = "";
} }
if (!$max_items) { if (!empty($config['widgets']['rssmaxitems']) && is_numeric($config['widgets']['rssmaxitems'])) {
$max_items = $config['widgets']['rssmaxitems'];
} else {
$max_items = 10; $max_items = 10;
} }
if (!$rsswidgetheight) { if (!empty($config['widgets']['rsswidgetheight']) && is_numeric($config['widgets']['rsswidgetheight'])) {
$rsswidgetheight = $config['widgets']['rsswidgetheight'];
} else {
$rsswidgetheight = 300; $rsswidgetheight = 300;
} }
if (!$rsswidgettextlength) { if (!empty($config['widgets']['rsswidgettextlength']) && is_numeric($config['widgets']['rsswidgettextlength'])) {
$rsswidgettextlength = 140; // oh twitter, how do we love thee? $rsswidgettextlength = $config['widgets']['rsswidgettextlength'];
}
if ($config['widgets']['rssfeed']) {
$textarea_txt = str_replace(",", "\n", $config['widgets']['rssfeed']);
} else { } else {
$textarea_txt = ""; $rsswidgettextlength = 140; // oh twitter, how do we love thee?
} }
?> ?>
<input type="hidden" id="rss-config" name="rss-config" value="" /> <input type="hidden" id="rss-config" name="rss-config" value="" />
...@@ -99,11 +97,10 @@ if ($config['widgets']['rssfeed']) { ...@@ -99,11 +97,10 @@ if ($config['widgets']['rssfeed']) {
<td> <td>
<select name='rssmaxitems' id='rssmaxitems'> <select name='rssmaxitems' id='rssmaxitems'>
<option value='<?= $max_items ?>'><?= $max_items ?></option> <option value='<?= $max_items ?>'><?= $max_items ?></option>
<?php <?php
for ($x=100; $x<5100; $x=$x+100) { for ($x=100; $x<5100; $x=$x+100) {
echo "<option value='{$x}'>{$x}</option>\n"; echo "<option value='{$x}'>{$x}</option>\n";
} }?>
?>
</select> </select>
</td> </td>
</tr> </tr>
...@@ -114,11 +111,10 @@ if ($config['widgets']['rssfeed']) { ...@@ -114,11 +111,10 @@ if ($config['widgets']['rssfeed']) {
<td> <td>
<select name='rsswidgetheight' id='rsswidgetheight'> <select name='rsswidgetheight' id='rsswidgetheight'>
<option value='<?= $rsswidgetheight ?>'><?= $rsswidgetheight ?>px</option> <option value='<?= $rsswidgetheight ?>'><?= $rsswidgetheight ?>px</option>
<?php <?php
for ($x=100; $x<5100; $x=$x+100) { for ($x=100; $x<5100; $x=$x+100) {
echo "<option value='{$x}'>{$x}px</option>\n"; echo "<option value='{$x}'>{$x}px</option>\n";
} }?>
?>
</select> </select>
</td> </td>
</tr> </tr>
...@@ -129,11 +125,10 @@ if ($config['widgets']['rssfeed']) { ...@@ -129,11 +125,10 @@ if ($config['widgets']['rssfeed']) {
<td> <td>
<select name='rsswidgettextlength' id='rsswidgettextlength'> <select name='rsswidgettextlength' id='rsswidgettextlength'>
<option value='<?= $rsswidgettextlength ?>'><?= $rsswidgettextlength ?></option> <option value='<?= $rsswidgettextlength ?>'><?= $rsswidgettextlength ?></option>
<?php <?php
for ($x=10; $x<5100; $x=$x+10) { for ($x=10; $x<5100; $x=$x+10) {
echo "<option value='{$x}'>{$x}</option>\n"; echo "<option value='{$x}'>{$x}</option>\n";
} }?>
?>
</select> </select>
</td> </td>
</tr> </tr>
...@@ -148,30 +143,20 @@ if ($config['widgets']['rssfeed']) { ...@@ -148,30 +143,20 @@ if ($config['widgets']['rssfeed']) {
<div id="rss-widgets" style="padding: 5px; height: <?=$rsswidgetheight?>px; overflow:scroll;"> <div id="rss-widgets" style="padding: 5px; height: <?=$rsswidgetheight?>px; overflow:scroll;">
<?php <?php
if (!is_dir("/tmp/simplepie")) { if (!is_dir("/tmp/simplepie")) {
mkdir("/tmp/simplepie"); mkdir("/tmp/simplepie");
mkdir("/tmp/simplepie/cache"); mkdir("/tmp/simplepie/cache");
} }
exec("chmod a+rw /tmp/simplepie/."); exec("chmod a+rw /tmp/simplepie/.");
exec("chmod a+rw /tmp/simplepie/cache/."); exec("chmod a+rw /tmp/simplepie/cache/.");
require_once("simplepie/simplepie.inc");
function textLimit($string, $length, $replacer = '...')
{
if (strlen($string) > $length) {
return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;
}
return $string;
}
$feed = new SimplePie(); $feed = new SimplePie();
$feed->set_cache_location("/tmp/simplepie/"); $feed->set_cache_location("/tmp/simplepie/");
$feed->set_feed_url($rss_feed_s); $feed->set_feed_url($rss_feed_s);
$feed->init(); $feed->init();
$feed->set_output_encoding('latin-1');
$feed->handle_content_type(); $feed->handle_content_type();
$counter = 1;
foreach ($feed->get_items() as $item) {
$feed = $item->get_feed();
$feed->strip_htmltags(); $feed->strip_htmltags();
$counter = 1;
foreach ($feed->get_items() as $item) {
echo "<a target='blank' href='" . $item->get_permalink() . "'>" . $item->get_title() . "</a><br />"; echo "<a target='blank' href='" . $item->get_permalink() . "'>" . $item->get_title() . "</a><br />";
$content = $item->get_content(); $content = $item->get_content();
$content = strip_tags($content); $content = strip_tags($content);
...@@ -182,7 +167,7 @@ foreach ($feed->get_items() as $item) { ...@@ -182,7 +167,7 @@ foreach ($feed->get_items() as $item) {
break; break;
} }
echo "<hr/>"; echo "<hr/>";
} }
?> ?>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment