--- loncom/lonsql 2007/09/12 03:40:29 1.88 +++ loncom/lonsql 2013/12/01 21:29:07 1.93 @@ -3,7 +3,7 @@ # The LearningOnline Network # lonsql - LON TCP-MySQL-Server Daemon for handling database requests. # -# $Id: lonsql,v 1.88 2007/09/12 03:40:29 raeburn Exp $ +# $Id: lonsql,v 1.93 2013/12/01 21:29:07 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -95,8 +95,6 @@ the database. =head1 Internals -=over 4 - =cut use strict; @@ -121,6 +119,8 @@ use GDBM_File; =pod +=over 4 + =item Global Variables =over 4 @@ -233,13 +233,13 @@ unless ($dbh = DBI->connect("DBI:mysql:l system("echo 'Cannot connect to MySQL database!' |". " mailto $emailto -s '$subj' > /dev/null"); - open(SMP,'>/home/httpd/html/lon-status/mysql.txt'); + open(SMP,">$perlvar{'lonDocRoot'}/lon-status/mysql.txt"); print SMP 'time='.time.'&mysql=defunct'."\n"; close(SMP); exit 1; } else { - unlink('/home/httpd/html/lon-status/mysql.txt'); + unlink("$perlvar{'lonDocRoot'}/lon-status/mysql.txt"); $dbh->disconnect; } @@ -447,7 +447,7 @@ sub make_new_child { ($arg2 =~ /\%\%/)) { ($srchby,$srchtype) = map {&unescape($_);} (split(/\%\%/,$arg2)); - my $srchterm = &unescape($arg3); + $srchterm = &unescape($arg3); } else { ($srchby,$srchtype,$srchterm) = map {&unescape($_);} ($arg1,$arg2,$arg3); @@ -456,6 +456,8 @@ sub make_new_child { $srchtype,$srchterm); } elsif ($query eq 'instdirsearch') { $result = &do_inst_dir_search($searchdomain,$arg1,$arg2,$arg3); + } elsif ($query eq 'getinstuser') { + $result = &get_inst_user($searchdomain,$arg1,$arg2); } elsif ($query eq 'prepare activity log') { my ($cid,$domain) = map {&unescape($_);} ($arg1,$arg2); &logthis('preparing activity log tables for '.$cid); @@ -487,7 +489,7 @@ sub make_new_child { $result = &allusers_table_update($query,$uname,$udom,\%userdata); } else { # Do an sql query - $result = &do_sql_query($query,$arg1,$arg2,$searchdomain); + $result = &do_sql_query($query,$arg1,$arg2,$arg3,$searchdomain); } # result does not need to be escaped because it has already been # escaped. @@ -594,6 +596,30 @@ sub do_inst_dir_search { return $response; } +sub get_inst_user { + my ($domain,$uname,$id) = @_; + $uname = &unescape($uname); + $id = &unescape($id); + my (%instusers,%instids,$result,$response); + eval { + local($SIG{__DIE__})='DEFAULT'; + $result=&localenroll::get_userinfo($domain,$uname,$id,\%instusers, + \%instids); + }; + if ($result eq 'ok') { + if (keys(%instusers) > 0) { + foreach my $key (keys(%instusers)) { + my $usrstr = &Apache::lonnet::freeze_escape($instusers{$key}); + $response .= &escape(&escape($key).'='.$usrstr).'&'; + } + } + $response=~s/\&$//; + } else { + $response = 'unavailable'; + } + return $response; +} + ######################################################## ######################################################## @@ -625,12 +651,30 @@ sub process_file { } sub do_sql_query { - my ($query,$custom,$customshow,$searchdomain) = @_; + my ($query,$custom,$customshow,$domainstr,$searchdomain) = @_; # # limit to searchdomain if given and table is metadata # - if (($searchdomain) && ($query=~/FROM metadata/)) { + if ($domainstr && ($query=~/FROM metadata/)) { + my $havingstr; + $domainstr = &unescape($domainstr); + if ($domainstr =~ /,/) { + foreach my $dom (split(/,/,$domainstr)) { + if ($dom =~ /^$LONCAPA::domain_re$/) { + $havingstr .= 'domain="'.$dom.'" OR '; + } + } + $havingstr =~ s/ OR $//; + } else { + if ($domainstr =~ /^$LONCAPA::domain_re$/) { + $havingstr = 'domain="'.$domainstr.'"'; + } + } + if ($havingstr) { + $query.=' HAVING ('.$havingstr.')'; + } + } elsif (($searchdomain) && ($query=~/FROM metadata/)) { $query.=' HAVING (domain="'.$searchdomain.'")'; } # &logthis('doing query ('.$searchdomain.')'.$query);