function execute_examinefleet()
{
    global $uid;
    $its_fleet = new fleet($_GET["fid"]);
    $can_see = false;
    if ($its_fleet->uid == $uid) {
        show_svg_message("There should be no button that would allow you doing this. :S");
        return false;
    }
    $allies = get_allied_ids($uid);
    if (is_array($allies)) {
        array_push($allies, $uid);
    } else {
        $allies[] = $uid;
    }
    $allied_fleet = in_array($its_fleet->uid, $allies);
    // check ob eigene oder alleierte planeten im system sind
    $sth = mysql_query("select 1 from planets where sid=" . $its_fleet->sid . " and uid in (" . implode(",", $allies) . ") LIMIT 1");
    if (!$sth) {
        show_svg_message("There was an Database error #mcontrol:exec_exam_fleet:1");
        return false;
    }
    if (mysql_num_rows($sth) > 0) {
        $can_see = true;
    }
    // check ob eigene oder alleierte flotten im system sind
    if (!$can_see) {
        $sth = mysql_query("select 1 from fleet_info where sid=" . $its_fleet->sid . " and uid in (" . implode(",", $allies) . ") LIMIT 1");
        if (!$sth) {
            show_svg_message("There was an Database error #mcontrol:exec_exam_fleet:2");
            return false;
        }
        if (mysql_num_rows($sth) > 0) {
            $can_see = true;
        }
    }
    if (can_see) {
        $doc = domxml_new_doc("1.0");
        $rootElem = $doc->create_element("SR_REQUEST");
        $rootElem->set_attribute("type", "examine_fleet");
        $rootElem->set_attribute("fid", $its_fleet->fid);
        $rootElem->set_attribute("owner", $its_fleet->uid);
        $missionElem = $doc->create_element("SR_FLEET_MISSION");
        $no_scan = false;
        // mission
        if ($allied_fleet) {
            // alleierte Flotte, wir sehen die mission
            // mission kann sich unterscheiden wenn das ziel feindlich ist.
            $no_target = false;
            $target_relation = false;
            if ($its_fleet->tsid > 0 && $its_fleet->tpid > 0) {
                $sth = mysql_query("SELECT s.name as s_name, p.name, p.uid, p.type, p.id FROM systems s LEFT JOIN planets p on s.id = p.sid WHERE s.id = " . $its_fleet->tsid . " and p.id=" . $its_fleet->tpid);
            } elseif ($its_fleet->tsid > 0) {
                $sth = mysql_query("SELECT name as s_name from systems where id = " . $its_fleet->tsid);
            } else {
                $no_target = true;
            }
            if (!$no_target) {
                if (!$sth || mysql_num_rows($sth) == 0) {
                    show_svg_message("ERR::Can not get Target");
                    return false;
                }
                $target_info = mysql_fetch_assoc($sth);
                if ($target_info["s_name"]) {
                    $missionElem->set_attribute("tsys_name", htmlspecialchars($target_info["s_name"]));
                }
                if ($target_info["name"]) {
                    if ($target_info["name"] == "Unnamed") {
                        $target_info["name"] = get_planetname($target_info["id"]);
                    }
                    $missionElem->set_attribute("tpla_name", htmlspecialchars($target_info["name"]));
                    $missionElem->set_attribute("tpla_type", $target_info["type"]);
                    if ($target_info["uid"] > 0) {
                        $target_relation = get_uids_relation($its_fleet->uid, $target_info["uid"]);
                    }
                }
                $eta = get_true_ETA_by_fid($its_fleet->fid);
                $rootElem->set_attribute("eta", $eta);
            }
            switch ($its_fleet->mission) {
                case M_MOVE:
                    if ($target_relation == "enemy") {
                        $missionElem->set_attribute("mission", "fleet_mission_attack.svgz");
                        $missionElem->set_attribute("missionText", "Attacking");
                    } elseif ($target_relation == "allie" || $target_relation == "same" || $its_fleet->tsid == 0) {
                        $missionElem->set_attribute("mission", "fleet_mission_defend.svgz");
                        $missionElem->set_attribute("missionText", "Defending");
                    } else {
                        $missionElem->set_attribute("mission", "fleet_mission_move.svgz");
                        $missionElem->set_attribute("missionText", "Moving");
                    }
                    break;
                case M_INVADE:
                    $missionElem->set_attribute("mission", "fleet_mission_invade.svgz");
                    $missionElem->set_attribute("missionText", "Invading");
                    break;
                case M_BOMB:
                    $missionElem->set_attribute("mission", "fleet_mission_bomb.svgz");
                    $missionElem->set_attribute("missionText", "Bombarding");
                    break;
                case M_COLONIZE:
                    $missionElem->set_attribute("mission", "fleet_mission_colonize.svgz");
                    $missionElem->set_attribute("missionText", "Colonizing");
                    break;
            }
            $missionElem->set_attribute("tactic", $its_fleet->tactic);
        } else {
            $missionElem->set_attribute("mission", "fleet_mission_unknown.svgz");
            $missionElem->set_attribute("missionText", "unknown mission");
            $missionElem->set_attribute("tactic", "-1");
            // gucken ob wir no-scan schiffe haben (special 'N' in shipvalues)
            $sth = mysql_query("SELECT s.prod_id FROM fleet f, shipvalues s WHERE s.special = 'N' AND s.prod_id = f.prod_id AND f.fid = " . $its_fleet->fid);
            if (!$sth) {
                show_svg_message("ERROR::Getting NOSCAN ships");
                return false;
            }
            if (mysql_num_rows($sth) > 0) {
                $no_scan = true;
            }
            // gucken ob der planet vielleicht so nen gebäude hat??
            if (!$no_scan && $its_fleet->pid > 0) {
                // alleierte der flotte
                $fleet_allies = get_allied_ids($its_fleet->uid);
                if (is_array($fleet_allies)) {
                    array_push($fleet_allies, $its_fleet->uid);
                } else {
                    $fleet_allies[] = $its_fleet->uid;
                }
                $sth = mysql_query("SELECT s.prod_id FROM planets p, constructions c join shipvalues s using(prod_id) where s.special='N' and c.pid = p.id and p.id = " . $its_fleet->pid . " and p.uid in (" . implode(",", $fleet_allies) . ")");
                if (!$sth) {
                    show_svg_message("ERROR::Getting NOSCAN buildings");
                    return false;
                }
                if (mysql_num_rows($sth) > 0) {
                    $no_scan = true;
                }
            }
        }
        $rootElem->append_child($missionElem);
        // ships
        if (!$no_scan) {
            foreach ($its_fleet->ships as $prod_id => $values) {
                $shipElem = $doc->create_element("SR_SHIP");
                $shipElem->set_attribute("prod_id", $prod_id);
                $shipElem->set_attribute("count", $values[0]);
                if ($allied_fleet) {
                    $shipElem->set_attribute("reload", $values[1]);
                } else {
                    $shipElem->set_attribute("reload", -1);
                }
                $rootElem->append_child($shipElem);
            }
        }
        // admiral
        $sth = mysql_query("SELECT a.pic, a.name, a.id, a.fid from admirals a join fleet_info f using(fid) having a.fid=" . $its_fleet->fid . "");
        if (!$sth) {
            show_svg_message("There was an Database error #mcontrol:exec_exam_fleet:3");
            return false;
        }
        if (mysql_num_rows($sth) > 0) {
            list($a_pic, $a_name, $a_id) = mysql_fetch_row($sth);
            $admiralElem = $doc->create_element("SR_ADMIRAL");
            $admiralElem->set_attribute("name", $a_name);
            $admiralElem->set_attribute("pic", $a_pic);
            $admiralElem->set_attribute("level", get_admiral_level($a_id));
            $rootElem->append_child($admiralElem);
            $rootElem->set_attribute("pic", "ADMIRAL");
        } else {
            if (!$no_scan) {
                $rootElem->set_attribute("pic", get_strongest_ship_by_fid($its_fleet->fid));
            } else {
                $rootElem->set_attribute("pic", 0);
            }
        }
        $doc->append_child($rootElem);
        echo $doc->dump_mem();
    } else {
        show_svg_message("Either this is a bug or you're a  h4xx0r. :S");
    }
}
function get_planetProduction($pid)
{
    global $uid;
    $puid = get_uid_by_pid($pid);
    $p_relation = get_uids_relation($uid, $puid);
    if ($p_relation == "same" || $p_relation == "allie") {
        $production_tag = create_production_tag($pid);
    }
    echo "<SR_REQUEST type=\"planet_info_prod\" pid=\"" . $pid . "\">";
    echo $production_tag;
    echo "</SR_REQUEST>";
}
function move_fleet($mission)
{
    global $uid;
    $fid = $_GET["fid"];
    $targetId = $_GET["targetId"];
    $targetType = $_GET["targetType"];
    // planet || system
    $fleet = new fleet($fid);
    // validation
    if ($uid != $fleet->uid) {
        if ($fleet->milminister == 1) {
            $sth = mysql_query("select a.milminister from users u left join alliance a on u.alliance = a.id where u.id = " . $uid);
            if (!$sth) {
                show_svg_message("ERR::GET MoD");
                return false;
            }
            list($its_mod) = mysql_fetch_row($sth);
            if ($its_mod != $uid) {
                show_svg_message("You can not command that fleet");
                return false;
            }
        } else {
            show_svg_message("You can not command that fleet");
            return false;
        }
    }
    if ($targetType == "system") {
        $destination_system = $targetId;
        $destination_planet = 0;
        $destination_name = get_systemname($targetId);
    } else {
        $sth = mysql_query("select sid, name, uid from planets where id = '{$targetId}'");
        if (!$sth || !mysql_num_rows($sth)) {
            return 0;
        }
        $dest_data = mysql_fetch_row($sth);
        $destination_system = $dest_data[0];
        $destination_name = $dest_data[1];
        $destination_planet = $targetId;
        $destination_planet_uid = $dest_data[2];
    }
    $max_warp = get_max_warp($uid);
    $route = move_to($fleet->sid, $destination_system, $max_warp);
    if (is_array($route)) {
        $fleet_mode = "defensive";
        // gucken ob der zielplanet ein feindlicher ist
        if ($destination_planet != 0 && $destination_planet_uid != 0) {
            if (get_uids_relation($uid, $destination_planet_uid) == "enemy") {
                $fleet_mode = "aggressive";
            }
        }
        set_mission($fid, $mission, $destination_system, $destination_planet);
        $its_mission = get_mission_by_mission_id($mission, $fleet_mode);
        set_route($route, $fid);
        $true_eta = get_true_ETA_by_fid($fid);
        $request .= "<SR_REQUEST oid=\"" . $fid . "\" type=\"FLEET_ROUTE\">";
        $request .= "<SR_ROUTE_INFO eta=\"" . $true_eta . "\" targetName=\"" . $destination_name . "\" sid=\"" . $fleet->sid . "\" mission=\"" . $mission . "\" missionName=\"" . $its_mission[0] . "\" missionSymbol=\"" . $its_mission[2] . "\" />";
        // general info
        if ($fleet->sid == $destination_system) {
            if ($destination_planet == 0 || $fleet->pid == 0) {
                if ($destination_planet == 0) {
                    $target_planet = $fleet->pid;
                    $target_system = $destination_system;
                } else {
                    $target_planet = $destination_planet;
                    $target_system = $fleet->sid;
                }
                $request .= "<SR_ROUTE_SYSTEM_TO_PLANET jumpNumber=\"0\" pid=\"" . $target_planet . "\" sid=\"" . $target_system . "\"/>";
            } else {
                $request .= "<SR_ROUTE_PLANET_TO_PLANET jumpNumber=\"0\" pid1=\"" . $fleet->pid . "\" pid2=\"" . $destination_planet . "\" sid=\"" . $fleet->sid . "\"/>";
            }
        } else {
            // wenn die flotte nicht schon überm stern hängt, muss sie erst dahin fliegen um das system zu verlassen
            $a = 0;
            if ($fleet->pid != 0) {
                $request .= "<SR_ROUTE_SYSTEM_TO_PLANET jumpNumber=\"" . $a++ . "\" pid=\"" . $fleet->pid . "\" sid=\"" . $fleet->sid . "\"/>";
            }
            for ($i = 0; $i < sizeof($route); $i++) {
                $request .= "<SR_ROUTE_SYSTEM jumpNumber=\"" . ($i + $a) . "\" sid=\"" . $route[$i] . "\"/>";
            }
            // wenn die flotte nicht zum einem anderen stern fliegt, sondern zu einem planeten, kommt noch ein inter-planetärer flug hinzu
            if ($destination_planet != 0) {
                $request .= "<SR_ROUTE_SYSTEM_TO_PLANET jumpNumber=\"" . ++$i . "\" pid=\"" . $destination_planet . "\" sid=\"" . $destination_system . "\"/>";
            }
        }
        $request .= "</SR_REQUEST>";
        echo $request;
        return true;
    } else {
        show_svg_message("Sir! Our fleet '" . get_fleet_name($_GET["fid"]) . "' can't carry out your command. The targets destination has an insuperable gap that exceeds our warp technolgy of " . $max_warp . " parsec.");
        return false;
    }
}
function draw_fleet($fids, $pid)
{
    global $uid;
    for ($i = 0; $i < sizeof($fids); $i++) {
        $its_uid = get_uid_by_fid($fids[$i]);
        $relation = get_uids_relation($uid, $its_uid);
        switch ($relation) {
            case "same":
                $same++;
                break;
            case "allie":
                $allie++;
                break;
            case "friend":
                $friend++;
                break;
            case "enemy":
                $enemy++;
                break;
            case "neutral":
                $neutral++;
                break;
        }
    }
    $root_x = 10;
    $root_y = 20;
    if ($same) {
        $x = $root_x * -1;
        $y = $root_y * -1;
        // $fleets .= "<g onclick=\"showUnits('".$pid."',95);\" class=\"fleetsOwn\">";
        $fleets .= "<g class=\"fleetsOwn\">";
        $fleets .= "<use xlink:href=\"#sShip\" x=\"" . $x . "\" y=\"" . $y . "\"/>";
        $x = $x - 3;
        $y = $y + 9;
        for ($i = 0; $i < $same; $i++) {
            $y -= 3;
            if ($i >= 4) {
                $x += -3;
                $y = $root_y * -1 + 9;
            }
            $fleets .= "<rect x=\"" . $x . "\" y=\"" . $y . "\" width=\"2\" height=\"1\"/>";
        }
        $fleets .= "</g>";
    }
    if ($allie) {
        $x = ($root_x + 10) * -1;
        $y = $root_y * 0 - 5;
        // $fleets .= "<g onclick=\"showUnits('".$pid."',96);\" class=\"fleetsAllied\">";
        $fleets .= "<g class=\"fleetsAllied\">";
        $fleets .= "<use xlink:href=\"#sShip\" x=\"" . $x . "\" y=\"" . $y . "\"/>";
        $x = $x - 3;
        $y = $y + 9;
        for ($i = 0; $i < $allie; $i++) {
            $y -= 3;
            if ($i >= 4) {
                $x += -3;
                $y = $root_y * 0 + 9;
            }
            $fleets .= "<rect x=\"" . $x . "\" y=\"" . $y . "\" width=\"2\" height=\"1\"/>";
        }
        $fleets .= "</g>";
    }
    if ($friend) {
        $x = $root_x * -1;
        $y = $root_y - 10;
        //$fleets .= "<g onclick=\"showUnits('".$pid."',97);\" class=\"fleetsFriend\">";
        $fleets .= "<g class=\"fleetsFriend\">";
        $fleets .= "<use xlink:href=\"#sShip\" x=\"" . $x . "\" y=\"" . $y . "\"/>";
        $x = $x - 3;
        $y = $y + 9;
        for ($i = 0; $i < $friend; $i++) {
            $y -= 3;
            if ($i >= 4) {
                $x += -3;
                $y = $root_y * -1 + 9;
            }
            $fleets .= "<rect x=\"" . $x . "\" y=\"" . $y . "\" width=\"2\" height=\"1\"/>";
        }
        $fleets .= "</g>";
    }
    if ($neutral) {
        $x = $root_x - 5;
        $y = $root_y - 10;
        //$fleets .= "<g onclick=\"showUnits('".$pid."',98);\" class=\"fleetsNeutral\">";
        $fleets .= "<g class=\"fleetsNeutral\">";
        $fleets .= "<use xlink:href=\"#sShip\" x=\"" . $x . "\" y=\"" . $y . "\"/>";
        $x = $x + 10;
        $y = $y + 9;
        for ($i = 0; $i < $neutral; $i++) {
            $y -= 3;
            if ($i >= 4) {
                $x += 3;
                $y = $root_y + 9;
            }
            $fleets .= "<rect x=\"" . $x . "\" y=\"" . $y . "\" width=\"2\" height=\"1\"/>";
        }
        $fleets .= "</g>";
    }
    if ($enemy) {
        $x = $root_x;
        $y = $root_y * 0 - 5;
        //$fleets .= "<g onclick=\"showUnits('".$pid."',99);\" class=\"fleetsEnemy\">";
        $fleets .= "<g class=\"fleetsEnemy\">";
        $fleets .= "<use xlink:href=\"#sShip\" x=\"" . $x . "\" y=\"" . $y . "\"/>";
        $x = $x + 10;
        $y = $y + 9;
        for ($i = 0; $i < $enemy; $i++) {
            $y -= 3;
            if ($i >= 4) {
                $x += 3;
                $y = $root_y + 9;
            }
            $fleets .= "<rect x=\"" . $x . "\" y=\"" . $y . "\" width=\"2\" height=\"1\"/>";
        }
        $fleets .= "</g>";
    }
    // hack, custom element, das anzeigt was für flotten bei dem planeten vertreten sind
    $fleets .= "<SR_FLEET_AT_PLANET enemy=\"" . $enemy . "\" allied=\"" . $allied . "\" own=\"" . $same . "\" friendly=\"" . $friend . "\" neutral=\"" . $neutral . "\"/>";
    return $fleets;
}
function infantery_info()
{
    global $pid;
    global $uid;
    global $skin;
    $sth = mysql_query("select id from planets where id={$pid} and uid={$uid}");
    if (!$sth) {
        show_error("Database failure!");
        return 0;
    }
    if (mysql_num_rows($sth) == 0) {
        show_error("Yeah! You just hit one of my security barriers:)!");
        return 0;
    }
    table_start("center", "500");
    table_head_text(array("Infantry"), "4");
    table_text_open("head");
    table_text_design("&nbsp;", "80", "", "", "head");
    table_text_design("&nbsp;", "420", "", "3", "head");
    table_text_close();
    /*
    $sth=mysql_query("select i.prod_id,i.count,i.uid from infantery as i, production as p where i.pid='".$pid."' and i.prod_id=p.prod_id");
    
    while ($infantery=mysql_fetch_array($sth))
    {
      $total_infantery[$infantery["prod_id"]][$infantery["uid"]] += $infantery["count"];
    }
    */
    $sth = mysql_query("select i.prod_id, i.count, i.uid, p.name, p.pic from infantery as i, production as p where i.pid='{$pid}' and i.prod_id=p.prod_id order by i.uid");
    while ($inf = mysql_fetch_array($sth)) {
        $inf_on_planet[$inf["uid"]][$inf["prod_id"]] = $inf["count"];
        $inf_info[$inf["prod_id"]]["name"] = $inf["name"];
        $inf_info[$inf["prod_id"]]["pic"] = $inf["pic"];
        if (!$user_info[$inf["uid"]]) {
            $user_info[$inf["uid"]]["relation"] = get_uids_relation($uid, $inf["uid"], 1);
            $user_info[$inf["uid"]]["name"] = get_name_by_uid($inf["uid"]);
        }
        $dummy3 = "";
    }
    if (is_array($inf_on_planet)) {
        foreach ($inf_on_planet as $i_uid => $inf) {
            foreach ($inf as $prod_id => $count) {
                table_text_open("text", "center");
                table_text_design("<img src='arts/" . $inf_info[$prod_id]["pic"] . "' border='1' alt='" . $infantery["description"] . "' align='center'></a>", "80", "", "", "text");
                table_text_design($inf_info[$prod_id]["name"] . " (" . $user_info[$i_uid]["name"] . ")", "320", "", "", "text");
                table_text_design($count, "100", "", "", "text");
                table_text_close();
            }
        }
    }
    if (!isset($dummy3)) {
        show_message("You have no infantry on this planet");
    }
    table_end();
    $sth4 = mysql_query("select * from i_production as ip,production as p where ip.planet_id='" . $pid . "' and p.prod_id=ip.prod_id");
    infantery($sth4);
}