| 1 | package Bio::Graphics::Browser2::UserTracks::Database; | ||
| 2 | |||
| 3 | # $Id: Database.pm 23607 2010-07-30 17:34:25Z cnvandev $ | ||
| 4 | use strict; | ||
| 5 | use base 'Bio::Graphics::Browser2::UserTracks'; | ||
| 6 | use Bio::Graphics::Browser2; | ||
| 7 | use Bio::Graphics::Browser2::UserDB; | ||
| 8 | use Bio::Graphics::Browser2::SendMail; | ||
| 9 | use DBI; | ||
| 10 | use Digest::MD5 qw(md5_hex); | ||
| 11 | use CGI qw(param url header()); | ||
| 12 | use Carp qw(confess cluck); | ||
| 13 | use File::Path qw(rmtree); | ||
| 14 | |||
| 15 | sub _new { | ||
| 16 | my $class = shift; | ||
| 17 | my $self = $class->SUPER::_new(@_); | ||
| 18 | |||
| 19 | # Attempt to login to the database or die, and access the necessary tables or create them. | ||
| 20 | my $globals = $self->globals; | ||
| 21 | my $credentials = $globals->user_account_db or warn "No credentials given to uploads DB in GBrowse.conf"; | ||
| 22 | my $uploadsdb = DBI->connect($credentials); | ||
| 23 | unless ($uploadsdb) { | ||
| 24 | print header(); | ||
| 25 | print "Error: Could not open user account database. If you are the administrator, run gbrowse_metadb_config.pl"; | ||
| 26 | die "Could not open user account database with $credentials"; | ||
| 27 | } | ||
| 28 | $self->uploadsdb($uploadsdb); | ||
| 29 | |||
| 30 | # Check to see if user accounts are enabled, set some commonly-used variables. | ||
| 31 | if ($globals->user_accounts) { | ||
| 32 | # BUG: Two copies of UserDB; one here and one in the Render object | ||
| 33 | $self->{userdb} = Bio::Graphics::Browser2::UserDB->new($globals); | ||
| 34 | $self->{username} = $self->{userdb}->username_from_sessionid($self->sessionid); | ||
| 35 | $self->{userid} = $self->{userdb}->userid_from_sessionid($self->sessionid); | ||
| 36 | } | ||
| 37 | |||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub uploadsdb { | ||
| 42 | my $self = shift; | ||
| 43 | my $d = $self->{uploadsdb}; | ||
| 44 | $self->{uploadsdb} = shift if @_; | ||
| 45 | $d; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub userdb { shift->{userdb} } | ||
| 49 | sub userid { shift->{userid} } | ||
| 50 | sub username { shift->{username} } | ||
| 51 | |||
| 52 | # Path - Returns the path to a specified file's owner's (or just the logged-in user's) data folder. | ||
| 53 | sub path { | ||
| 54 | my $self = shift; | ||
| 55 | my $file = shift; | ||
| 56 | my ($userid, $uploadsid); | ||
| 57 | if (defined $file) { | ||
| 58 | my $userdb = $self->{userdb}; | ||
| 59 | $userid = $self->owner($file); | ||
| 60 | $uploadsid = $userdb->get_uploads_id($userid); | ||
| 61 | } | ||
| 62 | |||
| 63 | $uploadsid ||= $self->uploadsid; | ||
| 64 | if ($uploadsid eq $self->uploadsid) { | ||
| 65 | return $self->SUPER::path(); | ||
| 66 | } else { | ||
| 67 | return $self->data_source->userdata($uploadsid); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | # Get File ID (File ID [, Owner ID]) - Returns a file's validated ID from the database. | ||
| 72 | sub get_file_id { | ||
| 73 | my $self = shift; | ||
| 74 | my $filename = shift; | ||
| 75 | my $uploadsdb = $self->{uploadsdb}; | ||
| 76 | my $userid = $self->{userid}; | ||
| 77 | my $data_source = $self->{data_source}; | ||
| 78 | |||
| 79 | # First, check my files. | ||
| 80 | my $uploads = $uploadsdb->selectrow_array("SELECT trackid FROM uploads WHERE path = ? AND userid = ? AND data_source = ?", undef, $filename, $userid, $data_source); | ||
| 81 | return $uploads if $uploads; | ||
| 82 | |||
| 83 | # Then, check files shared with me. | ||
| 84 | my $shared = $uploadsdb->selectrow_array("SELECT DISTINCT uploads.trackid FROM uploads LEFT JOIN sharing ON uploads.trackid = sharing.trackid WHERE sharing.userid = ? AND uploads.path = ? AND (uploads.sharing_policy = ? OR uploads.sharing_policy = ?) AND data_source = ?", undef, $userid, $filename, "casual", "group", $data_source); | ||
| 85 | return $shared if $shared; | ||
| 86 | |||
| 87 | # Lastly, check public files. | ||
| 88 | my $public = $uploadsdb->selectrow_array("SELECT trackid FROM uploads WHERE path = ? AND sharing_policy = ? AND data_source = ?", undef, $filename, "public", $data_source); | ||
| 89 | return $public if $public; | ||
| 90 | } | ||
| 91 | |||
| 92 | # Filename (File ID) - Returns the filename of any given ID. | ||
| 93 | sub filename { | ||
| 94 | my $self = shift; | ||
| 95 | my $file = shift or return; | ||
| 96 | return $self->field("path", $file); | ||
| 97 | } | ||
| 98 | |||
| 99 | # Now Function - return the database-dependent function for determining current date & time | ||
| 100 | sub nowfun { | ||
| 101 | my $self = shift; | ||
| 102 | my $globals = $self->{globals}; | ||
| 103 | return $globals->user_account_db =~ /sqlite/i ? "datetime('now','localtime')" : 'now()'; | ||
| 104 | } | ||
| 105 | |||
| 106 | # Get Uploaded Files () - Returns an array of the paths of files owned by the currently logged-in user. Can be publicly accessed. | ||
| 107 | sub get_uploaded_files { | ||
| 108 | my $self = shift; | ||
| 109 | my $userid = $self->{userid}; | ||
| 110 | my $uploadsdb = $self->{uploadsdb}; | ||
| 111 | my $data_source = $self->{data_source}; | ||
| 112 | my $rows = $uploadsdb->selectcol_arrayref("SELECT trackid FROM uploads WHERE userid = ? AND sharing_policy <> ? AND imported <> 1 AND data_source=? ORDER BY trackid", undef, $userid, "public", $data_source); | ||
| 113 | return @$rows; | ||
| 114 | } | ||
| 115 | |||
| 116 | # this is used to autocomplete usernames, descriptions and filenames | ||
| 117 | sub prefix_search { | ||
| 118 | my $self = shift; | ||
| 119 | my $prefix = shift; | ||
| 120 | |||
| 121 | my (%results); | ||
| 122 | if ($self->globals->user_accounts) { | ||
| 123 | my $userdb = $self->{userdb}; | ||
| 124 | my $user_matches = $userdb->match_sharing_user($self->datasource_name,$prefix); | ||
| 125 | foreach (@$user_matches) { | ||
| 126 | $results{$_} = "<i>$_</i>"; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | my $upload_matches = $self->match_uploads($self->datasource_name,$prefix); | ||
| 130 | foreach (@$upload_matches) { | ||
| 131 | $results{$_} = "<b>$_</b>"; | ||
| 132 | } | ||
| 133 | my @results = map {$results{$_}} sort {lc $a cmp lc $b} keys %results; | ||
| 134 | return \@results; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub user_search { | ||
| 138 | my $self = shift; | ||
| 139 | my $prefix = shift; | ||
| 140 | |||
| 141 | return unless $self->globals->user_accounts; | ||
| 142 | my $userdb = $self->{userdb}; | ||
| 143 | my $results = $userdb->match_user($prefix); | ||
| 144 | return $results; | ||
| 145 | } | ||
| 146 | |||
| 147 | # Get Public Files ([Search Term, Offset]) - Returns an array of available public files that the user hasn't added. Will filter results if the extra parameter is given. | ||
| 148 | sub get_public_files { | ||
| 149 | my $self = shift; | ||
| 150 | my $searchterm = shift; | ||
| 151 | my $offset = shift; | ||
| 152 | |||
| 153 | my $globals = $self->{globals}; | ||
| 154 | my $count = $globals->public_files; | ||
| 155 | my $data_source = $self->{data_source}; | ||
| 156 | |||
| 157 | my $search_id; | ||
| 158 | if ($self->{globals}->user_accounts) { | ||
| 159 | # If we find a user from the term (ID or username), we'll search by user. | ||
| 160 | my $userdb = $self->{userdb}; | ||
| 161 | $search_id = $userdb->get_user_id($searchterm); | ||
| 162 | } | ||
| 163 | |||
| 164 | # Make sure we're not looking for files outside of the range. | ||
| 165 | my $public_count = $self->public_count; | ||
| 166 | $offset = ($offset > $public_count)? $public_count : $offset; | ||
| 167 | |||
| 168 | my $uploadsdb = $self->{uploadsdb}; | ||
| 169 | my $userid = $self->{userid}; | ||
| 170 | |||
| 171 | # Basic selection statement, limit to public tracks from this data set. | ||
| 172 | my $ds = $uploadsdb->quote($data_source); | ||
| 173 | my $sql =<<END; | ||
| 174 | SELECT u.trackid FROM uploads u | ||
| 175 | WHERE u.sharing_policy='public' | ||
| 176 | AND u.data_source=$ds | ||
| 177 | END | ||
| 178 | ; | ||
| 179 | $sql .= "AND u.trackid NOT IN (SELECT trackid FROM sharing WHERE userid=$userid)" | ||
| 180 | if $userid; | ||
| 181 | |||
| 182 | # Search string - if we have a searched ID, use that as the userid. If not, match the searchterm to description, path or title. | ||
| 183 | $sql .= $search_id? " AND (u.userid = " . $uploadsdb->quote($search_id) . ")" | ||
| 184 | : " AND (description LIKE " . $uploadsdb->quote("%".$searchterm."%") | ||
| 185 | . " OR path LIKE " . $uploadsdb->quote("%".$searchterm."%") | ||
| 186 | . " OR title LIKE " . $uploadsdb->quote("%".$searchterm."%") . ")" if $searchterm; | ||
| 187 | |||
| 188 | # Limit & offset as needed... | ||
| 189 | $sql .= " ORDER BY public_count DESC LIMIT $count"; | ||
| 190 | $sql .= " OFFSET $offset" if $offset; | ||
| 191 | |||
| 192 | my $rows = $uploadsdb->selectcol_arrayref($sql); | ||
| 193 | return @$rows; | ||
| 194 | } | ||
| 195 | |||
| 196 | # Public Count ([Search Term]) - Returns the total number of public files available to a user. | ||
| 197 | # Will filter results if a search parameter is given. | ||
| 198 | sub public_count { | ||
| 199 | my $self = shift; | ||
| 200 | my $searchterm = shift; | ||
| 201 | my $uploadsdb = $self->{uploadsdb}; | ||
| 202 | my $userid = $self->{userid}; | ||
| 203 | my $data_source = $self->{data_source}; | ||
| 204 | |||
| 205 | my $search_id; | ||
| 206 | if ($self->{globals}->user_accounts) { | ||
| 207 | # If we find a user from the term (ID or username), we'll search by user. | ||
| 208 | my $userdb = $self->{userdb} ; | ||
| 209 | $search_id = $userdb->get_user_id($searchterm); | ||
| 210 | } | ||
| 211 | |||
| 212 | my $sql = "SELECT u.trackid FROM uploads u" | ||
| 213 | . " LEFT JOIN (SELECT s.trackid FROM sharing s WHERE s.userid = " . $uploadsdb->quote($userid) . ") s" | ||
| 214 | . " USING(trackid) WHERE s.trackid IS NULL" | ||
| 215 | . " AND sharing_policy = " . $uploadsdb->quote("public") | ||
| 216 | . " AND data_source = " . $uploadsdb->quote($data_source); | ||
| 217 | |||
| 218 | $sql .= $search_id? " AND (u.userid = " . $uploadsdb->quote($search_id) . ")" | ||
| 219 | : " AND (description LIKE " . $uploadsdb->quote("%".$searchterm."%") | ||
| 220 | . " OR path LIKE " . $uploadsdb->quote("%".$searchterm."%") | ||
| 221 | . " OR title LIKE " . $uploadsdb->quote("%".$searchterm."%") . ")" if $searchterm; | ||
| 222 | |||
| 223 | my $rows = $uploadsdb->selectcol_arrayref($sql); | ||
| 224 | return @$rows; | ||
| 225 | } | ||
| 226 | |||
| 227 | # Get Imported Files () - Returns an array of files imported by a user. | ||
| 228 | sub get_imported_files { | ||
| 229 | my $self = shift; | ||
| 230 | my $userid = $self->{userid}; | ||
| 231 | my $uploadsdb = $self->{uploadsdb}; | ||
| 232 | my $data_source = $self->{data_source}; | ||
| 233 | my $rows = $uploadsdb->selectcol_arrayref("SELECT trackid FROM uploads WHERE sharing_policy <> 'public' AND imported = 1 AND data_source = ? AND userid = ? ORDER BY trackid", undef, $data_source, $userid); | ||
| 234 | return @$rows; | ||
| 235 | } | ||
| 236 | |||
| 237 | # Get Added Public Files () - Returns an array of public files added to a user's tracks. | ||
| 238 | sub get_added_public_files { | ||
| 239 | my $self = shift; | ||
| 240 | my $userid = $self->{userid}; | ||
| 241 | my $uploadsdb = $self->{uploadsdb}; | ||
| 242 | my $data_source = $self->{data_source}; | ||
| 243 | my $sql = "SELECT DISTINCT uploads.trackid FROM uploads LEFT JOIN sharing ON uploads.trackid = sharing.trackid WHERE sharing.userid = ? AND uploads.sharing_policy = ? AND sharing.public = 1 AND uploads.data_source = ?"; | ||
| 244 | my $rows = $uploadsdb->selectcol_arrayref($sql, undef, $userid, "public", $data_source); | ||
| 245 | return @$rows; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub match_uploads { | ||
| 249 | my $self = shift; | ||
| 250 | my ($source,$prefix) = @_; | ||
| 251 | |||
| 252 | my $sql =<<END; | ||
| 253 | SELECT a.title | ||
| 254 | FROM uploads as a,sharing as b | ||
| 255 | WHERE a.userid=b.userid | ||
| 256 | AND a.sharing_policy='public' | ||
| 257 | AND a.title LIKE ? | ||
| 258 | END | ||
| 259 | ; | ||
| 260 | my $userid = $self->{userid}; | ||
| 261 | $sql .= "AND a.trackid NOT IN (SELECT trackid FROM sharing WHERE userid=$userid)" | ||
| 262 | if $userid; | ||
| 263 | |||
| 264 | my $uploadsdb = $self->{uploadsdb}; | ||
| 265 | my $search = $uploadsdb->quote($prefix); | ||
| 266 | $search =~ s/^'//; | ||
| 267 | $search =~ s/'$//; | ||
| 268 | $search = "$search%"; | ||
| 269 | my $select = $uploadsdb->prepare($sql) or die $uploadsdb->errstr; | ||
| 270 | $select->execute($search) or die $uploadsdb->errstr; | ||
| 271 | $prefix = quotemeta($prefix); | ||
| 272 | |||
| 273 | my @results; | ||
| 274 | while (my @a = $select->fetchrow_array) { | ||
| 275 | warn @a; | ||
| 276 | push @results,(grep /^$prefix/i,@a); | ||
| 277 | } | ||
| 278 | $select->finish; | ||
| 279 | return \@results; | ||
| 280 | } | ||
| 281 | |||
| 282 | # Get Shared Files () - Returns an array of files shared specifically to a user. | ||
| 283 | sub get_shared_files { | ||
| 284 | my $self = shift; | ||
| 285 | my $userid = $self->{userid}; | ||
| 286 | my $uploadsdb = $self->{uploadsdb}; | ||
| 287 | my $data_source = $self->{data_source}; | ||
| 288 | #Since upload IDs are all the same size, we don't have to worry about one ID repeated inside another so this next line is OK. Still, might be a good idea to secure this somehow? | ||
| 289 | my $rows = $uploadsdb->selectcol_arrayref("SELECT DISTINCT uploads.trackid FROM uploads LEFT JOIN sharing ON uploads.trackid = sharing.trackid WHERE sharing.userid = ? AND sharing.public = 0 AND (uploads.sharing_policy = ? OR uploads.sharing_policy = ?) AND uploads.userid <> ? AND uploads.data_source = ? ORDER BY uploads.trackid", undef, $userid, "group", "casual", $userid, $data_source); | ||
| 290 | return @$rows; | ||
| 291 | } | ||
| 292 | |||
| 293 | sub share_link { | ||
| 294 | my $self = shift; | ||
| 295 | my $file = shift or confess "No input or invalid input given to share()"; | ||
| 296 | my $permissions = $self->permissions($file); | ||
| 297 | return $self->share($file) | ||
| 298 | if ($permissions eq "public" || $permissions eq "casual"); # Can't hijack group files with a link, public are OK. | ||
| 299 | } | ||
| 300 | |||
| 301 | # Share (File[, Username OR User ID]) - Adds a public or shared track to a user's session. | ||
| 302 | sub share { | ||
| 303 | my $self = shift; | ||
| 304 | my $file = shift or confess "No input or invalid input given to share()"; | ||
| 305 | my $name_or_id = shift; | ||
| 306 | |||
| 307 | # If we've been passed a user ID, use that. If we've been passed a username, get the ID. | ||
| 308 | # If we haven't been passed anything, use the session user ID. | ||
| 309 | my $userid; | ||
| 310 | |||
| 311 | if ($self->{globals}->user_accounts) { | ||
| 312 | my $userdb = $self->{userdb}; | ||
| 313 | $userid = $userdb->get_user_id($name_or_id); | ||
| 314 | $self->{userid} ||= $userdb->add_named_session($self->sessionid, "an anonymous user"); | ||
| 315 | } else { | ||
| 316 | $userid = $name_or_id; | ||
| 317 | } | ||
| 318 | $userid ||= $self->{userid}; | ||
| 319 | |||
| 320 | my $sharing_policy = $self->permissions($file); | ||
| 321 | # No sense in adding yourself to a group. Also fixes a bug with nonsense users | ||
| 322 | # returning your ID and adding yourself instead of nothing. | ||
| 323 | # Users can add themselves to the sharing lists of casual or public files; | ||
| 324 | # Owners can add people to group lists but can't force anyone to have a public or casual file. | ||
| 325 | return if $self->is_mine($file) and | ||
| 326 | $sharing_policy =~ /(group|casual)/ and | ||
| 327 | $userid eq $self->{userid}; | ||
| 328 | if ((($sharing_policy =~ /(casual|public)/) && | ||
| 329 | ($userid eq $self->{userid})) || | ||
| 330 | ($self->is_mine($file) && ($sharing_policy =~ /group/))) { | ||
| 331 | my $public_flag = ($sharing_policy=~ /public/) ? 1 : 0; | ||
| 332 | my $uploadsdb = $self->{uploadsdb}; | ||
| 333 | |||
| 334 | # Get the current users. | ||
| 335 | return if $uploadsdb->selectrow_array("SELECT trackid FROM sharing WHERE trackid = ? AND userid = ? AND public = ?", | ||
| 336 | undef, $file, $userid, $public_flag); | ||
| 337 | |||
| 338 | # Add the file's tracks to the track lookup hash. | ||
| 339 | if ($userid eq $self->{userid}) { | ||
| 340 | my %track_lookup = $self->track_lookup; | ||
| 341 | $track_lookup{$_} = $file foreach $self->labels($file); | ||
| 342 | } else { | ||
| 343 | $self->email_sharee($file,$userid); | ||
| 344 | } | ||
| 345 | |||
| 346 | return $uploadsdb->do("INSERT INTO sharing (trackid, userid, public) VALUES (?, ?, ?)", | ||
| 347 | undef, $file, $userid, $public_flag); | ||
| 348 | } else { | ||
| 349 | warn "Share() attempted in an illegal situation on a $sharing_policy file ($file) by user #$userid, a non-owner."; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | # Unshare (File[, Username OR User ID]) - Removes an added public or shared track from a user's session. | ||
| 354 | sub unshare { | ||
| 355 | my $self = shift; | ||
| 356 | my $file = shift or confess "No input or invalid input given to unshare()"; | ||
| 357 | my $userid = shift || $self->{userid}; | ||
| 358 | |||
| 359 | # Users can remove themselves from the sharing lists of group, casual or public files; | ||
| 360 | # owners can remove people from casual or group items. | ||
| 361 | my $sharing_policy = $self->permissions($file); | ||
| 362 | if ((($sharing_policy =~ /(casual|public|group)/) | ||
| 363 | && ($userid eq $self->{userid})) | ||
| 364 | || ($self->is_mine($file) && ($sharing_policy =~ /(casual|group)/))) { | ||
| 365 | my $public_flag = ($sharing_policy=~ /public/)? 1 : 0; | ||
| 366 | my $uploadsdb = $self->{uploadsdb}; | ||
| 367 | |||
| 368 | # Get the current users. | ||
| 369 | return unless $uploadsdb->selectrow_array("SELECT trackid FROM sharing WHERE trackid = ? AND userid = ? AND public = ?", | ||
| 370 | undef, | ||
| 371 | $file, $userid, $public_flag); | ||
| 372 | |||
| 373 | # Remove the file's tracks from the track lookup hash. | ||
| 374 | if ($userid eq $self->{userid}) { | ||
| 375 | my %track_lookup = $self->track_lookup; | ||
| 376 | delete $track_lookup{$_} foreach $self->labels($file);; | ||
| 377 | } | ||
| 378 | |||
| 379 | return $uploadsdb->do("DELETE FROM sharing WHERE trackid = ? AND userid = ? AND public = ?", undef, $file, $userid, $public_flag); | ||
| 380 | } else { | ||
| 381 | warn "Unshare() attempted in an illegal situation on a $sharing_policy file ($file) by user #$userid, a non-owner."; | ||
| 382 | } | ||
| 383 | } | ||
| 384 | |||
| 385 | sub email_sharee { | ||
| 386 | my $self = shift; | ||
| 387 | my ($file,$recipient) = @_; | ||
| 388 | my $userdb = $self->userdb; | ||
| 389 | my $globals = $self->globals; | ||
| 390 | |||
| 391 | my $description = $self->description($file); | ||
| 392 | my $title = $self->title($file); | ||
| 393 | my $upload_name = $self->filename($file); | ||
| 394 | my @labels = $self->labels($file); | ||
| 395 | my ($from_fullname,$from_email) = $userdb->accountinfo_from_username($self->username); | ||
| 396 | $from_fullname ||= $self->username; | ||
| 397 | $from_fullname .= " ($from_email)" if $from_email; | ||
| 398 | my ($to_fullname,$to_email) = $self->userdb->accountinfo_from_username($self->userdb->username_from_userid($recipient)); | ||
| 399 | return unless $to_email; | ||
| 400 | |||
| 401 | my $gbrowse_link = $globals->gbrowse_url.'/'; | ||
| 402 | my $gbrowse_show_link = $gbrowse_link."?show=".Bio::Graphics::Browser2::Render->join_tracks(\@labels); | ||
| 403 | my $gbrowse_readd_link = $gbrowse_link."?share_link=$file"; | ||
| 404 | |||
| 405 | my $source = $self->data_source; | ||
| 406 | |||
| 407 | my $subject = Bio::Graphics::Browser2::Util->translate('SHARE_GROUP_EMAIL_SUBJECT',$source->description); | ||
| 408 | my $contents = Bio::Graphics::Browser2::Util->translate('SHARE_GROUP_EMAIL', | ||
| 409 | $from_fullname, | ||
| 410 | $gbrowse_link, | ||
| 411 | $gbrowse_show_link, | ||
| 412 | $title, | ||
| 413 | $description, | ||
| 414 | join(',',map {$source->setting($_=>'key')||$_} @labels), | ||
| 415 | $gbrowse_readd_link); | ||
| 416 | $contents = CGI::unescapeHTML($contents); | ||
| 417 | $subject = CGI::unescapeHTML($subject); | ||
| 418 | $self->Bio::Graphics::Browser2::SendMail::do_sendmail({ | ||
| 419 | from => $globals->email_address, | ||
| 420 | from_title => $globals->application_name, | ||
| 421 | to => $to_email, | ||
| 422 | subject => $subject, | ||
| 423 | msg => $contents},$globals); | ||
| 424 | } | ||
| 425 | |||
| 426 | # Field (Field, File ID[, Value]) - Returns (or, if defined, sets to the new value) the specified field of a file. | ||
| 427 | # This function is dangerous as it has direct access to the database and doesn't do any permissions checks (that's done at the individual field functions like title() and description(). | ||
| 428 | # Make sure you set your permissions at the function level, and never put this into Action.pm or you'll be able to corrupt your database from a URL request! | ||
| 429 | sub field { | ||
| 430 | my $self = shift; | ||
| 431 | my $field = shift or return; | ||
| 432 | my $file = shift or return; | ||
| 433 | my $value = shift; | ||
| 434 | my $uploadsdb = $self->{uploadsdb}; | ||
| 435 | |||
| 436 | if (defined $value) { | ||
| 437 | #Clean up the string | ||
| 438 | $value =~ s/^\s+//; | ||
| 439 | $value =~ s/\s+$//; | ||
| 440 | my $result = $uploadsdb->do("UPDATE uploads SET $field = ? WHERE trackid = ?", undef, $value, $file); | ||
| 441 | $self->update_modified($file); | ||
| 442 | return $result; | ||
| 443 | } else { | ||
| 444 | return $uploadsdb->selectrow_array("SELECT $field FROM uploads WHERE trackid = ?", undef, $file); | ||
| 445 | } | ||
| 446 | } | ||
| 447 | |||
| 448 | # Update Modified (File ID[, User ID]) - Updates the modification date/time of the specified file to right now. | ||
| 449 | sub update_modified { | ||
| 450 | my $self = shift; | ||
| 451 | my $uploadsdb = $self->{uploadsdb}; | ||
| 452 | my $file = shift or return; | ||
| 453 | my $now = $self->nowfun; | ||
| 454 | # Do not swap out this line for a field() call, since it's used inside field(). | ||
| 455 | return $uploadsdb->do("UPDATE uploads SET modification_date = $now WHERE trackid = " . $uploadsdb->quote($file)); | ||
| 456 | } | ||
| 457 | |||
| 458 | # Created (File ID) - Returns creation date of $file, cannot be set. | ||
| 459 | sub created { | ||
| 460 | my $self = shift; | ||
| 461 | my $file = shift or return; | ||
| 462 | return $self->field("creation_date", $file); | ||
| 463 | } | ||
| 464 | |||
| 465 | # Modified (File ID) - Returns date modified of $file, cannot be set (except by update_modified()). | ||
| 466 | sub modified { | ||
| 467 | my $self = shift; | ||
| 468 | my $file = shift or return; | ||
| 469 | return $self->field("modification_date", $file); | ||
| 470 | } | ||
| 471 | |||
| 472 | # Description (File ID[, Value]) - Returns a file's description, or changes the current description if defined. | ||
| 473 | sub description { | ||
| 474 | my $self = shift; | ||
| 475 | my $file = shift or return; | ||
| 476 | my $value = shift; | ||
| 477 | my $userid = $self->{userid}; | ||
| 478 | if ($value) { | ||
| 479 | if ($self->is_mine($file)) { | ||
| 480 | return $self->field("description", $file, $value) | ||
| 481 | } else { | ||
| 482 | warn "Change Description requested on $file by user #$userid, a non-owner."; | ||
| 483 | } | ||
| 484 | } else { | ||
| 485 | return $self->field("description", $file) | ||
| 486 | } | ||
| 487 | } | ||
| 488 | |||
| 489 | # Title (File ID[, Value]) - Returns a file's title, or changes the current title if defined. | ||
| 490 | sub title { | ||
| 491 | my $self = shift; | ||
| 492 | my $file = shift or return; | ||
| 493 | my $value = shift; | ||
| 494 | my $userid = $self->{userid}; | ||
| 495 | if ($value) { | ||
| 496 | if ($self->is_mine($file)) { | ||
| 497 | return $self->field("title", $file, $value) | ||
| 498 | } else { | ||
| 499 | warn "Change title requested on $file by user #$userid, a non-owner."; | ||
| 500 | } | ||
| 501 | } else { | ||
| 502 | return $self->field("title", $file) || $self->field("path", $file); | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | # Add File (Full Path[, Imported, Description, Sharing Policy, Owner's Uploads ID]) - Adds $file to the database under the current (or specified) owner. | ||
| 507 | sub add_file { | ||
| 508 | my $self = shift; | ||
| 509 | my $uploadsdb = $self->{uploadsdb}; | ||
| 510 | my $filename = shift; | ||
| 511 | my $imported = shift || 0; | ||
| 512 | my $description = shift; | ||
| 513 | |||
| 514 | my $userdb = $self->{userdb}; | ||
| 515 | $self->{userid} ||= $userdb->add_named_session($self->sessionid, "an anonymous user"); | ||
| 516 | |||
| 517 | my $userid = shift || $self->{userid}; | ||
| 518 | my $shared = shift || ($self =~ /admin/)? "public" : "private"; | ||
| 519 | my $data_source = $self->{data_source}; | ||
| 520 | |||
| 521 | # Add the file's tracks to the track lookup hash. | ||
| 522 | my %track_lookup = $self->track_lookup; | ||
| 523 | $track_lookup{$_} = $filename foreach $self->labels($filename); | ||
| 524 | |||
| 525 | my $fileid = md5_hex($userid.$filename.$data_source); | ||
| 526 | my $now = $self->nowfun; | ||
| 527 | my $result = $uploadsdb->do("DELETE FROM uploads WHERE trackid='$fileid'"); | ||
| 528 | $uploadsdb->do("INSERT INTO uploads (trackid, userid, path, description, imported, creation_date, modification_date, sharing_policy, data_source ) VALUES (?, ?, ?, ?, ?, $now, $now, ?, ?)", undef, $fileid, $userid, $filename, $description, $imported, $shared, $data_source) | ||
| 529 | or die $uploadsdb->error_str; | ||
| 530 | return $fileid; | ||
| 531 | } | ||
| 532 | |||
| 533 | # Delete File (File ID) - Deletes $file_id from the database. | ||
| 534 | sub delete_file { | ||
| 535 | my $self = shift; | ||
| 536 | my $file = shift or return; | ||
| 537 | |||
| 538 | my $userid = $self->{userid}; | ||
| 539 | my $uploadsid = $self->uploadsid; | ||
| 540 | my $filename = $self->filename($file); | ||
| 541 | # If the file doesn't exist, don't throw an error, just return. | ||
| 542 | if ($self->is_mine($file) || !$filename) { | ||
| 543 | if ($filename) { | ||
| 544 | # Get this information before the record is deleted from the database. | ||
| 545 | my $path = $self->track_path($file); | ||
| 546 | my $conf = $self->track_conf($file); | ||
| 547 | |||
| 548 | # First delete from the database - better to have a dangling file then a dangling reference to nothing. | ||
| 549 | my $uploadsdb = $self->{uploadsdb}; | ||
| 550 | $uploadsdb->do("DELETE FROM uploads WHERE trackid = ?", undef, $file); | ||
| 551 | $uploadsdb->do("DELETE FROM sharing WHERE trackid = ?", undef, $file); | ||
| 552 | |||
| 553 | # Remove the file's tracks from the track lookup hash. | ||
| 554 | my %track_lookup = $self->track_lookup; | ||
| 555 | delete $track_lookup{$_} foreach $self->labels($filename); | ||
| 556 | |||
| 557 | # Now remove the backend database. | ||
| 558 | my $loader = Bio::Graphics::Browser2::DataLoader->new($filename, | ||
| 559 | $path, | ||
| 560 | $conf, | ||
| 561 | $self->{config}, | ||
| 562 | $uploadsid); | ||
| 563 | $loader->drop_databases($conf); | ||
| 564 | |||
| 565 | # Then remove the file if it exists. | ||
| 566 | rmtree($path) or warn "Could not delete $path: $!" if -e $path; | ||
| 567 | } | ||
| 568 | } else { | ||
| 569 | warn "Delete of " . $filename . " requested by user #$userid, a non-owner."; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 573 | # Is Imported (File) - Returns 1 if an already-added track is imported, 0 if not. | ||
| 574 | sub is_imported { | ||
| 575 | my $self = shift; | ||
| 576 | my $file = shift or return; | ||
| 577 | my $uploadsdb = $self->{uploadsdb}; | ||
| 578 | return $self->field("imported", $file) || 0; | ||
| 579 | } | ||
| 580 | |||
| 581 | # Permissions (File[, New Permissions]) - Return or change the permissions. | ||
| 582 | sub permissions { | ||
| 583 | my $self = shift; | ||
| 584 | my $file = shift or return; | ||
| 585 | my $new_permissions = shift; | ||
| 586 | if ($new_permissions) { | ||
| 587 | my $userid = $self->{userid}; | ||
| 588 | if ($self->is_mine($file)) { | ||
| 589 | my $old_permissions = $self->field("sharing_policy", $file); | ||
| 590 | my $result = $self->field("sharing_policy", $file, $new_permissions); | ||
| 591 | if ((($old_permissions =~ /(casual|group)/) && ($new_permissions eq "public"))) { | ||
| 592 | my @old_users = ($old_permissions eq "public")? $self->shared_with($file) : $self->public_users($file); | ||
| 593 | $self->share($file, $_) foreach @old_users; | ||
| 594 | } | ||
| 595 | $self->share($file, $userid) if $new_permissions =~ /public/; # If we're switching to public permissions, share with the user so it doesn't disappear. | ||
| 596 | return $result; | ||
| 597 | } else { | ||
| 598 | warn "Permissions change on " . $file . "requested by user #$userid a non-owner."; | ||
| 599 | } | ||
| 600 | } else { | ||
| 601 | return $self->field("sharing_policy", $file); | ||
| 602 | } | ||
| 603 | } | ||
| 604 | |||
| 605 | # Is Mine (Filename) - Returns 1 if a track is owned by the logged-in (or specified) user, 0 if not. | ||
| 606 | sub is_mine { | ||
| 607 | my $self = shift; | ||
| 608 | my $file = shift or return; | ||
| 609 | my $owner = $self->owner($file); | ||
| 610 | return ($owner eq $self->{userid})? 1 : 0; | ||
| 611 | } | ||
| 612 | |||
| 613 | # Owner (Filename) - Returns the owner of the specified file. | ||
| 614 | sub owner { | ||
| 615 | my $self = shift; | ||
| 616 | my $file = shift or return; | ||
| 617 | my $uploadsdb = $self->{uploadsdb}; | ||
| 618 | return $self->field("userid", $file); | ||
| 619 | } | ||
| 620 | |||
| 621 | # Is Shared With Me (Filename) - Returns 1 if a track is shared with the logged-in (or specified) user, 0 if not. | ||
| 622 | sub is_shared_with_me { | ||
| 623 | my $self = shift; | ||
| 624 | my $file = shift or return 0; | ||
| 625 | my $userid = $self->{userid}; | ||
| 626 | my $uploadsdb = $self->{uploadsdb}; | ||
| 627 | my $results = $uploadsdb->selectcol_arrayref("SELECT trackid FROM sharing WHERE trackid = ? AND userid = ?", undef, $file, $userid); | ||
| 628 | return (@$results > 0); | ||
| 629 | } | ||
| 630 | |||
| 631 | # File Type (File ID) - Returns the type of a specified track, in relation to the user. | ||
| 632 | sub file_type { | ||
| 633 | my $self = shift; | ||
| 634 | my $file = shift or return; | ||
| 635 | return "public" if ($self->permissions($file) =~ /public/); | ||
| 636 | if ($self->is_mine($file)) { | ||
| 637 | return $self->is_imported($file)? "imported" : "uploaded"; | ||
| 638 | } else { return "shared" }; | ||
| 639 | } | ||
| 640 | |||
| 641 | # Shared With (File ID) - Returns an array of users a track is shared with. | ||
| 642 | sub shared_with { | ||
| 643 | my $self = shift; | ||
| 644 | my $file = shift; | ||
| 645 | return unless $self->permissions($file) =~ /(casual|group)/; | ||
| 646 | return $self->get_users($file, 0); | ||
| 647 | } | ||
| 648 | |||
| 649 | # Public Users (File ID) - Returns an array of users of a public track. | ||
| 650 | sub public_users { | ||
| 651 | my $self = shift; | ||
| 652 | my $file = shift; | ||
| 653 | return unless $self->permissions($file) =~ /public/; | ||
| 654 | return $self->get_users($file, 1); | ||
| 655 | } | ||
| 656 | |||
| 657 | # Public Users (File ID) - Returns an array of users of a public/shared track. | ||
| 658 | sub get_users { | ||
| 659 | my $self = shift; | ||
| 660 | my $file = shift; | ||
| 661 | my $public = shift; | ||
| 662 | my $uploadsdb = $self->{uploadsdb}; | ||
| 663 | my $usersref = $uploadsdb->selectcol_arrayref("SELECT userid FROM sharing WHERE trackid = ? AND public = ? ORDER BY userid", undef, $file, $public); | ||
| 664 | return @$usersref; | ||
| 665 | } | ||
| 666 | |||
| 667 | # Public Users (File ID) - Returns the username of the owner of a track. | ||
| 668 | sub owner_name { | ||
| 669 | my $self = shift; | ||
| 670 | my $file = shift; | ||
| 671 | my $userdb = $self->{userdb}; | ||
| 672 | my $owner_id = $self->owner($file); | ||
| 673 | return ($owner_id eq $self->{userid})? "you" : $userdb->username_from_userid($owner_id); | ||
| 674 | } | ||
| 675 | |||
| 676 | sub clone_database { | ||
| 677 | my $self = shift; | ||
| 678 | $self->userdb->clone_database; | ||
| 679 | $self->{uploadsdb}{InactiveDestroy} = 1; | ||
| 680 | $self->{uploadsdb} = $self->{uploadsdb}->clone | ||
| 681 | } | ||
| 682 | |||
| 683 | 1; |