Merge pull request #311 from solgenomics/topic/accession_list_trial_search
[sgn.git] / lib / CXGN / Scrap.pm
blobd7f78560041a70d24d1e2f8096911433ba64588a
1 package CXGN::Scrap;
3 =head1 NAME
5 CXGN::Scrap
7 =head1 DEPRECATED
9 Deprecated. Do not use in new code.
11 =cut
14 # =head1 DESCRIPTION
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.
21 # =cut
24 use strict;
25 use warnings;
26 use HTML::Entities ();
27 use Carp;
28 use CGI ();
29 use File::Path ();
30 use JSAN::ServerSide;
31 use CatalystX::GlobalContext '$c';
33 # =head1 OBJECT METHODS
35 # =head2 new
37 # Creates returns the singleton page-scrap object.
39 # #Example
40 # my $scrap=CXGN::Scrap->new();
42 # =cut
44 sub new {
45 my $class=shift;
46 my $self = bless {},$class;
47 $self->{content_type} = 'text/html';
48 $self->{request} = $c ? $c->request : '';
50 return $self;
53 sub get_request {
54 shift->{request}
56 sub get_apache_request {
57 shift->{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.
65 # #Example
66 # my($id,$name)=$scrap->get_encoded_arguments("id","name");
68 # =cut
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 {
73 my($self,@items)=@_;
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
83 # Args: none
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.
88 # =cut
90 sub get_all_encoded_arguments {
91 my ($self) = @_;
93 my @paramnames = $c->req->param;
94 return map {
95 my $p = $self->get_arguments($_);
96 $_ => HTML::Entities::encode_entities($p,"<>&'\";");
97 } @paramnames;
101 # =head2 get_upload
103 # Get the L<Catalyst::Request::Upload> object for the currently uploaded file, if any.
104 # This is compatible with L<Apache2::Upload>.
106 # =cut
108 sub get_upload {
109 # use catalyst to list file uploads
110 my ($field) = $c->req->upload
111 or return;
113 # and use CGI to actually read them, because they have been
114 # spooled for CGI's benefit by the catalyst CGI adaptor
115 my $cgi = CGI->new;
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.
124 # #Example
125 # my($fasta_file)=$scrap->get_arguments("fasta_file");
127 # =cut
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.
132 # --john
133 sub get_arguments {
134 my($self,@items)=@_;
135 my @args = map {
136 my @p = $c->req->param($_);
137 if(@p > 1) {
138 carp "WARNING: multiple parameters returned for argument '$_'";
141 else {
142 no warnings 'uninitialized';
143 length $p[0] ? $p[0] : undef
145 } @items;
146 return @args if wantarray;
147 return $args[0];
150 # =head2 jsan_use
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
158 # =cut
160 sub jsan_use {
161 shift;
162 push @{ $c->stash->{js_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:
172 # CXGN.Effects
173 # CXGN.Page.FormattingHelpers
174 # CXGN.Page.Toolbar
175 # CXGN.UserPrefs
177 # Args : none
178 # Ret : a string containing zero or more newline-separated
179 # include statements
180 # Side Effects: none
182 # =cut
184 sub jsan_render_includes { '' }
186 # sub jsan_render_includes {
187 # my ($self) = @_;
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);
197 # return join "\n",
198 # map qq|<script language="JavaScript" src="$_" type="text/javascript"></script>|,
199 # $self->_jsan->uris;
203 # =head2 cgi_params
205 # Wrapper for CGI->new()->Vars(). Used when you have many arguments with the same name.
207 # #Example
208 # my %params=$scrap->params();
210 # =cut
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
214 # hash reference)
215 sub cgi_params {
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())
224 # Args : none
225 # Ret : hostname string
226 # Side Effects: none
228 # =cut
230 sub get_hostname {
231 my (undef,$n) = split m| [:/]+ |x, $c->req->base;
232 return $n;
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
240 # web-crawling robot
241 # Args : none
242 # Ret : boolean
243 # Side Effects: none
245 # =cut
247 sub is_bot_request {
248 my $user_agent = $c->req->user_agent;
250 return 1 if
251 $user_agent =~ m|bot/\d|i #< will get google, msn
252 || $user_agent =~ /Yahoo!?\s+Slurp/i #< will get yahoo
255 return;
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
268 # =cut
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;
278 # =head2 path_to
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
287 # =cut
289 sub path_to {
290 shift;
291 $c->path_to(@_)
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
303 # =cut
305 sub tempfiles_subdir {
306 shift;
307 $c->tempfiles_subdir(@_);
311 sub get_conf {
312 my $self = shift;
313 return $c->get_conf(@_);
317 package fake_apache_upload;
318 use Moose;
319 use namespace::autoclean;
321 has 'cgi_upload' => ( is => 'ro', required => 1 );
323 sub fh {
324 return shift->cgi_upload;
327 __PACKAGE__->meta->make_immutable;
329 ####
330 1; # do not remove
331 ####