<?php

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

class Logs extends Model {
    function __construct() {
        parent::__construct();
    }
public function ConvertBytes($number){
$len = strlen ($number);
if ($len < 4){ return sprintf("%0.2f B", $number); }
if ($len>= 4 && $len <=6){ return sprintf("%0.2f KB", $number/1024); }
if ($len>= 7 && $len <=9){ return sprintf("%0.2f MB", $number/1024/1024); }
return sprintf("%0.2f GB", $number/1024/1024/1024);
}	
    public function createl()
    {
	$fp = fopen("logstxt/tracks.txt", 'a');
	ftruncate($fp, 0);
	fclose($fp);
foreach (glob("logstxt/temp/*.txt") as $filtrak)
    {
$filename = explode('/',$filtrak);
$filename = $filename[(count($filename) - 1)];	
$ip_f=preg_replace("/.txt/","",$filename);
file_put_contents('logstxt/tracks.txt', "[".$ip_f."]\r\n", FILE_APPEND);
$file=file($filtrak); 
$file=array_unique($file);
foreach($file as $key => $value){
file_put_contents('logstxt/tracks.txt', "".$value."", FILE_APPEND);
}
    }
    }
	
    public function logs($link)
    {
	$fn = ("logstxt/".$link.".txt");
	if (file_exists($fn)) {
	 $sz = $this->ConvertBytes(filesize($fn));
     $fp = file($fn);
	 $co = count($fp);	 
	 }
	 else {
	 $sz = 0;
	 $co = 0;	
	}
     $stats = array(
         'co' =>  $co,
         'sz' =>  $sz,
        );
	 return $stats;  
    }
	
    function delhttp()
    {
	$fp = fopen("logstxt/browsers.txt", 'a');
	ftruncate($fp, 0);
	fclose($fp);
    }	
    function delpop()
    {
	$fp = fopen("logstxt/mails.txt", 'a');
	ftruncate($fp, 0);
	fclose($fp);
    }
    function deltracks()
    {
	$fp = fopen("logstxt/tracks.txt", 'a');
	ftruncate($fp, 0);
	fclose($fp);
foreach (glob("logstxt/temp/*.txt") as $filtrak)
    {
unlink($filtrak);   
	}	
    }
    public function is_valid()
    {
        // TODO: Implement is_valid() method.
    }	
}