Пример #1
0
 /**
  * 現在の環境のHTTP_USER_AGENTの値に合致するインスタンスを返す
  *
  * @param string $agent ユーザーエージェント名
  * @param string $expect 期待値
  * @return void
  * @dataProvider findCurrentDataProvider
  */
 public function testFindCurrent($agent, $expect)
 {
     $_SERVER["HTTP_USER_AGENT"] = $agent;
     $result = $this->agent->findCurrent();
     if (!is_null($result)) {
         $this->assertEquals($expect, $result->name, '設定を正しく読み込めません');
     } else {
         $this->assertNull($result, '存在しないユーザーエージェント名で設定が読み込まれています');
     }
 }
Пример #2
0
 /**
  * beforeDispatch Event
  *
  * @param CakeEvent $event イベント
  * @return void|CakeResponse
  */
 public function beforeDispatch(CakeEvent $event)
 {
     $request = $event->data['request'];
     $response = $event->data['response'];
     $this->addDetectors($request);
     //アセットならスキップ
     if ($this->isAsset($request)) {
         Configure::write('BcRequest.asset', true);
         return;
     }
     //$_SERVER['HTTP_USER_AGENT']からエージェントを取得
     $agent = BcAgent::findCurrent();
     if (!is_null($agent) && $agent->isEnabled()) {
         if (!$request->is('admin') && $agent->shouldRedirects($request)) {
             $response->header('Location', $request->base . '/' . $agent->makeRedirectUrl($request));
             $response->statusCode(302);
             return $response;
         }
     }
     //URLからエージェントを取得
     $agentByUrl = BcAgent::findByUrl($request);
     if (!is_null($agentByUrl) && $agentByUrl->isEnabled()) {
         Configure::write('BcRequest.agent', $agentByUrl->name);
         Configure::write('BcRequest.agentPrefix', $agentByUrl->prefix);
         Configure::write('BcRequest.agentAlias', $agentByUrl->alias);
         /*
          * =========================================================
          * /m/files/... へのアクセスの場合、/files/... へ自動リダイレクト
          * CMSで作成するページ内のリンクは、モバイルでアクセスすると、
          * 自動的に、/m/ 付のリンクに書き換えられてしまう為、
          * files内のファイルへのリンクがリンク切れになってしまうので暫定対策。
          *
          * 2014/12/30 nakae bootstrap.phpから移行
          * =========================================================
          */
         $param = preg_replace('/^' . $agentByUrl->alias . '\\//', '', $request->url);
         if (preg_match('/^files/', $param)) {
             $response->statusCode(301);
             $response->header('Location', "{$request->base}/{$param}");
             return $response;
         }
     }
     //bootstrapから移動する
     //Configure::write('BcRequest.isUpdater', $this->isUpdate($request));
     //Configure::write('BcRequest.isMaintenance', $this->isMaintenance($request));
 }
Пример #3
0
 /**
  * 現在のサイトとユーザーエージェントに関連するサブサイトを取得する
  *
  * @param BcAbstractDetector $detector
  * @param bool $sameMainUrl
  * @return BcSite|null
  */
 public static function findCurrentSub($sameMainUrl = false, BcAgent $agent = null, $lang = null)
 {
     $currentSite = self::findCurrent();
     $sites = self::findAll();
     $subSite = null;
     if (!$lang) {
         $lang = BcLang::findCurrent();
     }
     if (!$agent) {
         $agent = BcAgent::findCurrent();
     }
     // 言語の一致するサブサイト候補に絞り込む
     $subSites = [];
     if ($lang) {
         foreach ($sites as $site) {
             if (!$sameMainUrl || $sameMainUrl && $site->sameMainUrl) {
                 if ($site->lang == $lang->name && $currentSite->id == $site->mainSiteId) {
                     $subSites[] = $site;
                     break;
                 }
             }
         }
     }
     if (!$subSites) {
         $subSites = $sites;
     }
     if ($agent) {
         foreach ($subSites as $subSite) {
             if (!$sameMainUrl || $sameMainUrl && $subSite->sameMainUrl) {
                 if ($subSite->device == $agent->name && $currentSite->id == $subSite->mainSiteId) {
                     return $subSite;
                 }
             }
         }
     }
     return null;
 }