Skip to content

Instantly share code, notes, and snippets.

@mattkatz
Created April 11, 2010 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattkatz/a121c91894044d32673e to your computer and use it in GitHub Desktop.
Save mattkatz/a121c91894044d32673e to your computer and use it in GitHub Desktop.
<html>
<body>
<h1>Reading List</h1>
<?php
class opmule {
//You've got to have a file in this same directory called "_cache" - I'm going to store a cache of the OPML in there
var $basefilepath;
var $baseuripath;
var $relpath;
var $localbasefilepath;
var $mainID;
var $maxAge =14400;
var $errors = array();
var $debuginfo;
var $_cachefile;
var $xml;
function opmule ($basefilepath = NULL, $baseuripath = NULL, $relpath = NULL, $localbasefilepath = NULL) { // Constructor
/* The user may pass the path parameters if for some reason the
autodiscovery in this function does not work (such as Aliased directories) */
$this->basefilepath = $basefilepath ? $basefilepath : $_SERVER['DOCUMENT_ROOT'];
$this->baseuripath = $baseuripath ? $baseuripath : 'http://'.$_SERVER['SERVER_NAME'];
if ($relpath)
$this->relpath = $relpath;
else
$this->relpath = str_replace($this->basefilepath, '', dirname(__FILE__)) ? str_replace($this->basefilepath, '', dirname(__FILE__)) : '';
if ($localbasefilepath)
$this->localbasefilepath = $localbasefilepath;
else
$this->localbasefilepath = $this->basefilepath;
}
function _error_messages () {
foreach ($this->errors as $msg) {
$errors .= '<li class="outlineItemErrors">'.$msg.'</li>';
}
return $errors;
}
function url_decode_no_spaces ($url) {
$url = html_entity_decode(urldecode($url));
$url = str_replace( ' ', '%20', $url);
return ($url);
}
function _fetch_file ($url, $flIsLocal = FALSE) {
if ($flIsLocal) {
return file_get_contents($url);
}
elseif (function_exists('curl_init')) {
// We have curl
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_USERAGENT,"Optimal/0.4");
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl_handle,CURLOPT_MAXREDIRS,2);
$xml = curl_exec($curl_handle);
curl_close($curl_handle);
}
elseif (ini_get(allow_url_fopen)) {
// We have allow_url_fopen
$xml = @file_get_contents($url);
}
else {
$this->errors[] = "Error: Your PHP installation must have either CURL or allow_url_fopen to use Optimal";
return false;
}
// Write non-local, non-cached xml to the cachefile
$handle = fopen($this->_cachefile,'w');
fwrite($handle,$xml);
fclose($handle);
return $xml;
} // End function _fetch_file
function getOPML($url){
// You can use locally stored files that are under your server's root
// document folder by specifying a relative local path
if (strpos($url, '/') === 0) { # a relative local path
$flIsLocal = TRUE;
} else {
$flIsLocal = FALSE;
}
$this->_cachefile = $this->basefilepath.$this->relpath.'/_cache/'.md5($url).'.'.strtolower($type).'.xml';
$this->debuginfo = "<br/>$type URL: <a href=\"$url\">$url</a><br/>\nCache file: ";
$this->debuginfo .= $this->_cachefile ? '<a href="'.$this->baseuripath.$this->relpath.'/_cache/'.basename($this->_cachefile).'">'.basename($this->_cachefile).'</a>' : "None";
$this->debuginfo .= "<br/>\n";
// Get file contents
if ($flIsLocal) {
// We are rendering a local file
$this->xml = $this->_fetch_file($this->localbasefilepath.$url, TRUE);
} elseif ( (!$flForceRefresh) && file_exists($this->_cachefile) && ( filesize($this->_cachefile) > 0 ) && ( filemtime($this->_cachefile) > ( time() - $maxAge ) ) ) {
// We have a local, non-empty, recent copy, so use it
$this->xml = $this->_fetch_file($this->_cachefile, TRUE);
} else {
$this->xml = $this->_fetch_file($url);
if (empty($this->xml) && file_exists($this->_cachefile)) { // Use a local cachefile as fallback if the remote fetch fails
$this->xml = $this->_fetch_file($this->_cachefile, TRUE);
$xmlIsCached = TRUE;
}
}
if (empty($this->xml)) {
$this->errors[] = "Error reading $type file.$this->debuginfo";
return $this->_error_messages();
}
}//end getOPML
function encaseIn($elm, $attribs, $text){
$result ='';
$result .= '<'.$elm.' '.$attribs.' >';
$result .= $text;
$result .= '</'.$elm.'>';
return $result;
}
function displayChildrenRecursive($xmlObj,$depth=1) {
$result = '';
if (count($xmlObj->children()) > 0) {
$result .= str_repeat("\t",$depth).'<ul class="opmlGroup">'."\n";
}
foreach($xmlObj->children() as $child) {
$result .= str_repeat("\t",$depth).'<li>';
if (isset($child['htmlUrl'])) {
$result .= '<a href="'.htmlentities($child['htmlUrl']). '" title="'.htmlentities($child['description']).'">';
}
else{
$result .= '<h'.($depth+1).'>';
}
if(isset($child['text'])){
$result .= htmlentities($child['text']);
}
if(isset($child['title'])){
$result .= htmlentities($child['title']);
}
if (isset($child['htmlUrl'])) {
$result .= '</a>';
}
else{
$result .= '</h'.($depth+1).'>';
}
if (isset($child['xmlUrl'])){
$result .= ' [<a href="' . htmlentities($child['xmlUrl']) . '">XML Feed</a>]';
}
$result .="</li> \n";
$result .= $this->displayChildrenRecursive($child,$depth+1);
}
if (count($xmlObj->children()) > 0) {
$result .= str_repeat("\t",$depth).'</ul>'."\n";
if ($depth > 1) { echo str_repeat("\t",$depth-1).'</li>'."\n"; }
}
return $result;
}//end displayChildrenRecursive
function displayOPML($url){
$this->getOPML($url);
$opmlFile = new SimpleXMLElement($this->_cachefile ,null,true);
return $this->displayChildrenRecursive($opmlFile->body);
}//end displayOPML
}
$pml = new opmule();
echo $pml->displayOPML('http://morelightmorelight.com/tiny/opml.php?op=publish&key=fb2c26edd2f69d8c57f0ad852fdf98e8a95726e6');
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment