Source for file helper.php

Documentation is available at helper.php

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

Documentation generated on Tue, 19 Nov 2013 15:04:24 +0100 by phpDocumentor 1.4.3