source: temp/trunk/html/admin/system/bkup.php @ 5493

Revision 5493, 9.3 KB checked in by kakinaka, 20 years ago (diff)

blank

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2require_once("../require.php");
3
4//¥Ú¡¼¥¸´ÉÍý¥¯¥é¥¹
5class LC_Page {
6    //¥³¥ó¥¹¥È¥é¥¯¥¿
7    function LC_Page() {
8        //¥á¥¤¥ó¥Æ¥ó¥×¥ì¡¼¥È¤Î»ØÄê
9        $this->tpl_mainpage = 'system/bkup.tpl';
10        $this->tpl_subnavi = 'system/subnavi.tpl';
11        $this->tpl_mainno = 'system';       
12        $this->tpl_subno = 'bkup';
13        $this->tpl_subtitle = '¥Ð¥Ã¥¯¥¢¥Ã¥×´ÉÍý';
14    }
15}
16
17$objPage = new LC_Page();
18$objView = new SC_AdminView();
19$objQuery = new SC_Query();
20
21// ¥»¥Ã¥·¥ç¥ó¥¯¥é¥¹
22$objSess = new SC_Session();
23// ǧ¾Ú²ÄÈݤÎȽÄê
24sfIsSuccess($objSess);
25
26switch($_POST['mode']) {
27// ¥Ð¥Ã¥¯¥¢¥Ã¥×¤òºîÀ®¤¹¤ë
28case 'bkup':
29    // ¥¨¥é¡¼¥Á¥§¥Ã¥¯
30    $arrErr = lfCheckError($_POST);
31
32    // ¥¨¥é¡¼¤¬¤Ê¤±¤ì¤Ð¥Ð¥Ã¥¯¥¢¥Ã¥×½èÍý¤ò¹Ô¤¦   
33    if (count($arrErr) <= 0) {
34        // ¥Ð¥Ã¥¯¥¢¥Ã¥×¥Õ¥¡¥¤¥ëºîÀ®
35        lfCreateBkupData();
36       
37        // DB¤Ë¥Ç¡¼¥¿¹¹¿·
38       
39    }
40   
41
42    break;
43   
44// ¥¤¥ó¥¹¥È¡¼¥ë
45case 'install':
46    // ¹¹¿·¾ðÊó¤òºÇ¿·¤Ë¤¹¤ë
47    lfLoadUpdateList();
48    // ¥â¥¸¥å¡¼¥ë·´¤Î¥¤¥ó¥¹¥È¡¼¥ë
49    lfInstallModule();
50    break;
51// ¥¢¥ó¥¤¥ó¥¹¥È¡¼¥ë
52case 'uninstall':
53    // ¹¹¿·¾ðÊó¤òºÇ¿·¤Ë¤¹¤ë
54    lfLoadUpdateList();
55    // ¥â¥¸¥å¡¼¥ë·´¤Î¥¤¥ó¥¹¥È¡¼¥ë   
56    lfUninstallModule();
57    break;
58default:
59    break;
60}
61
62// ¥Æ¥ó¥×¥ì¡¼¥È¥Õ¥¡¥¤¥ë¤ËÅϤ¹¥Ç¡¼¥¿¤ò¥»¥Ã¥È
63$objPage->arrErr = $arrErr;
64
65$objView->assignobj($objPage);      //ÊÑ¿ô¤ò¥Æ¥ó¥×¥ì¡¼¥È¤Ë¥¢¥µ¥¤¥ó¤¹¤ë
66$objView->display(MAIN_FRAME);      //¥Æ¥ó¥×¥ì¡¼¥È¤Î½ÐÎÏ
67
68//-------------------------------------------------------------------------------------------------------
69
70// ¥¨¥é¡¼¥Á¥§¥Ã¥¯
71function lfCheckError($array){
72    $objErr = new SC_CheckError($array);
73   
74    $objErr->doFunc(array("¥Ð¥Ã¥¯¥¢¥Ã¥×̾", "bkup_name", STEXT_LEN), array("EXIST_CHECK","MAX_LENGTH_CHECK"));
75   
76    return $objErr->arrErr;
77}
78
79// ¥Ð¥Ã¥¯¥¢¥Ã¥×¥Õ¥¡¥¤¥ëºîÀ®
80function lfCreateBkupData(){
81    $objQuery = new SC_Query();
82    $csv_data = "";
83   
84    // Á´¥Æ¡¼¥Ö¥ë¼èÆÀ
85    $arrTableList = lfGetTableList();
86   
87    // ³Æ¥Æ¡¼¥Ö¥ë¾ðÊó¤ò¼èÆÀ¤¹¤ë
88    foreach($arrTableList as $key => $val){
89       
90        // ¥Æ¡¼¥Ö¥ë¹½À®¤ò¼èÆÀ
91        $arrColumnList = lfGetColumnList($val);
92       
93        // ¥Æ¡¼¥Ö¥ë¹½À®¤ÎCSV½ÐÎϥǡ¼¥¿À¸À®
94       
95       
96        // Á´¥Ç¡¼¥¿¤ò¼èÆÀ
97        $arrData = $objQuery->getAll("SELECT * FROM $val");
98
99        // CSV¥Ç¡¼¥¿À¸À®
100        if (count($arrData) > 0) {
101           
102            // ¥«¥é¥à¤ò½ÐÎÏ
103            $arrKyes = sfGetCommaList(array_keys($arrData[0]), false);
104           
105            $arrDataTmp = sfGetCSVList($arrData[0], false);
106            sfprintr($arrDataTmp);
107           
108           
109            $csv_data .= $val . "\n";
110            $csv_data .= $arrKyes . "\n";
111            $csv_data .= "\n";
112        }   
113       
114        //sfprintr(array_keys($arrData[0]));
115        //sfprintr(sfGetCSVList($arrData));
116       
117    }
118   
119    sfprintr($csv_data);
120   
121   
122   
123   
124}
125
126// Á´¥Æ¡¼¥Ö¥ë¥ê¥¹¥È¤ò¼èÆÀ¤¹¤ë
127function lfGetTableList(){
128    $objQuery = new SC_Query();
129   
130    if(DB_TYPE == "pgsql"){
131        $sql = "SELECT tablename FROM pg_tables where tableowner = ? ORDER BY tablename ; ";
132        $arrRet = $objQuery->getAll($sql, array(DB_USER));
133        $arrRet = sfSwapArray($arrRet);
134        $arrRet = $arrRet['tablename'];
135    }else if(DB_TYPE == "mysql"){
136       
137    }
138   
139    return $arrRet;
140}
141
142// ¥Æ¡¼¥Ö¥ë¹½À®¤ò¼èÆÀ¤¹¤ë
143function lfGetColumnList($table_name){
144    $objQuery = new SC_Query();
145
146    if(DB_TYPE == "pgsql"){
147        $sql = "SELECT
148                    a.attname, t.typname, a.attnotnull, d.adsrc as defval, a.atttypmod, a.attnum as fldnum, e.description
149                FROM
150                    pg_class c,
151                    pg_type t,
152                    pg_attribute a left join pg_attrdef d on (a.attrelid=d.adrelid and a.attnum=d.adnum)
153                                   left join pg_description e on (a.attrelid=e.objoid and a.attnum=e.objsubid)
154                WHERE (c.relname=?) AND (c.oid=a.attrelid) AND (a.atttypid=t.oid) AND a.attnum > 0
155                ORDER BY fldnum";
156        $arrRet = $objQuery->getAll($sql, array($table_name));
157    }
158   
159    return sfswaparray($arrRet);
160
161}
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176// ¹¹¿·¥Õ¥¡¥¤¥ë¤Î¼èÆÀ
177function lfCopyUpdateFile($val) {
178    global $objPage;
179   
180    $src_path = sfRmDupSlash(UPDATE_HTTP . $val . ".txt");
181    $dst_path = sfRmDupSlash(ROOT_DIR . $val);
182    $flg_ok = true; // ½èÍý¤ÎÀ®¸ùȽÄê
183   
184    $src_fp = @fopen($src_path, "rb");
185   
186    if(!$src_fp) {
187        sfErrorHeader(">> " . $src_path . "¤Î¼èÆÀ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£");
188        $flg_ok = false;
189    } else {
190        // ¥Õ¥¡¥¤¥ë¤ò¤¹¤Ù¤ÆÆÉ¤ß¹þ¤à
191        $contents = '';
192        while (!feof($src_fp)) {
193            $contents .= fread($src_fp, 1024);
194        }
195        fclose($src_fp);
196       
197        // ¥Ç¥£¥ì¥¯¥È¥êºîÀ®¤ò»î¤ß¤ë
198        lfMakeDirectory($dst_path);
199        // ¥Õ¥¡¥¤¥ë½ñ¹þ¤ß       
200        $dst_fp = @fopen($dst_path, "wb");
201        if(!$dst_fp) {
202            sfErrorHeader(">> " . $dst_path . "¤ò¥ª¡¼¥×¥ó¤Ç¤­¤Þ¤»¤ó¡£");
203            $flg_ok = false;
204        } else {
205            fwrite($dst_fp, $contents);
206            fclose($dst_fp);
207        }
208    }
209   
210    if($flg_ok) {
211        $objPage->update_mess.= ">> " . $dst_path . "¡§¥³¥Ô¡¼À®¸ù<br>";
212    } else {
213        $objPage->update_mess.= ">> " . $dst_path . "¡§¥³¥Ô¡¼¼ºÇÔ<br>";     
214    }
215   
216    return $flg_ok;
217}
218
219// ¤¹¤Ù¤Æ¤Î¥Ñ¥¹¤Î¥Ç¥£¥ì¥¯¥È¥ê¤òºîÀ®¤¹¤ë
220function lfMakeDirectory($path) {
221    $pos = 0;
222    $cnt = 0;               // ̵¸Â¥ë¡¼¥×Âкö
223    $len = strlen($path);   // ̵¸Â¥ë¡¼¥×Âкö
224   
225    while($cnt <= $len) {
226        $pos = strpos($path, "/", $pos);
227        // ¤³¤³¤Ç¤ÎȽÄê¤Ï¡¢Åù¹æ3¤Ä¤ò»ÈÍÑ
228        if($pos === false) {
229            // ¥¹¥é¥Ã¥·¥å¤¬¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ï¥ë¡¼¥×¤«¤éÈ´¤±¤ë
230            break;
231        }
232        $pos++; // ʸ»úȯ¸«°ÌÃÖ¤ò°ìʸ»ú¿Ê¤á¤ë
233        $dir = substr($path, 0, $pos);
234       
235        // ¤¹¤Ç¤Ë¸ºß¤¹¤ë¤«¤É¤¦¤«Ä´¤Ù¤ë
236        if(!file_exists($dir)) {
237            mkdir($dir);
238        }
239        $cnt++; // ̵¸Â¥ë¡¼¥×Âкö
240    }
241}
242
243// ¹¹¿·¾ðÊó¤òºÇ¿·¤Ë¤¹¤ë
244function lfLoadUpdateList() {
245    $objQuery = new SC_Query();
246    $path = UPDATE_HTTP . "update.txt";
247    $fp = @fopen($path, "rb");
248   
249    if(!$fp) {
250        sfErrorHeader(">> " . $path . "¤Î¼èÆÀ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£");
251    } else {
252        while (!feof($fp)) {
253            $arrCSV = fgetcsv($fp, UPDATE_CSV_LINE_MAX);
254            // ¥«¥é¥à¿ô¤¬Àµ¾ï¤Ç¤¢¤Ã¤¿¾ì¹ç¤Î¤ß
255            if(count($arrCSV) == UPDATE_CSV_COL_MAX) {
256                // ¼èÆÀ¤·¤¿¥¢¥Ã¥×¥Ç¡¼¥È¾ðÊó¤òDB¤Ë½ñ¤­¹þ¤à
257                $sqlval['module_id'] = $arrCSV[0];
258                $sqlval['module_name'] = $arrCSV[1];
259                $sqlval['latest_version'] = $arrCSV[3];
260                $sqlval['module_explain'] = $arrCSV[4];
261                $sqlval['main_php'] = $arrCSV[5];
262                $sqlval['extern_php'] = $arrCSV[6];
263                $sqlval['install_sql'] = $arrCSV[7];
264                $sqlval['uninstall_sql'] = $arrCSV[8];             
265                $sqlval['other_files'] = $arrCSV[9];
266                $sqlval['del_flg'] = $arrCSV[10];
267                $sqlval['update_date'] = "now()";
268                $sqlval['release_date'] = $arrCSV[12];
269                // ´û¸¥ì¥³¡¼¥É¤Î¥Á¥§¥Ã¥¯
270                $cnt = $objQuery->count("dtb_update", "module_id = ?", array($sqlval['module_id']));
271                if($cnt > 0) {
272                    // ¤¹¤Ç¤Ë¼èÆÀ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¹¹¿·¤¹¤ë¡£
273                    $objQuery->update("dtb_update", $sqlval, "module_id = ?", array($sqlval['module_id']));
274                } else {
275                    // ¿·µ¬¥ì¥³¡¼¥É¤ÎÄɲÃ
276                    $sqlval['create_date'] = "now()";
277                    $objQuery->insert("dtb_update", $sqlval);
278                }
279            }
280        }
281        fclose($fp);
282    }
283}
284
285// ¥¤¥ó¥¹¥È¡¼¥ë½èÍý
286function lfInstallModule() {
287    global $objPage;
288   
289    $objQuery = new SC_Query();
290    $arrRet = $objQuery->select("module_id, extern_php, other_files, install_sql, latest_version", "dtb_update", "module_id = ?", array($_POST['module_id']));
291    $flg_ok = true; // ½èÍý¤ÎÀ®¸ùȽÄê
292   
293    if(count($arrRet) > 0) {
294        $arrFiles = array();
295        if($arrRet[0]['other_files'] != "") {
296            $arrFiles = split("\|", $arrRet[0]['other_files']);
297        }
298        $arrFiles[] = $arrRet[0]['extern_php'];
299        foreach($arrFiles as $val) {
300            // ¹¹¿·¥Õ¥¡¥¤¥ë¤Î¼èÆÀ
301            $ret=lfCopyUpdateFile($val);
302            if(!$ret) {
303                $flg_ok = false;
304            }
305        }
306    } else {
307        sfErrorHeader(">> Âоݤε¡Ç½¤Ï¡¢ÇÛÉÛ¤ò½ªÎ»¤·¤Æ¤ª¤ê¤Þ¤¹¡£");
308        $flg_ok = false;
309    }
310   
311    // ɬÍפÊSQLʸ¤Î¼Â¹Ô
312    if($arrRet[0]['install_sql'] != "") {
313        // SQLʸ¼Â¹Ô¡¢¥Ñ¥é¡¼¥á¡¼¥¿¤Ê¤·¡¢¥¨¥é¡¼Ìµ»ë
314        $arrInstallSql = split(";",$arrRet[0]['install_sql']);
315        foreach($arrInstallSql as $key => $val){
316            if (trim($val) != ""){
317                $ret = $objQuery->query(trim($val),"",true);
318            }
319        }
320        if(DB::isError($ret)) {
321            // ¥¨¥é¡¼Ê¸¤ò¼èÆÀ¤¹¤ë
322            ereg("\[(.*)\]", $ret->userinfo, $arrKey);
323            $objPage->update_mess.=">> ¥Æ¡¼¥Ö¥ë¹½À®¤ÎÊѹ¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£<br>";
324            $objPage->update_mess.= $arrKey[0] . "<br>";
325            $flg_ok = false;
326        } else {
327            $objPage->update_mess.=">> ¥Æ¡¼¥Ö¥ë¹½À®¤ÎÊѹ¹¤ò¹Ô¤¤¤Þ¤·¤¿¡£<br>";
328        }
329    }
330   
331    if($flg_ok) {
332        $sqlval['now_version'] = $arrRet[0]['latest_version'];
333        $sqlval['update_date'] = "now()";
334        $objQuery->update("dtb_update", $sqlval, "module_id = ?", array($arrRet[0]['module_id']));
335    }
336}
337
338// ¥¢¥ó¥¤¥ó¥¹¥È¡¼¥ë½èÍý
339function lfUninstallModule() {
340    global $objPage;
341   
342    $objQuery = new SC_Query();
343    $arrRet = $objQuery->select("module_id, extern_php, other_files, install_sql, uninstall_sql, latest_version", "dtb_update", "module_id = ?", array($_POST['module_id']));
344    $flg_ok = true; // ½èÍý¤ÎÀ®¸ùȽÄê
345   
346    if(count($arrRet) > 0) {
347        $arrFiles = array();
348        if($arrRet[0]['other_files'] != "") {
349            $arrFiles = split("\|", $arrRet[0]['other_files']);
350        }
351        $arrFiles[] = $arrRet[0]['extern_php'];
352        foreach($arrFiles as $val) {
353            $path = ROOT_DIR . $val;
354            if(file_exists($path)) {
355                // ¥Õ¥¡¥¤¥ë¤òºï½ü¤¹¤ë
356                if(unlink($path)) {
357                    $objPage->update_mess.= ">> " . $path . "¡§ºï½üÀ®¸ù<br>";
358                } else {
359                    $objPage->update_mess.= ">> " . $path . "¡§ºï½ü¼ºÇÔ<br>";
360                }
361            }
362        }
363       
364        // ɬÍפÊSQLʸ¤Î¼Â¹Ô
365        if($arrRet[0]['uninstall_sql'] != "") {
366            // SQLʸ¼Â¹Ô¡¢¥Ñ¥é¡¼¥á¡¼¥¿¤Ê¤·¡¢¥¨¥é¡¼Ìµ»ë
367            $ret = $objQuery->query($arrRet[0]['uninstall_sql'],"",true);
368            if(DB::isError($ret)) {
369                // ¥¨¥é¡¼Ê¸¤ò¼èÆÀ¤¹¤ë
370                ereg("\[(.*)\]", $ret->userinfo, $arrKey);
371                $objPage->update_mess.=">> ¥Æ¡¼¥Ö¥ë¹½À®¤ÎÊѹ¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£<br>";
372                $objPage->update_mess.= $arrKey[0] . "<br>";
373                $flg_ok = false;
374            } else {
375                $objPage->update_mess.=">> ¥Æ¡¼¥Ö¥ë¹½À®¤ÎÊѹ¹¤ò¹Ô¤¤¤Þ¤·¤¿¡£<br>";
376            }
377        }       
378    } else {
379        sfErrorHeader(">> Âоݤε¡Ç½¤Ï¡¢ÇÛÉÛ¤ò½ªÎ»¤·¤Æ¤ª¤ê¤Þ¤¹¡£");
380    }
381   
382    if($flg_ok) {
383        // ¥Ð¡¼¥¸¥ç¥ó¾ðÊó¤òºï½ü¤¹¤ë¡£
384        $sqlval['now_version'] = "";
385        $sqlval['update_date'] = "now()";
386        $objQuery->update("dtb_update", $sqlval, "module_id = ?", array($arrRet[0]['module_id']));
387    }
388}
389
390
391?>
Note: See TracBrowser for help on using the repository browser.