I'm having some trouble with my RSS feed parser. It probably isn't the best of code, hence my troubles with in..
If a feed is not available, my catch doesn't catch it and the feed is still parsed causing my homepage to fall over.
Can you take a look and see what I can do to make it better so its less harsh on my server...
<?php
function startElement($parser,$name,$attrs)
{
global $rss_channel, $currently_writing, $main;
switch($name)
{
case 'RSS':
case 'RDF:RDF':
case 'ITEMS':
$currently_writing = '';
break;
case 'CHANNEL':
$main = 'CHANNEL';
break;
case 'IMAGE':
$main = 'IMAGE';
$rss_channel['IMAGE'] = array();
break;
case 'ITEM':
$main = 'ITEMS';
break;
default:
$currently_writing = $name;
break;
}
}
function endElement($parser,$name)
{
global $rss_channel, $currently_writing, $item_counter;
$currently_writing = '';
if($name == 'ITEM')
{
$item_counter++;
}
}
function characterData($parser,$data)
{
global $rss_channel, $currently_writing, $main, $item_counter;
if($currently_writing != '')
{
switch($main)
{
case 'CHANNEL':
if(isset($rss_channel[$currently_writing]))
{
$rss_channel[$currently_writing] .= $data;
}
else
{
$rss_channel[$currently_writing] = $data;
}
break;
case 'IMAGE':
if(isset($rss_channel[$main][$currently_writing]))
{
$rss_channel[$main][$currently_writing] .= $data;
}
else
{
$rss_channel[$main][$currently_writing] = $data;
}
break;
case 'ITEMS':
if(isset($rss_channel[$main][$item_counter][$currently_writing]))
{
$rss_channel[$main][$item_counter][$currently_writing] .= $data;
}
else
{
$rss_channel[$main][$item_counter][$currently_writing] = $data;
}
break;
}
}
}
function removeEvilAttributes($tagSource)
{
$stripAttrib = "' (style|class)=\"(.*?)\"'i";
$tagSource = stripslashes($tagSource);
$tagSource = preg_replace($stripAttrib, '', $tagSource);
return $tagSource;
}
function removeEvilTags($source)
{
$allowedTags = '<p><a></a></p>';
$source = strip_tags($source, $allowedTags);
return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source);
}
set_time_limit(0);
$rss_channel = array();
$currently_writing = $main = $file = '';
$file = 'http://localhost/rss.xml';
if(($fp = fopen($file,'r')))
{
$item_counter = 0;
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser,'startElement','endElement');
xml_set_character_data_handler($xml_parser,'characterData');
while($data = fread($fp,4096))
{
if(!xml_parse($xml_parser,$data,feof($fp)))
{
die(sprintf('XML error: %s at line %d',xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
$html .= '<table class="feeds-table">'."\n";
$html .= '<tr>'."\n";
$html .= ' <td >'."\n";
$html .= ' <h2>News Headlines</h2>'."\n";
$html .= ' </td>'."\n";
$html .= '</tr>'."\n";
if(isset($rss_channel['ITEMS']))
{
if(count($rss_channel['ITEMS']) > 0)
{
for($i = 0;$i < 5; $i++)
{
$html .= '<tr>'."\n";
$html .= ' <td>'."\n";
$title = str_replace("'",""",$rss_channel['ITEMS'][$i]['TITLE']);
$description = str_replace("'","'",$rss_channel['ITEMS'][$i]['DESCRIPTION']);
$html .= ' <p><a href="'.$rss_channel['ITEMS'][$i]['LINK'].'" onclick="javascript:window.open(this.href);return false;">'.$title.'</a><br/>'.html_entity_decode($description).'</p>'."\n";
$html .= ' </td>'."\n";
$html .= '</tr>'."\n";
}
}
else
{
$html .= '<tr><td><p>there are no articles in this feed.</p></td></tr>';
}
}
$html .= '</table>';
}
?>