Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Joomla! External authentication script
  5. * @author usantisteban <usantisteban@othercode.es>
  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.txt
  8. */
  9.  
  10. if (version_compare(PHP_VERSION, '5.3.1', '<')) {
  11. die('Your host needs to use PHP 5.3.1 or higher to run this version of Joomla!');
  12. }
  13.  
  14. define('_JEXEC', 1);
  15. define('JPATH_BASE', __DIR__);
  16.  
  17. require_once JPATH_BASE . '/includes/defines.php';
  18. require_once JPATH_BASE . '/includes/framework.php';
  19. require_once JPATH_BASE . '/libraries/joomla/factory.php';
  20.  
  21. try {
  22.  
  23.  
  24. $input = JFactory::getApplication('site')->input;
  25. $configuration = JFactory::getConfig();
  26.  
  27.  
  28. $secret = $input->post->get('secret', null, 'string');
  29.  
  30. if ($secret !== $configuration->get('secret')) {
  31. throw new \Exception('Forbidden', 403);
  32. }
  33.  
  34. $username = $input->post->get('username', null, 'string');
  35. $password = $input->post->get('password', null, 'string');
  36.  
  37. if (!isset($username) || !isset($password)) {
  38. throw new \Exception('Bad request', 400);
  39. }
  40.  
  41. $db = JFactory::getDbo();
  42. $query = $db->getQuery(true);
  43. $query->select('id, password')
  44. ->from('#__users')
  45. ->where('username=' . $db->quote($username));
  46. $db->setQuery($query);
  47. $result = $db->loadObject();
  48.  
  49. header('Content-type: application/json');
  50.  
  51. if (JUserHelper::verifyPassword($password, $result->password, $result->id) === true) {
  52.  
  53. print json_encode($response = array(
  54. 'code' => 200,
  55. 'message' => 'Authenticate OK!',
  56. 'data' => JUser::getInstance($result->id)
  57. ), JSON_PRETTY_PRINT);
  58.  
  59. } else {
  60. throw new \Exception('Authenticate fail!', 401);
  61. }
  62.  
  63.  
  64. } catch (\Exception $e) {
  65.  
  66. print json_encode($response = array(
  67. 'code' => $e->getCode(),
  68. 'message' => $e->getMessage(),
  69. 'data' => false
  70. ), JSON_PRETTY_PRINT);
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement