86 lines
2.7 KiB
PHP
86 lines
2.7 KiB
PHP
<?php
|
|
// api/ping.php — Server diagnostics endpoint
|
|
// Visit this URL directly in your browser to check if PHP + SQLite are working
|
|
// e.g. http://yourdomain.com/chat/api/ping.php
|
|
|
|
header('Content-Type: application/json');
|
|
header('Cache-Control: no-store');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
require_once dirname(__DIR__) . '/bootstrap.php';
|
|
|
|
$results = [];
|
|
|
|
// 1. PHP version
|
|
$results['php_version'] = PHP_VERSION;
|
|
$results['php_ok'] = version_compare(PHP_VERSION, '8.1.0', '>=');
|
|
|
|
// 2. PDO SQLite
|
|
$results['pdo_sqlite'] = extension_loaded('pdo_sqlite');
|
|
|
|
// 3. ROOT_DIR and config.json
|
|
$root = ROOT_DIR;
|
|
$configFile = $root . '/config.json';
|
|
$results['config_exists'] = file_exists($configFile);
|
|
$results['config_writable'] = is_writable($configFile);
|
|
|
|
if ($results['config_exists']) {
|
|
$raw = file_get_contents($configFile);
|
|
$cfg = json_decode($raw, true);
|
|
$results['config_valid_json'] = ($cfg !== null);
|
|
} else {
|
|
$results['config_valid_json'] = false;
|
|
}
|
|
|
|
// 4. db/ directory writable
|
|
$dbPath = storagePath((string)getConfigVal('database.main_db', 'db/chat.sqlite'));
|
|
$dbDir = dirname($dbPath);
|
|
if (!is_dir($dbDir)) {
|
|
@mkdir($dbDir, 0775, true);
|
|
}
|
|
$results['db_dir_exists'] = is_dir($dbDir);
|
|
$results['db_dir_writable'] = is_writable($dbDir);
|
|
$results['db_file_writable'] = !is_file($dbPath) || is_writable($dbPath);
|
|
|
|
// 5. archive/ directory writable
|
|
$archiveDir = storagePath((string)getConfigVal('archive.archive_dir', 'archive'));
|
|
if (!is_dir($archiveDir)) {
|
|
@mkdir($archiveDir, 0775, true);
|
|
}
|
|
$results['archive_dir_exists'] = is_dir($archiveDir);
|
|
$results['archive_dir_writable'] = is_writable($archiveDir);
|
|
|
|
// 6. Try creating SQLite DB
|
|
$results['sqlite_create'] = false;
|
|
$results['sqlite_error'] = null;
|
|
if ($results['pdo_sqlite'] && $results['db_dir_writable']) {
|
|
try {
|
|
$testDb = new PDO('sqlite:' . $dbPath);
|
|
$testDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
$testDb->exec('PRAGMA journal_mode=WAL');
|
|
$testDb->exec('CREATE TABLE IF NOT EXISTS _ping_test (id INTEGER PRIMARY KEY)');
|
|
$results['sqlite_create'] = true;
|
|
} catch (Exception $e) {
|
|
$results['sqlite_error'] = $e->getMessage();
|
|
}
|
|
}
|
|
|
|
// 7. Session support
|
|
$results['sessions_available'] = function_exists('session_start');
|
|
|
|
// 8. Overall status
|
|
$results['all_ok'] = (
|
|
$results['php_ok'] &&
|
|
$results['pdo_sqlite'] &&
|
|
$results['config_exists'] &&
|
|
$results['config_valid_json'] &&
|
|
$results['db_dir_writable'] &&
|
|
$results['db_file_writable'] &&
|
|
$results['sqlite_create']
|
|
);
|
|
|
|
$results['server_time'] = date('Y-m-d H:i:s T');
|
|
|
|
http_response_code($results['all_ok'] ? 200 : 500);
|
|
echo json_encode($results, JSON_PRETTY_PRINT);
|