BxDolPush.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 BxDolDb 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. );
  54. $oChannel = curl_init();
  55. curl_setopt($oChannel, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
  56. curl_setopt($oChannel, CURLOPT_HTTPHEADER, array(
  57. 'Content-Type: application/json; charset=utf-8',
  58. 'Authorization: Basic ' . $this->_sRestApi
  59. ));
  60. curl_setopt($oChannel, CURLOPT_RETURNTRANSFER, true);
  61. curl_setopt($oChannel, CURLOPT_HEADER, false);
  62. curl_setopt($oChannel, CURLOPT_POST, true);
  63. curl_setopt($oChannel, CURLOPT_POSTFIELDS, json_encode($aFields));
  64. curl_setopt($oChannel, CURLOPT_SSL_VERIFYPEER, false);
  65. $sResult = curl_exec($oChannel);
  66. curl_close($oChannel);
  67. return $sResult;
  68. }
  69. }
  70. /** @} */