Vars.pm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 'tiled' => $not_ref,
  58. # title links here
  59. 'href' => $not_ref,
  60. # Link to post locally
  61. 'local_href' => $not_ref,
  62. # Post body
  63. 'data' => $not_ref,
  64. # How do I edit this post?
  65. 'form' => $not_ref,
  66. # Post is restricted to visibility to these ACLs if not public/unlisted
  67. 'acls' => \&Ref::Util::is_arrayref,
  68. 'id' => $not_ref,
  69. # Author of the post
  70. 'user' => $not_ref,
  71. 'created' => $not_ref,
  72. );
  73. =head2 filter($data,[$schema]) = %$data_filtered
  74. Filter the provided data through the default schema, and optionally a user-provided schema.
  75. Remove unwanted params to keep data slim & secure.
  76. =cut
  77. sub filter ($data, $user_schema={}) {
  78. %$user_schema = (
  79. %schema,
  80. %$user_schema,
  81. );
  82. # Filter all the irrelevant data
  83. foreach my $key ( keys(%$data) ) {
  84. # We need to have the key in the schema, and it validate.
  85. delete $data->{$key} unless List::Util::any { ( $_ eq $key ) && ( $user_schema->{$key}->( $data->{$key} ) ) } keys(%$user_schema);
  86. }
  87. return %$data;
  88. }
  89. 1;