Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/client/helper.php

Documentation is available at helper.php

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

Documentation generated on Mon, 05 Mar 2007 21:06:02 +0000 by phpDocumentor 1.3.1