From c939a5e710d56641cb79f558a79c0126a644bc98 Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Fri, 8 Jun 2012 12:08:33 +0200 Subject: [PATCH] Added server validation, and example html --- .idea/.name | 1 + .idea/MotionCAPTCHA.iml | 19 + .idea/codeStyleSettings.xml | 13 + .idea/encodings.xml | 5 + .idea/inspectionProfiles/Project_Default.xml | 11 + .../inspectionProfiles/profiles_settings.xml | 7 + .idea/misc.xml | 9 + .idea/modules.xml | 9 + .idea/scopes/scope_settings.xml | 5 + .idea/vcs.xml | 8 + .idea/workspace.xml | 515 ++++++++++++++++++ index.html | 47 ++ jquery.motionCaptcha.0.2.js | 22 +- library/JSON-js | 1 + library/phpDollar | 1 + motionCaptcha.php | 123 +++++ 16 files changed, 795 insertions(+), 1 deletion(-) create mode 100644 .idea/.name create mode 100644 .idea/MotionCAPTCHA.iml create mode 100644 .idea/codeStyleSettings.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/scopes/scope_settings.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 index.html create mode 160000 library/JSON-js create mode 160000 library/phpDollar create mode 100644 motionCaptcha.php diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..d1790cd --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +MotionCAPTCHA \ No newline at end of file diff --git a/.idea/MotionCAPTCHA.iml b/.idea/MotionCAPTCHA.iml new file mode 100644 index 0000000..0106c4a --- /dev/null +++ b/.idea/MotionCAPTCHA.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml new file mode 100644 index 0000000..9178b38 --- /dev/null +++ b/.idea/codeStyleSettings.xml @@ -0,0 +1,13 @@ + + + + + + + diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..c66df00 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,11 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..3b31283 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d96d1e5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + http://www.w3.org/1999/xhtml + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2e6d55d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..6833434 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..6e73616 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,515 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1339077817193 + 1339077817193 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/index.html b/index.html new file mode 100644 index 0000000..e0de93f --- /dev/null +++ b/index.html @@ -0,0 +1,47 @@ + + + + Motion CAPTCHA + + + + +
+ + + +
+ Demo +
    +
  • + + +
  • +
  • + + +
  • +
  • +
    +

    Please draw the shape in the box to submit the form:

    + +
    +
  • +
  • + + +
  • +
+
+
+ + + + + + + \ No newline at end of file diff --git a/jquery.motionCaptcha.0.2.js b/jquery.motionCaptcha.0.2.js index 1f362c5..500a594 100644 --- a/jquery.motionCaptcha.0.2.js +++ b/jquery.motionCaptcha.0.2.js @@ -70,7 +70,20 @@ jQuery.fn.motionCaptcha || (function($) { ctx.fillStyle = opts.canvasTextColor; // Set random shape - $canvas.addClass( opts.shapes[Math.floor(Math.random() * (opts.shapes.length) )] ); + //$canvas.addClass( opts.shapes[Math.floor(Math.random() * (opts.shapes.length) )] ); + + $.get( + opts.preprocessor, + { + uniqueid: $(opts.uniqueId).attr('value'), + method: 'gettemplate' + }, + function(data) { + if (data != false) { + $canvas.addClass(data); + } + } + ); // Set up Dollar Recognizer and drawing vars: var _isDown = false, @@ -150,6 +163,10 @@ jQuery.fn.motionCaptcha || (function($) { // Mouseup event: var touchEndEvent = function(event) { + + //$(opts.pointsId).attr('value', $(_points).serializeArray()); + $(opts.pointsId).attr('value', JSON.stringify(_points)); + // If mouse is down and canvas not locked: if ( !locked && _isDown ) { _isDown = false; @@ -258,6 +275,9 @@ jQuery.fn.motionCaptcha || (function($) { * Exposed default plugin settings, which can be overridden in plugin call. */ $.fn.motionCaptcha.defaults = { + preprocessor: 'motionCaptcha.php', // Ajax file, which gets a unique id and returns a shape + uniqueId: '#mc-unique', // The hidden field with the unique id stamp + pointsId: '#mc-points', // The hidden field which holds the points you are drawing actionId: '#mc-action', // The ID of the input containing the form action divId: '#mc', // If you use an ID other than '#mc' for the placeholder, pass it in here canvasId: '#mc-canvas', // The ID of the MotionCAPTCHA canvas element diff --git a/library/JSON-js b/library/JSON-js new file mode 160000 index 0000000..5ca5ccb --- /dev/null +++ b/library/JSON-js @@ -0,0 +1 @@ +Subproject commit 5ca5ccb6dfc74a973a4726550e5bb44c2e262ec3 diff --git a/library/phpDollar b/library/phpDollar new file mode 160000 index 0000000..8558987 --- /dev/null +++ b/library/phpDollar @@ -0,0 +1 @@ +Subproject commit 8558987919dc2e0119b93b29447ebdf3f0996e0d diff --git a/motionCaptcha.php b/motionCaptcha.php new file mode 100644 index 0000000..44680ac --- /dev/null +++ b/motionCaptcha.php @@ -0,0 +1,123 @@ +dollar = new phpDollar; + $this->dollar->removeTemplate('left square bracket'); + $this->dollar->removeTemplate('right square bracket'); + $this->dollar->removeTemplate('left curly brace'); + } + + protected function __clone() {} + + /** + * @static + * @return MotionCaptcha + */ + static public function getInstance() + { + if (self::$_instance === null) { + return self::$_instance = new self; + } + return self::$_instance; + } + + /** + * @static + * + * @param string $canvas which canvas are we validating up against? + * @param string $points JSON string of the points + */ + public function validate($uniqueid, $points) + { + $points = json_decode($points, true); + switch (json_last_error()) { + case JSON_ERROR_NONE: break; + case JSON_ERROR_DEPTH: throw new InvalidArgumentException('Maximum stack depth exceeded'); break; + case JSON_ERROR_STATE_MISMATCH: throw new InvalidArgumentException('Underflow or the modes mismatch'); break; + case JSON_ERROR_CTRL_CHAR: throw new InvalidArgumentException('Unexpected control character found'); break; + case JSON_ERROR_SYNTAX: throw new InvalidArgumentException('Syntax error, malformed JSON'); break; + case JSON_ERROR_UTF8: throw new InvalidArgumentException('Malformed UTF-8 characters, possibly incorrectly encoded'); break; + default: throw new InvalidArgumentException('Unknown error'); break; + } + + array_walk($points, function(&$item, $key) { + $item['x'] = $item['X']; + $item['y'] = $item['Y']; + unset($item['X']); + unset($item['Y']); + }); + + $usedCanvas = $this->dollar->recognizeStroke($points); + + if ($usedCanvas['strokeName'] == $this->getUsedTemplate($uniqueid) && $usedCanvas['strokeScore'] >= $this->minimumScore) { + return true; + } else { + return false; + } + + } + + public function getRandomTemplate() + { + $templates = $this->dollar->getTemplates(); + if (is_array($templates)) { + $random = array_rand($templates, 1); + return $templates[$random]; + } + return false; + } + + public function setSession($uniqueid) + { + $template = $this->getRandomTemplate(); + $_SESSION['motioncaptcha'][$uniqueid]['canvas'] = $template['templName']; + return $template['templName']; + } + + protected function getUsedTemplate($uid) + { + return $_SESSION['motioncaptcha'][$uid]['canvas']; + } + +} + +session_start(); + +if (isset($_GET, $_GET['method'], $_GET['uniqueid']) && $_GET['method'] == 'gettemplate') { + echo MotionCaptcha::getInstance()->setSession($_GET['uniqueid']); + exit; +} + +try { + $validation = MotionCaptcha::getInstance()->validate($_POST['uniqueid'], $_POST['points']); + if ($validation === true) { + // Do your other validations here! + // And insert your POST and other stuff + echo 'We are validated!'; + exit; + } else { + echo 'Validation failed'; + } +} catch (Exception $e) { + echo $e->getMessage(); +} \ No newline at end of file