Source for file ftp.php

Documentation is available at ftp.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Installation
  4.  * @subpackage  Model
  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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * FTP configuration model for the Joomla Core Installer.
  14.  *
  15.  * @package     Joomla.Installation
  16.  * @subpackage  Model
  17.  * @since       3.1
  18.  */
  19. {
  20.     /**
  21.      * Find the ftp filesystem root for a given user/pass pair.
  22.      *
  23.      * @param   array  $options  Configuration options.
  24.      *
  25.      * @return  mixed  FTP root for given FTP user, or boolean false if not found.
  26.      *
  27.      * @since   3.1
  28.      */
  29.     public function detectFtpRoot($options)
  30.     {
  31.         // Get the application
  32.         /* @var InstallationApplicationWeb $app */
  33.         $app JFactory::getApplication();
  34.  
  35.         // Get the options as a object for easier handling.
  36.         $options JArrayHelper::toObject($options);
  37.  
  38.         // Connect and login to the FTP server.
  39.         // Use binary transfer mode to be able to compare files.
  40.         @$ftp JClientFtp::getInstance($options->get('ftp_host')$options->get('ftp_port')array('type' => FTP_BINARY));
  41.  
  42.         // Check to make sure FTP is connected and authenticated.
  43.         if (!$ftp->isConnected())
  44.         {
  45.             $app->enqueueMessage($options->get('ftp_host'':' $options->get('ftp_port'' ' JText::_('INSTL_FTP_NOCONNECT')'error');
  46.             return false;
  47.         }
  48.  
  49.         if (!$ftp->login($options->get('ftp_user')$options->get('ftp_pass')))
  50.         {
  51.             $app->enqueueMessage(JText::_('INSTL_FTP_NOLOGIN')'error');
  52.             return false;
  53.         }
  54.  
  55.         // Get the current working directory from the FTP server.
  56.         $cwd $ftp->pwd();
  57.         if ($cwd === false)
  58.         {
  59.             $app->enqueueMessage(JText::_('INSTL_FTP_NOPWD')'error');
  60.             return false;
  61.         }
  62.         $cwd rtrim($cwd'/');
  63.  
  64.         // Get a list of folders in the current working directory.
  65.         $cwdFolders $ftp->listDetails(null'folders');
  66.         if ($cwdFolders === false || count($cwdFolders== 0)
  67.         {
  68.             $app->enqueueMessage(JText::_('INSTL_FTP_NODIRECTORYLISTING')'error');
  69.             return false;
  70.         }
  71.  
  72.         // Get just the folder names from the list of folder data.
  73.         for ($i 0$n count($cwdFolders)$i $n$i++)
  74.         {
  75.             $cwdFolders[$i$cwdFolders[$i]['name'];
  76.         }
  77.  
  78.         // Check to see if Joomla is installed at the FTP current working directory.
  79.         $paths array();
  80.         $known array('administrator''components''installation''language''libraries''plugins');
  81.         if (count(array_diff($known$cwdFolders)) == 0)
  82.         {
  83.             $paths[$cwd '/';
  84.         }
  85.  
  86.         // Search through the segments of JPATH_SITE looking for root possibilities.
  87.         $parts explode(DIRECTORY_SEPARATORJPATH_SITE);
  88.         $tmp '';
  89.         for ($i count($parts1$i >= 0$i--)
  90.         {
  91.             $tmp '/' $parts[$i$tmp;
  92.             if (in_array($parts[$i]$cwdFolders))
  93.             {
  94.                 $paths[$cwd $tmp;
  95.             }
  96.         }
  97.  
  98.         // Check all possible paths for the real Joomla installation by comparing version files.
  99.         $rootPath false;
  100.         $checkValue file_get_contents(JPATH_LIBRARIES '/cms/version/version.php');
  101.         foreach ($paths as $tmp)
  102.         {
  103.             $filePath rtrim($tmp'/''/libraries/cms/version/version.php';
  104.             $buffer null;
  105.             $ftp->read($filePath$buffer);
  106.             if ($buffer == $checkValue)
  107.             {
  108.                 $rootPath $tmp;
  109.                 break;
  110.             }
  111.         }
  112.  
  113.         // Close the FTP connection.
  114.         $ftp->quit();
  115.  
  116.         // Return an error if no root path was found.
  117.         if ($rootPath === false)
  118.         {
  119.             $app->enqueueMessage(JText::_('INSTL_FTP_UNABLE_DETECT_ROOT_FOLDER')'error');
  120.             return false;
  121.         }
  122.  
  123.         return $rootPath;
  124.     }
  125.  
  126.     /**
  127.      * Verify the FTP settings as being functional and correct.
  128.      *
  129.      * @param   array  $options  Configuration options.
  130.      *
  131.      * @return  mixed  FTP root for given FTP user, or boolean false if not found.
  132.      *
  133.      * @since   3.1
  134.      */
  135.     public function verifyFtpSettings($options)
  136.     {
  137.         // Get the application
  138.         /* @var InstallationApplicationWeb $app */
  139.         $app JFactory::getApplication();
  140.  
  141.         // Get the options as a object for easier handling.
  142.         $options JArrayHelper::toObject($options);
  143.  
  144.         // Connect and login to the FTP server.
  145.         @$ftp JClientFtp::getInstance($options->get('ftp_host')$options->get('ftp_port'));
  146.  
  147.         // Check to make sure FTP is connected and authenticated.
  148.         if (!$ftp->isConnected())
  149.         {
  150.             $app->enqueueMessage(JText::_('INSTL_FTP_NOCONNECT')'error');
  151.             return false;
  152.         }
  153.         if (!$ftp->login($options->get('ftp_user')$options->get('ftp_pass')))
  154.         {
  155.             $ftp->quit();
  156.             $app->enqueueMessage(JText::_('INSTL_FTP_NOLOGIN')'error');
  157.             return false;
  158.         }
  159.  
  160.         // Since the root path will be trimmed when it gets saved to configuration.php,
  161.         // we want to test with the same value as well.
  162.         $root rtrim($options->get('ftp_root')'/');
  163.  
  164.         // Verify PWD function
  165.         if ($ftp->pwd(=== false)
  166.         {
  167.             $ftp->quit();
  168.             $app->enqueueMessage(JText::_('INSTL_FTP_NOPWD')'error');
  169.             return false;
  170.         }
  171.  
  172.         // Verify root path exists
  173.         if (!$ftp->chdir($root))
  174.         {
  175.             $ftp->quit();
  176.             $app->enqueueMessage(JText::_('INSTL_FTP_NOROOT')'error');
  177.             return false;
  178.         }
  179.  
  180.         // Verify NLST function
  181.         if (($rootList $ftp->listNames()) === false)
  182.         {
  183.             $ftp->quit();
  184.             $app->enqueueMessage(JText::_('INSTL_FTP_NONLST')'error');
  185.             return false;
  186.         }
  187.  
  188.         // Verify LIST function
  189.         if ($ftp->listDetails(=== false)
  190.         {
  191.             $ftp->quit();
  192.             $app->enqueueMessage(JText::_('INSTL_FTP_NOLIST')'error');
  193.             return false;
  194.         }
  195.  
  196.         // Verify SYST function
  197.         if ($ftp->syst(=== false)
  198.         {
  199.             $ftp->quit();
  200.             $app->enqueueMessage(JText::_('INSTL_FTP_NOSYST')'error');
  201.             return false;
  202.         }
  203.  
  204.         // Verify valid root path, part one
  205.         $checkList array('robots.txt''index.php');
  206.         if (count(array_diff($checkList$rootList)))
  207.         {
  208.             $ftp->quit();
  209.             $app->enqueueMessage(JText::_('INSTL_FTP_INVALIDROOT')'error');
  210.             return false;
  211.         }
  212.  
  213.         // Verify RETR function
  214.         $buffer null;
  215.         if ($ftp->read($root '/libraries/cms/version/version.php'$buffer=== false)
  216.         {
  217.             $ftp->quit();
  218.             $app->enqueueMessage(JText::_('INSTL_FTP_NORETR')'error');
  219.             return false;
  220.         }
  221.  
  222.         // Verify valid root path, part two
  223.         $checkValue file_get_contents(JPATH_ROOT '/libraries/cms/version/version.php');
  224.         if ($buffer !== $checkValue)
  225.         {
  226.             $ftp->quit();
  227.             $app->enqueueMessage(JText::_('INSTL_FTP_INVALIDROOT')'error');
  228.             return false;
  229.         }
  230.  
  231.         // Verify STOR function
  232.         if ($ftp->create($root '/ftp_testfile'=== false)
  233.         {
  234.             $ftp->quit();
  235.             $app->enqueueMessage(JText::_('INSTL_FTP_NOSTOR')'error');
  236.             return false;
  237.         }
  238.  
  239.         // Verify DELE function
  240.         if ($ftp->delete($root '/ftp_testfile'=== false)
  241.         {
  242.             $ftp->quit();
  243.             $app->enqueueMessage(JText::_('INSTL_FTP_NODELE')'error');
  244.             return false;
  245.         }
  246.  
  247.         // Verify MKD function
  248.         if ($ftp->mkdir($root '/ftp_testdir'=== false)
  249.         {
  250.             $ftp->quit();
  251.             $app->enqueueMessage(JText::_('INSTL_FTP_NOMKD')'error');
  252.             return false;
  253.         }
  254.  
  255.         // Verify RMD function
  256.         if ($ftp->delete($root '/ftp_testdir'=== false)
  257.         {
  258.             $ftp->quit();
  259.             $app->enqueueMessage(JText::_('INSTL_FTP_NORMD')'error');
  260.             return false;
  261.         }
  262.  
  263.         $ftp->quit();
  264.         return true;
  265.     }
  266. }

Documentation generated on Tue, 19 Nov 2013 15:03:58 +0100 by phpDocumentor 1.4.3