forked from infosave2007/phpblockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat_functions.php
More file actions
90 lines (79 loc) · 3.19 KB
/
Copy pathcompat_functions.php
File metadata and controls
90 lines (79 loc) · 3.19 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
<?php
/**
* Global compatibility functions for legacy code
* This file provides backward compatibility for functions that may be called
* from various parts of the application
*/
if (!function_exists('getpdofromconfig')) {
/**
* Get PDO connection from configuration
* Compatibility function for legacy code
*/
function getpdofromconfig(array $config = null): PDO {
// Try to load configuration if not provided
if ($config === null) {
$configPaths = [
__DIR__ . '/config/install_config.json',
__DIR__ . '/config/config.json'
];
foreach ($configPaths as $configPath) {
if (file_exists($configPath)) {
$config = json_decode(file_get_contents($configPath), true);
if ($config) {
break;
}
}
}
if (!$config) {
throw new Exception('No configuration found for database connection');
}
}
// Extract database settings from various config formats
$host = $config['db_host'] ?? $config['database']['host'] ?? 'localhost';
$port = $config['db_port'] ?? $config['database']['port'] ?? 3306;
$username = $config['db_username'] ?? $config['database']['username'] ?? '';
$password = $config['db_password'] ?? $config['database']['password'] ?? '';
$database = $config['db_name'] ?? $config['database']['database'] ?? '';
if (empty($username)) {
throw new Exception('Database username is required');
}
if (empty($database)) {
throw new Exception('Database name is required');
}
// Try to use DatabaseManager first if available
if (class_exists('\\Blockchain\\Core\\Database\\DatabaseManager')) {
try {
return \Blockchain\Core\Database\DatabaseManager::getInstallerConnection([
'db_host' => $host,
'db_port' => $port,
'db_name' => $database,
'db_username' => $username,
'db_password' => $password
]);
} catch (Exception $e) {
// Fall back to manual connection
}
}
// Create PDO connection manually
$dsn = "mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4";
try {
$pdo = new PDO($dsn, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_TIMEOUT => 10,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4",
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
return $pdo;
} catch (PDOException $e) {
throw new Exception('Database connection failed: ' . $e->getMessage());
}
}
}
if (!function_exists('getPdoFromConfig')) {
/**
* Alternative naming for getpdofromconfig
*/
function getPdoFromConfig(array $config = null): PDO {
return getpdofromconfig($config);
}
}