Advertisement
Guest User

Untitled

a guest
Jun 21st, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.95 KB | None | 0 0
  1. function oauth_session_exists() {
  2.   if((is_array($_SESSION)) && (array_key_exists('oauth', $_SESSION))) {
  3.     return TRUE;
  4.   } else {
  5.     return FALSE;
  6.   }
  7. }
  8.  
  9. try {
  10.   // include the LinkedIn class
  11.   require_once('linkedin_3.2.0.class.php');
  12.  
  13.   // start the session
  14.   if(!session_start()) {
  15.     throw new LinkedInException('This script requires session support, which appears to be disabled according to session_start().');
  16.   }
  17.  
  18.   // display constants
  19.   $API_CONFIG = array(
  20.     'appKey'       => 'XXXXXXXXXXXX',
  21.       'appSecret'    => 'XXXXXXXXXXXXXXX',
  22.       'callbackUrl'  => NULL
  23.   );
  24.   define('DEMO_GROUP', 'Gautam');
  25.   define('DEMO_GROUP_NAME', '3');
  26.   define('PORT_HTTP', '80');
  27.   define('PORT_HTTP_SSL', '443');
  28.  
  29.   // set index
  30.   $_REQUEST[LINKEDIN::_GET_TYPE] = (isset($_REQUEST[LINKEDIN::_GET_TYPE])) ? $_REQUEST[LINKEDIN::_GET_TYPE] : '';
  31.   switch($_REQUEST[LINKEDIN::_GET_TYPE]) {
  32.     case 'initiate':
  33.       /**
  34.        * Handle user initiated LinkedIn connection, create the LinkedIn object.
  35.        */
  36.        
  37.       // check for the correct http protocol (i.e. is this script being served via http or https)
  38.       if($_SERVER['HTTPS'] == 'on') {
  39.         $protocol = 'https';
  40.       } else {
  41.  
  42.  
  43.         $protocol = 'http';
  44.       }
  45.      
  46.       // set the callback url
  47.       $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';
  48.       $OBJ_linkedin = new LinkedIn($API_CONFIG);
  49.      
  50.       // check for response from LinkedIn
  51.       $_GET[LINKEDIN::_GET_RESPONSE] = (isset($_GET[LINKEDIN::_GET_RESPONSE])) ? $_GET[LINKEDIN::_GET_RESPONSE] : '';
  52.       if(!$_GET[LINKEDIN::_GET_RESPONSE]) {
  53.         // LinkedIn hasn't sent us a response, the user is initiating the connection
  54.        
  55.         // send a request for a LinkedIn access token
  56.         $response = $OBJ_linkedin->retrieveTokenRequest();
  57.         if($response['success'] === TRUE) {
  58.           // store the request token
  59.           $_SESSION['oauth']['linkedin']['request'] = $response['linkedin'];
  60.          
  61.           // redirect the user to the LinkedIn authentication/authorisation page to initiate validation.
  62.           header('Location: ' . LINKEDIN::_URL_AUTH . $response['linkedin']['oauth_token']);
  63.         } else {
  64.           // bad token request
  65.           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>";
  66.         }
  67.       } else {
  68.         // 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
  69.         $response = $OBJ_linkedin->retrieveTokenAccess($_SESSION['oauth']['linkedin']['request']['oauth_token'], $_SESSION['oauth']['linkedin']['request']['oauth_token_secret'], $_GET['oauth_verifier']);
  70.         if($response['success'] === TRUE) {
  71.           // the request went through without an error, gather user's 'access' tokens
  72.           $_SESSION['oauth']['linkedin']['access'] = $response['linkedin'];
  73.          
  74.           // set the user as authorized for future quick reference
  75.           $_SESSION['oauth']['linkedin']['authorized'] = TRUE;
  76.            
  77.           // redirect the user back to the demo page
  78.           header('Location: ' . $_SERVER['PHP_SELF']);
  79.         } else {
  80.           // bad token access
  81.           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>";
  82.         }
  83.       }
  84.       break;
  85.  
  86.     case 'revoke':
  87.       /**
  88.        * Handle authorization revocation.
  89.        */
  90.                    
  91.       // check the session
  92.       if(!oauth_session_exists()) {
  93.         throw new LinkedInException('This script requires session support, which doesn\'t appear to be working correctly.');
  94.       }
  95.      
  96.       $OBJ_linkedin = new LinkedIn($API_CONFIG);
  97.       $OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
  98.       $response = $OBJ_linkedin->revoke();
  99.       if($response['success'] === TRUE) {
  100.         // revocation successful, clear session
  101.         session_unset();
  102.         $_SESSION = array();
  103.         if(session_destroy()) {
  104.           // session destroyed
  105.           header('Location: ' . $_SERVER['PHP_SELF']);
  106.         } else {
  107.           // session not destroyed
  108.           echo "Error clearing user's session";
  109.         }
  110.       } else {
  111.         // revocation failed
  112.         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>";
  113.       }
  114.       break;
  115.     default:
  116.       // nothing being passed back, display demo page
  117.      
  118.       // check PHP version
  119.       if(version_compare(PHP_VERSION, '5.0.0', '<')) {
  120.         throw new LinkedInException('You must be running version 5.x or greater of PHP to use this library.');
  121.       }
  122.      
  123.       // check for cURL
  124.       if(extension_loaded('curl')) {
  125.         $curl_version = curl_version();
  126.         $curl_version = $curl_version['version'];
  127.       } else {
  128.         throw new LinkedInException('You must load the cURL extension to use this library.');
  129.       }
  130.       ?>
  131.       <!DOCTYPE html>
  132.       <html lang="en">
  133.         <head>
  134.           <title>Simple-LinkedIn Demo</title>
  135.          
  136.           <meta charset="utf-8" />
  137.           <meta name="viewport" content="width=device-width" />
  138.           <meta name="description" content="A demonstration page for the Simple-LinkedIn PHP class." />
  139.           <meta name="keywords" content="simple-linkedin,php,linkedin,api,class,library" />
  140.          
  141.           <style>
  142.             body {font-family: Courier, monospace; font-size: 0.8em;}
  143.             footer {margin-top: 2em; text-align: center;}
  144.             pre {font-family: Courier, monospace; font-size: 0.8em;}
  145.           </style>
  146.         </head>
  147.         <body>
  148.           <h1><a href="<?php echo $_SERVER['PHP_SELF'];?>">Simple-LinkedIn Demo</a></h1>
  149.          
  150.           <p>Copyright 2010 - 2011, Paul Mennega, fiftyMission Inc. &lt;paul@fiftymission.net&gt;</p>
  151.          
  152.           <p>Released under the MIT License - http://www.opensource.org/licenses/mit-license.php</p>
  153.          
  154.           <p>Full source code for both the Simple-LinkedIn class and this demo script can be found at:</p>
  155.          
  156.           <ul>
  157.             <li><a href="http://code.google.com/p/simple-linkedinphp/">http://code.google.com/p/simple-linkedinphp/</a></li>
  158.           </ul>          
  159.  
  160.           <hr />
  161.          
  162.           <p style="font-weight: bold;">Demo using: Simple-LinkedIn v<?php echo LINKEDIN::_VERSION;?>, cURL v<?php echo $curl_version;?>, PHP v<?php echo phpversion();?></p>
  163.          
  164.           <ul>
  165.             <li>Please note: The Simple-LinkedIn class requires PHP 5+</li>
  166.           </ul>
  167.          
  168.           <hr />
  169.          
  170.           <?php
  171.           $_SESSION['oauth']['linkedin']['authorized'] = (isset($_SESSION['oauth']['linkedin']['authorized'])) ? $_SESSION['oauth']['linkedin']['authorized'] : FALSE;
  172.           if($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
  173.             $OBJ_linkedin = new LinkedIn($API_CONFIG);
  174.             $OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
  175.             $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_XML);
  176.             ?>
  177.             <ul>
  178.               <li><a href="#manage">Manage LinkedIn Authorization</a></li>
  179.               <li><a href="#application">Application Information</a></li>
  180.               <li><a href="#profile">Your Profile</a></li>
  181.               <li><a href="demo/network.php">Your Network</a>
  182.                 <ul>
  183.                   <li><a href="demo/network.php#networkStats">Stats</a></li>
  184.                   <li><a href="demo/network.php#networkConnections">Your Connections</a></li>
  185.                   <li><a href="demo/network.php#networkInvite">Invite Others to Join your LinkedIn Network</a></li>
  186.                   <li><a href="demo/network.php#networkUpdates">Recent Connection Updates</a></li>
  187.                   <li><a href="demo/network.php#peopleSearch">People Search</a></li>
  188.                 </ul>
  189.               </li>
  190.               <li><a href="demo/company.php">Company API</a>
  191.                 <ul>
  192.                   <li><a href="demo/company.php#companySpecific">Specific Company</a></li>
  193.                   <li><a href="demo/company.php#companyFollowed">Followed Companies</a></li>
  194.                   <li><a href="demo/company.php#companySuggested">Suggested Companies</a></li>
  195.                   <li><a href="demo/company.php#companySearch">Company Search</a></li>
  196.                 </ul>
  197.               </li>
  198.               <li><a href="demo/jobs.php">Jobs API</a>
  199.                 <ul>
  200.                   <li><a href="demo/jobs.php#jobsBookmarked">Bookmarked Jobs</a></li>
  201.                   <li><a href="demo/jobs.php#jobsSuggested">Suggested Jobs</a></li>
  202.                   <li><a href="demo/jobs.php#jobsSearch">Jobs Search</a></li>
  203.                 </ul>
  204.               </li>
  205.               <li><a href="demo/content.php">Creating / Sharing Content</a>
  206.                 <ul>
  207.                   <li><a href="demo/content.php#contentUpdate">Post Network Update</a></li>
  208.                   <li><a href="demo/content.php#contentShare">Post Share</a></li>
  209.                 </ul>
  210.               </li>
  211.               <?php
  212.                
  213.                 // check if the viewer is a member of the test group
  214.                 $response = $OBJ_linkedin->group(DEMO_GROUP, ':(relation-to-viewer:(membership-state))');
  215.               if($response['success'] === TRUE) {
  216.                   $result         = new SimpleXMLElement($response['linkedin']);
  217.                   $membership     = $result->{'relation-to-viewer'}->{'membership-state'}->code;
  218.                   $in_demo_group  = (($membership == 'non-member') || ($membership == 'blocked')) ? FALSE : TRUE;
  219.                   ?>
  220.                     <li><a href="demo/groups.php">Groups API</a>
  221.                     <ul>
  222.                       <li><a href="demo/groups.php#groupsSuggested">Suggested Groups</a></li>
  223.                       <li><a href="demo/groups.php#groupMemberships">Group Memberships</a></li>
  224.                       <li><a href="demo/groups.php#manageGroup">Manage '<?php echo DEMO_GROUP_NAME;?>' Group Membership</a></li>
  225.                       <?php
  226.                       if($in_demo_group) {
  227.                         ?>
  228.                           <li><a href="demo/groups.php#groupSettings">Group Settings</a></li>
  229.                           <li><a href="demo/groups.php#groupPosts">Group Posts</a></li>
  230.                           <li><a href="demo/groups.php#createPost">Create a Group Post</a></li>
  231.                             <?php
  232.                         }
  233.                         ?>
  234.                       </ul>
  235.                     </li>
  236.                     <?php
  237.                       } else {
  238.                         // request failed
  239.                     echo "Error retrieving group membership information: <br /><br />RESPONSE:<br /><br /><pre>" . print_r ($response, TRUE) . "</pre>";
  240.                       }
  241.                   ?>
  242.             </ul>
  243.             <?php
  244.           } else {
  245.             ?>
  246.             <ul>
  247.               <li><a href="#manage">Manage LinkedIn Authorization</a></li>
  248.             </ul>
  249.             <?php
  250.           }
  251.           ?>
  252.          
  253.           <hr />
  254.          
  255.           <h2 id="manage">Manage LinkedIn Authorization:</h2>
  256.           <?php
  257.           if($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
  258.             // user is already connected
  259.             ?>
  260.             <form id="linkedin_revoke_form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
  261.               <input type="hidden" name="<?php echo LINKEDIN::_GET_TYPE;?>" id="<?php echo LINKEDIN::_GET_TYPE;?>" value="revoke" />
  262.               <input type="submit" value="Revoke Authorization" />
  263.             </form>
  264.            
  265.             <hr />
  266.          
  267.             <h2 id="application">Application Information:</h2>
  268.            
  269.             <ul>
  270.               <li>Application Key:
  271.                 <ul>
  272.                   <li><?php echo $OBJ_linkedin->getApplicationKey();?></li>
  273.                 </ul>
  274.               </li>
  275.             </ul>
  276.            
  277.             <hr />
  278.            
  279.             <h2 id="profile">Your Profile:</h2>
  280.            
  281.             <?php
  282.             $response = $OBJ_linkedin->profile('~:(id,first-name,last-name,picture-url)');
  283.             if($response['success'] === TRUE) {
  284.               $response['linkedin'] = new SimpleXMLElement($response['linkedin']);
  285.               echo "<pre>" . print_r($response['linkedin'], TRUE) . "</pre>";
  286.             } else {
  287.               // request failed
  288.               echo "Error retrieving profile information:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  289.             }
  290.           } else {
  291.             // user isn't connected
  292.             ?>
  293.             <form id="linkedin_connect_form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
  294.               <input type="hidden" name="<?php echo LINKEDIN::_GET_TYPE;?>" id="<?php echo LINKEDIN::_GET_TYPE;?>" value="initiate" />
  295.               <input type="submit" value="Connect to LinkedIn" />
  296.             </form>
  297.             <?php
  298.           }
  299.           ?>
  300.           <footer>
  301.             <div>Copyright 2010 - 2011, fiftyMission Inc. (Paul Mennega &lt;<a href="mailto:paul@fiftymission.net">paul@fiftymission.net</a>&gt;)</div>
  302.             <div>Released under the MIT License - <a href="http://www.opensource.org/licenses/mit-license.php">http://www.opensource.org/licenses/mit-license.php</a></div>
  303.           </footer>
  304.         </body>
  305.       </html>
  306.       <?php
  307.       break;
  308.   }
  309. } catch(LinkedInException $e) {
  310.   // exception raised by library call
  311.   echo $e->getMessage();
  312. }
  313. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement