<?php



class Database
{
    private $my_dist_host;
    private $my_dist_user;
    private $my_dist_pass;

    private $rank_user_host;
    private $rank_user_user;
    private $rank_user_pass;
    private $rank_user_db;

    private $rank_proj_host;
    private $rank_proj_user;
    private $rank_proj_pass;
    private $rank_proj_db;

    private $mo_db;
    private $mo_host;

    public function __construct()
    {
        $this->my_dist_host = "167.114.96.213";
        $this->my_dist_user = "auditor";
        $this->my_dist_pass = "yb9738z";
        $this->my_dist_db = "auditor_distributed";

        $this->rank_user_host = 'rankwatch.com';
        $this->rank_user_user = 'campagin';
        $this->rank_user_pass = 'campagin';
        $this->rank_user_db = 'rankwatc_rankmoz';

        $this->rank_proj_host = '167.114.169.176';
        $this->rank_proj_user = 'root';
        $this->rank_proj_pass = '123456';
        $this->rank_proj_db = 'rankwatch_keywords';

        $this->mo_db = "distributed";
        $this->mo_host = "mongodb://127.0.0.1:27017";
    }


    public function logError($err)
    {
        $dir = '/var/www/html/logs/';
        $file = '/var/www/html/logs/error.log';
        if (!is_dir($dir)) {
            mkdir($dir);
        }
        $error = $err.print_r(error_get_last(), true);
        $fhn = fopen($file, 'a+');
        fwrite($fhn, $error."\n");
        fclose($fhn);
    }


    public function conDist($try = '')
    {
        if (empty($try)) {
            $try = 0;
        }

        $con_dist = new mysqli($this->my_dist_host, $this->my_dist_user, $this->my_dist_pass, $this->my_dist_db);
        if (mysqli_connect_error()) {
            if ($try <= 2) {
                $try += 1;
                // $this->logError(mysqli_connect_error());
                $this->conDist($try);
            }
        }

        return $con_dist;
    }


    public function conRankUser($try = '')
    {
        if (empty($try)) {
            $try = 0;
        }

        $con_rank_user = new mysqli($this->rank_user_host, $this->rank_user_user, $this->rank_user_pass, $this->rank_user_db);
        if (mysqli_connect_error()) {
            if ($try <= 2) {
                $try += 1;
                // $this->logError(mysqli_connect_error());
                $this->conRankUser($try);
            }
        }

        return $con_rank_user;
    }


    public function conRankProj($try = '')
    {
        if (empty($try)) {
            $try = 0;
        }
        $con_rank_proj = new mysqli($this->rank_proj_host, $this->rank_proj_user, $this->rank_proj_pass, $this->rank_proj_db);
        if (mysqli_connect_error()) {
            if ($try <= 2) {
                $try += 1;
                // $this->logError(mysqli_connect_error());
                $this->conRankProj($try);
            }
        }

        return $con_rank_proj;
    }


}
