getRequestHandler(); $requestHandler->setBaseUrl('https://www.tumblr.com/'); // Check if the user has already authenticated if(isset($_SESSION['perm_token']) && !empty($_SESSION['perm_token']) && isset($_SESSION['perm_secret']) && !empty($_SESSION['perm_secret'])) { $token = $_SESSION['perm_token']; $token_secret = $_SESSION['perm_secret']; } // Check if the user was here earlier by checking cookies else if (isset($_COOKIE['perm_token']) && !empty($_COOKIE['perm_token']) && isset($_COOKIE['perm_secret']) && !empty($_COOKIE['perm_secret'])) { $token = $_COOKIE['perm_token']; $token_secret = $_COOKIE['perm_secret']; } // Check if this is the user's first visit else if (!isset($_GET['oauth_verifier'])) { // Grab the oauth token $resp = $requestHandler->request('POST', 'oauth/request_token', array()); $out = $result = $resp->body; $data = array(); parse_str($out, $data); // Save temporary tokens to session $_SESSION['tmp_token'] = $data['oauth_token']; $_SESSION['tmp_secret'] = $data['oauth_token_secret']; // Redirect user to Tumblr auth page session_regenerate_id(true); $header_url = 'https://www.tumblr.com/oauth/authorize?oauth_token=' . $data['oauth_token']; header('Location: ' . $header_url); die(); } // Check if the user was just sent back from the Tumblr authentication site else { $verifier = $_GET['oauth_verifier']; // Use the stored temporary tokens $client->setToken($_SESSION['tmp_token'], $_SESSION['tmp_secret']); // Access the permanent tokens $resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier)); $out = $result = $resp->body; $data = array(); parse_str($out, $data); // Set permanent tokens $token = $data['oauth_token']; $token_secret = $data['oauth_token_secret'];; $_SESSION['perm_token'] = $data['oauth_token']; $_SESSION['perm_secret'] = $data['oauth_token_secret']; // Set cookies in case the user comes back later setcookie("perm_token", $_SESSION['perm_token']); setcookie("perm_secret", $_SESSION['perm_secret']); // Redirect user to homepage for a clean URL session_regenerate_id(true); $header_url = 'https://example.com/vox-populi/'; header('Location: ' . $header_url); die(); } // Authenticate via OAuth $client = new Tumblr\API\Client( $consumer_key, $consumer_secret, $token, $token_secret ); // Set up a function to check if variables are blank later (this function accepts 0, 0.0, and "0" as valid) function not_blank($value) { return !empty($value) && isset($value) && $value !== ''; } // Echo HTML contents echo ' Vox Populi - A Tumblr Web Client
'; // Get the user's blog name for welcome message if ($client->getUserInfo()) { $client->getUserInfo(); } else { // Echo rate-limit error message $rate_error = '
[429] Error: Tumblr rate limit exceeded. Please visit again tomorrow.
'; echo $rate_error; die(); } foreach ($client->getUserInfo()->user->blogs as $blog) { echo '

Welcome, ' . $blog->name . '!

'; } // Create function to allow a client to get 20 posts per page function get_dashboard_posts($client, $post_start, $limit, $post_type) { if ($post_type != NULL) { $dashboard_posts = $client->getDashboardPosts(array('limit' => $limit, 'offset' => $post_start, 'reblog_info' => true, 'type' => $post_type)); } else { $dashboard_posts = $client->getDashboardPosts(array('limit' => $limit, 'offset' => $post_start, 'reblog_info' => true)); } $card_columns = '
'; foreach ($dashboard_posts->posts as $post) { $card_columns .= '
'; $card_columns .= '
'; $card_columns .= '' . $post->blog_name . ''; if (not_blank($post->reblogged_from_name)) { $card_columns .= '' . $post->reblogged_from_name . ''; if (!$post->reblogged_from_following) { $card_columns .= 'Follow'; } } $card_columns .= '
'; // Add 'follow user' button if not following blog if ($post->followed) { $card_columns .= '
'; } else { $card_columns .= '
'; } // Add root blog (original poster) if ($post->reblogged_root_following) { $card_columns .= '
'; $card_columns .= ''; $card_columns .= '' . $post->reblogged_root_name . '
'; $card_columns .= '
'; } else { $card_columns .= '
'; $card_columns .= ''; $card_columns .= '' . $post->reblogged_root_name . '
'; if (strpos($post->reblogged_root_name, 'deactivated') == false) { $card_columns .= ''; } $card_columns .= '
'; } if ($post->type == 'photo') { $card_columns .= '...'; } if ($post->type == 'video') { $card_columns .= ''; } $card_columns .= '
'; if ($post->type == 'photo') { if (not_blank($post->caption)) { $card_columns .= '

' . $post->caption . '

'; } } if ($post->type == 'text') { if (not_blank($post->title)) { $card_columns .= '
' . $post->title . '
'; } if (not_blank($post->body)) { $card_columns .= '

' . $post->body . '

'; } } if ($post->type == 'answer') { $card_columns .= '

' . $post->asking_name . ': ' . $post->question . '

'; $card_columns .= '
'; $card_columns .= $post->answer; } if ($post->type == 'quote') { $card_columns .= '
' . $post->summary; $card_columns .= '

' . $post->reblog->comment . '
'; } if ($post->type == 'chat') { for ($i = 0; $i <= count($post->dialogue); $i++) { $card_columns .= '
' . (not_blank($post->dialogue[$i]->name) ? ('' . $post->dialogue[$i]->name . ': ') : "") . $post->dialogue[$i]->phrase . '
'; } } /* This seems to just post a duplicate of a QA answer if (isset($post->reblog->comment) && $post->reblog->comment !== '') { $card_columns .= '

' . $post->reblog->comment . '

'; } */ // $card_columns .= 'Visit Post →'; $card_columns .= ''; } else { // Unlike this post $card_columns .= '
'; } $card_columns .= '
'; } $card_columns .= '
'; return $card_columns; } // Create a loop to call as many dashboard posts as you want (results are returned in sets of 20 per API rules) // Can specify post type: text, chat, link, photo, audio, video, NULL if (isset($_GET['type'])) { $post_type = $_GET['type']; } else { $post_type = NULL; } if (isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; } $post_start = (($page - 1) * 20) + 1; $limit = 20; echo get_dashboard_posts($client, $post_start, $limit, $post_type); // Echo HTML page navigation // Embedded PHP tags here are calculating page numbers for URL parameters echo ' '; ?>