3 /***************************************************************************
\r
4 * xs_include_import2.php
\r
5 * ----------------------
\r
6 * copyright : (C) 2003 - 2005 CyberAlien
\r
7 * support : http://www.phpbbstyles.com
\r
11 * file revision : 75
\r
12 * project revision : 78
\r
13 * last modified : 05 Dec 2005 13:54:54
\r
15 ***************************************************************************/
\r
17 /***************************************************************************
\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
24 ***************************************************************************/
\r
26 if (!defined('IN_PHPBB') || !defined('IN_XS'))
\r
28 die("Hacking attempt");
\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
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
47 if(empty($list_only))
\r
52 $lang['xs_import_back'] = str_replace('{URL}', append_sid('xs_import.'.$phpEx), $lang['xs_import_back']);
\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
59 $file = XS_TEMP_DIR . xs_fix_dir($filename);
\r
60 $header = xs_get_style_header($file);
\r
61 if($header === false)
\r
63 if(defined('XS_CLONING'))
\r
65 @unlink($tmp_filename);
\r
67 xs_error($lang['xs_style_header_error_reason'] . $xs_header_error . '<br /><br />' . $lang['xs_import_back']);
\r
69 if($header['filesize'] != filesize($file))
\r
71 if(defined('XS_CLONING'))
\r
73 @unlink($tmp_filename);
\r
75 xs_error($lang['xs_style_header_error_incomplete'] . '<br /><br />' . $lang['xs_import_back']);
\r
77 $f = @fopen($file, 'rb');
\r
80 if(defined('XS_CLONING'))
\r
82 @unlink($tmp_filename);
\r
84 xs_error($lang['xs_error_cannot_open'] . '<br /><br />' . $lang['xs_import_back']);
\r
88 $write_local_dir .= $header['template'] . '/';
\r
90 fseek($f, $header['offset'], 0);
\r
91 $str = fread($f, filesize($file) - $header['offset']);
\r
93 $str = @gzuncompress($str);
\r
94 if($str === false || !strlen($str))
\r
96 if(defined('XS_CLONING'))
\r
98 @unlink($tmp_filename);
\r
100 xs_error($lang['xs_error_decompress_style'] . '<br /><br />' . $lang['xs_import_back']);
\r
106 $tmp_name = XS_TEMP_DIR . 'file_' . $filename . '_%02d.tmp';
\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
114 $data = unpack(TAR_HEADER_UNPACK, substr($str, $pos, 512));
\r
116 $data['filename'] = trim($data['prefix']) . trim($data['filename']);
\r
117 if(substr($data['filename'], 0, 2) === './')
\r
119 $data['filename'] = substr($data['filename'], 2);
\r
123 $save_filename = $write_local_dir . $data['filename'];
\r
127 $pos1 = strrpos($data['filename'], '/');
\r
130 $data['dir'] = substr($data['filename'], 0, $pos1);
\r
131 $data['file'] = substr($data['filename'], $pos1 + 1);
\r
136 $data['file'] = $data['filename'];
\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
147 xs_create_dir($save_filename);
\r
150 $data['offset'] = $pos;
\r
151 $contents = $data['size'] > 0 ? substr($str, $pos, $data['size']) : '';
\r
155 if(intval($data['typeflag']) == 5)
\r
158 if($data['filename'])
\r
160 $dirs[] = $data['filename'];
\r
165 if($data['filename'])
\r
171 $res = xs_write_file($save_filename, $contents);
\r
174 if(defined('XS_CLONING'))
\r
176 @unlink($tmp_filename);
\r
178 xs_error(str_replace('{FILE}', $save_filename, $lang['xs_error_cannot_create_file']) . '<br /><br />' . $lang['xs_import_back']);
\r
183 // write to temporary file
\r
185 $data['tmp'] = sprintf($tmp_name, $tmp_count);
\r
186 $f = @fopen($data['tmp'], 'wb');
\r
189 if(defined('XS_CLONING'))
\r
191 @unlink($tmp_filename);
\r
193 xs_error(str_replace('{FILE}', $data['tmp'], $lang['xs_error_cannot_create_tmp']) . '<br /><br />' . $lang['xs_import_back']);
\r
195 fwrite($f, $contents);
\r
199 elseif(!empty($get_file) && $get_file === $data['filename'])
\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
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
210 xs_message($lang['Information'], $str);
\r
214 $do_download = false;
\r
215 $content_type = '';
\r
216 if(empty($HTTP_GET_VARS['get_content']))
\r
218 if($ext === '.gif')
\r
220 $content_type = 'image/gif';
\r
222 elseif($ext === '.jpg' || $ext === '.jpe' || $ext === 'jpeg')
\r
224 $content_type = 'image/jpeg';
\r
226 elseif($ext === '.png')
\r
228 $content_type = 'image/png';
\r
232 $do_download = true;
\r
237 $do_download = true;
\r
239 xs_download_file($do_download ? basename($f) : '', $contents, $content_type);
\r
245 $list_data[$data['filename']] = $data;
\r
247 $files[] = $data['filename'];
\r
250 if(empty($data['filename']) && $is_file)
\r
252 $pos = strlen($str);
\r
256 $pos += floor(($data['size'] + 511) / 512) * 512;
\r
265 // show list of files. used for debug.
\r
266 $str = '<div align="left">';
\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
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
277 $str .= '<tr><td>' . htmlspecialchars($value['filename']) . '</td><td>';
\r
278 if($value['size'] > 0)
\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
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
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
287 $str .= str_replace('{NUM}', $value['size'], $lang['xs_import_file_size']) . '</td></tr>';
\r
289 $str .= '</table>';
\r
291 xs_message($lang['Information'], $str);
\r
297 // Generate actions list
\r
299 $actions = array();
\r
300 // chdir to template directory
\r
301 $actions[] = array(
\r
302 'command' => 'chdir',
\r
303 'dir' => 'templates'
\r
305 // create directory with template name
\r
306 $actions[] = array(
\r
307 'command' => 'mkdir',
\r
308 'dir' => $header['template'],
\r
311 // change directory
\r
312 $actions[] = array(
\r
313 'command' => 'chdir',
\r
314 'dir' => $header['template']
\r
316 // create all directories and upload all files
\r
317 $actions[] = array(
\r
318 'command' => 'exec',
\r
319 'list' => generate_actions_dirs()
\r
321 $ftp_log = array();
\r
323 $res = ftp_myexec($actions);
\r
324 /* echo "<!--\n\n";
\r
325 echo "\$actions dump:\n\n";
\r
327 echo "\n\n\$ftp_log dump:\n\n";
\r
329 echo "\n\n -->"; */
\r
330 // remove temporary files
\r
331 for($i=0; $i<count($items); $i++)
\r
333 if(!empty($items[$i]['tmp']))
\r
335 @unlink($items[$i]['tmp']);
\r
340 if(defined('XS_CLONING'))
\r
342 @unlink($tmp_filename);
\r
344 xs_error($ftp_error . '<br /><br />' . $lang['xs_import_back']);
\r
349 // Check if we need to install style
\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
357 $tmp = empty($HTTP_POST_VARS['import_install_'.$i]) ? 0 : 1;
\r
360 $set_default = $default == $i ? 1 : 0;
\r
361 $tmp_name = $header['styles'][$i];
\r
364 $install[] = $tmp_name;
\r
367 $default_name = $tmp_name;
\r
372 if(!count($install))
\r
374 if(defined('XS_CLONING'))
\r
376 @unlink($tmp_filename);
\r
378 xs_message($lang['Information'], $lang['xs_import_uploaded'] . '<br /><br />' . $lang['xs_import_back']);
\r
381 // Get list of installed styles
\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
387 if(defined('XS_CLONING'))
\r
389 @unlink($tmp_filename);
\r
391 xs_error($lang['xs_import_notinstall'] . '<br /><br />' . $lang['xs_import_back']);
\r
393 $style_rowset = $db->sql_fetchrowset($result);
\r
394 // run theme_info.cfg
\r
395 $data = xs_get_themeinfo($tpl);
\r
398 if(defined('XS_CLONING'))
\r
400 @unlink($tmp_filename);
\r
402 xs_error($lang['xs_import_notinstall2'] . '<br /><br />' . $lang['xs_import_back']);
\r
406 for($i=0; $i<count($install); $i++)
\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
413 if($data[$j]['style_name'] === $style_name)
\r
415 $style_data = $data[$j];
\r
418 // check if already installed
\r
420 for($j=0; $j<count($style_rowset); $j++)
\r
422 if($style_rowset[$j]['style_name'] === $style_name)
\r
424 $installed = $style_rowset[$j]['themes_id'];
\r
428 if(empty($style_data['style_name']) || empty($style_data['template_name']))
\r
430 if(defined('XS_CLONING'))
\r
432 @unlink($tmp_filename);
\r
434 xs_error(str_replace('{STYLE}', $style_name, $lang['xs_import_notinstall3']) . '<br /><br />' . $lang['xs_import_back']);
\r
440 foreach($style_data as $var => $value)
\r
446 $sql .= xs_sql($var) . " = '" . xs_sql($value) . "'";
\r
448 $sql = "UPDATE " . THEMES_TABLE . " SET " . $sql . " WHERE themes_id = '{$installed}'";
\r
453 $sql = "SELECT MAX(themes_id) AS total FROM " . THEMES_TABLE;
\r
454 if ( !($result = $db->sql_query($sql)) )
\r
456 if(defined('XS_CLONING'))
\r
458 @unlink($tmp_filename);
\r
460 xs_error($lang['xs_import_notinstall4'] . '<br /><br />' . $lang['xs_import_back']);
\r
462 if ( !($row = $db->sql_fetchrow($result)) )
\r
464 if(defined('XS_CLONING'))
\r
466 @unlink($tmp_filename);
\r
468 xs_error($lang['xs_import_notinstall4'] . '<br /><br />' . $lang['xs_import_back']);
\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
480 $sql1 .= xs_sql($var);
\r
481 $sql2 .= "'" . xs_sql($value) . "'";
\r
483 $sql = "INSERT INTO " . THEMES_TABLE . " (" . $sql1 . ") VALUES (" . $sql2 . ")";
\r
485 if ( !($result = $db->sql_query($sql)) )
\r
487 if(defined('XS_CLONING'))
\r
489 @unlink($tmp_filename);
\r
491 xs_error($lang['xs_import_notinstall5'] . '<br /><br />' . $lang['xs_import_back']);
\r
493 if($default_name === $style_name)
\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
500 if(defined('XS_CLONING'))
\r
502 @unlink($tmp_filename);
\r
504 if(count($install) && defined('XS_MODS_CATEGORY_HIERARCHY210'))
\r
506 // recache themes table
\r
507 if ( empty($themes) )
\r
509 $themes = new themes();
\r
511 if ( !empty($themes) )
\r
513 $themes->read(true);
\r
516 if(count($install) && defined('XS_MODS_CATEGORY_HIERARCHY'))
\r
520 xs_message($lang['Information'], $lang['xs_import_installed'] . '<br /><br />' . $lang['xs_import_back']);
\r