2
0

PackageGet.php 27 KB

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