PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/data/module/Net/UserAgent/Mobile.php

https://gitlab.com/raku.takayama/eccube-2_13
PHP | 457 lines | 181 code | 61 blank | 215 comment | 27 complexity | 4442841977962b906f41d70c7cae3036 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4. * PHP versions 4 and 5
  5. *
  6. * Copyright (c) 2003-2009 KUBO Atsuhiro <kubo@iteman.jp>,
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * @category Networking
  31. * @package Net_UserAgent_Mobile
  32. * @author KUBO Atsuhiro <kubo@iteman.jp>
  33. * @copyright 2003-2009 KUBO Atsuhiro <kubo@iteman.jp>
  34. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  35. * @version CVS: $Id$
  36. * @since File available since Release 0.1
  37. */
  38. require_once dirname(__FILE__) . '/../../PEAR.php';
  39. require_once dirname(__FILE__) . '/Mobile/Error.php';
  40. // {{{ GLOBALS
  41. /**
  42. * globals for fallback on no match
  43. *
  44. * @global boolean $GLOBALS['NET_USERAGENT_MOBILE_FallbackOnNomatch']
  45. */
  46. $GLOBALS['NET_USERAGENT_MOBILE_FallbackOnNomatch'] = false;
  47. // }}}
  48. // {{{ Net_UserAgent_Mobile
  49. /**
  50. * HTTP mobile user agent string parser
  51. *
  52. * Net_UserAgent_Mobile parses HTTP_USER_AGENT strings of (mainly Japanese) mobile
  53. * HTTP user agents. It'll be useful in page dispatching by user agents.
  54. * This package was ported from Perl's HTTP::MobileAgent.
  55. * See {@link http://search.cpan.org/search?mode=module&query=HTTP-MobileAgent}
  56. *
  57. * SYNOPSIS:
  58. * <code>
  59. * require_once 'Net/UserAgent/Mobile.php';
  60. *
  61. * $agent = &Net_UserAgent_Mobile::factory($agent_string);
  62. * // or $agent = &Net_UserAgent_Mobile::factory(); // to get from $_SERVER
  63. *
  64. * if ($agent->isDoCoMo()) {
  65. * // or if ($agent->getName() == 'DoCoMo')
  66. * // or if (strtolower(get_class($agent)) == 'http_mobileagent_docomo')
  67. * // it's NTT DoCoMo i-mode
  68. * // see what's available in Net_UserAgent_Mobile_DoCoMo
  69. * } elseif ($agent->isSoftBank()) {
  70. * // it's SoftBank
  71. * // see what's available in Net_UserAgent_Mobile_SoftBank
  72. * } elseif ($agent->isEZweb()) {
  73. * // it's KDDI/EZWeb
  74. * // see what's available in Net_UserAgent_Mobile_EZweb
  75. * } else {
  76. * // may be PC
  77. * // $agent is Net_UserAgent_Mobile_NonMobile
  78. * }
  79. *
  80. * $display = $agent->getDisplay(); // Net_UserAgent_Mobile_Display
  81. * if ($display->isColor()) {
  82. * ...
  83. * }
  84. * </code>
  85. *
  86. * @category Networking
  87. * @package Net_UserAgent_Mobile
  88. * @author KUBO Atsuhiro <kubo@iteman.jp>
  89. * @copyright 2003-2009 KUBO Atsuhiro <kubo@iteman.jp>
  90. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  91. * @version Release: 1.0.0
  92. * @since Class available since Release 0.1
  93. */
  94. class Net_UserAgent_Mobile
  95. {
  96. // {{{ properties
  97. /**#@+
  98. * @access public
  99. */
  100. /**#@-*/
  101. /**#@+
  102. * @access private
  103. */
  104. /**#@-*/
  105. /**#@+
  106. * @access public
  107. * @static
  108. */
  109. // }}}
  110. // {{{ factory()
  111. /**
  112. * create a new {@link Net_UserAgent_Mobile_Common} subclass instance
  113. *
  114. * parses HTTP headers and constructs {@link Net_UserAgent_Mobile_Common}
  115. * subclass instance.
  116. * If no argument is supplied, $_SERVER{'HTTP_*'} is used.
  117. *
  118. * @param string $userAgent User-Agent string
  119. * @return Net_UserAgent_Mobile_Common a newly created or an existing
  120. * Net_UserAgent_Mobile_Common object
  121. * @throws Net_UserAgent_Mobile_Error
  122. */
  123. function &factory($userAgent = null)
  124. {
  125. if (is_null($userAgent)) {
  126. $userAgent = @$_SERVER['HTTP_USER_AGENT'];
  127. }
  128. // parse User-Agent string
  129. if (Net_UserAgent_Mobile::isDoCoMo($userAgent)) {
  130. $driver = 'DoCoMo';
  131. } elseif (Net_UserAgent_Mobile::isEZweb($userAgent)) {
  132. $driver = 'EZweb';
  133. } elseif (Net_UserAgent_Mobile::isSoftBank($userAgent)) {
  134. $driver = 'SoftBank';
  135. } elseif (Net_UserAgent_Mobile::isWillcom($userAgent)) {
  136. $driver = 'Willcom';
  137. } else {
  138. $driver = 'NonMobile';
  139. }
  140. $class = "Net_UserAgent_Mobile_$driver";
  141. if (!class_exists($class)) {
  142. $file = dirname(__FILE__) . "/Mobile/{$driver}.php";
  143. if (!include_once $file) {
  144. return PEAR::raiseError(null,
  145. NET_USERAGENT_MOBILE_ERROR_NOT_FOUND,
  146. null, null,
  147. "Unable to include the $file file",
  148. 'Net_UserAgent_Mobile_Error', true
  149. );
  150. }
  151. }
  152. PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
  153. $instance = new $class($userAgent);
  154. PEAR::staticPopErrorHandling();
  155. $error = &$instance->getError();
  156. if (Net_UserAgent_Mobile::isError($error)) {
  157. if ($GLOBALS['NET_USERAGENT_MOBILE_FallbackOnNomatch']
  158. && $error->getCode() == NET_USERAGENT_MOBILE_ERROR_NOMATCH
  159. ) {
  160. $instance = &Net_UserAgent_Mobile::factory('Net_UserAgent_Mobile_Fallback_On_NoMatch');
  161. return $instance;
  162. }
  163. return PEAR::raiseError($error);
  164. }
  165. return $instance;
  166. }
  167. // }}}
  168. // {{{ singleton()
  169. /**
  170. * creates a new {@link Net_UserAgent_Mobile_Common} subclass instance or returns
  171. * a instance from existent ones
  172. *
  173. * @param string $userAgent User-Agent string
  174. * @return Net_UserAgent_Mobile_Common a newly created or an existing
  175. * Net_UserAgent_Mobile_Common object
  176. * @throws Net_UserAgent_Mobile_Error
  177. */
  178. function &singleton($userAgent = null)
  179. {
  180. static $instances;
  181. if (!isset($instances)) {
  182. $instances = array();
  183. }
  184. if (is_null($userAgent)) {
  185. $userAgent = @$_SERVER['HTTP_USER_AGENT'];
  186. }
  187. if (!array_key_exists($userAgent, $instances)) {
  188. $instances[$userAgent] = Net_UserAgent_Mobile::factory($userAgent);
  189. }
  190. return $instances[$userAgent];
  191. }
  192. // }}}
  193. // {{{ isError()
  194. /**
  195. * tell whether a result code from a Net_UserAgent_Mobile method is an error
  196. *
  197. * @param integer $value result code
  198. * @return boolean whether $value is an {@link Net_UserAgent_Mobile_Error}
  199. */
  200. function isError($value)
  201. {
  202. return is_object($value)
  203. && (strtolower(get_class($value)) == strtolower('Net_UserAgent_Mobile_Error')
  204. || is_subclass_of($value, 'Net_UserAgent_Mobile_Error'));
  205. }
  206. // }}}
  207. // {{{ errorMessage()
  208. /**
  209. * return a textual error message for a Net_UserAgent_Mobile error code
  210. *
  211. * @param integer $value error code
  212. * @return string error message, or null if the error code was not recognized
  213. */
  214. function errorMessage($value)
  215. {
  216. static $errorMessages;
  217. if (!isset($errorMessages)) {
  218. $errorMessages = array(
  219. NET_USERAGENT_MOBILE_ERROR => 'unknown error',
  220. NET_USERAGENT_MOBILE_ERROR_NOMATCH => 'no match',
  221. NET_USERAGENT_MOBILE_ERROR_NOT_FOUND => 'not found',
  222. NET_USERAGENT_MOBILE_OK => 'no error'
  223. );
  224. }
  225. if (Net_UserAgent_Mobile::isError($value)) {
  226. $value = $value->getCode();
  227. }
  228. return isset($errorMessages[$value]) ?
  229. $errorMessages[$value] :
  230. $errorMessages[NET_USERAGENT_MOBILE_ERROR];
  231. }
  232. // }}}
  233. // {{{ isMobile()
  234. /**
  235. * Checks whether or not the user agent is mobile by a given user agent string.
  236. *
  237. * @param string $userAgent
  238. * @return boolean
  239. * @since Method available since Release 0.31.0
  240. */
  241. function isMobile($userAgent = null)
  242. {
  243. if (Net_UserAgent_Mobile::isDoCoMo($userAgent)) {
  244. return true;
  245. } elseif (Net_UserAgent_Mobile::isEZweb($userAgent)) {
  246. return true;
  247. } elseif (Net_UserAgent_Mobile::isSoftBank($userAgent)) {
  248. return true;
  249. } elseif (Net_UserAgent_Mobile::isWillcom($userAgent)) {
  250. return true;
  251. }
  252. return false;
  253. }
  254. // }}}
  255. // {{{ isDoCoMo()
  256. /**
  257. * Checks whether or not the user agent is DoCoMo by a given user agent string.
  258. *
  259. * @param string $userAgent
  260. * @return boolean
  261. * @since Method available since Release 0.31.0
  262. */
  263. function isDoCoMo($userAgent = null)
  264. {
  265. if (is_null($userAgent)) {
  266. $userAgent = @$_SERVER['HTTP_USER_AGENT'];
  267. }
  268. if (preg_match('!^DoCoMo!', $userAgent)) {
  269. return true;
  270. }
  271. return false;
  272. }
  273. // }}}
  274. // {{{ isEZweb()
  275. /**
  276. * Checks whether or not the user agent is EZweb by a given user agent string.
  277. *
  278. * @param string $userAgent
  279. * @return boolean
  280. * @since Method available since Release 0.31.0
  281. */
  282. function isEZweb($userAgent = null)
  283. {
  284. if (is_null($userAgent)) {
  285. $userAgent = @$_SERVER['HTTP_USER_AGENT'];
  286. }
  287. if (preg_match('!^KDDI-!', $userAgent)) {
  288. return true;
  289. } elseif (preg_match('!^UP\.Browser!', $userAgent)) {
  290. return true;
  291. }
  292. return false;
  293. }
  294. // }}}
  295. // {{{ isSoftBank()
  296. /**
  297. * Checks whether or not the user agent is SoftBank by a given user agent string.
  298. *
  299. * @param string $userAgent
  300. * @return boolean
  301. * @since Method available since Release 0.31.0
  302. */
  303. function isSoftBank($userAgent = null)
  304. {
  305. if (is_null($userAgent)) {
  306. $userAgent = @$_SERVER['HTTP_USER_AGENT'];
  307. }
  308. if (preg_match('!^SoftBank!', $userAgent)) {
  309. return true;
  310. } elseif (preg_match('!^Semulator!', $userAgent)) {
  311. return true;
  312. } elseif (preg_match('!^Vodafone!', $userAgent)) {
  313. return true;
  314. } elseif (preg_match('!^Vemulator!', $userAgent)) {
  315. return true;
  316. } elseif (preg_match('!^MOT-!', $userAgent)) {
  317. return true;
  318. } elseif (preg_match('!^MOTEMULATOR!', $userAgent)) {
  319. return true;
  320. } elseif (preg_match('!^J-PHONE!', $userAgent)) {
  321. return true;
  322. } elseif (preg_match('!^J-EMULATOR!', $userAgent)) {
  323. return true;
  324. }
  325. return false;
  326. }
  327. // }}}
  328. // {{{ isWillcom()
  329. /**
  330. * Checks whether or not the user agent is Willcom by a given user agent string.
  331. *
  332. * @param string $userAgent
  333. * @return boolean
  334. * @since Method available since Release 0.31.0
  335. */
  336. function isWillcom($userAgent = null)
  337. {
  338. if (is_null($userAgent)) {
  339. $userAgent = @$_SERVER['HTTP_USER_AGENT'];
  340. }
  341. if (preg_match('!^Mozilla/3\.0\((?:DDIPOCKET|WILLCOM);!', $userAgent)) {
  342. return true;
  343. }
  344. return false;
  345. }
  346. // }}}
  347. // {{{ isSmartphone()
  348. /**
  349. * Checks whether or not the user agent is Smartphone by a given user agent string.
  350. *
  351. * @param string $userAgent
  352. * @return boolean
  353. * @since Method available since Release 0.31.0
  354. */
  355. function isSmartphone($userAgent = null)
  356. {
  357. if (is_null($userAgent)) {
  358. $userAgent = @$_SERVER['HTTP_USER_AGENT'];
  359. }
  360. $useragents = array(
  361. 'iPhone', // Apple iPhone
  362. 'iPod', // Apple iPod touch
  363. 'Android', // 1.5+ Android
  364. 'dream', // Pre 1.5 Android
  365. 'CUPCAKE', // 1.5+ Android
  366. 'blackberry9500', // Storm
  367. 'blackberry9530', // Storm
  368. 'blackberry9520', // Storm v2
  369. 'blackberry9550', // Storm v2
  370. 'blackberry9800', // Torch
  371. 'webOS', // Palm Pre Experimental
  372. 'incognito', // Other iPhone browser
  373. 'webmate', // Other iPhone browser
  374. 'Windows Phone OS' // Windows Phone
  375. );
  376. $pattern = implode("|", $useragents);
  377. return preg_match('/'.$pattern.'/', $userAgent);
  378. }
  379. /**#@-*/
  380. /**#@+
  381. * @access private
  382. */
  383. /**#@-*/
  384. // }}}
  385. }
  386. // }}}
  387. /*
  388. * Local Variables:
  389. * mode: php
  390. * coding: iso-8859-1
  391. * tab-width: 4
  392. * c-basic-offset: 4
  393. * c-hanging-comment-ender-p: nil
  394. * indent-tabs-mode: nil
  395. * End:
  396. */