1 /*
2    +----------------------------------------------------------------------+
3    | This source file is subject to version 3.01 of the PHP license,      |
4    | that is bundled with this package in the file LICENSE, and is        |
5    | available through the world-wide-web at the following url:           |
6    | https://www.php.net/license/3_01.txt                                 |
7    | If you did not receive a copy of the PHP license and are unable to   |
8    | obtain it through the world-wide-web, please send a note to          |
9    | license@php.net so we can mail you a copy immediately.               |
10    +----------------------------------------------------------------------+
11    | Authors: Gustavo Lopes <cataphract@php.net>                          |
12    +----------------------------------------------------------------------+
13 */
14 
15 
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 
20 #include "../intl_cppshims.h"
21 
22 #include <unicode/timezone.h>
23 #include <unicode/calendar.h>
24 #include "../intl_convertcpp.h"
25 
26 #include "../common/common_date.h"
27 
28 extern "C" {
29 #include "../intl_convert.h"
30 #define USE_TIMEZONE_POINTER 1
31 #include "timezone_class.h"
32 #include "timezone_arginfo.h"
33 #include <zend_exceptions.h>
34 #include <zend_interfaces.h>
35 #include <ext/date/php_date.h>
36 }
37 
38 using icu::Calendar;
39 
40 /* {{{ Global variables */
41 U_CDECL_BEGIN
42 zend_class_entry *TimeZone_ce_ptr = NULL;
43 zend_object_handlers TimeZone_handlers;
44 U_CDECL_END
45 /* }}} */
46 
47 /* {{{ timezone_object_construct */
timezone_object_construct(const TimeZone * zone,zval * object,int owned)48 U_CFUNC void timezone_object_construct(const TimeZone *zone, zval *object, int owned)
49 {
50 	TimeZone_object	*to;
51 
52 	object_init_ex(object, TimeZone_ce_ptr);
53 	TIMEZONE_METHOD_FETCH_OBJECT_NO_CHECK; /* fetch zend object from zval "object" into "to" */
54 	to->utimezone = zone;
55 	to->should_delete = owned;
56 }
57 /* }}} */
58 
59 /* {{{ timezone_convert_to_datetimezone
60  *	   Convert from TimeZone to DateTimeZone object */
timezone_convert_to_datetimezone(const TimeZone * timeZone,intl_error * outside_error,const char * func,zval * ret)61 U_CFUNC zval *timezone_convert_to_datetimezone(const TimeZone *timeZone,
62 											   intl_error *outside_error,
63 											   const char *func, zval *ret)
64 {
65 	UnicodeString		id;
66 	char				*message = NULL;
67 	php_timezone_obj	*tzobj;
68 	zval				arg;
69 
70 	timeZone->getID(id);
71 	if (id.isBogus()) {
72 		spprintf(&message, 0, "%s: could not obtain TimeZone id", func);
73 		intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR,
74 			message, 1);
75 		goto error;
76 	}
77 
78 	object_init_ex(ret, php_date_get_timezone_ce());
79 	tzobj = Z_PHPTIMEZONE_P(ret);
80 
81 	if (id.compare(0, 3, UnicodeString("GMT", sizeof("GMT")-1, US_INV)) == 0) {
82 		/* The DateTimeZone constructor doesn't support offset time zones,
83 		 * so we must mess with DateTimeZone structure ourselves */
84 		tzobj->initialized	  = 1;
85 		tzobj->type			  = TIMELIB_ZONETYPE_OFFSET;
86 		//convert offset from milliseconds to seconds
87 		tzobj->tzi.utc_offset = timeZone->getRawOffset() / 1000;
88 	} else {
89 		zend_string *u8str;
90 		/* Call the constructor! */
91 		u8str = intl_charFromString(id, &INTL_ERROR_CODE(*outside_error));
92 		if (!u8str) {
93 			spprintf(&message, 0, "%s: could not convert id to UTF-8", func);
94 			intl_errors_set(outside_error, INTL_ERROR_CODE(*outside_error),
95 				message, 1);
96 			goto error;
97 		}
98 		ZVAL_STR(&arg, u8str);
99 		zend_call_known_instance_method_with_1_params(
100 			Z_OBJCE_P(ret)->constructor, Z_OBJ_P(ret), NULL, &arg);
101 		if (EG(exception)) {
102 			spprintf(&message, 0,
103 				"%s: DateTimeZone constructor threw exception", func);
104 			intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR,
105 				message, 1);
106 			zend_object_store_ctor_failed(Z_OBJ_P(ret));
107 			zval_ptr_dtor(&arg);
108 			goto error;
109 		}
110 		zval_ptr_dtor(&arg);
111 	}
112 
113 	if (0) {
114 error:
115 		if (ret) {
116 			zval_ptr_dtor(ret);
117 		}
118 		ret = NULL;
119 	}
120 
121 	if (message) {
122 		efree(message);
123 	}
124 	return ret;
125 }
126 /* }}} */
127 
128 /* {{{ timezone_process_timezone_argument
129  * TimeZone argument processor. outside_error may be NULL (for static functions/constructors) */
timezone_process_timezone_argument(zval * zv_timezone,intl_error * outside_error,const char * func)130 U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone,
131 													 intl_error *outside_error,
132 													 const char *func)
133 {
134 	zval		local_zv_tz;
135 	char		*message = NULL;
136 	TimeZone	*timeZone;
137 
138 	if (zv_timezone == NULL || Z_TYPE_P(zv_timezone) == IS_NULL) {
139 		timelib_tzinfo *tzinfo = get_timezone_info();
140 		ZVAL_STRING(&local_zv_tz, tzinfo->name);
141 		zv_timezone = &local_zv_tz;
142 	} else {
143 		ZVAL_NULL(&local_zv_tz);
144 	}
145 
146 	if (Z_TYPE_P(zv_timezone) == IS_OBJECT &&
147 			instanceof_function(Z_OBJCE_P(zv_timezone), TimeZone_ce_ptr)) {
148 		TimeZone_object *to = Z_INTL_TIMEZONE_P(zv_timezone);
149 		if (to->utimezone == NULL) {
150 			spprintf(&message, 0, "%s: passed IntlTimeZone is not "
151 				"properly constructed", func);
152 			if (message) {
153 				intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
154 				efree(message);
155 			}
156 			zval_ptr_dtor_str(&local_zv_tz);
157 			return NULL;
158 		}
159 		timeZone = to->utimezone->clone();
160 		if (UNEXPECTED(timeZone == NULL)) {
161 			spprintf(&message, 0, "%s: could not clone TimeZone", func);
162 			if (message) {
163 				intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
164 				efree(message);
165 			}
166 			zval_ptr_dtor_str(&local_zv_tz);
167 			return NULL;
168 		}
169 	} else if (Z_TYPE_P(zv_timezone) == IS_OBJECT &&
170 			instanceof_function(Z_OBJCE_P(zv_timezone), php_date_get_timezone_ce())) {
171 
172 		php_timezone_obj *tzobj = Z_PHPTIMEZONE_P(zv_timezone);
173 
174 		zval_ptr_dtor_str(&local_zv_tz);
175 		return timezone_convert_datetimezone(tzobj->type, tzobj, 0,
176 			outside_error, func);
177 	} else {
178 		UnicodeString	id;
179 		UErrorCode		status = U_ZERO_ERROR; /* outside_error may be NULL */
180 		if (!try_convert_to_string(zv_timezone)) {
181 			zval_ptr_dtor_str(&local_zv_tz);
182 			return NULL;
183 		}
184 		if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone),
185 				&status) == FAILURE) {
186 			spprintf(&message, 0, "%s: Time zone identifier given is not a "
187 				"valid UTF-8 string", func);
188 			if (message) {
189 				intl_errors_set(outside_error, status, message, 1);
190 				efree(message);
191 			}
192 			zval_ptr_dtor_str(&local_zv_tz);
193 			return NULL;
194 		}
195 		timeZone = TimeZone::createTimeZone(id);
196 		if (UNEXPECTED(timeZone == NULL)) {
197 			spprintf(&message, 0, "%s: Could not create time zone", func);
198 			if (message) {
199 				intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
200 				efree(message);
201 			}
202 			zval_ptr_dtor_str(&local_zv_tz);
203 			return NULL;
204 		}
205 		if (*timeZone == TimeZone::getUnknown()) {
206 			spprintf(&message, 0, "%s: No such time zone: '%s'",
207 				func, Z_STRVAL_P(zv_timezone));
208 			if (message) {
209 				intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
210 				efree(message);
211 			}
212 			zval_ptr_dtor_str(&local_zv_tz);
213 			delete timeZone;
214 			return NULL;
215 		}
216 	}
217 
218 	zval_ptr_dtor_str(&local_zv_tz);
219 
220 	return timeZone;
221 }
222 /* }}} */
223 
224 /* {{{ clone handler for TimeZone */
TimeZone_clone_obj(zend_object * object)225 static zend_object *TimeZone_clone_obj(zend_object *object)
226 {
227 	TimeZone_object		*to_orig,
228 						*to_new;
229 	zend_object			*ret_val;
230 	intl_error_reset(NULL);
231 
232 	to_orig = php_intl_timezone_fetch_object(object);
233 	intl_error_reset(TIMEZONE_ERROR_P(to_orig));
234 
235 	ret_val = TimeZone_ce_ptr->create_object(object->ce);
236 	to_new  = php_intl_timezone_fetch_object(ret_val);
237 
238 	zend_objects_clone_members(&to_new->zo, &to_orig->zo);
239 
240 	if (to_orig->utimezone != NULL) {
241 		TimeZone	*newTimeZone;
242 
243 		newTimeZone = to_orig->utimezone->clone();
244 		to_new->should_delete = 1;
245 		if (!newTimeZone) {
246 			zend_string *err_msg;
247 			intl_errors_set_code(TIMEZONE_ERROR_P(to_orig),
248 				U_MEMORY_ALLOCATION_ERROR);
249 			intl_errors_set_custom_msg(TIMEZONE_ERROR_P(to_orig),
250 				"Could not clone IntlTimeZone", 0);
251 			err_msg = intl_error_get_message(TIMEZONE_ERROR_P(to_orig));
252 			zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
253 			zend_string_free(err_msg);
254 		} else {
255 			to_new->utimezone = newTimeZone;
256 		}
257 	} else {
258 		zend_throw_exception(NULL, "Cannot clone unconstructed IntlTimeZone", 0);
259 	}
260 
261 	return ret_val;
262 }
263 /* }}} */
264 
265 /* {{{ compare_objects handler for TimeZone
266  *     Can't be used for >, >=, <, <= comparisons */
TimeZone_compare_objects(zval * object1,zval * object2)267 static int TimeZone_compare_objects(zval *object1, zval *object2)
268 {
269 	TimeZone_object		*to1,
270 						*to2;
271 
272 	ZEND_COMPARE_OBJECTS_FALLBACK(object1, object2);
273 
274 	to1 = Z_INTL_TIMEZONE_P(object1);
275 	to2 = Z_INTL_TIMEZONE_P(object2);
276 
277 	if (to1->utimezone == NULL || to2->utimezone == NULL) {
278 		zend_throw_exception(NULL, "Comparison with at least one unconstructed "
279 				"IntlTimeZone operand", 0);
280 		/* intentionally not returning */
281 	} else {
282 		if (*to1->utimezone == *to2->utimezone) {
283 			return 0;
284 		}
285 	}
286 
287 	return ZEND_UNCOMPARABLE;
288 }
289 /* }}} */
290 
291 /* {{{ get_debug_info handler for TimeZone */
TimeZone_get_debug_info(zend_object * object,int * is_temp)292 static HashTable *TimeZone_get_debug_info(zend_object *object, int *is_temp)
293 {
294 	zval			zv;
295 	TimeZone_object	*to;
296 	const TimeZone	*tz;
297 	UnicodeString	ustr;
298 	zend_string     *u8str;
299 	HashTable 		*debug_info;
300 	UErrorCode		uec = U_ZERO_ERROR;
301 
302 	*is_temp = 1;
303 
304 	debug_info = zend_new_array(8);
305 
306 	to = php_intl_timezone_fetch_object(object);
307 	tz = to->utimezone;
308 
309 	if (tz == NULL) {
310 		ZVAL_FALSE(&zv);
311 		zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
312 		return debug_info;
313 	}
314 
315 	ZVAL_TRUE(&zv);
316 	zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
317 
318 	tz->getID(ustr);
319 	u8str = intl_convert_utf16_to_utf8(
320 		ustr.getBuffer(), ustr.length(), &uec);
321 	if (!u8str) {
322 		return debug_info;
323 	}
324 	ZVAL_NEW_STR(&zv, u8str);
325 	zend_hash_str_update(debug_info, "id", sizeof("id") - 1, &zv);
326 
327 	int32_t rawOffset, dstOffset;
328 	UDate now = Calendar::getNow();
329 	tz->getOffset(now, false, rawOffset, dstOffset, uec);
330 	if (U_FAILURE(uec)) {
331 		return debug_info;
332 	}
333 
334 	ZVAL_LONG(&zv, (zend_long)rawOffset);
335 	zend_hash_str_update(debug_info,"rawOffset", sizeof("rawOffset") - 1, &zv);
336 	ZVAL_LONG(&zv, (zend_long)(rawOffset + dstOffset));
337 	zend_hash_str_update(debug_info,"currentOffset", sizeof("currentOffset") - 1, &zv);
338 
339 	return debug_info;
340 }
341 /* }}} */
342 
343 /* {{{ void TimeZone_object_init(TimeZone_object* to)
344  * Initialize internals of TImeZone_object not specific to zend standard objects.
345  */
TimeZone_object_init(TimeZone_object * to)346 static void TimeZone_object_init(TimeZone_object *to)
347 {
348 	intl_error_init(TIMEZONE_ERROR_P(to));
349 	to->utimezone = NULL;
350 	to->should_delete = 0;
351 }
352 /* }}} */
353 
354 /* {{{ TimeZone_objects_free */
TimeZone_objects_free(zend_object * object)355 static void TimeZone_objects_free(zend_object *object)
356 {
357 	TimeZone_object* to = php_intl_timezone_fetch_object(object);
358 
359 	if (to->utimezone && to->should_delete) {
360 		delete to->utimezone;
361 		to->utimezone = NULL;
362 	}
363 	intl_error_reset(TIMEZONE_ERROR_P(to));
364 
365 	zend_object_std_dtor(&to->zo);
366 }
367 /* }}} */
368 
369 /* {{{ TimeZone_object_create */
TimeZone_object_create(zend_class_entry * ce)370 static zend_object *TimeZone_object_create(zend_class_entry *ce)
371 {
372 	TimeZone_object*	intern;
373 
374 	intern = (TimeZone_object*)ecalloc(1, sizeof(TimeZone_object) + sizeof(zval) * (ce->default_properties_count - 1));
375 
376 	zend_object_std_init(&intern->zo, ce);
377     object_properties_init(&intern->zo, ce);
378 	TimeZone_object_init(intern);
379 
380 	return &intern->zo;
381 }
382 /* }}} */
383 
384 /* {{{ timezone_register_IntlTimeZone_class
385  * Initialize 'IntlTimeZone' class
386  */
timezone_register_IntlTimeZone_class(void)387 U_CFUNC void timezone_register_IntlTimeZone_class(void)
388 {
389 	/* Create and register 'IntlTimeZone' class. */
390 	TimeZone_ce_ptr = register_class_IntlTimeZone();
391 	TimeZone_ce_ptr->create_object = TimeZone_object_create;
392 	TimeZone_ce_ptr->default_object_handlers = &TimeZone_handlers;
393 
394 	memcpy(&TimeZone_handlers, &std_object_handlers,
395 		sizeof TimeZone_handlers);
396 	TimeZone_handlers.offset = XtOffsetOf(TimeZone_object, zo);
397 	TimeZone_handlers.clone_obj = TimeZone_clone_obj;
398 	TimeZone_handlers.compare = TimeZone_compare_objects;
399 	TimeZone_handlers.get_debug_info = TimeZone_get_debug_info;
400 	TimeZone_handlers.free_obj = TimeZone_objects_free;
401 }
402 /* }}} */
403