/** * Uploads a single file. * * @param Request $request * @param Board $board * @return json */ public function putFile(Request $request, Board $board) { $input = Input::all(); $rules = []; PostRequest::rulesForFiles($board, $rules); $rules['files'][] = "required"; $validator = Validator::make($input, $rules); if (!$validator->passes()) { return json_encode(['errors' => $validator->errors()]); } $storage = new Collection(); foreach ($input['files'] as $file) { $ip = new IP($request->ip()); $uploadSize = (int) Cache::get("upstream_data_for_" . $ip->toLong(), 0); if ($uploadSize <= 52430000) { Cache::increment("upstream_data_for_" . $ip->toLong(), $file->getSize(), 2); $newStorage = FileStorage::storeUpload($file); $storage[$newStorage->hash] = $newStorage; Cache::decrement("upstream_data_for_" . $ip->toLong(), $file->getSize()); } else { return abort(429); } } return $storage; }
/** * Generates a snapshot in the database for the previous hour. * * @param \Carbon\Carbon $carbon A timestamp within the 0-60 minute block that is to be snapshotted. * @return array of new \App\Stats */ public function createStatsSnapshot(\Carbon\Carbon $carbon) { $carbonStart = $carbon->minute(0)->second(0); $carbonEnd = clone $carbonStart; $carbonEnd = $carbonEnd->addHour()->minute(0)->second(0)->subSecond(); $posts = $this->posts()->withTrashed()->where('created_at', '>=', $carbonStart)->where('created_at', '<=', $carbonEnd)->select('post_id', 'author_ip', 'reply_to')->get(); if ($posts->count() === 0) { return collect([]); } // Unique IPs. $authorsUnique = []; // Unique Post IDs. $postsUnique = []; // Unique \16 ranges. $rangesUnique = []; // Unique Thread Post IDs. $threadsUnique = []; foreach ($posts as $post) { $postsUnique[$post->post_id] = true; if (is_null($post->reply_to)) { $threadsUnique[$post->post_id] = false; } if (!is_null($post->author_ip)) { $ip = new IP($post->author_ip); if (!isset($authorsUnique[$ip->toText()])) { $authorsUnique[$ip->toText()] = $ip->toLong(); $range = new IP("{$ip->getStart()}/16"); $rangesUnique[$range->getStart()] = $range->toLong(); } } } // Save uniques $statsRows = []; $uniques = ['authors' => array_values($authorsUnique), 'posts' => array_keys($postsUnique), 'ranges' => array_values($rangesUnique), 'threads' => array_keys($threadsUnique)]; foreach ($uniques as $statsKey => $uniqueValues) { $statsBits = []; foreach ($uniqueValues as $uniqueValue) { $statsBits[] = ['unique' => (int) $uniqueValue]; } $statsRow = $this->stats()->updateOrCreate(['stats_time' => $carbonStart, 'stats_type' => $statsKey], ['counter' => count($statsBits)]); if (!$statsRow->exists) { $statsRow->save(); } $statsRow->uniques()->createMany($statsBits); $statsRows[] = $statsRow; } return collect($statsRows); }
/** * 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; $ip = new IP($this->ip()); $carbon = new \Carbon\Carbon(); $validator = $this->getValidatorInstance(); $messages = $validator->errors(); $isReply = $this->thread instanceof Post; if ($isReply) { $floodTime = site_setting('postFloodTime'); // Check global flood. $nextPostTime = Carbon::createFromTimestamp(Cache::get('last_post_for_' . $ip->toLong(), 0) + $floodTime); if ($nextPostTime->isFuture()) { $timeDiff = $nextPostTime->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. $nextPostTime = Carbon::createFromTimestamp(Cache::get('last_thread_for_' . $ip->toLong(), 0) + $floodTime); if ($nextPostTime->isFuture()) { $timeDiff = $nextPostTime->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("flood", trans("validation.custom.post_flood", ['time_left' => $floodTimer->diffInSeconds()])); $this->failedValidation($validator); return; } } } } // Validate individual files being uploaded right now. $this->validateOriginality(); } if (count($validator->errors())) { $this->failedValidation($validator); } else { if (!$this->passesAuthorization()) { $this->failedAuthorization(); } } }