BxDolPush.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * @param $a - array to fill with notification counter per module
  40. * @return total number of notifications
  41. */
  42. public static function getNotificationsCount($iProfileId = 0, &$aBubbles = null)
  43. {
  44. $iMemberIdCookie = null;
  45. $bLoggedMemberGlobals = null;
  46. if ($iProfileId && $iProfileId != bx_get_logged_profile_id()) {
  47. if (!empty($_COOKIE['memberID']))
  48. $iMemberIdCookie = $_COOKIE['memberID'];
  49. if (!empty($GLOBALS['logged']['member']))
  50. $bLoggedMemberGlobals = $GLOBALS['logged']['member'];
  51. $oProfile = BxDolProfile::getInstance($iProfileId);
  52. $_COOKIE['memberID'] = $oProfile ? $oProfile->getAccountId() : 0;
  53. $GLOBALS['logged']['member'] = $oProfile ? true : false;
  54. }
  55. if ($iProfileId && $iProfileId != bx_get_logged_profile_id())
  56. unset($GLOBALS['bxDolClasses']['BxDolMenu!sys_account_notifications']);
  57. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications'); // sys_toolbar_member
  58. if ($iProfileId && $iProfileId != bx_get_logged_profile_id())
  59. unset($GLOBALS['bxDolClasses']['BxDolMenu!sys_account_notifications']);
  60. $a = $oMenu->getMenuItems();
  61. $iBubbles = 0;
  62. foreach ($a as $r) {
  63. if (!$r['bx_if:addon']['condition'])
  64. continue;
  65. if (null !== $aBubbles)
  66. $aBubbles[$r['name']] = $r['bx_if:addon']['content']['addon'];
  67. $iBubbles += $r['bx_if:addon']['content']['addon'];
  68. }
  69. if ($iProfileId && $iProfileId != bx_get_logged_profile_id()) {
  70. if (null === $iMemberIdCookie)
  71. unset($_COOKIE['memberID']);
  72. else
  73. $_COOKIE['memberID'] = $iMemberIdCookie;
  74. if (null === $bLoggedMemberGlobals)
  75. unset($GLOBALS['logged']['member']);
  76. else
  77. $GLOBALS['logged']['member'] = $bLoggedMemberGlobals;
  78. }
  79. return $iBubbles;
  80. }
  81. public function send($iProfileId, $aMessage, $bAddToQueue = false)
  82. {
  83. if(empty($this->_sAppId) || empty($this->_sRestApi))
  84. return false;
  85. if($bAddToQueue && BxDolQueuePush::getInstance()->add($iProfileId, $aMessage))
  86. return true;
  87. $iBadgeCount = $this->getNotificationsCount($iProfileId);
  88. $aFields = array(
  89. 'app_id' => $this->_sAppId,
  90. 'filters' => array(
  91. array('field' => 'tag', 'key' => 'user', 'relation' => '=', 'value' => $iProfileId)
  92. ),
  93. 'contents' => !empty($aMessage['contents']) && is_array($aMessage['contents']) ? $aMessage['contents'] : array(),
  94. 'headings' => !empty($aMessage['headings']) && is_array($aMessage['headings']) ? $aMessage['headings'] : array(),
  95. 'web_url' => !empty($aMessage['url']) ? $aMessage['url'] : '',
  96. 'data' => array('url' => !empty($aMessage['url']) ? $aMessage['url'] : ''),
  97. 'chrome_web_icon' => !empty($aMessage['icon']) ? $aMessage['icon'] : '',
  98. 'ios_badgeType' => 'SetTo',
  99. 'ios_badgeCount' => $iBadgeCount,
  100. );
  101. $oChannel = curl_init();
  102. curl_setopt($oChannel, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
  103. curl_setopt($oChannel, CURLOPT_HTTPHEADER, array(
  104. 'Content-Type: application/json; charset=utf-8',
  105. 'Authorization: Basic ' . $this->_sRestApi
  106. ));
  107. curl_setopt($oChannel, CURLOPT_RETURNTRANSFER, true);
  108. curl_setopt($oChannel, CURLOPT_HEADER, false);
  109. curl_setopt($oChannel, CURLOPT_POST, true);
  110. curl_setopt($oChannel, CURLOPT_POSTFIELDS, json_encode($aFields));
  111. curl_setopt($oChannel, CURLOPT_SSL_VERIFYPEER, false);
  112. $sResult = curl_exec($oChannel);
  113. curl_close($oChannel);
  114. return $sResult;
  115. }
  116. }
  117. /** @} */