Source for file helper.php

Documentation is available at helper.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Client
  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. /**
  13.  * Client helper class
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Client
  17.  * @since       11.1
  18.  */
  19. {
  20.     /**
  21.      * Method to return the array of client layer configuration options
  22.      *
  23.      * @param   string   $client  Client name, currently only 'ftp' is supported
  24.      * @param   boolean  $force   Forces re-creation of the login credentials. Set this to
  25.      *                             true if login credentials in the session storage have changed
  26.      *
  27.      * @return  array    Client layer configuration options, consisting of at least
  28.      *                    these fields: enabled, host, port, user, pass, root
  29.      *
  30.      * @since   11.1
  31.      */
  32.     public static function getCredentials($client$force false)
  33.     {
  34.         static $credentials array();
  35.  
  36.         $client strtolower($client);
  37.  
  38.         if (!isset($credentials[$client]|| $force)
  39.         {
  40.             $config JFactory::getConfig();
  41.  
  42.             // Fetch the client layer configuration options for the specific client
  43.             switch ($client)
  44.             {
  45.                 case 'ftp':
  46.                     $options array(
  47.                         'enabled' => $config->get('ftp_enable'),
  48.                         'host' => $config->get('ftp_host'),
  49.                         'port' => $config->get('ftp_port'),
  50.                         'user' => $config->get('ftp_user'),
  51.                         'pass' => $config->get('ftp_pass'),
  52.                         'root' => $config->get('ftp_root'));
  53.                     break;
  54.  
  55.                 default:
  56.                     $options array('enabled' => false'host' => '''port' => '''user' => '''pass' => '''root' => '');
  57.                     break;
  58.             }
  59.  
  60.             // If user and pass are not set in global config lets see if they are in the session
  61.             if ($options['enabled'== true && ($options['user'== '' || $options['pass'== ''))
  62.             {
  63.                 $session JFactory::getSession();
  64.                 $options['user'$session->get($client '.user'null'JClientHelper');
  65.                 $options['pass'$session->get($client '.pass'null'JClientHelper');
  66.             }
  67.  
  68.             // If user or pass are missing, disable this client
  69.             if ($options['user'== '' || $options['pass'== '')
  70.             {
  71.                 $options['enabled'false;
  72.             }
  73.  
  74.             // Save the credentials for later use
  75.             $credentials[$client$options;
  76.         }
  77.  
  78.         return $credentials[$client];
  79.     }
  80.  
  81.     /**
  82.      * Method to set client login credentials
  83.      *
  84.      * @param   string  $client  Client name, currently only 'ftp' is supported
  85.      * @param   string  $user    Username
  86.      * @param   string  $pass    Password
  87.      *
  88.      * @return  boolean  True if the given login credentials have been set and are valid
  89.      *
  90.      * @since   11.1
  91.      */
  92.     public static function setCredentials($client$user$pass)
  93.     {
  94.         $return false;
  95.         $client strtolower($client);
  96.  
  97.         // Test if the given credentials are valid
  98.         switch ($client)
  99.         {
  100.             case 'ftp':
  101.                 $config JFactory::getConfig();
  102.                 $options array('enabled' => $config->get('ftp_enable')'host' => $config->get('ftp_host')'port' => $config->get('ftp_port'));
  103.  
  104.                 if ($options['enabled'])
  105.                 {
  106.                     $ftp JClientFtp::getInstance($options['host']$options['port']);
  107.  
  108.                     // Test the connection and try to log in
  109.                     if ($ftp->isConnected())
  110.                     {
  111.                         if ($ftp->login($user$pass))
  112.                         {
  113.                             $return true;
  114.                         }
  115.                         $ftp->quit();
  116.                     }
  117.                 }
  118.                 break;
  119.  
  120.             default:
  121.                 break;
  122.         }
  123.  
  124.         if ($return)
  125.         {
  126.             // Save valid credentials to the session
  127.             $session JFactory::getSession();
  128.             $session->set($client '.user'$user'JClientHelper');
  129.             $session->set($client '.pass'$pass'JClientHelper');
  130.  
  131.             // Force re-creation of the data saved within JClientHelper::getCredentials()
  132.             self::getCredentials($clienttrue);
  133.         }
  134.  
  135.         return $return;
  136.     }
  137.  
  138.     /**
  139.      * Method to determine if client login credentials are present
  140.      *
  141.      * @param   string  $client  Client name, currently only 'ftp' is supported
  142.      *
  143.      * @return  boolean  True if login credentials are available
  144.      *
  145.      * @since   11.1
  146.      */
  147.     public static function hasCredentials($client)
  148.     {
  149.         $return false;
  150.         $client strtolower($client);
  151.  
  152.         // Get (unmodified) credentials for this client
  153.         switch ($client)
  154.         {
  155.             case 'ftp':
  156.                 $config JFactory::getConfig();
  157.                 $options array('enabled' => $config->get('ftp_enable')'user' => $config->get('ftp_user')'pass' => $config->get('ftp_pass'));
  158.                 break;
  159.  
  160.             default:
  161.                 $options array('enabled' => false'user' => '''pass' => '');
  162.                 break;
  163.         }
  164.  
  165.         if ($options['enabled'== false)
  166.         {
  167.             // The client is disabled in global config, so let's pretend we are OK
  168.             $return true;
  169.         }
  170.         elseif ($options['user'!= '' && $options['pass'!= '')
  171.         {
  172.             // Login credentials are available in global config
  173.             $return true;
  174.         }
  175.         else
  176.         {
  177.             // Check if login credentials are available in the session
  178.             $session JFactory::getSession();
  179.             $user $session->get($client '.user'null'JClientHelper');
  180.             $pass $session->get($client '.pass'null'JClientHelper');
  181.  
  182.             if ($user != '' && $pass != '')
  183.             {
  184.                 $return true;
  185.             }
  186.         }
  187.  
  188.         return $return;
  189.     }
  190.  
  191.     /**
  192.      * Determine whether input fields for client settings need to be shown
  193.      *
  194.      * If valid credentials were passed along with the request, they are saved to the session.
  195.      * This functions returns an exception if invalid credentials have been given or if the
  196.      * connection to the server failed for some other reason.
  197.      *
  198.      * @param   string  $client  The name of the client.
  199.      *
  200.      * @return  mixed  True, if FTP settings; JError if using legacy tree.
  201.      *
  202.      * @since   11.1
  203.      * @throws  InvalidArgumentException if credentials invalid
  204.      */
  205.     public static function setCredentialsFromRequest($client)
  206.     {
  207.         // Determine whether FTP credentials have been passed along with the current request
  208.         $input JFactory::getApplication()->input;
  209.         $user $input->post->getString('username'null);
  210.         $pass $input->post->getString('password'null);
  211.  
  212.         if ($user != '' && $pass != '')
  213.         {
  214.             // Add credentials to the session
  215.             if (self::setCredentials($client$user$pass))
  216.             {
  217.                 $return false;
  218.             }
  219.             else
  220.             {
  221.                 if (class_exists('JError'))
  222.                 {
  223.                     $return JError::raiseWarning('SOME_ERROR_CODE'JText::_('JLIB_CLIENT_ERROR_HELPER_SETCREDENTIALSFROMREQUEST_FAILED'));
  224.                 }
  225.                 else
  226.                 {
  227.                     throw new InvalidArgumentException('Invalid user credentials');
  228.                 }
  229.             }
  230.         }
  231.         else
  232.         {
  233.             // Just determine if the FTP input fields need to be shown
  234.             $return !self::hasCredentials('ftp');
  235.         }
  236.  
  237.         return $return;
  238.     }
  239. }

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