Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.13 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Http\Requests;
  6. use App\Paste;
  7. use App\User;
  8. use Carbon\Carbon;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Cookie;
  12. use Illuminate\Support\Facades\Crypt;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Hash;
  15. use Illuminate\Support\Facades\Input;
  16. use Hashids\Hashids;
  17.  
  18. class PasteController extends Controller
  19. {
  20. public function submit(Requests\StorePaste $request)
  21. {
  22. $title = (empty(trim(Input::get('pasteTitle')))) ? 'Untitled' : Input::get('pasteTitle');
  23.  
  24. $expiration = Input::get('expire');
  25. $privacy = Input::get('privacy');
  26.  
  27. // Memeriksa apakah user sudah memilih Privacy di dropdown list
  28. // $possibleValuesPrivacy = array("link", "password", "private");
  29. // if (in_array($privacy, $possibleValuesPrivacy) == false) return view('welcome');
  30.  
  31. // Jika user memilih ingin dilindungi dengan password, kami memiliki pass-nya, jika tidak akan ditandai 'disabled'
  32. if ($privacy == 'password') $password = bcrypt(Input::get('pastePassword'));
  33. else $password = 'disabled';
  34.  
  35. $burnAfter = 0;
  36. // Menghasilkan timestamp kedaluwarsa
  37. switch ($expiration) {
  38. case 'never':
  39. $timestampExp = 0;
  40. break;
  41. case 'burn':
  42. $timestampExp = date('Y-m-d H:i:s', time());
  43. $burnAfter = 1;
  44. break;
  45. case '10m':
  46. $timestampExp = date('Y-m-d H:i:s', time()+(60*10));
  47. break;
  48. case '1h':
  49. $timestampExp = date('Y-m-d H:i:s', time()+(60*60));
  50. break;
  51. case '1d':
  52. $timestampExp = date('Y-m-d H:i:s', time()+(60*60*24));
  53. break;
  54. case '1w':
  55. $timestampExp = date('Y-m-d H:i:s', time()+(60*60*24*7));
  56. break;
  57. default:
  58. die("User input error.");
  59. break;
  60. }
  61.  
  62. // Pembuatan URL selama URL masih tersedia
  63. // $generatedLink = str_random(10);
  64. // $existingPasteWithGeneratedLink = Paste::where('link', $generatedLink)->first();
  65. // while (!is_null($existingPasteWithGeneratedLink)) {
  66. // $generatedLink = str_random(10);
  67. // $existingPasteWithGeneratedLink = Paste::where('link', $generatedLink)->first();
  68. // }
  69.  
  70. // URL dibuat dari id paste, lalu digenerate ke dalam 16 (huruf kecil & angka)
  71. // https://github.com/ivanakimov/hashids.php
  72. $hashids = new Hashids('', 16, 'abcdefghijklmnopqrstuvwxyz123456789');
  73. $generatedLinkDB = Paste::orderBy('id', 'desc')->limit(1)->firstOrFail();
  74. $generatedLink = $hashids->encode($generatedLinkDB->id + 1);
  75.  
  76. // https://github.com/vinkla/laravel-hashids
  77. // $generatedLinkRaw = Paste::orderBy('id', 'desc')->limit(1)->firstOrFail();
  78. // $generatedLink = Hashids::encode($generatedLinkRaw->id + 1);
  79.  
  80. Paste::create([
  81. 'link' => $generatedLink,
  82. 'userId' => (Auth::check()) ? Auth::id() : 0,
  83. 'views' => '0',
  84. 'title' => $title,
  85. 'content' => Crypt::encryptString(Input::get('pasteContent')),
  86. 'ip' => $request->ip(),
  87. 'expiration' => $timestampExp,
  88. 'privacy' => $privacy,
  89. 'password' => $password,
  90. 'noSyntax' => Input::has('noSyntax'),
  91. 'burnAfter' => $burnAfter,
  92. ]);
  93.  
  94. return redirect('/'.$generatedLink);
  95. }
  96.  
  97.  
  98. public function view($link, Request $request)
  99. {
  100. $paste = Paste::where('link', $link)->firstOrFail();
  101.  
  102. // Apakah user yang terhubung adalah orang yang membuat post paste?
  103. $isSameUser = ((Auth::user() == $paste->user && $paste->userId != 0)) ? true : false;
  104.  
  105. // Paste kadaluarsa
  106. if ($paste->expiration != 0) {
  107. if ($paste->burnAfter == 0) {
  108. if (time() > strtotime($paste->expiration)){
  109. if ($isSameUser) $expiration = "Expired";
  110. else abort('404');
  111. }
  112. else $expiration = Carbon::parse($paste->expiration)->diffForHumans();
  113. }
  114. else {
  115. // Peringatan burn after reading
  116. if (time() - strtotime($paste->expiration) > 3) {
  117. $disableBurn = true;
  118. $expiration = "Burn after reading";
  119. }
  120. else $expiration = "Burn after reading (next time)";
  121. }
  122. }
  123. else {
  124. $expiration = "Never";
  125. }
  126.  
  127. // Mengurus opsi privasi paste (TODO password)
  128. // https://stackoverflow.com/questions/30212390/laravel-middleware-return-variable-to-controller
  129. if ($paste->privacy == "private") {
  130. if ($isSameUser) $privacy = "Private";
  131. else abort('404');
  132. }
  133. elseif ($paste->privacy == "password") {
  134. $privacy = "Password-protected";
  135. if ($request->isMethod('post')) {
  136. if(!Hash::check(Input::get('pastePassword'), $paste->password)) return view('paste/password', ['link' => $paste->link, 'wrongPassword' => true]);
  137. }
  138. // Jika pengguna tidak sama dan paste dibuat lebih dari 3 detik yang lalu:
  139. elseif (!$isSameUser && time() - $paste->created_at->timestamp > 3) return view('paste/password', ['link' => $paste->link]);
  140. }
  141. elseif ($paste->privacy == "link") $privacy = "Public";
  142. else die("Error.");
  143.  
  144. // Memeriksa apakah burnAfter harus dihapus (dilakukan setelah pemeriksaan password)
  145. if (isset($disableBurn)) {
  146. $paste->burnAfter = 0;
  147. $paste->save();
  148. }
  149.  
  150. // Menambah angka view
  151. if (time()-$paste->updated_at->timestamp > 10) $paste->increment('views');
  152.  
  153. // Return view
  154. return view('paste/view', [
  155. 'username' => ($paste->userId != 0) ? $paste->user->username : "Guest",
  156. 'views' => $paste->views,
  157. 'sameUser' => $isSameUser,
  158. 'link' => $link,
  159. 'title' => $paste->title,
  160. 'content' => Crypt::decryptString($paste->content),
  161. 'expiration' => $expiration,
  162. 'privacy' => $privacy,
  163. 'date' => $paste->created_at->format('M jS, Y'),
  164. 'fulldate' => $paste->created_at->format('d/m/Y - H:i:s'),
  165. 'noSyntax' => $paste->noSyntax,
  166. ]);
  167. }
  168.  
  169. public function password($link, Request $request)
  170. {
  171. $paste = Paste::where('link', $link)->firstOrFail();
  172.  
  173. $messages = array(
  174. 'pastePassword.required' => 'Please enter a password',
  175. );
  176.  
  177. $this->validate($request, [
  178. 'pastePassword' => 'required',
  179. ], $messages);
  180.  
  181. if (Hash::check(Input::get('pastePassword'), $paste->password)) {
  182. Cookie::queue($paste->link, Input::get('pastePassword'), 15);
  183. return redirect('/'.$link);
  184. }
  185. else {
  186. return view('paste/password', ['link' => $paste->link, 'wrongPassword' => true]);
  187. }
  188. }
  189.  
  190. public function raw($link)
  191. {
  192. header('Content-Type: text/plain');
  193. $paste = Paste::where('link', $link)->firstOrFail();
  194.  
  195. // Apakah user yang terhubung adalah orang yang menulis paste?
  196. $isSameUser = ((Auth::user() == $paste->user && $paste->userId != 0)) ? true : false;
  197.  
  198. if ($paste->expiration != 0) {
  199. if ($paste->burnAfter == 0) {
  200. if (time() > strtotime($paste->expiration)) {
  201. if ($isSameUser) $expiration = "Expired";
  202. else abort('404');
  203. }
  204. else $expiration = Carbon::parse($paste->expiration)->diffForHumans();
  205. }
  206. }
  207.  
  208. if ($paste->privacy == "private") {
  209. if ($isSameUser) $privacy = "Private";
  210. else abort('404');
  211. }
  212. elseif ($paste->privacy == "password") {
  213. $privacy = "Password-protected";
  214. if ($request->isMethod('post')) {
  215. if (!Hash::check(Input::get('pastePassword'), $paste->password)) return view('paste/password', ['link' => $paste->link, 'wrongPassword' => true]);
  216. }
  217. // Jika pengguna tidak sama dan paste dibuat lebih dari 3 detik yang lalu:
  218. elseif (!$isSameUser && time() - $paste->created_at->timestamp > 3) return view('paste/password', ['link' => $paste->link]);
  219. }
  220. elseif ($paste->privacy == "link") $privacy = "Public";
  221. else die("Error.");
  222.  
  223. // Memeriksa apakah burnAfter harus dihapus (dilakukan setelah pemeriksaan password)
  224. if (isset($disableBurn)) {
  225. $paste->burnAfter = 0;
  226. $paste->save();
  227. }
  228.  
  229. // Menambah angka view
  230. if (time()-$paste->updated_at->timestamp > 10) $paste->increment('views');
  231.  
  232. return response(Crypt::decryptString($paste->content), 200)->header('Content-Type', 'text/plain');
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement