[system] / trunk / webwork2 / lib / WebworkWebservice.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork2/lib/WebworkWebservice.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7077 - (view) (download) (as text)

1 : gage 7075 #!/user/bin/perl -w
2 :    
3 : gage 3064
4 :    
5 :    
6 :     BEGIN {
7 : gage 6881 $main::VERSION = "2.4.9";
8 : gage 6229 use Cwd;
9 :     use WeBWorK::PG::Local;
10 :    
11 : gage 6881 use constant MP2 => ( exists $ENV{MOD_PERL_API_VERSION} and $ENV{MOD_PERL_API_VERSION} >= 2 );
12 :    
13 : gage 6242 ###############################################################################
14 :     # Configuration -- set to top webwork directory (webwork2) (set in webwork.apache2-config)
15 :     # Configuration -- set server name
16 :     ###############################################################################
17 :    
18 : gage 6881 our $webwork_directory = $WeBWorK::Constants::WEBWORK_DIRECTORY; #'/opt/webwork/webwork2';
19 :     print "WebworkWebservice: webwork_directory set to ", $WeBWorK::Constants::WEBWORK_DIRECTORY,
20 :     " via \$WeBWorK::Constants::WEBWORK_DIRECTORY set in webwork.apache2-config\n";
21 : gage 6242
22 :     $WebworkWebservice::HOST_NAME = 'localhost'; # Apache->server->server_hostname;
23 :     $WebworkWebservice::HOST_PORT = '80'; # Apache->server->port;
24 :    
25 :     ###############################################################################
26 :    
27 : gage 3064 eval "use lib '$webwork_directory/lib'"; die $@ if $@;
28 :     eval "use WeBWorK::CourseEnvironment"; die $@ if $@;
29 : gage 7075 my $seed_ce = new WeBWorK::CourseEnvironment({ webwork_dir => $webwork_directory });
30 :     my $webwork_url = $seed_ce->{webwork_url};
31 :     my $pg_dir = $seed_ce->{pg_dir};
32 : gage 3064 eval "use lib '$pg_dir/lib'"; die $@ if $@;
33 :    
34 :     $WebworkWebservice::WW_DIRECTORY = $webwork_directory;
35 :     $WebworkWebservice::PG_DIRECTORY = $pg_dir;
36 : gage 7075 $WebworkWebservice::SeedCE = $seed_ce;
37 : gage 6242
38 :     ###############################################################################
39 :    
40 : gage 7075 $WebworkWebservice::PASSWORD = 'xmluser'; # default password
41 :     $WebworkWebservice::COURSENAME = 'gage_course'; # default course
42 : gage 3064 }
43 :    
44 :    
45 :     use strict;
46 : gage 6881 use warnings;
47 : gage 3064
48 : gage 6242
49 : gage 6881
50 :    
51 :    
52 :     ###############################################################################
53 :     ###############################################################################
54 :    
55 : gage 7075 package WebworkWebservice;
56 : gage 6881
57 :    
58 : gage 3064
59 :     sub pretty_print_rh {
60 :     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
61 :     my $rh = shift;
62 :     my $indent = shift || 0;
63 : gage 6229
64 : gage 3064 my $out = "";
65 : gage 6229 return $out if $indent>10;
66 : gage 3064 my $type = ref($rh);
67 :    
68 :     if (defined($type) and $type) {
69 :     $out .= " type = $type; ";
70 :     } elsif ($rh == undef) {
71 : gage 3292 $out .= " type = scalar; ";
72 : gage 3064 }
73 :     if ( ref($rh) =~/HASH/ or "$rh" =~/HASH/ ) {
74 :     $out .= "{\n";
75 :     $indent++;
76 :     foreach my $key (sort keys %{$rh}) {
77 :     $out .= " "x$indent."$key => " . pretty_print_rh( $rh->{$key}, $indent ) . "\n";
78 :     }
79 :     $indent--;
80 :     $out .= "\n"." "x$indent."}\n";
81 :    
82 :     } elsif (ref($rh) =~ /ARRAY/ or "$rh" =~/ARRAY/) {
83 :     $out .= " ( ";
84 :     foreach my $elem ( @{$rh} ) {
85 :     $out .= pretty_print_rh($elem, $indent);
86 :    
87 :     }
88 :     $out .= " ) \n";
89 :     } elsif ( ref($rh) =~ /SCALAR/ ) {
90 :     $out .= "scalar reference ". ${$rh};
91 :     } elsif ( ref($rh) =~/Base64/ ) {
92 :     $out .= "base64 reference " .$$rh;
93 :     } else {
94 :     $out .= $rh;
95 :     }
96 :    
97 :     return $out." ";
98 :     }
99 :    
100 :    
101 :     use WebworkWebservice::RenderProblem;
102 :     use WebworkWebservice::LibraryActions;
103 :     use WebworkWebservice::MathTranslators;
104 :    
105 :     ###############################################################################
106 :     package WebworkXMLRPC;
107 :     use base qw(WebworkWebservice);
108 : gage 7075 use WeBWorK::Utils qw(runtime_use writeTimingLogEntry);
109 : gage 3064
110 :    
111 :    
112 : gage 7075 ###########################################################################
113 :     # authentication
114 :     ###########################################################################
115 :    
116 :     sub initiate_session {
117 :     my ($invocant, @args) = @_;
118 :     my $class = ref $invocant || $invocant;
119 :     my $rh_input = $args[0];
120 :     my $user_id = $rh_input->{userID};
121 :     my $password = $rh_input->{password};
122 :     my $courseName = $rh_input ->{courseID};
123 :     my $ce = $class->create_course_environment($courseName);
124 :     my $db = new WeBWorK::DB($ce->{dbLayout});
125 :    
126 :     my $language= $ce->{language} || "en";
127 :     my $language_handle = WeBWorK::Localize->get_handle($language) ;
128 :    
129 :     my $user_authen_module = WeBWorK::Authen::class($ce, "user_module");
130 :     runtime_use $user_authen_module;
131 :    
132 :     my $self = {
133 :     courseName => $courseName,
134 :     user_id => $user_id,
135 :     password => $password,
136 :     ce => $ce,
137 :     db => $db,
138 :     language_handle => $language_handle,
139 :     };
140 :     $self = bless $self, $class;
141 :     # need to bless self before it can be used as an argument for the authentication module
142 :     # The authentication module is expecting a WeBWorK::Request object
143 :     # But its only actual requirement is that the method maketext() is defined.
144 :    
145 :     my $authen = $user_authen_module->new($self);
146 :     my $authz = WeBWorK::Authz->new($self);
147 :    
148 :     $self->{authen} = $authen;
149 :     $self->{authz} = $authz;
150 :     # we need to trick some of the methods within the webwork framework
151 :     # since we are not coming in with a standard apache request
152 :     # FIXME: can/should we change this????
153 :     #
154 :     # We are borrowing tricks from the AuthenWeBWorK.pm module
155 :     #
156 :     #
157 :    
158 :    
159 :     # now, here's the problem... WeBWorK::Authen looks at $r->params directly, whereas we
160 :     # need to look at $user and $sent_pw. this is a perfect opportunity for a mixin, i think.
161 :     my $authenOK;
162 :     {
163 :     no warnings 'redefine';
164 :     local *WeBWorK::Authen::get_credentials = \&WebworkXMLRPC::get_credentials;
165 :     local *WeBWorK::Authen::maybe_send_cookie = \&WebworkXMLRPC::noop;
166 :     local *WeBWorK::Authen::maybe_kill_cookie = \&WebworkXMLRPC::noop;
167 :     local *WeBWorK::Authen::set_params = \&WebworkXMLRPC::noop;
168 :     local *WeBWorK::Authen::write_log_entry = \&WebworkXMLRPC::noop; # maybe fix this to log interactions FIXME
169 :     $authenOK = $authen->verify;
170 :     }
171 :    
172 : gage 7077 $self->{authenOK} = $authenOK;
173 :     $self->{authzOK} = $authz->hasPermissions($user_id, "access_instructor_tools");
174 : gage 7075 return $self;
175 :     }
176 :    
177 :    
178 :    
179 :    
180 :    
181 :     ###########################################################################
182 :     # identify course
183 :     ###########################################################################
184 :    
185 :     sub create_course_environment {
186 :     my $self = shift;
187 :     my $courseName = shift;
188 : gage 7077 my $ce = WeBWorK::CourseEnvironment->new(
189 :     {webwork_dir => $WebworkWebservice::WW_DIRECTORY,
190 :     courseName => $courseName
191 :     });
192 : gage 7075 # error messages
193 :     return ($ce);
194 :     }
195 :    
196 :     ###########################################################################
197 :     # security check -- check that the user is in fact a professor in the course
198 :     ###########################################################################
199 :     sub ce {
200 :     my $self = shift;
201 :     $self->{ce};
202 :     }
203 :     sub db {
204 :     my $self = shift;
205 :     $self->{db};
206 :     }
207 :     sub authz {
208 :     my $self = shift;
209 :     $self->{authz};
210 :     }
211 :     sub maketext {
212 :     my $self = shift;
213 :     $self->{language_handle}->maketext(@_);
214 :     }
215 :    
216 :    
217 :     sub get_credentials {
218 :     my $self = shift;
219 :     # self is an Authen object it contains an object r which is the WebworkXMLRPC object
220 :     # confusing isn't it?
221 :     $self->{user_id} = $self->{r}->{user_id};
222 :     $self->{session_key} = "";
223 :     $self->{password} = $self->{r}->{password};
224 :     $self->{login_type} = "normal";
225 :     $self->{credential_source} = "params";
226 :     return 1;
227 :     }
228 :    
229 :     sub noop {
230 :    
231 :     }
232 :     sub check_authorization {
233 :    
234 :    
235 :    
236 :     }
237 :    
238 : gage 3064 # respond to xmlrpc requests
239 :     sub listLib {
240 : gage 7075 my $class = shift;
241 : gage 3064 my $in = shift;
242 : gage 7075 my $self = $class->initiate_session($in);
243 : gage 7077 warn "incomiing request to listLib: class is ",ref($self);
244 :     warn "authentication for ",$self->{user_id}, " in course ", $self->{courseName}, " is = ", $self->{authenOK};
245 :     warn "authorization as instructor for ", $self->{user_id}, " is ", $self->{authzOK};
246 : gage 3064 return( WebworkWebservice::LibraryActions::listLib($in) );
247 :     }
248 : gage 7075 sub listLibraries { # returns a list of libraries for the default course
249 : gage 3064 my $self = shift;
250 :     my $in = shift;
251 :     return( WebworkWebservice::LibraryActions::listLibraries($in) );
252 :     }
253 :     sub renderProblem {
254 :     my $self = shift;
255 :     my $in = shift;
256 :     return( WebworkWebservice::RenderProblem::renderProblem($in) );
257 :     }
258 :     sub readFile {
259 :     my $self = shift;
260 :     my $in = shift;
261 :     return( WebworkWebservice::LibraryActions::readFile($in) );
262 :     }
263 : gage 3072 sub tex2pdf {
264 :     my $self = shift;
265 :     my $in = shift;
266 :     return( WebworkWebservice::MathTranslators::tex2pdf($in) );
267 :     }
268 : gage 3064
269 :     # -- SOAP::Lite -- guide.soaplite.com -- Copyright (C) 2001 Paul Kulchenko --
270 :     # test responses
271 :    
272 :     sub hi { shift if UNIVERSAL::isa($_[0] => __PACKAGE__); # grabs class reference
273 :     return "hello, world";
274 :     }
275 :     sub hello2 { shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
276 :     #print "Receiving request for hello world\n";
277 :     return "Hello world2";
278 :     }
279 :     sub bye {shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
280 :     return "goodbye, sad cruel world";
281 :     }
282 :    
283 :     sub languages {shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
284 :     return ["Perl", "C", "sh"];
285 :     }
286 :    
287 :     sub echo_self {
288 :     my $self = shift;
289 :     }
290 :    
291 :     sub echo {
292 : gage 3072 return join("|",("begin ", WebworkWebservice::pretty_print_rh(\@_), " end") );
293 : gage 3064 }
294 :    
295 :     sub pretty_print_rh {
296 :     WebworkWebservice::pretty_print_rh(@_);
297 :     }
298 :    
299 :    
300 :     sub tth {shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
301 :     my $in = shift;
302 :     my $tthpath = "/usr/local/bin/tth";
303 :     # $tthpath -L -f5 -r 2>/dev/null " . $inputString;
304 :     return $in;
305 :    
306 :     }
307 :    
308 :    
309 :    
310 :    
311 :     package WWd;
312 :    
313 :     #use lib '/home/gage/webwork/xmlrpc/daemon';
314 :     #use WebworkXMLRPC;
315 :    
316 :    
317 :    
318 :    
319 :     ############utilities
320 :    
321 :     sub echo {
322 : gage 3072 return "WWd package ".join("|",("begin ", WebworkWebservice::pretty_print_rh(\@_), " end") );
323 : gage 3064 }
324 :    
325 :     sub listLib {
326 :     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
327 :     my $in = shift;
328 :     return( Webwork::listLib($in) );
329 :     }
330 :     sub renderProblem {
331 :     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
332 :     my $in = shift;
333 :     return( Filter::filterObject( Webwork::renderProblem($in) ) );
334 :     }
335 :     sub readFile {
336 :     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
337 :     my $in = shift;
338 :     return( Webwork::readFile($in) );
339 :     }
340 :     # sub hello {
341 :     # shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
342 :     # print "Receiving request for hello world\n";
343 :     # return "Hello world?";
344 :     # }
345 :    
346 :    
347 :     # sub tth {
348 :     # shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
349 :     # my $in = shift;
350 :     # my $tthpath = "/usr/local/bin/tth";
351 :     # my $out;
352 :     # $inputString = "<<END_OF_TTH_INPUT_STRING;\n\n\n" . $in . "\nEND_OF_TTH_INPUT_STRING\necho \"\" >/dev/null";
353 :     # #it's not clear why another command is needed.
354 :     #
355 :     # if (-x $tthpath ) {
356 :     # my $tthcmd = "$tthpath -L -f5 -r 2>/dev/null " . $inputString;
357 :     # if (open(TTH, "$tthcmd |")) {
358 :     # local($/);
359 :     # $/ = undef;
360 :     # $out = <TTH>;
361 :     # $/ = "\n";
362 :     # close(TTH);
363 :     # }else {
364 :     # $out = "<BR>there has been an error in executing $tthcmd<BR>";
365 :     # }
366 :     # } else {
367 :     # $out = "<BR> Can't execute the program tth at |$tthpath|<BR>";
368 :     # }
369 :     #
370 :     # #return "<!-- \r\n" . $in . "\r\n-->\r\n\r\n" . $out . "\r\n\r\n";
371 :     # return $out;
372 :     #
373 :     # }
374 :    
375 :     package Filter;
376 :    
377 :    
378 :     sub is_hash_ref {
379 :     my $in =shift;
380 :     my $save_SIG_die_trap = $SIG{__DIE__};
381 :     $SIG{__DIE__} = sub {CORE::die(@_) };
382 :     my $out = eval{ %{ $in } };
383 :     $out = ($@ eq '') ? 1 : 0;
384 :     $@='';
385 :     $SIG{__DIE__} = $save_SIG_die_trap;
386 :     $out;
387 :     }
388 :     sub is_array_ref {
389 :     my $in =shift;
390 :     my $save_SIG_die_trap = $SIG{__DIE__};
391 :     $SIG{__DIE__} = sub {CORE::die(@_) };
392 :     my $out = eval{ @{ $in } };
393 :     $out = ($@ eq '') ? 1 : 0;
394 :     $@='';
395 :     $SIG{__DIE__} = $save_SIG_die_trap;
396 :     $out;
397 :     }
398 :     sub filterObject {
399 :    
400 :     my $is_hash = 0;
401 :     my $is_array =0;
402 :     my $obj = shift;
403 :     #print "Enter filterObject ", ref($obj), "\n";
404 :     my $type = ref($obj);
405 :     unless ($type) {
406 :     #print "leave filterObject with nothing\n";
407 :     return($obj);
408 :     }
409 :    
410 :    
411 :     if ( is_hash_ref($obj) ) {
412 :     #print "enter hash ", %{$obj},"\n";
413 :     my %obj_container= %{$obj};
414 :     foreach my $key (keys %obj_container) {
415 :     $obj_container{$key} = filterObject( $obj_container{$key} );
416 :     #print $key, " ", ref($obj_container{$key})," ", $obj_container{$key}, "\n";
417 :     }
418 :     #print "leave filterObject with HASH\n";
419 :     return( bless(\%obj_container,'HASH'));
420 :     };
421 :    
422 :    
423 :    
424 :     if ( is_array_ref($obj) ) {
425 :     #print "enter array ( ", @{$obj}," )\n";
426 :     my @obj_container= @{$obj};
427 :     foreach my $i (0..$#obj_container) {
428 :     $obj_container[$i] = filterObject( $obj_container[$i] );
429 :     #print "\[$i\] ", ref($obj_container[$i])," ", $obj_container[$i], "\n";
430 :     }
431 :     #print "leave filterObject with ARRAY\n";
432 :     return( bless(\@obj_container,'ARRAY'));
433 :     };
434 :    
435 :     }
436 :    
437 :    
438 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9