@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | * Collect the input_fields and sanitize them to prepare them for sending to the Query |
95 | 95 | */ |
96 | 96 | $input_fields = []; |
97 | - if (! empty($this->args['where'])) { |
|
97 | + if ( ! empty($this->args['where'])) { |
|
98 | 98 | $input_fields = $this->sanitizeInputFields($this->args['where']); |
99 | 99 | |
100 | 100 | // Since we do not have any falsy values in query params |
@@ -102,10 +102,10 @@ discard block |
||
102 | 102 | $input_fields = array_filter($input_fields); |
103 | 103 | |
104 | 104 | // Use the proper operator. |
105 | - if (! empty($input_fields['CNT_ISO']) && is_array($input_fields['CNT_ISO'])) { |
|
105 | + if ( ! empty($input_fields['CNT_ISO']) && is_array($input_fields['CNT_ISO'])) { |
|
106 | 106 | $input_fields['CNT_ISO'] = ['IN', $input_fields['CNT_ISO']]; |
107 | 107 | } |
108 | - if (! empty($input_fields['CNT_ISO3']) && is_array($input_fields['CNT_ISO3'])) { |
|
108 | + if ( ! empty($input_fields['CNT_ISO3']) && is_array($input_fields['CNT_ISO3'])) { |
|
109 | 109 | $input_fields['CNT_ISO3'] = ['IN', $input_fields['CNT_ISO3']]; |
110 | 110 | } |
111 | 111 | } |
@@ -113,12 +113,12 @@ discard block |
||
113 | 113 | /** |
114 | 114 | * Merge the input_fields with the default query_args |
115 | 115 | */ |
116 | - if (! empty($input_fields)) { |
|
116 | + if ( ! empty($input_fields)) { |
|
117 | 117 | $where_params = array_merge($where_params, $input_fields); |
118 | 118 | } |
119 | 119 | |
120 | 120 | // limit to active countries by default. |
121 | - if (!isset($this->args['where']['activeOnly']) || $this->args['where']['activeOnly']) { |
|
121 | + if ( ! isset($this->args['where']['activeOnly']) || $this->args['where']['activeOnly']) { |
|
122 | 122 | $where_params['CNT_active'] = true; |
123 | 123 | } |
124 | 124 | |
@@ -133,11 +133,11 @@ discard block |
||
133 | 133 | |
134 | 134 | $search = $this->getSearchKeywords($this->args['where']); |
135 | 135 | |
136 | - if (! empty($search)) { |
|
136 | + if ( ! empty($search)) { |
|
137 | 137 | // use OR operator to search in any of the fields |
138 | 138 | $where_params['OR'] = array( |
139 | - 'CNT_name' => array('LIKE', '%' . $search . '%'), |
|
140 | - 'CNT_ISO' => array('LIKE', '%' . $search . '%'), |
|
139 | + 'CNT_name' => array('LIKE', '%'.$search.'%'), |
|
140 | + 'CNT_ISO' => array('LIKE', '%'.$search.'%'), |
|
141 | 141 | ); |
142 | 142 | } |
143 | 143 |
@@ -16,170 +16,170 @@ |
||
16 | 16 | */ |
17 | 17 | class CountryConnectionResolver extends AbstractConnectionResolver |
18 | 18 | { |
19 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
20 | - public function get_loader_name(): string |
|
21 | - { |
|
22 | - return 'espresso_country'; |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * @return EEM_Country |
|
27 | - * @throws EE_Error |
|
28 | - * @throws InvalidArgumentException |
|
29 | - * @throws InvalidDataTypeException |
|
30 | - * @throws InvalidInterfaceException |
|
31 | - * @throws ReflectionException |
|
32 | - */ |
|
33 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
34 | - public function get_query(): EEM_Country |
|
35 | - { |
|
36 | - return EEM_Country::instance(); |
|
37 | - } |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * Return an array of item IDs from the query |
|
42 | - * |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
46 | - public function get_ids(): array |
|
47 | - { |
|
48 | - $results = $this->query->get_col($this->query_args); |
|
49 | - |
|
50 | - return ! empty($results) ? $results : []; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Get_query_amount |
|
55 | - * |
|
56 | - * Returns the max between what was requested and what is defined as the $max_query_amount to |
|
57 | - * ensure that queries don't exceed unwanted limits when querying data. |
|
58 | - * |
|
59 | - * @return int |
|
60 | - * @throws Exception |
|
61 | - */ |
|
62 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
63 | - public function get_query_amount() |
|
64 | - { |
|
65 | - // Override the default limit (100) for countries |
|
66 | - return 500; |
|
67 | - } |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * Here, we map the args from the input, then we make sure that we're only querying |
|
72 | - * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
|
73 | - * handle batch resolution of the posts. |
|
74 | - * |
|
75 | - * @return array |
|
76 | - * @throws InvalidArgumentException |
|
77 | - * @throws InvalidDataTypeException |
|
78 | - * @throws InvalidInterfaceException |
|
79 | - */ |
|
80 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
81 | - public function get_query_args(): array |
|
82 | - { |
|
83 | - $where_params = []; |
|
84 | - $query_args = []; |
|
85 | - |
|
86 | - $query_args['limit'] = $this->getLimit(); |
|
87 | - |
|
88 | - // Avoid multiple entries by join. |
|
89 | - $query_args['group_by'] = 'CNT_ISO'; |
|
90 | - |
|
91 | - $query_args['default_where_conditions'] = 'minimum'; |
|
92 | - |
|
93 | - /** |
|
94 | - * Collect the input_fields and sanitize them to prepare them for sending to the Query |
|
95 | - */ |
|
96 | - $input_fields = []; |
|
97 | - if (! empty($this->args['where'])) { |
|
98 | - $input_fields = $this->sanitizeInputFields($this->args['where']); |
|
99 | - |
|
100 | - // Since we do not have any falsy values in query params |
|
101 | - // Lets get rid of empty values |
|
102 | - $input_fields = array_filter($input_fields); |
|
103 | - |
|
104 | - // Use the proper operator. |
|
105 | - if (! empty($input_fields['CNT_ISO']) && is_array($input_fields['CNT_ISO'])) { |
|
106 | - $input_fields['CNT_ISO'] = ['IN', $input_fields['CNT_ISO']]; |
|
107 | - } |
|
108 | - if (! empty($input_fields['CNT_ISO3']) && is_array($input_fields['CNT_ISO3'])) { |
|
109 | - $input_fields['CNT_ISO3'] = ['IN', $input_fields['CNT_ISO3']]; |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Merge the input_fields with the default query_args |
|
115 | - */ |
|
116 | - if (! empty($input_fields)) { |
|
117 | - $where_params = array_merge($where_params, $input_fields); |
|
118 | - } |
|
119 | - |
|
120 | - // limit to active countries by default. |
|
121 | - if (!isset($this->args['where']['activeOnly']) || $this->args['where']['activeOnly']) { |
|
122 | - $where_params['CNT_active'] = true; |
|
123 | - } |
|
124 | - |
|
125 | - [$query_args, $where_params] = $this->mapOrderbyInputArgs($query_args, $where_params, 'CNT_ISO'); |
|
126 | - |
|
127 | - if (empty($query_args['order_by'])) { |
|
128 | - // set order_by to 'name' by default |
|
129 | - $query_args['order_by'] = [ |
|
130 | - 'CNT_name' => 'ASC', |
|
131 | - ]; |
|
132 | - } |
|
133 | - |
|
134 | - $search = $this->getSearchKeywords($this->args['where']); |
|
135 | - |
|
136 | - if (! empty($search)) { |
|
137 | - // use OR operator to search in any of the fields |
|
138 | - $where_params['OR'] = array( |
|
139 | - 'CNT_name' => array('LIKE', '%' . $search . '%'), |
|
140 | - 'CNT_ISO' => array('LIKE', '%' . $search . '%'), |
|
141 | - ); |
|
142 | - } |
|
143 | - |
|
144 | - $where_params = apply_filters( |
|
145 | - 'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__country_where_params', |
|
146 | - $where_params, |
|
147 | - $this->source, |
|
148 | - $this->args |
|
149 | - ); |
|
150 | - |
|
151 | - $query_args[] = $where_params; |
|
152 | - |
|
153 | - /** |
|
154 | - * Return the $query_args |
|
155 | - */ |
|
156 | - return apply_filters( |
|
157 | - 'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__country_query_args', |
|
158 | - $query_args, |
|
159 | - $this->source, |
|
160 | - $this->args |
|
161 | - ); |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - /** |
|
166 | - * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
167 | - * friendly keys. |
|
168 | - * |
|
169 | - * @param array $where_args |
|
170 | - * @return array |
|
171 | - */ |
|
172 | - public function sanitizeInputFields(array $where_args): array |
|
173 | - { |
|
174 | - $arg_mapping = [ |
|
175 | - 'isoIn' => 'CNT_ISO', |
|
176 | - 'in' => 'CNT_ISO', |
|
177 | - 'iso3In' => 'CNT_ISO3', |
|
178 | - ]; |
|
179 | - return $this->sanitizeWhereArgsForInputFields( |
|
180 | - $where_args, |
|
181 | - $arg_mapping, |
|
182 | - ['in'] |
|
183 | - ); |
|
184 | - } |
|
19 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
20 | + public function get_loader_name(): string |
|
21 | + { |
|
22 | + return 'espresso_country'; |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * @return EEM_Country |
|
27 | + * @throws EE_Error |
|
28 | + * @throws InvalidArgumentException |
|
29 | + * @throws InvalidDataTypeException |
|
30 | + * @throws InvalidInterfaceException |
|
31 | + * @throws ReflectionException |
|
32 | + */ |
|
33 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
34 | + public function get_query(): EEM_Country |
|
35 | + { |
|
36 | + return EEM_Country::instance(); |
|
37 | + } |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * Return an array of item IDs from the query |
|
42 | + * |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
46 | + public function get_ids(): array |
|
47 | + { |
|
48 | + $results = $this->query->get_col($this->query_args); |
|
49 | + |
|
50 | + return ! empty($results) ? $results : []; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Get_query_amount |
|
55 | + * |
|
56 | + * Returns the max between what was requested and what is defined as the $max_query_amount to |
|
57 | + * ensure that queries don't exceed unwanted limits when querying data. |
|
58 | + * |
|
59 | + * @return int |
|
60 | + * @throws Exception |
|
61 | + */ |
|
62 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
63 | + public function get_query_amount() |
|
64 | + { |
|
65 | + // Override the default limit (100) for countries |
|
66 | + return 500; |
|
67 | + } |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * Here, we map the args from the input, then we make sure that we're only querying |
|
72 | + * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
|
73 | + * handle batch resolution of the posts. |
|
74 | + * |
|
75 | + * @return array |
|
76 | + * @throws InvalidArgumentException |
|
77 | + * @throws InvalidDataTypeException |
|
78 | + * @throws InvalidInterfaceException |
|
79 | + */ |
|
80 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
81 | + public function get_query_args(): array |
|
82 | + { |
|
83 | + $where_params = []; |
|
84 | + $query_args = []; |
|
85 | + |
|
86 | + $query_args['limit'] = $this->getLimit(); |
|
87 | + |
|
88 | + // Avoid multiple entries by join. |
|
89 | + $query_args['group_by'] = 'CNT_ISO'; |
|
90 | + |
|
91 | + $query_args['default_where_conditions'] = 'minimum'; |
|
92 | + |
|
93 | + /** |
|
94 | + * Collect the input_fields and sanitize them to prepare them for sending to the Query |
|
95 | + */ |
|
96 | + $input_fields = []; |
|
97 | + if (! empty($this->args['where'])) { |
|
98 | + $input_fields = $this->sanitizeInputFields($this->args['where']); |
|
99 | + |
|
100 | + // Since we do not have any falsy values in query params |
|
101 | + // Lets get rid of empty values |
|
102 | + $input_fields = array_filter($input_fields); |
|
103 | + |
|
104 | + // Use the proper operator. |
|
105 | + if (! empty($input_fields['CNT_ISO']) && is_array($input_fields['CNT_ISO'])) { |
|
106 | + $input_fields['CNT_ISO'] = ['IN', $input_fields['CNT_ISO']]; |
|
107 | + } |
|
108 | + if (! empty($input_fields['CNT_ISO3']) && is_array($input_fields['CNT_ISO3'])) { |
|
109 | + $input_fields['CNT_ISO3'] = ['IN', $input_fields['CNT_ISO3']]; |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Merge the input_fields with the default query_args |
|
115 | + */ |
|
116 | + if (! empty($input_fields)) { |
|
117 | + $where_params = array_merge($where_params, $input_fields); |
|
118 | + } |
|
119 | + |
|
120 | + // limit to active countries by default. |
|
121 | + if (!isset($this->args['where']['activeOnly']) || $this->args['where']['activeOnly']) { |
|
122 | + $where_params['CNT_active'] = true; |
|
123 | + } |
|
124 | + |
|
125 | + [$query_args, $where_params] = $this->mapOrderbyInputArgs($query_args, $where_params, 'CNT_ISO'); |
|
126 | + |
|
127 | + if (empty($query_args['order_by'])) { |
|
128 | + // set order_by to 'name' by default |
|
129 | + $query_args['order_by'] = [ |
|
130 | + 'CNT_name' => 'ASC', |
|
131 | + ]; |
|
132 | + } |
|
133 | + |
|
134 | + $search = $this->getSearchKeywords($this->args['where']); |
|
135 | + |
|
136 | + if (! empty($search)) { |
|
137 | + // use OR operator to search in any of the fields |
|
138 | + $where_params['OR'] = array( |
|
139 | + 'CNT_name' => array('LIKE', '%' . $search . '%'), |
|
140 | + 'CNT_ISO' => array('LIKE', '%' . $search . '%'), |
|
141 | + ); |
|
142 | + } |
|
143 | + |
|
144 | + $where_params = apply_filters( |
|
145 | + 'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__country_where_params', |
|
146 | + $where_params, |
|
147 | + $this->source, |
|
148 | + $this->args |
|
149 | + ); |
|
150 | + |
|
151 | + $query_args[] = $where_params; |
|
152 | + |
|
153 | + /** |
|
154 | + * Return the $query_args |
|
155 | + */ |
|
156 | + return apply_filters( |
|
157 | + 'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__country_query_args', |
|
158 | + $query_args, |
|
159 | + $this->source, |
|
160 | + $this->args |
|
161 | + ); |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + /** |
|
166 | + * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
167 | + * friendly keys. |
|
168 | + * |
|
169 | + * @param array $where_args |
|
170 | + * @return array |
|
171 | + */ |
|
172 | + public function sanitizeInputFields(array $where_args): array |
|
173 | + { |
|
174 | + $arg_mapping = [ |
|
175 | + 'isoIn' => 'CNT_ISO', |
|
176 | + 'in' => 'CNT_ISO', |
|
177 | + 'iso3In' => 'CNT_ISO3', |
|
178 | + ]; |
|
179 | + return $this->sanitizeWhereArgsForInputFields( |
|
180 | + $where_args, |
|
181 | + $arg_mapping, |
|
182 | + ['in'] |
|
183 | + ); |
|
184 | + } |
|
185 | 185 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | */ |
46 | 46 | public function __construct(EEM_Datetime $datetime_model) |
47 | 47 | { |
48 | - $this->setName($this->namespace . 'Datetime'); |
|
48 | + $this->setName($this->namespace.'Datetime'); |
|
49 | 49 | $this->setDescription(__('An event date', 'event_espresso')); |
50 | 50 | $this->setIsCustomPostType(false); |
51 | 51 | parent::__construct($datetime_model); |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | ), |
99 | 99 | new GraphQLOutputField( |
100 | 100 | 'event', |
101 | - $this->namespace . 'Event', |
|
101 | + $this->namespace.'Event', |
|
102 | 102 | null, |
103 | 103 | esc_html__('Event of the datetime.', 'event_espresso') |
104 | 104 | ), |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | ), |
207 | 207 | new GraphQLOutputField( |
208 | 208 | 'status', |
209 | - $this->namespace . 'DatetimeStatusEnum', |
|
209 | + $this->namespace.'DatetimeStatusEnum', |
|
210 | 210 | 'get_active_status', |
211 | 211 | esc_html__('Datetime status', 'event_espresso') |
212 | 212 | ), |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | null, |
227 | 227 | esc_html__('Datetime venue ID', 'event_espresso'), |
228 | 228 | null, |
229 | - function (EE_Datetime $source) { |
|
229 | + function(EE_Datetime $source) { |
|
230 | 230 | $venue_ID = $source->venue_ID(); |
231 | 231 | return $venue_ID |
232 | 232 | // Since venue is a CPT, $type will be 'post' |
@@ -320,14 +320,14 @@ discard block |
||
320 | 320 | public function registerMutations(array $inputFields) |
321 | 321 | { |
322 | 322 | register_graphql_input_type( |
323 | - 'Update' . $this->name() . 'BaseInput', |
|
323 | + 'Update'.$this->name().'BaseInput', |
|
324 | 324 | [ |
325 | 325 | 'fields' => $inputFields, |
326 | 326 | ] |
327 | 327 | ); |
328 | 328 | // Register mutation to update an entity. |
329 | 329 | register_graphql_mutation( |
330 | - 'update' . $this->name(), |
|
330 | + 'update'.$this->name(), |
|
331 | 331 | [ |
332 | 332 | 'inputFields' => $inputFields, |
333 | 333 | 'outputFields' => [ |
@@ -339,10 +339,10 @@ discard block |
||
339 | 339 | 'mutateAndGetPayload' => DatetimeUpdate::mutateAndGetPayload($this->model, $this), |
340 | 340 | ] |
341 | 341 | ); |
342 | - $base_input = 'Update' . $this->name() . 'BaseInput'; |
|
342 | + $base_input = 'Update'.$this->name().'BaseInput'; |
|
343 | 343 | // Register mutation to update an entity. |
344 | 344 | register_graphql_mutation( |
345 | - 'bulkUpdate' . $this->name(), |
|
345 | + 'bulkUpdate'.$this->name(), |
|
346 | 346 | array_merge( |
347 | 347 | Datetime::bulkUpdateBaseConfig($base_input), |
348 | 348 | [ |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | |
354 | 354 | // Register mutation to update an entity. |
355 | 355 | register_graphql_mutation( |
356 | - 'bulkDelete' . $this->namespace . 'Entities', |
|
356 | + 'bulkDelete'.$this->namespace.'Entities', |
|
357 | 357 | [ |
358 | 358 | 'inputFields' => [ |
359 | 359 | 'entityIds' => [ |
@@ -364,7 +364,7 @@ discard block |
||
364 | 364 | ], |
365 | 365 | 'entityType' => [ |
366 | 366 | 'type' => [ |
367 | - 'non_null' => $this->namespace . 'ModelNameEnum', |
|
367 | + 'non_null' => $this->namespace.'ModelNameEnum', |
|
368 | 368 | ], |
369 | 369 | 'description' => esc_html__('The entity type for the IDs', 'event_espresso'), |
370 | 370 | ], |
@@ -386,7 +386,7 @@ discard block |
||
386 | 386 | ); |
387 | 387 | // Register mutation to delete an entity. |
388 | 388 | register_graphql_mutation( |
389 | - 'delete' . $this->name(), |
|
389 | + 'delete'.$this->name(), |
|
390 | 390 | [ |
391 | 391 | 'inputFields' => [ |
392 | 392 | 'id' => $inputFields['id'], |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | lcfirst($this->name()) => [ |
400 | 400 | 'type' => $this->name(), |
401 | 401 | 'description' => esc_html__('The object before it was deleted', 'event_espresso'), |
402 | - 'resolve' => static function ($payload) { |
|
402 | + 'resolve' => static function($payload) { |
|
403 | 403 | $deleted = (object) $payload['deleted']; |
404 | 404 | |
405 | 405 | return ! empty($deleted) ? $deleted : null; |
@@ -414,7 +414,7 @@ discard block |
||
414 | 414 | unset($inputFields['id']); |
415 | 415 | // Register mutation to update an entity. |
416 | 416 | register_graphql_mutation( |
417 | - 'create' . $this->name(), |
|
417 | + 'create'.$this->name(), |
|
418 | 418 | [ |
419 | 419 | 'inputFields' => $inputFields, |
420 | 420 | 'outputFields' => [ |
@@ -429,7 +429,7 @@ discard block |
||
429 | 429 | |
430 | 430 | // Register mutation to update an entity. |
431 | 431 | register_graphql_mutation( |
432 | - 'reorder' . $this->namespace . 'Entities', |
|
432 | + 'reorder'.$this->namespace.'Entities', |
|
433 | 433 | [ |
434 | 434 | 'inputFields' => [ |
435 | 435 | 'entityIds' => [ |
@@ -440,7 +440,7 @@ discard block |
||
440 | 440 | ], |
441 | 441 | 'entityType' => [ |
442 | 442 | 'type' => [ |
443 | - 'non_null' => $this->namespace . 'ModelNameEnum', |
|
443 | + 'non_null' => $this->namespace.'ModelNameEnum', |
|
444 | 444 | ], |
445 | 445 | 'description' => esc_html__('The entity type for the IDs', 'event_espresso'), |
446 | 446 | ], |
@@ -448,7 +448,7 @@ discard block |
||
448 | 448 | 'outputFields' => [ |
449 | 449 | 'ok' => [ |
450 | 450 | 'type' => 'Boolean', |
451 | - 'resolve' => function ($payload) { |
|
451 | + 'resolve' => function($payload) { |
|
452 | 452 | return (bool) $payload['ok']; |
453 | 453 | }, |
454 | 454 | ], |
@@ -37,423 +37,423 @@ |
||
37 | 37 | */ |
38 | 38 | class Datetime extends TypeBase |
39 | 39 | { |
40 | - /** |
|
41 | - * EventDate constructor. |
|
42 | - * |
|
43 | - * @param EEM_Datetime $datetime_model |
|
44 | - */ |
|
45 | - public function __construct(EEM_Datetime $datetime_model) |
|
46 | - { |
|
47 | - $this->setName($this->namespace . 'Datetime'); |
|
48 | - $this->setDescription(__('An event date', 'event_espresso')); |
|
49 | - $this->setIsCustomPostType(false); |
|
50 | - parent::__construct($datetime_model); |
|
51 | - } |
|
40 | + /** |
|
41 | + * EventDate constructor. |
|
42 | + * |
|
43 | + * @param EEM_Datetime $datetime_model |
|
44 | + */ |
|
45 | + public function __construct(EEM_Datetime $datetime_model) |
|
46 | + { |
|
47 | + $this->setName($this->namespace . 'Datetime'); |
|
48 | + $this->setDescription(__('An event date', 'event_espresso')); |
|
49 | + $this->setIsCustomPostType(false); |
|
50 | + parent::__construct($datetime_model); |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * @return GraphQLFieldInterface[] |
|
56 | - */ |
|
57 | - public function getFields(): array |
|
58 | - { |
|
59 | - $fields = [ |
|
60 | - new GraphQLField( |
|
61 | - 'id', |
|
62 | - ['non_null' => 'ID'], |
|
63 | - null, |
|
64 | - esc_html__('The globally unique ID for the object.', 'event_espresso') |
|
65 | - ), |
|
66 | - new GraphQLOutputField( |
|
67 | - 'dbId', |
|
68 | - ['non_null' => 'Int'], |
|
69 | - 'ID', |
|
70 | - esc_html__('The datetime ID.', 'event_espresso') |
|
71 | - ), |
|
72 | - new GraphQLOutputField( |
|
73 | - 'cacheId', |
|
74 | - ['non_null' => 'String'], |
|
75 | - null, |
|
76 | - esc_html__('The cache ID of the object.', 'event_espresso') |
|
77 | - ), |
|
78 | - new GraphQLField( |
|
79 | - 'capacity', |
|
80 | - 'Int', |
|
81 | - 'reg_limit', |
|
82 | - esc_html__('Registration Limit for this time', 'event_espresso'), |
|
83 | - [$this, 'parseInfiniteValue'] |
|
84 | - ), |
|
85 | - new GraphQLField( |
|
86 | - 'description', |
|
87 | - 'String', |
|
88 | - 'description', |
|
89 | - esc_html__('Description for Datetime', 'event_espresso') |
|
90 | - ), |
|
91 | - new GraphQLField( |
|
92 | - 'endDate', |
|
93 | - 'String', |
|
94 | - 'end_date_and_time', |
|
95 | - esc_html__('End date and time of the Event', 'event_espresso'), |
|
96 | - [$this, 'formatDatetime'] |
|
97 | - ), |
|
98 | - new GraphQLOutputField( |
|
99 | - 'event', |
|
100 | - $this->namespace . 'Event', |
|
101 | - null, |
|
102 | - esc_html__('Event of the datetime.', 'event_espresso') |
|
103 | - ), |
|
104 | - new GraphQLInputField( |
|
105 | - 'event', |
|
106 | - 'ID', |
|
107 | - null, |
|
108 | - esc_html__('Globally unique event ID of the datetime.', 'event_espresso') |
|
109 | - ), |
|
110 | - new GraphQLInputField( |
|
111 | - 'eventId', |
|
112 | - 'Int', |
|
113 | - null, |
|
114 | - esc_html__('Event ID of the datetime.', 'event_espresso') |
|
115 | - ), |
|
116 | - new GraphQLOutputField( |
|
117 | - 'isActive', |
|
118 | - 'Boolean', |
|
119 | - 'is_active', |
|
120 | - esc_html__('Flag indicating datetime is active', 'event_espresso') |
|
121 | - ), |
|
122 | - new GraphQLOutputField( |
|
123 | - 'isExpired', |
|
124 | - 'Boolean', |
|
125 | - 'is_expired', |
|
126 | - esc_html__('Flag indicating datetime is expired or not', 'event_espresso') |
|
127 | - ), |
|
128 | - new GraphQLField( |
|
129 | - 'isPrimary', |
|
130 | - 'Boolean', |
|
131 | - 'is_primary', |
|
132 | - esc_html__('Flag indicating datetime is primary one for event', 'event_espresso') |
|
133 | - ), |
|
134 | - new GraphQLOutputField( |
|
135 | - 'isSoldOut', |
|
136 | - 'Boolean', |
|
137 | - 'sold_out', |
|
138 | - esc_html__( |
|
139 | - 'Flag indicating whether the tickets sold for this datetime, met or exceed the registration limit', |
|
140 | - 'event_espresso' |
|
141 | - ) |
|
142 | - ), |
|
143 | - new GraphQLField( |
|
144 | - 'isTrashed', |
|
145 | - 'Boolean', |
|
146 | - null, |
|
147 | - esc_html__('Flag indicating datetime has been trashed.', 'event_espresso'), |
|
148 | - null, |
|
149 | - [$this, 'getIsTrashed'] |
|
150 | - ), |
|
151 | - new GraphQLOutputField( |
|
152 | - 'isUpcoming', |
|
153 | - 'Boolean', |
|
154 | - 'is_upcoming', |
|
155 | - esc_html__('Whether the date is upcoming', 'event_espresso') |
|
156 | - ), |
|
157 | - new GraphQLOutputField( |
|
158 | - 'length', |
|
159 | - 'Int', |
|
160 | - 'length', |
|
161 | - esc_html__('The length of the event (start to end time) in seconds', 'event_espresso') |
|
162 | - ), |
|
163 | - new GraphQLField( |
|
164 | - 'name', |
|
165 | - 'String', |
|
166 | - 'name', |
|
167 | - esc_html__('Datetime Name', 'event_espresso') |
|
168 | - ), |
|
169 | - new GraphQLField( |
|
170 | - 'order', |
|
171 | - 'Int', |
|
172 | - 'order', |
|
173 | - esc_html__('The order in which the Datetime is displayed', 'event_espresso') |
|
174 | - ), |
|
175 | - new GraphQLOutputField( |
|
176 | - 'parent', |
|
177 | - $this->name(), |
|
178 | - null, |
|
179 | - esc_html__('The parent datetime of the current datetime', 'event_espresso') |
|
180 | - ), |
|
181 | - new GraphQLInputField( |
|
182 | - 'parent', |
|
183 | - 'ID', |
|
184 | - null, |
|
185 | - esc_html__('The parent datetime ID', 'event_espresso') |
|
186 | - ), |
|
187 | - new GraphQLField( |
|
188 | - 'reserved', |
|
189 | - 'Int', |
|
190 | - 'reserved', |
|
191 | - esc_html__('Quantity of tickets reserved, but not yet fully purchased', 'event_espresso') |
|
192 | - ), |
|
193 | - new GraphQLField( |
|
194 | - 'startDate', |
|
195 | - 'String', |
|
196 | - 'start_date_and_time', |
|
197 | - esc_html__('Start date and time of the Event', 'event_espresso'), |
|
198 | - [$this, 'formatDatetime'] |
|
199 | - ), |
|
200 | - new GraphQLField( |
|
201 | - 'sold', |
|
202 | - 'Int', |
|
203 | - 'sold', |
|
204 | - esc_html__('How many sales for this Datetime that have occurred', 'event_espresso') |
|
205 | - ), |
|
206 | - new GraphQLOutputField( |
|
207 | - 'status', |
|
208 | - $this->namespace . 'DatetimeStatusEnum', |
|
209 | - 'get_active_status', |
|
210 | - esc_html__('Datetime status', 'event_espresso') |
|
211 | - ), |
|
212 | - new GraphQLInputField( |
|
213 | - 'tickets', |
|
214 | - ['list_of' => 'ID'], |
|
215 | - null, |
|
216 | - sprintf( |
|
217 | - '%1$s %2$s', |
|
218 | - esc_html__('Globally unique IDs of the tickets related to the datetime.', 'event_espresso'), |
|
219 | - esc_html__('Ignored if empty.', 'event_espresso') |
|
220 | - ) |
|
221 | - ), |
|
222 | - new GraphQLField( |
|
223 | - 'venue', |
|
224 | - 'String', |
|
225 | - null, |
|
226 | - esc_html__('Datetime venue ID', 'event_espresso'), |
|
227 | - null, |
|
228 | - function (EE_Datetime $source) { |
|
229 | - $venue_ID = $source->venue_ID(); |
|
230 | - return $venue_ID |
|
231 | - // Since venue is a CPT, $type will be 'post' |
|
232 | - ? Relay::toGlobalId('post', $venue_ID) |
|
233 | - : null; |
|
234 | - } |
|
235 | - ), |
|
236 | - ]; |
|
54 | + /** |
|
55 | + * @return GraphQLFieldInterface[] |
|
56 | + */ |
|
57 | + public function getFields(): array |
|
58 | + { |
|
59 | + $fields = [ |
|
60 | + new GraphQLField( |
|
61 | + 'id', |
|
62 | + ['non_null' => 'ID'], |
|
63 | + null, |
|
64 | + esc_html__('The globally unique ID for the object.', 'event_espresso') |
|
65 | + ), |
|
66 | + new GraphQLOutputField( |
|
67 | + 'dbId', |
|
68 | + ['non_null' => 'Int'], |
|
69 | + 'ID', |
|
70 | + esc_html__('The datetime ID.', 'event_espresso') |
|
71 | + ), |
|
72 | + new GraphQLOutputField( |
|
73 | + 'cacheId', |
|
74 | + ['non_null' => 'String'], |
|
75 | + null, |
|
76 | + esc_html__('The cache ID of the object.', 'event_espresso') |
|
77 | + ), |
|
78 | + new GraphQLField( |
|
79 | + 'capacity', |
|
80 | + 'Int', |
|
81 | + 'reg_limit', |
|
82 | + esc_html__('Registration Limit for this time', 'event_espresso'), |
|
83 | + [$this, 'parseInfiniteValue'] |
|
84 | + ), |
|
85 | + new GraphQLField( |
|
86 | + 'description', |
|
87 | + 'String', |
|
88 | + 'description', |
|
89 | + esc_html__('Description for Datetime', 'event_espresso') |
|
90 | + ), |
|
91 | + new GraphQLField( |
|
92 | + 'endDate', |
|
93 | + 'String', |
|
94 | + 'end_date_and_time', |
|
95 | + esc_html__('End date and time of the Event', 'event_espresso'), |
|
96 | + [$this, 'formatDatetime'] |
|
97 | + ), |
|
98 | + new GraphQLOutputField( |
|
99 | + 'event', |
|
100 | + $this->namespace . 'Event', |
|
101 | + null, |
|
102 | + esc_html__('Event of the datetime.', 'event_espresso') |
|
103 | + ), |
|
104 | + new GraphQLInputField( |
|
105 | + 'event', |
|
106 | + 'ID', |
|
107 | + null, |
|
108 | + esc_html__('Globally unique event ID of the datetime.', 'event_espresso') |
|
109 | + ), |
|
110 | + new GraphQLInputField( |
|
111 | + 'eventId', |
|
112 | + 'Int', |
|
113 | + null, |
|
114 | + esc_html__('Event ID of the datetime.', 'event_espresso') |
|
115 | + ), |
|
116 | + new GraphQLOutputField( |
|
117 | + 'isActive', |
|
118 | + 'Boolean', |
|
119 | + 'is_active', |
|
120 | + esc_html__('Flag indicating datetime is active', 'event_espresso') |
|
121 | + ), |
|
122 | + new GraphQLOutputField( |
|
123 | + 'isExpired', |
|
124 | + 'Boolean', |
|
125 | + 'is_expired', |
|
126 | + esc_html__('Flag indicating datetime is expired or not', 'event_espresso') |
|
127 | + ), |
|
128 | + new GraphQLField( |
|
129 | + 'isPrimary', |
|
130 | + 'Boolean', |
|
131 | + 'is_primary', |
|
132 | + esc_html__('Flag indicating datetime is primary one for event', 'event_espresso') |
|
133 | + ), |
|
134 | + new GraphQLOutputField( |
|
135 | + 'isSoldOut', |
|
136 | + 'Boolean', |
|
137 | + 'sold_out', |
|
138 | + esc_html__( |
|
139 | + 'Flag indicating whether the tickets sold for this datetime, met or exceed the registration limit', |
|
140 | + 'event_espresso' |
|
141 | + ) |
|
142 | + ), |
|
143 | + new GraphQLField( |
|
144 | + 'isTrashed', |
|
145 | + 'Boolean', |
|
146 | + null, |
|
147 | + esc_html__('Flag indicating datetime has been trashed.', 'event_espresso'), |
|
148 | + null, |
|
149 | + [$this, 'getIsTrashed'] |
|
150 | + ), |
|
151 | + new GraphQLOutputField( |
|
152 | + 'isUpcoming', |
|
153 | + 'Boolean', |
|
154 | + 'is_upcoming', |
|
155 | + esc_html__('Whether the date is upcoming', 'event_espresso') |
|
156 | + ), |
|
157 | + new GraphQLOutputField( |
|
158 | + 'length', |
|
159 | + 'Int', |
|
160 | + 'length', |
|
161 | + esc_html__('The length of the event (start to end time) in seconds', 'event_espresso') |
|
162 | + ), |
|
163 | + new GraphQLField( |
|
164 | + 'name', |
|
165 | + 'String', |
|
166 | + 'name', |
|
167 | + esc_html__('Datetime Name', 'event_espresso') |
|
168 | + ), |
|
169 | + new GraphQLField( |
|
170 | + 'order', |
|
171 | + 'Int', |
|
172 | + 'order', |
|
173 | + esc_html__('The order in which the Datetime is displayed', 'event_espresso') |
|
174 | + ), |
|
175 | + new GraphQLOutputField( |
|
176 | + 'parent', |
|
177 | + $this->name(), |
|
178 | + null, |
|
179 | + esc_html__('The parent datetime of the current datetime', 'event_espresso') |
|
180 | + ), |
|
181 | + new GraphQLInputField( |
|
182 | + 'parent', |
|
183 | + 'ID', |
|
184 | + null, |
|
185 | + esc_html__('The parent datetime ID', 'event_espresso') |
|
186 | + ), |
|
187 | + new GraphQLField( |
|
188 | + 'reserved', |
|
189 | + 'Int', |
|
190 | + 'reserved', |
|
191 | + esc_html__('Quantity of tickets reserved, but not yet fully purchased', 'event_espresso') |
|
192 | + ), |
|
193 | + new GraphQLField( |
|
194 | + 'startDate', |
|
195 | + 'String', |
|
196 | + 'start_date_and_time', |
|
197 | + esc_html__('Start date and time of the Event', 'event_espresso'), |
|
198 | + [$this, 'formatDatetime'] |
|
199 | + ), |
|
200 | + new GraphQLField( |
|
201 | + 'sold', |
|
202 | + 'Int', |
|
203 | + 'sold', |
|
204 | + esc_html__('How many sales for this Datetime that have occurred', 'event_espresso') |
|
205 | + ), |
|
206 | + new GraphQLOutputField( |
|
207 | + 'status', |
|
208 | + $this->namespace . 'DatetimeStatusEnum', |
|
209 | + 'get_active_status', |
|
210 | + esc_html__('Datetime status', 'event_espresso') |
|
211 | + ), |
|
212 | + new GraphQLInputField( |
|
213 | + 'tickets', |
|
214 | + ['list_of' => 'ID'], |
|
215 | + null, |
|
216 | + sprintf( |
|
217 | + '%1$s %2$s', |
|
218 | + esc_html__('Globally unique IDs of the tickets related to the datetime.', 'event_espresso'), |
|
219 | + esc_html__('Ignored if empty.', 'event_espresso') |
|
220 | + ) |
|
221 | + ), |
|
222 | + new GraphQLField( |
|
223 | + 'venue', |
|
224 | + 'String', |
|
225 | + null, |
|
226 | + esc_html__('Datetime venue ID', 'event_espresso'), |
|
227 | + null, |
|
228 | + function (EE_Datetime $source) { |
|
229 | + $venue_ID = $source->venue_ID(); |
|
230 | + return $venue_ID |
|
231 | + // Since venue is a CPT, $type will be 'post' |
|
232 | + ? Relay::toGlobalId('post', $venue_ID) |
|
233 | + : null; |
|
234 | + } |
|
235 | + ), |
|
236 | + ]; |
|
237 | 237 | |
238 | - return apply_filters( |
|
239 | - 'FHEE__EventEspresso_core_domain_services_graphql_types__datetime_fields', |
|
240 | - $fields, |
|
241 | - $this->name, |
|
242 | - $this->model |
|
243 | - ); |
|
244 | - } |
|
238 | + return apply_filters( |
|
239 | + 'FHEE__EventEspresso_core_domain_services_graphql_types__datetime_fields', |
|
240 | + $fields, |
|
241 | + $this->name, |
|
242 | + $this->model |
|
243 | + ); |
|
244 | + } |
|
245 | 245 | |
246 | 246 | |
247 | - /** |
|
248 | - * @param EE_Datetime $source The source that's passed down the GraphQL queries |
|
249 | - * @param array $args The inputArgs on the field |
|
250 | - * @param AppContext $context The AppContext passed down the GraphQL tree |
|
251 | - * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
|
252 | - * @return bool |
|
253 | - * @throws Exception |
|
254 | - * @throws InvalidArgumentException |
|
255 | - * @throws InvalidDataTypeException |
|
256 | - * @throws InvalidInterfaceException |
|
257 | - * @throws ReflectionException |
|
258 | - * @throws UserError |
|
259 | - * @throws UnexpectedEntityException |
|
260 | - */ |
|
261 | - public function getIsTrashed(EE_Datetime $source, array $args, AppContext $context, ResolveInfo $info): bool |
|
262 | - { |
|
263 | - return (bool) $source->get('DTT_deleted'); |
|
264 | - } |
|
247 | + /** |
|
248 | + * @param EE_Datetime $source The source that's passed down the GraphQL queries |
|
249 | + * @param array $args The inputArgs on the field |
|
250 | + * @param AppContext $context The AppContext passed down the GraphQL tree |
|
251 | + * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
|
252 | + * @return bool |
|
253 | + * @throws Exception |
|
254 | + * @throws InvalidArgumentException |
|
255 | + * @throws InvalidDataTypeException |
|
256 | + * @throws InvalidInterfaceException |
|
257 | + * @throws ReflectionException |
|
258 | + * @throws UserError |
|
259 | + * @throws UnexpectedEntityException |
|
260 | + */ |
|
261 | + public function getIsTrashed(EE_Datetime $source, array $args, AppContext $context, ResolveInfo $info): bool |
|
262 | + { |
|
263 | + return (bool) $source->get('DTT_deleted'); |
|
264 | + } |
|
265 | 265 | |
266 | - /** |
|
267 | - * Return the base mutation config for bulk update. |
|
268 | - * |
|
269 | - * @param string $base_input |
|
270 | - * @return array |
|
271 | - */ |
|
272 | - public static function bulkUpdateBaseConfig(string $base_input): array |
|
273 | - { |
|
274 | - return [ |
|
275 | - 'inputFields' => [ |
|
276 | - /** |
|
277 | - * represents the input that is unique for each entity |
|
278 | - * e.g. dates may be unique for datetimes and tickets |
|
279 | - */ |
|
280 | - 'uniqueInputs' => [ |
|
281 | - 'type' => [ |
|
282 | - 'non_null' => ['list_of' => $base_input], |
|
283 | - ], |
|
284 | - 'description' => esc_html__( |
|
285 | - 'List of unique inputs for each entity in bulk update', |
|
286 | - 'event_espresso' |
|
287 | - ), |
|
288 | - ], |
|
289 | - /** |
|
290 | - * represents the common input for all entities |
|
291 | - * e.g. capacity or quantity may be same for all dates/tickets |
|
292 | - */ |
|
293 | - 'sharedInput' => [ |
|
294 | - 'type' => $base_input, |
|
295 | - 'description' => esc_html__( |
|
296 | - 'Shared input for all entities in bulk update', |
|
297 | - 'event_espresso' |
|
298 | - ), |
|
299 | - ], |
|
300 | - ], |
|
301 | - 'outputFields' => [ |
|
302 | - 'updated' => [ |
|
303 | - 'type' => ['list_of' => 'ID'], |
|
304 | - ], |
|
305 | - 'failed' => [ |
|
306 | - 'type' => ['list_of' => 'ID'], |
|
307 | - ], |
|
308 | - ], |
|
309 | - ]; |
|
310 | - } |
|
266 | + /** |
|
267 | + * Return the base mutation config for bulk update. |
|
268 | + * |
|
269 | + * @param string $base_input |
|
270 | + * @return array |
|
271 | + */ |
|
272 | + public static function bulkUpdateBaseConfig(string $base_input): array |
|
273 | + { |
|
274 | + return [ |
|
275 | + 'inputFields' => [ |
|
276 | + /** |
|
277 | + * represents the input that is unique for each entity |
|
278 | + * e.g. dates may be unique for datetimes and tickets |
|
279 | + */ |
|
280 | + 'uniqueInputs' => [ |
|
281 | + 'type' => [ |
|
282 | + 'non_null' => ['list_of' => $base_input], |
|
283 | + ], |
|
284 | + 'description' => esc_html__( |
|
285 | + 'List of unique inputs for each entity in bulk update', |
|
286 | + 'event_espresso' |
|
287 | + ), |
|
288 | + ], |
|
289 | + /** |
|
290 | + * represents the common input for all entities |
|
291 | + * e.g. capacity or quantity may be same for all dates/tickets |
|
292 | + */ |
|
293 | + 'sharedInput' => [ |
|
294 | + 'type' => $base_input, |
|
295 | + 'description' => esc_html__( |
|
296 | + 'Shared input for all entities in bulk update', |
|
297 | + 'event_espresso' |
|
298 | + ), |
|
299 | + ], |
|
300 | + ], |
|
301 | + 'outputFields' => [ |
|
302 | + 'updated' => [ |
|
303 | + 'type' => ['list_of' => 'ID'], |
|
304 | + ], |
|
305 | + 'failed' => [ |
|
306 | + 'type' => ['list_of' => 'ID'], |
|
307 | + ], |
|
308 | + ], |
|
309 | + ]; |
|
310 | + } |
|
311 | 311 | |
312 | 312 | |
313 | - /** |
|
314 | - * @param array $inputFields The mutation input fields. |
|
315 | - * @throws InvalidArgumentException |
|
316 | - * @throws ReflectionException |
|
317 | - * @throws Exception |
|
318 | - */ |
|
319 | - public function registerMutations(array $inputFields) |
|
320 | - { |
|
321 | - register_graphql_input_type( |
|
322 | - 'Update' . $this->name() . 'BaseInput', |
|
323 | - [ |
|
324 | - 'fields' => $inputFields, |
|
325 | - ] |
|
326 | - ); |
|
327 | - // Register mutation to update an entity. |
|
328 | - register_graphql_mutation( |
|
329 | - 'update' . $this->name(), |
|
330 | - [ |
|
331 | - 'inputFields' => $inputFields, |
|
332 | - 'outputFields' => [ |
|
333 | - lcfirst($this->name()) => [ |
|
334 | - 'type' => $this->name(), |
|
335 | - 'resolve' => [$this, 'resolveFromPayload'], |
|
336 | - ], |
|
337 | - ], |
|
338 | - 'mutateAndGetPayload' => DatetimeUpdate::mutateAndGetPayload($this->model, $this), |
|
339 | - ] |
|
340 | - ); |
|
341 | - $base_input = 'Update' . $this->name() . 'BaseInput'; |
|
342 | - // Register mutation to update an entity. |
|
343 | - register_graphql_mutation( |
|
344 | - 'bulkUpdate' . $this->name(), |
|
345 | - array_merge( |
|
346 | - Datetime::bulkUpdateBaseConfig($base_input), |
|
347 | - [ |
|
348 | - 'mutateAndGetPayload' => DatetimeBulkUpdate::mutateAndGetPayload($this->model, $this), |
|
349 | - ] |
|
350 | - ) |
|
351 | - ); |
|
313 | + /** |
|
314 | + * @param array $inputFields The mutation input fields. |
|
315 | + * @throws InvalidArgumentException |
|
316 | + * @throws ReflectionException |
|
317 | + * @throws Exception |
|
318 | + */ |
|
319 | + public function registerMutations(array $inputFields) |
|
320 | + { |
|
321 | + register_graphql_input_type( |
|
322 | + 'Update' . $this->name() . 'BaseInput', |
|
323 | + [ |
|
324 | + 'fields' => $inputFields, |
|
325 | + ] |
|
326 | + ); |
|
327 | + // Register mutation to update an entity. |
|
328 | + register_graphql_mutation( |
|
329 | + 'update' . $this->name(), |
|
330 | + [ |
|
331 | + 'inputFields' => $inputFields, |
|
332 | + 'outputFields' => [ |
|
333 | + lcfirst($this->name()) => [ |
|
334 | + 'type' => $this->name(), |
|
335 | + 'resolve' => [$this, 'resolveFromPayload'], |
|
336 | + ], |
|
337 | + ], |
|
338 | + 'mutateAndGetPayload' => DatetimeUpdate::mutateAndGetPayload($this->model, $this), |
|
339 | + ] |
|
340 | + ); |
|
341 | + $base_input = 'Update' . $this->name() . 'BaseInput'; |
|
342 | + // Register mutation to update an entity. |
|
343 | + register_graphql_mutation( |
|
344 | + 'bulkUpdate' . $this->name(), |
|
345 | + array_merge( |
|
346 | + Datetime::bulkUpdateBaseConfig($base_input), |
|
347 | + [ |
|
348 | + 'mutateAndGetPayload' => DatetimeBulkUpdate::mutateAndGetPayload($this->model, $this), |
|
349 | + ] |
|
350 | + ) |
|
351 | + ); |
|
352 | 352 | |
353 | - // Register mutation to update an entity. |
|
354 | - register_graphql_mutation( |
|
355 | - 'bulkDelete' . $this->namespace . 'Entities', |
|
356 | - [ |
|
357 | - 'inputFields' => [ |
|
358 | - 'entityIds' => [ |
|
359 | - 'type' => [ |
|
360 | - 'non_null' => ['list_of' => 'ID'], |
|
361 | - ], |
|
362 | - 'description' => esc_html__('The list of GUIDs of the entities to be deleted.', 'event_espresso'), |
|
363 | - ], |
|
364 | - 'entityType' => [ |
|
365 | - 'type' => [ |
|
366 | - 'non_null' => $this->namespace . 'ModelNameEnum', |
|
367 | - ], |
|
368 | - 'description' => esc_html__('The entity type for the IDs', 'event_espresso'), |
|
369 | - ], |
|
370 | - 'deletePermanently' => [ |
|
371 | - 'type' => 'Boolean', |
|
372 | - 'description' => esc_html__('Whether to delete the entities permanently.', 'event_espresso'), |
|
373 | - ], |
|
374 | - ], |
|
375 | - 'outputFields' => [ |
|
376 | - 'deleted' => [ |
|
377 | - 'type' => ['list_of' => 'ID'], |
|
378 | - ], |
|
379 | - 'failed' => [ |
|
380 | - 'type' => ['list_of' => 'ID'], |
|
381 | - ], |
|
382 | - ], |
|
383 | - 'mutateAndGetPayload' => BulkEntityDelete::mutateAndGetPayload(), |
|
384 | - ] |
|
385 | - ); |
|
386 | - // Register mutation to delete an entity. |
|
387 | - register_graphql_mutation( |
|
388 | - 'delete' . $this->name(), |
|
389 | - [ |
|
390 | - 'inputFields' => [ |
|
391 | - 'id' => $inputFields['id'], |
|
392 | - 'deletePermanently' => [ |
|
393 | - 'type' => 'Boolean', |
|
394 | - 'description' => esc_html__('Whether to delete the entity permanently.', 'event_espresso'), |
|
395 | - ], |
|
396 | - ], |
|
397 | - 'outputFields' => [ |
|
398 | - lcfirst($this->name()) => [ |
|
399 | - 'type' => $this->name(), |
|
400 | - 'description' => esc_html__('The object before it was deleted', 'event_espresso'), |
|
401 | - 'resolve' => static function ($payload) { |
|
402 | - $deleted = (object) $payload['deleted']; |
|
353 | + // Register mutation to update an entity. |
|
354 | + register_graphql_mutation( |
|
355 | + 'bulkDelete' . $this->namespace . 'Entities', |
|
356 | + [ |
|
357 | + 'inputFields' => [ |
|
358 | + 'entityIds' => [ |
|
359 | + 'type' => [ |
|
360 | + 'non_null' => ['list_of' => 'ID'], |
|
361 | + ], |
|
362 | + 'description' => esc_html__('The list of GUIDs of the entities to be deleted.', 'event_espresso'), |
|
363 | + ], |
|
364 | + 'entityType' => [ |
|
365 | + 'type' => [ |
|
366 | + 'non_null' => $this->namespace . 'ModelNameEnum', |
|
367 | + ], |
|
368 | + 'description' => esc_html__('The entity type for the IDs', 'event_espresso'), |
|
369 | + ], |
|
370 | + 'deletePermanently' => [ |
|
371 | + 'type' => 'Boolean', |
|
372 | + 'description' => esc_html__('Whether to delete the entities permanently.', 'event_espresso'), |
|
373 | + ], |
|
374 | + ], |
|
375 | + 'outputFields' => [ |
|
376 | + 'deleted' => [ |
|
377 | + 'type' => ['list_of' => 'ID'], |
|
378 | + ], |
|
379 | + 'failed' => [ |
|
380 | + 'type' => ['list_of' => 'ID'], |
|
381 | + ], |
|
382 | + ], |
|
383 | + 'mutateAndGetPayload' => BulkEntityDelete::mutateAndGetPayload(), |
|
384 | + ] |
|
385 | + ); |
|
386 | + // Register mutation to delete an entity. |
|
387 | + register_graphql_mutation( |
|
388 | + 'delete' . $this->name(), |
|
389 | + [ |
|
390 | + 'inputFields' => [ |
|
391 | + 'id' => $inputFields['id'], |
|
392 | + 'deletePermanently' => [ |
|
393 | + 'type' => 'Boolean', |
|
394 | + 'description' => esc_html__('Whether to delete the entity permanently.', 'event_espresso'), |
|
395 | + ], |
|
396 | + ], |
|
397 | + 'outputFields' => [ |
|
398 | + lcfirst($this->name()) => [ |
|
399 | + 'type' => $this->name(), |
|
400 | + 'description' => esc_html__('The object before it was deleted', 'event_espresso'), |
|
401 | + 'resolve' => static function ($payload) { |
|
402 | + $deleted = (object) $payload['deleted']; |
|
403 | 403 | |
404 | - return ! empty($deleted) ? $deleted : null; |
|
405 | - }, |
|
406 | - ], |
|
407 | - ], |
|
408 | - 'mutateAndGetPayload' => DatetimeDelete::mutateAndGetPayload($this->model, $this), |
|
409 | - ] |
|
410 | - ); |
|
404 | + return ! empty($deleted) ? $deleted : null; |
|
405 | + }, |
|
406 | + ], |
|
407 | + ], |
|
408 | + 'mutateAndGetPayload' => DatetimeDelete::mutateAndGetPayload($this->model, $this), |
|
409 | + ] |
|
410 | + ); |
|
411 | 411 | |
412 | - // remove primary key from input. |
|
413 | - unset($inputFields['id']); |
|
414 | - // Register mutation to update an entity. |
|
415 | - register_graphql_mutation( |
|
416 | - 'create' . $this->name(), |
|
417 | - [ |
|
418 | - 'inputFields' => $inputFields, |
|
419 | - 'outputFields' => [ |
|
420 | - lcfirst($this->name()) => [ |
|
421 | - 'type' => $this->name(), |
|
422 | - 'resolve' => [$this, 'resolveFromPayload'], |
|
423 | - ], |
|
424 | - ], |
|
425 | - 'mutateAndGetPayload' => DatetimeCreate::mutateAndGetPayload($this->model, $this), |
|
426 | - ] |
|
427 | - ); |
|
412 | + // remove primary key from input. |
|
413 | + unset($inputFields['id']); |
|
414 | + // Register mutation to update an entity. |
|
415 | + register_graphql_mutation( |
|
416 | + 'create' . $this->name(), |
|
417 | + [ |
|
418 | + 'inputFields' => $inputFields, |
|
419 | + 'outputFields' => [ |
|
420 | + lcfirst($this->name()) => [ |
|
421 | + 'type' => $this->name(), |
|
422 | + 'resolve' => [$this, 'resolveFromPayload'], |
|
423 | + ], |
|
424 | + ], |
|
425 | + 'mutateAndGetPayload' => DatetimeCreate::mutateAndGetPayload($this->model, $this), |
|
426 | + ] |
|
427 | + ); |
|
428 | 428 | |
429 | - // Register mutation to update an entity. |
|
430 | - register_graphql_mutation( |
|
431 | - 'reorder' . $this->namespace . 'Entities', |
|
432 | - [ |
|
433 | - 'inputFields' => [ |
|
434 | - 'entityIds' => [ |
|
435 | - 'type' => [ |
|
436 | - 'non_null' => ['list_of' => 'ID'], |
|
437 | - ], |
|
438 | - 'description' => esc_html__('The reordered list of entity GUIDs.', 'event_espresso'), |
|
439 | - ], |
|
440 | - 'entityType' => [ |
|
441 | - 'type' => [ |
|
442 | - 'non_null' => $this->namespace . 'ModelNameEnum', |
|
443 | - ], |
|
444 | - 'description' => esc_html__('The entity type for the IDs', 'event_espresso'), |
|
445 | - ], |
|
446 | - ], |
|
447 | - 'outputFields' => [ |
|
448 | - 'ok' => [ |
|
449 | - 'type' => 'Boolean', |
|
450 | - 'resolve' => function ($payload) { |
|
451 | - return (bool) $payload['ok']; |
|
452 | - }, |
|
453 | - ], |
|
454 | - ], |
|
455 | - 'mutateAndGetPayload' => EntityReorder::mutateAndGetPayload(), |
|
456 | - ] |
|
457 | - ); |
|
458 | - } |
|
429 | + // Register mutation to update an entity. |
|
430 | + register_graphql_mutation( |
|
431 | + 'reorder' . $this->namespace . 'Entities', |
|
432 | + [ |
|
433 | + 'inputFields' => [ |
|
434 | + 'entityIds' => [ |
|
435 | + 'type' => [ |
|
436 | + 'non_null' => ['list_of' => 'ID'], |
|
437 | + ], |
|
438 | + 'description' => esc_html__('The reordered list of entity GUIDs.', 'event_espresso'), |
|
439 | + ], |
|
440 | + 'entityType' => [ |
|
441 | + 'type' => [ |
|
442 | + 'non_null' => $this->namespace . 'ModelNameEnum', |
|
443 | + ], |
|
444 | + 'description' => esc_html__('The entity type for the IDs', 'event_espresso'), |
|
445 | + ], |
|
446 | + ], |
|
447 | + 'outputFields' => [ |
|
448 | + 'ok' => [ |
|
449 | + 'type' => 'Boolean', |
|
450 | + 'resolve' => function ($payload) { |
|
451 | + return (bool) $payload['ok']; |
|
452 | + }, |
|
453 | + ], |
|
454 | + ], |
|
455 | + 'mutateAndGetPayload' => EntityReorder::mutateAndGetPayload(), |
|
456 | + ] |
|
457 | + ); |
|
458 | + } |
|
459 | 459 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | } |
61 | 61 | |
62 | 62 | // make sure we don't register twice |
63 | - if (isset(self::$_ee_message_type_registry[ $addon_name ])) { |
|
63 | + if (isset(self::$_ee_message_type_registry[$addon_name])) { |
|
64 | 64 | return true; |
65 | 65 | } |
66 | 66 | |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | ); |
83 | 83 | } |
84 | 84 | // setup $__ee_message_type_registry array from incoming values. |
85 | - self::$_ee_message_type_registry[ $addon_name ] = [ |
|
85 | + self::$_ee_message_type_registry[$addon_name] = [ |
|
86 | 86 | /** |
87 | 87 | * The file name for the message type being registered. |
88 | 88 | * Required. |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | */ |
264 | 264 | public static function deregister(string $addon_name = '') |
265 | 265 | { |
266 | - if (! empty(self::$_ee_message_type_registry[ $addon_name ])) { |
|
266 | + if ( ! empty(self::$_ee_message_type_registry[$addon_name])) { |
|
267 | 267 | // let's make sure that we remove any place this message type was made active |
268 | 268 | /** @var EE_Message_Resource_Manager $Message_Resource_Manager */ |
269 | 269 | $Message_Resource_Manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | ); |
276 | 276 | $Message_Resource_Manager->deactivate_message_type($addon_name, false); |
277 | 277 | } |
278 | - unset(self::$_ee_message_type_registry[ $addon_name ]); |
|
278 | + unset(self::$_ee_message_type_registry[$addon_name]); |
|
279 | 279 | } |
280 | 280 | |
281 | 281 | |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | public static function register_msgs_autoload_paths(array $paths): array |
312 | 312 | { |
313 | 313 | $autoload_paths = []; |
314 | - if (! empty(self::$_ee_message_type_registry)) { |
|
314 | + if ( ! empty(self::$_ee_message_type_registry)) { |
|
315 | 315 | foreach (self::$_ee_message_type_registry as $mt_reg) { |
316 | 316 | if (empty($mt_reg['autoloadpaths'])) { |
317 | 317 | continue; |
@@ -400,7 +400,7 @@ discard block |
||
400 | 400 | continue; |
401 | 401 | } |
402 | 402 | foreach ($mt_reg['messengers_supporting_default_template_pack_with'] as $messenger_slug) { |
403 | - $supports[ $messenger_slug ][] = $addon_name; |
|
403 | + $supports[$messenger_slug][] = $addon_name; |
|
404 | 404 | } |
405 | 405 | } |
406 | 406 | return $supports; |
@@ -472,7 +472,7 @@ discard block |
||
472 | 472 | string $file_extension, |
473 | 473 | EE_Messages_Template_Pack $template_pack |
474 | 474 | ): string { |
475 | - if (! $template_pack instanceof EE_Messages_Template_Pack_Default) { |
|
475 | + if ( ! $template_pack instanceof EE_Messages_Template_Pack_Default) { |
|
476 | 476 | return $base_path_or_url; |
477 | 477 | } |
478 | 478 | foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
@@ -480,7 +480,7 @@ discard block |
||
480 | 480 | if ($url && ! empty($mt_reg['base_url_for_default_variation'])) { |
481 | 481 | return $mt_reg['base_url_for_default_variation']; |
482 | 482 | } |
483 | - if (! $url && ! empty($mt_reg['base_path_for_default_variation'])) { |
|
483 | + if ( ! $url && ! empty($mt_reg['base_path_for_default_variation'])) { |
|
484 | 484 | return $mt_reg['base_path_for_default_variation']; |
485 | 485 | } |
486 | 486 | } |
@@ -10,485 +10,485 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Register_Message_Type implements EEI_Plugin_API |
12 | 12 | { |
13 | - /** |
|
14 | - * Holds values for registered message types |
|
15 | - * |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - protected static $_ee_message_type_registry = []; |
|
13 | + /** |
|
14 | + * Holds values for registered message types |
|
15 | + * |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + protected static $_ee_message_type_registry = []; |
|
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * Method for registering new message types in the EE_messages system. |
|
23 | - * Note: All message types must have the following files in order to work: |
|
24 | - * Template files for default templates getting setup. |
|
25 | - * See /core/libraries/messages/defaults/default/ for examples |
|
26 | - * (note that template files match a specific naming schema). |
|
27 | - * These templates will need to be registered with the default template pack. |
|
28 | - * - EE_Messages_Validator extended class(es). See /core/libraries/messages/validators/email/ |
|
29 | - * for examples. Note for any new message types, there will need to be a validator for each |
|
30 | - * messenger combo this message type can activate with. |
|
31 | - * - And of course the main EE_{Message_Type_Name}_message_type class that defines the new |
|
32 | - * message type and its properties. |
|
33 | - * |
|
34 | - * @param string $addon_name Whatever is defined for the $name property of |
|
35 | - * the message type you are registering (eg. |
|
36 | - * declined_registration). Required. |
|
37 | - * @param array $setup_args An array of arguments provided for registering the message type. |
|
38 | - * @throws EE_Error |
|
39 | - * } |
|
40 | - * @see inline docs in the register method for what can be passed in as arguments. |
|
41 | - * @since 4.3.0 |
|
42 | - */ |
|
43 | - public static function register(string $addon_name = '', array $setup_args = []): bool |
|
44 | - { |
|
45 | - // required fields MUST be present, so let's make sure they are. |
|
46 | - if ( |
|
47 | - ! isset($addon_name) |
|
48 | - || ! is_array($setup_args) |
|
49 | - || empty($setup_args['mtfilename']) |
|
50 | - || empty($setup_args['autoloadpaths']) |
|
51 | - ) { |
|
52 | - throw new EE_Error( |
|
53 | - esc_html__( |
|
54 | - 'In order to register a message type with EE_Register_Message_Type::register, you must include a unique name for the message type, plus an array containing the following keys: "mtfilename", "autoloadpaths"', |
|
55 | - 'event_espresso' |
|
56 | - ) |
|
57 | - ); |
|
58 | - } |
|
21 | + /** |
|
22 | + * Method for registering new message types in the EE_messages system. |
|
23 | + * Note: All message types must have the following files in order to work: |
|
24 | + * Template files for default templates getting setup. |
|
25 | + * See /core/libraries/messages/defaults/default/ for examples |
|
26 | + * (note that template files match a specific naming schema). |
|
27 | + * These templates will need to be registered with the default template pack. |
|
28 | + * - EE_Messages_Validator extended class(es). See /core/libraries/messages/validators/email/ |
|
29 | + * for examples. Note for any new message types, there will need to be a validator for each |
|
30 | + * messenger combo this message type can activate with. |
|
31 | + * - And of course the main EE_{Message_Type_Name}_message_type class that defines the new |
|
32 | + * message type and its properties. |
|
33 | + * |
|
34 | + * @param string $addon_name Whatever is defined for the $name property of |
|
35 | + * the message type you are registering (eg. |
|
36 | + * declined_registration). Required. |
|
37 | + * @param array $setup_args An array of arguments provided for registering the message type. |
|
38 | + * @throws EE_Error |
|
39 | + * } |
|
40 | + * @see inline docs in the register method for what can be passed in as arguments. |
|
41 | + * @since 4.3.0 |
|
42 | + */ |
|
43 | + public static function register(string $addon_name = '', array $setup_args = []): bool |
|
44 | + { |
|
45 | + // required fields MUST be present, so let's make sure they are. |
|
46 | + if ( |
|
47 | + ! isset($addon_name) |
|
48 | + || ! is_array($setup_args) |
|
49 | + || empty($setup_args['mtfilename']) |
|
50 | + || empty($setup_args['autoloadpaths']) |
|
51 | + ) { |
|
52 | + throw new EE_Error( |
|
53 | + esc_html__( |
|
54 | + 'In order to register a message type with EE_Register_Message_Type::register, you must include a unique name for the message type, plus an array containing the following keys: "mtfilename", "autoloadpaths"', |
|
55 | + 'event_espresso' |
|
56 | + ) |
|
57 | + ); |
|
58 | + } |
|
59 | 59 | |
60 | - // make sure we don't register twice |
|
61 | - if (isset(self::$_ee_message_type_registry[ $addon_name ])) { |
|
62 | - return true; |
|
63 | - } |
|
60 | + // make sure we don't register twice |
|
61 | + if (isset(self::$_ee_message_type_registry[ $addon_name ])) { |
|
62 | + return true; |
|
63 | + } |
|
64 | 64 | |
65 | - // make sure this was called in the right place! |
|
66 | - if ( |
|
67 | - ! did_action('EE_Brewing_Regular___messages_caf') |
|
68 | - || did_action('AHEE__EE_System__perform_activations_upgrades_and_migrations') |
|
69 | - ) { |
|
70 | - EE_Error::doing_it_wrong( |
|
71 | - __METHOD__, |
|
72 | - sprintf( |
|
73 | - esc_html__( |
|
74 | - 'A message type named "%s" has been attempted to be registered with the EE Messages System. It may or may not work because it should be only called on the "EE_Brewing_Regular___messages_caf" hook.', |
|
75 | - 'event_espresso' |
|
76 | - ), |
|
77 | - $addon_name |
|
78 | - ), |
|
79 | - '4.3.0' |
|
80 | - ); |
|
81 | - } |
|
82 | - // setup $__ee_message_type_registry array from incoming values. |
|
83 | - self::$_ee_message_type_registry[ $addon_name ] = [ |
|
84 | - /** |
|
85 | - * The file name for the message type being registered. |
|
86 | - * Required. |
|
87 | - * |
|
88 | - * @type string |
|
89 | - */ |
|
90 | - 'mtfilename' => (string) $setup_args['mtfilename'], |
|
91 | - /** |
|
92 | - * Autoload paths for classes used by the message type. |
|
93 | - * Required. |
|
94 | - * |
|
95 | - * @type array |
|
96 | - */ |
|
97 | - 'autoloadpaths' => (array) $setup_args['autoloadpaths'], |
|
98 | - /** |
|
99 | - * Messengers that the message type should be able to activate with. |
|
100 | - * Use messenger slugs. |
|
101 | - * |
|
102 | - * @type array |
|
103 | - */ |
|
104 | - 'messengers_to_activate_with' => ! empty($setup_args['messengers_to_activate_with']) |
|
105 | - ? (array) $setup_args['messengers_to_activate_with'] |
|
106 | - : [], |
|
107 | - /** |
|
108 | - * Messengers that the message type should validate with. |
|
109 | - * Use messenger slugs. |
|
110 | - * |
|
111 | - * @type array |
|
112 | - */ |
|
113 | - 'messengers_to_validate_with' => ! empty($setup_args['messengers_to_validate_with']) |
|
114 | - ? (array) $setup_args['messengers_to_validate_with'] |
|
115 | - : [], |
|
116 | - /** |
|
117 | - * Whether to force activate this message type the first time it is registered. |
|
118 | - * |
|
119 | - * @type bool False means its not activated by default and left up to the end user to activate. |
|
120 | - */ |
|
121 | - 'force_activation' => ! empty($setup_args['force_activation']) |
|
122 | - && $setup_args['force_activation'], |
|
123 | - /** |
|
124 | - * What messengers this message type supports the default template pack for. |
|
125 | - * Note: If you do not set this (or any of the following template pack/variation related arguments) to true, |
|
126 | - * then it is expected that the message type being registered is doing its own custom default template |
|
127 | - * pack/variation registration. |
|
128 | - * |
|
129 | - * If this is set and has values, then it is expected that the following arguments are also set in the incoming options |
|
130 | - * $setup_arguments array as well: |
|
131 | - * - 'base_path_for_default_templates' |
|
132 | - * |
|
133 | - * @type array Expect an array of messengers this supports default template packs for. |
|
134 | - */ |
|
135 | - 'messengers_supporting_default_template_pack_with' => isset($setup_args['messengers_supporting_default_template_pack_with']) |
|
136 | - ? (array) $setup_args['messengers_supporting_default_template_pack_with'] |
|
137 | - : [], |
|
138 | - /** |
|
139 | - * The base path where the default templates for this message type can be found. |
|
140 | - * |
|
141 | - * @type string |
|
142 | - */ |
|
143 | - 'base_path_for_default_templates' => $setup_args['base_path_for_default_templates'] ?? '', |
|
144 | - /** |
|
145 | - * The base path where the default variations for this message type can be found. |
|
146 | - * |
|
147 | - * @type string |
|
148 | - */ |
|
149 | - 'base_path_for_default_variation' => $setup_args['base_path_for_default_variation'] ?? '', |
|
150 | - /** |
|
151 | - * The base url for the default variations for this message type. |
|
152 | - * |
|
153 | - * @type string |
|
154 | - */ |
|
155 | - 'base_url_for_default_variation' => $setup_args['base_url_for_default_variation'] ?? '', |
|
156 | - ]; |
|
157 | - // add filters but only if they haven't already been set (these filters only need to be registered ONCE because |
|
158 | - // the callback handles all registered message types. |
|
159 | - if ( |
|
160 | - has_filter( |
|
161 | - 'FHEE__EED_Messages___set_messages_paths___MSG_PATHS', |
|
162 | - ['EE_Register_Message_Type', 'register_msgs_autoload_paths'] |
|
163 | - ) === false |
|
164 | - ) { |
|
165 | - add_filter( |
|
166 | - 'FHEE__EED_Messages___set_messages_paths___MSG_PATHS', |
|
167 | - ['EE_Register_Message_Type', 'register_msgs_autoload_paths'], |
|
168 | - 10 |
|
169 | - ); |
|
170 | - add_filter( |
|
171 | - 'FHEE__EE_messages__get_installed__messagetype_files', |
|
172 | - ['EE_Register_Message_Type', 'register_messagetype_files'], |
|
173 | - 10, |
|
174 | - 1 |
|
175 | - ); |
|
176 | - add_filter( |
|
177 | - 'FHEE__EE_messenger__get_default_message_types__default_types', |
|
178 | - ['EE_Register_Message_Type', 'register_messengers_to_activate_mt_with'], |
|
179 | - 10, |
|
180 | - 2 |
|
181 | - ); |
|
182 | - add_filter( |
|
183 | - 'FHEE__EE_messenger__get_valid_message_types__valid_types', |
|
184 | - ['EE_Register_Message_Type', 'register_messengers_to_validate_mt_with'], |
|
185 | - 10, |
|
186 | - 2 |
|
187 | - ); |
|
188 | - // actions |
|
189 | - add_action( |
|
190 | - 'AHEE__EE_Addon__initialize_default_data__begin', |
|
191 | - ['EE_Register_Message_Type', 'set_defaults'] |
|
192 | - ); |
|
65 | + // make sure this was called in the right place! |
|
66 | + if ( |
|
67 | + ! did_action('EE_Brewing_Regular___messages_caf') |
|
68 | + || did_action('AHEE__EE_System__perform_activations_upgrades_and_migrations') |
|
69 | + ) { |
|
70 | + EE_Error::doing_it_wrong( |
|
71 | + __METHOD__, |
|
72 | + sprintf( |
|
73 | + esc_html__( |
|
74 | + 'A message type named "%s" has been attempted to be registered with the EE Messages System. It may or may not work because it should be only called on the "EE_Brewing_Regular___messages_caf" hook.', |
|
75 | + 'event_espresso' |
|
76 | + ), |
|
77 | + $addon_name |
|
78 | + ), |
|
79 | + '4.3.0' |
|
80 | + ); |
|
81 | + } |
|
82 | + // setup $__ee_message_type_registry array from incoming values. |
|
83 | + self::$_ee_message_type_registry[ $addon_name ] = [ |
|
84 | + /** |
|
85 | + * The file name for the message type being registered. |
|
86 | + * Required. |
|
87 | + * |
|
88 | + * @type string |
|
89 | + */ |
|
90 | + 'mtfilename' => (string) $setup_args['mtfilename'], |
|
91 | + /** |
|
92 | + * Autoload paths for classes used by the message type. |
|
93 | + * Required. |
|
94 | + * |
|
95 | + * @type array |
|
96 | + */ |
|
97 | + 'autoloadpaths' => (array) $setup_args['autoloadpaths'], |
|
98 | + /** |
|
99 | + * Messengers that the message type should be able to activate with. |
|
100 | + * Use messenger slugs. |
|
101 | + * |
|
102 | + * @type array |
|
103 | + */ |
|
104 | + 'messengers_to_activate_with' => ! empty($setup_args['messengers_to_activate_with']) |
|
105 | + ? (array) $setup_args['messengers_to_activate_with'] |
|
106 | + : [], |
|
107 | + /** |
|
108 | + * Messengers that the message type should validate with. |
|
109 | + * Use messenger slugs. |
|
110 | + * |
|
111 | + * @type array |
|
112 | + */ |
|
113 | + 'messengers_to_validate_with' => ! empty($setup_args['messengers_to_validate_with']) |
|
114 | + ? (array) $setup_args['messengers_to_validate_with'] |
|
115 | + : [], |
|
116 | + /** |
|
117 | + * Whether to force activate this message type the first time it is registered. |
|
118 | + * |
|
119 | + * @type bool False means its not activated by default and left up to the end user to activate. |
|
120 | + */ |
|
121 | + 'force_activation' => ! empty($setup_args['force_activation']) |
|
122 | + && $setup_args['force_activation'], |
|
123 | + /** |
|
124 | + * What messengers this message type supports the default template pack for. |
|
125 | + * Note: If you do not set this (or any of the following template pack/variation related arguments) to true, |
|
126 | + * then it is expected that the message type being registered is doing its own custom default template |
|
127 | + * pack/variation registration. |
|
128 | + * |
|
129 | + * If this is set and has values, then it is expected that the following arguments are also set in the incoming options |
|
130 | + * $setup_arguments array as well: |
|
131 | + * - 'base_path_for_default_templates' |
|
132 | + * |
|
133 | + * @type array Expect an array of messengers this supports default template packs for. |
|
134 | + */ |
|
135 | + 'messengers_supporting_default_template_pack_with' => isset($setup_args['messengers_supporting_default_template_pack_with']) |
|
136 | + ? (array) $setup_args['messengers_supporting_default_template_pack_with'] |
|
137 | + : [], |
|
138 | + /** |
|
139 | + * The base path where the default templates for this message type can be found. |
|
140 | + * |
|
141 | + * @type string |
|
142 | + */ |
|
143 | + 'base_path_for_default_templates' => $setup_args['base_path_for_default_templates'] ?? '', |
|
144 | + /** |
|
145 | + * The base path where the default variations for this message type can be found. |
|
146 | + * |
|
147 | + * @type string |
|
148 | + */ |
|
149 | + 'base_path_for_default_variation' => $setup_args['base_path_for_default_variation'] ?? '', |
|
150 | + /** |
|
151 | + * The base url for the default variations for this message type. |
|
152 | + * |
|
153 | + * @type string |
|
154 | + */ |
|
155 | + 'base_url_for_default_variation' => $setup_args['base_url_for_default_variation'] ?? '', |
|
156 | + ]; |
|
157 | + // add filters but only if they haven't already been set (these filters only need to be registered ONCE because |
|
158 | + // the callback handles all registered message types. |
|
159 | + if ( |
|
160 | + has_filter( |
|
161 | + 'FHEE__EED_Messages___set_messages_paths___MSG_PATHS', |
|
162 | + ['EE_Register_Message_Type', 'register_msgs_autoload_paths'] |
|
163 | + ) === false |
|
164 | + ) { |
|
165 | + add_filter( |
|
166 | + 'FHEE__EED_Messages___set_messages_paths___MSG_PATHS', |
|
167 | + ['EE_Register_Message_Type', 'register_msgs_autoload_paths'], |
|
168 | + 10 |
|
169 | + ); |
|
170 | + add_filter( |
|
171 | + 'FHEE__EE_messages__get_installed__messagetype_files', |
|
172 | + ['EE_Register_Message_Type', 'register_messagetype_files'], |
|
173 | + 10, |
|
174 | + 1 |
|
175 | + ); |
|
176 | + add_filter( |
|
177 | + 'FHEE__EE_messenger__get_default_message_types__default_types', |
|
178 | + ['EE_Register_Message_Type', 'register_messengers_to_activate_mt_with'], |
|
179 | + 10, |
|
180 | + 2 |
|
181 | + ); |
|
182 | + add_filter( |
|
183 | + 'FHEE__EE_messenger__get_valid_message_types__valid_types', |
|
184 | + ['EE_Register_Message_Type', 'register_messengers_to_validate_mt_with'], |
|
185 | + 10, |
|
186 | + 2 |
|
187 | + ); |
|
188 | + // actions |
|
189 | + add_action( |
|
190 | + 'AHEE__EE_Addon__initialize_default_data__begin', |
|
191 | + ['EE_Register_Message_Type', 'set_defaults'] |
|
192 | + ); |
|
193 | 193 | |
194 | - // default template packs and variations related |
|
195 | - add_filter( |
|
196 | - 'FHEE__EE_Messages_Template_Pack_Default__get_supports', |
|
197 | - ['EE_Register_Message_Type', 'register_default_template_pack_supports'] |
|
198 | - ); |
|
199 | - add_filter( |
|
200 | - 'FHEE__EE_Template_Pack___get_specific_template__filtered_base_path', |
|
201 | - ['EE_Register_Message_Type', 'register_base_template_path'], |
|
202 | - 10, |
|
203 | - 6 |
|
204 | - ); |
|
205 | - add_filter( |
|
206 | - 'FHEE__EE_Messages_Template_Pack__get_variation__base_path_or_url', |
|
207 | - ['EE_Register_Message_Type', 'register_variation_base_path_or_url'], |
|
208 | - 10, |
|
209 | - 8 |
|
210 | - ); |
|
211 | - add_filter( |
|
212 | - 'FHEE__EE_Messages_Template_Pack__get_variation__base_path', |
|
213 | - ['EE_Register_Message_Type', 'register_variation_base_path_or_url'], |
|
214 | - 10, |
|
215 | - 8 |
|
216 | - ); |
|
217 | - } |
|
218 | - return true; |
|
219 | - } |
|
194 | + // default template packs and variations related |
|
195 | + add_filter( |
|
196 | + 'FHEE__EE_Messages_Template_Pack_Default__get_supports', |
|
197 | + ['EE_Register_Message_Type', 'register_default_template_pack_supports'] |
|
198 | + ); |
|
199 | + add_filter( |
|
200 | + 'FHEE__EE_Template_Pack___get_specific_template__filtered_base_path', |
|
201 | + ['EE_Register_Message_Type', 'register_base_template_path'], |
|
202 | + 10, |
|
203 | + 6 |
|
204 | + ); |
|
205 | + add_filter( |
|
206 | + 'FHEE__EE_Messages_Template_Pack__get_variation__base_path_or_url', |
|
207 | + ['EE_Register_Message_Type', 'register_variation_base_path_or_url'], |
|
208 | + 10, |
|
209 | + 8 |
|
210 | + ); |
|
211 | + add_filter( |
|
212 | + 'FHEE__EE_Messages_Template_Pack__get_variation__base_path', |
|
213 | + ['EE_Register_Message_Type', 'register_variation_base_path_or_url'], |
|
214 | + 10, |
|
215 | + 8 |
|
216 | + ); |
|
217 | + } |
|
218 | + return true; |
|
219 | + } |
|
220 | 220 | |
221 | 221 | |
222 | - /** |
|
223 | - * This just ensures that when an addon registers a message type that on initial activation/reactivation the |
|
224 | - * defaults the addon sets are taken care of. |
|
225 | - * |
|
226 | - * @throws EE_Error |
|
227 | - * @throws ReflectionException |
|
228 | - */ |
|
229 | - public static function set_defaults() |
|
230 | - { |
|
231 | - /** @type EE_Message_Resource_Manager $message_resource_manager */ |
|
232 | - $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
222 | + /** |
|
223 | + * This just ensures that when an addon registers a message type that on initial activation/reactivation the |
|
224 | + * defaults the addon sets are taken care of. |
|
225 | + * |
|
226 | + * @throws EE_Error |
|
227 | + * @throws ReflectionException |
|
228 | + */ |
|
229 | + public static function set_defaults() |
|
230 | + { |
|
231 | + /** @type EE_Message_Resource_Manager $message_resource_manager */ |
|
232 | + $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
233 | 233 | |
234 | - // for any message types with force activation, let's ensure they are activated |
|
235 | - foreach (self::$_ee_message_type_registry as $addon_name => $settings) { |
|
236 | - if ($settings['force_activation']) { |
|
237 | - foreach ($settings['messengers_to_activate_with'] as $messenger) { |
|
238 | - // DO not force activation if this message type has already been activated in the system |
|
239 | - if ( |
|
240 | - ! $message_resource_manager->has_message_type_been_activated_for_messenger( |
|
241 | - $addon_name, |
|
242 | - $messenger |
|
243 | - ) |
|
244 | - ) { |
|
245 | - $message_resource_manager->ensure_message_type_is_active($addon_name, $messenger); |
|
246 | - } |
|
247 | - } |
|
248 | - } |
|
249 | - } |
|
250 | - } |
|
234 | + // for any message types with force activation, let's ensure they are activated |
|
235 | + foreach (self::$_ee_message_type_registry as $addon_name => $settings) { |
|
236 | + if ($settings['force_activation']) { |
|
237 | + foreach ($settings['messengers_to_activate_with'] as $messenger) { |
|
238 | + // DO not force activation if this message type has already been activated in the system |
|
239 | + if ( |
|
240 | + ! $message_resource_manager->has_message_type_been_activated_for_messenger( |
|
241 | + $addon_name, |
|
242 | + $messenger |
|
243 | + ) |
|
244 | + ) { |
|
245 | + $message_resource_manager->ensure_message_type_is_active($addon_name, $messenger); |
|
246 | + } |
|
247 | + } |
|
248 | + } |
|
249 | + } |
|
250 | + } |
|
251 | 251 | |
252 | 252 | |
253 | - /** |
|
254 | - * This deregisters a message type that was previously registered with a specific message_type_name. |
|
255 | - * |
|
256 | - * @param string $addon_name the name for the message type that was previously registered |
|
257 | - * @return void |
|
258 | - * @throws EE_Error |
|
259 | - * @throws ReflectionException |
|
260 | - * @since 4.3.0 |
|
261 | - */ |
|
262 | - public static function deregister(string $addon_name = '') |
|
263 | - { |
|
264 | - if (! empty(self::$_ee_message_type_registry[ $addon_name ])) { |
|
265 | - // let's make sure that we remove any place this message type was made active |
|
266 | - /** @var EE_Message_Resource_Manager $Message_Resource_Manager */ |
|
267 | - $Message_Resource_Manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
268 | - // ensures that if this message type is registered again that it retains its previous active state vs |
|
269 | - // remaining inactive. |
|
270 | - $Message_Resource_Manager->remove_message_type_has_been_activated_from_all_messengers( |
|
271 | - $addon_name, |
|
272 | - true |
|
273 | - ); |
|
274 | - $Message_Resource_Manager->deactivate_message_type($addon_name, false); |
|
275 | - } |
|
276 | - unset(self::$_ee_message_type_registry[ $addon_name ]); |
|
277 | - } |
|
253 | + /** |
|
254 | + * This deregisters a message type that was previously registered with a specific message_type_name. |
|
255 | + * |
|
256 | + * @param string $addon_name the name for the message type that was previously registered |
|
257 | + * @return void |
|
258 | + * @throws EE_Error |
|
259 | + * @throws ReflectionException |
|
260 | + * @since 4.3.0 |
|
261 | + */ |
|
262 | + public static function deregister(string $addon_name = '') |
|
263 | + { |
|
264 | + if (! empty(self::$_ee_message_type_registry[ $addon_name ])) { |
|
265 | + // let's make sure that we remove any place this message type was made active |
|
266 | + /** @var EE_Message_Resource_Manager $Message_Resource_Manager */ |
|
267 | + $Message_Resource_Manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
268 | + // ensures that if this message type is registered again that it retains its previous active state vs |
|
269 | + // remaining inactive. |
|
270 | + $Message_Resource_Manager->remove_message_type_has_been_activated_from_all_messengers( |
|
271 | + $addon_name, |
|
272 | + true |
|
273 | + ); |
|
274 | + $Message_Resource_Manager->deactivate_message_type($addon_name, false); |
|
275 | + } |
|
276 | + unset(self::$_ee_message_type_registry[ $addon_name ]); |
|
277 | + } |
|
278 | 278 | |
279 | 279 | |
280 | - /** |
|
281 | - * callback for FHEE__EE_messages__get_installed__messagetype_files filter. |
|
282 | - * |
|
283 | - * @param array $messagetype_files The current array of message type file names |
|
284 | - * @return array Array of message type file names |
|
285 | - * @since 4.3.0 |
|
286 | - */ |
|
287 | - public static function register_messagetype_files(array $messagetype_files): array |
|
288 | - { |
|
289 | - if (empty(self::$_ee_message_type_registry)) { |
|
290 | - return $messagetype_files; |
|
291 | - } |
|
292 | - foreach (self::$_ee_message_type_registry as $mt_reg) { |
|
293 | - if (empty($mt_reg['mtfilename'])) { |
|
294 | - continue; |
|
295 | - } |
|
296 | - $messagetype_files[] = $mt_reg['mtfilename']; |
|
297 | - } |
|
298 | - return $messagetype_files; |
|
299 | - } |
|
280 | + /** |
|
281 | + * callback for FHEE__EE_messages__get_installed__messagetype_files filter. |
|
282 | + * |
|
283 | + * @param array $messagetype_files The current array of message type file names |
|
284 | + * @return array Array of message type file names |
|
285 | + * @since 4.3.0 |
|
286 | + */ |
|
287 | + public static function register_messagetype_files(array $messagetype_files): array |
|
288 | + { |
|
289 | + if (empty(self::$_ee_message_type_registry)) { |
|
290 | + return $messagetype_files; |
|
291 | + } |
|
292 | + foreach (self::$_ee_message_type_registry as $mt_reg) { |
|
293 | + if (empty($mt_reg['mtfilename'])) { |
|
294 | + continue; |
|
295 | + } |
|
296 | + $messagetype_files[] = $mt_reg['mtfilename']; |
|
297 | + } |
|
298 | + return $messagetype_files; |
|
299 | + } |
|
300 | 300 | |
301 | 301 | |
302 | - /** |
|
303 | - * callback for FHEE__EED_Messages___set_messages_paths___MSG_PATHS filter. |
|
304 | - * |
|
305 | - * @param array $paths array of paths to be checked by EE_messages autoloader. |
|
306 | - * @return array |
|
307 | - * @since 4.3.0 |
|
308 | - */ |
|
309 | - public static function register_msgs_autoload_paths(array $paths): array |
|
310 | - { |
|
311 | - $autoload_paths = []; |
|
312 | - if (! empty(self::$_ee_message_type_registry)) { |
|
313 | - foreach (self::$_ee_message_type_registry as $mt_reg) { |
|
314 | - if (empty($mt_reg['autoloadpaths'])) { |
|
315 | - continue; |
|
316 | - } |
|
317 | - $autoload_paths[] = $mt_reg['autoloadpaths']; |
|
318 | - } |
|
319 | - } |
|
320 | - return array_merge($paths, ...$autoload_paths); |
|
321 | - } |
|
302 | + /** |
|
303 | + * callback for FHEE__EED_Messages___set_messages_paths___MSG_PATHS filter. |
|
304 | + * |
|
305 | + * @param array $paths array of paths to be checked by EE_messages autoloader. |
|
306 | + * @return array |
|
307 | + * @since 4.3.0 |
|
308 | + */ |
|
309 | + public static function register_msgs_autoload_paths(array $paths): array |
|
310 | + { |
|
311 | + $autoload_paths = []; |
|
312 | + if (! empty(self::$_ee_message_type_registry)) { |
|
313 | + foreach (self::$_ee_message_type_registry as $mt_reg) { |
|
314 | + if (empty($mt_reg['autoloadpaths'])) { |
|
315 | + continue; |
|
316 | + } |
|
317 | + $autoload_paths[] = $mt_reg['autoloadpaths']; |
|
318 | + } |
|
319 | + } |
|
320 | + return array_merge($paths, ...$autoload_paths); |
|
321 | + } |
|
322 | 322 | |
323 | 323 | |
324 | - /** |
|
325 | - * callback for FHEE__EE_messenger__get_default_message_types__default_types filter. |
|
326 | - * |
|
327 | - * @param array $default_types array of message types activated with messenger ( |
|
328 | - * corresponds to the $name property of message type) |
|
329 | - * @param EE_messenger $messenger The EE_messenger the filter is called from. |
|
330 | - * @return array |
|
331 | - * @since 4.3.0 |
|
332 | - */ |
|
333 | - public static function register_messengers_to_activate_mt_with(array $default_types, EE_messenger $messenger): array |
|
334 | - { |
|
335 | - if (empty(self::$_ee_message_type_registry)) { |
|
336 | - return $default_types; |
|
337 | - } |
|
338 | - foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
339 | - if (empty($mt_reg['messengers_to_activate_with']) || empty($mt_reg['mtfilename'])) { |
|
340 | - continue; |
|
341 | - } |
|
342 | - // loop through each of the messengers and if it matches the loaded class |
|
343 | - // then we add this message type to the |
|
344 | - foreach ($mt_reg['messengers_to_activate_with'] as $msgr) { |
|
345 | - if ($messenger->name == $msgr) { |
|
346 | - $default_types[] = $addon_name; |
|
347 | - } |
|
348 | - } |
|
349 | - } |
|
324 | + /** |
|
325 | + * callback for FHEE__EE_messenger__get_default_message_types__default_types filter. |
|
326 | + * |
|
327 | + * @param array $default_types array of message types activated with messenger ( |
|
328 | + * corresponds to the $name property of message type) |
|
329 | + * @param EE_messenger $messenger The EE_messenger the filter is called from. |
|
330 | + * @return array |
|
331 | + * @since 4.3.0 |
|
332 | + */ |
|
333 | + public static function register_messengers_to_activate_mt_with(array $default_types, EE_messenger $messenger): array |
|
334 | + { |
|
335 | + if (empty(self::$_ee_message_type_registry)) { |
|
336 | + return $default_types; |
|
337 | + } |
|
338 | + foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
339 | + if (empty($mt_reg['messengers_to_activate_with']) || empty($mt_reg['mtfilename'])) { |
|
340 | + continue; |
|
341 | + } |
|
342 | + // loop through each of the messengers and if it matches the loaded class |
|
343 | + // then we add this message type to the |
|
344 | + foreach ($mt_reg['messengers_to_activate_with'] as $msgr) { |
|
345 | + if ($messenger->name == $msgr) { |
|
346 | + $default_types[] = $addon_name; |
|
347 | + } |
|
348 | + } |
|
349 | + } |
|
350 | 350 | |
351 | - return $default_types; |
|
352 | - } |
|
351 | + return $default_types; |
|
352 | + } |
|
353 | 353 | |
354 | 354 | |
355 | - /** |
|
356 | - * callback for FHEE__EE_messenger__get_valid_message_types__default_types filter. |
|
357 | - * |
|
358 | - * @param array $valid_types array of message types valid with messenger ( |
|
359 | - * corresponds to the $name property of message type) |
|
360 | - * @param EE_messenger $messenger The EE_messenger the filter is called from. |
|
361 | - * @return array |
|
362 | - * @since 4.3.0 |
|
363 | - */ |
|
364 | - public static function register_messengers_to_validate_mt_with(array $valid_types, EE_messenger $messenger): array |
|
365 | - { |
|
366 | - if (empty(self::$_ee_message_type_registry)) { |
|
367 | - return $valid_types; |
|
368 | - } |
|
369 | - foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
370 | - if (empty($mt_reg['messengers_to_validate_with']) || empty($mt_reg['mtfilename'])) { |
|
371 | - continue; |
|
372 | - } |
|
373 | - // loop through each of the messengers and if it matches the loaded class |
|
374 | - // then we add this message type to the |
|
375 | - foreach ($mt_reg['messengers_to_validate_with'] as $msgr) { |
|
376 | - if ($messenger->name == $msgr) { |
|
377 | - $valid_types[] = $addon_name; |
|
378 | - } |
|
379 | - } |
|
380 | - } |
|
355 | + /** |
|
356 | + * callback for FHEE__EE_messenger__get_valid_message_types__default_types filter. |
|
357 | + * |
|
358 | + * @param array $valid_types array of message types valid with messenger ( |
|
359 | + * corresponds to the $name property of message type) |
|
360 | + * @param EE_messenger $messenger The EE_messenger the filter is called from. |
|
361 | + * @return array |
|
362 | + * @since 4.3.0 |
|
363 | + */ |
|
364 | + public static function register_messengers_to_validate_mt_with(array $valid_types, EE_messenger $messenger): array |
|
365 | + { |
|
366 | + if (empty(self::$_ee_message_type_registry)) { |
|
367 | + return $valid_types; |
|
368 | + } |
|
369 | + foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
370 | + if (empty($mt_reg['messengers_to_validate_with']) || empty($mt_reg['mtfilename'])) { |
|
371 | + continue; |
|
372 | + } |
|
373 | + // loop through each of the messengers and if it matches the loaded class |
|
374 | + // then we add this message type to the |
|
375 | + foreach ($mt_reg['messengers_to_validate_with'] as $msgr) { |
|
376 | + if ($messenger->name == $msgr) { |
|
377 | + $valid_types[] = $addon_name; |
|
378 | + } |
|
379 | + } |
|
380 | + } |
|
381 | 381 | |
382 | - return $valid_types; |
|
383 | - } |
|
382 | + return $valid_types; |
|
383 | + } |
|
384 | 384 | |
385 | 385 | |
386 | - /** |
|
387 | - * Callback for `FHEE__EE_Messages_Template_Pack_Default__get_supports` filter to register this message type as |
|
388 | - * supporting the default template pack |
|
389 | - * |
|
390 | - * @param array $supports |
|
391 | - * |
|
392 | - * @return array |
|
393 | - */ |
|
394 | - public static function register_default_template_pack_supports(array $supports): array |
|
395 | - { |
|
396 | - foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
397 | - if (empty($mt_reg['messengers_supporting_default_template_pack_with'])) { |
|
398 | - continue; |
|
399 | - } |
|
400 | - foreach ($mt_reg['messengers_supporting_default_template_pack_with'] as $messenger_slug) { |
|
401 | - $supports[ $messenger_slug ][] = $addon_name; |
|
402 | - } |
|
403 | - } |
|
404 | - return $supports; |
|
405 | - } |
|
386 | + /** |
|
387 | + * Callback for `FHEE__EE_Messages_Template_Pack_Default__get_supports` filter to register this message type as |
|
388 | + * supporting the default template pack |
|
389 | + * |
|
390 | + * @param array $supports |
|
391 | + * |
|
392 | + * @return array |
|
393 | + */ |
|
394 | + public static function register_default_template_pack_supports(array $supports): array |
|
395 | + { |
|
396 | + foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
397 | + if (empty($mt_reg['messengers_supporting_default_template_pack_with'])) { |
|
398 | + continue; |
|
399 | + } |
|
400 | + foreach ($mt_reg['messengers_supporting_default_template_pack_with'] as $messenger_slug) { |
|
401 | + $supports[ $messenger_slug ][] = $addon_name; |
|
402 | + } |
|
403 | + } |
|
404 | + return $supports; |
|
405 | + } |
|
406 | 406 | |
407 | 407 | |
408 | - /** |
|
409 | - * Callback for FHEE__EE_Template_Pack___get_specific_template__filtered_base_path |
|
410 | - * |
|
411 | - * @param string $base_path The original base path for message templates |
|
412 | - * @param EE_messenger $messenger |
|
413 | - * @param EE_message_type $message_type |
|
414 | - * @param string $field The field requesting a template |
|
415 | - * @param string $context The context requesting a template |
|
416 | - * @param EE_Messages_Template_Pack $template_pack |
|
417 | - * |
|
418 | - * @return string |
|
419 | - */ |
|
420 | - public static function register_base_template_path( |
|
421 | - string $base_path, |
|
422 | - EE_messenger $messenger, |
|
423 | - EE_message_type $message_type, |
|
424 | - string $field, |
|
425 | - string $context, |
|
426 | - EE_Messages_Template_Pack $template_pack |
|
427 | - ): string { |
|
428 | - if ( |
|
429 | - ! $template_pack instanceof EE_Messages_Template_Pack_Default |
|
430 | - || ! $message_type instanceof EE_message_type |
|
431 | - ) { |
|
432 | - return $base_path; |
|
433 | - } |
|
434 | - foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
435 | - if ( |
|
436 | - $message_type->name === $addon_name |
|
437 | - && ! empty($mt_reg['base_path_for_default_templates']) |
|
438 | - ) { |
|
439 | - return $mt_reg['base_path_for_default_templates']; |
|
440 | - } |
|
441 | - } |
|
442 | - return $base_path; |
|
443 | - } |
|
408 | + /** |
|
409 | + * Callback for FHEE__EE_Template_Pack___get_specific_template__filtered_base_path |
|
410 | + * |
|
411 | + * @param string $base_path The original base path for message templates |
|
412 | + * @param EE_messenger $messenger |
|
413 | + * @param EE_message_type $message_type |
|
414 | + * @param string $field The field requesting a template |
|
415 | + * @param string $context The context requesting a template |
|
416 | + * @param EE_Messages_Template_Pack $template_pack |
|
417 | + * |
|
418 | + * @return string |
|
419 | + */ |
|
420 | + public static function register_base_template_path( |
|
421 | + string $base_path, |
|
422 | + EE_messenger $messenger, |
|
423 | + EE_message_type $message_type, |
|
424 | + string $field, |
|
425 | + string $context, |
|
426 | + EE_Messages_Template_Pack $template_pack |
|
427 | + ): string { |
|
428 | + if ( |
|
429 | + ! $template_pack instanceof EE_Messages_Template_Pack_Default |
|
430 | + || ! $message_type instanceof EE_message_type |
|
431 | + ) { |
|
432 | + return $base_path; |
|
433 | + } |
|
434 | + foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
435 | + if ( |
|
436 | + $message_type->name === $addon_name |
|
437 | + && ! empty($mt_reg['base_path_for_default_templates']) |
|
438 | + ) { |
|
439 | + return $mt_reg['base_path_for_default_templates']; |
|
440 | + } |
|
441 | + } |
|
442 | + return $base_path; |
|
443 | + } |
|
444 | 444 | |
445 | 445 | |
446 | - /** |
|
447 | - * Callback for FHEE__EE_Messages_Template_Pack__get_variation__base_path and |
|
448 | - * FHEE__EE_Messages_Template_Pack__get_variation__base_path_or_url hooks |
|
449 | - * |
|
450 | - * @param string $base_path_or_url The original incoming base url or path |
|
451 | - * @param string $messenger_slug The slug of the messenger the template is being generated |
|
452 | - * for. |
|
453 | - * @param string $message_type_slug The slug of the message type the template is being generated |
|
454 | - * for. |
|
455 | - * @param string $type The "type" of css being requested. |
|
456 | - * @param string $variation The variation being requested. |
|
457 | - * @param bool $url whether a url or path is being requested. |
|
458 | - * @param string $file_extension What file extension is expected for the variation file. |
|
459 | - * @param EE_Messages_Template_Pack $template_pack |
|
460 | - * |
|
461 | - * @return string |
|
462 | - */ |
|
463 | - public static function register_variation_base_path_or_url( |
|
464 | - string $base_path_or_url, |
|
465 | - string $messenger_slug, |
|
466 | - string $message_type_slug, |
|
467 | - string $type, |
|
468 | - string $variation, |
|
469 | - bool $url, |
|
470 | - string $file_extension, |
|
471 | - EE_Messages_Template_Pack $template_pack |
|
472 | - ): string { |
|
473 | - if (! $template_pack instanceof EE_Messages_Template_Pack_Default) { |
|
474 | - return $base_path_or_url; |
|
475 | - } |
|
476 | - foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
477 | - if ($addon_name === $message_type_slug) { |
|
478 | - if ($url && ! empty($mt_reg['base_url_for_default_variation'])) { |
|
479 | - return $mt_reg['base_url_for_default_variation']; |
|
480 | - } |
|
481 | - if (! $url && ! empty($mt_reg['base_path_for_default_variation'])) { |
|
482 | - return $mt_reg['base_path_for_default_variation']; |
|
483 | - } |
|
484 | - } |
|
485 | - } |
|
486 | - return $base_path_or_url; |
|
487 | - } |
|
446 | + /** |
|
447 | + * Callback for FHEE__EE_Messages_Template_Pack__get_variation__base_path and |
|
448 | + * FHEE__EE_Messages_Template_Pack__get_variation__base_path_or_url hooks |
|
449 | + * |
|
450 | + * @param string $base_path_or_url The original incoming base url or path |
|
451 | + * @param string $messenger_slug The slug of the messenger the template is being generated |
|
452 | + * for. |
|
453 | + * @param string $message_type_slug The slug of the message type the template is being generated |
|
454 | + * for. |
|
455 | + * @param string $type The "type" of css being requested. |
|
456 | + * @param string $variation The variation being requested. |
|
457 | + * @param bool $url whether a url or path is being requested. |
|
458 | + * @param string $file_extension What file extension is expected for the variation file. |
|
459 | + * @param EE_Messages_Template_Pack $template_pack |
|
460 | + * |
|
461 | + * @return string |
|
462 | + */ |
|
463 | + public static function register_variation_base_path_or_url( |
|
464 | + string $base_path_or_url, |
|
465 | + string $messenger_slug, |
|
466 | + string $message_type_slug, |
|
467 | + string $type, |
|
468 | + string $variation, |
|
469 | + bool $url, |
|
470 | + string $file_extension, |
|
471 | + EE_Messages_Template_Pack $template_pack |
|
472 | + ): string { |
|
473 | + if (! $template_pack instanceof EE_Messages_Template_Pack_Default) { |
|
474 | + return $base_path_or_url; |
|
475 | + } |
|
476 | + foreach (self::$_ee_message_type_registry as $addon_name => $mt_reg) { |
|
477 | + if ($addon_name === $message_type_slug) { |
|
478 | + if ($url && ! empty($mt_reg['base_url_for_default_variation'])) { |
|
479 | + return $mt_reg['base_url_for_default_variation']; |
|
480 | + } |
|
481 | + if (! $url && ! empty($mt_reg['base_path_for_default_variation'])) { |
|
482 | + return $mt_reg['base_path_for_default_variation']; |
|
483 | + } |
|
484 | + } |
|
485 | + } |
|
486 | + return $base_path_or_url; |
|
487 | + } |
|
488 | 488 | |
489 | 489 | |
490 | - public static function reset(): void |
|
491 | - { |
|
492 | - self::$_ee_message_type_registry = []; |
|
493 | - } |
|
490 | + public static function reset(): void |
|
491 | + { |
|
492 | + self::$_ee_message_type_registry = []; |
|
493 | + } |
|
494 | 494 | } |
@@ -12,46 +12,46 @@ |
||
12 | 12 | */ |
13 | 13 | interface EncryptionMethodInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * returns true if the encryption method is cryptographically secure |
|
17 | - * |
|
18 | - * @return bool |
|
19 | - */ |
|
20 | - public function isCryptographicallySecure(); |
|
21 | - |
|
22 | - |
|
23 | - /** |
|
24 | - * returns true if the method can be used on the current server |
|
25 | - * |
|
26 | - * @return bool |
|
27 | - */ |
|
28 | - public function canUse(); |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * returns a message explaining why the encryption method in question can or can not be used |
|
33 | - * |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - public function canUseNotice(); |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * encrypts data |
|
41 | - * |
|
42 | - * @param string $text_to_encrypt - the text to be encrypted |
|
43 | - * @param string $encryption_key_identifier - name of the encryption key to use |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function encrypt($text_to_encrypt, $encryption_key_identifier = ''); |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * decrypts data |
|
51 | - * |
|
52 | - * @param string $encrypted_text - the text to be decrypted |
|
53 | - * @param string $encryption_key_identifier - name of the encryption key to use |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - public function decrypt($encrypted_text, $encryption_key_identifier = ''); |
|
15 | + /** |
|
16 | + * returns true if the encryption method is cryptographically secure |
|
17 | + * |
|
18 | + * @return bool |
|
19 | + */ |
|
20 | + public function isCryptographicallySecure(); |
|
21 | + |
|
22 | + |
|
23 | + /** |
|
24 | + * returns true if the method can be used on the current server |
|
25 | + * |
|
26 | + * @return bool |
|
27 | + */ |
|
28 | + public function canUse(); |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * returns a message explaining why the encryption method in question can or can not be used |
|
33 | + * |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + public function canUseNotice(); |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * encrypts data |
|
41 | + * |
|
42 | + * @param string $text_to_encrypt - the text to be encrypted |
|
43 | + * @param string $encryption_key_identifier - name of the encryption key to use |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function encrypt($text_to_encrypt, $encryption_key_identifier = ''); |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * decrypts data |
|
51 | + * |
|
52 | + * @param string $encrypted_text - the text to be decrypted |
|
53 | + * @param string $encryption_key_identifier - name of the encryption key to use |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + public function decrypt($encrypted_text, $encryption_key_identifier = ''); |
|
57 | 57 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | */ |
121 | 121 | public function canUseNotice() |
122 | 122 | { |
123 | - if (! $this->openssl_installed) { |
|
123 | + if ( ! $this->openssl_installed) { |
|
124 | 124 | return esc_html__( |
125 | 125 | 'The PHP openssl server extension is required to use Openssl encryption. Please contact your hosting provider regarding this issue.', |
126 | 126 | 'event_espresso' |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | */ |
220 | 220 | protected function getHashAlgorithm() |
221 | 221 | { |
222 | - if (! $this->hash_algorithm) { |
|
222 | + if ( ! $this->hash_algorithm) { |
|
223 | 223 | // get installed hashing algorithms |
224 | 224 | $hash_algorithms = hash_algos(); |
225 | 225 | // filter array for "sha" algorithms |
@@ -17,266 +17,266 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class OpenSSL implements EncryptionMethodInterface |
19 | 19 | { |
20 | - /** |
|
21 | - * the default OPENSSL digest method to use |
|
22 | - */ |
|
23 | - const DEFAULT_DIGEST_METHOD = 'sha512'; |
|
24 | - |
|
25 | - /** |
|
26 | - * separates the encrypted text from the initialization vector |
|
27 | - */ |
|
28 | - const IV_DELIMITER = ':iv:'; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var Base64Encoder |
|
32 | - */ |
|
33 | - protected $base64_encoder; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var CipherMethod |
|
37 | - */ |
|
38 | - protected $cipher_method; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var array $digest_methods |
|
42 | - */ |
|
43 | - private $digest_methods = []; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var EncryptionKeyManagerInterface |
|
47 | - */ |
|
48 | - protected $encryption_key_manager; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var boolean |
|
52 | - */ |
|
53 | - private $openssl_installed; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var string |
|
57 | - */ |
|
58 | - private $min_php_version; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var string |
|
62 | - */ |
|
63 | - private $hash_algorithm; |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * To use custom a cipher method and/or encryption keys: |
|
68 | - * - extend this class |
|
69 | - * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
70 | - * - pass those to this constructor, like so: |
|
71 | - * |
|
72 | - * public function __construct(Base64Encoder $base64_encoder) { |
|
73 | - * parent::__construct( |
|
74 | - * $base64_encoder, |
|
75 | - * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME) |
|
76 | - * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME) |
|
77 | - * ); |
|
78 | - * } |
|
79 | - * |
|
80 | - * @param Base64Encoder $base64_encoder |
|
81 | - * @param CipherMethod $cipher_method |
|
82 | - * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
83 | - * @param string $min_php_version |
|
84 | - */ |
|
85 | - protected function __construct( |
|
86 | - Base64Encoder $base64_encoder, |
|
87 | - CipherMethod $cipher_method, |
|
88 | - EncryptionKeyManagerInterface $encryption_key_manager, |
|
89 | - $min_php_version |
|
90 | - ) { |
|
91 | - $this->base64_encoder = $base64_encoder; |
|
92 | - $this->cipher_method = $cipher_method; |
|
93 | - $this->encryption_key_manager = $encryption_key_manager; |
|
94 | - $this->min_php_version = $min_php_version; |
|
95 | - $this->openssl_installed = extension_loaded('openssl'); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * @return bool |
|
101 | - */ |
|
102 | - public function isCryptographicallySecure() |
|
103 | - { |
|
104 | - return true; |
|
105 | - } |
|
106 | - |
|
107 | - |
|
108 | - /** |
|
109 | - * @return bool |
|
110 | - */ |
|
111 | - public function canUse() |
|
112 | - { |
|
113 | - return $this->openssl_installed && version_compare(PHP_VERSION, $this->min_php_version, '>='); |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - public function canUseNotice() |
|
121 | - { |
|
122 | - if (! $this->openssl_installed) { |
|
123 | - return esc_html__( |
|
124 | - 'The PHP openssl server extension is required to use Openssl encryption. Please contact your hosting provider regarding this issue.', |
|
125 | - 'event_espresso' |
|
126 | - ); |
|
127 | - } |
|
128 | - if (version_compare(PHP_VERSION, $this->min_php_version, '<')) { |
|
129 | - return sprintf( |
|
130 | - esc_html__( |
|
131 | - 'PHP version %1$s or greater is required to use Openssl encryption. Please contact your hosting provider regarding this issue.', |
|
132 | - 'event_espresso' |
|
133 | - ), |
|
134 | - $this->min_php_version |
|
135 | - ); |
|
136 | - } |
|
137 | - return sprintf( |
|
138 | - esc_html__('OpenSSL v1 encryption using %1$s is available for use.', 'event_espresso'), |
|
139 | - OpenSSLv1::CIPHER_METHOD |
|
140 | - ); |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * Computes the digest hash value using the specified digest method. |
|
146 | - * If that digest method fails to produce a valid hash value, |
|
147 | - * then we'll grab the next digest method and recursively try again until something works. |
|
148 | - * |
|
149 | - * @param string $encryption_key |
|
150 | - * @param string $digest_method |
|
151 | - * @param bool $return_raw_data |
|
152 | - * @return string |
|
153 | - * @throws RuntimeException |
|
154 | - */ |
|
155 | - protected function getDigestHashValue( |
|
156 | - $encryption_key, |
|
157 | - $digest_method = OpenSSL::DEFAULT_DIGEST_METHOD, |
|
158 | - $return_raw_data = false |
|
159 | - ) { |
|
160 | - $digest_hash_value = openssl_digest($encryption_key, $digest_method, $return_raw_data); |
|
161 | - if ($digest_hash_value === false) { |
|
162 | - return $this->getDigestHashValue($this->getDigestMethod()); |
|
163 | - } |
|
164 | - return $digest_hash_value; |
|
165 | - } |
|
166 | - |
|
167 | - |
|
168 | - /** |
|
169 | - * Returns the NEXT element in the $digest_methods array. |
|
170 | - * If the $digest_methods array is empty, then we populate it |
|
171 | - * with the available values returned from openssl_get_md_methods(). |
|
172 | - * |
|
173 | - * @return string |
|
174 | - * @throws RuntimeException |
|
175 | - */ |
|
176 | - private function getDigestMethod() |
|
177 | - { |
|
178 | - $digest_method = prev($this->digest_methods); |
|
179 | - if (empty($this->digest_methods)) { |
|
180 | - $this->digest_methods = openssl_get_md_methods(); |
|
181 | - $digest_method = end($this->digest_methods); |
|
182 | - } |
|
183 | - if ($digest_method === false) { |
|
184 | - throw new RuntimeException( |
|
185 | - esc_html__( |
|
186 | - 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
187 | - 'event_espresso' |
|
188 | - ) |
|
189 | - ); |
|
190 | - } |
|
191 | - return $digest_method; |
|
192 | - } |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * @param string $encryption_key |
|
197 | - * @return int |
|
198 | - */ |
|
199 | - protected function calculateHashLength($encryption_key) |
|
200 | - { |
|
201 | - // get existing key length |
|
202 | - $prev_key_length = $this->encryption_key_manager->keyLength(); |
|
203 | - // set it to something HUGE |
|
204 | - $this->encryption_key_manager->setKeyLength(512); |
|
205 | - // generate a new weak key, which should just be a really long random string |
|
206 | - $test_text = $this->encryption_key_manager->generateEncryptionKey(false); |
|
207 | - // generate a hash using our test string and our real $encryption_key |
|
208 | - $hash = hash_hmac($this->getHashAlgorithm(), $test_text, $encryption_key, true); |
|
209 | - // reset key length back to original value |
|
210 | - $this->encryption_key_manager->setKeyLength($prev_key_length); |
|
211 | - // return the length of the hash |
|
212 | - return strlen($hash); |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * @return string |
|
218 | - */ |
|
219 | - protected function getHashAlgorithm() |
|
220 | - { |
|
221 | - if (! $this->hash_algorithm) { |
|
222 | - // get installed hashing algorithms |
|
223 | - $hash_algorithms = hash_algos(); |
|
224 | - // filter array for "sha" algorithms |
|
225 | - $hash_algorithms = preg_grep('/^sha\d{3}$/i', $hash_algorithms); |
|
226 | - // if no sha algorithms are installed, then just use md5 |
|
227 | - if (empty($hash_algorithms)) { |
|
228 | - $this->hash_algorithm = 'md5'; |
|
229 | - return $this->hash_algorithm; |
|
230 | - } |
|
231 | - // sort ascending using "natural ordering" |
|
232 | - sort($hash_algorithms, SORT_NATURAL); |
|
233 | - // return last item from array, which should be the strongest installed sha hash |
|
234 | - $this->hash_algorithm = array_pop($hash_algorithms); |
|
235 | - } |
|
236 | - return $this->hash_algorithm; |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * @param string $encrypted_text |
|
242 | - * @throws RuntimeException |
|
243 | - */ |
|
244 | - protected function validateEncryption($encrypted_text) |
|
245 | - { |
|
246 | - if ($encrypted_text === false) { |
|
247 | - throw new RuntimeException( |
|
248 | - sprintf( |
|
249 | - esc_html__('The following error occurred during OpenSSL encryption: %1$s', 'event_espresso'), |
|
250 | - $this->getOpenSslError() |
|
251 | - ) |
|
252 | - ); |
|
253 | - } |
|
254 | - } |
|
255 | - |
|
256 | - |
|
257 | - /** |
|
258 | - * @return false|string |
|
259 | - */ |
|
260 | - private function getOpenSslError() |
|
261 | - { |
|
262 | - $error = openssl_error_string(); |
|
263 | - return $error ?: esc_html__('Unknown Error', 'event_espresso'); |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * @param string $encrypted_text |
|
269 | - * @throws RuntimeException |
|
270 | - */ |
|
271 | - protected function validateDecryption($encrypted_text) |
|
272 | - { |
|
273 | - if ($encrypted_text === false) { |
|
274 | - throw new RuntimeException( |
|
275 | - sprintf( |
|
276 | - esc_html__('OpenSSL decryption failed for the following reason: %1$s', 'event_espresso'), |
|
277 | - $this->getOpenSslError() |
|
278 | - ) |
|
279 | - ); |
|
280 | - } |
|
281 | - } |
|
20 | + /** |
|
21 | + * the default OPENSSL digest method to use |
|
22 | + */ |
|
23 | + const DEFAULT_DIGEST_METHOD = 'sha512'; |
|
24 | + |
|
25 | + /** |
|
26 | + * separates the encrypted text from the initialization vector |
|
27 | + */ |
|
28 | + const IV_DELIMITER = ':iv:'; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var Base64Encoder |
|
32 | + */ |
|
33 | + protected $base64_encoder; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var CipherMethod |
|
37 | + */ |
|
38 | + protected $cipher_method; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var array $digest_methods |
|
42 | + */ |
|
43 | + private $digest_methods = []; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var EncryptionKeyManagerInterface |
|
47 | + */ |
|
48 | + protected $encryption_key_manager; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var boolean |
|
52 | + */ |
|
53 | + private $openssl_installed; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var string |
|
57 | + */ |
|
58 | + private $min_php_version; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var string |
|
62 | + */ |
|
63 | + private $hash_algorithm; |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * To use custom a cipher method and/or encryption keys: |
|
68 | + * - extend this class |
|
69 | + * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
70 | + * - pass those to this constructor, like so: |
|
71 | + * |
|
72 | + * public function __construct(Base64Encoder $base64_encoder) { |
|
73 | + * parent::__construct( |
|
74 | + * $base64_encoder, |
|
75 | + * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME) |
|
76 | + * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME) |
|
77 | + * ); |
|
78 | + * } |
|
79 | + * |
|
80 | + * @param Base64Encoder $base64_encoder |
|
81 | + * @param CipherMethod $cipher_method |
|
82 | + * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
83 | + * @param string $min_php_version |
|
84 | + */ |
|
85 | + protected function __construct( |
|
86 | + Base64Encoder $base64_encoder, |
|
87 | + CipherMethod $cipher_method, |
|
88 | + EncryptionKeyManagerInterface $encryption_key_manager, |
|
89 | + $min_php_version |
|
90 | + ) { |
|
91 | + $this->base64_encoder = $base64_encoder; |
|
92 | + $this->cipher_method = $cipher_method; |
|
93 | + $this->encryption_key_manager = $encryption_key_manager; |
|
94 | + $this->min_php_version = $min_php_version; |
|
95 | + $this->openssl_installed = extension_loaded('openssl'); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * @return bool |
|
101 | + */ |
|
102 | + public function isCryptographicallySecure() |
|
103 | + { |
|
104 | + return true; |
|
105 | + } |
|
106 | + |
|
107 | + |
|
108 | + /** |
|
109 | + * @return bool |
|
110 | + */ |
|
111 | + public function canUse() |
|
112 | + { |
|
113 | + return $this->openssl_installed && version_compare(PHP_VERSION, $this->min_php_version, '>='); |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + public function canUseNotice() |
|
121 | + { |
|
122 | + if (! $this->openssl_installed) { |
|
123 | + return esc_html__( |
|
124 | + 'The PHP openssl server extension is required to use Openssl encryption. Please contact your hosting provider regarding this issue.', |
|
125 | + 'event_espresso' |
|
126 | + ); |
|
127 | + } |
|
128 | + if (version_compare(PHP_VERSION, $this->min_php_version, '<')) { |
|
129 | + return sprintf( |
|
130 | + esc_html__( |
|
131 | + 'PHP version %1$s or greater is required to use Openssl encryption. Please contact your hosting provider regarding this issue.', |
|
132 | + 'event_espresso' |
|
133 | + ), |
|
134 | + $this->min_php_version |
|
135 | + ); |
|
136 | + } |
|
137 | + return sprintf( |
|
138 | + esc_html__('OpenSSL v1 encryption using %1$s is available for use.', 'event_espresso'), |
|
139 | + OpenSSLv1::CIPHER_METHOD |
|
140 | + ); |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * Computes the digest hash value using the specified digest method. |
|
146 | + * If that digest method fails to produce a valid hash value, |
|
147 | + * then we'll grab the next digest method and recursively try again until something works. |
|
148 | + * |
|
149 | + * @param string $encryption_key |
|
150 | + * @param string $digest_method |
|
151 | + * @param bool $return_raw_data |
|
152 | + * @return string |
|
153 | + * @throws RuntimeException |
|
154 | + */ |
|
155 | + protected function getDigestHashValue( |
|
156 | + $encryption_key, |
|
157 | + $digest_method = OpenSSL::DEFAULT_DIGEST_METHOD, |
|
158 | + $return_raw_data = false |
|
159 | + ) { |
|
160 | + $digest_hash_value = openssl_digest($encryption_key, $digest_method, $return_raw_data); |
|
161 | + if ($digest_hash_value === false) { |
|
162 | + return $this->getDigestHashValue($this->getDigestMethod()); |
|
163 | + } |
|
164 | + return $digest_hash_value; |
|
165 | + } |
|
166 | + |
|
167 | + |
|
168 | + /** |
|
169 | + * Returns the NEXT element in the $digest_methods array. |
|
170 | + * If the $digest_methods array is empty, then we populate it |
|
171 | + * with the available values returned from openssl_get_md_methods(). |
|
172 | + * |
|
173 | + * @return string |
|
174 | + * @throws RuntimeException |
|
175 | + */ |
|
176 | + private function getDigestMethod() |
|
177 | + { |
|
178 | + $digest_method = prev($this->digest_methods); |
|
179 | + if (empty($this->digest_methods)) { |
|
180 | + $this->digest_methods = openssl_get_md_methods(); |
|
181 | + $digest_method = end($this->digest_methods); |
|
182 | + } |
|
183 | + if ($digest_method === false) { |
|
184 | + throw new RuntimeException( |
|
185 | + esc_html__( |
|
186 | + 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
187 | + 'event_espresso' |
|
188 | + ) |
|
189 | + ); |
|
190 | + } |
|
191 | + return $digest_method; |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * @param string $encryption_key |
|
197 | + * @return int |
|
198 | + */ |
|
199 | + protected function calculateHashLength($encryption_key) |
|
200 | + { |
|
201 | + // get existing key length |
|
202 | + $prev_key_length = $this->encryption_key_manager->keyLength(); |
|
203 | + // set it to something HUGE |
|
204 | + $this->encryption_key_manager->setKeyLength(512); |
|
205 | + // generate a new weak key, which should just be a really long random string |
|
206 | + $test_text = $this->encryption_key_manager->generateEncryptionKey(false); |
|
207 | + // generate a hash using our test string and our real $encryption_key |
|
208 | + $hash = hash_hmac($this->getHashAlgorithm(), $test_text, $encryption_key, true); |
|
209 | + // reset key length back to original value |
|
210 | + $this->encryption_key_manager->setKeyLength($prev_key_length); |
|
211 | + // return the length of the hash |
|
212 | + return strlen($hash); |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * @return string |
|
218 | + */ |
|
219 | + protected function getHashAlgorithm() |
|
220 | + { |
|
221 | + if (! $this->hash_algorithm) { |
|
222 | + // get installed hashing algorithms |
|
223 | + $hash_algorithms = hash_algos(); |
|
224 | + // filter array for "sha" algorithms |
|
225 | + $hash_algorithms = preg_grep('/^sha\d{3}$/i', $hash_algorithms); |
|
226 | + // if no sha algorithms are installed, then just use md5 |
|
227 | + if (empty($hash_algorithms)) { |
|
228 | + $this->hash_algorithm = 'md5'; |
|
229 | + return $this->hash_algorithm; |
|
230 | + } |
|
231 | + // sort ascending using "natural ordering" |
|
232 | + sort($hash_algorithms, SORT_NATURAL); |
|
233 | + // return last item from array, which should be the strongest installed sha hash |
|
234 | + $this->hash_algorithm = array_pop($hash_algorithms); |
|
235 | + } |
|
236 | + return $this->hash_algorithm; |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * @param string $encrypted_text |
|
242 | + * @throws RuntimeException |
|
243 | + */ |
|
244 | + protected function validateEncryption($encrypted_text) |
|
245 | + { |
|
246 | + if ($encrypted_text === false) { |
|
247 | + throw new RuntimeException( |
|
248 | + sprintf( |
|
249 | + esc_html__('The following error occurred during OpenSSL encryption: %1$s', 'event_espresso'), |
|
250 | + $this->getOpenSslError() |
|
251 | + ) |
|
252 | + ); |
|
253 | + } |
|
254 | + } |
|
255 | + |
|
256 | + |
|
257 | + /** |
|
258 | + * @return false|string |
|
259 | + */ |
|
260 | + private function getOpenSslError() |
|
261 | + { |
|
262 | + $error = openssl_error_string(); |
|
263 | + return $error ?: esc_html__('Unknown Error', 'event_espresso'); |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * @param string $encrypted_text |
|
269 | + * @throws RuntimeException |
|
270 | + */ |
|
271 | + protected function validateDecryption($encrypted_text) |
|
272 | + { |
|
273 | + if ($encrypted_text === false) { |
|
274 | + throw new RuntimeException( |
|
275 | + sprintf( |
|
276 | + esc_html__('OpenSSL decryption failed for the following reason: %1$s', 'event_espresso'), |
|
277 | + $this->getOpenSslError() |
|
278 | + ) |
|
279 | + ); |
|
280 | + } |
|
281 | + } |
|
282 | 282 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | if ($child_node instanceof PrimaryJsonDataNode || $depth > 512) { |
79 | 79 | continue; |
80 | 80 | } |
81 | - $data[ $child_node_name ] = $child_node instanceof JsonDataNode |
|
81 | + $data[$child_node_name] = $child_node instanceof JsonDataNode |
|
82 | 82 | // feed data node back into this function |
83 | 83 | ? $this->initializeDataNodes($child_node, $depth) |
84 | 84 | // or assign data directly |
@@ -94,15 +94,15 @@ discard block |
||
94 | 94 | */ |
95 | 95 | public function printDataNode() |
96 | 96 | { |
97 | - if (!$this->primary_data_node instanceof PrimaryJsonDataNode) { |
|
97 | + if ( ! $this->primary_data_node instanceof PrimaryJsonDataNode) { |
|
98 | 98 | return; |
99 | 99 | } |
100 | 100 | // validate that the domain, node name, and target script are set |
101 | 101 | $domain = $this->primary_data_node->domain(); |
102 | 102 | $node_name = $this->primary_data_node->nodeName(); |
103 | - $data_valid = $this->validator->validateCriticalProperty($domain, 'domain route', false) |
|
103 | + $data_valid = $this->validator->validateCriticalProperty($domain, 'domain route', false) |
|
104 | 104 | && $this->validator->validateCriticalProperty($node_name, 'node name', false); |
105 | - if (! $data_valid) { |
|
105 | + if ( ! $data_valid) { |
|
106 | 106 | return; |
107 | 107 | } |
108 | 108 | // initialize and parse data from primary data node |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | $this->setPrimaryDataNode($data_node); |
133 | 133 | } |
134 | 134 | // and don't allow other nodes to be set until a primary is set |
135 | - if (! $this->primary_data_node instanceof PrimaryJsonDataNode) { |
|
135 | + if ( ! $this->primary_data_node instanceof PrimaryJsonDataNode) { |
|
136 | 136 | throw new DomainException( |
137 | 137 | esc_html__( |
138 | 138 | 'A PrimaryJsonDataNode needs to be set before data nodes can be added.', |
@@ -17,127 +17,127 @@ |
||
17 | 17 | */ |
18 | 18 | class JsonDataNodeHandler |
19 | 19 | { |
20 | - /** |
|
21 | - * @var PrimaryJsonDataNode $primary_data_node |
|
22 | - */ |
|
23 | - private $primary_data_node; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var JsonDataNodeValidator $validator |
|
27 | - */ |
|
28 | - private $validator; |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * @param JsonDataNodeValidator $validator |
|
33 | - */ |
|
34 | - public function __construct(JsonDataNodeValidator $validator) |
|
35 | - { |
|
36 | - $this->validator = $validator; |
|
37 | - } |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * @param JsonDataNode $data_node |
|
42 | - * @throws DomainException |
|
43 | - */ |
|
44 | - public function addDataNode(JsonDataNode $data_node) |
|
45 | - { |
|
46 | - if ($data_node->isNotInitialized()) { |
|
47 | - $this->validatePrimaryDataNode($data_node); |
|
48 | - $this->primary_data_node->addDataNode($data_node); |
|
49 | - } |
|
50 | - } |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * @param PrimaryJsonDataNode $primary_data_node |
|
55 | - */ |
|
56 | - public function setPrimaryDataNode(PrimaryJsonDataNode $primary_data_node) |
|
57 | - { |
|
58 | - $this->primary_data_node = $primary_data_node; |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * @param JsonDataNode $data_node |
|
64 | - * @param int $depth |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - private function initializeDataNodes(JsonDataNode $data_node, int $depth = 0): array |
|
68 | - { |
|
69 | - $depth++; |
|
70 | - $data = []; |
|
71 | - // initialize the data node if not done already |
|
72 | - if ($data_node->isNotInitialized()) { |
|
73 | - $data_node->initialize(); |
|
74 | - // loop thru the data node's data array |
|
75 | - foreach ($data_node->data(true) as $child_node_name => $child_node) { |
|
76 | - // don't parse node if it's the primary, OR if depth has exceeded wp_json_encode() limit |
|
77 | - if ($child_node instanceof PrimaryJsonDataNode || $depth > 512) { |
|
78 | - continue; |
|
79 | - } |
|
80 | - $data[ $child_node_name ] = $child_node instanceof JsonDataNode |
|
81 | - // feed data node back into this function |
|
82 | - ? $this->initializeDataNodes($child_node, $depth) |
|
83 | - // or assign data directly |
|
84 | - : $child_node; |
|
85 | - } |
|
86 | - } |
|
87 | - return $data; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * @throws DomainException |
|
93 | - */ |
|
94 | - public function printDataNode() |
|
95 | - { |
|
96 | - if (!$this->primary_data_node instanceof PrimaryJsonDataNode) { |
|
97 | - return; |
|
98 | - } |
|
99 | - // validate that the domain, node name, and target script are set |
|
100 | - $domain = $this->primary_data_node->domain(); |
|
101 | - $node_name = $this->primary_data_node->nodeName(); |
|
102 | - $data_valid = $this->validator->validateCriticalProperty($domain, 'domain route', false) |
|
103 | - && $this->validator->validateCriticalProperty($node_name, 'node name', false); |
|
104 | - if (! $data_valid) { |
|
105 | - return; |
|
106 | - } |
|
107 | - // initialize and parse data from primary data node |
|
108 | - $data = $this->initializeDataNodes($this->primary_data_node); |
|
109 | - // this prepends the current domain "use case" to the front of the array |
|
110 | - $data = ['domain' => $domain] + $data; |
|
111 | - // add legacy i18n strings |
|
112 | - $data['eei18n'] = EE_Registry::sanitize_i18n_js_strings(); |
|
113 | - // and finally, print the JSON encoded data to the DOM |
|
114 | - printf( |
|
115 | - "<script type='text/javascript' id='%s'>\nvar %s = %s\n</script>\n", |
|
116 | - $node_name, |
|
117 | - $node_name, |
|
118 | - json_encode($data) |
|
119 | - ); |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * @param JsonDataNode $data_node |
|
125 | - * @throws DomainException |
|
126 | - */ |
|
127 | - private function validatePrimaryDataNode(JsonDataNode $data_node) |
|
128 | - { |
|
129 | - // set primary data node if that's what the incoming node is |
|
130 | - if ($data_node instanceof PrimaryJsonDataNode) { |
|
131 | - $this->setPrimaryDataNode($data_node); |
|
132 | - } |
|
133 | - // and don't allow other nodes to be set until a primary is set |
|
134 | - if (! $this->primary_data_node instanceof PrimaryJsonDataNode) { |
|
135 | - throw new DomainException( |
|
136 | - esc_html__( |
|
137 | - 'A PrimaryJsonDataNode needs to be set before data nodes can be added.', |
|
138 | - 'event_espresso' |
|
139 | - ) |
|
140 | - ); |
|
141 | - } |
|
142 | - } |
|
20 | + /** |
|
21 | + * @var PrimaryJsonDataNode $primary_data_node |
|
22 | + */ |
|
23 | + private $primary_data_node; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var JsonDataNodeValidator $validator |
|
27 | + */ |
|
28 | + private $validator; |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * @param JsonDataNodeValidator $validator |
|
33 | + */ |
|
34 | + public function __construct(JsonDataNodeValidator $validator) |
|
35 | + { |
|
36 | + $this->validator = $validator; |
|
37 | + } |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * @param JsonDataNode $data_node |
|
42 | + * @throws DomainException |
|
43 | + */ |
|
44 | + public function addDataNode(JsonDataNode $data_node) |
|
45 | + { |
|
46 | + if ($data_node->isNotInitialized()) { |
|
47 | + $this->validatePrimaryDataNode($data_node); |
|
48 | + $this->primary_data_node->addDataNode($data_node); |
|
49 | + } |
|
50 | + } |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * @param PrimaryJsonDataNode $primary_data_node |
|
55 | + */ |
|
56 | + public function setPrimaryDataNode(PrimaryJsonDataNode $primary_data_node) |
|
57 | + { |
|
58 | + $this->primary_data_node = $primary_data_node; |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * @param JsonDataNode $data_node |
|
64 | + * @param int $depth |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + private function initializeDataNodes(JsonDataNode $data_node, int $depth = 0): array |
|
68 | + { |
|
69 | + $depth++; |
|
70 | + $data = []; |
|
71 | + // initialize the data node if not done already |
|
72 | + if ($data_node->isNotInitialized()) { |
|
73 | + $data_node->initialize(); |
|
74 | + // loop thru the data node's data array |
|
75 | + foreach ($data_node->data(true) as $child_node_name => $child_node) { |
|
76 | + // don't parse node if it's the primary, OR if depth has exceeded wp_json_encode() limit |
|
77 | + if ($child_node instanceof PrimaryJsonDataNode || $depth > 512) { |
|
78 | + continue; |
|
79 | + } |
|
80 | + $data[ $child_node_name ] = $child_node instanceof JsonDataNode |
|
81 | + // feed data node back into this function |
|
82 | + ? $this->initializeDataNodes($child_node, $depth) |
|
83 | + // or assign data directly |
|
84 | + : $child_node; |
|
85 | + } |
|
86 | + } |
|
87 | + return $data; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * @throws DomainException |
|
93 | + */ |
|
94 | + public function printDataNode() |
|
95 | + { |
|
96 | + if (!$this->primary_data_node instanceof PrimaryJsonDataNode) { |
|
97 | + return; |
|
98 | + } |
|
99 | + // validate that the domain, node name, and target script are set |
|
100 | + $domain = $this->primary_data_node->domain(); |
|
101 | + $node_name = $this->primary_data_node->nodeName(); |
|
102 | + $data_valid = $this->validator->validateCriticalProperty($domain, 'domain route', false) |
|
103 | + && $this->validator->validateCriticalProperty($node_name, 'node name', false); |
|
104 | + if (! $data_valid) { |
|
105 | + return; |
|
106 | + } |
|
107 | + // initialize and parse data from primary data node |
|
108 | + $data = $this->initializeDataNodes($this->primary_data_node); |
|
109 | + // this prepends the current domain "use case" to the front of the array |
|
110 | + $data = ['domain' => $domain] + $data; |
|
111 | + // add legacy i18n strings |
|
112 | + $data['eei18n'] = EE_Registry::sanitize_i18n_js_strings(); |
|
113 | + // and finally, print the JSON encoded data to the DOM |
|
114 | + printf( |
|
115 | + "<script type='text/javascript' id='%s'>\nvar %s = %s\n</script>\n", |
|
116 | + $node_name, |
|
117 | + $node_name, |
|
118 | + json_encode($data) |
|
119 | + ); |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * @param JsonDataNode $data_node |
|
125 | + * @throws DomainException |
|
126 | + */ |
|
127 | + private function validatePrimaryDataNode(JsonDataNode $data_node) |
|
128 | + { |
|
129 | + // set primary data node if that's what the incoming node is |
|
130 | + if ($data_node instanceof PrimaryJsonDataNode) { |
|
131 | + $this->setPrimaryDataNode($data_node); |
|
132 | + } |
|
133 | + // and don't allow other nodes to be set until a primary is set |
|
134 | + if (! $this->primary_data_node instanceof PrimaryJsonDataNode) { |
|
135 | + throw new DomainException( |
|
136 | + esc_html__( |
|
137 | + 'A PrimaryJsonDataNode needs to be set before data nodes can be added.', |
|
138 | + 'event_espresso' |
|
139 | + ) |
|
140 | + ); |
|
141 | + } |
|
142 | + } |
|
143 | 143 | } |
@@ -122,7 +122,7 @@ |
||
122 | 122 | // hash the raw encrypted text |
123 | 123 | $hmac = hash_hmac($this->getHashAlgorithm(), $encrypted_text, $key, true); |
124 | 124 | // concatenate everything into one big string and encode it again |
125 | - return $this->base64_encoder->encodeString($iv . $hmac . $encrypted_text); |
|
125 | + return $this->base64_encoder->encodeString($iv.$hmac.$encrypted_text); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 |
@@ -17,155 +17,155 @@ |
||
17 | 17 | */ |
18 | 18 | class OpenSSLv1 extends OpenSSL |
19 | 19 | { |
20 | - /** |
|
21 | - * name used for a default encryption key in case no others are set |
|
22 | - */ |
|
23 | - const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v1_key'; |
|
20 | + /** |
|
21 | + * name used for a default encryption key in case no others are set |
|
22 | + */ |
|
23 | + const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v1_key'; |
|
24 | 24 | |
25 | - /** |
|
26 | - * name used for saving encryption keys to the wp_options table |
|
27 | - */ |
|
28 | - const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v1_encryption_keys'; |
|
25 | + /** |
|
26 | + * name used for saving encryption keys to the wp_options table |
|
27 | + */ |
|
28 | + const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v1_encryption_keys'; |
|
29 | 29 | |
30 | - /** |
|
31 | - * the OPENSSL cipher method used |
|
32 | - */ |
|
33 | - const CIPHER_METHOD = 'aes-128-cbc'; |
|
30 | + /** |
|
31 | + * the OPENSSL cipher method used |
|
32 | + */ |
|
33 | + const CIPHER_METHOD = 'aes-128-cbc'; |
|
34 | 34 | |
35 | - /** |
|
36 | - * WP "options_name" used to store a verified available cipher method |
|
37 | - */ |
|
38 | - const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v1_cipher_method'; |
|
35 | + /** |
|
36 | + * WP "options_name" used to store a verified available cipher method |
|
37 | + */ |
|
38 | + const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v1_cipher_method'; |
|
39 | 39 | |
40 | 40 | |
41 | - /** |
|
42 | - * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
43 | - * - extend this class |
|
44 | - * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
45 | - * - pass those to this constructor, like so: |
|
46 | - * |
|
47 | - * public function __construct(Base64Encoder $base64_encoder) { |
|
48 | - * parent::__construct( |
|
49 | - * $base64_encoder, |
|
50 | - * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
51 | - * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
52 | - * '7.1.0' |
|
53 | - * ); |
|
54 | - * } |
|
55 | - * |
|
56 | - * @param Base64Encoder $base64_encoder |
|
57 | - * @param CipherMethod|null $cipher_method |
|
58 | - * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
59 | - * @param string $min_php_version defaults to 5.3.0 (when openssl added) |
|
60 | - */ |
|
61 | - public function __construct( |
|
62 | - Base64Encoder $base64_encoder, |
|
63 | - CipherMethod $cipher_method = null, |
|
64 | - EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
65 | - $min_php_version = '5.3.0' |
|
66 | - ) { |
|
67 | - parent::__construct( |
|
68 | - $base64_encoder, |
|
69 | - $cipher_method instanceof CipherMethod |
|
70 | - ? $cipher_method |
|
71 | - : new CipherMethod( |
|
72 | - OpenSSLv1::CIPHER_METHOD, |
|
73 | - OpenSSLv1::CIPHER_METHOD_OPTION_NAME |
|
74 | - ), |
|
75 | - $encryption_key_manager instanceof EncryptionKeyManager |
|
76 | - ? $encryption_key_manager |
|
77 | - : new EncryptionKeyManager( |
|
78 | - $base64_encoder, |
|
79 | - OpenSSLv1::DEFAULT_ENCRYPTION_KEY_ID, |
|
80 | - OpenSSLv1::ENCRYPTION_KEYS_OPTION_NAME |
|
81 | - ), |
|
82 | - $min_php_version |
|
83 | - ); |
|
84 | - } |
|
41 | + /** |
|
42 | + * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
43 | + * - extend this class |
|
44 | + * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
45 | + * - pass those to this constructor, like so: |
|
46 | + * |
|
47 | + * public function __construct(Base64Encoder $base64_encoder) { |
|
48 | + * parent::__construct( |
|
49 | + * $base64_encoder, |
|
50 | + * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
51 | + * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
52 | + * '7.1.0' |
|
53 | + * ); |
|
54 | + * } |
|
55 | + * |
|
56 | + * @param Base64Encoder $base64_encoder |
|
57 | + * @param CipherMethod|null $cipher_method |
|
58 | + * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
59 | + * @param string $min_php_version defaults to 5.3.0 (when openssl added) |
|
60 | + */ |
|
61 | + public function __construct( |
|
62 | + Base64Encoder $base64_encoder, |
|
63 | + CipherMethod $cipher_method = null, |
|
64 | + EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
65 | + $min_php_version = '5.3.0' |
|
66 | + ) { |
|
67 | + parent::__construct( |
|
68 | + $base64_encoder, |
|
69 | + $cipher_method instanceof CipherMethod |
|
70 | + ? $cipher_method |
|
71 | + : new CipherMethod( |
|
72 | + OpenSSLv1::CIPHER_METHOD, |
|
73 | + OpenSSLv1::CIPHER_METHOD_OPTION_NAME |
|
74 | + ), |
|
75 | + $encryption_key_manager instanceof EncryptionKeyManager |
|
76 | + ? $encryption_key_manager |
|
77 | + : new EncryptionKeyManager( |
|
78 | + $base64_encoder, |
|
79 | + OpenSSLv1::DEFAULT_ENCRYPTION_KEY_ID, |
|
80 | + OpenSSLv1::ENCRYPTION_KEYS_OPTION_NAME |
|
81 | + ), |
|
82 | + $min_php_version |
|
83 | + ); |
|
84 | + } |
|
85 | 85 | |
86 | 86 | |
87 | - /** |
|
88 | - * encrypts data |
|
89 | - * |
|
90 | - * @param string $text_to_encrypt - the text to be encrypted |
|
91 | - * @param string $encryption_key_identifier - cryptographically secure passphrase. will generate if necessary |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function encrypt($text_to_encrypt, $encryption_key_identifier = '') |
|
95 | - { |
|
96 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
97 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
98 | - // get initialization vector size |
|
99 | - $iv_length = openssl_cipher_iv_length($cipher_method); |
|
100 | - // generate initialization vector. |
|
101 | - // The second parameter ("crypto_strong") is passed by reference, |
|
102 | - // and is used to determines if the algorithm used was "cryptographically strong" |
|
103 | - // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
104 | - $iv = openssl_random_pseudo_bytes($iv_length, $is_strong); |
|
105 | - if ($iv === false || $is_strong === false) { |
|
106 | - throw new RuntimeException( |
|
107 | - esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
108 | - ); |
|
109 | - } |
|
110 | - $key = $this->getDigestHashValue($encryption_key); |
|
111 | - // encrypt it |
|
112 | - $encrypted_text = openssl_encrypt( |
|
113 | - $this->base64_encoder->encodeString($text_to_encrypt), // encode to remove special characters |
|
114 | - $cipher_method, |
|
115 | - $key, |
|
116 | - 0, |
|
117 | - $iv |
|
118 | - ); |
|
119 | - $this->validateEncryption($encrypted_text); |
|
120 | - $encrypted_text = trim($encrypted_text); |
|
121 | - // hash the raw encrypted text |
|
122 | - $hmac = hash_hmac($this->getHashAlgorithm(), $encrypted_text, $key, true); |
|
123 | - // concatenate everything into one big string and encode it again |
|
124 | - return $this->base64_encoder->encodeString($iv . $hmac . $encrypted_text); |
|
125 | - } |
|
87 | + /** |
|
88 | + * encrypts data |
|
89 | + * |
|
90 | + * @param string $text_to_encrypt - the text to be encrypted |
|
91 | + * @param string $encryption_key_identifier - cryptographically secure passphrase. will generate if necessary |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function encrypt($text_to_encrypt, $encryption_key_identifier = '') |
|
95 | + { |
|
96 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
97 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
98 | + // get initialization vector size |
|
99 | + $iv_length = openssl_cipher_iv_length($cipher_method); |
|
100 | + // generate initialization vector. |
|
101 | + // The second parameter ("crypto_strong") is passed by reference, |
|
102 | + // and is used to determines if the algorithm used was "cryptographically strong" |
|
103 | + // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
104 | + $iv = openssl_random_pseudo_bytes($iv_length, $is_strong); |
|
105 | + if ($iv === false || $is_strong === false) { |
|
106 | + throw new RuntimeException( |
|
107 | + esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
108 | + ); |
|
109 | + } |
|
110 | + $key = $this->getDigestHashValue($encryption_key); |
|
111 | + // encrypt it |
|
112 | + $encrypted_text = openssl_encrypt( |
|
113 | + $this->base64_encoder->encodeString($text_to_encrypt), // encode to remove special characters |
|
114 | + $cipher_method, |
|
115 | + $key, |
|
116 | + 0, |
|
117 | + $iv |
|
118 | + ); |
|
119 | + $this->validateEncryption($encrypted_text); |
|
120 | + $encrypted_text = trim($encrypted_text); |
|
121 | + // hash the raw encrypted text |
|
122 | + $hmac = hash_hmac($this->getHashAlgorithm(), $encrypted_text, $key, true); |
|
123 | + // concatenate everything into one big string and encode it again |
|
124 | + return $this->base64_encoder->encodeString($iv . $hmac . $encrypted_text); |
|
125 | + } |
|
126 | 126 | |
127 | 127 | |
128 | - /** |
|
129 | - * decrypts data |
|
130 | - * |
|
131 | - * @param string $encrypted_text - the text to be decrypted |
|
132 | - * @param string $encryption_key_identifier - cryptographically secure passphrase. will use default if necessary |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - public function decrypt($encrypted_text, $encryption_key_identifier = '') |
|
136 | - { |
|
137 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
138 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
139 | - $key = $this->getDigestHashValue($encryption_key); |
|
140 | - // decode our concatenated string |
|
141 | - $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
142 | - // get the string lengths used for the hash and iv |
|
143 | - $hash_length = $this->calculateHashLength($encryption_key); |
|
144 | - $iv_length = openssl_cipher_iv_length($cipher_method); |
|
145 | - // use the above lengths to snip the required values from the decoded string |
|
146 | - $iv = substr($encrypted_text, 0, $iv_length); |
|
147 | - $hmac = substr($encrypted_text, $iv_length, $hash_length); |
|
148 | - $encrypted_text_raw = substr($encrypted_text, $iv_length + $hash_length); |
|
149 | - // rehash the original raw encrypted text |
|
150 | - $rehash_mac = hash_hmac($this->getHashAlgorithm(), $encrypted_text_raw, $key, true); |
|
151 | - // timing attack safe comparison to determine if anything has changed |
|
152 | - if (hash_equals($hmac, $rehash_mac)) { |
|
153 | - // looks good, decrypt it, trim it, and return it |
|
154 | - $decrypted_text = openssl_decrypt( |
|
155 | - $encrypted_text_raw, |
|
156 | - $this->cipher_method->getCipherMethod(), |
|
157 | - $key, |
|
158 | - 0, |
|
159 | - $iv |
|
160 | - ); |
|
161 | - $this->validateDecryption($decrypted_text); |
|
162 | - return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
163 | - } |
|
164 | - throw new RuntimeException( |
|
165 | - esc_html__( |
|
166 | - 'Decryption failed because a hash comparison of the original text and the decrypted text was not the same, meaning something in the system or the encrypted data has changed.', |
|
167 | - 'event_espresso' |
|
168 | - ) |
|
169 | - ); |
|
170 | - } |
|
128 | + /** |
|
129 | + * decrypts data |
|
130 | + * |
|
131 | + * @param string $encrypted_text - the text to be decrypted |
|
132 | + * @param string $encryption_key_identifier - cryptographically secure passphrase. will use default if necessary |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + public function decrypt($encrypted_text, $encryption_key_identifier = '') |
|
136 | + { |
|
137 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
138 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
139 | + $key = $this->getDigestHashValue($encryption_key); |
|
140 | + // decode our concatenated string |
|
141 | + $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
142 | + // get the string lengths used for the hash and iv |
|
143 | + $hash_length = $this->calculateHashLength($encryption_key); |
|
144 | + $iv_length = openssl_cipher_iv_length($cipher_method); |
|
145 | + // use the above lengths to snip the required values from the decoded string |
|
146 | + $iv = substr($encrypted_text, 0, $iv_length); |
|
147 | + $hmac = substr($encrypted_text, $iv_length, $hash_length); |
|
148 | + $encrypted_text_raw = substr($encrypted_text, $iv_length + $hash_length); |
|
149 | + // rehash the original raw encrypted text |
|
150 | + $rehash_mac = hash_hmac($this->getHashAlgorithm(), $encrypted_text_raw, $key, true); |
|
151 | + // timing attack safe comparison to determine if anything has changed |
|
152 | + if (hash_equals($hmac, $rehash_mac)) { |
|
153 | + // looks good, decrypt it, trim it, and return it |
|
154 | + $decrypted_text = openssl_decrypt( |
|
155 | + $encrypted_text_raw, |
|
156 | + $this->cipher_method->getCipherMethod(), |
|
157 | + $key, |
|
158 | + 0, |
|
159 | + $iv |
|
160 | + ); |
|
161 | + $this->validateDecryption($decrypted_text); |
|
162 | + return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
163 | + } |
|
164 | + throw new RuntimeException( |
|
165 | + esc_html__( |
|
166 | + 'Decryption failed because a hash comparison of the original text and the decrypted text was not the same, meaning something in the system or the encrypted data has changed.', |
|
167 | + 'event_espresso' |
|
168 | + ) |
|
169 | + ); |
|
170 | + } |
|
171 | 171 | } |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | ); |
144 | 144 | $this->validateEncryption($encrypted_text); |
145 | 145 | // concatenate everything into one big string |
146 | - return $iv . $tag . $encrypted_text; |
|
146 | + return $iv.$tag.$encrypted_text; |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | ); |
166 | 166 | $this->validateEncryption($encrypted_text); |
167 | 167 | // prepend the initialization vector |
168 | - return $iv . $encrypted_text; |
|
168 | + return $iv.$encrypted_text; |
|
169 | 169 | } |
170 | 170 | |
171 | 171 |
@@ -23,221 +23,221 @@ |
||
23 | 23 | */ |
24 | 24 | class OpenSSLv2 extends OpenSSL |
25 | 25 | { |
26 | - /** |
|
27 | - * name used for a default encryption key in case no others are set |
|
28 | - */ |
|
29 | - const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v2_key'; |
|
30 | - |
|
31 | - /** |
|
32 | - * name used for saving encryption keys to the wp_options table |
|
33 | - */ |
|
34 | - const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v2_encryption_keys'; |
|
35 | - |
|
36 | - /** |
|
37 | - * the OPENSSL cipher method used |
|
38 | - */ |
|
39 | - const CIPHER_METHOD = 'aes-256-gcm'; |
|
40 | - |
|
41 | - /** |
|
42 | - * WP "options_name" used to store a verified available cipher method |
|
43 | - */ |
|
44 | - const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v2_cipher_method'; |
|
45 | - |
|
46 | - /** |
|
47 | - * The length of the authentication tag. Its value can be between 4 and 16 for GCM mode. |
|
48 | - */ |
|
49 | - const AUTH_TAG_LENGTH = 16; |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
54 | - * - extend this class |
|
55 | - * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
56 | - * - pass those to this constructor, like so: |
|
57 | - * |
|
58 | - * public function __construct(Base64Encoder $base64_encoder) { |
|
59 | - * parent::__construct( |
|
60 | - * $base64_encoder, |
|
61 | - * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
62 | - * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
63 | - * '7.1.0' |
|
64 | - * ); |
|
65 | - * } |
|
66 | - * |
|
67 | - * @param Base64Encoder $base64_encoder |
|
68 | - * @param CipherMethod|null $cipher_method |
|
69 | - * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
70 | - * @param string $min_php_version defaults to 7.1.0 |
|
71 | - * (when openssl auth tag and random_bytes() were added) |
|
72 | - */ |
|
73 | - public function __construct( |
|
74 | - Base64Encoder $base64_encoder, |
|
75 | - CipherMethod $cipher_method = null, |
|
76 | - EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
77 | - $min_php_version = '7.1.0' |
|
78 | - ) { |
|
79 | - parent::__construct( |
|
80 | - $base64_encoder, |
|
81 | - $cipher_method instanceof CipherMethod |
|
82 | - ? $cipher_method |
|
83 | - : new CipherMethod( |
|
84 | - OpenSSLv2::CIPHER_METHOD, |
|
85 | - OpenSSLv2::CIPHER_METHOD_OPTION_NAME |
|
86 | - ), |
|
87 | - $encryption_key_manager instanceof EncryptionKeyManager |
|
88 | - ? $encryption_key_manager |
|
89 | - : new EncryptionKeyManager( |
|
90 | - $base64_encoder, |
|
91 | - OpenSSLv2::DEFAULT_ENCRYPTION_KEY_ID, |
|
92 | - OpenSSLv2::ENCRYPTION_KEYS_OPTION_NAME |
|
93 | - ), |
|
94 | - $min_php_version |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * encrypts data |
|
101 | - * |
|
102 | - * @param string $text_to_encrypt - the text to be encrypted |
|
103 | - * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase. generated if not set |
|
104 | - * @param string $aad - [optional] additional authentication data |
|
105 | - * @return string |
|
106 | - * @throws Exception |
|
107 | - */ |
|
108 | - public function encrypt($text_to_encrypt, $encryption_key_identifier = '', $aad = '') |
|
109 | - { |
|
110 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
111 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
112 | - // generate initialization vector for the cipher method. |
|
113 | - $iv = random_bytes(openssl_cipher_iv_length($cipher_method)); |
|
114 | - // encrypt it (encode to remove special characters) |
|
115 | - $text_to_encrypt = $this->base64_encoder->encodeString($text_to_encrypt); |
|
116 | - $encrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
117 | - ? $this->authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
118 | - : $this->nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv); |
|
119 | - return $this->base64_encoder->encodeString($encrypted_text); |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * @param string $text_to_encrypt - the text to be encrypted |
|
125 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
126 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
127 | - * @param string $iv - the initialization vector |
|
128 | - * @param string $aad - additional authentication data |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - private function authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
132 | - { |
|
133 | - $encrypted_text = openssl_encrypt( |
|
134 | - $text_to_encrypt, |
|
135 | - $cipher_method, |
|
136 | - $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
137 | - 0, |
|
138 | - $iv, |
|
139 | - $tag, |
|
140 | - $aad, |
|
141 | - OpenSSLv2::AUTH_TAG_LENGTH |
|
142 | - ); |
|
143 | - $this->validateEncryption($encrypted_text); |
|
144 | - // concatenate everything into one big string |
|
145 | - return $iv . $tag . $encrypted_text; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * @param string $text_to_encrypt - the text to be encrypted |
|
151 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
152 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
153 | - * @param string $iv - the initialization vector |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - private function nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv) |
|
157 | - { |
|
158 | - $encrypted_text = openssl_encrypt( |
|
159 | - $text_to_encrypt, |
|
160 | - $cipher_method, |
|
161 | - $this->getDigestHashValue($encryption_key), |
|
162 | - 0, |
|
163 | - $iv |
|
164 | - ); |
|
165 | - $this->validateEncryption($encrypted_text); |
|
166 | - // prepend the initialization vector |
|
167 | - return $iv . $encrypted_text; |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * decrypts data |
|
173 | - * |
|
174 | - * @param string $encrypted_text - the text to be decrypted |
|
175 | - * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase uses default if not set |
|
176 | - * @param string $aad - [optional] additional authentication data |
|
177 | - * @return string |
|
178 | - */ |
|
179 | - public function decrypt($encrypted_text, $encryption_key_identifier = '', $aad = '') |
|
180 | - { |
|
181 | - $cipher_method = $this->cipher_method->getCipherMethod(); |
|
182 | - $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
183 | - // maybe decode |
|
184 | - $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
185 | - $iv_length = openssl_cipher_iv_length($cipher_method); |
|
186 | - // use the iv length to snip it from the decoded string |
|
187 | - $iv = substr($encrypted_text, 0, $iv_length); |
|
188 | - // then remove it from the rest of the decoded string |
|
189 | - $encrypted_text = substr($encrypted_text, $iv_length); |
|
190 | - // decrypt it |
|
191 | - $decrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
192 | - ? $this->authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
193 | - : $this->nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv); |
|
194 | - |
|
195 | - $this->validateDecryption($decrypted_text); |
|
196 | - return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * @param string $encrypted_text - the text to be decrypted |
|
202 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
203 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
204 | - * @param string $iv - the initialization vector |
|
205 | - * @param string $aad - additional authentication data |
|
206 | - * @return string|false |
|
207 | - */ |
|
208 | - private function authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
209 | - { |
|
210 | - // use the tag length to snip it from the decoded string |
|
211 | - $tag = substr($encrypted_text, 0, OpenSSLv2::AUTH_TAG_LENGTH); |
|
212 | - // then remove it from the rest of the decoded string |
|
213 | - $encrypted_text = substr($encrypted_text, OpenSSLv2::AUTH_TAG_LENGTH); |
|
214 | - return openssl_decrypt( |
|
215 | - $encrypted_text, |
|
216 | - $cipher_method, |
|
217 | - $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
218 | - 0, |
|
219 | - $iv, |
|
220 | - $tag, |
|
221 | - $aad |
|
222 | - ); |
|
223 | - } |
|
224 | - |
|
225 | - |
|
226 | - /** |
|
227 | - * @param string $encrypted_text - the text to be decrypted |
|
228 | - * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
229 | - * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
230 | - * @param string $iv - the initialization vector |
|
231 | - * @return string|false |
|
232 | - */ |
|
233 | - private function nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv) |
|
234 | - { |
|
235 | - return openssl_decrypt( |
|
236 | - $encrypted_text, |
|
237 | - $cipher_method, |
|
238 | - $this->getDigestHashValue($encryption_key), |
|
239 | - 0, |
|
240 | - $iv |
|
241 | - ); |
|
242 | - } |
|
26 | + /** |
|
27 | + * name used for a default encryption key in case no others are set |
|
28 | + */ |
|
29 | + const DEFAULT_ENCRYPTION_KEY_ID = 'default_openssl_v2_key'; |
|
30 | + |
|
31 | + /** |
|
32 | + * name used for saving encryption keys to the wp_options table |
|
33 | + */ |
|
34 | + const ENCRYPTION_KEYS_OPTION_NAME = 'ee_openssl_v2_encryption_keys'; |
|
35 | + |
|
36 | + /** |
|
37 | + * the OPENSSL cipher method used |
|
38 | + */ |
|
39 | + const CIPHER_METHOD = 'aes-256-gcm'; |
|
40 | + |
|
41 | + /** |
|
42 | + * WP "options_name" used to store a verified available cipher method |
|
43 | + */ |
|
44 | + const CIPHER_METHOD_OPTION_NAME = 'ee_openssl_v2_cipher_method'; |
|
45 | + |
|
46 | + /** |
|
47 | + * The length of the authentication tag. Its value can be between 4 and 16 for GCM mode. |
|
48 | + */ |
|
49 | + const AUTH_TAG_LENGTH = 16; |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * To use custom a cipher method and/or encryption keys and/or minimum PHP version: |
|
54 | + * - extend this class |
|
55 | + * - configure a new CipherMethod / EncryptionKeyManager in the constructor |
|
56 | + * - pass those to this constructor, like so: |
|
57 | + * |
|
58 | + * public function __construct(Base64Encoder $base64_encoder) { |
|
59 | + * parent::__construct( |
|
60 | + * $base64_encoder, |
|
61 | + * new CipherMethod(CIPHER_METHOD, CIPHER_METHOD_OPTION_NAME), |
|
62 | + * new EncryptionKeyManager(CUSTOM_KEY_ID, CUSTOM_KEYS_OPTION_NAME), |
|
63 | + * '7.1.0' |
|
64 | + * ); |
|
65 | + * } |
|
66 | + * |
|
67 | + * @param Base64Encoder $base64_encoder |
|
68 | + * @param CipherMethod|null $cipher_method |
|
69 | + * @param EncryptionKeyManagerInterface|null $encryption_key_manager |
|
70 | + * @param string $min_php_version defaults to 7.1.0 |
|
71 | + * (when openssl auth tag and random_bytes() were added) |
|
72 | + */ |
|
73 | + public function __construct( |
|
74 | + Base64Encoder $base64_encoder, |
|
75 | + CipherMethod $cipher_method = null, |
|
76 | + EncryptionKeyManagerInterface $encryption_key_manager = null, |
|
77 | + $min_php_version = '7.1.0' |
|
78 | + ) { |
|
79 | + parent::__construct( |
|
80 | + $base64_encoder, |
|
81 | + $cipher_method instanceof CipherMethod |
|
82 | + ? $cipher_method |
|
83 | + : new CipherMethod( |
|
84 | + OpenSSLv2::CIPHER_METHOD, |
|
85 | + OpenSSLv2::CIPHER_METHOD_OPTION_NAME |
|
86 | + ), |
|
87 | + $encryption_key_manager instanceof EncryptionKeyManager |
|
88 | + ? $encryption_key_manager |
|
89 | + : new EncryptionKeyManager( |
|
90 | + $base64_encoder, |
|
91 | + OpenSSLv2::DEFAULT_ENCRYPTION_KEY_ID, |
|
92 | + OpenSSLv2::ENCRYPTION_KEYS_OPTION_NAME |
|
93 | + ), |
|
94 | + $min_php_version |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * encrypts data |
|
101 | + * |
|
102 | + * @param string $text_to_encrypt - the text to be encrypted |
|
103 | + * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase. generated if not set |
|
104 | + * @param string $aad - [optional] additional authentication data |
|
105 | + * @return string |
|
106 | + * @throws Exception |
|
107 | + */ |
|
108 | + public function encrypt($text_to_encrypt, $encryption_key_identifier = '', $aad = '') |
|
109 | + { |
|
110 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
111 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
112 | + // generate initialization vector for the cipher method. |
|
113 | + $iv = random_bytes(openssl_cipher_iv_length($cipher_method)); |
|
114 | + // encrypt it (encode to remove special characters) |
|
115 | + $text_to_encrypt = $this->base64_encoder->encodeString($text_to_encrypt); |
|
116 | + $encrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
117 | + ? $this->authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
118 | + : $this->nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv); |
|
119 | + return $this->base64_encoder->encodeString($encrypted_text); |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * @param string $text_to_encrypt - the text to be encrypted |
|
125 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
126 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
127 | + * @param string $iv - the initialization vector |
|
128 | + * @param string $aad - additional authentication data |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + private function authenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv, $aad) |
|
132 | + { |
|
133 | + $encrypted_text = openssl_encrypt( |
|
134 | + $text_to_encrypt, |
|
135 | + $cipher_method, |
|
136 | + $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
137 | + 0, |
|
138 | + $iv, |
|
139 | + $tag, |
|
140 | + $aad, |
|
141 | + OpenSSLv2::AUTH_TAG_LENGTH |
|
142 | + ); |
|
143 | + $this->validateEncryption($encrypted_text); |
|
144 | + // concatenate everything into one big string |
|
145 | + return $iv . $tag . $encrypted_text; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * @param string $text_to_encrypt - the text to be encrypted |
|
151 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
152 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
153 | + * @param string $iv - the initialization vector |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + private function nonAuthenticatedEncrypt($text_to_encrypt, $cipher_method, $encryption_key, $iv) |
|
157 | + { |
|
158 | + $encrypted_text = openssl_encrypt( |
|
159 | + $text_to_encrypt, |
|
160 | + $cipher_method, |
|
161 | + $this->getDigestHashValue($encryption_key), |
|
162 | + 0, |
|
163 | + $iv |
|
164 | + ); |
|
165 | + $this->validateEncryption($encrypted_text); |
|
166 | + // prepend the initialization vector |
|
167 | + return $iv . $encrypted_text; |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * decrypts data |
|
173 | + * |
|
174 | + * @param string $encrypted_text - the text to be decrypted |
|
175 | + * @param string $encryption_key_identifier - [optional] cryptographically secure passphrase uses default if not set |
|
176 | + * @param string $aad - [optional] additional authentication data |
|
177 | + * @return string |
|
178 | + */ |
|
179 | + public function decrypt($encrypted_text, $encryption_key_identifier = '', $aad = '') |
|
180 | + { |
|
181 | + $cipher_method = $this->cipher_method->getCipherMethod(); |
|
182 | + $encryption_key = $this->encryption_key_manager->getEncryptionKey($encryption_key_identifier); |
|
183 | + // maybe decode |
|
184 | + $encrypted_text = $this->base64_encoder->decodeString($encrypted_text); |
|
185 | + $iv_length = openssl_cipher_iv_length($cipher_method); |
|
186 | + // use the iv length to snip it from the decoded string |
|
187 | + $iv = substr($encrypted_text, 0, $iv_length); |
|
188 | + // then remove it from the rest of the decoded string |
|
189 | + $encrypted_text = substr($encrypted_text, $iv_length); |
|
190 | + // decrypt it |
|
191 | + $decrypted_text = $this->cipher_method->usesAuthenticatedEncryptionMode() |
|
192 | + ? $this->authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
193 | + : $this->nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv); |
|
194 | + |
|
195 | + $this->validateDecryption($decrypted_text); |
|
196 | + return trim($this->base64_encoder->decodeString($decrypted_text)); |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * @param string $encrypted_text - the text to be decrypted |
|
202 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
203 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
204 | + * @param string $iv - the initialization vector |
|
205 | + * @param string $aad - additional authentication data |
|
206 | + * @return string|false |
|
207 | + */ |
|
208 | + private function authenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv, $aad) |
|
209 | + { |
|
210 | + // use the tag length to snip it from the decoded string |
|
211 | + $tag = substr($encrypted_text, 0, OpenSSLv2::AUTH_TAG_LENGTH); |
|
212 | + // then remove it from the rest of the decoded string |
|
213 | + $encrypted_text = substr($encrypted_text, OpenSSLv2::AUTH_TAG_LENGTH); |
|
214 | + return openssl_decrypt( |
|
215 | + $encrypted_text, |
|
216 | + $cipher_method, |
|
217 | + $this->getDigestHashValue($encryption_key, OpenSSL::DEFAULT_DIGEST_METHOD, OPENSSL_RAW_DATA), |
|
218 | + 0, |
|
219 | + $iv, |
|
220 | + $tag, |
|
221 | + $aad |
|
222 | + ); |
|
223 | + } |
|
224 | + |
|
225 | + |
|
226 | + /** |
|
227 | + * @param string $encrypted_text - the text to be decrypted |
|
228 | + * @param string $cipher_method - the OpenSSL cipher method used during encryption |
|
229 | + * @param string $encryption_key - cryptographically secure passphrase uses default if not set |
|
230 | + * @param string $iv - the initialization vector |
|
231 | + * @return string|false |
|
232 | + */ |
|
233 | + private function nonAuthenticatedDecrypt($encrypted_text, $cipher_method, $encryption_key, $iv) |
|
234 | + { |
|
235 | + return openssl_decrypt( |
|
236 | + $encrypted_text, |
|
237 | + $cipher_method, |
|
238 | + $this->getDigestHashValue($encryption_key), |
|
239 | + 0, |
|
240 | + $iv |
|
241 | + ); |
|
242 | + } |
|
243 | 243 | } |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | */ |
104 | 104 | protected function __construct() |
105 | 105 | { |
106 | - if (! defined('ESPRESSO_ENCRYPT')) { |
|
106 | + if ( ! defined('ESPRESSO_ENCRYPT')) { |
|
107 | 107 | define('ESPRESSO_ENCRYPT', true); |
108 | 108 | } |
109 | 109 | if (extension_loaded('openssl')) { |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | public static function instance() |
126 | 126 | { |
127 | 127 | // check if class object is instantiated |
128 | - if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
128 | + if ( ! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
129 | 129 | EE_Encryption::$_instance = new self(); |
130 | 130 | } |
131 | 131 | return EE_Encryption::$_instance; |
@@ -332,7 +332,7 @@ discard block |
||
332 | 332 | $iv |
333 | 333 | ); |
334 | 334 | // append the initialization vector |
335 | - $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
335 | + $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER.$iv; |
|
336 | 336 | // trim and maybe encode |
337 | 337 | return $this->_use_base64_encode |
338 | 338 | ? trim(base64_encode($encrypted_text)) |
@@ -515,7 +515,7 @@ discard block |
||
515 | 515 | if (empty($text_string)) { |
516 | 516 | return $text_string; |
517 | 517 | } |
518 | - $key_bits = str_split( |
|
518 | + $key_bits = str_split( |
|
519 | 519 | str_pad( |
520 | 520 | '', |
521 | 521 | strlen($text_string), |
@@ -525,8 +525,8 @@ discard block |
||
525 | 525 | ); |
526 | 526 | $string_bits = str_split($text_string); |
527 | 527 | foreach ($string_bits as $k => $v) { |
528 | - $temp = ord($v) + ord($key_bits[ $k ]); |
|
529 | - $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
528 | + $temp = ord($v) + ord($key_bits[$k]); |
|
529 | + $string_bits[$k] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
530 | 530 | } |
531 | 531 | $encrypted_text = implode('', $string_bits); |
532 | 532 | $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
@@ -568,10 +568,10 @@ discard block |
||
568 | 568 | STR_PAD_RIGHT |
569 | 569 | ) |
570 | 570 | ); |
571 | - $string_bits = str_split($encrypted_text); |
|
571 | + $string_bits = str_split($encrypted_text); |
|
572 | 572 | foreach ($string_bits as $k => $v) { |
573 | - $temp = ord($v) - ord($key_bits[ $k ]); |
|
574 | - $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
573 | + $temp = ord($v) - ord($key_bits[$k]); |
|
574 | + $string_bits[$k] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
575 | 575 | } |
576 | 576 | return implode('', $string_bits); |
577 | 577 | } |
@@ -585,16 +585,16 @@ discard block |
||
585 | 585 | protected function valid_base_64($string) |
586 | 586 | { |
587 | 587 | // ensure data is a string |
588 | - if (! is_string($string) || ! $this->_use_base64_encode) { |
|
588 | + if ( ! is_string($string) || ! $this->_use_base64_encode) { |
|
589 | 589 | return false; |
590 | 590 | } |
591 | 591 | $decoded = base64_decode($string, true); |
592 | 592 | // Check if there is no invalid character in string |
593 | - if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
593 | + if ( ! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
594 | 594 | return false; |
595 | 595 | } |
596 | 596 | // Decode the string in strict mode and send the response |
597 | - if (! base64_decode($string, true)) { |
|
597 | + if ( ! base64_decode($string, true)) { |
|
598 | 598 | return false; |
599 | 599 | } |
600 | 600 | // Encode and compare it to original one |
@@ -614,7 +614,7 @@ discard block |
||
614 | 614 | $iterations = ceil($length / 40); |
615 | 615 | $random_string = ''; |
616 | 616 | for ($i = 0; $i < $iterations; $i++) { |
617 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
617 | + $random_string .= sha1(microtime(true).mt_rand(10000, 90000)); |
|
618 | 618 | } |
619 | 619 | $random_string = substr($random_string, 0, $length); |
620 | 620 | return $random_string; |
@@ -25,582 +25,582 @@ |
||
25 | 25 | */ |
26 | 26 | class EE_Encryption implements InterminableInterface |
27 | 27 | { |
28 | - /** |
|
29 | - * key used for saving the encryption key to the wp_options table |
|
30 | - */ |
|
31 | - const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
32 | - |
|
33 | - /** |
|
34 | - * the OPENSSL cipher method used |
|
35 | - */ |
|
36 | - const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
37 | - |
|
38 | - /** |
|
39 | - * WP "options_name" used to store a verified available cipher method |
|
40 | - */ |
|
41 | - const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
42 | - |
|
43 | - /** |
|
44 | - * the OPENSSL digest method used |
|
45 | - */ |
|
46 | - const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
47 | - |
|
48 | - /** |
|
49 | - * separates the encrypted text from the initialization vector |
|
50 | - */ |
|
51 | - const OPENSSL_IV_DELIMITER = ':iv:'; |
|
52 | - |
|
53 | - /** |
|
54 | - * appended to text encrypted using the acme encryption |
|
55 | - */ |
|
56 | - const ACME_ENCRYPTION_FLAG = '::ae'; |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * instance of the EE_Encryption object |
|
61 | - */ |
|
62 | - protected static $_instance; |
|
63 | - |
|
64 | - /** |
|
65 | - * @var string $_encryption_key |
|
66 | - */ |
|
67 | - protected $_encryption_key; |
|
68 | - |
|
69 | - /** |
|
70 | - * @var string $cipher_method |
|
71 | - */ |
|
72 | - private $cipher_method = ''; |
|
73 | - |
|
74 | - /** |
|
75 | - * @var array $cipher_methods |
|
76 | - */ |
|
77 | - private $cipher_methods = []; |
|
78 | - |
|
79 | - /** |
|
80 | - * @var array $digest_methods |
|
81 | - */ |
|
82 | - private $digest_methods = []; |
|
83 | - |
|
84 | - /** |
|
85 | - * @var boolean $_use_openssl_encrypt |
|
86 | - */ |
|
87 | - protected $_use_openssl_encrypt = false; |
|
88 | - |
|
89 | - /** |
|
90 | - * @var boolean $_use_base64_encode |
|
91 | - */ |
|
92 | - protected $_use_base64_encode = false; |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * protected constructor to prevent direct creation |
|
97 | - */ |
|
98 | - protected function __construct() |
|
99 | - { |
|
100 | - if (! defined('ESPRESSO_ENCRYPT')) { |
|
101 | - define('ESPRESSO_ENCRYPT', true); |
|
102 | - } |
|
103 | - if (extension_loaded('openssl')) { |
|
104 | - $this->_use_openssl_encrypt = true; |
|
105 | - } |
|
106 | - if (function_exists('base64_encode')) { |
|
107 | - $this->_use_base64_encode = true; |
|
108 | - } |
|
109 | - EE_Error::doing_it_wrong(__METHOD__, esc_html__('Usage is deprecated.', 'event_espresso'), '5.0.8.p'); |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * singleton method used to instantiate class object |
|
115 | - * |
|
116 | - * @return EE_Encryption |
|
117 | - */ |
|
118 | - public static function instance() |
|
119 | - { |
|
120 | - // check if class object is instantiated |
|
121 | - if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
122 | - EE_Encryption::$_instance = new self(); |
|
123 | - } |
|
124 | - return EE_Encryption::$_instance; |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * get encryption key |
|
130 | - * |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - public function get_encryption_key() |
|
134 | - { |
|
135 | - // if encryption key has not been set |
|
136 | - if (empty($this->_encryption_key)) { |
|
137 | - // retrieve encryption_key from db |
|
138 | - $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
139 | - // WHAT?? No encryption_key in the db ?? |
|
140 | - if ($this->_encryption_key === '') { |
|
141 | - // let's make one. And md5 it to make it just the right size for a key |
|
142 | - $new_key = md5($this->generate_random_string()); |
|
143 | - // now save it to the db for later |
|
144 | - add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
145 | - // here's the key - FINALLY ! |
|
146 | - $this->_encryption_key = $new_key; |
|
147 | - } |
|
148 | - } |
|
149 | - return $this->_encryption_key; |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * encrypts data |
|
155 | - * |
|
156 | - * @param string $text_string - the text to be encrypted |
|
157 | - * @return string |
|
158 | - * @throws RuntimeException |
|
159 | - */ |
|
160 | - public function encrypt($text_string = '') |
|
161 | - { |
|
162 | - // you give me nothing??? GET OUT ! |
|
163 | - if (empty($text_string)) { |
|
164 | - return $text_string; |
|
165 | - } |
|
166 | - if ($this->_use_openssl_encrypt) { |
|
167 | - $encrypted_text = $this->openssl_encrypt($text_string); |
|
168 | - } else { |
|
169 | - $encrypted_text = $this->acme_encrypt($text_string); |
|
170 | - } |
|
171 | - return $encrypted_text; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * decrypts data |
|
177 | - * |
|
178 | - * @param string $encrypted_text - the text to be decrypted |
|
179 | - * @return string |
|
180 | - * @throws RuntimeException |
|
181 | - */ |
|
182 | - public function decrypt($encrypted_text = '') |
|
183 | - { |
|
184 | - // you give me nothing??? GET OUT ! |
|
185 | - if (empty($encrypted_text)) { |
|
186 | - return $encrypted_text; |
|
187 | - } |
|
188 | - // if PHP's mcrypt functions are installed then we'll use them |
|
189 | - if ($this->_use_openssl_encrypt) { |
|
190 | - $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
191 | - } else { |
|
192 | - $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
193 | - } |
|
194 | - return $decrypted_text; |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * encodes string with PHP's base64 encoding |
|
200 | - * |
|
201 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
202 | - * @param string $text_string the text to be encoded |
|
203 | - * @return string |
|
204 | - */ |
|
205 | - public function base64_string_encode($text_string = '') |
|
206 | - { |
|
207 | - // you give me nothing??? GET OUT ! |
|
208 | - if (empty($text_string) || ! $this->_use_base64_encode) { |
|
209 | - return $text_string; |
|
210 | - } |
|
211 | - // encode |
|
212 | - return base64_encode($text_string); |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * decodes string that has been encoded with PHP's base64 encoding |
|
218 | - * |
|
219 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
220 | - * @param string $encoded_string the text to be decoded |
|
221 | - * @return string |
|
222 | - * @throws RuntimeException |
|
223 | - */ |
|
224 | - public function base64_string_decode($encoded_string = '') |
|
225 | - { |
|
226 | - // you give me nothing??? GET OUT ! |
|
227 | - if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
228 | - return $encoded_string; |
|
229 | - } |
|
230 | - // decode |
|
231 | - $decoded_string = base64_decode($encoded_string); |
|
232 | - if ($decoded_string === false) { |
|
233 | - throw new RuntimeException( |
|
234 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
235 | - ); |
|
236 | - } |
|
237 | - return $decoded_string; |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * encodes url string with PHP's base64 encoding |
|
243 | - * |
|
244 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
245 | - * @param string $text_string the text to be encoded |
|
246 | - * @return string |
|
247 | - */ |
|
248 | - public function base64_url_encode($text_string = '') |
|
249 | - { |
|
250 | - // you give me nothing??? GET OUT ! |
|
251 | - if (empty($text_string) || ! $this->_use_base64_encode) { |
|
252 | - return $text_string; |
|
253 | - } |
|
254 | - // encode |
|
255 | - $encoded_string = base64_encode($text_string); |
|
256 | - // remove chars to make encoding more URL friendly |
|
257 | - return strtr($encoded_string, '+/=', '-_,'); |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * decodes url string that has been encoded with PHP's base64 encoding |
|
263 | - * |
|
264 | - * @see http://php.net/manual/en/function.base64-encode.php |
|
265 | - * @param string $encoded_string the text to be decoded |
|
266 | - * @return string |
|
267 | - * @throws RuntimeException |
|
268 | - */ |
|
269 | - public function base64_url_decode($encoded_string = '') |
|
270 | - { |
|
271 | - // replace previously removed characters |
|
272 | - $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
273 | - // you give me nothing??? GET OUT ! |
|
274 | - if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
275 | - return $encoded_string; |
|
276 | - } |
|
277 | - // decode |
|
278 | - $decoded_string = base64_decode($encoded_string); |
|
279 | - if ($decoded_string === false) { |
|
280 | - throw new RuntimeException( |
|
281 | - esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
282 | - ); |
|
283 | - } |
|
284 | - return $decoded_string; |
|
285 | - } |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * encrypts data using PHP's openssl functions |
|
290 | - * |
|
291 | - * @param string $text_string the text to be encrypted |
|
292 | - * @param string $cipher_method |
|
293 | - * @param string $encryption_key |
|
294 | - * @return string |
|
295 | - * @throws RuntimeException |
|
296 | - */ |
|
297 | - protected function openssl_encrypt( |
|
298 | - $text_string = '', |
|
299 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
300 | - $encryption_key = '' |
|
301 | - ) { |
|
302 | - // you give me nothing??? GET OUT ! |
|
303 | - if (empty($text_string)) { |
|
304 | - return $text_string; |
|
305 | - } |
|
306 | - $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
307 | - // get initialization vector size |
|
308 | - $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
309 | - // generate initialization vector. |
|
310 | - // The second parameter ("crypto_strong") is passed by reference, |
|
311 | - // and is used to determines if the algorithm used was "cryptographically strong" |
|
312 | - // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
313 | - $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
314 | - if ($iv === false || $is_strong === false) { |
|
315 | - throw new RuntimeException( |
|
316 | - esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
317 | - ); |
|
318 | - } |
|
319 | - // encrypt it |
|
320 | - $encrypted_text = openssl_encrypt( |
|
321 | - $text_string, |
|
322 | - $this->cipher_method, |
|
323 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
324 | - 0, |
|
325 | - $iv |
|
326 | - ); |
|
327 | - // append the initialization vector |
|
328 | - $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
329 | - // trim and maybe encode |
|
330 | - return $this->_use_base64_encode |
|
331 | - ? trim(base64_encode($encrypted_text)) |
|
332 | - : trim($encrypted_text); |
|
333 | - } |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * Returns a cipher method that has been verified to work. |
|
338 | - * First checks if the cached cipher has been set already and if so, returns that. |
|
339 | - * Then tests the incoming default and returns that if it's good. |
|
340 | - * If not, then it retrieves the previously tested and saved cipher method. |
|
341 | - * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
342 | - * to see what is available on the server, and returns the results. |
|
343 | - * |
|
344 | - * @param string $cipher_method |
|
345 | - * @return string |
|
346 | - * @throws RuntimeException |
|
347 | - */ |
|
348 | - protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
349 | - { |
|
350 | - if ($this->cipher_method !== '') { |
|
351 | - return $this->cipher_method; |
|
352 | - } |
|
353 | - // verify that the default cipher method can produce an initialization vector |
|
354 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
355 | - // nope? okay let's get what we found in the past to work |
|
356 | - $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
357 | - // oops... haven't tested available cipher methods yet |
|
358 | - if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
359 | - $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
360 | - } |
|
361 | - } |
|
362 | - return $cipher_method; |
|
363 | - } |
|
364 | - |
|
365 | - |
|
366 | - /** |
|
367 | - * @param string $cipher_method |
|
368 | - * @return string |
|
369 | - * @throws \RuntimeException |
|
370 | - */ |
|
371 | - protected function getAvailableCipherMethod($cipher_method) |
|
372 | - { |
|
373 | - // verify that the incoming cipher method can produce an initialization vector |
|
374 | - if (openssl_cipher_iv_length($cipher_method) === false) { |
|
375 | - // nope? then check the next cipher in the list of available cipher methods |
|
376 | - $cipher_method = next($this->cipher_methods); |
|
377 | - // what? there's no list? then generate that list and cache it, |
|
378 | - if (empty($this->cipher_methods)) { |
|
379 | - $this->cipher_methods = openssl_get_cipher_methods(); |
|
380 | - // then grab the first item from the list |
|
381 | - $cipher_method = reset($this->cipher_methods); |
|
382 | - } |
|
383 | - if ($cipher_method === false) { |
|
384 | - throw new RuntimeException( |
|
385 | - esc_html__( |
|
386 | - 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
387 | - 'event_espresso' |
|
388 | - ) |
|
389 | - ); |
|
390 | - } |
|
391 | - // verify that the next cipher method works |
|
392 | - return $this->getAvailableCipherMethod($cipher_method); |
|
393 | - } |
|
394 | - // if we've gotten this far, then we found an available cipher method that works |
|
395 | - // so save that for next time |
|
396 | - update_option( |
|
397 | - EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
398 | - $cipher_method |
|
399 | - ); |
|
400 | - return $cipher_method; |
|
401 | - } |
|
402 | - |
|
403 | - |
|
404 | - /** |
|
405 | - * decrypts data that has been encrypted with PHP's openssl functions |
|
406 | - * |
|
407 | - * @param string $encrypted_text the text to be decrypted |
|
408 | - * @param string $cipher_method |
|
409 | - * @param string $encryption_key |
|
410 | - * @return string |
|
411 | - * @throws RuntimeException |
|
412 | - */ |
|
413 | - protected function openssl_decrypt( |
|
414 | - $encrypted_text = '', |
|
415 | - $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
416 | - $encryption_key = '' |
|
417 | - ) { |
|
418 | - // you give me nothing??? GET OUT ! |
|
419 | - if (empty($encrypted_text)) { |
|
420 | - return $encrypted_text; |
|
421 | - } |
|
422 | - // decode |
|
423 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
424 | - ? $this->base64_url_decode($encrypted_text) |
|
425 | - : $encrypted_text; |
|
426 | - $encrypted_components = explode( |
|
427 | - EE_Encryption::OPENSSL_IV_DELIMITER, |
|
428 | - $encrypted_text, |
|
429 | - 2 |
|
430 | - ); |
|
431 | - // decrypt it |
|
432 | - $decrypted_text = openssl_decrypt( |
|
433 | - $encrypted_components[0], |
|
434 | - $this->getCipherMethod($cipher_method), |
|
435 | - $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
436 | - 0, |
|
437 | - $encrypted_components[1] |
|
438 | - ); |
|
439 | - $decrypted_text = trim($decrypted_text); |
|
440 | - return $decrypted_text; |
|
441 | - } |
|
442 | - |
|
443 | - |
|
444 | - /** |
|
445 | - * Computes the digest hash value using the specified digest method. |
|
446 | - * If that digest method fails to produce a valid hash value, |
|
447 | - * then we'll grab the next digest method and recursively try again until something works. |
|
448 | - * |
|
449 | - * @param string $digest_method |
|
450 | - * @param string $encryption_key |
|
451 | - * @return string |
|
452 | - * @throws RuntimeException |
|
453 | - */ |
|
454 | - protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') |
|
455 | - { |
|
456 | - $encryption_key = $encryption_key !== '' |
|
457 | - ? $encryption_key |
|
458 | - : $this->get_encryption_key(); |
|
459 | - $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
460 | - if ($digest_hash_value === false) { |
|
461 | - return $this->getDigestHashValue($this->getDigestMethod()); |
|
462 | - } |
|
463 | - return $digest_hash_value; |
|
464 | - } |
|
465 | - |
|
466 | - |
|
467 | - /** |
|
468 | - * Returns the NEXT element in the $digest_methods array. |
|
469 | - * If the $digest_methods array is empty, then we populate it |
|
470 | - * with the available values returned from openssl_get_md_methods(). |
|
471 | - * |
|
472 | - * @return string |
|
473 | - * @throws \RuntimeException |
|
474 | - */ |
|
475 | - protected function getDigestMethod() |
|
476 | - { |
|
477 | - $digest_method = prev($this->digest_methods); |
|
478 | - if (empty($this->digest_methods)) { |
|
479 | - $this->digest_methods = openssl_get_md_methods(); |
|
480 | - $digest_method = end($this->digest_methods); |
|
481 | - } |
|
482 | - if ($digest_method === false) { |
|
483 | - throw new RuntimeException( |
|
484 | - esc_html__( |
|
485 | - 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
486 | - 'event_espresso' |
|
487 | - ) |
|
488 | - ); |
|
489 | - } |
|
490 | - return $digest_method; |
|
491 | - } |
|
492 | - |
|
493 | - |
|
494 | - /** |
|
495 | - * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
496 | - * |
|
497 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
498 | - * @param string $text_string the text to be decrypted |
|
499 | - * @return string |
|
500 | - */ |
|
501 | - protected function acme_encrypt($text_string = '') |
|
502 | - { |
|
503 | - // you give me nothing??? GET OUT ! |
|
504 | - if (empty($text_string)) { |
|
505 | - return $text_string; |
|
506 | - } |
|
507 | - $key_bits = str_split( |
|
508 | - str_pad( |
|
509 | - '', |
|
510 | - strlen($text_string), |
|
511 | - $this->get_encryption_key(), |
|
512 | - STR_PAD_RIGHT |
|
513 | - ) |
|
514 | - ); |
|
515 | - $string_bits = str_split($text_string); |
|
516 | - foreach ($string_bits as $k => $v) { |
|
517 | - $temp = ord($v) + ord($key_bits[ $k ]); |
|
518 | - $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
519 | - } |
|
520 | - $encrypted_text = implode('', $string_bits); |
|
521 | - $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
522 | - return $this->_use_base64_encode |
|
523 | - ? base64_encode($encrypted_text) |
|
524 | - : $encrypted_text; |
|
525 | - } |
|
526 | - |
|
527 | - |
|
528 | - /** |
|
529 | - * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
530 | - * |
|
531 | - * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
532 | - * @param string $encrypted_text the text to be decrypted |
|
533 | - * @return string |
|
534 | - * @throws RuntimeException |
|
535 | - */ |
|
536 | - protected function acme_decrypt($encrypted_text = '') |
|
537 | - { |
|
538 | - // you give me nothing??? GET OUT ! |
|
539 | - if (empty($encrypted_text)) { |
|
540 | - return $encrypted_text; |
|
541 | - } |
|
542 | - // decode the data ? |
|
543 | - $encrypted_text = $this->valid_base_64($encrypted_text) |
|
544 | - ? $this->base64_url_decode($encrypted_text) |
|
545 | - : $encrypted_text; |
|
546 | - $encrypted_text = substr($encrypted_text, 0, -4); |
|
547 | - $key_bits = str_split( |
|
548 | - str_pad( |
|
549 | - '', |
|
550 | - strlen($encrypted_text), |
|
551 | - $this->get_encryption_key(), |
|
552 | - STR_PAD_RIGHT |
|
553 | - ) |
|
554 | - ); |
|
555 | - $string_bits = str_split($encrypted_text); |
|
556 | - foreach ($string_bits as $k => $v) { |
|
557 | - $temp = ord($v) - ord($key_bits[ $k ]); |
|
558 | - $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
559 | - } |
|
560 | - return implode('', $string_bits); |
|
561 | - } |
|
562 | - |
|
563 | - |
|
564 | - /** |
|
565 | - * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
566 | - * @param $string |
|
567 | - * @return bool |
|
568 | - */ |
|
569 | - protected function valid_base_64($string) |
|
570 | - { |
|
571 | - // ensure data is a string |
|
572 | - if (! is_string($string) || ! $this->_use_base64_encode) { |
|
573 | - return false; |
|
574 | - } |
|
575 | - $decoded = base64_decode($string, true); |
|
576 | - // Check if there is no invalid character in string |
|
577 | - if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
578 | - return false; |
|
579 | - } |
|
580 | - // Decode the string in strict mode and send the response |
|
581 | - if (! base64_decode($string, true)) { |
|
582 | - return false; |
|
583 | - } |
|
584 | - // Encode and compare it to original one |
|
585 | - return base64_encode($decoded) === $string; |
|
586 | - } |
|
587 | - |
|
588 | - |
|
589 | - /** |
|
590 | - * generate random string |
|
591 | - * |
|
592 | - * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
593 | - * @param int $length number of characters for random string |
|
594 | - * @return string |
|
595 | - */ |
|
596 | - public function generate_random_string($length = 40) |
|
597 | - { |
|
598 | - $iterations = ceil($length / 40); |
|
599 | - $random_string = ''; |
|
600 | - for ($i = 0; $i < $iterations; $i++) { |
|
601 | - $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
602 | - } |
|
603 | - $random_string = substr($random_string, 0, $length); |
|
604 | - return $random_string; |
|
605 | - } |
|
28 | + /** |
|
29 | + * key used for saving the encryption key to the wp_options table |
|
30 | + */ |
|
31 | + const ENCRYPTION_OPTION_KEY = 'ee_encryption_key'; |
|
32 | + |
|
33 | + /** |
|
34 | + * the OPENSSL cipher method used |
|
35 | + */ |
|
36 | + const OPENSSL_CIPHER_METHOD = 'AES-128-CBC'; |
|
37 | + |
|
38 | + /** |
|
39 | + * WP "options_name" used to store a verified available cipher method |
|
40 | + */ |
|
41 | + const OPENSSL_CIPHER_METHOD_OPTION_NAME = 'ee_openssl_cipher_method'; |
|
42 | + |
|
43 | + /** |
|
44 | + * the OPENSSL digest method used |
|
45 | + */ |
|
46 | + const OPENSSL_DIGEST_METHOD = 'sha512'; |
|
47 | + |
|
48 | + /** |
|
49 | + * separates the encrypted text from the initialization vector |
|
50 | + */ |
|
51 | + const OPENSSL_IV_DELIMITER = ':iv:'; |
|
52 | + |
|
53 | + /** |
|
54 | + * appended to text encrypted using the acme encryption |
|
55 | + */ |
|
56 | + const ACME_ENCRYPTION_FLAG = '::ae'; |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * instance of the EE_Encryption object |
|
61 | + */ |
|
62 | + protected static $_instance; |
|
63 | + |
|
64 | + /** |
|
65 | + * @var string $_encryption_key |
|
66 | + */ |
|
67 | + protected $_encryption_key; |
|
68 | + |
|
69 | + /** |
|
70 | + * @var string $cipher_method |
|
71 | + */ |
|
72 | + private $cipher_method = ''; |
|
73 | + |
|
74 | + /** |
|
75 | + * @var array $cipher_methods |
|
76 | + */ |
|
77 | + private $cipher_methods = []; |
|
78 | + |
|
79 | + /** |
|
80 | + * @var array $digest_methods |
|
81 | + */ |
|
82 | + private $digest_methods = []; |
|
83 | + |
|
84 | + /** |
|
85 | + * @var boolean $_use_openssl_encrypt |
|
86 | + */ |
|
87 | + protected $_use_openssl_encrypt = false; |
|
88 | + |
|
89 | + /** |
|
90 | + * @var boolean $_use_base64_encode |
|
91 | + */ |
|
92 | + protected $_use_base64_encode = false; |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * protected constructor to prevent direct creation |
|
97 | + */ |
|
98 | + protected function __construct() |
|
99 | + { |
|
100 | + if (! defined('ESPRESSO_ENCRYPT')) { |
|
101 | + define('ESPRESSO_ENCRYPT', true); |
|
102 | + } |
|
103 | + if (extension_loaded('openssl')) { |
|
104 | + $this->_use_openssl_encrypt = true; |
|
105 | + } |
|
106 | + if (function_exists('base64_encode')) { |
|
107 | + $this->_use_base64_encode = true; |
|
108 | + } |
|
109 | + EE_Error::doing_it_wrong(__METHOD__, esc_html__('Usage is deprecated.', 'event_espresso'), '5.0.8.p'); |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * singleton method used to instantiate class object |
|
115 | + * |
|
116 | + * @return EE_Encryption |
|
117 | + */ |
|
118 | + public static function instance() |
|
119 | + { |
|
120 | + // check if class object is instantiated |
|
121 | + if (! EE_Encryption::$_instance instanceof EE_Encryption) { |
|
122 | + EE_Encryption::$_instance = new self(); |
|
123 | + } |
|
124 | + return EE_Encryption::$_instance; |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * get encryption key |
|
130 | + * |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + public function get_encryption_key() |
|
134 | + { |
|
135 | + // if encryption key has not been set |
|
136 | + if (empty($this->_encryption_key)) { |
|
137 | + // retrieve encryption_key from db |
|
138 | + $this->_encryption_key = get_option(EE_Encryption::ENCRYPTION_OPTION_KEY, ''); |
|
139 | + // WHAT?? No encryption_key in the db ?? |
|
140 | + if ($this->_encryption_key === '') { |
|
141 | + // let's make one. And md5 it to make it just the right size for a key |
|
142 | + $new_key = md5($this->generate_random_string()); |
|
143 | + // now save it to the db for later |
|
144 | + add_option(EE_Encryption::ENCRYPTION_OPTION_KEY, $new_key); |
|
145 | + // here's the key - FINALLY ! |
|
146 | + $this->_encryption_key = $new_key; |
|
147 | + } |
|
148 | + } |
|
149 | + return $this->_encryption_key; |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * encrypts data |
|
155 | + * |
|
156 | + * @param string $text_string - the text to be encrypted |
|
157 | + * @return string |
|
158 | + * @throws RuntimeException |
|
159 | + */ |
|
160 | + public function encrypt($text_string = '') |
|
161 | + { |
|
162 | + // you give me nothing??? GET OUT ! |
|
163 | + if (empty($text_string)) { |
|
164 | + return $text_string; |
|
165 | + } |
|
166 | + if ($this->_use_openssl_encrypt) { |
|
167 | + $encrypted_text = $this->openssl_encrypt($text_string); |
|
168 | + } else { |
|
169 | + $encrypted_text = $this->acme_encrypt($text_string); |
|
170 | + } |
|
171 | + return $encrypted_text; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * decrypts data |
|
177 | + * |
|
178 | + * @param string $encrypted_text - the text to be decrypted |
|
179 | + * @return string |
|
180 | + * @throws RuntimeException |
|
181 | + */ |
|
182 | + public function decrypt($encrypted_text = '') |
|
183 | + { |
|
184 | + // you give me nothing??? GET OUT ! |
|
185 | + if (empty($encrypted_text)) { |
|
186 | + return $encrypted_text; |
|
187 | + } |
|
188 | + // if PHP's mcrypt functions are installed then we'll use them |
|
189 | + if ($this->_use_openssl_encrypt) { |
|
190 | + $decrypted_text = $this->openssl_decrypt($encrypted_text); |
|
191 | + } else { |
|
192 | + $decrypted_text = $this->acme_decrypt($encrypted_text); |
|
193 | + } |
|
194 | + return $decrypted_text; |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * encodes string with PHP's base64 encoding |
|
200 | + * |
|
201 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
202 | + * @param string $text_string the text to be encoded |
|
203 | + * @return string |
|
204 | + */ |
|
205 | + public function base64_string_encode($text_string = '') |
|
206 | + { |
|
207 | + // you give me nothing??? GET OUT ! |
|
208 | + if (empty($text_string) || ! $this->_use_base64_encode) { |
|
209 | + return $text_string; |
|
210 | + } |
|
211 | + // encode |
|
212 | + return base64_encode($text_string); |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * decodes string that has been encoded with PHP's base64 encoding |
|
218 | + * |
|
219 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
220 | + * @param string $encoded_string the text to be decoded |
|
221 | + * @return string |
|
222 | + * @throws RuntimeException |
|
223 | + */ |
|
224 | + public function base64_string_decode($encoded_string = '') |
|
225 | + { |
|
226 | + // you give me nothing??? GET OUT ! |
|
227 | + if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
228 | + return $encoded_string; |
|
229 | + } |
|
230 | + // decode |
|
231 | + $decoded_string = base64_decode($encoded_string); |
|
232 | + if ($decoded_string === false) { |
|
233 | + throw new RuntimeException( |
|
234 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
235 | + ); |
|
236 | + } |
|
237 | + return $decoded_string; |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * encodes url string with PHP's base64 encoding |
|
243 | + * |
|
244 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
245 | + * @param string $text_string the text to be encoded |
|
246 | + * @return string |
|
247 | + */ |
|
248 | + public function base64_url_encode($text_string = '') |
|
249 | + { |
|
250 | + // you give me nothing??? GET OUT ! |
|
251 | + if (empty($text_string) || ! $this->_use_base64_encode) { |
|
252 | + return $text_string; |
|
253 | + } |
|
254 | + // encode |
|
255 | + $encoded_string = base64_encode($text_string); |
|
256 | + // remove chars to make encoding more URL friendly |
|
257 | + return strtr($encoded_string, '+/=', '-_,'); |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * decodes url string that has been encoded with PHP's base64 encoding |
|
263 | + * |
|
264 | + * @see http://php.net/manual/en/function.base64-encode.php |
|
265 | + * @param string $encoded_string the text to be decoded |
|
266 | + * @return string |
|
267 | + * @throws RuntimeException |
|
268 | + */ |
|
269 | + public function base64_url_decode($encoded_string = '') |
|
270 | + { |
|
271 | + // replace previously removed characters |
|
272 | + $encoded_string = strtr($encoded_string, '-_,', '+/='); |
|
273 | + // you give me nothing??? GET OUT ! |
|
274 | + if (empty($encoded_string) || ! $this->valid_base_64($encoded_string)) { |
|
275 | + return $encoded_string; |
|
276 | + } |
|
277 | + // decode |
|
278 | + $decoded_string = base64_decode($encoded_string); |
|
279 | + if ($decoded_string === false) { |
|
280 | + throw new RuntimeException( |
|
281 | + esc_html__('Base 64 decoding failed.', 'event_espresso') |
|
282 | + ); |
|
283 | + } |
|
284 | + return $decoded_string; |
|
285 | + } |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * encrypts data using PHP's openssl functions |
|
290 | + * |
|
291 | + * @param string $text_string the text to be encrypted |
|
292 | + * @param string $cipher_method |
|
293 | + * @param string $encryption_key |
|
294 | + * @return string |
|
295 | + * @throws RuntimeException |
|
296 | + */ |
|
297 | + protected function openssl_encrypt( |
|
298 | + $text_string = '', |
|
299 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
300 | + $encryption_key = '' |
|
301 | + ) { |
|
302 | + // you give me nothing??? GET OUT ! |
|
303 | + if (empty($text_string)) { |
|
304 | + return $text_string; |
|
305 | + } |
|
306 | + $this->cipher_method = $this->getCipherMethod($cipher_method); |
|
307 | + // get initialization vector size |
|
308 | + $iv_size = openssl_cipher_iv_length($this->cipher_method); |
|
309 | + // generate initialization vector. |
|
310 | + // The second parameter ("crypto_strong") is passed by reference, |
|
311 | + // and is used to determines if the algorithm used was "cryptographically strong" |
|
312 | + // openssl_random_pseudo_bytes() will toggle it to either true or false |
|
313 | + $iv = openssl_random_pseudo_bytes($iv_size, $is_strong); |
|
314 | + if ($iv === false || $is_strong === false) { |
|
315 | + throw new RuntimeException( |
|
316 | + esc_html__('Failed to generate OpenSSL initialization vector.', 'event_espresso') |
|
317 | + ); |
|
318 | + } |
|
319 | + // encrypt it |
|
320 | + $encrypted_text = openssl_encrypt( |
|
321 | + $text_string, |
|
322 | + $this->cipher_method, |
|
323 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
324 | + 0, |
|
325 | + $iv |
|
326 | + ); |
|
327 | + // append the initialization vector |
|
328 | + $encrypted_text .= EE_Encryption::OPENSSL_IV_DELIMITER . $iv; |
|
329 | + // trim and maybe encode |
|
330 | + return $this->_use_base64_encode |
|
331 | + ? trim(base64_encode($encrypted_text)) |
|
332 | + : trim($encrypted_text); |
|
333 | + } |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * Returns a cipher method that has been verified to work. |
|
338 | + * First checks if the cached cipher has been set already and if so, returns that. |
|
339 | + * Then tests the incoming default and returns that if it's good. |
|
340 | + * If not, then it retrieves the previously tested and saved cipher method. |
|
341 | + * But if that doesn't exist, then calls getAvailableCipherMethod() |
|
342 | + * to see what is available on the server, and returns the results. |
|
343 | + * |
|
344 | + * @param string $cipher_method |
|
345 | + * @return string |
|
346 | + * @throws RuntimeException |
|
347 | + */ |
|
348 | + protected function getCipherMethod($cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD) |
|
349 | + { |
|
350 | + if ($this->cipher_method !== '') { |
|
351 | + return $this->cipher_method; |
|
352 | + } |
|
353 | + // verify that the default cipher method can produce an initialization vector |
|
354 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
355 | + // nope? okay let's get what we found in the past to work |
|
356 | + $cipher_method = get_option(EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, ''); |
|
357 | + // oops... haven't tested available cipher methods yet |
|
358 | + if ($cipher_method === '' || openssl_cipher_iv_length($cipher_method) === false) { |
|
359 | + $cipher_method = $this->getAvailableCipherMethod($cipher_method); |
|
360 | + } |
|
361 | + } |
|
362 | + return $cipher_method; |
|
363 | + } |
|
364 | + |
|
365 | + |
|
366 | + /** |
|
367 | + * @param string $cipher_method |
|
368 | + * @return string |
|
369 | + * @throws \RuntimeException |
|
370 | + */ |
|
371 | + protected function getAvailableCipherMethod($cipher_method) |
|
372 | + { |
|
373 | + // verify that the incoming cipher method can produce an initialization vector |
|
374 | + if (openssl_cipher_iv_length($cipher_method) === false) { |
|
375 | + // nope? then check the next cipher in the list of available cipher methods |
|
376 | + $cipher_method = next($this->cipher_methods); |
|
377 | + // what? there's no list? then generate that list and cache it, |
|
378 | + if (empty($this->cipher_methods)) { |
|
379 | + $this->cipher_methods = openssl_get_cipher_methods(); |
|
380 | + // then grab the first item from the list |
|
381 | + $cipher_method = reset($this->cipher_methods); |
|
382 | + } |
|
383 | + if ($cipher_method === false) { |
|
384 | + throw new RuntimeException( |
|
385 | + esc_html__( |
|
386 | + 'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.', |
|
387 | + 'event_espresso' |
|
388 | + ) |
|
389 | + ); |
|
390 | + } |
|
391 | + // verify that the next cipher method works |
|
392 | + return $this->getAvailableCipherMethod($cipher_method); |
|
393 | + } |
|
394 | + // if we've gotten this far, then we found an available cipher method that works |
|
395 | + // so save that for next time |
|
396 | + update_option( |
|
397 | + EE_Encryption::OPENSSL_CIPHER_METHOD_OPTION_NAME, |
|
398 | + $cipher_method |
|
399 | + ); |
|
400 | + return $cipher_method; |
|
401 | + } |
|
402 | + |
|
403 | + |
|
404 | + /** |
|
405 | + * decrypts data that has been encrypted with PHP's openssl functions |
|
406 | + * |
|
407 | + * @param string $encrypted_text the text to be decrypted |
|
408 | + * @param string $cipher_method |
|
409 | + * @param string $encryption_key |
|
410 | + * @return string |
|
411 | + * @throws RuntimeException |
|
412 | + */ |
|
413 | + protected function openssl_decrypt( |
|
414 | + $encrypted_text = '', |
|
415 | + $cipher_method = EE_Encryption::OPENSSL_CIPHER_METHOD, |
|
416 | + $encryption_key = '' |
|
417 | + ) { |
|
418 | + // you give me nothing??? GET OUT ! |
|
419 | + if (empty($encrypted_text)) { |
|
420 | + return $encrypted_text; |
|
421 | + } |
|
422 | + // decode |
|
423 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
424 | + ? $this->base64_url_decode($encrypted_text) |
|
425 | + : $encrypted_text; |
|
426 | + $encrypted_components = explode( |
|
427 | + EE_Encryption::OPENSSL_IV_DELIMITER, |
|
428 | + $encrypted_text, |
|
429 | + 2 |
|
430 | + ); |
|
431 | + // decrypt it |
|
432 | + $decrypted_text = openssl_decrypt( |
|
433 | + $encrypted_components[0], |
|
434 | + $this->getCipherMethod($cipher_method), |
|
435 | + $this->getDigestHashValue(EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key), |
|
436 | + 0, |
|
437 | + $encrypted_components[1] |
|
438 | + ); |
|
439 | + $decrypted_text = trim($decrypted_text); |
|
440 | + return $decrypted_text; |
|
441 | + } |
|
442 | + |
|
443 | + |
|
444 | + /** |
|
445 | + * Computes the digest hash value using the specified digest method. |
|
446 | + * If that digest method fails to produce a valid hash value, |
|
447 | + * then we'll grab the next digest method and recursively try again until something works. |
|
448 | + * |
|
449 | + * @param string $digest_method |
|
450 | + * @param string $encryption_key |
|
451 | + * @return string |
|
452 | + * @throws RuntimeException |
|
453 | + */ |
|
454 | + protected function getDigestHashValue($digest_method = EE_Encryption::OPENSSL_DIGEST_METHOD, $encryption_key = '') |
|
455 | + { |
|
456 | + $encryption_key = $encryption_key !== '' |
|
457 | + ? $encryption_key |
|
458 | + : $this->get_encryption_key(); |
|
459 | + $digest_hash_value = openssl_digest($encryption_key, $digest_method); |
|
460 | + if ($digest_hash_value === false) { |
|
461 | + return $this->getDigestHashValue($this->getDigestMethod()); |
|
462 | + } |
|
463 | + return $digest_hash_value; |
|
464 | + } |
|
465 | + |
|
466 | + |
|
467 | + /** |
|
468 | + * Returns the NEXT element in the $digest_methods array. |
|
469 | + * If the $digest_methods array is empty, then we populate it |
|
470 | + * with the available values returned from openssl_get_md_methods(). |
|
471 | + * |
|
472 | + * @return string |
|
473 | + * @throws \RuntimeException |
|
474 | + */ |
|
475 | + protected function getDigestMethod() |
|
476 | + { |
|
477 | + $digest_method = prev($this->digest_methods); |
|
478 | + if (empty($this->digest_methods)) { |
|
479 | + $this->digest_methods = openssl_get_md_methods(); |
|
480 | + $digest_method = end($this->digest_methods); |
|
481 | + } |
|
482 | + if ($digest_method === false) { |
|
483 | + throw new RuntimeException( |
|
484 | + esc_html__( |
|
485 | + 'OpenSSL support appears to be enabled on the server, but no digest methods are available. Please contact the server administrator.', |
|
486 | + 'event_espresso' |
|
487 | + ) |
|
488 | + ); |
|
489 | + } |
|
490 | + return $digest_method; |
|
491 | + } |
|
492 | + |
|
493 | + |
|
494 | + /** |
|
495 | + * encrypts data for acme servers that didn't bother to install PHP mcrypt |
|
496 | + * |
|
497 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
498 | + * @param string $text_string the text to be decrypted |
|
499 | + * @return string |
|
500 | + */ |
|
501 | + protected function acme_encrypt($text_string = '') |
|
502 | + { |
|
503 | + // you give me nothing??? GET OUT ! |
|
504 | + if (empty($text_string)) { |
|
505 | + return $text_string; |
|
506 | + } |
|
507 | + $key_bits = str_split( |
|
508 | + str_pad( |
|
509 | + '', |
|
510 | + strlen($text_string), |
|
511 | + $this->get_encryption_key(), |
|
512 | + STR_PAD_RIGHT |
|
513 | + ) |
|
514 | + ); |
|
515 | + $string_bits = str_split($text_string); |
|
516 | + foreach ($string_bits as $k => $v) { |
|
517 | + $temp = ord($v) + ord($key_bits[ $k ]); |
|
518 | + $string_bits[ $k ] = chr($temp > 255 ? ($temp - 256) : $temp); |
|
519 | + } |
|
520 | + $encrypted_text = implode('', $string_bits); |
|
521 | + $encrypted_text .= EE_Encryption::ACME_ENCRYPTION_FLAG; |
|
522 | + return $this->_use_base64_encode |
|
523 | + ? base64_encode($encrypted_text) |
|
524 | + : $encrypted_text; |
|
525 | + } |
|
526 | + |
|
527 | + |
|
528 | + /** |
|
529 | + * decrypts data for acme servers that didn't bother to install PHP mcrypt |
|
530 | + * |
|
531 | + * @see http://stackoverflow.com/questions/800922/how-to-encrypt-string-without-mcrypt-library-in-php |
|
532 | + * @param string $encrypted_text the text to be decrypted |
|
533 | + * @return string |
|
534 | + * @throws RuntimeException |
|
535 | + */ |
|
536 | + protected function acme_decrypt($encrypted_text = '') |
|
537 | + { |
|
538 | + // you give me nothing??? GET OUT ! |
|
539 | + if (empty($encrypted_text)) { |
|
540 | + return $encrypted_text; |
|
541 | + } |
|
542 | + // decode the data ? |
|
543 | + $encrypted_text = $this->valid_base_64($encrypted_text) |
|
544 | + ? $this->base64_url_decode($encrypted_text) |
|
545 | + : $encrypted_text; |
|
546 | + $encrypted_text = substr($encrypted_text, 0, -4); |
|
547 | + $key_bits = str_split( |
|
548 | + str_pad( |
|
549 | + '', |
|
550 | + strlen($encrypted_text), |
|
551 | + $this->get_encryption_key(), |
|
552 | + STR_PAD_RIGHT |
|
553 | + ) |
|
554 | + ); |
|
555 | + $string_bits = str_split($encrypted_text); |
|
556 | + foreach ($string_bits as $k => $v) { |
|
557 | + $temp = ord($v) - ord($key_bits[ $k ]); |
|
558 | + $string_bits[ $k ] = chr($temp < 0 ? ($temp + 256) : $temp); |
|
559 | + } |
|
560 | + return implode('', $string_bits); |
|
561 | + } |
|
562 | + |
|
563 | + |
|
564 | + /** |
|
565 | + * @see http://stackoverflow.com/questions/2556345/detect-base64-encoding-in-php#30231906 |
|
566 | + * @param $string |
|
567 | + * @return bool |
|
568 | + */ |
|
569 | + protected function valid_base_64($string) |
|
570 | + { |
|
571 | + // ensure data is a string |
|
572 | + if (! is_string($string) || ! $this->_use_base64_encode) { |
|
573 | + return false; |
|
574 | + } |
|
575 | + $decoded = base64_decode($string, true); |
|
576 | + // Check if there is no invalid character in string |
|
577 | + if (! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
578 | + return false; |
|
579 | + } |
|
580 | + // Decode the string in strict mode and send the response |
|
581 | + if (! base64_decode($string, true)) { |
|
582 | + return false; |
|
583 | + } |
|
584 | + // Encode and compare it to original one |
|
585 | + return base64_encode($decoded) === $string; |
|
586 | + } |
|
587 | + |
|
588 | + |
|
589 | + /** |
|
590 | + * generate random string |
|
591 | + * |
|
592 | + * @see http://stackoverflow.com/questions/637278/what-is-the-best-way-to-generate-a-random-key-within-php |
|
593 | + * @param int $length number of characters for random string |
|
594 | + * @return string |
|
595 | + */ |
|
596 | + public function generate_random_string($length = 40) |
|
597 | + { |
|
598 | + $iterations = ceil($length / 40); |
|
599 | + $random_string = ''; |
|
600 | + for ($i = 0; $i < $iterations; $i++) { |
|
601 | + $random_string .= sha1(microtime(true) . mt_rand(10000, 90000)); |
|
602 | + } |
|
603 | + $random_string = substr($random_string, 0, $length); |
|
604 | + return $random_string; |
|
605 | + } |
|
606 | 606 | } |