Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | <?php |
| 2 | class files { |
| 3 | const NAME_LENGTH = 16; |
| 4 | const MAX_SIZE = 6*1024*1024; |
| 5 | const READABLE_MAX_SIZE = "6 MB"; |
| 6 | |
| 7 | public static $acceptedFormats = ["pdf", "png", "jpg", "jpeg", "bmp", "gif"]; |
| 8 | public static $mimeTypes = array( |
| 9 | "pdf" => "application/pdf", |
| 10 | "png" => "image/png", |
| 11 | "jpg" => "image/jpeg", |
| 12 | "jpeg" => "image/jpeg", |
| 13 | "bmp" => "image/bmp", |
| 14 | "gif" => "image/gif" |
| 15 | ); |
| 16 | public static $readableMimeTypes = array( |
| 17 | "pdf" => "Documento PDF", |
| 18 | "png" => "Imagen PNG", |
| 19 | "jpg" => "Imagen JPG", |
| 20 | "jpeg" => "Imagen JPG", |
| 21 | "bmp" => "Imagen BMP", |
| 22 | "gif" => "Imagen GIF" |
| 23 | ); |
| 24 | public static $mimeTypesIcons = array( |
| 25 | "pdf" => "insert_drive_file", |
| 26 | "png" => "image", |
| 27 | "jpg" => "image", |
| 28 | "jpeg" => "image", |
| 29 | "bmp" => "image", |
| 30 | "gif" => "image" |
| 31 | ); |
| 32 | |
| 33 | public static function getAcceptAttribute($pretty = false) { |
| 34 | $formats = array_map(function($el) { |
| 35 | return ".".$el; |
| 36 | }, self::$acceptedFormats); |
| 37 | |
| 38 | return implode(($pretty ? ", " : ","), $formats); |
| 39 | } |
| 40 | |
| 41 | // From https://stackoverflow.com/a/31107425 |
| 42 | private static function randomStr(int $length = 64, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'): string { |
| 43 | if ($length < 1) { |
| 44 | throw new \RangeException("Length must be a positive integer"); |
| 45 | } |
| 46 | $pieces = []; |
| 47 | $max = mb_strlen($keyspace, '8bit') - 1; |
| 48 | for ($i = 0; $i < $length; ++$i) { |
| 49 | $pieces []= $keyspace[random_int(0, $max)]; |
| 50 | } |
| 51 | return implode('', $pieces); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | private static function getName($ext) { |
| 56 | global $conf; |
| 57 | |
| 58 | $filename = ""; |
| 59 | |
| 60 | do { |
| 61 | $filename = self::randomStr(self::NAME_LENGTH).".".$ext; |
| 62 | } while (file_exists($conf["attachmentsFolder"].$filename)); |
| 63 | |
| 64 | return $filename; |
| 65 | } |
| 66 | |
| 67 | public static function getFileExtension($file) { |
| 68 | $filenameExploded = explode(".", $file); |
| 69 | return mb_strtolower($filenameExploded[count($filenameExploded) - 1]); |
| 70 | } |
| 71 | |
| 72 | public static function uploadFile(&$file, &$name) { |
| 73 | global $conf; |
| 74 | |
| 75 | if (!isset($file["error"]) || is_array($file["error"])) return 1; |
| 76 | |
| 77 | switch ($file["error"]) { |
| 78 | case UPLOAD_ERR_OK: |
| 79 | break; |
| 80 | |
| 81 | case UPLOAD_ERR_INI_SIZE: |
| 82 | case UPLOAD_ERR_FORM_SIZE: |
| 83 | return 2; |
| 84 | break; |
| 85 | |
| 86 | default: |
| 87 | return 1; |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | $ext = self::getFileExtension($file["name"]); |
| 92 | |
| 93 | if ($file['size'] > self::MAX_SIZE) return 2; |
| 94 | if (!in_array($ext, self::$acceptedFormats)) return 3; |
| 95 | |
| 96 | $name = self::getName($ext); |
| 97 | |
| 98 | return (move_uploaded_file($file["tmp_name"], $conf["attachmentsFolder"].$name) ? 0 : 1); |
| 99 | } |
| 100 | |
| 101 | public static function removeFile($name) { |
| 102 | global $conf; |
| 103 | |
| 104 | return unlink($conf["attachmentsFolder"].$name); |
| 105 | } |
| 106 | } |