/sources/admin/Packages.php

https://github.com/Arantor/Elkarte · PHP · 2709 lines · 2036 code · 326 blank · 347 comment · 495 complexity · 9469e76cdb62de258aba71020cb11359 MD5 · raw file

Large files are truncated click here to view the full file

  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file is the main Package Manager.
  16. *
  17. */
  18. if (!defined('ELKARTE'))
  19. die('No access...');
  20. /**
  21. * This is the notoriously defunct package manager..... :/.
  22. */
  23. function Packages()
  24. {
  25. global $txt, $scripturl, $context;
  26. // @todo Remove this!
  27. if (isset($_GET['get']) || isset($_GET['pgdownload']))
  28. {
  29. require_once(ADMINDIR . '/PackageServers.php');
  30. return PackageServers();
  31. }
  32. isAllowedTo('admin_forum');
  33. // Load all the basic stuff.
  34. require_once(SUBSDIR . '/Package.subs.php');
  35. loadLanguage('Packages');
  36. loadTemplate('Packages', 'admin');
  37. $context['page_title'] = $txt['package'];
  38. // Delegation makes the world... that is, the package manager go 'round.
  39. $subActions = array(
  40. 'browse' => 'PackageBrowse',
  41. 'remove' => 'PackageRemove',
  42. 'list' => 'PackageList',
  43. 'ftptest' => 'PackageFTPTest',
  44. 'install' => 'PackageInstallTest',
  45. 'install2' => 'PackageInstall',
  46. 'uninstall' => 'PackageInstallTest',
  47. 'uninstall2' => 'PackageInstall',
  48. 'installed' => 'PackageBrowse',
  49. 'options' => 'PackageOptions',
  50. 'perms' => 'PackagePermissions',
  51. 'flush' => 'FlushInstall',
  52. 'examine' => 'ExamineFile',
  53. 'showoperations' => 'ViewOperations',
  54. );
  55. // Work out exactly who it is we are calling.
  56. if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
  57. $context['sub_action'] = $_REQUEST['sa'];
  58. else
  59. $context['sub_action'] = 'browse';
  60. // Set up some tabs...
  61. $context[$context['admin_menu_name']]['tab_data'] = array(
  62. 'title' => $txt['package_manager'],
  63. // @todo 'help' => 'registrations',
  64. 'description' => $txt['package_manager_desc'],
  65. 'tabs' => array(
  66. 'browse' => array(
  67. ),
  68. 'packageget' => array(
  69. 'description' => $txt['download_packages_desc'],
  70. ),
  71. 'installed' => array(
  72. 'description' => $txt['installed_packages_desc'],
  73. ),
  74. 'perms' => array(
  75. 'description' => $txt['package_file_perms_desc'],
  76. ),
  77. 'options' => array(
  78. 'description' => $txt['package_install_options_desc'],
  79. ),
  80. ),
  81. );
  82. // Call the function we're handing control to.
  83. $subActions[$context['sub_action']]();
  84. }
  85. /**
  86. * Test install a package.
  87. */
  88. function PackageInstallTest()
  89. {
  90. global $txt, $context, $scripturl, $modSettings, $smcFunc, $settings;
  91. // You have to specify a file!!
  92. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  93. redirectexit('action=admin;area=packages');
  94. $context['filename'] = preg_replace('~[\.]+~', '.', $_REQUEST['package']);
  95. // Do we have an existing id, for uninstalls and the like.
  96. $context['install_id'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 0;
  97. require_once(SUBSDIR . '/Package.subs.php');
  98. // Load up the package FTP information?
  99. create_chmod_control();
  100. // Make sure temp directory exists and is empty.
  101. if (file_exists(BOARDDIR . '/packages/temp'))
  102. deltree(BOARDDIR . '/packages/temp', false);
  103. if (!mktree(BOARDDIR . '/packages/temp', 0755))
  104. {
  105. deltree(BOARDDIR . '/packages/temp', false);
  106. if (!mktree(BOARDDIR . '/packages/temp', 0777))
  107. {
  108. deltree(BOARDDIR . '/packages/temp', false);
  109. create_chmod_control(array(BOARDDIR . '/packages/temp/delme.tmp'), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=' . $_REQUEST['sa'] . ';package=' . $_REQUEST['package'], 'crash_on_error' => true));
  110. deltree(BOARDDIR . '/packages/temp', false);
  111. if (!mktree(BOARDDIR . '/packages/temp', 0777))
  112. fatal_lang_error('package_cant_download', false);
  113. }
  114. }
  115. $context['uninstalling'] = $_REQUEST['sa'] == 'uninstall';
  116. // Change our last link tree item for more information on this Packages area.
  117. $context['linktree'][count($context['linktree']) - 1] = array(
  118. 'url' => $scripturl . '?action=admin;area=packages;sa=browse',
  119. 'name' => $context['uninstalling'] ? $txt['package_uninstall_actions'] : $txt['install_actions']
  120. );
  121. $context['page_title'] .= ' - ' . ($context['uninstalling'] ? $txt['package_uninstall_actions'] : $txt['install_actions']);
  122. $context['sub_template'] = 'view_package';
  123. if (!file_exists(BOARDDIR . '/packages/' . $context['filename']))
  124. {
  125. deltree(BOARDDIR . '/packages/temp');
  126. fatal_lang_error('package_no_file', false);
  127. }
  128. // Extract the files so we can get things like the readme, etc.
  129. if (is_file(BOARDDIR . '/packages/' . $context['filename']))
  130. {
  131. $context['extracted_files'] = read_tgz_file(BOARDDIR . '/packages/' . $context['filename'], BOARDDIR . '/packages/temp');
  132. if ($context['extracted_files'] && !file_exists(BOARDDIR . '/packages/temp/package-info.xml'))
  133. foreach ($context['extracted_files'] as $file)
  134. if (basename($file['filename']) == 'package-info.xml')
  135. {
  136. $context['base_path'] = dirname($file['filename']) . '/';
  137. break;
  138. }
  139. if (!isset($context['base_path']))
  140. $context['base_path'] = '';
  141. }
  142. elseif (is_dir(BOARDDIR . '/packages/' . $context['filename']))
  143. {
  144. copytree(BOARDDIR . '/packages/' . $context['filename'], BOARDDIR . '/packages/temp');
  145. $context['extracted_files'] = listtree(BOARDDIR . '/packages/temp');
  146. $context['base_path'] = '';
  147. }
  148. else
  149. fatal_lang_error('no_access', false);
  150. // Load up any custom themes we may want to install into...
  151. $request = $smcFunc['db_query']('', '
  152. SELECT id_theme, variable, value
  153. FROM {db_prefix}themes
  154. WHERE (id_theme = {int:default_theme} OR id_theme IN ({array_int:known_theme_list}))
  155. AND variable IN ({string:name}, {string:theme_dir})',
  156. array(
  157. 'known_theme_list' => explode(',', $modSettings['knownThemes']),
  158. 'default_theme' => 1,
  159. 'name' => 'name',
  160. 'theme_dir' => 'theme_dir',
  161. )
  162. );
  163. $theme_paths = array();
  164. while ($row = $smcFunc['db_fetch_assoc']($request))
  165. $theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
  166. $smcFunc['db_free_result']($request);
  167. // Get the package info...
  168. $packageInfo = getPackageInfo($context['filename']);
  169. if (!is_array($packageInfo))
  170. fatal_lang_error($packageInfo);
  171. $packageInfo['filename'] = $context['filename'];
  172. $context['package_name'] = isset($packageInfo['name']) ? $packageInfo['name'] : $context['filename'];
  173. // Set the type of extraction...
  174. $context['extract_type'] = isset($packageInfo['type']) ? $packageInfo['type'] : 'modification';
  175. // The mod isn't installed.... unless proven otherwise.
  176. $context['is_installed'] = false;
  177. // See if it is installed?
  178. $request = $smcFunc['db_query']('', '
  179. SELECT version, themes_installed, db_changes
  180. FROM {db_prefix}log_packages
  181. WHERE package_id = {string:current_package}
  182. AND install_state != {int:not_installed}
  183. ORDER BY time_installed DESC
  184. LIMIT 1',
  185. array(
  186. 'not_installed' => 0,
  187. 'current_package' => $packageInfo['id'],
  188. )
  189. );
  190. while ($row = $smcFunc['db_fetch_assoc']($request))
  191. {
  192. $old_themes = explode(',', $row['themes_installed']);
  193. $old_version = $row['version'];
  194. $db_changes = empty($row['db_changes']) ? array() : unserialize($row['db_changes']);
  195. }
  196. $smcFunc['db_free_result']($request);
  197. $context['database_changes'] = array();
  198. if (isset($packageInfo['uninstall']['database']))
  199. $context['database_changes'][] = $txt['execute_database_changes'] . ' - ' . $packageInfo['uninstall']['database'];
  200. elseif (!empty($db_changes))
  201. {
  202. foreach ($db_changes as $change)
  203. {
  204. if (isset($change[2]) && isset($txt['package_db_' . $change[0]]))
  205. $context['database_changes'][] = sprintf($txt['package_db_' . $change[0]], $change[1], $change[2]);
  206. elseif (isset($txt['package_db_' . $change[0]]))
  207. $context['database_changes'][] = sprintf($txt['package_db_' . $change[0]], $change[1]);
  208. else
  209. $context['database_changes'][] = $change[0] . '-' . $change[1] . (isset($change[2]) ? '-' . $change[2] : '');
  210. }
  211. }
  212. // Uninstalling?
  213. if ($context['uninstalling'])
  214. {
  215. // Wait, it's not installed yet!
  216. if (!isset($old_version) && $context['uninstalling'])
  217. {
  218. deltree(BOARDDIR . '/packages/temp');
  219. fatal_lang_error('package_cant_uninstall', false);
  220. }
  221. $actions = parsePackageInfo($packageInfo['xml'], true, 'uninstall');
  222. // Gadzooks! There's no uninstaller at all!?
  223. if (empty($actions))
  224. {
  225. deltree(BOARDDIR . '/packages/temp');
  226. fatal_lang_error('package_uninstall_cannot', false);
  227. }
  228. // Can't edit the custom themes it's edited if you're unisntalling, they must be removed.
  229. $context['themes_locked'] = true;
  230. // Only let them uninstall themes it was installed into.
  231. foreach ($theme_paths as $id => $data)
  232. if ($id != 1 && !in_array($id, $old_themes))
  233. unset($theme_paths[$id]);
  234. }
  235. elseif (isset($old_version) && $old_version != $packageInfo['version'])
  236. {
  237. // Look for an upgrade...
  238. $actions = parsePackageInfo($packageInfo['xml'], true, 'upgrade', $old_version);
  239. // There was no upgrade....
  240. if (empty($actions))
  241. $context['is_installed'] = true;
  242. else
  243. {
  244. // Otherwise they can only upgrade themes from the first time around.
  245. foreach ($theme_paths as $id => $data)
  246. if ($id != 1 && !in_array($id, $old_themes))
  247. unset($theme_paths[$id]);
  248. }
  249. }
  250. elseif (isset($old_version) && $old_version == $packageInfo['version'])
  251. $context['is_installed'] = true;
  252. if (!isset($old_version) || $context['is_installed'])
  253. $actions = parsePackageInfo($packageInfo['xml'], true, 'install');
  254. $context['actions'] = array();
  255. $context['ftp_needed'] = false;
  256. $context['has_failure'] = false;
  257. $chmod_files = array();
  258. // no actions found, return so we can display an error
  259. if (empty($actions))
  260. return;
  261. // This will hold data about anything that can be installed in other themes.
  262. $themeFinds = array(
  263. 'candidates' => array(),
  264. 'other_themes' => array(),
  265. );
  266. // Now prepare things for the template.
  267. foreach ($actions as $action)
  268. {
  269. // Not failed until proven otherwise.
  270. $failed = false;
  271. $thisAction = array();
  272. if ($action['type'] == 'chmod')
  273. {
  274. $chmod_files[] = $action['filename'];
  275. continue;
  276. }
  277. elseif ($action['type'] == 'readme' || $action['type'] == 'license')
  278. {
  279. $type = 'package_' . $action['type'];
  280. if (file_exists(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']))
  281. $context[$type] = htmlspecialchars(trim(file_get_contents(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']), "\n\r"));
  282. elseif (file_exists($action['filename']))
  283. $context[$type] = htmlspecialchars(trim(file_get_contents($action['filename']), "\n\r"));
  284. if (!empty($action['parse_bbc']))
  285. {
  286. require_once(SUBSDIR . '/Post.subs.php');
  287. preparsecode($context[$type]);
  288. $context[$type] = parse_bbc($context[$type]);
  289. }
  290. else
  291. $context[$type] = nl2br($context[$type]);
  292. continue;
  293. }
  294. // Don't show redirects.
  295. elseif ($action['type'] == 'redirect')
  296. continue;
  297. elseif ($action['type'] == 'error')
  298. {
  299. $context['has_failure'] = true;
  300. if (isset($action['error_msg']) && isset($action['error_var']))
  301. $context['failure_details'] = sprintf($txt['package_will_fail_' . $action['error_msg']], $action['error_var']);
  302. elseif (isset($action['error_msg']))
  303. $context['failure_details'] = isset($txt['package_will_fail_' . $action['error_msg']]) ? $txt['package_will_fail_' . $action['error_msg']] : $action['error_msg'];
  304. }
  305. elseif ($action['type'] == 'modification')
  306. {
  307. if (!file_exists(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']))
  308. {
  309. $context['has_failure'] = true;
  310. $context['actions'][] = array(
  311. 'type' => $txt['execute_modification'],
  312. 'action' => $smcFunc['htmlspecialchars'](strtr($action['filename'], array(BOARDDIR => '.'))),
  313. 'description' => $txt['package_action_error'],
  314. 'failed' => true,
  315. );
  316. }
  317. else
  318. {
  319. if ($action['boardmod'])
  320. $mod_actions = parseBoardMod(@file_get_contents(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']), true, $action['reverse'], $theme_paths);
  321. else
  322. $mod_actions = parseModification(@file_get_contents(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']), true, $action['reverse'], $theme_paths);
  323. if (count($mod_actions) == 1 && isset($mod_actions[0]) && $mod_actions[0]['type'] == 'error' && $mod_actions[0]['filename'] == '-')
  324. $mod_actions[0]['filename'] = $action['filename'];
  325. foreach ($mod_actions as $key => $mod_action)
  326. {
  327. // Lets get the last section of the file name.
  328. if (isset($mod_action['filename']) && substr($mod_action['filename'], -13) != '.template.php')
  329. $actual_filename = strtolower(substr(strrchr($mod_action['filename'], '/'), 1) . '||' . $action['filename']);
  330. elseif (isset($mod_action['filename']) && preg_match('~([\w]*)/([\w]*)\.template\.php$~', $mod_action['filename'], $matches))
  331. $actual_filename = strtolower($matches[1] . '/' . $matches[2] . '.template.php' . '||' . $action['filename']);
  332. else
  333. $actual_filename = $key;
  334. if ($mod_action['type'] == 'opened')
  335. $failed = false;
  336. elseif ($mod_action['type'] == 'failure')
  337. {
  338. if (empty($mod_action['is_custom']))
  339. $context['has_failure'] = true;
  340. $failed = true;
  341. }
  342. elseif ($mod_action['type'] == 'chmod')
  343. {
  344. $chmod_files[] = $mod_action['filename'];
  345. }
  346. elseif ($mod_action['type'] == 'saved')
  347. {
  348. if (!empty($mod_action['is_custom']))
  349. {
  350. if (!isset($context['theme_actions'][$mod_action['is_custom']]))
  351. $context['theme_actions'][$mod_action['is_custom']] = array(
  352. 'name' => $theme_paths[$mod_action['is_custom']]['name'],
  353. 'actions' => array(),
  354. 'has_failure' => $failed,
  355. );
  356. else
  357. $context['theme_actions'][$mod_action['is_custom']]['has_failure'] |= $failed;
  358. $context['theme_actions'][$mod_action['is_custom']]['actions'][$actual_filename] = array(
  359. 'type' => $txt['execute_modification'],
  360. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array(BOARDDIR => '.'))),
  361. 'description' => $failed ? $txt['package_action_failure'] : $txt['package_action_success'],
  362. 'failed' => $failed,
  363. );
  364. }
  365. elseif (!isset($context['actions'][$actual_filename]))
  366. {
  367. $context['actions'][$actual_filename] = array(
  368. 'type' => $txt['execute_modification'],
  369. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array(BOARDDIR => '.'))),
  370. 'description' => $failed ? $txt['package_action_failure'] : $txt['package_action_success'],
  371. 'failed' => $failed,
  372. );
  373. }
  374. else
  375. {
  376. $context['actions'][$actual_filename]['failed'] |= $failed;
  377. $context['actions'][$actual_filename]['description'] = $context['actions'][$actual_filename]['failed'] ? $txt['package_action_failure'] : $txt['package_action_success'];
  378. }
  379. }
  380. elseif ($mod_action['type'] == 'skipping')
  381. {
  382. $context['actions'][$actual_filename] = array(
  383. 'type' => $txt['execute_modification'],
  384. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array(BOARDDIR => '.'))),
  385. 'description' => $txt['package_action_skipping']
  386. );
  387. }
  388. elseif ($mod_action['type'] == 'missing' && empty($mod_action['is_custom']))
  389. {
  390. $context['has_failure'] = true;
  391. $context['actions'][$actual_filename] = array(
  392. 'type' => $txt['execute_modification'],
  393. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array(BOARDDIR => '.'))),
  394. 'description' => $txt['package_action_missing'],
  395. 'failed' => true,
  396. );
  397. }
  398. elseif ($mod_action['type'] == 'error')
  399. $context['actions'][$actual_filename] = array(
  400. 'type' => $txt['execute_modification'],
  401. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array(BOARDDIR => '.'))),
  402. 'description' => $txt['package_action_error'],
  403. 'failed' => true,
  404. );
  405. }
  406. // We need to loop again just to get the operations down correctly.
  407. foreach ($mod_actions as $operation_key => $mod_action)
  408. {
  409. // Lets get the last section of the file name.
  410. if (isset($mod_action['filename']) && substr($mod_action['filename'], -13) != '.template.php')
  411. $actual_filename = strtolower(substr(strrchr($mod_action['filename'], '/'), 1) . '||' . $action['filename']);
  412. elseif (isset($mod_action['filename']) && preg_match('~([\w]*)/([\w]*)\.template\.php$~', $mod_action['filename'], $matches))
  413. $actual_filename = strtolower($matches[1] . '/' . $matches[2] . '.template.php' . '||' . $action['filename']);
  414. else
  415. $actual_filename = $key;
  416. // We just need it for actual parse changes.
  417. if (!in_array($mod_action['type'], array('error', 'result', 'opened', 'saved', 'end', 'missing', 'skipping', 'chmod')))
  418. {
  419. if (empty($mod_action['is_custom']))
  420. $context['actions'][$actual_filename]['operations'][] = array(
  421. 'type' => $txt['execute_modification'],
  422. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array(BOARDDIR => '.'))),
  423. 'description' => $mod_action['failed'] ? $txt['package_action_failure'] : $txt['package_action_success'],
  424. 'position' => $mod_action['position'],
  425. 'operation_key' => $operation_key,
  426. 'filename' => $action['filename'],
  427. 'is_boardmod' => $action['boardmod'],
  428. 'failed' => $mod_action['failed'],
  429. 'ignore_failure' => !empty($mod_action['ignore_failure']),
  430. );
  431. // Themes are under the saved type.
  432. if (isset($mod_action['is_custom']) && isset($context['theme_actions'][$mod_action['is_custom']]))
  433. $context['theme_actions'][$mod_action['is_custom']]['actions'][$actual_filename]['operations'][] = array(
  434. 'type' => $txt['execute_modification'],
  435. 'action' => $smcFunc['htmlspecialchars'](strtr($mod_action['filename'], array(BOARDDIR => '.'))),
  436. 'description' => $mod_action['failed'] ? $txt['package_action_failure'] : $txt['package_action_success'],
  437. 'position' => $mod_action['position'],
  438. 'operation_key' => $operation_key,
  439. 'filename' => $action['filename'],
  440. 'is_boardmod' => $action['boardmod'],
  441. 'failed' => $mod_action['failed'],
  442. 'ignore_failure' => !empty($mod_action['ignore_failure']),
  443. );
  444. }
  445. }
  446. }
  447. }
  448. elseif ($action['type'] == 'code')
  449. {
  450. $thisAction = array(
  451. 'type' => $txt['execute_code'],
  452. 'action' => $smcFunc['htmlspecialchars']($action['filename']),
  453. );
  454. }
  455. elseif ($action['type'] == 'database')
  456. {
  457. $thisAction = array(
  458. 'type' => $txt['execute_database_changes'],
  459. 'action' => $smcFunc['htmlspecialchars']($action['filename']),
  460. );
  461. }
  462. elseif (in_array($action['type'], array('create-dir', 'create-file')))
  463. {
  464. $thisAction = array(
  465. 'type' => $txt['package_create'] . ' ' . ($action['type'] == 'create-dir' ? $txt['package_tree'] : $txt['package_file']),
  466. 'action' => $smcFunc['htmlspecialchars'](strtr($action['destination'], array(BOARDDIR => '.')))
  467. );
  468. }
  469. elseif ($action['type'] == 'hook')
  470. {
  471. $action['description'] = !isset($action['hook'], $action['function']) ? $txt['package_action_failure'] : $txt['package_action_success'];
  472. if (!isset($action['hook'], $action['function']))
  473. $context['has_failure'] = true;
  474. $thisAction = array(
  475. 'type' => $action['reverse'] ? $txt['execute_hook_remove'] : $txt['execute_hook_add'],
  476. 'action' => sprintf($txt['execute_hook_action'], $smcFunc['htmlspecialchars']($action['hook'])),
  477. );
  478. }
  479. elseif ($action['type'] == 'credits')
  480. {
  481. $thisAction = array(
  482. 'type' => $txt['execute_credits_add'],
  483. 'action' => sprintf($txt['execute_credits_action'], $smcFunc['htmlspecialchars']($action['title'])),
  484. );
  485. }
  486. elseif ($action['type'] == 'requires')
  487. {
  488. $installed = false;
  489. $version = true;
  490. // package missing required values?
  491. if (!isset($action['id']))
  492. $context['has_failure'] = true;
  493. else
  494. {
  495. // See if this dependancy is installed
  496. $request = $smcFunc['db_query']('', '
  497. SELECT version
  498. FROM {db_prefix}log_packages
  499. WHERE package_id = {string:current_package}
  500. AND install_state != {int:not_installed}
  501. ORDER BY time_installed DESC
  502. LIMIT 1',
  503. array(
  504. 'not_installed' => 0,
  505. 'current_package' => $action['id'],
  506. )
  507. );
  508. $installed = ($smcFunc['db_num_rows']($request) !== 0);
  509. if ($installed)
  510. list($version) = $smcFunc['db_fetch_row']($request);
  511. $smcFunc['db_free_result']($request);
  512. // do a version level check (if requested) in the most basic way
  513. $version = (isset($action['version']) ? $version == $action['version'] : true);
  514. }
  515. // Set success or failure information
  516. $action['description'] = ($installed && $version) ? $txt['package_action_success'] : $txt['package_action_failure'];
  517. $context['has_failure'] = !($installed && $version);
  518. $thisAction = array(
  519. 'type' => $txt['package_requires'],
  520. 'action' => $txt['package_check_for'] . ' ' . $action['id'] . (isset($action['version']) ? (' / ' . ($version ? $action['version'] : '<span class="error">' . $action['version'] . '</span>')) : ''),
  521. );
  522. }
  523. elseif (in_array($action['type'], array('require-dir', 'require-file')))
  524. {
  525. // Do this one...
  526. $thisAction = array(
  527. 'type' => $txt['package_extract'] . ' ' . ($action['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
  528. 'action' => $smcFunc['htmlspecialchars'](strtr($action['destination'], array(BOARDDIR => '.')))
  529. );
  530. // Could this be theme related?
  531. if (!empty($action['unparsed_destination']) && preg_match('~^\$(languagedir|languages_dir|imagesdir|themedir|themes_dir)~i', $action['unparsed_destination'], $matches))
  532. {
  533. // Is the action already stated?
  534. $theme_action = !empty($action['theme_action']) && in_array($action['theme_action'], array('no', 'yes', 'auto')) ? $action['theme_action'] : 'auto';
  535. // If it's not auto do we think we have something we can act upon?
  536. if ($theme_action != 'auto' && !in_array($matches[1], array('languagedir', 'languages_dir', 'imagesdir', 'themedir')))
  537. $theme_action = '';
  538. // ... or if it's auto do we even want to do anything?
  539. elseif ($theme_action == 'auto' && $matches[1] != 'imagesdir')
  540. $theme_action = '';
  541. // So, we still want to do something?
  542. if ($theme_action != '')
  543. $themeFinds['candidates'][] = $action;
  544. // Otherwise is this is going into another theme record it.
  545. elseif ($matches[1] == 'themes_dir')
  546. $themeFinds['other_themes'][] = strtolower(strtr(parse_path($action['unparsed_destination']), array('\\' => '/')) . '/' . basename($action['filename']));
  547. }
  548. }
  549. elseif (in_array($action['type'], array('move-dir', 'move-file')))
  550. $thisAction = array(
  551. 'type' => $txt['package_move'] . ' ' . ($action['type'] == 'move-dir' ? $txt['package_tree'] : $txt['package_file']),
  552. 'action' => $smcFunc['htmlspecialchars'](strtr($action['source'], array(BOARDDIR => '.'))) . ' => ' . $smcFunc['htmlspecialchars'](strtr($action['destination'], array(BOARDDIR => '.')))
  553. );
  554. elseif (in_array($action['type'], array('remove-dir', 'remove-file')))
  555. {
  556. $thisAction = array(
  557. 'type' => $txt['package_delete'] . ' ' . ($action['type'] == 'remove-dir' ? $txt['package_tree'] : $txt['package_file']),
  558. 'action' => $smcFunc['htmlspecialchars'](strtr($action['filename'], array(BOARDDIR => '.')))
  559. );
  560. // Could this be theme related?
  561. if (!empty($action['unparsed_filename']) && preg_match('~^\$(languagedir|languages_dir|imagesdir|themedir|themes_dir)~i', $action['unparsed_filename'], $matches))
  562. {
  563. // Is the action already stated?
  564. $theme_action = !empty($action['theme_action']) && in_array($action['theme_action'], array('no', 'yes', 'auto')) ? $action['theme_action'] : 'auto';
  565. $action['unparsed_destination'] = $action['unparsed_filename'];
  566. // If it's not auto do we think we have something we can act upon?
  567. if ($theme_action != 'auto' && !in_array($matches[1], array('languagedir', 'languages_dir', 'imagesdir', 'themedir')))
  568. $theme_action = '';
  569. // ... or if it's auto do we even want to do anything?
  570. elseif ($theme_action == 'auto' && $matches[1] != 'imagesdir')
  571. $theme_action = '';
  572. // So, we still want to do something?
  573. if ($theme_action != '')
  574. $themeFinds['candidates'][] = $action;
  575. // Otherwise is this is going into another theme record it.
  576. elseif ($matches[1] == 'themes_dir')
  577. $themeFinds['other_themes'][] = strtolower(strtr(parse_path($action['unparsed_filename']), array('\\' => '/')) . '/' . basename($action['filename']));
  578. }
  579. }
  580. if (empty($thisAction))
  581. continue;
  582. if ($context['uninstalling'])
  583. $file = in_array($action['type'], array('remove-dir', 'remove-file')) ? $action['filename'] : BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename'];
  584. else
  585. $file = BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename'];
  586. if (isset($action['filename']) && !file_exists($file))
  587. {
  588. $context['has_failure'] = true;
  589. $thisAction += array(
  590. 'description' => $txt['package_action_error'],
  591. 'failed' => true,
  592. );
  593. }
  594. // @todo None given?
  595. if (empty($thisAction['description']))
  596. $thisAction['description'] = isset($action['description']) ? $action['description'] : '';
  597. $context['actions'][] = $thisAction;
  598. }
  599. // Have we got some things which we might want to do "multi-theme"?
  600. if (!empty($themeFinds['candidates']))
  601. {
  602. foreach ($themeFinds['candidates'] as $action_data)
  603. {
  604. // Get the part of the file we'll be dealing with.
  605. preg_match('~^\$(languagedir|languages_dir|imagesdir|themedir)(\\|/)*(.+)*~i', $action_data['unparsed_destination'], $matches);
  606. if ($matches[1] == 'imagesdir')
  607. $path = '/' . basename($settings['default_images_url']);
  608. elseif ($matches[1] == 'languagedir' || $matches[1] == 'languages_dir')
  609. $path = '/languages';
  610. else
  611. $path = '';
  612. if (!empty($matches[3]))
  613. $path .= $matches[3];
  614. if (!$context['uninstalling'])
  615. $path .= '/' . basename($action_data['filename']);
  616. // Loop through each custom theme to note it's candidacy!
  617. foreach ($theme_paths as $id => $theme_data)
  618. {
  619. if (isset($theme_data['theme_dir']) && $id != 1)
  620. {
  621. $real_path = $theme_data['theme_dir'] . $path;
  622. // Confirm that we don't already have this dealt with by another entry.
  623. if (!in_array(strtolower(strtr($real_path, array('\\' => '/'))), $themeFinds['other_themes']))
  624. {
  625. // Check if we will need to chmod this.
  626. if (!mktree(dirname($real_path), false))
  627. {
  628. $temp = dirname($real_path);
  629. while (!file_exists($temp) && strlen($temp) > 1)
  630. $temp = dirname($temp);
  631. $chmod_files[] = $temp;
  632. }
  633. if ($action_data['type'] == 'require-dir' && !is_writable($real_path) && (file_exists($real_path) || !is_writable(dirname($real_path))))
  634. $chmod_files[] = $real_path;
  635. if (!isset($context['theme_actions'][$id]))
  636. $context['theme_actions'][$id] = array(
  637. 'name' => $theme_data['name'],
  638. 'actions' => array(),
  639. );
  640. if ($context['uninstalling'])
  641. $context['theme_actions'][$id]['actions'][] = array(
  642. 'type' => $txt['package_delete'] . ' ' . ($action_data['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
  643. 'action' => strtr($real_path, array('\\' => '/', BOARDDIR => '.')),
  644. 'description' => '',
  645. 'value' => base64_encode(serialize(array('type' => $action_data['type'], 'orig' => $action_data['filename'], 'future' => $real_path, 'id' => $id))),
  646. 'not_mod' => true,
  647. );
  648. else
  649. $context['theme_actions'][$id]['actions'][] = array(
  650. 'type' => $txt['package_extract'] . ' ' . ($action_data['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
  651. 'action' => strtr($real_path, array('\\' => '/', BOARDDIR => '.')),
  652. 'description' => '',
  653. 'value' => base64_encode(serialize(array('type' => $action_data['type'], 'orig' => $action_data['destination'], 'future' => $real_path, 'id' => $id))),
  654. 'not_mod' => true,
  655. );
  656. }
  657. }
  658. }
  659. }
  660. }
  661. // Trash the cache... which will also check permissions for us!
  662. package_flush_cache(true);
  663. if (file_exists(BOARDDIR . '/packages/temp'))
  664. deltree(BOARDDIR . '/packages/temp');
  665. if (!empty($chmod_files))
  666. {
  667. $ftp_status = create_chmod_control($chmod_files);
  668. $context['ftp_needed'] = !empty($ftp_status['files']['notwritable']) && !empty($context['package_ftp']);
  669. }
  670. $context['post_url'] = $scripturl .'?action=admin;area=packages;sa=' . ($context['uninstalling'] ? 'uninstall' : 'install') . ($context['ftp_needed'] ? '' : '2') . ';package=' . $context['filename'] . ';pid=' . $context['install_id'];
  671. checkSubmitOnce('register');
  672. }
  673. /**
  674. * Apply another type of (avatar, language, etc.) package.
  675. */
  676. function PackageInstall()
  677. {
  678. global $txt, $context, $boardurl, $scripturl, $modSettings;
  679. global $user_info, $smcFunc;
  680. // Make sure we don't install this mod twice.
  681. checkSubmitOnce('check');
  682. checkSession();
  683. // If there's no file, what are we installing?
  684. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  685. redirectexit('action=admin;area=packages');
  686. $context['filename'] = $_REQUEST['package'];
  687. // If this is an uninstall, we'll have an id.
  688. $context['install_id'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 0;
  689. require_once(SUBSDIR . '/Package.subs.php');
  690. // @todo Perhaps do it in steps, if necessary?
  691. $context['uninstalling'] = $_REQUEST['sa'] == 'uninstall2';
  692. // Set up the linktree for other.
  693. $context['linktree'][count($context['linktree']) - 1] = array(
  694. 'url' => $scripturl . '?action=admin;area=packages;sa=browse',
  695. 'name' => $context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']
  696. );
  697. $context['page_title'] .= ' - ' . ($context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']);
  698. $context['sub_template'] = 'extract_package';
  699. if (!file_exists(BOARDDIR . '/packages/' . $context['filename']))
  700. fatal_lang_error('package_no_file', false);
  701. // Load up the package FTP information?
  702. create_chmod_control(array(), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=' . $_REQUEST['sa'] . ';package=' . $_REQUEST['package']));
  703. // Make sure temp directory exists and is empty!
  704. if (file_exists(BOARDDIR . '/packages/temp'))
  705. deltree(BOARDDIR . '/packages/temp', false);
  706. else
  707. mktree(BOARDDIR . '/packages/temp', 0777);
  708. // Let the unpacker do the work.
  709. if (is_file(BOARDDIR . '/packages/' . $context['filename']))
  710. {
  711. $context['extracted_files'] = read_tgz_file(BOARDDIR . '/packages/' . $context['filename'], BOARDDIR . '/packages/temp');
  712. if (!file_exists(BOARDDIR . '/packages/temp/package-info.xml'))
  713. foreach ($context['extracted_files'] as $file)
  714. if (basename($file['filename']) == 'package-info.xml')
  715. {
  716. $context['base_path'] = dirname($file['filename']) . '/';
  717. break;
  718. }
  719. if (!isset($context['base_path']))
  720. $context['base_path'] = '';
  721. }
  722. elseif (is_dir(BOARDDIR . '/packages/' . $context['filename']))
  723. {
  724. copytree(BOARDDIR . '/packages/' . $context['filename'], BOARDDIR . '/packages/temp');
  725. $context['extracted_files'] = listtree(BOARDDIR . '/packages/temp');
  726. $context['base_path'] = '';
  727. }
  728. else
  729. fatal_lang_error('no_access', false);
  730. // Are we installing this into any custom themes?
  731. $custom_themes = array(1);
  732. $known_themes = explode(',', $modSettings['knownThemes']);
  733. if (!empty($_POST['custom_theme']))
  734. {
  735. foreach ($_POST['custom_theme'] as $tid)
  736. if (in_array($tid, $known_themes))
  737. $custom_themes[] = (int) $tid;
  738. }
  739. // Now load up the paths of the themes that we need to know about.
  740. $request = $smcFunc['db_query']('', '
  741. SELECT id_theme, variable, value
  742. FROM {db_prefix}themes
  743. WHERE id_theme IN ({array_int:custom_themes})
  744. AND variable IN ({string:name}, {string:theme_dir})',
  745. array(
  746. 'custom_themes' => $custom_themes,
  747. 'name' => 'name',
  748. 'theme_dir' => 'theme_dir',
  749. )
  750. );
  751. $theme_paths = array();
  752. $themes_installed = array(1);
  753. while ($row = $smcFunc['db_fetch_assoc']($request))
  754. $theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
  755. $smcFunc['db_free_result']($request);
  756. // Are there any theme copying that we want to take place?
  757. $context['theme_copies'] = array(
  758. 'require-file' => array(),
  759. 'require-dir' => array(),
  760. );
  761. if (!empty($_POST['theme_changes']))
  762. {
  763. foreach ($_POST['theme_changes'] as $change)
  764. {
  765. if (empty($change))
  766. continue;
  767. $theme_data = unserialize(base64_decode($change));
  768. if (empty($theme_data['type']))
  769. continue;
  770. $themes_installed[] = $theme_data['id'];
  771. $context['theme_copies'][$theme_data['type']][$theme_data['orig']][] = $theme_data['future'];
  772. }
  773. }
  774. // Get the package info...
  775. $packageInfo = getPackageInfo($context['filename']);
  776. if (!is_array($packageInfo))
  777. fatal_lang_error($packageInfo);
  778. $packageInfo['filename'] = $context['filename'];
  779. // Set the type of extraction...
  780. $context['extract_type'] = isset($packageInfo['type']) ? $packageInfo['type'] : 'modification';
  781. // Create a backup file to roll back to! (but if they do this more than once, don't run it a zillion times.)
  782. if (!empty($modSettings['package_make_full_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['filename'] . ($context['uninstalling'] ? '$$' : '$')))
  783. {
  784. $_SESSION['last_backup_for'] = $context['filename'] . ($context['uninstalling'] ? '$$' : '$');
  785. // @todo Internationalize this?
  786. package_create_backup(($context['uninstalling'] ? 'backup_' : 'before_') . strtok($context['filename'], '.'));
  787. }
  788. // The mod isn't installed.... unless proven otherwise.
  789. $context['is_installed'] = false;
  790. // Is it actually installed?
  791. $request = $smcFunc['db_query']('', '
  792. SELECT version, themes_installed, db_changes
  793. FROM {db_prefix}log_packages
  794. WHERE package_id = {string:current_package}
  795. AND install_state != {int:not_installed}
  796. ORDER BY time_installed DESC
  797. LIMIT 1',
  798. array(
  799. 'not_installed' => 0,
  800. 'current_package' => $packageInfo['id'],
  801. )
  802. );
  803. while ($row = $smcFunc['db_fetch_assoc']($request))
  804. {
  805. $old_themes = explode(',', $row['themes_installed']);
  806. $old_version = $row['version'];
  807. $db_changes = empty($row['db_changes']) ? array() : unserialize($row['db_changes']);
  808. }
  809. $smcFunc['db_free_result']($request);
  810. // Wait, it's not installed yet!
  811. // @todo Replace with a better error message!
  812. if (!isset($old_version) && $context['uninstalling'])
  813. {
  814. deltree(BOARDDIR . '/packages/temp');
  815. fatal_error('Hacker?', false);
  816. }
  817. // Uninstalling?
  818. elseif ($context['uninstalling'])
  819. {
  820. $install_log = parsePackageInfo($packageInfo['xml'], false, 'uninstall');
  821. // Gadzooks! There's no uninstaller at all!?
  822. if (empty($install_log))
  823. fatal_lang_error('package_uninstall_cannot', false);
  824. // They can only uninstall from what it was originally installed into.
  825. foreach ($theme_paths as $id => $data)
  826. if ($id != 1 && !in_array($id, $old_themes))
  827. unset($theme_paths[$id]);
  828. }
  829. elseif (isset($old_version) && $old_version != $packageInfo['version'])
  830. {
  831. // Look for an upgrade...
  832. $install_log = parsePackageInfo($packageInfo['xml'], false, 'upgrade', $old_version);
  833. // There was no upgrade....
  834. if (empty($install_log))
  835. $context['is_installed'] = true;
  836. else
  837. {
  838. // Upgrade previous themes only!
  839. foreach ($theme_paths as $id => $data)
  840. if ($id != 1 && !in_array($id, $old_themes))
  841. unset($theme_paths[$id]);
  842. }
  843. }
  844. elseif (isset($old_version) && $old_version == $packageInfo['version'])
  845. $context['is_installed'] = true;
  846. if (!isset($old_version) || $context['is_installed'])
  847. $install_log = parsePackageInfo($packageInfo['xml'], false, 'install');
  848. $context['install_finished'] = false;
  849. // @todo Make a log of any errors that occurred and output them?
  850. if (!empty($install_log))
  851. {
  852. $failed_steps = array();
  853. $failed_count = 0;
  854. foreach ($install_log as $action)
  855. {
  856. $failed_count++;
  857. if ($action['type'] == 'modification' && !empty($action['filename']))
  858. {
  859. if ($action['boardmod'])
  860. $mod_actions = parseBoardMod(file_get_contents(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
  861. else
  862. $mod_actions = parseModification(file_get_contents(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
  863. // Any errors worth noting?
  864. foreach ($mod_actions as $key => $action)
  865. {
  866. if ($action['type'] == 'failure')
  867. $failed_steps[] = array(
  868. 'file' => $action['filename'],
  869. 'large_step' => $failed_count,
  870. 'sub_step' => $key,
  871. 'theme' => 1,
  872. );
  873. // Gather the themes we installed into.
  874. if (!empty($action['is_custom']))
  875. $themes_installed[] = $action['is_custom'];
  876. }
  877. }
  878. elseif ($action['type'] == 'code' && !empty($action['filename']))
  879. {
  880. // This is just here as reference for what is available.
  881. global $txt, $modSettings, $context, $settings, $forum_version, $smcFunc;
  882. // Now include the file and be done with it ;).
  883. if (file_exists(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']))
  884. require(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']);
  885. }
  886. elseif ($action['type'] == 'credits')
  887. {
  888. // Time to build the billboard
  889. $credits_tag = array(
  890. 'url' => $action['url'],
  891. 'license' => $action['license'],
  892. 'copyright' => $action['copyright'],
  893. 'title' => $action['title'],
  894. );
  895. }
  896. elseif ($action['type'] == 'hook' && isset($action['hook'], $action['function']))
  897. {
  898. if ($action['reverse'])
  899. remove_integration_function($action['hook'], $action['function'], $action['include_file']);
  900. else
  901. add_integration_function($action['hook'], $action['function'], $action['include_file']);
  902. }
  903. // Only do the database changes on uninstall if requested.
  904. elseif ($action['type'] == 'database' && !empty($action['filename']) && (!$context['uninstalling'] || !empty($_POST['do_db_changes'])))
  905. {
  906. // These can also be there for database changes.
  907. global $txt, $modSettings, $context, $settings, $forum_version, $smcFunc;
  908. global $db_package_log;
  909. // We'll likely want the package specific database functionality!
  910. db_extend('packages');
  911. // Let the file work its magic ;)
  912. if (file_exists(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']))
  913. require(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']);
  914. }
  915. // Handle a redirect...
  916. elseif ($action['type'] == 'redirect' && !empty($action['redirect_url']))
  917. {
  918. $context['redirect_url'] = $action['redirect_url'];
  919. $context['redirect_text'] = !empty($action['filename']) && file_exists(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']) ? file_get_contents(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']) : ($context['uninstalling'] ? $txt['package_uninstall_done'] : $txt['package_installed_done']);
  920. $context['redirect_timeout'] = $action['redirect_timeout'];
  921. // Parse out a couple of common urls.
  922. $urls = array(
  923. '$boardurl' => $boardurl,
  924. '$scripturl' => $scripturl,
  925. '$session_var' => $context['session_var'],
  926. '$session_id' => $context['session_id'],
  927. );
  928. $context['redirect_url'] = strtr($context['redirect_url'], $urls);
  929. }
  930. }
  931. package_flush_cache();
  932. // First, ensure this change doesn't get removed by putting a stake in the ground (So to speak).
  933. package_put_contents(BOARDDIR . '/packages/installed.list', time());
  934. // See if this is already installed, and change it's state as required.
  935. $request = $smcFunc['db_query']('', '
  936. SELECT package_id, install_state, db_changes
  937. FROM {db_prefix}log_packages
  938. WHERE install_state != {int:not_installed}
  939. AND package_id = {string:current_package}
  940. ' . ($context['install_id'] ? ' AND id_install = {int:install_id} ' : '') . '
  941. ORDER BY time_installed DESC
  942. LIMIT 1',
  943. array(
  944. 'not_installed' => 0,
  945. 'install_id' => $context['install_id'],
  946. 'current_package' => $packageInfo['id'],
  947. )
  948. );
  949. $is_upgrade = false;
  950. while ($row = $smcFunc['db_fetch_assoc']($request))
  951. {
  952. // Uninstalling?
  953. if ($context['uninstalling'])
  954. {
  955. $smcFunc['db_query']('', '
  956. UPDATE {db_prefix}log_packages
  957. SET install_state = {int:not_installed}, member_removed = {string:member_name}, id_member_removed = {int:current_member},
  958. time_removed = {int:current_time}
  959. WHERE package_id = {string:package_id}
  960. AND id_install = {int:install_id}',
  961. array(
  962. 'current_member' => $user_info['id'],
  963. 'not_installed' => 0,
  964. 'current_time' => time(),
  965. 'package_id' => $row['package_id'],
  966. 'member_name' => $user_info['name'],
  967. 'install_id' => $context['install_id'],
  968. )
  969. );
  970. }
  971. // Otherwise must be an upgrade.
  972. else
  973. {
  974. $is_upgrade = true;
  975. $old_db_changes = empty($row['db_changes']) ? array() : unserialize($row['db_changes']);
  976. }
  977. }
  978. // Assuming we're not uninstalling, add the entry.
  979. if (!$context['uninstalling'])
  980. {
  981. // Any db changes from older version?
  982. if (!empty($old_db_changes))
  983. $db_package_log = empty($db_package_log) ? $old_db_changes : array_merge($old_db_changes, $db_package_log);
  984. // If there are some database changes we might want to remove then filter them out.
  985. if (!empty($db_package_log))
  986. {
  987. // We're really just checking for entries which are create table AND add columns (etc).
  988. $tables = array();
  989. /**
  990. * Table sorting function used in usort
  991. *
  992. * @param type $a
  993. * @param type $b
  994. * @return int
  995. */
  996. function sort_table_first($a, $b)
  997. {
  998. if ($a[0] == $b[0])
  999. return 0;
  1000. return $a[0] == 'remove_table' ? -1 : 1;
  1001. }
  1002. usort($db_package_log, 'sort_table_first');
  1003. foreach ($db_package_log as $k => $log)
  1004. {
  1005. if ($log[0] == 'remove_table')
  1006. $tables[] = $log[1];
  1007. elseif (in_array($log[1], $tables))
  1008. unset($db_package_log[$k]);
  1009. }
  1010. $db_changes = serialize($db_package_log);
  1011. }
  1012. else
  1013. $db_changes = '';
  1014. // What themes did we actually install?
  1015. $themes_installed = array_unique($themes_installed);
  1016. $themes_installed = implode(',', $themes_installed);
  1017. // What failed steps?
  1018. $failed_step_insert = serialize($failed_steps);
  1019. // Credits tag?
  1020. $credits_tag = (empty($credits_tag)) ? '' : serialize($credits_tag);
  1021. $smcFunc['db_insert']('',
  1022. '{db_prefix}log_packages',
  1023. array(
  1024. 'filename' => 'string', 'name' => 'string', 'package_id' => 'string', 'version' => 'string',
  1025. 'id_member_installed' => 'int', 'member_installed' => 'string','time_installed' => 'int',
  1026. 'install_state' => 'int', 'failed_steps' => 'string', 'themes_installed' => 'string',
  1027. 'member_removed' => 'int', 'db_changes' => 'string', 'credits' => 'string',
  1028. ),
  1029. array(
  1030. $packageInfo['filename'], $packageInfo['name'], $packageInfo['id'], $packageInfo['version'],
  1031. $user_info['id'], $user_info['name'], time(),
  1032. $is_upgrade ? 2 : 1, $failed_step_insert, $themes_installed,
  1033. 0, $db_changes, $credits_tag,
  1034. ),
  1035. array('id_install')
  1036. );
  1037. }
  1038. $smcFunc['db_free_result']($request);
  1039. $context['install_finished'] = true;
  1040. }
  1041. // If there's database changes - and they want them removed - let's do it last!
  1042. if (!empty($db_changes) && !empty($_POST['do_db_changes']))
  1043. {
  1044. // We're gonna be needing the package db functions!
  1045. db_extend('packages');
  1046. foreach ($db_changes as $change)
  1047. {
  1048. if ($change[0] == 'remove_table' && isset($change[1]))
  1049. $smcFunc['db_drop_table']($change[1]);
  1050. elseif ($change[0] == 'remove_column' && isset($change[2]))
  1051. $smcFunc['db_remove_column']($change[1], $change[2]);
  1052. elseif ($change[0] == 'remove_index' && isset($change[2]))
  1053. $smcFunc['db_remove_index']($change[1], $change[2]);
  1054. }
  1055. }
  1056. // Clean house... get rid of the evidence ;).
  1057. if (file_exists(BOARDDIR . '/packages/temp'))
  1058. deltree(BOARDDIR . '/packages/temp');
  1059. // Log what we just did.
  1060. logAction($context['uninstalling'] ? 'uninstall_package' : (!empty($is_upgrade) ? 'upgrade_package' : 'install_package'), array('package' => $smcFunc['htmlspecialchars']($packageInfo['name']), 'version' => $smcFunc['htmlspecialchars']($packageInfo['version'])), 'admin');
  1061. // Just in case, let's clear the whole cache to avoid anything going up the swanny.
  1062. clean_cache();
  1063. // Restore file permissions?
  1064. create_chmod_control(array(), array(), true);
  1065. }
  1066. /**
  1067. * List the files in a package.
  1068. */
  1069. function PackageList()
  1070. {
  1071. global $txt, $scripturl, $context;
  1072. require_once(SUBSDIR . '/Package.subs.php');
  1073. // No package? Show him or her the door.
  1074. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  1075. redirectexit('action=admin;area=packages');
  1076. $context['linktree'][] = array(
  1077. 'url' => $scripturl . '?action=admin;area=packages;sa=list;package=' . $_REQUEST['package'],
  1078. 'name' => $txt['list_file']
  1079. );
  1080. $context['page_title'] .= ' - ' . $txt['list_file'];
  1081. $context['sub_template'] = 'list';
  1082. // The filename...
  1083. $context['filename'] = $_REQUEST['package'];
  1084. // Let the unpacker do the work.
  1085. if (is_file(BOARDDIR . '/packages/' . $context['filename']))
  1086. $context['files'] = read_tgz_file(BOARDDIR . '/packages/' . $context['filename'], null);
  1087. elseif (is_dir(BOARDDIR . '/packages/' . $context['filename']))
  1088. $context['files'] = listtree(BOARDDIR . '/packages/' . $context['filename']);
  1089. }
  1090. /**
  1091. * Display one of the files in a package.
  1092. */
  1093. function ExamineFile()
  1094. {
  1095. global $txt, $scripturl, $context;
  1096. require_once(SUBSDIR . '/Package.subs.php');
  1097. // No package? Show him or her the door.
  1098. if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '')
  1099. redirectexit('action=admin;area=packages');
  1100. // No file? Show him or her the door.
  1101. if (!isset($_REQUEST['file']) || $_REQUEST['file'] == '')
  1102. redirectexit('action=admin;area=packages');
  1103. $_REQUEST['package'] = preg_replace('~[\.]+~', '.', strtr($_REQUEST['package'], array('/' => '_', '\\' => '_')));
  1104. $_REQUEST['file'] = preg_replace('~[\.]+~', '.', $_REQUEST['file']);
  1105. if (isset($_REQUEST['raw']))
  1106. {
  1107. if (is_file(BOARDDIR . '/packages/' . $_REQUEST['package']))
  1108. echo read_tgz_file(BOARDDIR . '/packages/' . $_REQUEST['package'], $_REQUEST['file'], true);
  1109. elseif (is_dir(BOARDDIR . '/packages/' . $_REQUEST['package']))
  1110. echo file_get_contents(BOARDDIR . '/packages/' . $_REQUEST['package'] . '/' . $_REQUEST['file']);
  1111. obExit(false);
  1112. }
  1113. $context['linktree'][count($context['linktree']) - 1] = array(
  1114. 'url' => $scripturl . '?action=admin;area=packages;sa=list;package=' . $_REQUEST['package'],
  1115. 'name' => $txt['package_examine_file']
  1116. );
  1117. $context['page_title'] .= ' - ' . $txt['package_examine_file'];
  1118. $context['sub_template'] = 'examine';
  1119. // The filename...
  1120. $context['package'] = $_REQUEST['package'];
  1121. $context['filename'] = $_REQUEST['file'];
  1122. // Let the unpacker do the work.... but make sure we handle images properly.
  1123. if (in_array(strtolower(strrchr($_REQUEST['file'], '.')), array('.bmp', '.gif', '.jpeg', '.jpg', '.png')))
  1124. $context['filedata'] = '<img src="' . $scripturl . '?action=admin;area=packages;sa=examine;package=' . $_REQUEST['package'] . ';file=' . $_REQUEST['file'] . ';raw" alt="' . $_REQUEST['file'] . '" />';
  1125. else
  1126. {
  1127. if (is_file(BOARDDIR . '/packages/' . $_REQUEST['package']))
  1128. $context['filedata'] = htmlspecialchars(read_tgz_file(BOARDDIR . '/packages/' . $_REQUEST['package'], $_REQUEST['file'], true));
  1129. elseif (is_dir(BOARDDIR . '/packages/' . $_REQUEST['package']))
  1130. $context['filedata'] = htmlspecialchars(file_get_contents(BOARDDIR . '/packages/' . $_REQUEST['package'] . '/' . $_REQUEST['file']));
  1131. if (strtolower(strrchr($_REQUEST['file'], '.')) == '.php')
  1132. $context['filedata'] = highlight_php_code($context['filedata']);
  1133. }
  1134. }
  1135. /**
  1136. * List the installed packages.
  1137. */
  1138. function InstalledList()
  1139. {
  1140. global $txt, $scripturl, $context;
  1141. $context['page_title'] .= ' - ' . $txt['installed_packages'];
  1142. $context['sub_template'] = 'view_installed';
  1143. // Load the installed mods and send them to the template.
  1144. $context['installed_mods'] = loadInstalledPackages();
  1145. }
  1146. /**
  1147. * Empty out the installed list.
  1148. */
  1149. function FlushInstall()
  1150. {
  1151. global $smcFunc;
  1152. // Always check the session.
  1153. checkSession('get');
  1154. include_once(SUBSDIR . '/Package.subs.php');
  1155. // Record when we last did this.
  1156. package_put_contents(BOARDDIR . '/packages/installed.list', time());
  1157. // Set everything as uninstalled.
  1158. $smcFunc['db_query']('', '
  1159. UPDATE {db_prefix}log_packages
  1160. SET install_state = {int:not_installed}',
  1161. array(
  1162. 'not_installed' => 0,
  1163. )
  1164. );
  1165. redirectexit('action=admin;area=packages;sa=installed');
  1166. }
  1167. /**
  1168. * Delete a package.
  1169. */
  1170. function PackageRemove()
  1171. {
  1172. global $scripturl;
  1173. // Check it.
  1174. checkSession('get');
  1175. // Ack, don't allow deletion of arbitrary files here, could become a security hole somehow!
  1176. if (!isset($_GET['package']) || $_GET['package'] == 'index.php' || $_GET['package'] == 'installed.list' || $_GET['package'] == 'backups')
  1177. redirectexit('action=admin;area=packages;sa=browse');
  1178. $_GET['package'] = preg_replace('~[\.]+~', '.', strtr($_GET['package'], array('/' => '_', '\\' => '_')));
  1179. // Can't delete what's not there.
  1180. if (file_exists(BOARDDIR . '/packages/' . $_GET['package']) && (substr($_GET['package'], -4) == '.zip' || substr($_GET['package'], -4) == '.tgz' || substr($_GET['package'], -7) == '.tar.gz' || is_dir(BOARDDIR . '/packages/' . $_GET['package'])) && $_GET['pac…