RestApi
1 month ago
ApiDataSanitizer.php
2 months ago
CustomFieldsRepository.php
1 month ago
index.php
3 years ago
ApiDataSanitizer.php
185 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\CustomFields; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use InvalidArgumentException; |
| 9 | use MailPoet\Entities\CustomFieldEntity; |
| 10 | |
| 11 | class ApiDataSanitizer { |
| 12 | |
| 13 | const ERROR_MANDATORY_ARGUMENT_MISSING = 1001; |
| 14 | const ERROR_MANDATORY_ARGUMENT_WRONG_TYPE = 1002; |
| 15 | const ERROR_PARAMS_WRONG_TYPE = 1003; |
| 16 | const ERROR_INVALID_TYPE = 1004; |
| 17 | const ERROR_INVALID_VALIDATE = 1005; |
| 18 | const ERROR_CHECKBOX_WRONG_VALUES_COUNT = 1006; |
| 19 | const ERROR_INVALID_DATE_FORMAT = 1007; |
| 20 | const ERROR_INVALID_DATE_TYPE = 1008; |
| 21 | const ERROR_NO_VALUES = 1009; |
| 22 | const ERROR_NO_VALUE = 1010; |
| 23 | |
| 24 | public function sanitize(array $data = []) { |
| 25 | $this->checkMandatoryStringParameter($data, 'name'); |
| 26 | $this->checkMandatoryStringParameter($data, 'type'); |
| 27 | $this->checkParamsType($data); |
| 28 | return [ |
| 29 | 'name' => $data['name'], |
| 30 | 'type' => strtolower($data['type']), |
| 31 | 'params' => $this->sanitizeParams($data), |
| 32 | ]; |
| 33 | } |
| 34 | |
| 35 | private function checkMandatoryStringParameter(array $data, $parameterName) { |
| 36 | if (empty($data[$parameterName])) { |
| 37 | // translators: %s is the name of the missing argument. |
| 38 | throw new InvalidArgumentException(sprintf(__('Mandatory argument "%s" is missing', 'mailpoet'), $parameterName), self::ERROR_MANDATORY_ARGUMENT_MISSING); |
| 39 | } |
| 40 | if (!is_string($data[$parameterName])) { |
| 41 | // translators: %s is the name of the malformed argument. |
| 42 | throw new InvalidArgumentException(sprintf(__('Mandatory argument "%s" has to be string', 'mailpoet'), $parameterName), self::ERROR_MANDATORY_ARGUMENT_WRONG_TYPE); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private function checkParamsType($data) { |
| 47 | if (isset($data['params']) && !is_array($data['params'])) { |
| 48 | throw new InvalidArgumentException(sprintf(__('Params has to be array', 'mailpoet')), self::ERROR_PARAMS_WRONG_TYPE); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private function sanitizeParams($data) { |
| 53 | $data['params'] = isset($data['params']) ? $data['params'] : []; |
| 54 | $result = []; |
| 55 | $result['required'] = $this->getRequired($data['params']); |
| 56 | $result['label'] = $this->getLabel($data); |
| 57 | return $result + $this->getExtraParams($data); |
| 58 | } |
| 59 | |
| 60 | private function getLabel($data) { |
| 61 | if (empty($data['params']['label'])) { |
| 62 | return $data['name']; |
| 63 | } else { |
| 64 | return $data['params']['label']; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private function getRequired($params) { |
| 69 | if (isset($params['required']) && $params['required']) { |
| 70 | return '1'; |
| 71 | } |
| 72 | return ''; |
| 73 | } |
| 74 | |
| 75 | private function getExtraParams($data) { |
| 76 | $type = strtolower($data['type']); |
| 77 | if (in_array($type, [CustomFieldEntity::TYPE_TEXT, CustomFieldEntity::TYPE_TEXTAREA], true)) { |
| 78 | return $this->getExtraParamsForText($data['params']); |
| 79 | } |
| 80 | |
| 81 | if (in_array($type, [CustomFieldEntity::TYPE_RADIO, CustomFieldEntity::TYPE_SELECT], true)) { |
| 82 | return $this->getExtraParamsForSelect($data['params']); |
| 83 | } |
| 84 | |
| 85 | if ($type === CustomFieldEntity::TYPE_CHECKBOX) { |
| 86 | return $this->getExtraParamsForCheckbox($data['params']); |
| 87 | } |
| 88 | |
| 89 | if ($type === CustomFieldEntity::TYPE_DATE) { |
| 90 | return $this->getExtraParamsForDate($data['params']); |
| 91 | } |
| 92 | |
| 93 | // translators: %s is the name of the type. |
| 94 | throw new InvalidArgumentException(sprintf(__('Invalid type "%s"', 'mailpoet'), $type), self::ERROR_INVALID_TYPE); |
| 95 | } |
| 96 | |
| 97 | private function getExtraParamsForText($params) { |
| 98 | if (isset($params['validate'])) { |
| 99 | $validate = trim(strtolower($params['validate'])); |
| 100 | if ($validate === '') { |
| 101 | return []; |
| 102 | } |
| 103 | if (in_array($validate, ['number', 'alphanum', 'phone'], true)) { |
| 104 | return ['validate' => $validate]; |
| 105 | } |
| 106 | throw new InvalidArgumentException(__('Validate parameter is not valid', 'mailpoet'), self::ERROR_INVALID_VALIDATE); |
| 107 | } |
| 108 | return []; |
| 109 | } |
| 110 | |
| 111 | private function getExtraParamsForCheckbox($params) { |
| 112 | if (empty($params['values']) || count($params['values']) > 1) { |
| 113 | throw new InvalidArgumentException(__('You need to pass exactly one value for checkbox', 'mailpoet'), self::ERROR_CHECKBOX_WRONG_VALUES_COUNT); |
| 114 | } |
| 115 | $value = reset($params['values']); |
| 116 | return ['values' => [$this->sanitizeValue($value)]]; |
| 117 | } |
| 118 | |
| 119 | private function getExtraParamsForDate($params) { |
| 120 | $dateType = (isset($params['date_type']) |
| 121 | ? $params['date_type'] |
| 122 | : 'year_month_day' |
| 123 | ); |
| 124 | $inputDateFormat = (isset($params['date_format']) |
| 125 | ? $params['date_format'] |
| 126 | : '' |
| 127 | ); |
| 128 | |
| 129 | switch ($dateType) { |
| 130 | case 'year_month_day': |
| 131 | if (!in_array($inputDateFormat, ['MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY/MM/DD'], true)) { |
| 132 | throw new InvalidArgumentException(__('Invalid date_format for year_month_day', 'mailpoet'), self::ERROR_INVALID_DATE_FORMAT); |
| 133 | } |
| 134 | $dateFormat = $inputDateFormat; |
| 135 | break; |
| 136 | case 'year_month': |
| 137 | if (!in_array($inputDateFormat, ['YYYY/MM', 'MM/YYYY', 'MM/YY'], true)) { |
| 138 | throw new InvalidArgumentException(__('Invalid date_format for year_month', 'mailpoet'), self::ERROR_INVALID_DATE_FORMAT); |
| 139 | } |
| 140 | $dateFormat = $inputDateFormat; |
| 141 | break; |
| 142 | case 'month': |
| 143 | $dateFormat = 'MM'; |
| 144 | break; |
| 145 | case 'year': |
| 146 | $dateFormat = 'YYYY'; |
| 147 | break; |
| 148 | case 'day': |
| 149 | $dateFormat = 'DD'; |
| 150 | break; |
| 151 | default: |
| 152 | throw new InvalidArgumentException(__('Invalid value for date_type', 'mailpoet'), self::ERROR_INVALID_DATE_TYPE); |
| 153 | } |
| 154 | return [ |
| 155 | 'date_type' => $dateType, |
| 156 | 'date_format' => $dateFormat, |
| 157 | 'is_default_today' => !empty($params['is_default_today']) ? '1' : '', |
| 158 | ]; |
| 159 | } |
| 160 | |
| 161 | private function getExtraParamsForSelect($params) { |
| 162 | if (empty($params['values'])) { |
| 163 | throw new InvalidArgumentException(__('You need to pass some values for this type', 'mailpoet'), self::ERROR_NO_VALUES); |
| 164 | } |
| 165 | $values = []; |
| 166 | foreach ($params['values'] as $value) { |
| 167 | $values[] = $this->sanitizeValue($value); |
| 168 | } |
| 169 | return ['values' => $values]; |
| 170 | } |
| 171 | |
| 172 | private function sanitizeValue($value) { |
| 173 | if (empty($value['value'])) { |
| 174 | throw new InvalidArgumentException(__('Value cannot be empty', 'mailpoet'), self::ERROR_NO_VALUE); |
| 175 | } |
| 176 | $result = ['value' => $value['value']]; |
| 177 | if (isset($value['is_checked']) && $value['is_checked']) { |
| 178 | $result['is_checked'] = '1'; |
| 179 | } else { |
| 180 | $result['is_checked'] = ''; |
| 181 | } |
| 182 | return $result; |
| 183 | } |
| 184 | } |
| 185 |