AsgardCms /
Media
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php namespace Modules\Media\Image; |
||
| 2 | |||
| 3 | use Illuminate\Contracts\Config\Repository; |
||
| 4 | use Illuminate\Support\Facades\App; |
||
| 5 | use Modules\Media\Entities\File; |
||
| 6 | |||
| 7 | class Imagy |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var \Intervention\Image\Image |
||
| 11 | */ |
||
| 12 | private $image; |
||
| 13 | /** |
||
| 14 | * @var \Illuminate\Filesystem\Filesystem |
||
| 15 | */ |
||
| 16 | private $finder; |
||
| 17 | /** |
||
| 18 | * @var ImageFactoryInterface |
||
| 19 | */ |
||
| 20 | private $imageFactory; |
||
| 21 | /** |
||
| 22 | * @var ThumbnailsManager |
||
| 23 | */ |
||
| 24 | private $manager; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * All the different images types where thumbnails should be created |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif']; |
||
| 31 | /** |
||
| 32 | * @var Repository |
||
| 33 | */ |
||
| 34 | private $config; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param ImageFactoryInterface $imageFactory |
||
| 38 | * @param ThumbnailsManager $manager |
||
| 39 | * @param Repository $config |
||
| 40 | */ |
||
| 41 | public function __construct(ImageFactoryInterface $imageFactory, ThumbnailsManager $manager, Repository $config) |
||
| 42 | { |
||
| 43 | $this->image = app('Intervention\Image\ImageManager'); |
||
| 44 | $this->finder = app('Illuminate\Filesystem\Filesystem'); |
||
| 45 | $this->imageFactory = $imageFactory; |
||
| 46 | $this->manager = $manager; |
||
| 47 | $this->config = $config; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get an image in the given thumbnail options |
||
| 52 | * @param string $path |
||
| 53 | * @param string $thumbnail |
||
| 54 | * @param bool $forceCreate |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | public function get($path, $thumbnail, $forceCreate = false) |
||
| 58 | { |
||
| 59 | if (!$this->isImage($path)) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | $filename = $this->config->get('asgard.media.config.files-path') . $this->newFilename($path, $thumbnail); |
||
| 64 | |||
| 65 | if ($this->returnCreatedFile($filename, $forceCreate)) { |
||
| 66 | return $filename; |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->makeNew($path, $filename, $thumbnail); |
||
| 70 | |||
| 71 | return $filename; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Return the thumbnail path |
||
| 76 | * @param string $originalImage |
||
| 77 | * @param string $thumbnail |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getThumbnail($originalImage, $thumbnail) |
||
| 81 | { |
||
| 82 | if (!$this->isImage($originalImage)) { |
||
| 83 | return $originalImage; |
||
| 84 | } |
||
| 85 | |||
| 86 | return $this->config->get('asgard.media.config.files-path') . $this->newFilename($originalImage, $thumbnail); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Create all thumbnails for the given image path |
||
| 91 | * @param string $path |
||
| 92 | */ |
||
| 93 | public function createAll($path) |
||
| 94 | { |
||
| 95 | if (!$this->isImage($path)) { |
||
| 96 | return; |
||
| 97 | } |
||
| 98 | |||
| 99 | foreach ($this->manager->all() as $thumbnail) { |
||
| 100 | $image = $this->image->make(public_path() . $path); |
||
| 101 | $filename = $this->config->get('asgard.media.config.files-path') . $this->newFilename($path, $thumbnail->name()); |
||
| 102 | foreach ($thumbnail->filters() as $manipulation => $options) { |
||
| 103 | $image = $this->imageFactory->make($manipulation)->handle($image, $options); |
||
| 104 | } |
||
| 105 | $image = $image->encode(pathinfo($path, PATHINFO_EXTENSION)); |
||
| 106 | $this->writeImage($filename, $image); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Prepend the thumbnail name to filename |
||
| 112 | * @param $path |
||
| 113 | * @param $thumbnail |
||
| 114 | * @return mixed|string |
||
| 115 | */ |
||
| 116 | private function newFilename($path, $thumbnail) |
||
| 117 | { |
||
| 118 | $filename = pathinfo($path, PATHINFO_FILENAME); |
||
| 119 | |||
| 120 | return $filename . '_' . $thumbnail . '.' . pathinfo($path, PATHINFO_EXTENSION); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Return the already created file if it exists and force create is false |
||
| 125 | * @param string $filename |
||
| 126 | * @param bool $forceCreate |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | private function returnCreatedFile($filename, $forceCreate) |
||
| 130 | { |
||
| 131 | return $this->finder->isFile(public_path() . $filename) && !$forceCreate; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Write the given image |
||
| 136 | * @param string $filename |
||
| 137 | * @param string $image |
||
| 138 | */ |
||
| 139 | private function writeImage($filename, $image) |
||
| 140 | { |
||
| 141 | $this->finder->put(public_path($filename), $image); |
||
| 142 | @chmod(public_path($filename), 0666); |
||
|
0 ignored issues
–
show
|
|||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Make a new image |
||
| 147 | * @param string $path |
||
| 148 | * @param string $filename |
||
| 149 | * @param string null $thumbnail |
||
| 150 | */ |
||
| 151 | private function makeNew($path, $filename, $thumbnail) |
||
| 152 | { |
||
| 153 | $image = $this->image->make(public_path() . $path); |
||
| 154 | |||
| 155 | foreach ($this->manager->find($thumbnail) as $manipulation => $options) { |
||
| 156 | $image = $this->imageFactory->make($manipulation)->handle($image, $options); |
||
| 157 | } |
||
| 158 | |||
| 159 | $image = $image->encode(pathinfo($path, PATHINFO_EXTENSION)); |
||
| 160 | |||
| 161 | $this->writeImage($filename, $image); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Check if the given path is en image |
||
| 166 | * @param string $path |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function isImage($path) |
||
| 170 | { |
||
| 171 | return in_array(pathinfo($path, PATHINFO_EXTENSION), $this->imageExtensions); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Delete all files on disk for the given file in storage |
||
| 176 | * This means the original and the thumbnails |
||
| 177 | * @param $file |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function deleteAllFor(File $file) |
||
| 181 | { |
||
| 182 | if (!$this->isImage($file->path)) { |
||
| 183 | return $this->finder->delete($file->path); |
||
| 184 | } |
||
| 185 | |||
| 186 | $paths[] = public_path() . $file->path; |
||
| 187 | $fileName = pathinfo($file->path, PATHINFO_FILENAME); |
||
| 188 | $extension = pathinfo($file->path, PATHINFO_EXTENSION); |
||
| 189 | foreach ($this->manager->all() as $thumbnail) { |
||
| 190 | $paths[] = public_path() . $this->config->get( |
||
| 191 | 'asgard.media.config.files-path' |
||
| 192 | ) . "{$fileName}_{$thumbnail->name()}.{$extension}"; |
||
| 193 | } |
||
| 194 | |||
| 195 | return $this->finder->delete($paths); |
||
| 196 | } |
||
| 197 | } |
||
| 198 |
If you suppress an error, we recommend checking for the error condition explicitly: