TOP ▲
itcore TOP
> TIPS
> diff.php
タグ:php diff ツール diff.php 画面からdiffを実行するPHPプログラム。| itcore 2018年
テスト実行
diff.php
プログラムソース
<!DOCTYPE html><html lang="ja">
<head><meta charset="utf-8">
<title>diff.php</title>
</head><body>
<?php
//----------------------------
$self = basename($_SERVER['PHP_SELF']);
$title = "<a href=$self>diff.php</a> 画面からdiffを実行するPHPプログラム。| itcore 2018年";
// 2018-01-27 初期開発
//----------------------------
uIniErrorOutput(); // エラーを画面に出す。
session_start();
$mode = uForm("mode");
$pass1 = uForm("pass1");
if (isset($_SESSION['pass1'])) $pass1 = $_SESSION['pass1'];
$dir1 = uForm("dir1");
$self_id = "diff";
print "
<h2>$title</h2>
<form action=$self method=post>
";
//----------------------------
// ログイン画面
//----------------------------
if ("test" != $pass1) {
  print "
    パスワード <input type=password name=pass1 value='test'><br>
    <input type=submit name=sub_login value='ログイン'><br>
  ";
  exit;
} else {
  $_SESSION['pass1'] = $pass1;
}
//----------------------------
// diff処理
//----------------------------
if ("" != uForm("sub_diff")) {
  if ("" == $dir1) {
    $dir2 = "";
  } else {
    $dir2 = "$dir1/";
  }
  $a_file = uForm("chk1");
  if (!isset($a_file[0])) {
    print "ファイルを選択してください。<br>\n";
    exit;
  } elseif (!isset($a_file[1])) {
    $file1 = $a_file[0];
    $cmd = "cat -n '$dir2$file1'";
  } else {
    $file1 = $a_file[0];
    $file2 = $a_file[1];
    $cmd = "diff  '$dir2$file1' '$dir2$file2'";
  }
  //print "file1=$file1 file2=$file2<br>\n";
  print "[" . htmlspecialchars($cmd) . "]<br>\n";
  $ret = shell_exec($cmd);
  print nl2br(uHtmlspecialchars2($ret)) . "<br>\n";
  exit;
}
//----------------------------
// ファイル一覧画面
//----------------------------
print "
  ディレクトリ $dir1<br>
";
if ("" == $dir1) $dir1 = ".";
$a_fname = uOpendirArrayGet($dir1);
if ("." == $dir1) {
  $dir2 = "";
} else {
  print "<a href=$self?dir1=" . dirname($dir1) . ">..</a><br>\n";
  print "<input type=hidden name=dir1 value='$dir1'>\n";
 $dir2 = "$dir1/";
}
sort($a_fname);
foreach ($a_fname as $fname) {
  if (is_dir("$dir2$fname")) print "<a href=$self?dir1=$dir2$fname>$fname</a><br>\n";
}
foreach ($a_fname as $fname) {
  if (is_file("$dir2$fname")) print "<input type=checkbox name=chk1[] value='$fname'>$fname<br>\n";
}
print "
  ファイルを2つ選択してdiffを実行してください。<br>
  1つだけ選ぶとcat -n になります。<br>
  <input type=submit name=sub_diff value='diff'><br>
</form>
<hr>
";
exit;
//----------------------------
// 共通関数
//----------------------------
// エラーを画面に出す。
function uIniErrorOutput() {
  error_reporting(E_ALL);
  ini_set('display_errors', 1);
}
// フォーム変数の受取。存在しない時は空文字を返す。
function uForm($var) {
  if (isset($_POST[$var])) return $_POST[$var];
  if (isset($_GET[$var])) return $_GET[$var];
  return "";
}
// htmlspecialchars 空白を に変換する。
function uHtmlspecialchars2($text) {
  $text = htmlspecialchars($text);
  $text = str_replace("\t", "    ", $text);
  $text = str_replace(" ", " ", $text);
  return $text;
}
// インデント付きのhtmlspecialchars 行頭の空白を に変換する。
function uHtmlspecialcharsIndent($text) {
  $a_line = explode("\n", $text); // 行に分割
  $text1 = "";
  for($i2 = 0; $i2 < count($a_line); $i2++) {
    $line = $a_line[$i2];
    for ($i = 0; $i < strlen($line); $i++) {
    $s1 = substr($line, $i, 1);
      if (" " == $s1) {
        $text1 .= " ";
      } else if ("\t" == $s1) {
        $text1 .= " ";
      } else {
        $text1 .= htmlspecialchars(substr($line, $i)) . "\n";
        break;
      }
    }
    if (count($a_line) -1 != $i2 && 0 == strlen($line)) $text1 .= "\n"; // 最終行以外の空行
  }
  return $text1;
}
// ディレクトリ配下のファイル名を配列に入れる。.と..は除く。
function uOpendirArrayGet($dir) {
  $a_fname = array();
//echo "dir=$dir<br>\n";
  $dh = opendir($dir);
  if (false === $dh) {
    echo "オープン出来ません。dir=$dir<br>\n";
    return $a_fname;
  }
  while (false !== ($fname = readdir($dh))) {
//echo "fname=$fname<br>\n";
    if ("." == $fname) continue; // 対象外
    if (".." == $fname) continue; // 対象外
    $a_fname[] = $fname;
  }
  closedir($dh);
  return $a_fname;
}
?>