diff --git a/util/build-mimetypes-cli-php55.php b/util/build-mimetypes-cli-php55.php new file mode 100644 index 000000000..59de6b2d1 --- /dev/null +++ b/util/build-mimetypes-cli-php55.php @@ -0,0 +1,217 @@ +#!/usr/bin/env php +options['mimeDetect'] = 'internal'; + $this->options['mimefile'] = $mimefile; + } + } + + /** + * Build MIME => representative extension table. + * + * This follows getExtentionByMime(): + * - base table from getMimeTable() + * - then fill missing MIME entries from mimeMap only + * - ignore wildcard extension (*) entries + * + * For forward compatibility, this exporter also accepts both + * 'staticMineMap' (historic typo in core) and 'staticMimeMap'. + * + * @return array + */ + public function buildMimeTypesMap() + { + $mimeMap = array(); + + if (isset($this->options['mimeMap']) && is_array($this->options['mimeMap'])) { + $mimeMap = $this->options['mimeMap']; + } + + if (isset($this->options['staticMineMap']) && is_array($this->options['staticMineMap']) && $this->options['staticMineMap']) { + $mimeMap = array_merge($mimeMap, $this->options['staticMineMap']); + } + + if (isset($this->options['staticMimeMap']) && is_array($this->options['staticMimeMap']) && $this->options['staticMimeMap']) { + $mimeMap = array_merge($mimeMap, $this->options['staticMimeMap']); + } + + if (isset($this->options['additionalMimeMap']) && is_array($this->options['additionalMimeMap']) && $this->options['additionalMimeMap']) { + $mimeMap = array_merge($mimeMap, $this->options['additionalMimeMap']); + } + + $extTable = array(); + foreach ($this->getMimeTable() as $ext => $_mime) { + if (!isset($extTable[$_mime])) { + $extTable[$_mime] = (string)$ext; + } + } + + foreach ($mimeMap as $pair => $_mime) { + $parts = explode(':', (string)$pair, 2); + $ext = isset($parts[0]) ? (string)$parts[0] : ''; + if ($ext !== '*' && !isset($extTable[$_mime])) { + $extTable[$_mime] = $ext; + } + } + + return $extTable; + } + + /** + * @param bool $pretty + * @return string + * @throws RuntimeException + */ + public function buildJavascript($pretty) + { + $flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; + if ($pretty) { + $flags |= JSON_PRETTY_PRINT; + } + + $json = json_encode($this->buildMimeTypesMap(), $flags); + if ($json === false) { + $message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Unknown JSON encoding error'; + throw new RuntimeException('json_encode() failed: ' . $message); + } + + return 'elFinder.prototype.mimeTypes = ' . $json . ';' . PHP_EOL; + } +} + +$mimefile = isset($options['mimefile']) ? $options['mimefile'] : $findDefaultPath(array( + 'php/mime.types', + '../php/mime.types', + 'mime.types', +)); + +if ($mimefile !== null && $mimefile !== '' && !is_file($mimefile)) { + fwrite(STDERR, 'mime.types not found: ' . $mimefile . "\n"); + exit(1); +} + +$defaultOutput = $findDefaultPath(array( + 'js/elFinder.mimetypes.js', + '../js/elFinder.mimetypes.js', + 'elFinder.mimetypes.js', +)); +if ($defaultOutput === null) { + $defaultOutput = $scriptDir . DIRECTORY_SEPARATOR . 'elFinder.mimetypes.js'; +} + +$output = isset($options['output']) ? $options['output'] : $defaultOutput; + +try { + $exporter = new MimeTypesExporterPhp55(); + $exporter->setGenerationMimeFile($mimefile); + $js = $exporter->buildJavascript(isset($options['pretty'])); + + if (isset($options['stdout'])) { + fwrite(STDOUT, $js); + exit(0); + } + + $outputDir = dirname($output); + if (!is_dir($outputDir)) { + throw new RuntimeException('Output directory does not exist: ' . $outputDir); + } + + if (file_put_contents($output, $js) === false) { + throw new RuntimeException('Failed to write output file: ' . $output); + } + + fwrite(STDOUT, 'Wrote: ' . $output . "\n"); + exit(0); +} catch (Exception $e) { + fwrite(STDERR, $e->getMessage() . "\n"); + exit(1); +} diff --git a/util/build-mimetypes-cli.php b/util/build-mimetypes-cli.php new file mode 100644 index 000000000..a80bd458f --- /dev/null +++ b/util/build-mimetypes-cli.php @@ -0,0 +1,204 @@ +#!/usr/bin/env php +options['mimeDetect'] = 'internal'; + $this->options['mimefile'] = $mimefile; + } + } + + /** + * Build MIME => representative extension table. + * + * This follows getExtentionByMime(): + * - base table from getMimeTable() + * - then fill missing MIME entries from mimeMap only + * - ignore wildcard extension (*) entries + * + * For forward compatibility, this exporter also accepts both + * 'staticMineMap' (historic typo in core) and 'staticMimeMap'. + */ + public function buildMimeTypesMap(): array + { + $mimeMap = []; + + if (isset($this->options['mimeMap']) && is_array($this->options['mimeMap'])) { + $mimeMap = $this->options['mimeMap']; + } + + if (isset($this->options['staticMineMap']) && is_array($this->options['staticMineMap']) && $this->options['staticMineMap']) { + $mimeMap = array_merge($mimeMap, $this->options['staticMineMap']); + } + + if (isset($this->options['staticMimeMap']) && is_array($this->options['staticMimeMap']) && $this->options['staticMimeMap']) { + $mimeMap = array_merge($mimeMap, $this->options['staticMimeMap']); + } + + if (isset($this->options['additionalMimeMap']) && is_array($this->options['additionalMimeMap']) && $this->options['additionalMimeMap']) { + $mimeMap = array_merge($mimeMap, $this->options['additionalMimeMap']); + } + + $extTable = array(); + foreach ($this->getMimeTable() as $ext => $_mime) { + if (!isset($extTable[$_mime])) { + $extTable[$_mime] = (string)$ext; + } + } + + foreach ($mimeMap as $pair => $_mime) { + $parts = explode(':', (string)$pair, 2); + $ext = isset($parts[0]) ? (string)$parts[0] : ''; + if ($ext !== '*' && !isset($extTable[$_mime])) { + $extTable[$_mime] = $ext; + } + } + + return $extTable; + } + + public function buildJavascript(bool $pretty = false): string + { + $flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; + if ($pretty) { + $flags |= JSON_PRETTY_PRINT; + } + + $json = json_encode($this->buildMimeTypesMap(), $flags); + if ($json === false) { + throw new RuntimeException('json_encode() failed: ' . json_last_error_msg()); + } + + return 'elFinder.prototype.mimeTypes = ' . $json . ';' . PHP_EOL; + } +} + +$mimefile = $options['mimefile'] ?? $findDefaultPath([ + 'php/mime.types', + '../php/mime.types', + 'mime.types', +]); + +if ($mimefile !== null && $mimefile !== '' && !is_file($mimefile)) { + fwrite(STDERR, "mime.types not found: {$mimefile}\n"); + exit(1); +} + +$output = $options['output'] ?? $findDefaultPath([ + 'js/elFinder.mimetypes.js', + '../js/elFinder.mimetypes.js', + 'elFinder.mimetypes.js', +]) ?? ($scriptDir . DIRECTORY_SEPARATOR . 'elFinder.mimetypes.js'); + +try { + $exporter = new MimeTypesExporter(); + $exporter->setGenerationMimeFile($mimefile); + $js = $exporter->buildJavascript(isset($options['pretty'])); + + if (isset($options['stdout'])) { + fwrite(STDOUT, $js); + exit(0); + } + + $outputDir = dirname($output); + if (!is_dir($outputDir)) { + throw new RuntimeException('Output directory does not exist: ' . $outputDir); + } + + if (file_put_contents($output, $js) === false) { + throw new RuntimeException('Failed to write output file: ' . $output); + } + + fwrite(STDOUT, "Wrote: {$output}\n"); + exit(0); +} catch (Throwable $e) { + fwrite(STDERR, $e->getMessage() . "\n"); + exit(1); +}