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

/sources/admin/PackageServers.php

https://github.com/Arantor/Elkarte
PHP | 764 lines | 529 code | 133 blank | 102 comment | 162 complexity | 0d2b84a6caf92f9f0a237327496fa8b6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file handles the package servers and packages download from Package Manager.
  16. *
  17. */
  18. if (!defined('ELKARTE'))
  19. die('No access...');
  20. /**
  21. * Browse the list of package servers, add servers...
  22. */
  23. function PackageServers()
  24. {
  25. global $txt, $scripturl, $context, $modSettings;
  26. isAllowedTo('admin_forum');
  27. require_once(SUBSDIR . '/Package.subs.php');
  28. // Use the Packages template... no reason to separate.
  29. loadLanguage('Packages');
  30. loadTemplate('Packages', 'admin');
  31. $context['page_title'] = $txt['package'];
  32. // Here is a list of all the potentially valid actions.
  33. $subActions = array(
  34. 'servers' => 'action_servers',
  35. 'add' => 'action_addserver',
  36. 'browse' => 'action_browseserver',
  37. 'download' => 'action_downloadpackage',
  38. 'remove' => 'action_removeserver',
  39. 'upload' => 'action_uploadpackage',
  40. );
  41. // Now let's decide where we are taking this...
  42. if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
  43. $context['sub_action'] = $_REQUEST['sa'];
  44. // We need to support possible old javascript links...
  45. elseif (isset($_GET['pgdownload']))
  46. $context['sub_action'] = 'download';
  47. else
  48. $context['sub_action'] = 'servers';
  49. // We need to force the "Download" tab as selected.
  50. $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'packageget';
  51. // Now create the tabs for the template.
  52. $context[$context['admin_menu_name']]['tab_data'] = array(
  53. 'title' => $txt['package_manager'],
  54. //'help' => 'registrations',
  55. 'description' => $txt['package_manager_desc'],
  56. 'tabs' => array(
  57. 'browse' => array(
  58. ),
  59. 'packageget' => array(
  60. 'description' => $txt['download_packages_desc'],
  61. ),
  62. 'installed' => array(
  63. 'description' => $txt['installed_packages_desc'],
  64. ),
  65. 'perms' => array(
  66. 'description' => $txt['package_file_perms_desc'],
  67. ),
  68. 'options' => array(
  69. 'description' => $txt['package_install_options_ftp_why'],
  70. ),
  71. ),
  72. );
  73. $subActions[$context['sub_action']]();
  74. }
  75. /**
  76. * Load a list of package servers.
  77. */
  78. function action_servers()
  79. {
  80. global $txt, $scripturl, $context, $modSettings, $smcFunc;
  81. // Ensure we use the correct template, and page title.
  82. $context['sub_template'] = 'servers';
  83. $context['page_title'] .= ' - ' . $txt['download_packages'];
  84. // Load the list of servers.
  85. $request = $smcFunc['db_query']('', '
  86. SELECT id_server, name, url
  87. FROM {db_prefix}package_servers',
  88. array(
  89. )
  90. );
  91. $context['servers'] = array();
  92. while ($row = $smcFunc['db_fetch_assoc']($request))
  93. {
  94. $context['servers'][] = array(
  95. 'name' => $row['name'],
  96. 'url' => $row['url'],
  97. 'id' => $row['id_server'],
  98. );
  99. }
  100. $smcFunc['db_free_result']($request);
  101. $context['package_download_broken'] = !is_writable(BOARDDIR . '/packages') || !is_writable(BOARDDIR . '/packages/installed.list');
  102. if ($context['package_download_broken'])
  103. {
  104. @chmod(BOARDDIR . '/packages', 0777);
  105. @chmod(BOARDDIR . '/packages/installed.list', 0777);
  106. }
  107. $context['package_download_broken'] = !is_writable(BOARDDIR . '/packages') || !is_writable(BOARDDIR . '/packages/installed.list');
  108. if ($context['package_download_broken'])
  109. {
  110. if (isset($_POST['ftp_username']))
  111. {
  112. require_once(SUBSDIR . '/FTPConnection.class.php');
  113. $ftp = new Ftp_Connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
  114. if ($ftp->error === false)
  115. {
  116. // I know, I know... but a lot of people want to type /home/xyz/... which is wrong, but logical.
  117. if (!$ftp->chdir($_POST['ftp_path']))
  118. {
  119. $ftp_error = $ftp->error;
  120. $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
  121. }
  122. }
  123. }
  124. if (!isset($ftp) || $ftp->error !== false)
  125. {
  126. if (!isset($ftp))
  127. {
  128. require_once(SUBSDIR . '/FTPConnection.class.php');
  129. $ftp = new Ftp_Connection(null);
  130. }
  131. elseif ($ftp->error !== false && !isset($ftp_error))
  132. $ftp_error = $ftp->last_message === null ? '' : $ftp->last_message;
  133. list ($username, $detect_path, $found_path) = $ftp->detect_path(BOARDDIR);
  134. if ($found_path || !isset($_POST['ftp_path']))
  135. $_POST['ftp_path'] = $detect_path;
  136. if (!isset($_POST['ftp_username']))
  137. $_POST['ftp_username'] = $username;
  138. $context['package_ftp'] = array(
  139. 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'),
  140. 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'),
  141. 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''),
  142. 'path' => $_POST['ftp_path'],
  143. 'error' => empty($ftp_error) ? null : $ftp_error,
  144. );
  145. }
  146. else
  147. {
  148. $context['package_download_broken'] = false;
  149. $ftp->chmod('packages', 0777);
  150. $ftp->chmod('packages/installed.list', 0777);
  151. $ftp->close();
  152. }
  153. }
  154. }
  155. /**
  156. * Browse a server's list of packages.
  157. */
  158. function action_browseserver()
  159. {
  160. global $txt, $boardurl, $context, $scripturl, $forum_version, $context, $smcFunc;
  161. if (isset($_GET['server']))
  162. {
  163. if ($_GET['server'] == '')
  164. redirectexit('action=admin;area=packages;get');
  165. $server = (int) $_GET['server'];
  166. // Query the server list to find the current server.
  167. $request = $smcFunc['db_query']('', '
  168. SELECT name, url
  169. FROM {db_prefix}package_servers
  170. WHERE id_server = {int:current_server}
  171. LIMIT 1',
  172. array(
  173. 'current_server' => $server,
  174. )
  175. );
  176. list ($name, $url) = $smcFunc['db_fetch_row']($request);
  177. $smcFunc['db_free_result']($request);
  178. // If the server does not exist, dump out.
  179. if (empty($url))
  180. fatal_lang_error('couldnt_connect', false);
  181. // If there is a relative link, append to the stored server url.
  182. if (isset($_GET['relative']))
  183. $url = $url . (substr($url, -1) == '/' ? '' : '/') . $_GET['relative'];
  184. // Clear any "absolute" URL. Since "server" is present, "absolute" is garbage.
  185. unset($_GET['absolute']);
  186. }
  187. elseif (isset($_GET['absolute']) && $_GET['absolute'] != '')
  188. {
  189. // Initialize the requried variables.
  190. $server = '';
  191. $url = $_GET['absolute'];
  192. $name = '';
  193. $_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language'];
  194. // Clear any "relative" URL. Since "server" is not present, "relative" is garbage.
  195. unset($_GET['relative']);
  196. $token = checkConfirm('get_absolute_url');
  197. if ($token !== true)
  198. {
  199. $context['sub_template'] = 'package_confirm';
  200. $context['page_title'] = $txt['package_servers'];
  201. $context['confirm_message'] = sprintf($txt['package_confirm_view_package_content'], htmlspecialchars($_GET['absolute']));
  202. $context['proceed_href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . urlencode($_GET['absolute']) . ';confirm=' . $token;
  203. return;
  204. }
  205. }
  206. // Minimum required parameter did not exist so dump out.
  207. else
  208. fatal_lang_error('couldnt_connect', false);
  209. // Attempt to connect. If unsuccessful... try the URL.
  210. if (!isset($_GET['package']) || file_exists($_GET['package']))
  211. $_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language'];
  212. // Check to be sure the packages.xml file actually exists where it is should be... or dump out.
  213. if ((isset($_GET['absolute']) || isset($_GET['relative'])) && !url_exists($_GET['package']))
  214. fatal_lang_error('packageget_unable', false, array($url . '/index.php'));
  215. // Might take some time.
  216. @set_time_limit(600);
  217. // Read packages.xml and parse into Xml_Array. (the true tells it to trim things ;).)
  218. require_once(SUBSDIR . '/XmlArray.class.php');
  219. $listing = new Xml_Array(fetch_web_data($_GET['package']), true);
  220. // Errm.... empty file? Try the URL....
  221. if (!$listing->exists('package-list'))
  222. fatal_lang_error('packageget_unable', false, array($url . '/index.php'));
  223. // List out the packages...
  224. $context['package_list'] = array();
  225. $listing = $listing->path('package-list[0]');
  226. // Use the package list's name if it exists.
  227. if ($listing->exists('list-title'))
  228. $name = $listing->fetch('list-title');
  229. // Pick the correct template.
  230. $context['sub_template'] = 'package_list';
  231. $context['page_title'] = $txt['package_servers'] . ($name != '' ? ' - ' . $name : '');
  232. $context['package_server'] = $server;
  233. // By default we use an unordered list, unless there are no lists with more than one package.
  234. $context['list_type'] = 'ul';
  235. $instmods = loadInstalledPackages();
  236. $installed_mods = array();
  237. // Look through the list of installed mods...
  238. foreach ($instmods as $installed_mod)
  239. $installed_mods[$installed_mod['package_id']] = $installed_mod['version'];
  240. // Get default author and email if they exist.
  241. if ($listing->exists('default-author'))
  242. {
  243. $default_author = $smcFunc['htmlspecialchars']($listing->fetch('default-author'));
  244. if ($listing->exists('default-author/@email'))
  245. $default_email = $smcFunc['htmlspecialchars']($listing->fetch('default-author/@email'));
  246. }
  247. // Get default web site if it exists.
  248. if ($listing->exists('default-website'))
  249. {
  250. $default_website = $smcFunc['htmlspecialchars']($listing->fetch('default-website'));
  251. if ($listing->exists('default-website/@title'))
  252. $default_title = $smcFunc['htmlspecialchars']($listing->fetch('default-website/@title'));
  253. }
  254. $the_version = strtr($forum_version, array('ELKARTE ' => ''));
  255. if (!empty($_SESSION['version_emulate']))
  256. $the_version = $_SESSION['version_emulate'];
  257. $packageNum = 0;
  258. $packageSection = 0;
  259. $sections = $listing->set('section');
  260. foreach ($sections as $i => $section)
  261. {
  262. $context['package_list'][$packageSection] = array(
  263. 'title' => '',
  264. 'text' => '',
  265. 'items' => array(),
  266. );
  267. $packages = $section->set('title|heading|text|remote|rule|modification|language|avatar-pack|theme|smiley-set');
  268. foreach ($packages as $thisPackage)
  269. {
  270. $package = array(
  271. 'type' => $thisPackage->name(),
  272. );
  273. if (in_array($package['type'], array('title', 'text')))
  274. $context['package_list'][$packageSection][$package['type']] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
  275. // It's a Title, Heading, Rule or Text.
  276. elseif (in_array($package['type'], array('heading', 'rule')))
  277. $package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
  278. // It's a Remote link.
  279. elseif ($package['type'] == 'remote')
  280. {
  281. $remote_type = $thisPackage->exists('@type') ? $thisPackage->fetch('@type') : 'relative';
  282. if ($remote_type == 'relative' && substr($thisPackage->fetch('@href'), 0, 7) != 'http://')
  283. {
  284. if (isset($_GET['absolute']))
  285. $current_url = $_GET['absolute'] . '/';
  286. elseif (isset($_GET['relative']))
  287. $current_url = $_GET['relative'] . '/';
  288. else
  289. $current_url = '';
  290. $current_url .= $thisPackage->fetch('@href');
  291. if (isset($_GET['absolute']))
  292. $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url;
  293. else
  294. $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;server=' . $context['package_server'] . ';relative=' . $current_url;
  295. }
  296. else
  297. {
  298. $current_url = $thisPackage->fetch('@href');
  299. $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url;
  300. }
  301. $package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
  302. $package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>';
  303. }
  304. // It's a package...
  305. else
  306. {
  307. if (isset($_GET['absolute']))
  308. $current_url = $_GET['absolute'] . '/';
  309. elseif (isset($_GET['relative']))
  310. $current_url = $_GET['relative'] . '/';
  311. else
  312. $current_url = '';
  313. $server_att = $server != '' ? ';server=' . $server : '';
  314. $package += $thisPackage->to_array();
  315. if (isset($package['website']))
  316. unset($package['website']);
  317. $package['author'] = array();
  318. if ($package['description'] == '')
  319. $package['description'] = $txt['package_no_description'];
  320. else
  321. $package['description'] = parse_bbc(preg_replace('~\[[/]?html\]~i', '', $smcFunc['htmlspecialchars']($package['description'])));
  322. $package['is_installed'] = isset($installed_mods[$package['id']]);
  323. $package['is_current'] = $package['is_installed'] && ($installed_mods[$package['id']] == $package['version']);
  324. $package['is_newer'] = $package['is_installed'] && ($installed_mods[$package['id']] > $package['version']);
  325. // This package is either not installed, or installed but old. Is it supported on this version?
  326. if (!$package['is_installed'] || (!$package['is_current'] && !$package['is_newer']))
  327. {
  328. if ($thisPackage->exists('version/@for'))
  329. $package['can_install'] = matchPackageVersion($the_version, $thisPackage->fetch('version/@for'));
  330. }
  331. // Okay, it's already installed AND up to date.
  332. else
  333. $package['can_install'] = false;
  334. $already_exists = getPackageInfo(basename($package['filename']));
  335. $package['download_conflict'] = is_array($already_exists) && $already_exists['id'] == $package['id'] && $already_exists['version'] != $package['version'];
  336. $package['href'] = $url . '/' . $package['filename'];
  337. $package['name'] = $smcFunc['htmlspecialchars']($package['name']);
  338. $package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>';
  339. $package['download']['href'] = $scripturl . '?action=admin;area=packages;get;sa=download' . $server_att . ';package=' . $current_url . $package['filename'] . ($package['download_conflict'] ? ';conflict' : '') . ';' . $context['session_var'] . '=' . $context['session_id'];
  340. $package['download']['link'] = '<a href="' . $package['download']['href'] . '">' . $package['name'] . '</a>';
  341. if ($thisPackage->exists('author') || isset($default_author))
  342. {
  343. if ($thisPackage->exists('author/@email'))
  344. $package['author']['email'] = $thisPackage->fetch('author/@email');
  345. elseif (isset($default_email))
  346. $package['author']['email'] = $default_email;
  347. if ($thisPackage->exists('author') && $thisPackage->fetch('author') != '')
  348. $package['author']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('author'));
  349. else
  350. $package['author']['name'] = $default_author;
  351. if (!empty($package['author']['email']))
  352. {
  353. // Only put the "mailto:" if it looks like a valid email address. Some may wish to put a link to an IM Form or other web mail form.
  354. $package['author']['href'] = preg_match('~^[\w\.\-]+@[\w][\w\-\.]+[\w]$~', $package['author']['email']) != 0 ? 'mailto:' . $package['author']['email'] : $package['author']['email'];
  355. $package['author']['link'] = '<a href="' . $package['author']['href'] . '">' . $package['author']['name'] . '</a>';
  356. }
  357. }
  358. if ($thisPackage->exists('website') || isset($default_website))
  359. {
  360. if ($thisPackage->exists('website') && $thisPackage->exists('website/@title'))
  361. $package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website/@title'));
  362. elseif (isset($default_title))
  363. $package['author']['website']['name'] = $default_title;
  364. elseif ($thisPackage->exists('website'))
  365. $package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website'));
  366. else
  367. $package['author']['website']['name'] = $default_website;
  368. if ($thisPackage->exists('website') && $thisPackage->fetch('website') != '')
  369. $authorhompage = $thisPackage->fetch('website');
  370. else
  371. $authorhompage = $default_website;
  372. if (stripos($authorhompage, 'a href') === false)
  373. {
  374. $package['author']['website']['href'] = $authorhompage;
  375. $package['author']['website']['link'] = '<a href="' . $authorhompage . '">' . $package['author']['website']['name'] . '</a>';
  376. }
  377. else
  378. {
  379. if (preg_match('/a href="(.+?)"/', $authorhompage, $match) == 1)
  380. $package['author']['website']['href'] = $match[1];
  381. else
  382. $package['author']['website']['href'] = '';
  383. $package['author']['website']['link'] = $authorhompage;
  384. }
  385. }
  386. else
  387. {
  388. $package['author']['website']['href'] = '';
  389. $package['author']['website']['link'] = '';
  390. }
  391. }
  392. $package['is_remote'] = $package['type'] == 'remote';
  393. $package['is_title'] = $package['type'] == 'title';
  394. $package['is_heading'] = $package['type'] == 'heading';
  395. $package['is_text'] = $package['type'] == 'text';
  396. $package['is_line'] = $package['type'] == 'rule';
  397. $packageNum = in_array($package['type'], array('title', 'heading', 'text', 'remote', 'rule')) ? 0 : $packageNum + 1;
  398. $package['count'] = $packageNum;
  399. if (!in_array($package['type'], array('title', 'text')))
  400. $context['package_list'][$packageSection]['items'][] = $package;
  401. if ($package['count'] > 1)
  402. $context['list_type'] = 'ol';
  403. }
  404. $packageSection++;
  405. }
  406. // Lets make sure we get a nice new spiffy clean $package to work with. Otherwise we get PAIN!
  407. unset($package);
  408. foreach ($context['package_list'] as $ps_id => $packageSection)
  409. {
  410. foreach ($packageSection['items'] as $i => $package)
  411. {
  412. if ($package['count'] == 0 || isset($package['can_install']))
  413. continue;
  414. $context['package_list'][$ps_id]['items'][$i]['can_install'] = false;
  415. $packageInfo = getPackageInfo($url . '/' . $package['filename']);
  416. if (is_array($packageInfo) && $packageInfo['xml']->exists('install'))
  417. {
  418. $installs = $packageInfo['xml']->set('install');
  419. foreach ($installs as $install)
  420. if (!$install->exists('@for') || matchPackageVersion($the_version, $install->fetch('@for')))
  421. {
  422. // Okay, this one is good to go.
  423. $context['package_list'][$ps_id]['items'][$i]['can_install'] = true;
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. /**
  431. * Download a package.
  432. */
  433. function action_downloadpackage()
  434. {
  435. global $txt, $scripturl, $context, $smcFunc;
  436. // Use the downloaded sub template.
  437. $context['sub_template'] = 'downloaded';
  438. // Security is good...
  439. checkSession('get');
  440. // To download something, we need a valid server or url.
  441. if (empty($_GET['server']) && (!empty($_GET['get']) && !empty($_REQUEST['package'])))
  442. fatal_lang_error('package_get_error_is_zero', false);
  443. if (isset($_GET['server']))
  444. {
  445. $server = (int) $_GET['server'];
  446. // Query the server table to find the requested server.
  447. $request = $smcFunc['db_query']('', '
  448. SELECT name, url
  449. FROM {db_prefix}package_servers
  450. WHERE id_server = {int:current_server}
  451. LIMIT 1',
  452. array(
  453. 'current_server' => $server,
  454. )
  455. );
  456. list ($name, $url) = $smcFunc['db_fetch_row']($request);
  457. $smcFunc['db_free_result']($request);
  458. // If server does not exist then dump out.
  459. if (empty($url))
  460. fatal_lang_error('couldnt_connect', false);
  461. $url = $url . '/';
  462. }
  463. else
  464. {
  465. // Initialize the requried variables.
  466. $server = '';
  467. $url = '';
  468. }
  469. if (isset($_REQUEST['byurl']) && !empty($_POST['filename']))
  470. $package_name = basename($_REQUEST['filename']);
  471. else
  472. $package_name = basename($_REQUEST['package']);
  473. if (isset($_REQUEST['conflict']) || (isset($_REQUEST['auto']) && file_exists(BOARDDIR . '/packages/' . $package_name)))
  474. {
  475. // Find the extension, change abc.tar.gz to abc_1.tar.gz...
  476. if (strrpos(substr($package_name, 0, -3), '.') !== false)
  477. {
  478. $ext = substr($package_name, strrpos(substr($package_name, 0, -3), '.'));
  479. $package_name = substr($package_name, 0, strrpos(substr($package_name, 0, -3), '.')) . '_';
  480. }
  481. else
  482. $ext = '';
  483. // Find the first available.
  484. $i = 1;
  485. while (file_exists(BOARDDIR . '/packages/' . $package_name . $i . $ext))
  486. $i++;
  487. $package_name = $package_name . $i . $ext;
  488. }
  489. // First make sure it's a package.
  490. $packageInfo = getPackageInfo($url . $_REQUEST['package']);
  491. if (!is_array($packageInfo))
  492. fatal_lang_error($packageInfo);
  493. // Use FTP if necessary.
  494. create_chmod_control(array(BOARDDIR . '/packages/' . $package_name), array('destination_url' => $scripturl . '?action=admin;area=packages;get;sa=download' . (isset($_GET['server']) ? ';server=' . $_GET['server'] : '') . (isset($_REQUEST['auto']) ? ';auto' : '') . ';package=' . $_REQUEST['package'] . (isset($_REQUEST['conflict']) ? ';conflict' : '') . ';' . $context['session_var'] . '=' . $context['session_id'], 'crash_on_error' => true));
  495. package_put_contents(BOARDDIR . '/packages/' . $package_name, fetch_web_data($url . $_REQUEST['package']));
  496. // Done! Did we get this package automatically?
  497. if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['package']) == 1 && strpos($_REQUEST['package'], 'dlattach') === false && isset($_REQUEST['auto']))
  498. redirectexit('action=admin;area=packages;sa=install;package=' . $package_name);
  499. // You just downloaded a mod from SERVER_NAME_GOES_HERE.
  500. $context['package_server'] = $server;
  501. $context['package'] = getPackageInfo($package_name);
  502. if (!is_array($context['package']))
  503. fatal_lang_error('package_cant_download', false);
  504. if ($context['package']['type'] == 'modification')
  505. $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['install_mod'] . ' ]</a>';
  506. elseif ($context['package']['type'] == 'avatar')
  507. $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['use_avatars'] . ' ]</a>';
  508. elseif ($context['package']['type'] == 'language')
  509. $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['add_languages'] . ' ]</a>';
  510. else
  511. $context['package']['install']['link'] = '';
  512. $context['package']['list_files']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $context['package']['filename'] . '">[ ' . $txt['list_files'] . ' ]</a>';
  513. // Free a little bit of memory...
  514. unset($context['package']['xml']);
  515. $context['page_title'] = $txt['download_success'];
  516. }
  517. /**
  518. * Upload a new package to the directory.
  519. */
  520. function action_uploadpackage()
  521. {
  522. global $txt, $scripturl, $context;
  523. // Setup the correct template, even though I'll admit we ain't downloading ;)
  524. $context['sub_template'] = 'downloaded';
  525. // @todo Use FTP if the packages directory is not writable.
  526. // Check the file was even sent!
  527. if (!isset($_FILES['package']['name']) || $_FILES['package']['name'] == '')
  528. fatal_lang_error('package_upload_error_nofile');
  529. elseif (!is_uploaded_file($_FILES['package']['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['package']['tmp_name'])))
  530. fatal_lang_error('package_upload_error_failed');
  531. // Make sure it has a sane filename.
  532. $_FILES['package']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['package']['name']);
  533. if (strtolower(substr($_FILES['package']['name'], -4)) != '.zip' && strtolower(substr($_FILES['package']['name'], -4)) != '.tgz' && strtolower(substr($_FILES['package']['name'], -7)) != '.tar.gz')
  534. fatal_lang_error('package_upload_error_supports', false, array('zip, tgz, tar.gz'));
  535. // We only need the filename...
  536. $packageName = basename($_FILES['package']['name']);
  537. // Setup the destination and throw an error if the file is already there!
  538. $destination = BOARDDIR . '/packages/' . $packageName;
  539. // @todo Maybe just roll it like we do for downloads?
  540. if (file_exists($destination))
  541. fatal_lang_error('package_upload_error_exists');
  542. // Now move the file.
  543. move_uploaded_file($_FILES['package']['tmp_name'], $destination);
  544. @chmod($destination, 0777);
  545. // If we got this far that should mean it's available.
  546. $context['package'] = getPackageInfo($packageName);
  547. $context['package_server'] = '';
  548. // Not really a package, you lazy bum!
  549. if (!is_array($context['package']))
  550. {
  551. @unlink($destination);
  552. loadLanguage('Errors');
  553. $txt[$context['package']] = str_replace('{MANAGETHEMEURL}', $scripturl . '?action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id'] . '#theme_install', $txt[$context['package']]);
  554. fatal_lang_error('package_upload_error_broken', false, $txt[$context['package']]);
  555. }
  556. // Is it already uploaded, maybe?
  557. elseif ($dir = @opendir(BOARDDIR . '/packages'))
  558. {
  559. while ($package = readdir($dir))
  560. {
  561. if ($package == '.' || $package == '..' || $package == 'temp' || $package == $packageName || (!(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'))
  562. continue;
  563. $packageInfo = getPackageInfo($package);
  564. if (!is_array($packageInfo))
  565. continue;
  566. if ($packageInfo['id'] == $context['package']['id'] && $packageInfo['version'] == $context['package']['version'])
  567. {
  568. @unlink($destination);
  569. loadLanguage('Errors');
  570. fatal_lang_error('package_upload_error_exists');
  571. }
  572. }
  573. closedir($dir);
  574. }
  575. if ($context['package']['type'] == 'modification')
  576. $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['install_mod'] . ' ]</a>';
  577. elseif ($context['package']['type'] == 'avatar')
  578. $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['use_avatars'] . ' ]</a>';
  579. elseif ($context['package']['type'] == 'language')
  580. $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['add_languages'] . ' ]</a>';
  581. else
  582. $context['package']['install']['link'] = '';
  583. $context['package']['list_files']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $context['package']['filename'] . '">[ ' . $txt['list_files'] . ' ]</a>';
  584. unset($context['package']['xml']);
  585. $context['page_title'] = $txt['package_uploaded_success'];
  586. }
  587. /**
  588. * Add a package server to the list.
  589. */
  590. function action_addserver()
  591. {
  592. global $smcFunc;
  593. // Validate the user.
  594. checkSession();
  595. // If they put a slash on the end, get rid of it.
  596. if (substr($_POST['serverurl'], -1) == '/')
  597. $_POST['serverurl'] = substr($_POST['serverurl'], 0, -1);
  598. // Are they both nice and clean?
  599. $servername = trim($smcFunc['htmlspecialchars']($_POST['servername']));
  600. $serverurl = trim($smcFunc['htmlspecialchars']($_POST['serverurl']));
  601. // Make sure the URL has the correct prefix.
  602. if (strpos($serverurl, 'http://') !== 0 && strpos($serverurl, 'https://') !== 0)
  603. $serverurl = 'http://' . $serverurl;
  604. $smcFunc['db_insert']('',
  605. '{db_prefix}package_servers',
  606. array(
  607. 'name' => 'string-255', 'url' => 'string-255',
  608. ),
  609. array(
  610. $servername, $serverurl,
  611. ),
  612. array('id_server')
  613. );
  614. redirectexit('action=admin;area=packages;get');
  615. }
  616. /**
  617. * Remove a server from the list.
  618. */
  619. function action_removeserver()
  620. {
  621. global $smcFunc;
  622. checkSession('get');
  623. $smcFunc['db_query']('', '
  624. DELETE FROM {db_prefix}package_servers
  625. WHERE id_server = {int:current_server}',
  626. array(
  627. 'current_server' => (int) $_GET['server'],
  628. )
  629. );
  630. redirectexit('action=admin;area=packages;get');
  631. }