<?php
include("config.php");
include("functions.php");
if(!empty($_POST["code"]) ){
    $code=$_POST["code"];
    $res=mysqli_query($conn,"SELECT * FROM `msg_archive` WHERE `body` LIKE '%".mysqli_real_escape_string($conn,$code)."%' AND `read`='0'");
    $num=mysqli_num_rows($res);
    if($num){
        $row=mysqli_fetch_assoc($res);
        $from =$row["from"];
        optIn($from);
        send($from,"Welcome to brightidea.mobi whatsApp service");
        $_SESSION["code"]=$code;
        $_SESSION["from"]=$from;
        $res=mysqli_query($conn,"UPDATE `msg_archive` SET `read`='1' ,`code`='".$code."' WHERE `id` =".$row["id"]);
        exit($from);
    }
}
if(!empty($_POST["msg_id"]) ){
    $msg_id=$_POST["msg_id"];
    $res=mysqli_query($conn,"SELECT * FROM `msg_archive` WHERE `message_id` LIKE '%".mysqli_real_escape_string($conn,$msg_id)."%'");
    $num=mysqli_num_rows($res);
    if($num){
        $row=mysqli_fetch_assoc($res);
        exit(json_encode(array("to"=>$row["to"],"type"=>$row["msg_type"],"body"=>$row["body"],"caption"=>$row["caption"],)));
    }
}
if(!empty($_POST["from"]) ){
    $from=$_POST["from"];
    if(!empty($_POST["get"])) {
        $res2 = mysqli_query($conn,"SELECT * FROM `msg_archive` WHERE (`from` LIKE '%" . mysqli_real_escape_string($conn,$from) . "%' OR `to` LIKE '%" . mysqli_real_escape_string($conn,$from) . "%') AND `id` >= (SELECT `id` FROM `msg_archive` WHERE `code`='".$_SESSION["code"]."') ORDER BY `id` ASC");
    }else{
        $res2 = mysqli_query($conn,"SELECT * FROM `msg_archive` WHERE (`from` LIKE '%".mysqli_real_escape_string($conn,$from)."%' OR `to` LIKE '%".mysqli_real_escape_string($conn,$from)."%') AND `read`='0' AND `id` >= (SELECT `id` FROM `msg_archive` WHERE `code`='".$_SESSION["code"]."') ORDER BY `id` ASC");
    }
    $num=mysqli_num_rows($res2);
    if($num){
        while ($row=mysqli_fetch_assoc($res2)){
            if(!empty($row["body"])) {
                if ($row["from"] == $from) {
                    $type="receiver";
                }else{
                    $type="sender";
                }
            ?>
                <div class="row message-body">
                    <div class="col-sm-12 message-main-<?php echo $type?>">
                        <div class="<?php echo $type?>">
                            <div class="message-text">
                                <?php
                                if($row["msg_type"]=="text") {
                                    $row["body"]=str_replace("\\\\n","<br>",$row["body"]);
                                    $row["body"]=str_replace("\\\\r","<br>",$row["body"]);
                                    $row["body"]=str_replace("\\\\t","<br>",$row["body"]);
                                    $row["body"]=str_replace("\n","<br>",$row["body"]);
                                    $row["body"]=str_replace("\r","<br>",$row["body"]);
                                    $row["body"]=str_replace("\t","<br>",$row["body"]);
                                    echo $row["body"];
                                }elseif($row["msg_type"]=="image"){
                                    echo "<img src='".$row["body"]."' class='img-fluid' style='width:auto;height:300px'>";
                                    echo !empty($row["caption"])?"<p>".$row["caption"]."</p>":"";
                                }elseif($row["msg_type"]=="video"){
                                    echo "<video style='width:300px;height:300px' controls><source src='".$row["body"]."' ></video>";
                                    echo !empty($row["caption"])?"<p>".$row["caption"]."</p>":"";
                                }elseif($row["msg_type"]=="voice" || $row["msg_type"]=="audio"){
                                    echo !empty($row["caption"])?"<p>".$row["caption"]."</p>":"";
                                    echo "<audio  controls ><source src='".$row["body"]."' ></audio>";
                                }elseif($row["msg_type"]=="document"){
                                    if(!empty($row["caption"]))
                                        echo "<i class='fas fa-file'></i><a target='_blank'  href='".$row["body"]."' >".$row["caption"]."</a>";
                                    else{
                                        $fileData=explode("/",$row["body"]);
                                        echo "<i class='fas fa-file'></i><a target='_blank'  href='".$row["body"]."' >".end($fileData)."</a>";
                                    }
                                }elseif($row["msg_type"]=="location"){
                                    echo "<a  target='  _blank' href='".$row["body"]."' ><img src='images/map.jpg' class='img-fluid' width='300px' height='300px'></a>";
                                }
                                ?>
                            </div>
                            <span class="message-time float-right"><?php echo date("h:i A", $row["date"]); ?></span>
                        </div>
                    </div>
                </div>
                <?php
            }
            mysqli_query($conn,"UPDATE `msg_archive` SET `read`='1' WHERE `id` =".$row["id"]);
        }
        exit();
    }
    exit("2");
}
exit("2");

