#
# $Id: lonsupportreq.pm,v 1.38 2006/10/12 22:47:31 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
package Apache::lonsupportreq;
use strict;
use lib qw(/home/httpd/lib/perl);
use MIME::Types;
use MIME::Lite;
use CGI::Cookie();
use Apache::Constants qw(:common);
use Apache::loncommon();
use Apache::lonnet;
use Apache::lonlocal;
use Apache::lonacc();
use Apache::courseclassifier;
use lib '/home/httpd/lib/perl/';
use LONCAPA;
sub handler {
my ($r) = @_;
&Apache::loncommon::content_type($r,'text/html');
$r->send_http_header;
if ($r->header_only) {
return OK;
}
&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['origurl','function']);
if ($r->uri eq '/adm/helpdesk') {
&Apache::lonacc::get_posted_cgi($r);
}
my $function = $env{'form.function'};
my $origurl = &unescape($env{'form.origurl'});
my $action = $env{'form.action'};
if ($action eq 'process') {
&print_request_receipt($r,$origurl,$function);
} else {
&print_request_form($r,$origurl,$function);
}
return OK;
}
sub print_request_form {
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);
my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
if (($tablecolor eq '') || ($tablecolor eq '#FFFFFF')) {
$tablecolor = '#EEEE99';
}
$ccode = '';
$os = $env{'browser.os'};
$browser = $env{'browser.type'};
$bversion = $env{'browser.version'};
$uhost = $env{'request.host'};
$uname = $env{'user.name'};
$udom = $env{'user.domain'};
$uhome = $env{'user.home'};
$urole = $env{'request.role'};
$usec = $env{'request.course.sec'};
$cid = $env{'request.course.id'};
$formname = 'logproblem';
if ($origurl =~ m-^http://-) {
$server = $origurl;
} else {
$server = 'http://'.$ENV{'SERVER_NAME'}.$origurl;
}
my $scripttag = (<<'END');
function validate() {
if (validmail(document.logproblem.email) == false) {
alert("The e-mail address you entered: "+document.logproblem.email.value+" is not a valid e-mail address.");
return;
}
document.logproblem.submit();
}
function validmail(field) {
var str = field.value;
if (window.RegExp) {
var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
var reg1 = new RegExp(reg1str);
var reg2 = new RegExp(reg2str);
if (!reg1.test(str) && reg2.test(str)) {
return true;
}
return false;
}
else
{
if(str.indexOf("@") >= 0) {
return true;
}
return false;
}
}
END
if ($cid =~ m/_/) {
($cdom,$cnum) = split/_/,$cid;
}
if ($cdom && $cnum) {
my %csettings = &Apache::lonnet::get('environment',['description','internal.coursecode','internal.sectionnums'],$cdom,$cnum);
$ctitle = $csettings{'description'};
$ccode = $csettings{'internal.coursecode'};
$sectionlist = $csettings{'internal.sectionnums'};
}
if ($env{'environment.critnotification'}) {
$email = $env{'environment.critnotification'};
}
if (!$email && $env{'environment.notification'}) {
$email = $env{'environment.notification'};
}
if ($env{'environment.lastname'}) {
$lastname = $env{'environment.lastname'};
}
if ($env{'environment.firstname'}) {
$firstname = $env{'environment.firstname'};
}
my @sections = split/,/,$sectionlist;
my %groupid = ();
foreach (@sections) {
my ($sec,$grp) = split/:/,$_;
$groupid{$sec} = $grp;
}
my $codedom = $Apache::lonnet::perlvar{'lonDefDomain'};
&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['codedom']);
if (exists($env{'form.codedom'})) {
$codedom = $env{'form.codedom'};
}
my $details_title;
if ($codedom) {
$details_title = ' ('.$codedom.')';
}
my %coursecodes = ();
my %codes = ();
my @codetitles = ();
my %cat_titles = ();
my %cat_order = ();
my %idlist = ();
my %idnums = ();
my %idlist_titles = ();
my $caller = 'global';
my $totcodes = 0;
my $format_reply;
my $jscript = '';
my $loaditems = qq|
function initialize_codes() {
return;
}
|;
if ($cdom) {
$codedom = $cdom;
}
if ($cnum) {
$coursecodes{$cnum} = $ccode;
if ($ccode eq '') {
$totcodes = &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,$codedom,$totcodes);
} else {
$coursecodes{$cnum} = $ccode;
$caller = $cnum;
$totcodes ++;
}
} else {
$totcodes = &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,$codedom,$totcodes);
}
if ($totcodes > 0) {
if ($ccode eq '') {
$format_reply = &Apache::lonnet::auto_instcode_format($caller,$codedom,\%coursecodes,\%codes,\@codetitles,\%cat_titles,\%cat_order);
if ($format_reply eq 'ok') {
my $numtypes = @codetitles;
&Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
my $longtitles_str = join('","',@{$longtitles});
my $allidlist = $idlist{$codetitles[0]};
$jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
$jscript .= $scripttext;
$jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
$loaditems = '';
}
}
}
my $js = '';
my %add_entries = (topmargin => "0",
marginheight => "0",
onLoad =>"initialize_codes()",);
my $start_page =
&Apache::loncommon::start_page('Support Request',$js,
{ 'function' => $function,
'add_entries' => \%add_entries,
'only_body' => 1,});
$r->print($start_page);
if ($r->uri eq '/adm/helpdesk') {
&print_header($r,$origurl);
}
$r->print(<<"END");
END
$r->print(&Apache::loncommon::end_page());
return;
}
sub print_request_receipt {
my ($r,$url,$function) = @_;
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');
my @cookievars = ('lonID');
my $admin = $Apache::lonnet::perlvar{'lonAdminMail'};
my $to = $Apache::lonnet::perlvar{'lonSupportEMail'};
my $from = $admin;
my $reporttime = &Apache::lonlocal::locallocaltime(time);
my $fontcolor = &Apache::loncommon::designparm($function.'.font');
my $vlinkcolor = &Apache::loncommon::designparm($function.'.vlink');
my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
my @formvars = ('username','email','uname','udom','sourceurl','phone','section','coursecode','title','subject','description','screenshot');
&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},\@formvars);
my $coursecode = $env{'form.coursecode'};
if ($coursecode eq '') {
if (defined($env{'form.Year'})) {
$coursecode .= $env{'form.Year'};
}
if (defined($env{'form.Semester'})) {
$coursecode .= $env{'form.Semester'};
}
if (defined($env{'form.Department'})) {
$coursecode .= $env{'form.Department'};
}
if (defined($env{'form.Number'})) {
$coursecode .= $env{'form.Number'};
}
}
my $supportmsg = qq|
Name: $env{'form.username'}
Email: $env{'form.email'}
Username/domain: $env{'form.uname'} - $env{'form.udom'}
Tel: $env{'form.phone'}
Course Information: $env{'form.title'} - $coursecode - section: $env{'form.section'}
Subject: $env{'form.subject'}
Description: $env{'form.description'}
URL: $env{'form.sourceurl'}
Date/Time: $reporttime
|;
my $descrip = $env{'form.description'};
$descrip =~ s#\n# #g;
my $displaymsg = qq|
Name: $env{'form.username'}
Email: $env{'form.email'}
Username/domain: $env{'form.uname'} - $env{'form.udom'}
Tel: $env{'form.phone'}
Course Information: $env{'form.title'} - $coursecode - section: $env{'form.section'}
Subject: $env{'form.subject'}
Description: $descrip
URL: $env{'form.sourceurl'}
Date/Time: $reporttime
|;
my $start_page =
&Apache::loncommon::start_page('Support request recorded',undef,
{'function' => $function,
'add_entries' => {
topmargin => "0",
marginheight => "0",
},
'only_body' => 1,});
$r->print(<<"END");
$start_page
END
if ($r->uri eq '/adm/helpdesk') {
&print_header($r,$url,'process');
}
if ($to =~ m/^[^\@]+\@[^\@]+$/) {
$r->print("
");
END
} else {
$r->print(<Warning: Problem with support e-mail address
As the e-mail address provided for this LON-CAPA server ($to) does not appear to be a valid e-mail address, your support request has not been sent to the LON-CAPA support staff or administrator at your institution. Instead a copy has been sent to the LON-CAPA support team at Michigan State University.
END
$to = 'helpdesk@lon-capa.org';
}
}
if (defined($env{'form.email'})) {
if ($env{'form.email'} =~ m/^[^\@]+\@[^\@]+$/) {
$from = $env{'form.email'};
}
}
my $subject = $env{'form.subject'};
$subject =~ s#(`)#'#g;
$subject =~ s#\$#\(\$\)#g;
$supportmsg =~ s#(`)#'#g;
$supportmsg =~ s#\$#\(\$\)#g;
$displaymsg =~ s#(`)#'#g;
$displaymsg =~ s#\$#\(\$\)#g;
my $fname;
my $attachmentpath = '';
my $attachmentsize = '';
if (defined($env{'user.name'})) {
if ($env{'form.screenshot.filename'}) {
$attachmentsize = length($env{'form.screenshot'});
if ($attachmentsize > 131072) {
$displaymsg .= " The uploaded screenshot file ($attachmentsize bytes) included with your request exceeded the maximum allowed size - 128 KB, and has therefore been discarded.";
} else {
$attachmentpath=&Apache::lonnet::userfileupload('screenshot',undef,'helprequests');
}
}
}
my %cookies = ();
my $cookie=CGI::Cookie->parse($r->header_in('Cookie'));
if ($$cookie{'lonID'} =~ /lonID=(\w+);/) {
$cookies{'lonID'} = $1;
}
if ($attachmentpath =~ m-/([^/]+)$-) {
$fname = $1;
$displaymsg .= " An uploaded screenshot file - $fname ($attachmentsize bytes) was included in the request sent by $env{'user.name'} from LON-CAPA domain: $env{'user.domain'}";
$supportmsg .= "\n";
foreach (@cookievars) {
$supportmsg .= "$_: $cookies{$_}\n";
}
foreach (@ENVvars) {
$supportmsg .= "$_: $ENV{$_}\n";
}
foreach (@envvars) {
$supportmsg .= "$_: $env{$_}\n";
}
}
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Type =>'TEXT',
Data => $supportmsg,
);
if ($attachmentpath) {
my ($type, $encoding) = MIME::Types::by_suffix($attachmentpath);
$msg->attach(Type => $type,
Path => $attachmentpath,
Filename => $fname
);
} else {
my $envdata = '';
foreach (@cookievars) {
$envdata .= "$_: $cookies{$_}\n";
}
foreach (@ENVvars) {
$envdata .= "$_: $ENV{$_}\n";
}
foreach (@envvars) {
$envdata .= "$_: $env{$_}\n";
}
foreach (@loncvars) {
$envdata .= "$_: $env{$_}\n";
}
$msg->attach(Type => 'TEXT',
Data => $envdata);
}
### Send it:
$msg->send('sendmail');
if ($attachmentpath =~ m#$Apache::lonnet::perlvar{'lonDaemons'}/tmp/helprequests/(\d+)/[^/]+#) {
unlink($attachmentpath);
}
$r->print(qq|
Your support request contained the following information:
'.&mt('
Please review the information in "Log-in help"').$getstarttext.' '.&mt('if you are unable to log-in').'. '.&mt('If your problem is still unresolved, the form below can be used to send a question to the LON-CAPA helpdesk').'. '.&mt('Note').': '.&mt('Student questions about course content should be directed to the course instructor').'.