-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.php
More file actions
153 lines (122 loc) · 4.43 KB
/
Copy pathstart.php
File metadata and controls
153 lines (122 loc) · 4.43 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
151
152
153
<?php
require_once __DIR__ . "/fn.php";
require_once __DIR__ . "/stop.php";
/**
* Check if a local port is currently in use.
*
* Attempts to open a socket connection to 127.0.0.1 on the given port.
* If the connection succeeds, the port is considered occupied.
*
* @param int $port Port number to check.
*
* @return bool Returns true if the port is in use, false otherwise.
*/
function isPortInUse($port) {
$conn = @fsockopen("127.0.0.1", $port);
if ($conn) {
fclose($conn);
return true;
}
return false;
}
// Default ports
$apachePort = 80;
$mysqlPort = 3306;
$redisPort = 6379;
$dbPath = __DIR__ . '/data/setting.db';
if (file_exists($dbPath) && extension_loaded('pdo_sqlite')) {
try {
$pdo = new PDO('sqlite:' . $dbPath);
$stmt = $pdo->query("SELECT name, value FROM settings WHERE name IN ('apache_port', 'mysql_port', 'redis_port')");
$settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
if (!empty($settings)) {
echo "Reading configuration from setting.db...\n";
$apachePort = isset($settings['apache_port']) ? (int)$settings['apache_port'] : $apachePort;
$mysqlPort = isset($settings['mysql_port']) ? (int)$settings['mysql_port'] : $mysqlPort;
$redisPort = isset($settings['redis_port']) ? (int)$settings['redis_port'] : $redisPort;
}
} catch (PDOException $e) {
echo "[WARNING] Could not read from setting.db: " . $e->getMessage() . "\n";
echo "Falling back to default ports.\n";
}
}
// Prepare replacements for config files
$replacements = [
'{APACHE_PORT}' => $apachePort,
'{MYSQL_PORT}' => $mysqlPort,
'{REDIS_PORT}' => $redisPort,
];
$phpPath = __DIR__ . '/php';
$phpExtPath = __DIR__ . '/php/ext';
$currentPath = getenv('PATH');
$paths = explode(PATH_SEPARATOR, $currentPath);
// Add only if not already present
if (!in_array($phpPath, $paths)) {
$paths[] = $phpPath;
}
if (!in_array($phpExtPath, $paths)) {
$paths[] = $phpExtPath;
}
// Rebuild and set the new PATH
putenv('PATH=' . implode(PATH_SEPARATOR, $paths));
// Ensure necessary directories
ensureDirectory(__DIR__ . "/www");
ensureDirectory(__DIR__ . "/tmp");
ensureDirectory(__DIR__ . "/data");
ensureDirectory(__DIR__ . "/data/mysql");
ensureDirectory(__DIR__ . "/data/redis");
ensureDirectory(__DIR__ . "/logs");
ensureDirectory(__DIR__ . "/sessions");
ensureDirectory(__DIR__ . "/apache/logs");
// Replace config templates
replaceAndWrite(__DIR__ . "/config/httpd-template.conf", __DIR__ . "/config/httpd.conf", $replacements);
replaceAndWrite(__DIR__ . "/config/php-template.ini", __DIR__ . "/php/php.ini", $replacements);
replaceAndWrite(__DIR__ . "/config/my-template.ini", __DIR__ . "/config/my.ini", $replacements);
replaceAndWrite(__DIR__ . "/config/redis.windows-template.conf", __DIR__ . "/redis/redis.windows.conf", $replacements);
replaceAndWrite(__DIR__ . "/config/redis.windows-service-template.conf", __DIR__ . "/redis/redis.windows-service.conf", $replacements);
echo "=== MagicAppBuilder Portable Installer ===\n";
// Set path
$apacheBin = __DIR__ . "/apache/bin/httpd.exe";
$mysqlBin = __DIR__ . "/mysql/bin/mysqld.exe";
$redisBin = __DIR__ . "/redis/redis-server.exe";
// Check dependencies
if (!file_exists($apacheBin)) {
echo "[ERROR] Apache not found!\n";
exit(1);
}
if (!file_exists($mysqlBin)) {
echo "[ERROR] MySQL/MariaDB not found!\n";
exit(1);
}
if (isPortInUse($apachePort)) {
echo "[WARNING] Port $apachePort (Apache) already in use.\n";
}
if (isPortInUse($mysqlPort)) {
echo "[WARNING] Port $mysqlPort (MariaDB) already in use.\n";
}
if (isPortInUse($redisPort)) {
echo "[WARNING] Port $redisPort (Redis) already in use.\n";
}
// Start MySQL
echo "Starting MySQL...\n";
$cmd1 = "start /B \"\" \"" . $mysqlBin . "\" --defaults-file=\"" . __DIR__ . "/config/my.ini"; // NOSONAR
pclose(popen($cmd1, "r"));
// Start Redis
echo "Starting Redis...\n";
$cmd2 = "start /B \"\" \"" . $redisBin . "\" \"" . __DIR__ . "/redis/redis.windows.conf\""; // NOSONAR
pclose(popen($cmd2, "r"));
// Start Apache
echo "Starting Apache...\n";
$cmd3 = "start /B \"\" \"" . $apacheBin . "\" -f \"" . __DIR__ . "/config/httpd.conf\""; // NOSONAR
pclose(popen($cmd3, "r"));
if($apachePort != 80)
{
$url = "http://localhost:$apachePort/MagicAppBuilder/";
}
else
{
$url = "http://localhost/MagicAppBuilder/";
}
echo "DONE. Access your app at $url\n";
$cmd4 = "start $url"; // NOSONAR
pclose(popen($cmd4, "r"));