root/lang/perl/Moxy/trunk/lib/Moxy/Plugin/UserID.pm @ 25467

Revision 25467, 2.4 kB (checked in by tokuhirom, 4 years ago)

please remember my sid.

Line 
1package Moxy::Plugin::UserID;
2use strict;
3use warnings;
4use base qw/Moxy::Plugin/;
5use URI::Escape qw/uri_unescape/;
6use CGI;
7
8sub get_user_id :Hook('request_filter') {
9    my ($self, $context, $args) = @_;
10
11    my $key = join(',', __PACKAGE__, $args->{mobile_attribute}->user_agent);
12    my $user_id = $args->{session}->get($key);
13    if ($user_id) {
14        # au subscriber id.
15        if ($args->{mobile_attribute}->is_ezweb) {
16            $args->{request}->header('X-Up-Subno' => $user_id);
17        } elsif ($args->{mobile_attribute}->is_docomo && $args->{request}->uri =~ /guid=ON/i) {
18            $args->{request}->header('X-DCMGUID' => $user_id);
19        }
20    }
21}
22
23# save user id
24sub save_user_id :Hook('request_filter') {
25    my ($self, $context, $args) = @_;
26
27    if ($args->{request}->uri =~ m{^http://userid\.moxy/(.+)}) {
28        my $back = uri_unescape($1);
29
30        my $r = CGI->new($args->{request}->content);
31
32        # store to user stash.
33        my $key = join(',', __PACKAGE__, $args->{mobile_attribute}->user_agent);
34        $args->{session}->set($key => $r->param('user_id'));
35
36        # save history
37        do {
38            my $key = join(',', __PACKAGE__, $args->{mobile_attribute}->user_agent, 'history');
39            my $history = $args->{session}->get($key) || [];
40            unshift @$history, $r->param('user_id');
41            $args->{session}->set($key => $history);
42        };
43
44        my $response = HTTP::Response->new( 302, 'Moxy(UserID)' );
45        $response->header(Location => $back);
46        $response;
47    }
48}
49
50sub control_panel :Hook {
51    my ($self, $context, $args) = @_;
52    return '' unless $args->{mobile_attribute}->is_ezweb || $args->{mobile_attribute}->is_docomo;
53
54    my $key = join(',', __PACKAGE__, $args->{mobile_attribute}->user_agent);
55    my $user_id = $args->{session}->get($key);
56    my $history = $args->{session}->get(join(',', __PACKAGE__, $args->{mobile_attribute}->user_agent, 'history'));
57
58    return $self->render_template(
59        $context,
60        'panel.tt' => {
61            user_id          => $user_id,
62            referer          => $args->{response}->request->uri,
63            mobile_attribute => $args->{mobile_attribute},
64            history          => $history,
65        }
66    );
67}
68
691;
70__END__
71
72=head1 NAME
73
74Moxy::Plugin::UserID
75
76=head1 SYNOPSIS
77
78  - module: UserID
79
80=head1 DESCRIPTION
81
82Send X-Up-Subno
83
84=head1 TODO
85
86    softbank support
87
88=head1 AUTHOR
89
90Tokuhiro Matsuno
91
92=head1 SEE ALSO
93
94L<Moxy>, L<Moxy::Plugin::ControlPanel>
Note: See TracBrowser for help on using the browser.