]> git.vanrenterghem.biz Git - www.vanrenterghem.biz.git/blob - phpBB2_old/admin/xs_include_import2.php
Baseline
[www.vanrenterghem.biz.git] / phpBB2_old / admin / xs_include_import2.php
1 <?php
3 /***************************************************************************
4  *                           xs_include_import2.php
5  *                           ----------------------
6  *   copyright            : (C) 2003, 2004 CyberAlien
7  *   support              : http://www.phpbbstyles.com
8  *
9  *   version              : 2.0.1
10  *
11  *   file revision        : 21
12  *   project revision     : 51
13  *   last modified        : 25 Aug 2004  11:51:47
14  *
15  ***************************************************************************/
17 /***************************************************************************
18  *
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.
23  *
24  ***************************************************************************/
26 if (!defined('IN_PHPBB') || !defined('IN_XS'))
27 {
28         die("Hacking attempt");
29 }
31 /*
33 Import style.
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
45 */
47 if(empty($list_only))
48 {
49         $list_only = false;
50 }
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);
61 if($header === false)
62 {
63         if(defined('XS_CLONING'))
64         {
65                 @unlink($tmp_filename);
66         }
67         xs_error($lang['xs_style_header_error_reason'] . $xs_header_error . '<br /><br />' . $lang['xs_import_back']);
68 }
69 if($header['filesize'] != filesize($file))
70 {
71         if(defined('XS_CLONING'))
72         {
73                 @unlink($tmp_filename);
74         }
75         xs_error($lang['xs_style_header_error_incomplete'] . '<br /><br />' . $lang['xs_import_back']);
76 }
77 $f = @fopen($file, 'rb');
78 if(!$f)
79 {
80         if(defined('XS_CLONING'))
81         {
82                 @unlink($tmp_filename);
83         }
84         xs_error($lang['xs_error_cannot_open'] . '<br /><br />' . $lang['xs_import_back']);
85 }
86 if($write_local)
87 {
88         $write_local_dir .= $header['template'] . '/';
89 }
90 fseek($f, $header['offset'], 0);
91 $str = fread($f, filesize($file) - $header['offset']);
92 fclose($f);
93 $str = @gzuncompress($str);
94 if($str === false || !strlen($str))
95 {
96         if(defined('XS_CLONING'))
97         {
98                 @unlink($tmp_filename);
99         }
100         xs_error($lang['xs_error_decompress_style'] . '<br /><br />' . $lang['xs_import_back']);
102 //
103 // unpack tar file
104 //
105 $pos = 0;
106 $tmp_name = XS_TEMP_DIR . 'file_' . $filename . '_%02d.tmp';
107 $tmp_count = 0;
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));
115         $pos += 512;
116         $data['filename'] = trim($data['prefix']) . trim($data['filename']);
117         if(substr($data['filename'], 0, 2) === './')
118         {
119                 $data['filename'] = substr($data['filename'], 2);
120         }
121         if($write_local)
122         {
123                 $save_filename = $write_local_dir . $data['filename'];
124         }
125         else
126         {
127                 $pos1 = strrpos($data['filename'], '/');
128                 if($pos1)
129                 {
130                         $data['dir'] = substr($data['filename'], 0, $pos1);
131                         $data['file'] = substr($data['filename'], $pos1 + 1);
132                 }
133                 else
134                 {
135                         $data['dir'] = '';
136                         $data['file'] = $data['filename'];
137                 }
138         }
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')
143         {
144                 $data['size'] = 0;
145                 if($write_local)
146                 {
147                         xs_create_dir($save_filename);
148                 }
149         }
150         $data['offset'] = $pos;
151         $contents = $data['size'] > 0 ? substr($str, $pos, $data['size']) : '';
152         $data['tmp'] = '';
153         // adding to list
154         $is_file = true;
155         if(intval($data['typeflag']) == 5)
156         {
157                 $is_file = false;
158                 if($data['filename'])
159                 {
160                         $dirs[] = $data['filename'];
161                 }
162         }
163         else
164         {
165                 if($data['filename'])
166                 {
167                         if(!$list_only)
168                         {
169                                 if($write_local)
170                                 {
171                                         $res = xs_write_file($save_filename, $contents);
172                                         if(!$res)
173                                         {
174                                                 if(defined('XS_CLONING'))
175                                                 {
176                                                         @unlink($tmp_filename);
177                                                 }
178                                                 xs_error(str_replace('{FILE}', $save_filename, $lang['xs_error_cannot_create_file']) . '<br /><br />' . $lang['xs_import_back']);
179                                         }
180                                 }
181                                 else
182                                 {
183                                         // write to temporary file
184                                         $tmp_count ++;                          
185                                         $data['tmp'] = sprintf($tmp_name, $tmp_count);
186                                         $f = @fopen($data['tmp'], 'wb');
187                                         if(!$f)
188                                         {
189                                                 if(defined('XS_CLONING'))
190                                                 {
191                                                         @unlink($tmp_filename);
192                                                 }
193                                                 xs_error(str_replace('{FILE}', $data['tmp'], $lang['xs_error_cannot_create_tmp']) . '<br /><br />' . $lang['xs_import_back']);
194                                         }
195                                         fwrite($f, $contents);
196                                         fclose($f);
197                                 }
198                         }
199                         elseif(!empty($get_file) && $get_file === $data['filename'])
200                         {
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))
205                                 {
206                                         // show as text
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>';
209                                         $str .= '</div>';
210                                         xs_message($lang['Information'], $str);
211                                 }
212                                 else
213                                 {
214                                         $do_download = false;
215                                         $content_type = '';
216                                         if(empty($HTTP_GET_VARS['get_content']))
217                                         {
218                                                 if($ext === '.gif')
219                                                 {
220                                                         $content_type = 'image/gif';
221                                                 }
222                                                 elseif($ext === '.jpg' || $ext === '.jpe' || $ext === 'jpeg')
223                                                 {
224                                                         $content_type = 'image/jpeg';
225                                                 }
226                                                 elseif($ext === '.png')
227                                                 {
228                                                         $content_type = 'image/png';
229                                                 }
230                                                 else
231                                                 {
232                                                         $do_download = true;
233                                                 }
234                                         }
235                                         else
236                                         {
237                                                 $do_download = true;
238                                         }
239                                         xs_download_file($do_download ? basename($f) : '', $contents, $content_type);
240                                         xs_exit();
241                                 }
242                         }
243                         else
244                         {
245                                 $list_data[$data['filename']] = $data;
246                         }
247                         $files[] = $data['filename'];
248                 }
249         }
250         if(empty($data['filename']) && $is_file)
251         {
252                 $pos = strlen($str);
253         }
254         else
255         {
256                 $pos += floor(($data['size'] + 511) / 512) * 512;
257                 if($is_file)
258                 {
259                         $items[] = $data;
260                 }
261         }
263 if($list_only)
265         // show list of files. used for debug.
266         $str = '<div align="left">';
267         // main data
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 />';
272         ksort($list_data);
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)
276         {
277                 $str .= '<tr><td>' . htmlspecialchars($value['filename']) . '</td><td>';
278                 if($value['size'] > 0)
279                 {
280                         $ext = strtolower(substr($var, strlen($var) - 4));
281                         if(xs_in_array($ext, $text_types) || xs_in_array($ext, $img_types))
282                         {
283                                 $str .= '[<a href="' . append_sid('xs_import.' . $phpEx . '?list=1&import=' . urlencode($filename) . '&get_file=' . urlencode($var)) . '">' . $lang['xs_import_view_lc'] . '</a>] ';
284                         }
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>] ';
286                 }
287                 $str .= str_replace('{NUM}', $value['size'], $lang['xs_import_file_size']) . '</td></tr>';
288         }
289         $str .= '</table>';
290         $str .= '</div>';
291         xs_message($lang['Information'], $str);
293 $str = '';
294 if(!$write_local)
296         //
297         // Generate actions list
298         //
299         $actions = array();
300         // chdir to template directory
301         $actions[] = array(
302                         'command'       => 'chdir',
303                         'dir'           => 'templates'
304                 );
305         // create directory with template name
306         $actions[] = array(
307                         'command'       => 'mkdir',
308                         'dir'           => $header['template'],
309                         'ignore'        => true
310                 );
311         // change directory
312         $actions[] = array(
313                         'command'       => 'chdir',
314                         'dir'           => $header['template']
315                 );
316         // create all directories and upload all files
317         $actions[] = array(
318                         'command'       => 'exec',
319                         'list'          => generate_actions_dirs()
320                 );
321         $ftp_log = array();
322         $ftp_error = '';
323         $res = ftp_myexec($actions);
324 /*      echo "<!--\n\n";
325         echo "\$actions dump:\n\n";
326         print_r($actions);
327         echo "\n\n\$ftp_log dump:\n\n";
328         print_r($ftp_log);
329         echo "\n\n -->"; */
330         // remove temporary files
331         for($i=0; $i<count($items); $i++)
332         {
333                 if(!empty($items[$i]['tmp']))
334                 {
335                         @unlink($items[$i]['tmp']);
336                 }
337         }
338         if(!$res)
339         {
340                 if(defined('XS_CLONING'))
341                 {
342                         @unlink($tmp_filename);
343                 }
344                 xs_error($ftp_error . '<br /><br />' . $lang['xs_import_back']);
345         }
348 //
349 // Check if we need to install style
350 //
351 $total = intval($HTTP_POST_VARS['total']);
352 $install = array();
353 $default_name = '';
354 for($i=0; $i<$total; $i++)
356         $tmp = empty($HTTP_POST_VARS['import_install_'.$i]) ? 0 : 1;
357         if($tmp)
358         {
359                 $set_default = empty($HTTP_POST_VARS['import_default_'.$i]) ? 0 : 1;
360                 $tmp_name = $header['styles'][$i];
361                 if($tmp_name)
362                 {
363                         $install[] = $tmp_name;
364                         if($set_default)
365                         {
366                                 $default_name = $tmp_name;
367                         }
368                 }
369         }
371 if(!count($install))
373         if(defined('XS_CLONING'))
374         {
375                 @unlink($tmp_filename);
376         }
377         xs_message($lang['Information'], $lang['xs_import_uploaded'] . '<br /><br />' . $lang['xs_import_back']);
379 //
380 // Get list of installed styles
381 //
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'))
387         {
388                 @unlink($tmp_filename);
389         }
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);
395 if(!@count($data))
397         if(defined('XS_CLONING'))
398         {
399                 @unlink($tmp_filename);
400         }
401         xs_error($lang['xs_import_notinstall2'] . '<br /><br />' . $lang['xs_import_back']);
403 // install styles
404 $default_id = 0;
405 for($i=0; $i<count($install); $i++)
407         $style_name = $install[$i];
408         $style_data = false;
409         // find entry in theme_info.cfg
410         for($j=0; $j<count($data); $j++)
411         {
412                 if($data[$j]['style_name'] === $style_name)
413                 {
414                         $style_data = $data[$j];
415                 }
416         }
417         // check if already installed
418         $installed = 0;
419         for($j=0; $j<count($style_rowset); $j++)
420         {
421                 if($style_rowset[$j]['style_name'] === $style_name)
422                 {
423                         $installed = $style_rowset[$j]['themes_id'];
424                 }
425         }
426         // install/update
427         if(empty($style_data['style_name']) || empty($style_data['template_name']))
428         {
429                 if(defined('XS_CLONING'))
430                 {
431                         @unlink($tmp_filename);
432                 }
433                 xs_error(str_replace('{STYLE}', $style_name, $lang['xs_import_notinstall3']) . '<br /><br />' . $lang['xs_import_back']);
434         }
435         if($installed)
436         {
437                 // update
438                 $sql = '';
439                 foreach($style_data as $var => $value)
440                 {
441                         if($sql)
442                         {
443                                 $sql .= ', ';
444                         }
445                         $sql .= xs_sql($var) . " = '" . xs_sql($value) . "'";
446                 }
447                 $sql = "UPDATE " . THEMES_TABLE . " SET " . $sql . " WHERE themes_id = '{$installed}'";
448         }
449         else
450         {
451                 // install
452                 $sql = "SELECT MAX(themes_id) AS total FROM " . THEMES_TABLE;
453                 if ( !($result = $db->sql_query($sql)) )
454                 {
455                         if(defined('XS_CLONING'))
456                         {
457                                 @unlink($tmp_filename);
458                         }
459                         xs_error($lang['xs_import_notinstall4'] . '<br /><br />' . $lang['xs_import_back']);
460                 }
461                 if ( !($row = $db->sql_fetchrow($result)) )
462                 {
463                         if(defined('XS_CLONING'))
464                         {
465                                 @unlink($tmp_filename);
466                         }
467                         xs_error($lang['xs_import_notinstall4'] . '<br /><br />' . $lang['xs_import_back']);
468                 }
469                 $installed = $row['total'] + 1;
470                 $style_data['themes_id'] = $installed;
471                 $sql1 = $sql2 = '';
472                 foreach($style_data as $var => $value)
473                 {
474                         if($sql1)
475                         {
476                                 $sql1 .= ', ';
477                                 $sql2 .= ', ';
478                         }
479                         $sql1 .= xs_sql($var);
480                         $sql2 .= "'" . str_replace("\'", "''", $value) . "'";
481                 }
482                 $sql = "INSERT INTO " . THEMES_TABLE . " (" . $sql1 . ") VALUES (" . $sql2 . ")";
483         }
484         if ( !($result = $db->sql_query($sql)) )
485         {
486                 if(defined('XS_CLONING'))
487                 {
488                         @unlink($tmp_filename);
489                 }
490                 xs_error($lang['xs_import_notinstall5'] . '<br /><br />' . $lang['xs_import_back']);
491         }
492         if($default_name === $style_name)
493         {
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);
497         }
499 if(defined('XS_CLONING'))
501         @unlink($tmp_filename);
503 if(count($install) && defined('XS_MODS_CATEGORY_HIERARCHY'))
505         cache_themes();
507 xs_message($lang['Information'], $lang['xs_import_installed'] . '<br /><br />' . $lang['xs_import_back']);
509 ?>