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