1
0

BxDolPrivacyQuery.php 6.2 KB

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