QBit/WebInterface/Controller.pm untested

filter: Statementstmt 0.0% 0 / 189 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 42 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 325 CC 19 SCAR 23.6
CC
19
Cov
0%
MODIFY_CODE_ATTRIBUTES
30.0
import
30.0
_get_url_absolute
20.0
CRAP 10.5
1 package QBit::WebInterface::Controller;
2 $QBit::WebInterface::Controller::VERSION = '0.031';
3 use qbit;
4  
5 use base qw(QBit::Application::Part);
6  
7 use Exception::WebInterface::Controller::CSRF;
8  
9 use Template 2.20;
10 use Template::Config;
11 use Digest::MD5 qw(md5_hex);
12  
13 use QBit::WebInterface::Controller::Form;
14  
15 our %TEMPLATE_PRE_DEFINE;
16  
17 __PACKAGE__->mk_ro_accessors(qw(path attrs));
18  
19 sub _register_cmd {
20 my ($package, $sub, $type, $process_method) = @_;
21  
22 my $pkg_stash = package_stash($package);
23 $pkg_stash->{'__CMDS__'} = [] unless exists($pkg_stash->{'__CMDS__'});
24  
25 push(
26 @{$pkg_stash->{'__CMDS__'}},
27 {
28 sub => $sub,
29 package => $package,
30 type => $type,
31 (defined($process_method) ? (process_method => $process_method) : ())
32 }
33 );
34 }
35  
36 sub _set_cmd_attr {
37 my ($package, $sub, $name, $value) = @_;
38  
39 my $pkg_stash = package_stash($package);
40 $pkg_stash->{'__CMD_ATTRS__'} = {} unless exists($pkg_stash->{'__CMD_ATTRS__'});
41  
42 $pkg_stash->{'__CMD_ATTRS__'}{$package, $sub}{$name} = $value;
43 }
44  
45 sub MODIFY_CODE_ATTRIBUTES {
46 my ($package, $sub, @attrs) = @_;
47  
48 my @unknown_attrs = ();
49  
50 foreach my $attr (@attrs) {
51 if ($attr =~ /^CMD$/) {
52 _register_cmd($package, $sub, 'CMD');
53 } elsif ($attr =~ /^DEFAULT$/) {
54 _register_cmd($package, $sub, 'DEFAULT');
55 } elsif ($attr =~ /^FORMCMD$/) {
56 _register_cmd($package, $sub, 'FORM', '_process_form');
57 } elsif ($attr =~ /^SAFE$/) {
58 $package->_set_cmd_attr($sub, SAFE => TRUE);
59 } else {
60 push(@unknown_attrs, $attr);
61 }
62 }
63  
64 return @unknown_attrs;
65 }
66  
67 sub import {
68 my ($package, %opts) = @_;
69  
70 $package->SUPER::import(%opts);
71  
72 $opts{'path'} ||= '';
73  
74 my $app_pkg = caller();
75 die gettext('Use only in QBit::WebInterface and QBit::Application descendant')
76 unless $app_pkg->isa('QBit::WebInterface')
77 && $app_pkg->isa('QBit::Application');
78  
79 my $pkg_stash = package_stash($package);
80  
81 $pkg_stash->{'import_opts'} = \%opts;
82  
83 my $app_pkg_stash = package_stash($app_pkg);
84 $app_pkg_stash->{'__CMDS__'} = {}
85 unless exists($app_pkg_stash->{'__CMDS__'});
86  
87 my $pkg_sym_table = package_sym_table($package);
88  
89 foreach my $cmd (@{$pkg_stash->{'__CMDS__'} || []}) {
90 my ($name) =
91 grep {
92 !ref($pkg_sym_table->{$_})
93 && defined(&{$pkg_sym_table->{$_}})
94 && $cmd->{'sub'} == \&{$pkg_sym_table->{$_}}
95 }
96 keys %$pkg_sym_table;
97  
98 $cmd->{'attrs'} = $pkg_stash->{'__CMD_ATTRS__'}{$cmd->{'package'}, $cmd->{'sub'}} || {};
99  
100 if ($cmd->{'type'} eq 'DEFAULT') {
101 $app_pkg_stash->{'__CMDS__'}{$opts{'path'}}{'__DEFAULT__'} =
102 $app_pkg_stash->{'__CMDS__'}{$opts{'path'}}{$name};
103 $app_pkg_stash->{'__CMDS__'}{$opts{'path'}}{'__DEFAULT__'}{'name'} = $name;
104 } else {
105 die gettext("Cmd \"%s\" is exists in package \"%s\"",
106 $name, $app_pkg_stash->{'__CMDS__'}{$opts{'path'}}{$name}{'package'})
107 if exists($app_pkg_stash->{'__CMDS__'}{$opts{'path'}}{$name});
108 $app_pkg_stash->{'__CMDS__'}{$opts{'path'}}{$name} = $cmd;
109 }
110 }
111  
112 {
113 no strict 'refs';
114 foreach my $method (qw(get_option request response)) {
115 *{"${package}::${method}"} = sub {shift->app->$method(@_)};
116 }
117 }
118 }
119  
120 sub timelog {shift->app->timelog}
121  
122 sub break {
123 my ($self) = @_;
124  
125 $self->{'__BREAK_CMD__'} = TRUE;
126 }
127  
128 sub redirect2url {
129 my ($self, $url) = @_;
130  
131 $url =~ s/[\r\n]//gs;
132  
133 $self->response->status(302);
134 $self->response->location($url);
135 }
136  
137 sub redirect2url_internal {
138 my ($self, $url) = @_;
139  
140 $url = $self->_get_url_absolute($url);
141  
142 return $self->redirect2url($url);
143 }
144  
145 sub _get_url_absolute {
146 my ($self, $url, %opts) = @_;
147  
148 if ($url =~ /\/\//) {
149 my $my_host = $self->request->server_name;
150 return $self->denied if $url =~ /^(https?:)?\/\/(?!\Q$my_host\E)/i;
151 } else {
152 $url = $self->request->url(no_uri => 1) . $url unless $url =~ /\/\//;
153 }
154  
155 return $url;
156 }
157  
158 sub redirect {
159 my ($self, $cmd, %params) = @_;
160  
161 my $path = delete($params{'path'});
162 my $anchor = delete($params{'#anchor'});
163  
164 my $url = $self->app->make_cmd($cmd, $path, %params);
165  
166 $url .= '#' . uri_escape($anchor) if defined($anchor);
167  
168 return $self->redirect2url($url);
169 }
170  
171 sub denied {
172 my ($self) = @_;
173  
174 $self->response->status(403);
175 }
176  
177 sub not_found {
178 my ($self) = @_;
179  
180 $self->response->status(404);
181 }
182  
183 sub as_text {
184 my ($self, $text) = @_;
185  
186 $self->response->content_type('text/plain; charset=UTF-8');
187 $self->response->data($text);
188 }
189  
190 sub as_json {
191 my ($self, $data) = @_;
192  
193 $self->response->content_type('application/json; charset=UTF-8');
194 $self->response->data(to_json($data));
195 }
196  
197 sub as_jsonp {
198 my ($self, $func, $data) = @_;
199  
200 $self->response->content_type('text/javascript; charset=UTF-8');
201 $self->response->data("$func(" . to_json($data) . ')');
202 }
203  
204 sub from_template {
205 my ($self, $name, %opts) = @_;
206  
207 $self->response->data($self->_process_template($name, %opts, pre_process => ['common.tt2']));
208  
209 return TRUE;
210 }
211  
212 sub send_file {
213 my ($self, %opts) = @_;
214  
215 throw Exception::BadArguments gettext("No 'data'") unless defined($opts{data});
216 $opts{content_type} = 'application/octet-stream' unless defined($opts{content_type});
217  
218 $self->response->data($opts{data});
219 $self->response->filename($opts{filename});
220 $self->response->content_type($opts{content_type});
221  
222 return TRUE;
223 }
224  
225 sub import_opts {
226 my ($self) = @_;
227  
228 return package_stash(ref($self))->{'import_opts'};
229 }
230  
231 sub stash_set {
232 my ($self, $name, $value) = @_;
233  
234 $self->response->add_cookie($name => $value);
235  
236 return $value;
237 }
238  
239 sub stash_get {
240 my ($self, $name) = @_;
241  
242 my $value = $self->request->cookie($name);
243  
244 return $value;
245 }
246  
247 sub stash_delete {
248 my ($self, $name) = @_;
249  
250 my $value = $self->request->cookie($name);
251 $self->response->delete_cookie($name);
252  
253 return $value;
254 }
255  
256 sub gen_anti_csrf_token {
257 my ($self, %opts) = @_;
258  
259 my $date = int(name2date($opts{'yesterday'} ? 'yesterday' : 'today', oformat => 'sec') / 86400);
260 my $cur_user_id = $self->get_option(cur_user => {})->{'id'} || '';
261 $opts{'url'} ||= '';
262  
263 my $res = md5_hex($self->get_option('salt', '') . $date . $cur_user_id . $opts{'url'});
264  
265 return $res;
266 }
267  
268 sub check_anti_csrf_token {
269 my ($self, $token, %opts) = @_;
270  
271 my %gen_opts = hash_transform(\%opts, [qw(url)]);
272  
273 return $token eq $self->gen_anti_csrf_token(%gen_opts)
274 || $token eq $self->gen_anti_csrf_token(%gen_opts, yesterday => TRUE);
275 }
276  
277 sub _process_template {
278 my ($self, $name, %opts) = @_;
279  
280 $self->timelog->start(gettext('Processing template'));
281  
282 my $wself = $self;
283 weaken($wself);
284  
285 $self->timelog->start(gettext('Creating TT2'));
286 local $Template::Config::STASH = 'Template::Stash::XS';
287 local $Template::Config::PROVIDER = 'QBit::WebInterface::Controller::Template::Provider';
288  
289 my $template = Template->new(
290 INCLUDE_PATH => [
291 @{$self->get_option('TemplateIncludePaths') || []},
292 $self->get_option('ApplicationPath') . 'templates',
293 $self->get_option('FrameworkPath') . 'QBit/templates',
294 ],
295 COMPILE_EXT => '.ttc',
296 COMPILE_DIR => $self->get_option('TemplateCachePath')
297 || '/tmp/tt_cache-' . $self->request->server_name() . '-' . $self->request->server_port() . "_$<",
298 EVAL_PERL => 1,
299 MINIMIZE => $self->get_option('MinimizeTemplate'),
300 PRE_CHOMP => 2,
301 POST_CHOMP => 2,
302 TRIM => 2,
303 RECURSION => 1,
304 FILTERS => $wself->{'FILTERS'},
305 PRE_DEFINE => {
306 request => $wself->request,
307  
308 get_option => sub {$wself->get_option(@_)},
309 check_rights => sub {$wself->check_rights(@_)},
310 gettext => sub {return gettext(shift, @_)},
311 ngettext => sub {return ngettext(shift, shift, shift, @_)},
312 pgettext => sub {return pgettext(shift, shift, @_)},
313 npgettext => sub {return npgettext(shift, shift, shift, shift, @_)},
314  
315 dumper => sub {
316 local $Data::Dumper::Terse = 1;
317 my $dump_text = Data::Dumper::Dumper(@_);
318 for ($dump_text) {
319 s/&/&amp;/g;
320 s/ /&nbsp;/g;
321 s/</&lt;/g;
322 s/>/&gt;/g;
323 s/\n/<br>\n/g;
324 }
325 return $dump_text;
326 },
327 to_json => sub {
328 return to_json(shift);
329 },
330 cmd_link => sub {
331 my ($new_cmd, $new_path, $params) = @_;
332 return $wself->app->make_cmd($new_cmd, $new_path, %{$params || {}});
333 },
334 safecmd_link => sub {
335 my ($new_cmd, $new_path, $params) = @_;
336 return $wself->app->make_cmd(
337 $new_cmd,
338 $new_path,
339 %{$params || {}},
340 sign => $wself->gen_anti_csrf_token(
341 url => $wself->app->_get_new_path($new_path, $wself->get_option('cur_cmdpath')) . '/'
342 . $wself->app->_get_new_cmd($new_cmd, $wself->get_option('cur_cmd'))
343 )
344 );
345 },
346 sign_token => sub {
347 my ($new_cmd, $new_path) = @_;
348  
349 return $wself->gen_anti_csrf_token(
350 url => $wself->app->_get_new_path($new_path, $wself->get_option('cur_cmdpath')) . '/'
351 . $wself->app->_get_new_cmd($new_cmd, $wself->get_option('cur_cmd')));
352 },
353 format_date => sub {
354 return format_date(shift, shift, %{shift || {}});
355 },
356 format_number => sub {
357 return format_number(shift, %{shift || {}});
358 },
359 %TEMPLATE_PRE_DEFINE,
360 },
361 PRE_PROCESS => $opts{'pre_process'},
362 POST_PROCESS => $opts{'post_process'},
363 ) || throw $Template::ERROR;
364  
365 $self->timelog->finish();
366  
367 my $tt_res = '';
368 $template->process($name, {%{$opts{'vars'} || {}}, template_name => $name}, \$tt_res) || throw $template->error();
369  
370 $self->timelog->finish();
371  
372 return \$tt_res;
373 }
374  
375 sub _process_form {
376 my ($self, %opts) = @_;
377  
378 QBit::WebInterface::Controller::Form->new(%opts, controller => $self)->process();
379 }
380  
381 TRUE;