<?php
# --------------------------------------------------------
# Basic Plastic SCM Integration with Mantis Bug Tracker
# Copyright (C) 2007 Codice Software S.L.
# This program is distributed under the terms and conditions of the GPL
# --------------------------------------------------------

# This script requires access to the Mantis API. It uses the mantis
# configuration settings to contact the mantis server.

require_once ('./core.php');

if(isset($_GET['info']))
{
    header('Content-type: text/plain');
    $output = "Unity VCS version: 2.0\n";

    $user = $_GET['user'];
    if (!check_user($user))
    {
        $output .= "Unexisting user: $user\n";
    }
    print ($output);
}
elseif(isset($_GET['getbugs']))
{
    header('Content-type: text/xml');

    $bug_ids = explode(",", $_GET['getbugs']);
    $bugs = get_bugs($bug_ids);
    $xml = to_xml($bugs);

    print ($xml);
}
elseif(isset($_GET['opentask']))
{
    $bug_id = $_GET['opentask'];
    if (isset($_GET['user']))
    {
        assign_bug($bug_id, $_GET['user']);
    }
    else
    {
        confirm_bug($bug_id);
    }
}
elseif (isset($_GET['pending'])) {
    header('Content-type: text/xml');
    $bugs = search_pending_bugs($_GET['user']);
    $xml = to_xml($bugs);

    print ($xml);
}
else
{
    header('Content-type: text/html');
}


###### auxiliary functions ######

function check_user($user_name)
{
    if (!isset($user_name))
        return true;

    if ($user_name === "")
        return true;

    $user_id = user_get_id_by_name($user_name);

    if (!$user_id)
        return false;
    return true;
}

# generate the xml output with the bug infos
function to_xml ($bugs)
{
    $encoding = "UTF-8";

    $output = "<BugsGroup>\n";
    $output .= "<Bugs>\n";
    foreach ($bugs as $bug_id => $bug_data)
    {
        $output .= "<Bug>\n";
        $output .= "<Id>$bug_id</Id>\n";

        # escape html characters
        $summary = xmlentities(htmlentities(
            $bug_data->summary, null, $encoding));
        $output .= "<Title>$summary</Title>\n";

        # escape html characters
        $description = xmlentities(htmlentities(
            $bug_data->description, null, $encoding));
        $output .= "<Description>$description</Description>\n";

        #covert id status to string
        $status = xmlentities(htmlentities(
            get_enum_element( 'status', $bug_data->status ), null, $encoding));
        $output .= "<Status>$status</Status>\n";

        #recover project name from id
        $project = xmlentities(htmlentities(
            project_get_field( $bug_data->project_id, 'name'), null, $encoding));
        $output .= "<Project>$project</Project>\n";

        $handler = get_handler_name($bug_data);
        $output .= "<Owner>$handler</Owner>\n";
        $output .= "</Bug>\n";
    }
    $output .= "</Bugs>\n";
    $output .= "</BugsGroup>\n";

    return $output;
}

function get_handler_name($bug_data)
{
    if ($bug_data->handler_id <= 0)
    {
        return "";
    }
    $encoding = "UTF-8";

    #recover user name from id
    return xmlentities(htmlentities(user_get_name($bug_data->handler_id), null, $encoding));
}

#query to recover the issue ids registered in the system
function get_all_bug_ids()
{
    $bug_ids = array();

    $t_bug_table = db_get_table( 'mantis_bug_table' );
    $query = "SELECT id FROM $t_bug_table";

    $result = db_query( $query );
    $bug_count = db_num_rows( $result );

    for ( $i=0 ; $i < $bug_count ; $i++ ) {
        $row = db_fetch_array( $result );
        array_unshift ($bug_ids, $row['id']);
    }

    return $bug_ids;
}

function search_pending_bugs($assignee)
{
    $bug_ids = array();

    $t_bug_table = db_get_table('mantis_bug_table');
    $status_assigned = config_get('bug_assigned_status');

    $query = "SELECT id FROM $t_bug_table WHERE status <= $status_assigned";

    if (isset($assignee))
    {
        $user_id = user_get_id_by_name($assignee);
        if (!$user_id)
        {
            return $bug_ids;
        }
        $query .= " AND handler_id = $user_id";
    }

    $result = db_query($query);
    $bug_count = db_num_rows( $result );

    for ( $i=0 ; $i < $bug_count ; $i++ ) {
        $row = db_fetch_array( $result );
        array_unshift ($bug_ids, $row['id']);
    }

    return get_bugs($bug_ids);
}

function assign_bug($bug_id, $assignee)
{
    $user_id = user_get_id_by_name($assignee);
    if (!$user_id)
    {
        confirm_bug($bug_id);
        return;
    }

    bug_ensure_exists($bug_id);
    bug_assign($bug_id, $user_id, 'Assigned in Plastic SCM');
}

function confirm_bug($bug_id)
{
    bug_ensure_exists($bug_id);
    bug_set_field($bug_id, 'status', CONFIRMED);
}

# get the extended bug information for each bug
function get_bugs($bug_ids)
{
    $bugs = array();

    foreach ($bug_ids as $id)
    {
        $bug_data = bug_get($id, true);
        $bugs[$id] = $bug_data;
    }

    return $bugs;
}


function xmlentities($string) {
   return str_replace ( array ( '&', '"', "'", '<', '>', '?' ), array ( '&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;', '&apos;' ), $string );
}

?>
