profile/index.php

289 lines · 16724 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="robots" content="none">
 31        <link rel="apple-touch-icon" sizes="180x180" href="../assets/favicon/apple-touch-icon.png">
 32        <link rel="icon" type="image/png" sizes="32x32" href="../assets/favicon/favicon-32x32.png">
 33        <link rel="icon" type="image/png" sizes="16x16" href="../assets/favicon/favicon-16x16.png">
 34        <link rel="manifest" href="../assets/favicon/site.webmanifest">
 35        <link rel="stylesheet" href="../assets/css/bootstrap-4.5.2.min.css">
 36        <link rel="stylesheet" href="../assets/css/app.css">
 37        <title>Profile | Vox Populi - A Tumblr Web Client</title>
 38        <style>
 39            @font-face {
 40                font-family: "IBM Plex Sans";
 41                src: url("../assets/fonts/IBMPlexSans-Regular.eot");
 42                src: url("../assets/fonts/IBMPlexSans-Regular.woff2") format("woff2"),
 43                    url("../assets/fonts/IBMPlexSans-Regular.woff") format("woff"),
 44                    url("../assets/fonts/IBMPlexSans-Regular.ttf") format("truetype");
 45            }
 46
 47            @font-face {
 48                font-family: "IBM Plex Sans";
 49                src: url("../assets/fonts/IBMPlexSans-Bold.eot");
 50                src: url("../assets/fonts/IBMPlexSans-Bold.woff2") format("woff2"),
 51                    url("../assets/fonts/IBMPlexSans-Bold.woff") format("woff"),
 52                    url("../assets/fonts/IBMPlexSans-Bold.ttf") format("truetype");
 53                font-weight: bold;
 54            }
 55
 56            @font-face {
 57                font-family: "IBM Plex Mono";
 58                src: url("../assets/fonts/IBMPlexMono-Regular.eot");
 59                src: url("../assets/fonts/IBMPlexMono-Regular.woff2") format("woff2"),
 60                    url("../assets/fonts/IBMPlexMono-Regular.woff") format("woff"),
 61                    url("../assets/fonts/IBMPlexMono-Regular.ttf") format("truetype");
 62            }
 63
 64            @font-face {
 65                font-family: "IBM Plex Mono";
 66                src: url("../assets/fonts/IBMPlexMono-Bold.eot");
 67                src: url("../assets/fonts/IBMPlexMono-Bold.woff2") format("woff2"),
 68                    url("../assets/fonts/IBMPlexMono-Bold.woff") format("woff"),
 69                    url("../assets/fonts/IBMPlexMono-Bold.ttf") format("truetype");
 70                font-weight: bold;
 71            }
 72        </style>
 73    </head>
 74
 75    <body>
 76        <div class="banner fixed-top" role="contentinfo">
 77            <p class="m-0 mr-2">Black Lives Matter.</p>
 78            <a href="https://developer.ibm.com/callforcode/racial-justice/" target="_blank" rel="noopener">Support the Call for Code®</a>
 79        </div>
 80        <nav class="navbar navbar-expand-lg navbar-dark fixed-top">
 81            <li class="nav-item d-block d-md-none">
 82                <a id="sidebarButton" class="nav-link" href="javascript:void(0)"><svg id="icon-side-menu"
 83                        xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 32 32" aria-hidden="true">
 84                        <path
 85                            d="M14 4H18V8H14zM4 4H8V8H4zM24 4H28V8H24zM14 14H18V18H14zM4 14H8V18H4zM24 14H28V18H24zM14 24H18V28H14zM4 24H8V28H4zM24 24H28V28H24z">
 86                        </path>
 87                    </svg></a>
 88            </li>
 89            <a class="navbar-brand" href="../dashboard/">Vox Populi</a>
 90
 91            <ul class="navbar-nav mr-0 ml-auto flex-row">
 92                <li class="nav-item">
 93                    <a id="search-button" class="nav-link" href="javascript:void(0)"><svg id="icon-search"
 94                            xmlns="http://www.w3.org/2000/svg" description="Open search" width="20" height="20"
 95                            viewBox="0 0 32 32" aria-hidden="true">
 96                            <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">
 97                            </path>
 98                        </svg></a>
 99                </li>
100            </ul>
101            <form id="search-form" class="form-inline h-100 d-none flex-fill" method="get"
102                action="../search.php">
103                <input class="form-control h-100 ml-auto" type="search" placeholder="Search" aria-label="Search"
104                    name="query">
105                <a id="search-close-button" class="nav-link" href="javascript:void(0)">
106                    <svg id="icon-search" xmlns="http://www.w3.org/2000/svg" description="Clear search" width="20"
107                        height="20" viewBox="0 0 32 32" aria-hidden="true">
108                        <path
109                            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">
110                        </path>
111                    </svg>
112                </a>
113            </form>
114        </nav>
115
116        <div class="container-fluid">
117            <div class="row">
118                <div id="sidebar" class="sidebar d-none d-md-block col-md-3 col-lg-2 p-0">
119                    <div class="sidebar-box p-0">
120                        <div>
121                            <ul class="actions">
122                                <li><p>Account</p></li>
123                                <hr class="sidebar-divider">
124                                <li><a href="../dashboard/">Dashboard</a></li>
125                                <li><a href="../profile/">Profile</a></li>
126                                <li><a href="../logout.php">Logout</a></li>
127                            </ul>
128                            <ul class="categories">
129                                <li><p>Filter</p></li>
130                                <hr class="sidebar-divider">
131                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == '') ? 'active' : ""); echo '" href="./">All Posts</a></li>
132                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'answer') ? 'active' : ""); echo '" href="./?type=answer">Answer</a></li>
133                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'audio') ? 'active' : ""); echo '" href="./?type=audio">Audio</a></li>
134                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'chat') ? 'active' : ""); echo '" href="./?type=chat">Chat</a></li>
135                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'link') ? 'active' : ""); echo '" href="./?type=link">Link</a></li>
136                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'photo') ? 'active' : ""); echo '" href="./?type=photo">Photo</a></li>
137                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'quote') ? 'active' : ""); echo '" href="./?type=quote">Quote</a></li>
138                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'text') ? 'active' : ""); echo '" href="./?type=text">Text</a></li>
139                                <li><a class="'; echo ((isset($_GET['type']) && $_GET['type'] == 'video') ? 'active' : ""); echo '" href="./?type=video">Video</a></li>
140                            </ul>
141                        </div>
142                        <div>
143                            <hr class="sidebar-divider">
144                            <ul class="meta">
145                                <li>
146                                    <a href="#" target="_blank">Source Code <svg
147                                            id="icon-external-link" xmlns="http://www.w3.org/2000/svg" width="16"
148                                            height="16" viewBox="0 0 16 16" aria-hidden="true">
149                                            <path
150                                                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">
151                                            </path>
152                                            <path d="M10 1L10 2 13.3 2 9 6.3 9.7 7 14 2.7 14 6 15 6 15 1z"></path>
153                                        </svg>
154                                    </a>
155                                </li>
156                                <li>
157                                    <a href="https://example.com" target="_blank">&copy; 0000</a>
158                                </li>
159                            </ul>
160                        </div>
161                    </div>
162                </div>
163                <div id="main" class="main col-xs-12 col-md-9 col-lg-10 ml-auto">
164                    <span class="homepage--dots"></span>';
165
166            // Get the user's blog name for welcome message
167            if ($client->getUserInfo()) {
168                $client->getUserInfo();
169            } else {
170                // Echo rate-limit error message
171                $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>';
172                echo $rate_error;
173                die();
174            }
175            foreach ($client->getUserInfo()->user->blogs as $blog) {
176                // Add header to show information about the blog
177                $blog_name = $blog->name;
178                $blog_description = $blog->description;
179                $blog_avatar = $blog->avatar[0]->url;
180                $blog_header_image = $blog->theme->header_image;
181                $blog_total_posts = $blog->total_posts;
182
183                echo '<div class="card blog-header">
184                        <img class="img-fluid blog-header-image" src="' . $blog_header_image . '">
185                        <div class="m-3">
186                            <div class="d-flex flex-row align-items-center">
187                                <img class="img-fluid mr-3 blog-avatar" height="64" width="64" src="' . $blog_avatar . '">
188                                <h1 class="m-0 blog-title">' . $blog_name . '</h1>
189                            </div>
190                            <hr>
191                            <p class="m-0 lead blog-description">' . $blog_description . '</p>
192                        </div>
193                      </div>';
194            }
195
196            // Create function to allow a client to get 20 posts per page
197            function get_blog_posts($client, $blog_identifier, $post_start, $limit, $post_type) {
198                if ($post_type != NULL) {
199                    $blog_posts = $client->getBlogPosts($blog_identifier, array('limit' => $limit, 'offset' => $post_start, 'reblog_info' => true, 'type' => $post_type));
200                } else {
201                    $blog_posts = $client->getBlogPosts($blog_identifier, array('limit' => $limit, 'offset' => $post_start, 'reblog_info' => true));
202                }
203
204                // Add posts to blog stream
205                $card_columns = '<div class="card-columns py-3">';
206                // print_r($blog_posts);
207                foreach ($blog_posts->posts as $post) {
208                    $card_columns .= post_header($client, $post);
209
210                    $card_columns .= post_media($post);
211                    $card_columns .= '<div class="card-body">';
212                    $card_columns .= post_content($post);
213                    // $card_columns .= '<a href="' . $post->post_url . '" class="card-link">Visit Post &rarr;</a>';
214                    $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>';
215                    $card_columns .= '<div class="post-icons"><a href="' . $post->post_url . '" target="_blank" title="View on Tumblr"><i data-feather="external-link"></i></a>';
216                    $card_columns .= '<a href="#" title="Share"><i data-feather="send"></i></a>';
217                    $card_columns .= '<a href="#" title="Comment"><i data-feather="message-square"></i></a>';
218                    $card_columns .= '<a href="#" title="Reblog"><i data-feather="repeat"></i></a>';
219
220                    if ($post->liked != true) {
221                        // Like this post
222                        $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>';
223                    } else {
224                        // Unlike this post
225                        $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>';
226                    }
227
228                    $card_columns .= '</div></div>';
229                }
230                $card_columns .= '</div>';
231                return $card_columns;
232            }
233
234            // Create a loop to call as many blog posts as you want (results are returned in sets of 20 per API rules)
235            // Can specify post type: text, chat, link, photo, audio, video, NULL
236            if (isset($_GET['type'])) {
237                $post_type = $_GET['type'];
238            } else {
239                $post_type = NULL;
240            }
241            if (isset($_GET['page'])) {
242                $page = $_GET['page'];
243            } else {
244                $page = 1;
245            }
246            $post_start = (($page - 1) * 20) + 1;
247            $limit = 20;
248            $blog_identifier = $blog_name . '.tumblr.com';
249            echo get_blog_posts($client, $blog_identifier, $post_start, $limit, $post_type);
250
251            // Echo HTML page navigation
252            // Embedded PHP tags here are calculating page numbers for URL parameters
253            echo '<nav aria-label="Page navigation">
254            <ul class="pagination justify-content-center">
255                <li class="page-item '; echo ($page <= 1 ? "disabled" : ""); echo'">
256                    <a class="page-link" href="./?page='; echo ($page-1); echo (isset($_GET['type']) ? "&type=" . $_GET['type'] : ""); echo '" aria-label="Previous">
257                        <span aria-hidden="true">&laquo;</span>
258                    </a>
259                </li>
260                <li class="page-item '; echo ($page <= 1 ? "disabled" : "");  echo '"><a class="page-link"
261                        href="./?page='; echo ($page-1); echo (isset($_GET['type']) ? "&type=" . $_GET['type'] : ""); echo '">'; echo ($page-1); echo '</a></li>
262                <li class="page-item active"><a class="page-link" href="#">'; echo $page; echo '</a></li>
263                <li class="page-item"><a class="page-link"
264                        href="./?page='; echo ($page+1); echo (isset($_GET['type']) ? "&type=" . $_GET['type'] : ""); echo '">'; echo ($page+1); echo '</a></li>
265                <li class="page-item">
266                    <a class="page-link" href="./?page='; echo ($page+1); echo (isset($_GET['type']) ? "&type=" . $_GET['type'] : ""); echo'" aria-label="Next">
267                        <span aria-hidden="true">&raquo;</span>
268                    </a>
269                </li>
270            </ul>
271        </nav>
272    </div>
273
274    </div>
275    </div>
276    <button class="btn-to-top" type="button" aria-label="Back to Top">
277        <svg id="icon-to-top" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 32 32"
278            aria-hidden="true">
279            <path d="M16 14L6 24 7.4 25.4 16 16.8 24.6 25.4 26 24zM4 8H28V10H4z"></path>
280        </svg>
281    </button>
282
283    <!-- JavaScript -->
284    <script src="../assets/js/feather-icons.4.2.8.min.js"></script>
285    <script src="../assets/js/jquery-3.5.1.min.js"></script>
286    <script src="../assets/js/bootstrap.bundle.min.js"></script>
287    <script src="../assets/js/app.js"></script>
288</body></html>';
289?>