[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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

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


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1