BxNtfsCronNotify.php 6.6 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 Notifications Notifications
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxNtfsCronNotify extends BxDolCron
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_iAddThreshold;
  16. public function __construct()
  17. {
  18. $this->_sModule = 'bx_notifications';
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. parent::__construct();
  21. }
  22. public function processing()
  23. {
  24. $CNF = &$this->_oModule->_oConfig->CNF;
  25. $iCount = (int)$this->_oModule->_oDb->queueGet(array('type' => 'count'));
  26. if($iCount > $CNF['PARAM_QUEUE_ADD_THRESHOLD'])
  27. return;
  28. $aEvents = $this->_oModule->_oDb->getEventsToProcess();
  29. foreach($aEvents as $aEvent) {
  30. if(!empty($aEvent['content']) && is_string($aEvent['content']))
  31. $aEvent['content'] = unserialize($aEvent['content']);
  32. $this->_sendNotifications($aEvent);
  33. }
  34. }
  35. protected function _sendNotifications(&$aEvent)
  36. {
  37. $aHandler = $this->_oModule->_oConfig->getHandlers($aEvent['type'] . '_' . $aEvent['action']);
  38. if(empty($aHandler) || !is_array($aHandler))
  39. return;
  40. $aDeliveryTypes = array();
  41. $iId = (int)$aEvent['id'];
  42. $iSilentMode = $this->_oModule->getSilentMode($aEvent['content']);
  43. switch($iSilentMode) {
  44. case BX_BASE_MOD_NTFS_SLTMODE_ABSOLUTE:
  45. case BX_NTFS_SLTMODE_ABSOLUTE:
  46. case BX_NTFS_SLTMODE_SITE:
  47. return;
  48. case BX_NTFS_SLTMODE_SITE_EMAIL:
  49. $aDeliveryTypes[] = BX_BASE_MOD_NTFS_DTYPE_EMAIL;
  50. break;
  51. case BX_NTFS_SLTMODE_SITE_PUSH:
  52. $aDeliveryTypes[] = BX_BASE_MOD_NTFS_DTYPE_PUSH;
  53. break;
  54. default:
  55. $aDeliveryTypes = array(BX_BASE_MOD_NTFS_DTYPE_EMAIL, BX_BASE_MOD_NTFS_DTYPE_PUSH);
  56. }
  57. $aSendUsing = array();
  58. foreach($aDeliveryTypes as $sDeliveryType) {
  59. $aHidden = $this->_oModule->_oConfig->getHandlersHidden($sDeliveryType);
  60. if(in_array($aHandler['id'], $aHidden))
  61. continue;
  62. $sMethodPostfix = bx_gen_method_name($sDeliveryType);
  63. $sMethodGet = 'getNotification' . $sMethodPostfix;
  64. $sMethodSend = 'sendNotification' . $sMethodPostfix;
  65. if(!$this->_oModule->_oTemplate->isMethodExists($sMethodGet) || !method_exists($this->_oModule, $sMethodSend))
  66. continue;
  67. $aSendUsing[$sDeliveryType] = array(
  68. 'method_get' => $sMethodGet,
  69. 'method_send' => $sMethodSend,
  70. );
  71. }
  72. if(empty($aSendUsing) || !is_array($aSendUsing))
  73. return;
  74. $iOwner = (int)$aEvent['owner_id'];
  75. $aRecipients = array();
  76. //--- Get recipients: Subscribers.
  77. $oConnection = BxDolConnection::getObjectInstance($this->_oModule->_oConfig->getObject('conn_subscriptions'));
  78. $aSubscribers = $oConnection->getConnectedInitiators($iOwner);
  79. if(!empty($aSubscribers) && is_array($aSubscribers)) {
  80. $oOwner = BxDolProfile::getInstance($iOwner);
  81. if(!empty($oOwner)) {
  82. $sSettingType = bx_srv($oOwner->getModule(), 'act_as_profile') ? BX_NTFS_STYPE_FOLLOW_MEMBER : BX_NTFS_STYPE_FOLLOW_CONTEXT;
  83. foreach($aSubscribers as $iSubscriber)
  84. $this->_addRecipient($iSubscriber, $sSettingType, $aRecipients);
  85. }
  86. }
  87. //--- Get recipients: Content owner.
  88. $iObjectOwner = (int)$aEvent['object_owner_id'];
  89. if($iOwner != $iObjectOwner)
  90. $this->_addRecipient($iObjectOwner, BX_NTFS_STYPE_PERSONAL, $aRecipients);
  91. $bDeliveryTimeout = $this->_oModule->_oConfig->getDeliveryTimeout() > 0;
  92. //--- Check recipients and send notifications.
  93. $oPrivacyInt = BxDolPrivacy::getObjectInstance($this->_oModule->_oConfig->getObject('privacy_view'));
  94. $oPrivacyExt = $this->_oModule->_oConfig->getPrivacyObject($aEvent['type'] . '_' . $aEvent['action']);
  95. foreach($aRecipients as $iRecipient => $aSettingTypes) {
  96. $iIdRead = $this->_oModule->_oDb->getLastRead($iRecipient);
  97. if($iIdRead >= $iId)
  98. continue;
  99. $oProfile = BxDolProfile::getInstance($iRecipient);
  100. if(!$oProfile)
  101. continue;
  102. if(!bx_srv($oProfile->getModule(), 'act_as_profile'))
  103. continue;
  104. if($oPrivacyExt !== false && !$oPrivacyExt->check($aEvent['id'], $iRecipient))
  105. continue;
  106. if($oPrivacyInt !== false && !$oPrivacyInt->check($aEvent['id'], $iRecipient))
  107. continue;
  108. foreach($aSendUsing as $sDeliveryType => $aDeliveryType)
  109. foreach($aSettingTypes as $sSettingType) {
  110. $aSetting = $this->_oModule->_oDb->getSetting(array('by' => 'tsu_allowed', 'handler_id' => $aHandler['id'], 'delivery' => $sDeliveryType, 'type' => $sSettingType, 'user_id' => $iRecipient));
  111. if(empty($aSetting) || !is_array($aSetting))
  112. continue;
  113. if((int)$aSetting['active_adm'] == 0 || (int)$aSetting['active_pnl'] == 0)
  114. continue;
  115. $mixedNotification = $this->_oModule->_oTemplate->{$aDeliveryType['method_get']}($iRecipient, $aEvent);
  116. if($mixedNotification === false)
  117. continue;
  118. /**
  119. * 'break' is essential in the next two conditions to avoid
  120. * duplicate sending to the same recipient.
  121. */
  122. if($bDeliveryTimeout && $this->_oModule->_oDb->queueAdd(array(
  123. 'profile_id' => $iRecipient,
  124. 'event_id' => $aEvent['id'],
  125. 'delivery' => $sDeliveryType,
  126. 'content' => serialize($mixedNotification),
  127. 'date' => time()
  128. )) !== false)
  129. break;
  130. if($this->_oModule->{$aDeliveryType['method_send']}($oProfile, $mixedNotification) !== false)
  131. break;
  132. }
  133. }
  134. }
  135. protected function _addRecipient($iUser, $sSettingType, &$aRecipients)
  136. {
  137. if(!isset($aRecipients[$iUser]))
  138. $aRecipients[$iUser] = array();
  139. $aRecipients[$iUser][] = $sSettingType;
  140. }
  141. }
  142. /** @} */