]> git.vanrenterghem.biz Git - www.vanrenterghem.biz.git/blob - phpBB2/admin/xs_include_import2.php
Baseline
[www.vanrenterghem.biz.git] / phpBB2 / admin / xs_include_import2.php
1 <?php\r
2 \r
3 /***************************************************************************\r
4  *                           xs_include_import2.php\r
5  *                           ----------------------\r
6  *   copyright            : (C) 2003 - 2005 CyberAlien\r
7  *   support              : http://www.phpbbstyles.com\r
8  *\r
9  *   version              : 2.3.1\r
10  *\r
11  *   file revision        : 75\r
12  *   project revision     : 78\r
13  *   last modified        : 05 Dec 2005  13:54:54\r
14  *\r
15  ***************************************************************************/\r
16 \r
17 /***************************************************************************\r
18  *\r
19  *   This program is free software; you can redistribute it and/or modify\r
20  *   it under the terms of the GNU General Public License as published by\r
21  *   the Free Software Foundation; either version 2 of the License, or\r
22  *   (at your option) any later version.\r
23  *\r
24  ***************************************************************************/\r
25 \r
26 if (!defined('IN_PHPBB') || !defined('IN_XS'))\r
27 {\r
28         die("Hacking attempt");\r
29 }\r
30 \r
31 /*\r
32 \r
33 Import style.\r
34 \r
35 $filename                       = style filename. it should be in temporary directory.\r
36 $write_local            = false if style should be uploaded via ftp, true if written directory to disk\r
37 $write_local_dir        = directory where to write. only if $write_local = true.\r
38 $list_only                      = true if only list files\r
39 $get_file                       = filename to get. empty if do not return any files\r
40 \r
41 $HTTP_POST_VARS['total']                                = total number of themes\r
42 $HTTP_POST_VARS['import_install_0']             = non-empty if install theme\r
43 $HTTP_POST_VARS['import_default']               = number of default style or -1 or empty\r
44 \r
45 */\r
46 \r
47 if(empty($list_only))\r
48 {\r
49         $list_only = false;\r
50 }\r
51 \r
52 $lang['xs_import_back'] = str_replace('{URL}', append_sid('xs_import.'.$phpEx), $lang['xs_import_back']);\r
53 \r
54 // list of text types. only last 4 characters of filename\r
55 $text_types = array('.tpl', '.htm', 'html', '.txt', '.css', '.cfg', '.php', '.xml');\r
56 // list of image types. if you add type make sure you add content-type header in code below\r
57 $img_types = array('.gif', '.jpg', '.jpe', 'jpeg', '.png');\r
58 \r
59 $file = XS_TEMP_DIR . xs_fix_dir($filename);\r
60 $header = xs_get_style_header($file);\r
61 if($header === false)\r
62 {\r
63         if(defined('XS_CLONING'))\r
64         {\r
65                 @unlink($tmp_filename);\r
66         }\r
67         xs_error($lang['xs_style_header_error_reason'] . $xs_header_error . '<br /><br />' . $lang['xs_import_back']);\r
68 }\r
69 if($header['filesize'] != filesize($file))\r
70 {\r
71         if(defined('XS_CLONING'))\r
72         {\r
73                 @unlink($tmp_filename);\r
74         }\r
75         xs_error($lang['xs_style_header_error_incomplete'] . '<br /><br />' . $lang['xs_import_back']);\r
76 }\r
77 $f = @fopen($file, 'rb');\r
78 if(!$f)\r
79 {\r
80         if(defined('XS_CLONING'))\r
81         {\r
82                 @unlink($tmp_filename);\r
83         }\r
84         xs_error($lang['xs_error_cannot_open'] . '<br /><br />' . $lang['xs_import_back']);\r
85 }\r
86 if($write_local)\r
87 {\r
88         $write_local_dir .= $header['template'] . '/';\r
89 }\r
90 fseek($f, $header['offset'], 0);\r
91 $str = fread($f, filesize($file) - $header['offset']);\r
92 fclose($f);\r
93 $str = @gzuncompress($str);\r
94 if($str === false || !strlen($str))\r
95 {\r
96         if(defined('XS_CLONING'))\r
97         {\r
98                 @unlink($tmp_filename);\r
99         }\r
100         xs_error($lang['xs_error_decompress_style'] . '<br /><br />' . $lang['xs_import_back']);\r
101 }\r
102 //\r
103 // unpack tar file\r
104 //\r
105 $pos = 0;\r
106 $tmp_name = XS_TEMP_DIR . 'file_' . $filename . '_%02d.tmp';\r
107 $tmp_count = 0;\r
108 $files = array();       // complete list of files\r
109 $list_data = array();   // result for list\r
110 $dirs = array();        // complete list of directories\r
111 $items = array();       // data\r
112 while($pos < strlen($str))\r
113 {\r
114         $data = unpack(TAR_HEADER_UNPACK, substr($str, $pos, 512));\r
115         $pos += 512;\r
116         $data['filename'] = trim($data['prefix']) . trim($data['filename']);\r
117         if(substr($data['filename'], 0, 2) === './')\r
118         {\r
119                 $data['filename'] = substr($data['filename'], 2);\r
120         }\r
121         if($write_local)\r
122         {\r
123                 $save_filename = $write_local_dir . $data['filename'];\r
124         }\r
125         else\r
126         {\r
127                 $pos1 = strrpos($data['filename'], '/');\r
128                 if($pos1)\r
129                 {\r
130                         $data['dir'] = substr($data['filename'], 0, $pos1);\r
131                         $data['file'] = substr($data['filename'], $pos1 + 1);\r
132                 }\r
133                 else\r
134                 {\r
135                         $data['dir'] = '';\r
136                         $data['file'] = $data['filename'];\r
137                 }\r
138         }\r
139         $data['size'] = octdec(trim($data['size']));\r
140         $data['mtime'] = octdec(trim($data['mtime']));\r
141         $data['typeflag'] = octdec(trim($data['typeflag']));\r
142         if($data['typeflag'] === '5')\r
143         {\r
144                 $data['size'] = 0;\r
145                 if($write_local)\r
146                 {\r
147                         xs_create_dir($save_filename);\r
148                 }\r
149         }\r
150         $data['offset'] = $pos;\r
151         $contents = $data['size'] > 0 ? substr($str, $pos, $data['size']) : '';\r
152         $data['tmp'] = '';\r
153         // adding to list\r
154         $is_file = true;\r
155         if(intval($data['typeflag']) == 5)\r
156         {\r
157                 $is_file = false;\r
158                 if($data['filename'])\r
159                 {\r
160                         $dirs[] = $data['filename'];\r
161                 }\r
162         }\r
163         else\r
164         {\r
165                 if($data['filename'])\r
166                 {\r
167                         if(!$list_only)\r
168                         {\r
169                                 if($write_local)\r
170                                 {\r
171                                         $res = xs_write_file($save_filename, $contents);\r
172                                         if(!$res)\r
173                                         {\r
174                                                 if(defined('XS_CLONING'))\r
175                                                 {\r
176                                                         @unlink($tmp_filename);\r
177                                                 }\r
178                                                 xs_error(str_replace('{FILE}', $save_filename, $lang['xs_error_cannot_create_file']) . '<br /><br />' . $lang['xs_import_back']);\r
179                                         }\r
180                                 }\r
181                                 else\r
182                                 {\r
183                                         // write to temporary file\r
184                                         $tmp_count ++;                          \r
185                                         $data['tmp'] = sprintf($tmp_name, $tmp_count);\r
186                                         $f = @fopen($data['tmp'], 'wb');\r
187                                         if(!$f)\r
188                                         {\r
189                                                 if(defined('XS_CLONING'))\r
190                                                 {\r
191                                                         @unlink($tmp_filename);\r
192                                                 }\r
193                                                 xs_error(str_replace('{FILE}', $data['tmp'], $lang['xs_error_cannot_create_tmp']) . '<br /><br />' . $lang['xs_import_back']);\r
194                                         }\r
195                                         fwrite($f, $contents);\r
196                                         fclose($f);\r
197                                 }\r
198                         }\r
199                         elseif(!empty($get_file) && $get_file === $data['filename'])\r
200                         {\r
201                                 // show contents of file\r
202                                 $f = $data['filename'];\r
203                                 $ext = strtolower(substr($f, strlen($f) - 4));\r
204                                 if(empty($HTTP_GET_VARS['get_content']) && xs_in_array($ext, $text_types))\r
205                                 {\r
206                                         // show as text\r
207                                         $str = '<div align="left">' . $lang['xs_import_list_contents'] . $f . ' [<a href="' . append_sid('xs_import.' . $phpEx . '?list=1&import=' . urlencode($filename) . '&get_file=' . urlencode($f) . '&get_content=1') . '">' . $lang['xs_import_download_lc'] . '</a>]<br /><br />';\r
208                                         $str .= '<textarea cols="120" rows="30" style="width: 100%">' . htmlspecialchars($contents) . '</textarea>';\r
209                                         $str .= '</div>';\r
210                                         xs_message($lang['Information'], $str);\r
211                                 }\r
212                                 else\r
213                                 {\r
214                                         $do_download = false;\r
215                                         $content_type = '';\r
216                                         if(empty($HTTP_GET_VARS['get_content']))\r
217                                         {\r
218                                                 if($ext === '.gif')\r
219                                                 {\r
220                                                         $content_type = 'image/gif';\r
221                                                 }\r
222                                                 elseif($ext === '.jpg' || $ext === '.jpe' || $ext === 'jpeg')\r
223                                                 {\r
224                                                         $content_type = 'image/jpeg';\r
225                                                 }\r
226                                                 elseif($ext === '.png')\r
227                                                 {\r
228                                                         $content_type = 'image/png';\r
229                                                 }\r
230                                                 else\r
231                                                 {\r
232                                                         $do_download = true;\r
233                                                 }\r
234                                         }\r
235                                         else\r
236                                         {\r
237                                                 $do_download = true;\r
238                                         }\r
239                                         xs_download_file($do_download ? basename($f) : '', $contents, $content_type);\r
240                                         xs_exit();\r
241                                 }\r
242                         }\r
243                         else\r
244                         {\r
245                                 $list_data[$data['filename']] = $data;\r
246                         }\r
247                         $files[] = $data['filename'];\r
248                 }\r
249         }\r
250         if(empty($data['filename']) && $is_file)\r
251         {\r
252                 $pos = strlen($str);\r
253         }\r
254         else\r
255         {\r
256                 $pos += floor(($data['size'] + 511) / 512) * 512;\r
257                 if($is_file)\r
258                 {\r
259                         $items[] = $data;\r
260                 }\r
261         }\r
262 }\r
263 if($list_only)\r
264 {\r
265         // show list of files. used for debug.\r
266         $str = '<div align="left">';\r
267         // main data\r
268         $str .= $lang['xs_import_list_filename'] . $header['filename'] . '<br />';\r
269         $str .= $lang['xs_import_list_template'] . $header['template'] . '<br />';\r
270         $str .= $lang['xs_import_list_comment'] . $header['comment'] . '<br />';\r
271         $str .= $lang['xs_import_list_styles'] . implode(', ', $header['styles']) . '<br />';\r
272         ksort($list_data);\r
273         $str .= '<br />' . str_replace('{NUM}', count($list_data), $lang['xs_import_list_files']) . '<br />';\r
274         $str .= '<table border="0" cellspacing="0" cellpadding="1" align="left">';\r
275         foreach($list_data as $var => $value)\r
276         {\r
277                 $str .= '<tr><td>' . htmlspecialchars($value['filename']) . '</td><td>';\r
278                 if($value['size'] > 0)\r
279                 {\r
280                         $ext = strtolower(substr($var, strlen($var) - 4));\r
281                         if(xs_in_array($ext, $text_types) || xs_in_array($ext, $img_types))\r
282                         {\r
283                                 $str .= '[<a href="' . append_sid('xs_import.' . $phpEx . '?list=1&import=' . urlencode($filename) . '&get_file=' . urlencode($var)) . '">' . $lang['xs_import_view_lc'] . '</a>] ';\r
284                         }\r
285                         $str .= '[<a href="' . append_sid('xs_import.' . $phpEx . '?list=1&import=' . urlencode($filename) . '&get_file=' . urlencode($var)) . '&get_content=1">' . $lang['xs_import_download_lc'] . '</a>] ';\r
286                 }\r
287                 $str .= str_replace('{NUM}', $value['size'], $lang['xs_import_file_size']) . '</td></tr>';\r
288         }\r
289         $str .= '</table>';\r
290         $str .= '</div>';\r
291         xs_message($lang['Information'], $str);\r
292 }\r
293 $str = '';\r
294 if(!$write_local)\r
295 {\r
296         //\r
297         // Generate actions list\r
298         //\r
299         $actions = array();\r
300         // chdir to template directory\r
301         $actions[] = array(\r
302                         'command'       => 'chdir',\r
303                         'dir'           => 'templates'\r
304                 );\r
305         // create directory with template name\r
306         $actions[] = array(\r
307                         'command'       => 'mkdir',\r
308                         'dir'           => $header['template'],\r
309                         'ignore'        => true\r
310                 );\r
311         // change directory\r
312         $actions[] = array(\r
313                         'command'       => 'chdir',\r
314                         'dir'           => $header['template']\r
315                 );\r
316         // create all directories and upload all files\r
317         $actions[] = array(\r
318                         'command'       => 'exec',\r
319                         'list'          => generate_actions_dirs()\r
320                 );\r
321         $ftp_log = array();\r
322         $ftp_error = '';\r
323         $res = ftp_myexec($actions);\r
324 /*      echo "<!--\n\n";\r
325         echo "\$actions dump:\n\n";\r
326         print_r($actions);\r
327         echo "\n\n\$ftp_log dump:\n\n";\r
328         print_r($ftp_log);\r
329         echo "\n\n -->"; */\r
330         // remove temporary files\r
331         for($i=0; $i<count($items); $i++)\r
332         {\r
333                 if(!empty($items[$i]['tmp']))\r
334                 {\r
335                         @unlink($items[$i]['tmp']);\r
336                 }\r
337         }\r
338         if(!$res)\r
339         {\r
340                 if(defined('XS_CLONING'))\r
341                 {\r
342                         @unlink($tmp_filename);\r
343                 }\r
344                 xs_error($ftp_error . '<br /><br />' . $lang['xs_import_back']);\r
345         }\r
346 }\r
347 \r
348 //\r
349 // Check if we need to install style\r
350 //\r
351 $total = intval($HTTP_POST_VARS['total']);\r
352 $default = isset($HTTP_POST_VARS['import_default']) && strlen($HTTP_POST_VARS['import_default']) ? intval($HTTP_POST_VARS['import_default']) : -1;\r
353 $install = array();\r
354 $default_name = '';\r
355 for($i=0; $i<$total; $i++)\r
356 {\r
357         $tmp = empty($HTTP_POST_VARS['import_install_'.$i]) ? 0 : 1;\r
358         if($tmp)\r
359         {\r
360                 $set_default = $default == $i ? 1 : 0;\r
361                 $tmp_name = $header['styles'][$i];\r
362                 if($tmp_name)\r
363                 {\r
364                         $install[] = $tmp_name;\r
365                         if($set_default)\r
366                         {\r
367                                 $default_name = $tmp_name;\r
368                         }\r
369                 }\r
370         }\r
371 }\r
372 if(!count($install))\r
373 {\r
374         if(defined('XS_CLONING'))\r
375         {\r
376                 @unlink($tmp_filename);\r
377         }\r
378         xs_message($lang['Information'], $lang['xs_import_uploaded'] . '<br /><br />' . $lang['xs_import_back']);\r
379 }\r
380 //\r
381 // Get list of installed styles\r
382 //\r
383 $tpl = $header['template'];\r
384 $sql = "SELECT themes_id, style_name FROM " . THEMES_TABLE . " WHERE template_name='" . xs_sql($tpl) . "'";\r
385 if(!$result = $db->sql_query($sql))\r
386 {\r
387         if(defined('XS_CLONING'))\r
388         {\r
389                 @unlink($tmp_filename);\r
390         }\r
391         xs_error($lang['xs_import_notinstall'] . '<br /><br />' . $lang['xs_import_back']);\r
392 }\r
393 $style_rowset = $db->sql_fetchrowset($result);\r
394 // run theme_info.cfg\r
395 $data = xs_get_themeinfo($tpl);\r
396 if(!@count($data))\r
397 {\r
398         if(defined('XS_CLONING'))\r
399         {\r
400                 @unlink($tmp_filename);\r
401         }\r
402         xs_error($lang['xs_import_notinstall2'] . '<br /><br />' . $lang['xs_import_back']);\r
403 }\r
404 // install styles\r
405 $default_id = 0;\r
406 for($i=0; $i<count($install); $i++)\r
407 {\r
408         $style_name = $install[$i];\r
409         $style_data = false;\r
410         // find entry in theme_info.cfg\r
411         for($j=0; $j<count($data); $j++)\r
412         {\r
413                 if($data[$j]['style_name'] === $style_name)\r
414                 {\r
415                         $style_data = $data[$j];\r
416                 }\r
417         }\r
418         // check if already installed\r
419         $installed = 0;\r
420         for($j=0; $j<count($style_rowset); $j++)\r
421         {\r
422                 if($style_rowset[$j]['style_name'] === $style_name)\r
423                 {\r
424                         $installed = $style_rowset[$j]['themes_id'];\r
425                 }\r
426         }\r
427         // install/update\r
428         if(empty($style_data['style_name']) || empty($style_data['template_name']))\r
429         {\r
430                 if(defined('XS_CLONING'))\r
431                 {\r
432                         @unlink($tmp_filename);\r
433                 }\r
434                 xs_error(str_replace('{STYLE}', $style_name, $lang['xs_import_notinstall3']) . '<br /><br />' . $lang['xs_import_back']);\r
435         }\r
436         if($installed)\r
437         {\r
438                 // update\r
439                 $sql = '';\r
440                 foreach($style_data as $var => $value)\r
441                 {\r
442                         if($sql)\r
443                         {\r
444                                 $sql .= ', ';\r
445                         }\r
446                         $sql .= xs_sql($var) . " = '" . xs_sql($value) . "'";\r
447                 }\r
448                 $sql = "UPDATE " . THEMES_TABLE . " SET " . $sql . " WHERE themes_id = '{$installed}'";\r
449         }\r
450         else\r
451         {\r
452                 // install\r
453                 $sql = "SELECT MAX(themes_id) AS total FROM " . THEMES_TABLE;\r
454                 if ( !($result = $db->sql_query($sql)) )\r
455                 {\r
456                         if(defined('XS_CLONING'))\r
457                         {\r
458                                 @unlink($tmp_filename);\r
459                         }\r
460                         xs_error($lang['xs_import_notinstall4'] . '<br /><br />' . $lang['xs_import_back']);\r
461                 }\r
462                 if ( !($row = $db->sql_fetchrow($result)) )\r
463                 {\r
464                         if(defined('XS_CLONING'))\r
465                         {\r
466                                 @unlink($tmp_filename);\r
467                         }\r
468                         xs_error($lang['xs_import_notinstall4'] . '<br /><br />' . $lang['xs_import_back']);\r
469                 }\r
470                 $installed = $row['total'] + 1;\r
471                 $style_data['themes_id'] = $installed;\r
472                 $sql1 = $sql2 = '';\r
473                 foreach($style_data as $var => $value)\r
474                 {\r
475                         if($sql1)\r
476                         {\r
477                                 $sql1 .= ', ';\r
478                                 $sql2 .= ', ';\r
479                         }\r
480                         $sql1 .= xs_sql($var);\r
481                         $sql2 .= "'" . xs_sql($value) . "'";\r
482                 }\r
483                 $sql = "INSERT INTO " . THEMES_TABLE . " (" . $sql1 . ") VALUES (" . $sql2 . ")";\r
484         }\r
485         if ( !($result = $db->sql_query($sql)) )\r
486         {\r
487                 if(defined('XS_CLONING'))\r
488                 {\r
489                         @unlink($tmp_filename);\r
490                 }\r
491                 xs_error($lang['xs_import_notinstall5'] . '<br /><br />' . $lang['xs_import_back']);\r
492         }\r
493         if($default_name === $style_name)\r
494         {\r
495                 $sql = "UPDATE " . CONFIG_TABLE . " SET config_value='{$installed}' WHERE config_name='default_style'";\r
496                 $board_config['default_style'] = $installed;\r
497                 $db->sql_query($sql);\r
498         }\r
499 }\r
500 if(defined('XS_CLONING'))\r
501 {\r
502         @unlink($tmp_filename);\r
503 }\r
504 if(count($install) && defined('XS_MODS_CATEGORY_HIERARCHY210'))\r
505 {\r
506         // recache themes table\r
507         if ( empty($themes) )\r
508         {\r
509                 $themes = new themes();\r
510         }\r
511         if ( !empty($themes) )\r
512         {\r
513                 $themes->read(true);\r
514         }\r
515 }\r
516 if(count($install) && defined('XS_MODS_CATEGORY_HIERARCHY'))\r
517 {\r
518         cache_themes();\r
519 }\r
520 xs_message($lang['Information'], $lang['xs_import_installed'] . '<br /><br />' . $lang['xs_import_back']);\r
521 \r
522 ?>