root/lang/perl/NanoA/trunk/plugin/mobile.pm @ 24563

Revision 24563, 2.1 kB (checked in by kazuho, 5 years ago)

mv NanoA::mobile_agent to plugin

RevLine 
[24447]1package plugin::mobile;
2
3use strict;
4use warnings;
5use utf8;
6
7use Encode;
8
[24561]9use base qw(NanoA::Plugin);
10
11sub init_plugin {
12    my ($klass, $controller) = @_;
13    NanoA::register_hook($controller, 'prerun', \&_prerun, 0);
14    NanoA::register_hook($controller, 'postrun', \&_postrun, 90);
[24447]15}
16
[24449]17sub _prerun {
18    my $app = shift;
19    my $charset = _mobile_encoding($app->mobile_agent);
20   
21    return
22        if $charset eq 'utf-8';
23   
24    # build query object by myself and register it, since in first prerun,
25    # there is no query object yet
[24452]26    $app->query(
27        sub {
28            NanoA::require_once('CGI/Simple.pm');
29            local $CGI::Simple::PARAM_UTF8 = undef;
30            my $q = CGI::Simple->new();
31            # error occurs when trying to replace contents using Vars
32            for my $n ($q->param) {
33                my @v = $q->param($n);
34                if (@v >= 2) {
35                    $_ = decode($charset, $_)
36                        for @v;
37                    $q->param($n, \@v);
38                } else {
39                    $q->param($n, decode($charset, $v[0]));
40                }
[24449]41            }
[24452]42            $q;
43        },
44    );
[24449]45}
46
[24447]47sub _postrun {
48    my ($app, $bodyref) = @_;
[24449]49    my $charset = _mobile_encoding($app->mobile_agent);
[24447]50   
[24449]51    return
52        if $charset eq 'utf-8';
53   
[24561]54    $app->header(
[24449]55        -charset => $charset eq 'cp932' ? 'Shift_JIS' : $charset,
[24447]56    );
57    $$bodyref = encode($charset, $$bodyref);
58}
59
[24449]60# taken from MENTA
[24447]61sub _mobile_encoding {
62    my $ma = shift;
63    return 'utf-8' if $ma->is_non_mobile;
64    # docomo の 3G 端末では utf8 の表示が保障されている
65    return 'utf-8' if $ma->is_docomo && $ma->xhtml_compliant;
66    # softbank 3G の一部端末は cp932 だと絵文字を送ってこない不具合がある
67    return 'utf-8' if $ma->is_softbank && $ma->is_type_3gc;
68    # au は https のときに utf8 だと文字化ける場合がある
69    return 'cp932';
70}
71
[24563]72no warnings 'redefine';
73
74sub NanoA::mobile_agent {
75    my $self = shift;
76    NanoA::require_once('HTTP/MobileAgent.pm');
77    $self->{stash}->{'HTTP::MobileAgent'} ||= HTTP::MobileAgent->new();
78}
79
[24447]801;
Note: See TracBrowser for help on using the browser.