9 Simplified page object, now a superclass of CXGN::Page. Also changed
10 to be a singleton class. Provides the apache_request bindings for
11 argument retrieval, but doesn't do much else. The motivation for this
12 module was to create a subclass of a simple page object for AJAX
21 use Apache2
::RequestUtil
();
37 Creates returns the singleton page-scrap object.
40 my $scrap=CXGN::Scrap->new();
46 my $self = bless {},$class;
47 $self->{request
} ||= Apache2
::RequestUtil
->request();
48 $self->{apache_request
} ||= Apache2
::Request
->new($self->{request
});
49 $self->{content_type
} = 'text/html';
50 $self->{vhost
} = SGN
::Context
->instance;
54 =head2 get_encoded_arguments
56 Gets arguments which are being sent in via GET or POST (doesn't matter which). Encodes the HTML entities in those arguments to keep clients from being able to submit evil HTML, javascript, etc. to be printed on our pages.
59 my($id,$name)=$scrap->get_encoded_arguments("id","name");
63 # use this one for all alphanumeric arguments, unless for some reason you don't
64 # want to filter out possibly evil characters (see below)
65 sub get_encoded_arguments
{
68 return map {HTML
::Entities
::encode_entities
($_,"<>&'\"")} $self->get_arguments(@items);
69 # encoding does not appear to work for foreign characters with
70 # umlauts, etc. so we're using this restricted version of the command
74 =head2 get_all_encoded_arguments
77 Ret : hash of ( argument name => HTML-encoded value of that argument )
79 WARNING: this method does not work for POSTs of type multipart/form-data, particularly with file uploads. This method only works with GET and other POST requests.
83 sub get_all_encoded_arguments
{
85 my $r = $self->{apache_request
};
86 my @paramnames = $r->param;
88 my $p = $self->get_arguments($_);
89 $_ => HTML
::Entities
::encode_entities
($p,"<>&'\";");
96 Get the L<Apache2::Upload> object for the currently uploaded file, if any.
102 my @upload_names = $self->{apache_request
}->upload();
104 return map $self->{apache_request
}->upload($_), @upload_names;
106 return $self->{apache_request
}->upload( $upload_names[0] );
112 Gets arguments which are being sent in via GET or POST (doesn't matter which). DOES NOT encode the HTML entities in those arguments, so be careful because it IS possible for clients to submit evil HTML, javascript, etc.
115 my($fasta_file)=$scrap->get_arguments("fasta_file");
119 # only use this method if you need unfiltered arguments with weird characters
120 # in them, like passwords and fasta file data. be aware that the user's agent
121 # (browser) could be capable of sending ALMOST ANYTHING to you as parameters.
125 my $apr = $self->{apache_request
};
127 my @p = $apr->param($_);
129 carp
"WARNING: multiple parameters returned for argument '$_'";
133 no warnings
'uninitialized';
134 length $p[0] ?
$p[0] : undef
137 return @args if wantarray;
143 Usage: $scrap->jsan_use('MyModule.Name');
144 Desc : add a javascript module (and its dependent javascript
145 modules) to the list of js modules needed by this page scrap
146 Args : list of module names in My.Module.Name form
147 Ret : nothing meaningful
152 my ($self,@uses) = @_;
153 $self->_jsan->add(my $a = $_) foreach @uses;
156 =head2 jsan_render_includes
158 Usage: my $str = $scrap->jsan_render_includes
159 Desc : render HTML script-tag includes for javascript
160 modules required with jsan_use(), plus the globally-used
161 javascript modules defined herein, currently:
164 CXGN.Page.FormattingHelpers
169 Ret : a string containing zero or more newline-separated
177 CXGN.Page.FormattingHelpers
181 sub jsan_render_includes
{
184 # add in our global JS, which is used for every page
185 # JSAN::ServerSide is pretty badly written. cannot use $_ to
186 # pass the name to add()
187 foreach my $js (@global_js) {
188 $self->_jsan->add($js);
192 map qq|<script language
="JavaScript" src
="$_" type
="text/javascript"></script
>|,
199 return $self->{jsan_serverside
} ||= JSAN
::ServerSide
->new( %{ $self->{context
}->_jsan_params } );
204 Wrapper for CGI->new()->Vars(). Used when you have many arguments with the same name.
207 my %params=$scrap->params();
211 # if you have lists of same-named parameters you will probably want to
212 # handle that yourself using this function which returns a hash (not
215 return CGI
->new()->Vars();
220 Usage: my $hostname = $page->hostname();
221 Desc : get the hostname in the current page request (from
222 Apache::Request::hostname())
224 Ret : hostname string
230 shift->{request
}->hostname();
235 Usage: my $req = $page->get_request();
236 Desc : get the Apache->request() object for the current page
239 Ret : an Apache object
249 =head2 get_apache_request
251 Usage: my $areq = $page->apache_request();
252 Desc : get the Apache::Request for this request, equivalent to
253 Apache::Request->instance( $page->request() )
255 Ret : an L<Apache::Request> object
260 sub get_apache_request
{
261 shift->{apache_request
}
265 =head2 is_bot_request
267 Usage: print "it's a bot" if $page->is_bot_request;
268 Desc : return true if this page request is probably coming from a
277 my $user_agent = shift->get_request->headers_in->{'User-Agent'};
280 ( $user_agent =~ m
|bot
/\d
|i
#< will get google, msn
281 || $user_agent =~ /Yahoo!?\s+Slurp/i #< will get yahoo
288 =head2 send_content_type_header
290 Usage: $page->send_content_type_header()
291 Desc : set an http content-type header
292 Args : optional string content type to send,
293 defaults to 'text/html'
294 Ret : nothing meaningful
295 Side Effects: dies on error
300 sub send_content_type_header
{
301 my ( $self, $type ) = @_;
302 $self->{request
}->content_type( $type || $self->{content_type
} || 'text/html');
308 Usage: $page->path_to('/something/somewhere.txt');
309 Desc : get the full path to a file relative to the
310 base path of the web server
311 Args : file path relative to the site root
312 Ret : absolute file path
313 Side Effects: dies on error
318 shift->{vhost
}->path_to(@_)
322 =head2 tempfiles_subdir
324 Usage: my $dir = $page->tempfiles_subdir('some','dir');
325 Desc : get the path to this site's web-server-accessible tempfiles directory.
326 Args : (optional) one or more directory names to append onto the tempdir root
327 Ret : full filesystem pathname to the relevant dir
328 Side Effects: dies on error
332 sub tempfiles_subdir
{
333 shift->{vhost
}->tempfiles_subdir(@_);
339 convenience method, forwards to to CXGN::VHost::get_conf();
341 equivalent to doing CXGN::VHost->new->get_conf(@_);
347 return $self->{vhost
}->get_conf(@_);
353 John Binns - John Binns <zombieite@gmail.com> (methods)
355 Christopher Carpita <csc32@cornell.edu> (constructor/org)