Annotation of loncom/interface/lonsource.pm, revision 1.26
1.1 taceyjo1 1: # The LearningOnline Network with CAPA
1.23 bisitz 2: # Source Code handler
1.1 taceyjo1 3: #
1.26 ! www 4: # $Id: lonsource.pm,v 1.25 2011/10/25 14:28:13 www Exp $
1.1 taceyjo1 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:
31: package Apache::lonsource;
32:
33: use strict;
1.11 albertel 34: use Apache::lonnet;
1.1 taceyjo1 35: use Apache::loncommon();
36: use Apache::lonhtmlcommon();
37: use Apache::lonsequence();
38: use Apache::Constants qw(:common :http);
39: use Apache::lonmeta;
40: use Apache::File;
41: use Apache::lonlocal;
42: use HTML::Entities;
1.17 www 43: use LONCAPA;
1.1 taceyjo1 44:
45: sub make_link {
1.4 taceyjo1 46: my ($filename, $listname) = @_;
1.18 albertel 47: my $sourcelink = "/adm/source?inhibitmenu=yes&filename=".$filename."&listname=".$listname;
1.2 albertel 48:
49: return $sourcelink;
1.1 taceyjo1 50: }
51:
52: sub stage_2 {
1.26 ! www 53: my ($r, $filename, $listname) = @_;
! 54: my ($author)=($filename=~/\/res\/[^\/]+\/([^\/]+)\//);
1.21 bisitz 55: $r->print(&Apache::loncommon::start_page('Copy Problem Source Code to Construction Space')
56: .&mt('Please enter the directory that you would like the source code to go into.')
57: .'<p>'
58: .&mt('Note: the path is in reference to the root of your construction space,'
59: .' and new directories will be automatically created.')
60: .'</p>');
1.13 www 61: $r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
1.5 taceyjo1 62: <input type="hidden" name="filename" value="'.$filename.'" />
63: <input type="hidden" name="listname" value="'.$listname.'" />
64: <input type="hidden" name="action" value="copy_stage" />
1.21 bisitz 65: <input type="text" size="50" name="newpath" value="/'.&mt('shared_source').'/'.$author.'" />
66: <input type="submit" value="'.&mt('Copy').'" />
1.5 taceyjo1 67: </form>');
68: return OK;
1.4 taceyjo1 69: }
70:
1.26 ! www 71: sub copy_author {
1.4 taceyjo1 72: my $role;
73: my $domain;
74: my $author_name;
1.19 albertel 75: if ($env{'request.role'} =~ m{^ca\.}) {
1.11 albertel 76: ($role, $domain, $author_name) = split(/\//,$env{'request.role'});
1.4 taceyjo1 77: } else {
1.5 taceyjo1 78: $role = "au.";
1.11 albertel 79: $domain = $env{'user.domain'};
80: $author_name = $env{'user.name'};
1.5 taceyjo1 81: }
1.26 ! www 82: return ($role,$author_name,$domain);
! 83: }
! 84:
! 85:
! 86: sub copy_stage {
! 87: my ($r, $filename, $listname, $newpath) = @_;
! 88:
! 89: #Figure out if we are author or co-author
! 90: my ($role,$author_name,$domain)=©_author();
1.5 taceyjo1 91:
1.26 ! www 92: # Construct path to copy and filter out any possibly nasty stuff
1.25 www 93: my $path_to_new_file = '/home/httpd/html/priv/'.$domain.'/'.$author_name.'/'.$newpath.'/'.$listname;
1.26 ! www 94: $path_to_new_file=~s/\.\.//g;
! 95: $path_to_new_file=~s/\~//g;
! 96: $path_to_new_file=~s/\/+/\//g;
! 97:
1.5 taceyjo1 98: #Just checking again for access as we want to make sure that it is really ok now that we have the real path
99:
1.25 www 100: my ($uname,$udom)= &Apache::loncacc::constructaccess($path_to_new_file);
1.20 albertel 101:
102: if (!$uname || !$udom) {
103: $r->print(&Apache::loncommon::start_page('Not Allowed'));
104: $r->print(&mt('Not allowed to create file [_1]', $path_to_new_file));
105: $r->print(&Apache::loncommon::end_page());
106: return;
1.4 taceyjo1 107: }
1.20 albertel 108:
109: #allowed
1.16 albertel 110: $r->print(&Apache::loncommon::start_page('Copying Source'));
1.5 taceyjo1 111: my $result = &Apache::loncfile::exists($uname, $udom, $path_to_new_file);
112: $r->print($result);
113: if(($result) && ($result =~ m|published|) ) {
1.26 ! www 114: &delete_copy_file($r, $newpath, $filename, $path_to_new_file, '1');
1.5 taceyjo1 115: } elsif(($result) && ($result =~ m|exists!|)) {
1.26 ! www 116: &confirm($r, $newpath, $filename, $path_to_new_file);
1.5 taceyjo1 117: } else {
1.26 ! www 118: ©_file($r, $newpath, $filename, $path_to_new_file);
1.5 taceyjo1 119: }
1.20 albertel 120:
121: $r->print(&Apache::loncommon::end_page());
1.4 taceyjo1 122: }
123:
124: sub confirm {
1.26 ! www 125: my ($r, $newpath, $filename, $path_to_new_file) = @_;
1.5 taceyjo1 126: $r->print("<b>Press delete to remove file and replace it with a copy of the source you are viewing</b><br /><br / >");
1.13 www 127: $r->print('<form name="delete_confirm" action="/adm/source" target="_parent" method="post">
1.5 taceyjo1 128: <input type="hidden" name="filename" value="'.$filename.'" />
129: <input type="hidden" name="path" value="'.$path_to_new_file.'" />
130: <input type="hidden" name="newpath" value="'.$newpath.'" />
131: <input type="hidden" name="action" value="delete_confirm" />
132:
133: <input type="submit" value="Delete" />
134: </form>');
1.4 taceyjo1 135: }
136:
1.6 taceyjo1 137: sub delete_copy_file {
1.26 ! www 138: my ($r, $newpath, $filename, $path_to_new_file, $type) = @_;
1.5 taceyjo1 139: if($type eq '1') {
1.23 bisitz 140: $r->print('<p><span class="LC_warning">'
141: .&mt('Cannot delete non-obsolete published file.')
142: .'</span><br />'
143: .&mt('Please use the code view in previous window to use shared code.')
144: .'<br /><br />');
145: $r->print('<input type="button" value="Close Window" name="close"'
1.24 raeburn 146: .' onclick="window.close()" />'
1.23 bisitz 147: .'</p>');
1.5 taceyjo1 148: } else {
149: if(-e $path_to_new_file) {
150: unless(unlink($path_to_new_file)) {
1.23 bisitz 151: $r->print('<p class="LC_error"">'.&mt('Error:').' '.$!.'</p>');
1.5 taceyjo1 152: return 0;
153: }
154: } else {
1.23 bisitz 155: $r->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
1.5 taceyjo1 156: return 0;
157: }
1.26 ! www 158: ©_file($r, $newpath, $filename, $path_to_new_file);
1.5 taceyjo1 159: }
1.1 taceyjo1 160: }
161:
1.4 taceyjo1 162: sub copy_file {
1.26 ! www 163: my ($r, $newpath, $filename, $path_to_new_file) = @_;
1.5 taceyjo1 164: $r->print("<b>Creating directories</b>");
1.26 ! www 165:
! 166: #Figure out if we are author or co-author
! 167: my ($role,$author_name,$domain)=©_author();
! 168:
! 169: my $path = '/home/httpd/html/priv/'.$domain.'/'.$author_name.'/';
1.5 taceyjo1 170: my @directories = split(/\//,$newpath);
1.26 ! www 171:
1.5 taceyjo1 172: foreach my $now_checking (@directories) {
173: if($now_checking ne '') {
174: $path = $path.'/'.$now_checking;
175: if(-e $path) {} #More moving along, isn't recursion fun'
176:
177: else {
178: unless(mkdir($path, 02770)) {
1.23 bisitz 179: $r->print('<p class="LC_error">'.&mt('Error:').' '.$!.'</p>');
1.5 taceyjo1 180: return 0;
181: }
182: unless(chmod(02770, ($path))) {
1.23 bisitz 183: $r->print('<p class="LC_error"> '.&mt('Error:').' '.$!.'</p>');
1.5 taceyjo1 184: return 0;
185: }
186: }
187: } else { } #Just move along
188:
189: }
190: $r->print("<br /><b>Copying File</b>");
1.6 taceyjo1 191: my $problem_filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
1.17 www 192: my $file_output = &includemeta(&Apache::lonnet::getfile($problem_filename),$filename);
1.5 taceyjo1 193: my $fs=Apache::File->new(">$path_to_new_file");
194: if (defined($fs)) {
195: print $fs $file_output;
196: }
197: $r->print("<br /><br />");
1.24 raeburn 198: $r->print('<input type="button" value="Close Window" name="close" onclick="window.close()" />');
1.5 taceyjo1 199: #Some 1.3'ish feature is to include the derivative feature, will go here..'
200: }
1.4 taceyjo1 201:
1.1 taceyjo1 202: sub print_item {
1.5 taceyjo1 203: my ($r, $filename) = @_;
1.17 www 204: my $file_output = &includemeta(&Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.$filename),$filename);
1.5 taceyjo1 205: my $count=0;
206: my $maxlength=-1;
207: foreach (split ("\n", $file_output)) {
208: $count+=int(length($_)/79);
209: $count++;
210: if (length($_) > $maxlength) {
211: $maxlength = length($_);
212: }
213: }
214: my $rows = $count;
215: my $cols = $maxlength;
216: $r->print('<textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
217: &HTML::Entities::encode($file_output,'<>&"').'</textarea>');
218: return OK;
219:
1.1 taceyjo1 220: }
221:
1.17 www 222: sub includemeta {
223: my ($file_output,$orgfilename)=@_;
224: my $escfilename=&escape($orgfilename);
225: my $copytime=time;
226: if ($file_output=~/\<meta\s*name\=\"isbasedonres\"/i) {
227: $file_output=~s/(\<meta\s*name\=\"isbasedonres\"\s*content\=\"[^\"]*)\"/$1\,\Q$escfilename\E\"/i;
228: } else {
229: $file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedonres\" content=\"\Q$escfilename\E\" \/\>/i;
230: }
231: if ($file_output=~/\<meta\s*name\=\"isbasedontime\"/i) {
232: $file_output=~s/(\<meta\s*name\=\"isbasedontime\"\s*content\=\"[^\"]*)\"/$1\,\Q$copytime\E\"/i;
233: } else {
234: $file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedontime\" content=\"\Q$copytime\E\" \/\>/i;
235: }
236: return $file_output;
237: }
1.1 taceyjo1 238:
1.5 taceyjo1 239: sub handler {
1.2 albertel 240: my $r=shift;
1.3 www 241: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.5 taceyjo1 242: ['filename','listname']);
1.11 albertel 243: my $filename = $env{'form.filename'};
244: my $listname = $env{'form.listname'};
1.3 www 245: my $source = &Apache::lonnet::metadata($filename,'sourceavail');
246: if ($source ne 'open') {
1.11 albertel 247: $env{'user.error.msg'}="$filename:cre:1:1:Source code not available";
1.5 taceyjo1 248: return HTTP_NOT_ACCEPTABLE;
1.7 taceyjo1 249: }
1.15 www 250: unless ((&Apache::lonnet::allowed('bre',$filename)) &&
251: (&Apache::lonnet::allowed('cre','/'))) {
1.11 albertel 252: $env{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
1.5 taceyjo1 253: return HTTP_NOT_ACCEPTABLE;
1.7 taceyjo1 254: }
1.20 albertel 255:
256: &Apache::loncommon::content_type($r,'text/html');
257: $r->send_http_header;
258:
1.11 albertel 259: if ($env{'form.action'} eq 'stage2') {
1.26 ! www 260: &stage_2($r, $filename,$listname);
1.11 albertel 261: } elsif($env{'form.action'} eq 'copy_stage') {
262: ©_stage($r, $filename,$env{'form.listname'},$env{'form.newpath'});
263: } elsif($env{'form.action'} eq 'delete_confirm') {
1.26 ! www 264: &delete_copy_file($r, $env{'form.newpath'}, $env{'form.filename'}, $env{'form.path'}, '0');
1.6 taceyjo1 265: } else {
1.20 albertel 266: $r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
1.24 raeburn 267: <input type="button" value="'.&mt('Close Window').'" name="close" onclick="window.close()" />
1.5 taceyjo1 268: <input type="hidden" name="filename" value="'.$filename.'" />
269: <input type="hidden" name="listname" value="'.$listname.'" />
270: <input type="hidden" name="action" value="stage2" />
1.21 bisitz 271: <input type="submit" value="'.&mt('Copy to Construction Space').'" />
1.5 taceyjo1 272: </form>');
273: $r->print('<hr />');
1.11 albertel 274: &print_item($r, $env{'form.filename'});
1.5 taceyjo1 275: }
1.2 albertel 276: return OK;
1.1 taceyjo1 277: }
278:
279: 1;
1.5 taceyjo1 280:
281:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>