This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* ArrayHelper provides additional array functionality that you can use in your
* application.
*
* For more details and usage information on ArrayHelper, see the [guide article on array helpers](guide:helper-array).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ArrayHelper extends BaseArrayHelper
{
}

View File

@@ -0,0 +1,965 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
use Yii;
use yii\base\Arrayable;
use yii\base\InvalidArgumentException;
/**
* BaseArrayHelper provides concrete implementation for [[ArrayHelper]].
*
* Do not use BaseArrayHelper. Use [[ArrayHelper]] instead.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class BaseArrayHelper
{
/**
* Converts an object or an array of objects into an array.
* @param object|array|string $object the object to be converted into an array
* @param array $properties a mapping from object class names to the properties that need to put into the resulting arrays.
* The properties specified for each class is an array of the following format:
*
* ```php
* [
* 'app\models\Post' => [
* 'id',
* 'title',
* // the key name in array result => property name
* 'createTime' => 'created_at',
* // the key name in array result => anonymous function
* 'length' => function ($post) {
* return strlen($post->content);
* },
* ],
* ]
* ```
*
* The result of `ArrayHelper::toArray($post, $properties)` could be like the following:
*
* ```php
* [
* 'id' => 123,
* 'title' => 'test',
* 'createTime' => '2013-01-01 12:00AM',
* 'length' => 301,
* ]
* ```
*
* @param bool $recursive whether to recursively converts properties which are objects into arrays.
* @return array the array representation of the object
*/
public static function toArray($object, $properties = [], $recursive = true)
{
if (is_array($object)) {
if ($recursive) {
foreach ($object as $key => $value) {
if (is_array($value) || is_object($value)) {
$object[$key] = static::toArray($value, $properties, true);
}
}
}
return $object;
} elseif (is_object($object)) {
if (!empty($properties)) {
$className = get_class($object);
if (!empty($properties[$className])) {
$result = [];
foreach ($properties[$className] as $key => $name) {
if (is_int($key)) {
$result[$name] = $object->$name;
} else {
$result[$key] = static::getValue($object, $name);
}
}
return $recursive ? static::toArray($result, $properties) : $result;
}
}
if ($object instanceof Arrayable) {
$result = $object->toArray([], [], $recursive);
} else {
$result = [];
foreach ($object as $key => $value) {
$result[$key] = $value;
}
}
return $recursive ? static::toArray($result, $properties) : $result;
}
return [$object];
}
/**
* Merges two or more arrays into one recursively.
* If each array has an element with the same string key value, the latter
* will overwrite the former (different from array_merge_recursive).
* Recursive merging will be conducted if both arrays have an element of array
* type and are having the same key.
* For integer-keyed elements, the elements from the latter array will
* be appended to the former array.
* You can use [[UnsetArrayValue]] object to unset value from previous array or
* [[ReplaceArrayValue]] to force replace former value instead of recursive merging.
* @param array $a array to be merged to
* @param array $b array to be merged from. You can specify additional
* arrays via third argument, fourth argument etc.
* @return array the merged array (the original arrays are not changed.)
*/
public static function merge($a, $b)
{
$args = func_get_args();
$res = array_shift($args);
while (!empty($args)) {
foreach (array_shift($args) as $k => $v) {
if ($v instanceof UnsetArrayValue) {
unset($res[$k]);
} elseif ($v instanceof ReplaceArrayValue) {
$res[$k] = $v->value;
} elseif (is_int($k)) {
if (array_key_exists($k, $res)) {
$res[] = $v;
} else {
$res[$k] = $v;
}
} elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
$res[$k] = self::merge($res[$k], $v);
} else {
$res[$k] = $v;
}
}
}
return $res;
}
/**
* Retrieves the value of an array element or object property with the given key or property name.
* If the key does not exist in the array or object, the default value will be returned instead.
*
* The key may be specified in a dot format to retrieve the value of a sub-array or the property
* of an embedded object. In particular, if the key is `x.y.z`, then the returned value would
* be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']`
* or `$array->x` is neither an array nor an object, the default value will be returned.
* Note that if the array already has an element `x.y.z`, then its value will be returned
* instead of going through the sub-arrays. So it is better to be done specifying an array of key names
* like `['x', 'y', 'z']`.
*
* Below are some usage examples,
*
* ```php
* // working with array
* $username = \yii\helpers\ArrayHelper::getValue($_POST, 'username');
* // working with object
* $username = \yii\helpers\ArrayHelper::getValue($user, 'username');
* // working with anonymous function
* $fullName = \yii\helpers\ArrayHelper::getValue($user, function ($user, $defaultValue) {
* return $user->firstName . ' ' . $user->lastName;
* });
* // using dot format to retrieve the property of embedded object
* $street = \yii\helpers\ArrayHelper::getValue($users, 'address.street');
* // using an array of keys to retrieve the value
* $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']);
* ```
*
* @param array|object $array array or object to extract value from
* @param string|\Closure|array $key key name of the array element, an array of keys or property name of the object,
* or an anonymous function returning the value. The anonymous function signature should be:
* `function($array, $defaultValue)`.
* The possibility to pass an array of keys is available since version 2.0.4.
* @param mixed $default the default value to be returned if the specified array key does not exist. Not used when
* getting value from an object.
* @return mixed the value of the element if found, default value otherwise
*/
public static function getValue($array, $key, $default = null)
{
if ($key instanceof \Closure) {
return $key($array, $default);
}
if (is_array($key)) {
$lastKey = array_pop($key);
foreach ($key as $keyPart) {
$array = static::getValue($array, $keyPart);
}
$key = $lastKey;
}
if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) {
return $array[$key];
}
if (($pos = strrpos($key, '.')) !== false) {
$array = static::getValue($array, substr($key, 0, $pos), $default);
$key = substr($key, $pos + 1);
}
if (is_object($array)) {
// this is expected to fail if the property does not exist, or __get() is not implemented
// it is not reliably possible to check whether a property is accessible beforehand
return $array->$key;
} elseif (is_array($array)) {
return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default;
}
return $default;
}
/**
* Writes a value into an associative array at the key path specified.
* If there is no such key path yet, it will be created recursively.
* If the key exists, it will be overwritten.
*
* ```php
* $array = [
* 'key' => [
* 'in' => [
* 'val1',
* 'key' => 'val'
* ]
* ]
* ];
* ```
*
* The result of `ArrayHelper::setValue($array, 'key.in.0', ['arr' => 'val']);` will be the following:
*
* ```php
* [
* 'key' => [
* 'in' => [
* ['arr' => 'val'],
* 'key' => 'val'
* ]
* ]
* ]
*
* ```
*
* The result of
* `ArrayHelper::setValue($array, 'key.in', ['arr' => 'val']);` or
* `ArrayHelper::setValue($array, ['key', 'in'], ['arr' => 'val']);`
* will be the following:
*
* ```php
* [
* 'key' => [
* 'in' => [
* 'arr' => 'val'
* ]
* ]
* ]
* ```
*
* @param array $array the array to write the value to
* @param string|array|null $path the path of where do you want to write a value to `$array`
* the path can be described by a string when each key should be separated by a dot
* you can also describe the path as an array of keys
* if the path is null then `$array` will be assigned the `$value`
* @param mixed $value the value to be written
* @since 2.0.13
*/
public static function setValue(&$array, $path, $value)
{
if ($path === null) {
$array = $value;
return;
}
$keys = is_array($path) ? $path : explode('.', $path);
while (count($keys) > 1) {
$key = array_shift($keys);
if (!isset($array[$key])) {
$array[$key] = [];
}
if (!is_array($array[$key])) {
$array[$key] = [$array[$key]];
}
$array = &$array[$key];
}
$array[array_shift($keys)] = $value;
}
/**
* Removes an item from an array and returns the value. If the key does not exist in the array, the default value
* will be returned instead.
*
* Usage examples,
*
* ```php
* // $array = ['type' => 'A', 'options' => [1, 2]];
* // working with array
* $type = \yii\helpers\ArrayHelper::remove($array, 'type');
* // $array content
* // $array = ['options' => [1, 2]];
* ```
*
* @param array $array the array to extract value from
* @param string $key key name of the array element
* @param mixed $default the default value to be returned if the specified key does not exist
* @return mixed|null the value of the element if found, default value otherwise
*/
public static function remove(&$array, $key, $default = null)
{
if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) {
$value = $array[$key];
unset($array[$key]);
return $value;
}
return $default;
}
/**
* Removes items with matching values from the array and returns the removed items.
*
* Example,
*
* ```php
* $array = ['Bob' => 'Dylan', 'Michael' => 'Jackson', 'Mick' => 'Jagger', 'Janet' => 'Jackson'];
* $removed = \yii\helpers\ArrayHelper::removeValue($array, 'Jackson');
* // result:
* // $array = ['Bob' => 'Dylan', 'Mick' => 'Jagger'];
* // $removed = ['Michael' => 'Jackson', 'Janet' => 'Jackson'];
* ```
*
* @param array $array the array where to look the value from
* @param string $value the value to remove from the array
* @return array the items that were removed from the array
* @since 2.0.11
*/
public static function removeValue(&$array, $value)
{
$result = [];
if (is_array($array)) {
foreach ($array as $key => $val) {
if ($val === $value) {
$result[$key] = $val;
unset($array[$key]);
}
}
}
return $result;
}
/**
* Indexes and/or groups the array according to a specified key.
* The input should be either multidimensional array or an array of objects.
*
* The $key can be either a key name of the sub-array, a property name of object, or an anonymous
* function that must return the value that will be used as a key.
*
* $groups is an array of keys, that will be used to group the input array into one or more sub-arrays based
* on keys specified.
*
* If the `$key` is specified as `null` or a value of an element corresponding to the key is `null` in addition
* to `$groups` not specified then the element is discarded.
*
* For example:
*
* ```php
* $array = [
* ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
* ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
* ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
* ];
* $result = ArrayHelper::index($array, 'id');
* ```
*
* The result will be an associative array, where the key is the value of `id` attribute
*
* ```php
* [
* '123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
* '345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone']
* // The second element of an original array is overwritten by the last element because of the same id
* ]
* ```
*
* An anonymous function can be used in the grouping array as well.
*
* ```php
* $result = ArrayHelper::index($array, function ($element) {
* return $element['id'];
* });
* ```
*
* Passing `id` as a third argument will group `$array` by `id`:
*
* ```php
* $result = ArrayHelper::index($array, null, 'id');
* ```
*
* The result will be a multidimensional array grouped by `id` on the first level, by `device` on the second level
* and indexed by `data` on the third level:
*
* ```php
* [
* '123' => [
* ['id' => '123', 'data' => 'abc', 'device' => 'laptop']
* ],
* '345' => [ // all elements with this index are present in the result array
* ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
* ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
* ]
* ]
* ```
*
* The anonymous function can be used in the array of grouping keys as well:
*
* ```php
* $result = ArrayHelper::index($array, 'data', [function ($element) {
* return $element['id'];
* }, 'device']);
* ```
*
* The result will be a multidimensional array grouped by `id` on the first level, by the `device` on the second one
* and indexed by the `data` on the third level:
*
* ```php
* [
* '123' => [
* 'laptop' => [
* 'abc' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop']
* ]
* ],
* '345' => [
* 'tablet' => [
* 'def' => ['id' => '345', 'data' => 'def', 'device' => 'tablet']
* ],
* 'smartphone' => [
* 'hgi' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone']
* ]
* ]
* ]
* ```
*
* @param array $array the array that needs to be indexed or grouped
* @param string|\Closure|null $key the column name or anonymous function which result will be used to index the array
* @param string|string[]|\Closure[]|null $groups the array of keys, that will be used to group the input array
* by one or more keys. If the $key attribute or its value for the particular element is null and $groups is not
* defined, the array element will be discarded. Otherwise, if $groups is specified, array element will be added
* to the result array without any key. This parameter is available since version 2.0.8.
* @return array the indexed and/or grouped array
*/
public static function index($array, $key, $groups = [])
{
$result = [];
$groups = (array) $groups;
foreach ($array as $element) {
$lastArray = &$result;
foreach ($groups as $group) {
$value = static::getValue($element, $group);
if (!array_key_exists($value, $lastArray)) {
$lastArray[$value] = [];
}
$lastArray = &$lastArray[$value];
}
if ($key === null) {
if (!empty($groups)) {
$lastArray[] = $element;
}
} else {
$value = static::getValue($element, $key);
if ($value !== null) {
if (is_float($value)) {
$value = StringHelper::floatToString($value);
}
$lastArray[$value] = $element;
}
}
unset($lastArray);
}
return $result;
}
/**
* Returns the values of a specified column in an array.
* The input array should be multidimensional or an array of objects.
*
* For example,
*
* ```php
* $array = [
* ['id' => '123', 'data' => 'abc'],
* ['id' => '345', 'data' => 'def'],
* ];
* $result = ArrayHelper::getColumn($array, 'id');
* // the result is: ['123', '345']
*
* // using anonymous function
* $result = ArrayHelper::getColumn($array, function ($element) {
* return $element['id'];
* });
* ```
*
* @param array $array
* @param string|\Closure $name
* @param bool $keepKeys whether to maintain the array keys. If false, the resulting array
* will be re-indexed with integers.
* @return array the list of column values
*/
public static function getColumn($array, $name, $keepKeys = true)
{
$result = [];
if ($keepKeys) {
foreach ($array as $k => $element) {
$result[$k] = static::getValue($element, $name);
}
} else {
foreach ($array as $element) {
$result[] = static::getValue($element, $name);
}
}
return $result;
}
/**
* Builds a map (key-value pairs) from a multidimensional array or an array of objects.
* The `$from` and `$to` parameters specify the key names or property names to set up the map.
* Optionally, one can further group the map according to a grouping field `$group`.
*
* For example,
*
* ```php
* $array = [
* ['id' => '123', 'name' => 'aaa', 'class' => 'x'],
* ['id' => '124', 'name' => 'bbb', 'class' => 'x'],
* ['id' => '345', 'name' => 'ccc', 'class' => 'y'],
* ];
*
* $result = ArrayHelper::map($array, 'id', 'name');
* // the result is:
* // [
* // '123' => 'aaa',
* // '124' => 'bbb',
* // '345' => 'ccc',
* // ]
*
* $result = ArrayHelper::map($array, 'id', 'name', 'class');
* // the result is:
* // [
* // 'x' => [
* // '123' => 'aaa',
* // '124' => 'bbb',
* // ],
* // 'y' => [
* // '345' => 'ccc',
* // ],
* // ]
* ```
*
* @param array $array
* @param string|\Closure $from
* @param string|\Closure $to
* @param string|\Closure $group
* @return array
*/
public static function map($array, $from, $to, $group = null)
{
$result = [];
foreach ($array as $element) {
$key = static::getValue($element, $from);
$value = static::getValue($element, $to);
if ($group !== null) {
$result[static::getValue($element, $group)][$key] = $value;
} else {
$result[$key] = $value;
}
}
return $result;
}
/**
* Checks if the given array contains the specified key.
* This method enhances the `array_key_exists()` function by supporting case-insensitive
* key comparison.
* @param string $key the key to check
* @param array $array the array with keys to check
* @param bool $caseSensitive whether the key comparison should be case-sensitive
* @return bool whether the array contains the specified key
*/
public static function keyExists($key, $array, $caseSensitive = true)
{
if ($caseSensitive) {
// Function `isset` checks key faster but skips `null`, `array_key_exists` handles this case
// http://php.net/manual/en/function.array-key-exists.php#107786
return isset($array[$key]) || array_key_exists($key, $array);
}
foreach (array_keys($array) as $k) {
if (strcasecmp($key, $k) === 0) {
return true;
}
}
return false;
}
/**
* Sorts an array of objects or arrays (with the same structure) by one or several keys.
* @param array $array the array to be sorted. The array will be modified after calling this method.
* @param string|\Closure|array $key the key(s) to be sorted by. This refers to a key name of the sub-array
* elements, a property name of the objects, or an anonymous function returning the values for comparison
* purpose. The anonymous function signature should be: `function($item)`.
* To sort by multiple keys, provide an array of keys here.
* @param int|array $direction the sorting direction. It can be either `SORT_ASC` or `SORT_DESC`.
* When sorting by multiple keys with different sorting directions, use an array of sorting directions.
* @param int|array $sortFlag the PHP sort flag. Valid values include
* `SORT_REGULAR`, `SORT_NUMERIC`, `SORT_STRING`, `SORT_LOCALE_STRING`, `SORT_NATURAL` and `SORT_FLAG_CASE`.
* Please refer to [PHP manual](http://php.net/manual/en/function.sort.php)
* for more details. When sorting by multiple keys with different sort flags, use an array of sort flags.
* @throws InvalidArgumentException if the $direction or $sortFlag parameters do not have
* correct number of elements as that of $key.
*/
public static function multisort(&$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR)
{
$keys = is_array($key) ? $key : [$key];
if (empty($keys) || empty($array)) {
return;
}
$n = count($keys);
if (is_scalar($direction)) {
$direction = array_fill(0, $n, $direction);
} elseif (count($direction) !== $n) {
throw new InvalidArgumentException('The length of $direction parameter must be the same as that of $keys.');
}
if (is_scalar($sortFlag)) {
$sortFlag = array_fill(0, $n, $sortFlag);
} elseif (count($sortFlag) !== $n) {
throw new InvalidArgumentException('The length of $sortFlag parameter must be the same as that of $keys.');
}
$args = [];
foreach ($keys as $i => $key) {
$flag = $sortFlag[$i];
$args[] = static::getColumn($array, $key);
$args[] = $direction[$i];
$args[] = $flag;
}
// This fix is used for cases when main sorting specified by columns has equal values
// Without it it will lead to Fatal Error: Nesting level too deep - recursive dependency?
$args[] = range(1, count($array));
$args[] = SORT_ASC;
$args[] = SORT_NUMERIC;
$args[] = &$array;
call_user_func_array('array_multisort', $args);
}
/**
* Encodes special characters in an array of strings into HTML entities.
* Only array values will be encoded by default.
* If a value is an array, this method will also encode it recursively.
* Only string values will be encoded.
* @param array $data data to be encoded
* @param bool $valuesOnly whether to encode array values only. If false,
* both the array keys and array values will be encoded.
* @param string $charset the charset that the data is using. If not set,
* [[\yii\base\Application::charset]] will be used.
* @return array the encoded data
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
*/
public static function htmlEncode($data, $valuesOnly = true, $charset = null)
{
if ($charset === null) {
$charset = Yii::$app ? Yii::$app->charset : 'UTF-8';
}
$d = [];
foreach ($data as $key => $value) {
if (!$valuesOnly && is_string($key)) {
$key = htmlspecialchars($key, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
}
if (is_string($value)) {
$d[$key] = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
} elseif (is_array($value)) {
$d[$key] = static::htmlEncode($value, $valuesOnly, $charset);
} else {
$d[$key] = $value;
}
}
return $d;
}
/**
* Decodes HTML entities into the corresponding characters in an array of strings.
* Only array values will be decoded by default.
* If a value is an array, this method will also decode it recursively.
* Only string values will be decoded.
* @param array $data data to be decoded
* @param bool $valuesOnly whether to decode array values only. If false,
* both the array keys and array values will be decoded.
* @return array the decoded data
* @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
*/
public static function htmlDecode($data, $valuesOnly = true)
{
$d = [];
foreach ($data as $key => $value) {
if (!$valuesOnly && is_string($key)) {
$key = htmlspecialchars_decode($key, ENT_QUOTES);
}
if (is_string($value)) {
$d[$key] = htmlspecialchars_decode($value, ENT_QUOTES);
} elseif (is_array($value)) {
$d[$key] = static::htmlDecode($value);
} else {
$d[$key] = $value;
}
}
return $d;
}
/**
* Returns a value indicating whether the given array is an associative array.
*
* An array is associative if all its keys are strings. If `$allStrings` is false,
* then an array will be treated as associative if at least one of its keys is a string.
*
* Note that an empty array will NOT be considered associative.
*
* @param array $array the array being checked
* @param bool $allStrings whether the array keys must be all strings in order for
* the array to be treated as associative.
* @return bool whether the array is associative
*/
public static function isAssociative($array, $allStrings = true)
{
if (!is_array($array) || empty($array)) {
return false;
}
if ($allStrings) {
foreach ($array as $key => $value) {
if (!is_string($key)) {
return false;
}
}
return true;
}
foreach ($array as $key => $value) {
if (is_string($key)) {
return true;
}
}
return false;
}
/**
* Returns a value indicating whether the given array is an indexed array.
*
* An array is indexed if all its keys are integers. If `$consecutive` is true,
* then the array keys must be a consecutive sequence starting from 0.
*
* Note that an empty array will be considered indexed.
*
* @param array $array the array being checked
* @param bool $consecutive whether the array keys must be a consecutive sequence
* in order for the array to be treated as indexed.
* @return bool whether the array is indexed
*/
public static function isIndexed($array, $consecutive = false)
{
if (!is_array($array)) {
return false;
}
if (empty($array)) {
return true;
}
if ($consecutive) {
return array_keys($array) === range(0, count($array) - 1);
}
foreach ($array as $key => $value) {
if (!is_int($key)) {
return false;
}
}
return true;
}
/**
* Check whether an array or [[\Traversable]] contains an element.
*
* This method does the same as the PHP function [in_array()](http://php.net/manual/en/function.in-array.php)
* but additionally works for objects that implement the [[\Traversable]] interface.
* @param mixed $needle The value to look for.
* @param array|\Traversable $haystack The set of values to search.
* @param bool $strict Whether to enable strict (`===`) comparison.
* @return bool `true` if `$needle` was found in `$haystack`, `false` otherwise.
* @throws InvalidArgumentException if `$haystack` is neither traversable nor an array.
* @see http://php.net/manual/en/function.in-array.php
* @since 2.0.7
*/
public static function isIn($needle, $haystack, $strict = false)
{
if ($haystack instanceof \Traversable) {
foreach ($haystack as $value) {
if ($needle == $value && (!$strict || $needle === $value)) {
return true;
}
}
} elseif (is_array($haystack)) {
return in_array($needle, $haystack, $strict);
} else {
throw new InvalidArgumentException('Argument $haystack must be an array or implement Traversable');
}
return false;
}
/**
* Checks whether a variable is an array or [[\Traversable]].
*
* This method does the same as the PHP function [is_array()](http://php.net/manual/en/function.is-array.php)
* but additionally works on objects that implement the [[\Traversable]] interface.
* @param mixed $var The variable being evaluated.
* @return bool whether $var is array-like
* @see http://php.net/manual/en/function.is-array.php
* @since 2.0.8
*/
public static function isTraversable($var)
{
return is_array($var) || $var instanceof \Traversable;
}
/**
* Checks whether an array or [[\Traversable]] is a subset of another array or [[\Traversable]].
*
* This method will return `true`, if all elements of `$needles` are contained in
* `$haystack`. If at least one element is missing, `false` will be returned.
* @param array|\Traversable $needles The values that must **all** be in `$haystack`.
* @param array|\Traversable $haystack The set of value to search.
* @param bool $strict Whether to enable strict (`===`) comparison.
* @throws InvalidArgumentException if `$haystack` or `$needles` is neither traversable nor an array.
* @return bool `true` if `$needles` is a subset of `$haystack`, `false` otherwise.
* @since 2.0.7
*/
public static function isSubset($needles, $haystack, $strict = false)
{
if (is_array($needles) || $needles instanceof \Traversable) {
foreach ($needles as $needle) {
if (!static::isIn($needle, $haystack, $strict)) {
return false;
}
}
return true;
}
throw new InvalidArgumentException('Argument $needles must be an array or implement Traversable');
}
/**
* Filters array according to rules specified.
*
* For example:
*
* ```php
* $array = [
* 'A' => [1, 2],
* 'B' => [
* 'C' => 1,
* 'D' => 2,
* ],
* 'E' => 1,
* ];
*
* $result = \yii\helpers\ArrayHelper::filter($array, ['A']);
* // $result will be:
* // [
* // 'A' => [1, 2],
* // ]
*
* $result = \yii\helpers\ArrayHelper::filter($array, ['A', 'B.C']);
* // $result will be:
* // [
* // 'A' => [1, 2],
* // 'B' => ['C' => 1],
* // ]
*
* $result = \yii\helpers\ArrayHelper::filter($array, ['B', '!B.C']);
* // $result will be:
* // [
* // 'B' => ['D' => 2],
* // ]
* ```
*
* @param array $array Source array
* @param array $filters Rules that define array keys which should be left or removed from results.
* Each rule is:
* - `var` - `$array['var']` will be left in result.
* - `var.key` = only `$array['var']['key'] will be left in result.
* - `!var.key` = `$array['var']['key'] will be removed from result.
* @return array Filtered array
* @since 2.0.9
*/
public static function filter($array, $filters)
{
$result = [];
$forbiddenVars = [];
foreach ($filters as $var) {
$keys = explode('.', $var);
$globalKey = $keys[0];
$localKey = isset($keys[1]) ? $keys[1] : null;
if ($globalKey[0] === '!') {
$forbiddenVars[] = [
substr($globalKey, 1),
$localKey,
];
continue;
}
if (!array_key_exists($globalKey, $array)) {
continue;
}
if ($localKey === null) {
$result[$globalKey] = $array[$globalKey];
continue;
}
if (!isset($array[$globalKey][$localKey])) {
continue;
}
if (!array_key_exists($globalKey, $result)) {
$result[$globalKey] = [];
}
$result[$globalKey][$localKey] = $array[$globalKey][$localKey];
}
foreach ($forbiddenVars as $var) {
list($globalKey, $localKey) = $var;
if (array_key_exists($globalKey, $result)) {
unset($result[$globalKey][$localKey]);
}
}
return $result;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,861 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
use Yii;
use yii\base\ErrorException;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
/**
* BaseFileHelper provides concrete implementation for [[FileHelper]].
*
* Do not use BaseFileHelper. Use [[FileHelper]] instead.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class BaseFileHelper
{
const PATTERN_NODIR = 1;
const PATTERN_ENDSWITH = 4;
const PATTERN_MUSTBEDIR = 8;
const PATTERN_NEGATIVE = 16;
const PATTERN_CASE_INSENSITIVE = 32;
/**
* @var string the path (or alias) of a PHP file containing MIME type information.
*/
public static $mimeMagicFile = '@yii/helpers/mimeTypes.php';
/**
* @var string the path (or alias) of a PHP file containing MIME aliases.
* @since 2.0.14
*/
public static $mimeAliasesFile = '@yii/helpers/mimeAliases.php';
/**
* Normalizes a file/directory path.
*
* The normalization does the following work:
*
* - Convert all directory separators into `DIRECTORY_SEPARATOR` (e.g. "\a/b\c" becomes "/a/b/c")
* - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c")
* - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c")
* - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c")
*
* @param string $path the file/directory path to be normalized
* @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`.
* @return string the normalized file/directory path
*/
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;
}
// the path may contain ".", ".." or double slashes, need to clean them up
if (strpos($path, "{$ds}{$ds}") === 0 && $ds == '\\') {
$parts = [$ds];
} else {
$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;
}
/**
* Returns the localized version of a specified file.
*
* The searching is based on the specified language code. In particular,
* a file with the same name will be looked for under the subdirectory
* whose name is the same as the language code. For example, given the file "path/to/view.php"
* and language code "zh-CN", the localized file will be looked for as
* "path/to/zh-CN/view.php". If the file is not found, it will try a fallback with just a language code that is
* "zh" i.e. "path/to/zh/view.php". If it is not found as well the original file will be returned.
*
* If the target and the source language codes are the same,
* the original file will be returned.
*
* @param string $file the original file
* @param string $language the target language that the file should be localized to.
* If not set, the value of [[\yii\base\Application::language]] will be used.
* @param string $sourceLanguage the language that the original file is in.
* If not set, the value of [[\yii\base\Application::sourceLanguage]] will be used.
* @return string the matching localized file, or the original file if the localized version is not found.
* If the target and the source language codes are the same, the original file will be returned.
*/
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;
}
$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;
}
/**
* Determines the MIME type of the specified file.
* This method will first try to determine the MIME type based on
* [finfo_open](http://php.net/manual/en/function.finfo-open.php). If the `fileinfo` extension is not installed,
* it will fall back to [[getMimeTypeByExtension()]] when `$checkExtension` is true.
* @param string $file the file name.
* @param string $magicFile name of the optional magic database file (or alias), usually something like `/path/to/magic.mime`.
* This will be passed as the second parameter to [finfo_open()](http://php.net/manual/en/function.finfo-open.php)
* when the `fileinfo` extension is installed. If the MIME type is being determined based via [[getMimeTypeByExtension()]]
* and this is null, it will use the file specified by [[mimeMagicFile]].
* @param bool $checkExtension whether to use the file extension to determine the MIME type in case
* `finfo_open()` cannot determine it.
* @return string the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined.
* @throws InvalidConfigException when the `fileinfo` PHP extension is not installed and `$checkExtension` is `false`.
*/
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);
}
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;
}
/**
* Determines the MIME type based on the extension name of the specified file.
* This method will use a local map between extension names and MIME types.
* @param string $file the file name.
* @param string $magicFile the path (or alias) of the file that contains all available MIME type information.
* If this is not set, the file specified by [[mimeMagicFile]] will be used.
* @return string|null the MIME type. Null is returned if the MIME type cannot be determined.
*/
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;
}
/**
* Determines the extensions by given MIME type.
* This method will use a local map between extension names and MIME types.
* @param string $mimeType file MIME type.
* @param string $magicFile the path (or alias) of the file that contains all available MIME type information.
* If this is not set, the file specified by [[mimeMagicFile]] will be used.
* @return array the extensions corresponding to the specified MIME type
*/
public static function getExtensionsByMimeType($mimeType, $magicFile = null)
{
$aliases = static::loadMimeAliases(static::$mimeAliasesFile);
if (isset($aliases[$mimeType])) {
$mimeType = $aliases[$mimeType];
}
$mimeTypes = static::loadMimeTypes($magicFile);
return array_keys($mimeTypes, mb_strtolower($mimeType, 'UTF-8'), true);
}
private static $_mimeTypes = [];
/**
* Loads MIME types from the specified file.
* @param string $magicFile the path (or alias) of the file that contains all available MIME type information.
* If this is not set, the file specified by [[mimeMagicFile]] will be used.
* @return array the mapping from file extensions to MIME types
*/
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];
}
private static $_mimeAliases = [];
/**
* Loads MIME aliases from the specified file.
* @param string $aliasesFile the path (or alias) of the file that contains MIME type aliases.
* If this is not set, the file specified by [[mimeAliasesFile]] will be used.
* @return array the mapping from file extensions to MIME types
* @since 2.0.14
*/
protected static function loadMimeAliases($aliasesFile)
{
if ($aliasesFile === null) {
$aliasesFile = static::$mimeAliasesFile;
}
$aliasesFile = Yii::getAlias($aliasesFile);
if (!isset(self::$_mimeAliases[$aliasesFile])) {
self::$_mimeAliases[$aliasesFile] = require $aliasesFile;
}
return self::$_mimeAliases[$aliasesFile];
}
/**
* Copies a whole directory as another one.
* The files and sub-directories will also be copied over.
* @param string $src the source directory
* @param string $dst the destination directory
* @param array $options options for directory copy. Valid options are:
*
* - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775.
* - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting.
* - filter: callback, a PHP callback that is called for each directory or file.
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered.
* The callback can return one of the following values:
*
* * true: the directory or file will be copied (the "only" and "except" options will be ignored)
* * false: the directory or file will NOT be copied (the "only" and "except" options will be ignored)
* * null: the "only" and "except" options will determine whether the directory or file should be copied
*
* - only: array, list of patterns that the file paths should match if they want to be copied.
* A path matches a pattern if it contains the pattern string at its end.
* For example, '.php' matches all file paths ending with '.php'.
* Note, the '/' characters in a pattern matches both '/' and '\' in the paths.
* If a file path matches a pattern in both "only" and "except", it will NOT be copied.
* - except: array, list of patterns that the files or directories should match if they want to be excluded from being copied.
* A path matches a pattern if it contains the pattern string at its end.
* Patterns ending with '/' apply to directory paths only, and patterns not ending with '/'
* apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b';
* and '.svn/' matches directory paths ending with '.svn'. Note, the '/' characters in a pattern matches
* both '/' and '\' in the paths.
* - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true.
* - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true.
* - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file.
* If the callback returns false, the copy operation for the sub-directory or file will be cancelled.
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or
* file to be copied from, while `$to` is the copy target.
* - afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied.
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or
* file copied from, while `$to` is the copy target.
* - copyEmptyDirectories: boolean, whether to copy empty directories. Set this to false to avoid creating directories
* that do not contain files. This affects directories that do not contain files initially as well as directories that
* do not contain files at the target destination because files have been filtered via `only` or `except`.
* Defaults to true. This option is available since version 2.0.12. Before 2.0.12 empty directories are always copied.
* @throws InvalidArgumentException if unable to open directory
*/
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 InvalidArgumentException('Trying to copy a directory to itself or a subdirectory.');
}
$dstExists = is_dir($dst);
if (!$dstExists && (!isset($options['copyEmptyDirectories']) || $options['copyEmptyDirectories'])) {
static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true);
$dstExists = true;
}
$handle = opendir($src);
if ($handle === false) {
throw new InvalidArgumentException("Unable to open directory: $src");
}
if (!isset($options['basePath'])) {
// this should be done only once
$options['basePath'] = realpath($src);
$options = static::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)) {
if (!$dstExists) {
// delay creation of destination directory until the first file is copied to avoid creating empty directories
static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true);
$dstExists = true;
}
copy($from, $to);
if (isset($options['fileMode'])) {
@chmod($to, $options['fileMode']);
}
} else {
// recursive copy, defaults to true
if (!isset($options['recursive']) || $options['recursive']) {
static::copyDirectory($from, $to, $options);
}
}
if (isset($options['afterCopy'])) {
call_user_func($options['afterCopy'], $from, $to);
}
}
}
closedir($handle);
}
/**
* Removes a directory (and all its content) recursively.
*
* @param string $dir the directory to be deleted recursively.
* @param array $options options for directory remove. Valid options are:
*
* - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too.
* Defaults to `false`, meaning the content of the symlinked directory would not be deleted.
* Only symlink would be removed in that default case.
*
* @throws ErrorException in case of failure
*/
public static function removeDirectory($dir, $options = [])
{
if (!is_dir($dir)) {
return;
}
if (!empty($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 {
static::unlink($path);
}
}
closedir($handle);
}
if (is_link($dir)) {
static::unlink($dir);
} else {
rmdir($dir);
}
}
/**
* Removes a file or symlink in a cross-platform way
*
* @param string $path
* @return bool
*
* @since 2.0.14
*/
public static function unlink($path)
{
$isWindows = DIRECTORY_SEPARATOR === '\\';
if (!$isWindows) {
return unlink($path);
}
if (is_link($path) && is_dir($path)) {
return rmdir($path);
}
try {
return unlink($path);
} catch (ErrorException $e) {
// last resort measure for Windows
$lines = [];
exec('DEL /F/Q ' . escapeshellarg($path), $lines, $deleteError);
return $deleteError !== 0;
}
}
/**
* Returns the files found under the specified directory and subdirectories.
* @param string $dir the directory under which the files will be looked for.
* @param array $options options for file searching. Valid options are:
*
* - `filter`: callback, a PHP callback that is called for each directory or file.
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered.
* The callback can return one of the following values:
*
* * `true`: the directory or file will be returned (the `only` and `except` options will be ignored)
* * `false`: the directory or file will NOT be returned (the `only` and `except` options will be ignored)
* * `null`: the `only` and `except` options will determine whether the directory or file should be returned
*
* - `except`: array, list of patterns excluding from the results matching file or directory paths.
* Patterns ending with slash ('/') apply to directory paths only, and patterns not ending with '/'
* apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b';
* and `.svn/` matches directory paths ending with `.svn`.
* If the pattern does not contain a slash (`/`), it is treated as a shell glob pattern
* and checked for a match against the pathname relative to `$dir`.
* Otherwise, the pattern is treated as a shell glob suitable for consumption by `fnmatch(3)`
* with the `FNM_PATHNAME` flag: wildcards in the pattern will not match a `/` in the pathname.
* For example, `views/*.php` matches `views/index.php` but not `views/controller/index.php`.
* A leading slash matches the beginning of the pathname. For example, `/*.php` matches `index.php` but not `views/start/index.php`.
* An optional prefix `!` which negates the pattern; any matching file excluded by a previous pattern will become included again.
* If a negated pattern matches, this will override lower precedence patterns sources. Put a backslash (`\`) in front of the first `!`
* for patterns that begin with a literal `!`, for example, `\!important!.txt`.
* Note, the '/' characters in a pattern matches both '/' and '\' in the paths.
* - `only`: array, list of patterns that the file paths should match if they are to be returned. Directory paths
* are not checked against them. Same pattern matching rules as in the `except` option are used.
* If a file path matches a pattern in both `only` and `except`, it will NOT be returned.
* - `caseSensitive`: boolean, whether patterns specified at `only` or `except` should be case sensitive. Defaults to `true`.
* - `recursive`: boolean, whether the files under the subdirectories should also be looked for. Defaults to `true`.
* @return array files found under the directory, in no particular order. Ordering depends on the files system used.
* @throws InvalidArgumentException if the dir is invalid.
*/
public static function findFiles($dir, $options = [])
{
$dir = self::clearDir($dir);
$options = self::setBasePath($dir, $options);
$list = [];
$handle = self::openDir($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 (is_dir($path) && (!isset($options['recursive']) || $options['recursive'])) {
$list = array_merge($list, static::findFiles($path, $options));
}
}
}
closedir($handle);
return $list;
}
/**
* Returns the directories found under the specified directory and subdirectories.
* @param string $dir the directory under which the files will be looked for.
* @param array $options options for directory searching. Valid options are:
*
* - `filter`: callback, a PHP callback that is called for each directory or file.
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered.
* The callback can return one of the following values:
*
* * `true`: the directory will be returned
* * `false`: the directory will NOT be returned
*
* - `recursive`: boolean, whether the files under the subdirectories should also be looked for. Defaults to `true`.
* @return array directories found under the directory, in no particular order. Ordering depends on the files system used.
* @throws InvalidArgumentException if the dir is invalid.
* @since 2.0.14
*/
public static function findDirectories($dir, $options = [])
{
$dir = self::clearDir($dir);
$options = self::setBasePath($dir, $options);
$list = [];
$handle = self::openDir($dir);
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $file;
if (is_dir($path) && static::filterPath($path, $options)) {
$list[] = $path;
if (!isset($options['recursive']) || $options['recursive']) {
$list = array_merge($list, static::findDirectories($path, $options));
}
}
}
closedir($handle);
return $list;
}
/*
* @param string $dir
*/
private static function setBasePath($dir, $options)
{
if (!isset($options['basePath'])) {
// this should be done only once
$options['basePath'] = realpath($dir);
$options = static::normalizeOptions($options);
}
return $options;
}
/*
* @param string $dir
*/
private static function openDir($dir)
{
$handle = opendir($dir);
if ($handle === false) {
throw new InvalidArgumentException("Unable to open directory: $dir");
}
return $handle;
}
/*
* @param string $dir
*/
private static function clearDir($dir)
{
if (!is_dir($dir)) {
throw new InvalidArgumentException("The dir argument must be a directory: $dir");
}
return rtrim($dir, DIRECTORY_SEPARATOR);
}
/**
* Checks if the given file path satisfies the filtering options.
* @param string $path the path of the file or directory to be checked
* @param array $options the filtering options. See [[findFiles()]] for explanations of
* the supported options.
* @return bool whether the file or directory satisfies the filtering options.
*/
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) {
// don't check PATTERN_NEGATIVE since those entries are not prefixed with !
return true;
}
return false;
}
return true;
}
/**
* Creates a new directory.
*
* This method is similar to the PHP `mkdir()` function except that
* it uses `chmod()` to set the permission of the created directory
* in order to avoid the impact of the `umask` setting.
*
* @param string $path path of the directory to be created.
* @param int $mode the permission to be set for the created directory.
* @param bool $recursive whether to create parent directories if they do not exist.
* @return bool whether the directory is created successfully
* @throws \yii\base\Exception if the directory could not be created (i.e. php error due to parallel changes)
*/
public static function createDirectory($path, $mode = 0775, $recursive = true)
{
if (is_dir($path)) {
return true;
}
$parentDir = dirname($path);
// recurse if parent dir does not exist and we are not at the root of the file system.
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)) {// https://github.com/yiisoft/yii2/issues/9288
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);
}
}
/**
* Performs a simple comparison of file or directory names.
*
* Based on match_basename() from dir.c of git 1.8.5.3 sources.
*
* @param string $baseName file or directory name to compare with the pattern
* @param string $pattern the pattern that $baseName will be compared against
* @param int|bool $firstWildcard location of first wildcard character in the $pattern
* @param int $flags pattern flags
* @return bool whether the name matches against pattern
*/
private static function matchBasename($baseName, $pattern, $firstWildcard, $flags)
{
if ($firstWildcard === false) {
if ($pattern === $baseName) {
return true;
}
} elseif ($flags & self::PATTERN_ENDSWITH) {
/* "*literal" matching against "fooliteral" */
$n = StringHelper::byteLength($pattern);
if (StringHelper::byteSubstr($pattern, 1, $n) === StringHelper::byteSubstr($baseName, -$n, $n)) {
return true;
}
}
$matchOptions = [];
if ($flags & self::PATTERN_CASE_INSENSITIVE) {
$matchOptions['caseSensitive'] = false;
}
return StringHelper::matchWildcard($pattern, $baseName, $matchOptions);
}
/**
* Compares a path part against a pattern with optional wildcards.
*
* Based on match_pathname() from dir.c of git 1.8.5.3 sources.
*
* @param string $path full path to compare
* @param string $basePath base of path that will not be compared
* @param string $pattern the pattern that path part will be compared against
* @param int|bool $firstWildcard location of first wildcard character in the $pattern
* @param int $flags pattern flags
* @return bool whether the path part matches against pattern
*/
private static function matchPathname($path, $basePath, $pattern, $firstWildcard, $flags)
{
// match with FNM_PATHNAME; the pattern has base implicitly in front of it.
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 the non-wildcard part is longer than the remaining pathname, surely it cannot match.
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 the whole pattern did not have a wildcard, then our prefix match is all we need; we do not need to call fnmatch at all.
if (empty($pattern) && empty($name)) {
return true;
}
}
$matchOptions = [
'filePath' => true
];
if ($flags & self::PATTERN_CASE_INSENSITIVE) {
$matchOptions['caseSensitive'] = false;
}
return StringHelper::matchWildcard($pattern, $name, $matchOptions);
}
/**
* Scan the given exclude list in reverse to see whether pathname
* should be ignored. The first match (i.e. the last on the list), if
* any, determines the fate. Returns the element which
* matched, or null for undecided.
*
* Based on last_exclude_matching_from_list() from dir.c of git 1.8.5.3 sources.
*
* @param string $basePath
* @param string $path
* @param array $excludes list of patterns to match $path against
* @return array|null null or one of $excludes item as an array with keys: 'pattern', 'flags'
* @throws InvalidArgumentException if any of the exclude patterns is not a string or an array with keys: pattern, flags, firstWildcard.
*/
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 InvalidArgumentException('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;
}
/**
* Processes the pattern, stripping special characters like / and ! from the beginning and settings flags instead.
* @param string $pattern
* @param bool $caseSensitive
* @throws InvalidArgumentException
* @return array with keys: (string) pattern, (int) flags, (int|bool) firstWildcard
*/
private static function parseExcludePattern($pattern, $caseSensitive)
{
if (!is_string($pattern)) {
throw new InvalidArgumentException('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;
}
/**
* Searches for the first wildcard character in the pattern.
* @param string $pattern the pattern to search in
* @return int|bool position of first wildcard character or false if not found
*/
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);
}
/**
* @param array $options raw options
* @return array normalized options
* @since 2.0.12
*/
protected 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;
}
}

View File

@@ -0,0 +1,556 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
use IntlDateFormatter;
use Yii;
/**
* BaseFormatConverter provides concrete implementation for [[FormatConverter]].
*
* Do not use BaseFormatConverter. Use [[FormatConverter]] instead.
*
* @author Carsten Brandt <mail@cebe.cc>
* @author Enrica Ruedin <e.ruedin@guggach.com>
* @since 2.0
*/
class BaseFormatConverter
{
/**
* @var array the php fallback definition to use for the ICU short patterns `short`, `medium`, `long` and `full`.
* This is used as fallback when the intl extension is not installed.
*/
public static $phpFallbackDatePatterns = [
'short' => [
'date' => 'n/j/y',
'time' => 'H:i',
'datetime' => 'n/j/y H:i',
],
'medium' => [
'date' => 'M j, Y',
'time' => 'g:i:s A',
'datetime' => 'M j, Y g:i:s A',
],
'long' => [
'date' => 'F j, Y',
'time' => 'g:i:sA',
'datetime' => 'F j, Y g:i:sA',
],
'full' => [
'date' => 'l, F j, Y',
'time' => 'g:i:sA T',
'datetime' => 'l, F j, Y g:i:sA T',
],
];
/**
* @var array the jQuery UI fallback definition to use for the ICU short patterns `short`, `medium`, `long` and `full`.
* This is used as fallback when the intl extension is not installed.
*/
public static $juiFallbackDatePatterns = [
'short' => [
'date' => 'd/m/y',
'time' => '',
'datetime' => 'd/m/y',
],
'medium' => [
'date' => 'M d, yy',
'time' => '',
'datetime' => 'M d, yy',
],
'long' => [
'date' => 'MM d, yy',
'time' => '',
'datetime' => 'MM d, yy',
],
'full' => [
'date' => 'DD, MM d, yy',
'time' => '',
'datetime' => 'DD, MM d, yy',
],
];
private static $_icuShortFormats = [
'short' => 3, // IntlDateFormatter::SHORT,
'medium' => 2, // IntlDateFormatter::MEDIUM,
'long' => 1, // IntlDateFormatter::LONG,
'full' => 0, // IntlDateFormatter::FULL,
];
/**
* Converts a date format pattern from [ICU format][] to [php date() function format][].
*
* The conversion is limited to date patterns that do not use escaped characters.
* Patterns like `d 'of' MMMM yyyy` which will result in a date like `1 of December 2014` may not be converted correctly
* because of the use of escaped characters.
*
* Pattern constructs that are not supported by the PHP format will be removed.
*
* [php date() function format]: http://php.net/manual/en/function.date.php
* [ICU format]: http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
*
* @param string $pattern date format pattern in ICU format.
* @param string $type 'date', 'time', or 'datetime'.
* @param string $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`.
* If not given, `Yii::$app->language` will be used.
* @return string The converted date format pattern.
*/
public static function convertDateIcuToPhp($pattern, $type = 'date', $locale = null)
{
if (isset(self::$_icuShortFormats[$pattern])) {
if (extension_loaded('intl')) {
if ($locale === null) {
$locale = Yii::$app->language;
}
if ($type === 'date') {
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE);
} elseif ($type === 'time') {
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]);
} else {
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]);
}
$pattern = $formatter->getPattern();
} else {
return static::$phpFallbackDatePatterns[$pattern][$type];
}
}
// http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
// escaped text
$escaped = [];
if (preg_match_all('/(?<!\')\'(.*?[^\'])\'(?!\')/', $pattern, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$match[1] = str_replace('\'\'', '\'', $match[1]);
$escaped[$match[0]] = '\\' . implode('\\', preg_split('//u', $match[1], -1, PREG_SPLIT_NO_EMPTY));
}
}
return strtr($pattern, array_merge($escaped, [
"''" => "\\'", // two single quotes produce one
'G' => '', // era designator like (Anno Domini)
'Y' => 'o', // 4digit year of "Week of Year"
'y' => 'Y', // 4digit year e.g. 2014
'yyyy' => 'Y', // 4digit year e.g. 2014
'yy' => 'y', // 2digit year number eg. 14
'u' => '', // extended year e.g. 4601
'U' => '', // cyclic year name, as in Chinese lunar calendar
'r' => '', // related Gregorian year e.g. 1996
'Q' => '', // number of quarter
'QQ' => '', // number of quarter '02'
'QQQ' => '', // quarter 'Q2'
'QQQQ' => '', // quarter '2nd quarter'
'QQQQQ' => '', // number of quarter '2'
'q' => '', // number of Stand Alone quarter
'qq' => '', // number of Stand Alone quarter '02'
'qqq' => '', // Stand Alone quarter 'Q2'
'qqqq' => '', // Stand Alone quarter '2nd quarter'
'qqqqq' => '', // number of Stand Alone quarter '2'
'M' => 'n', // Numeric representation of a month, without leading zeros
'MM' => 'm', // Numeric representation of a month, with leading zeros
'MMM' => 'M', // A short textual representation of a month, three letters
'MMMM' => 'F', // A full textual representation of a month, such as January or March
'MMMMM' => '',
'L' => 'n', // Stand alone month in year
'LL' => 'm', // Stand alone month in year
'LLL' => 'M', // Stand alone month in year
'LLLL' => 'F', // Stand alone month in year
'LLLLL' => '', // Stand alone month in year
'w' => 'W', // ISO-8601 week number of year
'ww' => 'W', // ISO-8601 week number of year
'W' => '', // week of the current month
'd' => 'j', // day without leading zeros
'dd' => 'd', // day with leading zeros
'D' => 'z', // day of the year 0 to 365
'F' => '', // Day of Week in Month. eg. 2nd Wednesday in July
'g' => '', // Modified Julian day. This is different from the conventional Julian day number in two regards.
'E' => 'D', // day of week written in short form eg. Sun
'EE' => 'D',
'EEE' => 'D',
'EEEE' => 'l', // day of week fully written eg. Sunday
'EEEEE' => '',
'EEEEEE' => '',
'e' => 'N', // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun
'ee' => 'N', // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year
'eee' => 'D',
'eeee' => 'l',
'eeeee' => '',
'eeeeee' => '',
'c' => 'N', // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun
'cc' => 'N', // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year
'ccc' => 'D',
'cccc' => 'l',
'ccccc' => '',
'cccccc' => '',
'a' => 'A', // AM/PM marker
'h' => 'g', // 12-hour format of an hour without leading zeros 1 to 12h
'hh' => 'h', // 12-hour format of an hour with leading zeros, 01 to 12 h
'H' => 'G', // 24-hour format of an hour without leading zeros 0 to 23h
'HH' => 'H', // 24-hour format of an hour with leading zeros, 00 to 23 h
'k' => '', // hour in day (1~24)
'kk' => '', // hour in day (1~24)
'K' => '', // hour in am/pm (0~11)
'KK' => '', // hour in am/pm (0~11)
'm' => 'i', // Minutes without leading zeros, not supported by php but we fallback
'mm' => 'i', // Minutes with leading zeros
's' => 's', // Seconds, without leading zeros, not supported by php but we fallback
'ss' => 's', // Seconds, with leading zeros
'S' => '', // fractional second
'SS' => '', // fractional second
'SSS' => '', // fractional second
'SSSS' => '', // fractional second
'A' => '', // milliseconds in day
'z' => 'T', // Timezone abbreviation
'zz' => 'T', // Timezone abbreviation
'zzz' => 'T', // Timezone abbreviation
'zzzz' => 'T', // Timezone full name, not supported by php but we fallback
'Z' => 'O', // Difference to Greenwich time (GMT) in hours
'ZZ' => 'O', // Difference to Greenwich time (GMT) in hours
'ZZZ' => 'O', // Difference to Greenwich time (GMT) in hours
'ZZZZ' => '\G\M\TP', // Time Zone: long localized GMT (=OOOO) e.g. GMT-08:00
'ZZZZZ' => '', // TIme Zone: ISO8601 extended hms? (=XXXXX)
'O' => '', // Time Zone: short localized GMT e.g. GMT-8
'OOOO' => '\G\M\TP', // Time Zone: long localized GMT (=ZZZZ) e.g. GMT-08:00
'v' => '\G\M\TP', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here
'vvvv' => '\G\M\TP', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here
'V' => '', // Time Zone: short time zone ID
'VV' => 'e', // Time Zone: long time zone ID
'VVV' => '', // Time Zone: time zone exemplar city
'VVVV' => '\G\M\TP', // Time Zone: generic location (falls back to OOOO) using the ICU defined fallback here
'X' => '', // Time Zone: ISO8601 basic hm?, with Z for 0, e.g. -08, +0530, Z
'XX' => 'O, \Z', // Time Zone: ISO8601 basic hm, with Z, e.g. -0800, Z
'XXX' => 'P, \Z', // Time Zone: ISO8601 extended hm, with Z, e.g. -08:00, Z
'XXXX' => '', // Time Zone: ISO8601 basic hms?, with Z, e.g. -0800, -075258, Z
'XXXXX' => '', // Time Zone: ISO8601 extended hms?, with Z, e.g. -08:00, -07:52:58, Z
'x' => '', // Time Zone: ISO8601 basic hm?, without Z for 0, e.g. -08, +0530
'xx' => 'O', // Time Zone: ISO8601 basic hm, without Z, e.g. -0800
'xxx' => 'P', // Time Zone: ISO8601 extended hm, without Z, e.g. -08:00
'xxxx' => '', // Time Zone: ISO8601 basic hms?, without Z, e.g. -0800, -075258
'xxxxx' => '', // Time Zone: ISO8601 extended hms?, without Z, e.g. -08:00, -07:52:58
]));
}
/**
* Converts a date format pattern from [php date() function format][] to [ICU format][].
*
* Pattern constructs that are not supported by the ICU format will be removed.
*
* [php date() function format]: http://php.net/manual/en/function.date.php
* [ICU format]: http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
*
* Since 2.0.13 it handles escaped characters correctly.
*
* @param string $pattern date format pattern in php date()-function format.
* @return string The converted date format pattern.
*/
public static function convertDatePhpToIcu($pattern)
{
// http://php.net/manual/en/function.date.php
$result = strtr($pattern, [
"'" => "''''", // single `'` should be encoded as `''`, which internally should be encoded as `''''`
// Day
'\d' => "'d'",
'd' => 'dd', // Day of the month, 2 digits with leading zeros 01 to 31
'\D' => "'D'",
'D' => 'eee', // A textual representation of a day, three letters Mon through Sun
'\j' => "'j'",
'j' => 'd', // Day of the month without leading zeros 1 to 31
'\l' => "'l'",
'l' => 'eeee', // A full textual representation of the day of the week Sunday through Saturday
'\N' => "'N'",
'N' => 'e', // ISO-8601 numeric representation of the day of the week, 1 (for Monday) through 7 (for Sunday)
'\S' => "'S'",
'S' => '', // English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
'\w' => "'w'",
'w' => '', // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
'\z' => "'z'",
'z' => 'D', // The day of the year (starting from 0) 0 through 365
// Week
'\W' => "'W'",
'W' => 'w', // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year)
// Month
'\F' => "'F'",
'F' => 'MMMM', // A full textual representation of a month, January through December
'\m' => "'m'",
'm' => 'MM', // Numeric representation of a month, with leading zeros 01 through 12
'\M' => "'M'",
'M' => 'MMM', // A short textual representation of a month, three letters Jan through Dec
'\n' => "'n'",
'n' => 'M', // Numeric representation of a month, without leading zeros 1 through 12, not supported by ICU but we fallback to "with leading zero"
'\t' => "'t'",
't' => '', // Number of days in the given month 28 through 31
// Year
'\L' => "'L'",
'L' => '', // Whether it's a leap year, 1 if it is a leap year, 0 otherwise.
'\o' => "'o'",
'o' => 'Y', // ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.
'\Y' => "'Y'",
'Y' => 'yyyy', // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
'\y' => "'y'",
'y' => 'yy', // A two digit representation of a year Examples: 99 or 03
// Time
'\a' => "'a'",
'a' => 'a', // Lowercase Ante meridiem and Post meridiem, am or pm
'\A' => "'A'",
'A' => 'a', // Uppercase Ante meridiem and Post meridiem, AM or PM, not supported by ICU but we fallback to lowercase
'\B' => "'B'",
'B' => '', // Swatch Internet time 000 through 999
'\g' => "'g'",
'g' => 'h', // 12-hour format of an hour without leading zeros 1 through 12
'\G' => "'G'",
'G' => 'H', // 24-hour format of an hour without leading zeros 0 to 23h
'\h' => "'h'",
'h' => 'hh', // 12-hour format of an hour with leading zeros, 01 to 12 h
'\H' => "'H'",
'H' => 'HH', // 24-hour format of an hour with leading zeros, 00 to 23 h
'\i' => "'i'",
'i' => 'mm', // Minutes with leading zeros 00 to 59
'\s' => "'s'",
's' => 'ss', // Seconds, with leading zeros 00 through 59
'\u' => "'u'",
'u' => '', // Microseconds. Example: 654321
// Timezone
'\e' => "'e'",
'e' => 'VV', // Timezone identifier. Examples: UTC, GMT, Atlantic/Azores
'\I' => "'I'",
'I' => '', // Whether or not the date is in daylight saving time, 1 if Daylight Saving Time, 0 otherwise.
'\O' => "'O'",
'O' => 'xx', // Difference to Greenwich time (GMT) in hours, Example: +0200
'\P' => "'P'",
'P' => 'xxx', // Difference to Greenwich time (GMT) with colon between hours and minutes, Example: +02:00
'\T' => "'T'",
'T' => 'zzz', // Timezone abbreviation, Examples: EST, MDT ...
'\Z' => "'Z'",
'Z' => '', // Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through 50400
// Full Date/Time
'\c' => "'c'",
'c' => "yyyy-MM-dd'T'HH:mm:ssxxx", // ISO 8601 date, e.g. 2004-02-12T15:19:21+00:00
'\r' => "'r'",
'r' => 'eee, dd MMM yyyy HH:mm:ss xx', // RFC 2822 formatted date, Example: Thu, 21 Dec 2000 16:01:07 +0200
'\U' => "'U'",
'U' => '', // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
'\\\\' => '\\',
]);
// remove `''` - the're result of consecutive escaped chars (`\A\B` will be `'A''B'`, but should be `'AB'`)
// real `'` are encoded as `''''`
return strtr($result, [
"''''" => "''",
"''" => '',
]);
}
/**
* Converts a date format pattern from [ICU format][] to [jQuery UI date format][].
*
* Pattern constructs that are not supported by the jQuery UI format will be removed.
*
* [jQuery UI date format]: http://api.jqueryui.com/datepicker/#utility-formatDate
* [ICU format]: http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
*
* @param string $pattern date format pattern in ICU format.
* @param string $type 'date', 'time', or 'datetime'.
* @param string $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`.
* If not given, `Yii::$app->language` will be used.
* @return string The converted date format pattern.
*/
public static function convertDateIcuToJui($pattern, $type = 'date', $locale = null)
{
if (isset(self::$_icuShortFormats[$pattern])) {
if (extension_loaded('intl')) {
if ($locale === null) {
$locale = Yii::$app->language;
}
if ($type === 'date') {
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE);
} elseif ($type === 'time') {
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]);
} else {
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]);
}
$pattern = $formatter->getPattern();
} else {
return static::$juiFallbackDatePatterns[$pattern][$type];
}
}
// http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
// escaped text
$escaped = [];
if (preg_match_all('/(?<!\')\'.*?[^\']\'(?!\')/', $pattern, $matches)) {
foreach ($matches[0] as $match) {
$escaped[$match] = $match;
}
}
return strtr($pattern, array_merge($escaped, [
'G' => '', // era designator like (Anno Domini)
'Y' => '', // 4digit year of "Week of Year"
'y' => 'yy', // 4digit year e.g. 2014
'yyyy' => 'yy', // 4digit year e.g. 2014
'yy' => 'y', // 2digit year number eg. 14
'u' => '', // extended year e.g. 4601
'U' => '', // cyclic year name, as in Chinese lunar calendar
'r' => '', // related Gregorian year e.g. 1996
'Q' => '', // number of quarter
'QQ' => '', // number of quarter '02'
'QQQ' => '', // quarter 'Q2'
'QQQQ' => '', // quarter '2nd quarter'
'QQQQQ' => '', // number of quarter '2'
'q' => '', // number of Stand Alone quarter
'qq' => '', // number of Stand Alone quarter '02'
'qqq' => '', // Stand Alone quarter 'Q2'
'qqqq' => '', // Stand Alone quarter '2nd quarter'
'qqqqq' => '', // number of Stand Alone quarter '2'
'M' => 'm', // Numeric representation of a month, without leading zeros
'MM' => 'mm', // Numeric representation of a month, with leading zeros
'MMM' => 'M', // A short textual representation of a month, three letters
'MMMM' => 'MM', // A full textual representation of a month, such as January or March
'MMMMM' => '',
'L' => 'm', // Stand alone month in year
'LL' => 'mm', // Stand alone month in year
'LLL' => 'M', // Stand alone month in year
'LLLL' => 'MM', // Stand alone month in year
'LLLLL' => '', // Stand alone month in year
'w' => '', // ISO-8601 week number of year
'ww' => '', // ISO-8601 week number of year
'W' => '', // week of the current month
'd' => 'd', // day without leading zeros
'dd' => 'dd', // day with leading zeros
'D' => 'o', // day of the year 0 to 365
'F' => '', // Day of Week in Month. eg. 2nd Wednesday in July
'g' => '', // Modified Julian day. This is different from the conventional Julian day number in two regards.
'E' => 'D', // day of week written in short form eg. Sun
'EE' => 'D',
'EEE' => 'D',
'EEEE' => 'DD', // day of week fully written eg. Sunday
'EEEEE' => '',
'EEEEEE' => '',
'e' => '', // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun
'ee' => '', // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year
'eee' => 'D',
'eeee' => '',
'eeeee' => '',
'eeeeee' => '',
'c' => '', // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun
'cc' => '', // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year
'ccc' => 'D',
'cccc' => 'DD',
'ccccc' => '',
'cccccc' => '',
'a' => '', // am/pm marker
'h' => '', // 12-hour format of an hour without leading zeros 1 to 12h
'hh' => '', // 12-hour format of an hour with leading zeros, 01 to 12 h
'H' => '', // 24-hour format of an hour without leading zeros 0 to 23h
'HH' => '', // 24-hour format of an hour with leading zeros, 00 to 23 h
'k' => '', // hour in day (1~24)
'kk' => '', // hour in day (1~24)
'K' => '', // hour in am/pm (0~11)
'KK' => '', // hour in am/pm (0~11)
'm' => '', // Minutes without leading zeros, not supported by php but we fallback
'mm' => '', // Minutes with leading zeros
's' => '', // Seconds, without leading zeros, not supported by php but we fallback
'ss' => '', // Seconds, with leading zeros
'S' => '', // fractional second
'SS' => '', // fractional second
'SSS' => '', // fractional second
'SSSS' => '', // fractional second
'A' => '', // milliseconds in day
'z' => '', // Timezone abbreviation
'zz' => '', // Timezone abbreviation
'zzz' => '', // Timezone abbreviation
'zzzz' => '', // Timezone full name, not supported by php but we fallback
'Z' => '', // Difference to Greenwich time (GMT) in hours
'ZZ' => '', // Difference to Greenwich time (GMT) in hours
'ZZZ' => '', // Difference to Greenwich time (GMT) in hours
'ZZZZ' => '', // Time Zone: long localized GMT (=OOOO) e.g. GMT-08:00
'ZZZZZ' => '', // Time Zone: ISO8601 extended hms? (=XXXXX)
'O' => '', // Time Zone: short localized GMT e.g. GMT-8
'OOOO' => '', // Time Zone: long localized GMT (=ZZZZ) e.g. GMT-08:00
'v' => '', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here
'vvvv' => '', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here
'V' => '', // Time Zone: short time zone ID
'VV' => '', // Time Zone: long time zone ID
'VVV' => '', // Time Zone: time zone exemplar city
'VVVV' => '', // Time Zone: generic location (falls back to OOOO) using the ICU defined fallback here
'X' => '', // Time Zone: ISO8601 basic hm?, with Z for 0, e.g. -08, +0530, Z
'XX' => '', // Time Zone: ISO8601 basic hm, with Z, e.g. -0800, Z
'XXX' => '', // Time Zone: ISO8601 extended hm, with Z, e.g. -08:00, Z
'XXXX' => '', // Time Zone: ISO8601 basic hms?, with Z, e.g. -0800, -075258, Z
'XXXXX' => '', // Time Zone: ISO8601 extended hms?, with Z, e.g. -08:00, -07:52:58, Z
'x' => '', // Time Zone: ISO8601 basic hm?, without Z for 0, e.g. -08, +0530
'xx' => '', // Time Zone: ISO8601 basic hm, without Z, e.g. -0800
'xxx' => '', // Time Zone: ISO8601 extended hm, without Z, e.g. -08:00
'xxxx' => '', // Time Zone: ISO8601 basic hms?, without Z, e.g. -0800, -075258
'xxxxx' => '', // Time Zone: ISO8601 extended hms?, without Z, e.g. -08:00, -07:52:58
]));
}
/**
* Converts a date format pattern from [php date() function format][] to [jQuery UI date format][].
*
* The conversion is limited to date patterns that do not use escaped characters.
* Patterns like `jS \o\f F Y` which will result in a date like `1st of December 2014` may not be converted correctly
* because of the use of escaped characters.
*
* Pattern constructs that are not supported by the jQuery UI format will be removed.
*
* [php date() function format]: http://php.net/manual/en/function.date.php
* [jQuery UI date format]: http://api.jqueryui.com/datepicker/#utility-formatDate
*
* @param string $pattern date format pattern in php date()-function format.
* @return string The converted date format pattern.
*/
public static function convertDatePhpToJui($pattern)
{
// http://php.net/manual/en/function.date.php
return strtr($pattern, [
// Day
'd' => 'dd', // Day of the month, 2 digits with leading zeros 01 to 31
'D' => 'D', // A textual representation of a day, three letters Mon through Sun
'j' => 'd', // Day of the month without leading zeros 1 to 31
'l' => 'DD', // A full textual representation of the day of the week Sunday through Saturday
'N' => '', // ISO-8601 numeric representation of the day of the week, 1 (for Monday) through 7 (for Sunday)
'S' => '', // English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
'w' => '', // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
'z' => 'o', // The day of the year (starting from 0) 0 through 365
// Week
'W' => '', // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year)
// Month
'F' => 'MM', // A full textual representation of a month, January through December
'm' => 'mm', // Numeric representation of a month, with leading zeros 01 through 12
'M' => 'M', // A short textual representation of a month, three letters Jan through Dec
'n' => 'm', // Numeric representation of a month, without leading zeros 1 through 12
't' => '', // Number of days in the given month 28 through 31
// Year
'L' => '', // Whether it's a leap year, 1 if it is a leap year, 0 otherwise.
'o' => '', // ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.
'Y' => 'yy', // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
'y' => 'y', // A two digit representation of a year Examples: 99 or 03
// Time
'a' => '', // Lowercase Ante meridiem and Post meridiem, am or pm
'A' => '', // Uppercase Ante meridiem and Post meridiem, AM or PM, not supported by ICU but we fallback to lowercase
'B' => '', // Swatch Internet time 000 through 999
'g' => '', // 12-hour format of an hour without leading zeros 1 through 12
'G' => '', // 24-hour format of an hour without leading zeros 0 to 23h
'h' => '', // 12-hour format of an hour with leading zeros, 01 to 12 h
'H' => '', // 24-hour format of an hour with leading zeros, 00 to 23 h
'i' => '', // Minutes with leading zeros 00 to 59
's' => '', // Seconds, with leading zeros 00 through 59
'u' => '', // Microseconds. Example: 654321
// Timezone
'e' => '', // Timezone identifier. Examples: UTC, GMT, Atlantic/Azores
'I' => '', // Whether or not the date is in daylight saving time, 1 if Daylight Saving Time, 0 otherwise.
'O' => '', // Difference to Greenwich time (GMT) in hours, Example: +0200
'P' => '', // Difference to Greenwich time (GMT) with colon between hours and minutes, Example: +02:00
'T' => '', // Timezone abbreviation, Examples: EST, MDT ...
'Z' => '', // Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through 50400
// Full Date/Time
'c' => 'yyyy-MM-dd', // ISO 8601 date, e.g. 2004-02-12T15:19:21+00:00, skipping the time here because it is not supported
'r' => 'D, d M yy', // RFC 2822 formatted date, Example: Thu, 21 Dec 2000 16:01:07 +0200, skipping the time here because it is not supported
'U' => '@', // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
]);
}
}

2293
vendor/yiisoft/yii2/helpers/BaseHtml.php vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,69 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* BaseHtmlPurifier provides concrete implementation for [[HtmlPurifier]].
*
* Do not use BaseHtmlPurifier. Use [[HtmlPurifier]] instead.
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class BaseHtmlPurifier
{
/**
* Passes markup through HTMLPurifier making it safe to output to end user.
*
* @param string $content The HTML content to purify
* @param array|\Closure|null $config The config to use for HtmlPurifier.
* If not specified or `null` the default config will be used.
* You can use an array or an anonymous function to provide configuration options:
*
* - An array will be passed to the `HTMLPurifier_Config::create()` method.
* - An anonymous function will be called after the config was created.
* The signature should be: `function($config)` where `$config` will be an
* instance of `HTMLPurifier_Config`.
*
* Here is a usage example of such a function:
*
* ```php
* // Allow the HTML5 data attribute `data-type` on `img` elements.
* $content = HtmlPurifier::process($content, function ($config) {
* $config->getHTMLDefinition(true)
* ->addAttribute('img', 'data-type', 'Text');
* });
* ```
*
* @return string the purified HTML content.
*/
public static function process($content, $config = null)
{
$configInstance = \HTMLPurifier_Config::create($config instanceof \Closure ? null : $config);
$configInstance->autoFinalize = false;
$purifier = \HTMLPurifier::instance($configInstance);
$purifier->config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
$purifier->config->set('Cache.SerializerPermissions', 0775);
static::configure($configInstance);
if ($config instanceof \Closure) {
call_user_func($config, $configInstance);
}
return $purifier->purify($content);
}
/**
* Allow the extended HtmlPurifier class to set some default config options.
* @param \HTMLPurifier_Config $config
* @since 2.0.3
*/
protected static function configure($config)
{
}
}

View File

@@ -0,0 +1,602 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
use Yii;
/**
* BaseInflector provides concrete implementation for [[Inflector]].
*
* Do not use BaseInflector. Use [[Inflector]] instead.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class BaseInflector
{
/**
* @var array the rules for converting a word into its plural form.
* The keys are the regular expressions and the values are the corresponding replacements.
*/
public static $plurals = [
'/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media)$/i' => '\1',
'/^(sea[- ]bass)$/i' => '\1',
'/(m)ove$/i' => '\1oves',
'/(f)oot$/i' => '\1eet',
'/(h)uman$/i' => '\1umans',
'/(s)tatus$/i' => '\1tatuses',
'/(s)taff$/i' => '\1taff',
'/(t)ooth$/i' => '\1eeth',
'/(quiz)$/i' => '\1zes',
'/^(ox)$/i' => '\1\2en',
'/([m|l])ouse$/i' => '\1ice',
'/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
'/(x|ch|ss|sh)$/i' => '\1es',
'/([^aeiouy]|qu)y$/i' => '\1ies',
'/(hive)$/i' => '\1s',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/sis$/i' => 'ses',
'/([ti])um$/i' => '\1a',
'/(p)erson$/i' => '\1eople',
'/(m)an$/i' => '\1en',
'/(c)hild$/i' => '\1hildren',
'/(buffal|tomat|potat|ech|her|vet)o$/i' => '\1oes',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
'/us$/i' => 'uses',
'/(alias)$/i' => '\1es',
'/(ax|cris|test)is$/i' => '\1es',
'/(currenc)y$/' => '\1ies',
'/s$/' => 's',
'/^$/' => '',
'/$/' => 's',
];
/**
* @var array the rules for converting a word into its singular form.
* The keys are the regular expressions and the values are the corresponding replacements.
*/
public static $singulars = [
'/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media|ss)$/i' => '\1',
'/^(sea[- ]bass)$/i' => '\1',
'/(s)tatuses$/i' => '\1tatus',
'/(f)eet$/i' => '\1oot',
'/(t)eeth$/i' => '\1ooth',
'/^(.*)(menu)s$/i' => '\1\2',
'/(quiz)zes$/i' => '\\1',
'/(matr)ices$/i' => '\1ix',
'/(vert|ind)ices$/i' => '\1ex',
'/^(ox)en/i' => '\1',
'/(alias)(es)*$/i' => '\1',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
'/([ftw]ax)es/i' => '\1',
'/(cris|ax|test)es$/i' => '\1is',
'/(shoe|slave)s$/i' => '\1',
'/(o)es$/i' => '\1',
'/ouses$/' => 'ouse',
'/([^a])uses$/' => '\1us',
'/([m|l])ice$/i' => '\1ouse',
'/(x|ch|ss|sh)es$/i' => '\1',
'/(m)ovies$/i' => '\1\2ovie',
'/(s)eries$/i' => '\1\2eries',
'/([^aeiouy]|qu)ies$/i' => '\1y',
'/([lr])ves$/i' => '\1f',
'/(tive)s$/i' => '\1',
'/(hive)s$/i' => '\1',
'/(drive)s$/i' => '\1',
'/([^fo])ves$/i' => '\1fe',
'/(^analy)ses$/i' => '\1sis',
'/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
'/([ti])a$/i' => '\1um',
'/(p)eople$/i' => '\1\2erson',
'/(m)en$/i' => '\1an',
'/(c)hildren$/i' => '\1\2hild',
'/(n)ews$/i' => '\1\2ews',
'/(n)etherlands$/i' => '\1\2etherlands',
'/eaus$/' => 'eau',
'/(currenc)ies$/' => '\1y',
'/^(.*us)$/' => '\\1',
'/s$/i' => '',
];
/**
* @var array the special rules for converting a word between its plural form and singular form.
* The keys are the special words in singular form, and the values are the corresponding plural form.
*/
public static $specials = [
'atlas' => 'atlases',
'beef' => 'beefs',
'brother' => 'brothers',
'cafe' => 'cafes',
'child' => 'children',
'cookie' => 'cookies',
'corpus' => 'corpuses',
'cow' => 'cows',
'curve' => 'curves',
'foe' => 'foes',
'ganglion' => 'ganglions',
'genie' => 'genies',
'genus' => 'genera',
'graffito' => 'graffiti',
'hoof' => 'hoofs',
'loaf' => 'loaves',
'man' => 'men',
'money' => 'monies',
'mongoose' => 'mongooses',
'move' => 'moves',
'mythos' => 'mythoi',
'niche' => 'niches',
'numen' => 'numina',
'occiput' => 'occiputs',
'octopus' => 'octopuses',
'opus' => 'opuses',
'ox' => 'oxen',
'pasta' => 'pasta',
'penis' => 'penises',
'sex' => 'sexes',
'soliloquy' => 'soliloquies',
'testis' => 'testes',
'trilby' => 'trilbys',
'turf' => 'turfs',
'wave' => 'waves',
'Amoyese' => 'Amoyese',
'bison' => 'bison',
'Borghese' => 'Borghese',
'bream' => 'bream',
'breeches' => 'breeches',
'britches' => 'britches',
'buffalo' => 'buffalo',
'cantus' => 'cantus',
'carp' => 'carp',
'chassis' => 'chassis',
'clippers' => 'clippers',
'cod' => 'cod',
'coitus' => 'coitus',
'Congoese' => 'Congoese',
'contretemps' => 'contretemps',
'corps' => 'corps',
'debris' => 'debris',
'diabetes' => 'diabetes',
'djinn' => 'djinn',
'eland' => 'eland',
'elk' => 'elk',
'equipment' => 'equipment',
'Faroese' => 'Faroese',
'flounder' => 'flounder',
'Foochowese' => 'Foochowese',
'gallows' => 'gallows',
'Genevese' => 'Genevese',
'Genoese' => 'Genoese',
'Gilbertese' => 'Gilbertese',
'graffiti' => 'graffiti',
'headquarters' => 'headquarters',
'herpes' => 'herpes',
'hijinks' => 'hijinks',
'Hottentotese' => 'Hottentotese',
'information' => 'information',
'innings' => 'innings',
'jackanapes' => 'jackanapes',
'Kiplingese' => 'Kiplingese',
'Kongoese' => 'Kongoese',
'Lucchese' => 'Lucchese',
'mackerel' => 'mackerel',
'Maltese' => 'Maltese',
'mews' => 'mews',
'moose' => 'moose',
'mumps' => 'mumps',
'Nankingese' => 'Nankingese',
'news' => 'news',
'nexus' => 'nexus',
'Niasese' => 'Niasese',
'Pekingese' => 'Pekingese',
'Piedmontese' => 'Piedmontese',
'pincers' => 'pincers',
'Pistoiese' => 'Pistoiese',
'pliers' => 'pliers',
'Portuguese' => 'Portuguese',
'proceedings' => 'proceedings',
'rabies' => 'rabies',
'rice' => 'rice',
'rhinoceros' => 'rhinoceros',
'salmon' => 'salmon',
'Sarawakese' => 'Sarawakese',
'scissors' => 'scissors',
'series' => 'series',
'Shavese' => 'Shavese',
'shears' => 'shears',
'siemens' => 'siemens',
'species' => 'species',
'swine' => 'swine',
'testes' => 'testes',
'trousers' => 'trousers',
'trout' => 'trout',
'tuna' => 'tuna',
'Vermontese' => 'Vermontese',
'Wenchowese' => 'Wenchowese',
'whiting' => 'whiting',
'wildebeest' => 'wildebeest',
'Yengeese' => 'Yengeese',
];
/**
* @var array fallback map for transliteration used by [[transliterate()]] when intl isn't available.
*/
public static $transliteration = [
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C',
'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O',
'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH',
'ß' => 'ss',
'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c',
'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o',
'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th',
'ÿ' => 'y',
];
/**
* Shortcut for `Any-Latin; NFKD` transliteration rule.
*
* The rule is strict, letters will be transliterated with
* the closest sound-representation chars. The result may contain any UTF-8 chars. For example:
* `获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?` will be transliterated to
* `huò qǔ dào dochira Ukraí̈nsʹka: g̀,ê, Srpska: đ, n̂, d̂! ¿Español?`.
*
* Used in [[transliterate()]].
* For detailed information see [unicode normalization forms](http://unicode.org/reports/tr15/#Normalization_Forms_Table)
* @see http://unicode.org/reports/tr15/#Normalization_Forms_Table
* @see transliterate()
* @since 2.0.7
*/
const TRANSLITERATE_STRICT = 'Any-Latin; NFKD';
/**
* Shortcut for `Any-Latin; Latin-ASCII` transliteration rule.
*
* The rule is medium, letters will be
* transliterated to characters of Latin-1 (ISO 8859-1) ASCII table. For example:
* `获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?` will be transliterated to
* `huo qu dao dochira Ukrainsʹka: g,e, Srpska: d, n, d! ¿Espanol?`.
*
* Used in [[transliterate()]].
* For detailed information see [unicode normalization forms](http://unicode.org/reports/tr15/#Normalization_Forms_Table)
* @see http://unicode.org/reports/tr15/#Normalization_Forms_Table
* @see transliterate()
* @since 2.0.7
*/
const TRANSLITERATE_MEDIUM = 'Any-Latin; Latin-ASCII';
/**
* Shortcut for `Any-Latin; Latin-ASCII; [\u0080-\uffff] remove` transliteration rule.
*
* The rule is loose,
* letters will be transliterated with the characters of Basic Latin Unicode Block.
* For example:
* `获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?` will be transliterated to
* `huo qu dao dochira Ukrainska: g,e, Srpska: d, n, d! Espanol?`.
*
* Used in [[transliterate()]].
* For detailed information see [unicode normalization forms](http://unicode.org/reports/tr15/#Normalization_Forms_Table)
* @see http://unicode.org/reports/tr15/#Normalization_Forms_Table
* @see transliterate()
* @since 2.0.7
*/
const TRANSLITERATE_LOOSE = 'Any-Latin; Latin-ASCII; [\u0080-\uffff] remove';
/**
* @var mixed Either a [[\Transliterator]], or a string from which a [[\Transliterator]] can be built
* for transliteration. Used by [[transliterate()]] when intl is available. Defaults to [[TRANSLITERATE_LOOSE]]
* @see http://php.net/manual/en/transliterator.transliterate.php
*/
public static $transliterator = self::TRANSLITERATE_LOOSE;
/**
* Converts a word to its plural form.
* Note that this is for English only!
* For example, 'apple' will become 'apples', and 'child' will become 'children'.
* @param string $word the word to be pluralized
* @return string the pluralized word
*/
public static function pluralize($word)
{
if (isset(static::$specials[$word])) {
return static::$specials[$word];
}
foreach (static::$plurals as $rule => $replacement) {
if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word);
}
}
return $word;
}
/**
* Returns the singular of the $word.
* @param string $word the english word to singularize
* @return string Singular noun.
*/
public static function singularize($word)
{
$result = array_search($word, static::$specials, true);
if ($result !== false) {
return $result;
}
foreach (static::$singulars as $rule => $replacement) {
if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word);
}
}
return $word;
}
/**
* Converts an underscored or CamelCase word into a English
* sentence.
* @param string $words
* @param bool $ucAll whether to set all words to uppercase
* @return string
*/
public static function titleize($words, $ucAll = false)
{
$words = static::humanize(static::underscore($words), $ucAll);
return $ucAll ? ucwords($words) : ucfirst($words);
}
/**
* Returns given word as CamelCased.
*
* Converts a word like "send_email" to "SendEmail". It
* will remove non alphanumeric character from the word, so
* "who's online" will be converted to "WhoSOnline".
* @see variablize()
* @param string $word the word to CamelCase
* @return string
*/
public static function camelize($word)
{
return str_replace(' ', '', ucwords(preg_replace('/[^A-Za-z0-9]+/', ' ', $word)));
}
/**
* Converts a CamelCase name into space-separated words.
* For example, 'PostTag' will be converted to 'Post Tag'.
* @param string $name the string to be converted
* @param bool $ucwords whether to capitalize the first letter in each word
* @return string the resulting words
*/
public static function camel2words($name, $ucwords = true)
{
$label = strtolower(trim(str_replace([
'-',
'_',
'.',
], ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
return $ucwords ? ucwords($label) : $label;
}
/**
* Converts a CamelCase name into an ID in lowercase.
* Words in the ID may be concatenated using the specified character (defaults to '-').
* For example, 'PostTag' will be converted to 'post-tag'.
* @param string $name the string to be converted
* @param string $separator the character used to concatenate the words in the ID
* @param bool|string $strict whether to insert a separator between two consecutive uppercase chars, defaults to false
* @return string the resulting ID
*/
public static function camel2id($name, $separator = '-', $strict = false)
{
$regex = $strict ? '/[A-Z]/' : '/(?<![A-Z])[A-Z]/';
if ($separator === '_') {
return strtolower(trim(preg_replace($regex, '_\0', $name), '_'));
}
return strtolower(trim(str_replace('_', $separator, preg_replace($regex, $separator . '\0', $name)), $separator));
}
/**
* Converts an ID into a CamelCase name.
* Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name.
* For example, 'post-tag' is converted to 'PostTag'.
* @param string $id the ID to be converted
* @param string $separator the character used to separate the words in the ID
* @return string the resulting CamelCase name
*/
public static function id2camel($id, $separator = '-')
{
return str_replace(' ', '', ucwords(implode(' ', explode($separator, $id))));
}
/**
* Converts any "CamelCased" into an "underscored_word".
* @param string $words the word(s) to underscore
* @return string
*/
public static function underscore($words)
{
return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $words));
}
/**
* Returns a human-readable string from $word.
* @param string $word the string to humanize
* @param bool $ucAll whether to set all words to uppercase or not
* @return string
*/
public static function humanize($word, $ucAll = false)
{
$word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
return $ucAll ? ucwords($word) : ucfirst($word);
}
/**
* Same as camelize but first char is in lowercase.
*
* Converts a word like "send_email" to "sendEmail". It
* will remove non alphanumeric character from the word, so
* "who's online" will be converted to "whoSOnline".
* @param string $word to lowerCamelCase
* @return string
*/
public static function variablize($word)
{
$word = static::camelize($word);
return strtolower($word[0]) . substr($word, 1);
}
/**
* Converts a class name to its table name (pluralized) naming conventions.
*
* For example, converts "Person" to "people".
* @param string $className the class name for getting related table_name
* @return string
*/
public static function tableize($className)
{
return static::pluralize(static::underscore($className));
}
/**
* Returns a string with all spaces converted to given replacement,
* non word characters removed and the rest of characters transliterated.
*
* If intl extension isn't available uses fallback that converts latin characters only
* and removes the rest. You may customize characters map via $transliteration property
* of the helper.
*
* @param string $string An arbitrary string to convert
* @param string $replacement The replacement to use for spaces
* @param bool $lowercase whether to return the string in lowercase or not. Defaults to `true`.
* @return string The converted string.
*/
public static function slug($string, $replacement = '-', $lowercase = true)
{
$string = static::transliterate($string);
$string = preg_replace('/[^a-zA-Z0-9=\s—-]+/u', '', $string);
$string = preg_replace('/[=\s—-]+/u', $replacement, $string);
$string = trim($string, $replacement);
return $lowercase ? strtolower($string) : $string;
}
/**
* Returns transliterated version of a string.
*
* If intl extension isn't available uses fallback that converts latin characters only
* and removes the rest. You may customize characters map via $transliteration property
* of the helper.
*
* @param string $string input string
* @param string|\Transliterator $transliterator either a [[\Transliterator]] or a string
* from which a [[\Transliterator]] can be built.
* @return string
* @since 2.0.7 this method is public.
*/
public static function transliterate($string, $transliterator = null)
{
if (static::hasIntl()) {
if ($transliterator === null) {
$transliterator = static::$transliterator;
}
return transliterator_transliterate($transliterator, $string);
}
return strtr($string, static::$transliteration);
}
/**
* @return bool if intl extension is loaded
*/
protected static function hasIntl()
{
return extension_loaded('intl');
}
/**
* Converts a table name to its class name.
*
* For example, converts "people" to "Person".
* @param string $tableName
* @return string
*/
public static function classify($tableName)
{
return static::camelize(static::singularize($tableName));
}
/**
* Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd ...
* @param int $number the number to get its ordinal value
* @return string
*/
public static function ordinalize($number)
{
if (in_array($number % 100, range(11, 13))) {
return $number . 'th';
}
switch ($number % 10) {
case 1:
return $number . 'st';
case 2:
return $number . 'nd';
case 3:
return $number . 'rd';
default:
return $number . 'th';
}
}
/**
* Converts a list of words into a sentence.
*
* Special treatment is done for the last few words. For example,
*
* ```php
* $words = ['Spain', 'France'];
* echo Inflector::sentence($words);
* // output: Spain and France
*
* $words = ['Spain', 'France', 'Italy'];
* echo Inflector::sentence($words);
* // output: Spain, France and Italy
*
* $words = ['Spain', 'France', 'Italy'];
* echo Inflector::sentence($words, ' & ');
* // output: Spain, France & Italy
* ```
*
* @param array $words the words to be converted into an string
* @param string $twoWordsConnector the string connecting words when there are only two
* @param string $lastWordConnector the string connecting the last two words. If this is null, it will
* take the value of `$twoWordsConnector`.
* @param string $connector the string connecting words other than those connected by
* $lastWordConnector and $twoWordsConnector
* @return string the generated sentence
* @since 2.0.1
*/
public static function sentence(array $words, $twoWordsConnector = null, $lastWordConnector = null, $connector = ', ')
{
if ($twoWordsConnector === null) {
$twoWordsConnector = Yii::t('yii', ' and ');
}
if ($lastWordConnector === null) {
$lastWordConnector = $twoWordsConnector;
}
switch (count($words)) {
case 0:
return '';
case 1:
return reset($words);
case 2:
return implode($twoWordsConnector, $words);
default:
return implode($connector, array_slice($words, 0, -1)) . $lastWordConnector . end($words);
}
}
}

View File

@@ -0,0 +1,121 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Class BaseIpHelper provides concrete implementation for [[IpHelper]]
*
* Do not use BaseIpHelper, use [[IpHelper]] instead.
*
* @author Dmytro Naumenko <d.naumenko.a@gmail.com>
* @since 2.0.14
*/
class BaseIpHelper
{
const IPV4 = 4;
const IPV6 = 6;
/**
* The length of IPv6 address in bits
*/
const IPV6_ADDRESS_LENGTH = 128;
/**
* The length of IPv4 address in bits
*/
const IPV4_ADDRESS_LENGTH = 32;
/**
* Gets the IP version. Does not perform IP address validation.
*
* @param string $ip the valid IPv4 or IPv6 address.
* @return int [[IPV4]] or [[IPV6]]
*/
public static function getIpVersion($ip)
{
return strpos($ip, ':') === false ? self::IPV4 : self::IPV6;
}
/**
* Checks whether IP address or subnet $subnet is contained by $subnet.
*
* For example, the following code checks whether subnet `192.168.1.0/24` is in subnet `192.168.0.0/22`:
*
* ```php
* IpHelper::inRange('192.168.1.0/24', '192.168.0.0/22'); // true
* ```
*
* In case you need to check whether a single IP address `192.168.1.21` is in the subnet `192.168.1.0/24`,
* you can use any of theses examples:
*
* ```php
* IpHelper::inRange('192.168.1.21', '192.168.1.0/24'); // true
* IpHelper::inRange('192.168.1.21/32', '192.168.1.0/24'); // true
* ```
*
* @param string $subnet the valid IPv4 or IPv6 address or CIDR range, e.g.: `10.0.0.0/8` or `2001:af::/64`
* @param string $range the valid IPv4 or IPv6 CIDR range, e.g. `10.0.0.0/8` or `2001:af::/64`
* @return bool whether $subnet is contained by $range
*
* @see https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
*/
public static function inRange($subnet, $range)
{
list($ip, $mask) = array_pad(explode('/', $subnet), 2, null);
list($net, $netMask) = array_pad(explode('/', $range), 2, null);
$ipVersion = static::getIpVersion($ip);
$netVersion = static::getIpVersion($net);
if ($ipVersion !== $netVersion) {
return false;
}
$maxMask = $ipVersion === self::IPV4 ? self::IPV4_ADDRESS_LENGTH : self::IPV6_ADDRESS_LENGTH;
$mask = isset($mask) ? $mask : $maxMask;
$netMask = isset($netMask) ? $netMask : $maxMask;
$binIp = static::ip2bin($ip);
$binNet = static::ip2bin($net);
return substr($binIp, 0, $netMask) === substr($binNet, 0, $netMask) && $mask >= $netMask;
}
/**
* Expands an IPv6 address to it's full notation.
*
* For example `2001:db8::1` will be expanded to `2001:0db8:0000:0000:0000:0000:0000:0001`
*
* @param string $ip the original valid IPv6 address
* @return string the expanded IPv6 address
*/
public static function expandIPv6($ip)
{
$hex = unpack('H*hex', inet_pton($ip));
return substr(preg_replace('/([a-f0-9]{4})/i', '$1:', $hex['hex']), 0, -1);
}
/**
* Converts IP address to bits representation.
*
* @param string $ip the valid IPv4 or IPv6 address
* @return string bits as a string
*/
public static function ip2bin($ip)
{
if (static::getIpVersion($ip) === self::IPV4) {
return str_pad(base_convert(ip2long($ip), 10, 2), self::IPV4_ADDRESS_LENGTH, '0', STR_PAD_LEFT);
}
$unpack = unpack('A16', inet_pton($ip));
$binStr = array_shift($unpack);
$bytes = self::IPV6_ADDRESS_LENGTH / 8; // 128 bit / 8 = 16 bytes
$result = '';
while ($bytes-- > 0) {
$result = sprintf('%08b', isset($binStr[$bytes]) ? ord($binStr[$bytes]) : '0') . $result;
}
return $result;
}
}

224
vendor/yiisoft/yii2/helpers/BaseJson.php vendored Normal file
View File

@@ -0,0 +1,224 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
use yii\base\Arrayable;
use yii\base\InvalidArgumentException;
use yii\web\JsExpression;
use yii\base\Model;
/**
* BaseJson provides concrete implementation for [[Json]].
*
* Do not use BaseJson. Use [[Json]] instead.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class BaseJson
{
/**
* List of JSON Error messages assigned to constant names for better handling of version differences.
* @var array
* @since 2.0.7
*/
public static $jsonErrorMessages = [
'JSON_ERROR_DEPTH' => 'The maximum stack depth has been exceeded.',
'JSON_ERROR_STATE_MISMATCH' => 'Invalid or malformed JSON.',
'JSON_ERROR_CTRL_CHAR' => 'Control character error, possibly incorrectly encoded.',
'JSON_ERROR_SYNTAX' => 'Syntax error.',
'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.', // PHP 5.3.3
'JSON_ERROR_RECURSION' => 'One or more recursive references in the value to be encoded.', // PHP 5.5.0
'JSON_ERROR_INF_OR_NAN' => 'One or more NAN or INF values in the value to be encoded', // PHP 5.5.0
'JSON_ERROR_UNSUPPORTED_TYPE' => 'A value of a type that cannot be encoded was given', // PHP 5.5.0
];
/**
* Encodes the given value into a JSON string.
*
* The method enhances `json_encode()` by supporting JavaScript expressions.
* In particular, the method will not encode a JavaScript expression that is
* represented in terms of a [[JsExpression]] object.
*
* Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification.
* You must ensure strings passed to this method have proper encoding before passing them.
*
* @param mixed $value the data to be encoded.
* @param int $options the encoding options. For more details please refer to
* <http://www.php.net/manual/en/function.json-encode.php>. Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`.
* @return string the encoding result.
* @throws InvalidArgumentException if there is any encoding error.
*/
public static function encode($value, $options = 320)
{
$expressions = [];
$value = static::processData($value, $expressions, uniqid('', true));
set_error_handler(function () {
static::handleJsonError(JSON_ERROR_SYNTAX);
}, E_WARNING);
$json = json_encode($value, $options);
restore_error_handler();
static::handleJsonError(json_last_error());
return $expressions === [] ? $json : strtr($json, $expressions);
}
/**
* Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code.
*
* The method enhances `json_encode()` by supporting JavaScript expressions.
* In particular, the method will not encode a JavaScript expression that is
* represented in terms of a [[JsExpression]] object.
*
* Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification.
* You must ensure strings passed to this method have proper encoding before passing them.
*
* @param mixed $value the data to be encoded
* @return string the encoding result
* @since 2.0.4
* @throws InvalidArgumentException if there is any encoding error
*/
public static function htmlEncode($value)
{
return static::encode($value, JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS);
}
/**
* Decodes the given JSON string into a PHP data structure.
* @param string $json the JSON string to be decoded
* @param bool $asArray whether to return objects in terms of associative arrays.
* @return mixed the PHP data
* @throws InvalidArgumentException if there is any decoding error
*/
public static function decode($json, $asArray = true)
{
if (is_array($json)) {
throw new InvalidArgumentException('Invalid JSON data.');
} elseif ($json === null || $json === '') {
return null;
}
$decode = json_decode((string) $json, $asArray);
static::handleJsonError(json_last_error());
return $decode;
}
/**
* Handles [[encode()]] and [[decode()]] errors by throwing exceptions with the respective error message.
*
* @param int $lastError error code from [json_last_error()](http://php.net/manual/en/function.json-last-error.php).
* @throws InvalidArgumentException if there is any encoding/decoding error.
* @since 2.0.6
*/
protected static function handleJsonError($lastError)
{
if ($lastError === JSON_ERROR_NONE) {
return;
}
$availableErrors = [];
foreach (static::$jsonErrorMessages as $const => $message) {
if (defined($const)) {
$availableErrors[constant($const)] = $message;
}
}
if (isset($availableErrors[$lastError])) {
throw new InvalidArgumentException($availableErrors[$lastError], $lastError);
}
throw new InvalidArgumentException('Unknown JSON encoding/decoding error.');
}
/**
* Pre-processes the data before sending it to `json_encode()`.
* @param mixed $data the data to be processed
* @param array $expressions collection of JavaScript expressions
* @param string $expPrefix a prefix internally used to handle JS expressions
* @return mixed the processed data
*/
protected static function processData($data, &$expressions, $expPrefix)
{
if (is_object($data)) {
if ($data instanceof JsExpression) {
$token = "!{[$expPrefix=" . count($expressions) . ']}!';
$expressions['"' . $token . '"'] = $data->expression;
return $token;
} elseif ($data instanceof \JsonSerializable) {
return static::processData($data->jsonSerialize(), $expressions, $expPrefix);
} elseif ($data instanceof Arrayable) {
$data = $data->toArray();
} elseif ($data instanceof \SimpleXMLElement) {
$data = (array) $data;
} else {
$result = [];
foreach ($data as $name => $value) {
$result[$name] = $value;
}
$data = $result;
}
if ($data === []) {
return new \stdClass();
}
}
if (is_array($data)) {
foreach ($data as $key => $value) {
if (is_array($value) || is_object($value)) {
$data[$key] = static::processData($value, $expressions, $expPrefix);
}
}
}
return $data;
}
/**
* Generates a summary of the validation errors.
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed.
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
*
* - showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise
* only the first error message for each attribute will be shown. Defaults to `false`.
*
* @return string the generated error summary
* @since 2.0.14
*/
public static function errorSummary($models, $options = [])
{
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false);
$lines = self::collectErrors($models, $showAllErrors);
return json_encode($lines);
}
/**
* Return array of the validation errors
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed.
* @param $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise
* only the first error message for each attribute will be shown.
* @return array of the validation errors
* @since 2.0.14
*/
private static function collectErrors($models, $showAllErrors)
{
$lines = [];
if (!is_array($models)) {
$models = [$models];
}
foreach ($models as $model) {
$lines = array_unique(array_merge($lines, $model->getErrorSummary($showAllErrors)));
}
return $lines;
}
}

View File

@@ -0,0 +1,107 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
use Yii;
use yii\base\InvalidArgumentException;
/**
* BaseMarkdown provides concrete implementation for [[Markdown]].
*
* Do not use BaseMarkdown. Use [[Markdown]] instead.
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class BaseMarkdown
{
/**
* @var array a map of markdown flavor names to corresponding parser class configurations.
*/
public static $flavors = [
'original' => [
'class' => 'cebe\markdown\Markdown',
'html5' => true,
],
'gfm' => [
'class' => 'cebe\markdown\GithubMarkdown',
'html5' => true,
],
'gfm-comment' => [
'class' => 'cebe\markdown\GithubMarkdown',
'html5' => true,
'enableNewlines' => true,
],
'extra' => [
'class' => 'cebe\markdown\MarkdownExtra',
'html5' => true,
],
];
/**
* @var string the markdown flavor to use when none is specified explicitly.
* Defaults to `original`.
* @see $flavors
*/
public static $defaultFlavor = 'original';
/**
* Converts markdown into HTML.
*
* @param string $markdown the markdown text to parse
* @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
* Defaults to [[$defaultFlavor]], if not set.
* @return string the parsed HTML output
* @throws InvalidArgumentException when an undefined flavor is given.
*/
public static function process($markdown, $flavor = null)
{
$parser = static::getParser($flavor);
return $parser->parse($markdown);
}
/**
* Converts markdown into HTML but only parses inline elements.
*
* This can be useful for parsing small comments or description lines.
*
* @param string $markdown the markdown text to parse
* @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
* Defaults to [[$defaultFlavor]], if not set.
* @return string the parsed HTML output
* @throws InvalidArgumentException when an undefined flavor is given.
*/
public static function processParagraph($markdown, $flavor = null)
{
$parser = static::getParser($flavor);
return $parser->parseParagraph($markdown);
}
/**
* @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
* Defaults to [[$defaultFlavor]], if not set.
* @return \cebe\markdown\Parser
* @throws InvalidArgumentException when an undefined flavor is given.
*/
protected static function getParser($flavor)
{
if ($flavor === null) {
$flavor = static::$defaultFlavor;
}
/* @var $parser \cebe\markdown\Markdown */
if (!isset(static::$flavors[$flavor])) {
throw new InvalidArgumentException("Markdown flavor '$flavor' is not defined.'");
} elseif (!is_object($config = static::$flavors[$flavor])) {
static::$flavors[$flavor] = Yii::createObject($config);
}
return static::$flavors[$flavor];
}
}

View File

@@ -0,0 +1,421 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
use Yii;
/**
* BaseStringHelper provides concrete implementation for [[StringHelper]].
*
* Do not use BaseStringHelper. Use [[StringHelper]] instead.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class BaseStringHelper
{
/**
* Returns the number of bytes in the given string.
* This method ensures the string is treated as a byte array by using `mb_strlen()`.
* @param string $string the string being measured for length
* @return int the number of bytes in the given string.
*/
public static function byteLength($string)
{
return mb_strlen($string, '8bit');
}
/**
* Returns the portion of string specified by the start and length parameters.
* This method ensures the string is treated as a byte array by using `mb_substr()`.
* @param string $string the input string. Must be one character or longer.
* @param int $start the starting position
* @param int $length the desired portion length. If not specified or `null`, there will be
* no limit on length i.e. the output will be until the end of the string.
* @return string the extracted part of string, or FALSE on failure or an empty string.
* @see http://www.php.net/manual/en/function.substr.php
*/
public static function byteSubstr($string, $start, $length = null)
{
return mb_substr($string, $start, $length === null ? mb_strlen($string, '8bit') : $length, '8bit');
}
/**
* Returns the trailing name component of a path.
* This method is similar to the php function `basename()` except that it will
* treat both \ and / as directory separators, independent of the operating system.
* This method was mainly created to work on php namespaces. When working with real
* file paths, php's `basename()` should work fine for you.
* Note: this method is not aware of the actual filesystem, or path components such as "..".
*
* @param string $path A path string.
* @param string $suffix If the name component ends in suffix this will also be cut off.
* @return string the trailing name component of the given path.
* @see http://www.php.net/manual/en/function.basename.php
*/
public static function basename($path, $suffix = '')
{
if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) === $suffix) {
$path = mb_substr($path, 0, -$len);
}
$path = rtrim(str_replace('\\', '/', $path), '/\\');
if (($pos = mb_strrpos($path, '/')) !== false) {
return mb_substr($path, $pos + 1);
}
return $path;
}
/**
* Returns parent directory's path.
* This method is similar to `dirname()` except that it will treat
* both \ and / as directory separators, independent of the operating system.
*
* @param string $path A path string.
* @return string the parent directory's path.
* @see http://www.php.net/manual/en/function.basename.php
*/
public static function dirname($path)
{
$pos = mb_strrpos(str_replace('\\', '/', $path), '/');
if ($pos !== false) {
return mb_substr($path, 0, $pos);
}
return '';
}
/**
* Truncates a string to the number of characters specified.
*
* @param string $string The string to truncate.
* @param int $length How many characters from original string to include into truncated string.
* @param string $suffix String to append to the end of truncated string.
* @param string $encoding The charset to use, defaults to charset currently used by application.
* @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags.
* This parameter is available since version 2.0.1.
* @return string the truncated string.
*/
public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
{
if ($encoding === null) {
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
}
if ($asHtml) {
return static::truncateHtml($string, $length, $suffix, $encoding);
}
if (mb_strlen($string, $encoding) > $length) {
return rtrim(mb_substr($string, 0, $length, $encoding)) . $suffix;
}
return $string;
}
/**
* Truncates a string to the number of words specified.
*
* @param string $string The string to truncate.
* @param int $count How many words from original string to include into truncated string.
* @param string $suffix String to append to the end of truncated string.
* @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags.
* This parameter is available since version 2.0.1.
* @return string the truncated string.
*/
public static function truncateWords($string, $count, $suffix = '...', $asHtml = false)
{
if ($asHtml) {
return static::truncateHtml($string, $count, $suffix);
}
$words = preg_split('/(\s+)/u', trim($string), null, PREG_SPLIT_DELIM_CAPTURE);
if (count($words) / 2 > $count) {
return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix;
}
return $string;
}
/**
* Truncate a string while preserving the HTML.
*
* @param string $string The string to truncate
* @param int $count
* @param string $suffix String to append to the end of the truncated string.
* @param string|bool $encoding
* @return string
* @since 2.0.1
*/
protected static function truncateHtml($string, $count, $suffix, $encoding = false)
{
$config = \HTMLPurifier_Config::create(null);
if (Yii::$app !== null) {
$config->set('Cache.SerializerPath', Yii::$app->getRuntimePath());
}
$lexer = \HTMLPurifier_Lexer::create($config);
$tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context());
$openTokens = [];
$totalCount = 0;
$depth = 0;
$truncated = [];
foreach ($tokens as $token) {
if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
$openTokens[$depth] = $token->name;
$truncated[] = $token;
++$depth;
} elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
if (false === $encoding) {
preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['', ''];
$token->data = $prefixSpace[1] . self::truncateWords(ltrim($token->data), $count - $totalCount, '');
$currentCount = self::countWords($token->data);
} else {
$token->data = self::truncate($token->data, $count - $totalCount, '', $encoding);
$currentCount = mb_strlen($token->data, $encoding);
}
$totalCount += $currentCount;
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
if ($token->name === $openTokens[$depth - 1]) {
--$depth;
unset($openTokens[$depth]);
$truncated[] = $token;
}
} elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
$truncated[] = $token;
}
if ($totalCount >= $count) {
if (0 < count($openTokens)) {
krsort($openTokens);
foreach ($openTokens as $name) {
$truncated[] = new \HTMLPurifier_Token_End($name);
}
}
break;
}
}
$context = new \HTMLPurifier_Context();
$generator = new \HTMLPurifier_Generator($config, $context);
return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
}
/**
* Check if given string starts with specified substring.
* Binary and multibyte safe.
*
* @param string $string Input string
* @param string $with Part to search inside the $string
* @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, $with must exactly match the starting of the string in order to get a true value.
* @return bool Returns true if first input starts with second input, false otherwise
*/
public static function startsWith($string, $with, $caseSensitive = true)
{
if (!$bytes = static::byteLength($with)) {
return true;
}
if ($caseSensitive) {
return strncmp($string, $with, $bytes) === 0;
}
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
return mb_strtolower(mb_substr($string, 0, $bytes, '8bit'), $encoding) === mb_strtolower($with, $encoding);
}
/**
* Check if given string ends with specified substring.
* Binary and multibyte safe.
*
* @param string $string Input string to check
* @param string $with Part to search inside of the $string.
* @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, $with must exactly match the ending of the string in order to get a true value.
* @return bool Returns true if first input ends with second input, false otherwise
*/
public static function endsWith($string, $with, $caseSensitive = true)
{
if (!$bytes = static::byteLength($with)) {
return true;
}
if ($caseSensitive) {
// Warning check, see http://php.net/manual/en/function.substr-compare.php#refsect1-function.substr-compare-returnvalues
if (static::byteLength($string) < $bytes) {
return false;
}
return substr_compare($string, $with, -$bytes, $bytes) === 0;
}
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
return mb_strtolower(mb_substr($string, -$bytes, mb_strlen($string, '8bit'), '8bit'), $encoding) === mb_strtolower($with, $encoding);
}
/**
* Explodes string into array, optionally trims values and skips empty ones.
*
* @param string $string String to be exploded.
* @param string $delimiter Delimiter. Default is ','.
* @param mixed $trim Whether to trim each element. Can be:
* - boolean - to trim normally;
* - string - custom characters to trim. Will be passed as a second argument to `trim()` function.
* - callable - will be called for each value instead of trim. Takes the only argument - value.
* @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false.
* @return array
* @since 2.0.4
*/
public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
{
$result = explode($delimiter, $string);
if ($trim) {
if ($trim === true) {
$trim = 'trim';
} elseif (!is_callable($trim)) {
$trim = function ($v) use ($trim) {
return trim($v, $trim);
};
}
$result = array_map($trim, $result);
}
if ($skipEmpty) {
// Wrapped with array_values to make array keys sequential after empty values removing
$result = array_values(array_filter($result, function ($value) {
return $value !== '';
}));
}
return $result;
}
/**
* Counts words in a string.
* @since 2.0.8
*
* @param string $string
* @return int
*/
public static function countWords($string)
{
return count(preg_split('/\s+/u', $string, null, PREG_SPLIT_NO_EMPTY));
}
/**
* Returns string representation of number value with replaced commas to dots, if decimal point
* of current locale is comma.
* @param int|float|string $value
* @return string
* @since 2.0.11
*/
public static function normalizeNumber($value)
{
$value = (string)$value;
$localeInfo = localeconv();
$decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null;
if ($decimalSeparator !== null && $decimalSeparator !== '.') {
$value = str_replace($decimalSeparator, '.', $value);
}
return $value;
}
/**
* Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
*
* > Note: Base 64 padding `=` may be at the end of the returned string.
* > `=` is not transparent to URL encoding.
*
* @see https://tools.ietf.org/html/rfc4648#page-7
* @param string $input the string to encode.
* @return string encoded string.
* @since 2.0.12
*/
public static function base64UrlEncode($input)
{
return strtr(base64_encode($input), '+/', '-_');
}
/**
* Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
*
* @see https://tools.ietf.org/html/rfc4648#page-7
* @param string $input encoded string.
* @return string decoded string.
* @since 2.0.12
*/
public static function base64UrlDecode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}
/**
* Safely casts a float to string independent of the current locale.
*
* The decimal separator will always be `.`.
* @param float|int $number a floating point number or integer.
* @return string the string representation of the number.
* @since 2.0.13
*/
public static function floatToString($number)
{
// . and , are the only decimal separators known in ICU data,
// so its safe to call str_replace here
return str_replace(',', '.', (string) $number);
}
/**
* Checks if the passed string would match the given shell wildcard pattern.
* This function emulates [[fnmatch()]], which may be unavailable at certain environment, using PCRE.
* @param string $pattern the shell wildcard pattern.
* @param string $string the tested string.
* @param array $options options for matching. Valid options are:
*
* - caseSensitive: bool, whether pattern should be case sensitive. Defaults to `true`.
* - escape: bool, whether backslash escaping is enabled. Defaults to `true`.
* - filePath: bool, whether slashes in string only matches slashes in the given pattern. Defaults to `false`.
*
* @return bool whether the string matches pattern or not.
* @since 2.0.14
*/
public static function matchWildcard($pattern, $string, $options = [])
{
if ($pattern === '*' && empty($options['filePath'])) {
return true;
}
$replacements = [
'\\\\\\\\' => '\\\\',
'\\\\\\*' => '[*]',
'\\\\\\?' => '[?]',
'\*' => '.*',
'\?' => '.',
'\[\!' => '[^',
'\[' => '[',
'\]' => ']',
'\-' => '-',
];
if (isset($options['escape']) && !$options['escape']) {
unset($replacements['\\\\\\\\']);
unset($replacements['\\\\\\*']);
unset($replacements['\\\\\\?']);
}
if (!empty($options['filePath'])) {
$replacements['\*'] = '[^/\\\\]*';
$replacements['\?'] = '[^/\\\\]';
}
$pattern = strtr(preg_quote($pattern, '#'), $replacements);
$pattern = '#^' . $pattern . '$#us';
if (isset($options['caseSensitive']) && !$options['caseSensitive']) {
$pattern .= 'i';
}
return preg_match($pattern, $string) === 1;
}
}

444
vendor/yiisoft/yii2/helpers/BaseUrl.php vendored Normal file
View File

@@ -0,0 +1,444 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
use Yii;
use yii\base\InvalidArgumentException;
/**
* BaseUrl provides concrete implementation for [[Url]].
*
* Do not use BaseUrl. Use [[Url]] instead.
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class BaseUrl
{
/**
* @var \yii\web\UrlManager URL manager to use for creating URLs
* @since 2.0.8
*/
public static $urlManager;
/**
* Creates a URL for the given route.
*
* This method will use [[\yii\web\UrlManager]] to create a URL.
*
* You may specify the route as a string, e.g., `site/index`. You may also use an array
* if you want to specify additional query parameters for the URL being created. The
* array format must be:
*
* ```php
* // generates: /index.php?r=site/index&param1=value1&param2=value2
* ['site/index', 'param1' => 'value1', 'param2' => 'value2']
* ```
*
* If you want to create a URL with an anchor, you can use the array format with a `#` parameter.
* For example,
*
* ```php
* // generates: /index.php?r=site/index&param1=value1#name
* ['site/index', 'param1' => 'value1', '#' => 'name']
* ```
*
* A route may be either absolute or relative. An absolute route has a leading slash (e.g. `/site/index`),
* while a relative route has none (e.g. `site/index` or `index`). A relative route will be converted
* into an absolute one by the following rules:
*
* - If the route is an empty string, the current [[\yii\web\Controller::route|route]] will be used;
* - If the route contains no slashes at all (e.g. `index`), it is considered to be an action ID
* of the current controller and will be prepended with [[\yii\web\Controller::uniqueId]];
* - If the route has no leading slash (e.g. `site/index`), it is considered to be a route relative
* to the current module and will be prepended with the module's [[\yii\base\Module::uniqueId|uniqueId]].
*
* Starting from version 2.0.2, a route can also be specified as an alias. In this case, the alias
* will be converted into the actual route first before conducting the above transformation steps.
*
* Below are some examples of using this method:
*
* ```php
* // /index.php?r=site%2Findex
* echo Url::toRoute('site/index');
*
* // /index.php?r=site%2Findex&src=ref1#name
* echo Url::toRoute(['site/index', 'src' => 'ref1', '#' => 'name']);
*
* // http://www.example.com/index.php?r=site%2Findex
* echo Url::toRoute('site/index', true);
*
* // https://www.example.com/index.php?r=site%2Findex
* echo Url::toRoute('site/index', 'https');
*
* // /index.php?r=post%2Findex assume the alias "@posts" is defined as "post/index"
* echo Url::toRoute('@posts');
* ```
*
* @param string|array $route use a string to represent a route (e.g. `index`, `site/index`),
* or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`).
* @param bool|string $scheme the URI scheme to use in the generated URL:
*
* - `false` (default): generating a relative URL.
* - `true`: returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::$hostInfo]].
* - string: generating an absolute URL with the specified scheme (either `http`, `https` or empty string
* for protocol-relative URL).
*
* @return string the generated URL
* @throws InvalidArgumentException a relative route is given while there is no active controller
*/
public static function toRoute($route, $scheme = false)
{
$route = (array) $route;
$route[0] = static::normalizeRoute($route[0]);
if ($scheme !== false) {
return static::getUrlManager()->createAbsoluteUrl($route, is_string($scheme) ? $scheme : null);
}
return static::getUrlManager()->createUrl($route);
}
/**
* Normalizes route and makes it suitable for UrlManager. Absolute routes are staying as is
* while relative routes are converted to absolute ones.
*
* A relative route is a route without a leading slash, such as "view", "post/view".
*
* - If the route is an empty string, the current [[\yii\web\Controller::route|route]] will be used;
* - If the route contains no slashes at all, it is considered to be an action ID
* of the current controller and will be prepended with [[\yii\web\Controller::uniqueId]];
* - If the route has no leading slash, it is considered to be a route relative
* to the current module and will be prepended with the module's uniqueId.
*
* Starting from version 2.0.2, a route can also be specified as an alias. In this case, the alias
* will be converted into the actual route first before conducting the above transformation steps.
*
* @param string $route the route. This can be either an absolute route or a relative route.
* @return string normalized route suitable for UrlManager
* @throws InvalidArgumentException a relative route is given while there is no active controller
*/
protected static function normalizeRoute($route)
{
$route = Yii::getAlias((string) $route);
if (strncmp($route, '/', 1) === 0) {
// absolute route
return ltrim($route, '/');
}
// relative route
if (Yii::$app->controller === null) {
throw new InvalidArgumentException("Unable to resolve the relative route: $route. No active controller is available.");
}
if (strpos($route, '/') === false) {
// empty or an action ID
return $route === '' ? Yii::$app->controller->getRoute() : Yii::$app->controller->getUniqueId() . '/' . $route;
}
// relative to module
return ltrim(Yii::$app->controller->module->getUniqueId() . '/' . $route, '/');
}
/**
* Creates a URL based on the given parameters.
*
* This method is very similar to [[toRoute()]]. The only difference is that this method
* requires a route to be specified as an array only. If a string is given, it will be treated as a URL.
* In particular, if `$url` is
*
* - an array: [[toRoute()]] will be called to generate the URL. For example:
* `['site/index']`, `['post/index', 'page' => 2]`. Please refer to [[toRoute()]] for more details
* on how to specify a route.
* - a string with a leading `@`: it is treated as an alias, and the corresponding aliased string
* will be returned.
* - an empty string: the currently requested URL will be returned;
* - a normal string: it will be returned as is.
*
* When `$scheme` is specified (either a string or `true`), an absolute URL with host info (obtained from
* [[\yii\web\UrlManager::$hostInfo]]) will be returned. If `$url` is already an absolute URL, its scheme
* will be replaced with the specified one.
*
* Below are some examples of using this method:
*
* ```php
* // /index.php?r=site%2Findex
* echo Url::to(['site/index']);
*
* // /index.php?r=site%2Findex&src=ref1#name
* echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']);
*
* // /index.php?r=post%2Findex assume the alias "@posts" is defined as "/post/index"
* echo Url::to(['@posts']);
*
* // the currently requested URL
* echo Url::to();
*
* // /images/logo.gif
* echo Url::to('@web/images/logo.gif');
*
* // images/logo.gif
* echo Url::to('images/logo.gif');
*
* // http://www.example.com/images/logo.gif
* echo Url::to('@web/images/logo.gif', true);
*
* // https://www.example.com/images/logo.gif
* echo Url::to('@web/images/logo.gif', 'https');
*
* // //www.example.com/images/logo.gif
* echo Url::to('@web/images/logo.gif', '');
* ```
*
*
* @param array|string $url the parameter to be used to generate a valid URL
* @param bool|string $scheme the URI scheme to use in the generated URL:
*
* - `false` (default): generating a relative URL.
* - `true`: returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::$hostInfo]].
* - string: generating an absolute URL with the specified scheme (either `http`, `https` or empty string
* for protocol-relative URL).
*
* @return string the generated URL
* @throws InvalidArgumentException a relative route is given while there is no active controller
*/
public static function to($url = '', $scheme = false)
{
if (is_array($url)) {
return static::toRoute($url, $scheme);
}
$url = Yii::getAlias($url);
if ($url === '') {
$url = Yii::$app->getRequest()->getUrl();
}
if ($scheme === false) {
return $url;
}
if (static::isRelative($url)) {
// turn relative URL into absolute
$url = static::getUrlManager()->getHostInfo() . '/' . ltrim($url, '/');
}
return static::ensureScheme($url, $scheme);
}
/**
* Normalize URL by ensuring that it use specified scheme.
*
* If URL is relative or scheme is not string, normalization is skipped.
*
* @param string $url the URL to process
* @param string $scheme the URI scheme used in URL (e.g. `http` or `https`). Use empty string to
* create protocol-relative URL (e.g. `//example.com/path`)
* @return string the processed URL
* @since 2.0.11
*/
public static function ensureScheme($url, $scheme)
{
if (static::isRelative($url) || !is_string($scheme)) {
return $url;
}
if (substr($url, 0, 2) === '//') {
// e.g. //example.com/path/to/resource
return $scheme === '' ? $url : "$scheme:$url";
}
if (($pos = strpos($url, '://')) !== false) {
if ($scheme === '') {
$url = substr($url, $pos + 1);
} else {
$url = $scheme . substr($url, $pos);
}
}
return $url;
}
/**
* Returns the base URL of the current request.
* @param bool|string $scheme the URI scheme to use in the returned base URL:
*
* - `false` (default): returning the base URL without host info.
* - `true`: returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::$hostInfo]].
* - string: returning an absolute base URL with the specified scheme (either `http`, `https` or empty string
* for protocol-relative URL).
* @return string
*/
public static function base($scheme = false)
{
$url = static::getUrlManager()->getBaseUrl();
if ($scheme !== false) {
$url = static::getUrlManager()->getHostInfo() . $url;
$url = static::ensureScheme($url, $scheme);
}
return $url;
}
/**
* Remembers the specified URL so that it can be later fetched back by [[previous()]].
*
* @param string|array $url the URL to remember. Please refer to [[to()]] for acceptable formats.
* If this parameter is not specified, the currently requested URL will be used.
* @param string $name the name associated with the URL to be remembered. This can be used
* later by [[previous()]]. If not set, [[\yii\web\User::setReturnUrl()]] will be used with passed URL.
* @see previous()
* @see \yii\web\User::setReturnUrl()
*/
public static function remember($url = '', $name = null)
{
$url = static::to($url);
if ($name === null) {
Yii::$app->getUser()->setReturnUrl($url);
} else {
Yii::$app->getSession()->set($name, $url);
}
}
/**
* Returns the URL previously [[remember()|remembered]].
*
* @param string $name the named associated with the URL that was remembered previously.
* If not set, [[\yii\web\User::getReturnUrl()]] will be used to obtain remembered URL.
* @return string|null the URL previously remembered. Null is returned if no URL was remembered with the given name
* and `$name` is not specified.
* @see remember()
* @see \yii\web\User::getReturnUrl()
*/
public static function previous($name = null)
{
if ($name === null) {
return Yii::$app->getUser()->getReturnUrl();
}
return Yii::$app->getSession()->get($name);
}
/**
* Returns the canonical URL of the currently requested page.
*
* The canonical URL is constructed using the current controller's [[\yii\web\Controller::route]] and
* [[\yii\web\Controller::actionParams]]. You may use the following code in the layout view to add a link tag
* about canonical URL:
*
* ```php
* $this->registerLinkTag(['rel' => 'canonical', 'href' => Url::canonical()]);
* ```
*
* @return string the canonical URL of the currently requested page
*/
public static function canonical()
{
$params = Yii::$app->controller->actionParams;
$params[0] = Yii::$app->controller->getRoute();
return static::getUrlManager()->createAbsoluteUrl($params);
}
/**
* Returns the home URL.
*
* @param bool|string $scheme the URI scheme to use for the returned URL:
*
* - `false` (default): returning a relative URL.
* - `true`: returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::$hostInfo]].
* - string: returning an absolute URL with the specified scheme (either `http`, `https` or empty string
* for protocol-relative URL).
*
* @return string home URL
*/
public static function home($scheme = false)
{
$url = Yii::$app->getHomeUrl();
if ($scheme !== false) {
$url = static::getUrlManager()->getHostInfo() . $url;
$url = static::ensureScheme($url, $scheme);
}
return $url;
}
/**
* Returns a value indicating whether a URL is relative.
* A relative URL does not have host info part.
* @param string $url the URL to be checked
* @return bool whether the URL is relative
*/
public static function isRelative($url)
{
return strncmp($url, '//', 2) && strpos($url, '://') === false;
}
/**
* Creates a URL by using the current route and the GET parameters.
*
* You may modify or remove some of the GET parameters, or add additional query parameters through
* the `$params` parameter. In particular, if you specify a parameter to be null, then this parameter
* will be removed from the existing GET parameters; all other parameters specified in `$params` will
* be merged with the existing GET parameters. For example,
*
* ```php
* // assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view"
*
* // /index.php?r=post%2Fview&id=123&src=google
* echo Url::current();
*
* // /index.php?r=post%2Fview&id=123
* echo Url::current(['src' => null]);
*
* // /index.php?r=post%2Fview&id=100&src=google
* echo Url::current(['id' => 100]);
* ```
*
* Note that if you're replacing array parameters with `[]` at the end you should specify `$params` as nested arrays.
* For a `PostSearchForm` model where parameter names are `PostSearchForm[id]` and `PostSearchForm[src]` the syntax
* would be the following:
*
* ```php
* // index.php?r=post%2Findex&PostSearchForm%5Bid%5D=100&PostSearchForm%5Bsrc%5D=google
* echo Url::current([
* $postSearch->formName() => ['id' => 100, 'src' => 'google'],
* ]);
* ```
*
* @param array $params an associative array of parameters that will be merged with the current GET parameters.
* If a parameter value is null, the corresponding GET parameter will be removed.
* @param bool|string $scheme the URI scheme to use in the generated URL:
*
* - `false` (default): generating a relative URL.
* - `true`: returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::$hostInfo]].
* - string: generating an absolute URL with the specified scheme (either `http`, `https` or empty string
* for protocol-relative URL).
*
* @return string the generated URL
* @since 2.0.3
*/
public static function current(array $params = [], $scheme = false)
{
$currentParams = Yii::$app->getRequest()->getQueryParams();
$currentParams[0] = '/' . Yii::$app->controller->getRoute();
$route = array_replace_recursive($currentParams, $params);
return static::toRoute($route, $scheme);
}
/**
* @return \yii\web\UrlManager URL manager used to create URLs
* @since 2.0.8
*/
protected static function getUrlManager()
{
return static::$urlManager ?: Yii::$app->getUrlManager();
}
}

View File

@@ -0,0 +1,272 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
use yii\base\Arrayable;
use yii\base\InvalidValueException;
/**
* BaseVarDumper provides concrete implementation for [[VarDumper]].
*
* Do not use BaseVarDumper. Use [[VarDumper]] instead.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class BaseVarDumper
{
private static $_objects;
private static $_output;
private static $_depth;
/**
* Displays a variable.
* This method achieves the similar functionality as var_dump and print_r
* but is more robust when handling complex objects such as Yii controllers.
* @param mixed $var variable to be dumped
* @param int $depth maximum depth that the dumper should go into the variable. Defaults to 10.
* @param bool $highlight whether the result should be syntax-highlighted
*/
public static function dump($var, $depth = 10, $highlight = false)
{
echo static::dumpAsString($var, $depth, $highlight);
}
/**
* Dumps a variable in terms of a string.
* This method achieves the similar functionality as var_dump and print_r
* but is more robust when handling complex objects such as Yii controllers.
* @param mixed $var variable to be dumped
* @param int $depth maximum depth that the dumper should go into the variable. Defaults to 10.
* @param bool $highlight whether the result should be syntax-highlighted
* @return string the string representation of the variable
*/
public static function dumpAsString($var, $depth = 10, $highlight = false)
{
self::$_output = '';
self::$_objects = [];
self::$_depth = $depth;
self::dumpInternal($var, 0);
if ($highlight) {
$result = highlight_string("<?php\n" . self::$_output, true);
self::$_output = preg_replace('/&lt;\\?php<br \\/>/', '', $result, 1);
}
return self::$_output;
}
/**
* @param mixed $var variable to be dumped
* @param int $level depth level
*/
private static function dumpInternal($var, $level)
{
switch (gettype($var)) {
case 'boolean':
self::$_output .= $var ? 'true' : 'false';
break;
case 'integer':
self::$_output .= (string)$var;
break;
case 'double':
self::$_output .= (string)$var;
break;
case 'string':
self::$_output .= "'" . addslashes($var) . "'";
break;
case 'resource':
self::$_output .= '{resource}';
break;
case 'NULL':
self::$_output .= 'null';
break;
case 'unknown type':
self::$_output .= '{unknown}';
break;
case 'array':
if (self::$_depth <= $level) {
self::$_output .= '[...]';
} elseif (empty($var)) {
self::$_output .= '[]';
} else {
$keys = array_keys($var);
$spaces = str_repeat(' ', $level * 4);
self::$_output .= '[';
foreach ($keys as $key) {
self::$_output .= "\n" . $spaces . ' ';
self::dumpInternal($key, 0);
self::$_output .= ' => ';
self::dumpInternal($var[$key], $level + 1);
}
self::$_output .= "\n" . $spaces . ']';
}
break;
case 'object':
if (($id = array_search($var, self::$_objects, true)) !== false) {
self::$_output .= get_class($var) . '#' . ($id + 1) . '(...)';
} elseif (self::$_depth <= $level) {
self::$_output .= get_class($var) . '(...)';
} else {
$id = array_push(self::$_objects, $var);
$className = get_class($var);
$spaces = str_repeat(' ', $level * 4);
self::$_output .= "$className#$id\n" . $spaces . '(';
if ('__PHP_Incomplete_Class' !== get_class($var) && method_exists($var, '__debugInfo')) {
$dumpValues = $var->__debugInfo();
if (!is_array($dumpValues)) {
throw new InvalidValueException('__debuginfo() must return an array');
}
} else {
$dumpValues = (array) $var;
}
foreach ($dumpValues as $key => $value) {
$keyDisplay = strtr(trim($key), "\0", ':');
self::$_output .= "\n" . $spaces . " [$keyDisplay] => ";
self::dumpInternal($value, $level + 1);
}
self::$_output .= "\n" . $spaces . ')';
}
break;
}
}
/**
* Exports a variable as a string representation.
*
* The string is a valid PHP expression that can be evaluated by PHP parser
* and the evaluation result will give back the variable value.
*
* This method is similar to `var_export()`. The main difference is that
* it generates more compact string representation using short array syntax.
*
* It also handles objects by using the PHP functions serialize() and unserialize().
*
* PHP 5.4 or above is required to parse the exported value.
*
* @param mixed $var the variable to be exported.
* @return string a string representation of the variable
*/
public static function export($var)
{
self::$_output = '';
self::exportInternal($var, 0);
return self::$_output;
}
/**
* @param mixed $var variable to be exported
* @param int $level depth level
*/
private static function exportInternal($var, $level)
{
switch (gettype($var)) {
case 'NULL':
self::$_output .= 'null';
break;
case 'array':
if (empty($var)) {
self::$_output .= '[]';
} else {
$keys = array_keys($var);
$outputKeys = ($keys !== range(0, count($var) - 1));
$spaces = str_repeat(' ', $level * 4);
self::$_output .= '[';
foreach ($keys as $key) {
self::$_output .= "\n" . $spaces . ' ';
if ($outputKeys) {
self::exportInternal($key, 0);
self::$_output .= ' => ';
}
self::exportInternal($var[$key], $level + 1);
self::$_output .= ',';
}
self::$_output .= "\n" . $spaces . ']';
}
break;
case 'object':
if ($var instanceof \Closure) {
self::$_output .= self::exportClosure($var);
} else {
try {
$output = 'unserialize(' . var_export(serialize($var), true) . ')';
} catch (\Exception $e) {
// serialize may fail, for example: if object contains a `\Closure` instance
// so we use a fallback
if ($var instanceof Arrayable) {
self::exportInternal($var->toArray(), $level);
return;
} elseif ($var instanceof \IteratorAggregate) {
$varAsArray = [];
foreach ($var as $key => $value) {
$varAsArray[$key] = $value;
}
self::exportInternal($varAsArray, $level);
return;
} elseif ('__PHP_Incomplete_Class' !== get_class($var) && method_exists($var, '__toString')) {
$output = var_export($var->__toString(), true);
} else {
$outputBackup = self::$_output;
$output = var_export(self::dumpAsString($var), true);
self::$_output = $outputBackup;
}
}
self::$_output .= $output;
}
break;
default:
self::$_output .= var_export($var, true);
}
}
/**
* Exports a [[Closure]] instance.
* @param \Closure $closure closure instance.
* @return string
*/
private static function exportClosure(\Closure $closure)
{
$reflection = new \ReflectionFunction($closure);
$fileName = $reflection->getFileName();
$start = $reflection->getStartLine();
$end = $reflection->getEndLine();
if ($fileName === false || $start === false || $end === false) {
return 'function() {/* Error: unable to determine Closure source */}';
}
--$start;
$source = implode("\n", array_slice(file($fileName), $start, $end - $start));
$tokens = token_get_all('<?php ' . $source);
array_shift($tokens);
$closureTokens = [];
$pendingParenthesisCount = 0;
foreach ($tokens as $token) {
if (isset($token[0]) && $token[0] === T_FUNCTION) {
$closureTokens[] = $token[1];
continue;
}
if ($closureTokens !== []) {
$closureTokens[] = isset($token[1]) ? $token[1] : $token;
if ($token === '}') {
$pendingParenthesisCount--;
if ($pendingParenthesisCount === 0) {
break;
}
} elseif ($token === '{') {
$pendingParenthesisCount++;
}
}
}
return implode('', $closureTokens);
}
}

19
vendor/yiisoft/yii2/helpers/Console.php vendored Normal file
View File

@@ -0,0 +1,19 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Console helper provides useful methods for command line related tasks such as getting input or formatting and coloring
* output.
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class Console extends BaseConsole
{
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* File system helper.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class FileHelper extends BaseFileHelper
{
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* FormatConverter provides functionality to convert between different formatting pattern formats.
*
* It provides functions to convert date format patterns between different conventions.
*
* @author Carsten Brandt <mail@cebe.cc>
* @author Enrica Ruedin <e.ruedin@guggach.com>
* @since 2.0
*/
class FormatConverter extends BaseFormatConverter
{
}

24
vendor/yiisoft/yii2/helpers/Html.php vendored Normal file
View File

@@ -0,0 +1,24 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Html provides a set of static methods for generating commonly used HTML tags.
*
* Nearly all of the methods in this class allow setting additional html attributes for the html
* tags they generate. You can specify, for example, `class`, `style` or `id` for an html element
* using the `$options` parameter. See the documentation of the [[tag()]] method for more details.
*
* For more details and usage information on Html, see the [guide article on html helpers](guide:helper-html).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Html extends BaseHtml
{
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* HtmlPurifier provides an ability to clean up HTML from any harmful code.
*
* Basic usage is the following:
*
* ```php
* echo HtmlPurifier::process($html);
* ```
*
* If you want to configure it:
*
* ```php
* echo HtmlPurifier::process($html, [
* 'Attr.EnableID' => true,
* ]);
* ```
*
* For more details please refer to [HTMLPurifier documentation](http://htmlpurifier.org/).
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class HtmlPurifier extends BaseHtmlPurifier
{
}

View File

@@ -0,0 +1,18 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Inflector pluralizes and singularizes English nouns. It also contains some other useful methods.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @since 2.0
*/
class Inflector extends BaseInflector
{
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Class IpHelper provides a set of IP-related static methods.
*
* Methods expect correct IP addresses.
* To validate IP addresses use [[\yii\validators\IpValidator|IpValidator]].
*
* @author Dmytro Naumenko <d.naumenko.a@gmail.com>
* @since 2.0.14
*/
class IpHelper extends BaseIpHelper
{
}

19
vendor/yiisoft/yii2/helpers/Json.php vendored Normal file
View File

@@ -0,0 +1,19 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Json is a helper class providing JSON data encoding and decoding.
* It enhances the PHP built-in functions `json_encode()` and `json_decode()`
* by supporting encoding JavaScript expressions and throwing exceptions when decoding fails.
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Json extends BaseJson
{
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Markdown provides an ability to transform markdown into HTML.
*
* Basic usage is the following:
*
* ```php
* $myHtml = Markdown::process($myText); // use original markdown flavor
* $myHtml = Markdown::process($myText, 'gfm'); // use github flavored markdown
* $myHtml = Markdown::process($myText, 'extra'); // use markdown extra
* ```
*
* You can configure multiple flavors using the [[$flavors]] property.
*
* For more details please refer to the [Markdown library documentation](https://github.com/cebe/markdown#readme).
*
* > Note: The Markdown library works with PHPDoc annotations so if you use it together with
* > PHP `opcache` make sure [it does not strip comments](http://php.net/manual/en/opcache.configuration.php#ini.opcache.save-comments).
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class Markdown extends BaseMarkdown
{
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Object that represents the replacement of array value while performing [[ArrayHelper::merge()]].
*
* Usage example:
*
* ```php
* $array1 = [
* 'ids' => [
* 1,
* ],
* 'validDomains' => [
* 'example.com',
* 'www.example.com',
* ],
* ];
*
* $array2 = [
* 'ids' => [
* 2,
* ],
* 'validDomains' => new \yii\helpers\ReplaceArrayValue([
* 'yiiframework.com',
* 'www.yiiframework.com',
* ]),
* ];
*
* $result = \yii\helpers\ArrayHelper::merge($array1, $array2);
* ```
*
* The result will be
*
* ```php
* [
* 'ids' => [
* 1,
* 2,
* ],
* 'validDomains' => [
* 'yiiframework.com',
* 'www.yiiframework.com',
* ],
* ]
* ```
*
* @author Robert Korulczyk <robert@korulczyk.pl>
* @since 2.0.10
*/
class ReplaceArrayValue
{
/**
* @var mixed value used as replacement.
*/
public $value;
/**
* Constructor.
* @param mixed $value value used as replacement.
*/
public function __construct($value)
{
$this->value = $value;
}
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* StringHelper.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class StringHelper extends BaseStringHelper
{
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Object that represents the removal of array value while performing [[ArrayHelper::merge()]].
*
* Usage example:
*
* ```php
* $array1 = [
* 'ids' => [
* 1,
* ],
* 'validDomains' => [
* 'example.com',
* 'www.example.com',
* ],
* ];
*
* $array2 = [
* 'ids' => [
* 2,
* ],
* 'validDomains' => new \yii\helpers\UnsetArrayValue(),
* ];
*
* $result = \yii\helpers\ArrayHelper::merge($array1, $array2);
* ```
*
* The result will be
*
* ```php
* [
* 'ids' => [
* 1,
* 2,
* ],
* ]
* ```
*
* @author Robert Korulczyk <robert@korulczyk.pl>
* @since 2.0.10
*/
class UnsetArrayValue
{
}

20
vendor/yiisoft/yii2/helpers/Url.php vendored Normal file
View File

@@ -0,0 +1,20 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Url provides a set of static methods for managing URLs.
*
* For more details and usage information on Url, see the [guide article on url helpers](guide:helper-url).
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class Url extends BaseUrl
{
}

View File

@@ -0,0 +1,27 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* VarDumper is intended to replace the buggy PHP function var_dump and print_r.
* It can correctly identify the recursively referenced objects in a complex
* object structure. It also has a recursive depth control to avoid indefinite
* recursive display of some peculiar variables.
*
* VarDumper can be used as follows,
*
* ```php
* VarDumper::dump($var);
* ```
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class VarDumper extends BaseVarDumper
{
}

View File

@@ -0,0 +1,9 @@
<?php
/**
* MIME aliases.
*
* This file contains aliases for MIME types.
*/
return [
'text/xml' => 'application/xml',
];

View File

@@ -0,0 +1,995 @@
<?php
/**
* MIME types.
*
* This file contains most commonly used MIME types
* according to file extension names.
* Its content is generated from the apache http mime.types file.
* http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup
* This file has been placed in the public domain for unlimited redistribution.
*/
return [
'3dml' => 'text/vnd.in3d.3dml',
'3ds' => 'image/x-3ds',
'3g2' => 'video/3gpp2',
'3gp' => 'video/3gpp',
'7z' => 'application/x-7z-compressed',
'aab' => 'application/x-authorware-bin',
'aac' => 'audio/x-aac',
'aam' => 'application/x-authorware-map',
'aas' => 'application/x-authorware-seg',
'abw' => 'application/x-abiword',
'ac' => 'application/pkix-attr-cert',
'acc' => 'application/vnd.americandynamics.acc',
'ace' => 'application/x-ace-compressed',
'acu' => 'application/vnd.acucobol',
'acutc' => 'application/vnd.acucorp',
'adp' => 'audio/adpcm',
'aep' => 'application/vnd.audiograph',
'afm' => 'application/x-font-type1',
'afp' => 'application/vnd.ibm.modcap',
'ahead' => 'application/vnd.ahead.space',
'ai' => 'application/postscript',
'aif' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'air' => 'application/vnd.adobe.air-application-installer-package+zip',
'ait' => 'application/vnd.dvb.ait',
'ami' => 'application/vnd.amiga.ami',
'apk' => 'application/vnd.android.package-archive',
'appcache' => 'text/cache-manifest',
'application' => 'application/x-ms-application',
'apr' => 'application/vnd.lotus-approach',
'arc' => 'application/x-freearc',
'asc' => 'application/pgp-signature',
'asf' => 'video/x-ms-asf',
'asm' => 'text/x-asm',
'aso' => 'application/vnd.accpac.simply.aso',
'asx' => 'video/x-ms-asf',
'atc' => 'application/vnd.acucorp',
'atom' => 'application/atom+xml',
'atomcat' => 'application/atomcat+xml',
'atomsvc' => 'application/atomsvc+xml',
'atx' => 'application/vnd.antix.game-component',
'au' => 'audio/basic',
'avi' => 'video/x-msvideo',
'aw' => 'application/applixware',
'azf' => 'application/vnd.airzip.filesecure.azf',
'azs' => 'application/vnd.airzip.filesecure.azs',
'azw' => 'application/vnd.amazon.ebook',
'bat' => 'application/x-msdownload',
'bcpio' => 'application/x-bcpio',
'bdf' => 'application/x-font-bdf',
'bdm' => 'application/vnd.syncml.dm+wbxml',
'bed' => 'application/vnd.realvnc.bed',
'bh2' => 'application/vnd.fujitsu.oasysprs',
'bin' => 'application/octet-stream',
'blb' => 'application/x-blorb',
'blorb' => 'application/x-blorb',
'bmi' => 'application/vnd.bmi',
'bmp' => 'image/bmp',
'book' => 'application/vnd.framemaker',
'box' => 'application/vnd.previewsystems.box',
'boz' => 'application/x-bzip2',
'bpk' => 'application/octet-stream',
'btif' => 'image/prs.btif',
'bz' => 'application/x-bzip',
'bz2' => 'application/x-bzip2',
'c' => 'text/x-c',
'c11amc' => 'application/vnd.cluetrust.cartomobile-config',
'c11amz' => 'application/vnd.cluetrust.cartomobile-config-pkg',
'c4d' => 'application/vnd.clonk.c4group',
'c4f' => 'application/vnd.clonk.c4group',
'c4g' => 'application/vnd.clonk.c4group',
'c4p' => 'application/vnd.clonk.c4group',
'c4u' => 'application/vnd.clonk.c4group',
'cab' => 'application/vnd.ms-cab-compressed',
'caf' => 'audio/x-caf',
'cap' => 'application/vnd.tcpdump.pcap',
'car' => 'application/vnd.curl.car',
'cat' => 'application/vnd.ms-pki.seccat',
'cb7' => 'application/x-cbr',
'cba' => 'application/x-cbr',
'cbr' => 'application/x-cbr',
'cbt' => 'application/x-cbr',
'cbz' => 'application/x-cbr',
'cc' => 'text/x-c',
'cct' => 'application/x-director',
'ccxml' => 'application/ccxml+xml',
'cdbcmsg' => 'application/vnd.contact.cmsg',
'cdf' => 'application/x-netcdf',
'cdkey' => 'application/vnd.mediastation.cdkey',
'cdmia' => 'application/cdmi-capability',
'cdmic' => 'application/cdmi-container',
'cdmid' => 'application/cdmi-domain',
'cdmio' => 'application/cdmi-object',
'cdmiq' => 'application/cdmi-queue',
'cdx' => 'chemical/x-cdx',
'cdxml' => 'application/vnd.chemdraw+xml',
'cdy' => 'application/vnd.cinderella',
'cer' => 'application/pkix-cert',
'cfs' => 'application/x-cfs-compressed',
'cgm' => 'image/cgm',
'chat' => 'application/x-chat',
'chm' => 'application/vnd.ms-htmlhelp',
'chrt' => 'application/vnd.kde.kchart',
'cif' => 'chemical/x-cif',
'cii' => 'application/vnd.anser-web-certificate-issue-initiation',
'cil' => 'application/vnd.ms-artgalry',
'cla' => 'application/vnd.claymore',
'class' => 'application/java-vm',
'clkk' => 'application/vnd.crick.clicker.keyboard',
'clkp' => 'application/vnd.crick.clicker.palette',
'clkt' => 'application/vnd.crick.clicker.template',
'clkw' => 'application/vnd.crick.clicker.wordbank',
'clkx' => 'application/vnd.crick.clicker',
'clp' => 'application/x-msclip',
'cmc' => 'application/vnd.cosmocaller',
'cmdf' => 'chemical/x-cmdf',
'cml' => 'chemical/x-cml',
'cmp' => 'application/vnd.yellowriver-custom-menu',
'cmx' => 'image/x-cmx',
'cod' => 'application/vnd.rim.cod',
'com' => 'application/x-msdownload',
'conf' => 'text/plain',
'cpio' => 'application/x-cpio',
'cpp' => 'text/x-c',
'cpt' => 'application/mac-compactpro',
'crd' => 'application/x-mscardfile',
'crl' => 'application/pkix-crl',
'crt' => 'application/x-x509-ca-cert',
'cryptonote' => 'application/vnd.rig.cryptonote',
'csh' => 'application/x-csh',
'csml' => 'chemical/x-csml',
'csp' => 'application/vnd.commonspace',
'css' => 'text/css',
'cst' => 'application/x-director',
'csv' => 'text/csv',
'cu' => 'application/cu-seeme',
'curl' => 'text/vnd.curl',
'cww' => 'application/prs.cww',
'cxt' => 'application/x-director',
'cxx' => 'text/x-c',
'dae' => 'model/vnd.collada+xml',
'daf' => 'application/vnd.mobius.daf',
'dart' => 'application/vnd.dart',
'dataless' => 'application/vnd.fdsn.seed',
'davmount' => 'application/davmount+xml',
'dbk' => 'application/docbook+xml',
'dcr' => 'application/x-director',
'dcurl' => 'text/vnd.curl.dcurl',
'dd2' => 'application/vnd.oma.dd2+xml',
'ddd' => 'application/vnd.fujixerox.ddd',
'deb' => 'application/x-debian-package',
'def' => 'text/plain',
'deploy' => 'application/octet-stream',
'der' => 'application/x-x509-ca-cert',
'dfac' => 'application/vnd.dreamfactory',
'dgc' => 'application/x-dgc-compressed',
'dic' => 'text/x-c',
'dir' => 'application/x-director',
'dis' => 'application/vnd.mobius.dis',
'dist' => 'application/octet-stream',
'distz' => 'application/octet-stream',
'djv' => 'image/vnd.djvu',
'djvu' => 'image/vnd.djvu',
'dll' => 'application/x-msdownload',
'dmg' => 'application/x-apple-diskimage',
'dmp' => 'application/vnd.tcpdump.pcap',
'dms' => 'application/octet-stream',
'dna' => 'application/vnd.dna',
'doc' => 'application/msword',
'docm' => 'application/vnd.ms-word.document.macroenabled.12',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'dot' => 'application/msword',
'dotm' => 'application/vnd.ms-word.template.macroenabled.12',
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'dp' => 'application/vnd.osgi.dp',
'dpg' => 'application/vnd.dpgraph',
'dra' => 'audio/vnd.dra',
'dsc' => 'text/prs.lines.tag',
'dssc' => 'application/dssc+der',
'dtb' => 'application/x-dtbook+xml',
'dtd' => 'application/xml-dtd',
'dts' => 'audio/vnd.dts',
'dtshd' => 'audio/vnd.dts.hd',
'dump' => 'application/octet-stream',
'dvb' => 'video/vnd.dvb.file',
'dvi' => 'application/x-dvi',
'dwf' => 'model/vnd.dwf',
'dwg' => 'image/vnd.dwg',
'dxf' => 'image/vnd.dxf',
'dxp' => 'application/vnd.spotfire.dxp',
'dxr' => 'application/x-director',
'ecelp4800' => 'audio/vnd.nuera.ecelp4800',
'ecelp7470' => 'audio/vnd.nuera.ecelp7470',
'ecelp9600' => 'audio/vnd.nuera.ecelp9600',
'ecma' => 'application/ecmascript',
'edm' => 'application/vnd.novadigm.edm',
'edx' => 'application/vnd.novadigm.edx',
'efif' => 'application/vnd.picsel',
'ei6' => 'application/vnd.pg.osasli',
'elc' => 'application/octet-stream',
'emf' => 'application/x-msmetafile',
'eml' => 'message/rfc822',
'emma' => 'application/emma+xml',
'emz' => 'application/x-msmetafile',
'eol' => 'audio/vnd.digital-winds',
'eot' => 'application/vnd.ms-fontobject',
'eps' => 'application/postscript',
'epub' => 'application/epub+zip',
'es3' => 'application/vnd.eszigno3+xml',
'esa' => 'application/vnd.osgi.subsystem',
'esf' => 'application/vnd.epson.esf',
'et3' => 'application/vnd.eszigno3+xml',
'etx' => 'text/x-setext',
'eva' => 'application/x-eva',
'evy' => 'application/x-envoy',
'exe' => 'application/x-msdownload',
'exi' => 'application/exi',
'ext' => 'application/vnd.novadigm.ext',
'ez' => 'application/andrew-inset',
'ez2' => 'application/vnd.ezpix-album',
'ez3' => 'application/vnd.ezpix-package',
'f' => 'text/x-fortran',
'f4v' => 'video/x-f4v',
'f77' => 'text/x-fortran',
'f90' => 'text/x-fortran',
'fbs' => 'image/vnd.fastbidsheet',
'fcdt' => 'application/vnd.adobe.formscentral.fcdt',
'fcs' => 'application/vnd.isac.fcs',
'fdf' => 'application/vnd.fdf',
'fe_launch' => 'application/vnd.denovo.fcselayout-link',
'fg5' => 'application/vnd.fujitsu.oasysgp',
'fgd' => 'application/x-director',
'fh' => 'image/x-freehand',
'fh4' => 'image/x-freehand',
'fh5' => 'image/x-freehand',
'fh7' => 'image/x-freehand',
'fhc' => 'image/x-freehand',
'fig' => 'application/x-xfig',
'flac' => 'audio/x-flac',
'fli' => 'video/x-fli',
'flo' => 'application/vnd.micrografx.flo',
'flv' => 'video/x-flv',
'flw' => 'application/vnd.kde.kivio',
'flx' => 'text/vnd.fmi.flexstor',
'fly' => 'text/vnd.fly',
'fm' => 'application/vnd.framemaker',
'fnc' => 'application/vnd.frogans.fnc',
'for' => 'text/x-fortran',
'fpx' => 'image/vnd.fpx',
'frame' => 'application/vnd.framemaker',
'fsc' => 'application/vnd.fsc.weblaunch',
'fst' => 'image/vnd.fst',
'ftc' => 'application/vnd.fluxtime.clip',
'fti' => 'application/vnd.anser-web-funds-transfer-initiation',
'fvt' => 'video/vnd.fvt',
'fxp' => 'application/vnd.adobe.fxp',
'fxpl' => 'application/vnd.adobe.fxp',
'fzs' => 'application/vnd.fuzzysheet',
'g2w' => 'application/vnd.geoplan',
'g3' => 'image/g3fax',
'g3w' => 'application/vnd.geospace',
'gac' => 'application/vnd.groove-account',
'gam' => 'application/x-tads',
'gbr' => 'application/rpki-ghostbusters',
'gca' => 'application/x-gca-compressed',
'gdl' => 'model/vnd.gdl',
'geo' => 'application/vnd.dynageo',
'gex' => 'application/vnd.geometry-explorer',
'ggb' => 'application/vnd.geogebra.file',
'ggt' => 'application/vnd.geogebra.tool',
'ghf' => 'application/vnd.groove-help',
'gif' => 'image/gif',
'gim' => 'application/vnd.groove-identity-message',
'gml' => 'application/gml+xml',
'gmx' => 'application/vnd.gmx',
'gnumeric' => 'application/x-gnumeric',
'gph' => 'application/vnd.flographit',
'gpx' => 'application/gpx+xml',
'gqf' => 'application/vnd.grafeq',
'gqs' => 'application/vnd.grafeq',
'gram' => 'application/srgs',
'gramps' => 'application/x-gramps-xml',
'gre' => 'application/vnd.geometry-explorer',
'grv' => 'application/vnd.groove-injector',
'grxml' => 'application/srgs+xml',
'gsf' => 'application/x-font-ghostscript',
'gtar' => 'application/x-gtar',
'gtm' => 'application/vnd.groove-tool-message',
'gtw' => 'model/vnd.gtw',
'gv' => 'text/vnd.graphviz',
'gxf' => 'application/gxf',
'gxt' => 'application/vnd.geonext',
'h' => 'text/x-c',
'h261' => 'video/h261',
'h263' => 'video/h263',
'h264' => 'video/h264',
'hal' => 'application/vnd.hal+xml',
'hbci' => 'application/vnd.hbci',
'hdf' => 'application/x-hdf',
'hh' => 'text/x-c',
'hlp' => 'application/winhlp',
'hpgl' => 'application/vnd.hp-hpgl',
'hpid' => 'application/vnd.hp-hpid',
'hps' => 'application/vnd.hp-hps',
'hqx' => 'application/mac-binhex40',
'htke' => 'application/vnd.kenameaapp',
'htm' => 'text/html',
'html' => 'text/html',
'hvd' => 'application/vnd.yamaha.hv-dic',
'hvp' => 'application/vnd.yamaha.hv-voice',
'hvs' => 'application/vnd.yamaha.hv-script',
'i2g' => 'application/vnd.intergeo',
'icc' => 'application/vnd.iccprofile',
'ice' => 'x-conference/x-cooltalk',
'icm' => 'application/vnd.iccprofile',
'ico' => 'image/x-icon',
'ics' => 'text/calendar',
'ief' => 'image/ief',
'ifb' => 'text/calendar',
'ifm' => 'application/vnd.shana.informed.formdata',
'iges' => 'model/iges',
'igl' => 'application/vnd.igloader',
'igm' => 'application/vnd.insors.igm',
'igs' => 'model/iges',
'igx' => 'application/vnd.micrografx.igx',
'iif' => 'application/vnd.shana.informed.interchange',
'imp' => 'application/vnd.accpac.simply.imp',
'ims' => 'application/vnd.ms-ims',
'in' => 'text/plain',
'ink' => 'application/inkml+xml',
'inkml' => 'application/inkml+xml',
'install' => 'application/x-install-instructions',
'iota' => 'application/vnd.astraea-software.iota',
'ipfix' => 'application/ipfix',
'ipk' => 'application/vnd.shana.informed.package',
'irm' => 'application/vnd.ibm.rights-management',
'irp' => 'application/vnd.irepository.package+xml',
'iso' => 'application/x-iso9660-image',
'itp' => 'application/vnd.shana.informed.formtemplate',
'ivp' => 'application/vnd.immervision-ivp',
'ivu' => 'application/vnd.immervision-ivu',
'jad' => 'text/vnd.sun.j2me.app-descriptor',
'jam' => 'application/vnd.jam',
'jar' => 'application/java-archive',
'java' => 'text/x-java-source',
'jisp' => 'application/vnd.jisp',
'jlt' => 'application/vnd.hp-jlyt',
'jnlp' => 'application/x-java-jnlp-file',
'joda' => 'application/vnd.joost.joda-archive',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpgm' => 'video/jpm',
'jpgv' => 'video/jpeg',
'jpm' => 'video/jpm',
'js' => 'application/javascript',
'json' => 'application/json',
'jsonml' => 'application/jsonml+json',
'kar' => 'audio/midi',
'karbon' => 'application/vnd.kde.karbon',
'kfo' => 'application/vnd.kde.kformula',
'kia' => 'application/vnd.kidspiration',
'kml' => 'application/vnd.google-earth.kml+xml',
'kmz' => 'application/vnd.google-earth.kmz',
'kne' => 'application/vnd.kinar',
'knp' => 'application/vnd.kinar',
'kon' => 'application/vnd.kde.kontour',
'kpr' => 'application/vnd.kde.kpresenter',
'kpt' => 'application/vnd.kde.kpresenter',
'kpxx' => 'application/vnd.ds-keypoint',
'ksp' => 'application/vnd.kde.kspread',
'ktr' => 'application/vnd.kahootz',
'ktx' => 'image/ktx',
'ktz' => 'application/vnd.kahootz',
'kwd' => 'application/vnd.kde.kword',
'kwt' => 'application/vnd.kde.kword',
'lasxml' => 'application/vnd.las.las+xml',
'latex' => 'application/x-latex',
'lbd' => 'application/vnd.llamagraphics.life-balance.desktop',
'lbe' => 'application/vnd.llamagraphics.life-balance.exchange+xml',
'les' => 'application/vnd.hhe.lesson-player',
'lha' => 'application/x-lzh-compressed',
'link66' => 'application/vnd.route66.link66+xml',
'list' => 'text/plain',
'list3820' => 'application/vnd.ibm.modcap',
'listafp' => 'application/vnd.ibm.modcap',
'lnk' => 'application/x-ms-shortcut',
'log' => 'text/plain',
'lostxml' => 'application/lost+xml',
'lrf' => 'application/octet-stream',
'lrm' => 'application/vnd.ms-lrm',
'ltf' => 'application/vnd.frogans.ltf',
'lvp' => 'audio/vnd.lucent.voice',
'lwp' => 'application/vnd.lotus-wordpro',
'lzh' => 'application/x-lzh-compressed',
'm13' => 'application/x-msmediaview',
'm14' => 'application/x-msmediaview',
'm1v' => 'video/mpeg',
'm21' => 'application/mp21',
'm2a' => 'audio/mpeg',
'm2v' => 'video/mpeg',
'm3a' => 'audio/mpeg',
'm3u' => 'audio/x-mpegurl',
'm3u8' => 'application/vnd.apple.mpegurl',
'm4a' => 'audio/mp4',
'm4u' => 'video/vnd.mpegurl',
'm4v' => 'video/x-m4v',
'ma' => 'application/mathematica',
'mads' => 'application/mads+xml',
'mag' => 'application/vnd.ecowin.chart',
'maker' => 'application/vnd.framemaker',
'man' => 'text/troff',
'mar' => 'application/octet-stream',
'mathml' => 'application/mathml+xml',
'mb' => 'application/mathematica',
'mbk' => 'application/vnd.mobius.mbk',
'mbox' => 'application/mbox',
'mc1' => 'application/vnd.medcalcdata',
'mcd' => 'application/vnd.mcd',
'mcurl' => 'text/vnd.curl.mcurl',
'mdb' => 'application/x-msaccess',
'mdi' => 'image/vnd.ms-modi',
'me' => 'text/troff',
'mesh' => 'model/mesh',
'meta4' => 'application/metalink4+xml',
'metalink' => 'application/metalink+xml',
'mets' => 'application/mets+xml',
'mfm' => 'application/vnd.mfmp',
'mft' => 'application/rpki-manifest',
'mgp' => 'application/vnd.osgeo.mapguide.package',
'mgz' => 'application/vnd.proteus.magazine',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mie' => 'application/x-mie',
'mif' => 'application/vnd.mif',
'mime' => 'message/rfc822',
'mj2' => 'video/mj2',
'mjp2' => 'video/mj2',
'mk3d' => 'video/x-matroska',
'mka' => 'audio/x-matroska',
'mks' => 'video/x-matroska',
'mkv' => 'video/x-matroska',
'mlp' => 'application/vnd.dolby.mlp',
'mmd' => 'application/vnd.chipnuts.karaoke-mmd',
'mmf' => 'application/vnd.smaf',
'mmr' => 'image/vnd.fujixerox.edmics-mmr',
'mng' => 'video/x-mng',
'mny' => 'application/x-msmoney',
'mobi' => 'application/x-mobipocket-ebook',
'mods' => 'application/mods+xml',
'mov' => 'video/quicktime',
'movie' => 'video/x-sgi-movie',
'mp2' => 'audio/mpeg',
'mp21' => 'application/mp21',
'mp2a' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'mp4' => 'video/mp4',
'mp4a' => 'audio/mp4',
'mp4s' => 'application/mp4',
'mp4v' => 'video/mp4',
'mpc' => 'application/vnd.mophun.certificate',
'mpe' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpg4' => 'video/mp4',
'mpga' => 'audio/mpeg',
'mpkg' => 'application/vnd.apple.installer+xml',
'mpm' => 'application/vnd.blueice.multipass',
'mpn' => 'application/vnd.mophun.application',
'mpp' => 'application/vnd.ms-project',
'mpt' => 'application/vnd.ms-project',
'mpy' => 'application/vnd.ibm.minipay',
'mqy' => 'application/vnd.mobius.mqy',
'mrc' => 'application/marc',
'mrcx' => 'application/marcxml+xml',
'ms' => 'text/troff',
'mscml' => 'application/mediaservercontrol+xml',
'mseed' => 'application/vnd.fdsn.mseed',
'mseq' => 'application/vnd.mseq',
'msf' => 'application/vnd.epson.msf',
'msh' => 'model/mesh',
'msi' => 'application/x-msdownload',
'msl' => 'application/vnd.mobius.msl',
'msty' => 'application/vnd.muvee.style',
'mts' => 'model/vnd.mts',
'mus' => 'application/vnd.musician',
'musicxml' => 'application/vnd.recordare.musicxml+xml',
'mvb' => 'application/x-msmediaview',
'mwf' => 'application/vnd.mfer',
'mxf' => 'application/mxf',
'mxl' => 'application/vnd.recordare.musicxml',
'mxml' => 'application/xv+xml',
'mxs' => 'application/vnd.triscape.mxs',
'mxu' => 'video/vnd.mpegurl',
'n-gage' => 'application/vnd.nokia.n-gage.symbian.install',
'n3' => 'text/n3',
'nb' => 'application/mathematica',
'nbp' => 'application/vnd.wolfram.player',
'nc' => 'application/x-netcdf',
'ncx' => 'application/x-dtbncx+xml',
'nfo' => 'text/x-nfo',
'ngdat' => 'application/vnd.nokia.n-gage.data',
'nitf' => 'application/vnd.nitf',
'nlu' => 'application/vnd.neurolanguage.nlu',
'nml' => 'application/vnd.enliven',
'nnd' => 'application/vnd.noblenet-directory',
'nns' => 'application/vnd.noblenet-sealer',
'nnw' => 'application/vnd.noblenet-web',
'npx' => 'image/vnd.net-fpx',
'nsc' => 'application/x-conference',
'nsf' => 'application/vnd.lotus-notes',
'ntf' => 'application/vnd.nitf',
'nzb' => 'application/x-nzb',
'oa2' => 'application/vnd.fujitsu.oasys2',
'oa3' => 'application/vnd.fujitsu.oasys3',
'oas' => 'application/vnd.fujitsu.oasys',
'obd' => 'application/x-msbinder',
'obj' => 'application/x-tgif',
'oda' => 'application/oda',
'odb' => 'application/vnd.oasis.opendocument.database',
'odc' => 'application/vnd.oasis.opendocument.chart',
'odf' => 'application/vnd.oasis.opendocument.formula',
'odft' => 'application/vnd.oasis.opendocument.formula-template',
'odg' => 'application/vnd.oasis.opendocument.graphics',
'odi' => 'application/vnd.oasis.opendocument.image',
'odm' => 'application/vnd.oasis.opendocument.text-master',
'odp' => 'application/vnd.oasis.opendocument.presentation',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'odt' => 'application/vnd.oasis.opendocument.text',
'oga' => 'audio/ogg',
'ogg' => 'audio/ogg',
'ogv' => 'video/ogg',
'ogx' => 'application/ogg',
'omdoc' => 'application/omdoc+xml',
'onepkg' => 'application/onenote',
'onetmp' => 'application/onenote',
'onetoc' => 'application/onenote',
'onetoc2' => 'application/onenote',
'opf' => 'application/oebps-package+xml',
'opml' => 'text/x-opml',
'oprc' => 'application/vnd.palm',
'org' => 'application/vnd.lotus-organizer',
'osf' => 'application/vnd.yamaha.openscoreformat',
'osfpvg' => 'application/vnd.yamaha.openscoreformat.osfpvg+xml',
'otc' => 'application/vnd.oasis.opendocument.chart-template',
'otf' => 'font/otf',
'otg' => 'application/vnd.oasis.opendocument.graphics-template',
'oth' => 'application/vnd.oasis.opendocument.text-web',
'oti' => 'application/vnd.oasis.opendocument.image-template',
'otp' => 'application/vnd.oasis.opendocument.presentation-template',
'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
'ott' => 'application/vnd.oasis.opendocument.text-template',
'oxps' => 'application/oxps',
'oxt' => 'application/vnd.openofficeorg.extension',
'p' => 'text/x-pascal',
'p10' => 'application/pkcs10',
'p12' => 'application/x-pkcs12',
'p7b' => 'application/x-pkcs7-certificates',
'p7c' => 'application/pkcs7-mime',
'p7m' => 'application/pkcs7-mime',
'p7r' => 'application/x-pkcs7-certreqresp',
'p7s' => 'application/pkcs7-signature',
'p8' => 'application/pkcs8',
'pas' => 'text/x-pascal',
'paw' => 'application/vnd.pawaafile',
'pbd' => 'application/vnd.powerbuilder6',
'pbm' => 'image/x-portable-bitmap',
'pcap' => 'application/vnd.tcpdump.pcap',
'pcf' => 'application/x-font-pcf',
'pcl' => 'application/vnd.hp-pcl',
'pclxl' => 'application/vnd.hp-pclxl',
'pct' => 'image/x-pict',
'pcurl' => 'application/vnd.curl.pcurl',
'pcx' => 'image/x-pcx',
'pdb' => 'application/vnd.palm',
'pdf' => 'application/pdf',
'pfa' => 'application/x-font-type1',
'pfb' => 'application/x-font-type1',
'pfm' => 'application/x-font-type1',
'pfr' => 'application/font-tdpfr',
'pfx' => 'application/x-pkcs12',
'pgm' => 'image/x-portable-graymap',
'pgn' => 'application/x-chess-pgn',
'pgp' => 'application/pgp-encrypted',
'pic' => 'image/x-pict',
'pkg' => 'application/octet-stream',
'pki' => 'application/pkixcmp',
'pkipath' => 'application/pkix-pkipath',
'plb' => 'application/vnd.3gpp.pic-bw-large',
'plc' => 'application/vnd.mobius.plc',
'plf' => 'application/vnd.pocketlearn',
'pls' => 'application/pls+xml',
'pml' => 'application/vnd.ctc-posml',
'png' => 'image/png',
'pnm' => 'image/x-portable-anymap',
'portpkg' => 'application/vnd.macports.portpkg',
'pot' => 'application/vnd.ms-powerpoint',
'potm' => 'application/vnd.ms-powerpoint.template.macroenabled.12',
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
'ppam' => 'application/vnd.ms-powerpoint.addin.macroenabled.12',
'ppd' => 'application/vnd.cups-ppd',
'ppm' => 'image/x-portable-pixmap',
'pps' => 'application/vnd.ms-powerpoint',
'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroenabled.12',
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'ppt' => 'application/vnd.ms-powerpoint',
'pptm' => 'application/vnd.ms-powerpoint.presentation.macroenabled.12',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'pqa' => 'application/vnd.palm',
'prc' => 'application/x-mobipocket-ebook',
'pre' => 'application/vnd.lotus-freelance',
'prf' => 'application/pics-rules',
'ps' => 'application/postscript',
'psb' => 'application/vnd.3gpp.pic-bw-small',
'psd' => 'image/vnd.adobe.photoshop',
'psf' => 'application/x-font-linux-psf',
'pskcxml' => 'application/pskc+xml',
'ptid' => 'application/vnd.pvi.ptid1',
'pub' => 'application/x-mspublisher',
'pvb' => 'application/vnd.3gpp.pic-bw-var',
'pwn' => 'application/vnd.3m.post-it-notes',
'pya' => 'audio/vnd.ms-playready.media.pya',
'pyv' => 'video/vnd.ms-playready.media.pyv',
'qam' => 'application/vnd.epson.quickanime',
'qbo' => 'application/vnd.intu.qbo',
'qfx' => 'application/vnd.intu.qfx',
'qps' => 'application/vnd.publishare-delta-tree',
'qt' => 'video/quicktime',
'qwd' => 'application/vnd.quark.quarkxpress',
'qwt' => 'application/vnd.quark.quarkxpress',
'qxb' => 'application/vnd.quark.quarkxpress',
'qxd' => 'application/vnd.quark.quarkxpress',
'qxl' => 'application/vnd.quark.quarkxpress',
'qxt' => 'application/vnd.quark.quarkxpress',
'ra' => 'audio/x-pn-realaudio',
'ram' => 'audio/x-pn-realaudio',
'rar' => 'application/x-rar-compressed',
'ras' => 'image/x-cmu-raster',
'rcprofile' => 'application/vnd.ipunplugged.rcprofile',
'rdf' => 'application/rdf+xml',
'rdz' => 'application/vnd.data-vision.rdz',
'rep' => 'application/vnd.businessobjects',
'res' => 'application/x-dtbresource+xml',
'rgb' => 'image/x-rgb',
'rif' => 'application/reginfo+xml',
'rip' => 'audio/vnd.rip',
'ris' => 'application/x-research-info-systems',
'rl' => 'application/resource-lists+xml',
'rlc' => 'image/vnd.fujixerox.edmics-rlc',
'rld' => 'application/resource-lists-diff+xml',
'rm' => 'application/vnd.rn-realmedia',
'rmi' => 'audio/midi',
'rmp' => 'audio/x-pn-realaudio-plugin',
'rms' => 'application/vnd.jcp.javame.midlet-rms',
'rmvb' => 'application/vnd.rn-realmedia-vbr',
'rnc' => 'application/relax-ng-compact-syntax',
'roa' => 'application/rpki-roa',
'roff' => 'text/troff',
'rp9' => 'application/vnd.cloanto.rp9',
'rpss' => 'application/vnd.nokia.radio-presets',
'rpst' => 'application/vnd.nokia.radio-preset',
'rq' => 'application/sparql-query',
'rs' => 'application/rls-services+xml',
'rsd' => 'application/rsd+xml',
'rss' => 'application/rss+xml',
'rtf' => 'application/rtf',
'rtx' => 'text/richtext',
's' => 'text/x-asm',
's3m' => 'audio/s3m',
'saf' => 'application/vnd.yamaha.smaf-audio',
'sbml' => 'application/sbml+xml',
'sc' => 'application/vnd.ibm.secure-container',
'scd' => 'application/x-msschedule',
'scm' => 'application/vnd.lotus-screencam',
'scq' => 'application/scvp-cv-request',
'scs' => 'application/scvp-cv-response',
'scurl' => 'text/vnd.curl.scurl',
'sda' => 'application/vnd.stardivision.draw',
'sdc' => 'application/vnd.stardivision.calc',
'sdd' => 'application/vnd.stardivision.impress',
'sdkd' => 'application/vnd.solent.sdkm+xml',
'sdkm' => 'application/vnd.solent.sdkm+xml',
'sdp' => 'application/sdp',
'sdw' => 'application/vnd.stardivision.writer',
'see' => 'application/vnd.seemail',
'seed' => 'application/vnd.fdsn.seed',
'sema' => 'application/vnd.sema',
'semd' => 'application/vnd.semd',
'semf' => 'application/vnd.semf',
'ser' => 'application/java-serialized-object',
'setpay' => 'application/set-payment-initiation',
'setreg' => 'application/set-registration-initiation',
'sfd-hdstx' => 'application/vnd.hydrostatix.sof-data',
'sfs' => 'application/vnd.spotfire.sfs',
'sfv' => 'text/x-sfv',
'sgi' => 'image/sgi',
'sgl' => 'application/vnd.stardivision.writer-global',
'sgm' => 'text/sgml',
'sgml' => 'text/sgml',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'shf' => 'application/shf+xml',
'sid' => 'image/x-mrsid-image',
'sig' => 'application/pgp-signature',
'sil' => 'audio/silk',
'silo' => 'model/mesh',
'sis' => 'application/vnd.symbian.install',
'sisx' => 'application/vnd.symbian.install',
'sit' => 'application/x-stuffit',
'sitx' => 'application/x-stuffitx',
'skd' => 'application/vnd.koan',
'skm' => 'application/vnd.koan',
'skp' => 'application/vnd.koan',
'skt' => 'application/vnd.koan',
'sldm' => 'application/vnd.ms-powerpoint.slide.macroenabled.12',
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
'slt' => 'application/vnd.epson.salt',
'sm' => 'application/vnd.stepmania.stepchart',
'smf' => 'application/vnd.stardivision.math',
'smi' => 'application/smil+xml',
'smil' => 'application/smil+xml',
'smv' => 'video/x-smv',
'smzip' => 'application/vnd.stepmania.package',
'snd' => 'audio/basic',
'snf' => 'application/x-font-snf',
'so' => 'application/octet-stream',
'spc' => 'application/x-pkcs7-certificates',
'spf' => 'application/vnd.yamaha.smaf-phrase',
'spl' => 'application/x-futuresplash',
'spot' => 'text/vnd.in3d.spot',
'spp' => 'application/scvp-vp-response',
'spq' => 'application/scvp-vp-request',
'spx' => 'audio/ogg',
'sql' => 'application/x-sql',
'src' => 'application/x-wais-source',
'srt' => 'application/x-subrip',
'sru' => 'application/sru+xml',
'srx' => 'application/sparql-results+xml',
'ssdl' => 'application/ssdl+xml',
'sse' => 'application/vnd.kodak-descriptor',
'ssf' => 'application/vnd.epson.ssf',
'ssml' => 'application/ssml+xml',
'st' => 'application/vnd.sailingtracker.track',
'stc' => 'application/vnd.sun.xml.calc.template',
'std' => 'application/vnd.sun.xml.draw.template',
'stf' => 'application/vnd.wt.stf',
'sti' => 'application/vnd.sun.xml.impress.template',
'stk' => 'application/hyperstudio',
'stl' => 'application/vnd.ms-pki.stl',
'str' => 'application/vnd.pg.format',
'stw' => 'application/vnd.sun.xml.writer.template',
'sub' => 'text/vnd.dvb.subtitle',
'sus' => 'application/vnd.sus-calendar',
'susp' => 'application/vnd.sus-calendar',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'svc' => 'application/vnd.dvb.service',
'svd' => 'application/vnd.svd',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
'swa' => 'application/x-director',
'swf' => 'application/x-shockwave-flash',
'swi' => 'application/vnd.aristanetworks.swi',
'sxc' => 'application/vnd.sun.xml.calc',
'sxd' => 'application/vnd.sun.xml.draw',
'sxg' => 'application/vnd.sun.xml.writer.global',
'sxi' => 'application/vnd.sun.xml.impress',
'sxm' => 'application/vnd.sun.xml.math',
'sxw' => 'application/vnd.sun.xml.writer',
't' => 'text/troff',
't3' => 'application/x-t3vm-image',
'taglet' => 'application/vnd.mynfc',
'tao' => 'application/vnd.tao.intent-module-archive',
'tar' => 'application/x-tar',
'tcap' => 'application/vnd.3gpp2.tcap',
'tcl' => 'application/x-tcl',
'teacher' => 'application/vnd.smart.teacher',
'tei' => 'application/tei+xml',
'teicorpus' => 'application/tei+xml',
'tex' => 'application/x-tex',
'texi' => 'application/x-texinfo',
'texinfo' => 'application/x-texinfo',
'text' => 'text/plain',
'tfi' => 'application/thraud+xml',
'tfm' => 'application/x-tex-tfm',
'tga' => 'image/x-tga',
'thmx' => 'application/vnd.ms-officetheme',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'tmo' => 'application/vnd.tmobile-livetv',
'torrent' => 'application/x-bittorrent',
'tpl' => 'application/vnd.groove-tool-template',
'tpt' => 'application/vnd.trid.tpt',
'tr' => 'text/troff',
'tra' => 'application/vnd.trueapp',
'trm' => 'application/x-msterminal',
'tsd' => 'application/timestamped-data',
'tsv' => 'text/tab-separated-values',
'ttc' => 'font/collection',
'ttf' => 'font/ttf',
'ttl' => 'text/turtle',
'twd' => 'application/vnd.simtech-mindmapper',
'twds' => 'application/vnd.simtech-mindmapper',
'txd' => 'application/vnd.genomatix.tuxedo',
'txf' => 'application/vnd.mobius.txf',
'txt' => 'text/plain',
'u32' => 'application/x-authorware-bin',
'udeb' => 'application/x-debian-package',
'ufd' => 'application/vnd.ufdl',
'ufdl' => 'application/vnd.ufdl',
'ulx' => 'application/x-glulx',
'umj' => 'application/vnd.umajin',
'unityweb' => 'application/vnd.unity',
'uoml' => 'application/vnd.uoml+xml',
'uri' => 'text/uri-list',
'uris' => 'text/uri-list',
'urls' => 'text/uri-list',
'ustar' => 'application/x-ustar',
'utz' => 'application/vnd.uiq.theme',
'uu' => 'text/x-uuencode',
'uva' => 'audio/vnd.dece.audio',
'uvd' => 'application/vnd.dece.data',
'uvf' => 'application/vnd.dece.data',
'uvg' => 'image/vnd.dece.graphic',
'uvh' => 'video/vnd.dece.hd',
'uvi' => 'image/vnd.dece.graphic',
'uvm' => 'video/vnd.dece.mobile',
'uvp' => 'video/vnd.dece.pd',
'uvs' => 'video/vnd.dece.sd',
'uvt' => 'application/vnd.dece.ttml+xml',
'uvu' => 'video/vnd.uvvu.mp4',
'uvv' => 'video/vnd.dece.video',
'uvva' => 'audio/vnd.dece.audio',
'uvvd' => 'application/vnd.dece.data',
'uvvf' => 'application/vnd.dece.data',
'uvvg' => 'image/vnd.dece.graphic',
'uvvh' => 'video/vnd.dece.hd',
'uvvi' => 'image/vnd.dece.graphic',
'uvvm' => 'video/vnd.dece.mobile',
'uvvp' => 'video/vnd.dece.pd',
'uvvs' => 'video/vnd.dece.sd',
'uvvt' => 'application/vnd.dece.ttml+xml',
'uvvu' => 'video/vnd.uvvu.mp4',
'uvvv' => 'video/vnd.dece.video',
'uvvx' => 'application/vnd.dece.unspecified',
'uvvz' => 'application/vnd.dece.zip',
'uvx' => 'application/vnd.dece.unspecified',
'uvz' => 'application/vnd.dece.zip',
'vcard' => 'text/vcard',
'vcd' => 'application/x-cdlink',
'vcf' => 'text/x-vcard',
'vcg' => 'application/vnd.groove-vcard',
'vcs' => 'text/x-vcalendar',
'vcx' => 'application/vnd.vcx',
'vis' => 'application/vnd.visionary',
'viv' => 'video/vnd.vivo',
'vob' => 'video/x-ms-vob',
'vor' => 'application/vnd.stardivision.writer',
'vox' => 'application/x-authorware-bin',
'vrml' => 'model/vrml',
'vsd' => 'application/vnd.visio',
'vsf' => 'application/vnd.vsf',
'vss' => 'application/vnd.visio',
'vst' => 'application/vnd.visio',
'vsw' => 'application/vnd.visio',
'vtu' => 'model/vnd.vtu',
'vxml' => 'application/voicexml+xml',
'w3d' => 'application/x-director',
'wad' => 'application/x-doom',
'wav' => 'audio/x-wav',
'wax' => 'audio/x-ms-wax',
'wbmp' => 'image/vnd.wap.wbmp',
'wbs' => 'application/vnd.criticaltools.wbs+xml',
'wbxml' => 'application/vnd.wap.wbxml',
'wcm' => 'application/vnd.ms-works',
'wdb' => 'application/vnd.ms-works',
'wdp' => 'image/vnd.ms-photo',
'weba' => 'audio/webm',
'webm' => 'video/webm',
'webp' => 'image/webp',
'wg' => 'application/vnd.pmi.widget',
'wgt' => 'application/widget',
'wks' => 'application/vnd.ms-works',
'wm' => 'video/x-ms-wm',
'wma' => 'audio/x-ms-wma',
'wmd' => 'application/x-ms-wmd',
'wmf' => 'application/x-msmetafile',
'wml' => 'text/vnd.wap.wml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmls' => 'text/vnd.wap.wmlscript',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'wmv' => 'video/x-ms-wmv',
'wmx' => 'video/x-ms-wmx',
'wmz' => 'application/x-msmetafile',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'wpd' => 'application/vnd.wordperfect',
'wpl' => 'application/vnd.ms-wpl',
'wps' => 'application/vnd.ms-works',
'wqd' => 'application/vnd.wqd',
'wri' => 'application/x-mswrite',
'wrl' => 'model/vrml',
'wsdl' => 'application/wsdl+xml',
'wspolicy' => 'application/wspolicy+xml',
'wtb' => 'application/vnd.webturbo',
'wvx' => 'video/x-ms-wvx',
'x32' => 'application/x-authorware-bin',
'x3d' => 'model/x3d+xml',
'x3db' => 'model/x3d+binary',
'x3dbz' => 'model/x3d+binary',
'x3dv' => 'model/x3d+vrml',
'x3dvz' => 'model/x3d+vrml',
'x3dz' => 'model/x3d+xml',
'xaml' => 'application/xaml+xml',
'xap' => 'application/x-silverlight-app',
'xar' => 'application/vnd.xara',
'xbap' => 'application/x-ms-xbap',
'xbd' => 'application/vnd.fujixerox.docuworks.binder',
'xbm' => 'image/x-xbitmap',
'xdf' => 'application/xcap-diff+xml',
'xdm' => 'application/vnd.syncml.dm+xml',
'xdp' => 'application/vnd.adobe.xdp+xml',
'xdssc' => 'application/dssc+xml',
'xdw' => 'application/vnd.fujixerox.docuworks',
'xenc' => 'application/xenc+xml',
'xer' => 'application/patch-ops-error+xml',
'xfdf' => 'application/vnd.adobe.xfdf',
'xfdl' => 'application/vnd.xfdl',
'xht' => 'application/xhtml+xml',
'xhtml' => 'application/xhtml+xml',
'xhvml' => 'application/xv+xml',
'xif' => 'image/vnd.xiff',
'xla' => 'application/vnd.ms-excel',
'xlam' => 'application/vnd.ms-excel.addin.macroenabled.12',
'xlc' => 'application/vnd.ms-excel',
'xlf' => 'application/x-xliff+xml',
'xlm' => 'application/vnd.ms-excel',
'xls' => 'application/vnd.ms-excel',
'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroenabled.12',
'xlsm' => 'application/vnd.ms-excel.sheet.macroenabled.12',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'xlt' => 'application/vnd.ms-excel',
'xltm' => 'application/vnd.ms-excel.template.macroenabled.12',
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'xlw' => 'application/vnd.ms-excel',
'xm' => 'audio/xm',
'xml' => 'application/xml',
'xo' => 'application/vnd.olpc-sugar',
'xop' => 'application/xop+xml',
'xpi' => 'application/x-xpinstall',
'xpl' => 'application/xproc+xml',
'xpm' => 'image/x-xpixmap',
'xpr' => 'application/vnd.is-xpr',
'xps' => 'application/vnd.ms-xpsdocument',
'xpw' => 'application/vnd.intercon.formnet',
'xpx' => 'application/vnd.intercon.formnet',
'xsl' => 'application/xml',
'xslt' => 'application/xslt+xml',
'xsm' => 'application/vnd.syncml+xml',
'xspf' => 'application/xspf+xml',
'xul' => 'application/vnd.mozilla.xul+xml',
'xvm' => 'application/xv+xml',
'xvml' => 'application/xv+xml',
'xwd' => 'image/x-xwindowdump',
'xyz' => 'chemical/x-xyz',
'xz' => 'application/x-xz',
'yang' => 'application/yang',
'yin' => 'application/yin+xml',
'z1' => 'application/x-zmachine',
'z2' => 'application/x-zmachine',
'z3' => 'application/x-zmachine',
'z4' => 'application/x-zmachine',
'z5' => 'application/x-zmachine',
'z6' => 'application/x-zmachine',
'z7' => 'application/x-zmachine',
'z8' => 'application/x-zmachine',
'zaz' => 'application/vnd.zzazz.deck+xml',
'zip' => 'application/zip',
'zir' => 'application/vnd.zul',
'zirz' => 'application/vnd.zul',
'zmm' => 'application/vnd.handheld-entertainment+xml',
123 => 'application/vnd.lotus-1-2-3',
];