<?php

/*******************************************************************************
 * bejelentkezes: www.archlinexp.com/maintenance/al.php?action=login&lang=hun|eng|ger|...
 * selectprogram: www.archlinexp.com/maintenance/al.php?action=selectprogram&lang=hun|eng|ger|...
 * selectprogramdialog: www.archlinexp.com/maintenance/al.php?action=selectprogramdialog&lang=hun|eng|ger|...
 ******************************************************************************/
error_reporting(E_ERROR | E_PARSE);
require_once('config.php');
require_once('libraries/phpmailer/PHPMailerAutoload.php');

$action = trim(strtolower($_GET['action']));
$lang = trim(strtolower($_GET['lang']));

switch ($action) {
  case 'login': {
      header("Location: https://www." . ($lang == 'hun' ? 'archline.hu/maintenance/al_login.php' : 'archlinexp.com/user'));
      exit;
      break;
    }
  case 'selectprogram': // xml output
    {
      require_once("class/selectprogram.class.php");
      $sp = new SelectProgram();
      $namirialNonProfit = false;
      $BIMLadderNonProfit = false;
      if (isset($_GET['parnterID']) && $_GET['parnterID'] == '2nt')
        $namirialNonProfit = true;
      else if (isset($_GET['parnterID']) && $_GET['parnterID'] == 'kbl')
        $BIMLadderNonProfit = true;
      $sp->getXML($_GET['pwd'], $_GET['npid'], $_GET['isid'], $namirialNonProfit, $BIMLadderNonProfit, $_GET['userid'], $_GET['commercial']);
      break;
    }
  case 'selectprogramdialog': // html output
    {
      /*require_once("class/selectprogramdialog.class.php");
      $spd = new SelectProgramDialog();
      $spd->getPrograms($_GET['pwd'], $_GET['userid']);*/
      break;
    }
  case 'checknonprofitexists': // xml output
    {
      require_once("class/selectprogram.class.php");
      $sp = new SelectProgram();
      $sp->checkNonProfitExists($_GET['npid'], $_GET['isid']);
      break;
    }
  case 'email': {
      if (isset($_GET["name"]) && isset($_GET["email"]) && isset($_GET["telephone"]) && isset($_GET["company"]) && isset($_GET["nonprofit"])) {

        $isEmailForNamirial = true;
        if (isset($_GET["parnterID"])) {
          if ($_GET["parnterID"] == "2nt")
            $isEmailForNamirial = true;
          else if ($_GET["parnterID"] == "kbl")
            $isEmailForNamirial = false;
        }
        $nev = base64_decode($_GET["name"]);
        $email = base64_decode($_GET["email"]);
        $phone = base64_decode($_GET["telephone"]);
        $company = base64_decode($_GET["company"]);
        $nonprofit = base64_decode($_GET["nonprofit"]);
        $year = isset($_GET["year"]) ? base64_decode($_GET["year"]) : "2018";
        $date = date('Y-m-d H:i:s', time());

        if ($nonprofit == 0) {
          if ($isEmailForNamirial)
            $message = "ARCHLine.XP NamirialBIM: New trial user\n";
          else
            $message = "ARCHLine.XP BIMLadder: New trial user\n";
        } else {
          if ($isEmailForNamirial)
            $message = "ARCHLine.XP NamirialBIM: New registered user\n";
          else
            $message = "ARCHLine.XP BIMLadder: New registered user\n";
        }

        $message .= $nev;
        $message .= "\n";
        $message .= $email;
        $message .= "\n";
        $message .= $phone;
        $message .= "\n";
        $message .= $company;
        $message .= "\n";

        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->CharSet = 'UTF-8';
        $mail->Host = $mailconfig['Host'];
        $mail->Username = $mailconfig['Username'];
        $mail->Password = $mailconfig['Password'];
        $mail->SMTPSecure = $mailconfig['SMTPSecure'];
        $mail->Port = $mailconfig['Port'];
        $mail->isHTML(TRUE);
        $mail->setFrom(DEFAULT_EMAIL);
        $mail->addAddress('support@cadline.hu');
        if ($isEmailForNamirial) {
          $mail->addBCC('register.archline@edilizianamirial.it');
          $mail->addBCC('info@cadlinesw.com');
        } else {
          //ba biztonsagi okokbol, hogy a kesobbiekben is vissza tudjuk keresni
          $mail->addCC('cadalog@gmail.com');
        }

        if ($nonprofit == 0) {
          if ($isEmailForNamirial)
            $mail->Subject = 'ARCHLine.XP NamirialBIM ' . $year . ' user registration - trial mode';
          else
            $mail->Subject = 'ARCHLine.XP BIMLadder ' . $year . ' user registration - trial mode';
        } else {
          if ($isEmailForNamirial)
            $mail->Subject = 'ARCHLine.XP NamirialBIM ' . $year . ' user registration - registered mode';
          else
            $mail->Subject = 'ARCHLine.XP BIMLadder ' . $year . ' user registration - registered mode';
        }

        $mail->msgHTML($message);
        $mail->AltBody = strip_tags($message);

        if ($mail->send()) {
          if (!$isEmailForNamirial)
            print 'Sent';  //ba namirial eseten kesobb jon egy header, nem szabad kiirni semmit, kulonben nem mukodik!
        }
        unset($mail);

        if ($isEmailForNamirial) {
          $namirialAction = $nonprofit == 0 ? 'trial-version' : 'registered-version';

          $url  = 'https://manager.cadlinesw.com/cadline-data/';
          $url .= '?msg=NamirialBIM';
          $url .= '&email=' . urlencode($email);
          $url .= '&name=' . urlencode($nev);
          $url .= '&phone=' . urlencode($phone);
          $url .= '&action=' . urlencode($namirialAction);
          $url .= '&business=' . urlencode($company);
          $url .= '&year=' . urlencode($year);

          $ch = curl_init();
          $timeout = 10;
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $data = curl_exec($ch);
          curl_close($ch);
          print "Sent";
          //header("Location: ".$url);
          //exit;
        }
        break;
      }
    }
}
$txt = "Időpont: " . date('Y-m-d H:i:s', time()) . "   IP: " . $_SERVER['REMOTE_ADDR'] . "\n";
error_log($txt, 3, "../tmp/al_log.txt");
