'Antispam', 'description' => 'Otočte obrázok do vodorovnej polohy.', 'submit' => 'Odoslať', 'cancel' => 'Zrušiť' ); // informacie o obrazku protected $imageInfo = array( 'width' => null, 'height' => null, 'type' => null, 'mime' => null ); // konfiguracia vzhladu protected $design = array( // Left Button 'leftButtonColor' => '#000', 'leftButtonColorHover' => '#fc0', 'leftButtonBorderColor' => '#ccc', 'leftButtonImage' => '', 'leftButtonImageHover' => '', // Right Button 'rightButtonColor' => '#000', 'rightButtonColorHover' => '#fc0', 'rightButtonBorderColor' => '#ccc', 'rightButtonImage' => '', 'rightButtonImageHover' => '', ); // ====[ Konstruktor ]===================================================== /** * konstruktor, nastavenie behu RotateAntispam * * @param array $opts - nastavenie jednotlivych hodnot pre beh RotatoAntispam * @param array $opts['debug'] - nastavenie DEBUG modu * @param array $opts['image'] - nastavenie defaultneho obrazka * @return void */ public function __construct($opts = array()) { $this->addLog('Spusteny konstruktor'); if (isset($opts['debug']) && is_bool($opts['debug'])) { $this->debug = $opts['debug']; $this->addLog('Debug nastaveny na hodnotu ' . (($this->debug) ? 'TRUE' : 'FALSE')); } else { $this->debug = false; $this->addLog('Debug bol nastaveny na default hodnotu FALSE'); } if (isset($opts['image']) && is_string($opts['image'])) { if ($this->setImage($opts['image'])) { $this->addLog('Obrazok pre rotovanie bol nastaveny'); } else { $this->setError('Chyba pri nastavovani obrazku'); } } else { $this->setError('Nebol nastaveny obrazok pre rotovanie'); } if (isset($opts['answer_name']) && is_string($opts['answer_name'])) { $this->answer_name = $opts['answer_name']; } else { $this->answer_name = 'answer_name_'.Time(); } $this->addLog('Nazov polozky odpovede bol nastaveny na hodnotu "'.$this->answer_name.'"'); if (isset($opts['submit_name']) && is_string($opts['submit_name'])) { $this->submit_name = $opts['submit_name']; $this->addLog('Nazov tlacitka pre odosielanie formulara bol nastaveny'); } else { $this->addLog('Nazov tlacitka pre odosielanie formulara NEBOL nastaveny'); } if (isset($opts['url']) && is_string($opts['url'])) { if ($this->setUrlRotateAntispam($opts['url'])) { $this->addLog('URL pre RotateAntispam bola nastavena'); } else { $this->addLog('URL pre RotateAntispam NEBOLA nastavena'); } } else { $this->setError('URL pre RotateAntispam NEBOLA zadana'); } if (isset($opts['encoding']) && is_string($opts['encoding'])) { if ($this->setOutputEncoding($opts['encoding'])) { $this->addLog('Vystupne kodovanie bolo nastavene na hodnotu '.$this->outputEncoding); } else { $this->addLog('Vystupne kodovanie NEBOLO nastavene na hodnotu '.$opts['encoding']); } } } // ====[ Metody pre zaznamenanie chyb a logov ]============================ /** * nastavi chybovu spravu, ktoru je v mozne vyziadat pomoocu getErrorMessage() * * @param string $msg - chybova sprava */ protected function setErrorMessage($msg) { $msg = 'Error: "'.$msg.'"'; $this->addLog($msg); $this->errorMessage = $msg; } /** * nastavi chybovu spravu a priznak chyby * * @param string $msg - chybova sprava */ protected function setError($msg) { $this->setErrorMessage($msg); $this->isError = true; if (!$this->debug) { $message = 'Error: Nastala chyba pri behu RotateAntispam. Nasleduju zaznamenavane poznamky pocas behu objektu RotateAntispam:'."\n" .'Umiestnenie RotateAntispam: '.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."\n" ."\n" .str_replace("
", "\n", $this->getLog()) ."\n" ."Koniec spravy."; die($msg); } } /** * vrati ci nastala chyba * * @return bool - ak nastala chyba vrati TRUE, inak FALSE */ public function isError() { return $this->isError; } /** * varati poslednu chybovu spravu * * @return string - posledna chybova sprava */ public function getErrorMessage() { return $this->errorMessage; } /** * prida poznamku pre neskorsi debug * * @param string $msg - poznamka */ protected function addLog($msg) { if ($this->debug) { $init_log = '--[ RotateAntispam ]----------------------------------------------------'; if (strpos($this->getLog(), $init_log) === false) { echo ''; $this->log .= $init_log.'
'; } $msg = str_replace("\n", '', $msg); $msg = str_replace("\r", '', $msg); echo ''; } $this->log .= $msg.'
'; } /** * vrati vsetky poznamku * * @return string - poznamky, ktore boli zaznamenane pocas behu */ public function getLog() { return $this->log; } // ====[ Sukromne metody ]================================================= /** * stavi URL adresu triedy * * @param $url - URL adresa k triede RotateAntispam * @return bool - uspesnost nastavenia */ protected function setUrlRotateAntispam($url) { // TODO: spravit nejake osetrenie URL $this->urlRotateAntispam = $url; return true; } /** * vrati URL adresu triedy * * @return string - URL adresa triedy */ protected function getUrlRotateAntispam() { return $this->urlRotateAntispam; } /** * vrati ziadany text * * @param string $key - kluc retazca * @return string - hodnota prisluchajuca klucu */ protected function getText($key) { $ret = $this->texts[$key]; return ($this->outputEncoding == 'UTF-8') ? $ret : iconv('UTF-8', $this->outputEncoding, $ret); } /** * vrati dizajn jedneho elementu * * @param string $key - kluc elementu * @return string - hodnota ziadaneho elementu */ protected function getDesignElement($key) { if (strlen($key) <= 0 || !isset($this->design[$key])) { return false; } return $this->design[$key]; } /** * vrati informacie o obrazku * * @param string $image - subor obrazka * @return array - pole s informacia o obrazku */ protected function getImageInfo($image) { if (strlen($image) <= 0) { return false; } $img = getimagesize($image); return array( 'width' => $img[0], 'height' => $img[1], 'type' => $img[2], 'mime' => $img['mime'] ); } /** * nastavi typ vystupneho kodovania * * @param string $encoding - nazov kodovania * @return bool - uspech nastavenia */ protected function setOutputEncoding($encoding) { $allowEncoding = array('UTF-8', 'CP1250', 'ISO-8859-1', 'ISO-8859-2', 'WINDOWS-1250'); $encoding = strtoupper($encoding); if (in_array($encoding, $allowEncoding)) { $this->outputEncoding = $encoding.'//TRANSLIT'; return true; } return false; } // ====[ Verejne metody ]================================================== /** * nastavi obrazok pre rotovanie * * @param string $image - subor s obrazkom * @return bool - uspesnot nastavenia */ public function setImage($image) { $this->addLog('Spusteny setImage'); $this->addLog('Vypis $_SESSION: '.json_encode($_SESSION)); if (!isset($image) || !is_string($image) || !(strlen($image) > 0)) { $this->setError('Obrazok \''.$image.'\' nebol nastaveny'); return false; } if (!file_exists($image)) { $this->setError('Zadany obrazok \''.$image.'\' neexistuje'); return false; } $this->image = $image; $relative_path = str_replace(dirname(__FILE__), '', realpath($image)); if (substr($relative_path, 0, 1) == '/' || substr($relative_path, 0, 1) == '\\') { $relative_path = substr($relative_path, 1); } $_SESSION['RotateAntispam']['image'] = $relative_path; $this->imageInfo = $this->getImageInfo($this->image); $this->addLog('Obrazok "'.$image.'" bol nastaveny'); return true; } /** * vrati HTML antispam ochrany * * @return string - HTML kod pre antispam ochrany */ public function getAntispamHtml() { $this->addLog('Spusteny getAntispamHtml'); $shift = rand(-10, 10); $_SESSION['RotateAntispam']['shift'] = $shift; $this->addLog('Shift nastaveny na hodnotu "'.$shift.'"'); $html = '
'.$this->getText('title').'
'; $leftButtonImage = (strlen($this->getDesignElement('leftButtonImage')) > 0 && strlen($this->getDesignElement('leftButtonImageHover')) > 0); if ($leftButtonImage) { $html .= ''; $html .= ''; $leftButtonImageInfo = $this->getImageInfo($this->getDesignElement('leftButtonImage')); } $rightButtonImage = (strlen($this->getDesignElement('rightButtonImage')) > 0 && strlen($this->getDesignElement('rightButtonImageHover')) > 0); if ($rightButtonImage) { $html .= ''; $html .= ''; $rightButtonImageInfo = $this->getImageInfo($this->getDesignElement('rightButtonImage')); } $submitType = (strlen($this->getText('submit')) <= 0) ? 'image' : 'submit'; $cancelType = (strlen($this->getText('cancel')) <= 0) ? 'image' : 'button'; $html .= '
'.$this->getText('description').'
'; return $html; } /** * nastavi ziadany text * * @param string $key - kluc retazca * @param string $value - hodnota prisluchajuca klucu * @return void */ public function setText($key, $value) { $this->texts[$key] = $value; } /** * nastavi dizajn * * @param string $arr - asociativne pole prvkov * @return void */ public function setDesign($arr) { if (!is_array($arr)) { return false; } foreach($arr as $key => $val) { $this->setDesignElement($key, $val); } } /** * nastavi dizajn jedneho elementu * * @param string $key - kluc elementu * @param string $val - hodnota elementu * @return void */ public function setDesignElement($key, $val) { if (strlen($key) <= 0) { return false; } $this->design[$key] = $val; } /** * skontroluje ovdpoved * * @param $answer - odpoved, ak nie je nastavena default to berie z $_POST * @return bool - spravnost odpovede, TRUE = spravna, FALSE = nespravna */ public function correctAnswer($answer_user = null) { $this->addLog('Spusta sa correctAnswer'); if (is_null($this->correctAnswer_cache)) { $this->addLog('Prvy krat spracuvavam'); $answer_user = (is_null($answer_user)) ? @$_POST[$this->answer_name] : $answer_user; $answer = @$_SESSION['RotateAntispam']['answer']; $this->addLog('Odpoved pouzivatela je "'.$answer_user.'"'); if (!isset($answer) || strlen($answer) <= 0) { $this->addLog('Odpoved nebola nastavena'); $this->correctAnswer_cache = false; } else { $this->addLog('Spravna odpoved je "'.$answer.'"'); $this->correctAnswer_cache = ($answer == $answer_user); } $_SESSION['RotateAntispam'] = null; unset($_SESSION['RotateAntispam']); } $this->addLog('Vraciam odpoved '.(($this->correctAnswer_cache) ? 'TRUE' : 'FALSE')); $this->addLog('
JavaScript LOG:
'.nl2br(@$_REQUEST['antispam_log'])); return $this->correctAnswer_cache; } // ====[ Funkcie - vseobecne ]============================================= } // class RotateAntispam error_reporting(E_ALL && ~E_NOTICE); // ak nie je nastartovana $_SESSION if (!isset($_SESSION) || strlen(session_id()) == 0) { session_start(); } function loadFromSession($name) { $ret = null; $maxSleep = 10; $sleep = 1; while (strlen($ret) <= 0 && $maxSleep > 0) { $ret = @$_SESSION['RotateAntispam'][$name]; sleep($sleep); // prve nacitanie = 0 $sleep = 1; $maxSleep--; } return $ret; } // Spracovanie vyziadaneho obrazku if (isset($_REQUEST['getImage'])) { $filename = loadFromSession('image'); if (strlen($filename) <= 0 || !file_exists($filename)) { die('Neexistuje obrazok "'.$filename.'"'); } $shift = loadFromSession('shift'); do { $degrees = (rand(-5,5) * 10) + $shift; } while($degrees == $shift || $_SESSION['RotateAntispam']['answer'] == $degrees); $_SESSION['RotateAntispam']['answer'] = (string)$degrees; if(!function_exists("imagerotate")) { function imagerotate($srcImg, $angle, $bgcolor=0, $ignore_transparent = 0) { return imagerotateEquivalent($srcImg, $angle, $bgcolor, $ignore_transparent); } } /* Imagerotate replacement. ignore_transparent is work for png images Also, have some standard functions for 90, 180 and 270 degrees. Rotation is clockwise */ function imagerotateEquivalent($srcImg, $angle, $bgcolor, $ignore_transparent = 0) { function rotateX($x, $y, $theta){ return $x * cos($theta) - $y * sin($theta); } function rotateY($x, $y, $theta){ return $x * sin($theta) + $y * cos($theta); } $srcw = imagesx($srcImg); $srch = imagesy($srcImg); //Normalize angle $angle %= 360; //Set rotate to clockwise //$angle = -$angle; if($angle == 0) { if ($ignore_transparent == 0) { imagesavealpha($srcImg, true); } return $srcImg; } // Convert the angle to radians $theta = deg2rad ($angle); //Standart case of rotate if ( (abs($angle) == 90) || (abs($angle) == 270) ) { $width = $srch; $height = $srcw; if ( ($angle == 90) || ($angle == -270) ) { $minX = 0; $maxX = $width; $minY = -$height+1; $maxY = 1; } else if ( ($angle == -90) || ($angle == 270) ) { $minX = -$width+1; $maxX = 1; $minY = 0; $maxY = $height; } } else if (abs($angle) === 180) { $width = $srcw; $height = $srch; $minX = -$width+1; $maxX = 1; $minY = -$height+1; $maxY = 1; } else { // Calculate the width of the destination image. $temp = array (rotateX(0, 0, 0-$theta), rotateX($srcw, 0, 0-$theta), rotateX(0, $srch, 0-$theta), rotateX($srcw, $srch, 0-$theta) ); $minX = floor(min($temp)); $maxX = ceil(max($temp)); $width = $maxX - $minX; // Calculate the height of the destination image. $temp = array (rotateY(0, 0, 0-$theta), rotateY($srcw, 0, 0-$theta), rotateY(0, $srch, 0-$theta), rotateY($srcw, $srch, 0-$theta) ); $minY = floor(min($temp)); $maxY = ceil(max($temp)); $height = $maxY - $minY; } $destimg = imagecreatetruecolor($width, $height); if ($ignore_transparent == 0) { imagefill($destimg, 0, 0, imagecolorallocatealpha($destimg, 255,255, 255, 127)); imagesavealpha($destimg, true); } // sets all pixels in the new image for($x=$minX; $x<$maxX; $x++) { for($y=$minY; $y<$maxY; $y++) { // fetch corresponding pixel from the source image $srcX = round(rotateX($x, $y, $theta)); $srcY = round(rotateY($x, $y, $theta)); if($srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY < $srch) { $color = imagecolorat($srcImg, $srcX, $srcY ); } else { $color = $bgcolor; } imagesetpixel($destimg, $x-$minX, $y-$minY, $color); } } return $destimg; } header('Content-type: image/png'); $source = imagecreatefrompng($filename); $color = imagecolorallocate($source, 204, 204, 204); $rotate = imagerotate($source, $degrees, $color); $img = getimagesize($filename); $image_info['width'] = $img[0]; $image_info['height'] = $img[1]; $image_info['type'] = $img[2]; $image_info['mime'] = $img['mime']; $rotate_width = imagesx($rotate); $rotate_height = imagesy($rotate); $trim_image = imagecreatetruecolor($image_info['width'], $image_info['height']); imagecopyresampled($trim_image, $rotate, 0, 0, ($rotate_width - $image_info['width']) / 2, ($rotate_height - $image_info['height']) / 2, $image_info['width'], $image_info['height'], $image_info['width'], $image_info['height'] ); imagecolortransparent($trim_image, $color); imagepng($trim_image); } ?>