BxDolPush.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 BxDolPush extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_sAppId;
  12. protected $_sRestApi;
  13. protected function __construct()
  14. {
  15. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  16. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  17. parent::__construct();
  18. $this->_sAppId = getParam('sys_push_app_id');
  19. $this->_sRestApi = getParam('sys_push_rest_api');
  20. }
  21. /**
  22. * Prevent cloning the instance
  23. */
  24. public function __clone()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. }
  29. /**
  30. * Get singleton instance of the class
  31. */
  32. public static function getInstance()
  33. {
  34. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  35. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolPush();
  36. return $GLOBALS['bxDolClasses'][__CLASS__];
  37. }
  38. /**
  39. * Get tags to send to PUSH server
  40. * @param $iProfileId - profile ID
  41. * @return array of tags
  42. */
  43. public static function getTags($iProfileId = false)
  44. {
  45. if (false === $iProfileId)
  46. $iProfileId = bx_get_logged_profile_id();
  47. $oProfile = BxDolProfile::getInstance($iProfileId);
  48. $oAccount = $oProfile ? $oProfile->getAccountObject() : null;
  49. if (!$oProfile || !$oAccount)
  50. return false;
  51. $sEmail = $oAccount->getEmail();
  52. $a = array (
  53. 'user_hash' => encryptUserId($iProfileId),
  54. 'real_name' => $oProfile->getDisplayName(),
  55. 'email' => $sEmail,
  56. 'email_hash' => $sEmail ? hash_hmac('sha256', $sEmail, getParam('sys_push_app_id')) : '',
  57. );
  58. bx_alert('system', 'push_tags', $iProfileId, $iProfileId, array('tags' => &$a));
  59. return $a;
  60. }
  61. /**
  62. * @param $a - array to fill with notification counter per module
  63. * @return total number of notifications
  64. */
  65. public static function getNotificationsCount($iProfileId = 0, &$aBubbles = null)
  66. {
  67. $iMemberIdCookie = null;
  68. $bLoggedMemberGlobals = null;
  69. if ($iProfileId && $iProfileId != bx_get_logged_profile_id()) {
  70. if (getLoggedId())
  71. $iMemberIdCookie = getLoggedId();
  72. if (!empty($GLOBALS['logged']['member']))
  73. $bLoggedMemberGlobals = $GLOBALS['logged']['member'];
  74. $oProfile = BxDolProfile::getInstance($iProfileId);
  75. $_COOKIE['memberID'] = $oProfile ? $oProfile->getAccountId() : 0;
  76. $GLOBALS['logged']['member'] = $oProfile ? true : false;
  77. }
  78. $aMenusObjects = array('sys_account_notifications', 'sys_toolbar_member');
  79. foreach ($aMenusObjects as $sMenuObject) {
  80. if ($iProfileId && $iProfileId != bx_get_logged_profile_id())
  81. unset($GLOBALS['bxDolClasses']['BxDolMenu!sys_account_notifications']);
  82. $oMenu = BxDolMenu::getObjectInstance($sMenuObject);
  83. if ($iProfileId && $iProfileId != bx_get_logged_profile_id())
  84. unset($GLOBALS['bxDolClasses']['BxDolMenu!sys_account_notifications']);
  85. $bSave = $oMenu->setDisplayAddons(true);
  86. $a = $oMenu->getMenuItems();
  87. $iBubbles = 0;
  88. foreach ($a as $r) {
  89. if (!$r['bx_if:addon']['condition'])
  90. continue;
  91. if (null !== $aBubbles)
  92. $aBubbles[$r['name']] = $r['bx_if:addon']['content']['addon'];
  93. $iBubbles += $r['bx_if:addon']['content']['addon'];
  94. }
  95. }
  96. if ($iProfileId && $iProfileId != bx_get_logged_profile_id()) {
  97. if (null === $iMemberIdCookie)
  98. unset($_COOKIE['memberID']);
  99. else
  100. $_COOKIE['memberID'] = $iMemberIdCookie;
  101. if (null === $bLoggedMemberGlobals)
  102. unset($GLOBALS['logged']['member']);
  103. else
  104. $GLOBALS['logged']['member'] = $bLoggedMemberGlobals;
  105. }
  106. return $iBubbles;
  107. }
  108. public function send($iProfileId, $aMessage, $bAddToQueue = false)
  109. {
  110. if(empty($this->_sAppId) || empty($this->_sRestApi))
  111. return false;
  112. if($bAddToQueue && BxDolQueuePush::getInstance()->add($iProfileId, $aMessage))
  113. return true;
  114. $aFields = array(
  115. 'app_id' => $this->_sAppId,
  116. 'filters' => array(
  117. array('field' => 'tag', 'key' => 'user_hash', 'relation' => '=', 'value' => encryptUserId($iProfileId))
  118. ),
  119. 'contents' => !empty($aMessage['contents']) && is_array($aMessage['contents']) ? $aMessage['contents'] : array(),
  120. 'headings' => !empty($aMessage['headings']) && is_array($aMessage['headings']) ? $aMessage['headings'] : array(),
  121. 'web_url' => !empty($aMessage['url']) ? $aMessage['url'] : '',
  122. 'data' => array('url' => !empty($aMessage['url']) ? $aMessage['url'] : ''),
  123. 'chrome_web_icon' => !empty($aMessage['icon']) ? $aMessage['icon'] : '',
  124. );
  125. if ('on' == getParam('bx_nexus_option_push_notifications_count')) {
  126. $iBadgeCount = $this->getNotificationsCount($iProfileId);
  127. $aFields['ios_badgeType'] = 'SetTo';
  128. $aFields['ios_badgeCount'] = $iBadgeCount;
  129. }
  130. $oChannel = curl_init();
  131. curl_setopt($oChannel, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
  132. curl_setopt($oChannel, CURLOPT_HTTPHEADER, array(
  133. 'Content-Type: application/json; charset=utf-8',
  134. 'Authorization: Basic ' . $this->_sRestApi
  135. ));
  136. curl_setopt($oChannel, CURLOPT_RETURNTRANSFER, true);
  137. curl_setopt($oChannel, CURLOPT_HEADER, false);
  138. curl_setopt($oChannel, CURLOPT_POST, true);
  139. curl_setopt($oChannel, CURLOPT_POSTFIELDS, json_encode($aFields));
  140. curl_setopt($oChannel, CURLOPT_SSL_VERIFYPEER, false);
  141. $sResult = curl_exec($oChannel);
  142. curl_close($oChannel);
  143. $oResult = @json_decode($sResult, true);
  144. if(isset($oResult['errors'])){
  145. foreach($oResult['errors'] as $sError) {
  146. bx_log('sys_push', $sError . " Message:" . json_encode($aMessage));
  147. }
  148. }
  149. return $sResult;
  150. }
  151. }
  152. /** @} */