/**
 * configures and initializes the osapi object
 *
 * @return osapi object
 */
function initIWIWConnect($consumerKey, $consumerSecret, $iwiwBaseURL = "http://iwiw.hu", $iwiwBaseApiURL = "http://api.iwiw.hu")
{
    // Log
    osapiLogger::setLevel(osapiLogger::INFO);
    osapiLogger::setAppender(new osapiFileAppender("/tmp/logs/osapi.log"));
    // Create an identifier for the local user's session
    session_start();
    $localUserId = session_id();
    //The persistent storage
    $storage = new osapiFileStorage('/tmp/osapistorage');
    //$storage = new osapiMemcacheStorage('127.0.0.1','11211');
    //the iwiw provider
    $provider = new osapiIwiwProvider($iwiwBaseURL, $iwiwBaseApiURL);
    $auth = osapiOAuth3Legged_10a_iwiw::performOAuthLogin($consumerKey, $consumerSecret, $storage, $provider, $localUserId);
    $osapi = new osapi($provider, $auth);
    return $osapi;
}
 /**
  * The 3 legged oauth class needs a way to store the access key and token
  * it uses the osapiStorage class to do so.
  *
  * Constructing this class will initiate the 3 legged oauth work flow, including redirecting
  * to the OAuth provider's site if required(!)
  *
  * @param string $consumerKey
  * @param string $consumerSecret
  * @param osapiStorage $storage storage class to use (file,apc,memcache,mysql)
  * @param osapiProvider $provider the provider configuration (required to get the oauth endpoints)
  * @param any $localUser the *local* user ID (this is not the user's ID on the social network site, but the user id on YOUR site, this is used to link the oauth access token to a local login)
  * @param any $userId the *remote* user ID, you can supply this user id if known but it's completely optional. If set it will be included in the oauth requests in the xoauth_requestor_id field)
  * @return osapiOAuth3Legged the logged-in provider instance
  */
 public static function performOAuthLogin($consumerKey, $consumerSecret, osapiStorage $storage, osapiProvider $provider, $localUserId = null, $userId = null)
 {
     $auth = new osapiOAuth3Legged_10a_iwiw($consumerKey, $consumerSecret, $storage, $provider, $localUserId, $userId);
     if (($token = $storage->get($auth->storageKey)) !== false) {
         $auth->accessToken = $token;
     } else {
         if (isset($_GET['oauth_verifier']) && isset($_GET['oauth_token']) && isset($_GET['uid'])) {
             $uid = $_GET['uid'];
             $secret = $auth->storage->get($auth->storageKey . ":nonce" . $uid);
             $auth->storage->delete($auth->storageKey . ":nonce" . $uid);
             $token = $auth->upgradeRequestToken($_GET['oauth_token'], $secret, $_GET['oauth_verifier']);
             $auth->redirectToOriginal();
         } else {
             // Initialize the OAuth dance, first request a request token, then kick the client to the authorize URL
             // First we store the current URL in our storage, so that when the oauth dance is completed we can return there
             $callbackUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
             $uid = uniqid();
             $token = $auth->obtainRequestToken($callbackUrl, $uid);
             $auth->storage->set($auth->storageKey . ":nonce" . $uid, $token->secret);
             $auth->redirectToAuthorization($token);
         }
     }
     return $auth;
 }