Diff for /loncom/lonnet/perl/lonnet.pm between versions 1.976.4.2 and 1.977

version 1.976.4.2, 2009/09/16 20:10:32 version 1.977, 2008/12/09 11:32:03
Line 73  package Apache::lonnet; Line 73  package Apache::lonnet;
 use strict;  use strict;
 use LWP::UserAgent();  use LWP::UserAgent();
 use HTTP::Date;  use HTTP::Date;
   use Image::Magick;
   
 # use Date::Parse;  # use Date::Parse;
 use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir  use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
             $_64bit %env %protocol);              $_64bit %env %protocol);
Line 97  use LONCAPA::Configuration; Line 99  use LONCAPA::Configuration;
 my $readit;  my $readit;
 my $max_connection_retries = 10;     # Or some such value.  my $max_connection_retries = 10;     # Or some such value.
   
   my $upload_photo_form = 0; #Variable to check  when user upload a photo 0=not 1=true
   
 require Exporter;  require Exporter;
   
 our @ISA = qw (Exporter);  our @ISA = qw (Exporter);
Line 177  sub create_connection { Line 181  sub create_connection {
     return 0;      return 0;
 }  }
   
 sub get_server_timezone {  
     my ($cnum,$cdom) = @_;  
     my $home=&homeserver($cnum,$cdom);  
     if ($home ne 'no_host') {  
         my $cachetime = 24*3600;  
         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);  
         if (defined($cached)) {  
             return $timezone;  
         } else {  
             my $timezone = &reply('servertimezone',$home);  
             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);  
         }  
     }  
 }  
   
 # -------------------------------------------------- Non-critical communication  # -------------------------------------------------- Non-critical communication
 sub subreply {  sub subreply {
Line 522  sub appenv { Line 512  sub appenv {
 # ----------------------------------------------------- Delete from Environment  # ----------------------------------------------------- Delete from Environment
   
 sub delenv {  sub delenv {
     my ($delthis,$regexp) = @_;      my $delthis=shift;
     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {      if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
         &logthis("<font color=\"blue\">WARNING: ".          &logthis("<font color=\"blue\">WARNING: ".
                 "Attempt to delete from environment ".$delthis);                  "Attempt to delete from environment ".$delthis);
Line 535  sub delenv { Line 525  sub delenv {
  tie(my %disk_env,'GDBM_File',$env{'user.environment'},   tie(my %disk_env,'GDBM_File',$env{'user.environment'},
     (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {      (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  foreach my $key (keys(%disk_env)) {   foreach my $key (keys(%disk_env)) {
             if ($regexp) {      if ($key=~/^$delthis/) { 
                 if ($key=~/^$delthis/) {   delete($env{$key});
                     delete($env{$key});   delete($disk_env{$key});
                     delete($disk_env{$key});      }
                 }  
             } else {  
                 if ($key=~/^\Q$delthis\E/) {  
                     delete($env{$key});  
                     delete($disk_env{$key});  
                 }  
             }  
  }   }
  untie(%disk_env);   untie(%disk_env);
     }      }
Line 1248  sub inst_userrules { Line 1231  sub inst_userrules {
 sub get_domain_defaults {  sub get_domain_defaults {
     my ($domain) = @_;      my ($domain) = @_;
     my $cachetime = 60*60*24;      my $cachetime = 60*60*24;
       my ($defauthtype,$defautharg,$deflang,%deftools);
     my ($result,$cached)=&is_cached_new('domdefaults',$domain);      my ($result,$cached)=&is_cached_new('domdefaults',$domain);
     if (defined($cached)) {      if (defined($cached)) {
         if (ref($result) eq 'HASH') {          if (ref($result) eq 'HASH') {
Line 1261  sub get_domain_defaults { Line 1245  sub get_domain_defaults {
         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'};           $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};          $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};          $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};  
         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};  
     } else {      } else {
         $domdefaults{'lang_def'} = &domain($domain,'lang_def');          $domdefaults{'lang_def'} = &domain($domain,'lang_def');
         $domdefaults{'auth_def'} = &domain($domain,'auth_def');          $domdefaults{'auth_def'} = &domain($domain,'auth_def');
Line 1809  sub ssi_body { Line 1791  sub ssi_body {
     }      }
     my $output='';      my $output='';
     my $response;      my $response;
     if ($filelink=~/^https?\:/) {      if ($filelink=~/^http\:/) {
        ($output,$response)=&externalssi($filelink);         ($output,$response)=&externalssi($filelink);
     } else {      } else {
        ($output,$response)=&ssi($filelink,%form);         ($output,$response)=&ssi($filelink,%form);
Line 2033  sub clean_filename { Line 2015  sub clean_filename {
     return $fname;      return $fname;
 }  }
   
   #Wrapper function for userphotoupload
   sub userphotoupload
   {
    my($formname,$subdir) = @_;
    $upload_photo_form = 1;
    return &userfileupload($formname,undef,$subdir);
   }
   
 # --------------- Take an uploaded file and put it into the userfiles directory  # --------------- Take an uploaded file and put it into the userfiles directory
 # input: $formname - the contents of the file are in $env{"form.$formname"}  # input: $formname - the contents of the file are in $env{"form.$formname"}
 #                    the desired filenam is in $env{"form.$formname.filename"}  #                    the desired filenam is in $env{"form.$formname.filename"}
Line 2159  sub finishuserfileupload { Line 2149  sub finishuserfileupload {
     return '/adm/notfound.html';      return '/adm/notfound.html';
  }   }
  close(FH);   close(FH);
    if($upload_photo_form==1)
    {
    my $ima = Image::Magick->new;                       
               $ima->Read($filepath.'/'.$file);
    if($ima->Get('width') > 300)
    {
    my $factor = $ima->Get('width')/300;
                 $ima->Scale( width=>300, height=>$ima->Get('height')/$factor );
    }
    if($ima->Get('height') > 400)
                   {
                           my $factor = $ima->Get('height')/400;
                           $ima->Scale( width=>$ima->Get('width')/$factor, height=>400);
                   }
    
   
    $ima->Write($filepath.'/'.$file);
    $upload_photo_form = 0;
    }
     }      }
     if ($parser eq 'parse') {      if ($parser eq 'parse') {
         my $parse_result = &extract_embedded_items($filepath.'/'.$file,$allfiles,          my $parse_result = &extract_embedded_items($filepath.'/'.$file,$allfiles,
Line 2624  sub courserolelog { Line 2633  sub courserolelog {
                 $storehash{'section'} = $sec;                  $storehash{'section'} = $sec;
             }              }
             &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom);              &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom);
             if (($trole ne 'st') || ($sec ne '')) {  
                 &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);  
             }  
         }          }
     }      }
     return;      return;
Line 2636  sub get_course_adv_roles { Line 2642  sub get_course_adv_roles {
     my ($cid,$codes) = @_;      my ($cid,$codes) = @_;
     $cid=$env{'request.course.id'} unless (defined($cid));      $cid=$env{'request.course.id'} unless (defined($cid));
     my %coursehash=&coursedescription($cid);      my %coursehash=&coursedescription($cid);
     my $crstype = &Apache::loncommon::course_type($cid);  
     my %nothide=();      my %nothide=();
     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {      foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
         if ($user !~ /:/) {          if ($user !~ /:/) {
Line 2649  sub get_course_adv_roles { Line 2654  sub get_course_adv_roles {
     my %dumphash=      my %dumphash=
             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});              &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
     my $now=time;      my $now=time;
     my %privileged;  
     foreach my $entry (keys %dumphash) {      foreach my $entry (keys %dumphash) {
  my ($tend,$tstart)=split(/\:/,$dumphash{$entry});   my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
         if (($tstart) && ($tstart<0)) { next; }          if (($tstart) && ($tstart<0)) { next; }
Line 2657  sub get_course_adv_roles { Line 2661  sub get_course_adv_roles {
         if (($tstart) && ($now<$tstart)) { next; }          if (($tstart) && ($now<$tstart)) { next; }
         my ($role,$username,$domain,$section)=split(/\:/,$entry);          my ($role,$username,$domain,$section)=split(/\:/,$entry);
  if ($username eq '' || $domain eq '') { next; }   if ($username eq '' || $domain eq '') { next; }
         unless (ref($privileged{$domain}) eq 'HASH') {   if ((&privileged($username,$domain)) && 
             my %dompersonnel =      (!$nothide{$username.':'.$domain})) { next; }
                 &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);  
             $privileged{$domain} = {};  
             foreach my $server (keys(%dompersonnel)) {  
                 if (ref($dompersonnel{$server}) eq 'HASH') {  
                     foreach my $user (keys(%{$dompersonnel{$server}})) {  
                         my ($trole,$uname,$udom) = split(/:/,$user);  
                         $privileged{$udom}{$uname} = 1;  
                     }  
                 }  
             }  
         }  
         if ((exists($privileged{$domain}{$username})) &&  
             (!$nothide{$username.':'.$domain})) { next; }  
  if ($role eq 'cr') { next; }   if ($role eq 'cr') { next; }
         if ($codes) {          if ($codes) {
             if ($section) { $role .= ':'.$section; }              if ($section) { $role .= ':'.$section; }
Line 2681  sub get_course_adv_roles { Line 2672  sub get_course_adv_roles {
                 $returnhash{$role}=$username.':'.$domain;                  $returnhash{$role}=$username.':'.$domain;
             }              }
         } else {          } else {
             my $key=&plaintext($role,$crstype);              my $key=&plaintext($role);
             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }              if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
             if ($returnhash{$key}) {              if ($returnhash{$key}) {
         $returnhash{$key}.=','.$username.':'.$domain;          $returnhash{$key}.=','.$username.':'.$domain;
Line 2716  sub get_my_roles { Line 2707  sub get_my_roles {
     }      }
     my %returnhash=();      my %returnhash=();
     my $now=time;      my $now=time;
     my %privileged;  
     foreach my $entry (keys(%dumphash)) {      foreach my $entry (keys(%dumphash)) {
         my ($role,$tend,$tstart);          my ($role,$tend,$tstart);
         if ($context eq 'userroles') {          if ($context eq 'userroles') {
Line 2765  sub get_my_roles { Line 2755  sub get_my_roles {
             }              }
         }          }
         if ($hidepriv) {          if ($hidepriv) {
             if ($context eq 'userroles') {              if ((&privileged($username,$domain)) &&
                 if ((&privileged($username,$domain)) &&                  (!$nothide{$username.':'.$domain})) { 
                     (!$nothide{$username.':'.$domain})) {                  next;
                     next;  
                 }  
             } else {  
                 unless (ref($privileged{$domain}) eq 'HASH') {  
                     my %dompersonnel =  
                         &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);  
                     $privileged{$domain} = {};  
                     if (keys(%dompersonnel)) {  
                         foreach my $server (keys(%dompersonnel)) {  
                             if (ref($dompersonnel{$server}) eq 'HASH') {  
                                 foreach my $user (keys(%{$dompersonnel{$server}})) {  
                                     my ($trole,$uname,$udom) = split(/:/,$user);  
                                     $privileged{$udom}{$uname} = $trole;  
                                 }  
                             }  
                         }  
                     }  
                 }  
                 if (exists($privileged{$domain}{$username})) {  
                     if (!$nothide{$username.':'.$domain}) {  
                         next;  
                     }  
                 }  
             }              }
         }          }
         if ($withsec) {          if ($withsec) {
Line 4439  sub is_portfolio_file { Line 4406  sub is_portfolio_file {
 }  }
   
 sub usertools_access {  sub usertools_access {
     my ($uname,$udom,$tool,$action) = @_;      my ($uname,$udom,$tool) = @_;
     my $access;      my $access;
     my %tools = (      my %tools = (
                   aboutme   => 1,                    aboutme   => 1,
Line 4453  sub usertools_access { Line 4420  sub usertools_access {
         $uname = $env{'user.name'};          $uname = $env{'user.name'};
     }      }
   
     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {      my $hashid=$uname.':'.$udom;
         if ($action ne 'reload') {      my ($result,$cached) = &is_cached_new('usertools.'.$tool,$hashid);
             return $env{'environment.availabletools.'.$tool};      if (defined($cached)) {
         }          return $result;
     }      }
   
     my ($toolstatus,$inststatus);      my ($toolstatus,$inststatus);
Line 4476  sub usertools_access { Line 4443  sub usertools_access {
         } else {          } else {
             $access = 0;              $access = 0;
         }          }
           &do_cache_new('usertools.'.$tool,$hashid,$access,600);
         return $access;          return $access;
     }      }
   
Line 4489  sub usertools_access { Line 4457  sub usertools_access {
                 } else {                  } else {
                     $access = 0;                      $access = 0;
                 }                  }
                   &do_cache_new('usertools.'.$tool,$hashid,$access,600);
                 return $access;                  return $access;
             }              }
         }          }
Line 4509  sub usertools_access { Line 4478  sub usertools_access {
                 } elsif ($hasnoaccess) {                  } elsif ($hasnoaccess) {
                     $access = 0;                       $access = 0; 
                 }                  }
                   &do_cache_new('usertools.'.$tool,$hashid,$access,600);
                 return $access;                  return $access;
             }              }
         } else {          } else {
Line 4518  sub usertools_access { Line 4488  sub usertools_access {
                 } elsif ($domdef{$tool}{'default'} == 0) {                  } elsif ($domdef{$tool}{'default'} == 0) {
                     $access = 0;                      $access = 0;
                 }                  }
                   &do_cache_new('usertools.'.$tool,$hashid,$access,600);
                 return $access;                  return $access;
             }              }
         }          }
     } else {      } else {
         $access = 1;          $access = 1;
           &do_cache_new('usertools.'.$tool,$hashid,$access,600);
         return $access;          return $access;
     }      }
 }  }
Line 5647  sub devalidate_getgroups_cache { Line 5619  sub devalidate_getgroups_cache {
 # ------------------------------------------------------------------ Plain Text  # ------------------------------------------------------------------ Plain Text
   
 sub plaintext {  sub plaintext {
     my ($short,$type,$cid,$forcedefault) = @_;      my ($short,$type,$cid) = @_;
     if ($short =~ /^cr/) {      if ($short =~ /^cr/) {
  return (split('/',$short))[-1];   return (split('/',$short))[-1];
     }      }
     if (!defined($cid)) {      if (!defined($cid)) {
         $cid = $env{'request.course.id'};          $cid = $env{'request.course.id'};
     }      }
     if (defined($cid) && ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '')) {      if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
         unless ($forcedefault) {          return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
             my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'};                                             '.plaintext'});
             &Apache::lonlocal::mt_escape(\$roletext);  
             return &Apache::lonlocal::mt($roletext);  
         }  
     }      }
     my %rolenames = (      my %rolenames = (
                       Course => 'std',                        Course => 'std',
Line 8368  sub repcopy_userfile { Line 8337  sub repcopy_userfile {
     if (-e $transferfile) { return 'ok'; }      if (-e $transferfile) { return 'ok'; }
     my $request;      my $request;
     $uri=~s/^\///;      $uri=~s/^\///;
     my $homeserver = &homeserver($cnum,$cdom);      $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
     my $protocol = $protocol{$homeserver};  
     $protocol = 'http' if ($protocol ne 'https');  
     $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);  
     my $response=$ua->request($request,$transferfile);      my $response=$ua->request($request,$transferfile);
 # did it work?  # did it work?
     if ($response->is_error()) {      if ($response->is_error()) {
Line 8386  sub repcopy_userfile { Line 8352  sub repcopy_userfile {
   
 sub tokenwrapper {  sub tokenwrapper {
     my $uri=shift;      my $uri=shift;
     $uri=~s|^https?\://([^/]+)||;      $uri=~s|^http\://([^/]+)||;
     $uri=~s|^/||;      $uri=~s|^/||;
     $env{'user.environment'}=~/\/([^\/]+)\.id/;      $env{'user.environment'}=~/\/([^\/]+)\.id/;
     my $token=$1;      my $token=$1;
Line 8394  sub tokenwrapper { Line 8360  sub tokenwrapper {
     if ($udom && $uname && $file) {      if ($udom && $uname && $file) {
  $file=~s|(\?\.*)*$||;   $file=~s|(\?\.*)*$||;
         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});          &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
         my $homeserver = &homeserver($uname,$udom);          return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
         my $protocol = $protocol{$homeserver};  
         $protocol = 'http' if ($protocol ne 'https');  
         return $protocol.'://'.&hostname($homeserver).'/'.$uri.  
                (($uri=~/\?/)?'&':'?').'token='.$token.                 (($uri=~/\?/)?'&':'?').'token='.$token.
                                '&tokenissued='.$perlvar{'lonHostID'};                                 '&tokenissued='.$perlvar{'lonHostID'};
     } else {      } else {
Line 8412  sub tokenwrapper { Line 8375  sub tokenwrapper {
 sub getuploaded {  sub getuploaded {
     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;      my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
     $uri=~s/^\///;      $uri=~s/^\///;
     my $homeserver = &homeserver($cnum,$cdom);      $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
     my $protocol = $protocol{$homeserver};  
     $protocol = 'http' if ($protocol ne 'https');  
     $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;  
     my $ua=new LWP::UserAgent;      my $ua=new LWP::UserAgent;
     my $request=new HTTP::Request($reqtype,$uri);      my $request=new HTTP::Request($reqtype,$uri);
     my $response=$ua->request($request);      my $response=$ua->request($request);
Line 8497  sub filelocation { Line 8457  sub filelocation {
   
 sub hreflocation {  sub hreflocation {
     my ($dir,$file)=@_;      my ($dir,$file)=@_;
     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {      unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
  $file=filelocation($dir,$file);   $file=filelocation($dir,$file);
     } elsif ($file=~m-^/adm/-) {      } elsif ($file=~m-^/adm/-) {
  $file=~s-^/adm/wrapper/-/-;   $file=~s-^/adm/wrapper/-/-;
Line 8693  sub get_dns { Line 8653  sub get_dns {
     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");      open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
     foreach my $dns (<$config>) {      foreach my $dns (<$config>) {
  next if ($dns !~ /^\^(\S*)/x);   next if ($dns !~ /^\^(\S*)/x);
         my $line = $1;   $alldns{$1} = 1;
         my ($host,$protocol) = split(/:/,$line);  
         if ($protocol ne 'https') {  
             $protocol = 'http';  
         }  
  $alldns{$host} = $protocol;  
     }      }
     while (%alldns) {      while (%alldns) {
  my ($dns) = keys(%alldns);   my ($dns) = keys(%alldns);
    delete($alldns{$dns});
  my $ua=new LWP::UserAgent;   my $ua=new LWP::UserAgent;
  my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");   my $request=new HTTP::Request('GET',"http://$dns$url");
  my $response=$ua->request($request);   my $response=$ua->request($request);
         delete($alldns{$dns});  
  next if ($response->is_error());   next if ($response->is_error());
  my @content = split("\n",$response->content);   my @content = split("\n",$response->content);
  &Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);   &Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
Line 9280  in the user's environment.db and in %env Line 9235  in the user's environment.db and in %env
   
 =item *  =item *
 X<delenv()>  X<delenv()>
 B<delenv($delthis,$regexp)>: removes all items from the session  B<delenv($regexp)>: removes all items from the session
 environment file that begin with $delthis. If the  environment file that matches the regular expression in $regexp. The
 optional second arg - $regexp - is true, $delthis is treated as a  values are also delted from the current processes %env.
 regular expression, otherwise \Q$delthis\E is used.  
 The values are also deleted from the current processes %env.  
   
 =item * get_env_multiple($name)   =item * get_env_multiple($name) 
   
Line 9381  and course level Line 9334  and course level
   
 =item *  =item *
   
 plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash  plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
 (rolesplain.tab); plain text explanation of a user role term.  explanation of a user role term
 $type is Course (default) or Community.  
 If $forcedefault evaluates to true, text returned will be default  
 text for $type. Otherwise, if this is a course, the text returned  
 will be a custom name for the role (if defined in the course's  
 environment).  If no custom name is defined the default is returned.  
   
 =item *  =item *
   

Removed from v.1.976.4.2  
changed lines
  Added in v.1.977


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>