PageRenderTime 26ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/adm/xs_clone.php

https://github.com/igorw-forks/icy_phoenix
PHP | 348 lines | 297 code | 15 blank | 36 comment | 51 complexity | 66b8401a8a4cded6b660f0cbe01cdef6 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Extra credits for this file
  13. * Vjacheslav Trushkin (http://www.stsoftware.biz)
  14. *
  15. */
  16. define('IN_ICYPHOENIX', true);
  17. if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './../');
  18. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  19. $no_page_header = true;
  20. require('pagestart.' . PHP_EXT);
  21. define('IN_XS', true);
  22. include_once('xs_include.' . PHP_EXT);
  23. $template->assign_block_vars('nav_left',array('ITEM' => '&raquo; <a href="' . append_sid('xs_clone.' . PHP_EXT) . '">' . $lang['xs_clone_styles'] . '</a>'));
  24. $lang['xs_clone_back'] = str_replace('{URL}', append_sid('xs_clone.' . PHP_EXT), $lang['xs_clone_back']);
  25. // Check required functions
  26. if(!@function_exists('gzcompress'))
  27. {
  28. xs_error($lang['xs_import_nogzip']);
  29. }
  30. // clone style
  31. if(!empty($_POST['clone_style']) && !defined('DEMO_MODE'))
  32. {
  33. $style = intval($_POST['clone_style']);
  34. $new_name = stripslashes($_POST['clone_name']);
  35. // get theme data
  36. $sql = "SELECT * FROM " . THEMES_TABLE . " WHERE themes_id='{$style}'";
  37. $db->sql_return_on_error(true);
  38. $result = $db->sql_query($sql);
  39. $db->sql_return_on_error(false);
  40. if(!$result)
  41. {
  42. xs_error($lang['xs_no_style_info'] . '<br /><br />' . $lang['xs_clone_back'], __LINE__, __FILE__);
  43. }
  44. $theme = $db->sql_fetchrow($result);
  45. if(empty($theme['themes_id']))
  46. {
  47. xs_error($lang['xs_no_themes'] . '<br /><br />' . $lang['xs_clone_back']);
  48. }
  49. if($theme['style_name'] === stripslashes($new_name))
  50. {
  51. xs_error($lang['xs_clone_taken'] . '<br /><br />' . $lang['xs_clone_back']);
  52. }
  53. // check for clone
  54. $sql = "SELECT themes_id FROM " . THEMES_TABLE . " WHERE style_name = '" . $db->sql_escape($new_name) . "'";
  55. $db->sql_return_on_error(true);
  56. $result = $db->sql_query($sql);
  57. $db->sql_return_on_error(false);
  58. if(!$result)
  59. {
  60. xs_error($lang['xs_no_theme_data'] . '<br /><br />' . $lang['xs_clone_back'], __LINE__, __FILE__);
  61. }
  62. $row = $db->sql_fetchrow($result);
  63. if(!empty($row['themes_id']))
  64. {
  65. xs_error($lang['xs_clone_taken'] . '<br /><br />' . $lang['xs_clone_back']);
  66. }
  67. // clone it
  68. $vars = array('style_name');
  69. $values = array($db->sql_escape($new_name));
  70. foreach($theme as $var => $value)
  71. {
  72. if(!is_integer($var) && $var !== 'style_name' && $var !== 'themes_id')
  73. {
  74. $vars[] = $var;
  75. $values[] = $db->sql_escape($value);
  76. }
  77. }
  78. $sql = "INSERT INTO " . THEMES_TABLE . " (" . implode(', ', $vars) . ") VALUES ('" . implode("','", $values) . "')";
  79. $db->sql_return_on_error(true);
  80. $result = $db->sql_query($sql);
  81. $db->sql_return_on_error(false);
  82. if(!$result)
  83. {
  84. xs_error($lang['xs_error_new_row'] . '<br /><br />' . $lang['xs_clone_back'], __LINE__, __FILE__);
  85. }
  86. xs_message($lang['Information'], $lang['xs_theme_cloned'] . '<br /><br />' . $lang['xs_clone_back']);
  87. }
  88. // clone template
  89. if(!empty($_POST['clone_tpl']) && !defined('DEMO_MODE'))
  90. {
  91. $old_name = xs_tpl_name($_POST['clone_tpl']);
  92. $new_name = xs_tpl_name($_POST['clone_style_name']);
  93. if(empty($new_name) || $new_name === $old_name)
  94. {
  95. xs_error($lang['xs_invalid_style_name'] . '<br /><br />' . $lang['xs_clone_back']);
  96. }
  97. // check if template exists
  98. if(@file_exists('../templates/'.$new_name))
  99. {
  100. xs_error($lang['xs_clone_style_exists'] . '<br /><br />' . $lang['xs_clone_back']);
  101. }
  102. // check variables
  103. $total = intval($_POST['total']);
  104. $vars = array('clone_tpl', 'clone_style_name', 'total');
  105. $count = 0;
  106. $list = array();
  107. for($i = 0; $i<$total; $i++)
  108. {
  109. $vars[] = 'clone_style_id_'.$i;
  110. $vars[] = 'clone_style_'.$i;
  111. $vars[] = 'clone_style_name_'.$i;
  112. if(!empty($_POST['clone_style_'.$i]) && !empty($_POST['clone_style_name_'.$i]))
  113. {
  114. // prepare for export
  115. $list[] = intval($_POST['clone_style_id_' . $i]);
  116. $_POST['export_style_' . $i] = $_POST['clone_style_' . $i];
  117. $_POST['export_style_id_' . $i] = $_POST['clone_style_id_' . $i];
  118. $_POST['export_style_name_' . $i] = $_POST['clone_style_name_' . $i];
  119. // prepare for import
  120. $_POST['import_install_' . $count] = '1';
  121. $count++;
  122. }
  123. }
  124. if(!$count)
  125. {
  126. xs_error($lang['xs_clone_no_select'] . '<br /><br />' . $lang['xs_clone_back']);
  127. }
  128. $request = array();
  129. for($i = 0; $i < sizeof($vars); $i++)
  130. {
  131. $request[$vars[$i]] = stripslashes($_POST[$vars[$i]]);
  132. }
  133. // get ftp configuration
  134. $write_local = false;
  135. if(!get_ftp_config(append_sid('xs_clone.' . PHP_EXT), $request, true))
  136. {
  137. xs_exit();
  138. }
  139. xs_ftp_connect(append_sid('xs_clone.' . PHP_EXT), $request, true);
  140. if($ftp === XS_FTP_LOCAL)
  141. {
  142. $write_local = true;
  143. $write_local_dir = '../templates/';
  144. }
  145. // prepare variables for export
  146. $export = $old_name;
  147. $exportas = $new_name;
  148. // Generate theme_info.cfg
  149. $sql = "SELECT * FROM " . THEMES_TABLE . " WHERE template_name = '$export' AND themes_id IN (" . implode(', ', $list) . ")";
  150. $db->sql_return_on_error(true);
  151. $result = $db->sql_query($sql);
  152. $db->sql_return_on_error(false);
  153. if(!$result)
  154. {
  155. xs_error($lang['xs_no_theme_data'] . $lang['xs_clone_back']);
  156. }
  157. $theme_rowset = $db->sql_fetchrowset($result);
  158. if(sizeof($theme_rowset) == 0)
  159. {
  160. xs_error($lang['xs_no_themes'] . '<br /><br />' . $lang['xs_clone_back']);
  161. }
  162. $theme_data = xs_generate_themeinfo($theme_rowset, $export, $exportas, $total);
  163. // prepare to pack
  164. $pack_error = '';
  165. $pack_list = array();
  166. $pack_replace = array('./theme_info.cfg' => $theme_data);
  167. // pack style
  168. for($i = 0; $i < sizeof($theme_rowset); $i++)
  169. {
  170. $id = $theme_rowset[$i]['themes_id'];
  171. $theme_name = $theme_rowset[$i]['style_name'];
  172. for($j=0; $j<$total; $j++)
  173. {
  174. if(!empty($_POST['export_style_name_' . $j]) && $_POST['export_style_id_' . $j] == $id)
  175. {
  176. $theme_name = stripslashes($_POST['export_style_name_'.$j]);
  177. }
  178. }
  179. $theme_rowset[$i]['style_name'] = $theme_name;
  180. }
  181. $data = pack_style($export, $exportas, $theme_rowset, '');
  182. // check errors
  183. if($pack_error)
  184. {
  185. xs_error(str_replace('{TPL}', $export, $lang['xs_export_error']) . $pack_error . '<br /><br />' . $lang['xs_clone_back']);
  186. }
  187. if(!$data)
  188. {
  189. xs_error(str_replace('{TPL}', $export, $lang['xs_export_error2']) . '<br /><br />' . $lang['xs_clone_back']);
  190. }
  191. // save as file
  192. $filename = 'clone_' . time() . '.tmp';
  193. $tmp_filename = XS_TEMP_DIR . $filename;
  194. $f = @fopen($tmp_filename, 'wb');
  195. if(!$f)
  196. {
  197. xs_error(str_replace('{FILE}', $tpl_filename, $lang['xs_error_cannot_create_tmp']) . '<br /><br />' . $lang['xs_clone_back']);
  198. }
  199. fwrite($f, $data);
  200. fclose($f);
  201. // prepare import variables
  202. $total = $count;
  203. $_POST['total'] = $count;
  204. $list_only = false;
  205. $get_file = '';
  206. define('XS_CLONING', true);
  207. $lang['xs_import_back'] = $lang['xs_clone_back'];
  208. include('xs_include_import.' . PHP_EXT);
  209. include('xs_include_import2.' . PHP_EXT);
  210. }
  211. // clone style menu
  212. if(!empty($_GET['clone']))
  213. {
  214. $style = stripslashes($_GET['clone']);
  215. $sql = "SELECT themes_id, style_name FROM " . THEMES_TABLE . " WHERE template_name = '" . $db->sql_escape($style) . "' ORDER BY style_name ASC";
  216. $db->sql_return_on_error(true);
  217. $result = $db->sql_query($sql);
  218. $db->sql_return_on_error(false);
  219. if(!$result)
  220. {
  221. xs_error($lang['xs_no_theme_data'] . '<br /><br />' . $lang['xs_clone_back'], __LINE__, __FILE__);
  222. }
  223. $theme_rowset = $db->sql_fetchrowset($result);
  224. if(sizeof($theme_rowset) == 0)
  225. {
  226. xs_error($lang['xs_no_themes'] . '<br /><br />' . $lang['xs_clone_back']);
  227. }
  228. $template->set_filenames(array('body' => XS_TPL_PATH . 'clone2.tpl'));
  229. // clone template
  230. $template->assign_vars(array(
  231. 'FORM_ACTION' => append_sid('xs_clone.' . PHP_EXT),
  232. 'CLONE_TEMPLATE' => htmlspecialchars($style),
  233. 'STYLE_ID' => $theme_rowset[0]['themes_id'],
  234. 'STYLE_NAME' => htmlspecialchars($theme_rowset[0]['style_name']),
  235. 'TOTAL' => sizeof($theme_rowset),
  236. 'L_CLONE_STYLE3' => str_replace('{STYLE}', htmlspecialchars($style), $lang['xs_clone_style3'])
  237. )
  238. );
  239. // clone styles
  240. for($i = 0; $i < sizeof($theme_rowset); $i++)
  241. {
  242. $template->assign_block_vars('styles', array(
  243. 'ID' => $theme_rowset[$i]['themes_id'],
  244. 'TPL' => htmlspecialchars($theme_rowset[$i]['template_name']),
  245. 'STYLE' => htmlspecialchars($theme_rowset[$i]['style_name']),
  246. 'L_CLONE' => str_replace('{STYLE}', htmlspecialchars($theme_rowset[$i]['style_name']), $lang['xs_clone_style2'])
  247. )
  248. );
  249. }
  250. if(sizeof($theme_rowset) == 1)
  251. {
  252. $template->assign_block_vars('switch_select_nostyle', array());
  253. if($theme_rowset[0]['style_name'] === $style)
  254. {
  255. $template->assign_block_vars('switch_onchange', array());
  256. }
  257. }
  258. else
  259. {
  260. $template->assign_block_vars('switch_select_style', array());
  261. for($i = 0; $i < sizeof($theme_rowset); $i++)
  262. {
  263. $template->assign_block_vars('switch_select_style.style', array(
  264. 'NUM' => $i,
  265. 'ID' => $theme_rowset[$i]['themes_id'],
  266. 'NAME' => htmlspecialchars($theme_rowset[$i]['style_name'])
  267. )
  268. );
  269. }
  270. }
  271. $template->pparse('body');
  272. xs_exit();
  273. }
  274. // get list of installed styles
  275. $sql = 'SELECT themes_id, template_name, style_name FROM ' . THEMES_TABLE . ' ORDER BY template_name';
  276. $db->sql_return_on_error(true);
  277. $result = $db->sql_query($sql);
  278. $db->sql_return_on_error(false);
  279. if(!$result)
  280. {
  281. xs_error($lang['xs_no_style_info'], __LINE__, __FILE__);
  282. }
  283. $style_rowset = $db->sql_fetchrowset($result);
  284. $prev_id = -1;
  285. $prev_tpl = '';
  286. $style_names = array();
  287. $j = 0;
  288. for($i = 0; $i < sizeof($style_rowset); $i++)
  289. {
  290. $item = $style_rowset[$i];
  291. if($item['template_name'] === $prev_tpl)
  292. {
  293. $style_names[] = htmlspecialchars($item['style_name']);
  294. }
  295. else
  296. {
  297. if($prev_id > 0)
  298. {
  299. $str = implode('<br />', $style_names);
  300. $str2 = urlencode($prev_tpl);
  301. $row_class = $xs_row_class[$j % 2];
  302. $j++;
  303. $template->assign_block_vars('styles', array(
  304. 'ROW_CLASS' => $row_class,
  305. 'TPL' => $prev_tpl,
  306. 'STYLES' => $str,
  307. 'U_CLONE' => 'xs_clone.' . PHP_EXT . "?clone={$str2}&amp;sid={$userdata['session_id']}",
  308. )
  309. );
  310. }
  311. $prev_id = $item['themes_id'];
  312. $prev_tpl = $item['template_name'];
  313. $style_names = array(htmlspecialchars($item['style_name']));
  314. }
  315. }
  316. if($prev_id > 0)
  317. {
  318. $str = implode('<br />', $style_names);
  319. $str2 = urlencode($prev_tpl);
  320. $row_class = $xs_row_class[$j % 2];
  321. $j++;
  322. $template->assign_block_vars('styles', array(
  323. 'ROW_CLASS' => $row_class,
  324. 'TPL' => $prev_tpl,
  325. 'STYLES' => $str,
  326. 'U_CLONE' => 'xs_clone.' . PHP_EXT . "?clone={$str2}&amp;sid={$userdata['session_id']}",
  327. )
  328. );
  329. }
  330. $template->set_filenames(array('body' => XS_TPL_PATH . 'clone.tpl'));
  331. $template->pparse('body');
  332. xs_exit();
  333. ?>