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 #include "../intl_cppshims.h"
16 
17 #include <unicode/calendar.h>
18 #include <unicode/gregocal.h>
19 #include <unicode/datefmt.h>
20 #include <unicode/smpdtfmt.h>
21 #include <unicode/locid.h>
22 
23 #include "../intl_convertcpp.h"
24 
25 extern "C" {
26 #include "../php_intl.h"
27 #include "../locale/locale.h"
28 #define USE_CALENDAR_POINTER 1
29 #include "../calendar/calendar_class.h"
30 #include <ext/date/php_date.h>
31 #include "../common/common_date.h"
32 }
33 
34 using icu::Locale;
35 using icu::DateFormat;
36 using icu::GregorianCalendar;
37 using icu::StringPiece;
38 using icu::SimpleDateFormat;
39 
40 static constexpr DateFormat::EStyle valid_styles[] = {
41 		DateFormat::kNone,
42 		DateFormat::kFull,
43 		DateFormat::kLong,
44 		DateFormat::kMedium,
45 		DateFormat::kShort,
46 		DateFormat::kFullRelative,
47 		DateFormat::kLongRelative,
48 		DateFormat::kMediumRelative,
49 		DateFormat::kShortRelative,
50 };
51 
valid_format(zval * z)52 static bool valid_format(zval *z) {
53 	if (Z_TYPE_P(z) == IS_LONG) {
54 		zend_long lval = Z_LVAL_P(z);
55 		for (int i = 0; i < sizeof(valid_styles) / sizeof(*valid_styles); i++) {
56 			if ((zend_long)valid_styles[i] == lval) {
57 				return true;
58 			}
59 		}
60 	}
61 
62 	return false;
63 }
64 
PHP_FUNCTION(datefmt_format_object)65 U_CFUNC PHP_FUNCTION(datefmt_format_object)
66 {
67 	zval				*object,
68 						*format = NULL;
69 	const char			*locale_str	= NULL;
70 	size_t				locale_len;
71 	bool				pattern		= false;
72 	UDate				date;
73 	TimeZone			*timeZone	= NULL;
74 	UErrorCode			status		= U_ZERO_ERROR;
75 	DateFormat			*df			= NULL;
76 	Calendar			*cal		= NULL;
77 	DateFormat::EStyle	dateStyle = DateFormat::kDefault,
78 						timeStyle = DateFormat::kDefault;
79 
80 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|zs!",
81 			&object, &format, &locale_str, &locale_len) == FAILURE) {
82 		RETURN_THROWS();
83 	}
84 
85 	if (!locale_str) {
86 		locale_str = intl_locale_get_default();
87 	}
88 
89 	if (format == NULL || Z_TYPE_P(format) == IS_NULL) {
90 		//nothing
91 	} else if (Z_TYPE_P(format) == IS_ARRAY) {
92 		HashTable *ht = Z_ARRVAL_P(format);
93 		if (zend_hash_num_elements(ht) != 2) {
94 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
95 					"datefmt_format_object: bad format; if array, it must have "
96 					"two elements", 0);
97 			RETURN_FALSE;
98 		}
99 
100 		uint32_t idx = 0;
101 		zval *z;
102 		ZEND_HASH_FOREACH_VAL(ht, z) {
103 			if (!valid_format(z)) {
104 				if (idx == 0) {
105 					intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
106 						"datefmt_format_object: bad format; the date format (first "
107 						"element of the array) is not valid", 0);
108 				} else {
109 					ZEND_ASSERT(idx == 1 && "We checked that there are two elements above");
110 					intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
111 						"datefmt_format_object: bad format; the time format (second "
112 						"element of the array) is not valid", 0);
113 				}
114 				RETURN_FALSE;
115 			}
116 			if (idx == 0) {
117 				dateStyle = (DateFormat::EStyle)Z_LVAL_P(z);
118 			} else {
119 				ZEND_ASSERT(idx == 1 && "We checked that there are two elements above");
120 				timeStyle = (DateFormat::EStyle)Z_LVAL_P(z);
121 			}
122 			idx++;
123 		} ZEND_HASH_FOREACH_END();
124 		ZEND_ASSERT(idx == 2 && "We checked that there are two elements above");
125 	} else if (Z_TYPE_P(format) == IS_LONG) {
126 		if (!valid_format(format)) {
127 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
128 					"datefmt_format_object: the date/time format type is invalid",
129 					0);
130 			RETURN_FALSE;
131 		}
132 		dateStyle = timeStyle = (DateFormat::EStyle)Z_LVAL_P(format);
133 	} else {
134 		if (!try_convert_to_string(format)) {
135 			RETURN_THROWS();
136 		}
137 		if (Z_STRLEN_P(format) == 0) {
138 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
139 					"datefmt_format_object: the format is empty", 0);
140 			RETURN_FALSE;
141 		}
142 		pattern = true;
143 	}
144 
145 	//there's no support for relative time in ICU yet
146 	if (timeStyle != DateFormat::NONE) {
147 		timeStyle = (DateFormat::EStyle)(timeStyle & ~DateFormat::kRelative);
148 	}
149 
150 	zend_class_entry *instance_ce = Z_OBJCE_P(object);
151 	if (instanceof_function(instance_ce, Calendar_ce_ptr)) {
152 		Calendar *obj_cal = calendar_fetch_native_calendar(Z_OBJ_P(object));
153 		if (obj_cal == NULL) {
154 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
155 					"datefmt_format_object: bad IntlCalendar instance: "
156 					"not initialized properly", 0);
157 			RETURN_FALSE;
158 		}
159 		timeZone = obj_cal->getTimeZone().clone();
160 		date = obj_cal->getTime(status);
161 		if (U_FAILURE(status)) {
162 			intl_error_set(NULL, status,
163 					"datefmt_format_object: error obtaining instant from "
164 					"IntlCalendar", 0);
165 			RETVAL_FALSE;
166 			goto cleanup;
167 		}
168 		cal = obj_cal->clone();
169 	} else if (instanceof_function(instance_ce, php_date_get_interface_ce())) {
170 		if (intl_datetime_decompose(object, &date, &timeZone, NULL,
171 				"datefmt_format_object") == FAILURE) {
172 			RETURN_FALSE;
173 		}
174 		cal = new GregorianCalendar(Locale::createFromName(locale_str), status);
175 		if (U_FAILURE(status)) {
176 			intl_error_set(NULL, status,
177 					"datefmt_format_object: could not create GregorianCalendar",
178 					0);
179 			RETVAL_FALSE;
180 			goto cleanup;
181 		}
182 	} else {
183 		intl_error_set(NULL, status, "datefmt_format_object: the passed object "
184 				"must be an instance of either IntlCalendar or DateTimeInterface",
185 				0);
186 		RETURN_FALSE;
187 	}
188 
189 	if (pattern) {
190 		StringPiece sp(Z_STRVAL_P(format));
191 		df = new SimpleDateFormat(
192 			UnicodeString::fromUTF8(sp),
193 			Locale::createFromName(locale_str),
194 			status);
195 
196 		if (U_FAILURE(status)) {
197 			intl_error_set(NULL, status,
198 					"datefmt_format_object: could not create SimpleDateFormat",
199 					0);
200 			RETVAL_FALSE;
201 			goto cleanup;
202 		}
203 	} else {
204 		df = DateFormat::createDateTimeInstance(dateStyle, timeStyle,
205 				Locale::createFromName(locale_str));
206 
207 		if (df == NULL) { /* according to ICU sources, this should never happen */
208 			intl_error_set(NULL, status,
209 					"datefmt_format_object: could not create DateFormat",
210 					0);
211 			RETVAL_FALSE;
212 			goto cleanup;
213 		}
214 	}
215 
216 	//must be in this order (or have the cal adopt the tz)
217 	df->adoptCalendar(cal);
218 	cal = NULL;
219 	df->adoptTimeZone(timeZone);
220 	timeZone = NULL;
221 
222 	{
223 		zend_string *u8str;
224 		UnicodeString result = UnicodeString();
225 		df->format(date, result);
226 
227 		u8str = intl_charFromString(result, &status);
228 		if (!u8str) {
229 			intl_error_set(NULL, status,
230 					"datefmt_format_object: error converting result to UTF-8",
231 					0);
232 			RETVAL_FALSE;
233 			goto cleanup;
234 		}
235 		RETVAL_STR(u8str);
236 	}
237 
238 
239 cleanup:
240 	delete df;
241 	delete timeZone;
242 	delete cal;
243 }
244