| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Access Control Cache |
| 38 | */ |
| 39 | class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_Cache { |
| 40 | |
| 41 | static $_cache = NULL; |
| 42 | |
| 43 | /** |
| 44 | * @param int $id |
| 45 | * |
| 46 | * @return mixed |
| 47 | */ |
| 48 | public static function &build($id) { |
| 49 | if (!self::$_cache) { |
| 50 | self::$_cache = array(); |
| 51 | } |
| 52 | |
| 53 | if (array_key_exists($id, self::$_cache)) { |
| 54 | return self::$_cache[$id]; |
| 55 | } |
| 56 | |
| 57 | // check if this entry exists in db |
| 58 | // if so retrieve and return |
| 59 | self::$_cache[$id] = self::retrieve($id); |
| 60 | if (self::$_cache[$id]) { |
| 61 | return self::$_cache[$id]; |
| 62 | } |
| 63 | |
| 64 | self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact($id); |
| 65 | self::store($id, self::$_cache[$id]); |
| 66 | return self::$_cache[$id]; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param int $id |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | public static function retrieve($id) { |
| 75 | $query = " |
| 76 | SELECT acl_id |
| 77 | FROM civicrm_acl_cache |
| 78 | WHERE contact_id = %1 |
| 79 | "; |
| 80 | $params = array(1 => array($id, 'Integer')); |
| 81 | |
| 82 | if ($id == 0) { |
| 83 | $query .= " OR contact_id IS NULL"; |
| 84 | } |
| 85 | |
| 86 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 87 | |
| 88 | $cache = array(); |
| 89 | while ($dao->fetch()) { |
| 90 | $cache[$dao->acl_id] = 1; |
| 91 | } |
| 92 | return $cache; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param int $id |
| 97 | * @param array $cache |
| 98 | */ |
| 99 | public static function store($id, &$cache) { |
| 100 | foreach ($cache as $aclID => $data) { |
| 101 | $dao = new CRM_ACL_DAO_Cache(); |
| 102 | if ($id) { |
| 103 | $dao->contact_id = $id; |
| 104 | } |
| 105 | $dao->acl_id = $aclID; |
| 106 | |
| 107 | $cache[$aclID] = 1; |
| 108 | |
| 109 | $dao->save(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param int $id |
| 115 | */ |
| 116 | public static function deleteEntry($id) { |
| 117 | if (self::$_cache && |
| 118 | array_key_exists($id, self::$_cache) |
| 119 | ) { |
| 120 | unset(self::$_cache[$id]); |
| 121 | } |
| 122 | |
| 123 | $query = " |
| 124 | DELETE FROM civicrm_acl_cache |
| 125 | WHERE contact_id = %1 |
| 126 | "; |
| 127 | $params = array(1 => array($id, 'Integer')); |
| 128 | CRM_Core_DAO::executeQuery($query, $params); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param int $id |
| 133 | */ |
| 134 | public static function updateEntry($id) { |
| 135 | // rebuilds civicrm_acl_cache |
| 136 | self::deleteEntry($id); |
| 137 | self::build($id); |
| 138 | |
| 139 | // rebuilds civicrm_acl_contact_cache |
| 140 | CRM_Contact_BAO_Contact_Permission::cache($id, CRM_Core_Permission::VIEW, TRUE); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Deletes all the cache entries. |
| 145 | */ |
| 146 | public static function resetCache() { |
| 147 | // reset any static caching |
| 148 | self::$_cache = NULL; |
| 149 | |
| 150 | // reset any db caching |
| 151 | $config = CRM_Core_Config::singleton(); |
| 152 | $smartGroupCacheTimeout = CRM_Contact_BAO_GroupContactCache::smartGroupCacheTimeout(); |
| 153 | |
| 154 | //make sure to give original timezone settings again. |
| 155 | $now = CRM_Utils_Date::getUTCTime(); |
| 156 | |
| 157 | $query = " |
| 158 | DELETE |
| 159 | FROM civicrm_acl_cache |
| 160 | WHERE modified_date IS NULL |
| 161 | OR (TIMESTAMPDIFF(MINUTE, modified_date, $now) >= $smartGroupCacheTimeout) |
| 162 | "; |
| 163 | CRM_Core_DAO::singleValueQuery($query); |
| 164 | |
| 165 | // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction |
| 166 | // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe |
| 167 | if (CRM_Core_Transaction::isActive()) { |
| 168 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () { |
| 169 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
| 170 | }); |
| 171 | } |
| 172 | else { |
| 173 | CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | } |