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