| 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 | if (SC_Utils_Ex::isBlank($arrApiConfig)) { |
|---|
| 57 | $arrApiConfig = SC_Api_Utils_Ex::getAuthConfig($operation_name); |
|---|
| 58 | } |
|---|
| 59 | if (!SC_Utils_Ex::isBlank($arrApiConfig['sub_data'])) { |
|---|
| 60 | $arrData = @unserialize($arrApiConfig['sub_data']); |
|---|
| 61 | if ($arrData === FALSE) { |
|---|
| 62 | return $arrApiConfig['sub_data']; |
|---|
| 63 | } else { |
|---|
| 64 | if ($key_name != '') { |
|---|
| 65 | return $arrData['key_name']; |
|---|
| 66 | } else { |
|---|
| 67 | return $arrData; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | return false; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * オペレーション名に対応した認証の設定情報を取得する |
|---|
| 76 | * Configが無い場合は、APIデフォルトを取得する |
|---|
| 77 | * |
|---|
| 78 | * @param string $operation_name |
|---|
| 79 | * @return array 設定配列 |
|---|
| 80 | */ |
|---|
| 81 | public function getApiConfig($operation_name) { |
|---|
| 82 | // 設定優先度 DB > plugin default > base |
|---|
| 83 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 84 | $where = 'operation_name Like ? AND del_flg = 0 AND enable = 1'; |
|---|
| 85 | $arrApiConfig = $objQuery->getRow('*', 'dtb_api_config', $where, array($operation_name)); |
|---|
| 86 | if (SC_Utils_Ex::isBlank($arrApiConfig)) { |
|---|
| 87 | $objApi = SC_Api_Utils_Ex::loadApiOperation($operation_name); |
|---|
| 88 | if (is_object($objApi)) { |
|---|
| 89 | $arrApiConfig = $objApi->getDefaultConfig(); |
|---|
| 90 | } |
|---|
| 91 | if (!SC_Utils_Ex::isBlank($arrApiConfig)) { |
|---|
| 92 | // デフォルト設定がロード出来た場合は自動で設定に反映 |
|---|
| 93 | $arrData = $arrApiConfig; |
|---|
| 94 | $arrData['update_date'] = 'CURRENT_TIMESTAMP'; |
|---|
| 95 | $arrData['api_config_id'] = $objQuery->nextVal('dtb_api_config_api_config_id'); |
|---|
| 96 | $objQuery->insert('dtb_api_config', $arrData); |
|---|
| 97 | } else { |
|---|
| 98 | // ロード出来ない場合はAPI_Defaultを適用 |
|---|
| 99 | $operation_name = 'Default'; |
|---|
| 100 | $objApi = SC_Api_Utils_Ex::loadApiOperation($operation_name); |
|---|
| 101 | $arrApiConfig = $objApi->getDefaultConfig(); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | return $arrApiConfig; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * APIログ |
|---|
| 109 | * |
|---|
| 110 | * @param text $msg 出力文字列 |
|---|
| 111 | * @param text $operation_name |
|---|
| 112 | @ @rturn void |
|---|
| 113 | */ |
|---|
| 114 | public function printApiLog($msg, $start_time = '' , $operation_name = '') { |
|---|
| 115 | if (!SC_Utils_Ex::isBlank($operation_name)) { |
|---|
| 116 | $msg = 'API_' . $operation_name . ':' . $msg; |
|---|
| 117 | } |
|---|
| 118 | if (!SC_Utils_Ex::isBlank($start_time)) { |
|---|
| 119 | $msg = '(RequestId:' . $start_time . ')' . $msg; |
|---|
| 120 | } |
|---|
| 121 | GC_Utils_Ex::gfPrintLog($msg, DATA_REALDIR . self::API_LOGFILE, self::API_DEBUG_MODE); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | * APIオペレーションに対応したAPIクラスをインスタンス化 |
|---|
| 126 | * |
|---|
| 127 | * @param string $operation_name オペレーション名 |
|---|
| 128 | * @param array $arrParam リクエストパラメーター |
|---|
| 129 | * @return object APIオペレーションクラスオブジェクト |
|---|
| 130 | */ |
|---|
| 131 | public function loadApiOperation($operation_name, $arrParam = array()) { |
|---|
| 132 | // API_UPLOADのほうが優先 |
|---|
| 133 | // API_UPLOAD > API_CLASS_EX > API_CLASS |
|---|
| 134 | if (file_exists(API_UPLOAD_REALDIR . $operation_name . '.php')) { |
|---|
| 135 | $api_operation_file = API_UPLOAD_REALDIR . $operation_name . '.php'; |
|---|
| 136 | $api_class_name = 'API_' . $operation_name; |
|---|
| 137 | } elseif (file_exists(API_CLASS_EX_REALDIR . $operation_name . '_Ex.php')) { |
|---|
| 138 | $api_operation_file = API_CLASS_EX_REALDIR . $operation_name . '_Ex.php'; |
|---|
| 139 | $api_class_name = 'API_' . $operation_name . '_Ex'; |
|---|
| 140 | } elseif (file_exists(API_CLASS_REALDIR . $operation_name . '.php')) { |
|---|
| 141 | $api_operation_file = API_CLASS_REALDIR . $operation_name . '.php'; |
|---|
| 142 | $api_class_name = 'API_' . $operation_name; |
|---|
| 143 | } else { |
|---|
| 144 | return false; |
|---|
| 145 | } |
|---|
| 146 | require_once $api_operation_file; |
|---|
| 147 | $objApiOperation = new $api_class_name ($arrParam); |
|---|
| 148 | |
|---|
| 149 | return $objApiOperation; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * API Operationファイル一覧 |
|---|
| 154 | * |
|---|
| 155 | * @return array $arrFiles |
|---|
| 156 | */ |
|---|
| 157 | public function getApiDirFiles() { |
|---|
| 158 | $arrFiles = array(); |
|---|
| 159 | // Core API ディレクトリ |
|---|
| 160 | if (is_dir(API_CLASS_EX_REALDIR)) { |
|---|
| 161 | if ($dh = opendir(API_CLASS_EX_REALDIR)) { |
|---|
| 162 | while (($file_name = readdir($dh)) !== false) { |
|---|
| 163 | if ($file_name != '.' && $file_name != '..' && substr($file_name, -4) == '.php') { |
|---|
| 164 | $arrFiles[] = API_CLASS_EX_REALDIR . $file_name; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | closedir($dh); |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | // downaloads APIディレクトリ (for Plugin) |
|---|
| 172 | if (is_dir(API_UPLOAD_REALDIR)) { |
|---|
| 173 | if ($dh = opendir(API_UPLOAD_REALDIR)) { |
|---|
| 174 | while (($file_name = readdir($dh)) !== false) { |
|---|
| 175 | if ($file_name != '.' && $file_name != '..' && substr($file_name, -4) == '.php') { |
|---|
| 176 | $arrFiles[] = API_UPLOAD_REALDIR . $file_name; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | closedir($dh); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | return $arrFiles; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | public function sendResponseJson($response_outer_name, &$arrResponse) { |
|---|
| 186 | header('Content-Type: application/json; charset=UTF-8'); |
|---|
| 187 | $arrResponse['response_name'] = $response_outer_name; |
|---|
| 188 | echo SC_Utils_Ex::jsonEncode($arrResponse); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | public function sendResponsePhp($response_outer_name, &$arrResponse) { |
|---|
| 192 | header('Content-Type: application/php; charset=UTF-8'); |
|---|
| 193 | $arrResponse['response_name'] = $response_outer_name; |
|---|
| 194 | echo serialize($arrResponse); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | public function sendResponseXml($response_outer_name, &$arrResponse) { |
|---|
| 198 | require_once 'XML/Serializer.php'; |
|---|
| 199 | |
|---|
| 200 | $options = array( |
|---|
| 201 | 'mode' => 'simplexml', |
|---|
| 202 | 'indent' => "\t", |
|---|
| 203 | 'linebreak' => "\n", |
|---|
| 204 | 'typeHints' => false, |
|---|
| 205 | 'addDecl' => true, |
|---|
| 206 | 'encoding' => 'UTF-8', |
|---|
| 207 | 'rootName' => $response_outer_name, |
|---|
| 208 | 'rootAttributes' => array('xmlns' => self::API_XMLNS . ECCUBE_VERSION, |
|---|
| 209 | 'xml:lang' => self::API_XML_LANG), |
|---|
| 210 | 'defaultTagName' => 'Response', |
|---|
| 211 | 'attributesArray' => '_attributes' |
|---|
| 212 | ); |
|---|
| 213 | |
|---|
| 214 | $objSerializer = new XML_Serializer($options); |
|---|
| 215 | $ret = $objSerializer->serialize($arrResponse); |
|---|
| 216 | $xml = $objSerializer->getSerializedData(); |
|---|
| 217 | header('Content-Type: text/xml; charset=UTF-8'); |
|---|
| 218 | echo $xml; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | } |
|---|