<?php
session_start();
include "../../inc/config.php";
session_check();
function uniqueName($file_name)
{
    $filename = $file_name;
    $filename = preg_replace("#[^a-z.0-9]#i", "", $filename);
    $ex       = explode(".", $filename); // split filename
    $fileExt  = end($ex); // ekstensi akhir
    $filename = time() . rand() . "." . $fileExt; //rename nama file';
    return $filename;
}
switch ($_GET["act"]) {
  case "in":
  
  if (!is_dir("../../../upload/logo")) {
              mkdir("../../../upload/logo");
            }
  
  $data = array();
  
  
                    if (!preg_match("/.(png|jpg|jpeg|gif|bmp)$/i", $_FILES["isi_gambar"]["name"]) ) {

              echo "pastikan file yang anda pilih png|jpg|jpeg|gif";
              exit();

            } else {
$db->compressImage($_FILES["isi_gambar"]["type"],$_FILES["isi_gambar"]["tmp_name"],"../../../upload/logo/",$_FILES["isi_gambar"]["name"],200,200);
            $logo = array("logo"=>$_FILES["isi_gambar"]["name"]);
              $data = array_merge($data,$logo);
            }
  
   
    $in = $db->insert("logo",$data);
    
    if ($in=true) {
      echo "good";
    } else {
      return false;
    }
    break;
  case "delete":
    
    
    //$db->deleteDirectory("../../../upload/logo/".$db->fetch_single_row("logo","id",$_POST["id"])->logo);
    
    $db->delete("logo","id",$_GET["id"]);
    break;
  case "up":
   $data = array();
   
/*   
                         if(isset($_FILES["isi_gambar"]["name"])) {
                        if (!preg_match("/.(png|jpg|jpeg|gif|bmp)$/i", $_FILES["isi_gambar"]["name"]) ) {

              echo "pastikan file yang anda pilih gambar";
              exit();

            } else {
$db->compressImage($_FILES["isi_gambar"]["type"],$_FILES["isi_gambar"]["tmp_name"],"../../../upload/logo/",$_FILES["isi_gambar"]["name"],200,200);
              $logo = array("logo"=>$_FILES["isi_gambar"]["name"]);
              $data = array_merge($data,$logo);
            }

                         }*/
   
    if($_POST["isi_gambar"]!="")
    {
     $gambar = $_POST["isi_gambar"];

     $image_array_1 = explode(";", $gambar);

     $image_array_2 = explode(",", $image_array_1[1]);

     $gambar = base64_decode($image_array_2[1]);

     //$imageName = time() . '.png';
     $imageName = uniqueName('gambar.png');

     file_put_contents("../../../upload/logo/".$imageName, $gambar);
     //copy($data, "../../../upload/back_profil_foto/".$imageName);
     $data['logo'] = $imageName;

      $up = $db->update("logo",$data,"id",$_POST["id"]);
    }
     echo "good";
    break;
  default:
    # code...
    break;
}

?>