Language: PHP
Label: safe
CWE: CWE-434 \u2014 Unrestricted Upload of File with Dangerous Type
The product allows the attacker to upload or transfer files of dangerous types that can be automatically processed within the product's environment.

Code:
						$path = Froxlor::getInstallDir() . '/img/';
						if (!is_dir($path) && !mkdir($path, 0775)) {
							throw new Exception("img directory does not exist and cannot be created");
						}

						// Make sure we can write to the upload directory
						if (!is_writable($path)) {
							if (!chmod($path, 0775)) {
								throw new Exception("Cannot write to img directory");
							}
						}

						if (Validate::validateBase64Image($value)) {
							$img_data = base64_decode($value);
							$img_filename = explode('?', $_data[$index_split[0] . '.' . $index_split[1]], 2)[0];

							$spl = explode('.', $img_filename);
							$file_extension = strtolower(array_pop($spl));
							unset($spl);

							if (!in_array($file_extension, [
								'jpeg',
								'jpg',
								'png',
								'gif'
							])) {
								throw new Exception("Invalid file-extension, use one of: jpeg, jpg, png, gif");
							}
							$img_filename = 'img/' . bin2hex(random_bytes(16)) . '.' . $file_extension;
							file_put_contents(Froxlor::getInstallDir() . '/' . $img_filename, $img_data);
							$img_index = $index_split[0].'.'.$index_split[1];
							Settings::Set($img_index, $img_filename . '?v=' . time());
						}
					}
				}
				// all good
				return true;
			} else {
				throw new Exception("Importing settings failed");
			}
		}
		throw new Exception("Invalid JSON data: " . json_last_error_msg());
	}