krz/vox-populi

clone: git clone https://gitbay.org/krz/vox-populi.git

main: action.php · raw

  1<?php
  2    require __DIR__ . '/vendor/autoload.php';
  3
  4    session_start();
  5
  6    $consumer_key = getenv('CONSUMER_KEY');
  7    $consumer_secret = getenv('CONSUMER_SECRET');
  8    $client = new Tumblr\API\Client($consumer_key, $consumer_secret);
  9    $requestHandler = $client->getRequestHandler();
 10    $requestHandler->setBaseUrl('https://www.tumblr.com/');
 11
 12    // Check if the user has already authenticated
 13    if(isset($_SESSION['perm_token']) && !empty($_SESSION['perm_token']) && isset($_SESSION['perm_secret']) && !empty($_SESSION['perm_secret'])) {
 14        $token = $_SESSION['perm_token'];
 15        $token_secret = $_SESSION['perm_secret'];
 16    }
 17    // Check if the user was here earlier by checking cookies
 18    else if (isset($_COOKIE['perm_token']) && !empty($_COOKIE['perm_token']) && isset($_COOKIE['perm_secret']) && !empty($_COOKIE['perm_secret'])) {
 19        $token = $_COOKIE['perm_token'];
 20        $token_secret = $_COOKIE['perm_secret'];
 21    }
 22    // Check if this is the user's first visit
 23    else if (!isset($_GET['oauth_verifier'])) {
 24
 25        // Grab the oauth token
 26        $resp = $requestHandler->request('POST', 'oauth/request_token', array());
 27        $out = $result = $resp->body;
 28        $data = array();
 29        parse_str($out, $data);
 30
 31        // Save temporary tokens to session
 32        $_SESSION['tmp_token'] = $data['oauth_token'];
 33        $_SESSION['tmp_secret'] = $data['oauth_token_secret'];
 34
 35        // Redirect user to Tumblr auth page
 36        session_regenerate_id(true);
 37        $header_url = 'https://www.tumblr.com/oauth/authorize?oauth_token=' . $data['oauth_token'];
 38        header('Location: ' . $header_url);
 39        die();
 40
 41    }
 42    // Check if the user was just sent back from the Tumblr authentication site
 43    else {
 44
 45        $verifier = $_GET['oauth_verifier'];
 46
 47        // Use the stored temporary tokens
 48        $client->setToken($_SESSION['tmp_token'], $_SESSION['tmp_secret']);
 49
 50        // Access the permanent tokens
 51        $resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier));
 52        $out = $result = $resp->body;
 53        $data = array();
 54        parse_str($out, $data);
 55
 56        // Set permanent tokens
 57        $token = $data['oauth_token'];
 58        $token_secret = $data['oauth_token_secret'];;
 59        $_SESSION['perm_token'] = $data['oauth_token'];
 60        $_SESSION['perm_secret'] = $data['oauth_token_secret'];
 61
 62        // Set cookies in case the user comes back later
 63        setcookie("perm_token", $_SESSION['perm_token']);
 64        setcookie("perm_secret", $_SESSION['perm_secret']);
 65
 66        // Redirect user to homepage for a clean URL
 67        session_regenerate_id(true);
 68        $header_url = 'https://example.com/vox-populi/';
 69        header('Location: ' . $header_url);
 70        die();
 71
 72    }
 73
 74    // Authenticate via OAuth
 75    $client = new Tumblr\API\Client(
 76        $consumer_key,
 77        $consumer_secret,
 78        $token,
 79        $token_secret
 80    );
 81
 82    // Set up a function to check if variables are blank later (this function accepts 0, 0.0, and "0" as valid)
 83    function not_blank($value) {
 84        return !empty($value) && isset($value) && $value !== '';
 85    }
 86
 87    // Grab the callback URL
 88    $url = $_GET['callback'];
 89    if (!not_blank($url)) {
 90        $url = 'Location: https://example.com/vox-populi/';
 91    }
 92
 93    // See what action we need to do
 94    $action = $_GET['action'];
 95    if (!not_blank($action)) {
 96        header('Location: ' . $url);
 97        die();
 98    }
 99
100    // Follow a new blog
101    if ($action == "follow") {
102        $blogName = $_GET['blog_name'];
103        if(not_blank($blogName)) {
104            $client->follow($blogName);
105            print 'success';
106        } else {
107            print 'failure';
108        }
109    }
110
111    // Unfollow a blog
112    if ($action == "unfollow") {
113        $blogName = $_GET['blog_name'];
114        if(not_blank($blogName)) {
115            $client->unfollow($blogName);
116            print 'success';
117        } else {
118            print 'failure';
119        }
120    }
121
122    // Like a post
123    if ($action == "like") {
124        $postId = $_GET['post_id'];
125        $reblogKey = $_GET['reblog_key'];
126        if(not_blank($postId) && not_blank($reblogKey)) {
127            $client->like($postId, $reblogKey);
128            print 'success';
129        } else {
130            print 'failure';
131        }
132    }
133
134    // Unlike a post
135    if ($action == "unlike") {
136        $postId = $_GET['post_id'];
137        $reblogKey = $_GET['reblog_key'];
138        if(not_blank($postId) && not_blank($reblogKey)) {
139            $client->unlike($postId, $reblogKey);
140            print 'success';
141        } else {
142            print 'failure';
143        }
144    }
145
146    // Reblog a post
147    if ($action == "reblog") {
148        $blogName = $_GET['blog_name'];
149        $id = $_GET['id'];
150        $reblogKey = $_GET['reblog_key'];
151        if(not_blank($blogName) && not_blank($id) && not_blank($reblogKey)) {
152            $client->reblogPost($blogName, $id, $reblogKey);
153            print 'success';
154        } else {
155            print 'failure';
156        }
157    }
158
159    // Unreblog a post
160    if ($action == "reblog") {
161        $blogName = $_GET['blog_name'];
162        $id = $_GET['id'];
163        $reblogKey = $_GET['reblog_key'];
164        if(not_blank($blogName) && not_blank($id) && not_blank($reblogKey)) {
165            $client->deletePost($blogName, $id, $reblogKey);
166            print 'success';
167        } else {
168            print 'failure';
169        }
170    }
171?>