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