PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/forum/Sources/Packages.php

https://github.com/leftnode/nooges.com
PHP | 2245 lines | 1713 code | 261 blank | 271 comment | 391 complexity | 6fe8e851aa842fb8d47aa71606038fe2 MD5 | raw file

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

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

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