diff --git a/docs/guide/input-validation.md b/docs/guide/input-validation.md index 0122046ed7..981657b7ba 100644 --- a/docs/guide/input-validation.md +++ b/docs/guide/input-validation.md @@ -518,7 +518,7 @@ class StatusValidator extends Validator public function clientValidateAttribute($model, $attribute, $view) { $statuses = json_encode(Status::find()->select('id')->asArray()->column()); - $message = json_encode($this->message); + $message = json_encode($this->message, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); return <<keywords)); + return json_encode(explode(',', $this->keywords), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } /** diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 3a3509e218..1215482f88 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -8,6 +8,8 @@ Yii Framework 2 Change Log - Bug #5584: `yii\rbac\DbRbacManager` should not delete items when deleting a rule on a database not supporting cascade update (mdmunir) - Bug #5601: Simple conditions in Query::where() and ActiveQuery::where() did not allow `yii\db\Expression` to be used as the value (cebe, stevekr) - Bug: Gii console command help information does not contain global options (qiangxue) +- Enh #5587: `json_encode` is now used with `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE` where it makes sense, also + it is now default for `Json::encode()` (samdark) - Enh #5600: Allow configuring debug panels in `yii\debug\Module::panels` as panel class name strings (qiangxue) - Enh #5613: Added `--overwrite` option to Gii console command to support overwriting all files (motin, qiangxue) - Enh #5646: Call `yii\base\ErrorHandler::unregister()` instead of `restore_*_handlers` directly (aivus) diff --git a/framework/captcha/CaptchaAction.php b/framework/captcha/CaptchaAction.php index ac9d5e9b3b..f4febbd01b 100644 --- a/framework/captcha/CaptchaAction.php +++ b/framework/captcha/CaptchaAction.php @@ -127,7 +127,7 @@ class CaptchaAction extends Action // we add a random 'v' parameter so that FireFox can refresh the image // when src attribute of image tag is changed 'url' => Url::to([$this->id, 'v' => uniqid()]), - ]); + ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } else { $this->setHttpHeaders(); Yii::$app->response->format = Response::FORMAT_RAW; diff --git a/framework/captcha/CaptchaValidator.php b/framework/captcha/CaptchaValidator.php index 7d122b3c24..14a14abe65 100644 --- a/framework/captcha/CaptchaValidator.php +++ b/framework/captcha/CaptchaValidator.php @@ -103,6 +103,6 @@ class CaptchaValidator extends Validator ValidationAsset::register($view); - return 'yii.validation.captcha(value, messages, ' . json_encode($options) . ');'; + return 'yii.validation.captcha(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; } } diff --git a/framework/grid/CheckboxColumn.php b/framework/grid/CheckboxColumn.php index 994600a6e3..7be244dbbf 100644 --- a/framework/grid/CheckboxColumn.php +++ b/framework/grid/CheckboxColumn.php @@ -84,7 +84,7 @@ class CheckboxColumn extends Column 'name' => $this->name, 'multiple' => $this->multiple, 'checkAll' => $name, - ]); + ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);"); if ($this->header !== null || !$this->multiple) { @@ -104,7 +104,7 @@ class CheckboxColumn extends Column } else { $options = $this->checkboxOptions; if (!isset($options['value'])) { - $options['value'] = is_array($key) ? json_encode($key) : $key; + $options['value'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key; } } diff --git a/framework/helpers/BaseJson.php b/framework/helpers/BaseJson.php index fa2e8e32e1..cf3dc4f5cd 100644 --- a/framework/helpers/BaseJson.php +++ b/framework/helpers/BaseJson.php @@ -28,10 +28,10 @@ class BaseJson * represented in terms of a [[JsExpression]] object. * @param mixed $value the data to be encoded * @param integer $options the encoding options. For more details please refer to - * . + * . Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`. * @return string the encoding result */ - public static function encode($value, $options = 0) + public static function encode($value, $options = 320) { $expressions = []; $value = static::processData($value, $expressions, uniqid()); diff --git a/framework/validators/BooleanValidator.php b/framework/validators/BooleanValidator.php index 2b902c86dc..09b45f3c3b 100644 --- a/framework/validators/BooleanValidator.php +++ b/framework/validators/BooleanValidator.php @@ -87,6 +87,6 @@ class BooleanValidator extends Validator ValidationAsset::register($view); - return 'yii.validation.boolean(value, messages, ' . json_encode($options) . ');'; + return 'yii.validation.boolean(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; } } diff --git a/framework/validators/CompareValidator.php b/framework/validators/CompareValidator.php index d96964b443..70bc3ffd3a 100644 --- a/framework/validators/CompareValidator.php +++ b/framework/validators/CompareValidator.php @@ -229,6 +229,6 @@ class CompareValidator extends Validator ValidationAsset::register($view); - return 'yii.validation.compare(value, messages, ' . json_encode($options) . ');'; + return 'yii.validation.compare(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; } } diff --git a/framework/validators/FileValidator.php b/framework/validators/FileValidator.php index 1dc536c01d..d43119c524 100644 --- a/framework/validators/FileValidator.php +++ b/framework/validators/FileValidator.php @@ -338,7 +338,7 @@ class FileValidator extends Validator { ValidationAsset::register($view); $options = $this->getClientOptions($object, $attribute); - return 'yii.validation.file(attribute, messages, ' . json_encode($options) . ');'; + return 'yii.validation.file(attribute, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; } /** diff --git a/framework/validators/ImageValidator.php b/framework/validators/ImageValidator.php index 8ce23b6f07..6643c6ceb0 100644 --- a/framework/validators/ImageValidator.php +++ b/framework/validators/ImageValidator.php @@ -166,7 +166,7 @@ class ImageValidator extends FileValidator { ValidationAsset::register($view); $options = $this->getClientOptions($object, $attribute); - return 'yii.validation.image(attribute, messages, ' . json_encode($options) . ', deferred);'; + return 'yii.validation.image(attribute, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ', deferred);'; } /** diff --git a/framework/validators/RangeValidator.php b/framework/validators/RangeValidator.php index 6ce9b2976e..370a1d5554 100644 --- a/framework/validators/RangeValidator.php +++ b/framework/validators/RangeValidator.php @@ -101,6 +101,6 @@ class RangeValidator extends Validator ValidationAsset::register($view); - return 'yii.validation.range(value, messages, ' . json_encode($options) . ');'; + return 'yii.validation.range(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; } } diff --git a/framework/validators/RequiredValidator.php b/framework/validators/RequiredValidator.php index 9f8a119a72..3b57876a09 100644 --- a/framework/validators/RequiredValidator.php +++ b/framework/validators/RequiredValidator.php @@ -107,6 +107,6 @@ class RequiredValidator extends Validator ValidationAsset::register($view); - return 'yii.validation.required(value, messages, ' . json_encode($options) . ');'; + return 'yii.validation.required(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; } } diff --git a/framework/validators/StringValidator.php b/framework/validators/StringValidator.php index a75a56a2a0..0ca40c6c69 100644 --- a/framework/validators/StringValidator.php +++ b/framework/validators/StringValidator.php @@ -183,6 +183,6 @@ class StringValidator extends Validator ValidationAsset::register($view); - return 'yii.validation.string(value, messages, ' . json_encode($options) . ');'; + return 'yii.validation.string(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; } } diff --git a/framework/web/User.php b/framework/web/User.php index 96baf99b6b..e4640409db 100644 --- a/framework/web/User.php +++ b/framework/web/User.php @@ -538,7 +538,7 @@ class User extends Component $identity->getId(), $identity->getAuthKey(), $duration, - ]); + ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $cookie->expire = time() + $duration; Yii::$app->getResponse()->getCookies()->add($cookie); } diff --git a/framework/widgets/ListView.php b/framework/widgets/ListView.php index 107c6b616f..dc8e79ffff 100644 --- a/framework/widgets/ListView.php +++ b/framework/widgets/ListView.php @@ -101,7 +101,7 @@ class ListView extends BaseListView $options = $this->itemOptions; $tag = ArrayHelper::remove($options, 'tag', 'div'); if ($tag !== false) { - $options['data-key'] = is_array($key) ? json_encode($key) : (string) $key; + $options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key; return Html::tag($tag, $content, $options); } else {