1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  Client
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 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  * @since  11.1
 16  */
 17 class JClientHelper
 18 {
 19     /**
 20      * Method to return the array of client layer configuration options
 21      *
 22      * @param   string   $client  Client name, currently only 'ftp' is supported
 23      * @param   boolean  $force   Forces re-creation of the login credentials. Set this to
 24      *                            true if login credentials in the session storage have changed
 25      *
 26      * @return  array    Client layer configuration options, consisting of at least
 27      *                   these fields: enabled, host, port, user, pass, root
 28      *
 29      * @since   11.1
 30      */
 31     public static function getCredentials($client, $force = false)
 32     {
 33         static $credentials = array();
 34 
 35         $client = strtolower($client);
 36 
 37         if (!isset($credentials[$client]) || $force)
 38         {
 39             $config = JFactory::getConfig();
 40 
 41             // Fetch the client layer configuration options for the specific client
 42             switch ($client)
 43             {
 44                 case 'ftp':
 45                     $options = array(
 46                         'enabled' => $config->get('ftp_enable'),
 47                         'host' => $config->get('ftp_host'),
 48                         'port' => $config->get('ftp_port'),
 49                         'user' => $config->get('ftp_user'),
 50                         'pass' => $config->get('ftp_pass'),
 51                         'root' => $config->get('ftp_root'),
 52                     );
 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 
116                         $ftp->quit();
117                     }
118                 }
119                 break;
120 
121             default:
122                 break;
123         }
124 
125         if ($return)
126         {
127             // Save valid credentials to the session
128             $session = JFactory::getSession();
129             $session->set($client . '.user', $user, 'JClientHelper');
130             $session->set($client . '.pass', $pass, 'JClientHelper');
131 
132             // Force re-creation of the data saved within JClientHelper::getCredentials()
133             self::getCredentials($client, true);
134         }
135 
136         return $return;
137     }
138 
139     /**
140      * Method to determine if client login credentials are present
141      *
142      * @param   string  $client  Client name, currently only 'ftp' is supported
143      *
144      * @return  boolean  True if login credentials are available
145      *
146      * @since   11.1
147      */
148     public static function hasCredentials($client)
149     {
150         $return = false;
151         $client = strtolower($client);
152 
153         // Get (unmodified) credentials for this client
154         switch ($client)
155         {
156             case 'ftp':
157                 $config = JFactory::getConfig();
158                 $options = array('enabled' => $config->get('ftp_enable'), 'user' => $config->get('ftp_user'), 'pass' => $config->get('ftp_pass'));
159                 break;
160 
161             default:
162                 $options = array('enabled' => false, 'user' => '', 'pass' => '');
163                 break;
164         }
165 
166         if ($options['enabled'] == false)
167         {
168             // The client is disabled in global config, so let's pretend we are OK
169             $return = true;
170         }
171         elseif ($options['user'] != '' && $options['pass'] != '')
172         {
173             // Login credentials are available in global config
174             $return = true;
175         }
176         else
177         {
178             // Check if login credentials are available in the session
179             $session = JFactory::getSession();
180             $user = $session->get($client . '.user', null, 'JClientHelper');
181             $pass = $session->get($client . '.pass', null, 'JClientHelper');
182 
183             if ($user != '' && $pass != '')
184             {
185                 $return = true;
186             }
187         }
188 
189         return $return;
190     }
191 
192     /**
193      * Determine whether input fields for client settings need to be shown
194      *
195      * If valid credentials were passed along with the request, they are saved to the session.
196      * This functions returns an exception if invalid credentials have been given or if the
197      * connection to the server failed for some other reason.
198      *
199      * @param   string  $client  The name of the client.
200      *
201      * @return  mixed  True, if FTP settings; JError if using legacy tree.
202      *
203      * @since   11.1
204      * @throws  InvalidArgumentException if credentials invalid
205      */
206     public static function setCredentialsFromRequest($client)
207     {
208         // Determine whether FTP credentials have been passed along with the current request
209         $input = JFactory::getApplication()->input;
210         $user = $input->post->getString('username', null);
211         $pass = $input->post->getString('password', null);
212 
213         if ($user != '' && $pass != '')
214         {
215             // Add credentials to the session
216             if (self::setCredentials($client, $user, $pass))
217             {
218                 $return = false;
219             }
220             else
221             {
222                 if (class_exists('JError'))
223                 {
224                     $return = JError::raiseWarning('SOME_ERROR_CODE', JText::_('JLIB_CLIENT_ERROR_HELPER_SETCREDENTIALSFROMREQUEST_FAILED'));
225                 }
226                 else
227                 {
228                     throw new InvalidArgumentException('Invalid user credentials');
229                 }
230             }
231         }
232         else
233         {
234             // Just determine if the FTP input fields need to be shown
235             $return = !self::hasCredentials('ftp');
236         }
237 
238         return $return;
239     }
240 }
241