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 | */ |
---|
31 | define('API_UPLOAD_REALDIR', DATA_REALDIR . 'downloads/api/'); |
---|
32 | define('API_CLASS_EX_REALDIR', CLASS_EX_REALDIR . 'api_extends/operations/'); |
---|
33 | define('API_CLASS_REALDIR', CLASS_REALDIR . 'api/operations/'); |
---|
34 | |
---|
35 | class SC_Api_Utils |
---|
36 | { |
---|
37 | /** API XML Namspase Header */ |
---|
38 | const API_XMLNS = 'http://www.ec-cube.net/ECCUBEApi/'; |
---|
39 | |
---|
40 | /** API XML lang */ |
---|
41 | const API_XML_LANG = 'ja'; |
---|
42 | |
---|
43 | /** API LOGFILE_NAME */ |
---|
44 | const API_LOGFILE = 'logs/api.log'; |
---|
45 | |
---|
46 | /** API_DEBUG_MODE */ |
---|
47 | const API_DEBUG_MODE = false; |
---|
48 | |
---|
49 | /** |
---|
50 | * オペレーション名に対応した追加の設定情報を取得する |
---|
51 | * |
---|
52 | * @param string $access_key |
---|
53 | * @return string 秘密鍵文字列 |
---|
54 | */ |
---|
55 | public function getOperationSubConfig($operation_name, $key_name = '', $arrApiConfig = '') |
---|
56 | { |
---|
57 | if (SC_Utils_Ex::isBlank($arrApiConfig)) { |
---|
58 | $arrApiConfig = SC_Api_Utils_Ex::getAuthConfig($operation_name); |
---|
59 | } |
---|
60 | if (!SC_Utils_Ex::isBlank($arrApiConfig['sub_data'])) { |
---|
61 | $arrData = @unserialize($arrApiConfig['sub_data']); |
---|
62 | if ($arrData === FALSE) { |
---|
63 | return $arrApiConfig['sub_data']; |
---|
64 | } else { |
---|
65 | if ($key_name != '') { |
---|
66 | return $arrData['key_name']; |
---|
67 | } else { |
---|
68 | return $arrData; |
---|
69 | } |
---|
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 | |
---|
108 | return $arrApiConfig; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * APIログ |
---|
113 | * |
---|
114 | * @param text $msg 出力文字列 |
---|
115 | * @param text $operation_name |
---|
116 | @ @rturn void |
---|
117 | */ |
---|
118 | public function printApiLog($msg, $start_time = '' , $operation_name = '') |
---|
119 | { |
---|
120 | if (!SC_Utils_Ex::isBlank($operation_name)) { |
---|
121 | $msg = 'API_' . $operation_name . ':' . $msg; |
---|
122 | } |
---|
123 | if (!SC_Utils_Ex::isBlank($start_time)) { |
---|
124 | $msg = '(RequestId:' . $start_time . ')' . $msg; |
---|
125 | } |
---|
126 | GC_Utils_Ex::gfPrintLog($msg, DATA_REALDIR . self::API_LOGFILE, self::API_DEBUG_MODE); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * APIオペレーションに対応したAPIクラスをインスタンス化 |
---|
131 | * |
---|
132 | * @param string $operation_name オペレーション名 |
---|
133 | * @param array $arrParam リクエストパラメーター |
---|
134 | * @return object APIオペレーションクラスオブジェクト |
---|
135 | */ |
---|
136 | public function loadApiOperation($operation_name, $arrParam = array()) |
---|
137 | { |
---|
138 | // API_UPLOADのほうが優先 |
---|
139 | // API_UPLOAD > API_CLASS_EX > API_CLASS |
---|
140 | if (file_exists(API_UPLOAD_REALDIR . $operation_name . '.php')) { |
---|
141 | $api_operation_file = API_UPLOAD_REALDIR . $operation_name . '.php'; |
---|
142 | $api_class_name = 'API_' . $operation_name; |
---|
143 | } elseif (file_exists(API_CLASS_EX_REALDIR . $operation_name . '_Ex.php')) { |
---|
144 | $api_operation_file = API_CLASS_EX_REALDIR . $operation_name . '_Ex.php'; |
---|
145 | $api_class_name = 'API_' . $operation_name . '_Ex'; |
---|
146 | } elseif (file_exists(API_CLASS_REALDIR . $operation_name . '.php')) { |
---|
147 | $api_operation_file = API_CLASS_REALDIR . $operation_name . '.php'; |
---|
148 | $api_class_name = 'API_' . $operation_name; |
---|
149 | } else { |
---|
150 | return false; |
---|
151 | } |
---|
152 | require_once $api_operation_file; |
---|
153 | $objApiOperation = new $api_class_name ($arrParam); |
---|
154 | |
---|
155 | return $objApiOperation; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * API Operationファイル一覧 |
---|
160 | * |
---|
161 | * @return array $arrFiles |
---|
162 | */ |
---|
163 | public function getApiDirFiles() |
---|
164 | { |
---|
165 | $arrFiles = array(); |
---|
166 | // Core API ディレクトリ |
---|
167 | if (is_dir(API_CLASS_EX_REALDIR)) { |
---|
168 | if ($dh = opendir(API_CLASS_EX_REALDIR)) { |
---|
169 | while (($file_name = readdir($dh)) !== false) { |
---|
170 | if ($file_name != '.' && $file_name != '..' && substr($file_name, -4) == '.php') { |
---|
171 | $arrFiles[] = API_CLASS_EX_REALDIR . $file_name; |
---|
172 | } |
---|
173 | } |
---|
174 | closedir($dh); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | // downaloads APIディレクトリ (for Plugin) |
---|
179 | if (is_dir(API_UPLOAD_REALDIR)) { |
---|
180 | if ($dh = opendir(API_UPLOAD_REALDIR)) { |
---|
181 | while (($file_name = readdir($dh)) !== false) { |
---|
182 | if ($file_name != '.' && $file_name != '..' && substr($file_name, -4) == '.php') { |
---|
183 | $arrFiles[] = API_UPLOAD_REALDIR . $file_name; |
---|
184 | } |
---|
185 | } |
---|
186 | closedir($dh); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | return $arrFiles; |
---|
191 | } |
---|
192 | |
---|
193 | public function sendResponseJson($response_outer_name, &$arrResponse) |
---|
194 | { |
---|
195 | header('Content-Type: application/json; charset=UTF-8'); |
---|
196 | $arrResponse['response_name'] = $response_outer_name; |
---|
197 | echo SC_Utils_Ex::jsonEncode($arrResponse); |
---|
198 | } |
---|
199 | |
---|
200 | public function sendResponsePhp($response_outer_name, &$arrResponse) |
---|
201 | { |
---|
202 | header('Content-Type: application/php; charset=UTF-8'); |
---|
203 | $arrResponse['response_name'] = $response_outer_name; |
---|
204 | echo serialize($arrResponse); |
---|
205 | } |
---|
206 | |
---|
207 | public function sendResponseXml($response_outer_name, &$arrResponse) |
---|
208 | { |
---|
209 | require_once 'XML/Serializer.php'; |
---|
210 | |
---|
211 | $options = array( |
---|
212 | 'mode' => 'simplexml', |
---|
213 | 'indent' => "\t", |
---|
214 | 'linebreak' => "\n", |
---|
215 | 'typeHints' => false, |
---|
216 | 'addDecl' => true, |
---|
217 | 'encoding' => 'UTF-8', |
---|
218 | 'rootName' => $response_outer_name, |
---|
219 | 'rootAttributes' => array('xmlns' => self::API_XMLNS . ECCUBE_VERSION, |
---|
220 | 'xml:lang' => self::API_XML_LANG), |
---|
221 | 'defaultTagName' => 'Response', |
---|
222 | 'attributesArray' => '_attributes' |
---|
223 | ); |
---|
224 | |
---|
225 | $objSerializer = new XML_Serializer($options); |
---|
226 | $ret = $objSerializer->serialize($arrResponse); |
---|
227 | $xml = $objSerializer->getSerializedData(); |
---|
228 | header('Content-Type: text/xml; charset=UTF-8'); |
---|
229 | echo $xml; |
---|
230 | } |
---|
231 | } |
---|