'~^[a-z0-9_]+$~i', 'object_perms' => '~^[a-z,\-_]+$~i', 'object_desc' => '/^[^\n\r\t]+$/', ); function getPrimaryKey() { return 'object_id'; } function getTable() { return 'k4_objects'; } } class ObjectsRecord extends FARecord { //------------------------------------------------- // Deal with caching some of the results. //------------------------------------------------- var $_perm_names; var $_perm_names_perms; var $_channel_names; var $_cache = array( 'perm_names' => FALSE, 'perm_names_perms' => FALSE, 'channel_names' => FALSE, ); //------------------------------------------------- // Some other nice variables. //------------------------------------------------- var $_role_id = 0; //------------------------------------------------- // Get all of the permission names associated with // this permission object. //------------------------------------------------- function &getPermissionNames() { //------------------------------------------------- // We don't have a cached version of this list stored // yet, let's create it. //------------------------------------------------- if(!$this->_cache['perm_names']) { $perms = array(); $order = 1; //------------------------------------------------- // Loop over the object permission names and add // some info in. //------------------------------------------------- foreach(explode(',', $this->get('object_perms')) as $perm_name) { $perms[] = array( 'perm_name' => $perm_name, 'perm_order' => $order ); $order++; } //------------------------------------------------- // Create a new list and cache it. //------------------------------------------------- $this->_perm_names = &new FAArrayIterator($perms); $this->_cache['perm_names'] = TRUE; } return $this->_perm_names; } //------------------------------------------------- // Set the role so that getPermissionNamesPerms knows // what role to get the permissions from. //------------------------------------------------- function setRole($role_id) { $this->_role_id = $role_id; } //------------------------------------------------- // Get the permission names with the grant/deny/neither // permissions. //------------------------------------------------- function &getPermissionNamesPerms() { //------------------------------------------------- // We don't have a cached version of this list stored // yet, let's create it. //------------------------------------------------- if(!$this->_cache['perm_names_perms']) { //------------------------------------------------- // Get the permission that we are editing based on // this object id and the role id that the code has // hopefully inputted. //------------------------------------------------- $finder = &$this->getFinder('perms'); $perm = &$finder->findWhere('object_id=? AND role_id=?', array($this->getId(), $this->_role_id), '', 1); $perm_name_perms = array(); if($perm !== NULL) { //------------------------------------------------- // Convert the grant/deny permissions into binary. //------------------------------------------------- $grant = base_convert($perm->get('perm_grant'), 10, 2); $deny = base_convert($perm->get('perm_deny'), 10, 2); } else { $grant = $deny = ''; } //------------------------------------------------- // Get the permissions and count them. //------------------------------------------------- $perms = preg_split('~,~', $this->get('object_perms'), -1, PREG_SPLIT_NO_EMPTY); $num_perms = count($perms); //------------------------------------------------- // Format the binary. //------------------------------------------------- $grant = strrev(str_pad($grant, $num_perms, '0', STR_PAD_LEFT)); $deny = strrev(str_pad($deny, $num_perms, '0', STR_PAD_RIGHT)); //------------------------------------------------- // Get the permission names so we can add to them. //------------------------------------------------- $perms = &$this->getPermissionNames(); $i = 0; //------------------------------------------------- // Loop over each permission name so that we can put // in if the perm has grant/none/deny. //------------------------------------------------- while($perms->next()) { $perm = &$perms->current(); //------------------------------------------------- // Add the perms in... //------------------------------------------------- $perm['perm_grant'] = $grant{$i}; $perm['perm_none'] = $grant{$i} == $deny{$i} ? 1 : 0; $perm['perm_deny'] = $deny{$i}; //------------------------------------------------- // Add the permission to the array that will be passed // to an array iterator. //------------------------------------------------- $perm_name_perms[] = &$perm; $i++; } //------------------------------------------------- // Create a new list and cache it. //------------------------------------------------- $this->_perm_names_perms = &new FAArrayIterator($perm_name_perms); $this->_cache['perm_names_perms'] = TRUE; } return $this->_perm_names_perms; } //------------------------------------------------- // Get a channel name for a specific object. //------------------------------------------------- /* function getChannelName() { $channel_name = ''; //------------------------------------------------- // So, we haven't cached the channel objects. Let's go // and get them :D //------------------------------------------------- if(!$this->_cache['channel_names']) { //------------------------------------------------- // Get the channels. //------------------------------------------------- $finder = &$this->getFinder('channels'); $channels = &$finder->findAll('', ''); //------------------------------------------------- // So, we've got some channels, let's loop them and // get put their names in an array. //------------------------------------------------- $this->_channel_names = array(); //------------------------------------------------- // Hazaa, we've got the channels. //------------------------------------------------- if($channels !== NULL) { //------------------------------------------------- // Loop through them and add their names to an array. //------------------------------------------------- while($channels->next()) { $channel = &$channels->current(); $this->_channel_names[$channel->getId()] = $channel->get('name'); } //------------------------------------------------- // Cache the results. //------------------------------------------------- $this->_cache['channel_names'] = TRUE; } } //------------------------------------------------- // Derive the channel id from the object name. //------------------------------------------------- preg_match("~[0-9]+$~", $this->get('object_name'), $matches); $channel_id = intval($matches[0]); //------------------------------------------------- // Get our channel name! //------------------------------------------------- $channel_name = isset($this->_channel_names[$channel_id]) ? $this->_channel_names[$channel_id] : $channel_name; //------------------------------------------------- // We're done! //------------------------------------------------- return $channel_name; }*/ //------------------------------------------------- // Remove a permission. //------------------------------------------------- function removePermission($perm_name) { //------------------------------------------------- // We're deleting a permission name. //------------------------------------------------- $order = 0; $i = 0; $names = &$this->getPermissionNames(); $new_names = array(); $found = FALSE; //------------------------------------------------- // Loop through the current permission names, find the // position of the one we're going to delete and also // make an array of the new permission names to update // the object with. //------------------------------------------------- while($names->next()) { $name = &$names->current(); if($name['perm_name'] == $perm_name) { $order = $i; $found = TRUE; } else { $new_names[] = $name['perm_name']; } $i++; } //------------------------------------------------- // if we have found the permission, let's do stuff! //------------------------------------------------- if($found) { //------------------------------------------------- // Set the new permissions and update the bitfields. //------------------------------------------------- $this->set('object_perms', implode(",", $new_names)); $this->_removefromBitfields($order, $i); } } //------------------------------------------------- // Update the bitfields for when a permission is removed. //------------------------------------------------- function _removefromBitfields($place, $num_perms) { //------------------------------------------------- // Now, let's go through all of the permissions that use // this object and fix their bitfields. //------------------------------------------------- $finder = &$this->getFinder('perms'); $perms = &$finder->findAllBy("object_id", $this->getId()); while($perms->next()) { $perm = &$perms->current(); //------------------------------------------------- // Get the changed permissions. //------------------------------------------------- $perm_grant = $this->_changeBitfield($perm->get('perm_grant'), $num_perms, $place); $perm_deny = $this->_changeBitfield($perm->get('perm_deny'), $num_perms, $place); //------------------------------------------------- // Update the permission. //------------------------------------------------- $perm->set('perm_grant', $perm_grant); $perm->set('perm_deny', $perm_deny); $perm->save(); } } //------------------------------------------------- // Change a bitfield and return the resulting number. //------------------------------------------------- function _changeBitfield($perm, $num_perms, $place = FALSE) { //------------------------------------------------- // Figure out what the bitfield is. //------------------------------------------------- $bitfield = base_convert($perm, 10, 2); $bitfield = str_pad($bitfield, $num_perms, '0', STR_PAD_RIGHT); if($place !== FALSE) { //------------------------------------------------- // Now remove the bit that represents out permission. //------------------------------------------------- $bitfield = substr_utf($bitfield, 0, $place) . substr_utf($bitfield, $place + 1); } //------------------------------------------------- // Figure out what the permission should end up being. //------------------------------------------------- $bitfield = strrev($bitfield); $perm = base_convert($bitfield, 2, 10); return $perm; } //------------------------------------------------- // Add a permission to an object. //------------------------------------------------- function addPermission($perm_name) { //------------------------------------------------- // Check if we should add this permission. //------------------------------------------------- if($this->permissionExists($perm_name) === FALSE) { $this->set('object_perms', $this->get('object_perms') .','. $perm_name); $this->_addToBitfields(); } } //------------------------------------------------- // Go through the new permissions and add a 0 to the // end of their bitfields. //------------------------------------------------- function _addToBitfields() { //------------------------------------------------- // Figure out how many perms we are dealing with. //------------------------------------------------- $perms = explode(',', $this->get('object_perms')); $num_perms = count($perms); //------------------------------------------------- // Update all the permissions that use this object. //------------------------------------------------- $finder = &$this->getFinder('perms'); $perms = &$finder->findAllBy("object_id", $this->getId()); while($perms->next()) { $perm = &$perms->current(); //------------------------------------------------- // Get the changed permissions. //------------------------------------------------- $perm_grant = $this->_changeBitfield($perm->get('perm_grant'), $num_perms, FALSE); $perm_deny = $this->_changeBitfield($perm->get('perm_deny'), $num_perms, FALSE); //------------------------------------------------- // Update the permission. //------------------------------------------------- $perm->set('perm_grant', $perm_grant); $perm->set('perm_deny', $perm_deny); $perm->save(); } } //------------------------------------------------- // Check to see if a permission exists in this object // and if so, return its position, otherwise, return // FALSE. //------------------------------------------------- function permissionExists($perm_name) { $exists = FALSE; $perms = &$this->getPermissionNames(); $postition = 1; while($perms->next()) { $name = &$perms->current(); if($name['perm_name'] == $perm_name) { $exists = TRUE; break; } $position++; } $ret = $position; if(!$exists) { $ret = FALSE; } return $ret; } //------------------------------------------------- // Delete all of the permissions relating to an object. //------------------------------------------------- function deleteAllPerms() { //------------------------------------------------- // Find all the permissions that use this object, loop // through them and delete them. //------------------------------------------------- $finder = &$this->getFinder('perms'); $perms = &$finder->findAllBy("object_id", $this->getId()); while($perms->next()) { $perm = &$perms->current(); $perm->delete(); } } } ?>