Code (PHP) pasted on 2017-10-28, 01:56 Raw Source
- <?php
- namespace TSHW\Paste;
- use League\OAuth2\Client\Provider\AbstractProvider;
- use League\Plates\Engine;
- class App {
- /** @var Engine */
- private $tpl;
- /** @var string */
- private $dataDir;
- /** @var AbstractProvider */
- private $oauthProvider;
- /** @var \GeSHi */
- private $highlighter;
- private $popularLanguages = [
- 'php', 'java', 'scala', 'javascript', 'xml', 'text', 'python'
- ];
- private $ignoredLanguages = [
- 'text'
- ];
- private $userWhitelist = [
- 'Tar-Minyatur',
- 'chani'
- ];
- /**
- * App constructor.
- * @param Engine $tpl Template engine (Plates)
- * @param string $dataDir Directory where pastes are stored
- * @param AbstractProvider $oauthProvider
- */
- public function __construct(Engine $tpl, string $dataDir, AbstractProvider $oauthProvider) {
- $this->tpl = $tpl;
- $this->oauthProvider = $oauthProvider;
- $this->highlighter = new \GeSHi();
- $this->highlighter->enable_classes(true);
- $this->highlighter->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
- $this->highlighter->set_line_style('background: #eaeaea; padding-left: 0.4em');
- $this->highlighter->set_header_type(GESHI_HEADER_NONE);
- $this->highlighter->set_overall_class('highlighted_code');
- }
- public function handleRequest() {
- $this->tpl->addData(['user' => $_SESSION['username']]);
- }
- $this->handleFormActions();
- $url = $_SERVER['REQUEST_URI'];
- }
- $this->showPaste($matches[1], $matches[2] === '/raw');
- } else if ($url === '/p/github-login') {
- $this->handleOauthLogin();
- } else if ($url === '/p/logout') {
- $this->redirect('/');
- } else if ($url === '/') {
- $this->enforceOauthLogin();
- $this->showIndex();
- } else {
- $this->showError('Oh damn, this page does not exist. :-(');
- }
- }
- private function redirect(string $path) {
- http_response_code(302);
- }
- private function showError(string $message) {
- http_response_code(400);
- }
- private function handleFormActions() {
- switch ($_POST['action']) {
- case "create":
- $pasteId = $this->createNewPaste($_POST);
- $this->redirect('/' . $pasteId);
- break;
- }
- }
- }
- $this->showError('Something went completely wrong. This should not have happened. :-( Please try again.');
- }
- $pasteId = $this->getNewPasteId();
- $this->showError('For some unclear reason I could not generate a new ID for you. :-( Please try again.');
- }
- $content = [
- 'language' => $language,
- 'creator' => $_SESSION['username'],
- 'code' => $params['code'],
- ];
- $this->showError('Your paste could not be created. Sorry about that. :-( Please try again.');
- }
- return $pasteId;
- }
- private function getNewPasteId() {
- $attempts = 3;
- $pasteId = null;
- do {
- $attempts -= 1;
- $pasteId = $newId;
- }
- return $pasteId;
- }
- private function showPaste(string $pasteId, bool $showRaw = false) {
- $filename = $this->dataDir . $pasteId;
- $this->showError('Oh no...looks like this paste is gone or never existed. :-(');
- }
- $this->showError('Oh no...looks like this paste a little more than I can chew right now. :-(');
- }
- if ($showRaw) {
- }
- $this->highlighter->set_source($content->code);
- $this->highlighter->set_language($content->language);
- if ($this->highlighter->error() !== false) {
- $this->showError("This is embarrassing. I failed to render the paste you wanted to open. Sorry. :-(");
- }
- echo $this->tpl->render('paste', [
- 'pasteId' => $pasteId,
- 'source' => $this->highlighter->parse_code(),
- 'stylesheet' => $this->highlighter->get_stylesheet()]);
- }
- private function showIndex() {
- $languages = [
- 'popular' => [],
- 'others' => []
- ];
- include $file;
- $languageName = $language_data['LANG_NAME'];
- $languages['popular'][$language] = $languageName;
- } else {
- $languages['others'][$language] = $languageName;
- }
- }
- echo $this->tpl->render('index', ['languages' => $languages]);
- }
- private function handleOauthLogin() {
- // If we don't have an authorization code then get one
- $authUrl = $this->oauthProvider->getAuthorizationUrl();
- $_SESSION['oauth2state'] = $this->oauthProvider->getState();
- $this->showError('Uh! Something went wrong with the authorization. I aborted this for security reasons, but please feel free to try again!');
- } else {
- // Try to get an access token (using the authorization code grant)
- try {
- $token = $this->oauthProvider->getAccessToken('authorization_code', [
- 'code' => $_GET['code']
- ]);
- // We got an access token, let's now get the user's details
- $user = $this->oauthProvider->getResourceOwner($token);
- $this->showError('Sorry, your username (' . $user->getNickname() . ') is not whitelisted and therefore I cannot allow you to create Pastes. :-(');
- }
- $_SESSION['username'] = $user->getNickname();
- $this->redirect('/');
- } catch (\Exception $e) {
- // Failed to get user details
- $this->showError('Oh no! Something is wrong with your authorization!');
- }
- }
- }
- private function enforceOauthLogin() {
- echo $this->tpl->render('login_required');
- }
- }
- }