2018-09-26 15:43:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file api.socialhub.php
|
|
|
|
* @date 2018-09-26
|
|
|
|
* @author Go Namhyeon <gnh1201@gmail.com>
|
|
|
|
* @brief SocialHub API (refactoring from SocioRouter API)
|
|
|
|
*/
|
|
|
|
|
|
|
|
loadHelper("hybridauth.lnk");
|
|
|
|
loadHelper("hybridauth.dbt");
|
|
|
|
|
|
|
|
$provider = get_requested_value("provider");
|
|
|
|
$action = get_requested_value("action");
|
|
|
|
$redirect_url = get_requested_value("redirect_url");
|
|
|
|
|
|
|
|
$user_id = get_reqeusted_value("user_id");
|
|
|
|
$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-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
|
|
|
// check hauth parameters
|
|
|
|
$is_hauth = false;
|
|
|
|
foreach($requests['_ALL'] as $k=>$v) {
|
|
|
|
if(strpos($k, "hauth") === false) {
|
|
|
|
$is_hauth = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load library
|
|
|
|
$configfile = load_hybridauth($provider);
|
|
|
|
if(!$configfile) {
|
|
|
|
set_error("can not load hybridauth library");
|
|
|
|
show_errors();
|
|
|
|
}
|
|
|
|
$hauth = new Hybrid_Auth($configfile);
|
|
|
|
|
|
|
|
// try session restore
|
|
|
|
$session_flag = false;
|
|
|
|
if(empty($connection_id)) {
|
2018-09-26 15:56:51 +00:00
|
|
|
$hauth_session = get_stored_hybridauth_session($connection_id);
|
2018-09-26 15:43:31 +00:00
|
|
|
if(!empty($hauth_session)) {
|
|
|
|
try {
|
|
|
|
$hauth->restoreSessionData($hauth_session);
|
|
|
|
$session_flag = true;
|
|
|
|
} catch(Exception $e) {
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// do authenticate
|
|
|
|
if(!$session_flag) {
|
|
|
|
try {
|
2018-09-26 16:06:21 +00:00
|
|
|
$hauth_adapter = $hauth->authenticate($provider);
|
2018-09-26 15:43:31 +00:00
|
|
|
} catch(Exception $e) {
|
2018-09-26 15:56:51 +00:00
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_null($hauth_adapter)) {
|
|
|
|
$hauth_session = $hauth->getSessionData();
|
|
|
|
$connection_id = store_hybridauth_session($hauth_session, $user_id);
|
|
|
|
if($connection_id) {
|
|
|
|
$session_flag = true;
|
2018-09-26 16:06:21 +00:00
|
|
|
$hauth_profile = $hauth_adapter->getUserProfile();
|
2018-09-26 15:56:51 +00:00
|
|
|
}
|
2018-09-26 15:43:31 +00:00
|
|
|
}
|
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 failed authenticate
|
|
|
|
if(!$session_flag) {
|
|
|
|
// if failed authenticate
|
|
|
|
redirect_uri(get_route_link("api.socialhub", array(
|
|
|
|
"provider" => $provider,
|
|
|
|
"action" => $action,
|
|
|
|
"redirect_url" => $redirect_url,
|
|
|
|
"user_id" => $user_id
|
|
|
|
), false));
|
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) {
|
|
|
|
case "inbound":
|
|
|
|
break;
|
|
|
|
case "outbound":
|
2018-09-26 16:06:21 +00:00
|
|
|
$hauth_adapter->setUserStatus($message);
|
|
|
|
|
|
|
|
|
2018-09-26 15:43:31 +00:00
|
|
|
break;
|
|
|
|
case "new":
|
|
|
|
break;
|
|
|
|
case "login":
|
2018-09-26 16:06:21 +00:00
|
|
|
$context = array(
|
|
|
|
"success" => true,
|
|
|
|
"message" => "Authenticated",
|
|
|
|
"user_id" => $user_id,
|
|
|
|
"provider" => $provider,
|
|
|
|
"profile" => $hauth_profile,
|
|
|
|
);
|
2018-09-26 15:43:31 +00:00
|
|
|
break;
|
|
|
|
}
|