<?php

include './conn.php';

class SMS {

    function PostToHost($host, $path, $data_to_send) {
        // Verwendet HTTPS
        $fp = fsockopen('ssl://' . $host, 443);
        fputs($fp, "POST $path HTTP/1.1\r\n");
        fputs($fp, "Host: $host\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: " . strlen($data_to_send) . "\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $data_to_send);
        $res = "";
        while (!feof($fp)) {
            $res .= fgets($fp, 128);
        }
        fclose($fp);
        return $res;
    }

    function SendSMS($user, $pw, $jobid, $Msg, $Rcp, $MsgTyp, $Sender) {

        $Msg = rawurlencode(utf8_encode($Msg));
        $Msg = str_replace("%C2%80", "%E2%82%AC", $Msg);
        $data = "User=$user&Password=$pw&Caption=" . rawurlencode($jobid) .
                "&Sender=" . rawurlencode($Sender) . "&SMSText=" . $Msg .
                "&Recipient=" . rawurlencode($Rcp) . "&SmsTyp=$MsgTyp&SendDate=";
        $x = $this->PostToHost("www.smscreator.de", "/gateway/Send.asmx/SendSimpleSMS", $data);
        return true;
    }

    public function SMS_SEND($confirm_code) {

        $fetchConf = $GLOBALS['mysqli']->query('SELECT * FROM conf1') or trigger_error($GLOBALS['mysqli']->error);
        while ($row = $fetchConf->fetch_array()) {
            $GLOBALS[$row["name"]] = $row['val'];
        }

        $sql = "SELECT id, order_no ,confirm_code ,DATE_FORMAT(pick_up_date,'%d.%m.%Y') AS pickupdate,TIME_FORMAT(pick_up_time,'%H:%i') AS pickuptime,pickup ,salutation ,surname ,(SELECT name FROM city WHERE id = city) AS city ,(SELECT code FROM zip WHERE id = zip) AS zip,(SELECT name FROM street WHERE id = street) AS street ,house_number ,phone ,car_type ,payment ,price  FROM orders  WHERE confirm_code = '" . $confirm_code . "'";
        $fetcsql = $GLOBALS['mysqli']->query($sql) or trigger_error($GLOBALS['mysqli']->error);
        $row = mysqli_fetch_array($fetcsql, MYSQLI_ASSOC);
        $ord_confirm_code = $row['confirm_code'];
        $ord_pickupdate = $row['pickupdate'];
        $ord_pickuptime = $row['pickuptime'];
        $ord_pickup = $row['pickup'];
        $ord_salutation = $row['salutation'];
        $ord_surname = $row['surname'];
        $ord_city = $row['city'];
        $ord_zip = $row['zip'];
        $ord_street = $row['street'];
        $ord_house_number = $row['house_number'];
        $ord_phone = $row['phone'];
        $ord_car_type = $row['car_type'];
        $ord_payment = $row['payment'];
        $ord_price = $row['price'];


        if ($ord_car_type == 'L') {
            $car = "Limousine";
        } else if ($ord_car_type == 'K') {
            $car = "Kombi";
        } else if ($ord_car_type == 'V') {
            $car = "Minivan";
        } else {
            $car = "Limousine";
        }

        if ($ord_pickup == 'ZUM') {
            $pickup = "zum Flughafen";
        } elseif ($ord_pickup == "VOM") {
            $pickup = "vom Flughafen";
        } else {
            $pickup = "";
        }

        $poruka = "Bestellbestätigung\n" . $ord_pickuptime . " " . $pickup . "\nAbholdatum: " . $ord_pickupdate . "\n" . $ord_street . " " . $ord_house_number . ", " . $ord_zip . " " . $ord_city . "\n" . $car . " Preis: " . $ord_price . "\n" . $ord_salutation . "  " . $ord_surname . "\nMfg ihr Airport Cab Team";

        $this->SendSMS($GLOBALS['sms_user'], $GLOBALS['sms_password'], $pickup, $poruka, $ord_phone, '18', $GLOBALS['sms_send_name']);


//        $url = 'http://www.smscreator.de/gateway/Send.asmx/SendSimpleSMS';
//        $fields = array(
//            'User' => urlencode($GLOBALS['sms_user']),
//            'Password' => urlencode($GLOBALS['sms_password']),
//            'Caption' => urlencode($pickup),
//            'Sender' => urlencode($GLOBALS['sms_send_name']),
//            'Recipient' => urlencode($ord_phone),
//            'SMSText' => urlencode($poruka),
//            'SMSTyp' => urlencode('6'),
//            'SendDate' => ('')
//        );
//        $fields_string = '';
//        foreach ($fields as $key => $value) {
//            $fields_string .= $key . '=' . $value . '&';
//        }
//        rtrim($fields_string, '&');
//        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_URL, $url);
//        curl_setopt($ch, CURLOPT_POST, count($fields));
//        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//        $result = curl_exec($ch);
//        curl_close($ch);
    }

//NEWWWWWWWWW
}
