BxNtfsResponse.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 BxNtfsResponse extends BxBaseModNotificationsResponse
  12. {
  13. public function __construct()
  14. {
  15. $this->_sModule = 'bx_notifications';
  16. parent::__construct();
  17. }
  18. /**
  19. * Overwritten method of parent class.
  20. *
  21. * @param BxDolAlerts $oAlert an instance of alert.
  22. */
  23. public function response($oAlert)
  24. {
  25. /**
  26. * @hooks
  27. * @hookdef hook-bx_notifications-before_register_alert 'bx_notifications', 'before_register_alert' - hook to override alert (hook) before processing
  28. * - $unit_name - equals `bx_notifications`
  29. * - $action - equals `before_register_alert`
  30. * - $object_id - not used
  31. * - $sender_id - not used
  32. * - $extra_params - array of additional params with the following array keys:
  33. * - `unit` - [string] alert (hook) unit
  34. * - `action` - [string] alert (hook) action
  35. * - `alert` - [array] by ref, an instance of alert (hook), @see BxDolAlerts, can be overridden in hook processing
  36. * @hook @ref hook-bx_notifications-before_register_alert
  37. */
  38. bx_alert($this->_oModule->getName(), 'before_register_alert', 0, 0, [
  39. 'unit' => $oAlert->sUnit,
  40. 'action' => $oAlert->sAction,
  41. 'alert' => &$oAlert,
  42. ]);
  43. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  44. if($iObjectPrivacyView == BX_DOL_PG_HIDDEN)
  45. return;
  46. $aHandler = $this->_oModule->_oConfig->getHandlers($oAlert->sUnit . '_' . $oAlert->sAction);
  47. if(empty($aHandler) || !is_array($aHandler))
  48. return;
  49. $iSilentMode = $this->_oModule->getSilentMode($oAlert->aExtras);
  50. if(in_array($iSilentMode, array(BX_BASE_MOD_NTFS_SLTMODE_ABSOLUTE, BX_NTFS_SLTMODE_ABSOLUTE)))
  51. return;
  52. switch($aHandler['type']) {
  53. case BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT:
  54. $sMethod = 'getInsertData' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  55. if(!method_exists($this, $sMethod)) {
  56. $sMethod = 'getInsertData' . bx_gen_method_name('any_' . $oAlert->sAction);
  57. if(!method_exists($this, $sMethod))
  58. $sMethod = 'getInsertData';
  59. }
  60. $aDataItems = $this->$sMethod($oAlert, $aHandler);
  61. $sMethod = 'get_notifications_insert_data';
  62. if(bx_is_srv($oAlert->sUnit, $sMethod))
  63. $aDataItems = bx_srv($oAlert->sUnit, $sMethod, [$oAlert, $aHandler, $aDataItems]);
  64. foreach($aDataItems as $aDataItem) {
  65. $iId = $this->_oModule->_oDb->insertEvent($aDataItem);
  66. if(empty($iId))
  67. continue;
  68. $this->_oModule->onPost($iId);
  69. }
  70. break;
  71. case BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE:
  72. $aEvent = $this->_getEventUpdate($oAlert, $aHandler);
  73. if(empty($aEvent) || !is_array($aEvent))
  74. break;
  75. $aDataItem = [
  76. 'object_privacy_view' => $iObjectPrivacyView
  77. ];
  78. $sMethod = 'get_notifications_update_data';
  79. if(bx_is_srv($oAlert->sUnit, $sMethod))
  80. $aDataItem = bx_srv($oAlert->sUnit, $sMethod, [$oAlert, $aHandler, $aEvent, $aDataItem]);
  81. $this->_oModule->_oDb->updateEvent($aDataItem, ['type' => $oAlert->sUnit, 'object_id' => $oAlert->iObject]);
  82. break;
  83. case BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE:
  84. if($oAlert->sUnit == 'profile' && $oAlert->sAction == 'delete') {
  85. $this->_oModule->_oDb->deleteEvent(array('owner_id' => $oAlert->iObject));
  86. $this->_oModule->_oDb->deleteEvent(array('action' => 'connection_added', 'object_id' => $oAlert->iObject));
  87. break;
  88. }
  89. $sMethod = 'getDeleteData' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  90. if(!method_exists($this, $sMethod))
  91. $sMethod = 'getDeleteData';
  92. $aDataItems = $this->$sMethod($oAlert, $aHandler);
  93. $sMethod = 'get_notifications_delete_data';
  94. if(bx_is_srv($oAlert->sUnit, $sMethod))
  95. $aDataItems = bx_srv($oAlert->sUnit, $sMethod, [$oAlert, $aHandler, $aDataItems]);
  96. foreach($aDataItems as $aDataItem)
  97. $this->_oModule->_oDb->deleteEvent($aDataItem);
  98. break;
  99. }
  100. }
  101. protected function getInsertData(&$oAlert, &$aHandler)
  102. {
  103. $iOwnerId = $oAlert->iSender;
  104. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  105. if($iObjectPrivacyView < 0)
  106. $iOwnerId = abs($iObjectPrivacyView);
  107. $mixedSubobjectId = $this->_getSubObjectId($oAlert->aExtras);
  108. if(!is_array($mixedSubobjectId))
  109. $mixedSubobjectId = array($mixedSubobjectId);
  110. $aResult = array();
  111. foreach($mixedSubobjectId as $iSubobjectId)
  112. $aResult[] = array(
  113. 'author_id' => $oAlert->iSender,
  114. 'owner_id' => $iOwnerId,
  115. 'type' => $oAlert->sUnit,
  116. 'action' => $oAlert->sAction,
  117. 'object_id' => $oAlert->iObject,
  118. 'object_owner_id' => $this->_getObjectOwnerId($oAlert->aExtras),
  119. 'object_privacy_view' => $iObjectPrivacyView,
  120. 'subobject_id' => $iSubobjectId,
  121. 'content' => $this->_getContent($oAlert->aExtras),
  122. 'source' => $this->_getSource($oAlert->aExtras),
  123. 'source_mac' => $this->_getSourceMac($oAlert->aExtras),
  124. 'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
  125. 'date' => $this->_getDate($oAlert->aExtras),
  126. 'processed' => 0
  127. );
  128. return $aResult;
  129. }
  130. protected function getDeleteData(&$oAlert, &$aHandler)
  131. {
  132. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  133. return array(
  134. array(
  135. 'type' => $oAlert->sUnit,
  136. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  137. 'object_id' => $oAlert->iObject,
  138. 'subobject_id' => $this->_getSubObjectId($oAlert->aExtras)
  139. )
  140. );
  141. }
  142. protected function getDeleteDataBxTimelineDelete(&$oAlert, &$aHandler)
  143. {
  144. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  145. return array(
  146. array(
  147. 'type' => $oAlert->sUnit,
  148. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  149. 'object_id' => $oAlert->iObject,
  150. 'subobject_id' => $this->_getSubObjectId($oAlert->aExtras)
  151. ),
  152. array(
  153. 'action' => 'timeline_post_common',
  154. 'subobject_id' => $oAlert->iObject
  155. )
  156. );
  157. }
  158. /*
  159. * Custom insert data getter for any module -> commentPost alerts.
  160. */
  161. protected function getInsertDataAnyCommentPost(&$oAlert, &$aHandler)
  162. {
  163. $CNF = &$this->_oModule->_oConfig->CNF;
  164. if(getParam($CNF['PARAM_REPLY_AS_COMMENT']) != 'on' && isset($oAlert->aExtras['comment_parent_id']) && (int)$oAlert->aExtras['comment_parent_id'] != 0)
  165. return [];
  166. return $this->getInsertData($oAlert, $aHandler);
  167. }
  168. /*
  169. * Custom insert data getter for comment -> added and deleted alerts.
  170. */
  171. protected function getInsertDataCommentAdded(&$oAlert, &$aHandler)
  172. {
  173. $CNF = &$this->_oModule->_oConfig->CNF;
  174. if(getParam($CNF['PARAM_COMMENT_POST_EXT']) != 'on')
  175. return [];
  176. $oCmts = BxDolCmts::getObjectInstance($oAlert->aExtras['object_system'], $oAlert->aExtras['object_id']);
  177. if(!$oCmts)
  178. return [];
  179. $iOwnerId = $oAlert->iSender;
  180. $iObjectId = $oAlert->aExtras['object_id'];
  181. $iObjectOwnerId = $this->_getObjectOwnerId($oAlert->aExtras);
  182. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  183. $aResults = $aRecipients = [];
  184. $aComments = $oCmts->getCommentsBy(['type' => 'object_id', 'object_id' => $iObjectId]);
  185. foreach($aComments as $aComment) {
  186. $iCmtAuthorId = (int)$aComment['cmt_author_id'];
  187. if(in_array($iCmtAuthorId, $aRecipients) || $iCmtAuthorId == $iOwnerId)
  188. continue;
  189. $aResults[] = [
  190. 'author_id' => $oAlert->iSender,
  191. 'owner_id' => $iOwnerId,
  192. 'type' => $oAlert->sUnit,
  193. 'action' => $oAlert->sAction,
  194. 'object_id' => $iObjectId,
  195. 'object_owner_id' => $iCmtAuthorId,
  196. 'object_privacy_view' => $iObjectPrivacyView,
  197. 'subobject_id' => $oAlert->aExtras['comment_uniq_id'],
  198. 'content' => $this->_getContent($oAlert->aExtras),
  199. 'source' => $this->_getSource($oAlert->aExtras),
  200. 'source_mac' => $this->_getSourceMac($oAlert->aExtras),
  201. 'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
  202. 'processed' => 0
  203. ];
  204. $aRecipients[] = $iCmtAuthorId;
  205. }
  206. return $aResults;
  207. }
  208. /**
  209. * Custom insert data getter for sys_profiles_friends -> connection_added and connection_removed alerts.
  210. */
  211. protected function getInsertDataSysProfilesFriendsConnectionAdded(&$oAlert, &$aHandler)
  212. {
  213. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  214. $iPrivacyView = $this->_oModule->_oConfig->getPrivacyViewDefault('event');
  215. return [[
  216. 'author_id' => $oAlert->iSender,
  217. 'owner_id' => $oAlert->aExtras['initiator'],
  218. 'type' => $oAlert->sUnit,
  219. 'action' => $oAlert->sAction,
  220. 'object_id' => $oAlert->aExtras['content'],
  221. 'object_owner_id' => $oAlert->aExtras['content'],
  222. 'object_privacy_view' => $iObjectPrivacyView,
  223. 'subobject_id' => 0,
  224. 'content' => $this->_getContent($oAlert->aExtras, array(
  225. 'request' => empty($oAlert->aExtras['mutual']) ? 1 : 0
  226. )),
  227. 'source' => $this->_getSource($oAlert->aExtras),
  228. 'source_mac' => $this->_getSourceMac($oAlert->aExtras),
  229. 'allow_view_event_to' => $iPrivacyView,
  230. 'processed' => 0
  231. ]];
  232. }
  233. protected function getInsertDataMetaMentionAdded(&$oAlert, &$aHandler)
  234. {
  235. $iOwnerId = $oAlert->iSender;
  236. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  237. if($iObjectPrivacyView < 0)
  238. $iOwnerId = abs($iObjectPrivacyView);
  239. $aResult = [[
  240. 'author_id' => $oAlert->iSender,
  241. 'owner_id' => $iOwnerId,
  242. 'type' => $oAlert->sUnit,
  243. 'action' => $oAlert->sAction,
  244. 'object_id' => $oAlert->iObject,
  245. 'object_owner_id' => $this->_getObjectOwnerId($oAlert->aExtras),
  246. 'object_privacy_view' => $iObjectPrivacyView,
  247. 'subobject_id' => 0,
  248. 'content' => $this->_getContent($oAlert->aExtras, array(
  249. 'content_id' => $oAlert->aExtras['content_id'],
  250. 'module' => $oAlert->aExtras['object']
  251. )),
  252. 'source' => $this->_getSource($oAlert->aExtras),
  253. 'source_mac' => $this->_getSourceMac($oAlert->aExtras),
  254. 'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
  255. 'processed' => 0
  256. ]];
  257. return $aResult;
  258. }
  259. protected function getDeleteDataSysProfilesFriendsConnectionRemoved(&$oAlert, &$aHandler)
  260. {
  261. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  262. $sAction = $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'];
  263. return [[
  264. 'owner_id' => $oAlert->aExtras['initiator'],
  265. 'type' => $oAlert->sUnit,
  266. 'action' => $sAction,
  267. 'object_id' => $oAlert->aExtras['content']
  268. ], [
  269. 'owner_id' => $oAlert->aExtras['content'],
  270. 'type' => $oAlert->sUnit,
  271. 'action' => $sAction,
  272. 'object_id' => $oAlert->aExtras['initiator']
  273. ]];
  274. }
  275. /**
  276. * Custom insert data getter for sys_profiles_subscriptions -> connection_added and connection_removed alerts.
  277. */
  278. protected function getInsertDataSysProfilesSubscriptionsConnectionAdded(&$oAlert, &$aHandler)
  279. {
  280. return [[
  281. 'author_id' => $oAlert->iSender,
  282. 'owner_id' => $oAlert->aExtras['initiator'],
  283. 'type' => $oAlert->sUnit,
  284. 'action' => $oAlert->sAction,
  285. 'object_id' => $oAlert->aExtras['content'],
  286. 'object_owner_id' => $oAlert->aExtras['content'],
  287. 'object_privacy_view' => $this->_getObjectPrivacyView($oAlert->aExtras),
  288. 'subobject_id' => 0,
  289. 'content' => $this->_getContent($oAlert->aExtras),
  290. 'source' => $this->_getSource($oAlert->aExtras),
  291. 'source_mac' => $this->_getSourceMac($oAlert->aExtras),
  292. 'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
  293. 'processed' => 0
  294. ]];
  295. }
  296. protected function getDeleteDataSysProfilesSubscriptionsConnectionRemoved(&$oAlert, &$aHandler)
  297. {
  298. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  299. return [[
  300. 'owner_id' => $oAlert->aExtras['initiator'],
  301. 'type' => $oAlert->sUnit,
  302. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  303. 'object_id' => $oAlert->aExtras['content']
  304. ]];
  305. }
  306. protected function _getObjectOwnerId($aExtras)
  307. {
  308. $iResult = parent::_getObjectOwnerId($aExtras);
  309. if(!empty($iResult))
  310. return $iResult;
  311. if(isset($aExtras['meta']))
  312. return (int)$aExtras['meta'];
  313. return 0;
  314. }
  315. protected function _getContent($aExtras, $aAdd = array())
  316. {
  317. $aResult = array();
  318. $iSilentMode = $this->_oModule->getSilentMode($aExtras);
  319. if($iSilentMode != BX_BASE_MOD_NTFS_SLTMODE_DISABLED)
  320. $aResult['silent_mode'] = $iSilentMode;
  321. if(!empty($aAdd) && is_array($aAdd))
  322. $aResult = array_merge($aResult, $aAdd);
  323. return !empty($aResult) && is_array($aResult) ? serialize($aResult) : '';
  324. }
  325. protected function _getSource($aExtras)
  326. {
  327. return $aExtras['source'] ?? '';
  328. }
  329. protected function _getSourceMac($aExtras)
  330. {
  331. return $aExtras['source_mac'] ?? '';
  332. }
  333. protected function _getDate($aExtras)
  334. {
  335. return isset($aExtras['date']) ? $aExtras['date'] : time();
  336. }
  337. }
  338. /** @} */