[LON-CAPA-cvs] cvs: loncom(version_2_11_X) /interface loncommon.pm

raeburn raeburn at source.lon-capa.org
Fri Aug 12 15:13:46 EDT 2016


raeburn		Fri Aug 12 19:13:46 2016 EDT

  Modified files:              (Branch: version_2_11_X)
    /loncom/interface	loncommon.pm 
  Log:
  - For 2.11
    - Backport 1.1234
  
  
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.1075.2.106 loncom/interface/loncommon.pm:1.1075.2.107
--- loncom/interface/loncommon.pm:1.1075.2.106	Thu Aug 11 00:15:06 2016
+++ loncom/interface/loncommon.pm	Fri Aug 12 19:13:44 2016
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1075.2.106 2016/08/11 00:15:06 raeburn Exp $
+# $Id: loncommon.pm,v 1.1075.2.107 2016/08/12 19:13:44 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -76,6 +76,8 @@
 use Encode();
 use Authen::Captcha;
 use Captcha::reCAPTCHA;
+use JSON::DWIW;
+use LWP::UserAgent;
 use Crypt::DES;
 use DynaLoader; # for Crypt::DES version
 
@@ -16014,29 +16016,30 @@
 sub captcha_display {
     my ($context,$lonhost) = @_;
     my ($output,$error);
-    my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
+    my ($captcha,$pubkey,$privkey,$version) =
+        &get_captcha_config($context,$lonhost);
     if ($captcha eq 'original') {
         $output = &create_captcha();
         unless ($output) {
             $error = 'captcha';
         }
     } elsif ($captcha eq 'recaptcha') {
-        $output = &create_recaptcha($pubkey);
+        $output = &create_recaptcha($pubkey,$version);
         unless ($output) {
             $error = 'recaptcha';
         }
     }
-    return ($output,$error,$captcha);
+    return ($output,$error,$captcha,$version);
 }
 
 sub captcha_response {
     my ($context,$lonhost) = @_;
     my ($captcha_chk,$captcha_error);
-    my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
+    my ($captcha,$pubkey,$privkey.$version) = &get_captcha_config($context,$lonhost);
     if ($captcha eq 'original') {
         ($captcha_chk,$captcha_error) = &check_captcha();
     } elsif ($captcha eq 'recaptcha') {
-        $captcha_chk = &check_recaptcha($privkey);
+        $captcha_chk = &check_recaptcha($privkey,$version);
     } else {
         $captcha_chk = 1;
     }
@@ -16045,7 +16048,7 @@
 
 sub get_captcha_config {
     my ($context,$lonhost) = @_;
-    my ($captcha,$pubkey,$privkey,$hashtocheck);
+    my ($captcha,$pubkey,$privkey,$version,$hashtocheck);
     my $hostname = &Apache::lonnet::hostname($lonhost);
     my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
     my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
@@ -16061,6 +16064,10 @@
                     }
                     if ($privkey && $pubkey) {
                         $captcha = 'recaptcha';
+                        $version = $hashtocheck->{'recaptchaversion'};
+                        if ($version ne '2') {
+                            $version = 1;
+                        }
                     } else {
                         $captcha = 'original';
                     }
@@ -16078,6 +16085,10 @@
             $privkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_private'};
             if ($privkey && $pubkey) {
                 $captcha = 'recaptcha';
+                $version = $domconfhash{$serverhomedom.'.login.recaptchaversion'};
+                if ($version ne '2') {
+                    $version = 1;
+                }
             } else {
                 $captcha = 'original';
             }
@@ -16085,7 +16096,7 @@
             $captcha = 'original';
         }
     }
-    return ($captcha,$pubkey,$privkey);
+    return ($captcha,$pubkey,$privkey,$version);
 }
 
 sub create_captcha {
@@ -16144,32 +16155,55 @@
 }
 
 sub create_recaptcha {
-    my ($pubkey) = @_;
-    my $use_ssl;
-    if ($ENV{'SERVER_PORT'} == 443) {
-        $use_ssl = 1;
-    }
-    my $captcha = Captcha::reCAPTCHA->new;
-    return $captcha->get_options_setter({theme => 'white'})."\n".
-           $captcha->get_html($pubkey,undef,$use_ssl).
-           &mt('If the text is hard to read, [_1] will replace them.',
-               '<img src="/res/adm/pages/refresh.gif" alt="reCAPTCHA refresh" />').
-           '<br /><br />';
+    my ($pubkey,$version) = @_;
+    if ($version >= 2) {
+        return '<div class="g-recaptcha" data-sitekey="'.$pubkey.'"></div>';
+    } else {
+        my $use_ssl;
+        if ($ENV{'SERVER_PORT'} == 443) {
+            $use_ssl = 1;
+        }
+        my $captcha = Captcha::reCAPTCHA->new;
+        return $captcha->get_options_setter({theme => 'white'})."\n".
+               $captcha->get_html($pubkey,undef,$use_ssl).
+               &mt('If the text is hard to read, [_1] will replace them.',
+                   '<img src="/res/adm/pages/refresh.gif" alt="reCAPTCHA refresh" />').
+               '<br /><br />';
+     }
 }
 
 sub check_recaptcha {
-    my ($privkey) = @_;
+    my ($privkey,$version) = @_;
     my $captcha_chk;
-    my $captcha = Captcha::reCAPTCHA->new;
-    my $captcha_result =
-        $captcha->check_answer(
-                                $privkey,
-                                $ENV{'REMOTE_ADDR'},
-                                $env{'form.recaptcha_challenge_field'},
-                                $env{'form.recaptcha_response_field'},
-                              );
-    if ($captcha_result->{is_valid}) {
-        $captcha_chk = 1;
+    if ($version >= 2) {
+        my $ua = LWP::UserAgent->new;
+        $ua->timeout(10);
+        my %info = (
+                     secret   => $privkey,
+                     response => $env{'form.g-recaptcha-response'},
+                     remoteip => $ENV{'REMOTE_ADDR'},
+                   );
+        my $response = $ua->post('https://www.google.com/recaptcha/api/siteverify',\%info);
+        if ($response->is_success)  {
+            my $data = JSON::DWIW->from_json($response->decoded_content);
+            if (ref($data) eq 'HASH') {
+                if ($data->{'success'}) {
+                    $captcha_chk = 1;
+                }
+            }
+        }
+    } else {
+        my $captcha = Captcha::reCAPTCHA->new;
+        my $captcha_result =
+            $captcha->check_answer(
+                                    $privkey,
+                                    $ENV{'REMOTE_ADDR'},
+                                    $env{'form.recaptcha_challenge_field'},
+                                    $env{'form.recaptcha_response_field'},
+                                  );
+        if ($captcha_result->{is_valid}) {
+            $captcha_chk = 1;
+        }
     }
     return $captcha_chk;
 }




More information about the LON-CAPA-cvs mailing list