session_var)) { $this->session_var = 'spaf_formvalidator'; } if (!isset($this->no_session)) { $this->no_session = false; } // below tasks are only required if $no_session is set to true if (isset($this->no_session) && $this->no_session) { // set class directory if none specified if ($this->work_dir == '') { $this->work_dir = dirname(__FILE__).'/'; } // set a relative path elseif (substr($this->work_dir, 0, 1) != '/') { $this->work_dir = dirname(__FILE__).'/'.$this->work_dir; } // add backslash at the end of path if necessary if (substr($this->work_dir, -1) != '/') { $this->work_dir .= '/'; } // launch garbage collector if (mt_rand(1, 100) < $this->gc_prob) { $this->launchGC(); } } // tasks that are required for session enabled operation else { // check if session is started if (!isset($_SESSION)) { session_start(); } } } // }}} // {{{ function setLibDir ($dir) { $this->lib_dir = $dir; } // }}} // {{{ function tagUser () { if ($this->no_session) { // generate validation word and secret identity cookie $tag = $this->getRandomString($this->char_num); $cookie = md5(microtime().$_SERVER['REMOTE_ADDR']); // set cookie setcookie($this->tag_cookie, $cookie, 0, '/'); $_COOKIE[$this->tag_cookie] = $cookie; // write to a file $this->writeFile($this->work_dir.$cookie.'.'.$this->work_ext, $tag); } else { // set session variable // ATTENTION! Session must be already started with session_start() $_SESSION[$this->session_var] = $this->getRandomString($this->char_num); } return true; } // }}} // {{{ function getUserTag () { // get current tag if ($this->no_session) { if (!isset($_COOKIE[$this->tag_cookie]) || isset($_GET['regen'])) { // user is not tagged - issue new tag $this->tagUser(); } // get the work file if (!file_exists($this->work_dir.$_COOKIE[$this->tag_cookie].'.'.$this->work_ext)) { // file does not exist - reissue the tag once again to recreate the file $this->tagUser(); } return @file_get_contents($this->work_dir.$_COOKIE[$this->tag_cookie].'.'.$this->work_ext); } else { if (!isset($_SESSION[$this->session_var]) || isset($_GET['regen'])) { // user is not tagged - issue new tag $this->tagUser(); } return $_SESSION[$this->session_var]; } } // }}} // {{{ function validRequest ($req) { return strtolower($this->getUserTag()) == strtolower($req) ? true : false; } // }}} // {{{ function getRandomString ($chars = 5) { $str = ''; $cnt = sizeof($this->chars); for ($i = 0; $i < $chars; $i++) { $str .= $this->chars[mt_rand(0, $cnt-1)]; } return $str; } // }}} // {{{ function streamImage () { // select random background $background = $this->backgrounds[mt_rand(0, sizeof($this->backgrounds)-1)]; // set proper image format according to selected background image $this->setImageFormat($background); // create image resource $function = "imagecreatefrom".$this->img_func_suffix; $image = $function($this->lib_dir.$background); // create color resources $colors = array(); $color_count = sizeof($this->colors); for ($i = 0; $i < $color_count; $i++) { $colors[] = imagecolorallocate($image, $this->colors[$i][0], $this->colors[$i][1], $this->colors[$i][2]); } $shadow = imagecolorallocate($image, $this->shadow_color[0], $this->shadow_color[1], $this->shadow_color[2]); // get secret word from session $word = $this->getUserTag(); // calculate geometrics $width = imagesx($image); $height = imagesy($image); $lenght = strlen($word); $step = floor(($width / $lenght) * 0.9); // put letters on background for ($i = 0; $i < $lenght; $i++) { // get current character $char = substr($word, $i, 1); // randomize letter display characteristics $font_size = $this->font_sizes[mt_rand(0, sizeof($this->font_sizes)-1)]; $data = array( 'size' => $font_size, 'angle' => mt_rand(-20, 20), 'x' => $step * $i + 5, 'y' => mt_rand($font_size+5, $height-5 ), 'color' => $colors[mt_rand(0, $color_count-1)], 'font' => $this->lib_dir.$this->fonts[mt_rand(0, sizeof($this->fonts)-1)] ); // put a shadow if (!isset($this->hide_shadow) || !$this->hide_shadow) { imagettftext($image, $font_size, $data['angle'], $data['x'] + 1, $data['y'] + 1, $shadow, $data['font'], $char); } // put a letter imagettftext($image, $font_size, $data['angle'], $data['x'], $data['y'], $data['color'], $data['font'], $char); } // stream image to browser $function = "image".$this->img_func_suffix; header('Content-Type: image/'.$this->img_func_suffix); $function($image); imagedestroy($image); return true; } // }}} // {{{ function setImageFormat ($file) { // get extention $arr = explode('.', $file); $ext = strtolower($arr[sizeof($arr) - 1]); // set appropriate formats switch ($ext) { case 'gif': case 'png': case 'jpeg': $this->img_func_suffix = $ext; break; case 'jpg': $this->img_func_suffix = 'jpeg'; break; default: // critical error - unsupported format die('ERROR: Unsupported format!'); break; } } // }}} // {{{ function destroy () { if ($this->no_session) { // remove physical file and cookie @unlink($this->work_dir.$_COOKIE[$this->tag_cookie].'.'.$this->work_ext); unset($_COOKIE[$this->tag_cookie]); setcookie($this->tag_cookie, '', 0, '/'); } else { // remove session variable unset($_SESSION[$this->session_var]); } return true; } // }}} // {{{ function launchGC () { // open work directory if ($dir = @opendir($this->work_dir)) { // check each file while (false !== ($file = @readdir($dir))) { $fdata = pathinfo($file); if ( $fdata['extension'] == $this->work_ext && (filemtime($this->work_dir.$file) < (time() - ($this->tag_ttl * 60))) ) { // remove expired file @unlink($this->work_dir.$file); } } @closedir($dir); } return true; } // }}} // {{{ function writeFile ($file, $content) { $fl = @fopen($file, 'w'); $ret = @fwrite($fl, $content); @fclose($fl); return $ret; } // }}} } ?>