2023-03-13 11:06:18 +03:00
|
|
|
|
<?php use JetBrains\PhpStorm\NoReturn;
|
|
|
|
|
|
|
|
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2023-12-31 21:46:27 +03:00
|
|
|
|
* IFLRandevu - İzmir Fen Lisesi Randevu Portalı
|
2021-11-02 12:44:37 +03:00
|
|
|
|
*
|
|
|
|
|
* @package EasyAppointments
|
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
|
* @link https://easyappointments.org
|
2021-11-02 12:44:37 +03:00
|
|
|
|
* @since v1.5.0
|
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Api library.
|
|
|
|
|
*
|
|
|
|
|
* Handles API related functionality.
|
|
|
|
|
*
|
|
|
|
|
* @package Libraries
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
class Api
|
|
|
|
|
{
|
2021-11-02 12:44:37 +03:00
|
|
|
|
/**
|
2023-03-13 11:06:18 +03:00
|
|
|
|
* @var EA_Controller|CI_Controller
|
2021-11-02 12:44:37 +03:00
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected EA_Controller|CI_Controller $CI;
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected int $default_length = 20;
|
2021-11-03 10:21:30 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var EA_Model
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected EA_Model $model;
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Api constructor.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$this->CI = &get_instance();
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
|
|
|
|
$this->CI->load->library('accounts');
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-03 10:21:30 +03:00
|
|
|
|
/**
|
|
|
|
|
* Load and use the provided model class.
|
|
|
|
|
*
|
|
|
|
|
* @param string $model
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
public function model(string $model): void
|
2021-11-03 10:21:30 +03:00
|
|
|
|
{
|
|
|
|
|
$this->CI->load->model($model);
|
|
|
|
|
|
|
|
|
|
$this->model = $this->CI->{$model};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Authorize the API request (Basic Auth or Bearer Token supported).
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
public function auth(): void
|
2021-11-02 12:44:37 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
try {
|
|
|
|
|
// Bearer token.
|
2021-11-02 12:44:37 +03:00
|
|
|
|
$api_token = setting('api_token');
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($api_token) && $api_token === $this->get_bearer_token()) {
|
2021-11-02 12:44:37 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
// Basic auth.
|
|
|
|
|
$username = $_SERVER['PHP_AUTH_USER'] ?? null;
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$password = $_SERVER['PHP_AUTH_PW'] ?? null;
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
2023-10-09 10:56:40 +03:00
|
|
|
|
$user_data = $this->CI->accounts->check_login($username, $password);
|
2022-05-10 00:26:28 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($user_data['role_slug']) || $user_data['role_slug'] !== DB_SLUG_ADMIN) {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
'The provided credentials do not match any admin user!',
|
|
|
|
|
401,
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'Unauthorized',
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-11-02 12:44:37 +03:00
|
|
|
|
}
|
2023-11-29 12:24:09 +03:00
|
|
|
|
} catch (Throwable) {
|
2021-11-02 12:44:37 +03:00
|
|
|
|
$this->request_authentication();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the bearer token value.
|
|
|
|
|
*
|
2023-03-13 11:06:18 +03:00
|
|
|
|
* @return string|null
|
2021-11-02 12:44:37 +03:00
|
|
|
|
*/
|
|
|
|
|
protected function get_bearer_token(): ?string
|
|
|
|
|
{
|
|
|
|
|
$headers = $this->get_authorization_header();
|
|
|
|
|
|
|
|
|
|
// HEADER: Get the access token from the header
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($headers)) {
|
|
|
|
|
if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
|
2021-11-02 12:44:37 +03:00
|
|
|
|
return $matches[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
return null;
|
2021-11-02 12:44:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the authorization header.
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function get_authorization_header(): ?string
|
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$headers = null;
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (isset($_SERVER['Authorization'])) {
|
2021-11-02 12:44:37 +03:00
|
|
|
|
$headers = trim($_SERVER['Authorization']);
|
2023-11-29 12:24:09 +03:00
|
|
|
|
} else {
|
|
|
|
|
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
|
2021-11-02 12:44:37 +03:00
|
|
|
|
// Nginx or fast CGI
|
|
|
|
|
$headers = trim($_SERVER['HTTP_AUTHORIZATION']);
|
2023-11-29 12:24:09 +03:00
|
|
|
|
} elseif (function_exists('apache_request_headers')) {
|
2021-11-02 12:44:37 +03:00
|
|
|
|
$requestHeaders = apache_request_headers();
|
|
|
|
|
|
|
|
|
|
// Server-side fix for bug in old Android versions (a nice side effect of this fix means we don't care
|
|
|
|
|
// about capitalization for Authorization).
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$requestHeaders = array_combine(
|
|
|
|
|
array_map('ucwords', array_keys($requestHeaders)),
|
2023-12-22 13:35:41 +03:00
|
|
|
|
array_values($requestHeaders),
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (isset($requestHeaders['Authorization'])) {
|
2021-11-02 12:44:37 +03:00
|
|
|
|
$headers = trim($requestHeaders['Authorization']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $headers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets request authentication headers.
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
#[NoReturn]
|
|
|
|
|
public function request_authentication(): void
|
2021-11-02 12:44:37 +03:00
|
|
|
|
{
|
|
|
|
|
header('WWW-Authenticate: Basic realm="Easy!Appointments"');
|
|
|
|
|
header('HTTP/1.0 401 Unauthorized');
|
|
|
|
|
exit('You are not authorized to use the API.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the search keyword value of the current request.
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function request_keyword(): ?string
|
|
|
|
|
{
|
|
|
|
|
return request('q');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the limit value of the current request.
|
|
|
|
|
*
|
|
|
|
|
* @return int|null
|
|
|
|
|
*/
|
|
|
|
|
public function request_limit(): ?int
|
|
|
|
|
{
|
|
|
|
|
return request('length', $this->default_length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the limit value of the current request.
|
|
|
|
|
*
|
|
|
|
|
* @return int|null
|
|
|
|
|
*/
|
|
|
|
|
public function request_offset(): ?int
|
|
|
|
|
{
|
|
|
|
|
$page = request('page', 1);
|
|
|
|
|
|
|
|
|
|
$length = request('length', $this->default_length);
|
|
|
|
|
|
|
|
|
|
return ($page - 1) * $length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the order by value of the current request.
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function request_order_by(): ?string
|
|
|
|
|
{
|
|
|
|
|
$sort = request('sort');
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$sort) {
|
|
|
|
|
return null;
|
2021-11-02 12:44:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-03 10:21:30 +03:00
|
|
|
|
$sort_tokens = array_map('trim', explode(',', $sort));
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
|
|
|
|
$order_by = [];
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($sort_tokens as $sort_token) {
|
2021-11-03 10:21:30 +03:00
|
|
|
|
$api_field = substr($sort_token, 1);
|
|
|
|
|
|
2021-11-06 15:30:12 +03:00
|
|
|
|
$direction_operator = substr($sort_token, 0, 1);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!in_array($direction_operator, ['-', '+'])) {
|
2023-01-23 09:54:06 +03:00
|
|
|
|
$direction_operator = '+';
|
|
|
|
|
$api_field = $sort_token;
|
2021-11-06 15:30:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 09:54:06 +03:00
|
|
|
|
$db_field = $this->model->db_field($api_field);
|
|
|
|
|
|
2021-11-06 15:30:12 +03:00
|
|
|
|
$direction = $direction_operator === '-' ? 'DESC' : 'ASC';
|
2021-11-02 12:44:37 +03:00
|
|
|
|
|
2021-11-03 10:21:30 +03:00
|
|
|
|
$order_by[] = $db_field . ' ' . $direction;
|
2021-11-02 12:44:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode(', ', $order_by);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 17:34:54 +03:00
|
|
|
|
* Get the chosen "fields" array of the current request.
|
2021-11-02 12:44:37 +03:00
|
|
|
|
*
|
|
|
|
|
* @return array|null
|
|
|
|
|
*/
|
|
|
|
|
public function request_fields(): ?array
|
|
|
|
|
{
|
|
|
|
|
$fields = request('fields');
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$fields) {
|
|
|
|
|
return null;
|
2021-11-02 12:44:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-03 10:21:30 +03:00
|
|
|
|
return array_map('trim', explode(',', $fields));
|
2021-11-02 12:44:37 +03:00
|
|
|
|
}
|
2021-11-06 17:34:54 +03:00
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 17:41:11 +03:00
|
|
|
|
* Get the provided "with" array of the current request.
|
2021-11-06 17:34:54 +03:00
|
|
|
|
*
|
|
|
|
|
* @return array|null
|
|
|
|
|
*/
|
2021-11-06 17:41:11 +03:00
|
|
|
|
public function request_with(): ?array
|
2021-11-06 17:34:54 +03:00
|
|
|
|
{
|
2021-11-06 17:41:11 +03:00
|
|
|
|
$with = request('with');
|
2021-11-06 17:34:54 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$with) {
|
|
|
|
|
return null;
|
2021-11-06 17:34:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 17:41:11 +03:00
|
|
|
|
return array_map('trim', explode(',', $with));
|
2021-11-06 17:34:54 +03:00
|
|
|
|
}
|
2021-11-02 12:44:37 +03:00
|
|
|
|
}
|