line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package meon::Web::Util; |
2
|
|
|
|
|
|
|
|
3
|
24
|
|
|
24
|
|
6279784
|
use Text::Unidecode 'unidecode'; |
|
24
|
|
|
|
|
55355
|
|
|
24
|
|
|
|
|
3014
|
|
4
|
24
|
|
|
24
|
|
11592
|
use Path::Class 'dir', 'file'; |
|
24
|
|
|
|
|
829227
|
|
|
24
|
|
|
|
|
1536
|
|
5
|
24
|
|
|
24
|
|
5293
|
use XML::LibXML::XPathContext; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Carp 'croak'; |
7
|
|
|
|
|
|
|
use Run::Env; |
8
|
|
|
|
|
|
|
use Email::MIME; |
9
|
|
|
|
|
|
|
use Email::Sender::Simple qw(sendmail); |
10
|
|
|
|
|
|
|
use File::MimeInfo 'mimetype'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub xpc { |
13
|
|
|
|
|
|
|
my $xpc = XML::LibXML::XPathContext->new; |
14
|
|
|
|
|
|
|
$xpc->registerNs('x', 'http://www.w3.org/1999/xhtml'); |
15
|
|
|
|
|
|
|
$xpc->registerNs('w', 'http://web.meon.eu/'); |
16
|
|
|
|
|
|
|
return $xpc; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub filename_cleanup { |
20
|
|
|
|
|
|
|
my ($self, $text) = @_; |
21
|
|
|
|
|
|
|
$text = unidecode($text); |
22
|
|
|
|
|
|
|
$text =~ s/\s/-/g; |
23
|
|
|
|
|
|
|
$text =~ s/-+/-/g; |
24
|
|
|
|
|
|
|
$text =~ s/[^A-Za-z0-9\-_]//g; |
25
|
|
|
|
|
|
|
return $text; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub username_cleanup { |
29
|
|
|
|
|
|
|
my ($self, $username, $folder) = @_; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$username = unidecode($username); |
32
|
|
|
|
|
|
|
$username =~ s/[^A-Za-z0-9]//g; |
33
|
|
|
|
|
|
|
while (length($username) < 4) { |
34
|
|
|
|
|
|
|
$username .= 'x'; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $base_username = $username; |
38
|
|
|
|
|
|
|
my $i = 1; |
39
|
|
|
|
|
|
|
while (-d dir($folder, $username)) { |
40
|
|
|
|
|
|
|
$i++; |
41
|
|
|
|
|
|
|
my $suffix = sprintf('%02d', $i); |
42
|
|
|
|
|
|
|
$username = $base_username.$suffix; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
return $username; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub path_fixup { |
49
|
|
|
|
|
|
|
my ($self, $path) = @_; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $username = ( |
52
|
|
|
|
|
|
|
meon::Web::env->user |
53
|
|
|
|
|
|
|
? $username = meon::Web::env->user->username |
54
|
|
|
|
|
|
|
: 'anonymous' |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$path =~ s/{\$USERNAME}/$username/; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
if ($path =~ m/^(.*){\$TIMELINE_NEWEST}/) { |
60
|
|
|
|
|
|
|
my $base_dir = dir(meon::Web::env->current_dir, (defined($1) ? $1 : ())); |
61
|
|
|
|
|
|
|
my $dir = $base_dir; |
62
|
|
|
|
|
|
|
while (my @subfolders = sort grep { $_->basename =~ m/^\d+$/ } grep { $_->is_dir } $dir->children(no_hidden => 1)) { |
63
|
|
|
|
|
|
|
$dir = pop(@subfolders); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
$dir = $dir->relative($base_dir); |
66
|
|
|
|
|
|
|
$dir .= ''; |
67
|
|
|
|
|
|
|
$path =~ s/{\$TIMELINE_NEWEST}/$dir/; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
if ($path =~ m/{\$COMMENT_TO}/) { |
71
|
|
|
|
|
|
|
my $comment_to = meon::Web::env->stash->{comment_to}; |
72
|
|
|
|
|
|
|
$path =~ s/{\$COMMENT_TO}/$comment_to/; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
return $path; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub full_path_fixup { |
79
|
|
|
|
|
|
|
my ($self, $path) = @_; |
80
|
|
|
|
|
|
|
$path = $self->path_fixup($path); |
81
|
|
|
|
|
|
|
my $cur_dir = meon::Web::env->current_dir; |
82
|
|
|
|
|
|
|
$cur_dir = meon::Web::env->content_dir |
83
|
|
|
|
|
|
|
if $path =~ m{^/}; |
84
|
|
|
|
|
|
|
$path = file($cur_dir, $path)->absolute; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub send_email { |
88
|
|
|
|
|
|
|
my ($class, %args) = @_; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my $from = $args{from} // croak 'need from'; |
91
|
|
|
|
|
|
|
my $to = $args{to} // croak 'need to'; |
92
|
|
|
|
|
|
|
my $bcc = $args{bcc}; |
93
|
|
|
|
|
|
|
my $subject = $args{subject} // croak 'need subject'; |
94
|
|
|
|
|
|
|
my $text = $args{text} // croak 'need text'; |
95
|
|
|
|
|
|
|
my @attachments = @{ $args{attachments} // [] }; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my @email_headers = ( |
98
|
|
|
|
|
|
|
header_str => [ |
99
|
|
|
|
|
|
|
From => $from, |
100
|
|
|
|
|
|
|
To => $to, |
101
|
|
|
|
|
|
|
($bcc && !Run::Env->prod ? (Bcc => $bcc) : ()), |
102
|
|
|
|
|
|
|
Subject => $subject, |
103
|
|
|
|
|
|
|
], |
104
|
|
|
|
|
|
|
); |
105
|
|
|
|
|
|
|
my @email_text = ( |
106
|
|
|
|
|
|
|
attributes => { |
107
|
|
|
|
|
|
|
content_type => "text/plain", |
108
|
|
|
|
|
|
|
charset => "UTF-8", |
109
|
|
|
|
|
|
|
encoding => "8bit", |
110
|
|
|
|
|
|
|
}, |
111
|
|
|
|
|
|
|
body_str => $text, |
112
|
|
|
|
|
|
|
); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
my $email; |
115
|
|
|
|
|
|
|
if (@attachments) { |
116
|
|
|
|
|
|
|
$email = Email::MIME->create( |
117
|
|
|
|
|
|
|
@email_headers, |
118
|
|
|
|
|
|
|
parts => [ |
119
|
|
|
|
|
|
|
Email::MIME->create(@email_text), |
120
|
|
|
|
|
|
|
( |
121
|
|
|
|
|
|
|
map { |
122
|
|
|
|
|
|
|
my $filename = file( |
123
|
|
|
|
|
|
|
ref($_) eq 'HASH' |
124
|
|
|
|
|
|
|
? $_->{filename} |
125
|
|
|
|
|
|
|
: $_ |
126
|
|
|
|
|
|
|
); |
127
|
|
|
|
|
|
|
my $basename = $filename->basename; |
128
|
|
|
|
|
|
|
my $content_type = ( |
129
|
|
|
|
|
|
|
ref($_) eq 'HASH' |
130
|
|
|
|
|
|
|
? $_->{content_type} |
131
|
|
|
|
|
|
|
: undef |
132
|
|
|
|
|
|
|
) // mimetype($basename) // 'application/octet-stream'; |
133
|
|
|
|
|
|
|
Email::MIME->create( |
134
|
|
|
|
|
|
|
attributes => { |
135
|
|
|
|
|
|
|
filename => $basename, |
136
|
|
|
|
|
|
|
content_type => $content_type, |
137
|
|
|
|
|
|
|
encoding => "base64", |
138
|
|
|
|
|
|
|
name => $basename, |
139
|
|
|
|
|
|
|
}, |
140
|
|
|
|
|
|
|
body => IO::Any->slurp($filename), |
141
|
|
|
|
|
|
|
); |
142
|
|
|
|
|
|
|
} @attachments |
143
|
|
|
|
|
|
|
), |
144
|
|
|
|
|
|
|
], |
145
|
|
|
|
|
|
|
); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
else { |
148
|
|
|
|
|
|
|
$email = Email::MIME->create( |
149
|
|
|
|
|
|
|
@email_headers, |
150
|
|
|
|
|
|
|
@email_text, |
151
|
|
|
|
|
|
|
); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
if (Run::Env->prod) { |
155
|
|
|
|
|
|
|
sendmail($email->as_string, { to => $bcc }) |
156
|
|
|
|
|
|
|
if $bcc; |
157
|
|
|
|
|
|
|
sendmail($email->as_string); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
else { |
160
|
|
|
|
|
|
|
warn $email->as_string; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |