Skip to content

Instantly share code, notes, and snippets.

@kazumich
Created March 16, 2026 01:36
Show Gist options
  • Select an option

  • Save kazumich/d01cf3bc9e33cf17bf3cd851a9fd357b to your computer and use it in GitHub Desktop.

Select an option

Save kazumich/d01cf3bc9e33cf17bf3cd851a9fd357b to your computer and use it in GitHub Desktop.
a-blog cms 環境チェック
<?php
// a-blog cms 環境チェック(phpinfo 不要)
// 公開しない前提:確認が終わったら削除してください。
header('Content-Type: text/plain; charset=UTF-8');
function ok($label, $value) {
echo "[OK] {$label}: {$value}\n";
}
function ng($label, $value) {
echo "[NG] {$label}: {$value}\n";
}
/**
* ファイルのパーミッション・オーナー情報を1行で返す
* 例: 0644 / uid=48(apache) gid=48(apache)
* posix_* 関数が使えない環境(Windows 等)でも最低限の情報を返す
*/
function file_meta(string $path): string {
if (!file_exists($path)) return '(file not found)';
$perms = fileperms($path);
$mode = $perms !== false ? sprintf('%04o', $perms & 0777) : '????';
$uid = function_exists('fileowner') ? @fileowner($path) : false;
$gid = function_exists('filegroup') ? @filegroup($path) : false;
// posix_getpwuid / posix_getgrgid が使える場合は名前も表示
if ($uid !== false && function_exists('posix_getpwuid')) {
$pw = posix_getpwuid($uid);
$ustr = $pw ? "uid={$uid}({$pw['name']})" : "uid={$uid}";
} elseif ($uid !== false) {
$ustr = "uid={$uid}";
} else {
$ustr = 'uid=n/a';
}
if ($gid !== false && function_exists('posix_getgrgid')) {
$gr = posix_getgrgid($gid);
$gstr = $gr ? "gid={$gid}({$gr['name']})" : "gid={$gid}";
} elseif ($gid !== false) {
$gstr = "gid={$gid}";
} else {
$gstr = 'gid=n/a';
}
return "{$mode} / {$ustr} {$gstr}";
}
ok('PHP', PHP_VERSION);
ok('SAPI', php_sapi_name());
ok('OS', PHP_OS_FAMILY);
// 重要設定
$settings = [
'memory_limit',
'max_execution_time',
'post_max_size',
'upload_max_filesize',
'max_input_vars',
'default_charset',
'date.timezone',
'disable_functions',
];
echo "\n== ini settings ==\n";
foreach ($settings as $k) {
$v = ini_get($k);
if ($v === false || $v === null) $v = '(not set)';
echo "{$k} = {$v}\n";
}
// 必須〜準必須の拡張(運用により必要性が変わるものも含む)
$exts = [
'mbstring','json','pcre','spl',
'pdo','mysqli','pdo_mysql',
'curl','openssl',
'dom','xml','simplexml',
'gd','fileinfo','zip'
];
echo "\n== extensions ==\n";
foreach ($exts as $e) {
if (extension_loaded($e)) ok($e, 'loaded'); else ng($e, 'not loaded');
}
// 関数が殺されていないか(phpinfo は除外)
$funcs = ['exec','shell_exec','proc_open','popen'];
echo "\n== functions (disabled?) ==\n";
$disabled = array_map('trim', explode(',', (string)ini_get('disable_functions')));
foreach ($funcs as $f) {
if (in_array($f, $disabled, true)) ng($f, 'disabled'); else ok($f, 'enabled');
}
// このファイル自身のパーミッション・オーナー情報
echo "\n== this script file ==\n";
$selfPath = __FILE__;
echo "path : {$selfPath}\n";
echo "meta : " . file_meta($selfPath) . "\n";
// PHP プロセスの実行ユーザー情報
echo "\n== process identity ==\n";
if (function_exists('posix_getuid')) {
$puid = posix_getuid();
$pgid = posix_getgid();
$peuid = posix_geteuid();
$pegid = posix_getegid();
$pw = posix_getpwuid($puid);
$gr = posix_getgrgid($pgid);
$epw = posix_getpwuid($peuid);
$egr = posix_getgrgid($pegid);
echo "uid : {$puid}" . ($pw ? " ({$pw['name']})" : '') . "\n";
echo "gid : {$pgid}" . ($gr ? " ({$gr['name']})" : '') . "\n";
echo "euid : {$peuid}" . ($epw ? " ({$epw['name']})" : '') . "\n";
echo "egid : {$pegid}" . ($egr ? " ({$egr['name']})" : '') . "\n";
} elseif (function_exists('get_current_user')) {
echo "process user : " . get_current_user() . " (posix_* unavailable)\n";
} else {
echo "process user : n/a\n";
}
// 書き込みテスト(a-blog cms はキャッシュ/一時書き込みが発生し得る)
echo "\n== write test ==\n";
$tmp = sys_get_temp_dir();
$path = $tmp . '/abcheck_' . getmypid() . '.txt';
$w = @file_put_contents($path, "test\n");
if ($w === false) {
ng("write to sys_get_temp_dir()", "failed: {$tmp}");
} else {
ok("write to sys_get_temp_dir()", "ok: {$path}");
echo " written file meta: " . file_meta($path) . "\n";
@unlink($path);
}
// 設置先ディレクトリへの書き込みテスト
// a-blog cms はキャッシュ・アップロード等でドキュメントルート以下への書き込みが発生する
echo "\n== write test (document root) ==\n";
$docRoot = rtrim($_SERVER['DOCUMENT_ROOT'] ?? '', '/\\');
$selfDir = rtrim(dirname(__FILE__), '/\\');
$targets = [];
// DOCUMENT_ROOT が取得できた場合は追加
if ($docRoot !== '') {
$targets['DOCUMENT_ROOT'] = $docRoot;
}
// このファイル自身の設置ディレクトリ(サブディレクトリ設置にも対応)
if ($selfDir !== '' && $selfDir !== $docRoot) {
$targets['script dir'] = $selfDir;
}
// DOCUMENT_ROOT と同一の場合はまとめて表示
if ($docRoot !== '' && $selfDir === $docRoot) {
$targets = ['DOCUMENT_ROOT (= script dir)' => $docRoot];
}
foreach ($targets as $label => $dir) {
if (!is_dir($dir)) {
ng("is_dir({$label})", "not a directory: {$dir}");
continue;
}
if (!is_writable($dir)) {
ng("is_writable({$label})", "not writable: {$dir}");
continue;
}
$probe = $dir . '/abcheck_write_' . getmypid() . '.tmp';
$result = @file_put_contents($probe, "test\n");
if ($result === false) {
ng("write to {$label}", "failed: {$dir}");
} else {
ok("write to {$label}", "ok: {$probe}");
echo " written file meta: " . file_meta($probe) . "\n";
@unlink($probe);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment