source: branches/version-2_13-dev/data/class/plugin/SC_Plugin_Util.php @ 22978

Revision 22978, 10.3 KB checked in by adachi, 11 years ago (diff)

#2308 開発合宿(2013/06)プラグイン改善分をマージ

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
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// プラグインのユーティリティクラス.
25class SC_Plugin_Util
26{
27    /**
28     * 稼働中のプラグインを取得する。
29     */
30    function getEnablePlugin()
31    {
32        $objQuery =& SC_Query_Ex::getSingletonInstance();
33        $col = '*';
34        $table = 'dtb_plugin';
35        $where = 'enable = 1';
36        // XXX 2.11.0 互換のため
37        $arrCols = $objQuery->listTableFields($table);
38        if (in_array('priority', $arrCols)) {
39            $objQuery->setOrder('priority DESC, plugin_id ASC');
40        }
41        $arrRet = $objQuery->select($col,$table,$where);
42
43        // プラグインフックポイントを取得.
44        $max = count($arrRet);
45        for ($i = 0; $i < $max; $i++) {
46            $plugin_id = $arrRet[$i]['plugin_id'];
47            $arrHookPoint = SC_Plugin_Util::getPluginHookPoint($plugin_id);
48            $arrRet[$i]['plugin_hook_point'] = $arrHookPoint;
49        }
50
51        return $arrRet;
52    }
53
54    /**
55     * インストールされているプラグインを取得する。
56     *
57     * @return array $arrRet インストールされているプラグイン.
58     */
59    function getAllPlugin()
60    {
61        $objQuery =& SC_Query_Ex::getSingletonInstance();
62        $col = '*';
63        $table = 'dtb_plugin';
64        // XXX 2.11.0 互換のため
65        $arrCols = $objQuery->listTableFields($table);
66        if (in_array('priority', $arrCols)) {
67            $objQuery->setOrder('plugin_id ASC');
68        }
69        $arrRet = $objQuery->select($col,$table);
70
71        return $arrRet;
72    }
73
74    /**
75     * プラグインIDをキーにプラグインを取得する。
76     *
77     * @param int $plugin_id プラグインID.
78     * @return array プラグインの基本情報.
79     */
80    function getPluginByPluginId($plugin_id)
81    {
82        $objQuery =& SC_Query_Ex::getSingletonInstance();
83        $col = '*';
84        $table = 'dtb_plugin';
85        $where = 'plugin_id = ?';
86        $plugin = $objQuery->getRow($col, $table, $where, array($plugin_id));
87
88        return $plugin;
89    }
90
91    /**
92     * プラグインコードをキーにプラグインを取得する。
93     *
94     * @param string $plugin_code プラグインコード.
95     * @return array プラグインの基本情報.
96     */
97    function getPluginByPluginCode($plugin_code)
98    {
99        $objQuery =& SC_Query_Ex::getSingletonInstance();
100        $col = '*';
101        $table = 'dtb_plugin';
102        $where = 'plugin_code = ?';
103        $plugin = $objQuery->getRow($col, $table, $where, array($plugin_code));
104
105        return $plugin;
106    }
107
108    /**
109     * プラグインIDをキーにプラグインを削除する。
110     *
111     * @param string $plugin_id プラグインID.
112     * @return array プラグインの基本情報.
113     */
114    function deletePluginByPluginId($plugin_id)
115    {
116        $objQuery =& SC_Query_Ex::getSingletonInstance();
117        $where = 'plugin_id = ?';
118        $objQuery->delete('dtb_plugin', $where, array($plugin_id));
119        $objQuery->delete('dtb_plugin_hookpoint', $where, array($plugin_id));
120    }
121
122    /**
123     * プラグインディレクトリの取得
124     *
125     * @return array $arrPluginDirectory
126     */
127    function getPluginDirectory()
128    {
129        $arrPluginDirectory = array();
130        if (is_dir(PLUGIN_UPLOAD_REALDIR)) {
131            if ($dh = opendir(PLUGIN_UPLOAD_REALDIR)) {
132                while (($pluginDirectory = readdir($dh)) !== false) {
133                    $arrPluginDirectory[] = $pluginDirectory;
134                }
135                closedir($dh);
136            }
137        }
138
139        return $arrPluginDirectory;
140    }
141
142    /**
143     * プラグインIDをキーに, プラグインフックポイントを取得する.
144     *
145     * @param integer $plugin_id
146     * @param integer $use_type 1=有効のみ 2=無効のみ 3=全て
147     * @return array フックポイントの一覧
148     */
149    function getPluginHookPoint($plugin_id, $use_type = 1)
150    {
151        $objQuery =& SC_Query_Ex::getSingletonInstance();
152        $cols = '*';
153        $from = 'dtb_plugin_hookpoint';
154        $where = 'plugin_id = ?';
155        switch ($use_type) {
156            case 1:
157                $where .= ' AND use_flg = true';
158            break;
159
160            case 2:
161                $where .= ' AND use_flg = false';
162            break;
163
164            case 3:
165            default:
166            break;
167        }
168        return $objQuery->select($cols, $from, $where, array($plugin_id));
169    }
170
171    /**
172     *  プラグインフックポイントを取得する.
173     *
174     * @param integer $use_type 1=有効のみ 2=無効のみ 3=全て
175     * @return array フックポイントの一覧
176     */
177    function getPluginHookPointList($use_type = 3)
178    {
179        $objQuery =& SC_Query_Ex::getSingletonInstance();
180        $objQuery->setOrder('hook_point ASC, priority DESC');
181        $cols = 'dtb_plugin_hookpoint.*, dtb_plugin.priority, dtb_plugin.plugin_name';
182        $from = 'dtb_plugin_hookpoint LEFT JOIN dtb_plugin USING(plugin_id)';
183        switch ($use_type) {
184            case 1:
185                $where = 'enable = 1 AND use_flg = true';
186            break;
187
188            case 2:
189                $where = 'enable = 1 AND use_flg = false';
190            break;
191
192            case 3:
193            default:
194                $where = '';
195            break;
196        }
197        return $objQuery->select($cols, $from, $where);
198        //$arrList = array();
199        //foreach ($arrRet AS $key=>$val) {
200        //    $arrList[$val['hook_point']][$val['plugin_id']] = $val;
201        //}
202        //return $arrList;
203    }
204
205    /**
206     * プラグイン利用に必須のモジュールチェック
207     *
208     * @param string $key  エラー情報を格納するキー
209     * @return array $arrErr エラー情報を格納した連想配列.
210     */
211    function checkExtension($key)
212    {
213        // プラグイン利用に必須のモジュール
214        // 'EC-CUBEバージョン' => array('モジュール名')
215        $arrRequireExtension = array(
216                                     '2.12.0' => array('dom'),
217                                     '2.12.1' => array('dom'),
218                                     '2.12.2' => array('dom')
219                                    );
220        // 必須拡張モジュールのチェック
221        $arrErr = array();
222        if (is_array($arrRequireExtension[ECCUBE_VERSION])) {
223            foreach ($arrRequireExtension[ECCUBE_VERSION] AS $val) {
224                if (!extension_loaded($val)) {
225                    $arrErr[$key] .= "※ プラグインを利用するには、拡張モジュール「${val}」が必要です。<br />";
226                }
227            }
228        }
229
230        return $arrErr;
231    }
232
233    /**
234     * フックポイントのON/OFF変更
235     *
236     * @param intger $plugin_hookpoint_id  フックポイントID
237     * @return bolean $use_flg:ture=ON、false=OFF
238     */
239    function setPluginHookPointChangeUse($plugin_hookpoint_id, $use_flg = false) {
240        $objQuery =& SC_Query_Ex::getSingletonInstance();
241        $sqlval['use_flg'] = $use_flg;
242        $objQuery->update('dtb_plugin_hookpoint', $sqlval, 'plugin_hookpoint_id = ?', array($plugin_hookpoint_id));
243    }
244
245    /**
246     * フックポイントで衝突する可能性のあるプラグインを判定.メッセージを返します.
247     *
248     * @param int $plugin_id プラグインID
249     * @return string $conflict_alert_message メッセージ
250     */
251    function checkConflictPlugin($plugin_id = '')
252    {
253        // フックポイントを取得します.
254        $where = 'T1.hook_point = ? AND NOT T1.plugin_id = ? AND T2.enable = ?';
255        if ($plugin_id > 0) {
256            $hookPoints = SC_Plugin_Util::getPluginHookPoint($plugin_id, '');
257        } else {
258            $hookPoints = SC_Plugin_Util::getPluginHookPointList(1);
259            $where .= ' AND T1.use_flg = true';
260        }
261
262        $conflict_alert_message = '';
263        $arrConflictPluginName = array();
264        $arrConflictHookPoint = array();
265        $objQuery =& SC_Query_Ex::getSingletonInstance();
266        $objQuery->setGroupBy('T1.hook_point, T1.plugin_id, T2.plugin_name');
267        $table = 'dtb_plugin_hookpoint AS T1 LEFT JOIN dtb_plugin AS T2 ON T1.plugin_id = T2.plugin_id';
268        foreach ($hookPoints as $hookPoint) {
269            // 競合するプラグインを取得する,
270            $conflictPlugins = $objQuery->select('T1.hook_point, T1.plugin_id, T2.plugin_name', $table, $where, array($hookPoint['hook_point'], $hookPoint['plugin_id'], PLUGIN_ENABLE_TRUE));
271
272            // プラグイン名重複を削除する為、専用の配列に格納し直す.
273            foreach ($conflictPlugins as $conflictPlugin) {
274                // プラグイン名が見つからなければ配列に格納
275                if (!in_array($conflictPlugin['plugin_name'], $arrConflictPluginName)) {
276                    $arrConflictPluginName[] = $conflictPlugin['plugin_name'];
277                }
278                // プラグイン名が見つからなければ配列に格納
279                if (!in_array($conflictPlugin['hook_point'], $arrConflictHookPoint)) {
280                    $arrConflictHookPoint[] = $conflictPlugin['hook_point'];
281                }
282            }
283        }
284
285        if ($plugin_id > 0) {
286            // メッセージをセットします.
287            foreach ($arrConflictPluginName as $conflictPluginName) {
288                $conflict_alert_message .= '* ' .  $conflictPluginName . 'と競合する可能性があります。<br/>';
289            }
290            return $conflict_alert_message;
291        } else {
292            return $arrConflictHookPoint;
293        }
294    }
295
296}
Note: See TracBrowser for help on using the repository browser.