Location: PHPKode > projects > Cadence Guestbook Host > cadence/class/gbook.php
<?php

/************************************
 * Cadence
 * Remotely Hosted Guestbook Script.
 * (c) 2006, Dennis Pedrie
 * www.CadenceBook.com
 * gbook.php
 ***********************************
 * Cadence Guestbook is licensed under
 * a Creative Commons License.
 * More information is available by visiting
 * http://creativecommons.org/licenses/by/3.0/
 * or the LICENSE file in the Cadence Root Folder
 ***********************************/

/**
* Main Guestbook Class.<br />
* Script Hub.
*
*
* @name gbook
* @package	Cadence Guestbook
* @author   Dennis Pedrie
* @copyright Dennis Pedrie
* @version	1.0
*/

class gbook {
	
	/**
	* Title of the Guestbook
	*
	* @var string
	*/
	var $title;
	
	/**
	* Style ID
	*
	* @var int
	*/
	var $style;
	
	/**
	* Whether the Guestbook has been closed.
	*
	* @var int. 0 or 1
	*/
	var $closed;
	
	/**
	* Allow Style
	*
	* @var int. 0 or 1
	*/
	var $allow_style;
	
	/**
	* Type of Comment Validation
	*
	* @var int
	*/
	var $val_type;
	
	/**
	* List of all available styles for this guestbook
	*
	* @var array
	*/
	
	var $gb_styles;
	
	/**
	* Flood Control Limit
	*
	* @var int
	*/
	
	var $flood;
	
	/**
	* Total Guestbook Posts
	*
	* @var int
	*/
	
	var $totalposts;
	
	/**
	* Admin Control Panel Notes
	*
	* @var int
	*/
	
	var $acpnotes;
	
	/**
	* Style Attributes. CSS, Images, etc.
	*
	* @var array
	*/
	var $stylevar = array();
	
	/**
	* Host's URL.
	*
	* @var string
	*/
	var $hosturl;
	
	/**
	* Host's Name
	*
	* @var string
	*/
	var $hostname;
	
	/**
	* Application Version
	*
	* @var string
	*/
	var $cadence_version;
	
	/**
	* Logged in?
	*
	* @var boolean
	*/
	var $logged = false;
	
	/**
	* Custom Styles<br />
	* This list is written by the class constructor, then used in the Style Selector.
	*
	* @var string
	*/
	var $cus_styles = "";
	
	/**
	* FlatFile CSS<br />
	* This list is written by the class constructor, then used in the Style Selector.
	*
	* @var boolean
	*/
	var $css_flat = "";
	
	/**
	* About Me Variable
	*
	* @var string
	*/
	var $about;
	
	/**
	* Display Name
	*
	* @var string
	*/
	var $displayname = null;
	
	/**
	* Logged E-Mail
	*
	* @var string
	*/
	var $email;
	
	/**
	* User Hash
	*
	* @var string
	*/
	var $hash;
	
	/**
	* SUBCLASS: Post
	*
	* @var string
	*/
	var $post;
	
	/**
	* SUBCLASS: Queries
	*
	* @var string
	*/
	var $q;
	
	/**
	* SUBCLASS: Smarty
	*
	* @var string
	*/
	var $tpl;
	
	/**
	* SUBCLASS: Post_Parser
	*
	* @var string
	*/
	var $parser;
	
	/**
	* SUBCLASS: Login
	*
	* @var string
	*/
	var $login;
	
	/**
	* SUBCLASS: DB
	*
	* @var string
	*/
	var $db;

	/**
	* SUBCLASS: Debug
	*
	* @var string
	*/
	var $debug;

	
	/**
	 * Starts the script and grabs all the style and book settings from the database.
	 *
	 * @name gbook
	 * @author Dennis Pedrie
	 * @version 1.0 BUILD 1000
	 * @param int Guestbook ID
	 *
	 **/
	function gbook($book) {
		global $db,$q,$post,$CONFIG;
		
		//Instantiate Classes
		$this->post = new post;
		$this->q = new queries;
		$this->tpl = new Smarty;
		$this->login = new login;
		$this->db = new db($CONFIG['sql_user'], $CONFIG['sql_pass'], $CONFIG['sql_dbname'], $CONFIG['sql_host']);
		$this->debug = new debug;
		$this->debug->timer_start();

		//Set Configuration Values
		$this->hosturl = $CONFIG['url'];
		$this->hostname = $CONFIG['host_title'];
		$this->cadence_version = $CONFIG['cadence_version'];
		
		//Get Guestbook Settings
		$settings = $db->get_results($q->getsettings($book));
		if($db->num_rows > 0) {
			foreach($settings as $settings) {
				$this->title 		= $settings->gbook_title;
				$this->style 		= $settings->gbook_style_id;
				$this->email 		= $settings->gbook_email;
				$this->closed 		= $settings->gbook_closed;
				$this->allow_style  = $settings->gbook_allow_usr_style;
				$this->val_type 	= $settings->gbook_val_type;
				$this->gb_styles 	= $settings->gbook_enabled_styles;
				$this->flood 		= $settings->gbook_flood;
				$this->about		= $settings->gbook_about;
				$this->totalposts   = $settings->gbook_posts;
				$this->styleid		= $settings->gbook_style;
				$this->auth			= $settings->gbook_hash;
				$this->created		= $settings->gbook_created;
				$this->display_name = $settings->gbook_display_name;
				$this->acpnotes		= (!empty($settings->gbook_notes)) ? $settings->gbook_notes : "Use this area to keep notes for later reference";
			}
		}
		
		//If the GB has been closed.
		if($this->closed == 1) {
			die("This Guestbook has been closed for <a href='index.php?p=tos'>TOS</a> violations.<br />
			Please contact management for more information.");
		}

		$styleid = false;
		$styleid = $post->clean_var(intval($_GET['styleid']));
		if($styleid != 0) {
			$this->customstyle($styleid);
		}

		//Check Custom User Style
		$gbstyle = intval($_COOKIE[COOKIE_PREFIX .'style']);

		if($gbstyle != 0) {
			unset($this->style);
			$this->style = $gbstyle;
		}

		//Return Style Vars.
		$this->get_style_vars($book,$this->style);
		
		//Get Custom styles here instead of in template.
		if($this->allow_style == 1) {
			$isstyle = ($_COOKIE[COOKIE_PREFIX .'style']) ? $_COOKIE[COOKIE_PREFIX .'style'] : false;
			$styles = $db->get_results($q->style_choose($book));
			foreach($styles as $styles) {
				$this->cus_style .= ($isstyle == $styles->style_id) ? "<option value='$styles->style_id' selected='selected'>$styles->style_name</option>\n" : "<option value='$styles->style_id'>$styles->style_name</option>\n";
			}
		}
		
		//Set Cookie Vars
		$this->displayname = $post->clean_var($_COOKIE[COOKIE_PREFIX .'display_name']);
		$this->email = $post->clean_var($_COOKIE[COOKIE_PREFIX .'email']);
		$this->hash = $post->clean_var($_COOKIE[COOKIE_PREFIX .'hash']);
	}
	
	/**
	 * Check Valid Guestbook ID
	 *
	 * @name check_id
	 * @author Dennis Pedrie
	 * @version 1.0 BUILD 1000
	 * @param int Guestbook ID
	 *
	 **/
	function check_id($book) {
		global $db,$q;
		if(basename($_SERVER["SCRIPT_FILENAME"]) == "index.php") {
			if(empty($book)) {
				return false;
			}
			$id = $db->get_var($q->checkgbook($book));
			if($db->num_rows > 0) {
				return true;
			}
			else {
				return false;
			}
		}
		if(basename($_SERVER["SCRIPT_FILENAME"]) == "admin.php" && empty($book)) {
			return header("location:index.php");
		}
	}
	
	/**
	 * Get the Style Vars for use in templates
	 *
	 * @name get_style_vars
	 * @author Dennis Pedrie
	 * @version 1.0 BUILD 1000
	 * @param int Guestbook ID
	 * @param int Style ID
	 *
	 **/
	function get_style_vars($book,$style) {
		global $db,$q,$CONFIG;
		
		//Query for the Style Content
		$style = $db->get_results($q->getstyle($this->style));
		
		if($db->num_rows == 0 && isset($book)) {
			trigger_error("Style Error: Style Does Not Exist!",E_USER_ERROR);
		}
		
		else {
			foreach($style as $style) {
				global $tpl;
							
				//Parse for PHP.
				$this->stylevar = array(
					'id'				=>	 $style->style_id,
					'css'				=>	 $CONFIG['url'] . "templates/". $style->style_id ."/styles.css",
					'img_edit'			=>	 $CONFIG['url'] . $style->style_img_edit,
					'img_del'			=>	 $CONFIG['url'] . $style->style_img_delete,
					'img_ipl'			=>	 $CONFIG['url'] . $style->style_img_iplogged,
					'smilie_angry'		=>	 $CONFIG['url'] . $style->smilie_angry,
					'smilie_biggrin'	=>	 $CONFIG['url'] . $style->smilie_biggrin,
					'smilie_blink'		=>	 $CONFIG['url'] . $style->smilie_blink,
					'smilie_closedeyes' =>	 $CONFIG['url'] . $style->smilie_closedeyes,
					'smilie_cool'		=>	 $CONFIG['url'] . $style->smilie_cool,
					'smilie_dry'		=>	 $CONFIG['url'] . $style->smilie_dry,
					'smilie_excl'		=>	 $CONFIG['url'] . $style->smilie_excl,
					'smilie_glare'		=>	 $CONFIG['url'] . $style->smilie_glare,
					'smilie_happy'		=>	 $CONFIG['url'] . $style->smilie_happy,
					'smilie_huh'		=>	 $CONFIG['url'] . $style->smilie_huh,
					'smilie_laugh'		=>	 $CONFIG['url'] . $style->smilie_laugh,
					'smilie_mad'		=>	 $CONFIG['url'] . $style->smilie_mad,
					'smilie_mellow'		=>	 $CONFIG['url'] . $style->smilie_mellow,
					'smilie_ninja'		=>	 $CONFIG['url'] . $style->smilie_ninja,
					'smilie_blush'		=>	 $CONFIG['url'] . $style->smilie_blush,
					'smilie_ohmy'		=>	 $CONFIG['url'] . $style->smilie_ohmy,
					'smilie_rolleyes'	=>	 $CONFIG['url'] . $style->smilie_rolleyes,
					'smilie_sad'		=>	 $CONFIG['url'] . $style->smilie_sad,
					'smilie_sleep'		=>	 $CONFIG['url'] . $style->smilie_sleep,
					'smilie_smile'		=>	 $CONFIG['url'] . $style->smilie_smile,
					'smilie_tongue'		=>	 $CONFIG['url'] . $style->smilie_tongue,
					'smilie_unsure'		=>	 $CONFIG['url'] . $style->smilie_tongue,
					'smilie_wacko'		=>	 $CONFIG['url'] . $style->smilie_wacko,
					'smilie_wink'		=>	 $CONFIG['url'] . $style->smilie_wink,
					'smilie_wub'		=>	 $CONFIG['url'] . $style->smilie_wub,
				);
				
				$this->css_flat = ($style->style_css_flat == 1) ? true : false;
				
				if($this->css_flat == true) {
					$tpl->assign('styleid',$this->style);
					$tpl->assign("style",$this->stylevar);
				}
			}

			return $this->stylevar;
		}
		
	}
	
	/**
	 * Returns Styleid
	 *
	 * @deprecated Do not use.
	 *
	 **/
	function returnstyle($book,$stylevar,$var) {
		
		return $stylevar[$var];
	}
	
	/**
	 * Changes the style.
	 *
	 * @name customstyle
	 * @author Dennis Pedrie
	 * @version 1.0 BUILD 1000
	 * @param int Style ID
	 *
	 **/
	
	function customstyle($styleid) {
		global $db,$q,$book;
		
		$checkstyle = $db->get_var($q->customstyle($styleid));
		if($db->num_rows > 0) {
			setcookie(COOKIE_PREFIX ."style","",time() - 3600,"/");
			setcookie(COOKIE_PREFIX ."style","$styleid",time()+60*60*24*15,"/");
			return header("location:index.php?book=$book");
		}
		else {
			return false;
		}
	}
	
	/**
	 * Kill the script, echo the error
	 *
	 * @name kill
	 * @author Dennis Pedrie
	 * @version 1.0 BUILD 1000
	 * @param string Message to echo
	 *
	 **/
	
	function kill($msg) {
		global $tpl;
		
		$tpl->assign('msg',$msg);
		($tpl->_tpl_vars['header_sent'] == 1) ? $tpl->display($this->style .'/header.tpl') : '';
		$tpl->display($this->style .'/kill.tpl');
		$tpl->display($this->style .'/footer.tpl');
		exit;
	}
	
	/**
	 * Rebuild Style Cache.<br />
	 * Style Cache saves a query by cacheing the style vars to files.<br />
	 * It is rebuilt every time the style is edited.<br />
	 * Doesn't do anything yet, so don't use it. :)
	 *
	 * @name build_style_cache
	 * @author Dennis Pedrie
	 * @version 1.0 BUILD 1000
	 * @param int Style ID
	 *
	 **/	
	function build_style_cache($style) {
	}
	
	function convert_name_to_id($name) {
		
	}
	
	function pagination($rows, $per_page, $current_page, $page_link) {
		
		// Create a Page Listing
		$this->pages = ceil($rows / $per_page);
		
		// If there's only one page, return now and don't bother
		if($this->pages == 1) {
			return;
		}
		
		// Pagination Prefix
		$output = "<a href=\"javascript:pagination('". $page_link ."','". $current_page ."','". $rows ."','". $this->pages ."','". $per_page ."')\" title=\"Choose a Page\">Pages:</a> (". $this->pages ." Total) ";
		
		// Should we show the FIRST PAGE link?
		if($current_page > 2) {
			$output .= " <a href=\"". $page_link ."&page=1\" title=\"First Page\"><<</a> ";
		}
		
		// Should we show the PREVIOUS PAGE link?
		if($current_page > 1) {
			$previous_page = $current_page - 1;
			$output .= " <a href=\"". $page_link ."&page=". $previous_page ."\" title=\"Previous Page\"><</a> ";
		}
		
		// Current Page Number
		$output .= " <strong>[ ". $current_page ." ]</strong> ";
		
		// Should we show the NEXT PAGE link?
		if($current_page < $this->pages) {
			$next_page = $current_page + 1;
			$output .= " <a href=\"". $page_link ."&page=". $next_page ."\" title=\"Next Page\">></a> ";
		}
		
		// Should we show the LAST PAGE link?
		if($current_page < $this->pages - 1) {
			$output .= " <a href=\"". $page_link ."&page=". $this->pages ."\" title=\"Last Page\">>></a> ";
		}
		
		// Return the output.
		return $output;
	}
}

/**
 * Debug Class. Clocks the Script's operating time.
 *
 * @name Debug
 * @author Dennis Pedrie
 * @since v1.0
 */
class debug {
	
	/**
	 * Start the clock
	 *
	 * @name timer_start
	 * @author Dennis Pedrie
	 * @since v1.1.0Beta2
	 */
	 
	function timer_start() {
		global $timestart;
		$mtime = explode(' ', microtime() );
		$timestart = $mtime[1] + $mtime[0];
		return true;
	}

	/**
	 * Stop the Clock.
	 *
	 * @name timer_stop
	 * @author Dennis Pedrie
	 * @since v1.1.0Beta2
	 */
	 
	function timer_stop($display = 0) {
		global $timestart, $timeend;
		$mtime = explode(' ', microtime());
		$timeend = $mtime[1] + $mtime[0];
		$timetotal = $timeend - $timestart;
		$timetotal = round(($timeend - $timestart), 3);
		return $timetotal;
	}
}

?>
Return current item: Cadence Guestbook Host