<?php session_start();
    if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
        include ('../../../scripts/classes.php');
            if($_SERVER['REQUEST_METHOD'] != 'POST'){exit("oups...");} 
            if ($_SESSION['lang']=="EN"){$lang="EN"; include ('../../../lang/en.php');}elseif ($_SESSION['lang']=="GR"){$lang="GR";include ('../../../lang/gr.php');}else {$lang="EN";include ('../../../en.php');}
            
            $user_id = Core::GetUserId();
            
            $array = getGlobalSettings(); foreach ($array as $settings){extract($settings);}   
    		$array_sms = getSMSSettings(); foreach ($array_sms as $sms_settings){extract($sms_settings);}                                                                      
            $sms_credits = getSMSBalance();
            ##### check if balance is still sufficient
            if ($sms_credits == 0) {$cannot_send = 1;}
            
            if ($cannot_send != 1){
                $phone = $_POST['phone'];
                $phone_prefix = $phones_country_pre.$phone;
                $sender = $_POST['sender'];
                $sms_text = $_POST['message'];
                $message = $sms_text;
                        $message = str_replace("%","%25",$message,$i);
						$message = str_replace(" ","+",$message,$i);
						$message = str_replace("Α","A",$message,$i);
						$message= str_replace("Ά","A",$message,$i);
						$message= str_replace("Β","B",$message,$i);
						$message = str_replace("Γ","%13",$message,$i);
						$message = str_replace("Δ","%10",$message,$i);
						$message = str_replace("Ε","E",$message,$i);
						$message = str_replace("Έ","E",$message,$i);
						$message = str_replace("Ζ","Z",$message,$i);
						$message = str_replace("Η","H",$message,$i);
						$message = str_replace("Ή","H",$message,$i);
						$message = str_replace("Θ","%19",$message,$i);
						$message = str_replace("Ι","I",$message,$i);
						$message = str_replace("Ί","I",$message,$i);
						$message = str_replace("Κ","K",$message,$i);
						$message = str_replace("Λ","%14",$message,$i);
						$message = str_replace("Μ","M",$message,$i);
						$message = str_replace("Ν","N",$message,$i);
						$message = str_replace("Ξ","%1A",$message,$i);
						$message = str_replace("Ο","O",$message,$i);
						$message = str_replace("Ό","O",$message,$i);
						$message = str_replace("Π","%16",$message,$i);
						$message = str_replace("Ρ","P",$message,$i);
						$message = str_replace("Σ","%18",$message,$i);
						$message = str_replace("ς","%18",$message,$i);
						$message = str_replace("Τ","T",$message,$i);
						$message = str_replace("Υ","Y",$message,$i);
						$message = str_replace("Ύ","Y",$message,$i);
						$message = str_replace("Φ","%12",$message,$i);
						$message = str_replace("Χ","X",$message,$i);
						$message = str_replace("Ψ","%17",$message,$i);
						$message = str_replace("Ω","%15",$message,$i);
						$message = str_replace("Ώ","%15",$message,$i); 
                        $message = str_replace("€","%80",$message,$i);
                        $message = str_replace("@","%40",$message,$i);
                        $message = str_replace("#","%23",$message,$i);
                        $message = str_replace("$","%24",$message,$i);
                        $message = str_replace("^","%5E",$message,$i);
                        $message = str_replace("&","%26",$message,$i);
                        $message = str_replace("~","%7E",$message,$i);
                        $message = str_replace("`","%60",$message,$i);
                        $message = str_replace("|","%7C",$message,$i);
                        $message = str_replace("\\","%5C",$message,$i);
                        $message = str_replace("\"","%3A",$message,$i);
                        $message = str_replace(";","%3B",$message,$i);
                        $message = str_replace("{","%7B",$message,$i);
                        $message = str_replace("}","%7D",$message,$i);
                        $message = str_replace("[","%5B",$message,$i);
                        $message = str_replace("]","%5D",$message,$i);
                        $message = str_replace("<","%3C",$message,$i);
                        $message = str_replace(">","%3E",$message,$i);
                        $message =preg_replace( "/\r|\n/", "%0A", $message);
				//end of characters mapping
				$url_text = $message;
                $message_id = generatePassword($length = 32, $strength = 2);
                $ftp_api_id = "HTTP API:$primary_http_api_id";
                #### end of characters mapping
                $status_url = $url_http_send;
                $status_url .= 'api_id='.UrlEncode($primary_http_api_id);
                $status_url .= '&user='.UrlEncode($primary_acc_user);
                $status_url .= '&password='.UrlEncode($primary_acc_pass);
                $status_url .= '&to='.UrlEncode($phone_prefix);
                $status_url .= '&from='.UrlEncode($sender);
                $status_url .= '&queue=1'; 
                $status_url .= '&req_feat=48'; 
                $status_url .= '&callback=3'; 
                $status_url .= '&climsgid='.UrlEncode($message_id);
                $status_url .= '&text='.$url_text;
                $url_return = file_get_contents($status_url);
                //http://api.clickatell.com/http/sendmsg?api_id=3289721&user=webnail&password=skata1234&to=306997070067&text=hey&from=webnail&cliMsgId=123456789&queue=1&callback=3
                //echo $url_return;
                //debugger
                //echo $phone.'<br />'.$sender.'<br />'.$message;
                if (substr($url_return, 0, 2) == 'ID' ) {
                    echo '<div class="success"><ul><li>'._SMS_WAS_SENT.$url_return.'</li></ul></div>';
                    mysqli_query($mysqli, "INSERT INTO sms_outbox 
                    (batch_id, phone, status, message_id, sender, message, message_url, priority_queue, delivery_delay, scheduled_time, source_module, created_on, created_by, campaign_id, customer_id, carrier) 
                    VALUES 
                    ('SINGLE', '$phone', '000', '$message_id', '$sender', '$sms_text', '$url_text','1', '', '', '$primary_http_api_id', NOW(), '$user_id', '', '', 'Clickatell')") or die("Error while updating sms_outbox");		
                }elseif(substr($url_return, 0, 3) == 'ERR' ){
                    echo '<div class="critical"><ul><li>'._ERROR_OCCURED_WHILE_SENDING_SMS.$url_return.'</li></ul></div>';
                }else{    
                    echo '<div class="critical"><ul><li>'._COULD_NOT_CONNECT_WITH_GATEWAY.'</li></ul></div>';
                }
            }else{
                echo '<div class="critical"><ul><li>'._ZERO_CREDITS_TEXT.'</li></ul></div>';
            }
} else {
    header('Location: login.php');
}
?>
