From d526decc4884710ae7fafe7aa5171e7f59b24292 Mon Sep 17 00:00:00 2001 From: jhunkeler Date: Sun, 27 Dec 2009 15:44:57 +0000 Subject: MySQL -> MySQLi Fixed memory leaks after Fetching arrays Removed all while loops that interfaced with $db->Fetch() Rewrote RSS module Fixed syntax error in donations module Fixed link structure for news module Reverting to old page display method git-svn-id: http://svn.3rd-infantry-division.org/testing/branches/Bayonet CMS v2@402 c5b2fb0a-d05d-0410-98c8-828840a80ff6 --- blocks/donations/index.php | 5 +- blocks/rss_agw/index.php | 175 +++++++++++++++++++++++++------------------ blocks/rss_agw/rssreader.php | 161 --------------------------------------- 3 files changed, 103 insertions(+), 238 deletions(-) delete mode 100644 blocks/rss_agw/rssreader.php (limited to 'blocks') diff --git a/blocks/donations/index.php b/blocks/donations/index.php index 4deb5dc..ff77657 100644 --- a/blocks/donations/index.php +++ b/blocks/donations/index.php @@ -3,9 +3,8 @@
- - + +
diff --git a/blocks/rss_agw/index.php b/blocks/rss_agw/index.php index 1ecad83..8649582 100644 --- a/blocks/rss_agw/index.php +++ b/blocks/rss_agw/index.php @@ -1,80 +1,107 @@ -load($cacheFile); - - $items = $rss->getItems(); #returns all rss items - - $numFeeds = 0; - echo "
"; - foreach($items as $story){ - if($story['category']=="Tournament Announcements"){ - $numFeeds++; - //$text = $story['description']; - //$text = strip_tags($text); - //$text = preg_replace("(\[font(.+?)...)","", $text); - //$text = stripBBCode($text); - - echo "{$story['title']}
- {$story['pubDate']}
"; - //echo "{$text}

"; - echo "

"; - } +// {{{ constants + +/** + * Armed Global Warfare Feed + */ +define('AGW_FEED', 'http://www.armedglobalwarfare.com/index.php?type=rss;action=.xml;limit=150'); + +/** + * Cache File + */ +define('AGW_CACHE', dirname(__FILE__) . '/rss_cache.xml'); +define('AGW_AGE_CACHE', time() - 3600); + +/** + * Feed Variables + */ +$agwFeed = array(); +$agwXml = null; +$agwProcess = true; + +// }}} + +// {{{ main + +/** + * Check existance of cache + */ +if (file_exists(AGW_CACHE) && (filectime(AGW_CACHE) > AGW_AGE_CACHE)) { + $agwFeed = simplexml_load_file(AGW_CACHE); + $agwProcess = false; +} + +/** + * If we don't have a cache then we'll need to build one + */ +if ($agwProcess === true) { + /** + * Set up global options for cURL to utilize for the transfer. + */ + $options = array(CURLOPT_FORBID_REUSE => true, + CURLOPT_POST => false, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 15, + CURLOPT_USERAGENT => 'Mozilla/5.0 (Compatible; libCURL)', + CURLOPT_VERBOSE => false); + + /** + * Initialize cURL + */ + $agwFeedSource = curl_init(AGW_FEED); + curl_setopt_array($agwFeedSource, $options); + + /** + * Execute cURL container and store the output + */ + $agwFeedOutput = curl_exec($agwFeedSource); + + /** + * Parse the received data + */ + + if (!curl_errno($agwFeedSource)) { + $agwFeed = simplexml_load_string($agwFeedOutput); + $agwXml = new SimpleXMLElement($agwFeedOutput); + + file_put_contents(AGW_CACHE, $agwXml->asXML(), LOCK_EX); + curl_close($agwFeedSource); } - if(!$numFeeds){ - echo "No new updates for this news feed."; + else { + curl_close($agwFeedSource); } - echo "
"; - // echo "
";
-  //print_r($items);
-   // echo "
"; -?> + + /** + * Check to make sure the results are not empty before proceeding. + */ + if (empty($agwFeed) || !is_object($agwFeed)) $agwFeed = array(); +} + +/** + * Process output + */ +foreach ($agwFeed->channel->item as $item) { + if ($item->category != 'Tournament Announcements') continue; + + echo '' . + '' . $item->title . '
' . + '' . $item->pubDate . '


' . PHP_EOL; +} +// }}} +?> diff --git a/blocks/rss_agw/rssreader.php b/blocks/rss_agw/rssreader.php deleted file mode 100644 index 41fa238..0000000 --- a/blocks/rss_agw/rssreader.php +++ /dev/null @@ -1,161 +0,0 @@ - - Published: 200801 :: blacknet :: via rssphp.net - - RSS_PHP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. - - Usage: - See the documentation at http://rssphp.net/documentation - Examples: - Can be found online at http://rssphp.net/examples -*/ - -class rss_php { - - public $document; - public $channel; - public $items; - -/**************************** - public load methods -***/ - # load RSS by URL - public function load($url=false, $unblock=true) { - if($url) { - if($unblock) { - $this->loadParser(file_get_contents($url, false, $this->randomContext())); - } else { - $this->loadParser(file_get_contents($url)); - } - } - } - # load raw RSS data - public function loadRSS($rawxml=false) { - if($rawxml) { - $this->loadParser($rawxml); - } - } - -/**************************** - public load methods - @param $includeAttributes BOOLEAN - return array; -***/ - # return full rss array - public function getRSS($includeAttributes=false) { - if($includeAttributes) { - return $this->document; - } - return $this->valueReturner(); - } - # return channel data - public function getChannel($includeAttributes=false) { - if($includeAttributes) { - return $this->channel; - } - return $this->valueReturner($this->channel); - } - # return rss items - public function getItems($includeAttributes=false) { - if($includeAttributes) { - return $this->items; - } - return $this->valueReturner($this->items); - } - -/**************************** - internal methods -***/ - private function loadParser($rss=false) { - if($rss) { - $this->document = array(); - $this->channel = array(); - $this->items = array(); - $DOMDocument = new DOMDocument; - $DOMDocument->strictErrorChecking = false; - $DOMDocument->loadXML($rss); - $this->document = $this->extractDOM($DOMDocument->childNodes); - } - } - - private function valueReturner($valueBlock=false) { - if(!$valueBlock) { - $valueBlock = $this->document; - } - foreach($valueBlock as $valueName => $values) { - if(isset($values['value'])) { - $values = $values['value']; - } - if(is_array($values)) { - $valueBlock[$valueName] = $this->valueReturner($values); - } else { - $valueBlock[$valueName] = $values; - } - } - return $valueBlock; - } - - private function extractDOM($nodeList,$parentNodeName=false) { - $itemCounter = 0; - foreach($nodeList as $values) { - if(substr($values->nodeName,0,1) != '#') { - if($values->nodeName == 'item') { - $nodeName = $values->nodeName.':'.$itemCounter; - $itemCounter++; - } else { - $nodeName = $values->nodeName; - } - $tempNode[$nodeName] = array(); - if($values->attributes) { - for($i=0;$values->attributes->item($i);$i++) { - $tempNode[$nodeName]['properties'][$values->attributes->item($i)->nodeName] = $values->attributes->item($i)->nodeValue; - } - } - if(!$values->firstChild) { - $tempNode[$nodeName]['value'] = $values->textContent; - } else { - $tempNode[$nodeName]['value'] = $this->extractDOM($values->childNodes, $values->nodeName); - } - if(in_array($parentNodeName, array('channel','rdf:RDF'))) { - if($values->nodeName == 'item') { - $this->items[] = $tempNode[$nodeName]['value']; - } elseif(!in_array($values->nodeName, array('rss','channel'))) { - $this->channel[$values->nodeName] = $tempNode[$nodeName]; - } - } - } elseif(substr($values->nodeName,1) == 'text') { - - $tempValue = trim(preg_replace('/\s\s+/',' ',str_replace("\n",' ', $values->textContent))); - if($tempValue) { - $tempNode = $tempValue; - } - } elseif(substr($values->nodeName,1) == 'cdata-section'){ - $tempNode = $values->textContent; - } - } - return $tempNode; - } - - private function randomContext() { - $headerstrings = array(); - $headerstrings['User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.'.rand(0,2).'; en-US; rv:1.'.rand(2,9).'.'.rand(0,4).'.'.rand(1,9).') Gecko/2007'.rand(10,12).rand(10,30).' Firefox/2.0.'.rand(0,1).'.'.rand(1,9); - $headerstrings['Accept-Charset'] = rand(0,1) ? 'en-gb,en;q=0.'.rand(3,8) : 'en-us,en;q=0.'.rand(3,8); - $headerstrings['Accept-Language'] = 'en-us,en;q=0.'.rand(4,6); - $setHeaders = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'."\r\n". - 'Accept-Charset: '.$headerstrings['Accept-Charset']."\r\n". - 'Accept-Language: '.$headerstrings['Accept-Language']."\r\n". - 'User-Agent: '.$headerstrings['User-Agent']."\r\n"; - $contextOptions = array( - 'http'=>array( - 'method'=>"GET", - 'header'=>$setHeaders - ) - ); - return stream_context_create($contextOptions); - } - -} - -?> \ No newline at end of file -- cgit