dashboard/index.php

283 lines · 16901 bytes

  1<?php
  2    require __DIR__ . '/../vendor/autoload.php';
  3    require_once __DIR__ . '/../includes/posts.php';
  4    require_once __DIR__ . '/../includes/auth.php';
  5
  6    session_start();
  7
  8    $consumer_key = getenv('CONSUMER_KEY');
  9    $consumer_secret = getenv('CONSUMER_SECRET');
 10    $client = new Tumblr\API\Client($consumer_key, $consumer_secret);
 11    $requestHandler = $client->getRequestHandler();
 12    $requestHandler->setBaseUrl('https://www.tumblr.com/');
 13
 14    list($token, $token_secret) = tumblr_tokens($client, $requestHandler);
 15
 16    // Authenticate via OAuth
 17    $client = new Tumblr\API\Client(
 18        $consumer_key,
 19        $consumer_secret,
 20        $token,
 21        $token_secret
 22    );
 23
 24
 25    // Echo HTML contents
 26    echo '<!doctype html><html lang="en"><head>
 27        <meta charset="utf-8">
 28        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
 29        <meta http-equiv="x-ua-compatible" content="ie=edge">
 30        <meta name="author" content="">
 31        <meta name="description" content="Vox Populi is a web client for Tumblr, allowing you to access you personal Tumblr dashboard without ads.">
 32        <link rel="apple-touch-icon" sizes="180x180" href="../assets/favicon/apple-touch-icon.png">
 33        <link rel="icon" type="image/png" sizes="32x32" href="../assets/favicon/favicon-32x32.png">
 34        <link rel="icon" type="image/png" sizes="16x16" href="../assets/favicon/favicon-16x16.png">
 35        <link rel="manifest" href="../assets/favicon/site.webmanifest">
 36        <link rel="stylesheet" href="../assets/css/bootstrap-4.5.2.min.css">
 37        <link rel="stylesheet" href="../assets/css/app.css">
 38        <title>Vox Populi - A Tumblr Web Client</title>
 39        <style>
 40            @font-face {
 41                font-family: "IBM Plex Sans";
 42                src: url("../assets/fonts/IBMPlexSans-Regular.eot");
 43                src: url("../assets/fonts/IBMPlexSans-Regular.woff2") format("woff2"),
 44                    url("../assets/fonts/IBMPlexSans-Regular.woff") format("woff"),
 45                    url("../assets/fonts/IBMPlexSans-Regular.ttf") format("truetype");
 46            }
 47
 48            @font-face {
 49                font-family: "IBM Plex Sans";
 50                src: url("../assets/fonts/IBMPlexSans-Bold.eot");
 51                src: url("../assets/fonts/IBMPlexSans-Bold.woff2") format("woff2"),
 52                    url("../assets/fonts/IBMPlexSans-Bold.woff") format("woff"),
 53                    url("../assets/fonts/IBMPlexSans-Bold.ttf") format("truetype");
 54                font-weight: bold;
 55            }
 56
 57            @font-face {
 58                font-family: "IBM Plex Mono";
 59                src: url("../assets/fonts/IBMPlexMono-Regular.eot");
 60                src: url("../assets/fonts/IBMPlexMono-Regular.woff2") format("woff2"),
 61                    url("../assets/fonts/IBMPlexMono-Regular.woff") format("woff"),
 62                    url("../assets/fonts/IBMPlexMono-Regular.ttf") format("truetype");
 63            }
 64
 65            @font-face {
 66                font-family: "IBM Plex Mono";
 67                src: url("../assets/fonts/IBMPlexMono-Bold.eot");
 68                src: url("../assets/fonts/IBMPlexMono-Bold.woff2") format("woff2"),
 69                    url("../assets/fonts/IBMPlexMono-Bold.woff") format("woff"),
 70                    url("../assets/fonts/IBMPlexMono-Bold.ttf") format("truetype");
 71                font-weight: bold;
 72            }
 73        </style>
 74    </head>
 75
 76    <body>
 77        <div class="banner fixed-top" role="contentinfo">
 78            <p class="m-0 mr-2">Black Lives Matter.</p>
 79            <a href="https://developer.ibm.com/callforcode/racial-justice/" target="_blank" rel="noopener">Support the Call for Code®</a>
 80        </div>
 81        <nav class="navbar navbar-expand-lg navbar-dark fixed-top">
 82            <li class="nav-item d-block d-md-none">
 83                <a id="sidebarButton" class="nav-link" href="javascript:void(0)"><svg id="icon-side-menu"
 84                        xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 32 32" aria-hidden="true">
 85                        <path
 86                            d="M14 4H18V8H14zM4 4H8V8H4zM24 4H28V8H24zM14 14H18V18H14zM4 14H8V18H4zM24 14H28V18H24zM14 24H18V28H14zM4 24H8V28H4zM24 24H28V28H24z">
 87                        </path>
 88                    </svg></a>
 89            </li>
 90            <a class="navbar-brand" href="./">Vox Populi</a>
 91
 92            <ul class="navbar-nav mr-0 ml-auto flex-row">
 93                <li class="nav-item">
 94                    <a id="search-button" class="nav-link" href="javascript:void(0)"><svg id="icon-search"
 95                            xmlns="http://www.w3.org/2000/svg" description="Open search" width="20" height="20"
 96                            viewBox="0 0 32 32" aria-hidden="true">
 97                            <path d="M30,28.59,22.45,21A11,11,0,1,0,21,22.45L28.59,30ZM5,14a9,9,0,1,1,9,9A9,9,0,0,1,5,14Z">
 98                            </path>
 99                        </svg></a>
100                </li>
101            </ul>
102            <form id="search-form" class="form-inline h-100 d-none flex-fill" method="get"
103                action="../search.php">
104                <input class="form-control h-100 ml-auto" type="search" placeholder="Search" aria-label="Search"
105                    name="query">
106                <a id="search-close-button" class="nav-link" href="javascript:void(0)">
107                    <svg id="icon-search" xmlns="http://www.w3.org/2000/svg" description="Clear search" width="20"
108                        height="20" viewBox="0 0 32 32" aria-hidden="true">
109                        <path
110                            d="M24 9.4L22.6 8 16 14.6 9.4 8 8 9.4 14.6 16 8 22.6 9.4 24 16 17.4 22.6 24 24 22.6 17.4 16 24 9.4z">
111                        </path>
112                    </svg>
113                </a>
114            </form>
115        </nav>
116
117        <div class="container-fluid">
118            <div class="row">
119                <div id="sidebar" class="sidebar d-none d-md-block col-md-3 col-lg-2 p-0">
120                    <div class="sidebar-box p-0">
121                        <div>
122                            <ul class="actions">
123                                <li><p>Account</p></li>
124                                <hr class="sidebar-divider">
125                                <li><a href="../dashboard/">Dashboard</a></li>
126                                <li><a href="../profile/">Profile</a></li>
127                                <li><a href="../logout.php">Logout</a></li>
128                            </ul>
129                            <ul class="categories">
130                                <li><p>Filter</p></li>
131                                <hr class="sidebar-divider">
132                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == '') ? 'active' : ""); echo '" href="./">All Posts</a></li>
133                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'answer') ? 'active' : ""); echo '" href="./?type=answer">Answer</a></li>
134                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'audio') ? 'active' : ""); echo '" href="./?type=audio">Audio</a></li>
135                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'chat') ? 'active' : ""); echo '" href="./?type=chat">Chat</a></li>
136                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'link') ? 'active' : ""); echo '" href="./?type=link">Link</a></li>
137                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'photo') ? 'active' : ""); echo '" href="./?type=photo">Photo</a></li>
138                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'quote') ? 'active' : ""); echo '" href="./?type=quote">Quote</a></li>
139                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'text') ? 'active' : ""); echo '" href="./?type=text">Text</a></li>
140                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'video') ? 'active' : ""); echo '" href="./?type=video">Video</a></li>
141                            </ul>
142                        </div>
143                        <div>
144                            <hr class="sidebar-divider">
145                            <ul class="meta">
146                                <li>
147                                <a href="#" target="_blank">Source Code <svg
148                                            id="icon-external-link" xmlns="http://www.w3.org/2000/svg" width="16"
149                                            height="16" viewBox="0 0 16 16" aria-hidden="true">
150                                            <path
151                                                d="M13,14H3c-0.6,0-1-0.4-1-1V3c0-0.6,0.4-1,1-1h5v1H3v10h10V8h1v5C14,13.6,13.6,14,13,14z">
152                                            </path>
153                                            <path d="M10 1L10 2 13.3 2 9 6.3 9.7 7 14 2.7 14 6 15 6 15 1z"></path>
154                                        </svg>
155                                    </a>
156                                </li>
157                                <li>
158                                    <a href="https://example.com" target="_blank">&copy; 0000</a>
159                                </li>
160                            </ul>
161                        </div>
162                    </div>
163                </div>
164                <div id="main" class="main col-xs-12 col-md-9 col-lg-10 ml-auto">
165                    <span class="homepage--dots"></span>';
166
167            // Get the user's blog name for welcome message
168            if ($client->getUserInfo()) {
169                $client->getUserInfo();
170            } else {
171                // Echo rate-limit error message
172                $rate_error = '<div id="rate-limit-exceeded" class="alert alert-danger my-5"><b>[429] Error</b>: Tumblr rate limit exceeded. Please visit again tomorrow.</div>';
173                echo $rate_error;
174                die();
175            }
176            foreach ($client->getUserInfo()->user->blogs as $blog) {
177                echo '<h1 class="text-center py-4">Welcome, <a href="https://' . $blog->name . '.tumblr.com">' . $blog->name . '</a>!</h1>';
178            }
179
180            // Create function to allow a client to get 20 posts per page
181            function get_dashboard_posts($client, $post_start, $limit, $post_type) {
182                if ($post_type != NULL) {
183                    $dashboard_posts = $client->getDashboardPosts(array('limit' => $limit, 'offset' => $post_start, 'reblog_info' => true, 'type' => $post_type));
184                } else {
185                    $dashboard_posts = $client->getDashboardPosts(array('limit' => $limit, 'offset' => $post_start, 'reblog_info' => true));
186                }
187                $card_columns = '<div class="card-columns">';
188                foreach ($dashboard_posts->posts as $post) {
189                    $card_columns .= post_header($client, $post);
190
191                    $card_columns .= post_media($post);
192                    $card_columns .= '<div class="card-body">';
193                    $card_columns .= post_content($post);
194                    /* This seems to just post a duplicate of a QA answer
195                    if (isset($post->reblog->comment) && $post->reblog->comment !== '') {
196                        $card_columns .= '<p class="card-text reblog-comment">' . $post->reblog->comment . '</p>';
197                    }
198                    */
199                    // $card_columns .= '<a href="' . $post->post_url . '" class="card-link">Visit Post &rarr;</a>';
200                    $card_columns .= '<div class="card-footer d-flex justify-content-between align-items-center p-0"><div class="note-count">' . number_format($post->note_count, 0) . ' notes</div>';
201                    $card_columns .= '<div class="post-icons"><a href="' . $post->post_url . '" target="_blank" title="View on Tumblr"><i data-feather="external-link"></i></a>';
202                    $card_columns .= '<a disabled href="#" title="Share"><i data-feather="send"></i></a>';
203                    $card_columns .= '<a disabled href="#" title="Comment"><i data-feather="message-square"></i></a>';
204
205                    // Set up actions to reblog/unreblog post
206                    if ($post->liked != true) {
207                        // Reblog this post
208                        $card_columns .= '<a href="javascript:void(0);" onclick="reblog(\'' . urlencode($post->blog_name) . '\', \'' . urlencode($post->id) . '\', \'' . urlencode($post->reblog_key) . '\');" title="Reblog" data-reblog-id="' . $post->id . '"><i data-feather="repeat"></i></a>';
209                    } else {
210                        // Unreblog this post
211                        $card_columns .= '<a href="javascript:void(0);" onclick="unblog(\'' . urlencode($post->blog_name) . '\', \'' . urlencode($post->id) . '\', \'' . urlencode($post->reblog_key) . '\');" title="Unblog" data-unreblog-id="' . $post->id . '"><i data-feather="repeat"></i></a>';
212                    }
213
214                    // Set up actions to like/unlike post
215                    if ($post->liked != true) {
216                        // Like this post
217                        $card_columns .= '<a href="javascript:void(0);" onclick="like(\'' . urlencode($post->id) . '\', \'' . urlencode($post->reblog_key) . '\');" title="Like" data-like-id="' . $post->id . '"><i data-feather="heart"></i></a></div></div>';
218                    } else {
219                        // Unlike this post
220                        $card_columns .= '<a href="javascript:void(0);" onclick="unlike(\'' . urlencode($post->id) . '\', \'' . urlencode($post->reblog_key) . '\');" title="Unlike" data-unlike-id="' . $post->id . '"><i data-feather="heart"></i></a></div></div>';
221                    }
222
223                    $card_columns .= '</div></div>';
224                }
225                $card_columns .= '</div>';
226                return $card_columns;
227            }
228
229            // Create a loop to call as many dashboard posts as you want (results are returned in sets of 20 per API rules)
230            // Can specify post type: text, chat, link, photo, audio, video, NULL
231            if (isset($_GET['type'])) {
232                $post_type = $_GET['type'];
233            } else {
234                $post_type = NULL;
235            }
236            if (isset($_GET['page'])) {
237                $page = $_GET['page'];
238            } else {
239                $page = 1;
240            }
241            $post_start = (($page - 1) * 20) + 1;
242            $limit = 20;
243            echo get_dashboard_posts($client, $post_start, $limit, $post_type);
244
245            // Echo HTML page navigation
246            // Embedded PHP tags here are calculating page numbers for URL parameters
247            echo '<nav aria-label="Page navigation">
248            <ul class="pagination justify-content-center">
249                <li class="page-item '; echo ($page <= 1 ? "disabled" : ""); echo'">
250                    <a class="page-link" href="./?page='; echo ($page-1); echo (isset($_GET['type']) ? "&type=" . $_GET['type'] : ""); echo '" aria-label="Previous">
251                        <span aria-hidden="true">&laquo;</span>
252                    </a>
253                </li>
254                <li class="page-item '; echo ($page <= 1 ? "disabled" : "");  echo '"><a class="page-link"
255                        href="./?page='; echo ($page-1); echo (isset($_GET['type']) ? "&type=" . $_GET['type'] : ""); echo '">'; echo ($page-1); echo '</a></li>
256                <li class="page-item active"><a class="page-link" href="#">'; echo $page; echo '</a></li>
257                <li class="page-item"><a class="page-link"
258                        href="./?page='; echo ($page+1); echo (isset($_GET['type']) ? "&type=" . $_GET['type'] : ""); echo '">'; echo ($page+1); echo '</a></li>
259                <li class="page-item">
260                    <a class="page-link" href="./?page='; echo ($page+1); echo (isset($_GET['type']) ? "&type=" . $_GET['type'] : ""); echo'" aria-label="Next">
261                        <span aria-hidden="true">&raquo;</span>
262                    </a>
263                </li>
264            </ul>
265        </nav>
266    </div>
267
268    </div>
269    </div>
270    <button class="btn-to-top" type="button" aria-label="Back to Top">
271        <svg id="icon-to-top" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 32 32"
272            aria-hidden="true">
273            <path d="M16 14L6 24 7.4 25.4 16 16.8 24.6 25.4 26 24zM4 8H28V10H4z"></path>
274        </svg>
275    </button>
276
277    <!-- JavaScript -->
278    <script src="../assets/js/feather-icons.4.2.8.min.js"></script>
279    <script src="../assets/js/jquery-3.5.1.min.js"></script>
280    <script src="../assets/js/bootstrap.bundle.min.js"></script>
281    <script src="../assets/js/app.js"></script>
282</body></html>';
283?>