source: branches/feature-module-update/html/admin/system/update.php @ 16582

Revision 16582, 8.8 KB checked in by nanasess, 19 years ago (diff)

ライセンス表記変更

  • 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-2007 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 */
23require_once("../require.php");
24
25//ページ管理クラス
26class LC_Page {
27    //コンストラクタ
28    function LC_Page() {
29        //メインテンプレートの指定
30        $this->tpl_mainpage = 'system/update.tpl';
31        $this->tpl_subnavi = 'system/subnavi.tpl';
32        $this->tpl_mainno = 'system';       
33        $this->tpl_subno = 'update';
34        $this->tpl_subtitle = 'アップデート管理';
35    }
36}
37
38$objPage = new LC_Page();
39$objView = new SC_AdminView();
40$objQuery = new SC_Query();
41
42// セッションクラス
43$objSess = new SC_Session();
44// 認証可否の判定
45sfIsSuccess($objSess);
46
47switch($_POST['mode']) {
48// アップデート情報ファイルを取得
49case 'edit':
50    // 更新情報を最新にする
51    lfLoadUpdateList();
52    break;
53// インストール
54case 'install':
55    // 更新情報を最新にする
56    lfLoadUpdateList();
57    // モジュール郡のインストール
58    lfInstallModule();
59    break;
60// アンインストール
61case 'uninstall':
62    // 更新情報を最新にする
63    lfLoadUpdateList();
64    // モジュール郡のインストール   
65    lfUninstallModule();
66    break;
67default:
68    break;
69}
70
71$col = "module_id, module_name, now_version, latest_version, module_explain, create_date, release_date";
72$objQuery->setorder("module_id");
73$objPage->arrUpdate = $objQuery->select($col, "dtb_update");
74
75$objView->assignobj($objPage);      //変数をテンプレートにアサインする
76$objView->display(MAIN_FRAME);      //テンプレートの出力
77//-------------------------------------------------------------------------------------------------------
78// 更新ファイルの取得
79function lfCopyUpdateFile($file) {
80    global $objPage;
81   
82    $src_path = sfRmDupSlash(UPDATE_HTTP . $file . ".txt");
83    $dst_path = sfRmDupSlash(UPDATE_PATH . $file);
84    $flg_ok = true; // 処理の成功判定
85   
86    $src_fp = @fopen($src_path, "rb");
87   
88    if(!$src_fp) {
89        sfErrorHeader(">> " . $src_path . "の取得に失敗しました。");
90        $flg_ok = false;
91    } else {
92        // ファイルをすべて読み込む
93        $contents = '';
94        while (!feof($src_fp)) {
95            $contents .= fread($src_fp, 1024);
96        }
97        fclose($src_fp);
98       
99        // ディレクトリ作成を試みる
100        lfMakeDirectory($dst_path);
101        // ファイル書込み     
102        $dst_fp = @fopen($dst_path, "wb");
103        if(!$dst_fp) {
104            sfErrorHeader(">> " . $dst_path . "をオープンできません。");
105            $flg_ok = false;
106        } else {
107            fwrite($dst_fp, $contents);
108            fclose($dst_fp);
109        }
110    }
111   
112    if($flg_ok) {
113        $objPage->update_mess.= ">> " . $dst_path . ":コピー成功<br>";
114    } else {
115        $objPage->update_mess.= ">> " . $dst_path . ":コピー失敗<br>";       
116    }
117   
118    return $flg_ok;
119}
120
121// すべてのパスのディレクトリを作成する
122function lfMakeDirectory($path) {
123    $pos = 0;
124    $cnt = 0;               // 無限ループ対策
125    $len = strlen($path);   // 無限ループ対策
126   
127    while($cnt <= $len) {
128        $pos = strpos($path, "/", $pos);
129        // ここでの判定は、等号3つを使用
130        if($pos === false) {
131            // スラッシュが見つからない場合はループから抜ける
132            break;
133        }
134        $pos++; // 文字発見位置を一文字進める
135        $dir = substr($path, 0, $pos);
136       
137        // すでに存在するかどうか調べる
138        if(!file_exists($dir)) {
139            mkdir($dir);
140        }
141        $cnt++; // 無限ループ対策
142    }
143}
144
145// 更新情報を最新にする
146function lfLoadUpdateList() {
147    $objQuery = new SC_Query();
148    $path = UPDATE_HTTP . "update.txt";
149    $fp = @fopen($path, "rb");
150   
151    if(!$fp) {
152        sfErrorHeader(">> " . $path . "の取得に失敗しました。");
153    } else {
154        while (!feof($fp)) {
155            $arrCSV = fgetcsv($fp, UPDATE_CSV_LINE_MAX);
156           
157            if(ereg("^#", $arrCSV[0])) {
158                continue;
159            }
160                       
161            // カラム数が正常であった場合のみ
162            if(count($arrCSV) == UPDATE_CSV_COL_MAX) {
163                // 取得したアップデート情報をDBに書き込む
164                $sqlval['module_id'] = $arrCSV[0];
165                $sqlval['module_name'] = $arrCSV[1];
166                $sqlval['latest_version'] = $arrCSV[3];
167                $sqlval['module_explain'] = $arrCSV[4];
168                $sqlval['main_php'] = $arrCSV[5];
169                $sqlval['extern_php'] = $arrCSV[6];
170                $sqlval['install_sql'] = $arrCSV[7];
171                $sqlval['uninstall_sql'] = $arrCSV[8];             
172                $sqlval['other_files'] = $arrCSV[9];
173                $sqlval['del_flg'] = $arrCSV[10];
174                $sqlval['update_date'] = "now()";
175                $sqlval['release_date'] = $arrCSV[12];
176                // 既存レコードのチェック
177                $cnt = $objQuery->count("dtb_update", "module_id = ?", array($sqlval['module_id']));
178                if($cnt > 0) {
179                    // すでに取得されている場合は更新する。   
180                    $objQuery->update("dtb_update", $sqlval, "module_id = ?", array($sqlval['module_id']));
181                } else {
182                    // 新規レコードの追加
183                    $sqlval['create_date'] = "now()";
184                    $objQuery->insert("dtb_update", $sqlval);
185                }
186            }
187        }
188        fclose($fp);
189    }
190}
191
192// インストール処理
193function lfInstallModule() {
194    global $objPage;
195   
196    $objQuery = new SC_Query();
197    $arrRet = $objQuery->select("module_id, extern_php, other_files, install_sql, latest_version", "dtb_update", "module_id = ?", array($_POST['module_id']));
198    $flg_ok = true; // 処理の成功判定
199   
200    if(count($arrRet) > 0) {
201        $arrFiles = array();
202        if($arrRet[0]['other_files'] != "") {
203            $arrFiles = split("\|", $arrRet[0]['other_files']);
204        }
205        $arrFiles[] = $arrRet[0]['extern_php'];
206        foreach($arrFiles as $val) {
207            // 更新ファイルの取得
208            $ret=lfCopyUpdateFile($val);
209            if(!$ret) {
210                $flg_ok = false;
211            }
212        }
213    } else {
214        sfErrorHeader(">> 対象の機能は、配布を終了しております。");
215        $flg_ok = false;
216    }
217   
218    // 必要なSQL文の実行
219    if($arrRet[0]['install_sql'] != "") {
220        // SQL文実行、パラーメータなし、エラー無視
221        $arrInstallSql = split(";",$arrRet[0]['install_sql']);
222        foreach($arrInstallSql as $key => $val){
223            if (trim($val) != ""){
224                $ret = $objQuery->query(trim($val),"",true);
225            }
226        }
227        if(DB::isError($ret)) {
228            // エラー文を取得する
229            ereg("\[(.*)\]", $ret->userinfo, $arrKey);
230            $objPage->update_mess.=">> テーブル構成の変更に失敗しました。<br>";
231            $objPage->update_mess.= $arrKey[0] . "<br>";
232            $flg_ok = false;
233        } else {
234            $objPage->update_mess.=">> テーブル構成の変更を行いました。<br>";
235        }
236    }
237   
238    if($flg_ok) {
239        $sqlval['now_version'] = $arrRet[0]['latest_version'];
240        $sqlval['update_date'] = "now()";
241        $objQuery->update("dtb_update", $sqlval, "module_id = ?", array($arrRet[0]['module_id']));
242    }
243}
244
245// アンインストール処理
246function lfUninstallModule() {
247    global $objPage;
248   
249    $objQuery = new SC_Query();
250    $arrRet = $objQuery->select("module_id, extern_php, other_files, install_sql, uninstall_sql, latest_version", "dtb_update", "module_id = ?", array($_POST['module_id']));
251    $flg_ok = true; // 処理の成功判定
252   
253    if(count($arrRet) > 0) {
254        $arrFiles = array();
255        if($arrRet[0]['other_files'] != "") {
256            $arrFiles = split("\|", $arrRet[0]['other_files']);
257        }
258        $arrFiles[] = $arrRet[0]['extern_php'];
259        foreach($arrFiles as $val) {
260            $path = DATA_PATH . $val;
261            if(file_exists($path)) {
262                // ファイルを削除する
263                if(unlink($path)) {
264                    $objPage->update_mess.= ">> " . $path . ":削除成功<br>";
265                } else {
266                    $objPage->update_mess.= ">> " . $path . ":削除失敗<br>";
267                }
268            }
269        }
270       
271        // 必要なSQL文の実行
272        if($arrRet[0]['uninstall_sql'] != "") {
273            // SQL文実行、パラーメータなし、エラー無視
274            $ret = $objQuery->query($arrRet[0]['uninstall_sql'],"",true);
275            if(DB::isError($ret)) {
276                // エラー文を取得する
277                ereg("\[(.*)\]", $ret->userinfo, $arrKey);
278                $objPage->update_mess.=">> テーブル構成の変更に失敗しました。<br>";
279                $objPage->update_mess.= $arrKey[0] . "<br>";
280                $flg_ok = false;
281            } else {
282                $objPage->update_mess.=">> テーブル構成の変更を行いました。<br>";
283            }
284        }       
285    } else {
286        sfErrorHeader(">> 対象の機能は、配布を終了しております。");
287    }
288   
289    if($flg_ok) {
290        // バージョン情報を削除する。
291        $sqlval['now_version'] = "";
292        $sqlval['update_date'] = "now()";
293        $objQuery->update("dtb_update", $sqlval, "module_id = ?", array($arrRet[0]['module_id']));
294    }
295}
296
297
298?>
Note: See TracBrowser for help on using the repository browser.