Chris@76: 'PackageServers',
Chris@76: 'add' => 'PackageServerAdd',
Chris@76: 'browse' => 'PackageGBrowse',
Chris@76: 'download' => 'PackageDownload',
Chris@76: 'remove' => 'PackageServerRemove',
Chris@76: 'upload' => 'PackageUpload',
Chris@76: );
Chris@76:
Chris@76: // Now let's decide where we are taking this...
Chris@76: if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
Chris@76: $context['sub_action'] = $_REQUEST['sa'];
Chris@76: // We need to support possible old javascript links...
Chris@76: elseif (isset($_GET['pgdownload']))
Chris@76: $context['sub_action'] = 'download';
Chris@76: else
Chris@76: $context['sub_action'] = 'servers';
Chris@76:
Chris@76: // We need to force the "Download" tab as selected.
Chris@76: $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'packageget';
Chris@76:
Chris@76: // Now create the tabs for the template.
Chris@76: $context[$context['admin_menu_name']]['tab_data'] = array(
Chris@76: 'title' => $txt['package_manager'],
Chris@76: //'help' => 'registrations',
Chris@76: 'description' => $txt['package_manager_desc'],
Chris@76: 'tabs' => array(
Chris@76: 'browse' => array(
Chris@76: ),
Chris@76: 'packageget' => array(
Chris@76: 'description' => $txt['download_packages_desc'],
Chris@76: ),
Chris@76: 'installed' => array(
Chris@76: 'description' => $txt['installed_packages_desc'],
Chris@76: ),
Chris@76: 'perms' => array(
Chris@76: 'description' => $txt['package_file_perms_desc'],
Chris@76: ),
Chris@76: 'options' => array(
Chris@76: 'description' => $txt['package_install_options_ftp_why'],
Chris@76: ),
Chris@76: ),
Chris@76: );
Chris@76:
Chris@76: $subActions[$context['sub_action']]();
Chris@76: }
Chris@76:
Chris@76: function PackageServers()
Chris@76: {
Chris@76: global $txt, $scripturl, $context, $boarddir, $sourcedir, $modSettings, $smcFunc;
Chris@76:
Chris@76: // Ensure we use the correct template, and page title.
Chris@76: $context['sub_template'] = 'servers';
Chris@76: $context['page_title'] .= ' - ' . $txt['download_packages'];
Chris@76:
Chris@76: // Load the list of servers.
Chris@76: $request = $smcFunc['db_query']('', '
Chris@76: SELECT id_server, name, url
Chris@76: FROM {db_prefix}package_servers',
Chris@76: array(
Chris@76: )
Chris@76: );
Chris@76: $context['servers'] = array();
Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76: {
Chris@76: $context['servers'][] = array(
Chris@76: 'name' => $row['name'],
Chris@76: 'url' => $row['url'],
Chris@76: 'id' => $row['id_server'],
Chris@76: );
Chris@76: }
Chris@76: $smcFunc['db_free_result']($request);
Chris@76:
Chris@76: $context['package_download_broken'] = !is_writable($boarddir . '/Packages') || !is_writable($boarddir . '/Packages/installed.list');
Chris@76:
Chris@76: if ($context['package_download_broken'])
Chris@76: {
Chris@76: @chmod($boarddir . '/Packages', 0777);
Chris@76: @chmod($boarddir . '/Packages/installed.list', 0777);
Chris@76: }
Chris@76:
Chris@76: $context['package_download_broken'] = !is_writable($boarddir . '/Packages') || !is_writable($boarddir . '/Packages/installed.list');
Chris@76:
Chris@76: if ($context['package_download_broken'])
Chris@76: {
Chris@76: if (isset($_POST['ftp_username']))
Chris@76: {
Chris@76: loadClassFile('Class-Package.php');
Chris@76: $ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
Chris@76:
Chris@76: if ($ftp->error === false)
Chris@76: {
Chris@76: // I know, I know... but a lot of people want to type /home/xyz/... which is wrong, but logical.
Chris@76: if (!$ftp->chdir($_POST['ftp_path']))
Chris@76: {
Chris@76: $ftp_error = $ftp->error;
Chris@76: $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: if (!isset($ftp) || $ftp->error !== false)
Chris@76: {
Chris@76: if (!isset($ftp))
Chris@76: {
Chris@76: loadClassFile('Class-Package.php');
Chris@76: $ftp = new ftp_connection(null);
Chris@76: }
Chris@76: elseif ($ftp->error !== false && !isset($ftp_error))
Chris@76: $ftp_error = $ftp->last_message === null ? '' : $ftp->last_message;
Chris@76:
Chris@76: list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
Chris@76:
Chris@76: if ($found_path || !isset($_POST['ftp_path']))
Chris@76: $_POST['ftp_path'] = $detect_path;
Chris@76:
Chris@76: if (!isset($_POST['ftp_username']))
Chris@76: $_POST['ftp_username'] = $username;
Chris@76:
Chris@76: $context['package_ftp'] = array(
Chris@76: 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'),
Chris@76: 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'),
Chris@76: 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''),
Chris@76: 'path' => $_POST['ftp_path'],
Chris@76: 'error' => empty($ftp_error) ? null : $ftp_error,
Chris@76: );
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: $context['package_download_broken'] = false;
Chris@76:
Chris@76: $ftp->chmod('Packages', 0777);
Chris@76: $ftp->chmod('Packages/installed.list', 0777);
Chris@76:
Chris@76: $ftp->close();
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Browse a server's list of packages.
Chris@76: function PackageGBrowse()
Chris@76: {
Chris@76: global $txt, $boardurl, $context, $scripturl, $boarddir, $sourcedir, $forum_version, $context, $smcFunc;
Chris@76:
Chris@76: if (isset($_GET['server']))
Chris@76: {
Chris@76: if ($_GET['server'] == '')
Chris@76: redirectexit('action=admin;area=packages;get');
Chris@76:
Chris@76: $server = (int) $_GET['server'];
Chris@76:
Chris@76: // Query the server list to find the current server.
Chris@76: $request = $smcFunc['db_query']('', '
Chris@76: SELECT name, url
Chris@76: FROM {db_prefix}package_servers
Chris@76: WHERE id_server = {int:current_server}
Chris@76: LIMIT 1',
Chris@76: array(
Chris@76: 'current_server' => $server,
Chris@76: )
Chris@76: );
Chris@76: list ($name, $url) = $smcFunc['db_fetch_row']($request);
Chris@76: $smcFunc['db_free_result']($request);
Chris@76:
Chris@76: // If the server does not exist, dump out.
Chris@76: if (empty($url))
Chris@76: fatal_lang_error('couldnt_connect', false);
Chris@76:
Chris@76: // If there is a relative link, append to the stored server url.
Chris@76: if (isset($_GET['relative']))
Chris@76: $url = $url . (substr($url, -1) == '/' ? '' : '/') . $_GET['relative'];
Chris@76:
Chris@76: // Clear any "absolute" URL. Since "server" is present, "absolute" is garbage.
Chris@76: unset($_GET['absolute']);
Chris@76: }
Chris@76: elseif (isset($_GET['absolute']) && $_GET['absolute'] != '')
Chris@76: {
Chris@76: // Initialize the requried variables.
Chris@76: $server = '';
Chris@76: $url = $_GET['absolute'];
Chris@76: $name = '';
Chris@76: $_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language'];
Chris@76:
Chris@76: // Clear any "relative" URL. Since "server" is not present, "relative" is garbage.
Chris@76: unset($_GET['relative']);
Chris@76:
Chris@76: $token = checkConfirm('get_absolute_url');
Chris@76: if ($token !== true)
Chris@76: {
Chris@76: $context['sub_template'] = 'package_confirm';
Chris@76:
Chris@76: $context['page_title'] = $txt['package_servers'];
Chris@76: $context['confirm_message'] = sprintf($txt['package_confirm_view_package_content'], htmlspecialchars($_GET['absolute']));
Chris@76: $context['proceed_href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . urlencode($_GET['absolute']) . ';confirm=' . $token;
Chris@76:
Chris@76: return;
Chris@76: }
Chris@76: }
Chris@76: // Minimum required parameter did not exist so dump out.
Chris@76: else
Chris@76: fatal_lang_error('couldnt_connect', false);
Chris@76:
Chris@76: // Attempt to connect. If unsuccessful... try the URL.
Chris@76: if (!isset($_GET['package']) || file_exists($_GET['package']))
Chris@76: $_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language'];
Chris@76:
Chris@76: // Check to be sure the packages.xml file actually exists where it is should be... or dump out.
Chris@76: if ((isset($_GET['absolute']) || isset($_GET['relative'])) && !url_exists($_GET['package']))
Chris@76: fatal_lang_error('packageget_unable', false, array($url . '/index.php'));
Chris@76:
Chris@76: // Might take some time.
Chris@76: @set_time_limit(600);
Chris@76:
Chris@76: // Read packages.xml and parse into xmlArray. (the true tells it to trim things ;).)
Chris@76: loadClassFile('Class-Package.php');
Chris@76: $listing = new xmlArray(fetch_web_data($_GET['package']), true);
Chris@76:
Chris@76: // Errm.... empty file? Try the URL....
Chris@76: if (!$listing->exists('package-list'))
Chris@76: fatal_lang_error('packageget_unable', false, array($url . '/index.php'));
Chris@76:
Chris@76: // List out the packages...
Chris@76: $context['package_list'] = array();
Chris@76:
Chris@76: $listing = $listing->path('package-list[0]');
Chris@76:
Chris@76: // Use the package list's name if it exists.
Chris@76: if ($listing->exists('list-title'))
Chris@76: $name = $listing->fetch('list-title');
Chris@76:
Chris@76: // Pick the correct template.
Chris@76: $context['sub_template'] = 'package_list';
Chris@76:
Chris@76: $context['page_title'] = $txt['package_servers'] . ($name != '' ? ' - ' . $name : '');
Chris@76: $context['package_server'] = $server;
Chris@76:
Chris@76: // By default we use an unordered list, unless there are no lists with more than one package.
Chris@76: $context['list_type'] = 'ul';
Chris@76:
Chris@76: $instmods = loadInstalledPackages();
Chris@76:
Chris@76: $installed_mods = array();
Chris@76: // Look through the list of installed mods...
Chris@76: foreach ($instmods as $installed_mod)
Chris@76: $installed_mods[$installed_mod['package_id']] = $installed_mod['version'];
Chris@76:
Chris@76: // Get default author and email if they exist.
Chris@76: if ($listing->exists('default-author'))
Chris@76: {
Chris@76: $default_author = $smcFunc['htmlspecialchars']($listing->fetch('default-author'));
Chris@76: if ($listing->exists('default-author/@email'))
Chris@76: $default_email = $smcFunc['htmlspecialchars']($listing->fetch('default-author/@email'));
Chris@76: }
Chris@76:
Chris@76: // Get default web site if it exists.
Chris@76: if ($listing->exists('default-website'))
Chris@76: {
Chris@76: $default_website = $smcFunc['htmlspecialchars']($listing->fetch('default-website'));
Chris@76: if ($listing->exists('default-website/@title'))
Chris@76: $default_title = $smcFunc['htmlspecialchars']($listing->fetch('default-website/@title'));
Chris@76: }
Chris@76:
Chris@76: $the_version = strtr($forum_version, array('SMF ' => ''));
Chris@76: if (!empty($_SESSION['version_emulate']))
Chris@76: $the_version = $_SESSION['version_emulate'];
Chris@76:
Chris@76: $packageNum = 0;
Chris@76: $packageSection = 0;
Chris@76:
Chris@76: $sections = $listing->set('section');
Chris@76: foreach ($sections as $i => $section)
Chris@76: {
Chris@76: $context['package_list'][$packageSection] = array(
Chris@76: 'title' => '',
Chris@76: 'text' => '',
Chris@76: 'items' => array(),
Chris@76: );
Chris@76:
Chris@76: $packages = $section->set('title|heading|text|remote|rule|modification|language|avatar-pack|theme|smiley-set');
Chris@76: foreach ($packages as $thisPackage)
Chris@76: {
Chris@76: $package = array(
Chris@76: 'type' => $thisPackage->name(),
Chris@76: );
Chris@76:
Chris@76: if (in_array($package['type'], array('title', 'text')))
Chris@76: $context['package_list'][$packageSection][$package['type']] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
Chris@76: // It's a Title, Heading, Rule or Text.
Chris@76: elseif (in_array($package['type'], array('heading', 'rule')))
Chris@76: $package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
Chris@76: // It's a Remote link.
Chris@76: elseif ($package['type'] == 'remote')
Chris@76: {
Chris@76: $remote_type = $thisPackage->exists('@type') ? $thisPackage->fetch('@type') : 'relative';
Chris@76:
Chris@76: if ($remote_type == 'relative' && substr($thisPackage->fetch('@href'), 0, 7) != 'http://')
Chris@76: {
Chris@76: if (isset($_GET['absolute']))
Chris@76: $current_url = $_GET['absolute'] . '/';
Chris@76: elseif (isset($_GET['relative']))
Chris@76: $current_url = $_GET['relative'] . '/';
Chris@76: else
Chris@76: $current_url = '';
Chris@76:
Chris@76: $current_url .= $thisPackage->fetch('@href');
Chris@76: if (isset($_GET['absolute']))
Chris@76: $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url;
Chris@76: else
Chris@76: $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;server=' . $context['package_server'] . ';relative=' . $current_url;
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: $current_url = $thisPackage->fetch('@href');
Chris@76: $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url;
Chris@76: }
Chris@76:
Chris@76: $package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.'));
Chris@76: $package['link'] = '' . $package['name'] . '';
Chris@76: }
Chris@76: // It's a package...
Chris@76: else
Chris@76: {
Chris@76: if (isset($_GET['absolute']))
Chris@76: $current_url = $_GET['absolute'] . '/';
Chris@76: elseif (isset($_GET['relative']))
Chris@76: $current_url = $_GET['relative'] . '/';
Chris@76: else
Chris@76: $current_url = '';
Chris@76:
Chris@76: $server_att = $server != '' ? ';server=' . $server : '';
Chris@76:
Chris@76: $package += $thisPackage->to_array();
Chris@76:
Chris@76: if (isset($package['website']))
Chris@76: unset($package['website']);
Chris@76: $package['author'] = array();
Chris@76:
Chris@76: if ($package['description'] == '')
Chris@76: $package['description'] = $txt['package_no_description'];
Chris@76: else
Chris@76: $package['description'] = parse_bbc(preg_replace('~\[[/]?html\]~i', '', $smcFunc['htmlspecialchars']($package['description'])));
Chris@76:
Chris@76: $package['is_installed'] = isset($installed_mods[$package['id']]);
Chris@76: $package['is_current'] = $package['is_installed'] && ($installed_mods[$package['id']] == $package['version']);
Chris@76: $package['is_newer'] = $package['is_installed'] && ($installed_mods[$package['id']] > $package['version']);
Chris@76:
Chris@76: // This package is either not installed, or installed but old. Is it supported on this version of SMF?
Chris@76: if (!$package['is_installed'] || (!$package['is_current'] && !$package['is_newer']))
Chris@76: {
Chris@76: if ($thisPackage->exists('version/@for'))
Chris@76: $package['can_install'] = matchPackageVersion($the_version, $thisPackage->fetch('version/@for'));
Chris@76: }
Chris@76: // Okay, it's already installed AND up to date.
Chris@76: else
Chris@76: $package['can_install'] = false;
Chris@76:
Chris@76: $already_exists = getPackageInfo(basename($package['filename']));
Chris@76: $package['download_conflict'] = is_array($already_exists) && $already_exists['id'] == $package['id'] && $already_exists['version'] != $package['version'];
Chris@76:
Chris@76: $package['href'] = $url . '/' . $package['filename'];
Chris@76: $package['name'] = $smcFunc['htmlspecialchars']($package['name']);
Chris@76: $package['link'] = '' . $package['name'] . '';
Chris@76: $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'];
Chris@76: $package['download']['link'] = '' . $package['name'] . '';
Chris@76:
Chris@76: if ($thisPackage->exists('author') || isset($default_author))
Chris@76: {
Chris@76: if ($thisPackage->exists('author/@email'))
Chris@76: $package['author']['email'] = $thisPackage->fetch('author/@email');
Chris@76: elseif (isset($default_email))
Chris@76: $package['author']['email'] = $default_email;
Chris@76:
Chris@76: if ($thisPackage->exists('author') && $thisPackage->fetch('author') != '')
Chris@76: $package['author']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('author'));
Chris@76: else
Chris@76: $package['author']['name'] = $default_author;
Chris@76:
Chris@76: if (!empty($package['author']['email']))
Chris@76: {
Chris@76: // 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.
Chris@76: $package['author']['href'] = preg_match('~^[\w\.\-]+@[\w][\w\-\.]+[\w]$~', $package['author']['email']) != 0 ? 'mailto:' . $package['author']['email'] : $package['author']['email'];
Chris@76: $package['author']['link'] = '' . $package['author']['name'] . '';
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: if ($thisPackage->exists('website') || isset($default_website))
Chris@76: {
Chris@76: if ($thisPackage->exists('website') && $thisPackage->exists('website/@title'))
Chris@76: $package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website/@title'));
Chris@76: elseif (isset($default_title))
Chris@76: $package['author']['website']['name'] = $default_title;
Chris@76: elseif ($thisPackage->exists('website'))
Chris@76: $package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website'));
Chris@76: else
Chris@76: $package['author']['website']['name'] = $default_website;
Chris@76:
Chris@76: if ($thisPackage->exists('website') && $thisPackage->fetch('website') != '')
Chris@76: $authorhompage = $thisPackage->fetch('website');
Chris@76: else
Chris@76: $authorhompage = $default_website;
Chris@76:
Chris@76: if (strpos(strtolower($authorhompage), 'a href') === false)
Chris@76: {
Chris@76: $package['author']['website']['href'] = $authorhompage;
Chris@76: $package['author']['website']['link'] = '' . $package['author']['website']['name'] . '';
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: if (preg_match('/a href="(.+?)"/', $authorhompage, $match) == 1)
Chris@76: $package['author']['website']['href'] = $match[1];
Chris@76: else
Chris@76: $package['author']['website']['href'] = '';
Chris@76: $package['author']['website']['link'] = $authorhompage;
Chris@76: }
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: $package['author']['website']['href'] = '';
Chris@76: $package['author']['website']['link'] = '';
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: $package['is_remote'] = $package['type'] == 'remote';
Chris@76: $package['is_title'] = $package['type'] == 'title';
Chris@76: $package['is_heading'] = $package['type'] == 'heading';
Chris@76: $package['is_text'] = $package['type'] == 'text';
Chris@76: $package['is_line'] = $package['type'] == 'rule';
Chris@76:
Chris@76: $packageNum = in_array($package['type'], array('title', 'heading', 'text', 'remote', 'rule')) ? 0 : $packageNum + 1;
Chris@76: $package['count'] = $packageNum;
Chris@76:
Chris@76: if (!in_array($package['type'], array('title', 'text')))
Chris@76: $context['package_list'][$packageSection]['items'][] = $package;
Chris@76:
Chris@76: if ($package['count'] > 1)
Chris@76: $context['list_type'] = 'ol';
Chris@76: }
Chris@76:
Chris@76: $packageSection++;
Chris@76: }
Chris@76:
Chris@76: // Lets make sure we get a nice new spiffy clean $package to work with. Otherwise we get PAIN!
Chris@76: unset($package);
Chris@76:
Chris@76: foreach ($context['package_list'] as $ps_id => $packageSection)
Chris@76: {
Chris@76: foreach ($packageSection['items'] as $i => $package)
Chris@76: {
Chris@76: if ($package['count'] == 0 || isset($package['can_install']))
Chris@76: continue;
Chris@76:
Chris@76: $context['package_list'][$ps_id]['items'][$i]['can_install'] = false;
Chris@76:
Chris@76: $packageInfo = getPackageInfo($url . '/' . $package['filename']);
Chris@76: if (is_array($packageInfo) && $packageInfo['xml']->exists('install'))
Chris@76: {
Chris@76: $installs = $packageInfo['xml']->set('install');
Chris@76: foreach ($installs as $install)
Chris@76: if (!$install->exists('@for') || matchPackageVersion($the_version, $install->fetch('@for')))
Chris@76: {
Chris@76: // Okay, this one is good to go.
Chris@76: $context['package_list'][$ps_id]['items'][$i]['can_install'] = true;
Chris@76: break;
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Download a package.
Chris@76: function PackageDownload()
Chris@76: {
Chris@76: global $txt, $scripturl, $boarddir, $context, $sourcedir, $smcFunc;
Chris@76:
Chris@76: // Use the downloaded sub template.
Chris@76: $context['sub_template'] = 'downloaded';
Chris@76:
Chris@76: // Security is good...
Chris@76: checkSession('get');
Chris@76:
Chris@76: // To download something, we need a valid server or url.
Chris@76: if (empty($_GET['server']) && (!empty($_GET['get']) && !empty($_REQUEST['package'])))
Chris@76: fatal_lang_error('package_get_error_is_zero', false);
Chris@76:
Chris@76: if (isset($_GET['server']))
Chris@76: {
Chris@76: $server = (int) $_GET['server'];
Chris@76:
Chris@76: // Query the server table to find the requested server.
Chris@76: $request = $smcFunc['db_query']('', '
Chris@76: SELECT name, url
Chris@76: FROM {db_prefix}package_servers
Chris@76: WHERE id_server = {int:current_server}
Chris@76: LIMIT 1',
Chris@76: array(
Chris@76: 'current_server' => $server,
Chris@76: )
Chris@76: );
Chris@76: list ($name, $url) = $smcFunc['db_fetch_row']($request);
Chris@76: $smcFunc['db_free_result']($request);
Chris@76:
Chris@76: // If server does not exist then dump out.
Chris@76: if (empty($url))
Chris@76: fatal_lang_error('couldnt_connect', false);
Chris@76:
Chris@76: $url = $url . '/';
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: // Initialize the requried variables.
Chris@76: $server = '';
Chris@76: $url = '';
Chris@76: }
Chris@76:
Chris@76: if (isset($_REQUEST['byurl']) && !empty($_POST['filename']))
Chris@76: $package_name = basename($_REQUEST['filename']);
Chris@76: else
Chris@76: $package_name = basename($_REQUEST['package']);
Chris@76:
Chris@76: if (isset($_REQUEST['conflict']) || (isset($_REQUEST['auto']) && file_exists($boarddir . '/Packages/' . $package_name)))
Chris@76: {
Chris@76: // Find the extension, change abc.tar.gz to abc_1.tar.gz...
Chris@76: if (strrpos(substr($package_name, 0, -3), '.') !== false)
Chris@76: {
Chris@76: $ext = substr($package_name, strrpos(substr($package_name, 0, -3), '.'));
Chris@76: $package_name = substr($package_name, 0, strrpos(substr($package_name, 0, -3), '.')) . '_';
Chris@76: }
Chris@76: else
Chris@76: $ext = '';
Chris@76:
Chris@76: // Find the first available.
Chris@76: $i = 1;
Chris@76: while (file_exists($boarddir . '/Packages/' . $package_name . $i . $ext))
Chris@76: $i++;
Chris@76:
Chris@76: $package_name = $package_name . $i . $ext;
Chris@76: }
Chris@76:
Chris@76: // First make sure it's a package.
Chris@76: $packageInfo = getPackageInfo($url . $_REQUEST['package']);
Chris@76: if (!is_array($packageInfo))
Chris@76: fatal_lang_error($packageInfo);
Chris@76:
Chris@76: // Use FTP if necessary.
Chris@76: 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));
Chris@76: package_put_contents($boarddir . '/Packages/' . $package_name, fetch_web_data($url . $_REQUEST['package']));
Chris@76:
Chris@76: // Done! Did we get this package automatically?
Chris@76: if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['package']) == 1 && strpos($_REQUEST['package'], 'dlattach') === false && isset($_REQUEST['auto']))
Chris@76: redirectexit('action=admin;area=packages;sa=install;package=' . $package_name);
Chris@76:
Chris@76: // You just downloaded a mod from SERVER_NAME_GOES_HERE.
Chris@76: $context['package_server'] = $server;
Chris@76:
Chris@76: $context['package'] = getPackageInfo($package_name);
Chris@76:
Chris@76: if (!is_array($context['package']))
Chris@76: fatal_lang_error('package_cant_download', false);
Chris@76:
Chris@76: if ($context['package']['type'] == 'modification')
Chris@76: $context['package']['install']['link'] = '[ ' . $txt['install_mod'] . ' ]';
Chris@76: elseif ($context['package']['type'] == 'avatar')
Chris@76: $context['package']['install']['link'] = '[ ' . $txt['use_avatars'] . ' ]';
Chris@76: elseif ($context['package']['type'] == 'language')
Chris@76: $context['package']['install']['link'] = '[ ' . $txt['add_languages'] . ' ]';
Chris@76: else
Chris@76: $context['package']['install']['link'] = '';
Chris@76:
Chris@76: $context['package']['list_files']['link'] = '[ ' . $txt['list_files'] . ' ]';
Chris@76:
Chris@76: // Free a little bit of memory...
Chris@76: unset($context['package']['xml']);
Chris@76:
Chris@76: $context['page_title'] = $txt['download_success'];
Chris@76: }
Chris@76:
Chris@76: // Upload a new package to the directory.
Chris@76: function PackageUpload()
Chris@76: {
Chris@76: global $txt, $scripturl, $boarddir, $context, $sourcedir;
Chris@76:
Chris@76: // Setup the correct template, even though I'll admit we ain't downloading ;)
Chris@76: $context['sub_template'] = 'downloaded';
Chris@76:
Chris@76: // !!! TODO: Use FTP if the Packages directory is not writable.
Chris@76:
Chris@76: // Check the file was even sent!
Chris@76: if (!isset($_FILES['package']['name']) || $_FILES['package']['name'] == '')
Chris@76: fatal_lang_error('package_upload_error_nofile');
Chris@76: elseif (!is_uploaded_file($_FILES['package']['tmp_name']) || (@ini_get('open_basedir') == '' && !file_exists($_FILES['package']['tmp_name'])))
Chris@76: fatal_lang_error('package_upload_error_failure');
Chris@76:
Chris@76: // Make sure it has a sane filename.
Chris@76: $_FILES['package']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['package']['name']);
Chris@76:
Chris@76: if (strtolower(substr($_FILES['package']['name'], -4)) != '.zip' && strtolower(substr($_FILES['package']['name'], -4)) != '.tgz' && strtolower(substr($_FILES['package']['name'], -7)) != '.tar.gz')
Chris@76: fatal_lang_error('package_upload_error_supports', false, array('zip, tgz, tar.gz'));
Chris@76:
Chris@76: // We only need the filename...
Chris@76: $packageName = basename($_FILES['package']['name']);
Chris@76:
Chris@76: // Setup the destination and throw an error if the file is already there!
Chris@76: $destination = $boarddir . '/Packages/' . $packageName;
Chris@76: // !!! Maybe just roll it like we do for downloads?
Chris@76: if (file_exists($destination))
Chris@76: fatal_lang_error('package_upload_error_exists');
Chris@76:
Chris@76: // Now move the file.
Chris@76: move_uploaded_file($_FILES['package']['tmp_name'], $destination);
Chris@76: @chmod($destination, 0777);
Chris@76:
Chris@76: // If we got this far that should mean it's available.
Chris@76: $context['package'] = getPackageInfo($packageName);
Chris@76: $context['package_server'] = '';
Chris@76:
Chris@76: // Not really a package, you lazy bum!
Chris@76: if (!is_array($context['package']))
Chris@76: {
Chris@76: @unlink($destination);
Chris@76: loadLanguage('Errors');
Chris@76: fatal_lang_error('package_upload_error_broken', false, $txt[$context['package']]);
Chris@76: }
Chris@76: // Is it already uploaded, maybe?
Chris@76: elseif ($dir = @opendir($boarddir . '/Packages'))
Chris@76: {
Chris@76: while ($package = readdir($dir))
Chris@76: {
Chris@76: 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'))
Chris@76: continue;
Chris@76:
Chris@76: $packageInfo = getPackageInfo($package);
Chris@76: if (!is_array($packageInfo))
Chris@76: continue;
Chris@76:
Chris@76: if ($packageInfo['id'] == $context['package']['id'] && $packageInfo['version'] == $context['package']['version'])
Chris@76: {
Chris@76: @unlink($destination);
Chris@76: loadLanguage('Errors');
Chris@76: fatal_lang_error('package_upload_error_exists');
Chris@76: }
Chris@76: }
Chris@76: closedir($dir);
Chris@76: }
Chris@76:
Chris@76: if ($context['package']['type'] == 'modification')
Chris@76: $context['package']['install']['link'] = '[ ' . $txt['install_mod'] . ' ]';
Chris@76: elseif ($context['package']['type'] == 'avatar')
Chris@76: $context['package']['install']['link'] = '[ ' . $txt['use_avatars'] . ' ]';
Chris@76: elseif ($context['package']['type'] == 'language')
Chris@76: $context['package']['install']['link'] = '[ ' . $txt['add_languages'] . ' ]';
Chris@76: else
Chris@76: $context['package']['install']['link'] = '';
Chris@76:
Chris@76: $context['package']['list_files']['link'] = '[ ' . $txt['list_files'] . ' ]';
Chris@76:
Chris@76: unset($context['package']['xml']);
Chris@76:
Chris@76: $context['page_title'] = $txt['package_uploaded_success'];
Chris@76: }
Chris@76:
Chris@76: // Add a package server to the list.
Chris@76: function PackageServerAdd()
Chris@76: {
Chris@76: global $smcFunc;
Chris@76:
Chris@76: // Validate the user.
Chris@76: checkSession();
Chris@76:
Chris@76: // If they put a slash on the end, get rid of it.
Chris@76: if (substr($_POST['serverurl'], -1) == '/')
Chris@76: $_POST['serverurl'] = substr($_POST['serverurl'], 0, -1);
Chris@76:
Chris@76: // Are they both nice and clean?
Chris@76: $servername = trim($smcFunc['htmlspecialchars']($_POST['servername']));
Chris@76: $serverurl = trim($smcFunc['htmlspecialchars']($_POST['serverurl']));
Chris@76:
Chris@76: // Make sure the URL has the correct prefix.
Chris@76: if (strpos($serverurl, 'http://') !== 0 && strpos($serverurl, 'https://') !== 0)
Chris@76: $serverurl = 'http://' . $serverurl;
Chris@76:
Chris@76: $smcFunc['db_insert']('',
Chris@76: '{db_prefix}package_servers',
Chris@76: array(
Chris@76: 'name' => 'string-255', 'url' => 'string-255',
Chris@76: ),
Chris@76: array(
Chris@76: $servername, $serverurl,
Chris@76: ),
Chris@76: array('id_server')
Chris@76: );
Chris@76:
Chris@76: redirectexit('action=admin;area=packages;get');
Chris@76: }
Chris@76:
Chris@76: // Remove a server from the list.
Chris@76: function PackageServerRemove()
Chris@76: {
Chris@76: global $smcFunc;
Chris@76:
Chris@76: checkSession('get');
Chris@76:
Chris@76: $smcFunc['db_query']('', '
Chris@76: DELETE FROM {db_prefix}package_servers
Chris@76: WHERE id_server = {int:current_server}',
Chris@76: array(
Chris@76: 'current_server' => (int) $_GET['server'],
Chris@76: )
Chris@76: );
Chris@76:
Chris@76: redirectexit('action=admin;area=packages;get');
Chris@76: }
Chris@76:
Chris@76: ?>