Vars.pm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package Trog::Vars;
  2. use strict;
  3. use warnings;
  4. use feature qw{signatures};
  5. no warnings qw{experimental};
  6. use Ref::Util();
  7. use List::Util qw{any};
  8. #1MB chunks
  9. our $CHUNK_SEP = 'tCMSep666YOLO42069';
  10. our $CHUNK_SIZE = 1024000;
  11. our %content_types = (
  12. text => "text/plain",
  13. html => "text/html",
  14. json => "application/json",
  15. blob => "application/octet-stream",
  16. xml => "text/xml",
  17. xsl => "text/xsl",
  18. css => "text/css",
  19. rss => "application/rss+xml",
  20. email => "multipart/related",
  21. );
  22. our %byct = reverse %Trog::Vars::content_types;
  23. our %cache_control = (
  24. revalidate => "no-cache, max-age=0",
  25. nocache => "no-store",
  26. static => "public, max-age=604800, immutable",
  27. );
  28. our $not_ref = sub {
  29. return !Ref::Util::is_ref(shift);
  30. };
  31. our $valid_cb = sub {
  32. my $subname = shift;
  33. my ($modname) = $subname =~ m/^([\w|:]+)::\w+$/;
  34. # Modules always return 0 if they succeed!
  35. eval { require $modname; } and do {
  36. WARN("Post uses a callback whos module ($modname) cannot be found!");
  37. return 0;
  38. };
  39. no strict 'refs';
  40. my $ref = eval '\&' . $subname;
  41. use strict;
  42. return Ref::Util::is_coderef($ref);
  43. };
  44. our $hashref_or_string = sub {
  45. my $subj = shift;
  46. return Ref::Util::is_hashref($subj) || $not_ref->($subj);
  47. };
  48. # Shared Post schema
  49. our %schema = (
  50. ## Parameters which must be in every single post
  51. 'title' => $not_ref,
  52. 'callback' => $valid_cb,
  53. 'tags' => \&Ref::Util::is_arrayref,
  54. 'version' => $not_ref,
  55. 'visibility' => $not_ref,
  56. 'aliases' => \&Ref::Util::is_arrayref,
  57. # title links here
  58. 'href' => $not_ref,
  59. # Link to post locally
  60. 'local_href' => $not_ref,
  61. # Post body
  62. 'data' => $not_ref,
  63. # How do I edit this post?
  64. 'form' => $not_ref,
  65. # Post is restricted to visibility to these ACLs if not public/unlisted
  66. 'acls' => \&Ref::Util::is_arrayref,
  67. 'id' => $not_ref,
  68. # Author of the post
  69. 'user' => $not_ref,
  70. 'created' => $not_ref,
  71. );
  72. =head2 filter($data,[$schema]) = %$data_filtered
  73. Filter the provided data through the default schema, and optionally a user-provided schema.
  74. Remove unwanted params to keep data slim & secure.
  75. =cut
  76. sub filter ( $data, $user_schema = {} ) {
  77. %$user_schema = (
  78. %schema,
  79. %$user_schema,
  80. );
  81. # Filter all the irrelevant data
  82. foreach my $key ( keys(%$data) ) {
  83. # We need to have the key in the schema, and it validate.
  84. delete $data->{$key} unless List::Util::any { ( $_ eq $key ) && ( $user_schema->{$key}->( $data->{$key} ) ) } keys(%$user_schema);
  85. #use Data::Dumper;
  86. #print Dumper($data);
  87. # All parameters in the schema are MANDATORY.
  88. foreach my $param ( keys(%$user_schema) ) {
  89. die "Missing mandatory parameter $param" unless exists $data->{$param};
  90. }
  91. }
  92. return %$data;
  93. }
  94. 1;