PHP Backend Architecture
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The PHP Backend Architecture of the Duplicator project serves as the core logic engine for document duplication accounting and management. It is designed as a modular system that operates within an Electron-wrapped environment, utilizing Caddy as a web server to bridge the frontend and the PHP runtime. The backend handles complex tasks including print run calculations, machine maintenance tracking (consumables), and document processing (PDF/Image manipulations).
The architecture follows a Model-View-Controller (MVC) pattern, although implemented with a focus on procedural-to-modular transition. It manages a SQLite or MySQL database to persist machine configurations, pricing, and accounting logs. The backend also includes a robust API layer for serving internal requests from the Electron process, such as system status checks and file management.
Sources: app/public/index.php, main-caddy.js:527-660, README.md
Core System Components
Execution Lifecycle
The backend lifecycle is initiated by the Electron process, which spawns a PHP built-in server or PHP-FPM instance. Every request is routed through app/public/index.php, which acts as the front controller. This file performs initial environment setup, session management, and routing based on URL parameters.
Initialization and Configuration
Initialization is handled by app/controler/functions/init.php and app/controler/func.php. These modules establish global utilities, load i18n translations, and define constants like APP_VERSION and APP_NAME.
| Component | Description | Source |
|---|---|---|
validate_input() | Sanitizes and validates data types (int, float, email, string). | app/controler/functions/init.php:107 |
generate_csrf_token() | Generates 32-byte hex tokens for security. | app/controler/functions/init.php:143 |
pdo_connect() | Established database connection based on conf.php. | app/controler/func.php |
Action($conf) | The primary entry point for model logic. | app/models/admin.php:13 |
Sources: app/controler/functions/init.php, app/controler/func.php, app/models/admin.php
Database Architecture
Data Persistence Layer
The system supports both SQLite and MySQL. In the Electron-packaged version, the backend dynamically resolves the database path to the user's data directory to ensure persistence during application updates. The DatabaseMigrationManager ensures the schema remains current across versions.
Database Management Module
The AdminDatabaseManager (and its SQLite counterpart) provides tools for database maintenance, including creation, renaming, and template application. It can initialize essential tables like active_database, prix, cons, and site_settings.
Sources: app/models/admin/DatabaseManager.php:13-100
API and Routing
Endpoint Handling
The backend serves several specialized endpoints via the front controller. These endpoints are triggered by the key($_GET) value.
- System Check:
?check_ghostscriptverifies PDF processing capabilities. - Database Operations:
?download_backupand?upload_backuphandle portability. - Machine Management:
?ajax_get_last_countersretrieves the most recent meter readings for a specific machine to calculate current print run costs. - PDF Processing:
?download_unimposedand?download_processedserve files generated by the document tools.
Sources: app/public/index.php:1-35, app/public/index.php:100-200, app/models/admin.php:125-155
Sequence: Print Run Meter Retrieval
When a user begins a new print run, the backend retrieves machine-specific meter data to optimize the accounting flow.
Security and Authentication
Admin Access Control
Access to administrative features is governed by a session-based password system. The backend enforces a mandatory password creation step if no active administrator exists in the admin_passwords table.
- Password Hashing: Utilizes
bcryptvia PHP'spassword_hash()andpassword_verify(). - Session Security: Sessions are stored in a cross-platform temporary directory (e.g.,
duplicator_sessions) resolved at runtime. - Secure Routing: The
$page_securearray inindex.phpacts as a whitelist for accessible modules. Any request for a page not in this list results in a403 Forbiddenresponse.
Sources: app/public/index.php:40-60, app/public/index.php:330-400, app/models/admin.php:157-190
Error Handling and Monitoring
The backend implements a global error and exception handler to prevent the application from crashing into a "white screen" state. These handlers capture PHP errors, log them to duplicator_errors.log in the system temp directory, and redirect the user back to the application interface with a standardized error message.
Sources: app/public/index.php:65-100, main-caddy.js:105-150
Conclusion
The PHP Backend Architecture of Duplicator is a robust, self-contained system that integrates tightly with the Electron host environment. By utilizing a modular approach to model management—splitting logic into specific managers for stats, prices, and machines—it provides a maintainable foundation for complex reproduction accounting. Its reliance on standard PHP components like PDO and built-in server capabilities ensures cross-platform compatibility across Windows, Linux, and macOS.
Sources: app/models/admin.php:192-230, main-caddy.js:750-850