Use PSR-4 standard for classloading

remotes/upstream/api-rewrite
Mike Koch 7 years ago
parent 2b0d2bb9cc
commit ad43c420bb

@ -1,17 +1,16 @@
<?php
namespace Core;
// Responsible for loading in all necessary classes. AKA a poor man's DI solution.
use BusinessLogic\Category\CategoryRetriever;
use BusinessLogic\Categories\CategoryRetriever;
use BusinessLogic\Security\BanRetriever;
use BusinessLogic\Security\UserContextBuilder;
use BusinessLogic\Tickets\TicketRetriever;
use DataAccess\CategoryGateway;
use DataAccess\Categories\CategoryGateway;
use DataAccess\Security\BanGateway;
use DataAccess\Security\UserGateway;
use DataAccess\Tickets\TicketGateway;
class ApplicationContext {
public $get;

@ -0,0 +1,50 @@
<?php
namespace BusinessObjects;
class Category {
/**
* @var int The Categories ID
*/
public $id;
/**
* @var int Categories order number
*/
public $catOrder;
/**
* @var bool Tickets autoassigned in this Categories
*/
public $autoAssign;
/**
* @var int The type of Categories (1 = Private, 2 = Public)
*/
public $type;
/**
* @var int The Categories's usage (0 = Tickets and Events, 1 = Tickets, 2 = Events)
*/
public $usage;
/**
* @var string? The color of the Categories
*/
public $color;
/**
* @var int The default Tickets priority
*/
public $priority;
/**
* @var int|null The manager for the Categories, if applicable
*/
public $manager;
/**
* @var bool Indication if the user has access to the Categories
*/
public $accessible;
}

@ -1,9 +1,9 @@
<?php
namespace BusinessLogic\Category;
namespace BusinessLogic\Categories;
use BusinessLogic\Security\UserContext;
use DataAccess\CategoryGateway;
use DataAccess\Categories\CategoryGateway;
class CategoryRetriever {
/**

@ -2,7 +2,7 @@
namespace BusinessLogic\Exceptions;
use BusinessLogic\Validation\ValidationModel;
use BusinessLogic\ValidationModel;
use Exception;
class ValidationException extends Exception {

@ -6,31 +6,35 @@ namespace BusinessLogic\Tickets;
class Ticket {
static function fromDatabaseRow($row, $linkedTicketsRs, $heskSettings) {
$ticket = new Ticket();
$ticket->id = $row['id'];
$ticket->id = intval($row['id']);
$ticket->trackingId = $row['trackid'];
$ticket->name = $row['name'];
$ticket->email = $row['email'];
$ticket->categoryId = $row['category'];
$ticket->priorityId = $row['priority'];
$ticket->categoryId = intval($row['category']);
$ticket->priorityId = intval($row['priority']);
$ticket->subject = $row['subject'];
$ticket->message = $row['message'];
$ticket->dateCreated = $row['dt'];
$ticket->lastChanged = $row['lastchange'];
$ticket->firstReplyDate = $row['firstreply'];
$ticket->closedDate = $row['closedat'];
$ticket->suggestedArticles = explode(',', $row['articles']);
if (trim($row['articles']) !== '') {
$ticket->suggestedArticles = explode(',', $row['articles']);
}
$ticket->ipAddress = $row['ip'];
$ticket->language = $row['language'];
$ticket->statusId = $row['status'];
$ticket->openedBy = $row['openedby'];
$ticket->firstReplyByUserId = $row['firstreplyby'];
$ticket->closedByUserId = $row['closedby'];
$ticket->numberOfReplies = $row['replies'];
$ticket->numberOfStaffReplies = $row['staffreplies'];
$ticket->ownerId = $row['owner'];
$ticket->statusId = intval($row['status']);
$ticket->openedBy = intval($row['openedby']);
$ticket->firstReplyByUserId = $row['firstreplyby'] === null ? null : intval($row['firstreplyby']);
$ticket->closedByUserId = $row['closedby'] === null ? null : intval($row['closedby']);
$ticket->numberOfReplies = intval($row['replies']);
$ticket->numberOfStaffReplies = intval($row['staffreplies']);
$ticket->ownerId = intval($row['owner']);
$ticket->timeWorked = $row['time_worked'];
$ticket->lastReplyBy = $row['lastreplier'];
$ticket->lastReplier = $row['replierid'];
$ticket->lastReplyBy = intval($row['lastreplier']);
$ticket->lastReplier = $row['replierid'] === null ? null : intval($row['replierid']);
$ticket->archived = intval($row['archive']) === 1;
$ticket->locked = intval($row['locked']) === 1;
@ -68,15 +72,23 @@ class Ticket {
$ticket->linkedTicketIds[] = $linkedTicketsRow['id'];
}
$ticket->location = array();
$ticket->location[0] = $row['latitude'];
$ticket->location[1] = $row['longitude'];
if ($row['latitude'] !== '' && $row['longitude'] !== '') {
$ticket->location = array();
$ticket->location[0] = $row['latitude'];
$ticket->location[1] = $row['longitude'];
}
$ticket->usesHtml = intval($row['html']) === 1;
$ticket->userAgent = $row['user_agent'];
$ticket->screenResolution = array();
$ticket->screenResolution[0] = $row['screen_resolution_width'];
$ticket->screenResolution[1] = $row['screen_resolution_height'];
if ($row['user_agent'] !== null && trim($row['user_agent']) !== '') {
$ticket->userAgent = $row['user_agent'];
}
if ($row['screen_resolution_height'] !== null && $row['screen_resolution_width'] !== null){
$ticket->screenResolution = array();
$ticket->screenResolution[0] = $row['screen_resolution_width'];
$ticket->screenResolution[1] = $row['screen_resolution_height'];
}
$ticket->dueDate = $row['due_date'];
$ticket->dueDateOverdueEmailSent = $row['overdue_email_sent'] !== null && intval($row['overdue_email_sent']) === 1;

@ -22,7 +22,7 @@ class TicketCreator {
throw new ValidationException($validationModel);
}
// Create the ticket
// Create the Tickets
}
/**
@ -30,7 +30,7 @@ class TicketCreator {
* @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
* @return ValidationModel If errorKeys is empty, validation successful. Otherwise invalid Tickets
*/
function validate($ticketRequest, $staff, $heskSettings, $modsForHeskSettings) {
$TICKET_PRIORITY_CRITICAL = 0;

@ -13,6 +13,6 @@ class SQLException extends Exception {
function __construct($failingQuery) {
$this->failingQuery = $failingQuery;
parent::__construct('A SQL exception occurred. Check the logs for more information.');
parent::__construct('A SQL Exceptions occurred. Check the logs for more information.');
}
}

@ -1,8 +1,9 @@
<?php
namespace DataAccess;
namespace DataAccess\Categories;
use BusinessObjects\Category;
use DataAccess\CommonDao;
use Exception;
class CategoryGateway extends CommonDao {

@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: Mike
* Date: 1/28/2017
* Time: 1:33 AM
*/
namespace DataAccess;

@ -13,7 +13,7 @@ function get_ticket_for_id($hesk_settings, $user, $id = NULL) {
$clause = $used_where_clause ? ' AND ' : ' WHERE ';
$used_where_clause = true;
$sql .= $clause . ' `category` IN (' . $user['categories'] . ')';
$sql .= $clause . ' `Categories` IN (' . $user['categories'] . ')';
$sql .= " AND ((`heskprivileges` LIKE '%can_view_tickets%' AND `owner` = " . intval($user['id']) . ")";
$sql .= " OR (`heskprivileges` LIKE '%can_view_unassigned%' AND `owner` = 0)";
$sql .= " OR (`heskprivileges` LIKE '%can_view_ass_others%' AND `owner` <> " . intval($user['id']) . "))";
@ -34,7 +34,7 @@ function build_results($response) {
$results = array();
while ($row = hesk_dbFetchAssoc($response)) {
$row['id'] = intval($row['id']);
$row['category'] = intval($row['category']);
$row['Categories'] = intval($row['Categories']);
$row['priority'] = intval($row['priority']);
$row['status'] = intval($row['status']);
$row['replierid'] = intval($row['replierid']);

@ -1,55 +1,15 @@
<?php
// Responsible for loading in all necessary scripts and kicking off the DependencyManager
// Files that are needed that aren't classes, as well as basic initialization
// Core requirements
define('IN_SCRIPT', 1);
define('HESK_PATH', '../');
require_once(__DIR__ . '/bootstrap.php');
require_once(__DIR__ . '/../hesk_settings.inc.php');
require_once(__DIR__ . '/../inc/common.inc.php');
require_once(__DIR__ . '/core/output.php');
require_once(__DIR__ . '/Link.php');
require_once(__DIR__ . '/Core/output.php');
require_once(__DIR__ . '/../hesk_settings.inc.php');
// Mods for HESK API Files
require_once(__DIR__ . '/http_response_code.php');
require_once(__DIR__ . '/dao/CommonDao.php');
require_once(__DIR__ . '/businesslogic/Helpers.php');
// User Context
require_once(__DIR__ . '/dao/security/UserGateway.php');
require_once(__DIR__ . '/businesslogic/security/UserContextBuilder.php');
require_once(__DIR__ . '/businesslogic/security/UserContextNotifications.php');
require_once(__DIR__ . '/businesslogic/security/UserContextPreferences.php');
require_once(__DIR__ . '/businesslogic/security/UserContext.php');
// Categories
require_once(__DIR__ . '/dao/category/CategoryGateway.php');
require_once(__DIR__ . '/businesslogic/category/CategoryRetriever.php');
require_once(__DIR__ . '/businesslogic/category/Category.php');
require_once(__DIR__ . '/controllers/CategoryController.php');
// Banned Emails / IP Addresses
require_once(__DIR__ . '/dao/security/BanGateway.php');
require_once(__DIR__ . '/businesslogic/security/BanRetriever.php');
require_once(__DIR__ . '/businesslogic/security/BannedEmail.php');
require_once(__DIR__ . '/businesslogic/security/BannedIp.php');
// Tickets
require_once(__DIR__ . '/dao/ticket/TicketGateway.php');
require_once(__DIR__ . '/businesslogic/ticket/Attachment.php');
require_once(__DIR__ . '/businesslogic/ticket/Ticket.php');
require_once(__DIR__ . '/businesslogic/ticket/CreateTicketByCustomerModel.php');
require_once(__DIR__ . '/businesslogic/ticket/TicketValidators.php');
require_once(__DIR__ . '/businesslogic/ticket/TicketCreator.php');
require_once(__DIR__ . '/businesslogic/ticket/TicketRetriever.php');
require_once(__DIR__ . '/controllers/TicketController.php');
// Exceptions
require_once(__DIR__ . '/businesslogic/exception/ApiFriendlyException.php');
require_once(__DIR__ . '/businesslogic/exception/InvalidAuthenticationTokenException.php');
require_once(__DIR__ . '/businesslogic/exception/MissingAuthenticationTokenException.php');
require_once(__DIR__ . '/businesslogic/exception/ValidationException.php');
require_once(__DIR__ . '/core/SQLException.php');
hesk_load_api_database_functions();
@ -57,5 +17,4 @@ hesk_load_api_database_functions();
require_once(__DIR__ . '/../inc/custom_fields.inc.php');
// Load the ApplicationContext
require_once(__DIR__ . '/ApplicationContext.php');
$applicationContext = new \Core\ApplicationContext();
$applicationContext = new \ApplicationContext();

@ -0,0 +1,8 @@
<?php
spl_autoload_register(function ($class) {
$file = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require($file);
}
});

@ -1,12 +1,6 @@
<?php
/**
* Created by PhpStorm.
* User: mkoch
* Date: 1/28/2017
* Time: 8:54 PM
*/
namespace BusinessLogic\Helpers;
namespace BusinessLogic;
class Helpers {

@ -1,6 +1,6 @@
<?php
namespace BusinessLogic\Validation;
namespace BusinessLogic;
class ValidationModel {
/**

@ -1,50 +0,0 @@
<?php
namespace BusinessObjects;
class Category {
/**
* @var int The category ID
*/
public $id;
/**
* @var int Category order number
*/
public $catOrder;
/**
* @var bool Tickets autoassigned in this category
*/
public $autoAssign;
/**
* @var int The type of category (1 = Private, 2 = Public)
*/
public $type;
/**
* @var int The category's usage (0 = Tickets and Events, 1 = Tickets, 2 = Events)
*/
public $usage;
/**
* @var string? The color of the category
*/
public $color;
/**
* @var int The default ticket priority
*/
public $priority;
/**
* @var int|null The manager for the category, if applicable
*/
public $manager;
/**
* @var bool Indication if the user has access to the category
*/
public $accessible;
}

@ -5,7 +5,7 @@ namespace BusinessLogic\Security;
use BusinessLogic\Exceptions\InvalidAuthenticationTokenException;
use BusinessLogic\Exceptions\MissingAuthenticationTokenException;
use BusinessLogic\Helpers\Helpers;
use BusinessLogic\Helpers;
use DataAccess\Security\UserGateway;
class UserContextBuilder {

@ -1,23 +0,0 @@
<?php
define('NULL_OR_EMPTY_STRING', 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e');
require_once(API_PATH . 'exception/AccessException.php');
function get_user_for_token_hash($hash, $hesk_settings) {
if ($hash == NULL_OR_EMPTY_STRING) {
throw new AccessException(400);
}
$user_id_sql = "SELECT `user_id` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "user_api_tokens`
WHERE `token` = '" . hesk_dbEscape($hash) . "'";
$user_id_rs = hesk_dbQuery($user_id_sql);
if (hesk_dbNumRows($user_id_rs) == 0) {
throw new AccessException(401);
}
$user_id = hesk_dbFetchAssoc($user_id_rs);
$user_sql = "SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "users` WHERE `id` = ".intval($user_id['user_id']);
$user_rs = hesk_dbQuery($user_sql);
return hesk_dbFetchAssoc($user_rs);
}

@ -1,5 +1,7 @@
<?php
// Properly handle error logging, as well as a fatal error workaround
//require_once(__DIR__ . '/autoload.php');
require_once(__DIR__ . '/bootstrap.php');
require_once(__DIR__ . '/autoload.php');
error_reporting(0);
set_error_handler('errorHandler');
@ -16,7 +18,7 @@ function handle404() {
function before() {
assertApiIsEnabled();
$token = \BusinessLogic\Helpers\Helpers::getHeader('X-AUTH-TOKEN');
$token = \BusinessLogic\Helpers::getHeader('X-AUTH-TOKEN');
buildUserContext($token);
}
@ -50,9 +52,9 @@ function exceptionHandler($exception) {
if (exceptionIsOfType($exception, 'SQLException')) {
/* @var $castedException \Core\Exceptions\SQLException */
$castedException = $exception;
print_error("Fought an uncaught exception", sprintf("%s\n\n%s", $castedException->failingQuery, $exception->getTraceAsString()));
print_error("Fought an uncaught Exceptions", sprintf("%s\n\n%s", $castedException->failingQuery, $exception->getTraceAsString()));
} else {
print_error("Fought an uncaught exception", sprintf("%s\n\n%s", $exception->getMessage(), $exception->getTraceAsString()));
print_error("Fought an uncaught Exceptions", sprintf("%s\n\n%s", $exception->getMessage(), $exception->getTraceAsString()));
}
}
@ -61,8 +63,8 @@ function exceptionHandler($exception) {
}
/**
* @param $exception Exception thrown exception
* @param $class string The name of the expected exception type
* @param $exception Exception thrown Exceptions
* @param $class string The name of the expected Exceptions type
* @return bool
*/
function exceptionIsOfType($exception, $class) {

Loading…
Cancel
Save