From 2e98a48f2b992c0a566689d3246880f6e81fbee3 Mon Sep 17 00:00:00 2001
From: Brion Vibber <brion@pobox.com>
Date: Sun, 20 Jun 2010 19:30:12 +0000
Subject: [PATCH] RecaptchaPlugin: fix for missing captcha on iPhone/Android.

MobileProfile serves pages out to iPhone and Android as application/xhtml+xml, which doesn't work with the default we we were loading recaptcha (as it used document.write). Switched to filling out a <div> from the AJAX API, which doesn't use document.write in the XHTML context.

Tested that view & submission works ok in following browsers:
Mobile: iPhone 3.1, Android 2.1, iPad 3.2 (this last doesn't trigger mobile theme tweaks)
Ubuntu 10.04: Firefox 3.6.3, Chrome 6
Mac 10.6: Safari 5/OS X 10.6.4
Windows 7: IE 8, Opera 10.56
---
 plugins/Recaptcha/RecaptchaPlugin.php | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/plugins/Recaptcha/RecaptchaPlugin.php b/plugins/Recaptcha/RecaptchaPlugin.php
index c585da43c4..f09d81ec00 100644
--- a/plugins/Recaptcha/RecaptchaPlugin.php
+++ b/plugins/Recaptcha/RecaptchaPlugin.php
@@ -62,12 +62,29 @@ class RecaptchaPlugin extends Plugin
     {
         $action->elementStart('li');
         $action->raw('<label for="recaptcha">Captcha</label>');
-        if($this->checkssl() === true) {
-            $action->raw(recaptcha_get_html($this->public_key), null, true);
-        } else { 
-            $action->raw(recaptcha_get_html($this->public_key));
-        }
+
+        // AJAX API will fill this div out.
+        // We're calling that instead of the regular one so we stay compatible
+        // with application/xml+xhtml output as for mobile.
+        $action->element('div', array('id' => 'recaptcha'));
         $action->elementEnd('li');
+        
+        $action->recaptchaPluginNeedsOutput = true;
+        return true;
+    }
+
+    function onEndShowScripts($action)
+    {
+        if (isset($action->recaptchaPluginNeedsOutput) && $action->recaptchaPluginNeedsOutput) {
+            // Load the AJAX API
+            $proto = $this->checkssl() ? 'https' : 'http';
+            $url = "$proto://api.recaptcha.net/js/recaptcha_ajax.js";
+            $action->script($url);
+            
+            // And when we're ready, fill out the captcha!
+            $key = json_encode($this->public_key);
+            $action->inlinescript("\$(function(){Recaptcha.create($key, 'recaptcha');});");
+        }
         return true;
     }
 
-- 
2.39.5