리자언니의 태그갯수 체크 덧붙임 (소켓+불일치표시+태그추가) (수정) > 그누4 팁자료실

그누4 팁자료실

그누보드4와 관련된 팁을 여러분들과 함께 공유하세요.
나누면 즐거움이 커집니다.

리자언니의 태그갯수 체크 덧붙임 (소켓+불일치표시+태그추가) (수정) 정보

리자언니의 태그갯수 체크 덧붙임 (소켓+불일치표시+태그추가) (수정)

본문

바로 아래 리자언니 팁을 제가 사용하려고 따비질을해보았습니다.^^

file() 함수를 막아놓은 서버에 세들어 사는분들께서는 전체적으로 이 코드로 적용해보시고,
그외 분들은 리자언니의 코드에 아래 추가된 케이스만 참조 더해주시면 될듯 합니다.
div,span,ul,li.dt,dd 태그까지 추가로 넣어두었습니다.

  짝이 맞지않는 태그는 빨갛게 칠해서 오른쪽으로 밀어 붙였습니다.^^
  이걸로 체크해보니 기본적인 태그에 손볼곳이 무척 많다는 걸 알았습니다.

  왜? 이런짓을.. "코딩이 제대로 되었는지 적어도 문열고 닫지 않은 부분까지는 확실하게 체크해보자!" 입니다.

풋 파일정도에 관리자 권한으로 링크합니다.
<a href="javascript:win_comment('<?=$g4['path']?>/htmlcheck.php?path=<?=$_SERVER['REQUEST_URI']?>');"> 태그체크</a>

파일 링크 경로와 htmlcheck.php상단 상대경로는 이 파일의 위치에 따라 맞게 수정하셔야 합니다.
htmlcheck.php 실행 권한도 관리자로 해주면 더욱 좋겠지만, 그냥 두었습니다.

  *체크할 페이지로 이동후 링크를 클릭해서 태그를 확인하시면 됩니다.
      코멘트용 새창으로 띄워 보여줍니다.

  *아래 업데이트된 파일로 실행된 스크린샷으로 교체하였습니다.

htmlcheck.php
/////////////////////////////////////////////////////////////////
<?
$g4_path = ".";
include_once($g4_path . '/' . 'common.php');
include_once($g4['path'] . '/' . 'head.sub.php');
?>
<style type="text/css">
/* 일치되지 않는 태그 표시
*****************************************/
.odd {padding-left:200px;padding-top:20px;font-weight:bold; color:red;}
</style>

<div style="margin:100px;">

<h3>[<?=$path?>] 페이지 html 체크</h3>
<hr>
$_SERVER['REQUEST_URI'] : <?=$_SERVER['REQUEST_URI']?>
<hr>

<?
if (!$path) {
  echo "?path= 값을 넘겨주세요.";
  exit;
}

function getpath($host,$path) {
  $contents="";
  $fp = @fsockopen($host, 80, &$errno, &$errstr, 5);
  if($fp) {
  // 소켓을 이용해서 서버에 html 형태로 테이타를 보냄
  fputs ($fp, "GET $path HTTP/1.0\nUser-Agent: Mozilla/4.0\nHost: $host\n\n");

    while(!feof($fp)) {
    $contents .= fgets($fp,1024);
    }
  fclose($fp);

  return $contents;
  }
}

$s=getpath($_SERVER["HTTP_HOST"],$path);//실행시킬 파일 경로

  //$s = implode(" ", file($url, "r"));
  $s = strtolower($s);
  $table1 = substr_count($s, "<table");
  $table2 = substr_count($s, "</table");
  $tr1 = substr_count($s, "<tr");
  $tr2 = substr_count($s, "</tr");
  $td1 = substr_count($s, "<td");
  $td2 = substr_count($s, "</td");
  $form1 = substr_count($s, "<form");
  $form2 = substr_count($s, "</form");
  $a1 = substr_count($s, "<a ");
  $a2 = substr_count($s, "</a");
  $div1 = substr_count($s, "<div");
  $div2 = substr_count($s, "</div");
  $span1 = substr_count($s, "<span");
  $span2 = substr_count($s, "</span");
  $ul1 = substr_count($s, "<ul");
  $ul2 = substr_count($s, "</ul");
  $li1 = substr_count($s, "<li");
  $li2 = substr_count($s, "</li");
  $dt1 = substr_count($s, "<dt");
  $dt2 = substr_count($s, "</dt");
  $dd1 = substr_count($s, "<dd");
  $dd2 = substr_count($s, "</dd");
  /* 일치여부 확인
  *****************************************/
  $table = ($table1 == $table2)?"":"odd";
  $tr= ($tr1 == $tr2)?"":"odd";
  $td= ($td1 == $td2)?"":"odd";
  $form= ($form1 == $form2)?"":"odd";
  $a= ($a1 == $a2)?"":"odd";
  $div= ($div1 == $div2)?"":"odd";
  $span= ($span1 == $span2)?"":"odd";
  $ul= ($ul1 == $ul2)?"":"odd";
  $li= ($li1 == $li2)?"":"odd";
  $dt= ($dt1 == $dt2)?"":"odd";
  $dd= ($dd1 == $dd2)?"":"odd";

echo "<div class='$table'>&lt;table&gt;  &nbsp;".$table1." ~~ ".$table2."&nbsp; &lt;/table&gt;</div>";
echo "<div class='$tr'>&lt;tr&gt;  &nbsp;".$tr1." ~~ ".$tr2."&nbsp; &lt;/tr&gt;</div>";
echo "<div class='$td'>&lt;td&gt;  &nbsp;".$td1." ~~ ".$td2."&nbsp; &lt;/td&gt;</div>";

echo "<div class='$form'>&lt;form&gt;  &nbsp;".$form1." ~~ ".$form2."&nbsp; &lt;/form&gt;</div>";
echo "<div class='$a'>&lt;a&gt;  &nbsp;".$a1." ~~ ".$a2."&nbsp; &lt;/a&gt;</div>";

echo "<div class='$div'>&lt;div&gt;  &nbsp;".$div1." ~~ ".$div2."&nbsp; &lt;/div&gt;</div>";
echo "<div class='$span'>&lt;span&gt;  &nbsp;".$span1." ~~ ".$span2."&nbsp; &lt;/span&gt;</div>";

echo "<div class='$ul'>&lt;ul&gt;  &nbsp;".$ul1." ~~ ".$ul2."&nbsp; &lt;/ul&gt;</div>";
echo "<div class='$li'>&lt;li&gt;  &nbsp;".$li1." ~~ ".$li2."&nbsp; &lt;/li&gt;</div>";

echo "<div class='$dt'>&lt;dt&gt;  &nbsp;".$dt1." ~~ ".$dt2."&nbsp; &lt;/dt&gt;</div>";
echo "<div class='$dd'>&lt;dd&gt;  &nbsp;".$dd1." ~~ ".$dd2."&nbsp; &lt;/dd&gt;</div>";

?>
<hr>

<center><strong><a href=javascript:self.close()><span style="font-size:9pt;">창닫기</span></a></center>

</div>
추천
0
  • 복사

댓글 4개

2006년 9월 20일 수요일 - 업데이트

  전체적으로 수정하고, 읽어온 파일의 html 태그를 바로 확인할수 있도록 추가하였습니다.
  file()함수 이거 사람 잡는군요.
  제가 세든 서버에서는 바로 읽어서 처리는 안되고, 불러와 저장한후 처리는 가능하군요.

원래 이것을 하려고 한게 아닌데 뿌리를 뽑아야 하기때문에 엄한데서 하루는 까먹고 있다는.. ^^
업데이트 한걸로 스크린샷 교체했습니다.

////////////////////////////////////////////////////////////
<?
$g4_path = "../..";
include_once($g4_path . '/' . 'common.php');
include_once($g4['path'] . '/' . 'head.sub.php');

// 소켓으로 읽어들인 파일 임시저장, g4/tmp 폴더생성 chmod 777
$tmp_dir = $g4['path'] . '/' . 'tmp';
$tmp_file = preg_replace("`^(.*)(/[^/]+)$`", "\\1/" . $tmp_dir . "\\2", __FILE__);

if (!is_dir("./" . $tmp_dir)) {
  echo "임시저장 디렉토리가 존재하지 않습니다.";
  exit;
}

if (!$path) {
  echo "?path= 값을 넘겨주세요.";
  exit;
}

ob_start();

function getpath($host,$path) {
  $contents="";
  $fp = @fsockopen($host, 80, &$errno, &$errstr, 5);
  if($fp) {
    fputs ($fp, "GET $path HTTP/1.0\nUser-Agent: Mozilla/4.0\nHost: $host\n\n");

    while(!feof($fp)) {
    $contents .= fgets($fp,1024);
    }
  fclose($fp);

  return $contents;
  }
}

$htmls = getpath($_SERVER["HTTP_HOST"],$path);//요청 파일 읽어옴
$htmls = trim(substr($htmls,strpos($htmls, "\r\n\r\n")));//헤더 삭제

ob_end_clean();

//임시 저장
$fp = @fopen($tmp_file, 'w');

if (empty($fp)) {
  echo "임시저장 디렉토리의 퍼미션을 확인하세요";
  exit;
}

fwrite($fp, $htmls);
fclose($fp);
chmod($tmp_file, 0777);

  //$url = "http://".$_SERVER["HTTP_HOST"].$path;//test - 바로 읽어서 처리할경우 막힘

  $s = implode(" ", file($tmp_file, "r"));
  $s = strtolower($s);

  $table1 = substr_count($s, "<table");
  $table2 = substr_count($s, "</table>");
  $tr1 = substr_count($s, "<tr");
  $tr2 = substr_count($s, "</tr>");
  $td1 = substr_count($s, "<td");
  $td2 = substr_count($s, "</td>");

  $form1 = substr_count($s, "<form");
  $form2 = substr_count($s, "</form>");

  $a1 = substr_count($s, "<a ");
  $a2 = substr_count($s, "</a>");
 
  $div1 = substr_count($s, "<div");
  $div2 = substr_count($s, "</div>");
 
  $span1 = substr_count($s, "<span");
  $span2 = substr_count($s, "</span>");
 
  $ul1 = substr_count($s, "<ul");
  $ul2 = substr_count($s, "</ul>");
 
  $li1 = substr_count($s, "<li>");
  $li2 = substr_count($s, "</li>");

  $dt1 = substr_count($s, "<dt");
  $dt2 = substr_count($s, "</dt>");
 
  $dd1 = substr_count($s, "<dd");
  $dd2 = substr_count($s, "</dd>");

  $font1 = substr_count($s, "<font");
  $font2 = substr_count($s, "</font>");

  /* 일치여부 확인
  *****************************************/
  $table = ($table1 == $table2)?"":"odd";
  $tr= ($tr1 == $tr2)?"":"odd";
  $td= ($td1 == $td2)?"":"odd";
  $form= ($form1 == $form2)?"":"odd";
  $a= ($a1 == $a2)?"":"odd";
  $div= ($div1 == $div2)?"":"odd";
  $span= ($span1 == $span2)?"":"odd";
  $ul= ($ul1 == $ul2)?"":"odd";
  $li= ($li1 == $li2)?"":"odd";
  $dt= ($dt1 == $dt2)?"":"odd";
  $dd= ($dd1 == $dd2)?"":"odd";
  $font= ($font1 == $font2)?"":"odd";

?><style type="text/css">
/* 일치되지 않는 태그 표시
*****************************************/
.odd {padding-left:200px;font-weight:bold; color:red;}
</style>

<div style="margin:20px 10px 10px 20px;">

  요청한 페이지 <span style="font-weight:bold;"><?=$_SERVER['REQUEST_URI']?></span>

  <div style='padding-top:20px;'>태그 갯수 출력</div>
  <div style='padding:10px;border:1px solid gray;'>
    <div class='<?=$table?>?>'>&lt;table&gt;  &nbsp;<?=$table1?> ~~ <?=$table2?>&nbsp; &lt;/table&gt;</div>
    <div class='<?=$tr?>'>&lt;tr&gt;  &nbsp;<?=$tr1?> ~~ <?=$tr2?>&nbsp; &lt;/tr&gt;</div>
    <div class='<?=$td?>'>&lt;td&gt;  &nbsp;<?=$td1?> ~~ <?=$td2?>&nbsp; &lt;/td&gt;</div>
    <div class='<?=$form?>'>&lt;form&gt;  &nbsp;<?=$form1?> ~~ <?=$form2?>&nbsp; &lt;/form&gt;</div>
    <div class='<?=$a?>'>&lt;a&gt;  &nbsp;<?=$a1?> ~~ <?=$a2?>&nbsp; &lt;/a&gt;</div>
    <div class='<?=$div?>'>&lt;div&gt;  &nbsp;<?=$div1?> ~~ <?=$div2?>&nbsp; &lt;/div&gt;</div>
    <div class='<?=$span?>'>&lt;span&gt;  &nbsp;<?=$span1?> ~~ <?=$span2?>&nbsp; &lt;/span&gt;</div>
    <div class='<?=$ul?>'>&lt;ul&gt;  &nbsp;<?=$ul1?> ~~ <?=$ul2?>&nbsp; &lt;/ul&gt;</div>
    <div class='<?=$li?>'>&lt;li&gt;  &nbsp;<?=$li1?> ~~ <?=$li2?>&nbsp; &lt;/li&gt;</div>
    <div class='<?=$dt?>'>&lt;dt&gt;  &nbsp;<?=$dt1?> ~~ <?=$dt2?>&nbsp; &lt;/dt&gt;</div>
    <div class='<?=$dd?>'>&lt;dd&gt;  &nbsp;<?=$dd1?> ~~ <?=$dd2?>&nbsp; &lt;/dd&gt;</div>
    <div class='<?=$font?>'>&lt;font&gt;  &nbsp;<?=$font1?> ~~ <?=$font2?>&nbsp; &lt;/font&gt;</div>
  </div>

</div>

<div style="margin:10px 10px 10px 20px;">

  <div style='padding-top:10px;'>html 출력</div>
  <textarea style='width:100%;padding:0px;font-size:11px;border:1px solid gray;' rows=15 readonly nowrap><?=$htmls?></textarea>

  <div align='center' style='padding-top:10px;'><strong><a href='javascript:self.close()'>[창닫기]</a></strong></div>

</div>

<?

include_once($g4['path'] . '/' . 'tail.sub.php');
?>
부족한 부분이 많을겁니다.^^

찾을 문자열 부분에서 자바 스크립트 내부 코딩시 "<\/div>" 또는 "<\/span>" 처럼 코딩하는 경우가 있으므로..
$div2 = substr_count($s, "/div>");
$span2 = substr_count($s, "/span>");
요렇게 바꿔주세요.
뒤늦게.. 
$s = implode(" ", file($tmp_file,r));  //해당부분 에러..
$s = implode(" ", file($tmp_file));  //수정시 정상작동..! r이 뭔지 모르겠습니당..!
© SIRSOFT
현재 페이지 제일 처음으로