Created
January 17, 2013 05:05
-
-
Save jhuckaby/4553800 to your computer and use it in GitHub Desktop.
RSS News Widget for CubixCloud.com.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// RSS Feed Widget for CubixCloud | |
// by Joseph Huckaby | |
// Copyright (c) 2012 CubixCloud.com | |
// Insert this into your HTML: | |
// <iframe width="570" height="243" frameborder="0" src="cc-news-widget.php" marginwidth="0" marginheight="0" vspace="0" hspace="0" scrolling="no"></iframe> | |
$rss_url = 'http://blog.cubixcloud.com/feed'; | |
$feed = XML_unserialize( file_get_contents($rss_url) ); | |
$articles = alwaysArray( $feed['rss']['channel']['item'] ); | |
?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>CubixCloud Blog</title> | |
<base href="http://blog.cubixcloud.com/" /> | |
<style type='text/css'> | |
body{margin:0;padding:0;overflow-x:hidden;} | |
#container{overflow:hidden;margin:0;padding:0;width:570px;height:243px;font-family:helvetica;font-size:13px;border:0px solid #CCCCCC;} | |
#header{margin:0px;padding:5px 5px 5px 5px;color:#FFFFFF;background-color:#0066FF;} | |
#header .feed_title{margin:0;padding:0;font-weight:bold;} | |
#header .feed_title a:link{color:#FFFFFF;text-decoration:none;} | |
#header .feed_title a:visited{color:#FFFFFF;text-decoration:none;} | |
#header .feed_title a:hover{color:#FFFFFF;text-decoration:underline;} | |
#header .feed_title a:active{color:#FFFFFF;text-decoration:none;} | |
#content{margin:0px;padding:5px 0px 0px 0px;background-color:#f2f2f2;} | |
#content .feed_item{margin:0 0 7px 0;padding:0 0 7px 0;border-bottom:1px dashed #CCCCCC;} | |
#content .feed_item_title{margin:1px 0 1px 3px;padding:1px 2px 1px 3px;color:#666666;font-weight:bold;} | |
#content .feed_item_title a:link{color:#666666;text-decoration:none;} | |
#content .feed_item_title a:visited{color:#666666;text-decoration:none;} | |
#content .feed_item_title a:hover{color:#666666;text-decoration:underline;} | |
#content .feed_item_title a:active{color:#666666;text-decoration:none;} | |
#content .feed_item_podcast{margin:0 0 0 3px;padding:0 0 0 3px;} | |
#content .feed_item_description { | |
margin:0 0 0 3px; | |
padding:0 2px 0 3px; | |
color:#666666; | |
line-height:17px; | |
height:17px; | |
white-space: nowrap; | |
width: 555px; | |
overflow: hidden; | |
text-overflow:ellipsis; | |
} | |
#footer{display:none;height:0px;margin:0px;padding:0px;color:#FFFFFF;background-color:#f2f2f2;} | |
</style> | |
</head> | |
<body> | |
<div id="container"><div id="content"> | |
<?php | |
foreach ($articles as $article) { | |
print '<div class="feed_item"><div class="feed_item_title"><a href="'.$article['link'].'" target="_blank">'.$article['title'].'</a></div><div class="feed_item_description">'.$article['description'].'</div></div>' . "\n"; | |
} | |
?> | |
</div><div id="footer"></div></div> | |
</body></html> | |
<?php | |
// Joe's PHP XML Parser | |
// Copyright (c) 2005 - 2012 by Joseph Huckaby | |
// MIT Licensed | |
// store last xml parser error | |
$last_xml_error = null; | |
class XML { | |
public $parser; // PHP XML parser object | |
public $tree; // XML hash tree | |
public $name; // Root document node name | |
public $error; // Last error encountered and line # | |
public $collapseAttribs = 0; // Set to 1 to collapse attributes into parent node | |
public $indentString = "\t"; // Pretty-print indent string for composing | |
public $output; // Holds XML output while composing | |
public $dtdNode; // DTD node to be composed under PI node | |
public $compress = 0; // Whether to compress output (no whitespace) | |
function XML($xmlstring="") { | |
// Class constructor | |
// If string is passed in, parse immediately | |
$this->dtdNode = ""; | |
if ($xmlstring) return( $this->parse($xmlstring) ); | |
return true; | |
} | |
function parse($xmlstring="") { | |
// Parse text into XML hash tree | |
// Returns root node | |
$this->parser = xml_parser_create(); | |
$this->tree = array(); | |
$this->name = ""; | |
$this->clearError(); | |
// Setup PHP XML parser | |
xml_set_object($this->parser, $this); | |
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); | |
xml_set_element_handler($this->parser, "startElement", "endElement"); | |
xml_set_character_data_handler($this->parser, "characterData"); | |
// If argument was file instead of string, load it now | |
if (!preg_match("/<.+?>/", $xmlstring)) $xmlstring = file_get_contents($xmlstring); | |
if (!$xmlstring || !strlen($xmlstring)) { | |
$this->throwError("File not found"); | |
xml_parser_free($this->parser); | |
return false; | |
} // file not found | |
// Issue command to parse | |
$result = xml_parse($this->parser, $xmlstring); | |
if (!$result) { | |
// error occured -- get string, line number and return false | |
$code = xml_get_error_code( $this->parser ); | |
$this->throwError( xml_error_string( $code ) . " on line " . xml_get_current_line_number( $this->parser ) ); | |
xml_parser_free($this->parser); | |
return false; | |
} | |
// Success -- free parser memory and return hash tree | |
xml_parser_free($this->parser); | |
return $this->tree; | |
} | |
function clearError() { | |
// clear error state | |
global $last_xml_error; | |
$last_xml_error = null; | |
$this->error = null; | |
} | |
function throwError($msg) { | |
// set error state | |
global $last_xml_error; | |
$last_xml_error = $msg; | |
$this->error = $msg; | |
} | |
function startElement($parser, $name, $attribs) { | |
// Callback function for PHP XML parser | |
// called when new elements are opened | |
$node = array(); | |
$node["_Name"] = $name; | |
if (count($attribs)) { | |
// Element has attributes -- gather them first | |
if ($this->collapseAttribs) { | |
foreach ($attribs as $key => $value) { | |
$node[$key] = $value; | |
} | |
} | |
else { | |
$node["_Attribs"] = array(); | |
foreach ($attribs as $key => $value) { | |
$node["_Attribs"][$key] = $value; | |
} | |
} | |
} | |
// Add new node onto stack | |
array_push( $this->tree, $node ); | |
} | |
function endElement($parser, $name) { | |
// Callback function for PHP XML parser | |
// called when elements are closed | |
// Pop most recent node off stack | |
$node = array_pop( $this->tree ); | |
// Trim whitespace from text data and delete | |
// if it is empty | |
if (isset($node["_Data"])) { | |
$node["_Data"] = trim($node["_Data"]); | |
if (!preg_match("/\S/", $node["_Data"])) unset($node["_Data"]); | |
} | |
// Extract name of node | |
$name = $node["_Name"]; | |
unset($node["_Name"]); | |
// $payload is a reference to the node object, or just | |
// the text data if there are no attribs or child elements | |
$payload = &$node; | |
// Count number of nodes left in stack | |
$lastnode = count($this->tree); | |
if (!$lastnode) { | |
// This the root node being closed, so we are done | |
// Set $tree to this node and return immediately. | |
$this->name = $name; | |
$this->tree = $node; | |
return; | |
} | |
else if ((isset($node["_Data"]) && count($node) == 1) || !count($node)) { | |
// simple text node or empty node -- collapse so that | |
// payload points to text data only | |
if (!isset($node["_Data"])) $node["_Data"] = ""; | |
$payload = &$node["_Data"]; | |
} | |
// Now we add this node as a child of our parent node | |
if (!isset($this->tree[$lastnode-1][$name]) || !$this->tree[$lastnode-1][$name]) { | |
// first time for this node name in parent node | |
// point directly to us (don't use array yet) | |
$this->tree[$lastnode-1][$name] = $payload; | |
} | |
else if (is_array($this->tree[$lastnode-1][$name]) && isset($this->tree[$lastnode-1][$name][0])) { | |
// node name already exists in parent node, and is real array | |
// so just push our node on the end | |
// print_r( $this->tree[$lastnode-1][$name] ); | |
array_push( $this->tree[$lastnode-1][$name], $payload ); | |
} | |
else { | |
// node name exists in parent node, but need to convert to true array | |
// and push our node on the end | |
$temp = $this->tree[$lastnode-1][$name]; | |
$this->tree[$lastnode-1][$name] = array(); | |
array_push( $this->tree[$lastnode-1][$name], $temp ); | |
array_push( $this->tree[$lastnode-1][$name], $payload ); | |
} | |
} | |
function characterData($parser, $data) { | |
// Callback function for PHP XML parser | |
// called when text data is encountered | |
$lastnode = count($this->tree); | |
if (!isset($this->tree[$lastnode-1]["_Data"]) || !$this->tree[$lastnode-1]["_Data"]) | |
$this->tree[$lastnode-1]["_Data"] = ""; | |
$this->tree[$lastnode-1]["_Data"] .= $data; | |
} | |
function getLastError() { | |
// Return last error encountered as string | |
// Also contains line number in source XML file | |
return $this->error; | |
} | |
function getTree() { | |
// Return hash tree | |
return $this->tree; | |
} | |
function setDTDNode($text) { | |
// set pre-composed DTD node for composing | |
$this->dtdNode = $text; | |
} | |
} // class XML | |
/** | |
* Static utility functions | |
**/ | |
function alwaysArray($obj) { | |
// detect if $obj is an associative array or a true array | |
// if associative, put inside a new true array as element 0 | |
if (!is_array($obj) || (is_array($obj) && !isset($obj[0]))) return array($obj); | |
else return $obj; | |
} | |
function XML_unserialize($text) { | |
// static wrapper around XML parser | |
// includes document node in object | |
$parser = new XML(); | |
$tree = $parser->parse( $text ); | |
if (!$tree) return null; | |
else { | |
// wrap document in root node | |
$xml = array(); | |
$xml[ $parser->name ] = $tree; | |
return $xml; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment