1
0

BxDolPushOneSignal.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolPushOneSignal extends BxDolPush
  10. {
  11. protected $_sEndpoint;
  12. protected $_sAppId;
  13. protected $_sRestApi;
  14. protected $_sShortName;
  15. protected $_sSafariWebId;
  16. protected $_bCodeAdded;
  17. protected function __construct($aObject, $oTemplate = null, $sDbClassName = '')
  18. {
  19. parent::__construct($aObject, $oTemplate, $sDbClassName);
  20. $this->_sEndpoint = 'https://onesignal.com/api/v1/notifications';
  21. $this->_sAppId = getParam('sys_push_onesignal_app_id');
  22. $this->_sRestApi = getParam('sys_push_onesignal_rest_api');
  23. $this->_sShortName = getParam('sys_push_onesignal_short_name');
  24. $this->_sSafariWebId = getParam('sys_push_onesignal_safari_id');
  25. $this->_bCodeAdded = false;
  26. }
  27. public function send($iProfileId, $aMessage, $bAddToQueue = false)
  28. {
  29. if(empty($this->_sAppId) || empty($this->_sRestApi))
  30. return false;
  31. if($bAddToQueue && BxDolQueuePush::getInstance()->add($iProfileId, $aMessage))
  32. return true;
  33. $sUrlWeb = $sUrlApp = !empty($aMessage['url']) ? $aMessage['url'] : '';
  34. if($this->_sApiUrlRootEmail !== false) {
  35. if($sUrlWeb)
  36. $sUrlWeb = str_replace(BX_DOL_URL_ROOT, $this->_sApiUrlRootEmail, $sUrlWeb);
  37. if(!empty($aMessage['contents']) && is_array($aMessage['contents']))
  38. foreach($aMessage['contents'] as $sKey => $sValue)
  39. $aMessage['contents'][$sKey] = str_replace(BX_DOL_URL_ROOT, $this->_sApiUrlRootEmail, $sValue);
  40. }
  41. if($this->_sApiUrlRootPush !== false) {
  42. if($sUrlApp)
  43. $sUrlApp = str_replace(BX_DOL_URL_ROOT, $this->_sApiUrlRootPush, $sUrlApp);
  44. }
  45. else
  46. $sUrlApp = $sUrlWeb;
  47. $aFilters = [
  48. ['field' => 'tag', 'key' => 'user_hash', 'relation' => '=', 'value' => encryptUserId($iProfileId)]
  49. ];
  50. $aContents = !empty($aMessage['contents']) && is_array($aMessage['contents']) ? $aMessage['contents'] : [];
  51. $aHeadings = !empty($aMessage['headings']) && is_array($aMessage['headings']) ? $aMessage['headings'] : [];
  52. $sIcon = !empty($aMessage['icon']) ? $aMessage['icon'] : BxTemplFunctions::getInstance()->getMainLogoUrl();
  53. $bResult = null;
  54. /**
  55. * @hooks
  56. * @hookdef hook-system-check_send_push 'system', 'check_send_push' - hook for disabling push sending
  57. * - $unit_name - equals `system`
  58. * - $action - equals `check_send_push`
  59. * - $object_id - recipient profile id
  60. * - $sender_id - not used
  61. * - $extra_params - array of additional params with the following array keys:
  62. * - `name` - [string] push notification processor
  63. * - `app_id` - [string] App ID
  64. * - `filters` - [array] filters list
  65. * - `contents` - [array] contents
  66. * - `headings` - [array] headings list
  67. * - `web_url` - [string] URL for WEB
  68. * - `app_url` - [string] URL for APP
  69. * - `icon` - [string] icon URL
  70. * - `override_result` - [boolean] override result of `send` function, if the result isn't null sending will stop
  71. * @hook @ref hook-system-check_send_push
  72. */
  73. bx_alert('system', 'check_send_push', $iProfileId, '', [
  74. 'name' => 'onesignal',
  75. 'app_id' => $this->_sAppId,
  76. 'filters' => $aFilters,
  77. 'contents' => $aContents,
  78. 'headings' => $aHeadings,
  79. 'web_url' => $sUrlWeb,
  80. 'app_url' => $sUrlApp,
  81. 'icon' => $sIcon,
  82. 'override_result' => &$bResult,
  83. ]);
  84. if($bResult !== null)
  85. return $bResult;
  86. $aFields = [
  87. 'app_id' => $this->_sAppId,
  88. 'filters' => $aFilters,
  89. 'contents' => $aContents,
  90. 'headings' => $aHeadings,
  91. 'web_url' => $sUrlWeb,
  92. 'app_url' => $sUrlApp,
  93. 'data' => [
  94. 'url' => $sUrlWeb
  95. ]
  96. ];
  97. if($sIcon)
  98. $aFields = array_merge($aFields, [
  99. 'chrome_web_icon' => $sIcon,
  100. 'large_icon' => $sIcon,
  101. 'ios_attachments' => [
  102. 'id' => $sIcon
  103. ]
  104. ]);
  105. if('on' == getParam('bx_nexus_option_push_notifications_count')) {
  106. $aFields['ios_badgeType'] = 'SetTo';
  107. $aFields['ios_badgeCount'] = $this->getNotificationsCount($iProfileId);
  108. }
  109. $oChannel = curl_init();
  110. curl_setopt($oChannel, CURLOPT_URL, $this->_sEndpoint);
  111. curl_setopt($oChannel, CURLOPT_HTTPHEADER, [
  112. 'Content-Type: application/json; charset=utf-8',
  113. 'Authorization: Basic ' . $this->_sRestApi
  114. ]);
  115. curl_setopt($oChannel, CURLOPT_RETURNTRANSFER, true);
  116. curl_setopt($oChannel, CURLOPT_HEADER, false);
  117. curl_setopt($oChannel, CURLOPT_POST, true);
  118. curl_setopt($oChannel, CURLOPT_POSTFIELDS, json_encode($aFields));
  119. if (getParam('sys_curl_ssl_allow_untrusted') == 'on')
  120. curl_setopt($oChannel, CURLOPT_SSL_VERIFYPEER, false);
  121. $sResult = curl_exec($oChannel);
  122. $oResult = @json_decode($sResult, true);
  123. if(isset($oResult['errors']))
  124. foreach($oResult['errors'] as $sError) {
  125. bx_log('sys_push', $sError . " Message:" . json_encode($aMessage), BX_LOG_ERR);
  126. }
  127. return $sResult;
  128. }
  129. }
  130. /** @} */