Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/client/helper.php

Documentation is available at helper.php

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

Documentation generated on Sat, 14 Nov 2009 11:14:22 +0000 by phpDocumentor 1.3.1