Exists($page, $type))
return array('title' => $page,
'content' => Subst('{{The page \'' . $page . '\' does not exist.}}'));
if(!$this->HasReadAccess($page, $type))
return array('title' => $page,
'content' => Subst('{{The page \'' . $page . '\' is protected.}}'));
$pagedata = $this->GetPageData($page, $type);
$data = $pagedata['data'];
if($extra) $data = array_merge($data, $extra);
$data['_page'] = $page;
$data['_type'] = $type;
$ret = $GLOBALS['templates'][$pagedata['template']]->GetPage($data);
return array('title' => Subst($ret['title']), 'content' => Subst($ret['content']));
}
function GetEditor($page, $type, $backlink) {
if(!$this->Exists($page, $type))
return array('title' => $page,
'content' => Subst('{{The page \'' . $page . '\' does not exist.}}'));
if(!$this->HasWriteAccess($page, $type))
return array('title' => $page,
'content' => Subst('{{The page \'' . $page . '\' is protected.}}'));
$pagedata = $this->GetPageData($page, $type);
$data = $pagedata['data'];
$data['_page'] = $page;
$data['_type'] = $type;
$data['_backlink'] = $backlink;
$ret = $GLOBALS['templates'][$pagedata['template']]->GetEditor($data);
return array('title' => Subst($ret['title']), 'content' => Subst($ret['content']));
}
function Exists($page, $type) {
if(array_key_exists($page . '.' . $type, $GLOBALS['modules']->pages))
return true;
$res = $GLOBALS['db']->Execute('SELECT id FROM pages WHERE name = ? AND type = ?', array($page, $type));
return ($res->RecordCount() > 0);
}
function HasReadAccess($page, $type) {
if(!$this->Exists($page, $type)) return false;
if($GLOBALS['user']->IsAdmin()) return true;
$gid = $GLOBALS['user']->gid;
$access = $this->GetAccess($page, $type);
return ((hexdec($access[0][$gid/4]) & (1 << ($gid%4))) != 0);
}
function HasWriteAccess($page, $type) {
if(!$this->Exists($page, $type)) return false;
if($GLOBALS['user']->IsAdmin()) return true;
$gid = $GLOBALS['user']->gid;
$access = $this->GetAccess($page, $type);
return ((hexdec($access[1][$gid/4]) & (1 << ($gid%4))) != 0);
}
function GetPageData($page, $type) {
if(!$this->Exists($page, $type)) return null;
if(!array_key_exists($page . '.' . $type, $this->pages)) {
$res = $GLOBALS['db']->Execute('SELECT template, data FROM pages WHERE name = ? AND type = ?', array($page, $type));
if($res->RecordCount()) {
parse_str($res->fields[1], $data);
$data = array_map('Unquote', $data);
$this->pages[$page . '.' . $type] = array('name' => $page, 'type' => $type, 'template' => $res->fields[0],
'access' => '', 'data' => $data);
}
else {
$xmldata = $GLOBALS['xmlparser']->ParseFile($GLOBALS['modules']->GetPagePath($page, $type));
if(!$xmldata) return null;
$info = $GLOBALS['xmlparser']->FindTag($xmldata, 'info');
if(!$info) return null;
$template = $GLOBALS['xmlparser']->FindTag($info, 'template');
if(!$template) return null;
if(count($template['children']) != 1) return;
if(!is_string($template['children'][0])) return;
$template = $template['children'][0];
$access = $GLOBALS['xmlparser']->FindTag($info, 'access');
if(!$access) return null;
if(count($access['children']) != 1) return;
if(!is_string($access['children'][0])) return;
$access = explode(':', $access['children'][0]);
$rawdata = $GLOBALS['xmlparser']->FindTag($xmldata, 'data');
$data = array();
foreach($rawdata['children'] as $field) {
if(!is_array($field)) continue;
if(count($field['children']) != 1) continue;
$data[$field['tag']] = $field['children'][0];
}
$this->pages[$page . '.' . $type] = array('name' => $page, 'type' => $type, 'template' => $template,
'access' => $access, 'data' => $data);
}
$res = $GLOBALS['db']->Execute('SELECT readaccess, writeaccess FROM privs WHERE name = ? AND type = ?', array($page, $type));
if($res->RecordCount())
$this->pages[$page . '.' . $type]['access'] = array($res->fields[0], $res->fields[1]);
}
return $this->pages[$page . '.' . $type];
}
function Add($name, $type, $template) {
if($this->Exists($name, $type)) return false;
$GLOBALS['db']->Execute('INSERT INTO pages (name, template, data, type) VALUES (?, ?, "", ?)',
array($name, $template, $type));
return ($GLOBALS['db']->Affected_Rows() > 0);
}
function Edit($page, $type, $data) {
$string = '';
foreach($data as $key => $val)
$string .= urlencode($key) . '=' . urlencode($val) . '&';
$res = $GLOBALS['db']->Execute('SELECT id FROM pages WHERE name = ? AND type = ?',
array($page, $type));
if($res->RecordCount()) {
$GLOBALS['db']->Execute('UPDATE pages SET data = ? WHERE name = ? AND type = ?',
array(substr($string, 0, -1), $page, $type));
return true;
}
$pagedata = $this->GetPageData($page, $type);
$GLOBALS['db']->Execute('INSERT INTO privs (name, type, readaccess, writeaccess) VALUES (?, ?, ?, ?)',
array($page, $type, $pagedata['access'][0], $pagedata['access'][1]));
$GLOBALS['db']->Execute('INSERT INTO pages (name, template, data, type) VALUES (?, ?, ?, ?)',
array($page, $pagedata['template'], $string, $type));
return ($GLOBALS['db']->Affected_Rows() > 0);
}
function Rename($page, $type, $new_name) {
if($this->Exists($new_name, $type)) return false;
$GLOBALS['db']->Execute('UPDATE privs SET name = ? WHERE name = ? AND type = ?', array($new_name, $page, $type));
$GLOBALS['db']->Execute('UPDATE pages SET name = ? WHERE name = ? AND type = ?', array($new_name, $page, $type));
return ($GLOBALS['db']->Affected_Rows() > 0);
}
function Copy($page, $type, $new_name) {
if($this->Exists($new_name, $type)) return false;
if(!$this->Exists($page, $type)) return false;
$pagedata = $this->GetPageData($page, $type);
$string = '';
foreach($pagedata['data'] as $key => $val)
$string .= urlencode($key) . '=' . urlencode($val) . '&';
$GLOBALS['db']->Execute('INSERT INTO pages (name, template, data, type) VALUES (?, ?, ?, ?)',
array($new_name, $pagedata['template'], $string, $type));
return ($GLOBALS['db']->Affected_Rows() > 0);
}
function Delete($page, $type) {
$GLOBALS['db']->Execute('DELETE FROM privs WHERE name = ? AND type = ?', array($page, $type));
$GLOBALS['db']->Execute('DELETE FROM pages WHERE name = ? AND type = ?', array($page, $type));
return ($GLOBALS['db']->Affected_Rows() > 0);
}
function GetList() {
$res = $GLOBALS['db']->Execute('SELECT name, type FROM pages');
$pages = array();
while($row = $res->FetchRow())
$pages[$row[0] . '.' . $row[1]] = null;
$pages = array_keys(array_merge($pages, $this->pages, $GLOBALS['modules']->pages));
sort($pages);
return $pages;
}
function GetAccess($page, $type) {
$res = $GLOBALS['db']->Execute('SELECT readaccess, writeaccess FROM privs WHERE name = ? AND type = ?', array($page, $type));
if($res->RecordCount())
return array($res->fields[0], $res->fields[1]);
$pagedata = $this->GetPageData($page, $type);
return $pagedata['access'];
}
function SetAccess($page, $type, $access) {
$res = $GLOBALS['db']->Execute('SELECT id FROM privs WHERE name = ? AND type = ?', array($page, $type));
if($res->RecordCount()) {
$GLOBALS['db']->Execute('UPDATE privs SET readaccess = ?, writeaccess = ? WHERE name = ? AND type = ?', array($access[0], $access[1], $page, $type));
return ($GLOBALS['db']->Affected_Rows() > 0);
}
$GLOBALS['db']->Execute('INSERT INTO privs (name, type, readaccess, writeaccess) VALUES (?, ?, ?, ?)', array($page, $type, $access[0], $access[1]));
return ($GLOBALS['db']->Affected_Rows() > 0);
}
}
$GLOBALS['pages'] = new Pages;
?>