-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-controller.js
408 lines (347 loc) · 11.8 KB
/
php-controller.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
const fs = require("fs");
const path = require("path");
const { exec, execSync } = require("child_process");
const colors = require("colors");
const Table = require("cli-table3");
const { reloadNginx } = require("./nginx");
const { nginxServersDirectory, logsDirectory } = require("./constants");
const inquirer = require("inquirer");
colors.enable();
// Specify the path to the folder containing PHP versions
const { phpVersionsPath } = require("./constants.js");
function getPhpVersions() {
const versions = [];
// Read the contents of the PHP folder
const phpVersionFolders = fs.readdirSync(phpVersionsPath);
for (const phpVersion of phpVersionFolders) {
const versionPath = path.join(phpVersionsPath, phpVersion);
// Check if it's a directory
if (fs.statSync(versionPath).isDirectory()) {
let wwwConfPath = path.join(versionPath, "php-fpm.d/www.conf");
let phpIniPath = path.join(versionPath, "php.ini");
// Check if it's PHP 5.6
if ("5.6" === phpVersion) {
wwwConfPath = path.join(versionPath, "php-fpm.conf");
phpIniPath = path.join(versionPath, "php.ini");
}
try {
const wwwConfData = fs.readFileSync(wwwConfPath, "utf8");
// Extract relevant information from www.conf
const listenMatch = wwwConfData.match(/listen\s+(.+?)(\s|;)/);
const userMatch = wwwConfData.match(/user\s+(.+?)(\s|;)/);
if (listenMatch && userMatch) {
const versionInfo = {
version: phpVersion,
configFile: wwwConfPath,
iniFile: phpIniPath,
fpmPort: parseListenDirective(wwwConfData),
userData: parseUserDirective(wwwConfData),
};
versions.push(versionInfo);
}
} catch (err) {
console.error(
`Error processing PHP version ${phpVersion}: ${err.message}`
);
}
}
}
return versions;
}
exports.getPhpVersions = getPhpVersions;
function displayTable(phpFPMInfo) {
const table = new Table({
head: [
"Version".cyan,
"Running".cyan,
"Listen".cyan,
"Config File".cyan,
".ini File".cyan,
],
});
phpFPMInfo.forEach((info) => {
table.push([
info.version,
info.isRunning,
info.fpmPort,
info.configFile,
info.iniFile,
]);
});
console.log(table.toString());
}
function getRunningPHPFPMVersions() {
return new Promise((resolve, reject) => {
// Initialize an array to track running PHP-FPM versions
const runningPHPFPMVersions = [];
// Run `ps aux | grep php-fpm` to get running PHP-FPM processes
exec("ps aux | grep php-fpm", (error, stdout, stderr) => {
if (error || stderr) {
reject(error || stderr);
return;
}
const lines = stdout.split("\n");
lines.forEach((line) => {
// Try to match the format "[email protected]"
const match = line.match(/php@(\d+\.\d+)/);
// If not matched, try to match the format "php-fpm: master process ({phpVersionsPath}/X.X/php-fpm.conf)"
if (!match) {
const masterMatch = line.match(
/php-fpm: master process \((\/[^)]+)\/php\/(\d+\.\d+)\/php-fpm.conf\)/
);
if (masterMatch) {
const version = masterMatch[2];
runningPHPFPMVersions.push(version);
}
} else {
const version = match[1];
runningPHPFPMVersions.push(version);
}
});
resolve(runningPHPFPMVersions);
});
});
}
exports.getRunningPHPFPMVersions = getRunningPHPFPMVersions;
async function listPHPFPMInfo() {
const phpFPMInfo = [];
// Get a list of PHP version folders
const phpVersionData = getPhpVersions();
try {
const runningPHPFPMVersions = await getRunningPHPFPMVersions();
phpVersionData.forEach((versionData) => {
const isRunning = runningPHPFPMVersions.includes(versionData.version);
phpFPMInfo.push({
...versionData,
isRunning: isRunning ? "Yes".blue : "No",
});
});
displayTable(phpFPMInfo);
} catch (error) {
console.error(`Error getting running PHP-FPM versions: ${error}`);
}
}
exports.listPHPFPMInfo = listPHPFPMInfo;
function parseListenDirective(wwwConfData) {
const listenMatch = wwwConfData.match(/listen\s*=\s*[^:]+:(\d+)/);
return listenMatch ? listenMatch[1].trim() : "N/A";
}
function parseUserDirective(wwwConfData) {
const userMatch = wwwConfData.match(
/user\s*=\s*([^;]+)\s*\n\s*group\s*=\s*([^;]+);/
);
return userMatch
? [userMatch[1].trim(), userMatch[2].trim()]
: ["N/A", "N/A"];
}
function testAddPHP(version) {
exec(`brew list | grep php@${version}`, (error, stdout, stderr) => {
if (error) {
console.error(
`Error checking if PHP ${version} is installed: ${error.message}`
);
return;
}
if (stdout) {
console.log(`PHP ${version} is already installed.`);
} else {
console.log(`Installing PHP ${version}...`);
exec(
`brew install php@${version}`,
(installError, installStdout, installStderr) => {
if (installError) {
console.error(
`Error installing PHP ${version}: ${installError.message}`
);
} else {
console.log(`PHP ${version} has been successfully installed.`);
}
}
);
}
});
}
exports.testAddPHP = testAddPHP;
function uninstallPhpVersion(version) {
exec(`brew list | grep php@${version}`, (error, stdout, stderr) => {
if (error) {
console.error(
`Error checking if PHP ${version} is installed: ${error.message}`
);
return;
}
if (stdout) {
console.log(`Uninstalling PHP ${version}...`);
exec(
`brew uninstall php@${version}`,
(uninstallError, uninstallStdout, uninstallStderr) => {
if (uninstallError) {
console.error(
`Error uninstalling PHP ${version}: ${uninstallError.message}`
);
} else {
console.log(`PHP ${version} has been successfully uninstalled.`);
}
}
);
} else {
console.log(`PHP ${version} is not installed.`);
}
});
}
exports.uninstallPhpVersion = uninstallPhpVersion;
function modifyPhpConfig(configPath, fpmPort) {
try {
const wwwConfData = fs.readFileSync(configPath, "utf8");
// Automatically get the current user using 'whoami'
const user = execSync("whoami", { encoding: "utf8" }).trim();
// Replace the user and listen directives
const updatedConfData = wwwConfData
.replace(/user\s*=\s*[^\n]+/, `user = ${user}`)
.replace(
/listen\s*=\s*127\.0\.0\.1:\d+/,
`listen = 127.0.0.1:${fpmPort}`
);
// Write the updated configuration back to the file
fs.writeFileSync(configPath, updatedConfData, "utf8");
console.log(
`PHP-FPM configuration updated with user ${user} and port ${fpmPort}.`
);
} catch (err) {
console.error(`Error modifying PHP-FPM configuration: ${err.message}`);
}
}
exports.modifyPhpConfig = modifyPhpConfig;
function restartPhpService(version) {
exec(`brew services restart php@${version}`, (error, stdout, stderr) => {
if (error) {
console.error(
`Error restarting PHP ${version} service: ${error.message}`
);
return;
}
console.log(`PHP ${version} service has been successfully restarted.`);
});
}
exports.restartPhpService = restartPhpService;
function PhpDoctor() {
const phpVersions = getPhpVersions();
let portNumber = 9020;
for (const versionInfo of phpVersions) {
const { version, configFile } = versionInfo;
const fpmPort = portNumber.toString();
// Modify PHP-FPM configuration with the current port
modifyPhpConfig(configFile, fpmPort);
// Increment the port number for the next iteration
portNumber++;
}
}
exports.PhpDoctor = PhpDoctor;
function startPhpService(version) {
exec(`brew services start php@${version}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error starting PHP ${version} service: ${error.message}`);
return;
}
console.log(`PHP ${version} service has been successfully started.`);
});
}
exports.startPhpService = startPhpService;
function stopPhpService(version) {
exec(`brew services stop php@${version}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error stopping PHP ${version} service: ${error.message}`);
return;
}
console.log(`PHP ${version} service has been successfully stopped.`);
});
}
exports.stopPhpService = stopPhpService;
function getPhpVersionFromNginxConfig(nginxConfigPath) {
const nginxConfig = fs.readFileSync(nginxConfigPath, "utf8");
const phpVersions = getPhpVersions();
let matchedVersion = null;
// Regular expression to match the PHP-FPM fastcgi_pass directive
const regex = /fastcgi_pass\s+127\.0\.0\.1:(\d+);/;
const match = nginxConfig.match(regex);
if (match && match[1]) {
const portNumber = match[1];
// Find the PHP version that matches the port number
matchedVersion = phpVersions.find(
(versionInfo) => versionInfo["fpmPort"] === portNumber
);
if (!matchedVersion) {
return `No PHP version matched with FPM Port ${portNumber}`;
}
return matchedVersion.version;
}
return "N/A";
}
exports.getPhpVersionFromNginxConfig = getPhpVersionFromNginxConfig;
async function changePHP(configFilePath, phpVersion) {
try {
// Read the Nginx configuration file
let nginxConfig = fs.readFileSync(configFilePath, "utf8");
// Get the PHP version's FPM Port based on the provided PHP version string
const phpVersions = getPhpVersions(); // Replace with the actual implementation of getPhpVersions
const phpVersionInfo = phpVersions.find(
(versionInfo) => versionInfo.version === phpVersion
);
if (!phpVersionInfo) {
console.error(`PHP version ${phpVersion} not found in PHP versions.`);
return;
}
const newPort = phpVersionInfo["fpmPort"];
// Replace the PHP-FPM port number in the Nginx configuration
const regex = /fastcgi_pass\s+127\.0\.0\.1:(\d+);/;
nginxConfig = nginxConfig.replace(
regex,
`fastcgi_pass 127.0.0.1:${newPort};`
);
// Write the updated configuration back to the file
fs.writeFileSync(configFilePath, nginxConfig, "utf8");
await reloadNginx();
console.log(
`Updated PHP-FPM port to ${newPort} in Nginx config for PHP version ${phpVersion}.`
);
} catch (err) {
console.error(`Error updating Nginx configuration: ${err.message}`);
}
}
exports.changePHP = changePHP;
async function selectPHP(siteName) {
try {
const phpVersions = getPhpVersions();
const choices = phpVersions.map((php) => ({
name: php.version,
value: php.version,
}));
const answers = await inquirer.prompt([
{
type: "list",
name: "phpVersion",
message: "Select a PHP version:",
choices: choices,
},
]);
const phpVersion = answers.phpVersion;
await changePHP(`${nginxServersDirectory}/${siteName}.conf`, phpVersion);
console.log("Done.");
} catch (error) {
console.error(`Error setting PHP: ${error.stack}`.red);
}
}
exports.selectPHP = selectPHP;
function addXdebugConfiguration(phpIniPath) {
const configToAdd = `[xdebug]\nzend_extension="xdebug.so"\nxdebug.mode=debug,develop\nxdebug.log = ${logsDirectory}/xdebug.log\nxdebug.remote_log = ${logsDirectory}/xdebug.log\nxdebug.client_port=9003\nxdebug.start_with_request=yes`;
// Check if the configuration already exists in php.ini
const existingConfig = fs.readFileSync(phpIniPath, "utf8");
if (existingConfig.indexOf("[xdebug]") === -1) {
// Append the new configuration to the php.ini file
fs.appendFileSync(phpIniPath, configToAdd);
console.log("Added xdebug configuration to php.ini.");
} else {
console.log("xdebug configuration already exists in php.ini.");
}
}
exports.addXdebugConfiguration = addXdebugConfiguration;