snippet.haprofile.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /** @var array $scriptProperties */
  3. $modx->error->reset();
  4. if (!$modx->loadClass('hybridauth', MODX_CORE_PATH . 'components/hybridauth/model/hybridauth/', false, true)) {
  5. return;
  6. }
  7. $HybridAuth = new HybridAuth($modx, $scriptProperties);
  8. $HybridAuth->initialize($modx->context->key);
  9. if ($modx->error->hasError()) {
  10. return $modx->error->message;
  11. } elseif (!$modx->user->isAuthenticated($modx->context->key)) {
  12. return $modx->lexicon('ha_err_not_logged_in');
  13. }
  14. if (empty($profileTpl)) {
  15. $profileTpl = 'tpl.HybridAuth.profile';
  16. }
  17. if (empty($profileFields)) {
  18. $profileFields = 'username:25,email:50,fullname:50,phone:12,mobilephone:12,dob:10,gender,address,country,city,state,zip,fax,photo,comment,website';
  19. }
  20. if (empty($requiredFields)) {
  21. $requiredFields = 'username,email,fullname';
  22. }
  23. if (empty($providerTpl)) {
  24. $providerTpl = 'tpl.HybridAuth.provider';
  25. }
  26. if (empty($activeProviderTpl)) {
  27. $activeProviderTpl = 'tpl.HybridAuth.provider.active';
  28. }
  29. $data = array();
  30. // Update of profile
  31. if ((!empty($_REQUEST['action']) && strtolower($_REQUEST['action']) == 'updateprofile') || (!empty($_REQUEST['hauth_action']) && strtolower($_REQUEST['hauth_action']) == 'updateprofile')) {
  32. $profileFields = array_map('trim', explode(',', $profileFields));
  33. foreach ($profileFields as $field) {
  34. if (strpos($field, ':') !== false) {
  35. list($key, $length) = explode(':', $field);
  36. } else {
  37. $key = $field;
  38. $length = 0;
  39. }
  40. if (isset($_REQUEST[$key])) {
  41. if ($key == 'comment') {
  42. $data[$key] = empty($length) ? $_REQUEST[$key] : substr($_REQUEST[$key], $length);
  43. } else {
  44. $data[$key] = $HybridAuth->Sanitize($_REQUEST[$key], $length);
  45. }
  46. }
  47. }
  48. $data['requiredFields'] = array_map('trim', explode(',', $requiredFields));
  49. /** @var modProcessorResponse $response */
  50. $response = $HybridAuth->runProcessor('web/user/update', $data);
  51. if ($response->isError()) {
  52. $data['error.message'] = $response->getMessage();
  53. foreach ($response->errors as $error) {
  54. $data['error.' . $error->field] = $error->message;
  55. }
  56. }
  57. $data['success'] = (integer)!$response->isError();
  58. }
  59. $add = array();
  60. if ($services = $modx->user->getMany('Services')) {
  61. /** @var haUserService $service */
  62. foreach ($services as $service) {
  63. $add = array_merge($add, $service->toArray(strtolower($service->get('provider') . '.')));
  64. }
  65. }
  66. $url = $HybridAuth->getUrl();
  67. $user = $modx->user->toArray();
  68. $profile = $modx->user->Profile->toArray();
  69. $data = array_merge(
  70. $user,
  71. $profile,
  72. $add,
  73. $data,
  74. array(
  75. 'login_url' => $url . 'login',
  76. 'logout_url' => $url . 'logout',
  77. 'providers' => $HybridAuth->getProvidersLinks($providerTpl, $activeProviderTpl),
  78. 'gravatar' => 'https://gravatar.com/avatar/' . md5(strtolower($profile['email'])),
  79. )
  80. );
  81. return $HybridAuth->getChunk($profileTpl, $data);