|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721 |
- <?php
-
-
- namespace yii\helpers;
-
- use Yii;
- use yii\base\ErrorException;
- use yii\base\InvalidConfigException;
- use yii\base\InvalidParamException;
-
-
- class BaseFileHelper
- {
- const PATTERN_NODIR = 1;
- const PATTERN_ENDSWITH = 4;
- const PATTERN_MUSTBEDIR = 8;
- const PATTERN_NEGATIVE = 16;
- const PATTERN_CASE_INSENSITIVE = 32;
-
-
-
- public static $mimeMagicFile = '@yii/helpers/mimeTypes.php';
-
-
-
-
- public static function normalizePath($path, $ds = DIRECTORY_SEPARATOR)
- {
- $path = rtrim(strtr($path, '/\\', $ds . $ds), $ds);
- if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) {
- return $path;
- }
-
- $parts = [];
- foreach (explode($ds, $path) as $part) {
- if ($part === '..' && !empty($parts) && end($parts) !== '..') {
- array_pop($parts);
- } elseif ($part === '.' || $part === '' && !empty($parts)) {
- continue;
- } else {
- $parts[] = $part;
- }
- }
- $path = implode($ds, $parts);
- return $path === '' ? '.' : $path;
- }
-
-
-
- public static function localize($file, $language = null, $sourceLanguage = null)
- {
- if ($language === null) {
- $language = Yii::$app->language;
- }
- if ($sourceLanguage === null) {
- $sourceLanguage = Yii::$app->sourceLanguage;
- }
- if ($language === $sourceLanguage) {
- return $file;
- }
- $desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file);
- if (is_file($desiredFile)) {
- return $desiredFile;
- } else {
- $language = substr($language, 0, 2);
- if ($language === $sourceLanguage) {
- return $file;
- }
- $desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file);
-
- return is_file($desiredFile) ? $desiredFile : $file;
- }
- }
-
-
-
- public static function getMimeType($file, $magicFile = null, $checkExtension = true)
- {
- if ($magicFile !== null) {
- $magicFile = Yii::getAlias($magicFile);
- }
- if (!extension_loaded('fileinfo')) {
- if ($checkExtension) {
- return static::getMimeTypeByExtension($file, $magicFile);
- } else {
- throw new InvalidConfigException('The fileinfo PHP extension is not installed.');
- }
- }
- $info = finfo_open(FILEINFO_MIME_TYPE, $magicFile);
-
- if ($info) {
- $result = finfo_file($info, $file);
- finfo_close($info);
-
- if ($result !== false) {
- return $result;
- }
- }
-
- return $checkExtension ? static::getMimeTypeByExtension($file, $magicFile) : null;
- }
-
-
-
- public static function getMimeTypeByExtension($file, $magicFile = null)
- {
- $mimeTypes = static::loadMimeTypes($magicFile);
-
- if (($ext = pathinfo($file, PATHINFO_EXTENSION)) !== '') {
- $ext = strtolower($ext);
- if (isset($mimeTypes[$ext])) {
- return $mimeTypes[$ext];
- }
- }
-
- return null;
- }
-
-
-
- public static function getExtensionsByMimeType($mimeType, $magicFile = null)
- {
- $mimeTypes = static::loadMimeTypes($magicFile);
- return array_keys($mimeTypes, mb_strtolower($mimeType, 'UTF-8'), true);
- }
-
- private static $_mimeTypes = [];
-
-
-
- protected static function loadMimeTypes($magicFile)
- {
- if ($magicFile === null) {
- $magicFile = static::$mimeMagicFile;
- }
- $magicFile = Yii::getAlias($magicFile);
- if (!isset(self::$_mimeTypes[$magicFile])) {
- self::$_mimeTypes[$magicFile] = require($magicFile);
- }
- return self::$_mimeTypes[$magicFile];
- }
-
-
-
- public static function copyDirectory($src, $dst, $options = [])
- {
- $src = static::normalizePath($src);
- $dst = static::normalizePath($dst);
-
- if ($src === $dst || strpos($dst, $src . DIRECTORY_SEPARATOR) === 0) {
- throw new InvalidParamException('Trying to copy a directory to itself or a subdirectory.');
- }
- if (!is_dir($dst)) {
- static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true);
- }
-
- $handle = opendir($src);
- if ($handle === false) {
- throw new InvalidParamException("Unable to open directory: $src");
- }
- if (!isset($options['basePath'])) {
-
- $options['basePath'] = realpath($src);
- $options = self::normalizeOptions($options);
- }
- while (($file = readdir($handle)) !== false) {
- if ($file === '.' || $file === '..') {
- continue;
- }
- $from = $src . DIRECTORY_SEPARATOR . $file;
- $to = $dst . DIRECTORY_SEPARATOR . $file;
- if (static::filterPath($from, $options)) {
- if (isset($options['beforeCopy']) && !call_user_func($options['beforeCopy'], $from, $to)) {
- continue;
- }
- if (is_file($from)) {
- copy($from, $to);
- if (isset($options['fileMode'])) {
- @chmod($to, $options['fileMode']);
- }
- } else {
-
- if (!isset($options['recursive']) || $options['recursive']) {
- static::copyDirectory($from, $to, $options);
- }
- }
- if (isset($options['afterCopy'])) {
- call_user_func($options['afterCopy'], $from, $to);
- }
- }
- }
- closedir($handle);
- }
-
-
-
- public static function removeDirectory($dir, $options = [])
- {
- if (!is_dir($dir)) {
- return;
- }
- if (isset($options['traverseSymlinks']) && $options['traverseSymlinks'] || !is_link($dir)) {
- if (!($handle = opendir($dir))) {
- return;
- }
- while (($file = readdir($handle)) !== false) {
- if ($file === '.' || $file === '..') {
- continue;
- }
- $path = $dir . DIRECTORY_SEPARATOR . $file;
- if (is_dir($path)) {
- static::removeDirectory($path, $options);
- } else {
- try {
- unlink($path);
- } catch (ErrorException $e) {
- if (DIRECTORY_SEPARATOR === '\\') {
-
- $lines = [];
- exec("DEL /F/Q \"$path\"", $lines, $deleteError);
- } else {
- throw $e;
- }
- }
- }
- }
- closedir($handle);
- }
- if (is_link($dir)) {
- unlink($dir);
- } else {
- rmdir($dir);
- }
- }
-
-
-
- public static function findFiles($dir, $options = [])
- {
- if (!is_dir($dir)) {
- throw new InvalidParamException("The dir argument must be a directory: $dir");
- }
- $dir = rtrim($dir, DIRECTORY_SEPARATOR);
- if (!isset($options['basePath'])) {
-
- $options['basePath'] = realpath($dir);
- $options = self::normalizeOptions($options);
- }
- $list = [];
- $handle = opendir($dir);
- if ($handle === false) {
- throw new InvalidParamException("Unable to open directory: $dir");
- }
- while (($file = readdir($handle)) !== false) {
- if ($file === '.' || $file === '..') {
- continue;
- }
- $path = $dir . DIRECTORY_SEPARATOR . $file;
- if (static::filterPath($path, $options)) {
- if (is_file($path)) {
- $list[] = $path;
- } elseif (!isset($options['recursive']) || $options['recursive']) {
- $list = array_merge($list, static::findFiles($path, $options));
- }
- }
- }
- closedir($handle);
-
- return $list;
- }
-
-
-
- public static function filterPath($path, $options)
- {
- if (isset($options['filter'])) {
- $result = call_user_func($options['filter'], $path);
- if (is_bool($result)) {
- return $result;
- }
- }
-
- if (empty($options['except']) && empty($options['only'])) {
- return true;
- }
-
- $path = str_replace('\\', '/', $path);
-
- if (!empty($options['except'])) {
- if (($except = self::lastExcludeMatchingFromList($options['basePath'], $path, $options['except'])) !== null) {
- return $except['flags'] & self::PATTERN_NEGATIVE;
- }
- }
-
- if (!empty($options['only']) && !is_dir($path)) {
- if (($except = self::lastExcludeMatchingFromList($options['basePath'], $path, $options['only'])) !== null) {
-
- return true;
- }
-
- return false;
- }
-
- return true;
- }
-
-
-
- public static function createDirectory($path, $mode = 0775, $recursive = true)
- {
- if (is_dir($path)) {
- return true;
- }
- $parentDir = dirname($path);
-
- if ($recursive && !is_dir($parentDir) && $parentDir !== $path) {
- static::createDirectory($parentDir, $mode, true);
- }
- try {
- if (!mkdir($path, $mode)) {
- return false;
- }
- } catch (\Exception $e) {
- if (!is_dir($path)) {
- throw new \yii\base\Exception("Failed to create directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);
- }
- }
- try {
- return chmod($path, $mode);
- } catch (\Exception $e) {
- throw new \yii\base\Exception("Failed to change permissions for directory \"$path\": " . $e->getMessage(), $e->getCode(), $e);
- }
- }
-
-
-
- private static function matchBasename($baseName, $pattern, $firstWildcard, $flags)
- {
- if ($firstWildcard === false) {
- if ($pattern === $baseName) {
- return true;
- }
- } elseif ($flags & self::PATTERN_ENDSWITH) {
-
- $n = StringHelper::byteLength($pattern);
- if (StringHelper::byteSubstr($pattern, 1, $n) === StringHelper::byteSubstr($baseName, -$n, $n)) {
- return true;
- }
- }
-
- $fnmatchFlags = 0;
- if ($flags & self::PATTERN_CASE_INSENSITIVE) {
- $fnmatchFlags |= FNM_CASEFOLD;
- }
-
- return fnmatch($pattern, $baseName, $fnmatchFlags);
- }
-
-
-
- private static function matchPathname($path, $basePath, $pattern, $firstWildcard, $flags)
- {
-
- if (isset($pattern[0]) && $pattern[0] === '/') {
- $pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern));
- if ($firstWildcard !== false && $firstWildcard !== 0) {
- $firstWildcard--;
- }
- }
-
- $namelen = StringHelper::byteLength($path) - (empty($basePath) ? 0 : StringHelper::byteLength($basePath) + 1);
- $name = StringHelper::byteSubstr($path, -$namelen, $namelen);
-
- if ($firstWildcard !== 0) {
- if ($firstWildcard === false) {
- $firstWildcard = StringHelper::byteLength($pattern);
- }
-
- if ($firstWildcard > $namelen) {
- return false;
- }
-
- if (strncmp($pattern, $name, $firstWildcard)) {
- return false;
- }
- $pattern = StringHelper::byteSubstr($pattern, $firstWildcard, StringHelper::byteLength($pattern));
- $name = StringHelper::byteSubstr($name, $firstWildcard, $namelen);
-
-
- if (empty($pattern) && empty($name)) {
- return true;
- }
- }
-
- $fnmatchFlags = FNM_PATHNAME;
- if ($flags & self::PATTERN_CASE_INSENSITIVE) {
- $fnmatchFlags |= FNM_CASEFOLD;
- }
-
- return fnmatch($pattern, $name, $fnmatchFlags);
- }
-
-
-
- private static function lastExcludeMatchingFromList($basePath, $path, $excludes)
- {
- foreach (array_reverse($excludes) as $exclude) {
- if (is_string($exclude)) {
- $exclude = self::parseExcludePattern($exclude, false);
- }
- if (!isset($exclude['pattern']) || !isset($exclude['flags']) || !isset($exclude['firstWildcard'])) {
- throw new InvalidParamException('If exclude/include pattern is an array it must contain the pattern, flags and firstWildcard keys.');
- }
- if ($exclude['flags'] & self::PATTERN_MUSTBEDIR && !is_dir($path)) {
- continue;
- }
-
- if ($exclude['flags'] & self::PATTERN_NODIR) {
- if (self::matchBasename(basename($path), $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) {
- return $exclude;
- }
- continue;
- }
-
- if (self::matchPathname($path, $basePath, $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) {
- return $exclude;
- }
- }
-
- return null;
- }
-
-
-
- private static function parseExcludePattern($pattern, $caseSensitive)
- {
- if (!is_string($pattern)) {
- throw new InvalidParamException('Exclude/include pattern must be a string.');
- }
-
- $result = [
- 'pattern' => $pattern,
- 'flags' => 0,
- 'firstWildcard' => false,
- ];
-
- if (!$caseSensitive) {
- $result['flags'] |= self::PATTERN_CASE_INSENSITIVE;
- }
-
- if (!isset($pattern[0])) {
- return $result;
- }
-
- if ($pattern[0] === '!') {
- $result['flags'] |= self::PATTERN_NEGATIVE;
- $pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern));
- }
- if (StringHelper::byteLength($pattern) && StringHelper::byteSubstr($pattern, -1, 1) === '/') {
- $pattern = StringHelper::byteSubstr($pattern, 0, -1);
- $result['flags'] |= self::PATTERN_MUSTBEDIR;
- }
- if (strpos($pattern, '/') === false) {
- $result['flags'] |= self::PATTERN_NODIR;
- }
- $result['firstWildcard'] = self::firstWildcardInPattern($pattern);
- if ($pattern[0] === '*' && self::firstWildcardInPattern(StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern))) === false) {
- $result['flags'] |= self::PATTERN_ENDSWITH;
- }
- $result['pattern'] = $pattern;
-
- return $result;
- }
-
-
-
- private static function firstWildcardInPattern($pattern)
- {
- $wildcards = ['*', '?', '[', '\\'];
- $wildcardSearch = function ($r, $c) use ($pattern) {
- $p = strpos($pattern, $c);
-
- return $r === false ? $p : ($p === false ? $r : min($r, $p));
- };
-
- return array_reduce($wildcards, $wildcardSearch, false);
- }
-
-
-
- private static function normalizeOptions(array $options)
- {
- if (!array_key_exists('caseSensitive', $options)) {
- $options['caseSensitive'] = true;
- }
- if (isset($options['except'])) {
- foreach ($options['except'] as $key => $value) {
- if (is_string($value)) {
- $options['except'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
- }
- }
- }
- if (isset($options['only'])) {
- foreach ($options['only'] as $key => $value) {
- if (is_string($value)) {
- $options['only'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
- }
- }
- }
- return $options;
- }
- }
|