<?php
require 'database.inc.php';
header('Content-Type: text/html; charset=utf-8');

function get_voip()
{
  return Config::getVOIP();
}
function send_sms_rappel_atelier($noatelier, $telephone, $vehicule, $date)
{
  $request = "UPDATE atelier" . ID_GARAGE . " SET DateRappel = '" . $date . "' WHERE NoAtelier = " . $noatelier;
  //echo $request;
  $smsconfig = get_config("SMS_CONFIG");
  if (strlen($smsconfig) != 0) {
    $result = file_get_contents(sprintf($smsconfig, urlencode($telephone), urlencode(sprintf(Config::getSMSRappelAtelier(), $vehicule))), false);
    if (strpos($result, 'OK') !== false) {
      echo "SMS envoyé avec succès !";
      Connection::executeRawQuery($request);
    } else {
      echo "Erreur d'envoit du SMS";
    }
  } else {
    $url = "https://www.voipbuster.com/myaccount/sendsms.php?" . get_voip() . "&to=$telephone&text=" . urlencode(sprintf(Config::getSMSRappelAtelier(), $vehicule));
    $result = file_get_contents($url, false);
    $dom = domxml_open_mem(trim($result));
    $root = $dom->document_element();
    //$request="UPDATE atelier".ID_GARAGE." SET Sms = '1' WHERE NoAtelier =".$no_atelier;
    if ($node_array = $root->get_elements_by_tagname('resultstring')) {
      $node = $node_array[0];
      $resultat = $node->get_content();
      if ($resultat == "success" || $resultat == "failure") {
        echo "SMS envoyé avec succès !";
        Connection::executeRawQuery($request);
      } else {
        echo "Erreur d'envoit du SMS";
      }
    }
  }
}
$noatelier = $_GET["no_atelier"];
$telephone = $_GET["no_tel"];
$vehicule = $_GET["vehicule"];
$date = $_GET["date"];
$values = array(".", " ", "/", ",", "-");
$telephone = str_replace($values, "", $telephone);
if (PHP_VERSION >= '5')
  require_once('lib/domxml-php4-to-php5.php');
if (substr($telephone, 0, 2) == "07") {
  $telephone = "+41" . substr($telephone, 1);
  send_sms_rappel_atelier($noatelier, $telephone, $vehicule, $date);
} else if (substr($telephone, 0, 4) == "+417") {
  send_sms_rappel_atelier($noatelier, $telephone, $vehicule, $date);
} else if (substr($telephone, 0, 4) == "+336") {
  send_sms_rappel_atelier($noatelier, $telephone, $vehicule, $date);
} else if (substr($telephone, 0, 5) == "00336") {
  send_sms_rappel_atelier($noatelier, $telephone, $vehicule, $date);
} else if (substr($telephone, 0, 4) == "+337") {
  send_sms_rappel_atelier($noatelier, $telephone, $vehicule, $date);
} else if (substr($telephone, 0, 5) == "00337") {
  send_sms_rappel_atelier($noatelier, $telephone, $vehicule, $date);
} else if (substr($telephone, 0, 5) == "00417") {
  send_sms_rappel_atelier($noatelier, $telephone, $vehicule, $date);
} else {
  echo "format téléphone non pris en compte " . $telephone;
}
//}
