source: branches/version-2_13-dev/data/class/api/SC_Api_Utils.php @ 23605

Revision 23605, 8.6 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

Scrutinizer Auto-Fixes

This patch was automatically generated as part of the following inspection:
 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/d8722894-69a6-4b1b-898d-43618035c60d

Enabled analysis tools:

  • PHP Analyzer
  • PHP PDepend
  • PHP Similarity Analyzer
  • PHP Change Tracking Analyzer
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2014 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    /** 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     * @return string 秘密鍵文字列
53     */
54    public function getOperationSubConfig($operation_name, $key_name = '', $arrApiConfig = '')
55    {
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
72        return false;
73    }
74
75    /**
76     * オペレーション名に対応した認証の設定情報を取得する
77     * Configが無い場合は、APIデフォルトを取得する
78     *
79     * @param  string $operation_name
80     * @return array  設定配列
81     */
82    public function getApiConfig($operation_name)
83    {
84        // 設定優先度 DB > plugin default > base
85        $objQuery =& SC_Query_Ex::getSingletonInstance();
86        $where = 'operation_name Like ? AND del_flg = 0 AND enable = 1';
87        $arrApiConfig = $objQuery->getRow('*', 'dtb_api_config', $where, array($operation_name));
88        if (SC_Utils_Ex::isBlank($arrApiConfig)) {
89            $objApi = SC_Api_Utils_Ex::loadApiOperation($operation_name);
90            if (is_object($objApi)) {
91                $arrApiConfig = $objApi->getDefaultConfig();
92            }
93            if (!SC_Utils_Ex::isBlank($arrApiConfig)) {
94                // デフォルト設定がロード出来た場合は自動で設定に反映
95                $arrData = $arrApiConfig;
96                $arrData['update_date'] = 'CURRENT_TIMESTAMP';
97                $arrData['api_config_id'] = $objQuery->nextVal('dtb_api_config_api_config_id');
98                $objQuery->insert('dtb_api_config', $arrData);
99            } else {
100                // ロード出来ない場合はAPI_Defaultを適用
101                $operation_name = 'Default';
102                $objApi = SC_Api_Utils_Ex::loadApiOperation($operation_name);
103                $arrApiConfig = $objApi->getDefaultConfig();
104            }
105        }
106
107        return $arrApiConfig;
108    }
109
110    /**
111     * APIログ
112     *
113     * @param string $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
189        return $arrFiles;
190    }
191
192    public function sendResponseJson($response_outer_name, &$arrResponse)
193    {
194        if (isset($arrResponse["callback"])) {
195            $callback = $arrResponse["callback"];
196            unset($arrResponse["callback"]);
197            header('Content-Type: application/javascript; charset=UTF-8');
198            $arrResponse['response_name'] = $response_outer_name;
199            echo $callback . "(" . SC_Utils_Ex::jsonEncode($arrResponse) . ")";
200        } else {
201            header('Content-Type: application/json; charset=UTF-8');
202            $arrResponse['response_name'] = $response_outer_name;
203            echo SC_Utils_Ex::jsonEncode($arrResponse);
204        }
205    }
206
207    public function sendResponsePhp($response_outer_name, &$arrResponse)
208    {
209        header('Content-Type: application/php; charset=UTF-8');
210        $arrResponse['response_name'] = $response_outer_name;
211        echo serialize($arrResponse);
212    }
213
214    public function sendResponseXml($response_outer_name, &$arrResponse)
215    {
216        require_once 'XML/Serializer.php';
217
218        $options = array(
219            'mode' => 'simplexml',
220            'indent' => "\t",
221            'linebreak' => "\n",
222            'typeHints' => false,
223            'addDecl' => true,
224            'encoding' => 'UTF-8',
225            'rootName' => $response_outer_name,
226            'rootAttributes' => array('xmlns' => self::API_XMLNS . ECCUBE_VERSION,
227                                        'xml:lang' => self::API_XML_LANG),
228            'defaultTagName' => 'Response',
229            'attributesArray' => '_attributes'
230        );
231
232        $objSerializer = new XML_Serializer($options);
233        $ret = $objSerializer->serialize($arrResponse);
234        $xml = $objSerializer->getSerializedData();
235        header('Content-Type: text/xml; charset=UTF-8');
236        echo $xml;
237    }
238}
Note: See TracBrowser for help on using the repository browser.