PackageGet.php 27 KB

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