News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Ayuda Integracion Chat

Started by ilosa, January 21, 2016, 08:05:43 AM

Previous topic - Next topic

ilosa

Buenas amigos,

Estoy intentando integrar un chat SSO a mi foro smf pero no consigo que me funcione correctamente.

Para la integracion, el soporte del chat me ha pasado esta API y varios archivos de ejemplo:

API:
<?php
# This file contains functions for the Website Toolbox chat room single sign on.
# Replace USERNAME with your chat room username.
define("HOST","USERNAME.discussionchatroom.com");
# Get The API Key from the Settings -> Single Sign On section of the Website Toolbox chat room admin area and replace APIKEY.
define("API_KEY","APIKEY");
# Initializing session if it is not started in client project files to assign SSO login access_token into $_SESSION['access_token']. The $_SESSION['access_token'] is used in chatRoomLogout function to logout from the Website Toolbox chat room.
# Checking current session status if it does not exist in client project files then session will be started.
if (!$_SESSION) {session_start();}
#Purpose: Function for registering a new user on the Website Toolbox chat room. 
#parmeter: Param $user an array containing information about the new user. The array user will contain mandatory values (username and email) which will be used to build URL query string to register a new user on the Website Toolbox chat room. The array $user can also contain optional value such as password, avatarUrl.
# URL with user and apikey parameter passed in doHTTPCall function to create a request using curl and getting access_token from the Website Toolbox chat room on successful registration.
# Assigned access_token into $_SESSION['access_token'].  
# The returned access_token is checked for null. If it's not null then loaded with "sso/token/login?access_token" url through IMG src to login to the Website Toolbox chat room.
#return: Parse and return user registration response status.
function chatRoomSignup($user) {
foreach ($user as $key => $value) {
  if ($value === NULL)
 $user[$key] = '';
}
# Generating a URL-encoded query string from the $user array.
$parameters http_build_query($userNULL'&');   
$URL "/sso/user/register?apikey=".API_KEY."&".$parameters;
# making a request using curl and getting response from the Website Toolbox chat room.
$response doHTTPCall($URL);
$response_json json_decode($responsetrue);
$access_token $response_json['access_token'];
# Check access_token for null. If access_token not null then load with "sso/token/login?access_token" url through IMG src to login to the Website Toolbox chat room.
if ($access_token) {
$_SESSION['access_token'] = $access_token;
echo "<br/><img src='http://".HOST."/sso/token/login?access_token=$access_token' border='0' width='1' height='1' alt=''/><a href='http://".HOST."'>CHAT ROOM</a><br/><a href='logout_example.php'>LOGOUT</a>";

# returning sso register response
return $response_json['success'];   
}
# Purpose: function for login to the Website Toolbox chat room. If given email does not exist, then the user is auto-regisered on the Website Toolbox chat room.
# parmeter: Param $user an array containing information about the currently logged in user. The array user will contain mandatory parameter (email) and optional parameters (username, avatarUrl, rememberMe and password) which passed with apikey in request URL.
# URL with user and apikey parameter passed in doHTTPCall function to create a request using curl and return access_token from the Website Toolbox chat room.
# Assigned access_token into $_SESSION['access_token'].  
# The returned access_token is checked for null. If it's not null then loaded with "sso/token/login?access_token" url through IMG src to login to the Website Toolbox chat room.
# return: Returns user's login status as true or false.
function chatRoomLogin($user) {
foreach ($user as $key => $value) {
  if ($value === NULL)
 $user[$key] = '';
}
# Generating a URL-encoded query string from the $user array.
$login_parameters http_build_query($userNULL'&');
# user details stored in session which will used later in chatRoomLogout function. 
$_SESSION['login_parameters'] = $login_parameters;
$URL "/sso/token/generate?apikey=".API_KEY."&".$login_parameters;
# making a request using curl and getting response from the Website Toolbox chat room.
$response doHTTPCall($URL);
$response_json json_decode($responsetrue);
$access_token $response_json['access_token'];
# Check access_token for null. If access_token not null then load with "sso/token/login?access_token" url through IMG src to login to the Website Toolbox chat room.
if ($access_token) {
$_SESSION['access_token'] = $access_token;
$rememberMe $user['rememberMe'];
if($rememberMe)
$rememberMe 1;
echo "<br/><img src='http://".HOST."/sso/token/login?access_token=$access_token&rememberMe=$rememberMe' border='0' width='1' height='1' alt=''/><a href='http://".HOST."'>CHAT ROOM</a><br/><a href='logout_example.php'>LOGOUT</a>";

return $response_json['success']; 
}
#Purpose: function for log out from the Website Toolbox chat room.
# It check for $_SESSION['access_token'] if it's not null then the "sso/token/logout?access_token" is loaded with IMG src to logout user from the Website Toolbox chat room.
# Reset access_token session variable $_SESSION['access_token'] to blank after successful log out.
# return: the function will return log out status as true or false.
function chatRoomLogout() {
# Check for access_token value. If it is not null then load /sso/token/logout?access_token url through IMG src to log out from the Website Toolbox chat room.
if($_SESSION['access_token']) {
echo "<img src='http://".HOST."/sso/token/logout?access_token=".$_SESSION['access_token']."' border='0' width='1' height='1' alt=''>";
# Reset access_token session variable after log out.
$_SESSION['access_token'] = '';
return true;
} else {
# If access_token is missing from session variable then making a HTTP request using curl and getting access_token from the Website Toolbox chat room. 
# Fetching user details from $_SESSION['login_parameters'], which was stored in session during user login.
# If access_token not null then the "/sso/token/logout?access_token" is loaded with IMG src to logout user from the Website Toolbox chat room. 
$URL "/sso/token/getToken?apikey=".API_KEY."&".$_SESSION['login_parameters'];
$response doHTTPCall($URL);
$response_json json_decode($responsetrue);
$response_message $response_json['message'];
$access_token $response_json['access_token'];
if($access_token) {
echo "<img src='http://".HOST."/sso/token/logout?access_token=".$access_token."' border='0' width='1' height='1' alt=''>";

return $response_json['success'];
}
}
#Purpose: Function for deleting user(s) from the Website Toolbox chat room. 
#parmeter: Param $user an array containing information about users, who need to be deleted. The array user will contain comma seperated emails, which will be used to build URL query string to delete user(s) from the Website Toolbox chat room. 
#URL with all parameter from $user array passed in doHTTPCall function to create a request using curl and getting response from the Website Toolbox chat room.
#return: Parse and return user deletion response status. 
function userDeletionFromChatRoom($user) {
foreach ($user as $key => $value) {
  if ($value === NULL)
 $user[$key] = '';
}
# Generating a URL-encoded query string from the $user array.
$parameters http_build_query($userNULL'&');   
$URL "/sso/user/delete?apikey=".API_KEY."&".$parameters;
# making a request using curl and getting response from the Website Toolbox.
$response doHTTPCall($URL);
$response_json json_decode($responsetrue);
# returning response of delete user API request
return $response_json['success'];   
}
#Purpose: Function for editing the details of the user for the Website Toolbox chat room. 
#parmeter: Param $user an array containing information about user. The array user will contain user's email and parameters which need to be updated and will be used to build URL query string to edit the details for the user at the Website Toolbox chat room. 
#URL with all parameter from $user array passed in doHTTPCall function to create a request using curl and getting response from the Website Toolbox chat room.
#return: Parse and return response status. 
function editChatRoomUserDetails($user) {
foreach ($user as $key => $value) {
  if ($value === NULL)
 $user[$key] = '';
}
# Generating a URL-encoded query string from the $user array.
$parameters http_build_query($userNULL'&');   
$URL "/sso/user/edit?apikey=".API_KEY."&".$parameters;
# making a request using curl and getting response from the Website Toolbox.
$response doHTTPCall($URL);
$response_json json_decode($responsetrue);
# returning response of edit user API request
return $response_json['success'];   
}
#Purpose: Create a request using curl and getting response from the Website Toolbox chat room.
#parmeter: request URL which will use to make curl request to the Website Toolbox chat room.
#return: return response from the Website Toolbox chat room.
function doHTTPCall($URL){
$ch curl_init("http://".HOST.$URL);
curl_setopt($chCURLOPT_HEADER0);
curl_setopt($chCURLOPT_FOLLOWLOCATION1);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
$response curl_exec($ch);      
curl_close($ch);
return $response;
}
?>


Y el ejemplo de login:
<?php
require_once dirname(__FILE__).'/chat_room_sso_functions.php';
// Important Note:
// You also need to update the Website Toolbox chat room username on line 5 in chat_room_sso_functions.php.
// You also need to update the Website Toolbox chat room API Key on line 7 in chat_room_sso_functions.php.
// Your code to process the login for the user on your website goes here.
// Fill in the user information in a way that the Website Toolbox chat room can understand.
$user = array();
// After successful login to your website, assign the username and email in user array. 
// The email address is a mandatory field for login.
//$user['email'] = '[email protected]';
// The username and password are optional parameters that will only be used if the account needs to be created because it doesn't already exist.
//$user['username'] = 'john';
//$user['password'] = 'john123';
// Assign avatarUrl (profile picture URL), if you want to set the Website Toolbox chat room profile picture same as your application. This image will be shown on the Website Toolbox chat room, if user has not set his/her avatar yet on the chat room. This is optional parameter. 
//$user['avatarUrl'] = 'http://fc09.deviantart.net/fs71/f/2010/330/9/e/profile_icon_by_art311-d33mwsf.png';
$user['username'] = $_POST['username'];
$user['email'] = $_POST['email'];
$user['password'] = $_POST['password'];
$user['avatarUrl'] = $_POST['avatarUrl'];
$user['rememberMe'] = $_POST['rememberMe'];
// The function will print an IMG tag to get login on the Website Toolbox chat room.
// You can also get access_token $_SESSION['access_token'] that can be further used for hiding "login" page after successful login and displaying "logout" page on your website.
// The function will return login status as boolean flag.
// true, if successfully logged in.
// false, if any error occurs.
$login_status chatRoomLogin($user);
if(
$login_status) {
// Redirect to secure members-only area since login was successful.
}
?>


He estado mirandolo y segun entiendo hay que modificar el archivo SSO dando la url del chat, la apikey y subirlo al foro.

Despues habria que añadir el codigo que viene en los ejemplos al login, registro y logout de mi foro.

He realizado los cambios en el archivo del SSO y subido al servidor.

Ahora mi duda es donde meto el codigo de los ejemplos. He estado haciendo pruebas añadiendolo en el archivo LogInOut.php en la parte del login pero no consiguo que funcione.

A ver si algun experto puede guiarme un poco.

Añadir que utilizo SMF 2.0.11 y tema exodus.

Si necesitais algun dato o codigo mas me lo decis por favor.

Gracias por adelantado y un saludo


ilosa

Buenas,

Sigo dandole vueltas al tema pero sin sacar nada en claro.

Segun entiendo, tengo que añadir el codigo de los ejemplos, creando un array que me pase los datos a traves de la API al chat.

Para ello tengo que añadir, para el login, por ejemplo, el siguiente codigo:

Esto en la primera linea para que cargue la api:
<?php
require_once dirname(__FILE__).'/chat_room_sso_functions.php';


Despues crear la array que recoge los datos para la API, entiendo que coge los datos del formulario, que tendre que cambiar los nombres de $_POST para que coincidan con los del foro, para que despues la API trabaje con ellos:

$user = array(
$user['username'] = $_POST['username'];
$user['email'] = $_POST['email'];
$user['password'] = $_POST['password'];
$user['avatarUrl'] = $_POST['avatarUrl'];
$user['rememberMe'] = $_POST['rememberMe'];
);


Y esto ultimo que entiendo es que si esta todo correcto manda los datos a la API y te redirige, aunque no lo tengo claro

$login_status = chatRoomLogin($user);
if($login_status) {
// Redirect to secure members-only area since login was successful.
}
?>


Lo que no se es, si este codigo tengo que ponerlo en el login.template, en el LogInOut o donde.

No os pido que me hagais el codigo, simplemente decirme si voy bien encaminado y guiarme un poco para ver si consigo hacerlo funcionar.


Un Saludo y gracias


Gluz

Lo que habría que hacer es subir el archivo API con otro nombre, algo como Subs-ChatAPI.php al directorio Sources, y luego en el otro en la parte que dice require_once debe ir la ruta y nombre de ese archivo que es donde están las funciones del chat.

En ese otro que es para mostrar el chat, por lo que veo hay que llenar un array con los datos de usuario, en el ejemplo está para que primero se use una forma para loguearse, pero si lo quieres usar para tu foro exclusivo para usuarios ya logueados en tu foro puedes llenar ese array con los datos del mismo foro, con la única desventaja de que si cambian de correo electrónico entu foro su usuario en el chat cambia, según veo.

Advertisement: