diff options
author | Yorhel <git@yorhel.nl> | 2021-01-19 13:12:17 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2021-01-20 09:06:12 +0100 |
commit | 9d1727c77d4b00d2a861c2c31f93d3aebff2f2d9 (patch) | |
tree | 61d77e79edb2590c06cca80c8d029d8630a5aabc /lib/VNDB/Util | |
parent | b35af5ba52f3bceb603e8c7dda10d4abb84c6bd9 (diff) |
v2rw: Rewrite done, time to clean up old v2 code
Yay!
There are no more request handlers in the VNDB::* namespace and no more
Javascript in data/js/. This cleans up a lot of old legacy code that
wasn't fun to maintain.
Diffstat (limited to 'lib/VNDB/Util')
-rw-r--r-- | lib/VNDB/Util/Auth.pm | 81 | ||||
-rw-r--r-- | lib/VNDB/Util/BrowseHTML.pm | 190 | ||||
-rw-r--r-- | lib/VNDB/Util/CommonHTML.pm | 34 | ||||
-rw-r--r-- | lib/VNDB/Util/LayoutHTML.pm | 44 | ||||
-rw-r--r-- | lib/VNDB/Util/Misc.pm | 93 | ||||
-rw-r--r-- | lib/VNDB/Util/ValidateTemplates.pm | 16 |
6 files changed, 0 insertions, 458 deletions
diff --git a/lib/VNDB/Util/Auth.pm b/lib/VNDB/Util/Auth.pm deleted file mode 100644 index f3094ff0..00000000 --- a/lib/VNDB/Util/Auth.pm +++ /dev/null @@ -1,81 +0,0 @@ -# Compatibility shim around VNWeb::Auth, new code should use that instead. -package VNDB::Util::Auth; - - -use strict; -use warnings; -use Exporter 'import'; -use TUWF ':html'; -use VNWeb::Auth; - - -our @EXPORT = qw| - authInfo authCan authGetCode authCheckCode authPref -|; - - -sub authInfo { - # Used to return a lot more, but only the id is still used now. - # (code using other fields has been migrated) - +{ id => auth->uid } -} - - -# returns whether the currently loggedin or anonymous user can perform -# a certain action. -sub authCan { - my(undef, $act) = @_; - auth && auth->{user}{"perm_$act"} -} - - -# Generate a code to be used later on to validate that the form was indeed -# submitted from our site and by the same user/visitor. Not limited to -# logged-in users. -# Arguments: -# form-id (ignored nowadyas) -# time (also ignored) -sub authGetCode { - auth->csrftoken; -} - - -# Validates the correctness of the returned code, creates an error page and -# returns false if it's invalid, returns true otherwise. Codes are valid for at -# least two and at most three hours. -# Arguments: -# [ form-id, [ code ] ] -# If the code is not given, uses the 'formcode' form parameter instead. If -# form-id is not given, the path of the current requests is used. -sub authCheckCode { - my $self = shift; - my $id = shift; - my $code = shift || $self->reqParam('formcode'); - return _incorrectcode($self) if !auth->csrfcheck($code); - 1; -} - - -sub _incorrectcode { - my $self = shift; - $self->resInit; - $self->htmlHeader(title => 'Validation code expired', noindex => 1); - - div class => 'mainbox'; - h1 'Validation code expired'; - div class => 'warning'; - p 'Please hit the back-button of your browser, refresh the page and try again.'; - end; - end; - - $self->htmlFooter; - return 0; -} - - -sub authPref { - my(undef, $key, $val) = @_; - @_ == 2 ? auth->pref($key)||'' : auth->prefSet($key, $val); -} - -1; diff --git a/lib/VNDB/Util/BrowseHTML.pm b/lib/VNDB/Util/BrowseHTML.pm deleted file mode 100644 index 3eb460a6..00000000 --- a/lib/VNDB/Util/BrowseHTML.pm +++ /dev/null @@ -1,190 +0,0 @@ - -package VNDB::Util::BrowseHTML; - -use strict; -use warnings; -use TUWF ':html', 'xml_escape'; -use Exporter 'import'; -use VNDB::Func; -use VNDB::Types; -use POSIX 'ceil'; - - -our @EXPORT = qw| htmlBrowse htmlBrowseNavigate htmlBrowseVN |; - - -# generates a browse box, arguments: -# items => arrayref with the list items -# options => hashref containing at least the keys s (sort key), o (order) and p (page) -# nextpage => whether there's a next page or not -# sorturl => base URL to append the sort options to (if there are any sortable columns) -# pageurl => base URL to append the page option to -# class => classname of the mainbox -# header => -# can be either an arrayref or subroutine reference, -# in the case of a subroutine, it will be called when the header should be written, -# in the case of an arrayref, the array should contain the header items. Each item -# can again be either an arrayref or subroutine ref. The arrayref would consist of -# two elements: the name of the header, and the name of the sorting column if it can -# be sorted -# row => subroutine ref, which is called for each item in $list, arguments will be -# $self, $item_number (starting from 0), $item_value -# footer => subroutine ref, called after all rows have been processed -sub htmlBrowse { - my($self, %opt) = @_; - - $opt{sorturl} .= $opt{sorturl} =~ /\?/ ? ';' : '?' if $opt{sorturl}; - - # top navigation - $self->htmlBrowseNavigate($opt{pageurl}, $opt{options}{p}, $opt{nextpage}, 't') if $opt{pageurl}; - - div class => 'mainbox browse'.($opt{class} ? ' '.$opt{class} : ''); - table class => 'stripe'; - - # header - thead; - Tr; - if(ref $opt{header} eq 'CODE') { - $opt{header}->($self); - } else { - for(0..$#{$opt{header}}) { - if(ref $opt{header}[$_] eq 'CODE') { - $opt{header}[$_]->($self, $_+1); - } else { - td class => $opt{header}[$_][3]||'tc'.($_+1), $opt{header}[$_][2] ? (colspan => $opt{header}[$_][2]) : (); - lit $opt{header}[$_][0]; - if($opt{header}[$_][1]) { - lit ' '; - $opt{options}{s} eq $opt{header}[$_][1] && $opt{options}{o} eq 'a' ? lit "\x{25B4}" : a href => "$opt{sorturl}o=a;s=$opt{header}[$_][1]", "\x{25B4}"; - $opt{options}{s} eq $opt{header}[$_][1] && $opt{options}{o} eq 'd' ? lit "\x{25BE}" : a href => "$opt{sorturl}o=d;s=$opt{header}[$_][1]", "\x{25BE}"; - } - end; - } - } - } - end; - end 'thead'; - - # footer - if($opt{footer}) { - tfoot; - $opt{footer}->($self); - end; - } - - # rows - $opt{row}->($self, $_+1, $opt{items}[$_]) - for 0..$#{$opt{items}}; - - end 'table'; - end 'div'; - - # bottom navigation - $self->htmlBrowseNavigate($opt{pageurl}, $opt{options}{p}, $opt{nextpage}, 'b') if $opt{pageurl}; -} - - -# creates next/previous buttons (tabs), if needed -# Arguments: page url, current page (1..n), nextpage (0/1 or [$total, $perpage]), alignment (t/b), noappend (0/1) -sub htmlBrowseNavigate { - my($self, $url, $p, $np, $al, $na) = @_; - my($cnt, $pp) = ref($np) ? @$np : ($p+$np, 1); - return if $p == 1 && $cnt <= $pp; - - $url .= $url =~ /\?/ ? ';p=' : '?p=' unless $na; - - my $tab = sub { - my($page, $label) = @_; - li; - a href => $url.$page; lit $label; end; - end; - }; - my $ell = sub { - use utf8; - li class => 'ellipsis'; - b '⋯'; - end; - }; - my $nc = 5; # max. number of buttons on each side - - div class => 'maintabs browsetabs '.($al eq 't' ? '' : 'bottom'); - ul; - $p > 2 and ref $np and $tab->(1, '« first'); - $p > $nc+1 and ref $np and $ell->(); - $p > $_ and ref $np and $tab->($p-$_, $p-$_) for (reverse 2..($nc>$p-2?$p-2:$nc-1)); - $p > 1 and $tab->($p-1, '‹ previous'); - end; - - ul; - my $l = ceil($cnt/$pp)-$p+1; - $l > 1 and $tab->($p+1, 'next ›'); - $l > $_ and $tab->($p+$_, $p+$_) for (2..($nc>$l-2?$l-2:$nc-1)); - $l > $nc+1 and $ell->(); - $l > 2 and $tab->($l+$p-1, 'last »'); - end; - end 'div'; -} - - -sub htmlBrowseVN { - my($self, $list, $f, $np, $url, $tagscore) = @_; - $self->htmlBrowse( - class => 'vnbrowse', - items => $list, - options => $f, - nextpage => $np, - pageurl => "$url;o=$f->{o};s=$f->{s}", - sorturl => $url, - header => [ - $tagscore ? [ 'Score', 'tagscore', undef, 'tc_s' ] : (), - [ 'Title', 'title', undef, $tagscore ? 'tc_t' : 'tc1' ], - $f->{vnlist} ? [ '', 0, undef, 'tc7' ] : (), - $f->{wish} ? [ '', 0, undef, 'tc8' ] : (), - [ '', 0, undef, 'tc2' ], - [ '', 0, undef, 'tc3' ], - [ 'Released', 'rel', undef, 'tc4' ], - [ 'Popularity', 'pop', undef, 'tc5' ], - [ 'Rating', 'rating', undef, 'tc6' ], - ], - row => sub { - my($s, $n, $l) = @_; - Tr; - if($tagscore) { - td class => 'tc_s'; - VNWeb::TT::Lib::tagscore_($l->{tagscore}); - end; - } - td class => $tagscore ? 'tc_t' : 'tc1'; - a href => '/v'.$l->{id}, title => $l->{original}||$l->{title}, shorten $l->{title}, 100; - end; - if($f->{vnlist}) { - td class => 'tc7'; - lit sprintf '<b class="%s">%d/%d</b>', $l->{userlist_obtained} == $l->{userlist_all} ? 'done' : 'todo', $l->{userlist_obtained}, $l->{userlist_all} if $l->{userlist_all}; - abbr title => join(', ', $l->{vnlist_labels}->@*), scalar $l->{vnlist_labels}->@* if $l->{vnlist_labels} && $l->{vnlist_labels}->@*; - abbr title => 'No labels', ' ' if $l->{vnlist_labels} && !$l->{vnlist_labels}->@*; - end 'td'; - } - td class => 'tc2'; - $_ ne 'oth' && cssicon $_, $PLATFORM{$_} - for (sort @{$l->{c_platforms}}); - end; - td class => 'tc3'; - cssicon "lang $_", $LANGUAGE{$_} - for (reverse sort @{$l->{c_languages}}); - end; - td class => 'tc4'; - lit fmtdatestr $l->{c_released}; - end; - td class => 'tc5', sprintf '%.2f', ($l->{c_popularity}||0)*100; - td class => 'tc6'; - txt sprintf '%.2f', ($l->{c_rating}||0)/10; - b class => 'grayedout', sprintf ' (%d)', $l->{c_votecount}; - end; - end 'tr'; - }, - ); -} - - -1; - diff --git a/lib/VNDB/Util/CommonHTML.pm b/lib/VNDB/Util/CommonHTML.pm deleted file mode 100644 index 85722f1b..00000000 --- a/lib/VNDB/Util/CommonHTML.pm +++ /dev/null @@ -1,34 +0,0 @@ - -package VNDB::Util::CommonHTML; - -use strict; -use warnings; -use Exporter 'import'; -use VNDB::Func; - -our @EXPORT = qw| - htmlMainTabs htmlDenied htmlSearchBox -|; - - -# generates the "main tabs". These are the commonly used tabs for -# 'objects', i.e. VN/producer/release entries and users -# Arguments: u/v/r/p/g/i/c/d, object, currently selected item (empty=main) -sub htmlMainTabs { - my($self, $type, $obj, $sel) = @_; - $obj->{entry_hidden} = $obj->{hidden}; - $obj->{entry_locked} = $obj->{locked}; - VNWeb::HTML::_maintabs_({ type => $type, dbobj => $obj, tab => $sel||''}); -} - - -# generates a full error page, including header and footer -sub htmlDenied { shift->resDenied } - - -sub htmlSearchBox { - shift; VNWeb::HTML::searchbox_(@_); -} - - -1; diff --git a/lib/VNDB/Util/LayoutHTML.pm b/lib/VNDB/Util/LayoutHTML.pm deleted file mode 100644 index a9e0c05f..00000000 --- a/lib/VNDB/Util/LayoutHTML.pm +++ /dev/null @@ -1,44 +0,0 @@ - -package VNDB::Util::LayoutHTML; - -use strict; -use warnings; -use TUWF ':html'; -use VNDB::Config; -use VNWeb::HTML; -use Exporter 'import'; - -our @EXPORT = qw|htmlHeader htmlFooter|; - -sub htmlHeader { # %options->{ title, noindex, search, feeds, metadata } - my($self, %o) = @_; - %VNWeb::HTML::pagevars = (); - - $o{og} = $o{metadata} ? +{ map +(s/og://r, $o{metadata}{$_}), keys $o{metadata}->%* } : undef; - $o{index} = !$o{noindex}; - - html lang => 'en'; - head sub { VNWeb::HTML::_head_(\%o) }; - body; - div id => 'bgright', ' '; - div id => 'header', sub { h1 sub { a href => '/', 'the visual novel database' } }; - div id => 'menulist', sub { VNWeb::HTML::_menu_(\%o) }; - div id => 'maincontent'; -} - - -sub htmlFooter { # %options => { pref_code => 1 } - my($self, %o) = @_; - div id => 'footer', sub { VNWeb::HTML::_footer_ }; - end 'div'; # maincontent - - # Abuse an empty noscript tag for the formcode to update a preference setting, if the page requires one. - noscript id => 'pref_code', title => $self->authGetCode('/xml/prefs.xml'), '' - if $o{pref_code} && $self->authInfo->{id}; - script type => 'text/javascript', src => config->{url_static}.'/g/vndb.js?'.config->{version}, ''; - VNWeb::HTML::_scripts_({}); - end 'body'; - end 'html'; -} - -1; diff --git a/lib/VNDB/Util/Misc.pm b/lib/VNDB/Util/Misc.pm deleted file mode 100644 index 6342c0c5..00000000 --- a/lib/VNDB/Util/Misc.pm +++ /dev/null @@ -1,93 +0,0 @@ - -package VNDB::Util::Misc; - -use strict; -use warnings; -use Exporter 'import'; -use TUWF ':html'; -use VNDB::Func; -use VNDB::Types; - -our @EXPORT = qw|filFetchDB filCompat|; - - -our %filfields = ( - vn => [qw|date_before date_after released length hasani hasshot tag_inc tag_exc taginc tagexc tagspoil lang olang plat staff_inc staff_exc ul_notblack ul_onwish ul_voted ul_onlist|], - release => [qw|type patch freeware doujin uncensored date_before date_after released minage lang olang resolution plat prod_inc prod_exc med voiced ani_story ani_ero engine|], - char => [qw|gender bloodt bust_min bust_max waist_min waist_max hip_min hip_max height_min height_max va_inc va_exc weight_min weight_max cup_min cup_max trait_inc trait_exc tagspoil role|], - staff => [qw|gender role truename lang|], -); - - -# Arguments: -# type ('vn', 'release' or 'char'), -# filter overwrite (string or undef), -# when defined, these filters will be used instead of the preferences, -# must point to a variable, will be modified in-place with the actually used filters -# options to pass to db*Get() before the filters (hashref or undef) -# these options can be overwritten by the filters or the next option -# options to pass to db*Get() after the filters (hashref or undef) -# these options overwrite all other options (pre-options and filters) - -sub filFetchDB { - my($self, $type, $overwrite, $pre, $post) = @_; - $pre = {} if !$pre; - $post = {} if !$post; - my $dbfunc = $self->can($type eq 'vn' ? 'dbVNGet' : $type eq 'release' ? 'dbReleaseGet' : $type eq 'char' ? 'dbCharGet' : 'dbStaffGet'); - my $prefname = 'filter_'.$type; - my $pref = $self->authPref($prefname); - - my $filters = fil_parse $overwrite // $pref, @{$filfields{$type}}; - - VNWeb::Filters::debug_validate($type, $filters); - - # compatibility - my $compat = $self->filCompat($type, $filters); - $self->authPref($prefname => fil_serialize $filters) if $compat && !defined $overwrite; - - # write the definite filter string in $overwrite - $_[2] = fil_serialize({map +( - exists($post->{$_}) ? ($_ => $post->{$_}) : - exists($filters->{$_}) ? ($_ => $filters->{$_}) : - exists($pre->{$_}) ? ($_ => $pre->{$_}) : (), - ), @{$filfields{$type}}}) if defined $overwrite; - - return $dbfunc->($self, %$pre, %$filters, %$post) if defined $overwrite or !keys %$filters;; - - # since incorrect filters can throw a database error, we have to special-case - # filters that originate from a preference setting, so that in case these are - # the cause of an error, they are removed. Not doing this will result in VNDB - # throwing 500's even for non-browse pages. We have to do some low-level - # PostgreSQL stuff with savepoints to ensure that an error won't affect our - # existing transaction. - my $dbh = $self->dbh; - $dbh->pg_savepoint('filter'); - my($r, $np); - my $OK = eval { - ($r, $np) = $dbfunc->($self, %$pre, %$filters, %$post); - 1; - }; - $dbh->pg_rollback_to('filter') if !$OK; - $dbh->pg_release('filter'); - - # error occured, let's try again without filters. if that succeeds we know - # it's the fault of the filter preference, and we should remove it. - if(!$OK) { - ($r, $np) = $dbfunc->($self, %$pre, %$post); - # if we're here, it means the previous function didn't die() (duh!) - $self->authPref($prefname => ''); - warn sprintf "Reset filter preference for userid %d. Old: %s\n", $self->authInfo->{id}||0, $pref; - } - return wantarray ? ($r, $np) : $r; -} - - -# Compatibility with old filters. Modifies the filter in-place and returns the number of changes made. -sub filCompat { - my($self, $type, $fil) = @_; - $type eq 'vn' ? VNWeb::Filters::filter_vn_compat($fil) : 0 -} - - -1; - diff --git a/lib/VNDB/Util/ValidateTemplates.pm b/lib/VNDB/Util/ValidateTemplates.pm deleted file mode 100644 index e28abcb2..00000000 --- a/lib/VNDB/Util/ValidateTemplates.pm +++ /dev/null @@ -1,16 +0,0 @@ -# This module implements various templates for formValidate() - -package VNDB::Util::ValidateTemplates; - -use strict; -use warnings; - - -TUWF::set( - validate_templates => { - id => { template => 'uint', max => 1<<40 }, - page => { template => 'uint', max => 1000 }, - } -); - -1; |