Ein Hook ist simpel: Von der bekannten Modulstruktur benötigt man nur die /config/config.php, in der man Klasse und Funktion an den gewünschten Hook hängt:
1 |
$GLOBALS['TL_HOOKS']['postUpload'][] = array('ZipUploadCustom', 'handleFiles'); |
Sowie die Klasse als solche (direkt im Modulordner):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// extend System to have access to the logger: class ZipUploadCustom extends System { public function handleFiles($arrFiles) { foreach ($arrFiles as $file) { $file = TL_ROOT . '/' . $file; if(substr(strtolower($file), -4) === '.zip') { $this->extractZIP($file); } } } // ... } |
Das war’s im Wesentlichen. Der Vollständigkeit halber hier noch die Funkion zum Entpacken, sowie ein wenig Aufräumen der hochgeladenen Dateien:
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 |
private function extractZIP($file) { $zip = new ZipArchive(); if ($zip->open($file) === TRUE) { $dir = dirname($file); // extract stuff: if($zip->extractTo($dir)) { // clean up: $this->cleanUpSystemFiles($dir); unlink($file); } $zip->close(); $this->log("ZIP \"$file\" successfully extracted", 'ZipUploadCustom extractZIP()', TL_FILES ); } else { $this->log("cannot open ZIP \"$file\"", 'ZipUploadCustom extractZIP()', TL_ERROR); } } private function cleanUpSystemFiles($dirPath) { $this->log("clean up $dirPath", 'ZipUploadCustom cleanUpSystemFiles()', TL_FILES); foreach (new DirectoryIterator($dirPath) as $fileInfo) { if($fileInfo->isDot()) continue; $fileName = $fileInfo->getFilename(); //handle directories: if($fileInfo->isDir()) switch (TRUE) { // unwanted directories, tbc: case $fileName == '__MACOSX': case strpos($fileName, '.') === 0: $this->log("deleting directory $fileName", 'ZipUploadCustom cleanUpSystemFiles()', TL_FILES); $this->deleteDirectory($fileInfo->getPathname()); break; } // handle files: else switch (TRUE) { // unwanted files, tbc: case strpos($fileName, '.') === 0: $this->log("unlinking $fileName", 'ZipUploadCustom cleanUpSystemFiles()', TL_FILES); unlink($fileInfo->getPathname()); break; } } } // http://stackoverflow.com/a/15111679 private function deleteDirectory($dirPath) { foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) { $path->isDir() ? rmdir($path->getPathname()) : unlink($path->getPathname()); } rmdir($dirPath); } |
HTH