<?php
function url_dasar(){
    //$_SERVER['SERVER_NAME'] : alamat website, misalkan websitemu.com
    // $_SERVER['SCRIPT_NAME'] : directory website, websitemu.com/blog/ $_SERVER['SCRIPT_NAME'] : blog
    $url_dasar  = "http://".$_SERVER['SERVER_NAME'].dirname($_SERVER['SCRIPT_NAME']);
    return $url_dasar;
}

 
function ambil_gambar($id_tulisan){
    global $koneksi;
    $sql1 = "select * from halaman where id = '$id_tulisan'";
    $q1   = mysqli_query($koneksi,$sql1);
    $r1   = mysqli_fetch_array($q1);
    $text = $r1['isi'];

    preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $text, $img);
    $gambar = $img[1]; // ../gambar/filename.jpg
    $gambar = str_replace("../gambar/","assets/images/",$gambar);
    return $gambar;
} 
function ambil_setting(){
    global $koneksi;
    $sql1 = "select * from setting";
    $q1   = mysqli_query($koneksi,$sql1);
    $r1   = mysqli_fetch_array($q1);
    return $r1;
}

function ambil_kutipan($id_tulisan){
    global $koneksi;
    $sql1   = "select * from halaman where id = '$id_tulisan'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    $text   = $r1['kutipan'];
    return $text;
}

function ambil_judul($id_tulisan){
    global $koneksi;
    $sql1   = "select * from halaman where id = '$id_tulisan'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    $text   = $r1['judul'];
    return $text;
}

function ambil_isi($id_tulisan){
    global $koneksi;
    $sql1   = "select * from halaman where id = '$id_tulisan'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    $text   = strip_tags($r1['isi']);
    return $text;
}
function bersihkan_judul($judul){
    $judul_baru     = strtolower($judul);
    $judul_baru     = preg_replace("/[^a-zA-Z0-9\s]/","",$judul_baru);
    $judul_baru     = str_replace(" ","-",$judul_baru);
    return $judul_baru;
}
function buat_link_halaman($id){
    global $koneksi;
    $sql1    = "select * from halaman where id = '$id'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    $judul  = bersihkan_judul($r1['judul']);
    // http://localhost/website-company-profile/halaman.php/8/judul
    return url_dasar()."/halaman.php/$id/$judul";
}

function buat_btn_halaman($id){
    global $koneksi;
    $sql1    = "select * from halaman where id = '$id'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    $judul  = $r1['button'];
    // http://localhost/website-company-profile/halaman.php/8/judul
    // return url_dasar()."/halaman.php/$id/$judul";
    return $judul;
}

function dapatkan_id(){
    $id     ="";
    if(isset($_SERVER['PATH_INFO'])){
        $id = dirname($_SERVER['PATH_INFO']);
        $id = preg_replace("/[^0-9]/","",$id);
    }
    return $id;
}

function set_isi($isi){
    $isi    = str_replace("../gambar/",url_dasar()."/gambar/",$isi);
    return $isi;
}

function maximum_kata($isi,$maximum){
    $array_isi = explode(" ",$isi);
    $array_isi = array_slice($array_isi,0,$maximum);
    $isi = implode(" ",$array_isi);
    return $isi;
}
function data_tutors(){
    global $koneksi;
    $sql1   = "select * from tutors";
    $q1     = mysqli_query($koneksi,$sql1);
  
    while ($r1= mysqli_fetch_assoc($q1)) {
      $result[] = $r1;
    }
    
return $result;


}
function data_services(){
    global $koneksi;
    $sql1   = "select * from blog where ket = 'services'";
    $q1     = mysqli_query($koneksi,$sql1);
  
    while ($r1= mysqli_fetch_assoc($q1)) {
      $result[] = $r1;
    }
    
return $result;


}

function data_setting(){
    global $koneksi;
    $sql1   = "select * from setting";
    $q1     = mysqli_query($koneksi,$sql1);
  
    while ($r1= mysqli_fetch_assoc($q1)) {
      $result[] = $r1;
    }
    
return $result;


}
function tutors_foto($id){
    global $koneksi;
    $sql1   = "select * from tutors where id = '$id'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    $foto   = $r1['foto'];

    if($foto){
        return $foto;
    }else{
        return 'tutors_default_picture.png';
    }
}
function tutors_nama($id){
    global $koneksi;
    $sql1   = "select * from tutors where id = '$id'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1); 
    $nama   = $r1['nama'];
    return $nama;
    
}

function tutors_isi($id){
    global $koneksi;
    $sql1   = "select * from tutors where id = '$id'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1); 
    $text   = strip_tags($r1['isi']);
    return $text;
    
}

function buat_link_tutors($id){
    global $koneksi;
    $sql1    = "select * from tutors where id = '$id'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    $nama  = bersihkan_judul($r1['nama']);
    // http://localhost/website-company-profile/halaman.php/8/judul
    return url_dasar()."/tutors.php/$id/$nama";
}

function partners_foto($id){
    global $koneksi;
    $sql1   = "select * from partners where id = '$id'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    $foto   = $r1['foto'];

    if($foto){
        return $foto;
    }else{
        return 'partners_default_picture.png';
    }
}

function buat_link_partners($id){
    global $koneksi;
    $sql1    = "select * from partners where id = '$id'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    $nama  = bersihkan_judul($r1['nama']);
    // http://localhost/website-company-profile/halaman.php/8/judul
    return url_dasar()."/partners.php/$id/$nama";
}

function ambil_isi_info($id,$kolom){
    global $koneksi;
    $sql1   = "select $kolom from info where id = '$id'";
    $q1     = mysqli_query($koneksi,$sql1);
    $r1     = mysqli_fetch_array($q1);
    return $r1[$kolom];
}

function data_page(){
    global $koneksi;
    $sql1   = "SELECT master_menu,nama_menu FROM halaman GROUP BY master_menu";
    $q1     = mysqli_query($koneksi,$sql1);
     
    while ($r1= mysqli_fetch_assoc($q1)) {
      $result[] = $r1;
    }
    
return $result;
}

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

function kirim_email($email_penerima, $nama_penerima,$judul_email,$isi_email){
    
    $email_pengirim     = "emailmu@gmail.com";
    $nama_pengirim      = "noreply";

    //Load Composer's autoloader
    require getcwd().'/vendor/autoload.php';

    //Instantiation and passing `true` enables exceptions
    $mail = new PHPMailer(true);

    try {
        //Server settings
        $mail->SMTPDebug = 0;                      //Enable verbose debug output
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
        $mail->Username   = $email_pengirim;                     //SMTP username
        $mail->Password   = 'passwordmu';                               //SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 587;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

        //Recipients
        $mail->setFrom($email_pengirim, $nama_pengirim);
        $mail->addAddress($email_penerima,$nama_penerima);     //Add a recipient
       

        

        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = $judul_email;
        $mail->Body    = $isi_email;
        

        $mail->send();
        return "sukses";
    } catch (Exception $e) {
        return "gagal: {$mail->ErrorInfo}";
    }
}

function sendwa($massage){

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.fonnte.com/send',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array(
'target' => '085778888701',
'message' => $massage,
'countryCode' => '62',
),
  CURLOPT_HTTPHEADER => array(
    'Authorization: djnbkXFZN1iS7A3i3_qH'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
return json_decode($response,true);
// $response =  str_replace('{', '[', $response) ;
// $response =  str_replace('}', ']', $response) ;
// $response =  str_replace(':', '=>', $response) ;
// return $response;
}
