Permalink
Browse files

Updating examples to use composer, updating README

  • Loading branch information...
1 parent 14a4803 commit 43d26c639f87bdaa24aab3ac7803e7f42a878d27 @AlekseyKorzun committed Aug 14, 2012
Showing with 393 additions and 390 deletions.
  1. +5 −8 Examples/Basic.php
  2. +0 −324 Package/Captcha.php
  3. +318 −0 Package/Captcha/Captcha.php
  4. +1 −1 Package/Captcha/Exception.php
  5. +53 −53 Package/Captcha/Response.php
  6. +16 −4 README.md
View
@@ -1,14 +1,11 @@
<?php
-namespace Application;
-use Library\Captcha;
-
/**
- * No autoloader
- */
-require '../Captcha.php';
+ * You must run `composer install` in order to generate autoloader for this example
+ */
+require __DIR__ . '/../vendor/autoload.php';
// New captcha instance
-$captcha = new Captcha();
+$captcha = new Captcha\Captcha();
$captcha->setPublicKey('publickey');
$captcha->setPrivateKey('privatekey');
@@ -18,6 +15,6 @@
// Perform validation (put this inside if ($_POST) {} condition for example)
$response = $captcha->check();
if (!$response->isValid()) {
- echo $response->getError();
+ echo $response->getError();
}
View
@@ -1,324 +0,0 @@
-<?php
-namespace Library;
-
-use Library\Captcha\Response;
-use Library\Captcha\Exception;
-
-/**
- * You should be using auto loader, remove this as needed
- */
-require 'Captcha/Response.php';
-require 'Captcha/Exception.php';
-
-/**
- * Copyright (c) 2012, Aleksey Korzun <al.ko@webfoundation.net>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * The views and conclusions contained in the software and documentation are those
- * of the authors and should not be interpreted as representing official policies,
- * either expressed or implied, of the FreeBSD Project.
- *
- * Proper library for reCaptcha service
- *
- * @author Aleksey Korzun <al.ko@webfoundation.net>
- * @throws Exception
- * @package library
- */
-class Captcha
-{
- /**
- * reCaptcha's API server
- *
- * @var string
- */
- const SERVER = 'http://api.recaptcha.net';
-
- /**
- * reCaptcha's secure API server
- *
- * @var string
- */
- const SERVER_SECURE = 'https://api-secure.recaptcha.net';
-
- /**
- * reCaptcha's verify server
- *
- * @var string
- */
- const VERIFY_SERVER = 'api-verify.recaptcha.net';
-
- /**
- * Private key
- *
- * @var string
- */
- protected $privateKey;
-
- /**
- * Public key
- *
- * @var string
- */
- protected $publicKey;
-
- /**
- * Custom error message to return
- *
- * @var string
- */
- protected $error;
-
- /**
- * Flag to use SSL for our request(s)
- *
- * @var bool
- */
- protected $isSsl = false;
-
- /**
- * Set SSL flag
- *
- * @param bool $flag
- * @return void
- */
- public function setSsl($flag = true)
- {
- $this->isSsl = (bool) $flag;
- }
-
- /**
- * Check if SSL is currently enabled
- *
- * @return bool
- */
- public function isSsl()
- {
- return (bool) $this->isSsl;
- }
-
- /**
- * Set public key
- *
- * @param string $key
- * @return reCaptcha
- */
- public function setPublicKey($key)
- {
- $this->publicKey = $key;
- return $this;
- }
-
- /**
- * Retrieve currently set public key
- *
- * @return string
- */
- public function getPublicKey()
- {
- return $this->publicKey;
- }
-
- /**
- * Set private key
- *
- * @param string $key
- * @return reCaptcha
- */
- public function setPrivateKey($key)
- {
- $this->privateKey = $key;
- return $this;
- }
-
- /**
- * Retrieve currently set private key
- *
- * @return string
- */
- public function getPrivateKey()
- {
- return $this->privateKey;
- }
-
- /**
- * Set public key
- *
- * @param string $error
- * @return reCaptcha
- */
- public function setError($error)
- {
- $this->error = (string) $error;
- return $this;
- }
-
- /**
- * Retrieve currently set error
- *
- * @return string
- */
- public function getError()
- {
- return $this->error;
- }
-
- /**
- * Generates reCaptcha form to output to your end user
- *
- * @throws Exception
- * @return string
- */
- public function html()
- {
- if (!$this->getPublicKey()) {
- throw new Exception('You must set public key provided by reCaptcha');
- }
-
- if ($this->isSsl()) {
- $server = self::SERVER_SECURE;
- } else {
- $server = self::SERVER;
- }
-
- $error = ($this->getError() ? '&amp;error=' . $this->getError() : null);
-
- return '<script type="text/javascript" src="' . $server . '/challenge?k=' . $this->getPublicKey() . $error . '"></script>
-
- <noscript>
- <iframe src="' . $server . '/noscript?k=' . $this->getPublicKey() . $error . '" height="300" width="500" frameborder="0"></iframe><br/>
- <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
- <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
- </noscript>';
- }
-
- /**
- * Checks and validates user's response
- *
- * @throws Exception
- * @return Response
- */
- public function check()
- {
- if (!$this->getPrivateKey()) {
- throw new Exception('You must set private key provided by reCaptcha');
- }
-
- $captcha_challenge = false;
- $captcha_response = false;
-
- // Skip processing of empty data
- if (isset($_POST['recaptcha_challenge_field']) && isset($_POST['recaptcha_response_field'])) {
- $captcha_challenge = $_POST['recaptcha_challenge_field'];
- $captcha_response = $_POST['recaptcha_response_field'];
- }
-
- // Instance of response object
- $response = new Response();
-
- // Discard SPAM submissions
- if (strlen($captcha_challenge) == 0 || strlen($captcha_response) == 0) {
- $response->setValid(false);
- $response->setError('Incorrect-captcha-sol');
- return $response;
- }
-
- $process = $this->process(
- array(
- 'privatekey' => $this->getPrivateKey(),
- 'remoteip' => $_SERVER['REMOTE_ADDR'],
- 'challenge' => $captcha_challenge,
- 'response' => $captcha_response
- )
- );
-
- $answers = explode("\n", $process [1]);
-
- if (trim($answers[0]) == 'true') {
- $response->setValid(true);
- } else {
- $response->setValid(false);
- $response->setError($answers[1]);
- }
-
- return $response;
- }
-
- /**
- * Make a signed validation request to reCaptcha's servers
- *
- * @throws Exception
- * @param array $parameters
- * @return string
- */
- protected function process($parameters)
- {
- // Properly encode parameters
- $parameters = $this->encode($parameters);
-
- $request = "POST /verify HTTP/1.0\r\n";
- $request .= "Host: " . self::VERIFY_SERVER . "\r\n";
- $request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
- $request .= "Content-Length: " . strlen($parameters) . "\r\n";
- $request .= "User-Agent: reCAPTCHA/PHP5\r\n";
- $request .= "\r\n";
- $request .= $parameters;
-
- if (false == ($socket = @fsockopen(self::VERIFY_SERVER, 80))) {
- throw new Exception('Could not open socket to: ' . self::VERIFY_SERVER);
- }
-
- fwrite($socket, $request);
-
- $response = '';
-
- while (!feof($socket) ) {
- $response .= fgets($socket, 1160);
- }
-
- fclose($socket);
-
- return explode("\r\n\r\n", $response, 2);
- }
-
- /**
- * Construct encoded URI string from an array
- *
- * @param array $parameters
- * @return string
- */
- protected function encode(array $parameters)
- {
- $uri = '';
-
- if ($parameters) {
- foreach ($parameters as $parameter => $value) {
- $uri .= $parameter . '=' . urlencode(stripslashes($value)) . '&';
- }
- }
-
- $uri = substr($uri, 0, strlen($uri)-1);
-
- return $uri;
- }
-}
-
Oops, something went wrong.

0 comments on commit 43d26c6

Please sign in to comment.