OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / include / lib / Net_UserAgent_Mobile-1.0.0 / Net / UserAgent / Mobile / SoftBank.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3
4 /**
5  * PHP versions 4 and 5
6  *
7  * Copyright (c) 2003-2009 KUBO Atsuhiro <kubo@iteman.jp>,
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  *     * Redistributions of source code must retain the above copyright
14  *       notice, this list of conditions and the following disclaimer.
15  *     * Redistributions in binary form must reproduce the above copyright
16  *       notice, this list of conditions and the following disclaimer in the
17  *       documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * @category   Networking
32  * @package    Net_UserAgent_Mobile
33  * @author     KUBO Atsuhiro <kubo@iteman.jp>
34  * @copyright  2003-2009 KUBO Atsuhiro <kubo@iteman.jp>
35  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD License
36  * @version    CVS: $Id: SoftBank.php 2043 2009-07-03 09:36:18Z fishbone $
37  * @link       http://creation.mb.softbank.jp/
38  * @since      File available since Release 0.20.0
39  *
40  * @modified-for        adjust for magic3 framework
41  * @modified-by         naoki hirata(naoki@aplo.co.jp)
42  * @modified-date       2009.7.3
43  */
44
45 //require_once 'Net/UserAgent/Mobile/Common.php';
46 //require_once 'Net/UserAgent/Mobile/Display.php';
47 //require_once 'Net/UserAgent/Mobile.php';
48 require_once dirname(__FILE__) . '/Common.php';         // modified by naoki.
49 require_once dirname(__FILE__) . '/Display.php';        // modified by naoki.
50
51 // {{{ Net_UserAgent_Mobile_SoftBank
52
53 /**
54  * SoftBank implementation
55  *
56  * Net_UserAgent_Mobile_SoftBank is a subclass of {@link Net_UserAgent_Mobile_Common},
57  * which implements SoftBank user agents.
58  *
59  * SYNOPSIS:
60  * <code>
61  * require_once 'Net/UserAgent/Mobile.php';
62  *
63  * $_SERVER['HTTP_USER_AGENT'] = 'J-PHONE/2.0/J-DN02';
64  * $agent = &Net_UserAgent_Mobile::factory();
65  *
66  * printf("Name: %s\n", $agent->getName()); // 'J-PHONE'
67  * printf("Version: %s\n", $agent->getVersion()); // 2.0
68  * printf("Model: %s\n", $agent->getModel()); // 'J-DN02'
69  * if ($agent->isPacketCompliant()) {
70  *     print "Packet is compliant.\n"; // false
71  * }
72  *
73  * // only availabe in Java compliant
74  * // e.g.) 'J-PHONE/4.0/J-SH51/SNXXXXXXXXX SH/0001a Profile/MIDP-1.0 Configuration/CLDC-1.0 Ext-Profile/JSCL-1.1.0'
75  * printf("Serial: %s\n", $agent->getSerialNumber()); // XXXXXXXXX
76  * printf("Vendor: %s\n", $agent->getVendor()); // 'SH'
77  * printf("Vendor Version: %s\n", $agent->getVendorVersion()); // '0001a'
78  *
79  * $info = $agent->getJavaInfo();  // array
80  * foreach ($info as $key => $value) {
81  *     print "$key: $value\n";
82  * }
83  * </code>
84  *
85  * @category   Networking
86  * @package    Net_UserAgent_Mobile
87  * @author     KUBO Atsuhiro <kubo@iteman.jp>
88  * @copyright  2003-2009 KUBO Atsuhiro <kubo@iteman.jp>
89  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD License
90  * @version    Release: 1.0.0
91  * @link       http://creation.mb.softbank.jp/
92  * @since      Class available since Release 0.20.0
93  */
94 class Net_UserAgent_Mobile_SoftBank extends Net_UserAgent_Mobile_Common
95 {
96
97     // {{{ properties
98
99     /**#@+
100      * @access public
101      */
102
103     /**#@-*/
104
105     /**#@+
106      * @access private
107      */
108
109     /**
110      * whether the agent is packet connection complicant or not
111      * @var boolean
112      */
113     var $_packetCompliant = false;
114
115     /**
116      * terminal unique serial number
117      * @var string
118      */
119     var $_serialNumber;
120
121     /**
122      * vendor code like 'SH'
123      * @var string
124      */
125     var $_vendor = '';
126
127     /**
128      * vendor version like '0001a'
129      * @var string
130      */
131     var $_vendorVersion;
132
133     /**
134      * Java profiles
135      * @var array
136      */
137     var $_javaInfo = array();
138
139     /**
140      * whether the agent is 3G
141      * @var boolean
142      */
143     var $_is3G = true;
144
145     /**
146      * the name of the mobile phone
147      * @var string
148      */
149     var $_msname = '';
150
151     /**#@-*/
152
153     /**#@+
154      * @access public
155      */
156
157     // }}}
158     // {{{ isJPhone()
159
160     /**
161      * returns true
162      *
163      * @return boolean
164      */
165     function isJPhone()
166     {
167         return $this->isSoftBank();
168     }
169
170     // }}}
171     // {{{ isVodafone()
172
173     /**
174      * returns true
175      *
176      * @return boolean
177      */
178     function isVodafone()
179     {
180         return $this->isSoftBank();
181     }
182
183     // }}}
184     // {{{ parse()
185
186     /**
187      * Parses HTTP_USER_AGENT string.
188      *
189      * @param string $userAgent User-Agent string
190      * @throws Net_UserAgent_Mobile_Error
191      */
192     function parse($userAgent)
193     {
194         $agent = explode(' ', $userAgent);
195         preg_match('!^(?:(SoftBank|Semulator|Vodafone|Vemulator|J-PHONE|J-EMULATOR)/\d\.\d|MOT-|MOTEMULATOR)!',
196                    $agent[0], $matches
197                    );
198         if (count($matches) > 1) {
199             $carrier = $matches[1];
200         } else {
201             $carrier = 'Motorola';
202         }
203
204         switch ($carrier) {
205         case 'SoftBank':
206         case 'Semulator':
207         case 'Vodafone':
208         case 'Vemulator':
209             $result = $this->_parseVodafone($agent);
210             break;
211         case 'J-PHONE':
212         case 'J-EMULATOR':
213             $result = $this->_parseJphone($agent);
214             break;
215         case 'Motorola':
216         case 'MOTEMULATOR':
217             $result = $this->_parseMotorola($agent);
218             break;
219         }
220
221         if (Net_UserAgent_Mobile::isError($result)) {
222             return $result;
223         }
224
225         $this->_msname = $this->getHeader('X-JPHONE-MSNAME');
226     }
227
228     // }}}
229     // {{{ makeDisplay()
230
231     /**
232      * create a new {@link Net_UserAgent_Mobile_Display} class instance
233      *
234      * @return Net_UserAgent_Mobile_Display
235      */
236     function makeDisplay() 
237     {
238         @list($width, $height) = explode('*', $this->getHeader('X-JPHONE-DISPLAY'));
239         $color = false;
240         $depth = 0;
241         if ($color_string = $this->getHeader('X-JPHONE-COLOR')) {
242             preg_match('!^([CG])(\d+)$!', $color_string, $matches);
243             $color = $matches[1] === 'C' ? true : false;
244             $depth = $matches[2];
245         }
246
247         return new Net_UserAgent_Mobile_Display(array('width'  => $width,
248                                                       'height' => $height,
249                                                       'depth'  => $depth,
250                                                       'color'  => $color)
251                                                 );
252     }
253
254     // }}}
255     // {{{ isPacketCompliant()
256
257     /**
258      * returns whether the agent is packet connection complicant or not
259      *
260      * @return boolean
261      */
262     function isPacketCompliant()
263     {
264         return $this->_packetCompliant;
265     }
266
267     // }}}
268     // {{{ getSerialNumber()
269
270     /**
271      * return terminal unique serial number. returns null if user forbids to send
272      * his/her serial number.
273      *
274      * @return string
275      */
276     function getSerialNumber()
277     {
278         return $this->_serialNumber;
279     }
280
281     // }}}
282     // {{{ getVendor()
283
284     /**
285      * returns vendor code like 'SH'
286      *
287      * @return string
288      */
289     function getVendor()
290     {
291         return $this->_vendor;
292     }
293
294     // }}}
295     // {{{ getVendorVersion()
296
297     /**
298      * returns vendor version like '0001a'. returns null if unknown.
299      *
300      * @return string
301      */
302     function getVendorVersion()
303     {
304         return $this->_vendorVersion;
305     }
306
307     // }}}
308     // {{{ getJavaInfo()
309
310     /**
311      * returns array of Java profiles
312      *
313      * Array structure is something like:
314      *
315      * - 'Profile'       => 'MIDP-1.0',
316      * - 'Configuration' => 'CLDC-1.0',
317      * - 'Ext-Profile'   => 'JSCL-1.1.0'
318      *
319      * @return array
320      */
321     function getJavaInfo()
322     {
323         return $this->_javaInfo;
324     }
325
326     // }}}
327     // {{{ getCarrierShortName()
328
329     /**
330      * returns the short name of the carrier
331      *
332      * @return string
333      */
334     function getCarrierShortName()
335     {
336         return 'S';
337     }
338
339     // }}}
340     // {{{ getCarrierLongName()
341
342     /**
343      * returns the long name of the carrier
344      *
345      * @return string
346      */
347     function getCarrierLongName()
348     {
349         return 'SoftBank';
350     }
351
352     // }}}
353     // {{{ isTypeC()
354
355     /**
356      * returns true if the type is C
357      *
358      * @return boolean
359      */
360     function isTypeC()
361     {
362         if ($this->_is3G || !preg_match('!^[32]\.!', $this->version)) {
363             return false;
364         }
365
366         return true;
367     }
368
369     // }}}
370     // {{{ isTypeP()
371
372     /**
373      * returns true if the type is P
374      *
375      * @return boolean
376      */
377     function isTypeP()
378     {
379         if ($this->_is3G || !preg_match('!^4\.!', $this->version)) {
380             return false;
381         }
382
383         return true;
384     }
385
386     // }}}
387     // {{{ isTypeW()
388
389     /**
390      * returns true if the type is W
391      *
392      * @return boolean
393      */
394     function isTypeW()
395     {
396         if ($this->_is3G || !preg_match('!^5\.!', $this->version)) {
397             return false;
398         }
399
400         return true;
401     }
402
403     // }}}
404     // {{{ isType3GC()
405
406     /**
407      * returns true if the type is 3GC
408      *
409      * @return boolean
410      */
411     function isType3GC()
412     {
413         return $this->_is3G;
414     }
415
416     // }}}
417     // {{{ getMsname()
418
419     /**
420      * returns the name of the mobile phone
421      *
422      * @return string the name of the mobile phone
423      */
424     function getMsname()
425     {
426         return $this->_msname;
427     }
428
429     // }}}
430     // {{{ isSoftBank()
431
432     /**
433      * returns true if the agent is SoftBank.
434      *
435      * @return boolean
436      */
437     function isSoftBank()
438     {
439         return true;
440     }
441
442     // }}}
443     // {{{ getUID()
444
445     /**
446      * Gets the UID of a subscriber.
447      *
448      * @return string
449      * @since Method available since Release 1.0.0RC1
450      */
451     function getUID()
452     {
453         if (array_key_exists('HTTP_X_JPHONE_UID', $_SERVER)) {
454             return $_SERVER['HTTP_X_JPHONE_UID'];
455         }
456     }
457
458     /**#@-*/
459
460     /**#@+
461      * @access private
462      */
463
464     // }}}
465     // {{{ _parseVodafone()
466
467     /**
468      * parse HTTP_USER_AGENT string for the Vodafone 3G aegnt
469      *
470      * @param array $agent parts of the User-Agent string
471      * @throws Net_UserAgent_Mobile_Error
472      */
473     function _parseVodafone($agent)
474     {
475         $this->_packetCompliant = true;
476
477         // Vodafone/1.0/V702NK/NKJ001 Series60/2.6 Nokia6630/2.39.148 Profile/MIDP-2.0 Configuration/CLDC-1.1
478         // Vodafone/1.0/V702NK/NKJ001/SN123456789012345 Series60/2.6 Nokia6630/2.39.148 Profile/MIDP-2.0 Configuration/CLDC-1.1
479         // Vodafone/1.0/V802SE/SEJ001/SN123456789012345 Browser/SEMC-Browser/4.1 Profile/MIDP-2.0 Configuration/CLDC-1.1
480         @list($this->name, $this->version, $this->_rawModel, $modelVersion,
481               $serialNumber) = explode('/', $agent[0]);
482         if ($serialNumber) {
483             if (!preg_match('!^SN(.+)!', $serialNumber, $matches)) {
484                 return $this->noMatch();
485             }
486
487             $this->_serialNumber = $matches[1];
488         }
489
490         if (!preg_match('!^([a-z]+)((?:[a-z]|\d){4})$!i', $modelVersion, $matches)) {
491             return $this->noMatch();
492         }
493
494         $this->_vendor = $matches[1];
495         $this->_vendorVersion = $matches[2];
496
497         for ($i = 2, $count = count($agent); $i < $count; ++$i) {
498             @list($key, $value) = explode('/', $agent[$i]);
499             $this->_javaInfo[$key] = $value;
500         }
501     }
502
503     // }}}
504     // {{{ _parseJphone()
505
506     /**
507      * parse HTTP_USER_AGENT string for the ancient agent
508      *
509      * @param array $agent parts of the User-Agent string
510      * @throws Net_UserAgent_Mobile_Error
511      */
512     function _parseJphone($agent)
513     {
514         $count = count($agent);
515         $this->_is3G = false;
516
517         if ($count > 1) {
518
519             // J-PHONE/4.0/J-SH51/SNJSHA3029293 SH/0001aa Profile/MIDP-1.0 Configuration/CLDC-1.0 Ext-Profile/JSCL-1.1.0
520             $this->_packetCompliant = true;
521             @list($this->name, $this->version, $this->_rawModel, $serialNumber) =
522                 explode('/', $agent[0]);
523             if ($serialNumber) {
524                 if (!preg_match('!^SN(.+)!', $serialNumber, $matches)) {
525                     return $this->noMatch();
526                 }
527
528                 $this->_serialNumber = $matches[1];
529             }
530
531             @list($this->_vendor, $this->_vendorVersion) = explode('/', $agent[1]);
532             for ($i = 2; $i < $count; ++$i) {
533                 @list($key, $value) = explode('/', $agent[$i]);
534                 $this->_javaInfo[$key] = $value;
535             }
536         } else {
537
538             // J-PHONE/2.0/J-DN02
539             @list($this->name, $this->version, $this->_rawModel, $serialNumber) =
540                 explode('/', $agent[0]);
541             if ($serialNumber) {
542                 if (!preg_match('!^SN(.+)!', $serialNumber, $matches)) {
543                     return $this->noMatch();
544                 }
545
546                 $this->_serialNumber = $matches[1];
547             }
548
549             if ($this->_rawModel) {
550                 if (preg_match('!V\d+([A-Z]+)!', $this->_rawModel, $matches)) {
551                     $this->_vendor = $matches[1];
552                 } elseif (preg_match('!J-([A-Z]+)!', $this->_rawModel, $matches)) {
553                     $this->_vendor = $matches[1];
554                 }
555             }
556         }
557     }
558
559     // }}}
560     // {{{ _parseMotorola()
561
562     /**
563      * parse HTTP_USER_AGENT string for the Motorola 3G aegnt
564      *
565      * @param array $agent parts of the User-Agent string
566      */
567     function _parseMotorola($agent)
568     {
569         $this->_packetCompliant = true;
570         $this->_vendor = 'MOT';
571
572         // MOT-V980/80.2F.2E. MIB/2.2.1 Profile/MIDP-2.0 Configuration/CLDC-1.1
573         @list($this->_rawModel, $this->_vendorVersion) = explode('/', $agent[0]);
574         $this->_model = substr(strrchr($this->_rawModel, '-'), 1);
575
576         for ($i = 2, $count = count($agent); $i < $count; ++$i) {
577             @list($key, $value) = explode('/', $agent[$i]);
578             $this->_javaInfo[$key] = $value;
579         }
580     }
581
582     /**#@-*/
583
584     // }}}
585 }
586
587 // }}}
588
589 /*
590  * Local Variables:
591  * mode: php
592  * coding: iso-8859-1
593  * tab-width: 4
594  * c-basic-offset: 4
595  * c-hanging-comment-ender-p: nil
596  * indent-tabs-mode: nil
597  * End:
598  */