$attachedFiles = sirportly_upload_attachments($_FILES['attachments']);
     ## New ticket params
     $params = array();
     $params['subject'] = $subject;
     $params['status'] = $newStatusId;
     $params['priority'] = $_POST['priorities'];
     $params['department'] = $deptid;
     $params['contact'] = $sirportlyContact;
     ## Custom field params
     foreach ($customfield as $cf => $value) {
         $params["custom[{$cf}]"] = $value;
     }
     ## Submit the ticket to Sirportly
     $sirportlyTicket = _doSirportlyAPICall('tickets/submit', $params);
     ## Check to see if we encountered any errors
     if (checkForSirportlyErrors($sirportlyTicket)) {
         $step = 2;
         $formattedErrorMessages = formatSirportlyErrors($ticket['errors']);
         $ca->assign('errormessage', $formattedErrorMessages);
         $ca->setTemplate('/templates/sirportly/supportticketsubmit-steptwo.tpl');
         $ca->output();
         return;
     }
     ## Add the first update
     $sirportlyTicketUpdate = _doSirportlyAPICall('tickets/post_update', array('ticket' => $sirportlyTicket['reference'], 'authenticated' => true, 'attachments' => implode($attachedFiles), 'contact' => $sirportlyTicket['contact']['id'], 'message' => $message));
     ## Set the ticket variables
     $_SESSION['tempticketdata'] = array('tid' => $sirportlyTicket['reference'], 'c' => $sirportlyTicket['id'], 'subject' => $sirportlyTicket['subject']);
     ## Redirect to the next step
     redir("step=4");
     break;
 case '4':
function findOrCreateSirportlyContact($uid, $cid)
{
    ## Setup the query
    $user_query = $uid === null ? 'is NULL' : "= '{$uid}'";
    $client_query = $cid === null ? 'is NULL' : "= '{$cid}'";
    $query = full_query("SELECT `sirportly_id` FROM `sirportly_contacts` WHERE `user_id` {$user_query} AND `contact_id` {$client_query}");
    $result = mysql_fetch_array($query, MYSQL_ASSOC);
    if (empty($result['sirportly_id'])) {
        ## Fetch the client details
        $clientDetails = getClientsDetails($uid, $cid);
        ## Attempt to search Sirportly for the contact
        $contactSearch = _doSirportlyAPICall('contacts/search', array('query' => $clientDetails['email'], 'types' => 'email', 'limit' => '1'));
        ## Check to see if we encountered any errors
        if (checkForSirportlyErrors($contactSearch)) {
            die('Unable to create Sirportly contact');
        }
        if (empty($contactSearch)) {
            ## Attempt to create the contact
            $createSirportlyContact = _doSirportlyAPICall('contacts/create', array('name' => $clientDetails['fullname'], 'company' => $clientDetails['company']));
            ## Check to see if we encountered any errors
            if (checkForSirportlyErrors($createSirportlyContact)) {
                die('Unable to create Sirportly contact');
            }
            ## Attempt to create the contact method
            $createSirportlyContactMethod = _doSirportlyAPICall('contacts/add_contact_method', array('contact' => $createSirportlyContact['id'], 'method_type' => 'email', 'data' => $clientDetails['email']));
            ## Check to see if we encountered any errors
            if (checkForSirportlyErrors($createSirportlyContactMethod)) {
                die('Unable to create Sirportly contact method');
            }
            ## Store the Sirportly contact ID for future
            storeSirportlyContact($uid, $cid, $createSirportlyContact['id']);
            ## Return the contact id
            return $createSirportlyContact['id'];
        } else {
            ## Store the Sirportly contact ID for future
            storeSirportlyContact($uid, $cid, $contactSearch['0']['contact']['id']);
            ## Return the contact id
            return $contactSearch['0']['contact']['id'];
        }
        ## If we got here something seriously went wrong
        die('Contact doesn\'t exist');
    } else {
        ## Return the contact id
        return $result['sirportly_id'];
    }
}
## Add an update to the ticket
if ($postreply) {
    check_token();
    if (!$replymessage) {
        $errormessage .= "<li>" . $_LANG['supportticketserrornomessage'];
    }
    $ca->assign('postingReply', isset($postreply));
    $ca->assign('errormessage', $errormessage);
    ## If we're error free attempt to submit the update to Sirportly
    if (!$errormessage) {
        ## Upload any attachments and store their tokens
        $attachedFiles = sirportly_upload_attachments($_FILES['attachments']);
        ## Submit the update
        $sirportlyTicketUpdate = _doSirportlyAPICall('tickets/post_update', array('ticket' => $tid, 'message' => $replymessage, 'attachments' => implode($attachedFiles), 'contact' => $sirportlyContact));
        ## Check to see if we encountered any errors
        if (checkForSirportlyErrors($sirportlyTicketUpdate)) {
            $formattedErrorMessages = formatSirportlyErrors($update['errors']);
            $ca->assign('errormessage', $formattedErrorMessages);
        } else {
            redir("tid=" . $tid . "&c=" . $c);
        }
    }
}
## Setup the updates
$updates = array();
foreach ($response['updates'] as $update) {
    if (!$update['private']) {
        ## Sort the attachments
        $attachments = array();
        foreach ($update['attachments'] as $attachment) {
            $attachments[] = array('id' => $attachment['id'], 'name' => $attachment['name']);