<?php
// Powered by Piminoff Nikita
// by pimnik98
// https://github.com/pimnik98/INIStorage

class Server
{
    private $html  = "<html><title>gLarkServer</title><body>gLarkServer ready!</body></html>";
	private $path  = "/virtual/";
	private $db = null;
	private $core = null;
	private $site = null;
	private $user = null;
    const MODE_NORMAL   = 0;   // Простой режим считывания INI
    const MODE_RAW      = 1;   // INI ключи-значения не будут обрабатываться
    const MODE_TYPED    = 2;   // Все значения будут преобразованы в свои сущности

    /**
     * Server инициализация
     * @param string $file - Путь к файлу
     */
    function __construct($file,$core,$db,$site,$user=false,$load=1)
    {
        if (is_readable($file)){
            $this->core = $core;
			$this->user = $user;
			$this->db = $db;
			$this->path = $file;
			$this->site = $site;
			if ($load){
				$this->load();
			}
		} else {
            new \Exception("Ошибка считывания файла. Недостаточно прав");
        }
    }

    /**
     * Загрузить файл
     */
    function load(){
       $line = @file_get_contents($this->path);
	   if (!$line){
		   new \Exception("Ошибка считывания файла. Ошибка на стороне сервера.");
	   }
	   $this->html = $line;
    }
	
	function legkoTag() {
		$c_news = $this->db->Count("news","WHERE `sid` = '".$this->site->id."'");
		if ($c_news > 0){
			$news = $this->db->getObject("SELECT * FROM `news` WHERE `sid` = '".$this->site->id."' ORDER BY `nid` DESC LIMIT 1");
		}
		$c_users = $this->db->Count("users","WHERE `sid` = '".$this->site->id."'");
		$c_online = $this->db->Count("users","WHERE `sid` = '".$this->site->id."' AND `lastVisit` > '".(time()-300)."'");
		$this->html = preg_replace('/\[legko.time\]/isU', date('H:i'), $this->html);
		$this->html = preg_replace('/\[legko.date\]/isU', date('d.m.Y'), $this->html);
		$this->html = preg_replace('/\[legko.times\]/isU', date('H:i:s'), $this->html);
        $this->html = preg_replace('/\[legko.domain\]/isU', $this->core->getSiteURL(), $this->html);
		
		$this->html = preg_replace('/\[legko.count,news\]/isU', $c_news, $this->html);
		$this->html = preg_replace('/\[legko.count,users\]/isU', $c_users, $this->html);
		$this->html = preg_replace('/\[legko.count,gb\]/isU', $this->db->Count("gb","WHERE `sid` = '".$this->site->id."'"), $this->html);
		$this->html = preg_replace('/\[legko.regonly\]/isU', "", $this->html);
		
		$this->html = preg_replace('/\[legko.username\]/isU', $this->user->getUsername(), $this->html);
		$this->html = preg_replace('/\[legko.ip\]/isU', $this->core->getIP(1), $this->html);
		$this->html = preg_replace('/\[legko.ua\]/isU', $this->core->getUA(1), $this->html); 
		$this->html = preg_replace('/\[legko.news\]/isU', ($c_news > 0?$news->ntitle:NULL), $this->html);
		$this->html = preg_replace('/\[legko.newsdate\]/isU', ($c_news > 0?$this->core->RealTime($news->ntime):NULL), $this->html);
		$this->html = preg_replace('/\[legko.newstext\]/isU', ($c_news > 0?$news->ntext:NULL), $this->html); 
		$this->html = preg_replace('/\[legko.online\]/isU', $c_online, $this->html);
		$this->html = preg_replace('/\[legko.online,g\]/isU', 0, $this->html);
		$this->html = preg_replace('/\[legko.online_test\]/isU', $c_online, $this->html);			
		$this->html = preg_replace('/\[legko.regcount\]/isU', $c_users, $this->html);
		$this->html = preg_replace('/\[legko.regcount,24h\]/isU', $this->db->Count("users","WHERE `sid` = '".$this->site->id."' AND `regTime` > '".(time()-86400)."'"), $this->html);
		$this->html = preg_replace('/\[debug.sql\]/isU', "WHERE `sid` = '".$this->site->id."' AND `regTime` > '".(time()-86400)."'", $this->html);
	}

	function legkoRegOnly(){
		// PHP 8
		if (phpversion() >= 8){
			if (str_contains($this->html,"[legko.regonly]")){
				return true;
			} else {
				return false;
			}
		} else {
			if (strpos($this->html,"[legko.regonly]") !== false){
				return true;
			} else {
				return false;
			}
		}
	}

    function setHTML($html){
		$this->html = $html;
	}
	
	function getHTML(){
		return $this->html;
	}
}