9 Deprecated. Do not use in new code.
16 # Simplified page object, now a superclass of CXGN::Page. Also changed
17 # to be a singleton class. Provides the bindings for argument
18 # retrieval, but doesn't do much else. The motivation for this module
19 # was to create a subclass of a simple page object for AJAX requests.
26 use HTML
::Entities
();
31 use CatalystX
::GlobalContext
'$c';
33 # =head1 OBJECT METHODS
37 # Creates returns the singleton page-scrap object.
40 # my $scrap=CXGN::Scrap->new();
46 my $self = bless {},$class;
47 $self->{content_type
} = 'text/html';
48 $self->{request
} = $c ?
$c->request : '';
56 sub get_apache_request
{
61 # =head2 get_encoded_arguments
63 # 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.
66 # my($id,$name)=$scrap->get_encoded_arguments("id","name");
70 # use this one for all alphanumeric arguments, unless for some reason you don't
71 # want to filter out possibly evil characters (see below)
72 sub get_encoded_arguments
{
75 return map {HTML
::Entities
::encode_entities
($_,"<>&'\"")} $self->get_arguments(@items);
76 # encoding does not appear to work for foreign characters with
77 # umlauts, etc. so we're using this restricted version of the command
81 # =head2 get_all_encoded_arguments
84 # Ret : hash of ( argument name => HTML-encoded value of that argument )
86 # 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.
90 sub get_all_encoded_arguments
{
93 my @paramnames = $c->req->param;
95 my $p = $self->get_arguments($_);
96 $_ => HTML
::Entities
::encode_entities
($p,"<>&'\";");
103 # Get the L<Catalyst::Request::Upload> object for the currently uploaded file, if any.
104 # This is compatible with L<Apache2::Upload>.
109 # use catalyst to list file uploads
110 my ($field) = $c->req->upload
113 # and use CGI to actually read them, because they have been
114 # spooled for CGI's benefit by the catalyst CGI adaptor
116 my @uploads = map fake_apache_upload
->new( cgi_upload
=> $_ ), CGI
->new->upload( $field );
117 return wantarray ?
@uploads : $uploads[0];
120 # =head2 get_arguments
122 # 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.
125 # my($fasta_file)=$scrap->get_arguments("fasta_file");
129 # only use this method if you need unfiltered arguments with weird characters
130 # in them, like passwords and fasta file data. be aware that the user's agent
131 # (browser) could be capable of sending ALMOST ANYTHING to you as parameters.
136 my @p = $c->req->param($_);
138 carp
"WARNING: multiple parameters returned for argument '$_'";
142 no warnings
'uninitialized';
143 length $p[0] ?
$p[0] : undef
146 return @args if wantarray;
152 # Usage: $scrap->jsan_use('MyModule.Name');
153 # Desc : add a javascript module (and its dependent javascript
154 # modules) to the list of js modules needed by this page scrap
155 # Args : list of module names in My.Module.Name form
156 # Ret : nothing meaningful
162 push @
{ $c->stash->{jsan_classes
} }, @_
165 # =head2 jsan_render_includes
167 # Usage: my $str = $scrap->jsan_render_includes
168 # Desc : render HTML script-tag includes for javascript
169 # modules required with jsan_use(), plus the globally-used
170 # javascript modules defined herein, currently:
173 # CXGN.Page.FormattingHelpers
178 # Ret : a string containing zero or more newline-separated
184 sub jsan_render_includes
{ '' }
186 # sub jsan_render_includes {
190 # # add in our global JS, which is used for every page
191 # # JSAN::ServerSide is pretty badly written. cannot use $_ to
192 # # pass the name to add()
193 # foreach my $js (@global_js) {
194 # $self->_jsan->add($js);
198 # map qq|<script language="JavaScript" src="$_" type="text/javascript"></script>|,
199 # $self->_jsan->uris;
205 # Wrapper for CGI->new()->Vars(). Used when you have many arguments with the same name.
208 # my %params=$scrap->params();
212 # if you have lists of same-named parameters you will probably want to
213 # handle that yourself using this function which returns a hash (not
216 return CGI
->new->Vars;
219 # =head2 get_hostname
221 # Usage: my $hostname = $page->hostname();
222 # Desc : get the hostname in the current page request (from
223 # CGI::server_name())
225 # Ret : hostname string
231 my (undef,$n) = split m
| [:/]+ |x
, $c->req->base;
236 # =head2 is_bot_request
238 # Usage: print "it's a bot" if $page->is_bot_request;
239 # Desc : return true if this page request is probably coming from a
248 my $user_agent = $c->req->user_agent;
251 $user_agent =~ m
|bot
/\d
|i
#< will get google, msn
252 || $user_agent =~ /Yahoo!?\s+Slurp/i #< will get yahoo
259 # =head2 send_content_type_header
261 # Usage: $page->send_content_type_header()
262 # Desc : set an http content-type header
263 # Args : optional string content type to send,
264 # defaults to 'text/html'
265 # Ret : nothing meaningful
266 # Side Effects: dies on error
271 sub send_content_type_header
{
272 my ( $self, $type ) = @_;
273 $c->res->content_type($type || $self->{content_type
} || 'text/html');
274 print "\n" unless $c->res->body;
280 # Usage: $page->path_to('/something/somewhere.txt');
281 # Desc : get the full path to a file relative to the
282 # base path of the web server
283 # Args : file path relative to the site root
284 # Ret : absolute file path
285 # Side Effects: dies on error
295 # =head2 tempfiles_subdir
297 # Usage: my $dir = $page->tempfiles_subdir('some','dir');
298 # Desc : get the path to this site's web-server-accessible tempfiles directory.
299 # Args : (optional) one or more directory names to append onto the tempdir root
300 # Ret : full filesystem pathname to the relevant dir
301 # Side Effects: dies on error
305 sub tempfiles_subdir
{
307 $c->tempfiles_subdir(@_);
313 return $c->get_conf(@_);
317 package fake_apache_upload
;
319 use namespace
::autoclean
;
321 has
'cgi_upload' => ( is
=> 'ro', required
=> 1 );
324 return shift->cgi_upload;
327 __PACKAGE__
->meta->make_immutable;