<?php
/**
 * This page contain all Site Common functions
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 * @todo           Nothing
 */

/**-------------------- Alert -------------------------
 * Return PHP Bootstrap Alert either (success or fails)
 *
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 *
 * @param $result
 * @return string
 */
function result($result){
    global $PHPResult;
    if(!empty($result) && is_numeric($result) && !empty($PHPResult[$result]["text"])){
        return '
    <div class="col-12 mb-4 alert-div">
        <div class="alert alert-'.((!empty($PHPResult[$result]["success"]) && $PHPResult[$result]["success"]==true)?"success":"danger").' alert-dismissible fade show" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">x</span>
            </button>
            '.$PHPResult[$result]["text"].'
        </div>
    </div>
    <script type="text/javascript">
            setTimeout(function () {
                $(".alert-div .alert").fadeOut(800);
                $(".alert-div").remove("slow").animate({height: "0"},1500);
            },7000);
    </script>
    ';
    }
    return '';
}

/**-------------------- Ajax Alert -------------------------
 * Return PHP Bootstrap Alert either (success or fails) with error Code
 *
 * @create_by      Mohammad aljaouni
 * @create_date    29-09-2020
 * @edited_by
 * @last_edit_date
 *
 * @param $code
 * @return JSON
 */
function printResult($code,$data=''){
    $res = array();
    $res["code"] =$code;
    $res["html"] = result($res["code"]);
    $res["data"] = $data;
    echo json_encode($res);
    exit;
}

/**-------------------- Generate New Password -------------------------
 * Return Password consist of Characters (Capital And Small letters) And Numbers
 * And You Can specify length of password by using $length parameter
 *
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 *
 * @param int $length
 * @return string
 */
function randomPassword($length=6) {
    $alphabet=array("abcdefghijklmnopqrstuwxyz", "ABCDEFGHIJKLMNOPQRSTUWXYZ", "0123456789");
    $alphabet = implode($alphabet);
    $pass = array();
    $alphaLength = strlen($alphabet) - 1;
    for ($i = 0; $i < $length; $i++)
        $pass[] = $alphabet[rand(0, $alphaLength)];
    return implode($pass);
}

//Mysqli Persistent Connection Close
function shutdown_mysqli() {
    global $conn;
    if(!empty($conn) && is_object($conn)) {
        mysqli_close($conn);
    }
}
register_shutdown_function('shutdown_mysqli');

/**-------------------- Send SMPT Email-------------------------
 * Send Email Using SMTP Protocol
 *
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 *
 * @globals_var $host, $smtpUsername, $smtpPassword, $From, $FromName,$footer
 * @param $to: Email How send to
 * @param $subject: Email Subject
 * @param $body: Email Body (You Can Add HTML And Arabic characters)
 * @param string $fromEmail: (Not Required) Custom Sender name
 * @param string $username: (Not Required) If You Want to send email by another mail account add username(default from config file ELSE empty)
 * @param string $password: (Not Required) If You Want to send email by another mail account add password(default from config file ELSE empty)
 * Port: Default PORT is 465 OR you can use 587
 * SMTPSecure: Default  is SSL And you can use tls
 *
 * @return bool
 * @throws phpmailerException
 */
function smptMail($to, $subject, $body, $fromEmail='', $username='', $password=''){
    include("class.smtp.php");
    include("PHPMailer.php");
    global $host, $smtpUsername, $smtpPassword, $From, $FromName, $footer,$align,$align2,$dir,$siteName,$siteEmail,$URI;
    $mail = new PHPMailer();
    $mail->Host =$host;
    $mail->isSMTP();
    $mail->SMTPDebug = 0;
    $mail->CharSet = 'UTF-8';
    $mail->Mailer ="smtp";
    $mail->Port ="465";
    $mail->SMTPSecure ="ssl";
    $mail->SMTPAuth =true;
    $mail->Username =$smtpUsername;
    $mail->Password =$smtpPassword;
    if(!empty($username)&&!empty($password)){
        $mail->Username=$username;
        $mail->Password=$password;
    }
    $From = !empty($fromEmail)?$fromEmail:$From;
    $FromName = !empty($FromName)?$FromName:"";
    $mail->SetFrom($From, $FromName);
    $mail->Subject = $subject;
    $template='
<!DOCTYPE html>
<html dir="'.$dir.'">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="shortcut icon" href="#" />
    <style>
        html{width:100%;direction: '.$dir.';}::-moz-selection{background:#46becf;color:#fff;text-shadow:1px 1px 0 #40b1c1}::selection{background:#46becf;color:#fff;text-shadow:1px 1px 0 #40b1c1}body{background-color:#fff;margin:0;padding:0}a{color:#46becf;text-decoration:none;font-weight:400;font-style:normal}a:hover{color:#3899a7;text-decoration:none;font-weight:400;font-style:normal}div,p{margin:0!important}table{border-collapse:collapse}@media only screen and (max-width:640px){body{width:auto!important}table table{width:100%!important}td[class=full_width]{width:100%!important}td[class=spacer]{width:30px!important}div[class=div_scale]{width:440px!important;margin:0 auto!important}table[class=table_scale]{width:440px!important;margin:0 auto!important}td[class=td_scale]{width:440px!important;margin:0 auto!important}img[class=img_scale]{width:100%!important;height:auto!important}}@media only screen and (max-width:479px){body{width:auto!important}table table{width:100%!important}td[class=full_width]{width:100%!important}div[class=div_scale]{width:280px!important;margin:0 auto!important}table[class=table_scale]{width:280px!important;margin:0 auto!important}td[class=td_scale]{width:280px!important;margin:0 auto!important}img[class=img_scale]{width:100%!important;height:auto!important}td[class=spacer]{display:none!important}td[class=center]{text-align:center!important}td[class=subject_line]{float:left;width:240px;display:block!important;text-align:left!important;padding:15px 20px!important}td[class=contact]{float:left;width:240px;display:block!important;text-align:left!important;padding:0 20px 15px!important;padding-bottom:20px!important}}
    </style>
</head>
<body align="center" style="text-align:center;">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#d0d0d0" style="padding: 0; margin: 0;" style="direction: '.$dir.';">
    <!-- START OF TOP BAR-->
    <tr>
        <td class="full_width" align="center" width="100%" bgcolor="#d0d0d0" style="">
            <div class="div_scale" style="width:600px;">
                <table class="table_scale" width="600" height="42" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#494949" style="padding:0; margin: 0;direction: '.$dir.';">
                    <tr>
                        <td class="spacer" width="20" align="left" valign="top" bgcolor="#494949" style="margin: 0 !important; padding: 0 !important; line-height: 0 !important;"></td>
                        <!-- START OF SUBJECT LINE-->
                        <td class="subject_line" align="left" valign="middle" width="270" bgcolor="#494949" style="padding-top: 10px; padding-bottom: 10px;">
                            <table width="100%" style="direction: '.$dir.';">
                                <tr>
                                    <td class="center" align="" valign="" style="font-family:Arial, sans-serif; font-style: italic; color:#d8d8d8; font-size:11px; line-height:18px;"> '.$siteName.'</td>
                                </tr>
                            </table>
                        </td>
                        <!-- END OF SUBJECT LINE-->
                        <td class="spacer" width="20" align="left" valign="top" bgcolor="#494949" style="margin: 0 !important; padding: 0 !important; line-height: 0 !important;"></td>
                        <!-- START OF CONTACT-->
                        <td class="contact" align="right" valign="middle" width="270" bgcolor="#494949" style="padding: 0px;">
                            <table width="100%" style="direction: '.$dir.';">
                                <tr>
                                    <td class="center" align="" valign="" style="text-align: '.$align2.'; font-family:Arial, sans-serif; font-style: italic; color:#d8d8d8; font-size:11px; line-height:100%;"><a href="mailto:'.$siteEmail.'"><img src="'.($URI."assets/images/".($_SESSION['langSymbol'])).'/email.png" alt="email" width="20" height="11" style="display: inline; vertical-align: middle; '.($_SESSION["language"]==1?"float: ".$align2.";":"").'" />'.$siteEmail.' </a></td>
                                </tr>
                            </table>
                        </td>
                        <!-- END OF CONTACT-->
                        <td class="spacer" width="20" align="left" valign="top" bgcolor="#494949" style="margin: 0 !important; padding: 0 !important; line-height: 0 !important;"></td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
    <!-- END OF TOP BAR-->
    <!-- START OF VERTIXAL SPACER BLOCK-->
    <tr>
        <td class="full_width" align="center" width="100%" bgcolor="#d0d0d0" style="">
            <div class="div_scale" style="width:600px;">
                <table class="table_scale" width="600" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#d0d0d0" style="padding:0; margin: 0;direction: '.$dir.';">
                    <tr>
                        <td class="td_scale" width="600" height="40" bgcolor="#d0d0d0" align="center" valign="middle" style="height: 40px; padding: 0px; font-size:0 ; color:#686868; font-family: Arial,sans-serif; line-height: 0; "></td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
    <!-- END OF VERTIXAL SPACER BLOCK-->
    <!-- START OF FEATURED AREA BLOCK-->
    <tr>
        <td class="full_width" align="center" width="100%" bgcolor="#d0d0d0" style="">
            <div class="div_scale" style="width:600px;">
                <table class="table_scale" width="600" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#f0f0f0" style="padding:0; margin: 0;direction: '.$dir.';">
                    <tr>
                        <td>
                            <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#46becf" style="margin: 0;direction: '.$dir.';">
                                <!-- START OF LEFT COLUMN-->
                                <tr>
                                    <td colspan="2" class="td_scale" width="600" bgcolor="#46becf" align="center" valign="top" style="padding: 0px; font-size:14px ; color:#959595; font-family: Arial,sans-serif; line-height: 24px; ">
                                </tr>
                                <!-- START OF BANNER-->
                                <tr>
                                    <td class="'.$align.'" align="'.$align.'" valign="top" bgcolor="#46becf" style="width:60px;padding:0; font-size: 16px; line-height: 24px; font-family:Lucida Sans Unicode; color:#262626; margin: 0 !important;">
                                        <a href="#" style="font-style: normal;"> <img class="img_scale" src="'.($URI."assets/images/".($_SESSION['langSymbol'])).'/logo-sm-light.png" width="50" alt="featured banner" border="0" style="display: block;padding: 10px" /> </a>
                                    </td>
                                    <td class="'.$align.'" align="'.$align.'" valign="top" bgcolor="#46becf" style="color: #fff;font-size: 26px;font-weight: bold;font-family: monospace;line-height: 75px;">'.$subject.'</td>
                                </tr>
                                <!-- END OF BANNER-->
                                <!-- START OF VERTICAL SPACER-->
                                <tr>
                                    <td colspan="2" height="20" bgcolor="#f0f0f0" style="padding:0; line-height: 0;"></td>
                                </tr>
                                <!-- END OF VERTICAL SPACER-->
                                <!-- START OF HEADING TITLE-->
                                <tr>
                                    <td  colspan="2" class="center" align="center" valign="top" bgcolor="#f0f0f0" style="padding: 0px 20px;  text-shadow: 1px 1px 0px #ffffff;font-size:24px ; color:#444444; font-family: Lucida Sans Unicode; line-height: 34px; ">'.$body.'</td>
                                </tr>
                                <!-- END OF HEADING TITLE-->
                                <!-- START OF VERTICAL SPACER-->
                                <tr>
                                    <td  colspan="2" height="10" bgcolor="#f0f0f0" style="padding:0; line-height: 0;"></td>
                                </tr>
                                <!-- END OF VERTICAL SPACER-->
                                <!-- START OF TEXT-->
                                <tr>
                                    <td  colspan="2" class="center" align="center" valign="top" bgcolor="#f0f0f0" style="padding: 0px 20px;  text-shadow: 1px 1px 0px #ffffff;font-size:13px ; color:#727272; font-family: Arial,sans-serif; line-height: 23px; ">

                                    </td>
                                </tr>
                                <!-- END OF TEXT-->
                                <!-- START OF VERTICAL SPACER-->
                                <tr>
                                    <td  colspan="2" height="20" bgcolor="#f0f0f0" style="padding:0; line-height: 0;"></td>
                                </tr>
                                <!-- END OF VERTICAL SPACER-->
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
    <!-- START OF FOOTER-->
    <tr>
        <td class="full_width" align="center" width="100%" bgcolor="#d0d0d0" style="">
            <div class="div_scale" style="width:600px;">
                <table class="table_scale" width="" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#353535" style="padding:0; margin: 0;direction: '.$dir.';">
                    <tr>
                        <td class="" align="center" valign="top" width="600" bgcolor="#353535" style="">
                            <table align="center" width="100%">
                                <!-- START OF LOGO-->
                                <tr>
                                    <td align="center" valign="top" style="border-top: 1px solid #484848; padding: 20px; font-size:13px ; color:#bbbbbb; font-family: Arial,sans-serif; line-height: 23px; ">
                                        <a href="#" style="font-style: normal;"> <img src="'.($URI."assets/images/".($_SESSION['langSymbol'])).'/logo-light.png" width="80" alt="logo" border="0" style="display: inline-block;" /></a><br>
                                        '.$footer["copy"].'
                                    </td>
                                </tr>
                                <!-- END OF LOGO-->
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
    <!-- END OF FOOTER-->
    <!-- START OF VERTIXAL SPACER BLOCK-->
    <tr>
        <td class="full_width" align="center" width="100%" bgcolor="#d0d0d0" style="">
            <div class="div_scale" style="width:600px;">
                <table class="table_scale" width="600" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#d0d0d0" style="padding:0; margin: 0;direction: '.$dir.';">
                    <tr>
                        <td class="td_scale" width="600" height="40" bgcolor="#d0d0d0" align="center" valign="middle" style="height: 40px; padding: 0px; font-size:0 ; color:#686868; font-family: Arial,sans-serif; line-height: 0; "></td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
    <!-- END OF VERTIXAL SPACER BLOCK-->
</table>
</body>
</html>';

    $mail->MsgHTML($template);
    $toArr = explode(';',$to);
    $count = count($toArr);
    if(!empty($count))
        foreach($toArr as $key=>$val)
            $mail->AddAddress($val, "");
    $mail->AltBody="To view the message, please use an HTML compatible email viewer!";
    if($mail->Send())
        return true;
    else
        return false;

}


/**-------------------- Generate New Password -------------------------
 * Create breadcrumbs By Array From Label Files (AR , EN)
 *
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 *
 * @globals_var  $breadcrumbs
 * @param int $breadcrumbs
 * @return string
 */
function breadcrumbs($pageName) {
    global $breadcrumbs;
    $data = "";
    if(!empty($pageName)){
        $pageName = explode(".",$pageName)[0];
        if(!empty($breadcrumbs[$pageName])){
            $data .= '<div class="col-md-12"><h4 class="page-title mb-1">'.(!empty($breadcrumbs[$pageName]["title"])?$breadcrumbs[$pageName]["title"]:"").'</h4>';
            if(!empty($breadcrumbs[$pageName]["links"]) && is_array($breadcrumbs[$pageName]["links"])){
                $data .= '<ol class="breadcrumb m-0">';
                foreach ($breadcrumbs[$pageName]["links"] as $link)
                    if(!empty($link["active"]) && $link["active"])
                        $data .= '<li class="breadcrumb-item active">'.(!empty($link["title"])?$link["title"]:"").'</li>';
                    else
                        $data .= '<li class="breadcrumb-item"><a href="'.(!empty($link["link"])?$link["link"]:"javascript: void(0);").'">'.(!empty($link["title"])?$link["title"]:"").'</a></li>';
                $data .= '</ol>';
            }
            $data .= '</div>';
        }
    }
    return $data;
}

/**-------------------- Get Client IP Address-------------------------
 * This function to get IP address by checking multi ways
 * And if not exist will return UNKNOWN
 *
 * @create_by      Mohammad aljaouni
 * @create_date    11-08-2020
 * @edited_by
 * @last_edit_date
 *
 * @return string
 */
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}





?>
