Stack = Array(); } function Push($values) { array_push($this->Stack, $values); } function Pop() { if ($this->Count() > 0) { return array_pop($this->Stack); } else { return false; } } function Get() { if ($this->Count() > 0) { // return end($this->Stack); return $this->Stack[count($this->Stack)-1]; } else { return false; } } function Update($values) { $this->Stack[count($this->Stack)-1] = $values; } function Count() { return count($this->Stack); } } class clsCachedPermissions { var $Allow = Array(); var $Deny = Array(); var $CatId; function clsCachedPermissions($CatId) { $this->CatId = $CatId; } function SetCatId($CatId) { $this->CatId = $CatId; } function CheckPermArray($Perm) { if (!isset($this->Allow[$Perm])) { $this->Allow[$Perm] = array(); $this->Deny[$Perm] = array(); } } function AddAllow($Perm, $GroupId) { $this->CheckPermArray($Perm); if (!in_array($GroupId, $this->Allow[$Perm])) { array_push($this->Allow[$Perm], $GroupId); $this->RemoveDeny($Perm, $GroupId); } } function AddDeny($Perm, $GroupId) { $this->CheckPermArray($Perm); if (!in_array($GroupId, $this->Deny[$Perm])) { array_push($this->Deny[$Perm], $GroupId); $this->RemoveAllow($Perm, $GroupId); } } function RemoveDeny($Perm, $GroupId) { if (in_array($GroupId, $this->Deny[$Perm])) { array_splice($this->Deny[$Perm], array_search($GroupId, $this->Deny[$Perm]), 1); } } function RemoveAllow($Perm, $GroupId) { if (in_array($GroupId, $this->Allow[$Perm])) { array_splice($this->Allow[$Perm], array_search($GroupId, $this->Allow[$Perm]), 1); } } function GetInsertSQL() { $values = array(); $has_deny = array(); /*foreach ($this->Deny as $perm => $groups) { if (count($groups) > 0) { $values[] = '('.$this->CatId.', '.$perm.', "", "'.join(',', $groups).'")'; $has_deny[] = $perm; } }*/ foreach ($this->Allow as $perm => $groups) { // if (in_array($perm, $has_deny)) continue; if (count($groups) > 0) { $values[] = '(' .$this->CatId. ', ' .$perm. ', "' .join(',', $groups). '", "")'; } } if (!$values) return ''; $sql = 'INSERT INTO '.GetTablePrefix().'PermCache (CategoryId, PermId, ACL, DACL) VALUES '.join(',', $values); return $sql; } } class clsCacheUpdater { var $Stack; var $iteration; var $totalCats; var $doneCats; var $table; var $root_prefixes = Array(); /** * Kernel Application * * @var kApplication */ var $Application = null; /** * Enter description here... * * @var kDBConnection */ var $Conn = null; function clsCacheUpdater($continuing=false) { $this->Application =& kApplication::Instance(); $this->Conn =& $this->Application->GetADODBConnection(); foreach ($this->Application->ModuleInfo as $module_name => $module_info) { $this->root_prefixes[ $module_info['RootCat'] ] = $module_info['Var']; } $this->conn =& GetADODBConnection(); $this->iteration = 0; $this->table=$GLOBALS['objSession']->GetEditTable('permCacheUpdate'); if (!$continuing) { $this->Stack =& new clsRecursionStack(); $sql = 'DELETE FROM '.GetTablePrefix().'PermCache'; $this->conn->Execute($sql); $this->initData(); } else { $this->getData(); // $this->SetStack($data); } } function getDonePercent() { if(!$this->totalCats)return 0; return intval( round( $this->doneCats / $this->totalCats * 100 ) ); } function getData() { $sql='SELECT data FROM '.$this->table; if( $rs = $this->conn->Execute($sql) ) $tmp = unserialize($rs->fields['data']); $this->totalCats = isset($tmp['totalCats']) ? $tmp['totalCats'] : 0; $this->doneCats = isset($tmp['doneCats']) ? $tmp['doneCats'] : 0; if(isset($tmp['stack'])) $this->Stack = $tmp['stack']; else $this->Stack = & new clsRecursionStack(); } function setData() { $tmp=Array ( 'totalCats' =>$this->totalCats, 'doneCats' =>$this->doneCats, 'stack' =>$this->Stack, ); $sql='DELETE FROM '.$this->table; $this->conn->Execute($sql); $sql='INSERT '.$this->table.' SET data="'.addslashes(serialize($tmp)).'"'; $this->conn->Execute($sql); } function initData() { $this->clearData(); // drop table before starting anyway $sql = 'CREATE TABLE '.$this->table.'(data LONGTEXT)'; $this->conn->Execute($sql); $sql='SELECT COUNT(*) as count FROM '.GetTablePrefix().'Category'; if( $rs = $this->conn->Execute($sql) ) $this->totalCats=$rs->fields['count']; $this->doneCats=0; } function clearData() { $sql='DROP TABLE IF EXISTS '.$this->table; $this->conn->Execute($sql); } function DoTheJob() { $data = $this->Stack->Get(); if ($data === false) { //If Stack is empty $data['current_id'] = 0; $data['title'] = Array(); $data['named_path'] = Array(); $data['category_template'] = ''; $data['item_template'] = ''; $this->Stack->Push($data); } if (!isset($data['queried'])) { $this->QueryTitle($data); $this->QueryChildren($data); $this->QueryPermissions($data); $data['queried'] = 1; if($sql = $data['perms']->GetInsertSQL()) { $this->conn->Execute($sql); $this->doneCats++; } $this->iteration++; } // start with first child if we haven't started yet if (!isset($data['current_child'])) $data['current_child'] = 0; // if we have more children if (isset($data['children'][$data['current_child']])) { $next_data = Array(); $next_data['title'] = $data['title']; $next_data['named_path'] = $data['named_path']; $next_data['category_template'] = $data['category_template']; $next_data['item_template'] = $data['item_template']; $next_data['current_id'] = $data['children'][$data['current_child']]; //next iteration should process child $next_data['perms'] = $data['perms']; //we should copy our permissions to child - inheritance $next_data['perms']->SetCatId($next_data['current_id']); $data['current_child']++; $this->Stack->Update($data); //we need to update ourself for the iteration after the next (or further) return to next child $this->Stack->Push($next_data); //next iteration should process this child return true; } else { $this->UpdateCachedPath($data); $this->Stack->Pop(); //remove ourself from stack if we have finished all the childs (or there are none) // we are getting here if we finished with current level, so check if it's first level - then bail out. return $this->Stack->Count() > 0; } } function UpdateCachedPath(&$data) { $fields_hash = Array( 'CachedNavbar' => implode('>', $data['title']), 'NamedParentPath' => implode('/', $data['named_path'] ), 'CachedCategoryTemplate'=> $data['category_template'], 'CachedItemTemplate' => $data['item_template'], ); $this->Conn->doUpdate($fields_hash, TABLE_PREFIX.'Category', 'CategoryId = '.$data['current_id']); } function QueryTitle(&$data) { $category_id = $data['current_id']; $sql = 'SELECT Name, Filename, CategoryTemplate, ItemTemplate FROM '.GetTablePrefix().'Category WHERE CategoryId = '.$category_id; $rs = $this->conn->Execute($sql); if ($rs && !$rs->EOF) { $data['title'][] = $rs->fields['Name']; $data['named_path'][] = $rs->fields['Filename']; // it is one of the modules root category $root_prefix = isset($this->root_prefixes[$category_id]) ? $this->root_prefixes[$category_id] : false; if ($root_prefix) { $fields_hash = Array(); if (!$rs->fields['CategoryTemplate']) { $rs->fields['CategoryTemplate'] = $this->Application->ConfigValue($root_prefix.'_CategoryTemplate'); $fields_hash['CategoryTemplate'] = $rs->fields['CategoryTemplate']; } if (!$rs->fields['ItemTemplate']) { $rs->fields['ItemTemplate'] = $this->Application->ConfigValue($root_prefix.'_ItemTemplate'); $fields_hash['ItemTemplate'] = $rs->fields['ItemTemplate']; } $this->Conn->doUpdate($fields_hash, TABLE_PREFIX.'Category', 'CategoryId = '.$category_id); } // if explicitly set, then use it; use parent template otherwise if ($rs->fields['CategoryTemplate']) { $data['category_template'] = $rs->fields['CategoryTemplate']; } if ($rs->fields['ItemTemplate']) { $data['item_template'] = $rs->fields['ItemTemplate']; } } } function QueryChildren(&$data) { $sql = sprintf('SELECT CategoryId FROM '.GetTablePrefix().'Category WHERE ParentId = %s', $data['current_id']); $rs = $this->conn->Execute($sql); $data['children'] = Array(); while ($rs && !$rs->EOF) { $data['children'][] = $rs->fields['CategoryId']; $rs->MoveNext(); } } function QueryPermissions(&$data) { // don't search for section "view" permissions here :) $sql = sprintf('SELECT ipc.PermissionConfigId, ip.GroupId, ip.PermissionValue FROM '.GetTablePrefix().'Permissions AS ip LEFT JOIN '.GetTablePrefix().'PermissionConfig AS ipc ON ipc.PermissionName = ip.Permission WHERE (CatId = %s) AND (Permission LIKE "%%.VIEW") AND (ipc.PermissionConfigId IS NOT NULL)', $data['current_id']); $rs = $this->conn->Execute($sql); //create permissions array only if we don't have it yet (set by parent) if (!isset($data['perms'])) { $data['perms'] = new clsCachedPermissions($data['current_id']); } while ($rs && !$rs->EOF) { if ($rs->fields['PermissionValue'] == 1) { $data['perms']->AddAllow($rs->fields['PermissionConfigId'], $rs->fields['GroupId']); } else { $data['perms']->AddDeny($rs->fields['PermissionConfigId'], $rs->fields['GroupId']); } $rs->MoveNext(); } } } ?>