/sites/all/modules/contrib/advisor/linkedin-php/login_with_linkedin.php
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
- <?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
- }
- ?>