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    | http://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 (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 (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_dtor */
TimeZone_objects_dtor(zend_object * object)355 static void TimeZone_objects_dtor(zend_object *object)
356 {
357 	zend_objects_destroy_object(object);
358 }
359 /* }}} */
360 
361 /* {{{ TimeZone_objects_free */
TimeZone_objects_free(zend_object * object)362 static void TimeZone_objects_free(zend_object *object)
363 {
364 	TimeZone_object* to = php_intl_timezone_fetch_object(object);
365 
366 	if (to->utimezone && to->should_delete) {
367 		delete to->utimezone;
368 		to->utimezone = NULL;
369 	}
370 	intl_error_reset(TIMEZONE_ERROR_P(to));
371 
372 	zend_object_std_dtor(&to->zo);
373 }
374 /* }}} */
375 
376 /* {{{ TimeZone_object_create */
TimeZone_object_create(zend_class_entry * ce)377 static zend_object *TimeZone_object_create(zend_class_entry *ce)
378 {
379 	TimeZone_object*	intern;
380 
381 	intern = (TimeZone_object*)ecalloc(1, sizeof(TimeZone_object) + sizeof(zval) * (ce->default_properties_count - 1));
382 
383 	zend_object_std_init(&intern->zo, ce);
384     object_properties_init(&intern->zo, ce);
385 	TimeZone_object_init(intern);
386 
387 	intern->zo.handlers = &TimeZone_handlers;
388 
389 	return &intern->zo;
390 }
391 /* }}} */
392 
393 /* {{{ timezone_register_IntlTimeZone_class
394  * Initialize 'IntlTimeZone' class
395  */
timezone_register_IntlTimeZone_class(void)396 U_CFUNC void timezone_register_IntlTimeZone_class(void)
397 {
398 	zend_class_entry ce;
399 
400 	/* Create and register 'IntlTimeZone' class. */
401 	INIT_CLASS_ENTRY(ce, "IntlTimeZone", class_IntlTimeZone_methods);
402 	ce.create_object = TimeZone_object_create;
403 	TimeZone_ce_ptr = zend_register_internal_class(&ce);
404 	if (!TimeZone_ce_ptr) {
405 		//can't happen now without bigger problems before
406 		php_error_docref(NULL, E_ERROR,
407 			"IntlTimeZone: class registration has failed.");
408 		return;
409 	}
410 
411 	memcpy(&TimeZone_handlers, &std_object_handlers,
412 		sizeof TimeZone_handlers);
413 	TimeZone_handlers.offset = XtOffsetOf(TimeZone_object, zo);
414 	TimeZone_handlers.clone_obj = TimeZone_clone_obj;
415 	TimeZone_handlers.compare = TimeZone_compare_objects;
416 	TimeZone_handlers.get_debug_info = TimeZone_get_debug_info;
417 	TimeZone_handlers.dtor_obj = TimeZone_objects_dtor;
418 	TimeZone_handlers.free_obj = TimeZone_objects_free;
419 
420 
421 	/* Declare 'IntlTimeZone' class constants */
422 #define TIMEZONE_DECL_LONG_CONST(name, val) \
423 	zend_declare_class_constant_long(TimeZone_ce_ptr, name, sizeof(name) - 1, \
424 		val)
425 
426 	TIMEZONE_DECL_LONG_CONST("DISPLAY_SHORT", TimeZone::SHORT);
427 	TIMEZONE_DECL_LONG_CONST("DISPLAY_LONG", TimeZone::LONG);
428 
429 	TIMEZONE_DECL_LONG_CONST("DISPLAY_SHORT_GENERIC", TimeZone::SHORT_GENERIC);
430 	TIMEZONE_DECL_LONG_CONST("DISPLAY_LONG_GENERIC", TimeZone::LONG_GENERIC);
431 	TIMEZONE_DECL_LONG_CONST("DISPLAY_SHORT_GMT", TimeZone::SHORT_GMT);
432 	TIMEZONE_DECL_LONG_CONST("DISPLAY_LONG_GMT", TimeZone::LONG_GMT);
433 	TIMEZONE_DECL_LONG_CONST("DISPLAY_SHORT_COMMONLY_USED", TimeZone::SHORT_COMMONLY_USED);
434 	TIMEZONE_DECL_LONG_CONST("DISPLAY_GENERIC_LOCATION", TimeZone::GENERIC_LOCATION);
435 
436 	TIMEZONE_DECL_LONG_CONST("TYPE_ANY", UCAL_ZONE_TYPE_ANY);
437 	TIMEZONE_DECL_LONG_CONST("TYPE_CANONICAL", UCAL_ZONE_TYPE_CANONICAL);
438 	TIMEZONE_DECL_LONG_CONST("TYPE_CANONICAL_LOCATION", UCAL_ZONE_TYPE_CANONICAL_LOCATION);
439 
440 	/* Declare 'IntlTimeZone' class properties */
441 
442 }
443 /* }}} */
444