<?php

function isAlphaNum($data) {

//    $pat = "^[a-fA-F0-9\']+$";
//    $pat = '^[a-fA-F0-9\']+$';
    $pat = '/^[a-fA-F0-9\']+$/';
//if (preg_match("/PHP/", "今PHPを勉強しています。")) {
//    if (ereg($pat, trim($data))) {
    if (preg_match($pat, trim($data))) {
        return true;
    } else {
        return false;
    }
}

// ファイルに保存
//$yyyymmdd = date('Ymd') ;
//$accese_history_file = "./tmp/colorhenkan_log_" . $yyyymmdd . ".txt" ; // すべて入っていると重いのでバッチで最新のだいたい１００ぐらいにしておく
//$accese_history_file = $accese_history_ .  "_" .   date( "Ym" ) . ".log" ;
//$accese_history_file = $accese_history_ .  "_" .   date( "Ymd" ) . ".log" ;
$accese_history_file_day = "./tmp/accese_history_" . date("Ymd") . ".log";
$accese_history_file_month = "./tmp/accese_history_" . date("Ym") . ".log";

$html = "";
$err_msg_html = "";
$fp_month = "" ;
$fp_day = "" ;
$subtitle     = "" ;

if (!file_exists($accese_history_file_day)) {
    touch($accese_history_file_day);
    chmod($accese_history_file_day, 0666);
}
if (!file_exists($accese_history_file_month)) {
    touch($accese_history_file_month);
    chmod($accese_history_file_month, 0666);
}
//$log_file            = "./log/log_file_" . date( "Ym" ) . ".log" ;
//if  ( !file_exists( $log_file ) ) {
//    touch( $log_file );
//    chmod( $log_file, 0666 );/
//}
// 入力
if (isset($_GET["input_a_box"])) {
    $input_a_box = $_GET["input_a_box"];
} else {
    $input_a_box = "";
}
if ($input_a_box == "index") {
    $input_a_box = str_replace('index', '', $input_a_box);
}
if( isset($_GET["subtitle"]) ){
	$subtitle  = $_GET["subtitle"]  ; 
}

//echo "iiiinput_a_box[" . $input_a_box . "]<br>" ;
//echo "sss subtitle[" . $subtitle . "]<br>" ;


if (file_exists("/var/www/html/colorhenkan/PRODUCTION.txt")) {
    define("DOMAIN_URL", "colorhenkan.pw");
    define("DIR_ROOT", "/var/www/html/colorhenkan/");
} else {
    define("DOMAIN_URL", "localhost/colorhenkan");
    define("DIR_ROOT", "D:/xampp/htdocs/colorhenkan/"); // php5 
}

//$output_file_name2 = DIR_ROOT . "tmp/colorhenkan_log.txt" ;

define("LOG_RECORD_CNT", 30); // logから読んでくるレコード数
define("TITLE", "色を表示する！カラーコード変換するツール"); // logから読んでくるレコード数
// 入力チェック
$input_a_box_count = mb_strlen($input_a_box);

//debug
//echo "input_a_box_count[" . $input_a_box_count . "]<br>" ;
//echo "isAlphaNum[" . isAlphaNum($input_a_box) . "]<br>" ;
// カラーコードの文字数が 0 or 6 の場合 only処理
if ($input_a_box_count == 0) {
    $input_a_box = "";
    $output_b_box = "";
} elseif (( $input_a_box_count == 6 ) and ( isAlphaNum($input_a_box) == true )) {

    // 古いファイルのリンクをあげる
//	$open_file_name  = "./tmp/" . $input_a_box . ".txt" ;
// 	// getで過去ファイルの指定があって、それが存在している場合
// //echo "jjj1[" . $open_file_name . "]" ;
// 	if  ( file_exists( $open_file_name ) == TRUE ) {
// //echo "jjj2" ;
// 		$handle = @fopen( $open_file_name , "r");
// 		if ($handle) {
// //echo "jjj3" ;
// 		    while (($buffer = fgets($handle, 4096)) !== false) {
// 		        echo $buffer;
// 				$buffer2 = ereg_replace("「|」",",",$buffer);
// 				$buffer_array = explode(",", $buffer2);
// 				$input_a_box  = $buffer_array[1] ; // input
// 				$output_b_box = $buffer_array[3] ; // output
// 		    }
// 		    if (!feof($handle)) {
// 		        echo "Error: unexpected fgets() fail\n";
// 		    }
// 		    fclose($handle);
// 		}
//	} else {
    // getで過去ファイルの指定あるのに ファイルが存在しない場合
    $output_b_box = $input_a_box;

//	}
} else {

    $err_msg_html = "カラーコードは、半角英数（0-9、A-F）の6文字で入力してください";
    $input_a_box = "";
    $output_b_box = "";
}



if ($output_b_box != "") {
    // 出力
//	$output_file_name  = "./tmp/accese_history_" . date('Ymd') . "_" . $input_a_box . ".txt" ;
    // ファイルに書く内容
//    $html .= "<span style='color:#" . $output_b_box . "'>■</span>" . "#" . $input_a_box . "のカラーコード";

	if( $subtitle != "" ) { 
		$html .= "<span style='color:#" . $output_b_box . "'>■</span>" . "#" . $input_a_box . "のカラーコード（" . $subtitle . "）";
//		$html .= "「身長<span style='font-size:47px;'>" . $input_a_box . "</span>cm の人の" . $cmbA_name . "は <span style='font-size:47px;'>"  . $output_b_box . "</span>kg」になります（" . $subtitle . "）" ;
	} else {
		$html .= "<span style='color:#" . $output_b_box . "'>■</span>" . "#" . $input_a_box . "のカラーコード";
	}

}


	if( $subtitle != "" ) { 
//	    $url = "<a href='http://" . DOMAIN_URL . "/" . $input_a_box . "/" . $subtitle . ".html' >" ;
            $url = "<a href='http://" . DOMAIN_URL . "/"  . $input_a_box . "/" . $subtitle . ".html' >" ;
	  } else {
//	    $url = "<a href='http://" . DOMAIN_URL . "/" . $input_a_box . ".html' >" ;
           $url = "<a href='http://" . DOMAIN_URL . "/"  . $input_a_box . ".html' >";
	}

    ob_start();
    echo $url;
    print_r($html);
    echo "</a>&nbsp;&nbsp;\n";
    $buffer = ob_get_contents();
    ob_end_clean();

    //日付ごとのファイル
    $fp_day = @fopen($accese_history_file_day, 'a+');
    fputs($fp_day, $buffer);
    fclose($fp_day);

    //月ごとのファイル
    $fp_month = @fopen($accese_history_file_month, 'a+');
    fputs($fp_month, $buffer);
    fclose($fp_month);


// 	// ファイル作成
// 	$fp = @fopen( $output_file_name , 'w'); 
// 	ob_start();                       
// echo $url ;;
// 
// 	print_r($html);       
// echo "</a>&nbsp;&nbsp;\n" ;
// 	$buffer = ob_get_contents();      
// 	ob_end_clean();                   
// 	fputs($fp,$buffer);               
// 	fclose($fp);
// 
// 	// ファイル作成2 $output_file_name2 = DIR_ROOT . "tmp/colorhenkan_log.txt" ;
// 	$fp2 = @fopen( $output_file_name2 , 'a+'); 
// 	ob_start();                       
// echo $url;
// 	print_r($html);       
// echo "</a>&nbsp;&nbsp;\n" ;
// 	$buffer2 = ob_get_contents();      
// 	ob_end_clean();                   
// 	fputs($fp2,$buffer2);               
// 	fclose($fp2);
// 
// 	// ファイル作成3 年月日ごとのファイル colorhenkan_log_" . $yyyymmdd . ".txt" ; // すべて入っていると重いのでバッチで最新のだいたい１００ぐらいにしておく
// 	$fp3 = @fopen( $accese_history_file , 'a+'); 
// 	ob_start();                       
// echo $url;
// 	print_r($html);       
// echo "</a>&nbsp;&nbsp;\n" ;
// 	$buffer3 = ob_get_contents();      
// 	ob_end_clean();                   
// 	fputs($fp3,$buffer3);               
// 	fclose($fp3);
//}
?>








<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <html lang="en" class="no-js">
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">






<?php
if ($html != "") {
//	echo '<TITLE>' . $html2 . ' ' . TITLE . '</TITLE>' ;
//	echo '<META name="description" content="' . TITLE . ' ' . $html2 . '">' ;

	if( $subtitle != "" ){
	    echo '<TITLE>#' . $output_b_box . 'の色表示 ' . TITLE . ' ' . $subtitle  . '</TITLE>';
	    echo '<META name="description" content="#' . $output_b_box . 'の色はこれ！ ' . TITLE . ' ' . $subtitle  . '">';
	    echo '<META name="keywords" content="#' . $output_b_box . ',colorcode,カラーコード,color,色変換 ' . $subtitle  . ' ">';
	} else {
	    echo '<TITLE>#' . $output_b_box . 'の色表示 ' . TITLE . '</TITLE>';
	    echo '<META name="description" content="#' . $output_b_box . 'の色はこれ！ ' . TITLE . '">';
	    echo '<META name="keywords" content="#' . $output_b_box . ',colorcode,カラーコード,color,色変換">';
	}
} else {
    echo '<TITLE>' . TITLE . '</TITLE>';
    echo '<META name="description" content="' . TITLE . '">';
    echo '<META name="keywords" content="colorcode,カラーコード,color,色変換">';
}
?>


            <link rel="shortcut icon" href="./favicon.ico">
            <!-- Bootstrap -->
            <script src="js_boxify/modernizr.custom.js"></script>
            <link href="css_boxify/bootstrap.min.css" rel="stylesheet">
            <link href="css_boxify/jquery.fancybox.css" rel="stylesheet">
            <link href="css_boxify/flickity.css" rel="stylesheet" >
            <link href="css_boxify/animate.css" rel="stylesheet">
            <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css_boxify/font-awesome.min.css" rel="stylesheet">
            <link href='http://fonts.googleapis.com/css?family=Nunito:400,300,700' rel='stylesheet' type='text/css'>
            <link href="css_boxify/styles.css" rel="stylesheet">
            <link href="css_boxify/queries.css" rel="stylesheet">
            <!-- Facebook and Twitter integration -->
            <meta property="og:title" content=""/>
            <meta property="og:image" content=""/>
            <meta property="og:url" content=""/>
            <meta property="og:site_name" content=""/>
            <meta property="og:description" content=""/>
            <meta name="twitter:title" content="" />
            <meta name="twitter:image" content="" />
            <meta name="twitter:url" content="" />
            <meta name="twitter:card" content="" />
            <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
            <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
            <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js_boxify/1.4.2/respond.min.js"></script>
            <![endif]-->

            <link rel="stylesheet" href="./css/style.css" type="text/css" />
            <script type="text/javascript" src="./js/js.js"></script>

            <style type='text/css'>
                <!--
                .rireki_box {
                    font-size:14px; 
                    margin:10px; 
                    background-color: #FFFFFF; 
                    padding:10px; 
                }
                .rireki_box a {
                    color:#3388FF;
                }
                -->
            </style>

        </head>
        <body>
            <!--[if lt IE 7]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
            <![endif]-->
            <!-- open/close -->

<?php
if ($err_msg_html != "") {
    echo "<div style='margin-left:20px; margin-right:20px;  margin-top:20px; margin-bottom:20px; border-bottom:3px solid #777777; border-top:3px solid #777777; background-color:#EEEEEE; padding:10px; '><div style='margin-top:0px; margin-left:20px; color:#333333; padding-right:10px; word-break:break-all; font-size:24px;'>";
    echo $err_msg_html;
    echo "</div></div>";
//	echo "ぱらあり" ;
} else {
//	echo "ぱらなし" ;
}
?>
            <header>
                <div class="texture-overlay"></div>
                <div class="container">
                    <a href="http://<?php echo DOMAIN_URL; ?>" style="color:#FFBBBB;">colorhenkan.pw</a>
                </div>
            </header>

            <div class="row hero-content" style="background-color:#EEFFCC;">
                <center>
                    <div class="col-md-12">
                        <h1 class="animated fadeInDown" style="background-color:#FFFFFF;  padding:10px 0px 10px 0px; color:#FFFFFF; font-size:46px;"><a href="http://<?php echo DOMAIN_URL; ?>" style="color:#222222;">
                                <img src="./img/colors.png" style="width:42px;">
                                <span style="color:#222222;">カ</span>
                                <span style="color:#22FF22;">ラ</span>
                                <span style="color:#2222FF;">ー</span>
                                <span style="color:#FF2222;">コ</span>
                                <span style="color:#FFFF22;">ー</span>
                                <span style="color:#22FFFF;">ド</span>
                                <span style="color:#FF22FF;">変</span>
                                <span style="color:#AA2222;">換</span>
                                <span style="color:#22AA22;">ツ</span>
                                <span style="color:#2222AA;">ー</span>
                                <span style="color:#AAAA22;">ル</span>
                            </a></h1>
                    </div>
                    <div style="width:80%; margin-left:10px; margin-bottom:0px; font-size:14px; "><img src="./img/icon_tag2.png" style="width:30px; margin-right:7px;">16進数のカラーコードを入力し、変換する！ボタンを押すと、その色を表示します。</div>
                </center>













                <div class="col-md-12">


                    <FORM name="frm_box" method="get" action="http://<?php echo DOMAIN_URL; ?>/index.php" >
                        <center>

                            <br>

                            <BLOCKQUOTE>
                                <div>

                                    <div>
                                        <div style="color:#33AA33; font-weight:bold;">入力（input）</div>
                                    </div>
                                    <div>
                                        <div bgcolor="#ccffcc" style="padding:10px; padding-left:20px; padding-right:3px;" >

<?php
//	echo '<textarea cols=' . MAX_X_TEXTAREA . ' rows=' . MAX_Y_TEXTAREA . ' type="text"  name="input_a_box" style="font-size:14px;">' . $input_a_box . "</textarea>";
echo '#' . '<INPUT  size="6"  type="text"  name="input_a_box" style="text-align:center; width:65px; font-size:14px; " value="' . $input_a_box . '">';
?>
                                            <br>
                                            <span style="width:80%; font-size:14px;">カラーコード（16進数6文字 半角、英数字）</span>
                                        </div>



                                        <div bgcolor="#AACCFF" style="padding:10px; margin-top:10px;" >
                                            <!--
                                            <input type="image" name="exec" value="" src="./img/search.jpg" style="border:0px;" alt="<?php echo TITLE; ?>" onmouseover="this.src='./img/search_on.jpg'" onmouseout="this.src='./img/search.jpg'" />
                                            -->
                                            <a href="javascript:frm_box.submit()" class="learn-btn animated fadeInUp" style="font-size:24px;">変換する！</a>

                                        </div>

                                        <br>


                                        <div style="color:#FF7777; font-weight:bold;">出力（output）</div>
                                        <div bgcolor="#ffcccc" style="padding:10px; padding-left:20px; padding-right:3px;" >

<?php
// エラーmsgない場合のみ 出力場所を表示
//echo "aaa[" . $err_msg_html . "]" ;
// 入力があってかつエラーなしの場合
if (( $input_a_box != "" ) and ( $err_msg_html == "" )) {
    if ($input_a_box > "888888") {
        echo '<div style="border:3px solid #555555; width:80%; height:60px; vertical-align:center; background-color:#' . $input_a_box . ' ;">#' . $input_a_box . 'の色です</span></div>';
    } else {
        echo '<div style="border:3px solid #555555; width:80%; height:60px; vertical-align:center; background-color:#' . $input_a_box . ' ;"><span style="color:#FFFFFF;">#' . $input_a_box . 'の色です</span></div>';
    }
}
?>

                                        </div>
		    <div style="clear:both;"></div>

                                        <!-- ads -->
                                        <div style="text-align:center; margin-top:10px; ">

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- colorhenkan.pw auto reponsive -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-8933330050465971"
     data-ad-slot="9349181841"
     data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
                                        </div>
                                        <!-- /ads -->

                                        <br>
                                    </div>

                                </div>
                            </BLOCKQUOTE>


                            <!-- twitter -->
                            <table style="font-size:12px;">
                                <tr>
                                    <td>
                                        この結果をツイートする→&nbsp;
                                    </td>
                                    <td>
                                        <a href="http://twitter.com/share" class="twitter-share-button" data-count="none" data-lang="ja">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
                                    </td>
                                </tr>
                            </table>
                            <!-- /twitter -->


                        </center>
                    </FORM>



                </div>
                <br/>






                <center>
                    <div class="col-md-12" style="margin-top:20px;">
                        <div class="sub_title"><img src="./img/icon_tag2.png" style="width:30px; margin-right:7px;">直近のカラーコード変換履歴</div>
                        <div class="rireki_box">
<?php
// // logファイルが存在している時のみ
// //if  ( file_exists( $output_file_name2 ) == TRUE ) {
// if (file_exists($fp_month) == TRUE) {
// //echo "iii[" . $output_file_name2 . "]" ;
// //	$f =file(  $output_file_name2 );
//     $f = file($fp_month);
//     krsort($f);
//     $i = 1;
//     foreach ($f as $key => $value) {
// //	 echo "key[" . $key ."]" ;
// //	 echo "value[" . $value ."]" ;
//         $v = split(",", $value);
//         print($v[0]);
//         if ($i > LOG_RECORD_CNT) {
//             break;
//         }
//         $i++;
//     }
// }

 if (file_exists($accese_history_file_month) == TRUE) {
     $f = file($accese_history_file_month);
     $i = 1;
     krsort($f);
     foreach ($f as $key => $value) {
         print($value);
         if ($i > LOG_RECORD_CNT) {
             break;
         }   
         $i++;
     }   
 }  


?>
                        </div>
                    </div>

                    <div style='font-size:12px;'><a href='http://<?php echo DOMAIN_URL; ?>/rireki.php' style="color:#333333;">→カラーコード変換履歴をもっとみる</a></div>
                    <br>

                    <div style='font-size:12px;'><a href='http://<?php echo DOMAIN_URL; ?>/rei.php' style="color:#333333;">→いろいろな色の例をみる（コーポレイトカラー・イメージカラー）</a></div>
                    <br>

                    <footer>
                        <div style="width:90%; font-size:12px; color:#888888; line-height:180%; ">指定したカラーコードを表示するサービス、ツールです。</div>
                        <div align="center"><font size=-1 style="color:#888888;">&copy;<?php echo date('Y') ?> <?php echo DOMAIN_URL; ?></font></div>

                    </footer>

                </center>

            </div>


            <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
            <script src="js_boxify/min/toucheffects-min.js"></script>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
            <script src="js_boxify/flickity.pkgd.min.js"></script>
            <script src="js_boxify/jquery.fancybox.pack.js"></script>
            <!-- Include all compiled plugins (below), or include individual files as needed -->
            <script src="js_boxify/retina.js"></script>
            <script src="js_boxify/waypoints.min.js"></script>
            <script src="js_boxify/bootstrap.min.js"></script>
            <script src="js_boxify/min/scripts-min.js"></script>
            <!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
        </body>
    </html>
