package JWebmail::Plugin::Helper; use Mojo::Base 'Mojolicious::Plugin'; use POSIX qw(floor round log ceil); use MIME::Base64; use Encode; use Mojo::Util 'xml_escape'; use List::Util qw(min max); use constant TRUE_RANDOM => eval { require Crypt::URandom; Crypt::URandom->import('urandom'); 1 }; use constant HMAC => eval { require Digest::HMAC_MD5; Digest::HMAC_MD5->import('hmac_md5'); 1 }; ### filter and checks for mojo validator sub mail_line { my ($v, $name, $value, @args) = @_; my $mail_addr = qr/\w+\@\w+\.\w+/; # my $unescaped_quote = qr/"(*nlb:\\)/; # greater perl version required my $unescaped_quote = qr/"(? ) | ( $mail_addr ))$ /xno; } sub filter_empty_upload { my ($v, $name, $value) = @_; return $value->filename ? $value : undef; } ### template formatting functions sub print_sizes10 { my $var = shift; if ($var == 0) { return '0 Byte'; } my $i = floor(((log($var)/log(10))+1e-5) / 3); my $expo = $i * 3; my @PREFIX; $PREFIX[0] = 'Byte'; $PREFIX[1] = 'kByte'; $PREFIX[2] = 'MByte'; $PREFIX[3] = 'GByte'; $PREFIX[4] = 'TByte'; $PREFIX[5] = 'PByte'; return sprintf('%.0f %s', $var / (10**$expo), $PREFIX[$i]); } sub print_sizes2 { my $var = shift; if ($var == 0) { return '0 Byte'; } my $i = floor(((log($var)/log(2))+1e-5) / 10); my $expo = $i * 10; my %PREFIX = ( 0 => 'Byte', 1 => 'KiByte', 2 => 'MiByte', 3 => 'GiByte', 4 => 'TiByte', 5 => 'PiByte', ); my $pref = $PREFIX{$i}; return round($var / (2**$expo)) . " $pref"; } sub d { qr/([[:digit:]]{$_[0]})/ } sub parse_iso_date { my @d = shift =~ m/@{[d(4).'-'.d(2).'-'.d(2).'T'.d(2).':'.d(2).':'.d(2).'Z']}/; return { year => $d[0], month => $d[1], mday => $d[2], hour => $d[3], min => $d[4], sec => $d[5], }; } ### mime type html render functions my $render_text_plain = sub { my ($c, $content) = @_; $content = xml_escape($content); $content =~ s/\n/
/g; return $content; }; my $render_text_html = sub { my $c_ = shift; return '