<? 
if (empty($STANICA)): $STANICA="NO NAME"; endif;
// ***************************
// *** EXPORT DAT PRO SOZA ***
// ***************************

// ***************************
// ***    NASTAVENI ZDE    ***
// ***************************

$odesilatel="rp8@mysoft.cz";
$prijemci="radek@hemelik.cz, radek@hemelik.com";
$dopis="Dobry den,\n\nzasilame Vam v priloze report stanice $STANICA za predchozi mesic.\n\nHezky den\n$STANICA";

// ***************************
// ***   KONEC NASTAVENI   ***
// ***************************

// ************************************************
// ******** NEMENIT NASLEDUJICI RADKY !!!! ********
// ************************************************

// Init
$start=date("U");
error_reporting(E_ALL);
// Definice obsahu
$def =
  array(
  array("ISRC",			"C", 20),
  array("SKLADBA",		"C", 64),
  array("MINUTY",		"N",  3, 0),
  array("SEKUNDY",		"N",  2, 0),
  array("AUTORI",		"C", 64),
  array("VYDAVATEL",	"C", 64),
  array("VYROBCA",		"C", 64),
  array("ROKVYROBY",	"N",  4, 0),
  array("INTERPRETI",	"C", 64),
  array("TITUL",		"C", 64),
  array("CISLONOSIC",	"C", 16),
  array("STANICA",		"C", 10),
  array("DATUMVYS", 	"D"),
  array("RELACIA",		"C", 32),
  array("TYP_POUZ",		"C",  2),
);

$stopka="NE";

// Provedení akcí
if (!file_exists("insert.txt")):
  echo "INSERT NENI\n"; 
else: 
  echo "INSERT JE!\n"; 
  $insert="ANO"; 
  $data= file("insert.txt");
endif;

if ($insert=="ANO"):
  // smyčka souborů
  for ($ifiles=0;$ifiles<count($data);$ifiles++):
    $soubor=substr($data[$ifiles],0,strlen($data[$ifiles])-2);
	if (file_exists($soubor)):
      $data2= file($soubor); // otevřeme data
      $nazev=substr($soubor,0,strpos($soubor,".")).".dbf";
	  echo "Soubor: $soubor (".count($data2)." zaznamu)\n";
      if (file_exists($nazev)): unlink($nazev); endif; // smazat soubor výstupních dat, abychom nepřidávali!
      // Databáze
      $databaze = @dbase_open("./" . $nazev, 2);
      //Pokud se nepodařilo otevřít zadaný soubor, vypíšeme chybové hlášení,
      if (!$databaze): 
        echo "Nepodarilo se otevrit databazovy soubor $nazev! Vytvorim jej.\n";
        // creation
        if (!dbase_create($nazev, $def)):
          echo "Nelze vytvorit databazi $nazev!\n";
        else:
          echo "Databaze $nazev byla vytvorena.\n";
          $databaze = @dbase_open("./" . $nazev, 2);
        endif;
      endif;
      // DATA
      for ($i=0;$i<count($data2);$i++):
        // TEXT
        $txt = $data2[$i];
        $ISRC=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $SKLADBA=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $MINUTY=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $SEKUNDY=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $AUTORI=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $VYDAVATEL=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $VYROBCA=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $ROKVYROBY=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $INTERPRETI=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $TITUL=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $CISLONOSIC=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $STANICA=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $DATUMVYS=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $RELACIA=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
        $TYPPOUZ=substr($txt,0,strpos($txt,"|")); 
        $txt=substr($txt,strpos($txt,"|")+1,strlen($txt));
		// validace délek dat
		$ISRC=substr($ISRC,0,20);
		$SKLADBA=substr($SKLADBA,0,64);
		$MINUTY=substr($MINUTY,0,3);
		$SEKUNDY=substr($SEKUNDY,0,2);
		$AUTORI=substr($AUTORI,0,64);
		$VYDAVATEL=substr($VYDAVATEL,0,64);
		$VYROBCA=substr($VYROBCA,0,64);
		$ROKVYROBY=substr($ROKVYROBY,0,4);
		$INTERPRETI=substr($INTERPRETI,0,64);
		$TITUL=substr($TITUL,0,64);
		$CISLONOSIC=substr($CISLONOSIC,0,16);
		$STANICA=substr($STANICA,0,10);
		$DATUMVYS=substr($DATUMVYS,0,8);
		$RELACIA=substr($RELACIA,0,32);
		$TYPPOUZ=substr($TYPPOUZ,0,2);
        // Definice záznamu
        $zaznam = array(
        "$ISRC",
        "$SKLADBA",
        "$MINUTY",
        "$SEKUNDY",
        "$AUTORI",
        "$VYDAVATEL",
        "$VYROBCA",
        "$ROKVYROBY",
        "$INTERPRETI",
        "$TITUL",
        "$CISLONOSIC",
        "$STANICA",
        "$DATUMVYS",
        "$RELACIA",
        "$TYPPOUZ");
        // Vložení záznamu
        dbase_add_record ($databaze, $zaznam);
      endfor;
    else:
	  echo "Soubor $soubor nebyl nalezen!";
	endif; // soubor nalezen
    // Zavření databáze
    dbase_close($databaze);
	// Odeslání dat
	$envelope["from"] = "$odesilatel";
    $envelope["to"] = "$prijemci";
    $part1["type"] = TYPEMULTIPART;
    $part1["subtype"] = "mixed";
    $f = fopen ($nazev, "r");
    $obsah = fread ($f, filesize($nazev));
    fclose ($f);
    $part2["type"] = TYPEAPPLICATION;
    $part2["encoding"] = ENCBINARY;
    $part2["subtype"] = "octet-stream";
    $part2["description"] = basename($nazev);
    $part2["contents.data"] = "$obsah";
    $part3["type"] = TYPETEXT;
    $part3["subtype"] = "plain";
    $part3["description"] = "text";
    $part3["contents.data"] = $dopis;
    $body[1] = $part1;
    $body[2] = $part2;
    $body[3] = $part3;
    $zprava = imap_mail_compose ($envelope, $body);
    $pom = StrPos($zprava, "\r\n\r\n");
    $hlavicka = SubStr($zprava, 0, $pom+2);
    $telo = SubStr($zprava, $pom, StrLen($zprava) - $pom);
	Mail($prijemci,"SOZA - report $STANICA - $nazev",$telo,$hlavicka);
    // Konec smyčky souborů
  endfor;
endif;

$stop=date("U");
echo "Doba trvani: ".($stop-$start);