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 $client->getUserInfo(); foreach ($client->getUserInfo()->user->blogs as $blog) { echo '

Search results for: ' . $_GET['query'] . '

'; } // Create function to allow a client to get 20 posts per page function get_tagged_posts($client, $before_date, $limit) { $tag = $_GET['query']; $tagged_posts = $client->getTaggedPosts($tag, $options=null); $card_columns = '
'; foreach ($tagged_posts as $post) { $card_columns .= '
'; $card_columns .= '
'; $card_columns .= '' . $post->blog_name . '
'; if ($post->followed != true) { // Send user to action.php to follow a new blog $query_string = 'action=follow&blog_name=' . urlencode($post->blog_name) . '&callback=' . urlencode('https://' . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI]); $card_columns .= '
'; } else { $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 .= ''; $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_tagged_posts($client, $post_start, $limit, $post_type); echo '
'; ?>