[LON-CAPA-cvs] cvs: loncom /interface domainprefs.pm loncommon.pm loncreateuser.pm lonsupportreq.pm

raeburn raeburn at source.lon-capa.org
Thu Feb 18 21:39:08 EST 2016


raeburn		Fri Feb 19 02:39:08 2016 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm loncommon.pm loncreateuser.pm 
                     	lonsupportreq.pm 
  Log:
  - Support version 2 of Google reCAPTCHA.
  
  
-------------- next part --------------
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.268 loncom/interface/domainprefs.pm:1.269
--- loncom/interface/domainprefs.pm:1.268	Thu Feb 18 21:30:33 2016
+++ loncom/interface/domainprefs.pm	Fri Feb 19 02:39:07 2016
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.268 2016/02/18 21:30:33 raeburn Exp $
+# $Id: domainprefs.pm,v 1.269 2016/02/19 02:39:07 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -4955,7 +4955,8 @@
 
 sub captcha_choice {
     my ($context,$settings,$itemcount) = @_;
-    my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
+    my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
+        $vertext,$currver);
     my %lt = &captcha_phrases();
     $keyentry = 'hidden';
     if ($context eq 'cancreate') {
@@ -4973,6 +4974,11 @@
             $pubtext = $lt{'pub'};
             $privtext = $lt{'priv'};
             $keyentry = 'text';
+            $vertext = $lt{'ver'};
+            $currver = $settings->{'recaptchaversion'};
+            if ($currver ne '2') {
+                $currver = 1;
+            }
         }
         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
             $currpub = $settings->{'recaptchakeys'}{'public'};
@@ -5006,7 +5012,11 @@
                $currpub.'" size="40" /></span><br />'."\n".
                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span> '."\n".
                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
-               $currpriv.'" size="40" /></span></td></tr></table>'."\n".
+               $currpriv.'" size="40" /></span><br />'.
+               '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span> '."\n".
+               '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
+               $currver.'" size="3" /></span><br />'.
+               '</td></tr></table>'."\n".
                '</td></tr>';
     return $output;
 }
@@ -6791,6 +6801,13 @@
                         $chgtxt .= '</ul>';
                         $resulttext .= '<li>'.$chgtxt.'</li>';
                     }
+                } elsif ($item eq 'recaptchaversion') {
+                    if (ref($loginhash{'login'}) eq 'HASH') {
+                        if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
+                            $resulttext .= '<li>'.&mt('ReCAPTCHA set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
+                                           '</li>';
+                        }
+                    }
                 } else {
                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
                 }
@@ -9482,7 +9499,8 @@
                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
-                            ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
+                            ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
+                            ($item eq 'recaptchaversion')) {
                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
                         } else {
                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
@@ -9706,6 +9724,7 @@
                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
+                            ($item eq 'recaptchaversion') ||
                             ($item eq 'emailusername') || ($item eq 'notify') ||
                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
@@ -10035,6 +10054,7 @@
 #
     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
+    $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
     if (ref($cancreate{'notify'}) eq 'HASH') {
         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
@@ -10196,6 +10216,11 @@
                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
                         }
                         $chgtext .= '</ul>';
+                    } elsif ($type eq 'recaptchaversion') {
+                        if ($savecaptcha{'captcha'} eq 'recaptcha') {
+                            $chgtext .= '<li>'.&mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type}).
+                                        '</li>';
+                        }
                     } elsif ($type eq 'emailusername') {
                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
                             if (ref($types) eq 'ARRAY') {
@@ -10316,7 +10341,7 @@
             $changes->{'captcha'} = 1;
         }
     }
-    my ($newpub,$newpriv,$currpub,$currpriv);
+    my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
     if ($newsettings->{'captcha'} eq 'recaptcha') {
         $newpub = $env{'form.'.$container.'_recaptchapub'};
         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
@@ -10326,6 +10351,12 @@
                                              public  => $newpub,
                                              private => $newpriv,
                                           };
+        $newversion = $env{'form.'.$container.'_recaptchaversion'};
+        $newversion =~ s/\D//g;
+        if ($newversion ne '2') {
+            $newversion = 1;
+        }
+        $newsettings->{'recaptchaversion'} = $newversion;
     }
     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
         $currpub = $current->{'recaptchakeys'}{'public'};
@@ -10337,6 +10368,23 @@
                                               }
         }
     }
+    if ($current->{'captcha'} eq 'recaptcha') {
+        $currversion = $current->{'recaptchaversion'};
+        if ($currversion ne '2') {
+            $currversion = 1;
+        }
+    }
+    if ($currversion ne $newversion) {
+        if ($container eq 'cancreate') {
+            if (ref($changes->{'cancreate'}) eq 'ARRAY') {
+                push(@{$changes->{'cancreate'}},'recaptchaversion');
+            } elsif (!defined($changes->{'cancreate'})) {
+                $changes->{'cancreate'} = ['recaptchaversion'];
+            }
+        } else {
+            $changes->{'recaptchaversion'} = 1;
+        }
+    }
     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
         if ($container eq 'cancreate') {
             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
@@ -13024,6 +13072,8 @@
     var pubitem;
     var privtext;
     var pubtext;
+    var versionitem;
+    var versiontext;
     if (document.getElementById(context+'_recaptchapub')) {
         pubitem = document.getElementById(context+'_recaptchapub');
     } else {
@@ -13044,6 +13094,16 @@
     } else {
         return;
     }
+    if (document.getElementById(context+'_recaptchaversion')) {
+        versionitem = document.getElementById(context+'_recaptchaversion');
+    } else {
+        return;
+    }
+    if (document.getElementById(context+'_recaptchavertxt')) {
+        versiontext = document.getElementById(context+'_recaptchavertxt');
+    } else {
+        return;
+    }
     if (caller.checked) {
         if (caller.value == 'recaptcha') {
             pubitem.type = 'text';
@@ -13052,11 +13112,16 @@
             privitem.size = '40';
             pubtext.innerHTML = "$lt{'pub'}";
             privtext.innerHTML = "$lt{'priv'}";
+            versionitem.type = 'text';
+            versionitem.size = '3';
+            versiontext.innerHTML = "$lt{'ver'}"; 
         } else {
             pubitem.type = 'hidden';
             privitem.type = 'hidden';
+            versionitem.type = 'hidden';
             pubtext.innerHTML = '';
             privtext.innerHTML = '';
+            versiontext.innerHTML = '';
         }
     }
     return;
@@ -13122,6 +13187,7 @@
                  original  => 'original (CAPTCHA)',
                  recaptcha => 'successor (ReCAPTCHA)',
                  notused   => 'unused',
+                 ver => 'ReCAPTCHA version (1 or 2)', 
     );
 }
 
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.1233 loncom/interface/loncommon.pm:1.1234
--- loncom/interface/loncommon.pm:1.1233	Wed Feb 17 19:15:48 2016
+++ loncom/interface/loncommon.pm	Fri Feb 19 02:39:07 2016
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1233 2016/02/17 19:15:48 raeburn Exp $
+# $Id: loncommon.pm,v 1.1234 2016/02/19 02:39:07 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -77,6 +77,8 @@
 use Text::Aspell;
 use Authen::Captcha;
 use Captcha::reCAPTCHA;
+use JSON::DWIW;
+use LWP::UserAgent;
 use Crypt::DES;
 use DynaLoader; # for Crypt::DES version
 use MIME::Lite;
@@ -16517,29 +16519,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;
     }
@@ -16548,7 +16551,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);
@@ -16564,6 +16567,10 @@
                     }
                     if ($privkey && $pubkey) {
                         $captcha = 'recaptcha';
+                        $version = $hashtocheck->{'recaptchaversion'};
+                        if ($version ne '2') {
+                            $version = 1;
+                        }
                     } else {
                         $captcha = 'original';
                     }
@@ -16581,6 +16588,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';
             }
@@ -16588,7 +16599,7 @@
             $captcha = 'original';
         }
     }
-    return ($captcha,$pubkey,$privkey);
+    return ($captcha,$pubkey,$privkey,$version);
 }
 
 sub create_captcha {
@@ -16647,32 +16658,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;
 }
Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.408 loncom/interface/loncreateuser.pm:1.409
--- loncom/interface/loncreateuser.pm:1.408	Wed Feb 17 19:15:48 2016
+++ loncom/interface/loncreateuser.pm	Fri Feb 19 02:39:07 2016
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.408 2016/02/17 19:15:48 raeburn Exp $
+# $Id: loncreateuser.pm,v 1.409 2016/02/19 02:39:07 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2164,7 +2164,7 @@
 
 sub personal_data_display {
     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray,
-        $now,$captchaform,$emailusername,$usertype) = @_;
+        $now,$captchaform,$emailusername,$usertype,$recaptchaversion) = @_;
     my ($output,%userenv,%canmodify,%canmodify_status);
     my @userinfo = ('firstname','middlename','lastname','generation',
                     'permanentemail','id');
@@ -2352,8 +2352,11 @@
     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
         if ($captchaform) {
             $output .= &Apache::lonhtmlcommon::row_title($lt{'valid'},
-                                                         'LC_pick_box_title')."\n".
-                       $captchaform."\n".'<br /><br />'.
+                                                         'LC_pick_box_title')."\n";
+            if ($recaptchaversion >=2) {
+                $output .= '<script src="https://www.google.com/recaptcha/api.js"></script>'."\n";
+            }
+            $output .= $captchaform."\n".'<br /><br />'.
                        &Apache::lonhtmlcommon::row_closure(1); 
             $rowcount ++;
         }
Index: loncom/interface/lonsupportreq.pm
diff -u loncom/interface/lonsupportreq.pm:1.81 loncom/interface/lonsupportreq.pm:1.82
--- loncom/interface/lonsupportreq.pm:1.81	Tue Jun 23 02:42:34 2015
+++ loncom/interface/lonsupportreq.pm	Fri Feb 19 02:39:07 2016
@@ -1,5 +1,5 @@
 #
-# $Id: lonsupportreq.pm,v 1.81 2015/06/23 02:42:34 musolffc Exp $
+# $Id: lonsupportreq.pm,v 1.82 2016/02/19 02:39:07 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -82,7 +82,8 @@
     my ($r,$origurl,$function) = @_;
     my ($os,$browser,$bversion,$uhost,$uname,$udom,$uhome,$urole,$usec,$email,$cid,
         $cdom,$cnum,$ctitle,$ccode,$sectionlist,$lastname,$firstname,$server,
-        $formname,$public,$homeserver);
+        $formname,$public,$homeserver,$knownuser,$captcha_form,$captcha_error,
+        $captcha,$recaptcha_version);
     $function = &Apache::loncommon::get_users_function() if (!$function);
     $ccode = '';
     $os = $env{'browser.os'};
@@ -103,6 +104,14 @@
             }
         }
     }
+    if (($env{'user.name'} =~ /^$match_username$/) && 
+        ($env{'user.domain'} =~ /^$match_domain$/) && (!$public)) {
+        $knownuser = 1;
+    } else {
+        my $lonhost = $r->dir_config('lonHostID');
+        ($captcha_form,$captcha_error,$captcha,$recaptcha_version) =
+            &Apache::loncommon::captcha_display('login',$lonhost);
+    }
     if ($homeserver) {
         $uhome = $env{'user.home'};
         $urole = $env{'request.role'};
@@ -275,6 +284,9 @@
 </script>
 <script type="text/javascript" src="/res/adm/includes/file_upload.js"></script>
 ENDJS
+    if ($recaptcha_version >=2) {
+        $js.= "\n".'<script src="https://www.google.com/recaptcha/api.js"></script>'."\n";
+    }
     my %add_entries = (
                        style    => "margin-top:0px;margin-bottom:0px;",
                        onload   => "initialize_codes();",
@@ -328,8 +340,8 @@
                &Apache::lonhtmlcommon::row_closure();
     $num ++;
     $i = $num%2;
-    if (($env{'user.name'} =~ /^$match_username$/) && (!$public)) {
-        if ($homeserver) { 
+    if ($knownuser) {
+        if ($homeserver) {
             $output .= &Apache::lonhtmlcommon::row_title($html_lt{'emac'},undef,$css[$i]).
                        '<input type="text" size="50" name="cc" value="" /><br />'."\n".
                        &Apache::lonhtmlcommon::row_closure();
@@ -342,8 +354,7 @@
                      &HTML::Entities::encode($udom,'"<>&').'" />'."\n";
     my $uname_input = '<input type="hidden" name="uname" value="'.
                       &HTML::Entities::encode($uname,'"<>&').'" />'."\n"; 
-    if (($env{'user.name'} =~ /^$match_username$/) && 
-        ($env{'user.domain'} =~ /^$match_domain$/) && (!$public)) {
+    if ($knownuser) {
         $output .= '<i>'.$html_lt{'unme'}.'</i>: '.$uname.'  <i>'.$html_lt{'doma'}.'</i>: '.$udom.$udom_input.$uname_input;
     } else {
         my $udomform = '';
@@ -503,7 +514,7 @@
                &Apache::lonhtmlcommon::row_closure();
     $num ++;
     $i = $num%2; 
-    if (($env{'user.name'} =~ /^$match_username$/) && (!$public)) {
+    if ($knownuser) {
         if ($homeserver) {
             $output .= &Apache::lonhtmlcommon::row_title($html_lt{'opfi'},undef,$css[$i])
                 .' <input type="file" name="screenshot" class="flUpload" size="20" />'
@@ -514,16 +525,13 @@
             $i = $num%2;
         }
     } else {
-        my $lonhost = $r->dir_config('lonHostID');
-        my ($captchaform,$error) = 
-            &Apache::loncommon::captcha_display('login',$lonhost);
-        if ($captchaform) {
+        if ($captcha_form) {
             $output .= &Apache::lonhtmlcommon::row_title(
                            '<span title="'.&mt('required').'">'.
                            &mt('Validation').
                            ' <span class="LC_info">*</span></span>'
                       ,undef,$css[$i]).
-                       $captchaform."\n".
+                       $captcha_form."\n".
                        &Apache::lonhtmlcommon::row_closure();
             $num ++;
             $i = $num%2;


More information about the LON-CAPA-cvs mailing list