[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/installer/ -> helper.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Installer
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  jimport('joomla.filesystem.file');
  13  jimport('joomla.filesystem.folder');
  14  jimport('joomla.filesystem.archive');
  15  jimport('joomla.filesystem.path');
  16  
  17  /**
  18   * Installer helper class
  19   *
  20   * @package     Joomla.Platform
  21   * @subpackage  Installer
  22   * @since       11.1
  23   */
  24  abstract class JInstallerHelper
  25  {
  26      /**
  27       * Downloads a package
  28       *
  29       * @param   string  $url     URL of file to download
  30       * @param   string  $target  Download target filename [optional]
  31       *
  32       * @return  mixed  Path to downloaded package or boolean false on failure
  33       *
  34       * @since   11.1
  35       */
  36  	public static function downloadPackage($url, $target = false)
  37      {
  38          $config = JFactory::getConfig();
  39  
  40          // Capture PHP errors
  41          $php_errormsg = 'Error Unknown';
  42          $track_errors = ini_get('track_errors');
  43          ini_set('track_errors', true);
  44  
  45          // Set user agent
  46          $version = new JVersion;
  47          ini_set('user_agent', $version->getUserAgent('Installer'));
  48  
  49          // Open the remote server socket for reading
  50          $inputHandle = @ fopen($url, "r");
  51          $error = strstr($php_errormsg, 'failed to open stream:');
  52          if (!$inputHandle)
  53          {
  54              JError::raiseWarning(42, JText::sprintf('JLIB_INSTALLER_ERROR_DOWNLOAD_SERVER_CONNECT', $error));
  55              return false;
  56          }
  57  
  58          $meta_data = stream_get_meta_data($inputHandle);
  59          foreach ($meta_data['wrapper_data'] as $wrapper_data)
  60          {
  61              if (substr($wrapper_data, 0, strlen("Content-Disposition")) == "Content-Disposition")
  62              {
  63                  $contentfilename = explode("\"", $wrapper_data);
  64                  $target = $contentfilename[1];
  65              }
  66          }
  67  
  68          // Set the target path if not given
  69          if (!$target)
  70          {
  71              $target = $config->get('tmp_path') . '/' . self::getFilenameFromURL($url);
  72          }
  73          else
  74          {
  75              $target = $config->get('tmp_path') . '/' . basename($target);
  76          }
  77  
  78          // Initialise contents buffer
  79          $contents = null;
  80  
  81          while (!feof($inputHandle))
  82          {
  83              $contents .= fread($inputHandle, 4096);
  84              if ($contents === false)
  85              {
  86                  JError::raiseWarning(44, JText::sprintf('JLIB_INSTALLER_ERROR_FAILED_READING_NETWORK_RESOURCES', $php_errormsg));
  87                  return false;
  88              }
  89          }
  90  
  91          // Write buffer to file
  92          JFile::write($target, $contents);
  93  
  94          // Close file pointer resource
  95          fclose($inputHandle);
  96  
  97          // Restore error tracking to what it was before
  98          ini_set('track_errors', $track_errors);
  99  
 100          // bump the max execution time because not using built in php zip libs are slow
 101          @set_time_limit(ini_get('max_execution_time'));
 102  
 103          // Return the name of the downloaded package
 104          return basename($target);
 105      }
 106  
 107      /**
 108       * Unpacks a file and verifies it as a Joomla element package
 109       * Supports .gz .tar .tar.gz and .zip
 110       *
 111       * @param   string  $p_filename  The uploaded package filename or install directory
 112       *
 113       * @return  array  Two elements: extractdir and packagefile
 114       *
 115       * @since   11.1
 116       */
 117  	public static function unpack($p_filename)
 118      {
 119          // Path to the archive
 120          $archivename = $p_filename;
 121  
 122          // Temporary folder to extract the archive into
 123          $tmpdir = uniqid('install_');
 124  
 125          // Clean the paths to use for archive extraction
 126          $extractdir = JPath::clean(dirname($p_filename) . '/' . $tmpdir);
 127          $archivename = JPath::clean($archivename);
 128  
 129          // Do the unpacking of the archive
 130          $result = JArchive::extract($archivename, $extractdir);
 131  
 132          if ($result === false)
 133          {
 134              return false;
 135          }
 136  
 137          /*
 138           * Let's set the extraction directory and package file in the result array so we can
 139           * cleanup everything properly later on.
 140           */
 141          $retval['extractdir'] = $extractdir;
 142          $retval['packagefile'] = $archivename;
 143  
 144          /*
 145           * Try to find the correct install directory.  In case the package is inside a
 146           * subdirectory detect this and set the install directory to the correct path.
 147           *
 148           * List all the items in the installation directory.  If there is only one, and
 149           * it is a folder, then we will set that folder to be the installation folder.
 150           */
 151          $dirList = array_merge(JFolder::files($extractdir, ''), JFolder::folders($extractdir, ''));
 152  
 153          if (count($dirList) == 1)
 154          {
 155              if (JFolder::exists($extractdir . '/' . $dirList[0]))
 156              {
 157                  $extractdir = JPath::clean($extractdir . '/' . $dirList[0]);
 158              }
 159          }
 160  
 161          /*
 162           * We have found the install directory so lets set it and then move on
 163           * to detecting the extension type.
 164           */
 165          $retval['dir'] = $extractdir;
 166  
 167          /*
 168           * Get the extension type and return the directory/type array on success or
 169           * false on fail.
 170           */
 171          if ($retval['type'] = self::detectType($extractdir))
 172          {
 173              return $retval;
 174          }
 175          else
 176          {
 177              return false;
 178          }
 179      }
 180  
 181      /**
 182       * Method to detect the extension type from a package directory
 183       *
 184       * @param   string  $p_dir  Path to package directory
 185       *
 186       * @return  mixed  Extension type string or boolean false on fail
 187       *
 188       * @since   11.1
 189       */
 190  	public static function detectType($p_dir)
 191      {
 192          // Search the install dir for an XML file
 193          $files = JFolder::files($p_dir, '\.xml$', 1, true);
 194  
 195          if (!count($files))
 196          {
 197              JError::raiseWarning(1, JText::_('JLIB_INSTALLER_ERROR_NOTFINDXMLSETUPFILE'));
 198              return false;
 199          }
 200  
 201          foreach ($files as $file)
 202          {
 203              if (!$xml = JFactory::getXML($file))
 204              {
 205                  continue;
 206              }
 207  
 208              if ($xml->getName() != 'install' && $xml->getName() != 'extension')
 209              {
 210                  unset($xml);
 211                  continue;
 212              }
 213  
 214              $type = (string) $xml->attributes()->type;
 215              // Free up memory
 216              unset($xml);
 217              return $type;
 218          }
 219  
 220          JError::raiseWarning(1, JText::_('JLIB_INSTALLER_ERROR_NOTFINDJOOMLAXMLSETUPFILE'));
 221          // Free up memory.
 222          unset($xml);
 223          return false;
 224      }
 225  
 226      /**
 227       * Gets a file name out of a url
 228       *
 229       * @param   string  $url  URL to get name from
 230       *
 231       * @return  mixed   String filename or boolean false if failed
 232       *
 233       * @since   11.1
 234       */
 235  	public static function getFilenameFromURL($url)
 236      {
 237          if (is_string($url))
 238          {
 239              $parts = explode('/', $url);
 240              return $parts[count($parts) - 1];
 241          }
 242          return false;
 243      }
 244  
 245      /**
 246       * Clean up temporary uploaded package and unpacked extension
 247       *
 248       * @param   string  $package    Path to the uploaded package file
 249       * @param   string  $resultdir  Path to the unpacked extension
 250       *
 251       * @return  boolean  True on success
 252       *
 253       * @since   11.1
 254       */
 255  	public static function cleanupInstall($package, $resultdir)
 256      {
 257          $config = JFactory::getConfig();
 258  
 259          // Does the unpacked extension directory exist?
 260          if (is_dir($resultdir))
 261          {
 262              JFolder::delete($resultdir);
 263          }
 264  
 265          // Is the package file a valid file?
 266          if (is_file($package))
 267          {
 268              JFile::delete($package);
 269          }
 270          elseif (is_file(JPath::clean($config->get('tmp_path') . '/' . $package)))
 271          {
 272              // It might also be just a base filename
 273              JFile::delete(JPath::clean($config->get('tmp_path') . '/' . $package));
 274          }
 275      }
 276  
 277      /**
 278       * Splits contents of a sql file into array of discreet queries.
 279       * Queries need to be delimited with end of statement marker ';'
 280       *
 281       * @param   string  $sql  The SQL statement.
 282       *
 283       * @return  array  Array of queries
 284       *
 285       * @since   11.1
 286       */
 287  	public static function splitSql($sql)
 288      {
 289          $db = JFactory::getDbo();
 290          return $db->splitSql($sql);
 291      }
 292  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1