<?php
session_start([0]);

class MainFunction
{
    function __construct()
    {
        $host = "localhost";
        $user = "angeeoyl_lords_message_mp3";
        $pw = "Q}Jkwc$[4hiB";
        $db = "angeeoyl_lords_message_mp3";
        $this->db = new PDO("mysql:$host", $user, $pw);
        $this->db->exec("set names utf8");
        $this->db->exec("CREATE DATABASE IF NOT EXISTS $db");
        $this->db->exec("USE $db");


    }

    
    function checkAuth($username, $password)
    {
        $password = md5($password);
        $to = $this->db->query("SELECT * FROM `users` WHERE `username` ='$username' AND `password` ='$password'  ");
        if ($to->rowCount() > 0) {
            $_SESSION['admin'] = $to->fetch(PDO::FETCH_ASSOC);
            return "ok";
        } else {
            return "bad";
        }
    }

    function store($title, $content,$audio, $image)
    {
        // echo "<pre>"; print_r($_FILES);die;
        $url = $this->Slug($title);

        $stmt = $this->db->prepare("insert into articles (title,content,audio,image,url) values (?,?,?,?,?) ");
        if (!empty($image['tmp_name']) && !empty($audio['tmp_name'])) {
            $imageName = rand(10,9999).'-'.date('Y-m-d') . "-" . $image['name'];
            $audioName = rand(10,9999).'-'.date('Y-m-d') . "-" . $audio['name'];
            $path = "../images/thumb/";
            move_uploaded_file($image['tmp_name'], $path . $imageName);
            move_uploaded_file($audio['tmp_name'], $path . $audioName);
            $stmt->execute([$title, $content,$audioName, $imageName,$url]);
        } else {
            $stmt->execute([$title, $content, null,null,$url]);
        }

    }

    function Slug($string)
    {
        return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
    }

    function delete($id)
    {
        $this->db->query("delete from articles where id = '$id' ");
        return true;
    }

    function update($title, $content,$audio ,$image, $id)
    {
        $url = $this->Slug($title);
  
        if (!empty($image['tmp_name']) && !empty($audio['tmp_name'])) {
          $stmt = $this->db->prepare("update articles set title = ?, url = ? ,content = ?,audio = ? ,image = ? where id = ? ");
            $imageName = rand(10,9999).'-'.date('Y-m-d') . "-" . $image['name'];
            $audioName = rand(10,9999).'-'.date('Y-m-d') . "-" . $audio['name'];
            $path = "../images/thumb/";
            move_uploaded_file($image['tmp_name'], $path . $imageName);
             move_uploaded_file($audio['tmp_name'], $path . $audioName);
            $stmt->execute([$title, $url,$content,$audioName, $imageName, $id]);
        } else {
            $stmt = $this->db->prepare("update articles set title = ?,url = ?, content = ? where id = ? ");
            $stmt->execute([$title,$url, $content, $id]);
        }
        // echo "<pre>"; print_r($stmt); die;
    }

    function getArticleById($id)
    {
        return $this->db->query("select * from articles where id = '$id' ")->fetch(PDO::FETCH_ASSOC);
    }

    function getArticle_sign($url)
    {
        return $this->db->query("select * from articles where url = '$url' ")->fetch(PDO::FETCH_ASSOC);
    }


  function getArticleByUrl($url)
    {
        return $this->db->query("select * from articles where url = '$url' ")->fetchAll(PDO::FETCH_ASSOC);
    }

    function getAllArticles()
    {
        return $this->db->query('select * from articles order by id desc')->fetchAll(PDO::FETCH_ASSOC);
    }

    function angels()
    {
        $data = [
            'lord_1', 
            'lord_2',
            'lord_3', 
            'lord_4', 
            'lord_5',
            'lord_6',
            'lord_7',
            'lord_8',
            'lord_9',
            'lord_10',
                      
        ];
        return $data;
    }



    function randomArticleSeven()
    {
        return $this->db->query("SELECT * FROM articles ORDER BY RAND() LIMIT 0,10")->fetchAll(PDO::FETCH_ASSOC);
    }

    function popularArticles()
    {
        return $this->db->query("SELECT * FROM articles ORDER BY RAND() desc LIMIT 0,3")->fetchAll(PDO::FETCH_ASSOC);
    }

    function randomArticleOne()
    {
        return $this->db->query("SELECT * FROM articles ORDER BY RAND() LIMIT 1")->fetch(PDO::FETCH_ASSOC);
    }
    function mergeArray($angels,$articles){

        $newArticle = [];
        foreach ($articles as $key => $val){
            if($angels){
                $angel = $angels[$key];
            }else{
                $angel =$angels[$key];
            }
            
            $newArticle[] = [
                'id' => $val['id'],
                'title' => $val['title'],
                'content' => $val['content'],
                'image' => $val['image'],
                'url' => $val['url'],
                'angel' => $angel
            ];
        }
        return $newArticle;
    }

 function single_slug($angels,$articles){
        
            // $uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments =$this->uri->segment(2);

// echo $uri_segments;die;
          
//           return $newArticle;
    }

    function updateView($id, $views)
    {
        $stmt = $this->db->prepare("update articles set views = ? where id = ? ");
        $stmt->execute([$views,$id]);
    }
    
    function add_adsense_code($detail)
    { 
			
	      $In_article_ads = $this->db->query("SELECT * FROM `adsense` WHERE `key` ='In-article-ads' ")->fetch(PDO::FETCH_ASSOC);
		  $In_article_ads_code = $In_article_ads['code'];
		  
	     
		$detail = str_replace("##in_article_ads##",$In_article_ads_code,$detail);
		
		return $detail;
		
    }
}

$use = new MainFunction();
