BxDrupalFormLogin.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. class BxDrupalFormLogin extends BxTemplFormLogin
  10. {
  11. public function __construct($aInfo, $oTemplate)
  12. {
  13. parent::__construct($aInfo, $oTemplate);
  14. }
  15. function isValid ()
  16. {
  17. if (!BxTemplFormView::isValid ())
  18. return false;
  19. $sId = trim($this->getCleanValue('ID'));
  20. $sPassword = $this->getCleanValue('Password');
  21. if ($sId != '') {
  22. $sErrorString = $this->checkPassword($sId, $sPassword, $this->getRole());
  23. $this->_setCustomError ($sErrorString);
  24. return $sErrorString ? false : true;
  25. }
  26. else {
  27. $this->_setCustomError (_t('_sys_txt_error_occured'));
  28. return false;
  29. }
  30. }
  31. function checkPassword ($sEmail, $sPassword, $iRole = 1)
  32. {
  33. $mixedData = $this->call(array(
  34. 'username' => $sEmail,
  35. 'password' => $sPassword,
  36. ));
  37. if (empty($mixedData) || is_string($mixedData) || !is_array($mixedData))
  38. return empty($mixedData) ? _t('_sys_txt_error_occured') : $mixedData;
  39. // create user if their doesn't exists
  40. bx_srv('bx_drupal', 'handle_user', array($mixedData));
  41. return '';
  42. }
  43. function call ($aData)
  44. {
  45. $rCurl = curl_init();
  46. curl_setopt($rCurl, CURLOPT_URL, getParam('bx_drupal_login_url'));
  47. curl_setopt($rCurl, CURLOPT_POST, 1);
  48. curl_setopt($rCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  49. curl_setopt($rCurl, CURLOPT_POSTFIELDS, json_encode($aData));
  50. curl_setopt($rCurl, CURLOPT_HEADER, FALSE);
  51. curl_setopt($rCurl, CURLOPT_RETURNTRANSFER, TRUE);
  52. curl_setopt($rCurl, CURLOPT_FAILONERROR, TRUE);
  53. $s = curl_exec($rCurl);
  54. $iHttpCode = curl_getinfo($rCurl, CURLINFO_HTTP_CODE);
  55. $mixedResult = '';
  56. // Check if login was successful
  57. if ($iHttpCode == 200) {
  58. // Convert json response as array
  59. $aData = @json_decode($s, TRUE);
  60. if (null === $aData)
  61. $mixedResult = _t('_bx_drupal_json_decode_failed', $mixedResult);
  62. else
  63. $mixedResult = $aData;
  64. }
  65. else {
  66. // Get error msg
  67. switch ($iHttpCode) {
  68. case 401:
  69. $mixedResult = _t('_bx_drupal_wrong_username_or_pass');
  70. break;
  71. default:
  72. $mixedResult = curl_error($rCurl);
  73. }
  74. }
  75. curl_close($rCurl);
  76. return $mixedResult;
  77. }
  78. }
  79. /** @} */