Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Service/ReCaptcha.php
Go to the documentation of this file.
00001 <?php
00023 require_once 'Zend/Service/Abstract.php';
00024 
00026 require_once 'Zend/Json.php';
00027 
00029 require_once 'Zend/Service/ReCaptcha/Response.php';
00030 
00041 class Zend_Service_ReCaptcha extends Zend_Service_Abstract
00042 {
00048     const API_SERVER = 'http://api.recaptcha.net';
00049 
00055     const API_SECURE_SERVER = 'https://api-secure.recaptcha.net';
00056 
00062     const VERIFY_SERVER = 'http://api-verify.recaptcha.net/verify';
00063 
00069     protected $_publicKey = null;
00070 
00076     protected $_privateKey = null;
00077 
00083     protected $_ip = null;
00084 
00090     protected $_params = array(
00091         'ssl' => false, /* Use SSL or not when generating the recaptcha */
00092         'error' => null, /* The error message to display in the recaptcha */
00093         'xhtml' => false /* Enable XHTML output (this will not be XHTML Strict
00094                             compliant since the IFRAME is necessary when
00095                             Javascript is disabled) */
00096     );
00097 
00105     protected $_options = array(
00106         'theme' => 'red',
00107         'lang' => 'en',
00108     );
00109 
00115     protected $_response = null;
00116 
00127     public function __construct($publicKey = null, $privateKey = null,
00128                                 $params = null, $options = null, $ip = null)
00129     {
00130         if ($publicKey !== null) {
00131             $this->setPublicKey($publicKey);
00132         }
00133 
00134         if ($privateKey !== null) {
00135             $this->setPrivateKey($privateKey);
00136         }
00137 
00138         if ($ip !== null) {
00139             $this->setIp($ip);
00140         } else if (isset($_SERVER['REMOTE_ADDR'])) {
00141             $this->setIp($_SERVER['REMOTE_ADDR']);
00142         }
00143 
00144         if ($params !== null) {
00145             $this->setParams($params);
00146         }
00147 
00148         if ($options !== null) {
00149             $this->setOptions($options);
00150         }
00151     }
00152 
00162     public function __toString()
00163     {
00164         try {
00165             $return = $this->getHtml();
00166         } catch (Exception $e) {
00167             $return = '';
00168             trigger_error($e->getMessage(), E_USER_WARNING);
00169         }
00170 
00171         return $return;
00172     }
00173 
00180     public function setIp($ip)
00181     {
00182         $this->_ip = $ip;
00183 
00184         return $this;
00185     }
00186 
00192     public function getIp()
00193     {
00194         return $this->_ip;
00195     }
00196 
00204     public function setParam($key, $value)
00205     {
00206         $this->_params[$key] = $value;
00207 
00208         return $this;
00209     }
00210 
00218     public function setParams($params)
00219     {
00220         if ($params instanceof Zend_Config) {
00221             $params = $params->toArray();
00222         }
00223 
00224         if (is_array($params)) {
00225             foreach ($params as $k => $v) {
00226                 $this->setParam($k, $v);
00227             }
00228         } else {
00230             require_once 'Zend/Service/ReCaptcha/Exception.php';
00231 
00232             throw new Zend_Service_ReCaptcha_Exception(
00233                 'Expected array or Zend_Config object'
00234             );
00235         }
00236 
00237         return $this;
00238     }
00239 
00245     public function getParams()
00246     {
00247         return $this->_params;
00248     }
00249 
00256     public function getParam($key)
00257     {
00258         return $this->_params[$key];
00259     }
00260 
00268     public function setOption($key, $value)
00269     {
00270         $this->_options[$key] = $value;
00271 
00272         return $this;
00273     }
00274 
00282     public function setOptions($options)
00283     {
00284         if ($options instanceof Zend_Config) {
00285             $options = $options->toArray();
00286         }
00287 
00288         if (is_array($options)) {
00289             foreach ($options as $k => $v) {
00290                 $this->setOption($k, $v);
00291             }
00292         } else {
00294             require_once 'Zend/Service/ReCaptcha/Exception.php';
00295 
00296             throw new Zend_Service_ReCaptcha_Exception(
00297                 'Expected array or Zend_Config object'
00298             );
00299         }
00300 
00301         return $this;
00302     }
00303 
00309     public function getOptions()
00310     {
00311         return $this->_options;
00312     }
00313 
00320     public function getOption($key)
00321     {
00322         return $this->_options[$key];
00323     }
00324 
00330     public function getPublicKey()
00331     {
00332         return $this->_publicKey;
00333     }
00334 
00341     public function setPublicKey($publicKey)
00342     {
00343         $this->_publicKey = $publicKey;
00344 
00345         return $this;
00346     }
00347 
00353     public function getPrivateKey()
00354     {
00355         return $this->_privateKey;
00356     }
00357 
00364     public function setPrivateKey($privateKey)
00365     {
00366         $this->_privateKey = $privateKey;
00367 
00368         return $this;
00369     }
00370 
00379     public function getHtml()
00380     {
00381         if ($this->_publicKey === null) {
00383             require_once 'Zend/Service/ReCaptcha/Exception.php';
00384 
00385             throw new Zend_Service_ReCaptcha_Exception('Missing public key');
00386         }
00387 
00388         $host = self::API_SERVER;
00389 
00390         if ((bool) $this->_params['ssl'] === true) {
00391             $host = self::API_SECURE_SERVER;
00392         }
00393 
00394         $htmlBreak = '<br>';
00395         $htmlInputClosing = '>';
00396 
00397         if ((bool) $this->_params['xhtml'] === true) {
00398             $htmlBreak = '<br />';
00399             $htmlInputClosing = '/>';
00400         }
00401 
00402         $errorPart = '';
00403 
00404         if (!empty($this->_params['error'])) {
00405             $errorPart = '&error=' . urlencode($this->_params['error']);
00406         }
00407 
00408         $reCaptchaOptions = '';
00409 
00410         if (!empty($this->_options)) {
00411             $encoded = Zend_Json::encode($this->_options);
00412             $reCaptchaOptions = <<<SCRIPT
00413 <script type="text/javascript">
00414     var RecaptchaOptions = {$encoded};
00415 </script>
00416 SCRIPT;
00417         }
00418 
00419         $return = $reCaptchaOptions;
00420         $return .= <<<HTML
00421 <script type="text/javascript"
00422    src="{$host}/challenge?k={$this->_publicKey}{$errorPart}">
00423 </script>
00424 HTML;
00425         $return .= <<<HTML
00426 <noscript>
00427    <iframe src="{$host}/noscript?k={$this->_publicKey}{$errorPart}"
00428        height="300" width="500" frameborder="0"></iframe>{$htmlBreak}
00429    <textarea name="recaptcha_challenge_field" rows="3" cols="40">
00430    </textarea>
00431    <input type="hidden" name="recaptcha_response_field"
00432        value="manual_challenge"{$htmlInputClosing}
00433 </noscript>
00434 HTML;
00435 
00436         return $return;
00437     }
00438 
00447     protected function _post($challengeField, $responseField)
00448     {
00449         if ($this->_privateKey === null) {
00451             require_once 'Zend/Service/ReCaptcha/Exception.php';
00452 
00453             throw new Zend_Service_ReCaptcha_Exception('Missing private key');
00454         }
00455 
00456         if ($this->_ip === null) {
00458             require_once 'Zend/Service/ReCaptcha/Exception.php';
00459 
00460             throw new Zend_Service_ReCaptcha_Exception('Missing ip address');
00461         }
00462 
00463         if (empty($challengeField)) {
00465             require_once 'Zend/Service/ReCaptcha/Exception.php';
00466             throw new Zend_Service_ReCaptcha_Exception('Missing challenge field');
00467         }
00468 
00469         if (empty($responseField)) {
00471             require_once 'Zend/Service/ReCaptcha/Exception.php';
00472 
00473             throw new Zend_Service_ReCaptcha_Exception('Missing response field');
00474         }
00475 
00476         /* Fetch an instance of the http client */
00477         $httpClient = self::getHttpClient();
00478 
00479         $postParams = array('privatekey' => $this->_privateKey,
00480                             'remoteip'   => $this->_ip,
00481                             'challenge'  => $challengeField,
00482                             'response'   => $responseField);
00483 
00484         /* Make the POST and return the response */
00485         return $httpClient->setUri(self::VERIFY_SERVER)
00486                           ->setParameterPost($postParams)
00487                           ->request(Zend_Http_Client::POST);
00488     }
00489 
00500     public function verify($challengeField, $responseField)
00501     {
00502         $response = $this->_post($challengeField, $responseField);
00503 
00504         return new Zend_Service_ReCaptcha_Response(null, null, $response);
00505     }
00506 }
 All Data Structures Namespaces Files Functions Variables Enumerations