[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2007 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Client; 11 12 use Joomla\CMS\Factory; 13 14 // phpcs:disable PSR1.Files.SideEffects 15 \defined('JPATH_PLATFORM') or die; 16 // phpcs:enable PSR1.Files.SideEffects 17 18 /** 19 * Client helper class 20 * 21 * @since 1.7.0 22 */ 23 class ClientHelper 24 { 25 /** 26 * Method to return the array of client layer configuration options 27 * 28 * @param string $client Client name, currently only 'ftp' is supported 29 * @param boolean $force Forces re-creation of the login credentials. Set this to 30 * true if login credentials in the session storage have changed 31 * 32 * @return array Client layer configuration options, consisting of at least 33 * these fields: enabled, host, port, user, pass, root 34 * 35 * @since 1.7.0 36 */ 37 public static function getCredentials($client, $force = false) 38 { 39 static $credentials = array(); 40 41 $client = strtolower($client); 42 43 if (!isset($credentials[$client]) || $force) { 44 $app = Factory::getApplication(); 45 46 // Fetch the client layer configuration options for the specific client 47 switch ($client) { 48 case 'ftp': 49 $options = array( 50 'enabled' => $app->get('ftp_enable'), 51 'host' => $app->get('ftp_host'), 52 'port' => $app->get('ftp_port'), 53 'user' => $app->get('ftp_user'), 54 'pass' => $app->get('ftp_pass'), 55 'root' => $app->get('ftp_root'), 56 ); 57 break; 58 59 default: 60 $options = array('enabled' => false, 'host' => '', 'port' => '', 'user' => '', 'pass' => '', 'root' => ''); 61 break; 62 } 63 64 // If user and pass are not set in global config lets see if they are in the session 65 if ($options['enabled'] == true && ($options['user'] == '' || $options['pass'] == '')) { 66 $session = Factory::getSession(); 67 $options['user'] = $session->get($client . '.user', null, 'JClientHelper'); 68 $options['pass'] = $session->get($client . '.pass', null, 'JClientHelper'); 69 } 70 71 // If user or pass are missing, disable this client 72 if ($options['user'] == '' || $options['pass'] == '') { 73 $options['enabled'] = false; 74 } 75 76 // Save the credentials for later use 77 $credentials[$client] = $options; 78 } 79 80 return $credentials[$client]; 81 } 82 83 /** 84 * Method to set client login credentials 85 * 86 * @param string $client Client name, currently only 'ftp' is supported 87 * @param string $user Username 88 * @param string $pass Password 89 * 90 * @return boolean True if the given login credentials have been set and are valid 91 * 92 * @since 1.7.0 93 */ 94 public static function setCredentials($client, $user, $pass) 95 { 96 $return = false; 97 $client = strtolower($client); 98 99 // Test if the given credentials are valid 100 switch ($client) { 101 case 'ftp': 102 $app = Factory::getApplication(); 103 $options = array('enabled' => $app->get('ftp_enable'), 'host' => $app->get('ftp_host'), 'port' => $app->get('ftp_port')); 104 105 if ($options['enabled']) { 106 $ftp = FtpClient::getInstance($options['host'], $options['port']); 107 108 // Test the connection and try to log in 109 if ($ftp->isConnected()) { 110 if ($ftp->login($user, $pass)) { 111 $return = true; 112 } 113 114 $ftp->quit(); 115 } 116 } 117 break; 118 119 default: 120 break; 121 } 122 123 if ($return) { 124 // Save valid credentials to the session 125 $session = Factory::getSession(); 126 $session->set($client . '.user', $user, 'JClientHelper'); 127 $session->set($client . '.pass', $pass, 'JClientHelper'); 128 129 // Force re-creation of the data saved within JClientHelper::getCredentials() 130 self::getCredentials($client, true); 131 } 132 133 return $return; 134 } 135 136 /** 137 * Method to determine if client login credentials are present 138 * 139 * @param string $client Client name, currently only 'ftp' is supported 140 * 141 * @return boolean True if login credentials are available 142 * 143 * @since 1.7.0 144 */ 145 public static function hasCredentials($client) 146 { 147 $return = false; 148 $client = strtolower($client); 149 150 // Get (unmodified) credentials for this client 151 switch ($client) { 152 case 'ftp': 153 $app = Factory::getApplication(); 154 $options = array('enabled' => $app->get('ftp_enable'), 'user' => $app->get('ftp_user'), 'pass' => $app->get('ftp_pass')); 155 break; 156 157 default: 158 $options = array('enabled' => false, 'user' => '', 'pass' => ''); 159 break; 160 } 161 162 if ($options['enabled'] == false) { 163 // The client is disabled in global config, so let's pretend we are OK 164 $return = true; 165 } elseif ($options['user'] != '' && $options['pass'] != '') { 166 // Login credentials are available in global config 167 $return = true; 168 } else { 169 // Check if login credentials are available in the session 170 $session = Factory::getSession(); 171 $user = $session->get($client . '.user', null, 'JClientHelper'); 172 $pass = $session->get($client . '.pass', null, 'JClientHelper'); 173 174 if ($user != '' && $pass != '') { 175 $return = true; 176 } 177 } 178 179 return $return; 180 } 181 182 /** 183 * Determine whether input fields for client settings need to be shown 184 * 185 * If valid credentials were passed along with the request, they are saved to the session. 186 * This functions returns an exception if invalid credentials have been given or if the 187 * connection to the server failed for some other reason. 188 * 189 * @param string $client The name of the client. 190 * 191 * @return boolean True if credentials are present 192 * 193 * @since 1.7.0 194 * @throws \InvalidArgumentException if credentials invalid 195 */ 196 public static function setCredentialsFromRequest($client) 197 { 198 // Determine whether FTP credentials have been passed along with the current request 199 $input = Factory::getApplication()->input; 200 $user = $input->post->getString('username', null); 201 $pass = $input->post->getString('password', null); 202 203 if ($user != '' && $pass != '') { 204 // Add credentials to the session 205 if (!self::setCredentials($client, $user, $pass)) { 206 throw new \InvalidArgumentException('Invalid user credentials'); 207 } 208 209 $return = false; 210 } else { 211 // Just determine if the FTP input fields need to be shown 212 $return = !self::hasCredentials('ftp'); 213 } 214 215 return $return; 216 } 217 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |