/
demo.php
188 lines (159 loc) · 7.71 KB
/
demo.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
function oauth_session_exists() {
if((is_array($_SESSION)) && (array_key_exists('oauth', $_SESSION))) {
return TRUE;
} else {
return FALSE;
}
}
try {
// include the LinkedIn class
require_once('linkedin.php');
// start the session
if(!session_start()) {
throw new LinkedInException('This script requires session support, which appears to be disabled according to session_start().');
}
// display constants etc.
$API_CONFIG = array(
'appKey' => '75wpz389ifrzbq',
'appSecret' => 'ZYEG6gBlFhhUAkfR',
'callbackUrl' => NULL
);
define('DEMO_GROUP', '4010474');
define('DEMO_GROUP_NAME', 'Simple LI Demo');
define('PORT_HTTP', '80');
define('PORT_HTTP_SSL', '443');
// set index
$_REQUEST[LINKEDIN::_GET_TYPE] = (isset($_REQUEST[LINKEDIN::_GET_TYPE])) ? $_REQUEST[LINKEDIN::_GET_TYPE] : '';
switch($_REQUEST[LINKEDIN::_GET_TYPE]) {
case 'initiate':
if($_SERVER['HTTPS'] == 'on') {
$protocol = 'https';
} else {
$protocol = 'http';
}
$API_CONFIG['callbackUrl'] = $protocol . '://' . $_SERVER['SERVER_NAME'] . ((($_SERVER['SERVER_PORT'] != PORT_HTTP) || ($_SERVER['SERVER_PORT'] != PORT_HTTP_SSL)) ? ':' . $_SERVER['SERVER_PORT'] : '') . $_SERVER['PHP_SELF'] . '?' . LINKEDIN::_GET_TYPE . '=initiate&' . LINKEDIN::_GET_RESPONSE . '=1';
$OBJ_linkedin = new LinkedIn($API_CONFIG);
// check for response from LinkedIn
$_GET[LINKEDIN::_GET_RESPONSE] = (isset($_GET[LINKEDIN::_GET_RESPONSE])) ? $_GET[LINKEDIN::_GET_RESPONSE] : '';
if(!$_GET[LINKEDIN::_GET_RESPONSE]) {
// LinkedIn hasn't sent us a response, the user is initiating the connection
// send a request for a LinkedIn access token
$response = $OBJ_linkedin->retrieveTokenRequest();
if($response['success'] === TRUE) {
// store the request token
$_SESSION['oauth']['linkedin']['request'] = $response['linkedin'];
// redirect the user to the LinkedIn authentication/authorisation page to initiate validation.
header('Location: ' . LINKEDIN::_URL_AUTH . $response['linkedin']['oauth_token']);
} else {
// bad token request
echo "Request token retrieval failed:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
}
} else {
// LinkedIn has sent a response, user has granted permission, take the temp access token, the user's secret and the verifier to request the user's real secret key
$response = $OBJ_linkedin->retrieveTokenAccess($_SESSION['oauth']['linkedin']['request']['oauth_token'], $_SESSION['oauth']['linkedin']['request']['oauth_token_secret'], $_GET['oauth_verifier']);
if($response['success'] === TRUE) {
// the request went through without an error, gather user's 'access' tokens
$_SESSION['oauth']['linkedin']['access'] = $response['linkedin'];
// set the user as authorized for future quick reference
$_SESSION['oauth']['linkedin']['authorized'] = TRUE;
// redirect the user back to the demo page
header('Location: ' . $_SERVER['PHP_SELF']);
} else {
// bad token access
echo "Access token retrieval failed:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
}
}
break;
case 'revoke':
if(!oauth_session_exists()) {
throw new LinkedInException('This script requires session support, which doesn\'t appear to be working correctly.');
}
$OBJ_linkedin = new LinkedIn($API_CONFIG);
$OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
$response = $OBJ_linkedin->revoke();
if($response['success'] === TRUE) {
// revocation successful, clear session
session_unset();
$_SESSION = array();
if(session_destroy()) {
// session destroyed
header('Location: ' . $_SERVER['PHP_SELF']);
} else {
// session not destroyed
echo "Error clearing user's session";
}
} else {
// revocation failed
echo "Error revoking user's token:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
}
break;
default:
// nothing being passed back, display demo page
// check PHP version
if(version_compare(PHP_VERSION, '5.0.0', '<')) {
throw new LinkedInException('You must be running version 5.x or greater of PHP to use this library.');
}
// check for cURL
if(extension_loaded('curl')) {
$curl_version = curl_version();
$curl_version = $curl_version['version'];
} else {
throw new LinkedInException('You must load the cURL extension to use this library.');
}
$_SESSION['oauth']['linkedin']['authorized'] = (isset($_SESSION['oauth']['linkedin']['authorized'])) ? $_SESSION['oauth']['linkedin']['authorized'] : FALSE;
if($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
$OBJ_linkedin = new LinkedIn($API_CONFIG);
$OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
$OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_XML);
// check if the viewer is a member of the test group
$response = $OBJ_linkedin->group(DEMO_GROUP, ':(relation-to-viewer:(membership-state))');
if($response['success'] === TRUE) {
$result = new SimpleXMLElement($response['linkedin']);
$membership = $result->{'relation-to-viewer'}->{'membership-state'}->code;
$in_demo_group = (($membership == 'non-member') || ($membership == 'blocked')) ? FALSE : TRUE;
} else {
// request failed
echo "Error retrieving group membership information: <br /><br />RESPONSE:<br /><br /><pre>" . print_r ($response, TRUE) . "</pre>";
}
?>
</ul>
<?php
} else {
}
?>
<?php
if($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
// user is already connected
?>
<form id="linkedin_revoke_form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
<input type="hidden" name="<?php echo LINKEDIN::_GET_TYPE;?>" id="<?php echo LINKEDIN::_GET_TYPE;?>" value="revoke" />
<input type="submit" value="Revoke Authorization" />
</form>
<?php
$response = $OBJ_linkedin->profile('~:(id,first-name,last-name,picture-url)');
if($response['success'] === TRUE) {
$response['linkedin'] = new SimpleXMLElement($response['linkedin']);
echo "<pre>" . print_r($response['linkedin'], TRUE) . "</pre>";
} else {
// request failed
echo "Error retrieving profile information:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
}
} else {
// user isn't connected
?>
<form id="linkedin_connect_form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
<input type="hidden" name="<?php echo LINKEDIN::_GET_TYPE;?>" id="<?php echo LINKEDIN::_GET_TYPE;?>" value="initiate" />
<input type="submit" value="Connect to LinkedIn" />
</form>
<?php
}
?>
<?php
break;
}
} catch(LinkedInException $e) {
// exception raised by library call
echo $e->getMessage();
}
?>