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