Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Http\Requests;
- use App\Paste;
- use App\User;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cookie;
- use Illuminate\Support\Facades\Crypt;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Input;
- use Hashids\Hashids;
- class PasteController extends Controller
- {
- public function submit(Requests\StorePaste $request)
- {
- $title = (empty(trim(Input::get('pasteTitle')))) ? 'Untitled' : Input::get('pasteTitle');
- $expiration = Input::get('expire');
- $privacy = Input::get('privacy');
- // Memeriksa apakah user sudah memilih Privacy di dropdown list
- // $possibleValuesPrivacy = array("link", "password", "private");
- // if (in_array($privacy, $possibleValuesPrivacy) == false) return view('welcome');
- // Jika user memilih ingin dilindungi dengan password, kami memiliki pass-nya, jika tidak akan ditandai 'disabled'
- if ($privacy == 'password') $password = bcrypt(Input::get('pastePassword'));
- else $password = 'disabled';
- $burnAfter = 0;
- // Menghasilkan timestamp kedaluwarsa
- switch ($expiration) {
- case 'never':
- $timestampExp = 0;
- break;
- case 'burn':
- $timestampExp = date('Y-m-d H:i:s', time());
- $burnAfter = 1;
- break;
- case '10m':
- $timestampExp = date('Y-m-d H:i:s', time()+(60*10));
- break;
- case '1h':
- $timestampExp = date('Y-m-d H:i:s', time()+(60*60));
- break;
- case '1d':
- $timestampExp = date('Y-m-d H:i:s', time()+(60*60*24));
- break;
- case '1w':
- $timestampExp = date('Y-m-d H:i:s', time()+(60*60*24*7));
- break;
- default:
- die("User input error.");
- break;
- }
- // Pembuatan URL selama URL masih tersedia
- // $generatedLink = str_random(10);
- // $existingPasteWithGeneratedLink = Paste::where('link', $generatedLink)->first();
- // while (!is_null($existingPasteWithGeneratedLink)) {
- // $generatedLink = str_random(10);
- // $existingPasteWithGeneratedLink = Paste::where('link', $generatedLink)->first();
- // }
- // URL dibuat dari id paste, lalu digenerate ke dalam 16 (huruf kecil & angka)
- // https://github.com/ivanakimov/hashids.php
- $hashids = new Hashids('', 16, 'abcdefghijklmnopqrstuvwxyz123456789');
- $generatedLinkDB = Paste::orderBy('id', 'desc')->limit(1)->firstOrFail();
- $generatedLink = $hashids->encode($generatedLinkDB->id + 1);
- // https://github.com/vinkla/laravel-hashids
- // $generatedLinkRaw = Paste::orderBy('id', 'desc')->limit(1)->firstOrFail();
- // $generatedLink = Hashids::encode($generatedLinkRaw->id + 1);
- Paste::create([
- 'link' => $generatedLink,
- 'userId' => (Auth::check()) ? Auth::id() : 0,
- 'views' => '0',
- 'title' => $title,
- 'content' => Crypt::encryptString(Input::get('pasteContent')),
- 'ip' => $request->ip(),
- 'expiration' => $timestampExp,
- 'privacy' => $privacy,
- 'password' => $password,
- 'noSyntax' => Input::has('noSyntax'),
- 'burnAfter' => $burnAfter,
- ]);
- return redirect('/'.$generatedLink);
- }
- public function view($link, Request $request)
- {
- $paste = Paste::where('link', $link)->firstOrFail();
- // Apakah user yang terhubung adalah orang yang membuat post paste?
- $isSameUser = ((Auth::user() == $paste->user && $paste->userId != 0)) ? true : false;
- // Paste kadaluarsa
- if ($paste->expiration != 0) {
- if ($paste->burnAfter == 0) {
- if (time() > strtotime($paste->expiration)){
- if ($isSameUser) $expiration = "Expired";
- else abort('404');
- }
- else $expiration = Carbon::parse($paste->expiration)->diffForHumans();
- }
- else {
- // Peringatan burn after reading
- if (time() - strtotime($paste->expiration) > 3) {
- $disableBurn = true;
- $expiration = "Burn after reading";
- }
- else $expiration = "Burn after reading (next time)";
- }
- }
- else {
- $expiration = "Never";
- }
- // Mengurus opsi privasi paste (TODO password)
- // https://stackoverflow.com/questions/30212390/laravel-middleware-return-variable-to-controller
- if ($paste->privacy == "private") {
- if ($isSameUser) $privacy = "Private";
- else abort('404');
- }
- elseif ($paste->privacy == "password") {
- $privacy = "Password-protected";
- if ($request->isMethod('post')) {
- if(!Hash::check(Input::get('pastePassword'), $paste->password)) return view('paste/password', ['link' => $paste->link, 'wrongPassword' => true]);
- }
- // Jika pengguna tidak sama dan paste dibuat lebih dari 3 detik yang lalu:
- elseif (!$isSameUser && time() - $paste->created_at->timestamp > 3) return view('paste/password', ['link' => $paste->link]);
- }
- elseif ($paste->privacy == "link") $privacy = "Public";
- else die("Error.");
- // Memeriksa apakah burnAfter harus dihapus (dilakukan setelah pemeriksaan password)
- if (isset($disableBurn)) {
- $paste->burnAfter = 0;
- $paste->save();
- }
- // Menambah angka view
- if (time()-$paste->updated_at->timestamp > 10) $paste->increment('views');
- // Return view
- return view('paste/view', [
- 'username' => ($paste->userId != 0) ? $paste->user->username : "Guest",
- 'views' => $paste->views,
- 'sameUser' => $isSameUser,
- 'link' => $link,
- 'title' => $paste->title,
- 'content' => Crypt::decryptString($paste->content),
- 'expiration' => $expiration,
- 'privacy' => $privacy,
- 'date' => $paste->created_at->format('M jS, Y'),
- 'fulldate' => $paste->created_at->format('d/m/Y - H:i:s'),
- 'noSyntax' => $paste->noSyntax,
- ]);
- }
- public function password($link, Request $request)
- {
- $paste = Paste::where('link', $link)->firstOrFail();
- $messages = array(
- 'pastePassword.required' => 'Please enter a password',
- );
- $this->validate($request, [
- 'pastePassword' => 'required',
- ], $messages);
- if (Hash::check(Input::get('pastePassword'), $paste->password)) {
- Cookie::queue($paste->link, Input::get('pastePassword'), 15);
- return redirect('/'.$link);
- }
- else {
- return view('paste/password', ['link' => $paste->link, 'wrongPassword' => true]);
- }
- }
- public function raw($link)
- {
- header('Content-Type: text/plain');
- $paste = Paste::where('link', $link)->firstOrFail();
- // Apakah user yang terhubung adalah orang yang menulis paste?
- $isSameUser = ((Auth::user() == $paste->user && $paste->userId != 0)) ? true : false;
- if ($paste->expiration != 0) {
- if ($paste->burnAfter == 0) {
- if (time() > strtotime($paste->expiration)) {
- if ($isSameUser) $expiration = "Expired";
- else abort('404');
- }
- else $expiration = Carbon::parse($paste->expiration)->diffForHumans();
- }
- }
- if ($paste->privacy == "private") {
- if ($isSameUser) $privacy = "Private";
- else abort('404');
- }
- elseif ($paste->privacy == "password") {
- $privacy = "Password-protected";
- if ($request->isMethod('post')) {
- if (!Hash::check(Input::get('pastePassword'), $paste->password)) return view('paste/password', ['link' => $paste->link, 'wrongPassword' => true]);
- }
- // Jika pengguna tidak sama dan paste dibuat lebih dari 3 detik yang lalu:
- elseif (!$isSameUser && time() - $paste->created_at->timestamp > 3) return view('paste/password', ['link' => $paste->link]);
- }
- elseif ($paste->privacy == "link") $privacy = "Public";
- else die("Error.");
- // Memeriksa apakah burnAfter harus dihapus (dilakukan setelah pemeriksaan password)
- if (isset($disableBurn)) {
- $paste->burnAfter = 0;
- $paste->save();
- }
- // Menambah angka view
- if (time()-$paste->updated_at->timestamp > 10) $paste->increment('views');
- return response(Crypt::decryptString($paste->content), 200)->header('Content-Type', 'text/plain');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement