<?php
include 'config.php';
//date_default_timezone_set('Asia/Jakarta');
ini_set('date.timezone', 'Asia/Jakarta');
$nowtgl = date('Y-m-d H:i:s');
$update ='20201103';

$con=mysqli_connect($dbhost,$dbuser,$dbpassword,$dbname);
if (!$con)
    echo '
    <html>
    <head>
        <title>Cek Koneksi...</title>
    </head>
    <body><br>

    <img src="https://yantonaim.web.id/wp-content/uploads/2020/08/animat-rocket-color.gif" width="20%" height="20%" style="display: block; margin: auto;"><br><center>Koneksi ke database Terputus atau mungkin lagi gangguan server</center>
    </body>
    </html>
    ';

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
  $media= 'HP Android';
}else{
  $media= 'Laptop/Komputer';
}

$dakun=mysqli_fetch_array(mysqli_query($con,"select * from akunn where id=1"));
$kodenegara = $dakun['kodenegara'];$hp = $dakun['hp'];$logo = $dakun['logo'];$captcha = $dakun['captcha'];$emailakun = $dakun['e2'];$proteksi = $dakun['proteksi']; $telegram_id = $dakun['idtelegram']; $secret_token = $dakun['tokentelegram'];

$apismtp=mysqli_fetch_array(mysqli_query($con,"select * from apismtp where id=1"));
$smtp1a = $apismtp['smtp1a'];$from_name = $apismtp['smtp1b'];$from_email = $apismtp['smtp1c'];$api_token = $apismtp['smtp1d'];

function validemail($email) {
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    return false;
  }
  
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&?'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",$local_array[$i])) {
      return false;
    }
  }
  
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false;
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$",$domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}

function special($p) {
		$p = htmlentities($p, ENT_COMPAT,'UTF-8');
		$p = str_replace('--','-&minus;',$p);
		return $p;
	}
	
function txtonly($p) {
	$p = strtolower(preg_replace("/[^a-zA-Z0-9]+/", "", $p));
	return $p;
	}

function curPageURL() {
	$pageURL = 'http';
	$parse = parse_url($_SERVER["REQUEST_URI"]);
	$path = $parse['path'];
	if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
		$pageURL .= "://";
	if (isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$path;
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$path;
	}  
 	return $pageURL;
}

function time_since($original)
{
  date_default_timezone_set('Asia/Jakarta');
  $chunks = array(
      array(60 * 60 * 24 * 365, 'tahun'),
      array(60 * 60 * 24 * 30, 'bulan'),
      array(60 * 60 * 24 * 7, 'minggu'),
      array(60 * 60 * 24, 'hari'),
      array(60 * 60, 'jam'),
      array(60, 'menit'),
  );
 
  $today = time();
  $since = $today - $original;
 
  if ($since > 604800)
  {
    $print = date("M jS", $original);
    if ($since > 31536000)
    {
      $print .= ", " . date("Y", $original);
    }
    return $print;
  }
 
  for ($i = 0, $j = count($chunks); $i < $j; $i++)
  {
    $seconds = $chunks[$i][0];
    $name = $chunks[$i][1];
 
    if (($count = floor($since / $seconds)) != 0)
      break;
  }
 
  $print = ($count == 1) ? '1 ' . $name : "$count {$name}";
  return $print . ' yang lalu';
}

function randomString($length = 6) {
	 $str = "";
	 $characters = array_merge(range('A','Z'), range('a','z'), range('0','9'));
	 $max = count($characters);
	 for ($i = 0; $i < $length; $i++) {
	  $rand = mt_rand(0, $max);
	  $str .= $characters[$rand];
	 }
	 return $str;
	}
	
function bacaURL($url){
     $session = curl_init(); // buat session
     // setting CURL
     curl_setopt($session, CURLOPT_URL, $url);
  curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
     $hasil = curl_exec($session);
     curl_close($session);
     return $hasil;
}	

function SendAPI_SMS($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

function sendMessage($telegram_id, $message_text, $secret_token) {
    $url = "https://api.telegram.org/bot" . $secret_token . "/sendMessage?parse_mode=markdown&chat_id=" . $telegram_id;
    $url = $url . "&text=" . urlencode($message_text);
    $ch = curl_init();
    $optArray = array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true
    );
    curl_setopt_array($ch, $optArray);
    $result = curl_exec($ch);
    curl_close($ch);
}

 function cekTypeImageUpload($tipe_file){
       $hasil = 0;  // kondisikan terlebih dahulu kalau image benar
       $tipe  = $tipe_file;
       if ($tipe != "image/jpg") {
          $hasil = 1; // kondisi image salah
       }
        
       return $hasil;
 }
 
function sendSMS($url, $data){
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $output = curl_exec($ch); 
    curl_close($ch);      
    return $output;
}

function sendWA($hp, $pesan, $img, $key) {
    $key=$key;
    $url='http://116.203.92.59/api/send_image_url';
    $img_url=$img;
    $data = array(
      "phone_no"=> $hp, //+6282150651122
      "key"		=>$key,
      "url"		=>$img_url,
      "message"	=>$pesan
    );
    $data_string = json_encode($data);
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 360);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($data_string))
    );
    echo $res=curl_exec($ch);
    curl_close($ch);
}

function sendWoowandroid($appid, $csid, $pesan, $hp) {
    $param = array(
    'app_id' => $appid, 
    'include_player_ids' => [$csid], 
    'data' => array(
        "type"      => 'Reminder', //opsional Reminder/After Checkout/Pending Payment/dll editable
        "message"   => $pesan,
        "no_wa"     => $hp //6282150651122
    ),
    'contents'  => array(
        "en"    => 'Woowa Title'
    ),
    "headings"  =>  array(
        "en"    => 'Woowa Notice'
    )
    );
    $data_json = json_encode($param);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://onesignal.com/api/v1/notifications');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Authorization: Basic NjY0NzE3MTYtMzc3ZC00YmY5LWJhNzQtOGRiMWM1ZTNhNzBh')); //os_auth don't change
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;
}

function sendWoowandroiddua($csid, $pesan, $hp) {
    $param = array(
        'data' => array(
            "message"   => $pesan,
            //, "schedule" => '2020-05-13 11:21:15',
            "number"     => $hp
        ),
        //id token android device
        'to' => $csid
    );
    $data_json = json_encode($param);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, $data_json);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: Bearer AIzaSyCyXH1aC4rWgMQhaJuQLUTDXfWRBgrCZF4')); //os_auth don't change
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;    
}    

function createIsgd($strURL) {
    $isgd = file_get_contents("https://is.gd/api.php?longurl=" . $strURL);
    return $isgd;
}

function qrcodelogo($isi_teks, $namafile,$tempdir,$logopath) {
    
    include "phpqrcode/qrlib.php";
    $quality = 'H'; 
    $ukuran = 8; 
    $padding = 0;
    
    QRCode::png($isi_teks,$tempdir.$namafile,QR_ECLEVEL_H,$ukuran,$padding);
    $filepath = $tempdir.$namafile;
    $QR = imagecreatefrompng($filepath);
            
    $logo = imagecreatefromstring(file_get_contents($logopath));
    $QR_width = imagesx($QR);
    $QR_height = imagesy($QR);
        
    $logo_width = imagesx($logo);
    $logo_height = imagesy($logo);
            
    $logo_qr_width = $QR_width/2.5;
    $scale = $logo_width/$logo_qr_width;
    $logo_qr_height = $logo_height/$scale;
            
    imagecopyresampled($QR, $logo, $QR_width/3.3, $QR_height/2.5, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
            
    imagepng($QR,$filepath);
}

function SendEmailMalketing($from_name,$from_email,$recipient,$subject,$content,$api_token){
    
    $params = [
    'from_name' => $from_name,
    'from_email' => $from_email,
    'recipient' => $recipient,
    'subject' => $subject,
    'content' => $content,
    'api_token' => $api_token,
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://app.mailketing.id/api/v1/send");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);
    //print_r($output);
    curl_close ($ch);
    
    $array = json_decode($output);
    $sts1 = $array->response;
    $sts2 = $array->status;
    IF($sts1 =='Mail Sent' && $sts2=='success'){
        //echo 'Sukses';
    }else{
        //echo 'Gagal';
    }
}

function AddEmailMalketing($first_name,$email,$api_token,$listID){
    
    $params = [
    'first_name' => $first_name,
    'email' => $email,
    'api_token' => $api_token,
    'list_id' => $listID,
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://app.mailketing.id/api/v1/addsubtolist");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);
    print_r($output);
    curl_close ($ch);
}

function domain_exists($email){
        list($user, $domain) = explode('@', $email);
        $arr= dns_get_record($domain,DNS_MX);
        if($arr[0]['host']==$domain&&!empty($arr[0]['target'])){
                return $arr[0]['target'];
        }
}

function temp($url){
    global $resulttemp;
    $json_url = $url;
    $ch = curl_init ($json_url);
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array('Content-type: application/json'),
    );
    curl_setopt_array ($ch, $options); 
    $resulttemp = curl_exec($ch); 
    //$show = print_r($resulttemp);
    //echo $show;
}

$url3 = 'https://app.biznis.top/cekup.php';
$ch = curl_init ($url3);
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array('Content-type: application/json'),
);
curl_setopt_array ($ch, $options); 
$result = curl_exec($ch); 
//print_r($result);
$decode3 = json_decode($result, true);