<?php



include("../asl.php");

ini_set('display_errors', '0');
/*
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
*/
?>



<!DOCTYPE HTML>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html ; charset=utf-8" />
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="persian" />

    <title>Telegram Bot</title>


    <link rel="Stylesheet" type="text/css" href="style.css" />





</head>

<body dir="rtl" align="center" style="font-family:Tahoma !important">
    <div align="center">
        <?php

        //    import_request_variables("p" , "a_");
        $a_pass = $_POST["pass"];
        $a_user = $_POST["user"];
        $a_mykey = $_POST["mykey"];
        $a_answer = $_POST["answer"];
        $a_myarakeys = $_POST["myarakeys"];
        $a_newuser = $_POST["newuser"];
        $a_newpass = $_POST["newpass"];
        $a_noa = $_POST["noa"];
        $a_shenase = $_POST["shenase"];
        $a_chatid = $_POST["chatid"];
        $a_text = $_POST["text"];
        $a_pfrom = $_POST["pfrom"];
        $a_pto = $_POST["pto"];


        try {
            $conn = new PDO("mysql:host=$servername;dbname=$mydb", $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            // set the PDO error mode to exception

            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            echo "Connection failed: " . $e->getMessage();
        }



        
                    if(isset($_POST['add'])){
                        $data=$_POST['data'];
                        //var_dump($data);
                        
                        $sql = "insert into package (title, dollar, time) values ('".$data['title']. "', '" .$data['dollar']. "','" .$data['time']. "' )";
                        $stmt = $conn->prepare($sql);
                        $stmt->execute();
                        //header('location:reservation.php');
                    }
                    if (isset($_POST['delete'])) {
                        $sql = "delete from package where id='$_POST[id]'";
                        $conn->exec($sql);
                        print("انجام شد");
                    }
                

        $sql = "select * from admin";
        $stmt = $conn->prepare($sql);
        $stmt->execute();

        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $show = $stmt->fetch();

        $user = $show["user"];
        $pass = $show["pass"];


        $getpass = hash_hmac('md5', $a_pass, 'salt');





        if ($user == $a_user && $pass == $getpass) {








            if ($a_newuser == "") {
            } else {

                $newpass = hash_hmac('md5', $a_newpass, 'salt');
                $sql = "update admin set user = \"$a_newuser\"  , pass=\"$newpass\"  ";
                $conn->exec($sql);
                echo "<p style='color:green' >Done</p>";



                function redirect($url)
                {
                    if (!headers_sent()) {
                        header('Location: ' . $url);
                        exit;
                    } else {
                        echo '<script type="text/javascript">';
                        echo 'window.location.href="' . $url . '";';
                        echo '</script>';
                        echo '<noscript>';
                        echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
                        echo '</noscript>';
                        exit;
                    }
                }
                redirect("enter.php");
            }
        } else {
            echo "<p style='color:red' >Wrong username or password</p><br />
                      <a href='enter.php' target='_blank' >back</a>
              
                      ";



            function redirect($url)
            {
                if (!headers_sent()) {
                    header('Location: ' . $url);
                    exit;
                } else {
                    echo '<script type="text/javascript">';
                    echo 'window.location.href="' . $url . '";';
                    echo '</script>';
                    echo '<noscript>';
                    echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
                    echo '</noscript>';
                    exit;
                }
            }




            redirect("enter.php");
        }








        function sendmessage($text, $chatid, $keys)
        {

            $send = "https://api.telegram.org/bot7232495194:AAFpC09QjGzFy9NQJFKmBxKhJTYoyLQ30TA/sendmessage?chat_id=$chatid&text=$text&parse_mode=Html&reply_markup=$keys";
            $sended = file_get_contents($send);
            return $sended;
        }

        function sendphoto($address, $chatid, $key, $caption)
        {


            $bot_url = "https://api.telegram.org/bot7232495194:AAFpC09QjGzFy9NQJFKmBxKhJTYoyLQ30TA/";
            $url = $bot_url . "sendPhoto?caption=$caption&chat_id=" . $chatid;
            $ch = curl_init();
            curl_setopt(
                $ch,
                CURLOPT_HTTPHEADER,
                array(
                    "Content-Type:multipart/form-data"
                )
            );
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt(
                $ch,
                CURLOPT_POSTFIELDS,
                array(
                    "photo" => "@" . "upload/" . $address,
                )
            );
            curl_setopt($ch, CURLOPT_INFILESIZE, filesize("upload/" . $address));
            $output = curl_exec($ch);
        }





        if ($a_noa == "sndmsg") {



            if (!$_FILES['myfile']['name'] == "" && $_FILES["myfile"]["error"] == 0) {




                $ext = end(explode('.', $_FILES['myfile']['name']));

                $formats = array('jpg', 'JPG', 'gif', 'GIF', 'png', 'PNG', 'RAR', 'rar', 'ZIP', 'zip', 'mp4', '3gp', 'mov');
                if (!in_array($ext, $formats)) {

                    print "<p style='color:green' >پسوند معتبر برای آپلود نیست</p>";
                } elseif (file_exists('upload/' . $_FILES["myfile"]["name"])) {
                    print "<p style='color:green' >متاسفیم این فایل قبلا اپلود شده لطفا نام ان را تغییر دهید</p>";
                } elseif (536870912 < $_FILES["myfile"]["size"]) {


                    print "<p style='color:green' >سایز فایل باید کمتر از نیم گیگ باشد</p>";
                } else {
                    $path = "upload/" . $_FILES["myfile"]["name"];
                    move_uploaded_file($_FILES["myfile"]["tmp_name"], $path);
                    print "
        <span class='green' >
        اپلود با موفقیت انجام 
        شد
        </span>
        ";
                }
                $namemyfile = $_FILES["myfile"]["name"];
            }

            if ($namemyfile == "" || $namemyfile == " ") {
            } else {
                print $namemyfile;
                sendphoto($namemyfile, $a_chatid, "", $caption);
            }
            sendmessage(urlencode("$a_text"), $a_chatid, "");

            print "sent";


    
        }




        ?>


        <br /><br />
        <h4 class="white">Admin Panel</h4>
        <div id="content" align="right">
            <div id="content-content">
                <table>
                    <tr>
                        <td><img class="radius" src="images/user.png" /></td>
                        <td class="gray" valign="bottom">خوش آمدید

                            <?php print $userse; ?>
                        </td>

                        <td>
                            <form target="_blank" method='post' action='index.php'>


                                <input type='submit' style='width:100%' class='font' value='صفحه اصلی' />
                                <input type='hidden' name='user' value="<?php echo $a_user  ?>" />
                                <input type='hidden' name='pass' value="<?php echo $a_pass  ?>" class='font' />
                            </form>
                        </td>
                        

                        <td>
                            <form target="_blank" method='post' action='users.php'>


                                <input type='submit' style='width:100%' class='font' value='نمایش کاربران' />
                                <input type='hidden' name='user' value="<?php echo $a_user  ?>" />
                                <input type='hidden' name='pass' value="<?php echo $a_pass  ?>" class='font' />
                            </form>
                        </td>

                       






                    </tr>

                </table>

                


                <br />
                <br />


                <br />
                <div id="fish">



                    <h5 class='gray'>◄ افزودن پکیج</h5>

                    <form enctype='multipart/form-data' method='post' action='package.php'>

                        <input type='hidden' name='user' value="<?php echo $a_user ?>" />
                        <input type='hidden' name='pass' value="<?php echo $a_pass ?>" />
                        <input type='hidden' name='noa' value="sndmsg" />


                        <table>
                            <tr>

                                <th class='th'>عنوان پکیج </th>
                                <th class='th'>قیمت به تتر</th>
                                
                                
                                

                            </tr>






                            <tr>

                                <td class='td'><input type='text' name='data[title]' /> </td>
                                <td class='td'><input type='text' name='data[dollar]' /></td>
                                
                            



                                


                            </tr>
                            <tr>
                                <th class='th'>مدت زمان پکیج</th>
                                <th class='th'>افزودن</th>
                            </tr>
                            <tr>
                                <td class='td'><input type='text' name='data[time]' /></td>
                                <td class='td'><input style="width: 100%;" name="add" type='submit' value='افزودن' /> </td>
                            </tr>











                        </table>
                    </form>



                    <h5 class='gray'>◄ نمایش کل پکیج ها</h5>

                    <?php
                   


                    ?>
                    

                    <table style='width:100%'>
                        <tr>

                            <th class='th'>عنوان پکیج</th>
                            <th class='th'>قیمت تومانی</th>
                            <th class='th'>مدت زمان پکیج</th>
                            <th class='th'>حذف</th>



                        </tr>
                        <tr>





                            <?php

                            include('../class/bot.php');
                            include('../class/general.php');
                            $bot = new bot("7232495194:AAFpC09QjGzFy9NQJFKmBxKhJTYoyLQ30TA", $content);
                            $general = new general();


                            if ($_POST['noa'] == 'change_subscription') {
                                $newTime = time() + ($_POST['num_days'] * 24 * 3600);

                                $sql = "update users set status='active',expire_time='" . $newTime . "',pay_time='" . time() . "'  where chatid='" . $_POST['chatid'] . "' ";
                                $conn->exec($sql);

                                print("<script>alert('تنظیمات انجام شد');</script>");
                            } elseif ($_POST['noa'] == 'block_user') {
                                $bot->sendmessage(urlencode("اشتراک شما به  اتمام رسید"), $_POST['chatid'], '');

                                $bot->banMember($_POST['chatid'], '-1001722279115');
                                $sql = "update users set status=\"deactive\"  where chatid='" . $_POST['chatid'] . "' ";
                                $conn->exec($sql);
                            } elseif ($_POST['noa'] == 'invite_user') {
                                $expireTime = time() + (30 * 24 * 3600);
                                $sql = "update users set status=\"active\" ,pay_time='" . time() . "',expire_time=\"$expireTime\" where chatid='" . $_POST['chatid'] . "' ";
                                $conn->exec($sql);


                                $channelId = '-1001722279115';
                                $bot->unBanMember($_POST['chatid'], $channelId);
                                $vipChannel = $bot->getChannelLink($channelId);
                                $bot->sendmessage(urlencode("
✅ پرداخت انجام شد
لینک کانال :
$vipChannel
⚠️دقت کنید این لینک یکبار مصرف می‌باشد
                                "), $_POST['chatid'], '');
                            }

                            $stmt = $conn->prepare("SELECT * FROM package");
                            $stmt->execute();
                            $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
                            $k = $stmt->fetchAll();
                            $num = $stmt->rowCount();
                            print " <h5 class='gray'>◄ تعداد پکیج ها: $num</h5>";
                            foreach ($k as $v) {
                                $firstArray=[$v["name"],$v["phone"],$v["chatid"] , $v["shopname"], $v["loan"] ,$v["prepay"],$v["monthlypay"],$v["expense"],$v["installment"]];
                                $secondArray[]=$firstArray;
                                unset($firstArray);
    
                 
                                
    
                                    print "
            <tr>
          
    <td align='center' class='td'  >" . $v["title"] . "</td>
    <td align='center' class='td'  >" . $v["dollar"] . "</td>
    <td align='center' class='td'  >" . $v["time"] . "</td>
    <td align='center' class='td'  >
    
    "; ?>
                                    <form method='post' action='package.php'>
                                        <input type='submit' name='delete' style='width:100%' class='font' value='حذف' />
                                        <input type='hidden' name='user' value="<?php echo $a_user  ?>" />
                                        <input type='hidden' name='pass' value="<?php echo $a_pass  ?>" class='font' />
                                        <input type='hidden' name='id' value="<?php echo $v["id"];  ?>" />
                                    </form>
    
                                <?php print "
    </td>
    
    
    
    </tr>
    
            
            ";
                                }
    
                                ?>
    
                            </tr>
                    </table>

                    <hr />
                    <hr />
                    <hr />

                    <br />
                    <br />
                    <br />
                    <br />






                </div>



















                <br />
                <br />
            </div>
        </div>




        <a style="text-decoration:none ;" href="#" target="_blank" class="gray">#</a>
    </div>

    <?php
    $conn = null;
    ?>
</body>

</html>
<?php
$conn = null;
?>
</body>

</html>
<?php
$conn = null;
?>
</body>

</html>