reasonableframework/route/api.social.php

232 lines
6.5 KiB
PHP
Raw Permalink Normal View History

2018-09-26 15:43:31 +00:00
<?php
/**
* @file api.social.php
2018-09-26 15:43:31 +00:00
* @date 2018-09-26
2022-11-25 14:15:20 +00:00
* @author Go Namhyeon <abuse@catswords.net>
* @brief SocialTools API (refactoring from SocioRouter API)
2018-09-26 15:43:31 +00:00
*/
loadHelper("hybridauth.lnk");
loadHelper("hybridauth.dbt");
2019-04-15 04:42:13 +00:00
loadHelper("socialtool");
2018-09-26 20:53:17 +00:00
set_session_token();
$_token = get_session_token();
2018-09-26 15:43:31 +00:00
$provider = get_requested_value("provider");
$action = get_requested_value("action");
2018-09-26 20:53:17 +00:00
$redirect_uri = get_requested_value("redirect_uri");
2018-09-26 16:46:58 +00:00
$user_id = get_requested_value("user_id");
2018-09-26 16:54:21 +00:00
2018-09-26 15:43:31 +00:00
$connection_id = get_requested_value("connection_id");
2018-09-26 16:06:21 +00:00
$message = get_requested_value("message");
2018-09-26 15:43:31 +00:00
2018-10-02 06:45:27 +00:00
// if make new connection
if($action != "new") {
2019-05-20 08:19:05 +00:00
$api_session_id = get_session("api_session_id");
2018-10-02 06:45:27 +00:00
} else {
2019-05-20 08:19:05 +00:00
$api_session_id = "";
set_session("api_session_id", $api_session_id);
2018-10-02 06:45:27 +00:00
}
2018-09-26 16:46:58 +00:00
$session_data = array();
2018-09-26 17:28:16 +00:00
if(!empty($api_session_id)) {
2019-05-20 08:19:05 +00:00
$fr = read_storage_file($api_session_id, array(
"storage_type" => "session"
));
if(!$fr) {
// renew api session id
$api_session_id = "";
set_session("api_session_id", $api_session_id);
} else {
$session_data = json_decode($fr);
$provider = get_property_value("provider", $session_data);
$action = get_property_value("action", $session_data);
$redirect_uri = get_property_value("redirect_uri", $session_data);
$user_id = get_property_value("user_id", $session_data);
$connection_id = get_property_value("connection_id", $session_data);
$message = get_property_value("message", $session_data);
}
2018-09-26 16:46:58 +00:00
}
2018-09-26 17:28:16 +00:00
if(empty($provider)) {
2019-05-20 08:19:05 +00:00
set_error("provider is required field.");
show_errors();
2018-09-26 17:28:16 +00:00
}
2018-09-26 15:56:51 +00:00
$hauth_adapter = null;
$hauth_session = null;
2018-09-26 16:06:21 +00:00
$hauth_profile = null;
2018-09-26 15:56:51 +00:00
2018-09-26 15:43:31 +00:00
// load library
2018-09-26 19:49:21 +00:00
$configfile = hybridauth_load($provider);
2018-09-26 15:43:31 +00:00
if(!$configfile) {
2019-05-20 08:19:05 +00:00
set_error("can not load hybridauth library");
show_errors();
2018-09-26 15:43:31 +00:00
}
$hauth = new Hybrid_Auth($configfile);
// try session restore
$session_flag = false;
2018-09-26 17:37:46 +00:00
if(!empty($connection_id)) {
2019-05-20 08:19:05 +00:00
$hauth_session = get_stored_hybridauth_session($connection_id);
if(!empty($hauth_session)) {
try {
$hauth->restoreSessionData($hauth_session);
$session_flag = true;
} catch(Exception $e) {
set_error("maybe, your connection is broken.");
show_errors();
}
}
2018-09-26 15:43:31 +00:00
}
2018-09-26 17:28:16 +00:00
// check hybridauth request
2018-09-26 21:11:24 +00:00
if($hauth->isConnectedWith($provider)) {
2019-05-20 08:19:05 +00:00
$hauth_session = $hauth->getSessionData();
$connection_id = store_hybridauth_session($hauth_session, $user_id);
if($connection_id) {
$session_flag = true;
}
2018-09-26 17:28:16 +00:00
}
2018-09-26 15:56:51 +00:00
2018-09-26 17:28:16 +00:00
// save session
$api_session_id = get_hashed_text(make_random_id(32));
$session_data = array(
2019-05-20 08:19:05 +00:00
"api_session_id" => $api_session_id,
"provider" => $provider,
"action" => $action,
"redirect_uri" => $redirect_uri,
"user_id" => $user_id,
"connection_id" => $connection_id,
"message" => $message
2018-09-26 17:28:16 +00:00
);
$fw = write_storage_file(json_encode($session_data), array(
2019-05-20 08:19:05 +00:00
"storage_type" => "session",
"filename" => $api_session_id
2018-09-26 17:28:16 +00:00
));
if(!$fw) {
2019-05-20 08:19:05 +00:00
set_error("maybe, your storage is write-protected.");
show_errors();
2018-09-26 17:28:16 +00:00
} else {
2019-05-20 08:19:05 +00:00
set_session("api_session_id", $api_session_id);
2018-09-26 17:28:16 +00:00
}
2018-09-26 19:49:21 +00:00
if(hybridauth_check_redirect()) {
2019-05-20 08:19:05 +00:00
hybridauth_process();
2018-09-26 19:49:21 +00:00
}
2018-09-26 17:28:16 +00:00
// try authenticate
try {
2019-05-20 08:19:05 +00:00
if(!$session_flag) {
$hauth_adapter = $hauth->authenticate($provider);
} else {
$hauth_adapter = $hauth->getAdapter($provider);
}
$session_flag = true;
2018-09-26 17:28:16 +00:00
} catch(Exception $e) {
2019-05-20 08:19:05 +00:00
$hauth_adapter = $hauth->authenticate($provider);
2018-09-26 15:56:51 +00:00
}
2018-09-26 15:43:31 +00:00
2018-09-26 15:56:51 +00:00
if(!$session_flag) {
2019-05-20 08:19:05 +00:00
// if failed authenticate
redirect_uri(get_route_link("api.social", array(
"provider" => $provider,
"action" => $action,
"redirect_uri" => $redirect_uri,
"user_id" => $user_id,
"connection_id" => $connection_id
), false));
2018-09-26 15:43:31 +00:00
}
2018-09-26 17:37:46 +00:00
// get user profile
$hauth_profile = $hauth_adapter->getUserProfile();
2018-09-26 15:43:31 +00:00
// do action
2018-09-26 16:06:21 +00:00
$context = array();
2018-09-26 15:43:31 +00:00
switch($action) {
2019-05-20 08:19:05 +00:00
case "inbound":
break;
case "outbound":
$response = social_send_message($provider, $hauth_adapter, $message);
$object_id = social_parse_object_id($provider, $response);
$context = array(
"success" => !(!$object_id),
"message" => "Have a nice day",
"user_id" => $user_id,
"provider" => $provider,
"object_id" => $object_id
);
break;
case "new":
$context = array(
"success" => true,
"message" => "Authenticated",
"user_id" => $user_id,
"provider" => $provider,
"profile" => $hauth_profile,
);
break;
case "login":
$context = array(
"success" => true,
"message" => "Authenticated",
"user_id" => $user_id,
"provider" => $provider,
"profile" => $hauth_profile,
);
break;
case "bgworker":
$response = social_send_message($provider, $hauth_adapter, $message);
$object_id = social_parse_object_id($provider, $response);
$context = array(
"success" => !(!$object_id),
"message" => "Have a nice day",
"id" => $user_id,
"connection" => $connection_id,
"provider" => $provider,
"object_id" => $object_id
);
break;
case "cancel": // listen cancel authenticated callback
break;
2019-05-20 08:20:36 +00:00
case "delete": // listen delete ping
2019-05-20 08:19:05 +00:00
break;
2019-05-20 08:20:36 +00:00
case "accept": // listen accept ping
2019-05-20 08:19:05 +00:00
break;
case "object": // get object by id
$object_id = get_requested_value("object_id");
$context = array(
"success" => true,
"message" => "Found",
"response" => social_get_object($provider, $hauth_adapter, $object_id)
);
break;
default:
set_error("Unknown action");
show_errors();
2018-09-26 15:43:31 +00:00
}
2018-09-26 17:28:16 +00:00
2018-09-26 20:53:17 +00:00
if(empty($redirect_uri)) {
2019-05-20 08:19:05 +00:00
header("Content-Type: application/json");
echo json_encode($context);
2018-09-26 20:53:17 +00:00
} else {
2019-05-20 08:19:05 +00:00
$_display_name = get_hashed_text($hauth_profile->displayName, "base64");
$_idt_hash = get_hashed_text($hauth_profile->identifier, "sha1");
$_idt_name = $_idt_hash . "@" . $provider;
$_idt = get_hashed_text($_idt_name, "sha1");
// renew api session id
$api_session_id = "";
set_session("api_session_id", $api_session_id);
// go to redirect uri
redirect_with_params($redirect_uri, array(
"connection_id" => $connection_id,
"provider" => $provider,
"display_name" => $_display_name,
"idt" => $_idt,
"_token" => $_token
));
2018-09-26 20:53:17 +00:00
}