-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-handler.php
More file actions
150 lines (123 loc) · 4.19 KB
/
install-handler.php
File metadata and controls
150 lines (123 loc) · 4.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
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
144
145
146
147
148
149
150
<?php
/**
* Installation Handler
* Processes installation steps via AJAX
*/
header('Content-Type: application/json');
$action = $_GET['action'] ?? '';
switch ($action) {
case 'check_requirements':
checkRequirements();
break;
case 'install_database':
installDatabase();
break;
default:
echo json_encode(['error' => 'Invalid action']);
}
function checkRequirements() {
$phpVersion = phpversion();
$phpOk = version_compare($phpVersion, '7.4.0', '>=');
$mysqlOk = extension_loaded('mysqli') || extension_loaded('pdo_mysql');
$pdoOk = extension_loaded('pdo');
$curlOk = extension_loaded('curl');
$jsonOk = extension_loaded('json');
$allOk = $phpOk && $mysqlOk && $pdoOk && $curlOk && $jsonOk;
echo json_encode([
'php_version' => $phpVersion,
'php_ok' => $phpOk,
'mysql_ok' => $mysqlOk,
'pdo_ok' => $pdoOk,
'curl_ok' => $curlOk,
'json_ok' => $jsonOk,
'all_ok' => $allOk
]);
}
function installDatabase() {
try {
$host = $_POST['db_host'] ?? 'localhost';
$port = $_POST['db_port'] ?? 3306;
$dbname = $_POST['db_name'] ?? 'rankmath_webapp';
$username = $_POST['db_user'] ?? 'root';
$password = $_POST['db_pass'] ?? '';
$prefix = $_POST['db_prefix'] ?? 'rm_';
// Connect to MySQL (without database first)
$dsn = "mysql:host=$host;port=$port;charset=utf8mb4";
$pdo = new PDO($dsn, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
// Create database if not exists
$pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbname` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
$pdo->exec("USE `$dbname`");
// Read and execute schema file
$schema = file_get_contents(__DIR__ . '/database/schema.sql');
// Replace prefix placeholder
$schema = str_replace('rm_', $prefix, $schema);
// Split into individual queries and execute
$queries = array_filter(array_map('trim', explode(';', $schema)));
foreach ($queries as $query) {
if (!empty($query)) {
$pdo->exec($query);
}
}
// Update config file
$configPath = __DIR__ . '/config/database.php';
$configContent = <<<PHP
<?php
/**
* Database Configuration
* Generated during installation
*/
return [
'host' => '$host',
'port' => $port,
'database' => '$dbname',
'username' => '$username',
'password' => '$password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '$prefix',
'options' => [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]
];
PHP;
file_put_contents($configPath, $configContent);
// Create .htaccess for Apache
$htaccess = <<<HTACCESS
# RankMath SEO Webapp
RewriteEngine On
# Redirect to install.php if not installed
RewriteCond %{REQUEST_URI} !^/rankmath/webapp/install.php
RewriteCond %{REQUEST_URI} !^/rankmath/webapp/install-handler.php
RewriteCond %{REQUEST_URI} !^/rankmath/webapp/assets/
RewriteCond %{DOCUMENT_ROOT}/rankmath/webapp/config/database.php !-f
RewriteRule ^(.*)$ install.php [L]
# API Routes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ api.php [L,QSA]
# Frontend Routes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
HTACCESS;
file_put_contents(__DIR__ . '/.htaccess', $htaccess);
echo json_encode([
'success' => true,
'message' => 'Database installed successfully!'
]);
} catch (PDOException $e) {
echo json_encode([
'success' => false,
'message' => 'Database error: ' . $e->getMessage()
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => 'Installation error: ' . $e->getMessage()
]);
}
}