PageRenderTime 39ms CodeModel.GetById 13ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Plug-in/xajax/examples/thewall/thewall.server.php

http://prosporous.googlecode.com/
PHP | 236 lines | 119 code | 24 blank | 93 comment | 13 complexity | 5bff4f139f47acebd4540902e3f0c9e8 MD5 | raw file
  1<?php
  2/*
  3	File: thewall.server.php
  4
  5	Example which demonstrates a xajax implementation of a graffiti wall.
  6	
  7	Title: Graffiti Wall Example
  8	
  9	Please see <copyright.inc.php> for a detailed description, copyright
 10	and license information.
 11*/
 12
 13/*
 14	Section: Files
 15	
 16	- <thewall.php>
 17	- <thewall.common.php>
 18	- <thewall.server.php>
 19*/
 20
 21/*
 22	@package xajax
 23	@version $Id: thewall.server.php 362 2007-05-29 15:32:24Z calltoconstruct $
 24	@copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
 25	@license http://www.xajaxproject.org/bsd_license.txt BSD License
 26*/
 27
 28/*
 29	Section: Define constants
 30	
 31	Integer: MAX_SCRIBBLES
 32	
 33	The number of scribbles that will be retained.  The default is 5.
 34*/
 35if (!defined ('MAX_SCRIBBLES'))
 36{
 37	define ('MAX_SCRIBBLES', 5);
 38}
 39
 40/*
 41	String: DATA_FILE
 42	
 43	The file that will be used to store the messages on the server.
 44*/
 45if (!defined ('DATA_FILE'))
 46{
 47	define ('DATA_FILE', "thewall.dta");
 48}
 49
 50/*
 51	Class: graffiti
 52*/
 53class graffiti
 54{
 55	/*
 56		String: html
 57		
 58		Stores the html that is being generated for the current request.
 59	*/
 60	var $html;
 61	
 62	/*
 63		Boolean: isValid
 64		
 65		Indicates that the html generated for this request is valid and
 66		can be added to the wall <DATA_FILE>.
 67	*/
 68	var $isValid = false;
 69	
 70	/*
 71		Constructor: graffiti
 72		
 73		Builds the <html> output and sets the <isValid> indicator.
 74	*/
 75	function graffiti($sHandle, $sWords)
 76	{
 77		if (trim($sHandle) == "" || trim($sWords) == "")
 78		{
 79			return;
 80		}
 81		$this->html  = "\n<div style=\"font-weight: bold;text-align:".$this->getRandomAlignment();
 82		$this->html .= ";color:".$this->getRandomColor().";\">";
 83		$this->html .= "<span style=\"font-size:".$this->getRandomFontSize()."%;\">";
 84		$this->html .= strip_tags(stripslashes($sWords));
 85		$this->html .= "</span><br/><span style=\"font-size: small;\">";
 86		$this->html .= " ~ ".strip_tags(stripslashes($sHandle))." ".date("m/d/Y H:i:s")."</span></div>";
 87		
 88		$this->isValid = true;
 89	}
 90	
 91	/*
 92		Function: getRandomFontSize
 93		
 94		Generate a font size based off a random number.
 95	*/
 96	function getRandomFontSize()
 97	{
 98		srand((double)microtime()*1000003);
 99		return rand(100,300);
100	}
101	
102	/*
103		Function: getRandomColor
104		
105		Generate a browser safe color based on a random number.
106	*/
107	function getRandomColor()
108	{
109		$sColor = "rgb(";
110		srand((double)microtime()*1000003);
111		$sColor .= rand(0,255).",";
112		srand((double)microtime()*1000003);
113		$sColor .= rand(0,255).",";
114		$sColor .= rand(0,255).")";
115		
116		return $sColor;
117	}
118	
119	/*
120		Function: getRandomAlignment
121		
122		Generates a text-alignment value based on a random number.
123	*/
124	function getRandomAlignment()
125	{
126		$sAlign = "";
127		srand((double)microtime()*1000003);
128		$textAlign = rand(0,2);
129		switch($textAlign)
130		{
131			case 0: $sAlign = "left"; break;
132			case 1: $sAlign = "right"; break;
133			case 2: $sAlign = "center"; break;
134			
135		}
136		return $sAlign;
137	}
138	
139	/*
140		Function: save
141		
142		Writes the current <graffiti->html> to the <DATA_FILE> when <graffiti->isValid>
143		or returns an error message.
144	*/
145	function save()
146	{
147		if ($this->isValid)
148		{
149			$rFile = @fopen(DATA_FILE,"a+");
150			if (!$rFile) {
151				return "ERROR: the graffiti data file could not be written to the " . dirname(realpath(DATA_FILE)) . " folder.";
152			}
153			fwrite($rFile, $this->html);
154			fclose($rFile);
155			return null;
156		}
157		else
158		{
159			return "Please supply both a handle and some graffiti to scribble on the wall.";
160		}
161	}
162}
163
164/*
165	Section: xajax request handlers
166	
167	Function: scribble
168	
169	Processes the users form input and passes the values to an instance
170	of the <graffiti> class.
171	
172	If the graffiti class generates and error, it is returned to the browser
173	using <xajax->alert>, otherwise, <xajax->script> is used to instruct the
174	browser to make a request to <updateWall> and <xajax->clear> is used to
175	clear the form input.
176*/
177function scribble($aFormValues)
178{
179	$sHandle = $aFormValues['handle'];
180	$sWords = $aFormValues['words'];
181	$objResponse = new xajaxResponse();
182	
183	$objGraffiti = new graffiti($sHandle,$sWords);
184	$sErrMsg = $objGraffiti->save();
185	if (!$sErrMsg)
186	{
187		$objResponse->script("xajax_updateWall();");
188		$objResponse->clear("words","value");
189	}
190	else
191		$objResponse->alert($sErrMsg);
192	
193	return $objResponse;
194}
195
196/*
197	Function: updateWall
198	
199	Processes the data previously written to the <DATA_FILE> by <graffiti->save>.
200*/
201function updateWall()
202{
203	$objResponse = new xajaxResponse();
204	
205	if (file_exists(DATA_FILE)) {
206		$aFile = @file(DATA_FILE);
207		if (!$aFile) {
208			$objResponse->addAlert("ERROR: the graffiti data file could not be written to the " . dirname(realpath(DATA_FILE)) . " folder.");
209			return $objResponse;
210		}
211		
212		$sHtmlSave = implode("\n",array_slice($aFile, -MAX_SCRIBBLES));
213		$sHtmlSave=str_replace("\n\n","\n",$sHtmlSave);
214	}
215	else {
216		$sHtmlSave = "";
217		$aFile = array();
218	}
219	$rFile = @fopen(DATA_FILE,"w+");
220	if (!$rFile) {
221		$objResponse->alert("ERROR: the graffiti data file could not be written to the " . dirname(realpath(DATA_FILE)) . " folder.");
222		return $objResponse;
223	}
224	fwrite($rFile, $sHtmlSave);
225	fclose($rFile);
226	
227	$sHtml = implode("\n",array_reverse(array_slice($aFile, -MAX_SCRIBBLES)));
228	
229	$objResponse->assign("theWall","innerHTML",$sHtml);
230
231	return $objResponse;
232}
233
234require("thewall.common.php");
235$xajax->processRequest();
236?>