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