/**
  * Download a package.
  *
  * - Accessed by action=admin;area=packageservers;sa=download
  */
 public function action_download()
 {
     global $txt, $scripturl, $context;
     require_once SUBSDIR . '/PackageServers.subs.php';
     // Use the downloaded sub template.
     $context['sub_template'] = 'downloaded';
     // Security is good...
     checkSession('get');
     // To download something, we need a valid server or url.
     if (empty($_GET['server']) && (!empty($_GET['get']) && !empty($_REQUEST['package']))) {
         fatal_lang_error('package_get_error_is_zero', false);
     }
     if (isset($_GET['server'])) {
         $server = (int) $_GET['server'];
         // Query the server table to find the requested server.
         $packageserver = fetchPackageServers($server);
         $url = $packageserver[0]['url'];
         // If server does not exist then dump out.
         if (empty($url)) {
             fatal_lang_error('couldnt_connect', false);
         }
         $url = $url . '/';
     } else {
         // Initialize the required variables.
         $server = '';
         $url = '';
     }
     // Entered a url and name to download?
     if (isset($_REQUEST['byurl']) && !empty($_POST['filename'])) {
         $package_name = basename($_REQUEST['filename']);
     } else {
         $package_name = basename($_REQUEST['package']);
     }
     // Is this a "master" package from github or bitbucket?
     if (preg_match('~^http(s)?://(www.)?(bitbucket\\.org|github\\.com)/(.+?(master(\\.zip|\\.tar\\.gz)))$~', $_REQUEST['package'], $matches) == 1) {
         // @todo maybe use the name/version in the package instead, although the link will be cleaner
         // Name this master.zip based on repo name in the link
         $path_parts = pathinfo($matches[4]);
         list(, $newname, ) = explode('/', $path_parts['dirname']);
         // Just to be safe, no invalid file characters
         $invalid = array_merge(array_map('chr', range(0, 31)), array('<', '>', ':', '"', '/', '\\', '|', '?', '*'));
         $package_name = str_replace($invalid, '_', $newname) . $matches[6];
         // We could read the package info and see if we have a duplicate id & version, however that is
         // not always accurate, especially when dealing with repos.  So for now just put in in no conflict mode
         // and do the save.
         $_REQUEST['auto'] = true;
     }
     if (isset($_REQUEST['conflict']) || isset($_REQUEST['auto']) && file_exists(BOARDDIR . '/packages/' . $package_name)) {
         // Find the extension, change abc.tar.gz to abc_1.tar.gz...
         if (strrpos(substr($package_name, 0, -3), '.') !== false) {
             $ext = substr($package_name, strrpos(substr($package_name, 0, -3), '.'));
             $package_name = substr($package_name, 0, strrpos(substr($package_name, 0, -3), '.')) . '_';
         } else {
             $ext = '';
         }
         // Find the first available.
         $i = 1;
         while (file_exists(BOARDDIR . '/packages/' . $package_name . $i . $ext)) {
             $i++;
         }
         $package_name = $package_name . $i . $ext;
     }
     // First make sure it's a package.
     $packageInfo = getPackageInfo($url . $_REQUEST['package']);
     if (!is_array($packageInfo)) {
         fatal_lang_error($packageInfo);
     }
     // Save the package to disk, use FTP if necessary
     create_chmod_control(array(BOARDDIR . '/packages/' . $package_name), array('destination_url' => $scripturl . '?action=admin;area=packageservers;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));
     package_put_contents(BOARDDIR . '/packages/' . $package_name, fetch_web_data($url . $_REQUEST['package']));
     // Done!  Did we get this package automatically?
     if (preg_match('~^http://[\\w_\\-]+\\.elkarte\\.net/~', $_REQUEST['package']) == 1 && strpos($_REQUEST['package'], 'dlattach') === false && isset($_REQUEST['auto'])) {
         redirectexit('action=admin;area=packages;sa=install;package=' . $package_name);
     }
     // You just downloaded a addon from SERVER_NAME_GOES_HERE.
     $context['package_server'] = $server;
     // Read in the newly saved package information
     $context['package'] = getPackageInfo($package_name);
     if (!is_array($context['package'])) {
         fatal_lang_error('package_cant_download', false);
     }
     if ($context['package']['type'] == 'modification') {
         $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['install_mod'] . ' ]</a>';
     } elseif ($context['package']['type'] == 'avatar') {
         $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['use_avatars'] . ' ]</a>';
     } elseif ($context['package']['type'] == 'language') {
         $context['package']['install']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=install;package=' . $context['package']['filename'] . '">[ ' . $txt['add_languages'] . ' ]</a>';
     } else {
         $context['package']['install']['link'] = '';
     }
     $context['package']['list_files']['link'] = '<a href="' . $scripturl . '?action=admin;area=packages;sa=list;package=' . $context['package']['filename'] . '">[ ' . $txt['list_files'] . ' ]</a>';
     // Free a little bit of memory...
     unset($context['package']['xml']);
     $context['page_title'] = $txt['download_success'];
 }
Exemplo n.º 2
0
 /**
  * Returns the contact details for a server
  *
  * - Reads the database to fetch the server url and name
  *
  * @return array
  */
 private function _package_server()
 {
     // Initialize the required variables.
     $name = '';
     $url = '';
     $server = '';
     if (isset($_GET['server'])) {
         if ($_GET['server'] == '') {
             redirectexit('action=admin;area=packageservers');
         }
         $server = (int) $_GET['server'];
         // Query the server table to find the requested server.
         $packageserver = fetchPackageServers($server);
         $url = $packageserver[0]['url'];
         $name = $packageserver[0]['name'];
         // If server does not exist then dump out.
         if (empty($url)) {
             fatal_lang_error('couldnt_connect', false);
         }
     }
     return array($name, $url, $server);
 }