BxDolPushWonderPush.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 BxDolPushWonderPush extends BxDolPush
  10. {
  11. protected $_sEndpoint;
  12. protected $_sAppId;
  13. protected $_sAcessToken;
  14. protected $_sWebKey;
  15. protected $_bCodeAdded;
  16. protected function __construct($aObject, $oTemplate = null, $sDbClassName = '')
  17. {
  18. parent::__construct($aObject, $oTemplate, $sDbClassName);
  19. $this->_sEndpoint = 'https://management-api.wonderpush.com/v1/deliveries';
  20. $this->_sAppId = getParam('sys_push_wonderpush_app_id');
  21. $this->_sAcessToken = getParam('sys_push_wonderpush_access_token');
  22. $this->_sWebKey = getParam('sys_push_wonderpush_web_key');
  23. $this->_bCodeAdded = false;
  24. }
  25. public function send($iProfileId, $aMessage, $bAddToQueue = false)
  26. {
  27. if(empty($this->_sAppId) || empty($this->_sAcessToken))
  28. return false;
  29. if($bAddToQueue && BxDolQueuePush::getInstance()->add($iProfileId, $aMessage))
  30. return true;
  31. // prepare params
  32. $sUrlWeb = $sUrlApp = !empty($aMessage['url']) ? $aMessage['url'] : '';
  33. if($this->_sApiUrlRootEmail !== false) {
  34. if($sUrlWeb)
  35. $sUrlWeb = str_replace(BX_DOL_URL_ROOT, $this->_sApiUrlRootEmail, $sUrlWeb);
  36. if(!empty($aMessage['contents']) && is_array($aMessage['contents']))
  37. foreach($aMessage['contents'] as $sKey => $sValue)
  38. $aMessage['contents'][$sKey] = str_replace(BX_DOL_URL_ROOT, $this->_sApiUrlRootEmail, $sValue);
  39. }
  40. if($this->_sApiUrlRootPush !== false) {
  41. if($sUrlApp)
  42. $sUrlApp = str_replace(BX_DOL_URL_ROOT, $this->_sApiUrlRootPush, $sUrlApp);
  43. }
  44. else
  45. $sUrlApp = $sUrlWeb;
  46. if(empty($aMessage['icon']))
  47. $aMessage['icon'] = BxTemplFunctions::getInstance()->getMainLogoUrl();
  48. // construct notification object
  49. $aFields = [
  50. 'accessToken' => $this->_sAcessToken,
  51. 'targetUserIds' => encryptUserId($iProfileId),
  52. ];
  53. $aNotification = [
  54. 'alert' => [],
  55. ];
  56. if (!empty($aMessage['contents'])) {
  57. $aNotification['alert']['text'] = is_array($aMessage['contents']) ? $aMessage['contents']['en'] : $aMessage['contents'];
  58. }
  59. if (!empty($aMessage['headings'])) {
  60. $aNotification['alert']['title'] = is_array($aMessage['headings']) ? $aMessage['headings']['en'] : $aMessage['headings'];
  61. }
  62. if(!empty($aMessage['icon'])){
  63. $aNotification['alert']['web'] = $aNotification['alert']['web'] ?? [];
  64. $aNotification['alert']['web']['icon'] = $aMessage['icon'];
  65. $aNotification['alert']['android'] = $aNotification['alert']['android'] ?? [];
  66. $aNotification['alert']['android']['largeIcon'] = $aMessage['icon'];
  67. $aNotification['alert']['ios'] = $aNotification['alert']['ios'] ?? [];
  68. $aNotification['alert']['ios']['attachments'] = ['url' => $aMessage['icon']];
  69. }
  70. if (!empty($sUrlWeb)) {
  71. $aNotification['alert']['web'] = $aNotification['alert']['web'] ?? [];
  72. $aNotification['alert']['web']['targetUrl'] = $sUrlWeb;
  73. $aFields['push'] = $aFields['push'] ?? [];
  74. $aFields['push']['custom'] = $aFields['push']['custom'] ?? [];
  75. $aFields['push']['custom']['url'] = $sUrlWeb;
  76. }
  77. if (!empty($sUrlApp)) {
  78. $aNotification['alert']['ios'] = $aNotification['alert']['ios'] ?? [];
  79. $aNotification['alert']['ios']['targetUrl'] = $sUrlApp;
  80. $aNotification['alert']['android'] = $aNotification['alert']['android'] ?? [];
  81. $aNotification['alert']['android']['targetUrl'] = $sUrlApp;
  82. }
  83. if('on' == getParam('bx_nexus_option_push_notifications_count')) {
  84. $iBadgeCount = $this->getNotificationsCount($iProfileId);
  85. $aNotification['alert']['ios'] = $aNotification['alert']['ios'] ?? [];
  86. $aNotification['alert']['ios']['badge'] = $iBadgeCount;
  87. }
  88. $aFields['notification'] = json_encode($aNotification);
  89. // send request
  90. $oChannel = curl_init();
  91. curl_setopt($oChannel, CURLOPT_URL, $this->_sEndpoint);
  92. curl_setopt($oChannel, CURLOPT_HTTPHEADER, [
  93. 'Content-Type: application/json; charset=utf-8',
  94. ]);
  95. curl_setopt($oChannel, CURLOPT_RETURNTRANSFER, true);
  96. curl_setopt($oChannel, CURLOPT_HEADER, false);
  97. curl_setopt($oChannel, CURLOPT_POST, true);
  98. curl_setopt($oChannel, CURLOPT_POSTFIELDS, json_encode($aFields));
  99. if (getParam('sys_curl_ssl_allow_untrusted') == 'on')
  100. curl_setopt($oChannel, CURLOPT_SSL_VERIFYPEER, false);
  101. $sResult = curl_exec($oChannel);
  102. $oResult = @json_decode($sResult, true);
  103. // bx_log('sys_push', "Payload:" . $aFields, BX_LOG_DEBUG);
  104. // bx_log('sys_push', "Result:" . $sResult, BX_LOG_DEBUG);
  105. if(isset($oResult['error'])) {
  106. bx_log('sys_push', "Error message:" . $oResult['error']['message'], BX_LOG_ERR);
  107. }
  108. return $sResult;
  109. }
  110. }
  111. /** @} */