Skip to content
Snippets Groups Projects
enmask.captcha.php 1.61 KiB
Newer Older
enmasksteve's avatar
enmasksteve committed
<?php

/**
 * @file
enmasksteve's avatar
enmasksteve committed
 * Enmask Captcha Class freely available.
enmasksteve's avatar
enmasksteve committed
 * @version 1.0
 * @copyright 2011
 * @Author : Enmask.com
 */

class EnmaskCaptcha {
  private $protocol;
  private $server;
  private $port;

  public function EnmaskCaptcha() {
    $this->protocol = "http://";
    $this->server = "enmask.com";
    $this->port = "";
  }

  public function getHtml($name) {
enmasksteve's avatar
enmasksteve committed
    // Only valid for drupal.
enmasksteve's avatar
enmasksteve committed
    global $language;
    $lang_code = $language->language;
enmasksteve's avatar
enmasksteve committed
    // Ends drupal code.
enmasksteve's avatar
enmasksteve committed

    if ($lang_code != '') {
      $js_append = 'data-enmask-langcode="' . $lang_code . '"';
    }
    else {
      $js_append = '';
    }

    $url = $this->protocol . $this->server . $this->port . "/Scripts/Enmask.Captcha.js";
    return '<script type="text/javascript" ' . $js_append . ' src="' . $url . '" data-enmask="true" data-enmask-name="' . $name . '"></script>';
  }

  /*
enmasksteve's avatar
enmasksteve committed
   * Validating by posting data in enmask server using drupal_http_request.
   */
enmasksteve's avatar
enmasksteve committed
  public function validate($captchaResponse, $captchaChallenge) {
    $url = $this->protocol . $this->server . $this->port . "/CaptchaFont/ValidateCaptcha";
    $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
    $data = array(
      'response' => $captchaResponse,
      'challenge' => $captchaChallenge);
    $buffer = drupal_http_request($url, array('headers' => array('Content-Type' => 'application/x-www-form-urlencoded'), 'method' => 'POST', 'data' => http_build_query($data, '', '&')));
enmasksteve's avatar
enmasksteve committed
    $result = json_decode($buffer->data);
    // Return message will be from the enmask server.
    return array($result->isValid, t('Captcha you entered was not valid.'));
enmasksteve's avatar
enmasksteve committed
  }
enmasksteve's avatar
enmasksteve committed
}