Comment: | First steps of file management implementation |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | dev |
Files: | files | file ages | folders |
SHA1: |
1d45c386597c360831fa6d182dda255f |
User & Date: | bohwaz on 2020-12-07 01:17:46 |
Other Links: | branch diff | manifest | tags |
2020-12-10
| ||
18:21 | Merge with trunk check-in: ab8bff586d user: bohwaz tags: dev | |
2020-12-07
| ||
01:17 | First steps of file management implementation check-in: 1d45c38659 user: bohwaz tags: dev | |
2020-12-06
| ||
18:56 | Merge trunk check-in: b4fdcab9fe user: bohwaz tags: dev | |
Modified src/config.dist.php from [9561d21e0d] to [a74f60285a].
︙ | ︙ | |||
351 352 353 354 355 356 357 | * * sinon la personnalisation des couleurs ne fonctionnera pas * * Défaut : [ADMIN_URL]static/gdin_bg.png */ //const ADMIN_BACKGROUND_IMAGE = 'http://mon-asso.fr/fond_garradin.png'; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | * * sinon la personnalisation des couleurs ne fonctionnera pas * * Défaut : [ADMIN_URL]static/gdin_bg.png */ //const ADMIN_BACKGROUND_IMAGE = 'http://mon-asso.fr/fond_garradin.png'; /** * Stockage des fichiers * * Indiquer ici le nom d'une classe de stockage de fichiers * (parmis celles disponibles dans lib/Garradin/Files/Backend) * * Indiquer NULL si vous souhaitez stocker les fichier dans la base * de données SQLite (valeur par défaut). * * Classes de stockage possibles : * - SQLite : enregistre dans la base de données (défaut) * - FileSystem : enregistrement des fichiers dans le système de fichier * - FileSystem : idem mais permet de spécifier un quota maximal * * Défaut : null */ //const FILE_STORAGE_BACKEND = null; /** * Configuration du stockage des fichiers * * Indiquer dans cette constante la configuration de la classe de stockage * des fichiers (en string). * * Valeurs possibles : * - SQLite : null, aucune configuration possible * - FileSystem : chemin du répertoire où doivent être stockés les fichiers, * %s doit être ajouté à la fin pour indiquer le répertoire et nom du fichier * - FileSystemQuota : idem, mais il faut rajouter ';quota=XXX' à la fin pour * indiquer la taille maximale de stockage autorisée. * * Défaut : null */ //const FILE_STORAGE_CONFIG = null; |
Added src/include/data/1.1.0_migration.sql version [8c30f24db3].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | -- Copy wiki pages content CREATE TEMP TABLE wiki_as_files (hash, size, content, name, path, uri, parent, created, modified, author_id, encrypted, content_id); INSERT INTO wiki_as_files SELECT sha1(contenu), LENGTH(contenu), contenu, uri || '.skriv', NULL, uri, parent, date_creation, date_modification, id_auteur, chiffrement, NULL FROM wiki_pages p INNER JOIN wiki_revisions r ON r.id_page = p.id AND r.revision = p.revision; -- Build back path, up to ten levels UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; -- Would probably be better with a recursive loop but hey, it works like that UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; UPDATE wiki_as_files waf SET path = (SELECT uri FROM wiki_as_files WHERE id = waf.parent) || '/' || path, parent = (SELECT parent FROM wiki_as_files WHERE id = waf.parent) WHERE parent > 0; INSERT INTO files_contents (hash, size, content) SELECT hash, size, content FROM wiki_as_files; UPDATE wiki_as_files SET content_id = (SELECT fc.id FROM files_contents fc WHERE fc.hash = wiki_as_files.hash); INSERT INTO files_search (id, content) SELECT (content_id, content) FROM wiki_as_files; INSERT INTO files (path, name, type, created, modified, content_id, author_id) SELECT path, name, CASE WHEN encrypted THEN 'text/encrypted' ELSE 'text/skriv' END, created, modified, content_id, author_id FROM wiki_as_files; DROP TRIGGER wiki_recherche_delete; DROP TRIGGER wiki_recherche_update; DROP TRIGGER wiki_recherche_contenu_insert; DROP TRIGGER wiki_recherche_contenu_chiffre; DROP TABLE wiki_recherche; DROP TABLE wiki_pages; DROP TABLE wiki_revisions; |
Added src/include/data/1.1.0_schema.sql version [d979d9fd8b].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | CREATE TABLE IF NOT EXISTS config ( -- Configuration de Garradin cle TEXT PRIMARY KEY NOT NULL, valeur TEXT ); CREATE TABLE IF NOT EXISTS membres_categories -- Catégories de membres ( id INTEGER PRIMARY KEY NOT NULL, nom TEXT NOT NULL, droit_wiki INTEGER NOT NULL DEFAULT 1, droit_membres INTEGER NOT NULL DEFAULT 1, droit_compta INTEGER NOT NULL DEFAULT 1, droit_inscription INTEGER NOT NULL DEFAULT 0, droit_connexion INTEGER NOT NULL DEFAULT 1, droit_config INTEGER NOT NULL DEFAULT 0, cacher INTEGER NOT NULL DEFAULT 0 ); -- Membres de l'asso -- Table dynamique générée par l'application -- voir Garradin\Membres\Champs.php CREATE TABLE IF NOT EXISTS membres_sessions -- Sessions ( selecteur TEXT NOT NULL, hash TEXT NOT NULL, id_membre INTEGER NOT NULL REFERENCES membres (id) ON DELETE CASCADE, expire INT NOT NULL, PRIMARY KEY (selecteur, id_membre) ); CREATE TABLE IF NOT EXISTS services -- Types de services (cotisations) ( id INTEGER PRIMARY KEY NOT NULL, label TEXT NOT NULL, description TEXT NULL, duration INTEGER NULL CHECK (duration IS NULL OR duration > 0), -- En jours start_date TEXT NULL CHECK (start_date IS NULL OR date(start_date) = start_date), end_date TEXT NULL CHECK (end_date IS NULL OR (date(end_date) = end_date AND date(end_date) >= date(start_date))) ); CREATE TABLE IF NOT EXISTS services_fees ( id INTEGER PRIMARY KEY NOT NULL, label TEXT NOT NULL, description TEXT NULL, amount INTEGER NULL, formula TEXT NULL, -- Formule de calcul du montant de la cotisation, si cotisation dynamique (exemple : membres.revenu_imposable * 0.01) id_service INTEGER NOT NULL REFERENCES services (id) ON DELETE CASCADE, id_account INTEGER NULL REFERENCES acc_accounts (id) ON DELETE SET NULL CHECK (id_account IS NULL OR id_year IS NOT NULL), -- NULL si le type n'est pas associé automatiquement à la compta id_year INTEGER NULL REFERENCES acc_years (id) ON DELETE SET NULL -- NULL si le type n'est pas associé automatiquement à la compta ); CREATE TABLE IF NOT EXISTS services_users -- Enregistrement des cotisations et activités ( id INTEGER NOT NULL PRIMARY KEY, id_user INTEGER NOT NULL REFERENCES membres (id) ON DELETE CASCADE, id_service INTEGER NOT NULL REFERENCES services (id) ON DELETE CASCADE, id_fee INTEGER NULL REFERENCES services_fees (id) ON DELETE CASCADE, paid INTEGER NOT NULL DEFAULT 0, expected_amount INTEGER NULL, date TEXT NOT NULL DEFAULT CURRENT_DATE CHECK (date(date) IS NOT NULL AND date(date) = date), expiry_date TEXT NULL CHECK (date(expiry_date) IS NULL OR date(expiry_date) = expiry_date) ); CREATE UNIQUE INDEX IF NOT EXISTS su_unique ON services_users (id_user, id_service, date); CREATE INDEX IF NOT EXISTS su_service ON services_users (id_service); CREATE INDEX IF NOT EXISTS su_fee ON services_users (id_fee); CREATE INDEX IF NOT EXISTS su_paid ON services_users (paid); CREATE INDEX IF NOT EXISTS su_expiry ON services_users (expiry_date); CREATE TABLE IF NOT EXISTS services_reminders -- Rappels de devoir renouveller une cotisation ( id INTEGER NOT NULL PRIMARY KEY, id_service INTEGER NOT NULL REFERENCES services (id) ON DELETE CASCADE, delay INTEGER NOT NULL, -- Délai en jours pour envoyer le rappel subject TEXT NOT NULL, body TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS services_reminders_sent -- Enregistrement des rappels envoyés à qui et quand ( id INTEGER NOT NULL PRIMARY KEY, id_user INTEGER NOT NULL REFERENCES membres (id) ON DELETE CASCADE, id_service INTEGER NOT NULL REFERENCES services (id) ON DELETE CASCADE, id_reminder INTEGER NOT NULL REFERENCES services_reminders (id) ON DELETE CASCADE, date TEXT NOT NULL DEFAULT CURRENT_DATE CHECK (date(date) IS NOT NULL AND date(date) = date) ); CREATE UNIQUE INDEX IF NOT EXISTS srs_index ON services_reminders_sent (id_user, id_service, id_reminder, date); CREATE INDEX IF NOT EXISTS srs_reminder ON services_reminders_sent (id_reminder); CREATE INDEX IF NOT EXISTS srs_user ON services_reminders_sent (id_user); -- -- COMPTA -- CREATE TABLE IF NOT EXISTS acc_charts -- Plans comptables : il peut y en avoir plusieurs ( id INTEGER NOT NULL PRIMARY KEY, country TEXT NOT NULL, code TEXT NULL, -- NULL = plan comptable créé par l'utilisateur label TEXT NOT NULL, archived INTEGER NOT NULL DEFAULT 0 -- 1 = archivé, non-modifiable ); CREATE TABLE IF NOT EXISTS acc_accounts -- Comptes des plans comptables ( id INTEGER NOT NULL PRIMARY KEY, id_chart INTEGER NOT NULL REFERENCES acc_charts ON DELETE CASCADE, code TEXT NOT NULL, -- peut contenir des lettres, eg. 53A, 53B, etc. label TEXT NOT NULL, description TEXT NULL, position INTEGER NOT NULL, -- position actif/passif/charge/produit type INTEGER NOT NULL DEFAULT 0, -- Type de compte spécial : banque, caisse, en attente d'encaissement, etc. user INTEGER NOT NULL DEFAULT 1 -- 1 = fait partie du plan comptable original, 0 = a été ajouté par l'utilisateur ); CREATE UNIQUE INDEX IF NOT EXISTS acc_accounts_codes ON acc_accounts (code, id_chart); CREATE INDEX IF NOT EXISTS acc_accounts_type ON acc_accounts (type); CREATE INDEX IF NOT EXISTS acc_accounts_position ON acc_accounts (position); CREATE TABLE IF NOT EXISTS acc_years -- Exercices ( id INTEGER NOT NULL PRIMARY KEY, label TEXT NOT NULL, start_date TEXT NOT NULL CHECK (date(start_date) IS NOT NULL AND date(start_date) = start_date), end_date TEXT NOT NULL CHECK (date(end_date) IS NOT NULL AND date(end_date) = end_date), closed INTEGER NOT NULL DEFAULT 0, id_chart INTEGER NOT NULL REFERENCES acc_charts (id) ); CREATE INDEX IF NOT EXISTS acc_years_closed ON acc_years (closed); CREATE TABLE IF NOT EXISTS acc_transactions -- Opérations comptables ( id INTEGER PRIMARY KEY NOT NULL, type INTEGER NOT NULL DEFAULT 0, -- Type d'écriture, 0 = avancée (normale) status INTEGER NOT NULL DEFAULT 0, -- Statut (bitmask) label TEXT NOT NULL, notes TEXT NULL, reference TEXT NULL, -- N° de pièce comptable date TEXT NOT NULL DEFAULT CURRENT_DATE CHECK (date(date) IS NOT NULL AND date(date) = date), validated INTEGER NOT NULL DEFAULT 0, -- 1 = écriture validée, non modifiable hash TEXT NULL, prev_hash TEXT NULL, id_year INTEGER NOT NULL REFERENCES acc_years(id), id_creator INTEGER NULL REFERENCES membres(id) ON DELETE SET NULL, id_related INTEGER NULL REFERENCES acc_transactions(id) ON DELETE SET NULL -- écriture liée (par ex. remboursement d'une dette) ); CREATE INDEX IF NOT EXISTS acc_transactions_year ON acc_transactions (id_year); CREATE INDEX IF NOT EXISTS acc_transactions_date ON acc_transactions (date); CREATE INDEX IF NOT EXISTS acc_transactions_related ON acc_transactions (id_related); CREATE INDEX IF NOT EXISTS acc_transactions_type ON acc_transactions (type); CREATE INDEX IF NOT EXISTS acc_transactions_status ON acc_transactions (status); CREATE TABLE IF NOT EXISTS acc_transactions_lines -- Lignes d'écritures d'une opération ( id INTEGER PRIMARY KEY NOT NULL, id_transaction INTEGER NOT NULL REFERENCES acc_transactions (id) ON DELETE CASCADE, id_account INTEGER NOT NULL REFERENCES acc_accounts (id), -- N° du compte dans le plan comptable credit INTEGER NOT NULL, debit INTEGER NOT NULL, reference TEXT NULL, -- Référence de paiement, eg. numéro de chèque label TEXT NULL, reconciled INTEGER NOT NULL DEFAULT 0, id_analytical INTEGER NULL REFERENCES acc_accounts(id) ON DELETE SET NULL, CONSTRAINT line_check1 CHECK ((credit * debit) = 0), CONSTRAINT line_check2 CHECK ((credit + debit) > 0) ); CREATE INDEX IF NOT EXISTS acc_transactions_lines_account ON acc_transactions_lines (id_account); CREATE INDEX IF NOT EXISTS acc_transactions_lines_analytical ON acc_transactions_lines (id_analytical); CREATE INDEX IF NOT EXISTS acc_transactions_lines_reconciled ON acc_transactions_lines (reconciled); CREATE TABLE IF NOT EXISTS acc_transactions_users -- Liaison des écritures et des membres ( id_user INTEGER NOT NULL REFERENCES membres (id) ON DELETE CASCADE, id_transaction INTEGER NOT NULL REFERENCES acc_transactions (id) ON DELETE CASCADE, id_service_user INTEGER NULL REFERENCES services_users (id) ON DELETE SET NULL, PRIMARY KEY (id_user, id_transaction) ); CREATE INDEX IF NOT EXISTS acc_transactions_users_service ON acc_transactions_users (id_service_user); CREATE TABLE IF NOT EXISTS plugins ( id TEXT NOT NULL PRIMARY KEY, officiel INTEGER NOT NULL DEFAULT 0, nom TEXT NOT NULL, description TEXT NULL, auteur TEXT NULL, url TEXT NULL, version TEXT NOT NULL, menu INTEGER NOT NULL DEFAULT 0, menu_condition TEXT NULL, config TEXT NULL ); CREATE TABLE IF NOT EXISTS plugins_signaux -- Association entre plugins et signaux (hooks) ( signal TEXT NOT NULL, plugin TEXT NOT NULL REFERENCES plugins (id), callback TEXT NOT NULL, PRIMARY KEY (signal, plugin) ); CREATE TABLE IF NOT EXISTS files -- Files metadata ( id INTEGER NOT NULL PRIMARY KEY, path TEXT NOT NULL, name TEXT NOT NULL, -- file name (eg. image1234.jpeg) type TEXT NULL, -- MIME type image INTEGER NOT NULL DEFAULT 0, -- 1 = image reconnue created TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP CHECK (datetime(created) IS NOT NULL AND datetime(created) = created), modified TEXT NULL CHECK (datetime(modified) IS NULL OR datetime(modified) = modified), content_id INTEGER NOT NULL REFERENCES files_contents (id) ON DELETE CASCADE, author_id INTEGER NULL REFERENCES membres (id) ON DELETE SET NULL ); CREATE INDEX IF NOT EXISTS files_path ON files (path); CREATE INDEX IF NOT EXISTS files_date ON files (datetime); CREATE TABLE IF NOT EXISTS files_contents -- Contenu des fichiers ( id INTEGER NOT NULL PRIMARY KEY, hash TEXT NOT NULL, -- Hash SHA1 du contenu du fichier size INTEGER NOT NULL, -- Taille en octets content BLOB NULL, storage TEXT NULL, -- Storage medium, NULL means stored in content BLOB storage_path TEXT NULL ); CREATE UNIQUE INDEX IF NOT EXISTS files_hash ON files_contents (hash); CREATE TABLE IF NOT EXISTS files_folders ( id INTEGER NOT NULL PRIMARY KEY, parent_id INTEGER NULL REFERENCES files_folders(id) ON DELETE CASCADE, name TEXT NOT NULL ); CREATE VIRTUAL TABLE IF NOT EXISTS files_search USING fts4 -- Search inside files content ( id INT PRIMARY KEY NOT NULL REFERENCES files_contents(id), content TEXT NULL -- Text content ); CREATE TABLE IF NOT EXISTS files_links -- This references use of a file outside of the documents module ( id INTEGER NOT NULL PRIMARY KEY, file_id INTEGER NOT NULL REFERENCES fichiers (id) ON DELETE CASCADE, user_id INTEGER NULL REFERENCES membres (id) ON DELETE CASCADE, transaction_id INTEGER NULL REFERENCES acc_transactions (id) ON DELETE CASCADE, config TEXT NULL REFERENCES config (valeur) ON DELETE CASCADE, web INTEGER NULL, -- Make sure that only one is filled CHECK ((user_id IS NULL) + (transaction_id IS NULL) + (config IS NULL) + (web IS NULL) = 1) ); CREATE UNIQUE INDEX files_links_unique ON files_links (file_id, user_id, transaction_id, config, web); -- FIXME: rename to english CREATE TABLE IF NOT EXISTS recherches -- Recherches enregistrées ( id INTEGER NOT NULL PRIMARY KEY, id_membre INTEGER NULL REFERENCES membres (id) ON DELETE CASCADE, -- Si non NULL, alors la recherche ne sera visible que par le membre associé intitule TEXT NOT NULL, creation TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP CHECK (datetime(creation) IS NOT NULL AND datetime(creation) = creation), cible TEXT NOT NULL, -- "membres" ou "compta" type TEXT NOT NULL, -- "json" ou "sql" contenu TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS compromised_passwords_cache -- Cache des hash de mots de passe compromis ( hash TEXT NOT NULL PRIMARY KEY ); CREATE TABLE IF NOT EXISTS compromised_passwords_cache_ranges -- Cache des préfixes de mots de passe compromis ( prefix TEXT NOT NULL PRIMARY KEY, date INTEGER NOT NULL ); |
Added src/include/lib/Garradin/Entities/Files/File.php version [4fd57e6360].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | <?php namespace Garradin\Entities\Files; use KD2\Image; use Garradin\DB; use Garradin\Entity; use Garradin\UserException; class File extends Entity { const TABLE = 'files'; protected $id; protected $name; protected $_types = [ 'id' => 'int', 'label' => 'name', ]; public function selfCheck(): void { parent::selfCheck(); } public function delete(): bool { $return = parent::delete(); // Clean up files Files::deleteOrphanFiles(); return $return; } static protected function store(?string $path, string $name, string $source_path = null, $source_content = null): self { assert($path || $content); $finfo = \finfo_open(\FILEINFO_MIME_TYPE); $file = new self; $file->path = $path; if ($source_path && !$source_content) { $file->hash = sha1_file($source_path); $file->size = filesize($source_path); $file->type = finfo_file($finfo, $source_path); } else { $file->hash = sha1($source_content); $file->size = strlen($source_content); $file->type = finfo_buffer($finfo, $source_content); } $file->image = preg_match('/^image\/(?:png|jpe?g|gif)$/', $file->type); // Check that it's a real image if ($file->image) { try { if ($source_path && !$source_content) { $i = new Image($source_path); } else { $i = Image::createFromBlob($source_content); } // Recompress PNG files from base64, assuming they are coming // from JS canvas which doesn't know how to gzip (d'oh!) if ($i->format() == 'png' && null !== $source_content) { $source_content = $i->output('png', true); $file->hash = sha1($source_content); $file->size = strlen($source_content); } unset($i); } catch (\RuntimeException $e) { if (strstr($e->getMessage(), 'No suitable image library found')) { throw new \RuntimeException('Le serveur n\'a aucune bibliothèque de gestion d\'image installée, et ne peut donc pas accepter les images. Installez Imagick ou GD.'); } throw new UserException('Fichier image invalide'); } } $db = DB::getInstance(); $db->begin(); // Il peut arriver que l'on renvoie ici un fichier déjà stocké, auquel cas, ne pas le re-stocker if ($content_id = $db->firstColumn('SELECT id FROM files_contents WHERE hash = ?;', $hash)) { $file->content_id = $content_id; } else { $db->preparedQuery('INSERT INTO files_contents (hash, size) VALUES (?, ?);', [$file->hash, (int)$file->size]); $file->content_id = $db->lastInsertRowID(); if (!Files::callStorage('store', $file, $path, $content)) { throw new UserException('Le fichier n\'a pas pu être enregistré.'); } } $file->save(); $db->commit(); return $file; } /** * Upload de fichier à partir d'une chaîne en base64 * @param string $name * @param string $content * @return File */ static public function storeFromBase64(?string $path, string $name, string $encoded_content): self { $content = base64_decode($encoded_content); return self::store($path, $name, null, $content); } /** * Upload du fichier par POST * @param array $file Caractéristiques du fichier envoyé * @return File */ static public function upload(?string $path, array $file): self { if (!empty($file['error'])) { throw new UserException(self::getErrorMessage($file['error'])); } if (empty($file['size']) || empty($file['name'])) { throw new UserException('Fichier reçu invalide : vide ou sans nom de fichier.'); } if (!is_uploaded_file($file['tmp_name'])) { throw new \RuntimeException('Le fichier n\'a pas été envoyé de manière conventionnelle.'); } $name = preg_replace('/\s+/', '_', $file['name']); $name = preg_replace('/[^\d\w._-]/ui', '', $name); return self::store($path, $name, $file['tmp_name']); } } |
Modified src/include/lib/Garradin/Fichiers.php from [76103311d2] to [9ff6c36622].
︙ | ︙ | |||
486 487 488 489 490 491 492 | case UPLOAD_ERR_EXTENSION: return 'Une extension du serveur a interrompu l\'envoi du fichier.'; default: return 'Erreur inconnue: ' . $error; } } | < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | case UPLOAD_ERR_EXTENSION: return 'Une extension du serveur a interrompu l\'envoi du fichier.'; default: return 'Erreur inconnue: ' . $error; } } /** * Envoie un fichier déjà stocké * * @param string $name Nom du fichier * @param string $hash Hash SHA1 du contenu du fichier * @return object Un objet Fichiers en cas de succès |
︙ | ︙ |
Added src/include/lib/Garradin/Files/Files.php version [85cdda535c].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php namespace Garradin\Files; use Garradin\Static_Cache; use Garradin\DB; use const Garradin\FILE_STORAGE_BACKEND; class Files { static public function callStorage(string $function, ...$args) { $storage = FILE_STORAGE_BACKEND ?? 'SQLite'; $class_name = get_class(__NAMESPACE__ . '\\Backend\\' . $storage); return call_user_func_array([$class_name, $function], $args); } static public function migrateStorage(string $from, string $to) { $res = EM::getInstance(File::class)->iterate('SELECT * FROM @TABLE;'); $from = get_class(__NAMESPACE__ . '\\Backend\\' . $from); $to = get_class(__NAMESPACE__ . '\\Backend\\' . $to); foreach ($res as $file) { $from_path = call_user_func([$from, 'path'], $file); call_user_func([$to, 'store'], $file, $from_path); } } } |
Added src/include/lib/Garradin/Files/Storage/FileSystem.php version [e05f9c9e78].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | <?php namespace Garradin\Files\Storage; use const Garradin\FILE_STORAGE_CONFIG; /** * This class provides storage in the file system * You need ton configure FILE_STORAGE_CONFIG to give a file path */ class FileSystem implements StorageInterface { static protected $_size; static protected function _getRoot() { if (!FILE_STORAGE_CONFIG) { throw new \RuntimeException('Le stockage de fichier n\'a pas été configuré (FILE_STORAGE_CONFIG est vide).'); } if (!is_writable(FILE_STORAGE_CONFIG)) { throw new \RuntimeException('Le répertoire de stockage des fichiers est protégé contre l\'écriture.'); } $target = rtrim(FILE_STORAGE_CONFIG, DIRECTORY_SEPARATOR); return realpath($target); } static protected function ensureDirectoryExists(string $path): void { if (is_dir($path)) { return; } $permissions = fileperms(self::_getRoot(null)); mkdir($path, $permissions & 0777, true); } static public function store(File $file, ?string $path, ?string $content): bool { $target = self::getPath($file); self::ensureDirectoryExists(dirname($target)); if (null !== $path) { return copy($path, $target); } else { return file_put_contents($target, $content); } } static public function list(?string $path): ?array { $path = self::_getRoot() . ($path ? DIRECTORY_SEPARATOR . $file->path : '') . DIRECTORY_SEPARATOR . '*'; $files = glob($path); $list = []; foreach ($files as $file) { } return $list; } static public function getPath(File $file): ?string { $path = ''; if ($file->path) { $path .= DIRECTORY_SEPARATOR . $file->path; } $path .= DIRECTORY_SEPARATOR . $file->name; return self::_getRoot() . $path; } static public function display(File $file): void { readfile(self::getPath($file)); } static public function fetch(File $file): string { return file_get_contents(self::getPath($file)); } static public function delete(File $file): bool { return unlink(self::getPath($file)); } static public function move(File $old_file, File $new_file): bool { $target = self::getPath($new_file); self::ensureDirectoryExists(dirname($target)); return rename(self::getPath($old_file), $target); } static public function getTotalSize(): ?int { if (null !== self::$_size) { return self::$_size; } $total = 0; $path = self::_getRoot(); foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS)) as $p) { $total += $p->getSize(); } self::$_size = (int) $total; return self::$_size; } static public function getRemainingQuota(): int { return disk_free_space(self::_getRoot()); } static public function getQuota(): int { return disk_total_space(self::_getRoot()); } } |
Added src/include/lib/Garradin/Files/Storage/FileSystemQuota.php version [66a57977b0].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?php namespace Garradin\Files\Storage; use const Garradin\FILE_STORAGE_CONFIG; /** * This class provides storage, same as FileSystem, * but adds the ability to define a custom quota. * To that end, just append ;quota=XXX to FILE_STORAGE_CONFIG * where XXX is the maximum storage allowed for that user, in bytes */ class FileSystemQuota extends FileSystem { static protected $quota; static protected $root; static protected function _getRoot() { if (null === self::$root) { if (!FILE_STORAGE_CONFIG) { throw new \RuntimeException('Le stockage de fichier n\'a pas été configuré (FILE_STORAGE_CONFIG est vide).'); } $target = strtok(FILE_STORAGE_CONFIG, ';'); if (!is_writable($target)) { throw new \RuntimeException('Le répertoire de stockage des fichiers est protégé contre l\'écriture.'); } strtok('='); $size = (int) strtok(''); if (!$size) { throw new \RuntimeException('Aucun quota indiqué dans FILE_STORAGE_CONFIG'); } $target = rtrim($target, DIRECTORY_SEPARATOR); self::$root = realpath($target); self::$quota = $size; } return self::$root; } static public function getRemainingQuota(): int { return self::getTotalSize() - self::getQuota(); } static public function getQuota(): int { self::_getRoot(); // Make sure quota is loaded return self::$quota; } } |
Added src/include/lib/Garradin/Files/Storage/SQLite.php version [09143891cd].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | <?php namespace Garradin\Files\Storage; use Garradin\Static_Cache; use Garradin\DB; class SQLite implements StorageInterface { /** * Renvoie le chemin vers le fichier local en cache, et le crée s'il n'existe pas * @return string Chemin local */ static protected function _getFilePathFromCache(File $file): string { $cache_id = 'files.' . $file->content_id; if (!Static_Cache::exists($cache_id)) { $blob = DB::getInstance()->openBlob('files_contents', 'content', (int)$file->content_id); Static_Cache::storeFromPointer($cache_id, $blob); fclose($blob); } return Static_Cache::getPath($cache_id); } static public function store(File $file, ?string $path, ?string $content): bool { $db = DB::getInstance(); $db->exec(sprintf('UPDATE files_contents SET blob = zeroblob(%d) WHERE id = %d;', $file->size, $file->content_id)); $blob = $db->openBlob('files_contents', 'content', $file->content_id, 'main', SQLITE3_OPEN_READWRITE); if (null !== $content) { fwrite($blob, $content); } else { fwrite($blob, file_get_contents($path)); } fclose($blob); return true; } static public function list(string $path): ?array { return null; } static public function getPath(File $file): ?string { return null; } static public function display(File $file): void { readfile(self::getFilePathFromCache($file)); } static public function fetch(File $file): string { return file_get_contents(self::_getFilePathFromCache($file)); } static public function delete(File $file): bool { $cache_id = 'files.' . $file->content_id; Static_Cache::remove($cache_id); return DB::getInstance()->delete('files_contents', 'id = ?', (int)$file->content_id); } static public function move(File $old_file, File $new_file): bool { return true; } static public function getTotalSize(): ?int { return (int) DB::getInstance()->firstColumn('SELECT SUM(size) FROM files_contents;'); } static public function getRemainingQuota(): int { return disk_free_space(dirname(DB_FILE)); } static public function getQuota(): int { return disk_total_space(dirname(DB_FILE)); } } |
Added src/include/lib/Garradin/Files/Storage/StorageInterface.php version [1b7b1b4fca].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?php namespace Garradin\Files\Storage; interface StorageInterface { static public function store(File $file, ?string $path, ?string $content): bool; /** * List files contained in a path, this must return an array of File instances * If this storage backend wants to leave the directory handling to Garradin, just return NULL. * @param string $path * @return array[File...] */ static public function list(string $path): ?array; /** * Should return full local file access path. * If storage backend cannot store the file locally, return NULL. * In that case a subsequent call to fetch() will be done. */ static public function getPath(File $file): ?string; static public function display(File $file): void; static public function fetch(File $file): string; static public function delete(File $file): bool; static public function move(File $old_file, File $new_file): bool; static public function getTotalSize(): int; static public function getRemainingQuota(): int; static public function getQuota(): int; } |