-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.php
More file actions
143 lines (121 loc) · 3.54 KB
/
tools.php
File metadata and controls
143 lines (121 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Tools and Utilities
*
* Common functions and constants used across the Body Refactoring application.
*
* @package BodyRefactoring
*/
/**
* Get the application version from composer.json.
*
* @return string The version number.
*/
function get_app_version(): string {
static $version = null;
if ( $version === null ) {
$composer_file = __DIR__ . '/composer.json';
if ( file_exists( $composer_file ) ) {
$composer_data = json_decode( file_get_contents( $composer_file ), true );
$version = $composer_data['version'] ?? '0.0.0';
} else {
$version = '0.0.0';
}
}
return $version;
}
/**
* Load environment variables from a .env file.
*
* @param string $path Path to the .env file.
*/
function load_env( string $path ): void {
if ( ! file_exists( $path ) ) {
http_response_code( 500 );
die( 'ERROR: .env file not found' );
}
$lines = file( $path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
foreach ( $lines as $line ) {
$line = trim( $line );
// Skip comments
if ( strpos( $line, '#' ) === 0 ) {
continue;
}
// Parse KEY=VALUE
if ( strpos( $line, '=' ) !== false ) {
list( $name, $value ) = explode( '=', $line, 2 );
$name = trim( $name );
$value = trim( $value );
if ( ! array_key_exists( $name, $_ENV ) ) {
putenv( "$name=$value" );
$_ENV[ $name ] = $value;
}
}
}
}
load_env( __DIR__ . '/.env' );
// Load config system
require_once __DIR__ . '/includes/config-loader.php';
// Define version constant
define( 'APP_VERSION', get_app_version() );
define( 'MODE_RESET_PASSWORD', getenv( 'RESET_PASSWORD_MODE' ) );
define( 'DEBUG_LOG_ENABLED', getenv( 'DEBUG_MODE' ) === 'true' );
// App customization - from config with .env fallback
define( 'APP_NAME', get_app_name() );
define( 'APP_ICON', get_app_icon() );
// Server paths and auth (always from .env)
define( 'SCHEDULE_PATH', getenv( 'SCHEDULE_PATH' ) ?: 'schedules' );
define( 'APP_PASSWORD_HASH', getenv( 'APP_PASSWORD_HASH' ) ?: '' );
define( 'SESSION_DURATION', (int) ( getenv( 'SESSION_DURATION' ) ?: 24966000 ) );
// Auth cookie name (derived from app name for multi-instance support)
define( 'AUTH_COOKIE_NAME', 'br_auth_' . substr( md5( APP_NAME ), 0, 8 ) );
/**
* Check if authentication is enabled.
*
* @return bool True if APP_PASSWORD_HASH is set.
*/
function is_auth_enabled(): bool {
return ! empty( APP_PASSWORD_HASH );
}
/**
* Check if user is authenticated.
*
* @return bool True if user has valid auth cookie.
*/
function is_authenticated(): bool {
if ( ! is_auth_enabled() ) {
return true; // No auth required
}
if ( ! isset( $_COOKIE[ AUTH_COOKIE_NAME ] ) ) {
return false;
}
// Cookie should contain a hash that matches our password hash
$expected_token = substr( hash( 'sha256', APP_PASSWORD_HASH ), 0, 32 );
return hash_equals( $expected_token, $_COOKIE[ AUTH_COOKIE_NAME ] );
}
/**
* Verify a password against the stored hash.
*
* @param string $password The password to verify.
* @return bool True if password is correct.
*/
function verify_password( string $password ): bool {
if ( ! is_auth_enabled() ) {
return false;
}
return password_verify( $password, APP_PASSWORD_HASH );
}
/**
* Set the authentication cookie.
*/
function set_auth_cookie(): void {
$token = substr( hash( 'sha256', APP_PASSWORD_HASH ), 0, 32 );
$expires = time() + SESSION_DURATION;
setcookie( AUTH_COOKIE_NAME, $token, $expires, '/', '', true, true );
}
/**
* Clear the authentication cookie (logout).
*/
function clear_auth_cookie(): void {
setcookie( AUTH_COOKIE_NAME, '', time() - 3600, '/', '', true, true );
}