source: branches/version-2_12-dev/data/class/api/SC_Api_Utils.php @ 22567

Revision 22567, 8.2 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 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/**
25 * API関係処理のユーティリティ
26 *
27 * @package Api
28 * @author LOCKON CO.,LTD.
29 * @version $Id$
30 */
31define('API_UPLOAD_REALDIR', DATA_REALDIR . 'downloads/api/');
32define('API_CLASS_EX_REALDIR', CLASS_EX_REALDIR . 'api_extends/operations/');
33define('API_CLASS_REALDIR', CLASS_REALDIR . 'api/operations/');
34
35class SC_Api_Utils
36{
37
38    /** API XML Namspase Header */
39    const API_XMLNS = 'http://www.ec-cube.net/ECCUBEApi/';
40
41    /** API XML lang */
42    const API_XML_LANG = 'ja';
43
44    /** API LOGFILE_NAME */
45    const API_LOGFILE = 'logs/api.log';
46
47    /** API_DEBUG_MODE */
48    const API_DEBUG_MODE = false;
49
50    /**
51     * オペレーション名に対応した追加の設定情報を取得する
52     *
53     * @param string $access_key
54     * @return string 秘密鍵文字列
55     */
56    public function getOperationSubConfig($operation_name, $key_name = '', $arrApiConfig = '')
57    {
58        if (SC_Utils_Ex::isBlank($arrApiConfig)) {
59            $arrApiConfig = SC_Api_Utils_Ex::getAuthConfig($operation_name);
60        }
61        if (!SC_Utils_Ex::isBlank($arrApiConfig['sub_data'])) {
62            $arrData = @unserialize($arrApiConfig['sub_data']);
63            if ($arrData === FALSE) {
64                return $arrApiConfig['sub_data'];
65            } else {
66                if ($key_name != '') {
67                    return $arrData['key_name'];
68                } else {
69                    return $arrData;
70                }
71            }
72        }
73        return false;
74    }
75
76    /**
77     * オペレーション名に対応した認証の設定情報を取得する
78     * Configが無い場合は、APIデフォルトを取得する
79     *
80     * @param string $operation_name
81     * @return array 設定配列
82     */
83    public function getApiConfig($operation_name)
84    {
85        // 設定優先度 DB > plugin default > base
86        $objQuery =& SC_Query_Ex::getSingletonInstance();
87        $where = 'operation_name Like ? AND del_flg = 0 AND enable = 1';
88        $arrApiConfig = $objQuery->getRow('*', 'dtb_api_config', $where, array($operation_name));
89        if (SC_Utils_Ex::isBlank($arrApiConfig)) {
90            $objApi = SC_Api_Utils_Ex::loadApiOperation($operation_name);
91            if (is_object($objApi)) {
92                $arrApiConfig = $objApi->getDefaultConfig();
93            }
94            if (!SC_Utils_Ex::isBlank($arrApiConfig)) {
95                // デフォルト設定がロード出来た場合は自動で設定に反映
96                $arrData = $arrApiConfig;
97                $arrData['update_date'] = 'CURRENT_TIMESTAMP';
98                $arrData['api_config_id'] = $objQuery->nextVal('dtb_api_config_api_config_id');
99                $objQuery->insert('dtb_api_config', $arrData);
100            } else {
101                // ロード出来ない場合はAPI_Defaultを適用
102                $operation_name = 'Default';
103                $objApi = SC_Api_Utils_Ex::loadApiOperation($operation_name);
104                $arrApiConfig = $objApi->getDefaultConfig();
105            }
106        }
107        return $arrApiConfig;
108    }
109
110    /**
111     * APIログ
112     *
113     * @param text $msg 出力文字列
114     * @param text $operation_name
115     @ @rturn void
116     */
117    public function printApiLog($msg, $start_time = '' , $operation_name = '')
118    {
119        if (!SC_Utils_Ex::isBlank($operation_name)) {
120            $msg = 'API_' . $operation_name . ':' . $msg;
121        }
122        if (!SC_Utils_Ex::isBlank($start_time)) {
123            $msg = '(RequestId:' . $start_time . ')' . $msg;
124        }
125        GC_Utils_Ex::gfPrintLog($msg, DATA_REALDIR . self::API_LOGFILE, self::API_DEBUG_MODE);
126    }
127
128    /**
129     * APIオペレーションに対応したAPIクラスをインスタンス化
130     *
131     * @param string $operation_name オペレーション名
132     * @param array $arrParam リクエストパラメーター
133     * @return object APIオペレーションクラスオブジェクト
134     */
135    public function loadApiOperation($operation_name, $arrParam = array())
136    {
137        // API_UPLOADのほうが優先
138        // API_UPLOAD > API_CLASS_EX > API_CLASS
139        if (file_exists(API_UPLOAD_REALDIR . $operation_name . '.php')) {
140            $api_operation_file =  API_UPLOAD_REALDIR . $operation_name . '.php';
141            $api_class_name = 'API_' . $operation_name;
142        } elseif (file_exists(API_CLASS_EX_REALDIR . $operation_name . '_Ex.php')) {
143            $api_operation_file =  API_CLASS_EX_REALDIR . $operation_name . '_Ex.php';
144            $api_class_name = 'API_' . $operation_name . '_Ex';
145        } elseif (file_exists(API_CLASS_REALDIR . $operation_name . '.php')) {
146            $api_operation_file =  API_CLASS_REALDIR . $operation_name . '.php';
147            $api_class_name = 'API_' . $operation_name;
148        } else {
149            return false;
150        }
151        require_once $api_operation_file;
152        $objApiOperation = new $api_class_name ($arrParam);
153
154        return $objApiOperation;
155    }
156
157    /**
158     * API Operationファイル一覧
159     *
160     * @return array $arrFiles
161     */
162    public function getApiDirFiles()
163    {
164        $arrFiles = array();
165        // Core API ディレクトリ
166        if (is_dir(API_CLASS_EX_REALDIR)) {
167            if ($dh = opendir(API_CLASS_EX_REALDIR)) {
168                while (($file_name = readdir($dh)) !== false) {
169                    if ($file_name != '.' && $file_name != '..' && substr($file_name, -4) == '.php') {
170                        $arrFiles[] = API_CLASS_EX_REALDIR . $file_name;
171                    }
172                }
173                closedir($dh);
174            }
175        }
176
177        // downaloads APIディレクトリ (for Plugin)
178        if (is_dir(API_UPLOAD_REALDIR)) {
179            if ($dh = opendir(API_UPLOAD_REALDIR)) {
180                while (($file_name = readdir($dh)) !== false) {
181                    if ($file_name != '.' && $file_name != '..' && substr($file_name, -4) == '.php') {
182                        $arrFiles[] = API_UPLOAD_REALDIR . $file_name;
183                    }
184                }
185                closedir($dh);
186            }
187        }
188        return $arrFiles;
189    }
190
191    public function sendResponseJson($response_outer_name, &$arrResponse)
192    {
193        header('Content-Type: application/json; charset=UTF-8');
194        $arrResponse['response_name'] = $response_outer_name;
195        echo SC_Utils_Ex::jsonEncode($arrResponse);
196    }
197
198    public function sendResponsePhp($response_outer_name, &$arrResponse)
199    {
200        header('Content-Type: application/php; charset=UTF-8');
201        $arrResponse['response_name'] = $response_outer_name;
202        echo serialize($arrResponse);
203    }
204
205    public function sendResponseXml($response_outer_name, &$arrResponse)
206    {
207        require_once 'XML/Serializer.php';
208
209        $options = array(
210            'mode' => 'simplexml',
211            'indent' => "\t",
212            'linebreak' => "\n",
213            'typeHints' => false,
214            'addDecl' => true,
215            'encoding' => 'UTF-8',
216            'rootName' => $response_outer_name,
217            'rootAttributes' => array('xmlns' => self::API_XMLNS . ECCUBE_VERSION,
218                                        'xml:lang' => self::API_XML_LANG),
219            'defaultTagName' => 'Response',
220            'attributesArray' => '_attributes'
221        );
222
223        $objSerializer = new XML_Serializer($options);
224        $ret = $objSerializer->serialize($arrResponse);
225        $xml = $objSerializer->getSerializedData();
226        header('Content-Type: text/xml; charset=UTF-8');
227        echo $xml;
228    }
229
230}
Note: See TracBrowser for help on using the repository browser.