source: branches/version-2_12-dev/data/class/helper/SC_Helper_Mobile.php @ 21470

Revision 21470, 16.6 KB checked in by Seasoft, 12 years ago (diff)

#1607 (未使用定義の削除)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24// {{{ requires
25require_once CLASS_REALDIR . '../module/Net/URL.php';
26require_once CLASS_REALDIR . 'SC_Query.php';
27
28/**
29 * モバイルのヘルパークラス.
30 *
31 * @package Helper
32 * @author LOCKON CO.,LTD.
33 * @version $Id$
34 */
35class SC_Helper_Mobile {
36
37    /** 基本MimeType */
38    var $defaultMimeType = 'application/force-download';
39
40    /** 拡張MimeType配列
41     * Application/octet-streamで対応出来ないファイルタイプのみ拡張子をキーに記述する
42     * 拡張子が本配列に存在しない場合は application/force-download を利用する */
43    var $arrMimetypes = array(
44            'html'=> 'text/html',
45            'css' => 'text/css',
46            'hdml'=> 'text/x-hdml',
47            'mmf' => 'application/x-smaf',
48            'jpeg'=> 'image/jpeg',
49            'jpg' => 'image/jpeg',
50            'gif' => 'image/gif',
51            'png' => 'image/png',
52            'bmp' => 'image/x-ms-bmp',
53            'amc' => 'application/x-mpeg',
54            '3g2' => 'video/3gpp2',
55            '3gp' => 'video/3gpp',
56            'jam' => 'application/x-jam',
57            'kjx' => 'application/x-kjx',
58            'jar' => 'application/java-archive',
59            'jad' => 'text/vnd.sun.j2me.app-descriptor',
60            'swf' => 'application/x-shockwave-flash',
61            'dmt' => 'application/x-decomail-template',
62            'khm' => 'application/x-kddi-htmlmail',
63            'hmt' => 'application/x-htmlmail-template',
64            'ucm' => 'application/x-ucf-package',
65            'ucp' => 'application/x-ucf-package',
66            'pdf' => 'application/pdf',
67            'wma' => 'audio/x-ms-wma',
68            'asf' => 'video/x-ms-asf',
69            'wax' => 'audio/x-ms-wax',
70            'wvx' => 'video/x-ms-wvx',
71            'wmv' => 'video/x-ms-wmv',
72            'asx' => 'video/asx',
73            'txt' => 'text/plain',
74            'exe' => 'application/octet-stream',
75            'zip' => 'application/zip',
76            'doc' => 'application/msword',
77            'xls' => 'application/vnd.ms-excel',
78            'ppt' => 'application/vnd.ms-powerpoint'
79        );
80
81    /**
82     * EC-CUBE がサポートする携帯端末かどうかをチェックする。
83     * 非対応端末の場合は /unsupported/ へリダイレクトする。
84     *
85     * @return void
86     */
87    function lfMobileCheckCompatibility() {
88        if (!SC_MobileUserAgent_Ex::isSupported()) {
89            header('Location: ' . ROOT_URLPATH . 'unsupported/' . DIR_INDEX_PATH);
90            exit;
91        }
92    }
93
94    /**
95     * 入力データを内部エンコーディングに変換し、絵文字を除去する。
96     *
97     * @param string &$value 入力データへの参照
98     * @return void
99     */
100    function lfMobileConvertInputValue(&$value) {
101        if (is_array($value)) {
102            foreach ($value as $key => $val) {
103                $this->lfMobileConvertInputValue($value[$key]);
104            }
105        } else {
106            // Shift JIS から内部エンコーディングに変換する。
107            $value = mb_convert_encoding($value, CHAR_CODE, 'SJIS');
108            // SoftBank? 以外の絵文字は外字領域に含まれるため、この段階で除去される。
109            // SoftBank? の絵文字を除去する。
110            $value = preg_replace('/\\x1b\\$[^\\x0f]*\\x0f/', '', $value);
111        }
112    }
113
114    /**
115     * モバイルサイト用の入力の初期処理を行う。
116     *
117     * @return void
118     */
119    function lfMobileInitInput() {
120        array_walk($_GET, array($this, 'lfMobileConvertInputValue'));
121        array_walk($_POST, array($this, 'lfMobileConvertInputValue'));
122        array_walk($_REQUEST, array($this, 'lfMobileConvertInputValue'));
123    }
124
125    /**
126     * dtb_mobile_ext_session_id テーブルを検索してセッションIDを取得する。
127     *
128     * @return string|null 取得したセッションIDを返す。
129     *                     取得できなかった場合は null を返す。
130     */
131    function lfMobileGetExtSessionId() {
132        if (!preg_match('|^' . ROOT_URLPATH . '(.*)$|', $_SERVER['SCRIPT_NAME'], $matches)) {
133            return null;
134        }
135
136        $url = $matches[1];
137        $time = date('Y-m-d H:i:s', time() - MOBILE_SESSION_LIFETIME);
138        $objQuery = new SC_Query_Ex();
139
140        foreach ($_REQUEST as $key => $value) {
141            $session_id = $objQuery->get('session_id', 'dtb_mobile_ext_session_id',
142                                         'param_key = ? AND param_value = ? AND url = ? AND create_date >= ?',
143                                         array($key, $value, $url, $time));
144            if (isset($session_id)) {
145                return $session_id;
146            }
147        }
148
149        return null;
150    }
151
152    /**
153     * パラメーターから有効なセッションIDを取得する。
154     *
155     * @return string|false 取得した有効なセッションIDを返す。
156     *                      取得できなかった場合は false を返す。
157     */
158    function lfMobileGetSessionId() {
159        // パラメーターからセッションIDを取得する。
160        $sessionId = @$_POST[session_name()];
161        if (!isset($sessionId)) {
162            $sessionId = @$_GET[session_name()];
163        }
164        if (!isset($sessionId)) {
165            $sessionId = $this->lfMobileGetExtSessionId();
166        }
167        if (!isset($sessionId)) {
168            return false;
169        }
170
171        // セッションIDの存在をチェックする。
172        $objSession = new SC_Helper_Session_Ex();
173        if ($objSession->sfSessRead($sessionId) === null) {
174            GC_Utils_Ex::gfPrintLog("Non-existent session id : sid=$sessionId");
175            return false;
176        }
177        return session_id($sessionId);
178    }
179
180    /**
181     * セッションデータが有効かどうかをチェックする。
182     *
183     * FIXME "@" でエラーを抑制するのは良くない
184     *
185     * @return boolean セッションデータが有効な場合は true、無効な場合は false を返す。
186     */
187    function lfMobileValidateSession() {
188        // 配列 mobile が登録されているかどうかをチェックする。
189        if (!is_array(@$_SESSION['mobile'])) {
190            return false;
191        }
192
193        // 有効期限を過ぎていないかどうかをチェックする。
194        if (intval(@$_SESSION['mobile']['expires']) < time()) {
195            GC_Utils_Ex::gfPrintLog("Session expired at " .
196                       date('Y/m/d H:i:s', @$_SESSION['mobile']['expires']) .
197                       ' : sid=' . session_id());
198
199            return false;
200        }
201
202        // 携帯端末の機種が一致するかどうかをチェックする。
203        $model = SC_MobileUserAgent_Ex::getModel();
204        if (@$_SESSION['mobile']['model'] != $model) {
205            GC_Utils_Ex::gfPrintLog("User agent model mismatch : " .
206                       "\"$model\" != \"" . @$_SESSION['mobile']['model'] .
207                       '" (expected), sid=' . session_id());
208            return false;
209        }
210
211        return true;
212    }
213
214    /**
215     * モバイルサイト用の出力の初期処理を行う。
216     *
217     * 出力の流れ
218     * 1. Smarty
219     * 2. 内部エンコーディングから Shift JIS に変換する。
220     * 3. 全角カタカナを半角カタカナに変換する。
221     * 4. 画像用のタグを調整する。
222     * 5. 絵文字タグを絵文字コードに変換する。
223     * 6. 出力
224     *
225     * @return void
226     */
227    function lfMobileInitOutput() {
228        // 出力用のエンコーディングを Shift JIS に固定する。
229        mb_http_output('SJIS-win');
230
231        // 端末に合わせて画像サイズを変換する。
232        ob_start(array('SC_MobileImage_Ex', 'handler'));
233
234        // 内部エンコーディングから Shift JIS に変換する。
235        ob_start('mb_output_handler');
236
237        //download.phpに対してカタカナ変換をするとファイルが壊れてしまうため回避する
238        if ($_SERVER['SCRIPT_FILENAME'] != HTML_REALDIR . "mypage/download.php") {
239            // 全角カタカナを半角カタカナに変換する。
240            ob_start(create_function('$buffer', 'return mb_convert_kana($buffer, "k");'));
241        }
242    }
243
244    /**
245     * モバイルサイト用の初期処理を行う。
246     *
247     * @return void
248     */
249    function sfMobileInit() {
250        $this->lfMobileInitInput();
251
252        if (basename(dirname($_SERVER['SCRIPT_NAME'])) != 'unsupported') {
253            $this->lfMobileCheckCompatibility();
254        }
255
256        $this->lfMobileInitOutput();
257    }
258
259    /**
260     * Location等でセッションIDを付加する必要があるURLにセッションIDを付加する。
261     *
262     * @return String
263     */
264    function gfAddSessionId($url = null) {
265        $objURL = new Net_URL($url);
266        $objURL->addQueryString(session_name(), session_id());
267        return $objURL->getURL();
268    }
269
270    /**
271     * セッション ID を付加した配列を返す.
272     *
273     * @param array $array 元となる配列
274     * @param array セッション ID を追加した配列
275     */
276    function sessionIdArray($array = array()) {
277        return array_merge($array, array(session_name() => session_id()));
278    }
279
280    /**
281     * 空メール用のトークンを生成する。
282     *
283     * @return string 生成したトークンを返す。
284     */
285    function lfGenerateKaraMailToken() {
286        $token_chars = '0123456789abcdefghijklmnopqrstuvwxyz';
287        $token_chars_length = strlen($token_chars);
288        $token_length = 10;
289        $token = '';
290
291        while ($token_length > 0) {
292            $token .= $token_chars{mt_rand(0, $token_chars_length - 1)};
293            --$token_length;
294        }
295
296        return $token;
297    }
298
299    /**
300     * 空メール管理テーブルに新規エントリーを登録し、トークンを返す。
301     *
302     * @param string $next_url 空メール受け付け後に遷移させるページ (モバイルサイトトップからの相対URL)
303     * @param string $session_id セッションID (省略した場合は現在のセッションID)
304     * @return string|false トークンを返す。エラーが発生した場合はfalseを返す。
305     */
306    function gfPrepareKaraMail($next_url, $session_id = null) {
307        if (!isset($session_id)) {
308            $session_id = session_id();
309        }
310
311        $objQuery = new SC_Query_Ex();
312
313        // GC
314        $time = date('Y-m-d H:i:s', time() - MOBILE_SESSION_LIFETIME);
315        $objQuery->delete('dtb_mobile_kara_mail', 'email IS NULL AND create_date < ?', array($time));
316
317        $objQuery->delete('dtb_mobile_kara_mail', 'session_id = ?', array($session_id));
318
319        $arrValues = array('session_id' => $session_id,
320                           'next_url'   => $next_url);
321
322        $try = 10;
323
324        while ($try > 0) {
325            $arrValues['token'] = $token = $this->lfGenerateKaraMailToken();
326
327            $arrValues['kara_mail_id'] = $objQuery->nextVal('dtb_mobile_kara_mail_kara_mail_id');
328            $objQuery->insert('dtb_mobile_kara_mail', $arrValues);
329            $exists = $objQuery->exists('dtb_mobile_kara_mail', 'token = ?', array($token));
330
331            if ($exists) {
332                break;
333            }
334
335            $objQuery->delete('dtb_mobile_kara_mail', 'session_id = ?', array($session_id));
336            $token = false;
337            --$try;
338        }
339
340        return $token;
341    }
342
343    /**
344     * 空メールから取得したメールアドレスを空メール管理テーブルに登録する。
345     *
346     * @param string $token トークン
347     * @param string $email メールアドレス
348     * @return boolean 成功した場合はtrue、失敗した場合はfalseを返す。
349     */
350    function gfRegisterKaraMail($token, $email) {
351        $objQuery = new SC_Query_Ex();
352
353        // GC
354        $time = date('Y-m-d H:i:s', time() - MOBILE_SESSION_LIFETIME);
355        $objQuery->delete('dtb_mobile_kara_mail',
356                          '(email IS NULL AND create_date < ?) OR (email IS NOT NULL AND receive_date < ?)',
357                          array($time, $time));
358
359        $kara_mail_id = $objQuery->get('kara_mail_id', 'dtb_mobile_kara_mail', 'token = ?', array($token));
360        if (!isset($kara_mail_id)) {
361            return false;
362        }
363
364        $arrValues = array('email' => $email);
365        $arrRawValues = array('receive_date' => 'CURRENT_TIMESTAMP');
366        $objQuery->update('dtb_mobile_kara_mail', $arrValues, 'kara_mail_id = ?', array($kara_mail_id), $arrRawValues);
367
368        return true;
369    }
370
371    /**
372     * 空メール管理テーブルからトークンが一致する行を削除し、
373     * 次に遷移させるページのURLを返す。 
374     *
375     * メールアドレスは $_SESSION['mobile']['kara_mail_from'] に登録される。
376     *
377     * @param string $token トークン
378     * @return string|false URLを返す。エラーが発生した場合はfalseを返す。
379     */
380    function gfFinishKaraMail($token) {
381        $objQuery = new SC_Query_Ex();
382
383        $arrRow = $objQuery->getRow(
384             'session_id, next_url, email'
385            ,'dtb_mobile_kara_mail'
386            ,'token = ? AND email IS NOT NULL AND receive_date >= ?'
387            ,array($token, date('Y-m-d H:i:s', time() - MOBILE_SESSION_LIFETIME))
388            ,DB_FETCHMODE_ORDERED
389        );
390
391        if (!isset($arrRow)) {
392            return false;
393        }
394
395        $objQuery->delete('dtb_mobile_kara_mail', 'token = ?', array($token));
396
397        list($session_id, $next_url, $email) = $arrRow;
398        $objURL = new Net_URL(HTTP_URL . $next_url);
399        $objURL->addQueryString(session_name(), $session_id);
400        $url = $objURL->getURL();
401
402        session_id($session_id);
403        session_start();
404        $_SESSION['mobile']['kara_mail_from'] = $email;
405        session_write_close();
406
407        return $url;
408    }
409
410    /**
411     * 外部サイト連携用にセッションIDとパラメーターの組み合わせを保存する。
412     *
413     * @param string $param_key パラメーター名
414     * @param string $param_value パラメーター値
415     * @param string $url URL
416     * @return void
417     */
418    function sfMobileSetExtSessionId($param_key, $param_value, $url) {
419        $objQuery = new SC_Query_Ex();
420
421        // GC
422        $time = date('Y-m-d H:i:s', time() - MOBILE_SESSION_LIFETIME);
423        $objQuery->delete('dtb_mobile_ext_session_id', 'create_date < ?', array($time));
424
425        $arrValues = array('session_id'  => session_id(),
426                           'param_key'   => $param_key,
427                           'param_value' => $param_value,
428                           'url'         => $url);
429
430        $objQuery->insert('dtb_mobile_ext_session_id', $arrValues);
431    }
432
433    /**
434     * メールアドレスが携帯のものかどうかを判別する。
435     *
436     * @param string $address メールアドレス
437     * @return boolean 携帯のメールアドレスの場合はtrue、それ以外の場合はfalseを返す。
438     */
439    function gfIsMobileMailAddress($address) {
440        $masterData = new SC_DB_MasterData_Ex();
441        $arrMobileMailDomains = $masterData->getMasterData("mtb_mobile_domain");
442
443        foreach ($arrMobileMailDomains as $domain) {
444            $domain = preg_quote($domain, '/');
445            if (preg_match("/@([^@]+\\.)?$domain\$/", $address)) {
446                return true;
447            }
448        }
449
450        return false;
451    }
452
453    /**
454     * ファイルのMIMEタイプを判別する
455     *
456     * @param string $filename ファイル名
457     * @return string MIMEタイプ
458     */
459    function getMimeType($filename) {
460        //ファイルの拡張子からコンテンツタイプを決定する
461        $file_extension = strtolower(substr(strrchr($filename,"."),1));
462        $mime_type = $this->defaultMimeType;
463        if (array_key_exists($file_extension, $this->arrMimetypes)) {
464            $mime_type = $this->arrMimetypes[$file_extension];
465        }
466        return $mime_type;
467    }
468}
Note: See TracBrowser for help on using the repository browser.