123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package Trog::Vars;
- use strict;
- use warnings;
- use feature qw{signatures};
- no warnings qw{experimental};
- use Ref::Util();
- use List::Util qw{any};
- #1MB chunks
- our $CHUNK_SEP = 'tCMSep666YOLO42069';
- our $CHUNK_SIZE = 1024000;
- our %content_types = (
- text => "text/plain",
- html => "text/html",
- json => "application/json",
- blob => "application/octet-stream",
- xml => "text/xml",
- xsl => "text/xsl",
- css => "text/css",
- rss => "application/rss+xml",
- email => "multipart/related",
- );
- our %byct = reverse %Trog::Vars::content_types;
- our %cache_control = (
- revalidate => "no-cache, max-age=0",
- nocache => "no-store",
- static => "public, max-age=604800, immutable",
- );
- our $not_ref = sub {
- return !Ref::Util::is_ref(shift);
- };
- our $valid_cb = sub {
- my $subname = shift;
- my ($modname) = $subname =~ m/^([\w|:]+)::\w+$/;
- # Modules always return 0 if they succeed!
- eval { require $modname; } and do {
- WARN("Post uses a callback whos module ($modname) cannot be found!");
- return 0;
- };
- no strict 'refs';
- my $ref = eval '\&' . $subname;
- use strict;
- return Ref::Util::is_coderef($ref);
- };
- our $hashref_or_string = sub {
- my $subj = shift;
- return Ref::Util::is_hashref($subj) || $not_ref->($subj);
- };
- # Shared Post schema
- our %schema = (
- ## Parameters which must be in every single post
- 'title' => $not_ref,
- 'callback' => $valid_cb,
- 'tags' => \&Ref::Util::is_arrayref,
- 'version' => $not_ref,
- 'visibility' => $not_ref,
- 'aliases' => \&Ref::Util::is_arrayref,
- # title links here
- 'href' => $not_ref,
- # Link to post locally
- 'local_href' => $not_ref,
- # Post body
- 'data' => $not_ref,
- # How do I edit this post?
- 'form' => $not_ref,
- # Post is restricted to visibility to these ACLs if not public/unlisted
- 'acls' => \&Ref::Util::is_arrayref,
- 'id' => $not_ref,
- # Author of the post
- 'user' => $not_ref,
- 'created' => $not_ref,
- );
- =head2 filter($data,[$schema]) = %$data_filtered
- Filter the provided data through the default schema, and optionally a user-provided schema.
- Remove unwanted params to keep data slim & secure.
- =cut
- sub filter ($data, $user_schema={}) {
- %$user_schema = (
- %schema,
- %$user_schema,
- );
- # Filter all the irrelevant data
- foreach my $key ( keys(%$data) ) {
- # We need to have the key in the schema, and it validate.
- delete $data->{$key} unless List::Util::any { ( $_ eq $key ) && ( $user_schema->{$key}->( $data->{$key} ) ) } keys(%$user_schema);
- #use Data::Dumper;
- #print Dumper($data);
- # All parameters in the schema are MANDATORY.
- foreach my $param (keys(%$user_schema)) {
- die "Missing mandatory parameter $param" unless exists $data->{$param};
- }
- }
- return %$data;
- }
- 1;
|