<main>
  <div class="container-fluid mt-3">
    <div class="row">
      <div class="col-10">
  <?php
  switch($op2) {

      case "php_save";
          
          file_put_contents($_POST['file_name'], $_POST['content']);
          $_GET['id']=$_POST['id'];
          edit($_POST['id']);
          break;
      case "edit";
        edit($_GET['id']);
        break;
      case "copy";
        $sql4 = "select id from `cp_filephp_2` order by id desc limit 1";
        $res4 = mysqli_query($db, $sql4);
        $rw4 = mysqli_fetch_assoc($res4);
        $id2=$rw4['id']+1;

        $sql3 = "select * from `cp_filephp_2` where id=".$_GET['id'];
        $res3 = mysqli_query($db, $sql3);
        $r3 = mysqli_fetch_assoc($res3);
        $idt=array(
          'id'=>$id2,
          //'modul'=>trim($r3['modul']),
          'slug'=>$r3['slug']."-",
          'folder'=>trim($r3['folder']),
          'param'=>trim($r3['param']),
          'method'=>trim($r3['method']),
          'php'=>gpc(trim($r3['php'])),
          'update_at'=>time(),
          'body'=>gpc(trim($r3['body'])),
        );
        Insert($db,"cp_filephp_2",$idt,0);
        edit($id2);
        break;
      case "edit2";
        //echo "<pre>"; print_r($_POST); echo "</pre>";
        $udt=array(
          //'modul'=>trim($_POST['modul']),
          'slug'=>$_POST['slug'],
          //'name'=>$_POST['name'],
          'param'=>trim($_POST['param']),
          'method'=>trim($_POST['method']),
          'folder'=>$_POST['folder'],
          //'file_jsapi'=>$_POST['file_jsapi'],
          'remark'=>trim($_POST['remark']),
          'php'=>gpc(trim($_POST['php'])),
          //'js'=>gpc(trim($_POST['js'])),
          //'js_api'=>gpc(trim($_POST['js_api'])),
          'body'=>gpc(trim($_POST['body'])),
          'update_at'=>time(),
          //'js_update_at'=>time(),
          //'jsapi_update_at'=>time(),
        );
        Update($db,"cp_filephp_2",$udt,"id=".$_POST['id'],0);
        $_GET['id']=$_POST['id'];
        edit($_POST['id']);
        break;
      case "list";
        list_filephp($q,$col,$q2,$col2,$q3,$col3);
      default;
    }          
          
  ?>
      </div>
      <div class="col-2 small">
<?php 
  $sql1 = "select `folder`,id,slug from `cp_filephp_2` where del=0 order  by `folder`,slug";
  $res1 = mysqli_query($db, $sql1);
  while($r1 = mysqli_fetch_assoc($res1)) {
    //$dt[$c]['folder']=$r1['folder'];
    if(isset($_GET['id']) && $_GET['id']==$r1['id']) {
      $sl='<b>'.substr($r1['folder'],16)."".$r1['slug'].'</b>';
    } else {
      $sl=substr($r1['folder'],16)."".$r1['slug'];
    }
    ?>
    <a href="?op=filephp&op2=edit&id=<?=$r1['id'];?>"><?=$sl;?></a><br>
    <?php
  }   

?>
        </div>
    </div>          
  </div>
</main>



<?php

function list_filephp($q,$col,$q2,$col2,$q3,$col3) {
  global $db,$ar;
  
  //print_r($ar);
  ?>
  
  <table class="table table-bordered" id="dataTable_5d" width="100%" cellspacing="0">
      <thead>
          <tr>
              <th>ID</th>
              <th>Folder</th>
              <th>Slug</th>
              <th>is_api</th>
              <th>remark</th>
              <th>PhpAt</th>
              <th>Menu</th>
          </tr>
      </thead>

      <tbody>
      <?php
      $sql = "select * from cp_filephp_2 where id>0 ";

      if($col!='') {
        $sql .= " and ".$col."='".$q."' ";
      }
      
      if($col2!='') {
        $sql .= " and ".$col2."='".$q2."' ";
      }

      if($col3!='') {
        $sql .= " and ".$col3."='".$q3."' ";
      }      
      
      //$sql .= " and st=1";
      $res = mysqli_query($db, $sql);
      //echo $sql;

      while($rw = mysqli_fetch_assoc($res)) {  
      ?>
      
          <tr>
              <td><?=$rw['id'];?></td>
              <td><a href="?op=filephp&col=folder&q=<?=$rw['folder'];?>"><?=$rw['folder'];?></a></td>
              <td><?=$rw['slug'];?></td>
              <td><?=$rw['type'];?></td>              
              <td><?=$rw['remark'];?></td>
              <td class="text-right"><?=date("Y-m-d H:i",$rw['php_update_at']);?></td>
              <td>
              
                <a class="btn btn-xs btn-primary" href="?op=filephp&op2=edit&id=<?=$rw['id'];?>">edit</a> 
                <a href="#" onClick="uCol(<?=$rw['id'];?>,'del',1,'cp_filephp');" class="btn btn-xs btn-color-red">del</a>
                <a href="#" onClick="uCol(<?=$rw['id'];?>,'type',0,'cp_filephp');" class="btn btn-xs btn-color-green">non-api</a>               
              </td>
          </tr>
        <?php
        }
        ?>  
          
      </tbody>
  </table>
  <br><?=$sql;?><br>
  <?php   
}

function edit($id) {
   global $db,$ar;    
  $sql = "select * from `cp_filephp_2` where id=".$id;
  $res = mysqli_query($db, $sql);
  //echo $sql;
  $rw = mysqli_fetch_assoc($res);
  $php_file=$ar['path_core'].''.$rw['folder'].''.$rw['slug'].'.php';
  if (file_exists($php_file)) {
    $filecontent=file_get_contents($php_file);
  } else {
    $filecontent="<?php

?>";      
  } 
  ?>
<div class="row">
  <div class="col-8">

  <form action="index.php?op=filephp&op2=edit2" method="post">
    <input type="hidden" name="id" value="<?=$id;?>">
    <div class="row">
      <div class="col-6">        
      </div>
      <div class="col-6 text-end">
        <?php if($rw['type']==1) { ?>
        <a href="tes_curl.php?id=<?=$id;?>&c=1" target="_blank" class="btn btn-xs btn-info">Tes cURL [1]</a>
      <?php } ?>
        <a class="btn btn-sm cl-dp5 mr-2" href="?op=filephp&op2=copy&id=<?=$id;?>">Copy This</a>
        <button type="submit" name="submit" class="btn btn-sm btn-primary">Save</button>
        
      </div>
    </div>

    <div class="row">
      <div class="col-3">
        <label for="folder" class="label">folder</label>
        <input type="text" class="form-control" name="folder" value="<?=$rw['folder'];?>">
        <label for="slug" class="label">slug</label>
        <input type="text" class="form-control" name="slug" value="<?=$rw['slug'];?>">          
        <label for="method" class="label">method</label>
        <input type="text" class="form-control" name="method" value="<?=$rw['method'];?>">                
        <label for="param" class="label">param</label>
        <textarea rows="4" id="param" class="form-control" name="param"><?=ugpc($rw['param']);?></textarea>          
        <label for="body" class="label">body</label>
        <textarea rows="4" id="body" class="form-control" name="body"><?=ugpc($rw['body']);?></textarea>
        <label for="remark" class="label">remark</label>
        <textarea rows="4" id="body" class="form-control" name="remark"><?=ugpc($rw['remark']);?></textarea>
      </div>
      <div class="col-3">
        <label for="php" class="label">php</label>         
        <textarea rows="20" id="php" class="form-control" name="php"><?=$rw['php'];?></textarea>
        <div id="editor-container-1" style="height:300px;"></div>
      </div>
      <div class="col-6"><pre name="body2" class="line-numbers" data-start="1" style="white-space:pre-wrap;"><code contenteditable class="language-javascript"><?=htmlspecialchars($filecontent);?></code></pre>
      </div>
    </div>
  </form>
  </div>
  <div class="col-4">
    <?php 
    ?>    
    <div class="card">
      <div class="card-body">
<?=$ar['path_core'];?>
        <h5 class="card-title"><?=$rw['folder'];?><?=$rw['slug'];?>.php</h5>
        <form method="post" action="index.php?op=filephp&op2=php_save">
          <input type="hidden" name="file_name" value="<?=$php_file;?>">
          <input type="hidden" name="id" value="<?=$rw['id'];?>">
          <textarea class="form-control" rows="20" name="content"><?=htmlspecialchars($filecontent);?></textarea>
          <input type="submit" name="submit" class="btn btn-success mt-2">
        </form>
      </div>
    </div>
  </div>
</div>
  <?php
  //echo "<pre>"; print_r($rw); echo "</pre>";
}

?>

/home/u1731472/public_html/
sub/cpapp_ekbis/inc/inc.filephp.php