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