From e1176ec4eea8fb8f141f50cc0d8aa63514bff6fd Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Thu, 26 Jan 2017 22:00:45 -0500 Subject: [PATCH] Getting started on ticket endpoint... again --- api/businesslogic/ValidationException.php | 4 + api/businesslogic/ValidationModel.php | 2 + .../ticket/CreateTicketByCustomerModel.php | 2 + api/businesslogic/ticket/TicketCreator.php | 117 ++++++++++++++++++ api/businesslogic/ticket/ticket_creator.php | 104 ---------------- api/index.php | 11 +- 6 files changed, 131 insertions(+), 109 deletions(-) create mode 100644 api/businesslogic/ticket/TicketCreator.php diff --git a/api/businesslogic/ValidationException.php b/api/businesslogic/ValidationException.php index 3f174212..b2e3f398 100644 --- a/api/businesslogic/ValidationException.php +++ b/api/businesslogic/ValidationException.php @@ -1,5 +1,9 @@ errorKeys) > 0) { + require_once(__DIR__ . '/../ValidationException.php'); + + // Validation failed + throw new ValidationException($validationModel); + } + + // Create the ticket + } + + /** + * @param $ticketRequest CreateTicketByCustomerModel + * @param $staff bool + * @param $heskSettings array HESK settings + * @param $modsForHeskSettings array Mods for HESK settings + * @return ValidationModel If errorKeys is empty, validation successful. Otherwise invalid ticket + */ + function validate($ticketRequest, $staff, $heskSettings, $modsForHeskSettings) { + require_once(__DIR__ . '/../email_validators.php'); + require_once(__DIR__ . '/../../dao/category_dao.php'); + //require_once('../category/retriever.php'); + //require_once('../bans/retriever.php'); + + $TICKET_PRIORITY_CRITICAL = 0; + + $validationModel = new ValidationModel(); + + if ($ticketRequest->name === NULL || $ticketRequest->name == '') { + $validationModel->errorKeys[] = 'NO_NAME'; + } + + if (hesk_validateEmail($ticketRequest->email, $heskSettings['multi_eml'], false)) { + $validationModel->errorKeys[] = 'INVALID_OR_MISSING_EMAIL'; + } + + if (intval($ticketRequest->category) === 0) { + $allCategories = null; + $validationModel->errorKeys[] = 'NO_CATEGORY'; + } + + // Don't allow critical priority tickets + if ($heskSettings['cust_urgency'] && intval($ticketRequest->priority) === $TICKET_PRIORITY_CRITICAL) { + $validationModel->errorKeys[] = 'CRITICAL_PRIORITY_FORBIDDEN'; + } + + if ($heskSettings['require_subject'] === 1 && + ($ticketRequest->subject === NULL || $ticketRequest->subject === '')) { + $validationModel->errorKeys[] = 'SUBJECT_REQUIRED'; + } + + if ($heskSettings['require_message'] === 1 && + ($ticketRequest->message === NULL || $ticketRequest->message === '')) { + $validationModel->errorKeys[] = 'MESSAGE_REQUIRED'; + } + + foreach ($heskSettings['custom_fields'] as $key => $value) { + // TODO Only check categories that apply to this custom field + if ($value['use'] == 1 && hesk_is_custom_field_in_category($key, intval($ticketRequest->category))) { + $custom_field_value = $ticketRequest->customFields[$key]; + if (empty($custom_field_value)) { + $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::NO_VALUE'; + continue; + } + switch($value['type']) { + case 'date': + if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $custom_field_value)) { + $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::INVALID_DATE'; + } else { + // Actually validate based on range + $date = strtotime($custom_field_value . ' t00:00:00'); + $dmin = strlen($value['value']['dmin']) ? strtotime($value['value']['dmin'] . ' t00:00:00') : false; + $dmax = strlen($value['value']['dmax']) ? strtotime($value['value']['dmax'] . ' t00:00:00') : false; + + if ($dmin && $dmin > $date) { + $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::DATE_BEFORE_MIN::MIN-' . $dmin . '::ENTERED-' . $date; + } elseif ($dmax && $dmax < $date) { + $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::DATE_AFTER_MAX::MAX-' . $dmax . '::ENTERED-' . $date; + } + } + break; + case 'email': + if (!hesk_validateEmail($custom_field_value, $value['value']['multiple'], false)) { + $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::INVALID_OR_MISSING_EMAIL'; + } + break; + } + } + } + + // TODO Check bans (email only; don't check IP on REST requests as they'll most likely be sent via servers) + // TODO submit_ticket.php:320-322 + + // TODO Check if we're at the max number of tickets + // TODO submit_ticket.php:325-334 + + return $validationModel; + } +} \ No newline at end of file diff --git a/api/businesslogic/ticket/ticket_creator.php b/api/businesslogic/ticket/ticket_creator.php index 5edfee18..a4abe2da 100644 --- a/api/businesslogic/ticket/ticket_creator.php +++ b/api/businesslogic/ticket/ticket_creator.php @@ -1,106 +1,2 @@ errorKeys) > 0) { - require_once(__DIR__ . '/../ValidationException.php'); - - // Validation failed - throw new ValidationException($validationModel); - } - - // Create the ticket -} - -/** - * @param $ticket_request CreateTicketByCustomerModel - * @param $staff bool - * @return ValidationModel If errorKeys is empty, validation successful. Otherwise invalid ticket - */ -function validate($ticket_request, $staff, $hesk_settings, $modsForHesk_settings) { - require_once(__DIR__ . '/../email_validators.php'); - require_once(__DIR__ . '/../../dao/category_dao.php'); - //require_once('../category/retriever.php'); - //require_once('../bans/retriever.php'); - - $TICKET_PRIORITY_CRITICAL = 0; - - $validationModel = new ValidationModel(); - - if ($ticket_request->name === NULL || $ticket_request->name == '') { - $validationModel->errorKeys[] = 'NO_NAME'; - } - - if (hesk_validateEmail($ticket_request->email, $hesk_settings['multi_eml'], false)) { - $validationModel->errorKeys[] = 'INVALID_OR_MISSING_EMAIL'; - } - - if (intval($ticket_request->category) === 0) { - $allCategories = null; - $validationModel->errorKeys[] = 'NO_CATEGORY'; - } - - // Don't allow critical priority tickets - if ($hesk_settings['cust_urgency'] && intval($ticket_request->priority) === $TICKET_PRIORITY_CRITICAL) { - $validationModel->errorKeys[] = 'CRITICAL_PRIORITY_FORBIDDEN'; - } - - if ($hesk_settings['require_subject'] === 1 && - ($ticket_request->subject === NULL || $ticket_request->subject === '')) { - $validationModel->errorKeys[] = 'SUBJECT_REQUIRED'; - } - - if ($hesk_settings['require_message'] === 1 && - ($ticket_request->message === NULL || $ticket_request->message === '')) { - $validationModel->errorKeys[] = 'MESSAGE_REQUIRED'; - } - - foreach ($hesk_settings['custom_fields'] as $key => $value) { - // TODO Only check categories that apply to this custom field - if ($value['use'] == 1 && hesk_is_custom_field_in_category($key, intval($ticket_request->category))) { - $custom_field_value = $ticket_request->customFields[$key]; - if (empty($custom_field_value)) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::NO_VALUE'; - continue; - } - switch($v['type']) { - case 'date': - if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $custom_field_value)) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::INVALID_DATE'; - } else { - // Actually validate based on range - $date = strtotime($custom_field_value . ' t00:00:00'); - $dmin = strlen($value['value']['dmin']) ? strtotime($value['value']['dmin'] . ' t00:00:00') : false; - $dmax = strlen($value['value']['dmax']) ? strtotime($value['value']['dmax'] . ' t00:00:00') : false; - - if ($dmin && $dmin > $date) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::DATE_BEFORE_MIN::MIN-' . $dmin . '::ENTERED-' . $date; - } elseif ($dmax && $dmax < $date) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::DATE_AFTER_MAX::MAX-' . $dmax . '::ENTERED-' . $date; - } - } - break; - case 'email': - if (!hesk_validateEmail($custom_field_value, $value['value']['multiple'], false)) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::INVALID_OR_MISSING_EMAIL'; - } - break; - } - } - } - - // TODO Check bans (email only; don't check IP on REST requests as they'll most likely be sent via servers) - // TODO submit_ticket.php:320-322 - - // TODO Check if we're at the max number of tickets - // TODO submit_ticket.php:325-334 - - return $validationModel; -} \ No newline at end of file diff --git a/api/index.php b/api/index.php index 20ae1aa1..6ecf566c 100644 --- a/api/index.php +++ b/api/index.php @@ -3,15 +3,16 @@ define('IN_SCRIPT', 1); define('HESK_PATH', '../'); require_once(__DIR__ . '/core/common.php'); -require(__DIR__ . '/Link.php'); -require(__DIR__ . '/../hesk_settings.inc.php'); +require_once(__DIR__ . '/Link.php'); +require_once(__DIR__ . '/../hesk_settings.inc.php'); // Controllers -require(__DIR__ . '/controllers/CategoryController.php'); +require_once(__DIR__ . '/controllers/CategoryController.php'); hesk_load_api_database_functions(); +require_once(__DIR__ . '/../inc/custom_fields.inc.php'); // Properly handle error logging, as well as a fatal error workaround -error_reporting(0); // Override hesk_settings. We're smarter than it +error_reporting(0); set_error_handler('errorHandler'); register_shutdown_function('fatalErrorShutdownHandler'); @@ -21,7 +22,7 @@ function handle404() { } function assertApiIsEnabled() { - throw new Exception("Some exception here!", 33); + } function errorHandler($errorNumber, $errorMessage, $errorFile, $errorLine) {