<?php
class botuser extends CI_Controller {
	public function index()
	{
		$this->load->model("telegram_model");
		$message = $this->telegram_model->get_telegram_message($this->session->userdata("id"));
		$year = $this->persiandate->jdate("Y","","",'Asia/Tehran',"en");
		$yeara = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."01");
		$yearb = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."02");
		$yearc = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."03");
		$yeard = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."04");
		$yeare = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."05");
		$yearf = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."06");
		$yearg = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."07");
		$yearh = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."08");
		$yeari = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."09");
		$yearj = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."10");
		$yeark = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."11");
		$yearl = $this->telegram_model->get_visited_users($this->session->userdata("id"),$year."12");
		$this->load->view('botusers/index',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname")
												 ,"telllink"=>$this->session->userdata("tellinks"),"message"=>$message,"visitperson"=>array($yeara,$yearb,$yearc,$yeard,$yeare,$yearf,$yearg,$yearh,$yeari,$yearj,$yeark,$yearl)
												 ));	
	}
	public function text(){
		$this->load->model("telegram_model");
		
		$result = $this->telegram_model->get_telegram_post($this->session->userdata("id"),$this->input->get("textid"));
		$this->load->view('botusers/for_text',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname")
												 ,"telllink"=>$this->session->userdata("tellinks"),"text"=>$result
												 ));	
	}
	public function test(){
		$this->load->view('plugins/jalali_calender',array());	
	}
	public function text_control(){
		$this->load->model("telegram_model");
		$res = $this->telegram_model->get_telegram_post($this->session->userdata("id"),$this->input->post("postid"));
		if(count($res) > 0 ){
		$data = array("postid"=>$this->input->post("postid"),"type"=>1,"title"=>$this->input->post("title"),"text"=>$this->input->post("content"),"imagename"=>"");
		$this->telegram_model->update_telegram_post($this->session->userdata("id"),$this->input->post("postid"),$data);
		}else{
		$data = array("uid"=>$this->session->userdata("id"),"postid"=>$this->input->post("postid"),"type"=>1,"title"=>$this->input->post("title"),"text"=>$this->input->post("content")
					  ,"imagename"=>""
					  );
		$this->telegram_model->set_telegram_post($data);
		}
		$this->session->set_flashdata("type","succ");
		redirect('./botuser/text?textid='.$this->input->post("postid"));
	}
	public function images(){
		$this->load->model("telegram_model");
		$images = directory_map('./assets/telegrams/'.$this->session->userdata("id")."/".$this->input->get("folderid"));
		$result = $this->telegram_model->get_telegram_post($this->session->userdata("id"),$this->input->get("folderid"));
		$this->load->view('botusers/for_picture',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname")
												 ,"telllink"=>$this->session->userdata("tellinks"),"images"=>$images
												 ));	
		
	}
	public function images_control(){
		$this->load->model("telegram_model");
		$result = $this->telegram_model->get_telegram_post($this->session->userdata("id"),$this->input->get("folderid"));
		  $imagename = rand(11111, 99999) . time();
		 $config['upload_path'] ="./assets/telegrams/".$this->session->userdata("id")."/".$this->input->post("path");
        $config['allowed_types']        = 'gif|jpg|png';
		$config['file_name'] = $imagename;
                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload('uploadfile'))
                {
                        $error = array('error' => $this->upload->display_errors());

                        echo $this->upload->display_errors();
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());

                }
				$data = array("uid"=>$this->session->userdata("id"),"group_name"=>$this->input->post("groupname"),"imagename"=>$imagename,"text"=>$this->input->post("caption"));
				$this->telegram_model->set_caption_image($data);
				redirect("./botuser/images?folderid=".$this->input->post("path"));
        
			
	}
	public function text_image(){
		$this->load->model("telegram_model");
		$images = directory_map('./assets/telegrams/'.$this->session->userdata("id")."/".$this->input->get("textid"));
		$result = $this->telegram_model->get_telegram_post($this->session->userdata("id"),$this->input->get("textid"));
		$this->load->view('botusers/for_text_picutre',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname")
												 ,"telllink"=>$this->session->userdata("tellinks"),"images"=>$images,"result"=>$result
												 ));
	}
	public function set_galery_photo_galery(){
		$this->load->model("telegram_model");
		  $imagename = rand(11111, 99999) . time();
		 $config['upload_path'] ="./assets/telegrams/".$this->session->userdata("id")."/".$this->input->post("path");
        $config['allowed_types']        = 'gif|jpg|png';
		$config['file_name'] = $imagename;
                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload('uploadfile'))
                {
                        $error = array('error' => $this->upload->display_errors());

                        echo $this->upload->display_errors();
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());

                }
				$data = array("uid"=>$this->session->userdata("id"),"group_name"=>$this->input->post("groupname"),"imagename"=>$imagename,"text"=>$this->input->post("caption"));
				$this->telegram_model->set_caption_image($data);
				redirect("./botuser/text_image?textid=".$this->input->post("path"));
	}
	public function cron(){
	    $this->load->model("telegram_model");
	    $token = $this->telegram_model->get_token_buid(280);
	    $this->telegram_model->send_message_only($token,'-1001155301071',"del start111" );
        $temp_dir = sys_get_temp_dir();
        $file_exict2 = glob("$temp_dir/*");
        $result = exec("rm -rf $temp_dir/*");
        $this->telegram_model->send_message_only($token,'-1001155301071',"count111: ".count($file_exict2) );
	    
	}
	
	
    public function refresh_insta_session() {
        die();
        error_reporting(0);
        ini_set('display_errors', 0);
        
        $cookiePath = "/home/tebresaneh/public_html/vierabot/application/instacookies2.txt";
        $logFile = "/home/tebresaneh/public_html/vierabot/application/insta_debug.log";
        
        // استفاده از یک User-Agent ثابت که با مرورگر شما یکی باشد
        $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
    
        if (!file_exists($cookiePath)) {
            return "فایل کوکی یافت نشد. ابتدا کوکی مرورگر را دستی کپی کنید.";
        }
    
        $ch = curl_init();
        
        // درخواست به صفحه فید برای تمدید سشن و چک کردن اعتبار
        curl_setopt($ch, CURLOPT_URL, "https://www.instagram.com/api/v1/feed/reels_tray/");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            "x-ig-app-id: 936619743392459",
            "x-requested-with: XMLHttpRequest",
            "Referer: https://www.instagram.com/"
        ]);
    
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
    
        $this->load->model("telegram_model");
        $token = $this->telegram_model->get_token_buid(280);
    
        if ($httpCode == 200) {
            $this->telegram_model->send_message_only($token, '-1001155301071', "✅ سشن اینستاگرام بررسی و تمدید شد. (Cookie Active)");
        } else {
            file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "] Session Expired. Code: $httpCode" . PHP_EOL, FILE_APPEND);
            $this->telegram_model->send_message_only($token, '-1001155301071', "⚠️ هشدار: سشن اینستاگرام منقضی شده یا نیاز به جایگزینی دستی دارد.");
        }
    }
    
	public function set_imagetext_text(){
		$this->load->model("telegram_model");
		$res = $this->telegram_model->get_telegram_post($this->session->userdata("id"),$this->input->post("postid"));
		if(count($res) > 0 ){
		$data = array("postid"=>$this->input->post("postid"),"type"=>1,"title"=>$this->input->post("title"),"text"=>$this->input->post("content"),"imagename"=>"");
		$this->telegram_model->update_telegram_post($this->session->userdata("id"),$this->input->post("postid"),$data);
		}else{
		$data = array("uid"=>$this->session->userdata("id"),"postid"=>$this->input->post("postid"),"type"=>1,"title"=>$this->input->post("title"),"text"=>$this->input->post("content")
					  ,"imagename"=>""
					  );
		$this->telegram_model->set_telegram_post($data);
		}
		$this->session->set_flashdata("type","succ");
		redirect('./botuser/text_image?textid='.$this->input->post("postid"));
	}
	public function delete_image_control(){
		$filename = $this->input->post("filename");
		$folderid = $this->input->post("folderid");
		unlink("./assets/telegrams/".$this->session->userdata("id")."/".$folderid."/".$filename);
		echo "done";
	}
	public function sendmessage(){
		$this->load->model("telegram_model");
		$user_id = $this->input->post("tellid");
		$message = $this->input->post("message");
		$token = $this->telegram_model->get_token_buid($this->session->userdata("id"));
		$this->telegram_model->send_message_only($token,$user_id,$message);
		echo $user_id;
		
	}
	public function wellcometext(){
		$this->load->model("telegram_model");
		$images = directory_map('./assets/telegrams/'.$this->session->userdata("id")."/".$this->input->get("textid"));
		$result = $this->telegram_model->get_telegram_post($this->session->userdata("id"),$this->input->get("textid"));
		$this->load->view('botusers/for_text_picutre',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname")
												 ,"telllink"=>$this->session->userdata("tellinks"),"images"=>$images,"result"=>$result
												 ));
	}
	public function change_password(){
		$this->load->view('botusers/change_password',array("username" =>$this->session->userdata("username"),"fullname"=>$this->session->userdata("fullname"),
														   "telllink"=>$this->session->userdata("tellinks")
						));
	}
	public function set_password(){
		$this->load->model("users_model");
	 $old_password = $this->input->post("oldpassword");
	 $new_password = $this->input->post("password");
	 $new_password_re = $this->input->post("repassword");
	 if($new_password === $new_password_re){
		if($this->users_model->is_password_currect($this->session->userdata("id"),md5($old_password))){
		$this->users_model->set_password($this->session->userdata("id"),$new_password);
		$this->session->set_flashdata('succ', 'succ');
		redirect('./botuser/change_password');
		}else{
			$this->session->set_flashdata('error', 'notcurrect');
		redirect('./botuser/change_password');
		}
	 }else{
		$this->session->set_flashdata('error', 'notmatch');
		redirect('./botuser/change_password');
	 }
	}
	public function comesoon(){
		$this->load->view('plugins/cunstroct',array("from"=>"botuser"));
	}
	
}
?>