Files
itflow/api/v1/ticket_ccs/create.php

84 lines
3.5 KiB
PHP

<?php
/*
* API - Ticket CCs - Create
* POST /api/v1/ticket_ccs/create.php
*
* Add CCs to a ticket, as Add CC does on its page - each is copied in on every
* email about the ticket from then on, and their replies count as the client's.
* Not on a closed ticket, and never one of ITFlow's own addresses.
*
* Parameters (POST, JSON body):
* api_key required - Your API key
* ticket_id required - The ticket
* ticket_cc_email required - An email address, or several: a list, or one string
* separated by commas
* ticket_cc_notify optional - 1 emails each new CC a link to the ticket now, as
* "Email them a link to this ticket now" does
* client_id the ticket's client - needed when the key's user is limited to
* certain clients
*
* Answer: insert_id (the first added), added - each CC added, with its ticket_cc_id -
* and notified, how many were emailed.
*/
require_once '../validate_api_key.php';
require_once '../require_post_method.php';
// Whether ITFlow sends email ($config_smtp_provider)
require_once "../../../includes/load_global_settings.php";
$insert_id = false;
unset($_POST['ticket_cc_id']);
require_once 'ticket_cc_find.php';
if ($ticket) {
if ($cc_locked) {
apiFormRefuse($cc_locked);
}
$emails = ticketCcEmailsValid($_POST['ticket_cc_email'] ?? '', $cc_bad);
if ($emails === null) {
apiFormRefuse("ticket_cc_email has '$cc_bad', which isn't an email address");
}
if (!$emails) {
apiFormRefuse('ticket_cc_email is needed - an email address, or several');
}
$ticket_id = intval($ticket['ticket_id']);
$reference = ticketReference($ticket);
$added = ticketAddCcs($ticket_id, $emails);
if (!$added) {
apiFormRefuse('No new CCs - ' . implode(', ', $emails) . (count($emails) === 1 ? ' is' : ' are') . " CC'd on ticket $reference already, or ITFlow's own");
}
// Each one added, with its id - to take them off again
$added_sql = implode(',', array_map(function ($email) { return "'" . escapeSql($email) . "'"; }, $added));
$added_rows = [];
$sql = mysqli_query($mysqli, "SELECT ticket_cc_id, ticket_cc_email FROM ticket_ccs WHERE ticket_cc_ticket_id = $ticket_id AND ticket_cc_email IN ($added_sql) ORDER BY ticket_cc_id");
while ($row = mysqli_fetch_assoc($sql)) {
$added_rows[] = ['ticket_cc_id' => intval($row['ticket_cc_id']), 'ticket_cc_email' => $row['ticket_cc_email']];
}
$insert_id = $added_rows[0]['ticket_cc_id'] ?? false;
$return_arr['added'] = $added_rows;
// Each told, when asked and ITFlow sends email - as the box on Add CC, shown only then
$notified = 0;
if (intval($_POST['ticket_cc_notify'] ?? 0) === 1 && !empty($config_smtp_provider)) {
$notified = ticketMailCcsAdded($ticket_id, $added, stripslashes($session_name));
}
$return_arr['notified'] = $notified;
$reference_sql = escapeSql($reference);
foreach ($added as $cc_email) {
$cc_email_sql = escapeSql($cc_email);
logTicketHistory($ticket_id, "$session_name added $cc_email_sql as a CC via the API ($api_key_name)");
logAudit("Ticket", "Edit", "Added $cc_email_sql as a CC on ticket $reference_sql via API ($api_key_name)", $client_id, $ticket_id);
}
logAudit("API", "Success", "Added " . count($added) . " CC(s) to ticket $reference_sql via API ($api_key_name)", $client_id);
}
// Output
require_once '../create_output.php';