Exists($page, $type)) return array('title' => $page, 'content' => ErrorMessage('PageNotFound', array('page' => $page))); if($type) $res = $GLOBALS['db']->Execute('SELECT id, name, type, template, data FROM pages WHERE name = ? AND type = ?', array($page, $type)); else $res = $GLOBALS['db']->Execute('SELECT id, name, type, template, data FROM pages WHERE id = ?', $page); if(!$this->HasAccess($page, $type)) return array('title' => $res->fields[1], 'content' => ErrorMessage('Forbidden', array('page' => $res->fields[1]))); $data = null; parse_str($res->fields[4], $data); $data = array_map('Unquote', $data); if($extra) $data = array_merge($data, $extra); $data['_id'] = $res->fields[0]; $data['_page'] = $res->fields[1]; $data['_type'] = $res->fields[2]; return $GLOBALS['templates'][$res->fields[3]]->Get($data); } function GetEditor($page, $type = null) { if(!$this->Exists($page, $type)) return array('title' => $page, 'content' => ErrorMessage('PageNotFound', array('page' => $page))); if($type) $res = $GLOBALS['db']->Execute('SELECT id, name, type, template, data FROM pages WHERE name = ? AND type = ?', array($page, $type)); else $res = $GLOBALS['db']->Execute('SELECT id, name, type, template, data FROM pages WHERE id = ?', $page); if(!$GLOBALS['user']->IsAdmin()) return array('title' => $res->fields[1], 'content' => ErrorMessage('Forbidden', array('page' => $res->fields[1]))); parse_str($res->fields[4], $data = null); $data = array_map('Unquote', $data); $data['_id'] = $res->fields[0]; $data['_page'] = $res->fields[1]; $data['_type'] = $res->fields[2]; return $this->Get($res->fields[3], 'e', array('_data' => $data)); } function GetName($id) { $res = $GLOBALS['db']->Execute('SELECT name FROM pages WHERE id = ?', $id); return $res->fields[0]; } function GetType($id) { $res = $GLOBALS['db']->Execute('SELECT type FROM pages WHERE id = ?', $id); return $res->fields[0]; } function Exists($page, $type = null) { if($type) $res = $GLOBALS['db']->Execute('SELECT id FROM pages WHERE name = ? AND type = ?', array($page, $type)); else $res = $GLOBALS['db']->Execute('SELECT id FROM pages WHERE id = ?', $page); return ($res->RecordCount() > 0); } function HasAccess($page, $type = null) { $gid = $GLOBALS['user']->gid; if($type) $res = $GLOBALS['db']->Execute('SELECT access FROM pages WHERE name = ? AND type = ?', array($page, $type)); else $res = $GLOBALS['db']->Execute('SELECT access FROM pages WHERE id = ?', $page); if(!$res->RecordCount()) return false; return ($GLOBALS['user']->IsAdmin() || (ord($res->fields[0][$gid/8]) & (1 << ($gid%8))) != 0); } function Add($name, $template, $type) { if($this->Exists($name, $type)) return 0; $GLOBALS['db']->Execute('INSERT INTO pages (name, template, access, data, type) VALUES (?, ?, 0, "", ?)', array($name, $templates, $type)); return $GLOBALS['db']->Insert_ID(); } function Edit($page, $data, $type = null) { $string = ''; foreach($data as $key => $val) $string .= urlencode($key) . '=' . urlencode($val) . '&'; if($type) $GLOBALS['db']->Execute('UPDATE pages SET data = ? WHERE name = ? AND type = ?', array(substr($string, 0, -1), $page, $type)); else $GLOBALS['db']->Execute('UPDATE pages SET data = ? WHERE id = ?', array(substr($string, 0, -1), $page)); return ($GLOBALS['db']->Affected_Rows() > 0); } function Rename($page, $new_name, $type = null) { if($type) { if($this->Exists($new_name, $type)) return; $GLOBALS['db']->Execute('UPDATE pages SET name = ? WHERE name = ? AND type = ?', array($new_name, $page, $type)); } else { if($this->Exists($new_name, $this->GetType($type))) return; $GLOBALS['db']->Execute('UPDATE pages SET name = ? WHERE id = ?', array($new_name, $page)); } return ($GLOBALS['db']->Affected_Rows() > 0); } function Copy($page, $new_name, $type = null) { if($type) { if($this->Exists($new_name, $type)) return 0; $res = $GLOBALS['db']->Execute('SELECT * FROM pages WHERE name = ? AND type = ?', array($page, $type)); } else { if($this->Exists($new_name, $this->GetType($type))) return 0; $res = $GLOBALS['db']->Execute('SELECT template, data, type FROM pages WHERE id = ?', $page); } $GLOBALS['db']->Execute('INSERT INTO pages (name, template, access, data, type) VALUES (?, ?, 0, ?, ?)', array($new_name, $res->fields[0], $res->fields[1], $res->fields[2])); return $GLOBALS['db']->Insert_ID(); } function Delete($page, $type = null) { if($type) $GLOBALS['db']->Execute('DELETE FROM pages WHERE name = ? AND type = ?', array($page, $type)); else $GLOBALS['db']->Execute('DELETE FROM pages WHERE id = ?', $page); return ($GLOBALS['db']->Affected_Rows() > 0); } } $GLOBALS['pages'] = new Pages; ?>