PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/Sources/Packages.php

https://github.com/smf-portal/SMF2.1
PHP | 2669 lines | 2140 code | 259 blank | 270 comment | 426 complexity | 150a0a546f8c3d2987075728e2ede4e3 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file