BxDolPushOneSignal.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. $aFields = [
  48. 'app_id' => $this->_sAppId,
  49. 'filters' => [
  50. ['field' => 'tag', 'key' => 'user_hash', 'relation' => '=', 'value' => encryptUserId($iProfileId)]
  51. ],
  52. 'contents' => !empty($aMessage['contents']) && is_array($aMessage['contents']) ? $aMessage['contents'] : [],
  53. 'headings' => !empty($aMessage['headings']) && is_array($aMessage['headings']) ? $aMessage['headings'] : [],
  54. 'web_url' => $sUrlWeb,
  55. 'app_url' => $sUrlApp,
  56. 'data' => [
  57. 'url' => $sUrlWeb
  58. ],
  59. ];
  60. if(empty($aMessage['icon']))
  61. $aMessage['icon'] = BxTemplFunctions::getInstance()->getMainLogoUrl();
  62. if(!empty($aMessage['icon'])){
  63. $aFields['chrome_web_icon'] = $aMessage['icon'];
  64. $aFields['large_icon'] = $aMessage['icon'];
  65. $aFields['ios_attachments'] = ['id'=> $aMessage['icon']];
  66. }
  67. if('on' == getParam('bx_nexus_option_push_notifications_count')) {
  68. $iBadgeCount = $this->getNotificationsCount($iProfileId);
  69. $aFields['ios_badgeType'] = 'SetTo';
  70. $aFields['ios_badgeCount'] = $iBadgeCount;
  71. }
  72. $oChannel = curl_init();
  73. curl_setopt($oChannel, CURLOPT_URL, $this->_sEndpoint);
  74. curl_setopt($oChannel, CURLOPT_HTTPHEADER, [
  75. 'Content-Type: application/json; charset=utf-8',
  76. 'Authorization: Basic ' . $this->_sRestApi
  77. ]);
  78. curl_setopt($oChannel, CURLOPT_RETURNTRANSFER, true);
  79. curl_setopt($oChannel, CURLOPT_HEADER, false);
  80. curl_setopt($oChannel, CURLOPT_POST, true);
  81. curl_setopt($oChannel, CURLOPT_POSTFIELDS, json_encode($aFields));
  82. if (getParam('sys_curl_ssl_allow_untrusted') == 'on')
  83. curl_setopt($oChannel, CURLOPT_SSL_VERIFYPEER, false);
  84. $sResult = curl_exec($oChannel);
  85. $oResult = @json_decode($sResult, true);
  86. if(isset($oResult['errors']))
  87. foreach($oResult['errors'] as $sError) {
  88. bx_log('sys_push', $sError . " Message:" . json_encode($aMessage), BX_LOG_ERR);
  89. }
  90. return $sResult;
  91. }
  92. }
  93. /** @} */