Location: PHPKode > projects > Stack's Wiki > stacks-wiki-0.5.1/www/includes/page.php
<?
require_once("params.php");
require_once("database.php");
require_once("markdown.php");

class Page
{
	var $id;
	var $category;
	var $raw_content;
	var $formatted_content;
	var $created_by;
	var $created_on;
	var $revised_by;
	var $revised_on;
	var $links;
	var $exists;
	
	function Page($id = "Home Page")
	{
		global $database_prefix;
		if ($id == "") $id = "Home Page";
		
		/* Try to pull the page from the database */
		$db = get_connection();
		$sql  = "SELECT * FROM " . $database_prefix . "pages WHERE id = '";
		$sql .= mysql_real_escape_string($id, $db) . "'";
		$result = mysql_query($sql, $db);
		if (!$result) die("Could not get page data: " . mysql_error());
		
		if (mysql_num_rows($result) == 0)
		{
			/* Page doesn't exist, create the default data */
			$this->id = $id;
			$this->category = "";
			$this->raw_content = "";
			$this->formatted_content  = "<h1>" . $this->id;
			$this->formatted_content .= " Does Not Exist, Yet!</h1>";
			$this->formatted_content .= "<p>If you would like to create ";
			$this->formatted_content .= "this page, login and click the ";
			$this->formatted_content .= "&quot;Edit&quot; link below.</p>";
			$this->created_by = "";
			$this->created_on = "";
			$this->revised_by = "";
			$this->revised_on = "";
			$this->links = array();
			$this->exists = false;
		}
		else
		{
			/* Page exists, fill the class variables */
			$row = mysql_fetch_assoc($result);
			$this->id = $id;
			$this->category = $row["category"];
			$this->raw_content = $row["raw_content"];
			$this->formatted_content  = $row["formatted_content"];
			$this->created_by = $row["created_by"];
			$this->created_on = $row["created_on"];
			$this->revised_by = $row["revised_by"];
			$this->revised_on = $row["revised_on"];
			$this->links = array();
			$this->exists = true;
			
			/* Grab associated links */
			mysql_free_result($result);
			$sql  = "SELECT `link_id`, `exists` FROM `" . $database_prefix;
			$sql .= "links` WHERE `page_id` = '";
			$sql .= mysql_real_escape_string($this->id, $db) . "'";
			$result = mysql_query($sql, $db);
			if (!$result) die("Could not pull links: " . mysql_error());
			
			while ($row = mysql_fetch_array($result))
			{
				$this->links[$row[0]] = $row[1];
			}
		}
		
		mysql_free_result($result);
		mysql_close($db);
	}
	
	function format_link($matches)
	{
		global $database_prefix;
		
		$match = html_entity_decode($matches[1]);
		
		if ($match == $this->id) 
			$exists = true;
		elseif (array_key_exists($match, $this->links)) 
			$exists = $this->links[$match];
		else
		{
			$sql  = "SELECT COUNT(id) FROM `" . $database_prefix . "pages` ";
			$sql .= " WHERE id = '" . mysql_real_escape_string($match);
			$sql .= "'";
			$result = mysql_query($sql);
			if (!$result) die ("Invalid query: " . mysql_error());
			
			$row = mysql_fetch_row($result);
			if ($row[0] != 0) $exists = true;
			else $exists = false;
			
			$this->links[$match] = $exists;
			mysql_free_result($result);
		}
		
		$formatted = "";
		if ($exists)
		{
			$formatted  = "<a href=\"?id=" . rawurlencode($match);
			$formatted .= "\" class=\"page_exists\">";
			$formatted .= $matches[1] . "</a>";
		}
		else
		{
			$formatted  = "<span class=\"missing\">" . $matches[1] . "</span>";
			$formatted .= "<a href=\"?id=" . rawurlencode($match) . "\" ";
			$formatted .= "class=\"page_missing\">?</a>";
		}
		
		return $formatted;
	}
	
	function reformat_content($raw)
	{
		global $page_footer;
		
		$this->raw_content = $raw;
		$formatted = Markdown($raw);
		$formatted .= "<p class=\"content_footer\">\n";
		$formatted .= "\tBelongs to the <a href=\"categories.php?category=";
		$formatted .= $this->category . "\" class=\"page_exists\">";
		$formatted .= $this->category . "</a> category<br />\n";
		$formatted .= "\tCreated on " . $this->created_on . " by [[" . $this->created_by . "]]<br />\n";
		if ($this->revised_on != null)
		{
			$formatted .= "\tRevised on " . $this->revised_on . " by [[" . $this->revised_by . "]]<br />\n";
		}
		$formatted .= "\t" . $page_footer . "\n";
		$formatted .= "</p>";
		$formatted = $this->get_category() . "\n". $formatted;
		
		
		$db = get_connection();
		$this->links = array();
		$this->formatted_content = preg_replace_callback('/\[\[(.+)\]\]/U', array(&$this, 'format_link'), $formatted);
		mysql_close($db);
	}
	
	function save()
	{
		global $database_prefix;
		
		/* Save the main page data */
		$db = get_connection();
		if ($this->exists)
		{
			/* Update page data */
			$sql  = "UPDATE `" . $database_prefix . "pages` SET `category` = '";
			$sql .= mysql_real_escape_string($this->category) . "', ";
			$sql .= "`raw_content` = '";
			$sql .= mysql_real_escape_string($this->raw_content);
			$sql .= "', `formatted_content` = '";
			$sql .= mysql_real_escape_string($this->formatted_content);
			$sql .= "', `revised_by` = '";
			$sql .= mysql_real_escape_string($this->revised_by);
			$sql .= "', `revised_on` = NOW() WHERE ";
			$sql .= "`id` = '" . mysql_real_escape_string($this->id) . "'";
			if (!mysql_query($sql, $db))
				die ("Could not save page: " . mysql_error());
			
			/* Clear out old link data */
			$sql  = "DELETE FROM `" . $database_prefix . "links` WHERE ";
			$sql .= "`page_id` = '" . mysql_real_escape_string($this->id) . "'";
			if (!mysql_query($sql, $db))
				die ("Could not clear links: " . mysql_error());
			
			/* Insert new link data */
			foreach (array_keys($this->links) as $key)
			{
				$sql  = "INSERT INTO `" . $database_prefix . "links` ";
				$sql .= " (`page_id`,`link_id`,`exists`) VALUES (";
				$sql .= "'" . mysql_real_escape_string($this->id) . "',";
				$sql .= "'" . mysql_real_escape_string($key) . "',";
				$sql .= $this->links[$key] ? "1" : "0";
				$sql .= ")";
				if (!mysql_query($sql, $db)) 
					die ("Could not insert link: " . mysql_error());
			}
		}
		else
		{
			$sql  = "INSERT INTO `" . $database_prefix . "pages` ";
			$sql .= "(`id`,`category`, `raw_content`, `formatted_content`,";
			$sql .= " `created_by`, `created_on`) VALUES ";
			$sql .= "('" . mysql_real_escape_string($this->id) . "','";
			$sql .= mysql_real_escape_string($this->category);
			$sql .= "','" . mysql_real_escape_string($this->raw_content);
			$sql .= "','" . mysql_real_escape_string($this->formatted_content);
			$sql .= "','" . mysql_real_escape_string($this->created_by);
			$sql .= "', NOW())";
			if (!mysql_query($sql, $db)) 
				die ("Could not save page: " . mysql_error());
			
			/* Insert new link data */
			foreach (array_keys($this->links) as $key)
			{
				$sql  = "INSERT INTO `" . $database_prefix . "links` ";
				$sql .= " (`page_id`,`link_id`,`exists`) VALUES (";
				$sql .= "'" . mysql_real_escape_string($this->id) . "',";
				$sql .= "'" . mysql_real_escape_string($key) . "',";
				$sql .= $this->links[$key] ? "1" : "0";
				$sql .= ")";
				if (!mysql_query($sql, $db))
					die ("Could not insert link: " . mysql_error());
			}
			
			/* Recreate pages that linked here */
			$sql  = "SELECT `page_id` FROM `" . $database_prefix . "links` ";
			$sql .= "WHERE `link_id` = '";
			$sql .= mysql_real_escape_string($this->id) . "'";
			$result = mysql_query($sql, $db);
			if (!$result)
				die ("Could not get link information: " . mysql_error());
			
			while ($row = mysql_fetch_array($result))
			{
				$other_page = new Page($row[0]);
				$other_page->reformat_content($other_page->raw_content);
				$other_page->save();
			}
			mysql_free_result($result);
		}
	}
	
	function delete()
	{
		global $database_prefix;
		$db = get_connection();
		
		/* Delete page data */
		$sql  = "DELETE FROM `" . $database_prefix . "pages` WHERE `id` = '";
		$sql .= mysql_real_escape_string($this->id) . "'";
		if (!mysql_query($sql, $db))
			die ("Could not delete page: " . mysql_error());
		
		/* Delete page links */
		$sql  = "DELETE FROM `" . $database_prefix;
		$sql .= "links` WHERE `page_id` = '";
		$sql .= mysql_real_escape_string($this->id) . "'";
		if (!mysql_query($sql, $db))
			die ("Could not delete links: " . mysql_error());
		
		
		/* Recreate pages that linked here */
		$sql  = "SELECT `page_id` FROM `" . $database_prefix . "links` WHERE ";
		$sql .= "`link_id` = '" . mysql_real_escape_string($this->id) . "'";
		$result = mysql_query($sql, $db);
		if (!$result) die ("Could not get link information: " . mysql_error());
	
		while ($row = mysql_fetch_array($result))
		{
			$other_page = new Page($row[0]);
			$other_page->reformat_content($other_page->raw_content);
			$other_page->save();
		}
		
		$this->Page($this->id);
	}
	
	function get_category()
	{
		$image = "";
		
		if (file_exists("../categories/" . $this->category . ".png"))
		{
			$dimensions = getimagesize("../categories/" . $this->category . ".png");
			$image = "<img class=\"category_image\" height=\"" . $dimensions[1] . "\" width=\"" . $dimensions[0] . "\" src=\"categories/" . $this->category . ".png\" />";
		}
		
		return $image;
	}
}
?>
Return current item: Stack's Wiki