<?php
define("SLACK_REPLY_MAX_LEN", 3072);

function _slack_init($token, $chan_id, array $fields, $channel_field_name = 'channel')
{
  if(!isset($fields['token']))
    $fields['token'] = $token;
  if(!isset($fields[$channel_field_name]))
    $fields[$channel_field_name] = $chan_id;
  return $fields;
}

function _slack_curl_exec($ch, $debug)
{
  curl_setopt($ch, CURLOPT_VERBOSE, $debug);

  $result = curl_exec($ch);
  curl_close($ch);
  
  if($debug)
  {
    echo "SLACK RAW RESP BEGIN\n";
    var_dump($result);
    echo "SLACK RAW RESP END\n\n";
  }

  $resp = json_decode($result, true);

  if($debug)
  {
    echo "SLACK PARSED JSON RESP BEGIN\n";
    var_dump($resp);
    echo "SLACK PARSED JSON RESP END\n";
  }

  return $resp;
}

function slack_send_file($token, $chan_id, $post_text, $file_path, $mime, $fields = array(), $debug = false)
{
  $file_name = basename($file_path);
  $file = new \CurlFile($file_path, $mime, $file_name);

  $header = array();
  $header[] = 'Content-Type: multipart/form-data';

  $fields = _slack_init($token, $chan_id, $fields, 'channels');
  $fields['file'] =  $file;
  $fields['filename'] =  $file_name;
  $fields['title'] = $file_name;
  $fields['initial_comment'] = $post_text;
  $fields['filetype'] = pathinfo($file_path, PATHINFO_EXTENSION);

  $ch = curl_init("https://slack.com/api/files.upload");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  curl_setopt($ch, CURLOPT_MAX_SEND_SPEED_LARGE, 10*1024*1024);
  curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

  return _slack_curl_exec($ch, $debug);
}

function slack_send_message($token, $chan_id, $message, $fields = array(), $debug = false)
{
  $ch = curl_init("https://slack.com/api/chat.postMessage");
  $fields = _slack_init($token, $chan_id, $fields);
  $fields["text"] = $message;

  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  return _slack_curl_exec($ch, $debug);
}

function slack_send_reply($token, $chan_id, $thread_ts, $message, $fields = array(), $debug = false)
{
  $fields["thread_ts"] = $thread_ts;
  return slack_send_message($message, $fields, $debug, $token, $chan_id);
}

function slack_send_thread_with_replies($token, $chan_id, $message, array $replies, $fields = array(), $debug = false)
{
  $slack_orig_resp = slack_send_message(
    $token,
    $chan_id,
    $message,
    $fields,
    $debug,
  );

  if(!isset($slack_orig_resp['ts']))
  {
    echo "Failed to post to Slack: response contains no 'ts' value to post thread replies\n"; 
    return null;
  }

  $ts = $slack_orig_resp['ts'];

  foreach($replies as $reply)
  {
    slack_send_reply(
      $ts,
      $reply,
      $fields,
      $debug,
      $token,
      $chan_id
    );
  }

  return $ts;
}

function slack_trim_reply($reply, $len = SLACK_REPLY_MAX_LEN)
{
  if(strlen($reply) < $len)
    return $reply;
  return substr($reply, 0, $len - 3) . "...";
}