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
150 /* TODO Throw proper Error exceptions for uninitialized classes and failure to clone */
151 if (to->utimezone == NULL) {
152 spprintf(&message, 0, "%s: passed IntlTimeZone is not "
153 "properly constructed", func);
154 if (message) {
155 intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
156 efree(message);
157 }
158 zval_ptr_dtor_str(&local_zv_tz);
159 return NULL;
160 }
161 timeZone = to->utimezone->clone();
162 if (UNEXPECTED(timeZone == NULL)) {
163 spprintf(&message, 0, "%s: could not clone TimeZone", func);
164 if (message) {
165 intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
166 efree(message);
167 }
168 zval_ptr_dtor_str(&local_zv_tz);
169 return NULL;
170 }
171 } else if (Z_TYPE_P(zv_timezone) == IS_OBJECT &&
172 instanceof_function(Z_OBJCE_P(zv_timezone), php_date_get_timezone_ce())) {
173
174 php_timezone_obj *tzobj = Z_PHPTIMEZONE_P(zv_timezone);
175
176 zval_ptr_dtor_str(&local_zv_tz);
177 return timezone_convert_datetimezone(tzobj->type, tzobj, 0,
178 outside_error, func);
179 } else {
180 UnicodeString id;
181 UErrorCode status = U_ZERO_ERROR; /* outside_error may be NULL */
182 if (!try_convert_to_string(zv_timezone)) {
183 zval_ptr_dtor_str(&local_zv_tz);
184 return NULL;
185 }
186 if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone),
187 &status) == FAILURE) {
188 spprintf(&message, 0, "%s: Time zone identifier given is not a "
189 "valid UTF-8 string", func);
190 if (message) {
191 intl_errors_set(outside_error, status, message, 1);
192 efree(message);
193 }
194 zval_ptr_dtor_str(&local_zv_tz);
195 return NULL;
196 }
197 timeZone = TimeZone::createTimeZone(id);
198 if (UNEXPECTED(timeZone == NULL)) {
199 spprintf(&message, 0, "%s: Could not create time zone", func);
200 if (message) {
201 intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
202 efree(message);
203 }
204 zval_ptr_dtor_str(&local_zv_tz);
205 return NULL;
206 }
207 if (*timeZone == TimeZone::getUnknown()) {
208 spprintf(&message, 0, "%s: No such time zone: '%s'",
209 func, Z_STRVAL_P(zv_timezone));
210 if (message) {
211 intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
212 efree(message);
213 }
214 zval_ptr_dtor_str(&local_zv_tz);
215 delete timeZone;
216 return NULL;
217 }
218 }
219
220 zval_ptr_dtor_str(&local_zv_tz);
221
222 return timeZone;
223 }
224 /* }}} */
225
226 /* {{{ clone handler for TimeZone */
TimeZone_clone_obj(zend_object * object)227 static zend_object *TimeZone_clone_obj(zend_object *object)
228 {
229 TimeZone_object *to_orig = php_intl_timezone_fetch_object(object);
230 zend_object *ret_val = TimeZone_ce_ptr->create_object(object->ce);
231 TimeZone_object *to_new = php_intl_timezone_fetch_object(ret_val);
232
233 zend_objects_clone_members(&to_new->zo, &to_orig->zo);
234
235 if (to_orig->utimezone != NULL) {
236 TimeZone *newTimeZone = to_orig->utimezone->clone();
237 to_new->should_delete = true;
238 if (!newTimeZone) {
239 zend_throw_error(NULL, "Failed to clone IntlTimeZone");
240 } else {
241 to_new->utimezone = newTimeZone;
242 }
243 } else {
244 zend_throw_error(NULL, "Cannot clone uninitialized IntlTimeZone");
245 }
246
247 return ret_val;
248 }
249 /* }}} */
250
251 /* {{{ compare_objects handler for TimeZone
252 * Can't be used for >, >=, <, <= comparisons */
TimeZone_compare_objects(zval * object1,zval * object2)253 static int TimeZone_compare_objects(zval *object1, zval *object2)
254 {
255 TimeZone_object *to1,
256 *to2;
257
258 ZEND_COMPARE_OBJECTS_FALLBACK(object1, object2);
259
260 to1 = Z_INTL_TIMEZONE_P(object1);
261 to2 = Z_INTL_TIMEZONE_P(object2);
262
263 if (to1->utimezone == NULL || to2->utimezone == NULL) {
264 zend_throw_exception(NULL, "Comparison with at least one unconstructed "
265 "IntlTimeZone operand", 0);
266 /* intentionally not returning */
267 } else {
268 if (*to1->utimezone == *to2->utimezone) {
269 return 0;
270 }
271 }
272
273 return ZEND_UNCOMPARABLE;
274 }
275 /* }}} */
276
277 /* {{{ get_debug_info handler for TimeZone */
TimeZone_get_debug_info(zend_object * object,int * is_temp)278 static HashTable *TimeZone_get_debug_info(zend_object *object, int *is_temp)
279 {
280 zval zv;
281 TimeZone_object *to;
282 const TimeZone *tz;
283 UnicodeString ustr;
284 zend_string *u8str;
285 HashTable *debug_info;
286 UErrorCode uec = U_ZERO_ERROR;
287
288 *is_temp = 1;
289
290 debug_info = zend_new_array(8);
291
292 to = php_intl_timezone_fetch_object(object);
293 tz = to->utimezone;
294
295 if (tz == NULL) {
296 ZVAL_FALSE(&zv);
297 zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
298 return debug_info;
299 }
300
301 ZVAL_TRUE(&zv);
302 zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
303
304 tz->getID(ustr);
305 u8str = intl_convert_utf16_to_utf8(
306 ustr.getBuffer(), ustr.length(), &uec);
307 if (!u8str) {
308 return debug_info;
309 }
310 ZVAL_NEW_STR(&zv, u8str);
311 zend_hash_str_update(debug_info, "id", sizeof("id") - 1, &zv);
312
313 int32_t rawOffset, dstOffset;
314 UDate now = Calendar::getNow();
315 tz->getOffset(now, false, rawOffset, dstOffset, uec);
316 if (U_FAILURE(uec)) {
317 return debug_info;
318 }
319
320 ZVAL_LONG(&zv, (zend_long)rawOffset);
321 zend_hash_str_update(debug_info,"rawOffset", sizeof("rawOffset") - 1, &zv);
322 ZVAL_LONG(&zv, (zend_long)(rawOffset + dstOffset));
323 zend_hash_str_update(debug_info,"currentOffset", sizeof("currentOffset") - 1, &zv);
324
325 return debug_info;
326 }
327 /* }}} */
328
329 /* {{{ void TimeZone_object_init(TimeZone_object* to)
330 * Initialize internals of TImeZone_object not specific to zend standard objects.
331 */
TimeZone_object_init(TimeZone_object * to)332 static void TimeZone_object_init(TimeZone_object *to)
333 {
334 intl_error_init(TIMEZONE_ERROR_P(to));
335 to->utimezone = NULL;
336 to->should_delete = 0;
337 }
338 /* }}} */
339
340 /* {{{ TimeZone_objects_free */
TimeZone_objects_free(zend_object * object)341 static void TimeZone_objects_free(zend_object *object)
342 {
343 TimeZone_object* to = php_intl_timezone_fetch_object(object);
344
345 if (to->utimezone && to->should_delete) {
346 delete to->utimezone;
347 to->utimezone = NULL;
348 }
349 intl_error_reset(TIMEZONE_ERROR_P(to));
350
351 zend_object_std_dtor(&to->zo);
352 }
353 /* }}} */
354
355 /* {{{ TimeZone_object_create */
TimeZone_object_create(zend_class_entry * ce)356 static zend_object *TimeZone_object_create(zend_class_entry *ce)
357 {
358 TimeZone_object* intern;
359
360 intern = (TimeZone_object*)ecalloc(1, sizeof(TimeZone_object) + sizeof(zval) * (ce->default_properties_count - 1));
361
362 zend_object_std_init(&intern->zo, ce);
363 object_properties_init(&intern->zo, ce);
364 TimeZone_object_init(intern);
365
366 return &intern->zo;
367 }
368 /* }}} */
369
370 /* {{{ timezone_register_IntlTimeZone_class
371 * Initialize 'IntlTimeZone' class
372 */
timezone_register_IntlTimeZone_class(void)373 U_CFUNC void timezone_register_IntlTimeZone_class(void)
374 {
375 /* Create and register 'IntlTimeZone' class. */
376 TimeZone_ce_ptr = register_class_IntlTimeZone();
377 TimeZone_ce_ptr->create_object = TimeZone_object_create;
378 TimeZone_ce_ptr->default_object_handlers = &TimeZone_handlers;
379
380 memcpy(&TimeZone_handlers, &std_object_handlers,
381 sizeof TimeZone_handlers);
382 TimeZone_handlers.offset = XtOffsetOf(TimeZone_object, zo);
383 TimeZone_handlers.clone_obj = TimeZone_clone_obj;
384 TimeZone_handlers.compare = TimeZone_compare_objects;
385 TimeZone_handlers.get_debug_info = TimeZone_get_debug_info;
386 TimeZone_handlers.free_obj = TimeZone_objects_free;
387 }
388 /* }}} */
389