<?php

require_once "linkedin.class.php";
$linkedin = new linkedin();
$linkedin->init();
# not needed for public profile call.
$linkedin->get_public_profile_by_public_url('http://www.linkedin.com/in/nileshgamit');
            ?>
          <ul>
            <li><a href="#manage">Manage LinkedIn Authorization</a></li>
          </ul>
          <?php 
        }
        ?>
        
        <hr />
        
        <h2 id="manage">Manage LinkedIn Authorization:</h2>
        <?php 
        if ($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
            // user is already connected
            try {
                $OBJ_linkedin = new linkedin();
                $OBJ_linkedin->set_token_access($_SESSION['oauth']['linkedin']['access']);
                ?>
            <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>
            } else {
                ?>
            <ul>
              <li><a href="#manage">Manage LinkedIn Authorization</a></li>
            </ul>
            <?php 
            }
            ?>
          
          <hr />
          
          <h2 id="manage">Manage LinkedIn Authorization:</h2>
          <?php 
            if ($_SESSION['oauth']['linkedin']['authorized'] === TRUE) {
                // user is already connected
                $OBJ_linkedin = new linkedin();
                if ($OBJ_linkedin->set_token_access($_SESSION['oauth']['linkedin']['access'])) {
                    ?>
              <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>
    } else {
        // CSRF attack? Or did you mix up your states?
        exit;
    }
} else {
    if (empty($_SESSION['expires_at']) || time() > $_SESSION['expires_at']) {
        // Token has expired, clear the state
        $_SESSION = array();
    }
    if (empty($_SESSION['access_token'])) {
        // Start authorization process
        getAuthorizationCode();
    }
}
require_once "../linkedin/connect.php";
$linkedin = new linkedin(API_KEY, API_SECRET, SCOPE);
$linkedin->set($_SESSION['access_token'], $_SESSION['expires_in'], $_SESSION['expires_at'], $_SESSION['state']);
$linkedin->save();
// Congratulations! You have a valid token. Now fetch your profile
$user = fetch('GET', '/v1/people/~:(firstName,lastName)');
print "Hello {$user->firstName} {$user->lastName}.";
print "<pre>";
print "Access token " . $_SESSION['access_token'];
print "</pre>";
exit;
function getAuthorizationCode()
{
    $params = array('response_type' => 'code', 'client_id' => API_KEY, 'scope' => SCOPE, 'state' => uniqid('', true), 'redirect_uri' => REDIRECT_URI);
    // Authentication request
    $url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
    // Needed to identify request when it returns to us
function fetch_linkedin($url)
{
    global $current_file;
    global $source_machine;
    global $spell_checked;
    global $source_id, $fetchFromOutside, $newResult, $lastResult;
    $title = substr($url, 26);
    $title = str_replace('/', "", $title);
    $query = "select * from data_linked where title = '" . mysql_real_escape_string($title) . "'";
    $result = mysql_query($query) or trigger_error(mysql_error() . " in {$query}", E_USER_ERROR);
    if (mysql_num_rows($result) && !$fetchFromOutside) {
        $row = mysql_fetch_array($result);
        $source_id = $row['id'];
        $linked_return = $row['ans'];
    } else {
        require_once 'class.linkedin.php';
        $out = new linkedin($url);
        $linked_return = $out->getabout();
        if ($linked_return) {
            if (mysql_num_rows($result)) {
                $source_id = $row['id'];
                $str = substr($linked_return, 0, 500);
                $newResult = str2hex($str);
                if ($newResult != $lastResult) {
                    $query = "update data_linked set ans='" . mysql_real_escape_string($linked_return) . "' where id=" . $source_id;
                    mysql_query($query) or trigger_error(mysql_error() . " in {$query}", E_USER_ERROR);
                }
            } else {
                $query = "insert into data_linked(title,ans) values('" . mysql_real_escape_string($title) . "','" . mysql_real_escape_string($linked_return) . "')";
                mysql_query($query) or trigger_error(mysql_error() . " in {$query}", E_USER_ERROR);
                $source_id = mysql_insert_id();
            }
        }
    }
    $current_file = "data_linked/ans/id/{$source_id}";
    $source_machine = "db";
    return $linked_return;
}