TOP ▲ itcore TOPTIPStool_grep.php  タグ:tool grep ソース

TOOL grep | itcore 2021年

<?php
//--------------------------------------------------------------
// grep情報 grep.php
// 注意:このプログラムはサーバファイルの内容を表示しますので、
//       BASIC認証などのセキュアな環境で実行するようにしてください。
//--------------------------------------------------------------
include_once "../.env.php"; // 環境依存インクルード
include_once "inc_common.php"; // 共通インクルード
list($datetime1, $datetime2, $self, $menu, $today) = uInit(); // 初期設定
$title = "grep情報";
include_once "inc_header.php"; // ヘッダインクルード

// デバッグ情報
if (20210422 == $today) {
  u_debug_echo();
}
// 制御系データ
$mode = uForm("mode");
// 検索条件
$grep_chk_ignore_case = uFormGrep("grep_chk_ignore_case"); // 大文字小文字無視
$grep_max = uFormGrep("grep_max");
if ("" == $grep_max) {
  $grep_max = 100;
}
$grep_word = uFormGrep("grep_word");
$grep_dir = uFormGrep("grep_dir");
//echo "debug41 grep_chk_ignore_case=$grep_chk_ignore_case grep_word=$grep_word<br>\n";
// 共通的な変数の初期化
$html_th = $html_td = $html_submit = $html_hidden = "";
$no = 0;
//--------------------
// ヘッダ
//--------------------
?>
<script>
function uJsClear() {
  document.form1.grep_max.value = "100";
  document.form1.grep_word.value = "";
  s1 = "";
  s1 += "app, 2\n";
  s1 += "public, 2\n";
  s1 += "resources, 2\n";
  document.form1.grep_dir.value = s1;
  document.form1.grep_chk_ignore_case.checked = true;
}
</script>
<?php
echo <<<EOT
<form name=form1 method=post action=$self enctype="multipart/form-data">
EOT;
//--------------------
// grep 実行
//--------------------
$a_grep = explode("\n", $grep_dir);
$html_grep_result = "";
$count = 0;
if ("" != uForm("sub_grep") && "" != $grep_word) {
  $html_grep_result .= "<div id=grep_result>■検索結果</div>\n";
  foreach ($a_grep as $dir_csv) {
    list ($dir, $grep) = explode(",", $dir_csv);
    $dir = "/var/prj/$dir";
    //echo "debug100 dir=$dir grep=$grep<br>\n";
    $cmd = "";
    $case = "";
    if ($grep_chk_ignore_case) {
      $case = "i";
    }
    if (1 == $grep) {
      $cmd = "grep -n$case '$grep_word' $dir/* 2>/dev/null\n";
    } elseif (2 == $grep) {
      $cmd = "grep -Rn$case '$grep_word' $dir 2>/dev/null\n";
    }
    if ("" != $cmd) {
      //echo "debug116 cmd=$cmd grep=$grep <br>\n";
      //list($cnt, $html) = u_grep_html($cmd, &$count, $grep_max);
      $html = u_grep_html($cmd, $count, $grep_max);
      $html_grep_result .= $html;
      //$count += $cnt;
      //echo "debug111 count=$count cnt=$cnt grep_max=$grep_max<br>\n";
      if ($count >= $grep_max) {
        echo "<font color=red>max行に達しました。count=$count grep_max=$grep_max</font><br>\n";
        break;  // MAX行に達した。
      }
    }
  }
}
//--------------------
// TOP 一覧
//--------------------
if ("" == $mode) {
  $msg = "";
  echo <<<EOT
<INPUT TYPE=button VALUE="初期設定値" onClick="uJsClear()">
<br>
max行 <input type=text name=grep_max value="$grep_max" size=3 required>
検索ワード <input type=text name=grep_word value="$grep_word" required>
<input type=checkbox name=grep_chk_ignore_case value=checked $grep_chk_ignore_case>大文字小文字を無視する。
<br>
対象ディレクトリ(/var/prj配下) 1:サブディレクトリ配下は対象外 2:対象
<textarea name=grep_dir rows=10>
$grep_dir
</textarea>
<br>
<input type=submit name=sub_grep value="grep実行">
$msg
$html_grep_result
EOT;
}

//-----------------------------
// 関数
//-----------------------------
// フォーム変数取得 セッション変数保存
function uFormGrep($var)
{
  if ("" != uForm("sub_grep")) {
    // grepが実行された場合はその値を有効にしてセッション変数にセットする。
    $value = uForm($var);
    if (!is_array($value)) {
      $value = trim($value);
    } // 配列でなければtrimする。
    $value = uSessionSet($var, $value);
  } else {
    // 検索以外の時はセッション変数から値を取得する。
    $value = uSession($var);
  }
  return $value;
}
// grepコマンド実行
function u_grep_html($cmd, &$count, $grep_max)
{
  global $grep_chk_ignore_case, $grep_word, $a_site_dir;
  $html = "";
  $html .= "<table>\n";
  $html .= "<tr><th>" . htmlspecialchars($cmd) . "\n";
  $html .= "<tr><td>\n";
  //$ret = shell_exec($cmd);
  //$a_line = explode("\n", $ret);
  $a_line = [];
  $ret = exec("$cmd 2>/dev/null", $a_line, $result_code);
  //echo "debug190 ret=$ret cmd=$cmd result_code=$result_code<br>\n";
  if (0 != $ret) {
    echo "ERROR192 result_code=$result_code<br>\n";
  }
  foreach ($a_line as $line) {
    if ("" == trim($line) || "Binary file " == substr($line, 0, 12)) {
      continue;
    }
    // 検索されたワードに色付けする。
    $pos1 = strpos($line, ":"); // 行番号の前のコロン
    $pos2 = strpos($line, ":", $pos1 + 1); //  行番号の後のコロン
    $file = substr($line, 0, $pos1); // /var/www/xxx/public/css/layout.css
    // grepした結果なので必ずヒットする
    if ($grep_chk_ignore_case) {
      $pos = stripos($line, $grep_word, $pos2);
    } else {
      $pos = strpos($line, $grep_word, $pos2);
    }
    // ジャンプするファイルを識別する。
    $s1 = "";
    $line_no = substr($line, $pos1 + 1, $pos2 - $pos1 - 1);
    list ($file) = explode(":", $line);
    $link1 = "git.php?mode=cat&file=$file&line_no=$line_no#line_no_$line_no";
    $s1 .= "<a target=_blank href=$link1>" . substr($line, 0, $pos1) . "</a>";
    $s1 .= uHtmlPre(substr($line, $pos1, $pos - $pos1));
    $s1 .= "<font color=red><b>";
    $s1 .= uHtmlPre(substr($line, $pos, strlen($grep_word)));
    $s1 .= "</b></font>";
    $s1 .= uHtmlPre(substr($line, $pos + strlen($grep_word)));
    // 長い行は途中から省略
    $max_length = 500;
    if (mb_strlen($s1) > $max_length) {
      $s1 = mb_substr($s1, 0, $max_length) . " <font color=red>以降省略</font>";
    }
    $html .= "$s1<br>\n";
    $count++;
    if ($count >= $grep_max) {
      break;
    }
  }
  $html .= "</table>\n";
  return $html;
}

?>