PackageGet.php 27 KB

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