public function saveStat($board_id, $name, $timestamp, $data = '') { $count = $this->dc->qb()->select('COUNT(*) as count')->from($this->dc->p('plugin_fu_board_statistics'), 'bs')->where('board_id = :board_id')->andWhere('name = :name')->setParameter(':board_id', $board_id)->setParameter(':name', $name)->execute()->fetch()['count']; if (!$count) { $this->dc->getConnection()->insert($this->dc->p('plugin_fu_board_statistics'), ['board_id' => $board_id, 'name' => $name, 'timestamp' => $timestamp, 'data' => json_encode($data)]); } else { $this->dc->qb()->update($this->dc->p('plugin_fu_board_statistics'))->where('board_id = :board_id')->andWhere('name = :name')->set('timestamp', ':timestamp')->set('data', ':data')->setParameter(':board_id', $board_id)->setParameter(':name', $name)->setParameter(':timestamp', $timestamp)->setParameter(':data', json_encode($data))->execute(); } }
/** * @param string $username * @param string $password * @param string $email * @param int $group * @param array $profile_fields * @return array|bool * @throws UpdateException */ public function createUser($username, $password, $email, $group = 1, array $profile_fields = array()) { $password = trim($password); $email = filter_var(trim($email), FILTER_VALIDATE_EMAIL); if (empty($username) || empty($password) || empty($email)) { throw new UpdateException('Username, password and email address can\'t be empty.', 1); } $same_users = $this->dc->qb()->select('*')->from($this->dc->p('users'), 'l')->where('l.username = :username')->orWhere('l.email = :email')->setParameter(':username', $username)->setParameter(':email', $email)->execute()->fetchAll(); if (count($same_users) > 0) { if (in_array(strtolower($email), array_map('strtolower', current($same_users)))) { throw new UpdateException('Email address already exists', 2); } else { throw new UpdateException('Username already exists', 3); } } $activated = (bool) $this->preferences->get('foolframe.auth.disable_registration_email'); $activation_key = null; $activation_key_hashed = null; if (!$activated) { // get a string for validation email $activation_key = sha1(uniqid() . time()); $activation_key_hashed = $this->passwordHash($activation_key); if (!$activation_key_hashed) { throw new UpdateException('Issues hashing the activation key', 4); } } $hashed_password = $this->passwordHash($password); if (!$hashed_password) { throw new UpdateException('Issues hashing the password', 4); } $user = array('username' => (string) $username, 'password' => $hashed_password, 'email' => $email, 'group_id' => (int) $group, 'activated' => (int) $activated, 'activation_key' => $activation_key_hashed, 'profile_fields' => serialize($profile_fields), 'created_at' => time()); $result = $this->dc->getConnection()->insert($this->dc->p('users'), $user); return $result ? array($this->dc->getConnection()->lastInsertId($this->dc->p('users_id_seq')), $activation_key) : false; }
/** * Adds a new ban * * @param string $ip_decimal The IP of the banned user in decimal format * @param string $reason The reason for the ban * @param int $length The length of the ban in seconds * @param array $board_ids The array of board IDs, global ban if left empty * * @return \Foolz\Foolfuuka\Model\Ban * @throws \Foolz\Foolfuuka\Model\BanException */ public function add($ip_decimal, $reason, $length, $board_ids = []) { // 0 is a global ban if (empty($board_ids)) { $board_ids = [0]; } else { // check that all ids are existing boards $valid_board_ids = []; foreach ($this->radix_coll->getAll() as $board) { $valid_board_ids[] = $board->id; } foreach ($board_ids as $id) { if (!in_array($id, $valid_board_ids)) { throw new BanException(_i('You entered a non-existent board ID.')); } } } if (!ctype_digit((string) $ip_decimal)) { throw new BanException(_i('You entered an invalid IP.')); } if (mb_strlen($reason, 'utf-8') > 10000) { throw new BanException(_i('You entered a too long reason for the ban.')); } if (!ctype_digit($length)) { throw new BanException(_i('You entered an invalid length for the ban.')); } $time = time(); $objects = []; try { $old = $this->getByIp($ip_decimal); } catch (BanNotFoundException $e) { $old = false; } foreach ($board_ids as $board_id) { $new = new Ban($this->getContext()); $new->ip = $ip_decimal; $new->reason = $reason; $new->start = $time; $new->length = $length; $new->board_id = $board_id; $new->creator_id = $this->getAuth()->getUser()->getId(); $new->appeal = ''; if (isset($old[$new->board_id])) { if ($new->length < $old[$new->board_id]->length) { $new->length = $old[$new->board_id]->length; } $this->dc->qb()->update($this->dc->p('banned_posters'))->where('id = :id')->set('start', $new->start)->set('length', $new->length)->set('creator_id', $new->creator_id)->setParameter(':id', $old[$new->board_id]->id)->execute(); } else { $this->dc->getConnection()->insert($this->dc->p('banned_posters'), ['ip' => $new->ip, 'reason' => $new->reason, 'start' => $new->start, 'length' => $new->length, 'board_id' => $new->board_id, 'creator_id' => $new->creator_id, 'appeal' => $new->appeal]); } $objects[] = $new; } return $objects; }
/** * Remove the tables associated to the Radix */ public function removeTables() { $tables = ['', '_deleted', '_images', '_threads', '_users', '_daily']; $sm = $this->dc->getConnection()->getSchemaManager(); $schema = $sm->createSchema(); foreach ($tables as $table) { $schema->dropTable($this->getTable($table)); } foreach ($schema->getMigrateFromSql($sm->createSchema(), $sm->getDatabasePlatform()) as $query) { $this->dc->getConnection()->query($query); } }
public function install_modules() { $this->config->addPackage('unknown', ASSETSPATH); $class_name = $this->config->get('unknown', 'package', 'main.class_name'); $name_lowercase = strtolower($class_name); $modules = ['foolframe' => ['context' => '\\Foolz\\FoolFrame\\Model\\Context', 'namespace' => 'foolz/foolframe'], $name_lowercase => ['context' => $this->config->get('unknown', 'package', 'main.class_context'), 'namespace' => 'foolz/' . $name_lowercase]]; $dc = new DoctrineConnection($this->getContext(), $this->config); $sm = SchemaManager::forge($dc->getConnection(), $dc->getPrefix()); Schema::load($this->getContext(), $sm); $schema_class = '\\Foolz\\' . $class_name . '\\Model\\Schema'; $schema_class::load($this->getContext(), $sm); $sm->commit(); $this->config->set('foolz/foolframe', 'config', 'modules.installed', $modules); $this->config->set('foolz/foolframe', 'config', 'install.installed', true); $this->config->save('foolz/foolframe', 'config'); }
public function set($setting, $value, $reload = true) { // if array, serialize value if (is_array($value)) { $value = serialize($value); } $count = $this->dc->qb()->select('COUNT(*) as count')->from($this->dc->p('preferences'), 'p')->where('p.name = :name')->setParameter(':name', $setting)->execute()->fetch()['count']; if ($count > 0) { $this->dc->qb()->update($this->dc->p('preferences'))->set('value', ':value')->where('name = :name')->setParameters([':value' => $value, ':name' => $setting])->execute(); } else { $this->dc->getConnection()->insert($this->dc->p('preferences'), ['name' => $setting, 'value' => $value]); } if ($reload) { return $this->load(true); } return $this->preferences; }
/** * Insert custom preferences. One must use this for 'internal' preferences * * @param \Foolz\Foolfuuka\Model\Radix|int $board_id can also be the board object * @param string $name The name of the value to insert * @param mixed $value The value to insert */ public function savePreferences($board_id, $name, $value) { if (is_object($board_id)) { $board_id = $board_id->id; } $result = $this->dc->qb()->select('COUNT(*) as count')->from($this->dc->p('boards_preferences'), 'p')->where('board_id = :board_id', 'name = :name')->setParameter(':board_id', $board_id)->setParameter(':name', $name)->execute()->fetch(); if ($result['count']) { $this->dc->qb()->update($this->dc->p('boards_preferences'))->set('value', $this->dc->getConnection()->quote($value))->where('board_id = :board_id', 'name = :name')->setParameter(':board_id', $board_id)->setParameter(':name', $name)->execute(); } else { $this->dc->getConnection()->insert($this->dc->p('boards_preferences'), ['board_id' => $board_id, 'name' => $name, 'value' => $value]); } // only set if object exists if (isset($this->preloaded_radixes[$board_id])) { // avoid the complete reloading $this->preloaded_radixes[$board_id]->{$name} = $value; } $this->clearCache(); }
public function install($slug) { $plugin = $this->loader->get($slug); $plugin->install(); $this->dc->getConnection()->insert($this->dc->p('plugins'), ['slug' => $slug, 'enabled' => 1]); $this->clearCache(); // run the schema update $sm = \Foolz\FoolFrame\Model\SchemaManager::forge($this->dc->getConnection(), $this->dc->getPrefix() . 'plugin_'); foreach ($this->getInstalled() as $enabled) { try { $plug = $this->loader->get($enabled['slug']); if (!$plug->isBootstrapped()) { $plug->bootstrap(); } \Foolz\Plugin\Hook::forge('Foolz\\FoolFrame\\Model\\Plugin::install#' . $plug->getConfig('name'))->setParam('context', $this->getContext())->setParam('schema', $sm->getCodedSchema())->execute(); } catch (\OutOfBoundsException $e) { } } $sm->commit(); $this->clearCache(); }
/** * Adds a new report to the database * * @param \Foolz\FoolFuuka\Model\Radix $radix The Radix to which the Report is referred to * @param int $id The ID of the object being reported (doc_id or media_id) * @param string $reason The reason for the report * @param string $ip_reporter The IP in decimal format * @param string $mode The type of column (doc_id or media_id) * * @return \Foolz\FoolFuuka\Model\Report The created report * @throws ReportMediaNotFoundException If the reported media_id doesn't exist * @throws ReportCommentNotFoundException If the reported doc_id doesn't exist * @throws ReportReasonTooLongException If the reason inserted was too long * @throws ReportSentTooManyException If the user sent too many moderation in a timeframe * @throws ReportReasonNullException If the report reason is null * @throws ReportAlreadySubmittedException If the reporter’s IP has already submitted a report for the post. * @throws ReportSubmitterBannedException If the reporter’s IP has been banned. */ public function p_add($radix, $id, $reason, $ip_reporter, $mode = 'doc_id') { if (trim($reason) === '') { throw new ReportReasonNullException(_i('A reason must be included with your report.')); } if (mb_strlen($reason, 'utf-8') > 2048) { throw new ReportReasonTooLongException(_i('The reason for you report was too long.')); } $report = new Report($this->getContext()); $report->radix = $radix; $report->board_id = $radix->id; $report->reason = $reason; $report->ip_reporter = $ip_reporter; switch ($mode) { case 'media_id': try { $this->media_factory->getByMediaId($report->radix, $id); $report->media_id = (int) $id; } catch (MediaNotFoundException $e) { throw new ReportMediaNotFoundException(_i('The media file you are reporting could not be found.')); } break; default: try { Board::forge($this->getContext())->getPost()->setRadix($report->radix)->setOptions('doc_id', $id)->getComments(); $report->doc_id = (int) $id; } catch (BoardException $e) { throw new ReportCommentNotFoundException(_i('The post you are reporting could not be found.')); } } $report = $this->validateUserReport($report); $report->created = time(); $this->dc->getConnection()->insert($this->dc->p('reports'), ['board_id' => $report->board_id, 'doc_id' => $report->doc_id, 'media_id' => $report->media_id, 'reason' => $report->reason, 'ip_reporter' => $report->ip_reporter, 'created' => $report->created]); $this->clearCache(); return $report; }
/** * Gets a thread * * @return \Foolz\Foolslide\Model\Board The current object * @throws BoardThreadNotFoundException If the thread wasn't found */ protected function p_getThreadComments() { $this->profiler->log('Board::getThreadComments Start'); extract($this->options); // determine type switch ($type) { case 'from_doc_id': $query_result = $this->dc->qb()->select('*')->from($this->radix->getTable(), 'r')->leftJoin('r', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')->where('thread_num = :thread_num')->andWhere('doc_id > :latest_doc_id')->orderBy('num', 'ASC')->addOrderBy('subnum', 'ASC')->setParameter(':thread_num', $num)->setParameter(':latest_doc_id', $latest_doc_id)->execute()->fetchAll(); break; case 'ghosts': $query_result = $this->dc->qb()->select('*')->from($this->radix->getTable(), 'r')->leftJoin('r', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')->where('thread_num = :thread_num')->where('subnum <> 0')->orderBy('num', 'ASC')->addOrderBy('subnum', 'ASC')->setParameter(':thread_num', $num)->execute()->fetchAll(); break; case 'last_x': try { // we save some cache memory by only saving last_50, so it must always fail otherwise if ($last_limit != 50) { throw new \OutOfBoundsException(); } $query_result = Cache::item('foolslide.model.board.getThreadComments.last_50.' . md5(serialize([$this->radix->shortname, $num])))->get(); } catch (\OutOfBoundsException $e) { $subquery_first = $this->dc->qb()->select('*')->from($this->radix->getTable(), 'xr')->where('num = ' . $this->dc->getConnection()->quote($num))->setMaxResults(1)->getSQL(); $subquery_last = $this->dc->qb()->select('*')->from($this->radix->getTable(), 'xrr')->where('thread_num = ' . $this->dc->getConnection()->quote($num))->orderBy('num', 'DESC')->addOrderBy('subnum', 'DESC')->setMaxResults($last_limit)->getSQL(); $query_result = $this->dc->qb()->select('*')->from('((' . $subquery_first . ') UNION (' . $subquery_last . '))', 'r')->leftJoin('r', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')->orderBy('num', 'ASC')->addOrderBy('subnum', 'ASC')->execute()->fetchAll(); // cache only if it's last_50 if ($last_limit == 50) { $cache_time = 300; if ($this->radix->archive) { $cache_time = 30; // over 7 days is old $old = time() - 604800; // set a very long cache time for archive threads older than a week, in case a ghost post will bump it foreach ($query_result as $k => $r) { if ($r['timestamp'] < $old) { $cache_time = 300; break; } } } Cache::item('foolslide.model.board.getThreadComments.last_50.' . md5(serialize([$this->radix->shortname, $num])))->set($query_result, $cache_time); } } break; case 'thread': try { $query_result = Cache::item('foolslide.model.board.getThreadComments.thread.' . md5(serialize([$this->radix->shortname, $num])))->get(); } catch (\OutOfBoundsException $e) { $query_result = $this->dc->qb()->select('*')->from($this->radix->getTable(), 'r')->leftJoin('r', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')->where('thread_num = :thread_num')->orderBy('num', 'ASC')->addOrderBy('subnum', 'ASC')->setParameter(':thread_num', $num)->execute()->fetchAll(); $cache_time = 300; if ($this->radix->archive) { $cache_time = 30; // over 7 days is old $old = time() - 604800; // set a very long cache time for archive threads older than a week, in case a ghost post will bump it foreach ($query_result as $k => $r) { if ($r['timestamp'] < $old) { $cache_time = 300; break; } } } Cache::item('foolslide.model.board.getThreadComments.thread.' . md5(serialize([$this->radix->shortname, $num])))->set($query_result, $cache_time); } break; } if (!count($query_result) && isset($latest_doc_id)) { return $this->comments = $this->comments_unsorted = []; } if (!count($query_result)) { throw new BoardThreadNotFoundException(_i('There\'s no such a thread.')); } foreach ($query_result as $key => $row) { $data = new CommentBulk(); $data->import($row, $this->radix); unset($query_result[$key]); $this->comments_unsorted[] = $data; } unset($query_result); foreach ($this->comments_unsorted as $key => $bulk) { if ($bulk->comment->op == 0) { $this->comments[$bulk->comment->thread_num]['posts'][$bulk->comment->num . ($bulk->comment->subnum == 0 ? '' : '_' . $bulk->comment->subnum)] =& $this->comments_unsorted[$key]; } else { $this->comments[$bulk->comment->num]['op'] =& $this->comments_unsorted[$key]; } } $this->profiler->logMem('Board $this->comments', $this->comments); $this->profiler->logMem('Board $this', $this); $this->profiler->log('Board::getThreadComments End'); return $this; }
/** * Delete the post and eventually the entire thread if it's OP * Also deletes the images when it's the only post with that image * * @param null $password * @param bool $force * @param bool $thread * @throws CommentSendingDatabaseException * @throws CommentDeleteWrongPassException * @return array|bool */ protected function p_delete($password = null, $force = false, $thread = false) { if (!$this->getAuth()->hasAccess('comment.passwordless_deletion') && $force !== true) { if (!password_verify($password, $this->comment->getDelpass())) { throw new CommentDeleteWrongPassException(_i('You did not provide the correct deletion password.')); } } try { $this->dc->getConnection()->beginTransaction(); // check that the post isn't already in deleted $has_deleted = $this->dc->qb()->select('COUNT(*) as found')->from($this->radix->getTable('_deleted'), 'd')->where('doc_id = :doc_id')->setParameter(':doc_id', $this->comment->doc_id)->execute()->fetch(); if (!$has_deleted['found']) { // throw into _deleted table $this->dc->getConnection()->executeUpdate('INSERT INTO ' . $this->radix->getTable('_deleted') . ' ' . $this->dc->qb()->select('*')->from($this->radix->getTable(), 't')->where('doc_id = ' . $this->dc->getConnection()->quote($this->comment->doc_id))->getSQL()); } // delete post $this->dc->qb()->delete($this->radix->getTable())->where('doc_id = :doc_id')->setParameter(':doc_id', $this->comment->doc_id)->execute(); // purge reports $this->dc->qb()->delete($this->dc->p('reports'))->where('board_id = :board_id')->andWhere('doc_id = :doc_id')->setParameter(':board_id', $this->radix->id)->setParameter(':doc_id', $this->comment->doc_id)->execute(); // clear cache $this->radix_coll->clearCache(); // remove image file if (isset($this->media)) { $media_sql = $this->dc->qb()->select('COUNT(*)')->from($this->radix->getTable(), 't')->where('media_id = :media_id')->setParameter(':media_id', $this->media->media_id)->getSQL(); $this->dc->qb()->update($this->radix->getTable('_images'))->set('total', '(' . $media_sql . ')')->where('media_id = :media_id')->setParameter(':media_id', $this->media->media_id)->execute(); $has_image = $this->dc->qb()->select('total')->from($this->radix->getTable('_images'), 'ti')->where('media_id = :media_id')->setParameter(':media_id', $this->media->media_id)->execute()->fetch(); if (!$has_image || !$has_image['total']) { $media = new Media($this->getContext(), $this->bulk); $media->delete(); } } // if this is OP, delete replies too if ($this->comment->op) { // delete thread data $this->dc->qb()->delete($this->radix->getTable('_threads'))->where('thread_num = :thread_num')->setParameter(':thread_num', $this->comment->thread_num)->execute(); // process each comment $comments = $this->dc->qb()->select('doc_id')->from($this->radix->getTable(), 'b')->where('thread_num = :thread_num')->setParameter(':thread_num', $this->comment->thread_num)->execute()->fetchAll(); foreach ($comments as $comment) { $post = Board::forge($this->getContext())->getPost()->setOptions('doc_id', $comment['doc_id'])->setRadix($this->radix)->getComments(); $post = current($post); $post = new Comment($this->getContext(), $post); $post->delete(null, true, true); } } else { // if this is not triggered by a thread deletion, update the thread table if ($thread === false && !$this->radix->archive) { $time_last = ' ( COALESCE(GREATEST( time_op, ( SELECT MAX(timestamp) FROM ' . $this->radix->getTable() . ' xr WHERE thread_num = ' . $this->dc->getConnection()->quote($this->comment->thread_num) . ' AND subnum = 0 ) ), time_op) )'; $time_bump = ' ( COALESCE(GREATEST( time_op, ( SELECT MAX(timestamp) FROM ' . $this->radix->getTable() . ' xr WHERE thread_num = ' . $this->dc->getConnection()->quote($this->comment->thread_num) . ' AND subnum = 0 AND (email <> \'sage\' OR email IS NULL) ) ), time_op) )'; $time_ghost = ' ( SELECT MAX(timestamp) FROM ' . $this->radix->getTable() . ' xr WHERE thread_num = ' . $this->dc->getConnection()->quote($this->comment->thread_num) . ' AND subnum <> 0 )'; $time_ghost_bump = ' ( SELECT MAX(timestamp) FROM ' . $this->radix->getTable() . ' xr WHERE thread_num = ' . $this->dc->getConnection()->quote($this->comment->thread_num) . ' AND subnum <> 0 AND (email <> \'sage\' OR email IS NULL) )'; // update thread information $this->dc->qb()->update($this->radix->getTable('_threads'))->set('time_last', $time_last)->set('time_bump', $time_bump)->set('time_ghost', $time_ghost)->set('time_ghost_bump', $time_ghost_bump)->set('time_last_modified', ':time')->set('nreplies', 'nreplies - 1')->set('nimages', $this->media === null ? 'nimages' : 'nimages - 1')->where('thread_num = :thread_num')->setParameter(':time', $this->getRadixTime())->setParameter(':thread_num', $this->comment->thread_num)->execute(); } } $this->dc->getConnection()->commit(); $this->clearCache(); if ($thread === false) { $this->audit->log(Audit::AUDIT_DEL_POST, ['radix' => $this->radix->id, 'doc_id' => $this->comment->doc_id, 'thread_num' => $this->comment->thread_num, 'num' => $this->comment->num, 'subnum' => $this->comment->subnum]); } } catch (\Doctrine\DBAL\DBALException $e) { $this->logger->error('\\Foolz\\FoolFuuka\\Model\\CommentInsert: ' . $e->getMessage()); $this->dc->getConnection()->rollBack(); throw new CommentSendingDatabaseException(_i('Something went wrong when deleting the post in the database. Try again.')); } return $this; }
/** * Inserts the media uploaded (static::forgeFromUpload()) * * @param string $microtime The time in microseconds to use for the local filename * @param boolean $is_op True if the thumbnail sizes should be for OP, false if they should be for a reply * * @return \Foolz\Foolfuuka\Model\Media The current object updated with the insert data * @throws MediaInsertInvalidFormatException In case the media is not a valid format * @throws MediaInsertDomainException In case the media uploaded is in example too small or validations don't pass * @throws MediaInsertRepostException In case the media has been reposted too recently according to control panel settings * @throws MediaThumbnailCreationException If the thumbnail fails to generate */ public function p_insert($microtime, $is_op) { $file = $this->temp_file; $this->op = $is_op; $data = \Foolz\Plugin\Hook::forge('Foolz\\Foolfuuka\\Model\\Media::insert.result.media_data')->setParams(['dimensions' => getimagesize($file->getPathname()), 'file' => $file, 'name' => $file->getClientOriginalName(), 'path' => $file->getPathname(), 'hash' => base64_encode(pack("H*", md5(file_get_contents($file->getPathname())))), 'size' => $file->getClientSize(), 'time' => $microtime, 'media_orig' => $microtime . '.' . strtolower($file->getClientOriginalExtension()), 'preview_orig' => $microtime . 's.' . strtolower($file->getClientOriginalExtension())])->execute()->getParams(); if (!($getimagesize = $data['dimensions'])) { throw new MediaInsertInvalidFormatException(_i('The file you uploaded is not allowed.')); } // if width and height are lower than 25 reject the image if ($getimagesize[0] < 25 || $getimagesize[1] < 25) { throw new MediaInsertDomainException(_i('The file you uploaded is too small.')); } if ($getimagesize[0] > $this->radix->getValue('max_image_size_width') || $getimagesize[1] > $this->radix->getValue('max_image_size_height')) { throw new MediaInsertDomainException(_i('The dimensions of the file you uploaded is too large.')); } $this->media->media_w = $getimagesize[0]; $this->media->media_h = $getimagesize[1]; $this->media->media_filename = $data['name']; $this->media->media_hash = $data['hash']; $this->media->media_size = $data['size']; $this->media->media_orig = $data['media_orig']; $this->media->preview_orig = $data['preview_orig']; $do_thumb = true; $do_full = true; try { $duplicate = CommentBulk::forge($this->radix, null, $this->media_factory->getByMediaHash($this->radix, $this->media->media_hash)); $duplicate = new Media($this->getContext(), $duplicate); // we want the current media to work with the same filenames as previously stored $this->media->media_id = $duplicate->media->media_id; $this->media->media = $duplicate->media->media; $this->media->media_orig = $duplicate->media->media; $this->media->preview_op = $duplicate->media->preview_op; $this->media->preview_reply = $duplicate->media->preview_reply; if (!$this->getAuth()->hasAccess('comment.limitless_comment') && $this->radix->getValue('min_image_repost_time')) { // if it's -1 it means that image reposting is disabled, so this image shouldn't pass if ($this->radix->getValue('min_image_repost_time') == -1) { throw new MediaInsertRepostException(_i('This image has already been posted once. This board doesn\'t allow image reposting.')); } // we don't have to worry about archives with weird timestamps, we can't post images there $duplicate_entry = $this->dc->qb()->select('COUNT(*) as count, MAX(timestamp) as max_timestamp')->from($this->radix->getTable(), 'r')->where('media_id = :media_id')->andWhere('timestamp > :timestamp')->setParameter('media_id', $duplicate->media->media_id)->setParameter('timestamp', time() - $this->radix->getValue('min_image_repost_time'))->setMaxResults(1)->execute()->fetch(); if ($duplicate_entry['count']) { $datetime = new \DateTime(date('Y-m-d H:i:s', $duplicate_entry['max_timestamp'] + $this->radix->getValue('min_image_repost_time'))); $remain = $datetime->diff(new \DateTime()); throw new MediaInsertRepostException(_i('This image has been posted recently. You will be able to post it again in %s.', ($remain->d > 0 ? $remain->d . ' ' . _i('day(s)') : '') . ' ' . ($remain->h > 0 ? $remain->h . ' ' . _i('hour(s)') : '') . ' ' . ($remain->i > 0 ? $remain->i . ' ' . _i('minute(s)') : '') . ' ' . ($remain->s > 0 ? $remain->s . ' ' . _i('second(s)') : ''))); } } // if we're here, we got the media $duplicate_dir = $duplicate->getDir(); if ($duplicate_dir !== null && file_exists($duplicate_dir)) { $do_full = false; } $duplicate->op = $is_op; $duplicate_dir_thumb = $duplicate->getDir(true, true); if ($duplicate_dir_thumb !== null && file_exists($duplicate_dir_thumb)) { $duplicate_dir_thumb_size = getimagesize($duplicate_dir_thumb); $this->media->preview_w = $duplicate_dir_thumb_size[0]; $this->media->preview_h = $duplicate_dir_thumb_size[1]; $do_thumb = false; } } catch (MediaNotFoundException $e) { } if ($do_thumb) { $thumb_width = $this->radix->getValue('thumbnail_reply_width'); $thumb_height = $this->radix->getValue('thumbnail_reply_height'); if ($is_op) { $thumb_width = $this->radix->getValue('thumbnail_op_width'); $thumb_height = $this->radix->getValue('thumbnail_op_height'); } if (!file_exists($this->pathFromFilename(true, $is_op))) { mkdir($this->pathFromFilename(true, $is_op), 0777, true); } $return = \Foolz\Plugin\Hook::forge('Foolz\\Foolfuuka\\Model\\Media::insert.result.create_thumbnail')->setObject($this)->setParams(['thumb_width' => $thumb_width, 'thumb_height' => $thumb_height, 'exec' => str_replace(' ', '\\ ', $this->preferences->get('foolframe.imagick.convert_path')), 'is_op' => $is_op, 'media' => $file, 'thumb' => $this->pathFromFilename(true, $is_op, true)])->execute()->get(); if ($return instanceof \Foolz\Plugin\Void) { if ($this->radix->getValue('enable_animated_gif_thumbs') && strtolower($file->getClientOriginalExtension()) === 'gif') { exec(str_replace(' ', '\\ ', $this->preferences->get('foolframe.imagick.convert_path')) . " " . $data['path'] . " -coalesce -treedepth 4 -colors 256 -quality 80 -background none " . "-resize \"" . $thumb_width . "x" . $thumb_height . ">\" " . $this->pathFromFilename(true, $is_op, true)); } else { exec(str_replace(' ', '\\ ', $this->preferences->get('foolframe.imagick.convert_path')) . " " . $data['path'] . "[0] -quality 80 -background none " . "-resize \"" . $thumb_width . "x" . $thumb_height . ">\" " . $this->pathFromFilename(true, $is_op, true)); } } if (!file_exists($this->pathFromFilename(true, $is_op, true))) { throw new MediaThumbnailCreationException(_i('The thumbnail failed to generate.')); } $thumb_getimagesize = getimagesize($this->pathFromFilename(true, $is_op, true)); $this->media->preview_w = $thumb_getimagesize[0]; $this->media->preview_h = $thumb_getimagesize[1]; if ($do_full) { if (!file_exists($this->pathFromFilename())) { mkdir($this->pathFromFilename(), 0777, true); } copy($data['path'], $this->pathFromFilename(false, false, true)); } } if (function_exists('exif_read_data') && in_array(strtolower($file->getClientOriginalExtension()), ['jpg', 'jpeg', 'tiff'])) { $exif_data = null; getimagesize($data['path'], $exif_data); if (!isset($exif_data['APP1']) || strpos($exif_data['APP1'], 'Exif') === 0) { $exif = exif_read_data($data['path']); if ($exif !== false) { $this->media->exif = $exif; } } } if (!$this->media->media_id) { $this->dc->getConnection()->insert($this->radix->getTable('_images'), ['media_hash' => $this->media->media_hash, 'media' => $this->media->media_orig, 'preview_op' => $this->op ? $this->media->preview_orig : null, 'preview_reply' => !$this->op ? $this->media->preview_orig : null, 'total' => 1, 'banned' => 0]); $this->media->media_id = $this->dc->getConnection()->lastInsertId($this->radix->getTable('_images_media_id_seq')); } else { $media_sql = $this->dc->qb()->select('COUNT(*)')->from($this->radix->getTable(), 't')->where('media_id = :media_id')->setParameter(':media_id', $this->media->media_id)->getSQL(); $query = $this->dc->qb()->update($this->radix->getTable('_images')); if ($this->media === null) { $query->set('media', ':media_orig')->setParameter(':media_orig', $this->media->preview_orig); } if ($this->op && $this->media->preview_op === null) { $query->set('preview_op', ':preview_orig')->setParameter(':preview_orig', $this->media->preview_orig); } if (!$this->op && $this->media->preview_reply === null) { $query->set('preview_reply', ':preview_orig')->setParameter(':preview_orig', $this->media->preview_orig); } $query->set('total', '(' . $media_sql . ')'); $query->where('media_id = :media_id')->setParameter(':media_id', $this->media->media_id)->execute(); } return $this; }
public function log($type, $data) { $this->dc->getConnection()->insert($this->dc->p('audit_log'), ['timestamp' => time(), 'user' => $this->getAuth()->getUser()->getId(), 'type' => $type, 'data' => json_encode($data)]); }
/** * Adds a new report to the database * * @param \Foolz\Foolslide\Model\Radix $radix The Radix to which the Report is referred to * @param int $id The ID of the object being reported (doc_id or media_id) * @param string $reason The reason for the report * @param string $ip_reporter The IP in decimal format * @param string $mode The type of column (doc_id or media_id) * * @return \Foolz\Foolslide\Model\Report The created report * @throws ReportMediaNotFoundException If the reported media_id doesn't exist * @throws ReportCommentNotFoundException If the reported doc_id doesn't exist * @throws ReportReasonTooLongException If the reason inserted was too long * @throws ReportSentTooManyException If the user sent too many moderation in a timeframe * @throws ReportReasonNullException If the report reason is null * @throws ReportAlreadySubmittedException If the reporter’s IP has already submitted a report for the post. * @throws ReportSubmitterBannedException If the reporter’s IP has been banned. */ public function p_add($radix, $id, $reason, $ip_reporter, $mode = 'doc_id') { $new = new Report($this->getContext()); $new->radix = $radix; $new->board_id = $radix->id; if ($mode === 'media_id') { try { $this->media_factory->getByMediaId($new->radix, $id); } catch (MediaNotFoundException $e) { throw new ReportMediaNotFoundException(_i('The media file you are reporting could not be found.')); } $new->media_id = (int) $id; } else { try { Board::forge($this->getContext())->getPost()->setRadix($new->radix)->setOptions('doc_id', $id)->getComments(); } catch (BoardException $e) { throw new ReportCommentNotFoundException(_i('The post you are reporting could not be found.')); } $new->doc_id = (int) $id; } if (trim($reason) === null) { throw new ReportReasonNullException(_i('A reason must be included with your report.')); } if (mb_strlen($reason, 'utf-8') > 2048) { throw new ReportReasonTooLongException(_i('The reason for you report was too long.')); } $new->reason = $reason; $new->ip_reporter = $ip_reporter; // check how many moderation have been sent in the last hour to prevent spam $row = $this->dc->qb()->select('COUNT(*) as count')->from($this->dc->p('reports'), 'r')->where('created > :time')->andWhere('ip_reporter = :ip_reporter')->setParameter(':time', time() - 86400)->setParameter(':ip_reporter', $new->ip_reporter)->execute()->fetch(); if ($row['count'] > 25) { throw new ReportSentTooManyException(_i('You have submitted too many reports within an hour.')); } $reported = $this->dc->qb()->select('COUNT(*) as count')->from($this->dc->p('reports'), 'r')->where('board_id = :board_id')->andWhere('ip_reporter = :ip_reporter')->andWhere('doc_id = :doc_id')->setParameters([':board_id' => $new->board_id, ':doc_id' => $new->doc_id, ':ip_reporter' => $new->ip_reporter])->execute()->fetch(); if ($reported['count'] > 0) { throw new ReportSubmitterBannedException(_i('You can only submit one report per post.')); } if ($ban = $this->ban_factory->isBanned($new->ip_reporter, $new->radix)) { if ($ban->board_id == 0) { $banned_string = _i('It looks like you were banned on all boards.'); } else { $banned_string = _i('It looks like you were banned on /' . $new->radix->shortname . '/.'); } if ($ban->length) { $banned_string .= ' ' . _i('This ban will last until:') . ' ' . date(DATE_COOKIE, $ban->start + $ban->length) . '.'; } else { $banned_string .= ' ' . _i('This ban will last forever.'); } if ($ban->reason) { $banned_string .= ' ' . _i('The reason for this ban is:') . ' «' . $ban->reason . '».'; } if ($ban->appeal_status == Ban::APPEAL_NONE) { $banned_string .= ' ' . _i('If you\'d like to appeal to your ban, go to the :appeal page.', '<a href="' . $this->uri->create($new->radix->shortname . '/appeal') . '">' . _i('appeal') . '</a>'); } elseif ($ban->appeal_status == Ban::APPEAL_PENDING) { $banned_string .= ' ' . _i('Your appeal is pending.'); } throw new ReportSubmitterBannedException($banned_string); } $new->created = time(); $this->dc->getConnection()->insert($this->dc->p('reports'), ['board_id' => $new->board_id, 'doc_id' => $new->doc_id, 'media_id' => $new->media_id, 'reason' => $new->reason, 'ip_reporter' => $new->ip_reporter, 'created' => $new->created]); $this->clearCache(); return $new; }
public function action_database_setup() { if ($this->getPost()) { $validator = new Validator(); $validator->add('hostname', _i('Database Hostname'), [new Trim(), new Assert\NotBlank()])->add('prefix', _i('Table Prefix'), [new Trim()])->add('username', _i('Username'), [new Trim(), new Assert\NotBlank()])->add('database', _i('Database name'), [new Trim(), new Assert\NotBlank()]); $validator->validate($this->getPost()); if (!$validator->getViolations()->count()) { $input = $validator->getFinalValues(); $input['password'] = $this->getPost('password'); $input['type'] = $this->getPost('type'); if ($this->install->check_database($input)) { $this->install->setup_database($input); $dc = new DoctrineConnection($this->getContext(), $this->config); $sm = SchemaManager::forge($dc->getConnection(), $dc->getPrefix()); Schema::load($this->getContext(), $sm); $sm->commit(); $this->install->create_salts(); return new RedirectResponse($this->uri->create('install/create_admin')); } else { $this->notices->set('warning', _i('Connection to specified database failed. Please check your connection details again.')); } } else { $this->notices->set('warning', $validator->getViolations()->getText()); } } $this->process('database_setup'); $this->param_manager->setParam('method_title', _i('Database Setup')); $this->builder->createPartial('body', 'install/database_setup'); return new Response($this->builder->build()); }