Initial commit

This commit is contained in:
Hamza-Ayed
2026-05-21 00:40:47 +03:00
commit 8e429d8313
14 changed files with 1152 additions and 0 deletions

18
backend/.env.example Normal file
View File

@@ -0,0 +1,18 @@
# Application Settings
APP_NAME=Nabeh
APP_ENV=development
APP_DEBUG=true
APP_URL=http://localhost:8000
# Main Master Database Configuration
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=nabeh_master
DB_USERNAME=root
DB_PASSWORD=
# AI Model Configuration
GEMINI_API_KEY=
# Messaging Gateway Settings
WHATSAPP_GATEWAY_URL=http://localhost:3722

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Controllers;
use App\Core\Request;
use App\Core\Response;
/**
* Foundation controller offering model loading, request parsing, and validation helpers.
*/
class BaseController
{
/**
* Validate request payload and return array of errors if any fail
*
* @param Request $request
* @param array $rules List of keys and their validation constraints (e.g. ['email' => 'required|email'])
* @return array Empty if valid, otherwise contains error messages
*/
protected function validate(Request $request, array $rules): array
{
$errors = [];
$data = $request->getBody();
foreach ($rules as $field => $constraints) {
$value = $data[$field] ?? null;
$constraintsArray = explode('|', $constraints);
foreach ($constraintsArray as $constraint) {
if ($constraint === 'required') {
if ($value === null || $value === '') {
$errors[$field][] = "The {$field} field is required.";
}
} elseif ($constraint === 'email') {
if ($value !== null && $value !== '' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
$errors[$field][] = "The {$field} must be a valid email address.";
}
} elseif (strpos($constraint, 'min:') === 0) {
$min = (int) substr($constraint, 4);
if ($value !== null && strlen((string)$value) < $min) {
$errors[$field][] = "The {$field} must be at least {$min} characters.";
}
}
}
}
return $errors;
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Core;
use PDO;
use PDOException;
/**
* PDO Database wrapper using Singleton pattern.
*/
class Database
{
private static ?PDO $instance = null;
/**
* Get active PDO database instance
*
* @return PDO
* @throws PDOException
*/
public static function getConnection(): PDO
{
if (self::$instance === null) {
$host = getenv('DB_HOST') ?: '127.0.0.1';
$port = getenv('DB_PORT') ?: '3306';
$dbName = getenv('DB_DATABASE') ?: 'nabeh_master';
$username = getenv('DB_USERNAME') ?: 'root';
$password = getenv('DB_PASSWORD') ?: '';
$dsn = "mysql:host={$host};port={$port};dbname={$dbName};charset=utf8mb4";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
self::$instance = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
// Log the exact error internally but hide sensitive DSN on production
error_log("Database Connection Error: " . $e->getMessage());
throw new PDOException("Could not connect to the database. Check database settings.");
}
}
return self::$instance;
}
/**
* Shorthand execute statement with parameters
*
* @param string $sql
* @param array $params
* @return \PDOStatement
*/
public static function query(string $sql, array $params = []): \PDOStatement
{
$pdo = self::getConnection();
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
return $stmt;
}
/**
* Retrieve all matching records
*/
public static function select(string $sql, array $params = []): array
{
return self::query($sql, $params)->fetchAll();
}
/**
* Retrieve single matching record
*/
public static function selectOne(string $sql, array $params = [])
{
return self::query($sql, $params)->fetch() ?: null;
}
/**
* Insert record and return last inserted ID
*/
public static function insert(string $sql, array $params = []): string
{
$pdo = self::getConnection();
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
return $pdo->lastInsertId();
}
/**
* Execute generic non-query SQL (Update/Delete) and return affected rows
*/
public static function execute(string $sql, array $params = []): int
{
return self::query($sql, $params)->rowCount();
}
}

86
backend/app/Core/Env.php Normal file
View File

@@ -0,0 +1,86 @@
<?php
namespace App\Core;
/**
* Lightweight secure environment variable loader
*/
class Env
{
/**
* Load environment variables from a file path
*
* @param string $path
* @throws \Exception
*/
public static function load(string $path): void
{
if (!file_exists($path)) {
// Create a default if it doesn't exist to prevent crash, or throw
if (file_exists($path . '.example')) {
copy($path . '.example', $path);
} else {
throw new \Exception("Environment file not found at: {$path}");
}
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$line = trim($line);
// Skip comments and empty lines
if (empty($line) || strpos($line, '#') === 0) {
continue;
}
// Split by the first equals sign
if (strpos($line, '=') !== false) {
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
$value = trim($value);
// Strip surrounding quotes
if (preg_match('/^"(.+)"$/', $value, $matches) || preg_match("/^'(.+)'$/", $value, $matches)) {
$value = $matches[1];
}
// Inject into PHP superglobals and env
putenv("{$key}={$value}");
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
}
}
/**
* Retrieve environment variable with optional default value
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get(string $key, $default = null)
{
$val = getenv($key);
if ($val === false) {
return $default;
}
switch (strtolower($val)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'null':
case '(null)':
return null;
case 'empty':
case '(empty)':
return '';
}
return $val;
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace App\Core;
/**
* Handles HTTP requests, extracting query params, body data, and headers.
*/
class Request
{
private string $method;
private string $path;
private array $queryParams;
private array $bodyParams;
private array $headers;
public function __construct()
{
$this->method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
// Extract clean path without query parameters
$uri = $_SERVER['REQUEST_URI'] ?? '/';
$path = explode('?', $uri)[0];
$this->path = '/' . trim($path, '/');
$this->queryParams = $_GET;
$this->headers = $this->extractHeaders();
$this->bodyParams = $this->parseBody();
}
public function getMethod(): string
{
return $this->method;
}
public function getPath(): string
{
return $this->path;
}
public function getQueryParams(): array
{
return $this->queryParams;
}
public function getQuery(string $key, $default = null)
{
return $this->queryParams[$key] ?? $default;
}
public function getBody(): array
{
return $this->bodyParams;
}
public function get(string $key, $default = null)
{
return $this->bodyParams[$key] ?? ($this->queryParams[$key] ?? $default);
}
public function getHeaders(): array
{
return $this->headers;
}
public function getHeader(string $key, $default = null): ?string
{
$keyLower = strtolower($key);
return $this->headers[$keyLower] ?? $default;
}
private function extractHeaders(): array
{
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[strtolower(str_replace('_', '-', substr($name, 5)))] = $value;
} elseif ($name == 'CONTENT_TYPE') {
$headers['content-type'] = $value;
} elseif ($name == 'CONTENT_LENGTH') {
$headers['content-length'] = $value;
}
}
return $headers;
}
private function parseBody(): array
{
if ($this->method === 'GET') {
return [];
}
$contentType = $this->getHeader('content-type', '');
if (strpos($contentType, 'application/json') !== false) {
$input = file_get_contents('php://input');
$data = json_decode($input, true);
return is_array($data) ? $data : [];
}
return $_POST;
}
}

View File

@@ -0,0 +1,93 @@
<?php
namespace App\Core;
/**
* Handles generating and sending consistent API and HTML responses.
*/
class Response
{
private int $statusCode = 200;
private array $headers = [];
public function setStatusCode(int $code): self
{
$this->statusCode = $code;
return $this;
}
public function getStatusCode(): int
{
return $this->statusCode;
}
public function setHeader(string $name, string $value): self
{
$this->headers[$name] = $value;
return $this;
}
/**
* Send JSON response and terminate execution
*
* @param mixed $data
* @param int $code
* @return void
*/
public function json($data, int $code = 200): void
{
$this->setStatusCode($code);
$this->setHeader('Content-Type', 'application/json; charset=utf-8');
// Setup base CORS headers for our API
$this->setHeader('Access-Control-Allow-Origin', '*');
$this->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
$this->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
$this->sendHeaders();
http_response_code($this->statusCode);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}
/**
* Send success JSON response
*/
public function success(string $message, array $data = [], int $code = 200): void
{
$this->json([
'status' => 'success',
'message' => $message,
'data' => $data
], $code);
}
/**
* Send error JSON response
*/
public function error(string $message, int $code = 400, array $errors = []): void
{
$response = [
'status' => 'error',
'message' => $message
];
if (!empty($errors)) {
$response['errors'] = $errors;
}
$this->json($response, $code);
}
private function sendHeaders(): void
{
if (headers_sent()) {
return;
}
foreach ($this->headers as $name => $value) {
header("{$name}: {$value}");
}
}
}

127
backend/app/Core/Router.php Normal file
View File

@@ -0,0 +1,127 @@
<?php
namespace App\Core;
/**
* Basic regex-based router supporting dynamic parameters, middlewares, and CORS OPTIONS.
*/
class Router
{
private array $routes = [];
private array $globalMiddleware = [];
/**
* Define a GET route
*/
public function get(string $path, $handler, array $middleware = []): void
{
$this->addRoute('GET', $path, $handler, $middleware);
}
/**
* Define a POST route
*/
public function post(string $path, $handler, array $middleware = []): void
{
$this->addRoute('POST', $path, $handler, $middleware);
}
/**
* Define a PUT route
*/
public function put(string $path, $handler, array $middleware = []): void
{
$this->addRoute('PUT', $path, $handler, $middleware);
}
/**
* Define a DELETE route
*/
public function delete(string $path, $handler, array $middleware = []): void
{
$this->addRoute('DELETE', $path, $handler, $middleware);
}
/**
* Add global middleware applied to all routes
*/
public function use($middleware): void
{
$this->globalMiddleware[] = $middleware;
}
private function addRoute(string $method, string $path, $handler, array $middleware): void
{
// Convert path matching expressions: /api/tickets/{id} -> regex
$pattern = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '(?P<$1>[^/]+)', $path);
$pattern = '#^' . $pattern . '$#';
$this->routes[] = [
'method' => $method,
'path' => $path,
'pattern' => $pattern,
'handler' => $handler,
'middleware' => $middleware
];
}
/**
* Match current request and execute middleware and controller action
*/
public function dispatch(Request $request, Response $response): void
{
$method = $request->getMethod();
$path = $request->getPath();
// Handle CORS Preflight Preemptively
if ($method === 'OPTIONS') {
$response->setHeader('Access-Control-Allow-Origin', '*');
$response->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
$response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
$response->setStatusCode(200);
exit;
}
foreach ($this->routes as $route) {
if ($route['method'] === $method && preg_match($route['pattern'], $path, $matches)) {
// Filter named captures from regex match
$params = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
// Run global middleware first
foreach ($this->globalMiddleware as $mw) {
$mwInstance = new $mw();
$mwInstance->handle($request, $response);
}
// Run route specific middleware
foreach ($route['middleware'] as $mw) {
$mwInstance = new $mw();
$mwInstance->handle($request, $response);
}
// Execute Controller
$handler = $route['handler'];
if (is_array($handler) && count($handler) === 2) {
list($controllerClass, $action) = $handler;
if (class_exists($controllerClass)) {
$controller = new $controllerClass();
if (method_exists($controller, $action)) {
// Call action with Request, Response and URI dynamic parameters
call_user_func_array([$controller, $action], array_merge([$request, $response], $params));
return;
}
}
} elseif (is_callable($handler)) {
call_user_func_array($handler, array_merge([$request, $response], $params));
return;
}
$response->error("Handler error for route: {$path}", 500);
return;
}
}
// Route not found
$response->error("Route not found: [{$method}] {$path}", 404);
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Models;
use App\Core\Database;
/**
* Base database model implementing generic active-record CRUD routines.
*/
abstract class BaseModel
{
protected static string $table = '';
protected static string $primaryKey = 'id';
/**
* Retrieve all records
*/
public static function all(): array
{
$table = static::$table;
return Database::select("SELECT * FROM {$table}");
}
/**
* Find a record by its primary key
*
* @param mixed $id
* @return array|null
*/
public static function find($id): ?array
{
$table = static::$table;
$primaryKey = static::$primaryKey;
return Database::selectOne("SELECT * FROM {$table} WHERE {$primaryKey} = :id LIMIT 1", ['id' => $id]);
}
/**
* Insert a new record
*
* @param array $data Assocation array of columns and values
* @return string Last inserted primary key ID
*/
public static function create(array $data): string
{
$table = static::$table;
$columns = implode(', ', array_keys($data));
$placeholders = ':' . implode(', :', array_keys($data));
$sql = "INSERT INTO {$table} ({$columns}) VALUES ({$placeholders})";
return Database::insert($sql, $data);
}
/**
* Update an existing record
*
* @param mixed $id Primary key ID
* @param array $data Associative array of changes
* @return int Affected rows count
*/
public static function update($id, array $data): int
{
$table = static::$table;
$primaryKey = static::$primaryKey;
$sets = [];
foreach (array_keys($data) as $column) {
$sets[] = "{$column} = :{$column}";
}
$setSql = implode(', ', $sets);
$sql = "UPDATE {$table} SET {$setSql} WHERE {$primaryKey} = :_id";
$params = $data;
$params['_id'] = $id;
return Database::execute($sql, $params);
}
/**
* Delete record by ID
*/
public static function delete($id): int
{
$table = static::$table;
$primaryKey = static::$primaryKey;
$sql = "DELETE FROM {$table} WHERE {$primaryKey} = :id";
return Database::execute($sql, ['id' => $id]);
}
}

48
backend/app/bootstrap.php Normal file
View File

@@ -0,0 +1,48 @@
<?php
/**
* Nabeh Application Bootstrap Loader
* Handles PSR-4 Autoloading, security settings, and error handling.
*/
// Define absolute path to application root
define('APP_ROOT', dirname(__DIR__));
// 1. PSR-4 Autoloader
spl_autoload_register(function ($class) {
// Namespace prefix
$prefix = 'App\\';
// Directory mapping for the prefix
$base_dir = APP_ROOT . '/app/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return; // Move to next registered autoloader
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
// 2. Load Environment Variables
try {
\App\Core\Env::load(APP_ROOT . '/.env');
} catch (\Exception $e) {
// In production, log error; in development, print it
error_log('Env Load Error: ' . $e->getMessage());
}
// 3. Configure Error Reporting based on environment
$isDebug = filter_var(getenv('APP_DEBUG') ?: true, FILTER_VALIDATE_BOOLEAN);
if ($isDebug) {
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
} else {
ini_set('display_errors', '0');
error_reporting(0);
}

18
backend/composer.json Normal file
View File

@@ -0,0 +1,18 @@
{
"name": "nabeh/backend",
"description": "Nabeh Customer Service AI Platform Backend API",
"type": "project",
"license": "proprietary",
"require": {
"php": ">=8.1"
},
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
"config": {
"optimize-autoloader": true,
"sort-packages": true
}
}

34
backend/public/index.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
/**
* Nabeh API Front Controller
* Single entry point handling routing and application bootstrap.
*/
// 1. Boot the application (autoloader, env, errors)
require_once dirname(__DIR__) . '/app/bootstrap.php';
use App\Core\Request;
use App\Core\Response;
use App\Core\Router;
// 2. Initialize request and response objects
$request = new Request();
$response = new Response();
$router = new Router();
// 3. Define basic routes
$router->get('/api/health', function ($request, $response) {
$response->json([
'status' => 'success',
'message' => 'Nabeh API is healthy',
'details' => [
'app_name' => getenv('APP_NAME') ?: 'Nabeh',
'environment' => getenv('APP_ENV') ?: 'development',
'php_version' => PHP_VERSION,
'time' => date('Y-m-d H:i:s')
]
]);
});
// 4. Dispatch the request
$router->dispatch($request, $response);