<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Wa_sas
{
  var $username, $host, $password;
  function __construct()
  {
    // $this->host = "http://sasdev.jala.my.id:3000/send/";
    //$this->host = "http://139.59.235.205:3000/send/";
    $this->host = "http://devkedungdoro.aplikasi.web.id:7001/send/";
    $this->username = "sasdev";
    $this->password = "sasdev!#102938";
  }
  function fix_phone($phone)
  {
    //remove - and space
    $phone = str_replace("-", "", $phone);
    $phone = str_replace(" ", "", $phone);
    //remove 1st +
    if (substr($phone, 0, 1) == "+") {
      $phone = substr($phone, 1);
    }
    if (substr($phone, 0, 1) == "0") {
      $phone = "62" . substr($phone, 1);
    }
    if (substr($phone, 0, 2) != "62") {
      $phone = "62" . $phone;
    }
    return $phone;
  }

  function send_image_group(
    $phone,
    $caption,
    $url_image,
    $contentType,
    $file_name,
    $extension,
    $view_once = false,
    $compress = false
  ) {
    $tmpFile = tempnam(sys_get_temp_dir(), 'sasdev') . ".$extension";
    file_put_contents($tmpFile, file_get_contents($url_image));
    $data = [
      "phone" => $phone,
      "caption" => $caption,
      "view_once" => $view_once,
      "compress" => $compress,
      "image" => curl_file_create($tmpFile, $contentType, $file_name)
    ];
    $ch = curl_init($this->host . "image");
    //curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
    $return = curl_exec($ch);
    curl_close($ch);
    unlink($tmpFile);
    $j_return = json_decode($return, true);
    if (json_last_error() != 0) {
      return ["code" => "ERROR", "message" => "Error Json Decode : $return"];
    }
    return $j_return;
  }
  function send_message($phone, $message, $is_group = false)
  {
    $data = [
      "phone" => $is_group ? $phone : $this->fix_phone($phone),
      "message" => $message
    ];
    $ch = curl_init($this->host . "message");
    curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $return = curl_exec($ch);
    curl_close($ch);
    $j_return = json_decode($return, true);

    if (json_last_error() != 0) {
      return ["code" => "ERROR", "message" => "Error Json Decode : $return"];
    }
    return $j_return;
  }
  function send_image(
    $phone,
    $caption,
    $url_image,
    $contentType,
    $file_name,
    $extension,
    $view_once = false,
    $compress = false
  ) {
    $tmpFile = tempnam(sys_get_temp_dir(), 'sasdev') . ".$extension";
    file_put_contents($tmpFile, file_get_contents($url_image));
    $data = [
      "phone" => $this->fix_phone($phone),
      "caption" => $caption,
      "view_once" => $view_once,
      "compress" => $compress,
      "image" => curl_file_create($tmpFile, $contentType, $file_name)
    ];
    $ch = curl_init($this->host . "image");
    //curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
    $return = curl_exec($ch);
    curl_close($ch);
    unlink($tmpFile);
    $j_return = json_decode($return, true);
    if (json_last_error() != 0) {
      return ["code" => "ERROR", "message" => "Error Json Decode : $return"];
    }
    return $j_return;
  }
  function send_file(
    $phone,
    $caption,
    $url_file,
    $contentType,
    $file_name,
    $extension,
    $compress = false
  ) {
    $tmpFile = tempnam(sys_get_temp_dir(), 'sasdev') . ".$extension";
    file_put_contents($tmpFile, file_get_contents($url_file));
    $data = [
      "phone" => $this->fix_phone($phone),
      "caption" => $caption,
      "compress" => $compress,
      "file" => curl_file_create($tmpFile, $contentType, $file_name)
    ];
    $ch = curl_init($this->host . "file");
    //curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
    $return = curl_exec($ch);
    curl_close($ch);
    unlink($tmpFile);
    $j_return = json_decode($return, true);
    if (json_last_error() != 0) {
      return ["code" => "ERROR", "message" => "Error Json Decode : $return"];
    }
    return $j_return;
  }
}
