--- loncom/lonnet/perl/lonnet.pm 2009/09/16 20:10:32 1.976.4.2 +++ loncom/lonnet/perl/lonnet.pm 2008/12/09 11:32:03 1.977 @@ -1,7 +1,7 @@ # The LearningOnline Network # TCP networking package # -# $Id: lonnet.pm,v 1.976.4.2 2009/09/16 20:10:32 raeburn Exp $ +# $Id: lonnet.pm,v 1.977 2008/12/09 11:32:03 amueller Exp $ # # Copyright Michigan State University Board of Trustees # @@ -73,6 +73,8 @@ package Apache::lonnet; use strict; use LWP::UserAgent(); use HTTP::Date; +use Image::Magick; + # use Date::Parse; use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $_64bit %env %protocol); @@ -97,6 +99,8 @@ use LONCAPA::Configuration; my $readit; 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; our @ISA = qw (Exporter); @@ -177,20 +181,6 @@ sub create_connection { 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 sub subreply { @@ -522,7 +512,7 @@ sub appenv { # ----------------------------------------------------- Delete from Environment sub delenv { - my ($delthis,$regexp) = @_; + my $delthis=shift; if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) { &logthis("WARNING: ". "Attempt to delete from environment ".$delthis); @@ -535,17 +525,10 @@ sub delenv { tie(my %disk_env,'GDBM_File',$env{'user.environment'}, (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) { foreach my $key (keys(%disk_env)) { - if ($regexp) { - if ($key=~/^$delthis/) { - delete($env{$key}); - delete($disk_env{$key}); - } - } else { - if ($key=~/^\Q$delthis\E/) { - delete($env{$key}); - delete($disk_env{$key}); - } - } + if ($key=~/^$delthis/) { + delete($env{$key}); + delete($disk_env{$key}); + } } untie(%disk_env); } @@ -1248,6 +1231,7 @@ sub inst_userrules { sub get_domain_defaults { my ($domain) = @_; my $cachetime = 60*60*24; + my ($defauthtype,$defautharg,$deflang,%deftools); my ($result,$cached)=&is_cached_new('domdefaults',$domain); if (defined($cached)) { if (ref($result) eq 'HASH') { @@ -1261,8 +1245,6 @@ sub get_domain_defaults { $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_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 { $domdefaults{'lang_def'} = &domain($domain,'lang_def'); $domdefaults{'auth_def'} = &domain($domain,'auth_def'); @@ -1809,7 +1791,7 @@ sub ssi_body { } my $output=''; my $response; - if ($filelink=~/^https?\:/) { + if ($filelink=~/^http\:/) { ($output,$response)=&externalssi($filelink); } else { ($output,$response)=&ssi($filelink,%form); @@ -2033,6 +2015,14 @@ sub clean_filename { 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 # input: $formname - the contents of the file are in $env{"form.$formname"} # the desired filenam is in $env{"form.$formname.filename"} @@ -2159,6 +2149,25 @@ sub finishuserfileupload { return '/adm/notfound.html'; } 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') { my $parse_result = &extract_embedded_items($filepath.'/'.$file,$allfiles, @@ -2624,9 +2633,6 @@ sub courserolelog { $storehash{'section'} = $sec; } &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom); - if (($trole ne 'st') || ($sec ne '')) { - &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum); - } } } return; @@ -2636,7 +2642,6 @@ sub get_course_adv_roles { my ($cid,$codes) = @_; $cid=$env{'request.course.id'} unless (defined($cid)); my %coursehash=&coursedescription($cid); - my $crstype = &Apache::loncommon::course_type($cid); my %nothide=(); foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) { if ($user !~ /:/) { @@ -2649,7 +2654,6 @@ sub get_course_adv_roles { my %dumphash= &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'}); my $now=time; - my %privileged; foreach my $entry (keys %dumphash) { my ($tend,$tstart)=split(/\:/,$dumphash{$entry}); if (($tstart) && ($tstart<0)) { next; } @@ -2657,21 +2661,8 @@ sub get_course_adv_roles { if (($tstart) && ($now<$tstart)) { next; } my ($role,$username,$domain,$section)=split(/\:/,$entry); if ($username eq '' || $domain eq '') { next; } - unless (ref($privileged{$domain}) eq 'HASH') { - my %dompersonnel = - &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 ((&privileged($username,$domain)) && + (!$nothide{$username.':'.$domain})) { next; } if ($role eq 'cr') { next; } if ($codes) { if ($section) { $role .= ':'.$section; } @@ -2681,7 +2672,7 @@ sub get_course_adv_roles { $returnhash{$role}=$username.':'.$domain; } } else { - my $key=&plaintext($role,$crstype); + my $key=&plaintext($role); if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; } if ($returnhash{$key}) { $returnhash{$key}.=','.$username.':'.$domain; @@ -2716,7 +2707,6 @@ sub get_my_roles { } my %returnhash=(); my $now=time; - my %privileged; foreach my $entry (keys(%dumphash)) { my ($role,$tend,$tstart); if ($context eq 'userroles') { @@ -2765,32 +2755,9 @@ sub get_my_roles { } } if ($hidepriv) { - if ($context eq 'userroles') { - if ((&privileged($username,$domain)) && - (!$nothide{$username.':'.$domain})) { - 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 ((&privileged($username,$domain)) && + (!$nothide{$username.':'.$domain})) { + next; } } if ($withsec) { @@ -4439,7 +4406,7 @@ sub is_portfolio_file { } sub usertools_access { - my ($uname,$udom,$tool,$action) = @_; + my ($uname,$udom,$tool) = @_; my $access; my %tools = ( aboutme => 1, @@ -4453,10 +4420,10 @@ sub usertools_access { $uname = $env{'user.name'}; } - if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) { - if ($action ne 'reload') { - return $env{'environment.availabletools.'.$tool}; - } + my $hashid=$uname.':'.$udom; + my ($result,$cached) = &is_cached_new('usertools.'.$tool,$hashid); + if (defined($cached)) { + return $result; } my ($toolstatus,$inststatus); @@ -4476,6 +4443,7 @@ sub usertools_access { } else { $access = 0; } + &do_cache_new('usertools.'.$tool,$hashid,$access,600); return $access; } @@ -4489,6 +4457,7 @@ sub usertools_access { } else { $access = 0; } + &do_cache_new('usertools.'.$tool,$hashid,$access,600); return $access; } } @@ -4509,6 +4478,7 @@ sub usertools_access { } elsif ($hasnoaccess) { $access = 0; } + &do_cache_new('usertools.'.$tool,$hashid,$access,600); return $access; } } else { @@ -4518,11 +4488,13 @@ sub usertools_access { } elsif ($domdef{$tool}{'default'} == 0) { $access = 0; } + &do_cache_new('usertools.'.$tool,$hashid,$access,600); return $access; } } } else { $access = 1; + &do_cache_new('usertools.'.$tool,$hashid,$access,600); return $access; } } @@ -5647,19 +5619,16 @@ sub devalidate_getgroups_cache { # ------------------------------------------------------------------ Plain Text sub plaintext { - my ($short,$type,$cid,$forcedefault) = @_; + my ($short,$type,$cid) = @_; if ($short =~ /^cr/) { return (split('/',$short))[-1]; } if (!defined($cid)) { $cid = $env{'request.course.id'}; } - if (defined($cid) && ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '')) { - unless ($forcedefault) { - my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; - &Apache::lonlocal::mt_escape(\$roletext); - return &Apache::lonlocal::mt($roletext); - } + if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) { + return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short. + '.plaintext'}); } my %rolenames = ( Course => 'std', @@ -8368,10 +8337,7 @@ sub repcopy_userfile { if (-e $transferfile) { return 'ok'; } my $request; $uri=~s/^\///; - my $homeserver = &homeserver($cnum,$cdom); - my $protocol = $protocol{$homeserver}; - $protocol = 'http' if ($protocol ne 'https'); - $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri); + $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri); my $response=$ua->request($request,$transferfile); # did it work? if ($response->is_error()) { @@ -8386,7 +8352,7 @@ sub repcopy_userfile { sub tokenwrapper { my $uri=shift; - $uri=~s|^https?\://([^/]+)||; + $uri=~s|^http\://([^/]+)||; $uri=~s|^/||; $env{'user.environment'}=~/\/([^\/]+)\.id/; my $token=$1; @@ -8394,10 +8360,7 @@ sub tokenwrapper { if ($udom && $uname && $file) { $file=~s|(\?\.*)*$||; &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}}); - my $homeserver = &homeserver($uname,$udom); - my $protocol = $protocol{$homeserver}; - $protocol = 'http' if ($protocol ne 'https'); - return $protocol.'://'.&hostname($homeserver).'/'.$uri. + return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri. (($uri=~/\?/)?'&':'?').'token='.$token. '&tokenissued='.$perlvar{'lonHostID'}; } else { @@ -8412,10 +8375,7 @@ sub tokenwrapper { sub getuploaded { my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_; $uri=~s/^\///; - my $homeserver = &homeserver($cnum,$cdom); - my $protocol = $protocol{$homeserver}; - $protocol = 'http' if ($protocol ne 'https'); - $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri; + $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri; my $ua=new LWP::UserAgent; my $request=new HTTP::Request($reqtype,$uri); my $response=$ua->request($request); @@ -8497,7 +8457,7 @@ sub filelocation { sub hreflocation { my ($dir,$file)=@_; - unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) { + unless (($file=~m-^http://-i) || ($file=~m-^/-)) { $file=filelocation($dir,$file); } elsif ($file=~m-^/adm/-) { $file=~s-^/adm/wrapper/-/-; @@ -8693,19 +8653,14 @@ sub get_dns { open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab"); foreach my $dns (<$config>) { next if ($dns !~ /^\^(\S*)/x); - my $line = $1; - my ($host,$protocol) = split(/:/,$line); - if ($protocol ne 'https') { - $protocol = 'http'; - } - $alldns{$host} = $protocol; + $alldns{$1} = 1; } while (%alldns) { my ($dns) = keys(%alldns); + delete($alldns{$dns}); 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); - delete($alldns{$dns}); next if ($response->is_error()); my @content = split("\n",$response->content); &Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60); @@ -9280,11 +9235,9 @@ in the user's environment.db and in %env =item * X -B: removes all items from the session -environment file that begin with $delthis. If the -optional second arg - $regexp - is true, $delthis is treated as a -regular expression, otherwise \Q$delthis\E is used. -The values are also deleted from the current processes %env. +B: removes all items from the session +environment file that matches the regular expression in $regexp. The +values are also delted from the current processes %env. =item * get_env_multiple($name) @@ -9381,13 +9334,8 @@ and course level =item * -plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash -(rolesplain.tab); plain text 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. +plaintext($short) : return value in %prp hash (rolesplain.tab); plain text +explanation of a user role term =item *