「全国版住所検索システムテ」で、スクリプトからMySQLに接続するためのPHP側のプログラム

ファイル名は、sql.php にします。
それにしても、危険だなぁ〜

<?php

define( '_CHAR_CODE', 'UTF-8' ); // 文字コード
define( '_RS', chr(30) );        // レコードの区切文字
define( '_US', chr(31) );        // フィールドの区切り文字
define( '_HOST', 'localhost' );  // MySQLの設定を参照
define( '_USER', 'root' );       // MySQLの設定を参照
define( '_PASS', '********' );   // MySQLの設定を参照
define( '_REFERER', 'http://' ); //ささやかなURLのチェック

mb_language( 'Japanese' );
mb_http_output( _CHAR_CODE );
mb_internal_encoding( _CHAR_CODE );
header ( 'Content-Type: text/html; charset=utf-8' );
ob_start( 'mb_output_handler');


//呼び出し元を確認
$pos = strpos( $_SERVER[ HTTP_REFERER ], _REFERER );
if( false === $pos || 0 < $pos )
 exit( 'Error!!'._RS.'Intercepted access.' );

//接続確認
if( !$d = mysql_connect( _HOST, _USER, _PASS ) ) 
  exit( 'Error!!'._RS.' No connect.' );

//SQLコマンドの読み出し
$cmnd = $_POST[ 'cmnd' ];
if( !$cmnd ) exit( 'Connected MySQL' ); //SQLが無い場合は接続確認

//複数のSQLを切り分け実行
$sqls = split( _US, $cmnd );
for( $c = 0 ; $sql = $sqls[ $c++ ]; )
  if(! $r = mysql_query( $sql, $d ) )
    exit( 'Error!! SQLが実行できません。'._RS.$sql._RS.mysql_error( $d ) );

//最後のSQL文の結果だけを返す
while( $row = mysql_fetch_array( $r, MYSQL_NUM ) )
  $b[ ] = join( _US, $row );

echo $b ? join( _RS, $b ): '';