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