i-lab-advisor /sites/all/modules/contrib/advisor/linkedin-php/login_with_linkedin.php

Language PHP Lines 365
MD5 Hash 25ddae694c5eea6260063b163d7b9a44 Estimated Cost $5,353 (why?)
Repository https://bitbucket.org/extensionengine/i-lab-advisor.git View Raw File
  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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/*
 * login_with_linkedin.php
 *
 * @(#) $Id: login_with_linkedin.php,v 1.1 2012/11/19 08:26:37 mlemos Exp $
 *
 */
	
	require('http-client/http.php');
	require('oauth_client.php');
	include_once('../includes/enable-ajax.inc.php');
	
	//session_start();
	//$_SESSION["ajax"]=112;
	
	$client = new oauth_client_class;
	//$client->ResetAccessToken();
	$client->debug = 1;
	$client->debug_http = 1;
	$client->server = 'LinkedIn';
	$client->redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].
		dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/login_with_linkedin.php';

	$client->client_id = '97n73ullhk71'; $application_line = __LINE__;
	$client->client_secret = 'janjXVBAz6OX0yzW';

	/*  API permission scopes
	 *  Separate scopes with a space, not with +
	 */
	$client->scope = 'r_fullprofile r_contactinfo r_emailaddress';

	if(strlen($client->client_id) == 0
	|| strlen($client->client_secret) == 0)
		die('Please go to LinkedIn Apps page https://www.linkedin.com/secure/developer?newapp= , '.
			'create an application, and in the line '.$application_line.
			' set the client_id to Consumer key and client_secret with Consumer secret. '.
			'The Callback URL must be '.$client->redirect_uri).' Make sure you enable the '.
			'necessary permissions to execute the API calls your application needs.';

	if(($success = $client->Initialize()))
	{
		if(($success = $client->Process()))
		{
			if(strlen($client->access_token))
			{
				$success = $client->CallAPI(
					'http://api.linkedin.com/v1/people/~:(id,first-name,last-name,public-profile-url,location:(name),industry,positions,educations,picture-url,patents:(title,summary,status),publications:(title,publisher,url,summary),skills:(skill,proficiency,years),certifications:(name,number,start-date,end-date),email-address,primary-twitter-account)', 
					'GET', array(
						'format'=>'json'
					), array('FailOnAccessError'=>true), $user);
			}
		}
		$success = $client->Finalize($success);
	}
	if($client->exit)
		exit;
	if(strlen($client->authorization_error))
	{
		$client->error = $client->authorization_error;
		$success = false;
	}
	if($success)
	{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>LinkedIn OAuth client results</title>
</head>
<body>
<?php
	
	$host="localhost"; // Host name 
	$username="root"; //Mysql username 
	$password="dingac708"; // Mysql password 
	$db_name="innovation_lab_drupal_production"; // Database name 
		
	// Connect to server and select database.
	mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
	mysql_select_db("$db_name")or die("cannot select DB");
		
	$linkedin_user_id = mysql_real_escape_string($user->id);
				 
	 //Check if user allready exist
	$user_tbl = "a_mentor_profile";
	$user_sql = "SELECT * FROM $user_tbl WHERE a_mentor_profile.linkedin_id = '$linkedin_user_id'";
	$user_result = mysql_query($user_sql);
	//echo $user_result; echo ("lalala");
	
	if(mysql_num_rows($user_result) > 0){
	$userData=mysql_fetch_array($user_result, MYSQL_ASSOC);
	$_SESSION['mentorId']=$userData["id"];
	$popupData->id = $userData["id"];
	$popupData->name = $userData["first_name"]; 
	$popupData->mentorStateId = $userData["advisor_state_id"];
?>
	<script type="text/javascript">
		window.opener.userExists(<?php echo json_encode($popupData); ?>); //Call callback function 
		window.close(); // Close the current popup
	</script>

<?php
	}
	else {
	
	
		
			//Need to add code to check if user is allready in database
			echo "unutar IFA SAM";
			
			$linkedin_id  = $user->id;
			$first_name = mysql_real_escape_string($user->firstName);
			$last_name = mysql_real_escape_string($user->lastName);
			if(isset($user->publicProfileUrl)) $public_profile_url = mysql_real_escape_string($user->publicProfileUrl); else $public_profile_url = "NULL";
			if(isset($user->location->name)) $location = mysql_real_escape_string($user->location->name); else $location = "NULL";
			if(isset($user->pictureUrl)) $profile_picture = mysql_real_escape_string($user->pictureUrl); else $profile_picture = "NULL";
			if(isset($user->emailAddress)) $email = mysql_real_escape_string($user->emailAddress); else $email = "NULL";
			if(isset($user->primaryTwitterAccount->providerAccountName)) $twitter = mysql_real_escape_string($user->primaryTwitterAccount->providerAccountName); else $twitter = "NULL";
			
			//Check if industry is allready stored and make appropriate actions
			//First check if industry is present in response
			if(isset($user->industry)) {
				 
				 $industry_api_name = mysql_real_escape_string($user->industry);
				 
				 //Check if industry is allready stored
				 $industry_tbl = "a_industry";
				 $industry_sql = "SELECT id FROM $industry_tbl WHERE a_industry.name = '$industry_api_name'";
				 $industry_result = mysql_query($industry_sql);
				 $industry_row = mysql_fetch_array( $industry_result );
				 
				 //if it's not make new entry in a_industry table else get id
				 if (!$industry_row){
					$ind_api_name = mysql_real_escape_string($user->industry);
					$industry_sql_insert = "INSERT INTO $industry_tbl (name) VALUES ('$ind_api_name')";
					$industry_insert_result = mysql_query($industry_sql_insert);
					$industry_result_id = mysql_insert_id();
				 } else {
					$industry_result_id = $industry_row['id'];
					}
			} else $industry_result_id = "NULL";
			
			$tbl_name="a_mentor_profile"; // Table name
			// Insert data into "a_mentor_profile" table 
			$sql="INSERT INTO $tbl_name(linkedin_id, industry_id, first_name, last_name, location_name, profile_picture, email, twitter, advisor_state_id,public_profile_url)VALUES('$linkedin_id', '$industry_result_id', '$first_name', '$last_name', '$location', '$profile_picture', '$email', '$twitter','2','$public_profile_url')";
			
			//check if everithing went OK
			mysql_query($sql)or die(mysql_error());
			
			//get latest added mentor id we'll need it later
			$current_mentor_id = mysql_insert_id();
			$_SESSION['mentorId']= $current_mentor_id;
			
			//Get data and fill a_position table
			
			if(isset($user->positions->values)){
				foreach($user->positions->values as $position){
					if(isset($position->title)) $position_title = mysql_real_escape_string($position->title); else $position_title = "NULL";
					if(isset($position->summary)) $position_summary = mysql_real_escape_string($position->summary); else $position_summary = "NULL";
					if(isset($position->startDate->month) and isset($position->startDate->year)){
					
						$date_string = $position->startDate->year . "-" . $position->startDate->month . "-" . "01";
						$otherStamp = strtotime($date_string);
						$position_start_date = date("Y-m-d", $otherStamp);
						
					} else $position_start_date = NULL;
					if(isset($position->endDate->month) and isset($position->endDate->year)){
					
						$date_string = $position->endDate->year . "-" . $position->endDate->month . "-" . "01";
						$otherStamp = strtotime($date_string);
						$position_end_date = date("Y-m-d", $otherStamp);
						
					} else $position_end_date = NULL;
					if(isset($position->isCurrent)) $position_iscurrent = $position->isCurrent; else $position_iscurrent = "NULL";
					if(isset($position->company->name)) $position_companyname = mysql_real_escape_string($position->company->name); else $position_companyname = "NULL";
					if(isset($position->company->size)) $position_size = mysql_real_escape_string($position->company->size); else $position_size = "NULL";
					
					//Check if industry is allready stored and make appropriate actions
					//First check if industry is present in response
					if(isset($position->company->industry)) {
						 
						 $industry_api_name = mysql_real_escape_string($position->company->industry);
						 
						 //Check if industry is allready stored
						 $industry_tbl = "a_industry";
						 $industry_sql = "SELECT id FROM $industry_tbl WHERE a_industry.name = '$industry_api_name'";
						 $industry_result = mysql_query($industry_sql);
						 $industry_row = mysql_fetch_array( $industry_result );
						 
						 //if it's not make new entry in a_industry table else get id
						 if (!$industry_row){
							$ind_api_name = mysql_real_escape_string($position->company->industry);
							$industry_sql_insert = "INSERT INTO $industry_tbl (name) VALUES ('$ind_api_name')";
							$industry_insert_result = mysql_query($industry_sql_insert);
							$industry_result_id = mysql_insert_id();
						 } else {
							$industry_result_id = $industry_row['id'];
							}
					} else $industry_result_id = "NULL";
					
					//Insert data into table
					$tbl_name = "a_position";
					$sql="INSERT INTO $tbl_name(mentor_id, industry_id, title, summary, company_name, start_date, end_date, size)VALUES('$current_mentor_id', '$industry_result_id', '$position_title', '$position_summary', '$position_companyname', '$position_start_date', '$position_end_date', '$position_size')";
					mysql_query($sql)or die(mysql_error());
				}
			}
			
			// Get data for a_education table and save it
			if(isset($user->educations->values)){
				foreach($user->educations->values as $education){
					if(isset($education->schoolName)) $education_schoolname = mysql_real_escape_string($education->schoolName); else $education_schoolname = "NULL";
					if(isset($education->degree)) $education_degree = mysql_real_escape_string($education->degree); else $education_degree = "NULL";
					if(isset($education->startDate->year)) $education_startdate = $education->startDate->year; else $education_startdate = "NULL";
					if(isset($education->endDate->year)) $education_enddate = $education->endDate->year; else $education_enddate = "NULL";

					//Insert data into table
					$tbl_name = "a_education";
					$sql="INSERT INTO $tbl_name(mentor_id, school_name, degree, start_date, end_date)VALUES('$current_mentor_id', '$education_schoolname', '$education_degree', '$education_startdate', '$education_enddate')";
					
					mysql_query($sql)or die(mysql_error());
					//$result=mysql_query($sql);
				}
			}
			
			// Get data for a_patent table and save it
			if(isset($user->patents->values)){
				foreach($user->patents->values as $patent){
					if(isset($patent->status->name)) $patent_status = mysql_real_escape_string($patent->status->name); else $patent_status = "NULL";
					if(isset($patent->title)) $patent_title = mysql_real_escape_string($patent->title); else $patent_title = "NULL";
					if(isset($patent->summary)) $patent_summary = mysql_real_escape_string($patent->summary); else $patent_summary = "NULL";
					
					//Insert data into table
					$tbl_name = "a_patent";
					$sql="INSERT INTO $tbl_name(mentor_id, title, status, summary)VALUES('$current_mentor_id', '$patent_title', '$patent_status', '$patent_summary')";
					
					mysql_query($sql)or die(mysql_error());
					
				}
			}
			
			// Get data for a_publication table and save it
			
			if(isset($user->publications->values)){
				foreach($user->publications->values as $publication){
					//if(isset($publication->publisher->name)) $publication_publisher = mysql_real_escape_string($publication->publisher->name); else $publication_publisher = "NULL";
					if(isset($publication->summary)) $publication_summary = mysql_real_escape_string($publication->summary); else $publication_sumamry = "NULL";
					if(isset($publication->title)) $publication_title = mysql_real_escape_string($publication->title); else $publication_title = "NULL";
					if(isset($publication->url)) $publication_url = mysql_real_escape_string($publication->url); else $publication_url = "NULL";
					
					//Insert data into table
					$tbl_name = "a_publication";
					$sql="INSERT INTO $tbl_name(mentor_id, title, url)VALUES('$current_mentor_id', '$publication_title', '$publication_url')";
					
					mysql_query($sql)or die(mysql_error());
				}
			}
			
			// Get data for a_mentor_skill table and save it
			
			if(isset($user->skills->values)){
				foreach($user->skills->values as $skill){
					
					//Check if skill is allready stored and make appropriate actions
					//First check if skill is present in response
					if(isset($skill->skill->name)) {
						 
						 $skill_name = mysql_real_escape_string($skill->skill->name);
						 
						 //Check if skill is allready stored
						 $skill_tbl = "a_skill";
						 $skill_sql = "SELECT id FROM $skill_tbl WHERE a_skill.name = '$skill_name'";
						 $skill_result = mysql_query($skill_sql);
						 $skill_row = mysql_fetch_array( $skill_result );
						 
						 //if it's not make new entry in a_skill table else get id
						 if (!$skill_row){
							$skill_api_name = mysql_real_escape_string($skill->skill->name);
							$skill_sql_insert = "INSERT INTO $skill_tbl (name) VALUES ('$skill_api_name')";
							$skill_insert_result = mysql_query($skill_sql_insert);
							$skill_result_id = mysql_insert_id();
						 } else {
							$skill_result_id = $skill_row['id'];
							}
						} else $skill_result_id = "NULL";
						
					if(isset($skill->years->name)) $skill_years = $skill->years->name; else $skill_years = "NULL";
					if(isset($skill->proficiency->name)) $skill_proficiency = mysql_real_escape_string($skill->proficiency->name); else $skill_proficiency = "NULL";
					
					//Insert data into table
					$tbl_name = "a_mentor_skill";
					$sql="INSERT INTO $tbl_name(mentor_id, skill_id, years, proficiency)VALUES('$current_mentor_id', '$skill_result_id', '$skill_years', '$skill_proficiency')";
					
					mysql_query($sql)or die(mysql_error());
				}
			}
			
			
			// Get data for a_certification table and save it
			
			if(isset($user->certifications->values)){
				foreach($user->certifications->values as $certification){
					
					if(isset($certification->name)) $certification_name = mysql_real_escape_string($certification->name); else $certification_name = "NULL";
					if(isset($certification->number)) $certification_number = mysql_real_escape_string($certification->number); else $certification_number = "NULL";
					if(isset($certification->startDate->month) and isset($certification->startDate->year)){
					
						$date_string = $certification->startDate->year . "-" . $certification->startDate->month . "-" . "01";
						$otherStamp = strtotime($date_string);
						$position_start_date = date("Y-m-d", $otherStamp);
						
					} else $position_start_date = "NULL";
					if(isset($certification->endDate->month) and isset($certification->endDate->year)){
					
						$date_string = $certification->endDate->year . "-" . $certification->endDate->month . "-" . "01";
						$otherStamp = strtotime($date_string);
						$position_end_date = date("Y-m-d", $otherStamp);
						
					} else $position_end_date = "NULL";
					
					//Insert data into table
					$tbl_name = "a_certification";
					$sql="INSERT INTO $tbl_name(mentor_id, name, licence_number, start_date, end_date)VALUES('$current_mentor_id', '$certification_name', '$certification_number', '$position_start_date', '$position_end_date')";
					
					mysql_query($sql)or die(mysql_error());
				}
			}
			$popupData->id=$current_mentor_id;
			$popupData->name=$first_name;
?>
<script type="text/javascript">
		window.opener.popupCallback(<?php echo json_encode($popupData);?>); //Call callback function 
		window.close(); // Close the current popup
	</script>

<?php
		}
		
		echo '<h1>', HtmlSpecialChars($user->firstName), 
			' you have logged in successfully with LinkedIn!</h1>';
		echo '<pre>', HtmlSpecialChars(print_r($user, 1)), '</pre>';
		//echo '<pre>', HtmlSpecialChars($user->primary-twitter-account->provider-account-name),'</pre>';
	
?>
</body>
</html>
<?php
	}
	else
	{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>OAuth client error</title>
</head>
<body>
<h1>OAuth client error</h1>
<p>Error: <?php echo HtmlSpecialChars($client->error); ?></p>
</body>
</html>
<?php
	}

?>
Back to Top