Annotation of loncom/interface/lonsource.pm, revision 1.35
1.1 taceyjo1 1: # The LearningOnline Network with CAPA
1.23 bisitz 2: # Source Code handler
1.1 taceyjo1 3: #
1.35 ! raeburn 4: # $Id: lonsource.pm,v 1.34 2015/05/23 21:02:39 raeburn 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.28 raeburn 43: use LONCAPA qw(:DEFAULT :match);
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.33 raeburn 55: $r->print(&Apache::loncommon::start_page('Copy Problem Source Code to Authoring Space',undef,
56: {'only_body' => 1,})
1.21 bisitz 57: .&mt('Please enter the directory that you would like the source code to go into.')
58: .'<p>'
1.31 bisitz 59: .&mt('Note: the path is in reference to the root of your Authoring Space,'
1.21 bisitz 60: .' and new directories will be automatically created.')
61: .'</p>');
1.13 www 62: $r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
1.5 taceyjo1 63: <input type="hidden" name="filename" value="'.$filename.'" />
64: <input type="hidden" name="listname" value="'.$listname.'" />
65: <input type="hidden" name="action" value="copy_stage" />
1.21 bisitz 66: <input type="text" size="50" name="newpath" value="/'.&mt('shared_source').'/'.$author.'" />
67: <input type="submit" value="'.&mt('Copy').'" />
1.33 raeburn 68: </form>'.
69: &Apache::loncommon::end_page());
1.5 taceyjo1 70: return OK;
1.4 taceyjo1 71: }
72:
1.26 www 73: sub copy_author {
1.4 taceyjo1 74: my $role;
75: my $domain;
76: my $author_name;
1.19 albertel 77: if ($env{'request.role'} =~ m{^ca\.}) {
1.11 albertel 78: ($role, $domain, $author_name) = split(/\//,$env{'request.role'});
1.4 taceyjo1 79: } else {
1.5 taceyjo1 80: $role = "au.";
1.11 albertel 81: $domain = $env{'user.domain'};
82: $author_name = $env{'user.name'};
1.5 taceyjo1 83: }
1.26 www 84: return ($role,$author_name,$domain);
85: }
86:
87:
88: sub copy_stage {
89: my ($r, $filename, $listname, $newpath) = @_;
90:
1.35 ! raeburn 91: my ($path_to_new_file,$uname,$udom) = &get_path_to_newfile($r,$newpath,$listname);
1.20 albertel 92:
93: #allowed
1.35 ! raeburn 94: if ($path_to_new_file) {
! 95: $r->print(&Apache::loncommon::start_page('Copying Source',undef,{'only_body' => 1}));
! 96: my $result = &Apache::loncfile::exists($uname, $udom, $path_to_new_file);
! 97: $r->print($result);
! 98: if (($result) && ($result =~ /published/)) {
! 99: &delete_copy_file($r, $newpath, $filename, $path_to_new_file, '1');
! 100: } elsif (($result) && ($result =~ /exists\!/)) {
! 101: &confirm($r, $newpath, $filename, $listname);
! 102: } else {
! 103: ©_file($r, $newpath, $filename, $path_to_new_file);
! 104: }
! 105: $r->print(&Apache::loncommon::end_page());
1.5 taceyjo1 106: }
1.34 raeburn 107: return;
1.4 taceyjo1 108: }
109:
110: sub confirm {
1.35 ! raeburn 111: my ($r, $newpath, $filename, $listname) = @_;
1.32 bisitz 112: $r->print('<b>'.&mt('Press delete to remove file and replace it with a copy of the source you are viewing.').'</b><br /><br />');
1.13 www 113: $r->print('<form name="delete_confirm" action="/adm/source" target="_parent" method="post">
1.5 taceyjo1 114: <input type="hidden" name="filename" value="'.$filename.'" />
1.35 ! raeburn 115: <input type="hidden" name="listname" value="'.$listname.'" />
1.5 taceyjo1 116: <input type="hidden" name="newpath" value="'.$newpath.'" />
117: <input type="hidden" name="action" value="delete_confirm" />
118: <input type="submit" value="Delete" />
119: </form>');
1.34 raeburn 120: return;
1.4 taceyjo1 121: }
122:
1.6 taceyjo1 123: sub delete_copy_file {
1.26 www 124: my ($r, $newpath, $filename, $path_to_new_file, $type) = @_;
1.35 ! raeburn 125: if ($type eq '1') {
1.23 bisitz 126: $r->print('<p><span class="LC_warning">'
127: .&mt('Cannot delete non-obsolete published file.')
128: .'</span><br />'
129: .&mt('Please use the code view in previous window to use shared code.')
130: .'<br /><br />');
1.33 raeburn 131: $r->print('<form name="delete_done" action="/adm/source" target="_parent" method="post">'
132: .'<input type="button" value="'.&mt('Close Window').'" name="close"'
1.24 raeburn 133: .' onclick="window.close()" />'
1.34 raeburn 134: .'</form></p>');
1.35 ! raeburn 135: return;
1.5 taceyjo1 136: } else {
1.33 raeburn 137: $r->print(&Apache::loncommon::start_page('Copying Source',undef,{'only_body' => 1}));
1.35 ! raeburn 138: if (-e $path_to_new_file) {
! 139: unless (unlink($path_to_new_file)) {
1.23 bisitz 140: $r->print('<p class="LC_error"">'.&mt('Error:').' '.$!.'</p>');
1.5 taceyjo1 141: return 0;
142: }
143: } else {
1.33 raeburn 144: $r->print('<p class="LC_error">'.&mt('No such file').'</p>');
1.5 taceyjo1 145: return 0;
146: }
1.26 www 147: ©_file($r, $newpath, $filename, $path_to_new_file);
1.33 raeburn 148: $r->print(&Apache::loncommon::end_page());
1.35 ! raeburn 149: return;
1.5 taceyjo1 150: }
1.1 taceyjo1 151: }
152:
1.4 taceyjo1 153: sub copy_file {
1.26 www 154: my ($r, $newpath, $filename, $path_to_new_file) = @_;
1.32 bisitz 155: $r->print('<b>'.&mt('Creating directories').'</b>');
1.26 www 156:
157: #Figure out if we are author or co-author
158: my ($role,$author_name,$domain)=©_author();
159:
1.27 raeburn 160: my $path = $r->dir_config('lonDocRoot')."/priv/$domain/$author_name/";
1.5 taceyjo1 161: my @directories = split(/\//,$newpath);
1.26 www 162:
1.5 taceyjo1 163: foreach my $now_checking (@directories) {
164: if($now_checking ne '') {
165: $path = $path.'/'.$now_checking;
166: if(-e $path) {} #More moving along, isn't recursion fun'
167:
168: else {
169: unless(mkdir($path, 02770)) {
1.23 bisitz 170: $r->print('<p class="LC_error">'.&mt('Error:').' '.$!.'</p>');
1.5 taceyjo1 171: return 0;
172: }
173: unless(chmod(02770, ($path))) {
1.23 bisitz 174: $r->print('<p class="LC_error"> '.&mt('Error:').' '.$!.'</p>');
1.5 taceyjo1 175: return 0;
176: }
177: }
178: } else { } #Just move along
179:
180: }
1.32 bisitz 181: $r->print('<br /><b>'.&mt('Copying File').'</b>');
1.6 taceyjo1 182: my $problem_filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
1.17 www 183: my $file_output = &includemeta(&Apache::lonnet::getfile($problem_filename),$filename);
1.5 taceyjo1 184: my $fs=Apache::File->new(">$path_to_new_file");
185: if (defined($fs)) {
186: print $fs $file_output;
187: }
188: $r->print("<br /><br />");
1.33 raeburn 189: $r->print('<form name="copied_file" action="/adm/source" target="_parent" method="post">'
190: .'<input type="button" value="'
191: .&mt('Close Window').'" name="close" onclick="window.close()" />'
192: .'</form>');
1.5 taceyjo1 193: #Some 1.3'ish feature is to include the derivative feature, will go here..'
1.34 raeburn 194: return;
1.5 taceyjo1 195: }
1.4 taceyjo1 196:
1.1 taceyjo1 197: sub print_item {
1.28 raeburn 198: my ($r,$filename,$listname) = @_;
199: my $file_output =
200: &includemeta(&Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.$filename),
201: $filename);
202: $r->print(&Apache::loncommon::start_page('View Source Code',undef,
203: {'only_body' => 1}));
204: if ($file_output ne '') {
205: my $access_to_cstr;
206: my $lonhost = $r->dir_config('lonHostID');
207: if (&Apache::lonnet::is_library($lonhost)) {
208: my @possdoms = &Apache::lonnet::current_machine_domains();
209: foreach my $dom (@possdoms) {
210: if ($env{"user.role.au./$dom/"}) {
211: $access_to_cstr = 1;
212: last;
213: }
214: }
215: unless ($access_to_cstr) {
216: foreach my $key (keys(%env)) {
217: if ($key =~ m{^\Quser.role.ca./\E($match_domain)/}) {
218: my $adom = $1;
219: if (grep(/^\Q$adom\E$/,@possdoms)) {
220: $access_to_cstr = 1;
221: last;
222: }
223: }
224: }
225: }
226: if ($access_to_cstr) {
227: $r->print('
228: <form name="copy" action="/adm/source" target="_parent" method="post">
229: <input type="button" value="'.&mt('Close Window').'" name="close" onclick="window.close();" />
230: <input type="hidden" name="filename" value="'.$filename.'" />
231: <input type="hidden" name="listname" value="'.$listname.'" />
232: <input type="hidden" name="action" value="stage2" />
1.30 raeburn 233: <input type="submit" value="'.&mt('Copy to Authoring Space').'" />
1.28 raeburn 234: </form><hr />
235: ');
236: } else {
237: $r->print('<p><span class="LC_info">'.
238: &mt('Source code is displayed, but you can not copy to Authoring Space, as you do not have an author or co-author role on this server.').
239: '</span></p><a href="javascript:window.close();">'.&mt('Close Window').
240: '</a><br /><hr />'
241: );
242: }
243: } else {
244: $r->print('<p><span class="LC_info">'.
245: &mt('Source code is displayed, but you can not copy to Authoring Space on this server.').
246: '</span></p><a href="javascript:window.close();">'.&mt('Close Window').
247: '</a><br /><hr />'
248: );
249:
250: }
251: my $count=0;
252: my $maxlength=-1;
253: foreach (split ("\n", $file_output)) {
254: $count+=int(length($_)/79);
255: $count++;
256: if (length($_) > $maxlength) {
257: $maxlength = length($_);
258: }
1.5 taceyjo1 259: }
1.28 raeburn 260: my $rows = $count;
261: my $cols = $maxlength;
262: $r->print('<form name="showsrc" action="" method="post" onsubmit="return false">'."\n".
263: '<textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
264: &HTML::Entities::encode($file_output,'<>&"').'</textarea></form>');
265: } else {
266: $r->print('<p class="LC_warning">'.
267: &mt('Unable to retrieve file contents.').
268: '</p><a href="javascript:window.close();">'.&mt('Close Window').'</a>'
269: );
1.5 taceyjo1 270: }
1.28 raeburn 271: $r->print(&Apache::loncommon::end_page());
272: return;
1.1 taceyjo1 273: }
274:
1.17 www 275: sub includemeta {
276: my ($file_output,$orgfilename)=@_;
277: my $escfilename=&escape($orgfilename);
278: my $copytime=time;
279: if ($file_output=~/\<meta\s*name\=\"isbasedonres\"/i) {
280: $file_output=~s/(\<meta\s*name\=\"isbasedonres\"\s*content\=\"[^\"]*)\"/$1\,\Q$escfilename\E\"/i;
281: } else {
282: $file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedonres\" content=\"\Q$escfilename\E\" \/\>/i;
283: }
284: if ($file_output=~/\<meta\s*name\=\"isbasedontime\"/i) {
285: $file_output=~s/(\<meta\s*name\=\"isbasedontime\"\s*content\=\"[^\"]*)\"/$1\,\Q$copytime\E\"/i;
286: } else {
287: $file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedontime\" content=\"\Q$copytime\E\" \/\>/i;
288: }
1.28 raeburn 289: if ($file_output eq '-1') {
290: return;
291: } else {
292: return $file_output;
293: }
1.17 www 294: }
1.1 taceyjo1 295:
1.35 ! raeburn 296: sub get_path_to_newfile {
! 297: my ($r,$newpath,$listname) = @_;
! 298:
! 299: #Figure out if we are author or co-author
! 300: my ($role,$author_name,$domain) = ©_author();
! 301:
! 302: # Construct path to copy and filter out any possibly nasty stuff
! 303: my $path = $r->dir_config('lonDocRoot')."/priv/$domain/$author_name/";
! 304: my $path_to_new_file = $path."$newpath/$listname";
! 305: $path_to_new_file=~s/\.\.//g;
! 306: $path_to_new_file=~s/\~//g;
! 307: $path_to_new_file=~s/\/+/\//g;
! 308:
! 309: #Just checking again for access as we want to make sure that it is really ok
! 310: #now that we have the real path
! 311:
! 312: my ($uname,$udom)= &Apache::lonnet::constructaccess($path_to_new_file);
! 313:
! 314: if (!$uname || !$udom) {
! 315: $r->print(&Apache::loncommon::start_page('Not Allowed',undef,{'only_body' => 1}));
! 316: $r->print(&mt('Not allowed to create file [_1]', $path_to_new_file));
! 317: $r->print(&Apache::loncommon::end_page());
! 318: if (wantarray) {
! 319: return();
! 320: } else {
! 321: return;
! 322: }
! 323: }
! 324: if (wantarray) {
! 325: return ($path_to_new_file,$uname,$udom);
! 326: } else {
! 327: return $path_to_new_file;
! 328: }
! 329: }
! 330:
1.5 taceyjo1 331: sub handler {
1.2 albertel 332: my $r=shift;
1.3 www 333: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.5 taceyjo1 334: ['filename','listname']);
1.11 albertel 335: my $filename = $env{'form.filename'};
336: my $listname = $env{'form.listname'};
1.35 ! raeburn 337:
1.3 www 338: my $source = &Apache::lonnet::metadata($filename,'sourceavail');
339: if ($source ne 'open') {
1.11 albertel 340: $env{'user.error.msg'}="$filename:cre:1:1:Source code not available";
1.5 taceyjo1 341: return HTTP_NOT_ACCEPTABLE;
1.28 raeburn 342: }
343: unless (&Apache::lonnet::allowed('bre',$filename)) {
1.11 albertel 344: $env{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
1.5 taceyjo1 345: return HTTP_NOT_ACCEPTABLE;
1.28 raeburn 346: }
347: unless (&Apache::lonnet::allowed('cre','/')) {
348: $env{'user.error.msg'}="$filename:cre:1:1:Access to source code denied";
349: return HTTP_NOT_ACCEPTABLE;
350: }
1.35 ! raeburn 351: my $newpath = $env{'form.newpath'};
1.20 albertel 352:
353: &Apache::loncommon::content_type($r,'text/html');
354: $r->send_http_header;
355:
1.11 albertel 356: if ($env{'form.action'} eq 'stage2') {
1.28 raeburn 357: &stage_2($r,$filename,$listname);
1.11 albertel 358: } elsif($env{'form.action'} eq 'copy_stage') {
1.35 ! raeburn 359: ©_stage($r,$filename,$listname,$newpath);
1.11 albertel 360: } elsif($env{'form.action'} eq 'delete_confirm') {
1.35 ! raeburn 361: my $path_to_new_file = &get_path_to_newfile($r,$newpath,$listname);
! 362: if ($path_to_new_file) {
! 363: &delete_copy_file($r, $newpath, $filename, $path_to_new_file, '0');
! 364: }
1.6 taceyjo1 365: } else {
1.28 raeburn 366: &print_item($r,$filename,$listname);
1.5 taceyjo1 367: }
1.2 albertel 368: return OK;
1.1 taceyjo1 369: }
370:
371: 1;
1.5 taceyjo1 372:
373:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>