BxDolPush.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. public function send($iProfileId, $aMessage, $bAddToQueue = false)
  39. {
  40. if(empty($this->_sAppId) || empty($this->_sRestApi))
  41. return false;
  42. if($bAddToQueue && BxDolQueuePush::getInstance()->add($iProfileId, $aMessage))
  43. return true;
  44. $aFields = array(
  45. 'app_id' => $this->_sAppId,
  46. 'filters' => array(
  47. array('field' => 'tag', 'key' => 'user', 'relation' => '=', 'value' => $iProfileId)
  48. ),
  49. 'contents' => !empty($aMessage['contents']) && is_array($aMessage['contents']) ? $aMessage['contents'] : array(),
  50. 'headings' => !empty($aMessage['headings']) && is_array($aMessage['headings']) ? $aMessage['headings'] : array(),
  51. 'url' => !empty($aMessage['url']) ? $aMessage['url'] : '',
  52. 'chrome_web_icon' => !empty($aMessage['icon']) ? $aMessage['icon'] : '',
  53. 'ios_badgeType' => 'Increase',
  54. 'ios_badgeCount' => 1,
  55. );
  56. $oChannel = curl_init();
  57. curl_setopt($oChannel, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
  58. curl_setopt($oChannel, CURLOPT_HTTPHEADER, array(
  59. 'Content-Type: application/json; charset=utf-8',
  60. 'Authorization: Basic ' . $this->_sRestApi
  61. ));
  62. curl_setopt($oChannel, CURLOPT_RETURNTRANSFER, true);
  63. curl_setopt($oChannel, CURLOPT_HEADER, false);
  64. curl_setopt($oChannel, CURLOPT_POST, true);
  65. curl_setopt($oChannel, CURLOPT_POSTFIELDS, json_encode($aFields));
  66. curl_setopt($oChannel, CURLOPT_SSL_VERIFYPEER, false);
  67. $sResult = curl_exec($oChannel);
  68. curl_close($oChannel);
  69. return $sResult;
  70. }
  71. }
  72. /** @} */