PageRenderTime 69ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/php/Sources/Packages.php

https://github.com/dekoza/openshift-smf-2.0.7
PHP | 2262 lines | 1775 code | 239 blank | 248 comment | 385 complexity | cb31a21519b75ef18993d2cae73d8489 MD5 | raw file
Possible License(s): BSD-3-Clause

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

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

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