3 /***************************************************************************
4 * xs_include_import2.php
5 * ----------------------
6 * copyright : (C) 2003, 2004 CyberAlien
7 * support : http://www.phpbbstyles.com
12 * project revision : 51
13 * last modified : 25 Aug 2004 11:51:47
15 ***************************************************************************/
17 /***************************************************************************
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 ***************************************************************************/
26 if (!defined('IN_PHPBB') || !defined('IN_XS'))
28 die("Hacking attempt");
35 $filename = style filename. it should be in temporary directory.
36 $write_local = false if style should be uploaded via ftp, true if written directory to disk
37 $write_local_dir = directory where to write. only if $write_local = true.
38 $list_only = true if only list files
39 $get_file = filename to get. empty if do not return any files
41 $HTTP_POST_VARS['total'] = total number of themes
42 $HTTP_POST_VARS['import_install_0'] = non-empty if install theme
43 $HTTP_POST_VARS['import_default_0'] = non-empty if make it default
52 $lang['xs_import_back'] = str_replace('{URL}', append_sid('xs_import.'.$phpEx), $lang['xs_import_back']);
54 // list of text types. only last 4 characters of filename
55 $text_types = array('.tpl', '.htm', 'html', '.txt', '.css', '.cfg', '.php', '.xml');
56 // list of image types. if you add type make sure you add content-type header in code below
57 $img_types = array('.gif', '.jpg', '.jpe', 'jpeg', '.png');
59 $file = XS_TEMP_DIR . $filename;
60 $header = xs_get_style_header($file);
63 if(defined('XS_CLONING'))
65 @unlink($tmp_filename);
67 xs_error($lang['xs_style_header_error_reason'] . $xs_header_error . '<br /><br />' . $lang['xs_import_back']);
69 if($header['filesize'] != filesize($file))
71 if(defined('XS_CLONING'))
73 @unlink($tmp_filename);
75 xs_error($lang['xs_style_header_error_incomplete'] . '<br /><br />' . $lang['xs_import_back']);
77 $f = @fopen($file, 'rb');
80 if(defined('XS_CLONING'))
82 @unlink($tmp_filename);
84 xs_error($lang['xs_error_cannot_open'] . '<br /><br />' . $lang['xs_import_back']);
88 $write_local_dir .= $header['template'] . '/';
90 fseek($f, $header['offset'], 0);
91 $str = fread($f, filesize($file) - $header['offset']);
93 $str = @gzuncompress($str);
94 if($str === false || !strlen($str))
96 if(defined('XS_CLONING'))
98 @unlink($tmp_filename);
100 xs_error($lang['xs_error_decompress_style'] . '<br /><br />' . $lang['xs_import_back']);
106 $tmp_name = XS_TEMP_DIR . 'file_' . $filename . '_%02d.tmp';
108 $files = array(); // complete list of files
109 $list_data = array(); // result for list
110 $dirs = array(); // complete list of directories
111 $items = array(); // data
112 while($pos < strlen($str))
114 $data = unpack(TAR_HEADER_UNPACK, substr($str, $pos, 512));
116 $data['filename'] = trim($data['prefix']) . trim($data['filename']);
117 if(substr($data['filename'], 0, 2) === './')
119 $data['filename'] = substr($data['filename'], 2);
123 $save_filename = $write_local_dir . $data['filename'];
127 $pos1 = strrpos($data['filename'], '/');
130 $data['dir'] = substr($data['filename'], 0, $pos1);
131 $data['file'] = substr($data['filename'], $pos1 + 1);
136 $data['file'] = $data['filename'];
139 $data['size'] = octdec(trim($data['size']));
140 $data['mtime'] = octdec(trim($data['mtime']));
141 $data['typeflag'] = octdec(trim($data['typeflag']));
142 if($data['typeflag'] === '5')
147 xs_create_dir($save_filename);
150 $data['offset'] = $pos;
151 $contents = $data['size'] > 0 ? substr($str, $pos, $data['size']) : '';
155 if(intval($data['typeflag']) == 5)
158 if($data['filename'])
160 $dirs[] = $data['filename'];
165 if($data['filename'])
171 $res = xs_write_file($save_filename, $contents);
174 if(defined('XS_CLONING'))
176 @unlink($tmp_filename);
178 xs_error(str_replace('{FILE}', $save_filename, $lang['xs_error_cannot_create_file']) . '<br /><br />' . $lang['xs_import_back']);
183 // write to temporary file
185 $data['tmp'] = sprintf($tmp_name, $tmp_count);
186 $f = @fopen($data['tmp'], 'wb');
189 if(defined('XS_CLONING'))
191 @unlink($tmp_filename);
193 xs_error(str_replace('{FILE}', $data['tmp'], $lang['xs_error_cannot_create_tmp']) . '<br /><br />' . $lang['xs_import_back']);
195 fwrite($f, $contents);
199 elseif(!empty($get_file) && $get_file === $data['filename'])
201 // show contents of file
202 $f = $data['filename'];
203 $ext = strtolower(substr($f, strlen($f) - 4));
204 if(empty($HTTP_GET_VARS['get_content']) && xs_in_array($ext, $text_types))
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 />';
208 $str .= '<textarea cols="120" rows="30" style="width: 100%">' . htmlspecialchars($contents) . '</textarea>';
210 xs_message($lang['Information'], $str);
214 $do_download = false;
216 if(empty($HTTP_GET_VARS['get_content']))
220 $content_type = 'image/gif';
222 elseif($ext === '.jpg' || $ext === '.jpe' || $ext === 'jpeg')
224 $content_type = 'image/jpeg';
226 elseif($ext === '.png')
228 $content_type = 'image/png';
239 xs_download_file($do_download ? basename($f) : '', $contents, $content_type);
245 $list_data[$data['filename']] = $data;
247 $files[] = $data['filename'];
250 if(empty($data['filename']) && $is_file)
256 $pos += floor(($data['size'] + 511) / 512) * 512;
265 // show list of files. used for debug.
266 $str = '<div align="left">';
268 $str .= $lang['xs_import_list_filename'] . $header['filename'] . '<br />';
269 $str .= $lang['xs_import_list_template'] . $header['template'] . '<br />';
270 $str .= $lang['xs_import_list_comment'] . $header['comment'] . '<br />';
271 $str .= $lang['xs_import_list_styles'] . implode(', ', $header['styles']) . '<br />';
273 $str .= '<br />' . str_replace('{NUM}', count($list_data), $lang['xs_import_list_files']) . '<br />';
274 $str .= '<table border="0" cellspacing="0" cellpadding="1" align="left">';
275 foreach($list_data as $var => $value)
277 $str .= '<tr><td>' . htmlspecialchars($value['filename']) . '</td><td>';
278 if($value['size'] > 0)
280 $ext = strtolower(substr($var, strlen($var) - 4));
281 if(xs_in_array($ext, $text_types) || xs_in_array($ext, $img_types))
283 $str .= '[<a href="' . append_sid('xs_import.' . $phpEx . '?list=1&import=' . urlencode($filename) . '&get_file=' . urlencode($var)) . '">' . $lang['xs_import_view_lc'] . '</a>] ';
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>] ';
287 $str .= str_replace('{NUM}', $value['size'], $lang['xs_import_file_size']) . '</td></tr>';
291 xs_message($lang['Information'], $str);
297 // Generate actions list
300 // chdir to template directory
302 'command' => 'chdir',
305 // create directory with template name
307 'command' => 'mkdir',
308 'dir' => $header['template'],
313 'command' => 'chdir',
314 'dir' => $header['template']
316 // create all directories and upload all files
319 'list' => generate_actions_dirs()
323 $res = ftp_myexec($actions);
325 echo "\$actions dump:\n\n";
327 echo "\n\n\$ftp_log dump:\n\n";
330 // remove temporary files
331 for($i=0; $i<count($items); $i++)
333 if(!empty($items[$i]['tmp']))
335 @unlink($items[$i]['tmp']);
340 if(defined('XS_CLONING'))
342 @unlink($tmp_filename);
344 xs_error($ftp_error . '<br /><br />' . $lang['xs_import_back']);
349 // Check if we need to install style
351 $total = intval($HTTP_POST_VARS['total']);
354 for($i=0; $i<$total; $i++)
356 $tmp = empty($HTTP_POST_VARS['import_install_'.$i]) ? 0 : 1;
359 $set_default = empty($HTTP_POST_VARS['import_default_'.$i]) ? 0 : 1;
360 $tmp_name = $header['styles'][$i];
363 $install[] = $tmp_name;
366 $default_name = $tmp_name;
373 if(defined('XS_CLONING'))
375 @unlink($tmp_filename);
377 xs_message($lang['Information'], $lang['xs_import_uploaded'] . '<br /><br />' . $lang['xs_import_back']);
380 // Get list of installed styles
382 $tpl = $header['template'];
383 $sql = "SELECT themes_id, style_name FROM " . THEMES_TABLE . " WHERE template_name='" . xs_sql($tpl) . "'";
384 if(!$result = $db->sql_query($sql))
386 if(defined('XS_CLONING'))
388 @unlink($tmp_filename);
390 xs_error($lang['xs_import_notinstall'] . '<br /><br />' . $lang['xs_import_back']);
392 $style_rowset = $db->sql_fetchrowset($result);
393 // run theme_info.cfg
394 $data = xs_get_themeinfo($tpl);
397 if(defined('XS_CLONING'))
399 @unlink($tmp_filename);
401 xs_error($lang['xs_import_notinstall2'] . '<br /><br />' . $lang['xs_import_back']);
405 for($i=0; $i<count($install); $i++)
407 $style_name = $install[$i];
409 // find entry in theme_info.cfg
410 for($j=0; $j<count($data); $j++)
412 if($data[$j]['style_name'] === $style_name)
414 $style_data = $data[$j];
417 // check if already installed
419 for($j=0; $j<count($style_rowset); $j++)
421 if($style_rowset[$j]['style_name'] === $style_name)
423 $installed = $style_rowset[$j]['themes_id'];
427 if(empty($style_data['style_name']) || empty($style_data['template_name']))
429 if(defined('XS_CLONING'))
431 @unlink($tmp_filename);
433 xs_error(str_replace('{STYLE}', $style_name, $lang['xs_import_notinstall3']) . '<br /><br />' . $lang['xs_import_back']);
439 foreach($style_data as $var => $value)
445 $sql .= xs_sql($var) . " = '" . xs_sql($value) . "'";
447 $sql = "UPDATE " . THEMES_TABLE . " SET " . $sql . " WHERE themes_id = '{$installed}'";
452 $sql = "SELECT MAX(themes_id) AS total FROM " . THEMES_TABLE;
453 if ( !($result = $db->sql_query($sql)) )
455 if(defined('XS_CLONING'))
457 @unlink($tmp_filename);
459 xs_error($lang['xs_import_notinstall4'] . '<br /><br />' . $lang['xs_import_back']);
461 if ( !($row = $db->sql_fetchrow($result)) )
463 if(defined('XS_CLONING'))
465 @unlink($tmp_filename);
467 xs_error($lang['xs_import_notinstall4'] . '<br /><br />' . $lang['xs_import_back']);
469 $installed = $row['total'] + 1;
470 $style_data['themes_id'] = $installed;
472 foreach($style_data as $var => $value)
479 $sql1 .= xs_sql($var);
480 $sql2 .= "'" . str_replace("\'", "''", $value) . "'";
482 $sql = "INSERT INTO " . THEMES_TABLE . " (" . $sql1 . ") VALUES (" . $sql2 . ")";
484 if ( !($result = $db->sql_query($sql)) )
486 if(defined('XS_CLONING'))
488 @unlink($tmp_filename);
490 xs_error($lang['xs_import_notinstall5'] . '<br /><br />' . $lang['xs_import_back']);
492 if($default_name === $style_name)
494 $sql = "UPDATE " . CONFIG_TABLE . " SET config_value='{$installed}' WHERE config_name='default_style'";
495 $board_config['default_style'] = $installed;
496 $db->sql_query($sql);
499 if(defined('XS_CLONING'))
501 @unlink($tmp_filename);
503 if(count($install) && defined('XS_MODS_CATEGORY_HIERARCHY'))
507 xs_message($lang['Information'], $lang['xs_import_installed'] . '<br /><br />' . $lang['xs_import_back']);