BxDolPush.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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' => $iProfileId,
  54. 'user_hash' => encryptUserId($iProfileId),
  55. 'real_name' => $oProfile->getDisplayName(),
  56. 'email' => $sEmail,
  57. 'email_hash' => $sEmail ? hash_hmac('sha256', $sEmail, getParam('sys_push_app_id')) : '',
  58. );
  59. bx_alert('system', 'push_tags', $iProfileId, $iProfileId, array('tags' => &$a));
  60. return $a;
  61. }
  62. /**
  63. * @param $a - array to fill with notification counter per module
  64. * @return total number of notifications
  65. */
  66. public static function getNotificationsCount($iProfileId = 0, &$aBubbles = null)
  67. {
  68. $iMemberIdCookie = null;
  69. $bLoggedMemberGlobals = null;
  70. if ($iProfileId && $iProfileId != bx_get_logged_profile_id()) {
  71. if (!empty($_COOKIE['memberID']))
  72. $iMemberIdCookie = $_COOKIE['memberID'];
  73. if (!empty($GLOBALS['logged']['member']))
  74. $bLoggedMemberGlobals = $GLOBALS['logged']['member'];
  75. $oProfile = BxDolProfile::getInstance($iProfileId);
  76. $_COOKIE['memberID'] = $oProfile ? $oProfile->getAccountId() : 0;
  77. $GLOBALS['logged']['member'] = $oProfile ? true : false;
  78. }
  79. $aMenusObjects = array('sys_account_notifications', 'sys_toolbar_member');
  80. foreach ($aMenusObjects as $sMenuObject) {
  81. if ($iProfileId && $iProfileId != bx_get_logged_profile_id())
  82. unset($GLOBALS['bxDolClasses']['BxDolMenu!sys_account_notifications']);
  83. $oMenu = BxDolMenu::getObjectInstance($sMenuObject);
  84. if ($iProfileId && $iProfileId != bx_get_logged_profile_id())
  85. unset($GLOBALS['bxDolClasses']['BxDolMenu!sys_account_notifications']);
  86. $bSave = $oMenu->setDisplayAddons(true);
  87. $a = $oMenu->getMenuItems();
  88. $iBubbles = 0;
  89. foreach ($a as $r) {
  90. if (!$r['bx_if:addon']['condition'])
  91. continue;
  92. if (null !== $aBubbles)
  93. $aBubbles[$r['name']] = $r['bx_if:addon']['content']['addon'];
  94. $iBubbles += $r['bx_if:addon']['content']['addon'];
  95. }
  96. }
  97. if ($iProfileId && $iProfileId != bx_get_logged_profile_id()) {
  98. if (null === $iMemberIdCookie)
  99. unset($_COOKIE['memberID']);
  100. else
  101. $_COOKIE['memberID'] = $iMemberIdCookie;
  102. if (null === $bLoggedMemberGlobals)
  103. unset($GLOBALS['logged']['member']);
  104. else
  105. $GLOBALS['logged']['member'] = $bLoggedMemberGlobals;
  106. }
  107. return $iBubbles;
  108. }
  109. public function send($iProfileId, $aMessage, $bAddToQueue = false)
  110. {
  111. if(empty($this->_sAppId) || empty($this->_sRestApi))
  112. return false;
  113. if($bAddToQueue && BxDolQueuePush::getInstance()->add($iProfileId, $aMessage))
  114. return true;
  115. $iBadgeCount = $this->getNotificationsCount($iProfileId);
  116. $aFields = array(
  117. 'app_id' => $this->_sAppId,
  118. 'filters' => array(
  119. array('field' => 'tag', 'key' => 'user_hash', 'relation' => '=', 'value' => encryptUserId($iProfileId))
  120. ),
  121. 'contents' => !empty($aMessage['contents']) && is_array($aMessage['contents']) ? $aMessage['contents'] : array(),
  122. 'headings' => !empty($aMessage['headings']) && is_array($aMessage['headings']) ? $aMessage['headings'] : array(),
  123. 'web_url' => !empty($aMessage['url']) ? $aMessage['url'] : '',
  124. 'data' => array('url' => !empty($aMessage['url']) ? $aMessage['url'] : ''),
  125. 'chrome_web_icon' => !empty($aMessage['icon']) ? $aMessage['icon'] : '',
  126. 'ios_badgeType' => 'SetTo',
  127. 'ios_badgeCount' => $iBadgeCount,
  128. );
  129. $oChannel = curl_init();
  130. curl_setopt($oChannel, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
  131. curl_setopt($oChannel, CURLOPT_HTTPHEADER, array(
  132. 'Content-Type: application/json; charset=utf-8',
  133. 'Authorization: Basic ' . $this->_sRestApi
  134. ));
  135. curl_setopt($oChannel, CURLOPT_RETURNTRANSFER, true);
  136. curl_setopt($oChannel, CURLOPT_HEADER, false);
  137. curl_setopt($oChannel, CURLOPT_POST, true);
  138. curl_setopt($oChannel, CURLOPT_POSTFIELDS, json_encode($aFields));
  139. curl_setopt($oChannel, CURLOPT_SSL_VERIFYPEER, false);
  140. $sResult = curl_exec($oChannel);
  141. curl_close($oChannel);
  142. $oResult = @json_decode($sResult, true);
  143. if(isset($oResult['errors'])){
  144. foreach($oResult['errors'] as $sError) {
  145. bx_log('sys_push', $sError . " Message:" . json_encode($aMessage));
  146. }
  147. }
  148. return $sResult;
  149. }
  150. }
  151. /** @} */