From 39e8fb2036945303836c461a61f133b0059c8991 Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Mon, 22 May 2023 15:19:08 -0500 Subject: initial commit --- profile/index.php | 414 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 414 insertions(+) create mode 100644 profile/index.php (limited to 'profile') diff --git a/profile/index.php b/profile/index.php new file mode 100644 index 0000000..ad33c28 --- /dev/null +++ b/profile/index.php @@ -0,0 +1,414 @@ +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 ' + + + + + + + + + + + Profile | 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) { + // Add header to show information about the blog + $blog_name = $blog->name; + $blog_description = $blog->description; + $blog_avatar = $blog->avatar[0]->url; + $blog_header_image = $blog->theme->header_image; + $blog_total_posts = $blog->total_posts; + + echo '
+ +
+
+ +

' . $blog_name . '

+
+
+

' . $blog_description . '

+
+
'; + } + + // Create function to allow a client to get 20 posts per page + function get_blog_posts($client, $blog_identifier, $post_start, $limit, $post_type) { + if ($post_type != NULL) { + $blog_posts = $client->getBlogPosts($blog_identifier, array('limit' => $limit, 'offset' => $post_start, 'reblog_info' => true, 'type' => $post_type)); + } else { + $blog_posts = $client->getBlogPosts($blog_identifier, array('limit' => $limit, 'offset' => $post_start, 'reblog_info' => true)); + } + + // Add posts to blog stream + $card_columns = '
'; + // print_r($blog_posts); + foreach ($blog_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 . '
'; + } + } + // $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 blog 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; + $blog_identifier = $blog_name . '.tumblr.com'; + echo get_blog_posts($client, $blog_identifier, $post_start, $limit, $post_type); + + // Echo HTML page navigation + // Embedded PHP tags here are calculating page numbers for URL parameters + echo ' + + + + + + + + + + + +'; +?> -- cgit v1.2.3-70-g09d2