Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
helper.php
Aller à la documentation de ce fichier.
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  */
20 {
21  /**
22  * Method to return the array of client layer configuration options
23  *
24  * @param string $client Client name, currently only 'ftp' is supported
25  * @param boolean $force Forces re-creation of the login credentials. Set this to
26  * true if login credentials in the session storage have changed
27  *
28  * @return array Client layer configuration options, consisting of at least
29  * these fields: enabled, host, port, user, pass, root
30  *
31  * @since 11.1
32  */
33  public static function getCredentials($client, $force = false)
34  {
35  static $credentials = array();
36 
37  $client = strtolower($client);
38 
39  if (!isset($credentials[$client]) || $force)
40  {
41  $config = JFactory::getConfig();
42 
43  // Fetch the client layer configuration options for the specific client
44  switch ($client)
45  {
46  case 'ftp':
47  $options = array(
48  'enabled' => $config->get('ftp_enable'),
49  'host' => $config->get('ftp_host'),
50  'port' => $config->get('ftp_port'),
51  'user' => $config->get('ftp_user'),
52  'pass' => $config->get('ftp_pass'),
53  'root' => $config->get('ftp_root'));
54  break;
55 
56  default:
57  $options = array('enabled' => false, 'host' => '', 'port' => '', 'user' => '', 'pass' => '', 'root' => '');
58  break;
59  }
60 
61  // If user and pass are not set in global config lets see if they are in the session
62  if ($options['enabled'] == true && ($options['user'] == '' || $options['pass'] == ''))
63  {
64  $session = JFactory::getSession();
65  $options['user'] = $session->get($client . '.user', null, 'JClientHelper');
66  $options['pass'] = $session->get($client . '.pass', null, 'JClientHelper');
67  }
68 
69  // If user or pass are missing, disable this client
70  if ($options['user'] == '' || $options['pass'] == '')
71  {
72  $options['enabled'] = false;
73  }
74 
75  // Save the credentials for later use
76  $credentials[$client] = $options;
77  }
78 
79  return $credentials[$client];
80  }
81 
82  /**
83  * Method to set client login credentials
84  *
85  * @param string $client Client name, currently only 'ftp' is supported
86  * @param string $user Username
87  * @param string $pass Password
88  *
89  * @return boolean True if the given login credentials have been set and are valid
90  *
91  * @since 11.1
92  */
93  public static function setCredentials($client, $user, $pass)
94  {
95  $return = false;
96  $client = strtolower($client);
97 
98  // Test if the given credentials are valid
99  switch ($client)
100  {
101  case 'ftp':
102  $config = JFactory::getConfig();
103  $options = array('enabled' => $config->get('ftp_enable'), 'host' => $config->get('ftp_host'), 'port' => $config->get('ftp_port'));
104 
105  if ($options['enabled'])
106  {
107  $ftp = JClientFtp::getInstance($options['host'], $options['port']);
108 
109  // Test the connection and try to log in
110  if ($ftp->isConnected())
111  {
112  if ($ftp->login($user, $pass))
113  {
114  $return = true;
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 }