PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/extensionengine/i-lab-advisor
PHP | 364 lines | 249 code | 69 blank | 46 comment | 86 complexity | 25ddae694c5eea6260063b163d7b9a44 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, AGPL-1.0, MIT
  1. <?php
  2. /*
  3. * login_with_linkedin.php
  4. *
  5. * @(#) $Id: login_with_linkedin.php,v 1.1 2012/11/19 08:26:37 mlemos Exp $
  6. *
  7. */
  8. require('http-client/http.php');
  9. require('oauth_client.php');
  10. include_once('../includes/enable-ajax.inc.php');
  11. //session_start();
  12. //$_SESSION["ajax"]=112;
  13. $client = new oauth_client_class;
  14. //$client->ResetAccessToken();
  15. $client->debug = 1;
  16. $client->debug_http = 1;
  17. $client->server = 'LinkedIn';
  18. $client->redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].
  19. dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/login_with_linkedin.php';
  20. $client->client_id = '97n73ullhk71'; $application_line = __LINE__;
  21. $client->client_secret = 'janjXVBAz6OX0yzW';
  22. /* API permission scopes
  23. * Separate scopes with a space, not with +
  24. */
  25. $client->scope = 'r_fullprofile r_contactinfo r_emailaddress';
  26. if(strlen($client->client_id) == 0
  27. || strlen($client->client_secret) == 0)
  28. die('Please go to LinkedIn Apps page https://www.linkedin.com/secure/developer?newapp= , '.
  29. 'create an application, and in the line '.$application_line.
  30. ' set the client_id to Consumer key and client_secret with Consumer secret. '.
  31. 'The Callback URL must be '.$client->redirect_uri).' Make sure you enable the '.
  32. 'necessary permissions to execute the API calls your application needs.';
  33. if(($success = $client->Initialize()))
  34. {
  35. if(($success = $client->Process()))
  36. {
  37. if(strlen($client->access_token))
  38. {
  39. $success = $client->CallAPI(
  40. '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)',
  41. 'GET', array(
  42. 'format'=>'json'
  43. ), array('FailOnAccessError'=>true), $user);
  44. }
  45. }
  46. $success = $client->Finalize($success);
  47. }
  48. if($client->exit)
  49. exit;
  50. if(strlen($client->authorization_error))
  51. {
  52. $client->error = $client->authorization_error;
  53. $success = false;
  54. }
  55. if($success)
  56. {
  57. ?>
  58. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  59. <html>
  60. <head>
  61. <title>LinkedIn OAuth client results</title>
  62. </head>
  63. <body>
  64. <?php
  65. $host="localhost"; // Host name
  66. $username="root"; //Mysql username
  67. $password="dingac708"; // Mysql password
  68. $db_name="innovation_lab_drupal_production"; // Database name
  69. // Connect to server and select database.
  70. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  71. mysql_select_db("$db_name")or die("cannot select DB");
  72. $linkedin_user_id = mysql_real_escape_string($user->id);
  73. //Check if user allready exist
  74. $user_tbl = "a_mentor_profile";
  75. $user_sql = "SELECT * FROM $user_tbl WHERE a_mentor_profile.linkedin_id = '$linkedin_user_id'";
  76. $user_result = mysql_query($user_sql);
  77. //echo $user_result; echo ("lalala");
  78. if(mysql_num_rows($user_result) > 0){
  79. $userData=mysql_fetch_array($user_result, MYSQL_ASSOC);
  80. $_SESSION['mentorId']=$userData["id"];
  81. $popupData->id = $userData["id"];
  82. $popupData->name = $userData["first_name"];
  83. $popupData->mentorStateId = $userData["advisor_state_id"];
  84. ?>
  85. <script type="text/javascript">
  86. window.opener.userExists(<?php echo json_encode($popupData); ?>); //Call callback function
  87. window.close(); // Close the current popup
  88. </script>
  89. <?php
  90. }
  91. else {
  92. //Need to add code to check if user is allready in database
  93. echo "unutar IFA SAM";
  94. $linkedin_id = $user->id;
  95. $first_name = mysql_real_escape_string($user->firstName);
  96. $last_name = mysql_real_escape_string($user->lastName);
  97. if(isset($user->publicProfileUrl)) $public_profile_url = mysql_real_escape_string($user->publicProfileUrl); else $public_profile_url = "NULL";
  98. if(isset($user->location->name)) $location = mysql_real_escape_string($user->location->name); else $location = "NULL";
  99. if(isset($user->pictureUrl)) $profile_picture = mysql_real_escape_string($user->pictureUrl); else $profile_picture = "NULL";
  100. if(isset($user->emailAddress)) $email = mysql_real_escape_string($user->emailAddress); else $email = "NULL";
  101. if(isset($user->primaryTwitterAccount->providerAccountName)) $twitter = mysql_real_escape_string($user->primaryTwitterAccount->providerAccountName); else $twitter = "NULL";
  102. //Check if industry is allready stored and make appropriate actions
  103. //First check if industry is present in response
  104. if(isset($user->industry)) {
  105. $industry_api_name = mysql_real_escape_string($user->industry);
  106. //Check if industry is allready stored
  107. $industry_tbl = "a_industry";
  108. $industry_sql = "SELECT id FROM $industry_tbl WHERE a_industry.name = '$industry_api_name'";
  109. $industry_result = mysql_query($industry_sql);
  110. $industry_row = mysql_fetch_array( $industry_result );
  111. //if it's not make new entry in a_industry table else get id
  112. if (!$industry_row){
  113. $ind_api_name = mysql_real_escape_string($user->industry);
  114. $industry_sql_insert = "INSERT INTO $industry_tbl (name) VALUES ('$ind_api_name')";
  115. $industry_insert_result = mysql_query($industry_sql_insert);
  116. $industry_result_id = mysql_insert_id();
  117. } else {
  118. $industry_result_id = $industry_row['id'];
  119. }
  120. } else $industry_result_id = "NULL";
  121. $tbl_name="a_mentor_profile"; // Table name
  122. // Insert data into "a_mentor_profile" table
  123. $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')";
  124. //check if everithing went OK
  125. mysql_query($sql)or die(mysql_error());
  126. //get latest added mentor id we'll need it later
  127. $current_mentor_id = mysql_insert_id();
  128. $_SESSION['mentorId']= $current_mentor_id;
  129. //Get data and fill a_position table
  130. if(isset($user->positions->values)){
  131. foreach($user->positions->values as $position){
  132. if(isset($position->title)) $position_title = mysql_real_escape_string($position->title); else $position_title = "NULL";
  133. if(isset($position->summary)) $position_summary = mysql_real_escape_string($position->summary); else $position_summary = "NULL";
  134. if(isset($position->startDate->month) and isset($position->startDate->year)){
  135. $date_string = $position->startDate->year . "-" . $position->startDate->month . "-" . "01";
  136. $otherStamp = strtotime($date_string);
  137. $position_start_date = date("Y-m-d", $otherStamp);
  138. } else $position_start_date = NULL;
  139. if(isset($position->endDate->month) and isset($position->endDate->year)){
  140. $date_string = $position->endDate->year . "-" . $position->endDate->month . "-" . "01";
  141. $otherStamp = strtotime($date_string);
  142. $position_end_date = date("Y-m-d", $otherStamp);
  143. } else $position_end_date = NULL;
  144. if(isset($position->isCurrent)) $position_iscurrent = $position->isCurrent; else $position_iscurrent = "NULL";
  145. if(isset($position->company->name)) $position_companyname = mysql_real_escape_string($position->company->name); else $position_companyname = "NULL";
  146. if(isset($position->company->size)) $position_size = mysql_real_escape_string($position->company->size); else $position_size = "NULL";
  147. //Check if industry is allready stored and make appropriate actions
  148. //First check if industry is present in response
  149. if(isset($position->company->industry)) {
  150. $industry_api_name = mysql_real_escape_string($position->company->industry);
  151. //Check if industry is allready stored
  152. $industry_tbl = "a_industry";
  153. $industry_sql = "SELECT id FROM $industry_tbl WHERE a_industry.name = '$industry_api_name'";
  154. $industry_result = mysql_query($industry_sql);
  155. $industry_row = mysql_fetch_array( $industry_result );
  156. //if it's not make new entry in a_industry table else get id
  157. if (!$industry_row){
  158. $ind_api_name = mysql_real_escape_string($position->company->industry);
  159. $industry_sql_insert = "INSERT INTO $industry_tbl (name) VALUES ('$ind_api_name')";
  160. $industry_insert_result = mysql_query($industry_sql_insert);
  161. $industry_result_id = mysql_insert_id();
  162. } else {
  163. $industry_result_id = $industry_row['id'];
  164. }
  165. } else $industry_result_id = "NULL";
  166. //Insert data into table
  167. $tbl_name = "a_position";
  168. $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')";
  169. mysql_query($sql)or die(mysql_error());
  170. }
  171. }
  172. // Get data for a_education table and save it
  173. if(isset($user->educations->values)){
  174. foreach($user->educations->values as $education){
  175. if(isset($education->schoolName)) $education_schoolname = mysql_real_escape_string($education->schoolName); else $education_schoolname = "NULL";
  176. if(isset($education->degree)) $education_degree = mysql_real_escape_string($education->degree); else $education_degree = "NULL";
  177. if(isset($education->startDate->year)) $education_startdate = $education->startDate->year; else $education_startdate = "NULL";
  178. if(isset($education->endDate->year)) $education_enddate = $education->endDate->year; else $education_enddate = "NULL";
  179. //Insert data into table
  180. $tbl_name = "a_education";
  181. $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')";
  182. mysql_query($sql)or die(mysql_error());
  183. //$result=mysql_query($sql);
  184. }
  185. }
  186. // Get data for a_patent table and save it
  187. if(isset($user->patents->values)){
  188. foreach($user->patents->values as $patent){
  189. if(isset($patent->status->name)) $patent_status = mysql_real_escape_string($patent->status->name); else $patent_status = "NULL";
  190. if(isset($patent->title)) $patent_title = mysql_real_escape_string($patent->title); else $patent_title = "NULL";
  191. if(isset($patent->summary)) $patent_summary = mysql_real_escape_string($patent->summary); else $patent_summary = "NULL";
  192. //Insert data into table
  193. $tbl_name = "a_patent";
  194. $sql="INSERT INTO $tbl_name(mentor_id, title, status, summary)VALUES('$current_mentor_id', '$patent_title', '$patent_status', '$patent_summary')";
  195. mysql_query($sql)or die(mysql_error());
  196. }
  197. }
  198. // Get data for a_publication table and save it
  199. if(isset($user->publications->values)){
  200. foreach($user->publications->values as $publication){
  201. //if(isset($publication->publisher->name)) $publication_publisher = mysql_real_escape_string($publication->publisher->name); else $publication_publisher = "NULL";
  202. if(isset($publication->summary)) $publication_summary = mysql_real_escape_string($publication->summary); else $publication_sumamry = "NULL";
  203. if(isset($publication->title)) $publication_title = mysql_real_escape_string($publication->title); else $publication_title = "NULL";
  204. if(isset($publication->url)) $publication_url = mysql_real_escape_string($publication->url); else $publication_url = "NULL";
  205. //Insert data into table
  206. $tbl_name = "a_publication";
  207. $sql="INSERT INTO $tbl_name(mentor_id, title, url)VALUES('$current_mentor_id', '$publication_title', '$publication_url')";
  208. mysql_query($sql)or die(mysql_error());
  209. }
  210. }
  211. // Get data for a_mentor_skill table and save it
  212. if(isset($user->skills->values)){
  213. foreach($user->skills->values as $skill){
  214. //Check if skill is allready stored and make appropriate actions
  215. //First check if skill is present in response
  216. if(isset($skill->skill->name)) {
  217. $skill_name = mysql_real_escape_string($skill->skill->name);
  218. //Check if skill is allready stored
  219. $skill_tbl = "a_skill";
  220. $skill_sql = "SELECT id FROM $skill_tbl WHERE a_skill.name = '$skill_name'";
  221. $skill_result = mysql_query($skill_sql);
  222. $skill_row = mysql_fetch_array( $skill_result );
  223. //if it's not make new entry in a_skill table else get id
  224. if (!$skill_row){
  225. $skill_api_name = mysql_real_escape_string($skill->skill->name);
  226. $skill_sql_insert = "INSERT INTO $skill_tbl (name) VALUES ('$skill_api_name')";
  227. $skill_insert_result = mysql_query($skill_sql_insert);
  228. $skill_result_id = mysql_insert_id();
  229. } else {
  230. $skill_result_id = $skill_row['id'];
  231. }
  232. } else $skill_result_id = "NULL";
  233. if(isset($skill->years->name)) $skill_years = $skill->years->name; else $skill_years = "NULL";
  234. if(isset($skill->proficiency->name)) $skill_proficiency = mysql_real_escape_string($skill->proficiency->name); else $skill_proficiency = "NULL";
  235. //Insert data into table
  236. $tbl_name = "a_mentor_skill";
  237. $sql="INSERT INTO $tbl_name(mentor_id, skill_id, years, proficiency)VALUES('$current_mentor_id', '$skill_result_id', '$skill_years', '$skill_proficiency')";
  238. mysql_query($sql)or die(mysql_error());
  239. }
  240. }
  241. // Get data for a_certification table and save it
  242. if(isset($user->certifications->values)){
  243. foreach($user->certifications->values as $certification){
  244. if(isset($certification->name)) $certification_name = mysql_real_escape_string($certification->name); else $certification_name = "NULL";
  245. if(isset($certification->number)) $certification_number = mysql_real_escape_string($certification->number); else $certification_number = "NULL";
  246. if(isset($certification->startDate->month) and isset($certification->startDate->year)){
  247. $date_string = $certification->startDate->year . "-" . $certification->startDate->month . "-" . "01";
  248. $otherStamp = strtotime($date_string);
  249. $position_start_date = date("Y-m-d", $otherStamp);
  250. } else $position_start_date = "NULL";
  251. if(isset($certification->endDate->month) and isset($certification->endDate->year)){
  252. $date_string = $certification->endDate->year . "-" . $certification->endDate->month . "-" . "01";
  253. $otherStamp = strtotime($date_string);
  254. $position_end_date = date("Y-m-d", $otherStamp);
  255. } else $position_end_date = "NULL";
  256. //Insert data into table
  257. $tbl_name = "a_certification";
  258. $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')";
  259. mysql_query($sql)or die(mysql_error());
  260. }
  261. }
  262. $popupData->id=$current_mentor_id;
  263. $popupData->name=$first_name;
  264. ?>
  265. <script type="text/javascript">
  266. window.opener.popupCallback(<?php echo json_encode($popupData);?>); //Call callback function
  267. window.close(); // Close the current popup
  268. </script>
  269. <?php
  270. }
  271. echo '<h1>', HtmlSpecialChars($user->firstName),
  272. ' you have logged in successfully with LinkedIn!</h1>';
  273. echo '<pre>', HtmlSpecialChars(print_r($user, 1)), '</pre>';
  274. //echo '<pre>', HtmlSpecialChars($user->primary-twitter-account->provider-account-name),'</pre>';
  275. ?>
  276. </body>
  277. </html>
  278. <?php
  279. }
  280. else
  281. {
  282. ?>
  283. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  284. <html>
  285. <head>
  286. <title>OAuth client error</title>
  287. </head>
  288. <body>
  289. <h1>OAuth client error</h1>
  290. <p>Error: <?php echo HtmlSpecialChars($client->error); ?></p>
  291. </body>
  292. </html>
  293. <?php
  294. }
  295. ?>