| 1 | package HTTP::MobileAgent::Plugin::Locator; |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | use HTTP::MobileAgent; |
|---|
| 6 | use Carp; |
|---|
| 7 | use UNIVERSAL::require; |
|---|
| 8 | use UNIVERSAL::can; |
|---|
| 9 | |
|---|
| 10 | use base qw( Exporter ); |
|---|
| 11 | our @EXPORT_OK = qw( $LOCATOR_AUTO_FROM_COMPLIANT $LOCATOR_AUTO $LOCATOR_GPS $LOCATOR_BASIC ); |
|---|
| 12 | our %EXPORT_TAGS = (locator => [@EXPORT_OK]); |
|---|
| 13 | |
|---|
| 14 | our $VERSION = '0.01'; |
|---|
| 15 | |
|---|
| 16 | our $DOCOMO_GPS_COMPLIANT_MODELS = qr/(?:903i(?!TV|X)|(?:90[45]|SA[78]0[02])i)/; |
|---|
| 17 | |
|---|
| 18 | our $LOCATOR_AUTO_FROM_COMPLIANT = 1; |
|---|
| 19 | our $LOCATOR_AUTO = 2; |
|---|
| 20 | our $LOCATOR_GPS = 3; |
|---|
| 21 | our $LOCATOR_BASIC = 4; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | sub import { |
|---|
| 25 | my ( $class ) = @_; |
|---|
| 26 | no strict 'refs'; |
|---|
| 27 | *{"HTTP\::MobileAgent\::gps_compliant"} = \&_gps_compliant; |
|---|
| 28 | *{"HTTP\::MobileAgent\::gps_parameter"} = \&_gps_parameter; |
|---|
| 29 | *{"HTTP\::MobileAgent\::locator"} = sub { $class->new( @_ ) }; |
|---|
| 30 | *{"HTTP\::MobileAgent\::get_location"} = sub { |
|---|
| 31 | my ( $self, $stuff, $option_ref ) = @_; |
|---|
| 32 | my $params = _prepare_params( $stuff ); |
|---|
| 33 | $self->locator( $params, $option_ref )->get_location( $params ); |
|---|
| 34 | }; |
|---|
| 35 | |
|---|
| 36 | $class->export_to_level(1, @_); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | sub _gps_compliant { |
|---|
| 40 | my $self = shift; |
|---|
| 41 | if ( $self->is_docomo ) { |
|---|
| 42 | return $self->model =~ $DOCOMO_GPS_COMPLIANT_MODELS; |
|---|
| 43 | } elsif ( $self->is_ezweb ) { |
|---|
| 44 | my @specs = split //, $ENV{ HTTP_X_UP_DEVCAP_MULTIMEDIA } || ''; |
|---|
| 45 | return defined $specs[ 1 ] && $specs[ 1 ] =~ /^[23]$/; |
|---|
| 46 | } elsif ( $self->is_softbank ) { |
|---|
| 47 | return $self->is_type_3gc; |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | sub _gps_parameter { |
|---|
| 53 | my ( $self, $stuff ) = @_; |
|---|
| 54 | |
|---|
| 55 | my $params = _prepare_params( $stuff ); |
|---|
| 56 | |
|---|
| 57 | if ( $self->is_docomo ) { |
|---|
| 58 | return (!defined $params->{AREACODE}) ? 1 : 0; |
|---|
| 59 | } |
|---|
| 60 | elsif ( $self->is_ezweb ) { |
|---|
| 61 | return ( $params->{datum} =~ /^\d+$/ ) ? 1 : 0; |
|---|
| 62 | } |
|---|
| 63 | elsif ( $self->is_softbank ) { |
|---|
| 64 | return ( defined $params->{pos} ) ? 1 : 0; |
|---|
| 65 | } |
|---|
| 66 | elsif ( $self->is_airh_phone ) { |
|---|
| 67 | return 0; |
|---|
| 68 | } |
|---|
| 69 | else { |
|---|
| 70 | croak( "Invalid mobile user agent: " . $self->user_agent ); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | sub new { |
|---|
| 76 | my ( $class, $agent, $params, $option_ref ) = @_; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | my $sub_locator = _get_sub_locator($agent, $params, $option_ref); |
|---|
| 80 | |
|---|
| 81 | my $locator_class = "HTTP::MobileAgent::Plugin::Locator\::$sub_locator"; |
|---|
| 82 | $locator_class->require or die $!; |
|---|
| 83 | return bless {}, $locator_class; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | sub get_location { die "ABSTRACT METHOD" } |
|---|
| 87 | |
|---|
| 88 | sub _get_sub_locator { |
|---|
| 89 | my ( $agent, $params, $option_ref ) = @_; |
|---|
| 90 | |
|---|
| 91 | my $carrier = ( $agent->is_docomo ) ? 'DoCoMo' : |
|---|
| 92 | ( $agent->is_ezweb ) ? 'EZweb' : |
|---|
| 93 | ( $agent->is_softbank ) ? 'SoftBank' : |
|---|
| 94 | ( $agent->is_airh_phone ) ? 'Willcom' : undef |
|---|
| 95 | ; |
|---|
| 96 | if ( !$carrier ) { |
|---|
| 97 | croak( "Invalid mobile user agent: " . $agent->user_agent ); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | my $locator; |
|---|
| 101 | if ( !defined $option_ref |
|---|
| 102 | || !defined $option_ref->{locator} |
|---|
| 103 | || $option_ref->{locator} eq $LOCATOR_AUTO_FROM_COMPLIANT ) |
|---|
| 104 | { |
|---|
| 105 | $locator = ( $agent->gps_compliant ) ? 'GPS' : 'BasicLocation'; |
|---|
| 106 | } |
|---|
| 107 | elsif ( $option_ref->{locator} eq $LOCATOR_AUTO ) { |
|---|
| 108 | $locator = ( $agent->gps_parameter( $params ) ) ? 'GPS' : 'BasicLocation'; |
|---|
| 109 | } |
|---|
| 110 | elsif ( $option_ref->{locator} eq $LOCATOR_GPS ) { |
|---|
| 111 | $locator = 'GPS'; |
|---|
| 112 | } |
|---|
| 113 | elsif ( $option_ref->{locator} eq $LOCATOR_BASIC ) { |
|---|
| 114 | $locator = 'BasicLocation'; |
|---|
| 115 | } |
|---|
| 116 | else { |
|---|
| 117 | croak( "Invalid locator: " . $option_ref->{locator} ); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | return $carrier . '::' . $locator; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | sub _prepare_params { |
|---|
| 124 | my $stuff = shift; |
|---|
| 125 | if ( ref $stuff && eval { $stuff->can( 'param' ) } ) { |
|---|
| 126 | return +{ map { $_ => $stuff->param( $_ ) } $stuff->param }; |
|---|
| 127 | } |
|---|
| 128 | else { |
|---|
| 129 | return $stuff; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | 1; |
|---|
| 134 | __END__ |
|---|
| 135 | |
|---|
| 136 | =head1 NAME |
|---|
| 137 | |
|---|
| 138 | HTTP::MobileAgent::Plugin::Locator - Handling mobile location information plugin for HTTP::MobileAgent |
|---|
| 139 | |
|---|
| 140 | =head1 SYNOPSIS |
|---|
| 141 | |
|---|
| 142 | use CGI; |
|---|
| 143 | use HTTP::MobileAgent; |
|---|
| 144 | use HTTP::MobileAgent::Plugin::Locator; |
|---|
| 145 | |
|---|
| 146 | # get location is Geo::Coordinates::Converter::Point instance formatted wgs84 |
|---|
| 147 | my $q = CGI->new; |
|---|
| 148 | my $agent = HTTP::MobileAgent->new; |
|---|
| 149 | my $location = $agent->get_location( $q ); |
|---|
| 150 | |
|---|
| 151 | print "lat is " . $location->lat; |
|---|
| 152 | print "lng is " . $location->lng; |
|---|
| 153 | |
|---|
| 154 | =head1 METHODS |
|---|
| 155 | |
|---|
| 156 | =over |
|---|
| 157 | |
|---|
| 158 | =item get_location([params], $option_ref); |
|---|
| 159 | |
|---|
| 160 | return Geo::Coordinates::Converter::Point instance formatted if specify gps or basic location parameters sent from carrier. The parameters are different by each carrier. |
|---|
| 161 | |
|---|
| 162 | This method accept a Apache instance, CGI instance or hashref of query parameters. |
|---|
| 163 | |
|---|
| 164 | =item gps_compliant() |
|---|
| 165 | |
|---|
| 166 | returns if the agent is GPS compliant. |
|---|
| 167 | |
|---|
| 168 | =back |
|---|
| 169 | |
|---|
| 170 | =head1 CLASSES |
|---|
| 171 | |
|---|
| 172 | =over |
|---|
| 173 | |
|---|
| 174 | =item HTTP::MobileAgent::Plugin::Locator::DoCoMo::BasicLocation |
|---|
| 175 | |
|---|
| 176 | for iArea data support. |
|---|
| 177 | |
|---|
| 178 | =item HTTP::MobileAgent::Plugin::Locator::DoCoMo::GPS |
|---|
| 179 | |
|---|
| 180 | for GPS data support. |
|---|
| 181 | |
|---|
| 182 | =item HTTP::MobileAgent::Plugin::Locator::EZweb::BasicLocation |
|---|
| 183 | |
|---|
| 184 | for basic location information data support. |
|---|
| 185 | |
|---|
| 186 | =item HTTP::MobileAgent::Plugin::Locator::EZweb::GPS |
|---|
| 187 | |
|---|
| 188 | for EZnavi data support. |
|---|
| 189 | |
|---|
| 190 | =item HTTP::MobileAgent::Plugin::Locator::SoftBank::BasicLocation |
|---|
| 191 | |
|---|
| 192 | for basic location information data support. |
|---|
| 193 | |
|---|
| 194 | =item HTTP::MobileAgent::Plugin::Locator::SoftBank::GPS |
|---|
| 195 | |
|---|
| 196 | for GPS data support. |
|---|
| 197 | |
|---|
| 198 | =item HTTP::MobileAgent::Plugin::Locator::Willcom::BasicLocation |
|---|
| 199 | |
|---|
| 200 | for basic location information data support. |
|---|
| 201 | |
|---|
| 202 | =back |
|---|
| 203 | |
|---|
| 204 | =head1 EXAMPLES |
|---|
| 205 | |
|---|
| 206 | There is request template using C<Template> in eg directory and mod_rewrite configuration for ezweb extraordinary parameter handling. |
|---|
| 207 | |
|---|
| 208 | =head1 AUTHOR |
|---|
| 209 | |
|---|
| 210 | Yoshiki Kurihara E<lt>kurihara __at__ cpan.orgE<gt> with many feedbacks and changes from: |
|---|
| 211 | |
|---|
| 212 | Tokuhiro Matsuno E<lt>tokuhiro __at__ mobilefactory.jpE<gt> |
|---|
| 213 | |
|---|
| 214 | =head1 SEE ALSO |
|---|
| 215 | |
|---|
| 216 | C<HTTP::MobileAgent>, C<Geo::Coordinates::Converter>, C<Geo::Coordinates::Converter::Point>, C<Geo::Coordinates::Converter::iArea>, C<http://coderepos.org/share/log/lang/perl/HTTP-MobileAgent-Plugin-Locator/> |
|---|
| 217 | |
|---|
| 218 | =head1 LICENCE AND COPYRIGHT |
|---|
| 219 | |
|---|
| 220 | Copyright (c) 2008, Yoshiki Kurihara E<lt>kurihara __at__ cpan.orgE<gt>. All rights reserved. |
|---|
| 221 | |
|---|
| 222 | This module is free software; you can redistribute it and/or |
|---|
| 223 | modify it under the same terms as Perl itself. See L<perlartistic>. |
|---|
| 224 | |
|---|
| 225 | =head1 DISCLAIMER OF WARRANTY |
|---|
| 226 | |
|---|
| 227 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
|---|
| 228 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
|---|
| 229 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
|---|
| 230 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
|---|
| 231 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 232 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
|---|
| 233 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
|---|
| 234 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
|---|
| 235 | NECESSARY SERVICING, REPAIR, OR CORRECTION. |
|---|
| 236 | |
|---|
| 237 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
|---|
| 238 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
|---|
| 239 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE |
|---|
| 240 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
|---|
| 241 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
|---|
| 242 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
|---|
| 243 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
|---|
| 244 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
|---|
| 245 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
|---|
| 246 | SUCH DAMAGES. |
|---|