Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | <?php |
Adrià Vilanova Martínez | 5af8651 | 2023-12-02 20:44:16 +0100 | [diff] [blame^] | 2 | /* |
| 3 | * hores |
| 4 | * Copyright (c) 2023 Adrià Vilanova Martínez |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as |
| 8 | * published by the Free Software Foundation, either version 3 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public |
| 17 | * License along with this program. |
| 18 | * If not, see http://www.gnu.org/licenses/. |
| 19 | */ |
| 20 | |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 21 | class files { |
| 22 | const NAME_LENGTH = 16; |
| 23 | const MAX_SIZE = 6*1024*1024; |
| 24 | const READABLE_MAX_SIZE = "6 MB"; |
| 25 | |
| 26 | public static $acceptedFormats = ["pdf", "png", "jpg", "jpeg", "bmp", "gif"]; |
| 27 | public static $mimeTypes = array( |
| 28 | "pdf" => "application/pdf", |
| 29 | "png" => "image/png", |
| 30 | "jpg" => "image/jpeg", |
| 31 | "jpeg" => "image/jpeg", |
| 32 | "bmp" => "image/bmp", |
| 33 | "gif" => "image/gif" |
| 34 | ); |
| 35 | public static $readableMimeTypes = array( |
| 36 | "pdf" => "Documento PDF", |
| 37 | "png" => "Imagen PNG", |
| 38 | "jpg" => "Imagen JPG", |
| 39 | "jpeg" => "Imagen JPG", |
| 40 | "bmp" => "Imagen BMP", |
| 41 | "gif" => "Imagen GIF" |
| 42 | ); |
| 43 | public static $mimeTypesIcons = array( |
| 44 | "pdf" => "insert_drive_file", |
| 45 | "png" => "image", |
| 46 | "jpg" => "image", |
| 47 | "jpeg" => "image", |
| 48 | "bmp" => "image", |
| 49 | "gif" => "image" |
| 50 | ); |
| 51 | |
| 52 | public static function getAcceptAttribute($pretty = false) { |
| 53 | $formats = array_map(function($el) { |
| 54 | return ".".$el; |
| 55 | }, self::$acceptedFormats); |
| 56 | |
| 57 | return implode(($pretty ? ", " : ","), $formats); |
| 58 | } |
| 59 | |
| 60 | // From https://stackoverflow.com/a/31107425 |
| 61 | private static function randomStr(int $length = 64, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'): string { |
| 62 | if ($length < 1) { |
| 63 | throw new \RangeException("Length must be a positive integer"); |
| 64 | } |
| 65 | $pieces = []; |
| 66 | $max = mb_strlen($keyspace, '8bit') - 1; |
| 67 | for ($i = 0; $i < $length; ++$i) { |
| 68 | $pieces []= $keyspace[random_int(0, $max)]; |
| 69 | } |
| 70 | return implode('', $pieces); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | private static function getName($ext) { |
| 75 | global $conf; |
| 76 | |
| 77 | $filename = ""; |
| 78 | |
| 79 | do { |
| 80 | $filename = self::randomStr(self::NAME_LENGTH).".".$ext; |
| 81 | } while (file_exists($conf["attachmentsFolder"].$filename)); |
| 82 | |
| 83 | return $filename; |
| 84 | } |
| 85 | |
| 86 | public static function getFileExtension($file) { |
| 87 | $filenameExploded = explode(".", $file); |
| 88 | return mb_strtolower($filenameExploded[count($filenameExploded) - 1]); |
| 89 | } |
| 90 | |
| 91 | public static function uploadFile(&$file, &$name) { |
| 92 | global $conf; |
| 93 | |
| 94 | if (!isset($file["error"]) || is_array($file["error"])) return 1; |
| 95 | |
| 96 | switch ($file["error"]) { |
| 97 | case UPLOAD_ERR_OK: |
| 98 | break; |
| 99 | |
| 100 | case UPLOAD_ERR_INI_SIZE: |
| 101 | case UPLOAD_ERR_FORM_SIZE: |
| 102 | return 2; |
| 103 | break; |
| 104 | |
| 105 | default: |
| 106 | return 1; |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | $ext = self::getFileExtension($file["name"]); |
| 111 | |
| 112 | if ($file['size'] > self::MAX_SIZE) return 2; |
| 113 | if (!in_array($ext, self::$acceptedFormats)) return 3; |
| 114 | |
| 115 | $name = self::getName($ext); |
| 116 | |
| 117 | return (move_uploaded_file($file["tmp_name"], $conf["attachmentsFolder"].$name) ? 0 : 1); |
| 118 | } |
| 119 | |
| 120 | public static function removeFile($name) { |
| 121 | global $conf; |
| 122 | |
| 123 | return unlink($conf["attachmentsFolder"].$name); |
| 124 | } |
| 125 | } |