Parent Directory
|
Revision Log
added REAL logout support. keys now get invalidated at logout. also, fixed a bug in classlist (see the diff). also, added a sub to Utils (see the diff). -sam
1 ################################################################################ 2 # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project 3 # $Id$ 4 ################################################################################ 5 6 package WeBWorK::ContentGenerator; 7 8 =head1 NAME 9 10 WeBWorK::ContentGenerator - base class for modules that generate page content. 11 12 =cut 13 14 use strict; 15 use warnings; 16 use Apache::Constants qw(:common); 17 use CGI qw(); 18 use URI::Escape; 19 use WeBWorK::DB::Auth; 20 use WeBWorK::Utils qw(readFile); 21 use Carp qw(cluck); 22 23 ################################################################################ 24 # This is a very unruly file, so I'm going to use very large comments to divide 25 # it into logical sections. 26 ################################################################################ 27 28 # new(Apache::Request, WeBWorK::CourseEnvironment) - create a new instance of a 29 # content generator. Usually only called by the dispatcher, although one might 30 # be able to use it for things like "sub-requests". Uh... uh... I have to think 31 # about that one. The dispatcher uses this idiom: 32 # 33 # 34 # WeBWorK::ContentGenerator::WHATEVER->new($r, $ce)->go(@whatever); 35 # 36 # and throws away the result ;) 37 # 38 sub new($$$) { 39 my $invocant = shift; 40 my $class = ref($invocant) || $invocant; 41 my $self = {}; 42 ($self->{r}, $self->{courseEnvironment}) = @_; 43 bless $self, $class; 44 return $self; 45 } 46 47 ################################################################################ 48 # Invocation and template processing 49 ################################################################################ 50 51 # go(@otherArguments) - render a page, using methods from the particular 52 # subclass of ContentGenerator. @otherArguments is passed to each method, so 53 # that the dispatcher can pass CG-specific data. The order of calls looks like 54 # this: 55 # 56 # * &pre_header_initialize - give subclasses a chance to do initialization 57 # necessary for generating the HTTP header. 58 # * &header - this class provides a standard HTTP header with Content-Type 59 # text/html. Subclasses are welcome to overload this for things like 60 # an image-creation content generator or a PDF generator. 61 # * &initialize - let subclasses do post-header initialization. 62 # * any "template escapes" defined in the system template and supported by 63 # the subclass. Generic implementations of &title and &body are provided. 64 # 65 sub go { 66 my $self = shift; 67 my $r = $self->{r}; 68 my $courseEnvironment = $self->{courseEnvironment}; 69 70 $self->pre_header_initialize(@_) if $self->can("pre_header_initialize"); 71 $self->header(@_); 72 return OK if $r->header_only; 73 74 $self->initialize(@_) if $self->can("initialize"); 75 $self->template($courseEnvironment->{templates}->{system}, @_); 76 77 return OK; 78 } 79 80 # template(STRING, @otherArguments) - parse a template, looking for escapes of 81 # the form <!--#NAME ARG1="FOO" ARG2="BAR"--> and calling a member function NAME 82 # (if available) for each NAME. The escapes are called like: 83 # 84 # $self->NAME(@otherArguments, \%escapeArguments) 85 # 86 # where @otherArguments originates in the dispatcher and %escapeArguments is 87 # parsed out of the escape itself (i.e. ARG1 => FOO, ARG2 => BAR) 88 # 89 sub template { 90 my ($self, $templateFile) = (shift, shift); 91 my $r = $self->{r}; 92 my $courseEnvironment = $self->{courseEnvironment}; 93 my @ifstack = (1); # Start off in printing mode 94 # say $ifstack[-1] to get the result of the last <#!--if--> 95 96 # so even though the variable $/ APPEARS to contain a newline, 97 # <TEMPLATE> is slurping the whole file into the first element of 98 # @template ONLY AFTER THE TRANSLATOR RUNS. WTF!!! 99 # 100 #open(TEMPLATE, $templateFile) or die "Couldn't open template $templateFile"; 101 #my @template = <TEMPLATE>; 102 #close TEMPLATE; 103 # 104 # Let's try something else instead: 105 my @template = split /\n/, readFile($templateFile); 106 107 foreach my $line (@template) { 108 # This is incremental regex processing. 109 # the /c is so that pos($line) doesn't die when the regex fails. 110 while ($line =~ m/\G(.*?)<!--#(\w*)((?:\s+.*?)?)-->/gc) { 111 my ($before, $function, $raw_args) = ($1, $2, $3); 112 my @args = ($raw_args =~ /\S/) ? cook_args($raw_args) : (); 113 114 if ($ifstack[-1]) { 115 print $before; 116 } 117 118 if ($function eq "if") { 119 push @ifstack, $self->$function(@_, [@args]); 120 } elsif ($function eq "else" and @ifstack > 1) { 121 $ifstack[-1] = not $ifstack[-1]; 122 } elsif ($function eq "endif" and @ifstack > 1) { 123 pop @ifstack; 124 } elsif ($ifstack[-1]) { 125 if ($self->can($function)) { 126 print $self->$function(@_, {@args}); 127 } 128 } 129 } 130 131 if ($ifstack[-1]) { 132 print substr($line, (defined pos $line) ? pos $line : 0), "\n"; 133 } 134 } 135 } 136 137 # cook_args(STRING) - parses a string of the form ARG1="FOO" ARG2="BAR". Returns 138 # a list which pairs into key/values and fits nicely in {}s. 139 # 140 sub cook_args($) { 141 my ($raw_args) = @_; 142 my @args = (); 143 144 # Boy I love m//g in scalar context! Go read the camel book, heathen. 145 # First, get the whole token with the quotes on both ends... 146 while ($raw_args =~ m/\G\s*(\w*)="((?:[^"\\]|\\.)*)"/g) { 147 my ($key, $value) = ($1, $2); 148 # ... then, rip out all the protecty backspaces 149 $value =~ s/\\(.)/$1/g; 150 push @args, $key => $value; 151 } 152 153 return @args; 154 } 155 156 # This is different. It probably shouldn't print anything (except in debugging cases) 157 # and it should return a boolean, not a string. &if is called in a nonstandard way 158 # by &template, with $args as an arrayref instead of a hashref. this is a hack! yay! 159 160 # OK, this is a pluggin architecture. it iterates through attributes of the "if" tag, 161 # and for each predicate $p, it calls &if_$p in an object-oriented way, continuing the 162 # grand templating theme of an object-oriented pluggable architecture using ->can($). 163 sub if { 164 my ($self, $args) = @_[0,-1]; 165 # A single if "or"s it's components. Nesting produces "and". 166 167 my @args = @$args; # Hahahahaha, get it?! 168 169 if (@args % 2 != 0) { 170 # flip out and kill people, but do not commit seppuku 171 print '<!--&if recieved an uneven number of arguments. This shouldn\'t happen, but I\'ll let it slide.-->\n'; 172 } 173 174 while (@args > 1) { 175 my ($key, $value) = (shift @args, shift @args); 176 177 # a non-existent &if_$key is the same as a false result, but we're ORing, so it's OK 178 my $sub = "if_$key"; # perl doesn't like it when you try to construct a string right in a method invocation 179 if ($self->can("if_$key") and $self->$sub("$value")) { 180 return 1; 181 } 182 } 183 184 return 0; 185 } 186 187 ################################################################################ 188 # Macros used by content generators to render common idioms 189 ################################################################################ 190 191 # pathMacro(HASHREF, LIST) - helper macro for <!--#path--> escape: the hash 192 # reference contains the "style", "image", and "text" arguments to the escape. 193 # The LIST consists of ordered key-value pairs of the form: 194 # 195 # "Page Name" => URL 196 # 197 # If the page should not have a link associated with it, the URL should be left 198 # empty. Authentication data is added to the URL so you don't have to. A fully- 199 # formed path line is returned, suitable for returning by a function 200 # implementing the #path escape. 201 # 202 sub pathMacro { 203 my $self = shift; 204 my %args = %{ shift() }; 205 my @path = @_; 206 my $sep; 207 if ($args{style} eq "image") { 208 $sep = CGI::img({-src=>$args{image}, -alt=>$args{text}}); 209 } else { 210 $sep = $args{text}; 211 } 212 my $auth = $self->url_authen_args; 213 my @result; 214 while (@path) { 215 my $name = shift @path; 216 my $url = shift @path; 217 push @result, $url 218 ? CGI::a({-href=>"$url?$auth"}, $name) 219 : $name; 220 } 221 return join($sep, @result), "\n"; 222 } 223 224 sub siblingsMacro { 225 my $self = shift; 226 my @siblings = @_; 227 my $sep = CGI::br(); 228 my $auth = $self->url_authen_args; 229 my @result; 230 while (@siblings) { 231 my $name = shift @siblings; 232 my $url = shift @siblings; 233 push @result, $url 234 ? CGI::a({-href=>"$url?$auth"}, $name) 235 : $name; 236 } 237 return join($sep, @result), "\n"; 238 } 239 240 sub navMacro { 241 my $self = shift; 242 my %args = %{ shift() }; 243 my @links = @_; 244 my $auth = $self->url_authen_args; 245 my @result; 246 while (@links) { 247 my $name = shift @links; 248 my $url = shift @links; 249 push @result, $url 250 ? CGI::a({-href=>"$url?$auth"}, $name) 251 : $name; 252 } 253 return join($args{separator}, @result), "\n"; 254 } 255 256 # hidden_fields(LIST) - return hidden <INPUT> tags for each field mentioned in 257 # LIST (or all fields if list is empty), taking data from the current request. 258 # 259 sub hidden_fields($;@) { 260 my $self = shift; 261 my $r = $self->{r}; 262 my @fields = @_; 263 @fields or @fields = $r->param; 264 my $courseEnvironment = $self->{courseEnvironment}; 265 my $html = ""; 266 267 foreach my $param (@fields) { 268 my $value = $r->param($param); 269 $html .= CGI::input({-type=>"hidden",-name=>"$param",-value=>"$value"}); 270 } 271 return $html; 272 } 273 274 # hidden_authen_fields() - use hidden_fields to return hidden <INPUT> tags for 275 # request fields used in authentication. 276 # 277 sub hidden_authen_fields($) { 278 my $self = shift; 279 return $self->hidden_fields("user","effectiveUser","key"); 280 } 281 282 # url_args(LIST) - return a URL query string (without the leading `?') 283 # containing values for each field mentioned in LIST, or all fields if list is 284 # empty. Data is taken from the current request. 285 # 286 sub url_args($;@) { 287 my $self = shift; 288 my $r = $self->{r}; 289 my @fields = @_; 290 @fields or @fields = $r->param; 291 my $courseEnvironment = $self->{courseEnvironment}; 292 293 my @pairs; 294 foreach my $param (@fields) { 295 my $value = $r->param($param) || ""; 296 push @pairs, uri_escape($param) . "=" . uri_escape($value); 297 } 298 299 return join("&", @pairs); 300 } 301 302 # url_authen_args() - use url_args to return a URL query string for request 303 # fields used in authentication. 304 # 305 sub url_authen_args($) { 306 my $self = shift; 307 my $r = $self->{r}; 308 return $self->url_args("user","effectiveUser","key"); 309 } 310 311 # print_form_data(BEGIN, MIDDLE, END, OMIT) - return a string containing request 312 # fields not matched by OMIT, placing BEGIN before each field name, MIDDLE 313 # between each field and its value, and END after each value. Values are taken 314 # from the current request. OMIT is a quoted reguar expression. 315 # 316 sub print_form_data { 317 my ($self, $begin, $middle, $end, $qr_omit) = @_; 318 my $return_string = ""; 319 my $r=$self->{r}; 320 my @form_data = $r->param; 321 foreach my $name (@form_data) { 322 next if ($qr_omit and $name =~ /$qr_omit/); 323 my @values = $r->param($name); 324 foreach my $variable (qw(begin name middle value end)) { 325 no strict 'refs'; 326 ${$variable} = "" unless defined ${$variable}; 327 } 328 foreach my $value (@values) { 329 $return_string .= "$begin$name$middle$value$end"; 330 } 331 } 332 return $return_string; 333 } 334 335 ################################################################################ 336 # Generic versions of template escapes 337 ################################################################################ 338 339 # Reminder: here are the template functions currently defined: 340 # 341 # head 342 # path 343 # style = text|image 344 # image = URL of image 345 # text = text separator 346 # links 347 # siblings 348 # nav 349 # style = text|image 350 # imageprefix = prefix to image URL 351 # imagesuffix = suffix to image URL 352 # separator = HTML to place in between links 353 # title 354 # body 355 356 sub header { 357 my $self = shift; 358 my $r = $self->{r}; 359 $r->content_type('text/html'); 360 $r->send_http_header(); 361 } 362 363 # drunk code. rewrite. 364 sub links { 365 my $self = shift; 366 my $ce = $self->{courseEnvironment}; 367 my $userName = $self->{r}->param("user"); 368 my $courseName = $ce->{courseName}; 369 my $root = $ce->{webworkURLs}->{root}; 370 my $permLevel = WeBWorK::DB::Auth->new($ce)->getPermissions($userName); 371 my $key = WeBWorK::DB::Auth->new($ce)->getKey($userName); 372 return "" unless defined $key; 373 374 my $probSets = "$root/$courseName/?" . $self->url_authen_args(); 375 my $prefs = "$root/$courseName/options/?" . $self->url_authen_args(); 376 my $prof = "$root/$courseName/prof/?" . $self->url_authen_args(); 377 my $help = $ce->{webworkURLs}->{docs} . "?" . $self->url_authen_args(); 378 my $logout = "$root/$courseName/logout/?" . $self->url_authen_args(); 379 380 my $profLine = ($permLevel > 0) 381 ? CGI::a({-href=>$prof}, "Professor") . CGI::br() 382 : ""; 383 #if ($permLevel > 0) { 384 # $profLine = CGI::a({-href=>$prof}, "Professor") . CGI::br(), 385 #} 386 387 return 388 CGI::a({-href=>$probSets}, "Problem Sets"), CGI::br(), 389 CGI::a({-href=>$prefs}, "User Options"), CGI::br(), 390 $profLine, 391 CGI::a({-href=>$help}, "Help"), CGI::br(), 392 CGI::a({-href=>$logout}, "Log Out"), CGI::br(), 393 ; 394 } 395 396 # &if_can will return 1 if the current object->can("do $_[1]") 397 sub if_can ($$) { 398 my ($self, $arg) = (@_); 399 400 if ($self->can("$arg")) { 401 return 1; 402 } else { 403 return 0; 404 } 405 } 406 407 1; 408 409 __END__ 410 411 =head1 AUTHOR 412 413 Written by Dennis Lambe Jr., malsyned (at) math.rochester.edu 414 and Sam Hathaway, sh002i (at) math.rochester.edu. 415 416 =cut
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |