function export()
{
    header('Content-Type: text/plain');
    if (empty($_REQUEST['request']) || $_REQUEST['request'] != 'round' && empty($_REQUEST['round_id'])) {
        exit('Empty query');
    }
    $request = request_variable('request');
    $round_id = abs((int) request_variable('round_id'));
    if (!in_array($request, array('kingdom', 'player', 'round'))) {
        exit('Invalid request: round, kingdom, or player');
    }
    if ($request != 'round' && empty($round_id)) {
        exit('Invalid round id');
    }
    $sql = new SQL_Generator();
    if ($request == 'round') {
        $sql->select(array(array('rounds', 'round_id'), array('rounds', 'name'), array('rounds', 'starttime'), array('rounds', 'stoptime')));
        $sql->where(array('rounds', 'public', 1));
    } else {
        $sql->select(array($request . 's', $request . '_id'));
        if ($request == 'player') {
            $sql->select(array($request . 's', 'kingdom_id'));
        }
        $sql->select(array(array($request . 's', 'name'), array($request . 's', 'score'), array($request . 's', 'score_peak')));
        $sql->where(array($request . 's', 'round_id', $round_id));
    }
    $sql->orderby(array($request . 's', $request . '_id', 'asc'));
    $db_result = $sql->execute();
    while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
        $output = implode(',', $db_row) . ",\n";
        if (substr_count($output, ',') > count($db_row)) {
            $output = '';
            $multiple = false;
            foreach ($db_row as $value) {
                if ($multiple) {
                    $output .= ',';
                } else {
                    $multiple = true;
                }
                $output .= str_replace(',', '', $value);
            }
            $output .= ",\n";
        }
        echo $output;
    }
    exit;
}
 function get_timestamp(&$tpl_name, &$tpl_timestamp, &$smarty_obj)
 {
     $sql = new SQL_Generator();
     $sql->select(array(array('news_entries', 'news_entry_id'), array('news_entries', 'lastmodified')));
     $sql->where(array('news_entries', 'news_entry_id', $tpl_name));
     $db_result = $sql->execute();
     if (mysql_num_rows($db_result)) {
         $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
         $tpl_timestamp = $db_row['lastmodified'];
         return true;
     } else {
         return false;
     }
 }
function permissions_check($type, $id, $actions = array(), $handle = true)
{
    $sql = new SQL_Generator();
    // players permissions: allow for everything
    // planets permissions disallow/allow for planet
    // owner = only if they own it
    // grant = sudo for everything
    // research, build, commission, military = specific grant
    $acceptable_actions = array('research', 'build', 'commission', 'military');
    if (empty($actions)) {
        $actions = $acceptable_actions;
        $handle = false;
    }
    if ($type < 1 || $type > 3) {
        error(__FILE__, __LINE__, 'PERMISSIONS_INVALID', 'Invalid permissions check.');
    }
    if (!is_array($actions)) {
        $actions = array($actions);
    }
    if (empty($id)) {
        error(__FILE__, __LINE__, 'PERMISSIONS_INVALID_ID', 'Invalid permissions id specified.');
    }
    $tables = array(PERMISSION_PLANET => 'planet', PERMISSION_ARMY => 'armygroup', PERMISSION_NAVY => 'navygroup');
    $table = $tables[$type];
    $sql->select(array(array($table . 's', 'kingdom_id'), array($table . 's', 'player_id')));
    $sql->where(array(array($table . 's', $table . '_id', $id), array('players', 'player_id', $_SESSION['player_id'])));
    $sql->limit(1);
    $db_query = $sql->generate();
    $db_result = mysql_query($db_query);
    $check = mysql_fetch_array($db_result, MYSQL_ASSOC);
    $grant = false;
    $level = 0;
    while (in_array('grant', $actions)) {
        unset($actions[array_search('grant', $actions)]);
        $actions = $acceptable_actions;
    }
    // check if they're in the same kingdom.
    if ($_SESSION['kingdom_id'] == $check['kingdom_id']) {
        // check if they're the owner. Overrides all permissions.
        if ($_SESSION['player_id'] == $check['player_id']) {
            $grant = true;
            $return['owner'] = true;
            foreach ($actions as $action) {
                $return[$action] = true;
            }
        } else {
            $return['owner'] = false;
            while (in_array('owner', $actions)) {
                unset($actions[array_search('owner', $actions)]);
            }
            if (count($actions) > 0) {
                $db_query = "SELECT * FROM `permissions` WHERE `player_id` = '" . $_SESSION['player_id'] . "' AND ((`type` = '" . $type . "' AND `id` = '" . $id . "') OR `type` = '0') ORDER BY `type` DESC LIMIT 1";
                //					 $db_query = $sql->generate();
                $db_result = mysql_query($db_query);
                $check = mysql_fetch_array($db_result, MYSQL_ASSOC);
                foreach ($actions as $level => $action) {
                    if (in_array($action, $acceptable_actions) && $check[$action] == 1) {
                        $grant = true;
                        $return[$action] = true;
                    } else {
                        $return[$action] = false;
                    }
                }
            }
        }
    } else {
        $return['owner'] = false;
        foreach ($actions as $action) {
            $return[$action] = false;
        }
    }
    if ($handle) {
        if (!$grant) {
            global $smarty;
            $smarty->append('status', 'You do not have permission to access that.');
            $smarty->display('error.tpl');
            exit;
        } elseif ($grant && !$return[$actions[0]]) {
            $pages = array('research' => 'research.php', 'build' => 'buildings.php', 'commission' => 'units.php', 'military' => 'military.php');
            redirect($pages[$action]);
        }
    }
    $return['grant'] = $grant;
    return $return;
}
if (!file_exists($file)) {
    exit('function not found');
}
require_once $file;
if (!function_exists($fn)) {
    exit('function not found');
}
$fn();
exit($fn);
// The rest of this file is remenants of the previous cleaner mess.
exit('Done.');
// TODO: Add blueprint to concept_id link
foreach (array('army', 'navy', 'weapon') as $type) {
    $unit_types = array();
    $sql->select(array(array($type . 'blueprints', $type . 'blueprint_id'), array($type . 'designs', $type . 'concept_id')));
    $sql->where(array(array($type . 'designs', $type . 'design_id', array($type . 'blueprints', $type . 'design_id'))));
    $db_result = $sql->execute();
    while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
        $unit_types[$db_row[$type . 'concept_id']][] = $db_row[$type . 'blueprint_id'];
    }
    foreach ($unit_types as $unit_type => $units) {
        $sql->set(array($type . 'blueprints', $type . 'concept_id', $unit_type));
        $sql->where(array($type . 'blueprints', $type . 'blueprint_id', $units, 'IN'));
        $db_result = $sql->execute();
    }
}
//	 $sql->select(array('tasks', 'task_id'));
//	 $sql->where(array(
//		 array('tasks', 'type', array(TASK_BUILD, TASK_RESEARCH, TASK_UPGRADE, TASK_UNIT), 'IN'),
//		 array('planets', 'planet_id', array('tasks', 'planet_id')),
//		 array('tasks', 'kingdom_id', array('planets', 'kingdom_id'), '!='),