1 | <?php |
---|
2 | |
---|
3 | function autoupdate_deltree($path) |
---|
4 | { |
---|
5 | if (is_dir($path)) |
---|
6 | { |
---|
7 | $fh = opendir($path); |
---|
8 | while ($file = readdir($fh)) |
---|
9 | { |
---|
10 | if ($file != '.' and $file != '..') |
---|
11 | { |
---|
12 | $pathfile = $path . '/' . $file; |
---|
13 | if (is_dir($pathfile)) |
---|
14 | { |
---|
15 | autoupdate_deltree($pathfile); |
---|
16 | } |
---|
17 | else |
---|
18 | { |
---|
19 | @unlink($pathfile); |
---|
20 | } |
---|
21 | } |
---|
22 | } |
---|
23 | closedir($fh); |
---|
24 | return @rmdir($path); |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | function autoupdate_add_index($path) |
---|
29 | { |
---|
30 | if (is_dir($path)) |
---|
31 | { |
---|
32 | $fh = opendir($path); |
---|
33 | while ($file = readdir($fh)) |
---|
34 | { |
---|
35 | if ($file != '.' and $file != '..') |
---|
36 | { |
---|
37 | $pathfile = $path . '/' . $file; |
---|
38 | if (is_dir($pathfile)) |
---|
39 | { |
---|
40 | autoupdate_add_index($pathfile); |
---|
41 | } |
---|
42 | } |
---|
43 | } |
---|
44 | $fp = @fopen($path.'/index.php', 'w'); |
---|
45 | @fwrite($fp, AU_DEFAULT_INDEX); |
---|
46 | @fclose($fp); |
---|
47 | closedir($fh); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | function process_obsolete_list($file) |
---|
52 | { |
---|
53 | if (file_exists(PHPWG_ROOT_PATH.$file) |
---|
54 | and $old_files = file(PHPWG_ROOT_PATH.$file, FILE_IGNORE_NEW_LINES) |
---|
55 | and !empty($old_files)) |
---|
56 | { |
---|
57 | array_push($old_files, $file); |
---|
58 | foreach($old_files as $old_file) |
---|
59 | { |
---|
60 | $path = PHPWG_ROOT_PATH.$old_file; |
---|
61 | if (is_file($path)) |
---|
62 | { |
---|
63 | @unlink($path); |
---|
64 | } |
---|
65 | elseif (is_dir($path)) |
---|
66 | { |
---|
67 | autoupdate_deltree($path); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | function move_local_files($dir) |
---|
74 | { |
---|
75 | global $page, $cfgBase, $cfgUser, $cfgPassword, $cfgHote, $prefixeTable; |
---|
76 | |
---|
77 | if ((!is_dir($dir) and !mkdir($dir, 0777)) |
---|
78 | or (!is_dir($dir.'/config') and !mkdir($dir.'/config')) |
---|
79 | or (!is_dir($dir.'/css') and !mkdir($dir.'/css')) |
---|
80 | or (!is_dir($dir.'/language') and !mkdir($dir.'/language'))) |
---|
81 | { |
---|
82 | autoupdate_deltree($dir); |
---|
83 | array_push($page['errors'], l10n('Unable to write new local directory.')); |
---|
84 | return; |
---|
85 | } |
---|
86 | |
---|
87 | // Add index.php |
---|
88 | autoupdate_add_index($dir); |
---|
89 | |
---|
90 | // mysql.inc.php |
---|
91 | $file = PHPWG_ROOT_PATH.'include/mysql.inc.php'; |
---|
92 | if (is_readable($file)) |
---|
93 | { |
---|
94 | $file_content = '<?php |
---|
95 | $conf[\'dblayer\'] = \'mysql\'; |
---|
96 | $conf[\'db_base\'] = \''.$cfgBase.'\'; |
---|
97 | $conf[\'db_user\'] = \''.$cfgUser.'\'; |
---|
98 | $conf[\'db_password\'] = \''.$cfgPassword.'\'; |
---|
99 | $conf[\'db_host\'] = \''.$cfgHote.'\'; |
---|
100 | |
---|
101 | $prefixeTable = \''.$prefixeTable.'\'; |
---|
102 | |
---|
103 | define(\'PHPWG_INSTALLED\', true);'; |
---|
104 | if (defined('PWG_CHARSET')) |
---|
105 | { |
---|
106 | $file_content.= ' |
---|
107 | define(\'PWG_CHARSET\', \'utf-8\'); |
---|
108 | define(\'DB_CHARSET\', \'utf8\'); |
---|
109 | define(\'DB_COLLATE\', \'\');'; |
---|
110 | } |
---|
111 | $file_content.= ' |
---|
112 | ?>'; |
---|
113 | $new_config_file = $dir.'/config/database.inc.php'; |
---|
114 | |
---|
115 | if (!($fp = @fopen($new_config_file, 'w')) |
---|
116 | or !@fwrite($fp, $file_content) |
---|
117 | or !@fclose($fp)) |
---|
118 | { |
---|
119 | array_push($page['errors'], l10n('Unable to write new local directory.')); |
---|
120 | return; |
---|
121 | } |
---|
122 | @chmod($new_config_file, 0755); |
---|
123 | } |
---|
124 | |
---|
125 | // config_local.inc.php |
---|
126 | $file = PHPWG_ROOT_PATH.'include/config_local.inc.php'; |
---|
127 | if (is_readable($file)) |
---|
128 | { |
---|
129 | copy($file, $dir.'/config/config.inc.php'); |
---|
130 | @chmod($file, 0755); |
---|
131 | } |
---|
132 | |
---|
133 | // languages |
---|
134 | $language_dir = opendir(PHPWG_ROOT_PATH.'language'); |
---|
135 | while ($file = readdir($language_dir)) |
---|
136 | { |
---|
137 | $path = PHPWG_ROOT_PATH.'language/'.$file; |
---|
138 | if (!is_link($path) and is_dir($path) and is_readable($path.'/local.lang.php')) |
---|
139 | { |
---|
140 | $content = file_get_contents($path.'/local.lang.php'); |
---|
141 | $langdef = explode('.',$file); |
---|
142 | if (count($langdef)>1) |
---|
143 | { |
---|
144 | $content = utf8_encode($content); |
---|
145 | } |
---|
146 | $filename = $dir.'/language/'.$langdef[0].'.lang.php'; |
---|
147 | $fp = @fopen($filename, 'w'); |
---|
148 | @fwrite($fp, $content); |
---|
149 | @fclose($fp); |
---|
150 | @chmod($filename, 0755); |
---|
151 | } |
---|
152 | } |
---|
153 | closedir($language_dir); |
---|
154 | |
---|
155 | // template-common/local-layout.css |
---|
156 | $file = PHPWG_ROOT_PATH.'template-common/local-layout.css'; |
---|
157 | if (is_readable($file)) |
---|
158 | { |
---|
159 | copy($file, $dir.'/css/rules.css'); |
---|
160 | @chmod($file, 0755); |
---|
161 | } |
---|
162 | |
---|
163 | // template/xxx/local-layout.css |
---|
164 | $known_templates = array( |
---|
165 | 'yoga' => 'default', |
---|
166 | 'floPure' => 'Pure_default', |
---|
167 | 'floOs' => 'OS_default', |
---|
168 | 'gally' => 'gally-default', |
---|
169 | 'simple' => 'simple', |
---|
170 | ); |
---|
171 | |
---|
172 | foreach ($known_templates as $old_tpl => $new_tpl) |
---|
173 | { |
---|
174 | $file = PHPWG_ROOT_PATH.'template/'.$old_tpl.'/local-layout.css'; |
---|
175 | if (is_readable($file)) |
---|
176 | { |
---|
177 | copy($file, $dir.'/css/'.$new_tpl.'-rules.css'); |
---|
178 | @chmod($file, 0755); |
---|
179 | } |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | function upgrade_to($upgrade_to, &$step) |
---|
184 | { |
---|
185 | global $page, $conf, $template; |
---|
186 | |
---|
187 | if (!version_compare($_POST['upgrade_to'], PHPWG_VERSION, '>')) |
---|
188 | { |
---|
189 | redirect(get_admin_plugin_menu_link(AUTOUPDATE_PATH . '/autoupdate.php')); |
---|
190 | } |
---|
191 | |
---|
192 | if ($step == 2) |
---|
193 | { |
---|
194 | preg_match('/(\d+\.\d+)\.(\d+)/', PHPWG_VERSION, $matches); |
---|
195 | $code = $matches[1].'.x_to_'.$_POST['upgrade_to']; |
---|
196 | $dl_code = str_replace(array('.', '_'), '', $code); |
---|
197 | $remove_path = $code; |
---|
198 | $obsolete_list = 'obsolete.list'; |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | $code = $_POST['upgrade_to']; |
---|
203 | $dl_code = $code; |
---|
204 | $remove_path = version_compare($code, '2.0.8', '>=') ? 'piwigo' : 'piwigo-'.$code; |
---|
205 | $obsolete_list = PHPWG_ROOT_PATH.'install/obsolete.list'; |
---|
206 | |
---|
207 | if (version_compare(PHPWG_VERSION, '2.1', '<')) |
---|
208 | { |
---|
209 | move_local_files(PHPWG_ROOT_PATH.'local'); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | if (empty($page['errors'])) |
---|
214 | { |
---|
215 | $path = $conf['local_data_dir'].'/autoupdate'; |
---|
216 | $filename = $path.'/'.$code.'.zip'; |
---|
217 | @mkgetdir($path); |
---|
218 | |
---|
219 | $chunk_num = 0; |
---|
220 | $end = false; |
---|
221 | $zip = @fopen($filename, 'w'); |
---|
222 | while (!$end) |
---|
223 | { |
---|
224 | $chunk_num++; |
---|
225 | if (@fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.$dl_code.'&chunk_num='.$chunk_num, $result) |
---|
226 | and $input = @unserialize($result)) |
---|
227 | { |
---|
228 | if (0 == $input['remaining']) |
---|
229 | { |
---|
230 | $end = true; |
---|
231 | } |
---|
232 | @fwrite($zip, base64_decode($input['data'])); |
---|
233 | } |
---|
234 | else |
---|
235 | { |
---|
236 | $end = true; |
---|
237 | } |
---|
238 | } |
---|
239 | @fclose($zip); |
---|
240 | |
---|
241 | if (@filesize($filename)) |
---|
242 | { |
---|
243 | $zip = new PclZip($filename); |
---|
244 | if ($result = $zip->extract(PCLZIP_OPT_PATH, PHPWG_ROOT_PATH, |
---|
245 | PCLZIP_OPT_REMOVE_PATH, $remove_path, |
---|
246 | PCLZIP_OPT_SET_CHMOD, 0755, |
---|
247 | PCLZIP_OPT_REPLACE_NEWER)) |
---|
248 | { |
---|
249 | //Check if all files were extracted |
---|
250 | $error = ''; |
---|
251 | foreach($result as $extract) |
---|
252 | { |
---|
253 | if (!in_array($extract['status'], array('ok', 'filtered', 'already_a_directory'))) |
---|
254 | { |
---|
255 | // Try to change chmod and extract |
---|
256 | if (@chmod(PHPWG_ROOT_PATH.$extract['filename'], 0777) |
---|
257 | and ($res = $zip->extract(PCLZIP_OPT_BY_NAME, $remove_path.'/'.$extract['filename'], |
---|
258 | PCLZIP_OPT_PATH, PHPWG_ROOT_PATH, |
---|
259 | PCLZIP_OPT_REMOVE_PATH, $remove_path, |
---|
260 | PCLZIP_OPT_SET_CHMOD, 0755, |
---|
261 | PCLZIP_OPT_REPLACE_NEWER)) |
---|
262 | and isset($res[0]['status']) |
---|
263 | and $res[0]['status'] == 'ok') |
---|
264 | { |
---|
265 | continue; |
---|
266 | } |
---|
267 | else |
---|
268 | { |
---|
269 | $error .= $extract['filename'].': '.$extract['status']."\n"; |
---|
270 | } |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | if (empty($error)) |
---|
275 | { |
---|
276 | process_obsolete_list($obsolete_list); |
---|
277 | autoupdate_deltree($conf['local_data_dir'].'/autoupdate'); |
---|
278 | invalidate_user_cache(true); |
---|
279 | $template->delete_compiled_templates(); |
---|
280 | if ($step == 2) |
---|
281 | { |
---|
282 | array_push($page['infos'], sprintf(l10n('autoupdate_success'), $upgrade_to)); |
---|
283 | $template->assign('CHECK_VERSION', true); |
---|
284 | $step = 0; |
---|
285 | } |
---|
286 | else |
---|
287 | { |
---|
288 | redirect(PHPWG_ROOT_PATH.'upgrade.php?now='); |
---|
289 | } |
---|
290 | } |
---|
291 | else |
---|
292 | { |
---|
293 | file_put_contents($conf['local_data_dir'].'/autoupdate/log_error.txt', $error); |
---|
294 | $relative_path = trim(str_replace(dirname(dirname(dirname(dirname(__FILE__)))), '', $conf['local_data_dir']), '/\\'); |
---|
295 | array_push($page['errors'], sprintf(l10n('autoupdate_extract_fail'), PHPWG_ROOT_PATH.$relative_path.'/autoupdate/log_error.txt')); |
---|
296 | } |
---|
297 | } |
---|
298 | else |
---|
299 | { |
---|
300 | autoupdate_deltree($conf['local_data_dir'].'/autoupdate'); |
---|
301 | array_push($page['errors'], l10n('autoupdate_fail')); |
---|
302 | } |
---|
303 | } |
---|
304 | else |
---|
305 | { |
---|
306 | array_push($page['errors'], l10n('Piwigo cannot retrieve upgrade file from server')); |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | define('AU_DEFAULT_INDEX', file_get_contents(AUTOUPDATE_PATH.'index.php')); |
---|
312 | ?> |
---|