Skip to content

Commit b2d3f37

Browse files
author
Taois
committed
feat: php验证优化
确保二进制权限
1 parent 67704bf commit b2d3f37

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

libs/php.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {execFile} from 'child_process';
55
import {promisify} from 'util';
66
import {getSitesMap} from "../utils/sites-map.js";
77
import {computeHash, deepCopy, getNowTime, urljoin} from "../utils/utils.js";
8+
import {prepareBinary} from "../utils/binHelper.js";
89
import {md5} from "../libs_drpy/crypto-util.js";
910
import {fastify} from "../controllers/fastlogger.js";
1011
// import dotenv from 'dotenv';
@@ -51,7 +52,14 @@ function json2Object(json) {
5152

5253
// Execute PHP bridge
5354
const callPhpMethod = async (filePath, methodName, env, ...args) => {
54-
const phpPath = process.env.PHP_PATH || 'php';
55+
let phpPath = process.env.PHP_PATH || 'php';
56+
57+
const validPath = prepareBinary(phpPath);
58+
if (!validPath) {
59+
throw new Error(`PHP executable not found or invalid: ${phpPath}`);
60+
}
61+
phpPath = validPath;
62+
5563
const phpMethodName = methodMapping[methodName] || methodName;
5664

5765
const cliArgs = [

spider/php/config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
if (in_array($file, ['index.php', 'spider.php', 'example_t4.php', 'test_runner.php']) ||
4343
$file === $self ||
4444
strpos($file, '_') === 0 ||
45-
fnmatch('config*.php', $file)) {
45+
fnmatch('config*.php', $file) ||
46+
stripos($file, 'test') !== false) {
4647
continue;
4748
}
4849

utils/binHelper.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
/**
5+
* 确保文件具有执行权限 (Linux/macOS)
6+
* @param {string} filePath 文件绝对路径
7+
*/
8+
export function ensureExecutable(filePath) {
9+
if (process.platform === "win32") {
10+
// Windows 不需要 chmod,直接返回
11+
return;
12+
}
13+
try {
14+
if (!fs.existsSync(filePath)) {
15+
return;
16+
}
17+
const stats = fs.statSync(filePath);
18+
if (!(stats.mode & 0o111)) {
19+
fs.chmodSync(filePath, 0o755);
20+
console.log(`[binHelper] 已为文件 ${filePath} 添加执行权限`);
21+
}
22+
} catch (err) {
23+
console.error(`[binHelper] 无法设置执行权限: ${filePath}`, err.message);
24+
}
25+
}
26+
27+
/**
28+
* 检查并准备二进制文件(检查存在性 + 赋予权限)
29+
* @param {string} binPath 二进制文件路径或命令
30+
* @returns {string|null} 如果是现有文件路径返回路径,如果是全局命令返回原命令,如果文件不存在返回 null
31+
*/
32+
export function prepareBinary(binPath) {
33+
if (!binPath) return null;
34+
35+
// 如果不包含路径分隔符,假定是全局命令(如 'php', 'node'),直接返回
36+
// 注意:这里简单判断,如果用户写 ./php 或 /usr/bin/php 都会进入下面的 exist 检查
37+
if (!binPath.includes('/') && !binPath.includes('\\')) {
38+
return binPath;
39+
}
40+
41+
// 如果是路径,检查是否存在
42+
if (fs.existsSync(binPath)) {
43+
ensureExecutable(binPath);
44+
return binPath;
45+
}
46+
47+
// 路径不存在
48+
return null;
49+
}

utils/phpEnv.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
import { execFile } from 'child_process';
22
import { promisify } from 'util';
3+
import { prepareBinary } from './binHelper.js';
34

45
const execFileAsync = promisify(execFile);
56

67
export let isPhpAvailable = false;
78

89
export const checkPhpAvailable = async () => {
9-
const phpPath = process.env.PHP_PATH || 'php';
10+
let phpPath = process.env.PHP_PATH || 'php';
11+
12+
// Check existence and permissions
13+
const validPath = prepareBinary(phpPath);
14+
if (!validPath) {
15+
console.warn(`⚠️ PHP binary not found or invalid: ${phpPath}`);
16+
isPhpAvailable = false;
17+
return false;
18+
}
19+
phpPath = validPath;
20+
1021
try {
22+
console.log(`[phpEnv] Verifying PHP executable: ${phpPath}`);
1123
await execFileAsync(phpPath, ['-v']);
1224
isPhpAvailable = true;
13-
console.log('✅ PHP environment check passed.');
25+
console.log(`✅ PHP environment check passed (${phpPath}).`);
1426
} catch (e) {
1527
isPhpAvailable = false;
16-
console.warn('⚠️ PHP environment check failed. PHP features will be disabled.');
28+
console.warn(`⚠️ PHP environment check failed. PHP features will be disabled.`);
29+
console.warn(`[phpEnv] Error details:`, e.message);
1730
// console.error(e);
1831
}
1932
return isPhpAvailable;

utils/pluginManager.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "fs";
22
import path from "path";
33
import {spawn} from "child_process";
44
import {fileURLToPath, pathToFileURL} from "url";
5+
import {ensureExecutable} from "./binHelper.js";
56

67
// 获取 pluginManager.js 的目录
78
const __filename = fileURLToPath(import.meta.url);
@@ -54,22 +55,6 @@ function getPluginBinary(rootDir, pluginPath, pluginName) {
5455
return path.join(binDir, binaryName);
5556
}
5657

57-
function ensureExecutable(filePath) {
58-
if (process.platform === "win32") {
59-
// Windows 不需要 chmod,直接返回
60-
return;
61-
}
62-
try {
63-
const stats = fs.statSync(filePath);
64-
if (!(stats.mode & 0o111)) {
65-
fs.chmodSync(filePath, 0o755);
66-
console.log(`[pluginManager] 已为插件 ${filePath} 添加执行权限`);
67-
}
68-
} catch (err) {
69-
console.error(`[pluginManager] 无法设置执行权限: ${filePath}`, err.message);
70-
}
71-
}
72-
7358
/**
7459
* 启动插件
7560
* @param {Object} plugin 插件配置

0 commit comments

Comments
 (0)