<?php
if(isset($_POST) == true) {
    require_once("hivasok.php");

    $config['db_host']='localhost';
    $config['db_user']='cadline';
    $config['db_pass']='Shea6hoo';
    $config['db_name']='cl_hlusers';

    $msh = new hivasok();
    $msh->init($config['db_host'], $config['db_name'], $config['db_user'], $config['db_pass']);
    $cntry = $_POST['orszag'];


    //Utolsó order Number
    $msh->query("SELECT `order` FROM `o_orders` WHERE `country` = '{$cntry}' ORDER BY `id` DESC limit 1");
    $order=$msh->fetchNext();
    $orderNumber = $order['order'];
    // Majd a rákövetkező, ez kerül be adatbázisba
    $kezdo = strpos($orderNumber, "-")+1;
    $veg = strpos($orderNumber, "/")-2;
    $szam = (int)substr($orderNumber, $kezdo, $veg)+1;
    $prOrderNumber = "E-".$szam."/".date("Y");

    $msh->query("SELECT `ctrNameEng` FROM `countries` WHERE `ctrID` = '{$cntry}'");
    $ctryName = $msh->fetchNext();
    $orszagNev = $ctryName['ctrNameEng'];



    //Fájlnév
    // $temp = explode(".", $_FILES["file"]["name"]); <-- Ha megváltozik a fájl neve, akkor a kiterjesztés itt lesz
    // $fileName = $fajlNev.'.'.end($temp);

    $fileName = $_FILES["file"]["name"];

    //Célmappa
    $targetDir = "../../";
    $targetFilePath = $targetDir.$fileName;

    // Engedélyezett fájlkiterjesztések
    $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
    $allowTypes = array('jpg', 'png', 'jpeg', 'gif', 'pdf', 'docx', 'doc', 'txt', 'bmp');

    if (in_array($fileType, $allowTypes)) {
        // Feltöltés a szerverre
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)) {
            // $eredmeny['sikeres'] = "Sikeres felöltés";
        }
        else {
            $eredmeny['hiba'] = "Error in file uploading";
            echo json_encode($eredmeny);
            return false;
        }
    }
    else {
        $eredmeny['hiba'] = "Unknown file extension";
        echo json_encode($eredmeny);
        return false;
    }


      //recipient
      $to = 'office@cadline.hu';
      //sender
      $from = 'partnering@archlinexp.com';
      $fromName = 'ARCHLineXP Retail Store Enrollment Certification';

      //email subject
      $subject = 'Attachment for order: '.$orszagNev.' '.$prOrderNumber.' Non-profit';

      //attachment file path
      $file = $targetFilePath;

      //email body content
      $htmlContent = '<h1>Enrollment Certification</h1>';

      //header for sender info
      $headers = "From: $fromName"." <".$from.">";

      //boundary
      $semi_rand = md5(time());
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

      //headers for attachment
      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

      //multipart boundary
      $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";

      //preparing attachment
      if(!empty($file) > 0){
          if(is_file($file)){
              $message .= "--{$mime_boundary}\n";
              $fp =    @fopen($file,"rb");
              $data =  @fread($fp,filesize($file));

              @fclose($fp);
              $data = chunk_split(base64_encode($data));
              $message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" .
              "Content-Description: ".basename($files[$i])."\n" .
              "Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" .
              "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
          }
      }
      // $message .= "--{$mime_boundary}--";
      $returnpath = "-f" . $from;

      //send email
      $mail = @mail($to, $subject, $message, $headers, $returnpath);

      //email sending status
      // echo $mail?"<h1>Mail sent.</h1>":"<h1>Mail sending failed.</h1>";

      unlink($targetFilePath);

}
?>
