function &open($info) { static $connections = array(); $name = serialize($info); if (isset($connections[$name])) { return $connections[$name]; } if (!isset($info['driver'])) { return Error::pitch(new FAError("No database driver specified.", __FILE__, __LINE__)); } $driver = DBA_BASE_DIR . "/drivers/{$info['driver']}.php"; $class = "{$info['driver']}Connection"; if (!is_readable($driver)) { return Error::pitch(new FAError("Driver does not exist.", __FILE__, __LINE__)); } require_once $driver; if (!class_exists($class)) { return Error::pitch(new FAError("Driver class does not exist or is misnamed.", __FILE__, __LINE__)); } $dba =& new $class(); if (!is_a($dba, 'FADBConnection')) { return Error::pitch(new FAError("Driver class does not extend FADBConnection.", __FILE__, __LINE__)); } // Error is thrown in the constructor (hopefully) $dba->connect($info); $connections[$name] = $dba; return $dba; }
/** * Get the user's ID by their email * @param email The email used to get the user id * @return either an exception or the user id */ function GetIdByEmail($email) { $email = $this->dba->Quote($email); if (!($result = $this->dba->GetValue("SELECT id FROM " . USERS . " WHERE email = '{$email}'"))) { return Error::pitch(new FAError("Could not get the User ID.", __FILE__, __LINE__)); } return $result; }
function parse($data, $is_final = TRUE) { if ($this->parser == NULL) { $this->parser = xml_parser_create(); xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); xml_set_object($this->parser, $this); xml_set_character_data_handler($this->parser, '_charData'); xml_set_default_handler($this->parser, '_charData'); xml_set_element_handler($this->parser, '_openTag', '_closeTag'); } $data = preg_replace('/&([a-zA-Z0-9#]+);/', '{ent{$1}}', $data); if (!($ret = xml_parse($this->parser, $data, $is_final))) { $code = xml_get_error_code($this->parser); Error::pitch(new FAError(xml_error_string($code), __FILE__, __LINE__)); } if ($is_final) { xml_parser_free($this->parser); $this->parser = NULL; $this->finalize(); } return $ret; }
function connect($info) { if (!isset($info['database']) || !isset($info['directory'])) { $this->valid = FALSE; return Error::pitch(new FAError("Missing required connection information.", __FILE__, __LINE__)); } if (!function_exists('sqlite_open')) { $this->valid = FALSE; return Error::pitch(new FAError("Please make sure that SQLite is properly installed.", '--', '--')); } $link = @sqlite_open($info['directory'] . '/' . $info['database'], 0666); if (!is_resource($link)) { $this->valid = FALSE; return Error::pitch(new FAError("Unable to connect to the database.", __FILE__, __LINE__)); } /*if (!mysql_select_db($info['database'])) { $error = sqlite_error_string(sqlite_last_error($link)); sqlite_close($link); $this->valid = FALSE; return Error::pitch(new FAError("Unable to select database: $error", __FILE__, __LINE__)); }*/ $this->link = $link; return TRUE; }
function insertNode($request, $category_id = FALSE, $forum_id = FALSE, $group_id = FALSE, $user_id = FALSE) { /** * Error checking on request fields */ if (!isset($request['parent_id'])) { return Error::pitch(new FAError('L_INVALIDMAPID', __FILE__, __LINE__)); } if (!isset($request['varname'])) { return Error::pitch(new FAError('L_MAPSNEEDVARNAME', __FILE__, __LINE__)); } if (!isset($request['name'])) { return Error::pitch(new FAError('L_MAPSNEEDNAME', __FILE__, __LINE__)); } /** * Start building info for the queries */ /* Get the last node to the furthest right in the tree */ $last_node = $this->dba->GetRow("SELECT * FROM " . MAPS . " WHERE row_level = 1 ORDER BY row_right DESC LIMIT 1"); $level = 1; /* Is this a top level node? */ if (intval($request['parent_id']) == 0) { $left = $last_node['row_right'] + 1; $level = 1; $parent = array('category_id' => intval($category_id), 'forum_id' => intval($forum_id), 'group_id' => intval($group_id), 'user_id' => intval($user_id)); $parent_id = 0; /* If we are actually dealing with a parent node */ } else { if (intval($request['parent_id']) > 0) { /* Get the parent node */ $parent = $this->dba->GetRow("SELECT * FROM " . MAPS . " WHERE id = " . intval($request['parent_id'])); /* Check if the parent node exists */ if (!is_array($parent) || empty($parent)) { return Error::pitch(new FAError('L_INVALIDMAPID', __FILE__, __LINE__)); } /* Find out how many nodes are on the current level */ $num_on_level = $this->getNumOnLevel($parent['row_left'], $parent['row_right'], $parent['row_level'] + 1); /* If there are more than 1 nodes on the current level */ if ($num_on_level > 0) { $left = $parent['row_right']; } else { $left = $parent['row_left'] + 1; } $parent_id = $parent['id']; /* Should we need to reset some of the $parent values? */ $parent['category_id'] = !$category_id ? $parent['category_id'] : intval($category_id); $parent['forum_id'] = !$forum_id ? $parent['forum_id'] : intval($forum_id); $parent['group_id'] = !$group_id ? $parent['group_id'] : intval($group_id); $parent['user_id'] = !$user_id ? $parent['user_id'] : intval($user_id); /* Set this nodes level */ $level = $parent['row_level'] + 1; } else { return Error::pitch(new FAError('L_INVALIDMAPID', __FILE__, __LINE__)); } } $right = $left + 1; /** * Build the queries */ /* Prepare the queries */ $update_a =& $this->dba->prepareStatement("UPDATE " . MAPS . " SET row_right = row_right+2 WHERE row_left < ? AND row_right >= ?"); $update_b =& $this->dba->prepareStatement("UPDATE " . MAPS . " SET row_left = row_left+2, row_right=row_right+2 WHERE row_left >= ?"); $insert =& $this->dba->prepareStatement("INSERT INTO " . MAPS . " (row_left,row_right,row_level,name,varname,category_id,forum_id,user_id,group_id,can_view,can_add,can_edit,can_del,inherit,value,parent_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); /* Set the insert variables needed */ $update_a->setInt(1, $left); $update_a->setInt(2, $left); $update_b->setInt(1, $left); /* Set the inserts for adding the actual node */ $insert->setInt(1, $left); $insert->setInt(2, $right); $insert->setInt(3, $level); $insert->setString(4, $request['name']); $insert->setString(5, $request['varname']); $insert->setInt(6, $parent['category_id']); $insert->setInt(7, $parent['forum_id']); $insert->setInt(8, $parent['user_id']); $insert->setInt(9, $parent['group_id']); $insert->setInt(10, @$request['can_view']); $insert->setInt(11, @$request['can_add']); $insert->setInt(12, @$request['can_edit']); $insert->setInt(13, @$request['can_del']); $insert->setInt(14, @$request['inherit']); $insert->setString(15, @$request['value']); $insert->setInt(16, $parent_id); /** * Execute the queries */ /* Execute the queries */ $update_a->executeUpdate(); $update_b->executeUpdate(); $insert->executeUpdate(); if (!@touch(CACHE_FILE, time() - 86460)) { @unlink(CACHE_FILE); } }
function connect($info) { if (!isset($info['server']) || !isset($info['user']) || !isset($info['pass']) || !isset($info['database'])) { $this->valid = FALSE; return Error::pitch(new FAError("Missing required connection information.", __FILE__, __LINE__)); } if (!function_exists('mysqli_connect')) { $this->valid = FALSE; return Error::pitch(new FAError("Please make sure that MySQL is properly installed.", '--', '--')); } $link = @mysqli_connect($info['server'], $info['user'], $info['pass'], $info['database']); if (!$link) { $this->valid = FALSE; return Error::pitch(new FAError("Unable to connect to the database: " . mysqli_errno($this->link), __FILE__, __LINE__)); } $this->link = $link; return TRUE; }