PageRenderTime 25ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/client/helper.php

https://github.com/pollen8/joomla-platform
PHP | 370 lines | 240 code | 30 blank | 100 comment | 69 complexity | f0b7078bbd67ab1df264fc94ce33d774 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Client
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. <<<<<<< HEAD
  10. defined('JPATH_PLATFORM') or die;
  11. =======
  12. defined('JPATH_PLATFORM') or die();
  13. >>>>>>> upstream/master
  14. /**
  15. * Client helper class
  16. *
  17. * @package Joomla.Platform
  18. * @subpackage Client
  19. * @since 11.1
  20. */
  21. class JClientHelper
  22. {
  23. /**
  24. * Method to return the array of client layer configuration options
  25. *
  26. * @param string $client Client name, currently only 'ftp' is supported
  27. * @param boolean $force Forces re-creation of the login credentials. Set this to
  28. <<<<<<< HEAD
  29. * true if login credentials in the session storage have changed
  30. *
  31. * @return array Client layer configuration options, consisting of at least
  32. * these fields: enabled, host, port, user, pass, root
  33. * @since 11.1
  34. */
  35. public static function getCredentials($client, $force=false)
  36. =======
  37. * true if login credentials in the session storage have changed
  38. *
  39. * @return array Client layer configuration options, consisting of at least
  40. * these fields: enabled, host, port, user, pass, root
  41. *
  42. * @since 11.1
  43. */
  44. public static function getCredentials($client, $force = false)
  45. >>>>>>> upstream/master
  46. {
  47. static $credentials = array();
  48. $client = strtolower($client);
  49. <<<<<<< HEAD
  50. if (!isset($credentials[$client]) || $force) {
  51. =======
  52. if (!isset($credentials[$client]) || $force)
  53. {
  54. >>>>>>> upstream/master
  55. // Initialise variables.
  56. $config = JFactory::getConfig();
  57. // Fetch the client layer configuration options for the specific client
  58. <<<<<<< HEAD
  59. switch ($client) {
  60. case 'ftp':
  61. $options = array(
  62. 'enabled' => $config->get('ftp_enable'),
  63. 'host' => $config->get('ftp_host'),
  64. 'port' => $config->get('ftp_port'),
  65. 'user' => $config->get('ftp_user'),
  66. 'pass' => $config->get('ftp_pass'),
  67. 'root' => $config->get('ftp_root')
  68. );
  69. break;
  70. default:
  71. $options = array(
  72. 'enabled' => false,
  73. 'host' => '',
  74. 'port' => '',
  75. 'user' => '',
  76. 'pass' => '',
  77. 'root' => ''
  78. );
  79. =======
  80. switch ($client)
  81. {
  82. case 'ftp':
  83. $options = array(
  84. 'enabled' => $config->get('ftp_enable'),
  85. 'host' => $config->get('ftp_host'),
  86. 'port' => $config->get('ftp_port'),
  87. 'user' => $config->get('ftp_user'),
  88. 'pass' => $config->get('ftp_pass'),
  89. 'root' => $config->get('ftp_root'));
  90. break;
  91. default:
  92. $options = array('enabled' => false, 'host' => '', 'port' => '', 'user' => '', 'pass' => '', 'root' => '');
  93. >>>>>>> upstream/master
  94. break;
  95. }
  96. // If user and pass are not set in global config lets see if they are in the session
  97. <<<<<<< HEAD
  98. if ($options['enabled'] == true && ($options['user'] == '' || $options['pass'] == '')) {
  99. $session = JFactory::getSession();
  100. $options['user'] = $session->get($client.'.user', null, 'JClientHelper');
  101. $options['pass'] = $session->get($client.'.pass', null, 'JClientHelper');
  102. }
  103. // If user or pass are missing, disable this client
  104. if ($options['user'] == '' || $options['pass'] == '') {
  105. =======
  106. if ($options['enabled'] == true && ($options['user'] == '' || $options['pass'] == ''))
  107. {
  108. $session = JFactory::getSession();
  109. $options['user'] = $session->get($client . '.user', null, 'JClientHelper');
  110. $options['pass'] = $session->get($client . '.pass', null, 'JClientHelper');
  111. }
  112. // If user or pass are missing, disable this client
  113. if ($options['user'] == '' || $options['pass'] == '')
  114. {
  115. >>>>>>> upstream/master
  116. $options['enabled'] = false;
  117. }
  118. // Save the credentials for later use
  119. $credentials[$client] = $options;
  120. }
  121. return $credentials[$client];
  122. }
  123. /**
  124. * Method to set client login credentials
  125. *
  126. <<<<<<< HEAD
  127. * @param string $client Client name, currently only 'ftp' is supported
  128. * @param string $user Username
  129. * @param string $pass Password
  130. *
  131. * @return boolean True if the given login credentials have been set and are valid
  132. =======
  133. * @param string $client Client name, currently only 'ftp' is supported
  134. * @param string $user Username
  135. * @param string $pass Password
  136. *
  137. * @return boolean True if the given login credentials have been set and are valid
  138. *
  139. >>>>>>> upstream/master
  140. * @since 11.1
  141. */
  142. public static function setCredentials($client, $user, $pass)
  143. {
  144. $return = false;
  145. $client = strtolower($client);
  146. // Test if the given credentials are valid
  147. <<<<<<< HEAD
  148. switch ($client) {
  149. case 'ftp':
  150. $config = JFactory::getConfig();
  151. $options = array(
  152. 'enabled' => $config->get('ftp_enable'),
  153. 'host' => $config->get('ftp_host'),
  154. 'port' => $config->get('ftp_port'),
  155. );
  156. if ($options['enabled']) {
  157. =======
  158. switch ($client)
  159. {
  160. case 'ftp':
  161. $config = JFactory::getConfig();
  162. $options = array('enabled' => $config->get('ftp_enable'), 'host' => $config->get('ftp_host'), 'port' => $config->get('ftp_port'));
  163. if ($options['enabled'])
  164. {
  165. >>>>>>> upstream/master
  166. jimport('joomla.client.ftp');
  167. $ftp = JFTP::getInstance($options['host'], $options['port']);
  168. // Test the conection and try to log in
  169. <<<<<<< HEAD
  170. if ($ftp->isConnected()) {
  171. if ($ftp->login($user, $pass)) {
  172. =======
  173. if ($ftp->isConnected())
  174. {
  175. if ($ftp->login($user, $pass))
  176. {
  177. >>>>>>> upstream/master
  178. $return = true;
  179. }
  180. $ftp->quit();
  181. }
  182. }
  183. break;
  184. default:
  185. break;
  186. }
  187. <<<<<<< HEAD
  188. if ($return) {
  189. // Save valid credentials to the session
  190. $session = JFactory::getSession();
  191. $session->set($client.'.user', $user, 'JClientHelper');
  192. $session->set($client.'.pass', $pass, 'JClientHelper');
  193. =======
  194. if ($return)
  195. {
  196. // Save valid credentials to the session
  197. $session = JFactory::getSession();
  198. $session->set($client . '.user', $user, 'JClientHelper');
  199. $session->set($client . '.pass', $pass, 'JClientHelper');
  200. >>>>>>> upstream/master
  201. // Force re-creation of the data saved within JClientHelper::getCredentials()
  202. JClientHelper::getCredentials($client, true);
  203. }
  204. return $return;
  205. }
  206. /**
  207. * Method to determine if client login credentials are present
  208. *
  209. <<<<<<< HEAD
  210. * @param string Client name, currently only 'ftp' is supported
  211. *
  212. * @return boolean True if login credentials are available
  213. =======
  214. * @param string $client Client name, currently only 'ftp' is supported
  215. *
  216. * @return boolean True if login credentials are available
  217. *
  218. >>>>>>> upstream/master
  219. * @since 11.1
  220. */
  221. public static function hasCredentials($client)
  222. {
  223. $return = false;
  224. $client = strtolower($client);
  225. // Get (unmodified) credentials for this client
  226. <<<<<<< HEAD
  227. switch ($client) {
  228. case 'ftp':
  229. $config = JFactory::getConfig();
  230. $options = array(
  231. 'enabled' => $config->get('ftp_enable'),
  232. 'user' => $config->get('ftp_user'),
  233. 'pass' => $config->get('ftp_pass')
  234. );
  235. break;
  236. default:
  237. $options = array(
  238. 'enabled' => false,
  239. 'user' => '',
  240. 'pass' => ''
  241. );
  242. break;
  243. }
  244. if ($options['enabled'] == false) {
  245. // The client is disabled in global config, so let's pretend we are OK
  246. $return = true;
  247. } else if ($options['user'] != '' && $options['pass'] != '') {
  248. // Login credentials are available in global config
  249. $return = true;
  250. } else {
  251. // Check if login credentials are available in the session
  252. $session = JFactory::getSession();
  253. $user = $session->get($client.'.user', null, 'JClientHelper');
  254. $pass = $session->get($client.'.pass', null, 'JClientHelper');
  255. if ($user != '' && $pass != '') {
  256. =======
  257. switch ($client)
  258. {
  259. case 'ftp':
  260. $config = JFactory::getConfig();
  261. $options = array('enabled' => $config->get('ftp_enable'), 'user' => $config->get('ftp_user'), 'pass' => $config->get('ftp_pass'));
  262. break;
  263. default:
  264. $options = array('enabled' => false, 'user' => '', 'pass' => '');
  265. break;
  266. }
  267. if ($options['enabled'] == false)
  268. {
  269. // The client is disabled in global config, so let's pretend we are OK
  270. $return = true;
  271. }
  272. else if ($options['user'] != '' && $options['pass'] != '')
  273. {
  274. // Login credentials are available in global config
  275. $return = true;
  276. }
  277. else
  278. {
  279. // Check if login credentials are available in the session
  280. $session = JFactory::getSession();
  281. $user = $session->get($client . '.user', null, 'JClientHelper');
  282. $pass = $session->get($client . '.pass', null, 'JClientHelper');
  283. if ($user != '' && $pass != '')
  284. {
  285. >>>>>>> upstream/master
  286. $return = true;
  287. }
  288. }
  289. return $return;
  290. }
  291. /**
  292. * Determine whether input fields for client settings need to be shown
  293. *
  294. * If valid credentials were passed along with the request, they are saved to the session.
  295. * This functions returns an exception if invalid credentials have been given or if the
  296. * connection to the server failed for some other reason.
  297. *
  298. <<<<<<< HEAD
  299. * @param string $client
  300. *
  301. * @return mixed True, if FTP settings should be shown or an exception
  302. =======
  303. * @param string $client The name of the client.
  304. *
  305. * @return mixed True, if FTP settings should be shown or an exception
  306. *
  307. >>>>>>> upstream/master
  308. * @since 11.1
  309. */
  310. public static function setCredentialsFromRequest($client)
  311. {
  312. // Determine wether FTP credentials have been passed along with the current request
  313. $user = JRequest::getString('username', null, 'POST', JREQUEST_ALLOWRAW);
  314. $pass = JRequest::getString('password', null, 'POST', JREQUEST_ALLOWRAW);
  315. if ($user != '' && $pass != '')
  316. {
  317. // Add credentials to the session
  318. <<<<<<< HEAD
  319. if (JClientHelper::setCredentials($client, $user, $pass)) {
  320. $return = false;
  321. } else {
  322. =======
  323. if (JClientHelper::setCredentials($client, $user, $pass))
  324. {
  325. $return = false;
  326. }
  327. else
  328. {
  329. >>>>>>> upstream/master
  330. $return = JError::raiseWarning('SOME_ERROR_CODE', JText::_('JLIB_CLIENT_ERROR_HELPER_SETCREDENTIALSFROMREQUEST_FAILED'));
  331. }
  332. }
  333. else
  334. {
  335. // Just determine if the FTP input fields need to be shown
  336. $return = !JClientHelper::hasCredentials('ftp');
  337. }
  338. return $return;
  339. }
  340. }