<?php define('MODE_KEYWORDS', 0x01 ); define('MODE_ALL_PICS', 0x02 ); define('SORRY', "This seems to happen a lot when querying Flickr. I have to figure this one out. Very sorry."); define('FOOTER', "<br /><a href='". $_SERVER['PHP_SELF']. "?mode=tags'>Take this Test with Words (Tags)</a> --- <a href='". $_SERVER['PHP_SELF']. "?mode=pics'>Take this Test with Pictures Only</a><br /><br /><strong><i><a href='http://www.nextbbs.com/do_topic_id_573_post_3927'>To My Post & Source Code</i></strong></a>"); define('DB_TABLE', 'cfr_challenges'); // ++ Modify these two lines define('DB_DSN', 'mysql://labs:labs@localhost/labs'); define('FLICKR_KEY', '749b450d27abbffe315c244d49dc6cac'); // -- class Captcha { var $debug, $mode, $seedtag, $childtag, $credits, $f; function Captcha() { // Change this line to switch modes $this->mode = MODE_ALL_PICS; if(isset($_GET['mode']) && $_GET['mode']== 'tags') $this->mode = MODE_KEYWORDS; $this->debug = 2; $this-> credits = array(); $this->challenge(); } { if($this->debug>0) { if($this->debug>1) { $f = fopen("/tmp/captcha.log", "a"); } } } function challenge() { // Did they submit? if(isset($_POST['hiddenid']) && isset($_POST['hiddenf'])) { $this->processResponse(&$_POST['hiddenid'], &$_POST['hiddenf']); } else { $this->askQuestion(); } } function processResponse($id, $reply) { require_once 'DB.php'; $db =& DB::connect(DB_DSN); if (PEAR::isError($db)) { } $res = $db->query("SELECT valid FROM `".DB_TABLE."` WHERE `cid`='".$id."'"); $row = $res->fetchRow(DB_FETCHMODE_ORDERED); $valid = $row[0]; $res->free(); // Get rid of this challenge $db->query("DELETE FROM `".DB_TABLE."` WHERE `cid`='".$id."'"); // Sort reply for($i=0; $i<strlen($reply); $i++) $tmp[] = $reply[$i]; $sortedreply = ''; foreach($tmp as $c) $sortedreply .= $c; // if($sortedreply == $valid) print "<h1>Congratulations! You passed. You must be a human.</h1>"; else print "<h3><div style='color:red;'>Oh, I'm sorry, this is not the correct answer.</div></h3>"; } function _getPictures($args) { $bRandom = &$args['random']; $nKeep = &$args['keep']; $nMaxSize = &$args['maxSize']; for($ctr=0; $ctr<MAX_RETRIES; $ctr++) { if($bRandom) { 'date_posted_desc', 'date_taken_desc', 'interestingness_desc'); $sortOrderIdx = rand(0, 2); $p_sort = $sortOrders[$sortOrderIdx]; $takenOrUpload = rand(0, 1); $p_maxDate = $takenOrUpload == 1 ? 'max_taken_date' : 'max_upload_date'; $aWhileBack = time() - 182 * 24 * 60 * 60; $p_maxDateValue = date('Y-m-d H:i:s', $stopAt); $args = 'license' => '4', // Attribution License 'per_page' => $nMaxSize, 'sort' => $p_sort, $p_maxDate => $p_maxDateValue, ); } else { 'cat', 'cats', 'baby', 'beach', 'dog', 'dogs', 'flower', 'flowers', 'food', 'garden', 'girl', 'graffiti', 'house', 'kids', 'lake', 'mountain', 'mountains', 'park', 'rock', 'sign', 'turtle', 'snow', 'sun', 'sunset', 'tree', 'trees', 'wedding', 'zoo', ); $l = count($seedtags) - 1; $this->seedtag = $seedtags[$idx]; $this->log("Using Tag: ".$this->seedtag."<br />"); $this->log( "Related tags:<br>
"); $comma=''; $tags = $this->f->tags_getRelated($this->seedtag); foreach ($tags['tag'] as $tag) { $this->log($comma.$tag); $comma = ','; } $l = count($tags['tag']); $this->childtag = $tags['tag'][$idx]; $this->log( "Retrieving Photos with tag '".$this->seedtag."' and child tag '".$this->childtag."':<br>
"); $args = "license" => '4', // Attribution License 'per_page' => $nMaxSize, 'tag_mode' => 'all', 'tags' =>$this->seedtag.','.$this->childtag, ); } $photos = $this->f->photos_search($args); if(count($photos['photo']) < $nMaxSize) { $this->log("Error retrieving info from Flicker...retrying<br />"); } else break; } if(count($photos['photo']) < $nMaxSize) die("</div><a href='". $_SERVER['PHP_SELF']. "'>Not enough sample images".SORRY. " Click to try again.</a>"); while(count($photosArray)< $nKeep) { $r = rand(0, $nMaxSize - 1); if(! isset($photosArray[$r])) $photosArray[$r] = true; } if($this->mode==MODE_ALL_PICS && !$bRandom) { while(count($queryArray)< $nKeep) { $r = rand(0, $nMaxSize - 1); if(! isset($photosArray[$r]) && ! isset($queryArray[$r])) $queryArray[$r] = true; } } $this->log("Keeping only $nKeep pictures<br />"); $runner = 0; foreach ($photos['photo'] as $photo) { // Build image and link tags for each photo if(isset($photosArray[$runner])) { $photosArray[$runner] = array('valid'=> ($bRandom? false: true), 'link'=> $this-> f-> buildPhotoURL($photo, "Square")); $this->credits .= "<a href='http://www.flickr.com/photos/$photo[owner]/$photo[id]'>" . $photo[title] . "</a><br />"; # $this->log($samplePhotos[$runner]); } else if($this-> mode==MODE_ALL_PICS && isset($queryArray[$runner])) { $queryArray[$runner] = array('link'=> $this-> f-> buildPhotoURL($photo, "Square")); $credits .= "<a href='http://www.flickr.com/photos/$photo[owner]/$photo[id]'>" . $photo[title] . "</a><br />"; } $runner ++; } if($this->mode==MODE_ALL_PICS && !$bRandom) return array($photosArray, $queryArray); else return $photosArray; } function _getDecoy($prefix, $idx, $link) { // Retrieve picture from Flickr $this->f->req->setMethod(HTTP_REQUEST_METHOD_GET); $this->f->req->setURL($link); if(!$this->f->req->sendRequest()) { print "Error while retrieving picture from Flickr. Time out?<br /><br />"; return null; } $response = $this->f->req->getResponseBody(); $fname = 'tmp/'.$prefix.$idx.'.jpg'; $h = fopen($fname, "wb"); return $fname; } function askQuestion() { require_once("phpFlickr/phpFlickr.php"); // Create new phpFlickr object $this->f = new phpFlickr(FLICKR_KEY); // 1st, discover 50 random pictures $maxSample = 50; $maxValid = 50; <HTML> <BODY> <form id='submitform' action='{$_SERVER['PHP_SELF']}' method='post'> <input type="hidden" id="hiddenf" name="hiddenf" value="" /> <div id="kitty"> <style type="text/css"> #kitty { text-align:center; } table.kittyauth { margin:auto; border: none; background-color:#000;<
|