[insert_php]
/**
* Allows applications to access user’s Slack information by using Slack’s OAuth functionality.
*/
class SlackOAuth{
private $client_secret;
protected $slack_endpoint = “https://slack.com/”;
protected $auth = [
‘client_id’ => null,
‘state’ => null,
‘scope’ => null,
‘team’ => null,
‘redirect’ => null
];
/**
*
* @param String $client_id Client ID as given by registration of the Slack application.
* @param String $secret Client Secret as given by registration of the Slack application.
* @param Array $scope Array of states to request. Identify, Read, Post, Client, Admin
* @param String $state Unique string to be passed back with Slack’s redirect for verification.
* @param String $team Team ID to request authorization.
* @param String $redirect URL for Slack to redirect after authorization.
*/
public function __construct($client_id = ‘24691365365.24691661989’, $secret = ‘8546adc74cae90974f93af36b57caac5’, $scope = [], $state = ”, $team = ”, $redirect = ”, $code = ”){
$this->client_secret = $secret;
$this->auth[‘client_id’] = $client_id;
$this->auth[‘state’] = $state;
$this->auth[‘scope’] = implode(“,”, $scope);
$this->auth[‘team’] = $team;
$this->auth[‘redirect’] = $redirect;
echo ‘construct done’;
}
/**
* Creates a URL to authenticate the application.
* @param String $redirect URL for Slack to redirect after confirmation.
* @return String Slack URL: OAuth/Authorize with Client ID and possible State, Scope, Team, and Redirect fields.
*/
public function auth_url($redirect = ”){
if(session_status()==PHP_SESSION_NONE){
session_start();
}
if(empty($this->auth[‘state’])){
$this->auth[‘state’] = $this->rand_str();
}
if(!empty($redirect)){
$this->auth[‘redirect’] = $redirect;
}
$_SESSION[‘state’] = $this->auth[‘state’];
$url = $this->slack_endpoint.’oauth/authorize?’.http_build_query(array_filter($this->auth), ”, ‘&’);
echo ‘url: ‘ . $url;
return $url;
}
/**
* Exchanges an OAuth code for an API access token. Forms a payload to send to the Slack OAuth/Access API call.
* @param String $code The code returned from Slack’s redirect OAuth/Authorize.
* @return Object Generic object that is the JSON decoded string returned from the payload.
* @throws Exception If the Object->ok property is false, will throw with the response’s error.
*/
public function auth_access($code){
$code = $_GET[‘code’];
$payload = [];
$payload[‘url’] = $this->slack_endpoint.’api/oauth.access’;
$payload[‘post’][‘client_id’] = $this->auth[‘client_id’];
$payload[‘post’][‘client_secret’] = $this->client_secret;
$payload[‘post’][‘code’] = $code;
$payload[‘post’][‘redirect_uri’] = $this->auth[‘redirect’];
$response = json_decode($this->_post($payload));
if(!$response->ok){
throw new Exception(‘OAuth.Access: ‘.$response->error);
}
echo ‘response: ‘ . $response;
return $response;
}
/**
* Computes a pseudo-random string.
* @return String Pseudo-random string.
*/
public function rand_str(){
return md5(uniqid(rand(), true));
}
/**
* Posts a payload to a URL.
* @param Array $payload The payload to be posted.
* @return String Response of payload. (From CURLOPT_RETURNTRANSFER)
*/
private function _post($payload){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $payload[‘url’]);
/*
* Comment this out for insecure connections, otherwise implement SSL verification.
*/
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if(isset($payload[‘post’])){
curl_setopt($ch, CURLOPT_POST, count($payload[‘post’]));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload[‘post’]));
}
$ret = curl_exec($ch);
curl_close($ch);
echo ‘ret: ‘ . $ret;
$slackArray = json_decode($ret);

print_r($slackArray);

echo $slackArray->access_token . “
“;
echo $slackArray->scope . “
“;
echo $slackArray->team_name . “
“;
echo $slackArray->team_id . “
“;
echo $slackArray->bot->bot_user_id . “
“;
echo $slackArray->bot->bot_access_token . “
“;

$access_token = $slackArray->access_token;
$scope = $slackArray->scope;
$team_name = $slackArray->team_name;
$team_id = $slackArray->team_id;
$bot_user_id = $slackArray->bot->bot_user_id;
$bot_access_token = $slackArray->bot->bot_access_token;

//$access_token = mysql_real_escape_string($slackArray->access_token);
//$scope = mysql_real_escape_string($slackArray->scope);
//$team_name = mysql_real_escape_string($slackArray->team_name);
//$team_id = mysql_real_escape_string($slackArray->team_id);
//$bot_user_id = mysql_real_escape_string($slackArray->bot->bot_user_id);
//$bot_access_token = mysql_real_escape_string($slackArray->bot->bot_access_token);

$servername = “localhost”;
$username = “avikaido_botreg”;
$password = “Mudsh4rk!”;
$dbname = “avikaido_botreg”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}

$sql = “INSERT INTO brakd_bots (fbb_access_token, fbb_scope, fbb_team_name, fbb_team_id, fbb_bot_user_id, fbb_bot_access_token)
VALUES (‘$access_token’, ‘$scope’, ‘$team_name’, ‘$team_id’, ‘$bot_user_id’, ‘$bot_access_token’)”;

if ($conn->query($sql) === TRUE) {
echo “New record created successfully”;
} else {
echo “Error: ” . $sql . “
” . $conn->error;
}

return $ret;

}
}

$obj = new SlackOAuth();
echo $obj->auth_url(), PHP_EOL, $obj->auth_access(), PHP_EOL, $obj->_post(), PHP_OEL;
echo $obj;

$conn->close();
[/insert_php]