[21713] | 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 | /** |
---|
| 25 | * APIの実行処理本体 |
---|
| 26 | * 権限チェックと設定チェックを行い、APIオペレーション本体を呼び出す。 |
---|
| 27 | * 結果データの生成 |
---|
| 28 | * |
---|
| 29 | * @package Api |
---|
| 30 | * @author LOCKON CO.,LTD. |
---|
| 31 | * @version $Id$ |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | class SC_Api_Operation { |
---|
| 36 | |
---|
| 37 | /** API_DEBUG_MODE */ |
---|
| 38 | const API_DEBUG_MODE = false; |
---|
| 39 | |
---|
| 40 | /** 認証タイプ */ |
---|
| 41 | const API_AUTH_TYPE_REFERER = '1'; // リファラー |
---|
| 42 | const API_AUTH_TYPE_SESSION_TOKEN = '2'; // CSRF TOKEN |
---|
| 43 | const API_AUTH_TYPE_API_SIGNATURE = '3'; // API 署名認証 推奨 |
---|
| 44 | const API_AUTH_TYPE_CUSTOMER = '4'; // 会員認証 |
---|
| 45 | const API_AUTH_TYPE_MEMBER = '5'; // 管理者認証 |
---|
| 46 | const API_AUTH_TYPE_CUSTOMER_LOGIN_SESSION = '6'; // 顧客ログインセッションが有効 |
---|
| 47 | const API_AUTH_TYPE_MEMBER_LOGIN_SESSION = '7'; // 管理者ログインセッションが有効 |
---|
| 48 | const API_AUTH_TYPE_IP = '8'; // IP認証 |
---|
| 49 | const API_AUTH_TYPE_HOST = '9'; // ホスト認証 |
---|
| 50 | const API_AUTH_TYPE_SSL = '10'; // SSL強制 |
---|
| 51 | const API_AUTH_TYPE_OPEN = '99'; // 完全オープン |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * 有効な管理者ID/PASSかどうかチェックする |
---|
| 55 | * |
---|
| 56 | * @param string $member_id ログインID文字列 |
---|
| 57 | * @param string $member_password ログインパスワード文字列 |
---|
| 58 | * @return boolean ログイン情報が有効な場合 true |
---|
| 59 | */ |
---|
| 60 | protected function checkMemberAccount($member_id, $member_password) { |
---|
| 61 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 62 | //パスワード、saltの取得 |
---|
| 63 | $cols = 'password, salt'; |
---|
| 64 | $table = 'dtb_member'; |
---|
| 65 | $where = 'login_id = ? AND del_flg <> 1 AND work = 1'; |
---|
[21722] | 66 | $arrData = $objQuery->getRow($cols, $table, $where, array($member_id)); |
---|
[21713] | 67 | if (SC_Utils_Ex::isBlank($arrData)) { |
---|
| 68 | return false; |
---|
| 69 | } |
---|
| 70 | // ユーザー入力パスワードの判定 |
---|
[21722] | 71 | if (SC_Utils_Ex::sfIsMatchHashPassword($member_password, $arrData['password'], $arrData['salt'])) { |
---|
[21713] | 72 | return true; |
---|
| 73 | } |
---|
| 74 | return false; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * 会員ログインチェックを実行する. |
---|
| 79 | * |
---|
| 80 | * @param string $login_email ログインメールアドレス |
---|
| 81 | * @param string $password ログインパスワード |
---|
| 82 | * @return boolean ログインに成功した場合 true; 失敗した場合 false |
---|
| 83 | */ |
---|
| 84 | protected function checkCustomerAccount($login_email, $login_password) { |
---|
| 85 | $objCustomer = new SC_Customer_Ex(); |
---|
| 86 | if ($objCustomer->getCustomerDataFromEmailPass($login_password, $login_email)) { |
---|
| 87 | return true; |
---|
| 88 | } else { |
---|
| 89 | return false; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * リファラーチェックを実行する. |
---|
| 95 | * |
---|
| 96 | * @return boolean チェックに成功した場合 true; 失敗した場合 false |
---|
| 97 | */ |
---|
| 98 | protected function checkReferer() { |
---|
| 99 | $ret = false; |
---|
| 100 | if(!SC_Utils_Ex::isBlank($_SERVER['HTTP_REFERER'])) { |
---|
| 101 | $domain = SC_Utils_Ex::sfIsHTTPS() ? HTTPS_URL : HTTP_URL; |
---|
| 102 | $pattern = sprintf('|^%s.*|', $domain); |
---|
| 103 | $referer = $_SERVER['HTTP_REFERER']; |
---|
| 104 | if (preg_match($pattern, $referer)) { |
---|
| 105 | $ret = true; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | return $ret; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * HMAC-SHA 署名認証チェック |
---|
| 113 | * Refer: http://www.soumu.go.jp/main_sosiki/joho_tsusin/top/ninshou-law/law-index.html |
---|
| 114 | * |
---|
| 115 | * @param string 実行処理名 |
---|
| 116 | * @param array リクエストパラメータ |
---|
| 117 | * @return boolean 署名認証に成功した場合 true; 失敗した場合 false |
---|
| 118 | */ |
---|
| 119 | protected function checkApiSignature($operation_name, $arrParam, $arrApiConfig) { |
---|
| 120 | if (SC_Utils_Ex::isBlank($arrParam['Signature'])) { |
---|
| 121 | return false; |
---|
| 122 | } |
---|
| 123 | if (SC_Utils_Ex::isBlank($arrParam['Timestamp'])) { |
---|
| 124 | return false; |
---|
| 125 | } |
---|
| 126 | /* |
---|
| 127 | $allow_account_id = SC_Api_Operation_Ex::getOperationSubConfig($operation_name, 'allow_account_id', $arrApiConfig); |
---|
| 128 | if (!SC_Utils_Ex::isBlank($allow_account_id) and) { |
---|
| 129 | $arrAllowAccountIds = explode('|', $allow_account_id); |
---|
| 130 | |
---|
| 131 | } |
---|
| 132 | */ |
---|
| 133 | |
---|
| 134 | $access_key = $arrParam['AccessKeyId']; |
---|
| 135 | $secret_key = SC_Api_Operation_Ex::getApiSecretKey($access_key); |
---|
[21722] | 136 | if(SC_Utils_Ex::isBlank($secret_key)) { |
---|
| 137 | return false; |
---|
| 138 | } |
---|
[21713] | 139 | |
---|
| 140 | // バイト順に並び替え |
---|
| 141 | ksort($arrParam); |
---|
| 142 | |
---|
| 143 | // 規定の文字列フォーマットを作成する |
---|
| 144 | // Refer: https://images-na.ssl-images-amazon.com/images/G/09/associates/paapi/dg/index.html?Query_QueryAuth.html |
---|
| 145 | $check_str = ''; |
---|
| 146 | foreach($arrParam as $key => $val) { |
---|
| 147 | switch ($key) { |
---|
| 148 | case 'Signature': |
---|
| 149 | break; |
---|
| 150 | default: |
---|
| 151 | $check_str .= '&' . SC_Utils_Ex::encodeRFC3986($key) . '=' . SC_Utils_Ex::encodeRFC3986($val); |
---|
| 152 | break; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | $check_str = substr($check_str, 1); |
---|
| 156 | $check_str = strtoupper($_SERVER['REQUEST_METHOD']) . "\n" |
---|
| 157 | . strtolower($_SERVER['SERVER_NAME']) . "\n" |
---|
| 158 | . $_SERVER['PHP_SELF'] . "\n" |
---|
| 159 | . $check_str; |
---|
| 160 | $signature = base64_encode(hash_hmac('sha256', $check_str, $secret_key, true)); |
---|
| 161 | if($signature === $arrParam['Signature']) { |
---|
| 162 | return true; |
---|
| 163 | } |
---|
| 164 | return false; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * IPチェックを実行する. |
---|
| 169 | * |
---|
| 170 | * @param string 実行処理名 |
---|
| 171 | * @return boolean チェックに成功した場合 true; 失敗した場合 false |
---|
| 172 | */ |
---|
| 173 | protected function checkIp($operation_name) { |
---|
| 174 | $ret = false; |
---|
| 175 | $allow_hosts = SC_Api_Utils_Ex::getOperationSubConfig($operation_name, 'allow_hosts'); |
---|
| 176 | $arrAllowHost = explode("\n", $allow_hosts); |
---|
| 177 | if (is_array($arrAllowHost) && count($arrAllowHost) > 0) { |
---|
| 178 | if (array_search($_SERVER['REMOTE_ADDR'], $arrAllowHost) !== FALSE) { |
---|
| 179 | $ret = true; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | return $ret; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | * ApiAccessKeyに対応した秘密鍵を取得する。 |
---|
| 187 | * |
---|
| 188 | * @param string $access_key |
---|
| 189 | * @return string 秘密鍵文字列 |
---|
| 190 | */ |
---|
| 191 | protected function getApiSecretKey($access_key) { |
---|
| 192 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[21722] | 193 | $secret_key = $objQuery->get('api_secret_key', 'dtb_api_account', 'api_access_key = ? and enable = 1 and del_flg = 0', array($access_key)); |
---|
[21713] | 194 | return $secret_key; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * オペレーションの実行権限をチェックする |
---|
| 199 | * |
---|
| 200 | * @param string オペレーション名 |
---|
| 201 | * @param array リクエストパラメータ |
---|
| 202 | * @return boolean 権限がある場合 true; 無い場合 false |
---|
| 203 | */ |
---|
[21722] | 204 | protected function checkOperationAuth($operation_name, &$arrParam, &$arrApiConfig) { |
---|
[21713] | 205 | if (SC_Utils_Ex::isBlank($operation_name)) { |
---|
| 206 | return false; |
---|
| 207 | } |
---|
| 208 | $arrAuthTypes = explode("|", $arrApiConfig['auth_types']); |
---|
| 209 | $result = false; |
---|
| 210 | foreach ($arrAuthTypes as $auth_type) { |
---|
| 211 | $ret = false; |
---|
| 212 | switch ($auth_type) { |
---|
| 213 | case self::API_AUTH_TYPE_REFERER: |
---|
| 214 | $ret = SC_Api_Operation_Ex::checkReferer(); |
---|
| 215 | break; |
---|
| 216 | case self::API_AUTH_TYPE_SESSION_TOKEN: |
---|
| 217 | $ret = SC_Helper_Session_Ex::isValidToken(false); |
---|
| 218 | break; |
---|
| 219 | case self::API_AUTH_TYPE_API_SIGNATURE: |
---|
| 220 | $ret = SC_Api_Operation_Ex::checkApiSignature($operation_name, $arrParam, $arrApiConfig); |
---|
| 221 | break; |
---|
| 222 | case self::API_AUTH_TYPE_CUSTOMER: |
---|
| 223 | $ret = SC_Api_Operation_Ex::checkCustomerAccount($arrParam['login_email'], $arrParam['login_password']); |
---|
| 224 | break; |
---|
| 225 | case self::API_AUTH_TYPE_MEMBER: |
---|
| 226 | $ret = SC_Api_Operation_Ex::checkMemberAccount($arrParam['member_id'], $arrParam['member_password']); |
---|
| 227 | break; |
---|
| 228 | case self::API_AUTH_TYPE_CUSTOMER_LOGIN_SESSION: |
---|
| 229 | $objCustomer = new SC_Customer_Ex(); |
---|
| 230 | $ret = $objCustomer->isLoginSuccess(); |
---|
| 231 | break; |
---|
| 232 | case self::API_AUTH_TYPE_MEMBER_LOGIN_SESSION: |
---|
| 233 | $ret = SC_Utils_Ex::sfIsSuccess(new SC_Session_Ex(), false); |
---|
| 234 | break; |
---|
| 235 | case self::API_AUTH_TYPE_IP: |
---|
| 236 | $ret = SC_Api_Operation_Ex::checkIp($operation_name); |
---|
| 237 | break; |
---|
| 238 | case self::API_AUTH_TYPE_HOST: |
---|
| 239 | $ret = SC_Api_Operation_Ex::checkHost($operation_name); |
---|
| 240 | break; |
---|
| 241 | case self::API_AUTH_TYPE_SSL: |
---|
| 242 | $ret = SC_Utils_Ex::sfIsHTTPS(); |
---|
| 243 | break; |
---|
| 244 | case self::API_AUTH_TYPE_OPEN: |
---|
| 245 | $result = true; |
---|
| 246 | break 2; // foreachも抜ける |
---|
| 247 | default: |
---|
| 248 | $ret = false; |
---|
| 249 | break; |
---|
| 250 | } |
---|
| 251 | if($ret === true) { |
---|
| 252 | $result = true; |
---|
| 253 | }else{ |
---|
| 254 | $result = false; |
---|
| 255 | break; // 1つでもfalseがあれば,その時点で終了 |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | return $result; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | * APIのリクエスト基本パラメーターの設定 |
---|
| 263 | * |
---|
| 264 | * @param object SC_FormParam |
---|
| 265 | * @return void |
---|
| 266 | */ |
---|
| 267 | protected function setApiBaseParam(&$objFormParam) { |
---|
| 268 | $objFormParam->addParam('Operation', 'Operation', STEXT_LEN, 'an', array('EXIST_CHECK', 'GRAPH_CHECK', 'MAX_LENGTH_CHECK')); |
---|
| 269 | $objFormParam->addParam('Service', 'Service', STEXT_LEN, 'an', array('EXIST_CHECK', 'GRAPH_CHECK', 'MAX_LENGTH_CHECK')); |
---|
| 270 | $objFormParam->addParam('Style', 'Style', STEXT_LEN, 'an', array('GRAPH_CHECK', 'MAX_LENGTH_CHECK')); |
---|
| 271 | $objFormParam->addParam('Validate', 'Validate', STEXT_LEN, 'an', array('GRAPH_CHECK', 'MAX_LENGTH_CHECK')); |
---|
| 272 | $objFormParam->addParam('Version', 'Version', STEXT_LEN, 'an', array('GRAPH_CHECK', 'MAX_LENGTH_CHECK')); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | * API実行 |
---|
| 277 | * |
---|
| 278 | * @param array $arrPost リクエストパラメーター |
---|
| 279 | * @return array( string レスポンス名, array レスポンス配列 ) |
---|
| 280 | */ |
---|
| 281 | public function doApiAction($arrPost) { |
---|
| 282 | // 実行時間計測用 |
---|
| 283 | $start_time = microtime(true); |
---|
| 284 | |
---|
| 285 | $objFormParam = new SC_FormParam_Ex(); |
---|
| 286 | SC_Api_Operation_Ex::setApiBaseParam($objFormParam); |
---|
| 287 | $objFormParam->setParam($arrPost); |
---|
| 288 | $objFormParam->convParam(); |
---|
| 289 | |
---|
| 290 | $arrErr = $objFormParam->checkError(); |
---|
| 291 | if (SC_Utils_Ex::isBlank($arrErr)) { |
---|
| 292 | $arrParam = $objFormParam->getHashArray(); |
---|
| 293 | $operation_name = $arrParam['Operation']; |
---|
| 294 | $service_name = $arrParam['Service']; |
---|
| 295 | $style_name = $arrParam['Style']; |
---|
| 296 | $validate_flag = $arrParam['Validate']; |
---|
| 297 | $api_version = $arrParam['Version']; |
---|
| 298 | |
---|
| 299 | SC_Api_Utils_Ex::printApiLog('access', $start_time, $operation_name); |
---|
| 300 | // API設定のロード |
---|
| 301 | $arrApiConfig = SC_Api_Utils_Ex::getApiConfig($operation_name); |
---|
| 302 | |
---|
[21722] | 303 | if (SC_Api_Operation_Ex::checkOperationAuth($operation_name, $arrPost, $arrApiConfig)) { |
---|
[21713] | 304 | SC_Api_Utils_Ex::printApiLog('Authority PASS', $start_time, $operation_name); |
---|
| 305 | |
---|
| 306 | // オペレーション権限OK |
---|
| 307 | // API オブジェクトをロード |
---|
| 308 | $objApiOperation = SC_Api_Utils_Ex::loadApiOperation($operation_name, $arrParam); |
---|
| 309 | |
---|
| 310 | if (is_object($objApiOperation) && method_exists($objApiOperation, 'doAction')) { |
---|
| 311 | // API オペレーション実行 |
---|
| 312 | $operation_result = $objApiOperation->doAction($arrPost); |
---|
| 313 | |
---|
| 314 | // オペレーション結果処理 |
---|
| 315 | if($operation_result) { |
---|
| 316 | $arrOperationRequestValid = $objApiOperation->getRequestValidate(); |
---|
| 317 | $arrResponseBody = $objApiOperation->getResponseArray(); |
---|
| 318 | $response_group_name = $objApiOperation->getResponseGroupName(); |
---|
| 319 | }else{ |
---|
| 320 | $arrErr = $objApiOperation->getErrorArray(); |
---|
| 321 | } |
---|
| 322 | }else{ |
---|
| 323 | $arrErr['ECCUBE.Operation.NoLoad'] = 'オペレーションをロード出来ませんでした。'; |
---|
| 324 | } |
---|
| 325 | }else{ |
---|
| 326 | $arrErr['ECCUBE.Authority.NoAuthority'] = 'オペレーションの実行権限がありません。'; |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | if (count($arrErr) == 0) { |
---|
| 331 | // 実行成功 |
---|
| 332 | $arrResponseValidSection = array( 'Request' => array( |
---|
| 333 | 'IsValid' => 'True', |
---|
| 334 | $operation_name . 'Request' => $arrOperationRequestValid |
---|
| 335 | ) |
---|
| 336 | ); |
---|
| 337 | $response_outer = $operation_name . 'Response'; |
---|
| 338 | SC_Api_Utils_Ex::printApiLog('Operation SUCCESS', $start_time, $response_outer); |
---|
| 339 | } else { |
---|
| 340 | // 実行失敗 |
---|
| 341 | $arrResponseErrorSection = array(); |
---|
| 342 | foreach ($arrErr as $error_code => $error_msg) { |
---|
| 343 | $arrResponseErrorSection[] = array('Code' => $error_code, 'Message' => $error_msg); |
---|
| 344 | } |
---|
| 345 | $arrResponseValidSection = array( 'Request' => array( |
---|
| 346 | 'IsValid' => 'False', |
---|
| 347 | 'Errors' => array('Error' => $arrResponseErrorSection) |
---|
| 348 | ) |
---|
| 349 | ); |
---|
| 350 | if (is_object($objApiOperation)) { |
---|
| 351 | $response_outer = $operation_name . 'Response'; |
---|
| 352 | }else{ |
---|
| 353 | $response_outer = 'ECCUBEApiCommonResponse'; |
---|
| 354 | } |
---|
| 355 | SC_Api_Utils_Ex::printApiLog('Operation FAILED', $start_time, $response_outer); |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | $arrResponse = array(); |
---|
| 359 | $arrResponse['OperationRequest'] = SC_Api_Operation_Ex::getOperationRequestEcho($arrPost, $start_time); |
---|
| 360 | $arrResponse[$response_group_name] = array(); // Items |
---|
| 361 | $arrResponse[$response_group_name] = $arrResponseValidSection; |
---|
| 362 | if (is_array($arrResponseBody)) { |
---|
| 363 | $arrResponse[$response_group_name] = array_merge((array)$arrResponse[$response_group_name], (array)$arrResponseBody); |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | return array($response_outer, $arrResponse); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | /** |
---|
| 370 | * APIのリクエストのエコー情報の作成 |
---|
| 371 | * |
---|
| 372 | * @param array $arrParam リクエストパラメーター |
---|
| 373 | * @param float $start_time 実行時間計測用開始時間 |
---|
| 374 | * @return array エコー情報配列 (XML用の _attributes 指定入り) |
---|
| 375 | */ |
---|
| 376 | protected function getOperationRequestEcho($arrParam, $start_time) { |
---|
| 377 | $arrRet = array( |
---|
| 378 | 'HTTPHeaders' => array('Header' => array('_attributes' => array('Name' => 'UserAgent', |
---|
| 379 | 'Value' => htmlspecialchars($_SERVER['HTTP_USER_AGENT'])))), |
---|
| 380 | 'RequestId' => $start_time, |
---|
| 381 | 'Arguments' => array(), |
---|
| 382 | ); |
---|
| 383 | foreach($arrParam as $key => $val) { |
---|
| 384 | $arrRet['Arguments'][] = array('_attributes' => array('Name' => htmlentities($key, ENT_NOQUOTES, 'UTF-8'), 'Value' => htmlentities($val, ENT_NOQUOTES, 'UTF-8'))); |
---|
| 385 | } |
---|
| 386 | $arrRet['RequestProcessingTime'] = microtime(true) - $start_time; |
---|
| 387 | return $arrRet; |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | // TODO: ここらへんは SC_Displayに持って行きたい所だが・・ |
---|
| 391 | public function sendApiResponse($type, $response_outer_name, &$arrResponse) { |
---|
| 392 | switch($type) { |
---|
| 393 | case 'xml': |
---|
| 394 | SC_Api_Utils_Ex::sendResponseXml($response_outer_name, $arrResponse); |
---|
| 395 | break; |
---|
| 396 | case 'php': |
---|
| 397 | SC_Api_Utils_Ex::sendResponsePhp($response_outer_name, $arrResponse); |
---|
| 398 | break; |
---|
| 399 | case 'json': |
---|
| 400 | default: |
---|
| 401 | SC_Api_Utils_Ex::sendResponseJson($response_outer_name, $arrResponse); |
---|
| 402 | break; |
---|
| 403 | } |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | } |
---|