public function index() { $posts = Post::select()->orderBy('id', 'desc')->get(); $categorias = Categoria::orderBy('titulo')->get(); //dd($posts, $categorias); return view('welcome', compact('categorias', 'posts')); }
public function comments(Request $request) { $comments = Comment::select('comment_body', 'post_id', 'created_at')->orderBy('post_id')->get(); $posts = []; foreach ($comments as $i => $comment) { $associated_posts = Post::select('post_title')->where('id', $comment->post_id)->value('post_title'); $posts[$comment->post_id]["post_title"] = $associated_posts; $posts[$comment->post_id]["comments"][] = ["created_at" => $comment->created_at, "body" => $comment->comment_body]; } return view('admin.comments', ['posts' => $posts]); }
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $forum_groups = App\Forum_group::all(); $forums = App\Forum::all(); foreach ($forum_groups as $group) { $arr = []; foreach ($forums as $forum) { if ($group->id == $forum->forum_group_id) { $arr[] = $forum; } } $group->forums = $arr; foreach ($group->forums as $forum) { $forum->post_count = App\Post::join('threads', 'threads.id', '=', 'posts.thread_id')->join('forums', 'threads.forum_id', '=', 'forums.id')->where('threads.forum_id', '=', $forum->id)->count(); $forum->thread_count = App\Thread::where('forum_id', '=', $forum->id)->count(); $forum->latest = App\Post::select('users.username', 'posts.*', 'threads.title')->join('threads', 'threads.id', '=', 'posts.thread_id')->join('forums', 'threads.forum_id', '=', 'forums.id')->join('users', 'users.id', '=', 'posts.user_id')->where('forums.id', '=', $forum->id)->orderBy('posts.created_at', 'desc')->first(); } } $this->data['forum_groups'] = $forum_groups; return view('forum.index', $this->data); }
public function canPostWithoutCaptcha(PermissionUser $user) { // Check if site requires captchas. if (!site_setting('captchaEnabled')) { return true; } // Check if this user can bypass captchas. if ($user->canPostWithoutCaptcha($this)) { return true; } // Begin to check captchas for last answers. $ip = new IP(); $lastCaptcha = Captcha::select('created_at', 'cracked_at')->where(function ($query) use($ip) { // Find captchas answered by this user. $query->where('client_ip', $ip); // Pull the lifespan of a captcha. // This is the number of minutes between successful entries. $captchaLifespan = (int) site_setting('captchaLifespanTime', 0); if ($captchaLifespan > 0) { $query->whereNotNull('cracked_at'); $query->where('cracked_at', '>=', \Carbon\Carbon::now()->subMinutes($captchaLifespan)); } })->orderBy('cracked_at', 'desc')->first(); $requireCaptcha = !$lastCaptcha instanceof Captcha; if (!$requireCaptcha) { $captchaLifespan = (int) site_setting('captchaLifespanPosts'); if ($captchaLifespan > 0) { $postsWithCaptcha = Post::select('created_at')->where('author_ip', $ip)->where('created_at', '>=', $lastCaptcha->created_at)->count(); $requireCaptcha = $postsWithCaptcha >= $captchaLifespan; } } return !$requireCaptcha; }
public function statistics() { $thread = Thread::select(DB::raw("count(id) as thread_count, sum(views) as views_count"))->first(); $posts = Post::select(DB::raw("count(id) as posts_count, count(image_url) as image_count"))->first(); return response()->json(['stats' => ['thread_count' => $thread->thread_count, 'views_count' => $thread->views_count, 'posts_count' => $posts->posts_count, 'image_count' => $posts->image_count]]); }
/** * Validate the class instance. * This overrides the default invocation to provide additional rules after the controller is setup. * * @return void */ public function validate() { $board = $this->board; $user = $this->user; $validator = $this->getValidatorInstance(); $messages = $validator->errors(); $isReply = $this->thread instanceof Post; if ($isReply) { // Check global flood. $lastPost = Post::select('created_at')->where('author_ip', inet_pton($this->ip()))->where('created_at', '>=', \Carbon\Carbon::now()->subSeconds(5))->first(); if ($lastPost instanceof Post) { $timeDiff = 5 - $lastPost->created_at->diffInSeconds() + 1; $messages = $validator->errors(); $messages->add("flood", trans_choice("validation.custom.post_flood", $timeDiff, ['time_left' => $timeDiff])); $this->failedValidation($validator); return; } } else { // Check global flood. $lastThread = Post::select('created_at')->where('author_ip', inet_pton($this->ip()))->where('created_at', '>=', \Carbon\Carbon::now()->subSeconds(20))->op()->first(); if ($lastThread instanceof Post) { $timeDiff = 20 - $lastThread->created_at->diffInSeconds() + 1; $messages = $validator->errors(); $messages->add("flood", trans_choice("validation.custom.thread_flood", $timeDiff, ['time_left' => $timeDiff])); $this->failedValidation($validator); return; } } // Board-level setting validaiton. $validator->sometimes('captcha', "required|captcha", function ($input) use($board) { return !$board->canPostWithoutCaptcha($this->user); }); if (!$validator->passes()) { $this->failedValidation($validator); } else { if (!$this->user->canAdminConfig() && $board->canPostWithoutCaptcha($this->user)) { // Check last post time for flood. $floodTime = site_setting('postFloodTime'); if ($floodTime > 0) { $lastPost = Post::getLastPostForIP(); if ($lastPost) { $floodTimer = clone $lastPost->created_at; $floodTimer->addSeconds($floodTime); if ($floodTimer->isFuture()) { $messages->add("body", trans("validation.custom.post_flood", ['time_left' => $floodTimer->diffInSeconds()])); } } } } // Validate individual files. $input = $this->all(); // Process uploads. if (isset($input['files'])) { $uploads = $input['files']; if (count($uploads) > 0) { foreach ($uploads as $uploadIndex => $upload) { // If a file is uploaded that has a specific filename, it breaks the process. if (method_exists($upload, "getPathname") && !file_exists($upload->getPathname())) { $messages->add("files.{$uploadIndex}", trans("validation.custom.file_corrupt", ["filename" => $upload->getClientOriginalName()])); } } } } } if (count($validator->errors())) { $this->failedValidation($validator); } else { if (!$this->passesAuthorization()) { $this->failedAuthorization(); } } }
/** * Validate the class instance. * This overrides the default invocation to provide additional rules after the controller is setup. * * @return void */ public function validate() { $board = $this->board; $thread = $this->thread; $user = $this->user; $validator = $this->getValidatorInstance(); $messages = $validator->errors(); $isReply = $this->thread instanceof Post; if ($isReply) { $floodTime = site_setting('postFloodTime'); // Check global flood. $lastPost = Post::select('created_at')->whereAuthorIP($this->ip())->where('created_at', '>=', \Carbon\Carbon::now()->subSeconds($floodTime))->first(); if ($lastPost instanceof Post) { $timeDiff = $floodTime - $lastPost->created_at->diffInSeconds() + 1; $messages->add("flood", trans_choice("validation.custom.post_flood", $timeDiff, ['time_left' => $timeDiff])); $this->failedValidation($validator); return; } } else { $floodTime = site_setting('threadFloodTime'); // Check global flood. $lastThread = Post::select('created_at')->whereAuthorIP($this->ip())->where('created_at', '>=', \Carbon\Carbon::now()->subSeconds($floodTime))->op()->first(); if ($lastThread instanceof Post) { $timeDiff = $floodTime - $lastThread->created_at->diffInSeconds() + 1; $messages->add("flood", trans_choice("validation.custom.thread_flood", $timeDiff, ['time_left' => $timeDiff])); $this->failedValidation($validator); return; } } // Board-level setting validaiton. $validator->sometimes('captcha', "required|captcha", function ($input) use($board) { return !$board->canPostWithoutCaptcha($this->user); }); if (!$validator->passes()) { $this->failedValidation($validator); } else { if (!$this->user->canAdminConfig() && $board->canPostWithoutCaptcha($this->user)) { // Check last post time for flood. $floodTime = site_setting('postFloodTime'); if ($floodTime > 0) { $lastPost = Post::getLastPostForIP(); if ($lastPost) { $floodTimer = clone $lastPost->created_at; $floodTimer->addSeconds($floodTime); if ($floodTimer->isFuture()) { $messages->add("body", trans("validation.custom.post_flood", ['time_left' => $floodTimer->diffInSeconds()])); } } } } // Validate individual files being uploaded right now. $this->validateOriginality(); } if (count($validator->errors())) { $this->failedValidation($validator); } else { if (!$this->passesAuthorization()) { $this->failedAuthorization(); } } }
public function canPostWithoutCaptcha(PermissionUser $user) { if ($user->canPostWithoutCaptcha($this)) { return true; } $lastCaptcha = Captcha::select('created_at', 'cracked_at')->where('client_ip', inet_pton(Request::ip()))->where('created_at', '>=', \Carbon\Carbon::now()->subHour())->whereNotNull('cracked_at')->orderBy('cracked_at', 'desc')->first(); if ($lastCaptcha instanceof Captcha) { $postsWithCaptcha = Post::select('created_at')->where('author_ip', inet_pton(Request::ip()))->where('created_at', '>=', $lastCaptcha->created_at)->count(); return $postsWithCaptcha <= 10; } return false; }
/** * Get posts in chronological order * @param int $n number of posts * @return Collection Posts collection */ public function last($n = 3) { return Post::select('id', 'title', 'text', 'active', 'user_id')->with(['tags' => function ($q) { $q->select('id', 'title'); }])->with(['comments' => function ($q) { $q->active()->select('active', 'post_id'); }])->with(['user' => function ($q) { $q->select('id', 'name', 'email'); }])->orderBy('id', 'desc')->take($n)->get(); }
public function show($id) { return Post::select('*')->with(['user' => function ($q) { $q->select('id', 'name', 'email'); }])->find($id); }