Exemple #1
0
 /**
  * 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;
 }
Exemple #2
0
 public function post_mod_actions()
 {
     if (!$this->checkCsrfToken()) {
         return $this->response->setData(['error' => _i('The security token was not found. Please try again.')]);
     }
     if (!$this->getAuth()->hasAccess('comment.mod_capcode')) {
         return $this->response->setData(['error' => _i('Access Denied.')])->setStatusCode(403);
     }
     if (!$this->check_board()) {
         return $this->response->setData(['error' => _i('No board was selected.')])->setStatusCode(422);
     }
     if ($this->getPost('action') === 'delete_report') {
         try {
             $this->report_coll->delete($this->getPost('id'));
         } catch (\Foolz\Foolfuuka\Model\ReportException $e) {
             return $this->response->setData(['error' => $e->getMessage()])->setStatusCode(404);
         }
         return $this->response->setData(['success' => _i('The report was deleted.')]);
     }
     if ($this->getPost('action') === 'delete_post') {
         try {
             $comments = Board::forge($this->getContext())->getPost()->setOptions('doc_id', $this->getPost('id'))->setRadix($this->radix)->getComments();
             $comment = current($comments);
             $comment = new Comment($this->getContext(), $comment);
             $comment->delete();
         } catch (\Foolz\Foolfuuka\Model\BoardException $e) {
             return $this->response->setData(['error' => $e->getMessage()])->setStatusCode(404);
         }
         return $this->response->setData(['success' => _i('This post was deleted.')]);
     }
     if ($this->getPost('action') === 'delete_image') {
         try {
             $media = $this->media_factory->getByMediaId($this->radix, $this->getPost('id'));
             $media = new Media($this->getContext(), CommentBulk::forge($this->radix, null, $media));
             $media->delete(true, true, true);
         } catch (\Foolz\Foolfuuka\Model\MediaNotFoundException $e) {
             return $this->response->setData(['error' => $e->getMessage()])->setStatusCode(404);
         }
         return $this->response->setData(['success' => _i('This image was deleted.')]);
     }
     if ($this->getPost('action') === 'ban_image_local' || $this->getPost('action') === 'ban_image_global') {
         $global = false;
         if ($this->getPost('action') === 'ban_image_global') {
             $global = true;
         }
         try {
             $media = $this->media_factory->getByMediaId($this->radix, $this->getPost('id'));
             $media = new Media($this->getContext(), CommentBulk::forge($this->radix, null, $media));
             $media->ban($global);
         } catch (\Foolz\Foolfuuka\Model\MediaNotFoundException $e) {
             return $this->response->setData(['error' => $e->getMessage()])->setStatusCode(404);
         }
         return $this->response->setData(['success' => _i('This image was banned.')]);
     }
     if ($this->getPost('action') === 'ban_user') {
         try {
             $this->ban_factory->add(Inet::ptod($this->getPost('ip')), $this->getPost('reason'), $this->getPost('length'), $this->getPost('board_ban') === 'global' ? array() : array($this->radix->id));
         } catch (\Foolz\Foolfuuka\Model\BanException $e) {
             return $this->response->setData(['error' => $e->getMessage()])->setStatusCode(404);
         }
         return $this->response->setData(['success' => _i('This user was banned.')]);
     }
 }