示例#1
0
 public static function API()
 {
     #-- load data
     if ($id = @$_GET[CAPTCHA_PARAM_ID]) {
         #-- special case
         if ($id == 'base.js') {
             easy_captcha_utility::js_base();
         } else {
             $c = new easy_captcha($id = NULL, $ignore_expiration = 1);
             $expired = !$c->is_valid();
             #-- JS-RPC request, check entered solution on the fly
             if ($test = @$_REQUEST[CAPTCHA_PARAM_INPUT]) {
                 #-- check
                 if ($expired || empty($c->image)) {
                     die(easy_captcha_utility::js_header('alert("captcha error: request invalid (wrong storage id) / or expired");'));
                 }
                 if (0 >= $c->ajax_tries--) {
                     $c->log("::API", "JS-RPC", "ajax_tries exhausted ({$c->ajax_tries})");
                 }
                 $ok = $c->image->solved($test) || $c->text->solved($test);
                 #-- sendresult
                 easy_captcha_utility::js_rpc($ok);
             } else {
                 if ($expired) {
                     $type = "image/png";
                     $bin = easy_captcha_utility::expired_png();
                 } else {
                     $type = "image/jpeg";
                     $bin = $c->image->jpeg();
                 }
                 header("Pragma: no-cache");
                 header("Cache-Control: no-cache, no-store, must-revalidate, private");
                 header("Expires: " . gmdate("r", time()));
                 header("Content-Length: " . strlen($bin));
                 header("Content-Type: {$type}");
                 print $bin;
             }
         }
         exit;
     }
 }