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

raeburn raeburn at source.lon-capa.org
Mon Aug 27 02:28:06 EDT 2012


raeburn		Mon Aug 27 06:28:06 2012 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm loncommon.pm lonsupportreq.pm 
  Log:
  - "Contact Helpdesk" form (as used by users who are not logged in), can require
     CAPTCHA validation.  Default is not to require CAPTCHA.
  
  
-------------- next part --------------
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.168 loncom/interface/domainprefs.pm:1.169
--- loncom/interface/domainprefs.pm:1.168	Mon Aug 27 00:53:22 2012
+++ loncom/interface/domainprefs.pm	Mon Aug 27 06:28:05 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.168 2012/08/27 00:53:22 raeburn Exp $
+# $Id: domainprefs.pm,v 1.169 2012/08/27 06:28:05 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1054,7 +1054,9 @@
                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
             }
             $datatable .= '</td></tr>';
+            $itemcount ++;
         }
+        $datatable .= &captcha_choice('login',$settings,$itemcount);
     }
     return $datatable;
 }
@@ -3299,9 +3301,10 @@
                                              $dom,$numinrow,$othertitle,
                                              'statustocreate');
                 $$rowtotal ++;
+                $rownum ++;
             }
         }
-        $datatable .= &captcha_choice('cancreate',$createsettings);
+        $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
     } else {
         my @contexts = ('author','course','domain');
         my @authtypes = ('int','krb4','krb5','loc');
@@ -3354,14 +3357,14 @@
 }
 
 sub captcha_choice {
-    my ($context,$settings) = @_;
+    my ($context,$settings,$itemcount) = @_;
     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
     my %lt = &captcha_phrases();
     $keyentry = 'hidden';
     if ($context eq 'cancreate') {
         $rowname = &mt('CAPTCHA validation (e-mail as username)');
-    } elsif ($context eq 'help') {
-        $rowname =  &mt('CAPTCHA validation');
+    } elsif ($context eq 'login') {
+        $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
     }
     if (ref($settings) eq 'HASH') {
         if ($settings->{'captcha'}) {
@@ -3381,8 +3384,9 @@
     } else {
         $checked{'original'} = ' checked="checked"';
     }
-    my $output = '<tr class="LC_odd_row">'.
-                 '<td class="LC_left_item">'.$rowname.'</td><td class="LC_right_item" colspan="2">'."\n".
+    my $css_class = $itemcount%2?' class="LC_odd_row"':'';
+    my $output = '<tr'.$css_class.'>'.
+                 '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
                  '<table><tr><td>'."\n";
     foreach my $option ('original','recaptcha','notused') {
         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
@@ -4613,7 +4617,6 @@
                                 $new = ''; 
                             }
                         }
-
                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
                     }
                 }
@@ -4724,6 +4727,7 @@
             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
         }
     }
+    &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 
     my $defaulthelpfile = '/adm/loginproblems.html';
     my $defaulttext = &mt('Default in use');
@@ -4819,6 +4823,42 @@
                             }
                         }
                     }
+                } elsif ($item eq 'captcha') {
+                    if (ref($loginhash{'login'}) eq 'HASH') {
+                        my $chgtxt; 
+                        if ($loginhash{'login'}{$item} eq 'notused') {
+                            $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
+                        } else {
+                            my %captchas = &captcha_phrases();
+                            if ($captchas{$loginhash{'login'}{$item}}) {
+                                $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
+                            } else {
+                                $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
+                            }
+                        }
+                        $resulttext .= '<li>'.$chgtxt.'</li>';
+                    }
+                } elsif ($item eq 'recaptchakeys') {
+                    if (ref($loginhash{'login'}) eq 'HASH') {
+                        my ($privkey,$pubkey);
+                        if (ref($loginhash{'login'}{$item}) eq 'HASH') {
+                            $pubkey = $loginhash{'login'}{$item}{'public'};
+                            $privkey = $loginhash{'login'}{$item}{'private'};
+                        }
+                        my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
+                        if (!$pubkey) {
+                            $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
+                        } else {
+                            $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
+                        }
+                        if (!$privkey) {
+                            $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
+                        } else {
+                            $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
+                        }
+                        $chgtxt .= '</ul>';
+                        $resulttext .= '<li>'.$chgtxt.'</li>';
+                    }
                 } else {
                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
                 }
@@ -6911,30 +6951,44 @@
         $newsettings->{'captcha'} = 'original';
     }
     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
-        if (ref($changes->{'cancreate'}) eq 'ARRAY') {
-            push(@{$changes->{'cancreate'}},'captcha');
-        } elsif (!defined($changes->{'cancreate'})) {
-            $changes->{'cancreate'} = ['captcha'];
+        if ($container eq 'cancreate') { 
+            if (ref($changes->{'cancreate'}) eq 'ARRAY') {
+                push(@{$changes->{'cancreate'}},'captcha');
+            } elsif (!defined($changes->{'cancreate'})) {
+                $changes->{'cancreate'} = ['captcha'];
+            }
+        } else {
+            $changes->{'captcha'} = 1;
         }
     }
     my ($newpub,$newpriv,$currpub,$currpriv);
     if ($newsettings->{'captcha'} eq 'recaptcha') {
         $newpub = $env{'form.'.$container.'_recaptchapub'};
         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
+        $newpub =~ s/\W//g;
+        $newpriv =~ s/\W//g;
+        $newsettings->{'recaptchakeys'} = {
+                                             public  => $newpub,
+                                             private => $newpriv,
+                                          };
     }
-    $newsettings->{'recaptchakeys'} = {
-                                         public  => $newpub,
-                                         private => $newpriv,
-                                      };
     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
         $currpub = $current->{'recaptchakeys'}{'public'};
         $currpriv = $current->{'recaptchakeys'}{'private'};
+        $newsettings->{'recaptchakeys'} = {
+                                             public  => '',
+                                             private => '',
+                                          }
     }
     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
-        if (ref($changes->{'cancreate'}) eq 'ARRAY') {
-            push(@{$changes->{'cancreate'}},'recaptchakeys');
-        } elsif (!defined($changes->{'cancreate'})) {
-            $changes->{'cancreate'} = ['recaptchakeys'];
+        if ($container eq 'cancreate') {
+            if (ref($changes->{'cancreate'}) eq 'ARRAY') {
+                push(@{$changes->{'cancreate'}},'recaptchakeys');
+            } elsif (!defined($changes->{'cancreate'})) {
+                $changes->{'cancreate'} = ['recaptchakeys'];
+            }
+        } else {
+            $changes->{'recaptchakeys'} = 1; 
         }
     }
     return;
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.1094 loncom/interface/loncommon.pm:1.1095
--- loncom/interface/loncommon.pm:1.1094	Sat Aug 25 04:34:44 2012
+++ loncom/interface/loncommon.pm	Mon Aug 27 06:28:06 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1094 2012/08/25 04:34:44 raeburn Exp $
+# $Id: loncommon.pm,v 1.1095 2012/08/27 06:28:06 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -14070,7 +14070,7 @@
     my ($context,$lonhost) = @_;
     my ($output,$error);
     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
-    if ($captcha eq 'captcha') {
+    if ($captcha eq 'original') {
         $output = &create_captcha();
         unless ($output) {
             $error = 'captcha'; 
@@ -14078,7 +14078,7 @@
     } elsif ($captcha eq 'recaptcha') {
         $output = &create_recaptcha($pubkey);
         unless ($output) {
-            $error = 'recpatcha'; 
+            $error = 'recaptcha'; 
         }
     }
     return ($output,$error);
@@ -14088,7 +14088,7 @@
     my ($context,$lonhost) = @_;
     my ($captcha_chk,$captcha_error);
     my ($captcha,$pubkey,$privkey) = &get_captcha_config($context,$lonhost);
-    if ($captcha eq 'captcha') {
+    if ($captcha eq 'original') {
         ($captcha_chk,$captcha_error) = &check_captcha();
     } elsif ($captcha eq 'recaptcha') {
         $captcha_chk = &check_recaptcha($privkey);
@@ -14100,28 +14100,44 @@
 
 sub get_captcha_config {
     my ($context,$lonhost) = @_;
-    my ($captcha,$pubkey,$privkey);
+    my ($captcha,$pubkey,$privkey,$hashtocheck);
     my $hostname = &Apache::lonnet::hostname($lonhost);
     my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
     my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
-    my %domconfig = &Apache::lonnet::get_dom('configuration',[$context],$serverhomedom);
-    if (ref($domconfig{$context}) eq 'HASH') {
-        if ($domconfig{$context}{'captcha'} eq 'recaptcha') {
-            if (ref($domconfig{$context}{'recaptchakeys'}) eq 'HASH') {
-                $pubkey = $domconfig{$context}{'recaptchakeys'}{'public'};
-                $privkey = $domconfig{$context}{'recaptchakeys'}{'private'};
+    if ($context eq 'usercreation') {
+        my %domconfig = &Apache::lonnet::get_dom('configuration',[$context],$serverhomedom);
+        if (ref($domconfig{$context}) eq 'HASH') {
+            $hashtocheck = $domconfig{$context}{'cancreate'};
+            if (ref($hashtocheck) eq 'HASH') {
+                if ($hashtocheck->{'captcha'} eq 'recaptcha') {
+                    if (ref($hashtocheck->{'recaptchakeys'}) eq 'HASH') {
+                        $pubkey = $hashtocheck->{'recaptchakeys'}{'public'};
+                        $privkey = $hashtocheck->{'recaptchakeys'}{'private'};
+                    }
+                    if ($privkey && $pubkey) {
+                        $captcha = 'recaptcha';
+                    } else {
+                        $captcha = 'original';
+                    }
+                } elsif ($hashtocheck->{'captcha'} ne 'notused') {
+                    $captcha = 'original';
+                }
             }
+        } else {
+            $captcha = 'captcha';
+        }
+    } elsif ($context eq 'login') {
+        my %domconfhash = &Apache::loncommon::get_domainconf($serverhomedom);
+        if ($domconfhash{$serverhomedom.'.login.captcha'} eq 'recaptcha') {
+            $pubkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_public'};
+            $privkey = $domconfhash{$serverhomedom.'.login.recaptchakeys_private'};
             if ($privkey && $pubkey) {
                 $captcha = 'recaptcha';
+            } else {
+                $captcha = 'original';
             }
-        } elsif ($domconfig{$context}{'captcha'} eq 'notused') {
-            $captcha = '';
-        } elsif ($domconfig{$context}{'captcha'} eq 'captcha') {
-            $captcha = 'captcha';
-        } else {
-            if ($context eq 'usercreation') {
-                $captcha = 'captcha';
-            }
+        } elsif ($domconfhash{$serverhomedom.'.login.captcha'} eq 'original') {
+            $captcha = 'original';
         }
     }
     return ($captcha,$pubkey,$privkey);
Index: loncom/interface/lonsupportreq.pm
diff -u loncom/interface/lonsupportreq.pm:1.68 loncom/interface/lonsupportreq.pm:1.69
--- loncom/interface/lonsupportreq.pm:1.68	Mon Aug 27 02:02:57 2012
+++ loncom/interface/lonsupportreq.pm	Mon Aug 27 06:28:06 2012
@@ -1,5 +1,5 @@
 #
-# $Id: lonsupportreq.pm,v 1.68 2012/08/27 02:02:57 raeburn Exp $
+# $Id: lonsupportreq.pm,v 1.69 2012/08/27 06:28:06 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -488,6 +488,18 @@
             $num ++;
             $i = $num%2;
         }
+    } else {
+        my $lonhost = $r->dir_config('lonHostID');
+        my ($captchaform,$error) = 
+            &Apache::loncommon::captcha_display('login',$lonhost);
+        if ($captchaform) {
+            $output .= &Apache::lonhtmlcommon::row_title(&mt('Validation'),undef,
+                                                         $css[$i])."\n".
+                       $captchaform."\n".
+                       &Apache::lonhtmlcommon::row_closure();
+            $num ++;
+            $i = $num%2;
+        }
     }
     $output .= &Apache::lonhtmlcommon::row_title($lt{'fini'},undef,$css[$i]);
     $output .= <<END;
@@ -517,6 +529,32 @@
 
 sub print_request_receipt {
     my ($r,$url,$function) = @_;
+    my $public;
+    if (($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public')) {
+        $public = 1;
+    }
+    unless (($env{'user.name'} =~ /^$match_username$/) && (!$public)) {
+        my $lonhost = $r->dir_config('lonHostID');
+        my ($captcha_chk,$captcha_error) = 
+            &Apache::loncommon::captcha_response('login',$lonhost);
+        if ($captcha_chk != 1) {
+            $r->print(&Apache::loncommon::start_page('Support request failed',undef,
+                                       {'function'    => $function,
+                                        'add_entries' => {
+                                            topmargin    => "0",
+                                            marginheight => "0",
+                                        },
+                                        'only_body'   => 1,}));
+            if ($r->uri eq '/adm/helpdesk') {
+                &print_header($r,$url,'process');
+            }
+            $r->print('<h3>'.&mt('Support request failed').'</h3>'.
+                      '<span class="LC_error">'.
+                      &mt('Validation of the code you entered failed.').
+                      &Apache::loncommon::end_page());
+            return;
+        }
+    }
     my @ENVvars = ('HTTP_HOST','HTTP_USER_AGENT','REMOTE_ADDR','SERVER_ADDR','SERVER_NAME');
     my @envvars = ('browser.os','browser.type','browser.version','user.home','request.role');
     my @loncvars = ('user.name','user.domain','request.course.sec','request.course.id');


More information about the LON-CAPA-cvs mailing list