<?php
  include("./control/session.inc");
  $c1 = $_POST['c1'];

function form()
{

  $c1 = $_POST['c1'];
  if($_POST['c1'] =="1")
  {

  $name = $_POST['name'];
  $meta_dscr = $_POST['meta_dscr'];
  $meta = $_POST['meta'];
  $head = $_POST['head'];
  $title = $_POST['title'];
  $content = $_POST['content'];

//コンテンツの名前 エラー処理

    if($_POST['name'] == "" or !preg_match( '/^[a-zA-Z0-9-_]+$/',$_POST['name'])){
    $fmess1 = "<font color=\"red\">●有効な文字列は半角英数と「-」「_」です。(入力必須)</font>";
    $check1 = "3";

    }elseif(preg_match( '/^[a-zA-Z0-9-_]+$/',$_POST['name'])){
      $file = $name.'.inc';
      foreach (glob('./content/'.$file) as $filename) {
      if($filename == './content/'.$file){
      $fmess1 = "<font color=\"red\">●既に使用されおります。別の文字を入力してください。</font>";
      $check1 = "2";
      }
    }

  }

  if($check1 != '2' and $check1 != '3'){
  $fmess1 = 'Good!';
  $check1 ='1';
  }

//titleタグ エラー処理

  if($_POST['title'] == ""){
  $fmess3 = "<font color=\"red\">●titleタグの内容です。全角30文字前後が目安です。(入力必須)</font>";
  $check3 ='2';
  }else{
  $fmess3 = 'Good!';
  $check3 ='1';
  }

//書き込み準備

  if($check1 == "1" and $check3 == "1"){

  $gif_an = <<<EOD
<div id="main_r1">
<h2>コンテンツの保存中です</h2>
<br>
<div class="loader"></div>…Creating
</div>
EOD;
  print("$gif_an");

  $c_conf = <<<KOEY
<?php

  \$fl_name = '$name';
  \$meta_dscr = '$meta_dscr';

  \$meta  = <<<EOD
$meta
EOD;

  \$head  = <<<EOD
$head
EOD;

  \$title = '$title';
?>
KOEY;

$c_text = <<<KOEY
{$content}
KOEY;

  $file1 = $name.'.php';
  $file2 = $name.'.inc';
  $dir = 'content/';

//書き出し処理

  file_put_contents($dir.$file1,$c_conf,LOCK_EX);
  file_put_contents($dir.$file2,$c_text,LOCK_EX);
  $usr = "creating.inc";
  header("Refresh: 2; URL=AYANASU_control.php?cname=creating_r&name={$name}&r=1");

    }else{
    include('./control/form.inc'); 
    print $form;
    }

//初期画面表示

  }elseif($_POST['c1'] ==""){
  $fmess1 = '使用可能な文字列は半角英数と「-」「_」です。';
  $fmess2 = '使用可能な文字列は半角英数と「-」「_」です。';
  $fmess3 = 'titleタグの内容です。全角30文字前後が目安です。';
  include('./control/form.inc'); 
  print $form;
  }


}//function form()

?>