1年ほど前に,fs_moodle用に以下のようなconfig.php(とfsconfig.php)のチェックをする関数を作りました.$configpathはサーバ上でのフルパス名(ファイル名含む)です.これで一応,6種類のチェックが...できていると思います.
// config.php, fs_config.phpの頭とお尻のチェック(2008/10/23)
// 戻り値
// 1 : OK
// 0 : ファイルが存在しない
// -1 : ファイルの中身が空
// -2 : BOMが頭にある
// -3 : 余計な文字が頭にある(BOM以外)
// -4 : 余計な文字がお尻にある
// -5 : 文字コードがASCII/UTF-8以外である
function check_config_head_and_tail($configpath)
{
if (!file_exists($configpath)) return 0;
if (!($farray = file($configpath))) return -1;
// チェック1(BOM)
$c1 = ord(substr($farray[0], 0, 1));
$c2 = ord(substr($farray[0], 1, 1));
$c3 = ord(substr($farray[0], 2, 1));
if (($c1 == 0xef) && ($c2 == 0xbb) && ($c3 == 0xbf)) return -2;
if (($c1 == 0xfe) && ($c2 == 0xff)) return -2; // UTF-16
if (($c1 == 0xff) && ($c2 == 0xfe)) return -2; // UTF-16
// チェック2
if (strncmp($farray[0], "<?php", 5) != 0) return -3;
$tail = rtrim($farray[count($farray) - 1], "\n");
$tail = rtrim($tail, "\r");
$tail = rtrim($tail, "\n");
if ($tail != "?>") return -4;
// チェック3(文字コード)
$encname = mb_detect_encoding(file_get_contents($configpath));
if (($encname != 'ASCII') && ($encname != 'UTF-8')) return -5;
return 1;
}