<?php
include($_SERVER['DOCUMENT_ROOT']."/error_monitoring.php");
extract($_POST);extract($_GET);extract($_SERVER);
// error_reporting(E_ALL);
$kartka = 5;

   opcache_compile_file($_SERVER['DOCUMENT_ROOT'].'/dane.php');
   opcache_compile_file($_SERVER['DOCUMENT_ROOT'].'/error_monitoring.php');
   opcache_compile_file($_SERVER['DOCUMENT_ROOT']."/preload.php");

//Generuj ustawienia tresc   
   $jezyk = wykryj_wybrany_jezyk();

//Generuj ustawienia 
   $ustawienia = gen_ustawienia($jezyk);   
   $ustawienia['jezyk'] = $jezyk;


if(!empty($_GET['pobierz'])){ $pobierz = $_GET['pobierz']; }else{ $pobierz = ''; }

if(preg_match("#^([a-zA-Z0-9\-\_]{1,100})-pobierz\.(jpg|png|mp4)*$#", $link, $p_plik))
{ $id = $p_plik[1]; $pobierz = "tak"; $typ = $p_plik[2]; }
elseif(preg_match("#^([a-zA-Z0-9\-\_]{1,100})\.(jpg|png|mp4)*$#", $link, $p_plik))
{ $id = $p_plik[1]; $typ = $p_plik[2]; }


if(preg_match("#^([a-zA-Z0-9\-\_]{1,100})+$#", $id))
{


   if($typ=='mp4')
   { $adres_tmp = $_SERVER['DOCUMENT_ROOT']."/tmp/{$jezyk}/kar_{$id}.mp4"; $header = "Content-Type: video/mp4"; $koncowka = "mp4"; }
   else
   { $adres_tmp = $_SERVER['DOCUMENT_ROOT']."/tmp/{$jezyk}/kar_{$id}.jpg"; $header = 'Content-type: image/png'; $koncowka = "png";}



if(is_file($adres_tmp))
{
  if($pobierz=="tak")
  {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    //header('Content-type: image/png');
  }
  else
  { header($header); }

  header('Content-Length: '.filesize($adres_tmp));
  header('accept-ranges: bytes');
  echo file_get_contents($adres_tmp);
  //readfile($adres_tmp);
  exit;
}
else
{
   //Twórz katalogi 
   if(!is_dir($_SERVER['DOCUMENT_ROOT']."/tmp/")){ mkdir($_SERVER['DOCUMENT_ROOT']."/tmp/", 0777); }
   if(!is_dir($_SERVER['DOCUMENT_ROOT']."/tmp/{$jezyk}/")){ mkdir($_SERVER['DOCUMENT_ROOT']."/tmp/{$jezyk}/", 0777); }

      // echo "XX:$id $typ $adres_tmp";
   include("../funkcje/img_wspolne.php");
   // if($jezyk<>'pl'){ $ustawienia['sciezka_img'] = "$ustawienia[sciezka]/kartki_/en/"; }
   
   if(is_numeric($id)){ $sql_dop =  "OR id='$id'";  }else{ $sql_dop = ''; }

   $row = mysqli_fetch_assoc(mysqli_query(sql(), "SELECT * FROM $ustawienia[tabela_kartki] WHERE skrut='$id' $sql_dop"));
   $nazwa_plik = $row['skrut'];
   $tresc = $row['tresc'];
   if(!empty($row['id']))
   {

        $tresc = str_replace('\"', '"', $tresc);
        $tresc = str_replace("\'", "'", $tresc);
        if($row['kartka_typ']=='fil')
        {

          parse_str($row['arr'], $T_array);
          $row_schemat = mysqli_fetch_assoc(mysqli_query(sql(),"SELECT id_baza FROM `kartki_schematy_swiat` WHERE `jezyk`='$ustawienia[jezyk]' AND id='$row[kartka]'"));
          
          // print_r($T_array);
           $params = [
    "strona"=> "createfreecard_com",
    "kartka" => $row_schemat['id_baza'],
    "jezyk" => $jezyk,
    "tresc" => $T_array['tresc'],
    "tresc2" => $T_array['tresc2'],
    "tresc3" => $T_array['tresc3'],
    "tresc4" => $T_array['tresc4'],
    "numer" => $T_array['numer'],
    "numer2" => $T_array['numer2'],
    "numer3" => $T_array['numer3'],
    "numer4" => $T_array['numer4'],
    "numer5" => $T_array['numer5']
];

      $adres_pobierz = "http://".U_wybierz_serwer('fil')."/pliki/kartki_filmiki/gen.php?".http_build_query($params);
      // $adres_pobierz = "http://strony.panelek.com/pliki/kartki_filmiki/gen.php?".http_build_query($params);
      // $adres_pobierz = "http://162.55.153.250/pliki/kartki_filmiki/gen.php?".http_build_query($params);
      $tresc_plik = file_get_contents($adres_pobierz);   
      if(!preg_match('/(ftypisom)/', substr($tresc_plik, 0, 30)))  // zamknij jeśli plik nie jest mp4
      { 
        header('Cache-Control: no-cache, no-store, must-revalidate'); // Zapobiega buforowaniu
        header('Pragma: no-cache'); // Dla starszych przeglądarek
        header('Expires: 0'); // Ustala datę wygaśnięcia na przeszłość
        header('CF-Cache-Status: DYNAMIC'); // Wymusza na Cloudflare traktowanie pliku jako dynamicznego         
        trigger_error("CREATE ERROR ftypisom: $adres_pobierz"); 
        exit; 
      }
           file_save($adres_tmp, $tresc_plik);
           if(filesize($adres_tmp)<2000){ unlink($adres_tmp); }
           header($header);
           if($pobierz=='tak')
           { 
              header('Content-Description: File Transfer');
              header('Content-Type: application/octet-stream');
              header('Content-Disposition: attachment; filename="creategreetingcards.eu-'.$nazwa_plik.'.'.$koncowka.'"');
              header('Content-Transfer-Encoding: binary');
              header('Connection: Keep-Alive');
              header('Expires: 0');
              header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
              header('Pragma: public');
           }
           else
           {
             
           }
           header('Content-Length: '.filesize($adres_tmp)); 
           header('accept-ranges: bytes');
           echo $tresc_plik; exit;
    
        }
        elseif($row['kartka_typ']=='v2')
        {

          $row_schemat = mysqli_fetch_assoc(mysqli_query(sql(),"SELECT id_baza FROM `kartki_schematy_swiat` WHERE `jezyk`='$ustawienia[jezyk]' AND id='$row[kartka]'"));
          parse_str($row['arr'], $T_array);
           $params = [
    "strona"=> "createfreecard_com",
    "kartka" => $row_schemat['id_baza'],
    "base64" => 'nie',
    "jezyk" => $jezyk,
    "tresc" => $T_array['tresc'],
    "tresc2" => $T_array['tresc2'],
    "tresc3" => $T_array['tresc3'],
    "tresc4" => $T_array['tresc4'],
    "numer" => $T_array['numer'],
    "numer2" => $T_array['numer2'],
    "numer3" => $T_array['numer3'],
    "numer4" => $T_array['numer4'],
    "numer5" => $T_array['numer5']
];

           $adres = "http://".U_wybierz_serwer('v2')."/pliki/kartki_v2/gen.php?".http_build_query($params); 
           // $adres = "http://strony.panelek.com/pliki/kartki_v2/gen.php?".http_build_query($params); 
           // $adres = "http://162.55.153.250/pliki/kartki_v2/gen.php?".http_build_query($params); 
           $tresc_plik = file_get_contents($adres);   
           file_save($adres_tmp, $tresc_plik);
           list($width, $height) = getimagesize($adres_tmp);
           if($width<100 OR $height<100){ unlink($adres_tmp); trigger_error("CREATE v2 ERROR: $adres"); }
           header($header);
           if($pobierz=='tak')
           { 
              header('Content-Description: File Transfer');
              header('Content-Type: application/octet-stream');
              header('Content-Disposition: attachment; filename="creategreetingcards.eu-'.$nazwa_plik.'.'.$koncowka.'"');
              header('Content-Transfer-Encoding: binary');
              header('Connection: Keep-Alive');
              header('Expires: 0');
              header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
              header('Pragma: public');
           }
           
           echo $tresc_plik; exit;
    
        }
        else
        {
         $row_schemat = mysqli_fetch_assoc(mysqli_query(sql(),"SELECT id_baza FROM `kartki_schematy_swiat` WHERE `jezyk`='$ustawienia[jezyk]' AND id='$row[kartka]'"));
    $params = [
    "typ" => 'katalog_kartka',
    "strona"=> "createfreecard_com",
    "kartka" => $row_schemat['id_baza'],
    "base64" => 'nie',
    "jezyk" => $jezyk,
    "tresc" => $row['tresc']
];

          $adres = "http://".U_wybierz_serwer('v1')."/pliki/kartki_v1/gen.php?".http_build_query($params);  

          // $adres = "http://strony.panelek.com/pliki/kartki_v1/gen.php?".http_build_query($params);  
          $tresc_plik = file_get_contents($adres); 
          // $tresc_plik = file_get_contents("http://162.55.153.250/pliki/kartki_v1/gen.php?".http_build_query($params)); 

           file_save($adres_tmp, $tresc_plik);
           list($width, $height) = getimagesize($adres_tmp);
           if($width<100 OR $height<100){ /*unlink($adres_tmp); */ trigger_error("CREATE v1 W:$width oraz H:$height ERROR: $adres"); exit; }

           header($header);
           if($pobierz=='tak')
           { 
              header('Content-Description: File Transfer');
              header('Content-Type: application/octet-stream');
              header('Content-Disposition: attachment; filename="creategreetingcards.eu-'.$nazwa_plik.'.'.$koncowka.'"');
              header('Content-Transfer-Encoding: binary');
              header('Connection: Keep-Alive');
              header('Expires: 0');
              header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
              header('Pragma: public');
           }          
           echo $tresc_plik;  exit;      
        }   
           echo $tresc_plik;  exit;

   }
}
}

function file_save($file, $data)
{
 $handle = @fopen($file,"w+");
 fputs($handle, $data);
 fclose($handle);
}







?>