PHP - Integracion Chat SSO

 
Vista:
sin imagen de perfil

Integracion Chat SSO

Publicado por ilosa (1 intervención) el 23/01/2016 13:17:58
Buenas amigos,

Necesito que me echéis una mano, por favor, os cuento;

Estoy montando un foro para un amigo, he decir que tengo muy pocos conocimientos de php, en su momento estudie html y javascrip, que me sirve un poco para entender parte del código o hacer modificaciones pero ya con otras cosas me pierdo.

La cosa es que queremos integrar un chat extero con Single Sign On para que los usuarios solo tengan que loguearse en el foro y accedan al chat con el mismo user y pass.

Desde el soporte del chat me han dado una api y varios archivos de ejemplo para la integración con php.

El foro esta montado en SMF pero entiendo que al basarse en php no hay problema.

La API que me han dado, en la que tengo que modificar el USERNAME y APIKEY:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?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($user, NULL, '&');
	$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($response, true);
	$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($user, NULL, '&');
	# 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($response, true);
	$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($response, true);
		$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($user, NULL, '&');
	$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($response, true);
	# 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($user, NULL, '&');
	$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($response, true);
	# 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($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$response = curl_exec($ch);      
	curl_close($ch);
	return $response;
}
?>

Y el archivo de ejemplo de login:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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'] = 'john.php@anonymous.com';
// 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.
}
?>

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:
1
2
<?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:
1
2
3
4
5
6
7
$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
1
2
3
4
5
$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.

A ver si algun experto puede guiarme un poco

Un Saludo y gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder