Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Created August 14, 2009 11:25
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 philsturgeon/167774 to your computer and use it in GitHub Desktop.
Save philsturgeon/167774 to your computer and use it in GitHub Desktop.
<?php
/*
* ExternalCMS Class
*
* This class abstracts the functionality of intergrating HL cms based sites with external CMS's
* How did this happen? Were there no other options? WHY AM I WRITING THIS?!
*
* @author Phil Sturgeon
*/
class ExternalCMS
{
private $meta_title = NULL;
private $port = 80;
private $protocol = 'http://';
function __construct()
{
global $smarty;
// Cache is used throughout, may as well load it once
$this->cache = new Cache();
// Grab it the once then save refference
$this->smarty = &$smarty;
// Running in secure mode or not?
$this->isSecure( $_SERVER['SERVER_PORT'] == 443 );
}
function setTitle($title)
{
$this->meta_title = $title;
}
function getHeader($section = NULL, $basic_mode = FALSE)
{
header ('Content-type: text/html; charset=iso-8859-1');
// Should we look for the full version or the basic version?
$cache_name = $basic_mode ? 'no_header' : 'header';
// Returns false if no cached file found
$header_code = $this->_getCache( $cache_name, $section );
if(empty($header_code))
{
list($header_code)=explode(
'<!-- HEADER -->',
$this->_fetchContent('header', $section)
);
// Add in our own metadata
$header_code = str_replace('<!-- META DATA -->', $this->smarty->fetch('external_metadata.tpl'), $header_code);
$this->_setCache($cache_name, $section, $header_code);
}
// If there is a meta title passed, override whatever is there
if($this->meta_title !== NULL)
{
$header_code = preg_replace('#<title>(.+)</title>#', '<title>'.$this->meta_title.'</title>', $header_code);
}
return $header_code;
}
function getTopNav($section = NULL, $basic_mode = FALSE)
{
// Should we look for the full version or the basic version?
$cache_name = $basic_mode ? 'no_header' : 'header';
// Returns false if no cached file found
$topnav_code = $this->_getCache( $cache_name, $section );
if(empty($topnav_code))
{
$topnav_code = substr(
$header_code = $this->_fetchContent('topnav', $section),
$start = strpos($header_code, '<!-- HEADER -->'),
strpos($header_code, '<!-- BREADCRUMBS -->') - $start
);
$this->_setCache($cache_name, $section, $topnav_code);
}
return $topnav_code;
}
function getFooter($section = NULL, $basic_mode = FALSE)
{
// Do we want the footer or not?
$footer_type = $basic_mode ? 'no_footer' : 'footer';
// Returns false if no cached file found
$footer_code = $this->_getCache( $footer_type, $section );
if(empty($footer_code))
{
list($footer_code)=array_reverse(explode(
'<!-- START TRADING FOOTER -->',
$this->_fetchContent($footer_type, $section)
));
$this->_setCache($footer_type, $section, $footer_code);
// Add in our own metadata
$footer_code = str_replace('<!-- ONLOAD -->', $this->smarty->fetch('external_footer.tpl'), $footer_code);
}
// Some stupid strange bug, lets just get rid of it if it exists
$footer_code = str_replace('</html><br/>', '</html>', $footer_code);
return $footer_code;
}
public function setLogin($clientnum)
{
$remoteClient = new Remote_client('internet/scripts/remote_server_user.php', 'createLoginHash',
array('clientnum'=> $clientnum)
);
$remoteClient-> debug(0); // Set debug level
$result = $remoteClient->execute(); // Execute request and obtain response
if(array_key_exists('error', $result) or !array_key_exists('hash', $result))
{
return FALSE;
}
setcookie(EXTERNAL_CMS_COOKIE_NAME, $result['hash'], EXTERNAL_CMS_COOKIE_EXPIRES, '/', EXTERNAL_CMS_COOKIE_DOMAIN);
return TRUE;
}
public function clearLogin($clientnum)
{
// Already gone, thats our job done
if(!isset($_COOKIE[EXTERNAL_CMS_COOKIE_NAME]))
{
return;
}
// Grab the cookie hash to check against the database
$hash = $_COOKIE[EXTERNAL_CMS_COOKIE_NAME];
// Kill the cookie (this is more important that clearing the database)
setcookie(EXTERNAL_CMS_COOKIE_NAME, '', time()-42000, '/', EXTERNAL_CMS_COOKIE_DOMAIN);
$remoteClient = new Remote_client('internet/scripts/remote_server_user.php', 'deleteLoginHash',
array('hash'=> $hash, 'clientnum'=> $clientnum)
);
$remoteClient->debug(0); // Set debug level
$remoteClient->execute(); // Execute request
}
public function isSecure($secure = TRUE)
{
// Don't switch if NULL is specifically set. TRUE or FALSE or empty argument will switch answers
if($secure !== NULL)
{
$this->port = $secure ? 443 : 80;
$this->protocol = $secure ? 'https://' : 'http://';
}
}
private function _fetchContent($item, $section)
{
$guess = 'EXTERNAL_CMS_'.strtoupper($section).'_'.strtoupper($item);
if($section !== NULL && defined($guess))
{
$url = constant($guess);
}
else
{
$url = constant('EXTERNAL_CMS_DEFAULT_'.strtoupper($item));
}
return file_get_contents($this->protocol . $url) .'<br/>';
}
private function _getCache($name, $section)
{
if(!empty($section))
{
$name = $section . '/' . $name;
}
$this->cache->get( SITE . '/' . $name . '-' . $this->port);
}
private function _setCache($name, $section, $code)
{
if(!empty($section))
{
$name = $section . '/' . $name;
}
$this->cache->write( $code, SITE . '/' . $name . '-' . $this->port);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment