Showing
2 changed files
with
124 additions
and
0 deletions
@@ -16,6 +16,7 @@ use Yaf\Dispatcher; | @@ -16,6 +16,7 @@ use Yaf\Dispatcher; | ||
16 | use WebPlugin\Cache; | 16 | use WebPlugin\Cache; |
17 | use WebPlugin\Helpers; | 17 | use WebPlugin\Helpers; |
18 | use Hood\Session; | 18 | use Hood\Session; |
19 | +use WebPlugin\Mobile; | ||
19 | 20 | ||
20 | class WebAction extends Controller_Abstract | 21 | class WebAction extends Controller_Abstract |
21 | { | 22 | { |
@@ -48,6 +49,7 @@ class WebAction extends Controller_Abstract | @@ -48,6 +49,7 @@ class WebAction extends Controller_Abstract | ||
48 | */ | 49 | */ |
49 | public function init() | 50 | public function init() |
50 | { | 51 | { |
52 | + Mobile::isGoMobile(); | ||
51 | $this->_request = $this->getRequest(); | 53 | $this->_request = $this->getRequest(); |
52 | 54 | ||
53 | // 设置环境变量 | 55 | // 设置环境变量 |
library/Plugin/Mobile.php
0 → 100644
1 | +<?php | ||
2 | +/** | ||
3 | + * Created by PhpStorm. | ||
4 | + * User: haishengshen | ||
5 | + * Date: 14-3-6 | ||
6 | + * Time: 下午1:15 | ||
7 | + */ | ||
8 | +namespace WebPlugin; | ||
9 | +class Mobile | ||
10 | +{ | ||
11 | + /** | ||
12 | + * 获取用户User-Agent | ||
13 | + * @return bool | ||
14 | + */ | ||
15 | + public static function getUserAgent() | ||
16 | + { | ||
17 | + if (!isset($_SERVER['HTTP_USER_AGENT']) || empty($_SERVER['HTTP_USER_AGENT'])) { | ||
18 | + return ''; | ||
19 | + } | ||
20 | + return $_SERVER['HTTP_USER_AGENT']; | ||
21 | + } | ||
22 | + | ||
23 | + /** | ||
24 | + * 获取当前地址 | ||
25 | + * @return array | ||
26 | + */ | ||
27 | + public static function getNowUrl() | ||
28 | + { | ||
29 | + return array('host'=>$_SERVER['HTTP_HOST'], 'uri'=> $_SERVER['REQUEST_URI']); | ||
30 | + } | ||
31 | + | ||
32 | + /** | ||
33 | + * 获取手机版的对应地址 | ||
34 | + * @param $host | ||
35 | + * @return mixed | ||
36 | + */ | ||
37 | + public static function newHost($host) | ||
38 | + { | ||
39 | + if ($host == 'www.yohobuy.com' || $host == 'new.yohobuy.com' || stristr($host, 'new.yohobuy.com')) { | ||
40 | + return str_replace(array('www.yohobuy', 'new.yohobuy'), 'm.yohobuy', $host); | ||
41 | + } | ||
42 | + | ||
43 | + if ($host == 'huodong.yohobuy.com') { | ||
44 | + return str_replace('huodong.yohobuy', 'm.yohobuy', $host); | ||
45 | + } | ||
46 | + return str_replace('yohobuy.com', 'm.yohobuy.com', $host); | ||
47 | + } | ||
48 | + | ||
49 | + | ||
50 | + public static function match($url, $host, $uri) | ||
51 | + { | ||
52 | + //guang.yohobuy.com | ||
53 | + //guang.yohobuy.com/1.html | ||
54 | + $list = array( | ||
55 | + array( | ||
56 | + 'match' => '#^([guang\.]*)yohobuy\.com(\/*)$#', | ||
57 | + ), | ||
58 | + array( | ||
59 | + 'match' => '#([guang\.]*)yohobuy.com/([0-9]+).html#', | ||
60 | + 'to' => 'guang.m.yohobuy.com/info/index?id='.substr($uri,1,(strpos($uri,'.')-1)) , | ||
61 | + ), | ||
62 | + ); | ||
63 | + foreach ($list as $value) { | ||
64 | + if (preg_match($value['match'], $url) && !empty($value['to'])) { | ||
65 | + return $value['to']; | ||
66 | + } else if (preg_match($value['match'], $url)) { | ||
67 | + $newHost = self::newHost($host); | ||
68 | + return $newHost . $uri; | ||
69 | + } | ||
70 | + } | ||
71 | + | ||
72 | + $continueList = array('www', 'list', 'search'); | ||
73 | + $hostSplit = explode('.', $host); | ||
74 | + return 'm.yohobuy.com'; | ||
75 | + } | ||
76 | + | ||
77 | + /** | ||
78 | + * 获取新的URL | ||
79 | + * @return string | ||
80 | + */ | ||
81 | + public static function getNewUrl() | ||
82 | + { | ||
83 | + $url = self::getNowUrl(); | ||
84 | + return self::match($url['host'].$url['uri'], $url['host'], $url['uri']); | ||
85 | + } | ||
86 | + | ||
87 | + /** | ||
88 | + * 判断是否手机 | ||
89 | + * @return bool | ||
90 | + */ | ||
91 | + public static function isMobile() | ||
92 | + { | ||
93 | + $userAgent = self::getUserAgent(); | ||
94 | + $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte"); | ||
95 | + $is_mobile = false; | ||
96 | + if (stristr($userAgent, 'ipad')) { | ||
97 | + return $is_mobile; | ||
98 | + } | ||
99 | + foreach ($mobile_agents as $device) { | ||
100 | + if (stristr($userAgent, $device)) { | ||
101 | + $is_mobile = true; | ||
102 | + break; | ||
103 | + } | ||
104 | + } | ||
105 | + return $is_mobile; | ||
106 | + } | ||
107 | + | ||
108 | + /** | ||
109 | + * 是否转到手机版 | ||
110 | + * @return bool | ||
111 | + */ | ||
112 | + public static function isGoMobile() | ||
113 | + { | ||
114 | + if (empty($_COOKIE['m2w']) && self::isMobile()) { | ||
115 | + $newUrl = self::getNewUrl(); | ||
116 | + Header("HTTP/1.1 301 Moved Permanently"); | ||
117 | + header('Location:http://'.$newUrl); | ||
118 | + exit; | ||
119 | + } | ||
120 | + return true; | ||
121 | + } | ||
122 | +} |
-
Please register or login to post a comment