$val) { $code=self::prepare(self::expost($key)); if (($val[0]==$code || $val[1]==$code) && $code!='') { $check=true; unset($_SESSION['divtcha'][$key]); $_SESSION['divtchaPokusu']=0; } if ($val=='off') { $off=true; } } if ($off) {$check=2;} return $check; } else { $_SESSION['divtcha']=array(); return false; } } else { $_SESSION['divtcha']=array(); return false; } } /** * divtchaS::hex2bin() * * @param string $hexdata * @return string * * Slouží k převodu hexadecimálního kódu na binární data */ static function hex2bin($hexdata) { for ($i=0;$i0) { $this->setFont($images); $images="MASK"; } $this->setImageFolder($images); $this->setBackgroundColor($bcolor); $this->setColor($color); $this->setFontSize($fontsize); $this->setAlign($align); if (!isset($_SESSION['divtcha'])) {$_SESSION['divtcha']=array();} //$sc=uniqid('DIVTCHA'); //$ac=uniqid('DIVTCHA'); //$_SESSION['divtcha'][$sc]=''; //$this->id=$sc; //$this->aid=$ac; $this->set($count,$ncount,$bw,$security); } /** * divtcha::set() * * nastaví DIVTCHA a připraví vše potřebné. * * @param integer $count * @param integer $ncount * @param bool $bw * @param integer $security * @return void */ private function set($count,$ncount=0,$bw=false,$security=0) { $this->security=$security; $this->count=$count; $this->ncount=$ncount; if ($this->security>3) { $this->elements=$elements=array("span","em","strong","sub","sup","big","del"); } else { $this->elements=$elements=array("div"); } $this->setRandomParts(); $this->bw=$bw; $text=$this->getNewCode($this->count,$this->ncount); $this->codesource=$text; $this->codeText=$text; $_SESSION['divtcha'][$this->random['input']][1]=''; if ($this->security>4) { $this->fakeCodeText=$this->getNewCode($this->count,$this->ncount);; } } /** * divtcha::setAlternativeQuestions() * * Načte ze zadaného souboru databázi logických otázek. * Syntaxe je typu CSV, to znamená jedna otázka na řádek a otázka s odpovědí oddělené středníky * např. * Kolik hodin má den;24; * * @param string $soubor * @return void */ public function setAlternativeQuestions($soubor) { if (file_exists($soubor)) { $ex=array(); $data=file_get_contents($soubor); $data=explode(";\r\n",$data); foreach($data as $key=>$val) { $parts=explode(';',$val); $ex[count($ex)]['q']=$parts[0]; $ex[(count($ex)-1)]['a']=$parts[1]; } $this->alternativeDatabase=$ex; } else { echo "SOUBOR S ALTERNATIVNÍMI OTÁZKAMI NEEXISTUJE"; } } /** * divtcha::setImageFolder() * * nastavuje cestu k obrázkům písmen * * @param string $folder * @return void */ private function setImageFolder($folder) { $this->imageFolder=$folder; } /** * divtcha::setBackgroundColor() * * Nastaví barvu pozadí (zadávat v hexa) * * @param string $color * @return void */ public function setBackgroundColor($color) { $this->bg=$color; } /** * divtcha::setColor() * * Nastaví barvu textu (zadávat v hexa) * * @param string $color * @return void */ public function setColor($color) { $this->color=$color; } /** * divtcha::setAlign() * * Nastaví zarovnání boxu s kódem (left,right,none) * * @param string $align * @return void */ public function setAlign($align) { $this->align=$align; } /** * divtcha::setRandomParts() * * nastaví názvy tříd a id elementů v závislosti na úrovni zabezpečení * * @return void */ public function setRandomParts() { if ($this->security>0) { $this->random['boxName']=$this->getNewCode(2).'_'.$this->getNewCode(4); $this->random['input']=$this->getNewCode(2).'_'.$this->getNewCode(4); $this->random['handicap']=$this->getNewCode(3).'_'.$this->getNewCode(4); $this->random['handicapId']=$this->getNewCode(3).'_'.$this->getNewCode(4); $this->random['handicapIdBox']=$this->getNewCode(3).'_'.$this->getNewCode(4); $this->random['playFunction']=$this->getNewCode(3).'_'.$this->getNewCode(4); } else { $this->random['boxName']='box'; $this->random['input']='text'; $this->random['handicap']='handicap'; $this->random['handicapId']='handicapPlay'; $this->random['handicapIdBox']='handicapPlayBox'; $this->random['playFunction']='playSound'; } if ($this->security>2) { $this->random['xName']=$this->getNewCode(rand(1,2)); $this->random['yName']=$this->getNewCode(rand(1,2)); } else { $this->random['xName']='x'; $this->random['yName']='y'; } $this->random['blind']=$this->getNewCode(2).'_'.$this->getNewCode(4); } /** * divtcha::setLimit() * * Metoda umožňující nastavení blokování po určeném počtu pokusů ($try) na určený počet sekund ($block) * * @param integer $try * @param integer $block * @return void */ public function setLimit($try,$block) { $this->limit=true; $this->try=$try; $this->block=$block; if (!isset($_SESSION['divtchaPokusu'])) {$_SESSION['divtchaPokusu']=0;} if (!isset($_SESSION['divtchaBlocking'])) {$_SESSION['divtchaBlocking']=false;} if ($_SESSION['divtchaPokusu']>=$try) { $_SESSION['divtchaBlocking']=true; if (!isset($_SESSION['divtchaBlockingTime'])) {$_SESSION['divtchaBlockingTime']=time();} if (($_SESSION['divtchaBlockingTime']+$block)blocking=true; } } } /** * divtcha::getSecurity() * * Vrátí nastavení úrovně zabezpečení * * @return integer */ public function getSecurity() { return $this->security; } /** * divtcha::setFont() * * nastavuje font * * @param string $font * @return void */ private function setFont($font) { $this->font=$font; } /** * divtcha::setFontSize() * * nastavuje velikost fontu v případě použití kódu v textu * * @param integer $size * @return void */ private function setFontSize($size) { $this->fontSize=$size; } /** * divtcha::generate() * * Má za úkol vygenerovat potřebné kódy * * @return void */ private function generate() { if (!$this->output) { if ($this->blocking) { $this->codeText='VYPNUTO'; $this->codesource="off"; } $data=$this->text2div($this->codeText,$this->bg,$this->color); $this->code=$data['content']; $this->style=$data['style']; $this->output=true; } } /** * divtcha::generateAlternativeQuestion() * * Vygeneruje alternativní otázku * Z důvodu umožnění nasadit DIVTCHA bez nutnosti psát databázi otázek jsou čísla a doplňková slova (ovoce) napsaná přimo zde * * @return void */ private function generateAlternativeQuestion() { if (rand(0,1)==0 || count($this->alternativeDatabase)==0) { $mashups=array('jablko','hruška','banán','švestka','jablek','hrušek','banánů','švestek','jablka','hrušky','banány','švestky'); $numbers[0]=array('nula','jedna','dva','tři','čtyři','pět','šest','sedm','osm','devět'); $numbers[1]=array('nula','jednička','dvojka','trojka','čtyřka','pětka','šestka','sedmička','osmička','devítka'); $numbers[2]=array('nula','jeden','dva','tři','čtvery','patery','šestero','sedmero','osm','devatero'); $firstn=rand(0,(count($numbers)-1)); $secondn=rand(0,(count($numbers)-1)); $first=rand(0,9); $second=rand(0,9); $vy=$numbers[$firstn][$first]." ".$mashups[rand(0,(count($mashups)-1))]." plus ".$numbers[$secondn][$second]." ".$mashups[rand(0,(count($mashups)-1))]; $this->alternativeQuestion="Kolik je ".$vy."? (pouze číslo)"; $this->alternativeAnswer=$first+$second; } else { $qa['q']=''; while ($qa['q']=='') { $qa=$this->alternativeDatabase[rand(0,(count($this->alternativeDatabase)))]; $this->alternativeQuestion=$qa['q']; $this->alternativeAnswer=$qa['a']; } } } /** * divtcha::rgb2hex() * * Převede RGB barvu na hexadecimální reprezentaci * * @param integer $r * @param integer $g * @param integer $b * @return string */ private function rgb2hex($r,$g,$b) { return bin2hex(chr($r)).bin2hex(chr($g)).bin2hex(chr($b)); } /** * divtcha::getNewCode() * * Vytvoří kód jedinečný pro relaci. * Řidí se nastavením, které říká počet písmen a čísel v kódu. Čísla budou vždy za písmeny a písmena budou příjemně čitelná * * @param integer $count * @param integer $ncount * @return string */ private function getNewCode($count,$ncount=0) { $souhlasky=array('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','x','z'); $samohlasky=array('a','e','i','u'); $generated=false; while($generated==false || in_array($text,divtchaS::$codes)) { $text=''; for($i=0; $i<$count; $i++) { if($i%2==0) {$text.=$samohlasky[rand(0,count($samohlasky)-1)];} else{$text.=$souhlasky[rand(0,count($souhlasky)-1)];} } for($i=0; $i<$ncount; $i++) { $text.=rand(1,9); } $generated=true; } divtchaS::$codes[]=$text; return $text; } /** * divtcha::char2div() * * Vytvoří elementy DIV, které budou tvořit zadané písmeno * * @param char $char * @param integer $left * @param string $bg * @param string $pbg * @return */ private function char2div($char,$left,$bg,$pbg) { $cesta=$this->getImageFolder().$char.'.jpg'; if (file_exists($cesta)) { $velikost = getimagesize($cesta); $obrazek = imagecreatefromjpeg($cesta); $ret = ""; for ($x = 0; $x < $velikost[0]; $x++) { for ($y = 0; $y < $velikost[1]; $y++) { if ($x < $velikost[0] && $y < $velikost[1]) { $pixel = imagecolorat($obrazek,$x,$y); $cols = imagecolorsforindex($obrazek,$pixel); $r = $cols['red']; $g = $cols['green']; $b = $cols['blue']; if ($this->bw) { $gray=($r+$g+$b)/3; if ($gray<=180) {$r=0;$g=0;$b=0;} else {$r=255;$g=255;$b=255;} } $color=$this->rgb2hex($r,$g,$b); $bgc=" style=\"background-color:#".$color."\""; if ($color!=$bg) { if ($color!=$pbg) { $style=$bgc; } else { $style=''; } $element=$this->elements[rand(0,count($this->elements)-1)]; if ($this->security>1) { $xw=bin2hex($x+$left); $yw=bin2hex($y); } else { $xw=($x+$left); $yw=$y; } $ret.="<$element class=\"".$this->random['xName']."".$xw." ".$this->random['yName']."".$yw."\"$style>"; } } } } ImageDestroy($obrazek); $retx[0]=$ret; $retx[1]=$velikost[0]; $retx[2]=$velikost[1]; return $retx; } else { return ''; } } /** * divtcha::directText2div() * * Převede text do elementů DIV, které je budou tvořit a vizuálně budou zapadat do okolního textu * * @param string $text * @param string $bg * @param string $pbg * @return string */ private function directText2div($text,$bg,$pbg) { $velikostPisma=$this->fontSize; $metriky_koment=ImageTTFBBox($velikostPisma, 0, $this->font,$text); $metriky_first=ImageTTFBBox($velikostPisma, 0, $this->font,substr($text,$i,1)); $velikost[0]=$metriky_koment[2]+2; $velikost[1]=0; for($i=0; $ifont,substr($text,$i,1)); if ($metriky_koment[3]>$velikost[1]) {$velikost[1]=$metriky_koment[3];} } $velikost[1]+=$velikostPisma; $obrazek = imagecreatetruecolor($velikost[0],$velikost[1]); $user=ImageColorAllocate($obrazek, hexdec(substr($this->color,0,2)), hexdec(substr($this->color,2,2)), hexdec(substr($this->color,4,2))); $bila=ImageColorAllocate($obrazek, 255, 255, 255); $userbg=ImageColorAllocate($obrazek, hexdec(substr($this->bg,0,2)), hexdec(substr($this->bg,2,2)), hexdec(substr($this->bg,4,2))); ImageFilledRectangle($obrazek, 0, 0, $velikost[0],$velikost[1], $userbg); ImageTTFText($obrazek, $velikostPisma, 0, 0, $metriky_first[3]+$velikostPisma+1, $user, $this->font, $text); $ret = ""; for ($x = 0; $x < $velikost[0]; $x++) { for ($y = 0; $y < $velikost[1]; $y++) { if ($x < $velikost[0] && $y < $velikost[1]) { $pixel = imagecolorat($obrazek,$x,$y); $cols = imagecolorsforindex($obrazek,$pixel); $r = $cols['red']; $g = $cols['green']; $b = $cols['blue']; if ($this->bw) { $gray=($r+$g+$b)/3; if ($gray<=180) {$r=0;$g=0;$b=0;} else {$r=255;$g=255;$b=255;} } $color=$this->rgb2hex($r,$g,$b); $bgc=" style=\"background-color:#".$color."\""; if ($color!=$bg) { if ($color!=$pbg) { $style=$bgc; } else { $style=''; } $element=$this->elements[rand(0,count($this->elements)-1)]; if ($this->security>1) { $xw=bin2hex($x+$left); $yw=bin2hex($y); } else { $xw=($x+$left); $yw=$y; } $ret.="<$element class=\"".$this->random['xName']."".$xw." ".$this->random['yName']."".$yw."\"$style>"; } } } } ImageDestroy($obrazek); $retx[0]=$ret; $retx[1]=$velikost[0]; $retx[2]=$velikost[1]; return $retx; } /** * divtcha::text2div() * * Převede text na elementz DIV, které ho budou tvořit. Využívá veškeré dostupné nastavení * * @param string $text * @param string $bg * @param string $pbg * @return string */ private function text2div($text,$bg,$pbg) { $text=strtolower($text); $ret=''; $left=0; $maxheight=0; $this->generateAlternativeQuestion(); $ddivtcha=rand(0,(strlen($text)-1)); if ($this->getImageFolder()!='MASK/') { for($i=0; $ichar2div(substr($text,$i,1),$left,$bg,$pbg); $height=$data[2]; if ($maxheight<$height) {$maxheight=$height;} $left+=$data[1]; $ret.=$data[0]; } } else { $data=$this->directText2div($text,$bg,$pbg); $height=$data[2]; if ($maxheight<$height) {$maxheight=$height;} $left+=$data[1]; $ret.=$data[0]; } $classes=""; for($i=0; $i<$left; $i++) { if ($this->security>1){$x=bin2hex($i);}else{$x=$i;} $classes.=".".$this->random['xName']."".$x."{left:".$i."px;}"; } for($i=0; $i<$maxheight; $i++) { if ($this->security>1){$y=bin2hex($i);}else{$y=$i;} $classes.=".".$this->random['yName']."".$y."{top:".$i."px;}"; } $element=$this->elements[rand(0,count($this->elements)-1)]; $ret="<$element id=\"".$this->random['boxName']."\" style=\"width:".$left."px; height:".($maxheight)."px\">".$ret.""; $this->alternativeQuestionExport=''; if (($left+10)>$this->security5['codeWidth']) {$this->security5['codeWidth']=$left+10;} return array("content"=>$ret,"style"=>$classes); } /** * divtcha::getStyle() * * Připraví a vrátí styl a script potřebný pro zobrazení DIVTCHA kódu. Script je potřeba pouze pro přehrátí zvukového kódu pro nevidomé. * * $handicap určuje soubor, který bude použit jako pozadí pro tlačítko pro spuštění zvukové alternativy kódu pro nevidomé. * $audio určuje soubor audio.php zpracovávající audio výstup * * @param string $handicap * @param string $audio * @return string */ public function getStyle($handicap='handicap.gif',$audio='audio.php') { $prestyle=''; $this->generate(); if ($this->getImageFolder()=='MASK/') { $border="none"; } else { $border="#333 1px solid"; } if ($this->security>4) { $prestyle.="#".$this->random['inputTable']."{border:#333 1px solid;} #".$this->random['inputTable']." td{padding:5px; background-color:#eee;} #".$this->random['inputBoxBoxId']."{float: left;width:".$this->security5['codeWidth']."px;overflow:hidden;height:60px;} #".$this->random['inputBoxId']."{float: left;width:0px;overflow:hidden;height:".$this->security5['codeHeight']."px;display:block;} #".$this->random['inputBoxBoxId'].":hover #".$this->random['inputBoxId']."{width:".$this->security5['codeWidth']."px; margin-top:20px;} #".$this->random['inputId']."{width:".$this->security5['inputWidth']."px;height:20px;border:#333 1px solid;}"; } $prestyle.="#".$this->random['blind']." {position:absolute; top:-500px; left:-500px;} #".$this->random['handicap']."{width:20px; height:20px; margin:0px 2px 0px 2px; border:#333 1px solid; background-image:url('".$handicap."'); overflow:hidden; font-size:0px; padding-left:10px; background-position:50% 50%; cursor:pointer;}"; if ($this->align!='') {$align=' float:'.$this->align;} else {$align='';} $prestyle.="#".$this->random['boxName']."{display:block; border:$border;background-color:#".$this->bg.";position:relative;overflow:hidden;$align}#".$this->random['boxName']." ".join(",#".$this->random['boxName']." ",$this->elements)."{display:block;position:absolute;background-color:#".$this->color.";width:1px;height:1px;border:none;overflow:hidden;}.dchAbsolute{position:absolute;}"; $js="