PageRenderTime 225ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/include/inc_ext/SOLMETRA_FormValidator/SPAF_FormValidator.class.php

http://phpwcms.googlecode.com/
PHP | 332 lines | 210 code | 29 blank | 93 comment | 34 complexity | e26596673e2479738ff0b7dbfffc5f67 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, ISC, MIT, LGPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.0, BSD-3-Clause
  1. <?php
  2. /* ----------------------------------------------------------------------------
  3. SPAF_FormValidator.class.php
  4. ------------------------------------------------------------------------------
  5. version : 1.01
  6. author : martynas@solmetra.com
  7. ------------------------------------------------------------------------------
  8. Form validation class
  9. --------------------------------------------------------------------------- */
  10. class SPAF_FormValidator {
  11. // {{{
  12. // !!! EDITABLE CONFIGURATION ===============================================
  13. var $lib_dir = 'lib/';
  14. var $backgrounds = array('01.png', '02.png', '03.png', '04.png', '05.png',
  15. '06.png', '07.png', '08.png', '09.png', '10.png',
  16. '11.png', '12.png');
  17. var $fonts = array('solmetra1.ttf', 'solmetra2.ttf', 'solmetra3.ttf',
  18. 'solmetra4.ttf');
  19. var $font_sizes = array(13, 14, 15);
  20. var $colors = array(
  21. array(221, 27, 27),
  22. array(94, 71, 212),
  23. array(212, 71, 210),
  24. array(8, 171, 0),
  25. array(234, 142, 0)
  26. );
  27. var $shadow_color = array(255, 255, 255);
  28. var $hide_shadow = false;
  29. var $char_num = 5;
  30. var $chars = array('A', 'C', 'D', 'E', 'F', 'H', 'J', 'K', 'L', 'M',
  31. 'N', 'O', 'P', 'R', 'S', 'T', 'Y', '3', '4', '6',
  32. '7', '9');
  33. var $session_var = 'spaf_form_validator_tag';
  34. var $no_session = false; // If this is set to true following config
  35. // variables must also be set
  36. var $work_dir = 'work'; // If $no_session is set to true set this variable
  37. // to the directory in which FormValidator will
  38. // create its temporary files.
  39. // If path begins with a backslash (i.e. /tmp),
  40. // FormValidator will assume it's an absolute path
  41. // Otherwise path will be treated as relative to
  42. // FormValidator class location.
  43. // Please note that this directory must be
  44. // writable to PHP scripts.
  45. var $work_ext = 'spaf'; // An extention to use for temporary work files
  46. var $tag_ttl = 120; // Number of minutes to consider user tag valid
  47. // Used only in conjunction with:
  48. // $no_session = true
  49. var $tag_cookie = 'spaf_formvalidator'; // the name of cookie to be used
  50. // for tagging a user
  51. var $gc_prob = 1; // Percental probability for garbage collector to
  52. // launch per each instance of FormValidator
  53. // class. Garbage collector is needed to remove
  54. // old user tag files from disk if you use
  55. // $no_session = true
  56. // 0 means GC will never launch
  57. // 100 means GC will launch everytime you
  58. // instantiate this class
  59. // !!! DO NOT CHANGE ANYTHING BELOW THIS LINE ===============================
  60. // }}}
  61. // {{{
  62. var $img_func_suffix = 'png';
  63. // }}}
  64. // {{{
  65. function SPAF_FormValidator () {
  66. // set properties that might have been accidentally removed
  67. if (!isset($this->session_var)) { $this->session_var = 'spaf_formvalidator'; }
  68. if (!isset($this->no_session)) { $this->no_session = false; }
  69. // below tasks are only required if $no_session is set to true
  70. if (isset($this->no_session) && $this->no_session) {
  71. // set class directory if none specified
  72. if ($this->work_dir == '') {
  73. $this->work_dir = dirname(__FILE__).'/';
  74. }
  75. // set a relative path
  76. elseif (substr($this->work_dir, 0, 1) != '/') {
  77. $this->work_dir = dirname(__FILE__).'/'.$this->work_dir;
  78. }
  79. // add backslash at the end of path if necessary
  80. if (substr($this->work_dir, -1) != '/') {
  81. $this->work_dir .= '/';
  82. }
  83. // launch garbage collector
  84. if (mt_rand(1, 100) < $this->gc_prob) {
  85. $this->launchGC();
  86. }
  87. }
  88. // tasks that are required for session enabled operation
  89. else {
  90. // check if session is started
  91. if (!isset($_SESSION)) {
  92. session_start();
  93. }
  94. }
  95. }
  96. // }}}
  97. // {{{
  98. function setLibDir ($dir) {
  99. $this->lib_dir = $dir;
  100. }
  101. // }}}
  102. // {{{
  103. function tagUser () {
  104. if ($this->no_session) {
  105. // generate validation word and secret identity cookie
  106. $tag = $this->getRandomString($this->char_num);
  107. $cookie = md5(microtime().$_SERVER['REMOTE_ADDR']);
  108. // set cookie
  109. setcookie($this->tag_cookie, $cookie, 0, '/');
  110. $_COOKIE[$this->tag_cookie] = $cookie;
  111. // write to a file
  112. $this->writeFile($this->work_dir.$cookie.'.'.$this->work_ext, $tag);
  113. }
  114. else {
  115. // set session variable
  116. // ATTENTION! Session must be already started with session_start()
  117. $_SESSION[$this->session_var] = $this->getRandomString($this->char_num);
  118. }
  119. return true;
  120. }
  121. // }}}
  122. // {{{
  123. function getUserTag () {
  124. // get current tag
  125. if ($this->no_session) {
  126. if (!isset($_COOKIE[$this->tag_cookie]) || isset($_GET['regen'])) {
  127. // user is not tagged - issue new tag
  128. $this->tagUser();
  129. }
  130. // get the work file
  131. if (!file_exists($this->work_dir.$_COOKIE[$this->tag_cookie].'.'.$this->work_ext)) {
  132. // file does not exist - reissue the tag once again to recreate the file
  133. $this->tagUser();
  134. }
  135. return @file_get_contents($this->work_dir.$_COOKIE[$this->tag_cookie].'.'.$this->work_ext);
  136. }
  137. else {
  138. if (!isset($_SESSION[$this->session_var]) || isset($_GET['regen'])) {
  139. // user is not tagged - issue new tag
  140. $this->tagUser();
  141. }
  142. return $_SESSION[$this->session_var];
  143. }
  144. }
  145. // }}}
  146. // {{{
  147. function validRequest ($req) {
  148. return strtolower($this->getUserTag()) == strtolower($req)
  149. ? true
  150. : false;
  151. }
  152. // }}}
  153. // {{{
  154. function getRandomString ($chars = 5) {
  155. $str = '';
  156. $cnt = sizeof($this->chars);
  157. for ($i = 0; $i < $chars; $i++) {
  158. $str .= $this->chars[mt_rand(0, $cnt-1)];
  159. }
  160. return $str;
  161. }
  162. // }}}
  163. // {{{
  164. function streamImage () {
  165. // select random background
  166. $background = $this->backgrounds[mt_rand(0, sizeof($this->backgrounds)-1)];
  167. // set proper image format according to selected background image
  168. $this->setImageFormat($background);
  169. // create image resource
  170. $function = "imagecreatefrom".$this->img_func_suffix;
  171. $image = $function($this->lib_dir.$background);
  172. // create color resources
  173. $colors = array();
  174. $color_count = sizeof($this->colors);
  175. for ($i = 0; $i < $color_count; $i++) {
  176. $colors[] = imagecolorallocate($image,
  177. $this->colors[$i][0],
  178. $this->colors[$i][1],
  179. $this->colors[$i][2]);
  180. }
  181. $shadow = imagecolorallocate($image,
  182. $this->shadow_color[0],
  183. $this->shadow_color[1],
  184. $this->shadow_color[2]);
  185. // get secret word from session
  186. $word = $this->getUserTag();
  187. // calculate geometrics
  188. $width = imagesx($image);
  189. $height = imagesy($image);
  190. $lenght = strlen($word);
  191. $step = floor(($width / $lenght) * 0.9);
  192. // put letters on background
  193. for ($i = 0; $i < $lenght; $i++) {
  194. // get current character
  195. $char = substr($word, $i, 1);
  196. // randomize letter display characteristics
  197. $font_size = $this->font_sizes[mt_rand(0, sizeof($this->font_sizes)-1)];
  198. $data = array(
  199. 'size' => $font_size,
  200. 'angle' => mt_rand(-20, 20),
  201. 'x' => $step * $i + 5,
  202. 'y' => mt_rand($font_size+5, $height-5 ),
  203. 'color' => $colors[mt_rand(0, $color_count-1)],
  204. 'font' => $this->lib_dir.$this->fonts[mt_rand(0, sizeof($this->fonts)-1)]
  205. );
  206. // put a shadow
  207. if (!isset($this->hide_shadow) || !$this->hide_shadow) {
  208. imagettftext($image,
  209. $font_size,
  210. $data['angle'],
  211. $data['x'] + 1,
  212. $data['y'] + 1,
  213. $shadow,
  214. $data['font'],
  215. $char);
  216. }
  217. // put a letter
  218. imagettftext($image,
  219. $font_size,
  220. $data['angle'],
  221. $data['x'],
  222. $data['y'],
  223. $data['color'],
  224. $data['font'],
  225. $char);
  226. }
  227. // stream image to browser
  228. $function = "image".$this->img_func_suffix;
  229. header('Content-Type: image/'.$this->img_func_suffix);
  230. $function($image);
  231. imagedestroy($image);
  232. return true;
  233. }
  234. // }}}
  235. // {{{
  236. function setImageFormat ($file) {
  237. // get extention
  238. $arr = explode('.', $file);
  239. $ext = strtolower($arr[sizeof($arr) - 1]);
  240. // set appropriate formats
  241. switch ($ext) {
  242. case 'gif':
  243. case 'png':
  244. case 'jpeg':
  245. $this->img_func_suffix = $ext;
  246. break;
  247. case 'jpg':
  248. $this->img_func_suffix = 'jpeg';
  249. break;
  250. default:
  251. // critical error - unsupported format
  252. die('ERROR: Unsupported format!');
  253. break;
  254. }
  255. }
  256. // }}}
  257. // {{{
  258. function destroy () {
  259. if ($this->no_session) {
  260. // remove physical file and cookie
  261. @unlink($this->work_dir.$_COOKIE[$this->tag_cookie].'.'.$this->work_ext);
  262. unset($_COOKIE[$this->tag_cookie]);
  263. setcookie($this->tag_cookie, '', 0, '/');
  264. }
  265. else {
  266. // remove session variable
  267. unset($_SESSION[$this->session_var]);
  268. }
  269. return true;
  270. }
  271. // }}}
  272. // {{{
  273. function launchGC () {
  274. // open work directory
  275. if ($dir = @opendir($this->work_dir)) {
  276. // check each file
  277. while (false !== ($file = @readdir($dir))) {
  278. $fdata = pathinfo($file);
  279. if (
  280. $fdata['extension'] == $this->work_ext
  281. &&
  282. (filemtime($this->work_dir.$file) < (time() - ($this->tag_ttl * 60)))
  283. )
  284. {
  285. // remove expired file
  286. @unlink($this->work_dir.$file);
  287. }
  288. }
  289. @closedir($dir);
  290. }
  291. return true;
  292. }
  293. // }}}
  294. // {{{
  295. function writeFile ($file, $content) {
  296. $fl = @fopen($file, 'w');
  297. $ret = @fwrite($fl, $content);
  298. @fclose($fl);
  299. return $ret;
  300. }
  301. // }}}
  302. }
  303. ?>