<?php

// 2014.02.02(일) by BiHon (http://dreamphp.com/)

header('Content-Type: text/html; charset=cp949'); // 완성형. utf-8로 하려면 인코딩 바꿔 저장, 아래 CP949→UTF-8 바꾸기

echo '<xmp style="font-family:GulimChe;font-size:12px;line-height:100%;">';

$path = './'; // *.ssd 파일이 위치한 경로 (절대경로 or 상대경로 : 끝에 경로 구분자 붙일 것!)

$files = array(); // 파일명 담을 배열
$dh = opendir($path); // 위 경로 열기
while ( ($read=readdir($dh))!==false ) // 하나씩 읽기
{
    if ( $read[0]=='.' || !is_file($path.$read) || strpos(strtolower($read),'.ssd')===false ) continue; // 거르기
    $files[] = $read; // *.ssd 파일명만 배열에 추가
}
closedir($dh); // 닫기

if ( count($files)==0 ) exit('.SSD 파일이 없습니다.'); // 파일 없는 경우 종료

natsort($files); // 정렬

$bar1 = str_repeat('=',70).PHP_EOL; // 구분선 1
$bar2 = str_repeat('-',70).PHP_EOL; // 구분선 2

$chk = 0; // 확인을 위한 변수

foreach ( $files as $file ) // 파일 반복
{
	sscanf($file,'%04s%02s%02s',$y,$m,$d); // 파일명에서 날짜
	echo $bar1.'+ '.$y.'-'.$m.'-'.$d.PHP_EOL.$bar2; // 구분선.0000-00-00.구분선 출력
	$temp = file_get_contents($path.$file,null,null,32); // 내용 읽기
	if ( strlen($temp)%70!=0 ) continue; // 70바이트 배수 아니면 스킵
	$chk = 1; // 하나라도 성공하면 1 할당
	$temp2 = str_split($temp); // 글자별로 나누기
	$temp3 = array_chunk($temp2,70); // 70개씩 그룹
	foreach ( $temp3 as $arr ) // 배열 반복
	{
		$arr = array_reverse($arr); // 뒤집기
		$line = rtrim(iconv('JOHAB','CP949',implode('',$arr))); // 묶은 후 인코딩 변환
		echo $line.PHP_EOL; // 출력
	}
	echo $bar2.PHP_EOL; // 구분선 출력
}

if ( !$chk ) echo '유효한 SSD 파일이 없습니다.'.PHP_EOL;
