Skip to content

Commit

Permalink
re-apply changes from duncan #23
Browse files Browse the repository at this point in the history
  • Loading branch information
michield committed Jun 8, 2016
1 parent 7d93d25 commit 143373e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 79 deletions.
88 changes: 48 additions & 40 deletions onyx-rss.php
Expand Up @@ -54,33 +54,38 @@ class ONYX_RSS
// Forward compatibility with PHP v.5
// http://www.phpvolcano.com/eide/php5.php?page=start

public function __construct()
{
$this->conf = array();
$this->conf['error'] = '<br /><strong>Error on line %s of '.__FILE__.'</strong>: %s<br />';
$this->conf['cache_path'] = dirname(__FILE__);
$this->conf['cache_time'] = 180;
$this->conf['debug_mode'] = true;
$this->conf['fetch_mode'] = ONYX_FETCH_ASSOC;
$this->lasterror = '';

if (!function_exists('xml_parser_create')) {
$this->raiseError((__LINE__ - 2), ONYX_ERR_NO_PARSER);

return false;
}
public function __construct()
{
$this->conf = array();
$this->conf['error'] = '<br /><strong>Error on line %s of '.__FILE__.'</strong>: %s<br />';
$this->conf['cache_path'] = dirname(__FILE__);
$this->conf['cache_time'] = 180;
$this->conf['debug_mode'] = true;
$this->conf['fetch_mode'] = ONYX_FETCH_ASSOC;
$this->lasterror = '';
$this->context = stream_context_create(array(
'http'=>array(
'timeout' => 5.0
)
));

if (!function_exists('xml_parser_create')) {
$this->raiseError((__LINE__ - 2), ONYX_ERR_NO_PARSER);

$this->parser = @xml_parser_create();
if (!is_resource($this->parser)) {
$this->raiseError((__LINE__ - 3), ONYX_ERR_NO_PARSER);
return false;
}

return false;
}
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
xml_set_character_data_handler($this->parser, 'cdata');
}
$this->parser = @xml_parser_create();
if (!is_resource($this->parser)) {
$this->raiseError((__LINE__ - 3), ONYX_ERR_NO_PARSER);

return false;
}
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
xml_set_character_data_handler($this->parser, 'cdata');
}

public function parse($uri, $file = false, $time = false, $local = false)
{
Expand All @@ -102,9 +107,10 @@ public function parse($uri, $file = false, $time = false, $local = false)
$time = $this->conf['cache_time'];
}
$this->rss['cache_age'] = file_exists($file) ? ceil((time() - filemtime($file)) / 60) : 0;
$cacheHasExpired = $time <= $this->rss['cache_age'];

clearstatcache();
if (!$local && file_exists($file)) {
if (!$local && file_exists($file) && $cacheHasExpired) {
if (($mod = $this->mod_time($uri)) === false) {
$this->raiseError((__LINE__ - 2), ONYX_ERR_INVALID_URI);

Expand All @@ -115,12 +121,13 @@ public function parse($uri, $file = false, $time = false, $local = false)
} elseif ($local) {
$mod = (file_exists($file) && ($m = filemtime($uri))) ? $m : time() + 3600;
}
$feedHasNewContent = $mod >= (time() - ($this->rss['cache_age'] * 60));
}
if (!$file ||
($file && !file_exists($file)) ||
($file && file_exists($file) && $time <= $this->rss['cache_age'] && $mod >= (time() - ($this->rss['cache_age'] * 60)))) {
($file && file_exists($file) && $cacheHasExpired && $feedHasNewContent)) {
clearstatcache();
if (!($fp = @fopen($uri, 'r'))) {
if (!($fp = @fopen($uri, 'r', false, $this->context))) {
$this->raiseError((__LINE__ - 2), ONYX_ERR_INVALID_URI);

return false;
Expand All @@ -146,6 +153,10 @@ public function parse($uri, $file = false, $time = false, $local = false)
$this->rss['cache_age'] = 0;
}
} else {
if ($cacheHasExpired) {
touch($file);
$this->rss['cache_age'] = 0;
}
clearstatcache();
if (!($fp = @fopen($file, 'r'))) {
$this->raiseError((__LINE__ - 2), 'Could not read contents of cache file (<em>'.$cache_file.'</em>).');
Expand Down Expand Up @@ -311,17 +322,14 @@ public function endBuffer()

//private function raiseError($line, $err)

public function raiseError($line, $err)
{
if ($this->conf['debug_mode']) {
// Format error message
$errorMsg = sprintf( $this->conf['error'], $line, $err );
// Raise phpList error
Error( $errorMsg );
} else {
$this->lasterror = $err;
}
}
public function raiseError($line, $err)
{
if ($this->conf['debug_mode']) {
printf($this->conf['error'], $line, $err);
} else {
$this->lasterror = $err;
}
}

public function setCachePath($path)
{
Expand All @@ -348,7 +356,7 @@ public function setFetchMode($mode)
public function mod_time($uri)
{
if (function_exists('version_compare') && version_compare(phpversion(), '4.3.0') >= 0) {
if (!($fp = @fopen($uri, 'r'))) {
if (!($fp = fopen($uri, 'r', false, $this->context))) {
return false;
}

Expand Down
101 changes: 62 additions & 39 deletions rssfeed.php
@@ -1,41 +1,30 @@
<?php

// Initialise var for holding news items HTML
$news = '';

// Set maximum number of news items to display
$max = 10;

// Show fewer news items for certain pages
if (
empty($_SESSION['adminloggedin'])
|| isset($_GET['page']) && !in_array($_GET['page'], array('home', 'about', 'dashboard', 'community','login'))
) {
// Reduce max news items count
$max = 3;
}
/**
* Build the news div element from the rss feed items.
*
* @param ONYX_RSS $rss onyx-rss instance
* @param int $max the maximum number of feed items to return
*
* @return string the generated html or an empty string
*/
function buildNews($rss, $max)
{
if ($rss->numItems() == 0) {
return '';
}

// Load Onxy RSS class file
include dirname(__FILE__).'/onyx-rss.php';
$news = '';
$count = 0;
// reset index so that feed items can be processed more than once
$rss->rss['output_index'] = -1;

$rss = new ONYX_RSS();

// Disable debugging output unless we are running in test mode
if( ! DEVVERSION ) {
$rss->setDebugMode(false);
}

$rss->setCachePath($GLOBALS['tmpdir']);
// Set expiry time to 24hrs (sets $rss->conf['cache_time'])
$rss->setExpiryTime(1440);
// Parse the RSS feed
$parseresult = $rss->parse('https://www.phplist.org/newslist/feed/', 'phplistnews');
// Set the counter to zero
$count = 0;
if ($parseresult) {
while ($item = $rss->getNextItem()) {
$count++;
if ($count > $max) break;

if ($count > $max) {
break;
}
$date = $item['pubdate'];
$date = str_replace('00:00:00 +0000', '', $date);
$date = str_replace('00:00:00 +0100', '', $date);
Expand All @@ -44,7 +33,7 @@
$date = str_replace($regs[0], '', $date);
}

// remove the '<p>&nbsp;</p>' in the descriptions
## remove the '<p>&nbsp;</p>' in the descriptions
$desc = $item['description'];
$desc = str_replace('<p>&nbsp;</p>', '', $desc);
$desc = '';
Expand All @@ -54,12 +43,46 @@
'.$desc.'
</li>';
}

$format = <<<END
<div id="newsfeed" class="menutableright block">
<h3>%s</h3>
<ul>%s</ul>
</div>
END;

return sprintf($format, s('phpList community news'), $news);
}

/**
* Generate the short and long news sidebars from an rss feed then cache
* in the session.
*/

$newsSize = 'long';

if (empty($_SESSION['adminloggedin'])
|| (isset($_GET['page']) && !in_array($_GET['page'], array('home', 'about', 'dashboard', 'community','login')))) {
$newsSize = 'short';
}

if (isset($_SESSION['news'][$newsSize])) {
echo $_SESSION['news'][$newsSize];

return;
}

include dirname(__FILE__) . '/onyx-rss.php';
$rss = new ONYX_RSS();
if( ! DEVVERSION ) {
$rss->setDebugMode(false);
}
$rss->setCachePath($GLOBALS['tmpdir']);
$rss->setExpiryTime(1440);
$parseresult = $rss->parse('https://www.phplist.org/newslist/feed/', 'phplistnews');

if (!empty($news)) {
print '<div id="newsfeed" class="menutableright block">';
print '
<h3>'.s('phpList community news').'</h3>
<ul>'.$news.'</ul>';
print '</div>';
if ($parseresult) {
$_SESSION['news']['short'] = buildNews($rss, 3);
$_SESSION['news']['long'] = buildNews($rss, 10);
echo $_SESSION['news'][$newsSize];
}

0 comments on commit 143373e

Please sign in to comment.