Annotation of loncom/homework/daxesave.pm, revision 1.2

1.1       damieng     1: # The LearningOnline Network
                      2: # Convert and save a problem from Daxe.
                      3: #
1.2     ! damieng     4: # $Id: daxesave.pm,v 1.1 2015/12/03 20:40:27 damieng Exp $
1.1       damieng     5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: ###
                     29: 
                     30: package Apache::daxesave;
                     31: 
                     32: use Apache::Constants;
                     33: use Apache::lonnet;
                     34: use Try::Tiny;
                     35: use File::Copy;
                     36: 
                     37: use Apache::lonacc;
                     38: use Apache::loncommon;
                     39: use Apache::xml_to_loncapa;
                     40: 
                     41: sub handler {
                     42:     my $request = shift;
                     43:     
1.2     ! damieng    44:     $request->content_type('text/plain');
        !            45:     
1.1       damieng    46:     my $path = $env{'form.path'}; # should be in the form "/daxeopen/priv/..."
                     47:     $path =~ s/^\/daxeopen//;
                     48:     
                     49:     my $allowed;
                     50:     my ($ownername,$ownerdom,$ownerhome) = 
                     51:         &Apache::lonnet::constructaccess($path, 'setpriv');
                     52:     if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {
                     53:         unless ($ownerhome eq 'no_host') {
                     54:             my @hosts = &Apache::lonnet::current_machine_ids();
                     55:             if (grep(/^\Q$ownerhome\E$/,@hosts)) {
                     56:                 $allowed = 1;
                     57:             }
                     58:         }
                     59:     }
                     60:     unless ($allowed) {
1.2     ! damieng    61:         $request->log_reason("Unauthorized path: $path", $path);
        !            62:         $request->print("error\nUnauthorized path: $path");
        !            63:         $request->status(403);
        !            64:         return OK;
1.1       damieng    65:     }
                     66: 
                     67:     my $newpath = &Apache::lonnet::filelocation('', $path);
                     68: 
                     69:     my $contents = $env{'form.file'};
                     70:     
                     71:     try {
                     72:         $contents = &Apache::xml_to_loncapa::convert_file($contents);
                     73:     } catch {
                     74:         $request->print("error\nconvert failed for $path: $_");
                     75:         return OK;
                     76:     };
                     77:     
                     78:     my $filebak = $newpath.".bak";
                     79:     if (-e $newpath) {
                     80:         copy($newpath, $filebak); # errors ignored
                     81:     }
                     82:     open my $out, '>:encoding(UTF-8)', $newpath;
                     83:     print $out $contents;
                     84:     close $out;
                     85:     
                     86:     $request->print("ok\n");
                     87:     return OK;
                     88: }
                     89: 
                     90: 1;
                     91: __END__

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