1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | database_migration - a plugin for Piwigo | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2010 Nicolas Roudaire http://www.nikrou.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | This program is free software; you can redistribute it and/or modify | |
---|
8 | // | it under the terms of the GNU General Public License version 2 as | |
---|
9 | // | published by the Free Software Foundation | |
---|
10 | // | | |
---|
11 | // | This program is distributed in the hope that it will be useful, but | |
---|
12 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
13 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
14 | // | General Public License for more details. | |
---|
15 | // | | |
---|
16 | // | You should have received a copy of the GNU General Public License | |
---|
17 | // | along with this program; if not, write to the Free Software | |
---|
18 | // | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
---|
19 | // | MA 02110-1301 USA | |
---|
20 | // +-----------------------------------------------------------------------+ |
---|
21 | |
---|
22 | define('PHPWG_ROOT_PATH', '../../'); |
---|
23 | define('DM_PLUGIN_ROOT', dirname(__FILE__)); |
---|
24 | define('PHPWG_DEFAULT_LANGUAGE', 'en_UK'); |
---|
25 | |
---|
26 | include_once PHPWG_ROOT_PATH . 'include/functions.inc.php'; |
---|
27 | include_once DM_PLUGIN_ROOT . '/include/config.class.php'; |
---|
28 | |
---|
29 | load_language('plugin.lang', DM_PLUGIN_ROOT.'/'); |
---|
30 | |
---|
31 | $conf['local_data_dir'] = PHPWG_ROOT_PATH . '_data'; |
---|
32 | $conf['show_queries'] = false; |
---|
33 | $conf['php_extension_in_urls'] = true; |
---|
34 | $conf['debug_l10n'] = false; |
---|
35 | |
---|
36 | $response = array(); |
---|
37 | |
---|
38 | if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { |
---|
39 | $xmlhttprequest = true; |
---|
40 | } else { |
---|
41 | $xmlhttprequest = false; |
---|
42 | } |
---|
43 | |
---|
44 | $me = new dmConfig(DM_PLUGIN_ROOT, 'database_migration'); |
---|
45 | $me->load_config(); |
---|
46 | |
---|
47 | if (!empty($_POST['dm_key']) && ($_POST['dm_key']==$me->dm_key)) { |
---|
48 | $connection = $me->getConnection($_POST['dblayer']); |
---|
49 | if ($connection==null) { |
---|
50 | addError(l10n('unknown dblayer')); |
---|
51 | } else { |
---|
52 | $dblayer = $_POST['dblayer']; |
---|
53 | include_once PHPWG_ROOT_PATH . 'include/dblayer/functions_'.$dblayer.'.inc.php'; |
---|
54 | |
---|
55 | $pwg_db_link = pwg_db_connect($connection['db_host'], $connection['db_user'], $connection['db_password'], $connection['db_base']); |
---|
56 | $result = execute_sqlfile(PHPWG_ROOT_PATH.'install/piwigo_structure-'.$dblayer.'.sql', |
---|
57 | 'piwigo_', |
---|
58 | $connection['prefix']); |
---|
59 | if (!$result) { |
---|
60 | addError(l10n('Cannot create struture')); |
---|
61 | } else { |
---|
62 | $structure_file = PHPWG_ROOT_PATH .'/install/piwigo_structure-mysql.sql'; |
---|
63 | $content = file_get_contents($structure_file); |
---|
64 | $pattern = '!CREATE TABLE `([^`]*)`!'; |
---|
65 | preg_match_all($pattern, $content, $matches); |
---|
66 | |
---|
67 | $Tables = $matches[1]; |
---|
68 | $ignoreTables = array('piwigo_history', 'piwigo_history_summary', 'piwigo_sessions', 'piwigo_user_cache', 'piwigo_user_cache_categories'); |
---|
69 | $dump_dir = $conf['local_data_dir'].'/plugins/database_migration/'; |
---|
70 | foreach ($Tables as $table) { |
---|
71 | if (in_array($table, $ignoreTables)) { |
---|
72 | continue; |
---|
73 | } |
---|
74 | $result = execute_sqlfile("{$dump_dir}/{$table}.dump", |
---|
75 | 'piwigo_', |
---|
76 | $connection['prefix']); |
---|
77 | if (!$result) { |
---|
78 | addError(l10n(sprintf('Cannot import %s content', $table))); |
---|
79 | break; |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | } else { |
---|
85 | addError(l10n('Hacking attempt!')); |
---|
86 | } |
---|
87 | |
---|
88 | if (empty($response)) { |
---|
89 | addInfo(l10n('Import completed')); |
---|
90 | } |
---|
91 | |
---|
92 | if ($xmlhttprequest) { |
---|
93 | header("Content-Type: application/json"); |
---|
94 | echo json_encode($response); |
---|
95 | exit(); |
---|
96 | } else { |
---|
97 | echo '<pre>', print_r($response, true), '</pre>'; |
---|
98 | } |
---|
99 | |
---|
100 | function addError($message) { |
---|
101 | global $response; |
---|
102 | |
---|
103 | $response['error'] = $message; |
---|
104 | } |
---|
105 | |
---|
106 | function addInfo($message) { |
---|
107 | global $response; |
---|
108 | |
---|
109 | $response['info'] = $message; |
---|
110 | } |
---|
111 | |
---|
112 | function execute_sqlfile($filepath, $replaced, $replacing) |
---|
113 | { |
---|
114 | $sql_lines = file($filepath); |
---|
115 | $query = ''; |
---|
116 | foreach ($sql_lines as $sql_line) |
---|
117 | { |
---|
118 | $sql_line = trim($sql_line); |
---|
119 | if (preg_match('/(^--|^$)/', $sql_line)) |
---|
120 | { |
---|
121 | continue; |
---|
122 | } |
---|
123 | $query.= ' '.$sql_line; |
---|
124 | if (preg_match('/;$/', $sql_line)) |
---|
125 | { |
---|
126 | $query = trim($query); |
---|
127 | $query = str_replace($replaced, $replacing, $query); |
---|
128 | |
---|
129 | $result = pwg_query($query); |
---|
130 | $query = ''; |
---|
131 | } |
---|
132 | } |
---|
133 | return true; |
---|
134 | } |
---|
135 | ?> |
---|