BxDolPrivacyQuery.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 BxDolPrivacyQuery extends BxDolDb
  10. {
  11. protected $_aObject;
  12. protected $_sTable;
  13. protected $_sFieldId;
  14. protected $_sFieldOwnerId;
  15. protected $_sCachePrivacyObject;
  16. protected $_sCachePrivacyObjectDefault;
  17. protected $_sCacheGroup;
  18. protected $_sCacheGroupFriends;
  19. protected $_sCacheGroupsActive;
  20. protected $_sCacheTestedObject;
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->_aObject = array();
  25. $this->_sCachePrivacyObject = 'sys_privacy_object_';
  26. $this->_sCachePrivacyObjectDefault = 'sys_privacy_object_default_';
  27. $this->_sCacheGroup = 'sys_privacy_group_';
  28. $this->_sCacheGroupFriends = 'sys_privacy_group_friends_';
  29. $this->_sCacheGroupsActVis = 'sys_privacy_group_act_vis';
  30. $this->_sCacheTestedObject = 'sys_privacy_tested_object_';
  31. }
  32. static public function getPrivacyObject($sObject)
  33. {
  34. $oDb = BxDolDb::getInstance();
  35. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_privacy` WHERE `object` = ?", $sObject);
  36. $aObject = $oDb->getRow($sQuery);
  37. if(!$aObject || !is_array($aObject))
  38. return false;
  39. return $aObject;
  40. }
  41. public function init($aObject = array())
  42. {
  43. if(empty($aObject) || !is_array($aObject))
  44. return;
  45. $this->_aObject = $aObject;
  46. $this->_sTable = $this->_aObject['table'];
  47. $this->_sFieldId = $this->_aObject['table_field_id'];
  48. $this->_sFieldOwnerId = $this->_aObject['table_field_author'];
  49. }
  50. function getObjectInfo($sAction, $iObjectId)
  51. {
  52. if(empty($this->_sTable) || empty($this->_sFieldId) || empty($this->_sFieldOwnerId))
  53. return array();
  54. $sQuery = $this->prepare("SELECT `" . $this->_sFieldOwnerId . "` AS `owner_id`, `" . $sAction . "` AS `group_id` FROM `" . $this->_sTable . "` WHERE `" . $this->_sFieldId . "`=? LIMIT 1", $iObjectId);
  55. $sCacheKey = $this->_sCacheTestedObject . $this->_sTable . '_' . $this->_aObject['action'] . '_' . $iObjectId;
  56. return $this->fromMemory($sCacheKey, 'getRow', $sQuery);
  57. }
  58. function getGroupsBy($aParams)
  59. {
  60. switch($aParams['type']) {
  61. case 'id':
  62. $sCacheFunction = 'fromCache';
  63. $sCacheName = $this->_sCacheGroup . $aParams['id'];
  64. $sMethod = 'getRow';
  65. $sWhereClause = $this->prepareAsString("`id`=?", (int)$aParams['id']);
  66. break;
  67. case 'active':
  68. $sCacheFunction = 'fromMemory';
  69. $sCacheName = $this->_sCacheGroupsActVis;
  70. $sMethod = 'getAll';
  71. $sWhereClause = "`active`='1' AND `visible`='1'";
  72. break;
  73. }
  74. $sSql = "SELECT
  75. `id`,
  76. `title`,
  77. `check`,
  78. `active`
  79. FROM `sys_privacy_groups`
  80. WHERE " . $sWhereClause;
  81. return !empty($sCacheFunction) && !empty($sCacheName) ? $this->$sCacheFunction($sCacheName, $sMethod, $sSql) : $this->$sMethod($sSql);
  82. }
  83. function getActions($iOwnerId)
  84. {
  85. $sQuery = $this->prepare("SELECT
  86. `tm`.`uri` AS `module_uri`,
  87. `tm`.`title` AS `module_title`,
  88. `ta`.`id` AS `action_id`,
  89. `ta`.`title` AS `action_title`,
  90. `ta`.`default_group` AS `action_default_value`,
  91. `td`.`group_id` AS `default_value`
  92. FROM `sys_objects_privacy` AS `ta`
  93. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  94. INNER JOIN `sys_modules` AS `tm` ON `ta`.`module`=`tm`.`uri`
  95. WHERE 1
  96. ORDER BY `tm`.`title`", $iOwnerId);
  97. return $this->getAll($sQuery);
  98. }
  99. function getTitle($sModule, $sAction)
  100. {
  101. $aAction = $this->_getAction($sModule, $sAction);
  102. return !empty($aAction) && isset($aAction['title']) ? $aAction['title'] : '';
  103. }
  104. function getDefaultGroup($sModule, $sAction)
  105. {
  106. $aAction = $this->_getAction($sModule, $sAction);
  107. return !empty($aAction) && isset($aAction['default_group']) ? $aAction['default_group'] : BX_DOL_PG_DEFAULT;
  108. }
  109. function getDefaultGroupByUser($sModule, $sAction, $iOwnerId)
  110. {
  111. $sQuery = $this->prepare("SELECT
  112. `td`.`group_id`
  113. FROM `sys_objects_privacy` AS `ta`
  114. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  115. WHERE
  116. `ta`.`module`=? AND `ta`.`action`=?
  117. LIMIT 1", $iOwnerId, $sModule, $sAction);
  118. $sCacheKey = $this->_sCachePrivacyObjectDefault . $sModule . '_' . $sAction . '_' . $iOwnerId;
  119. return $this->fromMemory($sCacheKey, 'getOne', $sQuery);
  120. }
  121. function replaceDefaulfGroup($iActionId, $iOwnerId, $iGroupId)
  122. {
  123. $sSql = $this->prepare("REPLACE INTO
  124. `sys_privacy_defaults`
  125. SET
  126. `owner_id`=?,
  127. `action_id`=?,
  128. `group_id`=?", $iOwnerId, $iActionId, $iGroupId);
  129. return $this->query($sSql);
  130. }
  131. function getContentByGroupAsSQLPart($sField, $mixedGroupId)
  132. {
  133. if(is_array($mixedGroupId))
  134. $sWhere = " AND `" . $this->_sTable . "`.`" . $sField . "` IN (" . $this->implode_escape($mixedGroupId) . ")";
  135. else
  136. $sWhere = $this->prepareAsString(" AND `" . $this->_sTable . "`.`" . $sField . "` = ?", $mixedGroupId);
  137. return array(
  138. 'where' => $sWhere
  139. );
  140. }
  141. protected function _getAction($sModule, $sAction)
  142. {
  143. $sQuery = $this->prepare("SELECT
  144. `id`,
  145. `module`,
  146. `action`,
  147. `title`,
  148. `default_group`
  149. FROM `sys_objects_privacy` AS `ta`
  150. WHERE `module`=? AND `action`=?
  151. LIMIT 1", $sModule, $sAction);
  152. $sCacheKey = $this->_sCachePrivacyObject . $sModule . '_' . $sAction;
  153. return $this->fromCache($sCacheKey, 'getRow', $sQuery);
  154. }
  155. }
  156. /** @} */