blib/lib/Bio/Graphics/Browser2.pm untested

filter: Statementstmt 0.0% 0 / 337 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 150 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 92 Podpod 0.0% 0 / 79 totaltotal 0.0% 0 / 748 CC 46 SCAR 21.4
CC
46
Cov
0%
session_driver
42.0
resolve_path
30.0
data_source_show
20.0
CRAP 8.5
1 package Bio::Graphics::Browser2;
2 # $Id$
3 # Globals and utilities for GBrowse and friends
4  
5 our $VERSION = '2.55';
6  
7 use strict;
8 use warnings;
9 use base 'Bio::Graphics::Browser2::AuthorizedFeatureFile';
10  
11 use File::Spec;
12 use File::Path 'mkpath';
13 use File::Basename 'dirname','basename';
14 use Text::ParseWords 'shellwords';
15 use File::Path 'mkpath';
16 use Bio::Graphics::Browser2::DataSource;
17 use Bio::Graphics::Browser2::Session;
18 use GBrowse::ConfigData;
19 use Carp qw(croak carp confess cluck);
20  
21 use constant DEFAULT_MASTER => 'GBrowse.conf';
22  
23 my (%CONFIG_CACHE,$HAS_DBFILE,$HAS_STORABLE);
24  
25 # Open a globals object with a config file in the standard location.
26 sub open_globals {
27 my $self = shift;
28 my $conf_dir = $self->config_base;
29 my $conf_file = $ENV{GBROWSE_MASTER} || DEFAULT_MASTER;
30 my $path = File::Spec->catfile($conf_dir,$conf_file);
31 die "No GBrowse configuration file at $path!" unless -r $path;
32 return $self->new($path);
33 }
34  
35 sub new {
36 my $class = shift;
37 my $config_file_path = shift;
38  
39 # Cache the config info so we don't need to reparse in a persistent (e.g. modperl) environment
40 my $mtime = (stat($config_file_path))[9] || 0;
41 if (exists $CONFIG_CACHE{$config_file_path}
42 && $CONFIG_CACHE{$config_file_path}{mtime} >= $mtime) {
43 return $CONFIG_CACHE{$config_file_path}{object};
44 }
45  
46 my $self = $class->SUPER::new(-file=>$config_file_path,
47 -safe=>1);
48  
49 # a little trick here -- force the setting of "config_base" from the config file
50 # base if not explicitly overridden
51 unless ($self->setting('general' => 'config_base')) {
52 my $dir = dirname($config_file_path);
53 $self->setting('general' => 'config_base',$dir);
54 }
55  
56 $CONFIG_CACHE{$config_file_path}{object} = $self;
57 $CONFIG_CACHE{$config_file_path}{mtime} = $mtime;
58 return $self;
59 }
60  
61 ## methods for dealing with paths
62 sub resolve_path {
63 my $self = shift;
64 my $path = shift;
65 my $path_type = shift; # one of "config" "htdocs" or "url"
66 return unless $path;
67 return $path if $path =~ m!^/!; # absolute path
68 return $path if $path =~ m!\|\s*$!; # a pipe
69 return $path if $path =~ m!^(http|ftp):!; # an URL
70 my $method = ${path_type}."_base";
71 $self->can($method) or confess "path_type must be one of 'config','htdocs', or 'url'";
72 my $base = $self->$method or return $path;
73 return File::Spec->catfile($base,$path);
74 }
75  
76 sub config_path {
77 my $self = shift;
78 my $option = shift;
79 $self->resolve_path($self->setting(general => $option),'config');
80 }
81  
82 sub htdocs_path {
83 my $self = shift;
84 my $option = shift;
85 $self->resolve_path($self->setting(general => $option),'htdocs')
86 || "$ENV{DOCUMENT_ROOT}/gbrowse2";
87 }
88  
89 sub url_path {
90 my $self = shift;
91 my $option = shift;
92 $self->resolve_path( scalar($self->setting(general => $option)),'url');
93 }
94  
95 sub config_base {$ENV{GBROWSE_CONF}
96 || eval {shift->setting(general=>'config_base')}
97 || GBrowse::ConfigData->config('conf')
98 || '/etc/GBrowse2' }
99 sub htdocs_base {$ENV{GBROWSE_HTDOCS}
100 || eval{shift->setting(general=>'htdocs_base')}
101 || GBrowse::ConfigData->config('htdocs')
102 || '/var/www/gbrowse2' }
103 sub url_base {eval{shift->setting(general=>'url_base')}
104 || basename(GBrowse::ConfigData->config('htdocs'))
105 || '/gbrowse2' }
106  
107 sub tmp_base {eval{shift->setting(general=>'tmp_base')}
108 || GBrowse::ConfigData->config('tmp')
109 || '/tmp' }
110 sub persistent_base {
111 my $self = shift;
112 my $base = $self->setting(general=>'persistent_base');
113 return $base || $self->tmp_base; # for compatibility with pre 2.27 installs
114 }
115 sub db_base {
116 my $self = shift;
117 my $base = $self->setting(general=>'db_base');
118 return $base || File::Spec->catfile(shift->persistent_base,'databases');
119 }
120 sub userdata_base {
121 my $self = shift;
122 my $base = $self->setting(general=>'userdata_base');
123 return $base || File::Spec->catfile($self->persistent_base,'userdata');
124 }
125  
126 # these are url-relative options
127 sub button_url { shift->url_path('buttons') }
128 sub balloon_url { shift->url_path('balloons') }
129 sub openid_url { shift->url_path('openid') }
130 sub js_url { shift->url_path('js') }
131 sub help_url { shift->url_path('gbrowse_help') }
132 sub stylesheet_url { shift->url_path('stylesheet') }
133 sub auth_plugin { shift->setting(general=>'authentication plugin') }
134  
135 # this returns the base URL and path info for use in constructing
136 # links. For example, if gbrowse is running at http://foo.bar/cgi-bin/gb2/gbrowse/yeast,
137 # it will return the list ('http://foo.bar/cgi-bin/gb2','yeast')
138 sub gbrowse_base {
139 my $self = shift;
140 my $url = CGI::url();
141 my $source = $self->get_source_from_cgi;
142 $source = CGI::escape($source);
143 $url =~ s!/[^/]*$!!;
144 return ($url,$source);
145 }
146  
147 # this returns the URL of the "master" gbrowse instance
148 sub gbrowse_url {
149 my $self = shift;
150 my $fallback_source = shift;
151 my ($base,$source) = $self->gbrowse_base;
152 $source ||= $fallback_source if $fallback_source;
153 return "$base/gbrowse/$source";
154 }
155  
156 sub make_path {
157 my $self = shift;
158 my $path = shift;
159 return unless $path =~ /^(.+)$/;
160 $path = $1;
161 mkpath($path,0,0777) unless -d $path;
162 }
163  
164 sub tmpdir {
165 my $self = shift;
166 my @components = @_;
167 my $path = File::Spec->catfile($self->tmp_base,@components);
168 $self->make_path($path) unless -d $path;
169 return $path;
170 }
171  
172 sub user_dir {
173 my $self = shift;
174 my @components = @_;
175 my $base = $self->userdata_base;
176 return File::Spec->catfile($base,@components);
177 }
178  
179 sub admin_dir {
180 my $self = shift;
181 my @components = @_;
182 my $path = $self->admin_dbs();
183 return File::Spec->catfile($path,@components);
184 }
185  
186 sub tmpimage_dir {
187 my $self = shift;
188 return $self->tmpdir('images',@_);
189 }
190  
191 sub image_url {
192 my $self = shift;
193 my $path = File::Spec->catfile($self->url_base,'i');
194 return $path;
195 }
196  
197 sub cache_dir {
198 my $self = shift;
199 my $path = File::Spec->catfile($self->tmp_base,'cache',@_);
200 $self->make_path($path) unless -d $path;
201 return $path;
202 }
203  
204 sub session_locks {
205 my $self = shift;
206 my $path = File::Spec->catfile($self->tmp_base,'locks',@_);
207 $self->make_path($path) unless -d $path;
208 return $path;
209 }
210  
211 # return one of
212 # 'flock' -- standard flock locking
213 # 'nfs' -- use File::NFSLock
214 # 'mysql' -- use mysql advisory locks
215 sub session_locktype {
216 my $self = shift;
217 return $self->setting(general=>'session lock type') || 'default';
218 }
219  
220 sub session_dir {
221 my $self = shift;
222 my $path = File::Spec->catfile($self->persistent_base,'sessions',@_);
223 $self->make_path($path) unless -d $path;
224 return $path;
225 }
226  
227 sub slave_dir {
228 my $self = shift;
229 my $path = $self->setting(general=>'tmp_slave') || '/tmp/gbrowse_slave';
230 $self->make_path($path) unless -d $path;
231 return $path;
232 }
233  
234 sub slave_status_path {
235 my $self = shift;
236 my $path = File::Spec->catfile($self->tmp_base,'slave_status');
237 return $path;
238 }
239  
240 # these are relative to the config base
241 sub plugin_path { shift->config_path('plugin_path') }
242 sub language_path { shift->config_path('language_path') }
243 sub templates_path { shift->config_path('templates_path') }
244 sub moby_path { shift->config_path('moby_path') }
245  
246 sub global_timeout { shift->setting(general=>'global_timeout') || 60 }
247 sub remember_settings_time { shift->setting(general=>'expire session') || '1M' }
248 sub cache_time { shift->setting(general=>'expire cache') || '2h' }
249 sub upload_time { shift->setting(general=>'expire uploads') || '6w' }
250 sub datasources_expire { shift->setting(general=>'expire data sources') || '10m' }
251 sub url_fetch_timeout { shift->setting(general=>'url_fetch_timeout') }
252 sub url_fetch_max_size { shift->setting(general=>'url_fetch_max_size') }
253  
254 sub application_name { shift->setting(general=>'application_name') || 'GBrowse' }
255 sub application_name_long { shift->setting(general=>'application_name_long') || 'The Generic Genome Browser' }
256 sub email_address { shift->setting(general=>'email_address') || 'noreply@gbrowse.com' }
257 sub smtp { my $smtp = shift->setting(general=>'smtp_gateway'); return if $smtp eq 'none'; return $smtp }
258 sub smtp_enabled { return defined shift->smtp; }
259 sub user_account_db { shift->setting(general=>'user_account_db') } # Used by uploads & user databases, they set their own defaults.
260 sub user_accounts { my $self = shift;
261 return $self->setting(general=>'user_accounts') ||
262 $self->setting(general=>'user_accounts') ||
263 0; }
264 sub user_accounts_allow_registration
265 {
266 my $val = shift->setting(general=>'user_accounts_registration');
267 return 1 unless defined $val;
268 return $val;
269 }
270 sub user_accounts_allow_openid
271 {
272 my $val = shift->setting(general=>'user_accounts_openid');
273 return 1 unless defined $val;
274 return $val;
275 }
276 sub public_files { shift->setting(general=>'public_files') || 10 }
277 sub admin_account { shift->setting(general=>'admin_account') }
278 sub admin_dbs { shift->setting(general=>'admin_dbs') }
279 sub openid_secret {
280 return GBrowse::ConfigData->config('OpenIDConsumerSecret')
281 }
282  
283 # uploads
284 sub upload_db_adaptor {
285 my $self = shift;
286 my $adaptor = $self->setting(general=>'upload_db_adaptor') || $self->setting(general=>'userdb_adaptor');
287 $adaptor or return;
288 warn "The upload_db_adaptor in your Gbrowse.conf file isn't in the DBI::<module> format: remember, it's not a connection string."
289 if $adaptor =~ /^DBI/ && $adaptor !~ /(^DBI::+)/i;
290 return $adaptor;
291 }
292 sub upload_db_host {
293 my $self = shift;
294 return $self->setting(general=>'upload_db_host') || $self->setting(general=>'userdb_host') || 'localhost'
295 }
296 sub upload_db_user {
297 my $self = shift;
298 return $self->setting(general=>'upload_db_user') || $self->setting(general=>'userdb_user') || '';
299 }
300 sub upload_db_pass {
301 my $self = shift;
302 return $self->setting(general=>'upload_db_pass') || $self->setting(general=>'userdb_pass') || '';
303 }
304  
305 sub session_driver {
306 my $self = shift;
307 my $driver = $self->setting(general=>'session driver');
308 return $driver if $driver;
309  
310 $HAS_DBFILE = eval "require DB_File; 1" || 0
311 unless defined $HAS_DBFILE;
312 $HAS_STORABLE = eval "require Storable; 1" || 0
313 unless defined $HAS_STORABLE;
314  
315 my $sdriver = $HAS_DBFILE ? 'db_file' : 'file';
316 my $serializer = $HAS_STORABLE ? 'storable' : 'default';
317  
318 return "driver:$sdriver;serializer:$serializer";
319 }
320  
321 sub session_args {
322 my $self = shift;
323 my %args = shellwords($self->setting(general=>'session args')||'');
324 return \%args if %args;
325 return {Directory=>$self->session_dir};
326 }
327  
328 ## methods for dealing with data sources
329 sub data_sources {
330 return sort grep {!/^\s*=~/ && !/:plugin$/} shift->SUPER::configured_types();
331 }
332  
333 sub data_source_description {
334 my $self = shift;
335 my $dsn = shift;
336 return $self->setting($dsn=>'description');
337 }
338  
339 sub data_source_restrict {
340 my $self = shift;
341 my $dsn = shift;
342 return $self->setting($dsn=>'restrict');
343 }
344  
345 sub data_source_show {
346 my $self = shift;
347 my $dsn = shift;
348 my ($username,$authenticator) = @_;
349 return if $self->setting($dsn=>'hide');
350  
351 # because globals are cached between use, we do not want usernames
352 # to be defined outside the scope of this call
353 local $self->{'.authenticated_username'} = $username if defined $username;
354 local $self->{'.authenticator'} = $authenticator if defined $authenticator;
355 return $self->authorized($dsn);
356 }
357  
358 sub data_source_path {
359 my $self = shift;
360 my $dsn = shift;
361 my ($regex_key) = grep { $dsn =~ /^$_$/ } map { $_ =~ s/^=~//; $_ } grep { $_ =~ /^=~/ } keys(%{$self->{config}});
362 if ($regex_key) {
363 my $path = $self->resolve_path($self->setting("=~".$regex_key=>'path'),'config');
364 my @matches = ($dsn =~ /$regex_key/);
365 for (my $i = 1; $i <= scalar(@matches); $i++) {
366 $path =~ s/\$$i/$matches[$i-1]/g;
367 }
368 return $self->resolve_path($path, 'config');
369 }
370 my $path = $self->setting($dsn=>'path') or return;
371 $self->resolve_path($path,'config');
372 }
373  
374 sub authorized {
375 my $self = shift;
376 my $sourcename = shift;
377 my ($username,$authenticator) = @_;
378 local $self->{'.authenticated_username'} = $username if defined $username;
379 local $self->{'.authenticator'} = $authenticator if defined $authenticator;
380 return $self->SUPER::authorized($sourcename);
381 }
382  
383 sub create_data_source {
384 my $self = shift;
385 my $dsn = shift;
386 my $path = $self->data_source_path($dsn) or return;
387 my ($regex_key) = grep { $dsn =~ /^$_$/ } map { $_ =~ s/^=~//; $_ } grep { $_ =~ /^=~/ } keys(%{$self->{config}});
388 my $name = $dsn;
389 if ($regex_key) { $dsn = "=~".$regex_key; }
390 my $source = Bio::Graphics::Browser2::DataSource->new($path,
391 $name,
392 $self->data_source_description($dsn),
393 $self) or return;
394 if (my $adbs = $self->admin_dbs) {
395 my $path = File::Spec->catfile($adbs,$dsn);
396 my $expr = "$path/*/*.conf";
397 $source->add_conf_files($expr);
398 }
399 return $source;
400 }
401  
402 sub max_features {
403 my $self = shift;
404 my $max = $self->setting(general => 'maximum features');
405 return 5000 unless defined $max;
406 return $max;
407 }
408  
409 sub default_source {
410 my $self = shift;
411 my $source = $self->setting(general => 'default source');
412 return $source if $self->valid_source($source);
413 return ($self->data_sources)[0];
414 }
415  
416 sub valid_source {
417 my $self = shift;
418 my $proposed_source = shift;
419  
420 if (!exists($self->{config}{$proposed_source})) {
421 my ($regex_key) = grep { $proposed_source =~ /^$_$/ } map { $_ =~ s/^=~//; $_ } grep { $_ =~ /^=~/ } keys(%{$self->{config}});
422 return unless $regex_key;
423 my $path = $self->data_source_path("=~" . $regex_key) or return;
424 return -e $path || $path =~ /\|\s*$/;
425 }
426  
427 return unless exists $self->{config}{$proposed_source};
428 my $path = $self->data_source_path($proposed_source) or return;
429 return -e $path || $path =~ /\|\s*$/;
430 }
431  
432 sub get_source_from_cgi {
433 my $self = shift;
434  
435 my $source = CGI::param('source') || CGI::param('src') || CGI::path_info();
436 $source =~ s!\#$!!; # get rid of trailing # left by IE
437 $source =~ s!^/+!!; # get rid of leading & trailing / from path_info()
438 $source =~ s!/+$!!;
439
440 $source;
441 }
442  
443 sub update_data_source {
444 my $self = shift;
445 my $session = shift;
446 my $new_source = shift;
447 my $old_source = $session->source || $self->default_source;
448  
449 $new_source ||= $self->get_source_from_cgi();
450  
451 my $source;
452 if ($self->valid_source($new_source)) {
453 $session->source($new_source);
454 $source = $new_source;
455 } else {
456 my $fallback_source = $self->valid_source($old_source)
457 ? $old_source
458 : $self->default_source;
459 $session->source($fallback_source);
460 $source = $fallback_source;
461 }
462 return $source;
463 }
464  
465 sub time2sec {
466 my $self = shift;
467 my $time = shift;
468 $time =~ s/\s*#.*$//; # strip comments
469  
470 my(%mult) = ('s'=>1,
471 'm'=>60,
472 'h'=>60*60,
473 'd'=>60*60*24,
474 'w'=>60*60*24*7,
475 'M'=>60*60*24*30,
476 'y'=>60*60*24*365);
477 my $offset = $time;
478 if (!$time || (lc($time) eq 'now')) {
479 $offset = 0;
480 } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([smhdwMy])/) {
481 $offset = ($mult{$2} || 1)*$1;
482 }
483 return $offset;
484 }
485  
486 ## methods for dealing with the session
487 sub session {
488 my $self = shift;
489 my ($id,$mode) = @_;
490  
491 $id ||= undef;
492 my @args = (driver => $self->session_driver,
493 args => $self->session_args,
494 source => $self->default_source,
495 lockdir => $self->session_locks,
496 locktype => $self->session_locktype,
497 mode => $mode || 'exclusive',
498 expires => $self->remember_settings_time);
499 return Bio::Graphics::Browser2::Session->new(@args,id => $id);
500 }
501  
502 sub authorized_session {
503 my $self = shift;
504 my ($id,$authority,$shared_ok) = @_;
505  
506 $id ||= undef;
507 my $session = $self->session($id,$shared_ok ? 'shared' : 'exclusive');
508  
509 return $session unless $session->private;
510  
511 if ($session->match_nonce($authority,CGI::remote_addr())) {
512 return $session;
513 } else {
514 cluck "UNAUTHORIZED ATTEMPT";
515 return $self->session('xyzzy');
516 }
517 }
518  
519 1;