/**
  *
  */
 function exists($access_rule)
 {
     $result = xanth_db_query("SELECT * FROM access_rule WHERE name = '%s'", $access_rule);
     if ($row = xanth_db_fetch_object($result)) {
         return TRUE;
     }
     return FALSE;
 }
 /**
  *
  */
 function get($name)
 {
     $result = xanth_db_query("SELECT * FROM entry_type WHERE name = '%s'", $name);
     if ($row = xanth_db_fetch_object($result)) {
         return new xEntryType($row->name, $row->view_mode_id);
     }
     return NULL;
 }
function xanth_db_install_settings()
{
    //settings
    xanth_db_query("\r\n\t\tCREATE TABLE settings (\r\n\t\tsite_name VARCHAR(256) NOT NULL,\r\n\t\tsite_description VARCHAR(512) NOT NULL,\r\n\t\tsite_keywords VARCHAR(128) NOT NULL,\r\n\t\tsite_theme VARCHAR(32) NOT NULL\r\n\t\t)TYPE=InnoDB");
    xanth_db_query("INSERT INTO settings (site_name,site_description,site_keywords,site_theme) VALUES ('','','','')");
    $access = new xAccessRule('manage settings', 'Settings');
    $access->insert();
}
 /**
  *
  */
 function find_all()
 {
     $elems = array();
     $result = xanth_db_query("SELECT * FROM visual_element");
     while ($row = xanth_db_fetch_object($result)) {
         $elems[] = new xVisualElement($row->name);
     }
     return $elems;
 }
 /**
  *
  */
 function get($area_name)
 {
     $elems = array();
     $result = xanth_db_query("SELECT * FROM theme_area WHERE name = '%s'", $area_name);
     if ($row = xanth_db_fetch_object($result)) {
         return new xThemeArea($row->name, $row->view_mode);
     }
     return NULL;
 }
 /**
  * Return a new xContentFormat object or NULL
  */
 function load($name)
 {
     $result = xanth_db_query("SELECT * FROM content_format WHERE name = '%s'", $name);
     if ($row = xanth_db_fetch_object($result)) {
         $format = new xContentFormat($row->name, $row->description);
         return $format;
     }
     return NULL;
 }
/**
* @file Installation procedures for core
*/
function xanth_db_install_core()
{
    //log
    xanth_db_query("\r\n\t\tCREATE TABLE xanth_log (\r\n\t\tlevel MEDIUMINT NOT NULL,\r\n\t\tcomponent VARCHAR(32) NOT NULL,\r\n\t\tmessage TEXT NOT NULL,\r\n\t\tfilename  VARCHAR(255) NOT NULL,\r\n\t\tline MEDIUMINT NOT NULL,\r\n\t\ttimestamp TIMESTAMP\r\n\t\t)TYPE=InnoDB");
    //sessions
    xanth_db_query("\r\n\t\tCREATE TABLE sessions (\r\n\t\tsession_id VARCHAR(32) NOT NULL,\r\n\t\tsession_data TEXT NOT NULL,\r\n\t\tsession_timestamp TIMESTAMP NOT NULL,\r\n\t\tPRIMARY KEY  (session_id)\r\n\t\t)TYPE=InnoDB");
    //Modules
    xanth_db_query("\r\n\t\tCREATE TABLE modules (\r\n\t\tname VARCHAR(32) NOT NULL,\r\n\t\tpath VARCHAR(255) NOT NULL,\r\n\t\tenabled TINYINT NOT NULL,\r\n\t\tPRIMARY KEY  (name)\r\n\t\t)TYPE=InnoDB");
}
function xanth_db_install_category()
{
    //category
    xanth_db_query("\r\n\t\tCREATE TABLE category (\r\n\t\tid INT UNSIGNED NOT NULL AUTO_INCREMENT,\r\n\t\ttitle VARCHAR(255) NOT NULL,\r\n\t\tdescription TEXT NOT NULL,\r\n\t\tview_mode_id INT UNSIGNED,\r\n\t\tparent_id INT UNSIGNED,\r\n\t\tPRIMARY KEY (id),\r\n\t\tUNIQUE(title),\r\n\t\tINDEX(parent_id),\r\n\t\tINDEX(view_mode_id),\r\n\t\tFOREIGN KEY(parent_id) REFERENCES category(id) ON DELETE CASCADE,\r\n\t\tFOREIGN KEY(view_mode_id) REFERENCES view_mode(id) ON DELETE SET NULL\r\n\t\t)TYPE=InnoDB");
    //category to entry type
    xanth_db_query("\r\n\t\tCREATE TABLE category_to_entry_type (\r\n\t\tcat_id INT UNSIGNED NOT NULL,\r\n\t\tentry_type VARCHAR(32) NOT NULL,\r\n\t\tUNIQUE(cat_id,entry_type),\r\n\t\tINDEX(cat_id),\r\n\t\tINDEX(entry_type),\r\n\t\tFOREIGN KEY(cat_id) REFERENCES category(id) ON DELETE CASCADE,\r\n\t\tFOREIGN KEY(entry_type) REFERENCES entry_type(name) ON DELETE CASCADE\r\n\t\t)TYPE=InnoDB");
    //install some access rule
    $access = new xAccessRule('manage category', 'Category');
    $access->insert();
}
function xanth_db_install_entry_type()
{
    //entry type
    xanth_db_query("\r\n\t\tCREATE TABLE entry_type (\r\n\t\tname VARCHAR(32) NOT NULL,\r\n\t\tview_mode_id INT UNSIGNED,\r\n\t\tPRIMARY KEY (name),\r\n\t\tINDEX(view_mode_id),\r\n\t\tFOREIGN KEY (view_mode_id) REFERENCES view_mode(id) ON DELETE SET NULL\r\n\t\t)TYPE=InnoDB");
    $access = new xAccessRule('manage entry type', 'Entry Type');
    $access->insert();
    //some default tipes
    $type = new xEntryType('StaticEntry');
    $type->insert();
}
function xanth_db_install_view_mode()
{
    //visual element
    xanth_db_query("\r\n\t\tCREATE TABLE visual_element (\r\n\t\tname VARCHAR(32) NOT NULL,\r\n\t\tPRIMARY KEY (name)\r\n\t\t)TYPE=InnoDB");
    //display mode
    xanth_db_query("\r\n\t\tCREATE TABLE view_mode (\r\n\t\tid INT UNSIGNED AUTO_INCREMENT,\r\n\t\tname VARCHAR(32) NOT NULL,\r\n\t\trelative_visual_element VARCHAR(32) NOT NULL,\r\n\t\tdefault_for_element TINYINT UNSIGNED NOT NULL,\r\n\t\tdisplay_procedure TEXT NOT NULL,\r\n\t\tPRIMARY KEY (id),\r\n\t\tINDEX(relative_visual_element),\r\n\t\tFOREIGN KEY (relative_visual_element) REFERENCES visual_element(name) ON DELETE CASCADE\r\n\t\t)TYPE=InnoDB");
    //access rule
    $access = new xAccessRule('manage view_mode', 'View Mode');
    $access->insert();
}
function xanth_db_install_user()
{
    //Users
    xanth_db_query("\r\n\t\tCREATE TABLE user (\r\n\t\tid INT UNSIGNED AUTO_INCREMENT NOT NULL,\r\n\t\tusername VARCHAR(32) NOT NULL,\r\n\t\tpassword VARCHAR(64) NOT NULL,\r\n\t\temail VARCHAR(128) NOT NULL,\r\n\t\tcookie_token VARCHAR(64) NOT NULL,\r\n\t\tPRIMARY KEY (id),\r\n\t\tUNIQUE(username),\r\n\t\tINDEX(username),\r\n\t\tUNIQUE(email)\r\n\t\t)TYPE=InnoDB");
    //User to role
    xanth_db_query("\r\n\t\tCREATE TABLE user_to_role (\r\n\t\tuserid INT UNSIGNED NOT NULL,\r\n\t\troleName VARCHAR(32) NOT NULL,\r\n\t\tUNIQUE(userid,roleName),\r\n\t\tINDEX(userid),\r\n\t\tINDEX(roleName),\r\n\t\tFOREIGN KEY (userid) REFERENCES user(id) ON DELETE CASCADE,\r\n\t\tFOREIGN KEY (roleName) REFERENCES role(name) ON DELETE CASCADE\r\n\t\t)TYPE=InnoDB");
    $user = new xUser('', 'admin', '*****@*****.**');
    $user->insert('pass');
    $user->add_in_role('administrator');
    //create a box for login
    $login_box = new xBox('login_box', 'Login', NULL, 'Full Html', 0, 'sidebar left');
    $login_box->insert();
}
function xanth_db_install_content_format()
{
    //content format
    xanth_db_query("\r\n\t\tCREATE TABLE content_format (\r\n\t\tname VARCHAR(64) NOT NULL,\r\n\t\tdescription VARCHAR(256) NOT NULL,\r\n\t\tPRIMARY KEY(name)\r\n\t\t)TYPE=InnoDB");
    $cf = new xContentFormat('Php source', 'Php scripts are allowed and executed.');
    $cf->insert();
    $cf = new xContentFormat('Full Html', 'All html tags are allowed.');
    $cf->insert();
    $cf = new xContentFormat('BBCode', 'Enable the use of a slightly modified version of BBCode tags. 
		Also converts all special html chars in html entities and line breaks in br');
    $cf->insert();
    $cf = new xContentFormat('Filtered text', 'Converts all special html chars in html entities and line breaks in br.');
    $cf->insert();
}
function xanth_db_install_theme()
{
    //theme
    xanth_db_query("\r\n\t\tCREATE TABLE theme (\r\n\t\tname VARCHAR(32) NOT NULL,\r\n\t\tPRIMARY KEY (name)\r\n\t\t)TYPE=InnoDB");
    //theme to elements
    xanth_db_query("\r\n\t\tCREATE TABLE theme_to_elements (\r\n\t\ttheme_name VARCHAR(32) NOT NULL,\r\n\t\tvisual_element VARCHAR(32) NOT NULL,\r\n\t\tview_mode INT UNSIGNED NOT NULL,\r\n\t\tUNIQUE (theme_name,visual_element),\r\n\t\tINDEX(theme_name),INDEX(visual_element),INDEX(view_mode),\r\n\t\tFOREIGN KEY (theme_name) REFERENCES theme(name) ON DELETE CASCADE,\r\n\t\tFOREIGN KEY (visual_element) REFERENCES visual_element(name) ON DELETE CASCADE,\r\n\t\tFOREIGN KEY (view_mode) REFERENCES view_mode(id) ON DELETE CASCADE\r\n\t\t)TYPE=InnoDB");
    //theme to elements
    xanth_db_query("\r\n\t\tCREATE TABLE theme_area (\r\n\t\tname VARCHAR(32) NOT NULL,\r\n\t\tview_mode INT UNSIGNED,\r\n\t\tPRIMARY KEY (name),\r\n\t\tINDEX(view_mode),\r\n\t\tFOREIGN KEY (view_mode) REFERENCES view_mode(id) ON DELETE SET NULL\r\n\t\t)TYPE=InnoDB");
    //register new visual element
    $element = new xVisualElement('area');
    $element->insert();
    //...and the default view mode
    $proc = '
$output = \'\';
foreach($boxes as $box)
{
	$output .= "<div class=\\"box\\">$box</div>";
}
return $output;
';
    $view = new xViewMode(0, 'Default area view', 'area', TRUE, $proc);
    $view->insert();
    //content area view mode
    $proc = '
		return $page_content;
	';
    $content_view = new xViewMode(0, 'Content area view', 'area', FALSE, $proc);
    $content_view->insert();
    //footer area view mode
    $proc = '
		return \'Page created with \'. xPageElement::get_db_query_count() .\' queries in \'.xPageElement::get_execution_time().\' seconds\';
	';
    $foot_view = new xViewMode(0, 'Footer area view', 'area', FALSE, $proc);
    $foot_view->insert();
    //default theme areas
    $area = new xThemeArea('sidebar left');
    $area->insert();
    $area = new xThemeArea('content', $content_view->id);
    $area->insert();
    $area = new xThemeArea('footer', $foot_view->id);
    $area->insert();
    //access rule
    $access = new xAccessRule('manage theme', 'Theme');
    $access->insert();
}
function xanth_db_install_entry()
{
    //entry
    xanth_db_query("\r\n\t\tCREATE TABLE entry (\r\n\t\tid INT UNSIGNED NOT NULL AUTO_INCREMENT,\r\n\t\ttitle VARCHAR(256) NOT NULL,\r\n\t\ttype VARCHAR(32) NOT NULL,\r\n\t\tauthor VARCHAR(64) NOT NULL,\r\n\t\tcontent TEXT NOT NULL,\r\n\t\tcontent_format VARCHAR(64) NOT NULL,\r\n\t\tpublished TINYINT NOT NULL,\r\n\t\tdescription VARCHAR(512) NOT NULL,\r\n\t\tkeywords VARCHAR(128) NOT NULL,\r\n\t\tcreation_time TIMESTAMP NOT NULL,\r\n\t\tPRIMARY KEY (id),\r\n\t\tINDEX(type),\r\n\t\tINDEX(content_format),\r\n\t\tFOREIGN KEY(content_format) REFERENCES content_format(name) ON DELETE RESTRICT,\r\n\t\tFOREIGN KEY(type) REFERENCES entry_type(name) ON DELETE RESTRICT\r\n\t\t)TYPE=InnoDB");
    //category to entry
    xanth_db_query("\r\n\t\tCREATE TABLE categorytoentry (\r\n\t\tentryId INT UNSIGNED NOT NULL,\r\n\t\tcatId INT UNSIGNED NOT NULL,\r\n\t\tUNIQUE(entryId,catId),\r\n\t\tINDEX(entryId),\r\n\t\tINDEX(catId),\r\n\t\tFOREIGN KEY(entryId) REFERENCES entry(id) ON DELETE CASCADE,\r\n\t\tFOREIGN KEY(catId) REFERENCES category(id) ON DELETE CASCADE\r\n\t\t)TYPE=InnoDB");
    //install a new visual element
    $element = new xVisualElement('entry');
    $element->insert();
    //...and the default view mode
    $proc = '
return \'<div class="title">\'.$this->title.\'</div><div class="body">\'.$this->content.\'</div>\';
';
    $view = new xViewMode(0, 'Default entry view', 'entry', TRUE, $proc);
    $view->insert();
    //install some access rule
    $access = new xAccessRule('view entry', 'Entry');
    $access->insert();
    $access = new xAccessRule('create entry', 'Entry');
    $access->insert();
    $access = new xAccessRule('edit entry', 'Entry');
    $access->insert();
}
function xanth_db_install_box()
{
    //box
    xanth_db_query("\r\n\t\tCREATE TABLE box (\r\n\t\tname VARCHAR(64) NOT NULL,\r\n\t\ttitle VARCHAR(255),\r\n\t\tcontent TEXT,\r\n\t\tcontent_format VARCHAR(64) NOT NULL,\r\n\t\tarea VARCHAR(32),\r\n\t\tis_user_defined TINYINT NOT NULL,\r\n\t\tPRIMARY KEY(name),\r\n\t\tINDEX(content_format),\r\n\t\tFOREIGN KEY(content_format) REFERENCES content_format(name)\r\n\t\t)TYPE=InnoDB");
    //install a new visual element
    $element = new xVisualElement('box');
    $element->insert();
    //...and the default view mode
    $proc = '
return \'<strong>\' . $this->title .\'</strong> <br />\' . $this->content;
';
    $view = new xViewMode(0, 'Default box view', 'box', TRUE, $proc);
    $view->insert();
    //another view mode for box
    $proc = '
return $this->content;
';
    $view = new xViewMode(0, 'Box view without title', 'box', FALSE, $proc);
    $view->insert();
    //install some access rule
    $access = new xAccessRule('manage box', 'Box');
    $access->insert();
}
 /**
  *
  */
 function get_view_mode_procedure($element)
 {
     if (isset($this->themed_elements[$element])) {
         $result = xanth_db_query("SELECT * FROM view_mode WHERE id = %d", $this->themed_elements[$element]);
         if ($row = xanth_db_fetch_object($result)) {
             return $row->display_procedure;
         }
         return NULL;
     } else {
         //return the default view mode for element
         $result = xanth_db_query("SELECT * FROM view_mode WHERE relative_visual_element = '%s' AND default_for_element = %d", $element, TRUE);
         if ($row = xanth_db_fetch_object($result)) {
             return $row->display_procedure;
         }
         xanth_log(LOG_LEVEL_FATAL_ERROR, 'Default view mode for visual element ' . $element . ' not found', __CLASS__ . '::' . __FUNCTION__);
         return NULL;
     }
 }
/**
* Return last inserted id or NULL on error
*/
function xanth_db_get_last_id()
{
    $result = xanth_db_query('SELECT LAST_INSERT_ID() as id');
    if ($row = xanth_db_fetch_array($result)) {
        return $row['id'];
    }
    return NULL;
}
 /**
  *
  */
 function disable()
 {
     if ($this->exists()) {
         $result = xanth_db_query("SELECT enabled FROM modules WHERE name = '%s'", $this->name);
         if ($row = xanth_db_fetch_array($result)) {
             if ($row['enabled']) {
                 xanth_db_query("UPDATE modules SET enabled = 0 WHERE name = '%s'", $this->name);
             }
         }
         return true;
     }
     return false;
 }
 /**
  * List all box in an area.
  */
 function find($area = '')
 {
     $boxes = array();
     if (empty($area)) {
         $result = xanth_db_query("SELECT * FROM box");
     } else {
         $result = xanth_db_query("SELECT * FROM box WHERE area = '%s'", $area);
     }
     while ($row = xanth_db_fetch_array($result)) {
         $current_box = new xBox($row['name'], $row['title'], $row['content'], $row['content_format'], $row['is_user_defined'], $row['area']);
         if (!$current_box->user_defined) {
             //retrieve built-in box content
             $current_box->content = xanth_invoke_mono_hook(MONO_HOOK_CREATE_BOX_CONTENT, $current_box->name);
         } else {
             $content_format = new xContentFormat($row['content_format'], '');
             $current_box->content = $content_format->apply_to($current_box->content);
         }
         $boxes[] = $current_box;
     }
     return $boxes;
 }
 /**
  *
  */
 function has_access_rule($access_rule)
 {
     $result = xanth_db_query("SELECT * FROM role_access_rule WHERE roleName = '%s' AND access_rule = '%s'", $this->name, $access_rule);
     if (xanth_db_fetch_object($result)) {
         return TRUE;
     }
     return FALSE;
 }
 /**
  *
  */
 function find_all()
 {
     xanth_db_start_transaction();
     $entries = array();
     $result = xanth_db_query("SELECT * FROM entry");
     for ($i = 0; $row = xanth_db_fetch_object($result); $i++) {
         $entries[$i] = new xEntry($row->id, $row->title, $row->type, $row->author, $row->content, $row->content_format, $row->published, $row->description, $row->keywords, array(), xanth_db_decode_timestamp($row->creation_time));
         $result2 = xanth_db_query("SELECT * FROM categorytoentry,category WHERE entryId = %d AND category.id = catId", $row->id);
         $categories = array();
         while ($row = xanth_db_fetch_object($result2)) {
             $categories[] = new xCategory($row->id, $row->title, $row->parent_id);
         }
         $entry[$i]->categories = $categories;
     }
     xanth_db_commit();
     return $entries;
 }
function on_session_gc($max_lifetime)
{
    xanth_db_query("DELETE FROM sessions WHERE UNIX_TIMESTAMP(session_timestamp) < UNIX_TIMESTAMP(%d)", time() - $max_lifetime);
}
 /**
  *
  */
 function load()
 {
     global $xanth_settings;
     $result = xanth_db_query("SELECT * FROM settings");
     $xanth_settings = xanth_db_fetch_array($result);
 }
 /**
  *
  */
 function find_by_entry_type($entry_type)
 {
     $categories = array();
     $result = xanth_db_query("SELECT * FROM category_to_entry_type WHERE entry_type = '%s'", $entry_type);
     while ($row = xanth_db_fetch_object($result)) {
         $categories[] = get($row->cat_id);
     }
     return $categories;
 }
 /**
  *
  */
 function get_default_for_element($visual_element)
 {
     $modes = array();
     $result = xanth_db_query("SELECT * FROM view_mode WHERE relative_visual_element = '%s' AND default_for_element = %d", $visual_element, TRUE);
     if ($row = xanth_db_fetch_object($result)) {
         return new xViewMode($row->id, $row->name, $row->relative_visual_element, $row->default_for_element, $row->display_procedure);
     }
     return NULL;
 }
 /**
  *
  */
 function _update_persistent_login($username)
 {
     //generate a new login_token
     $cookie_token = md5(uniqid(rand(), true));
     xanth_db_query("UPDATE user SET cookie_token = '%s' WHERE username = '******'", $cookie_token, $username);
     $cookie = serialize(array($username, $cookie_token));
     setcookie('xanth_login', $cookie, time() + 31104000);
 }