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 const 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 		uint32_t         idx;
94 		zval			*z;
95 
96 		if (zend_hash_num_elements(ht) != 2) {
97 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
98 					"datefmt_format_object: bad format; if array, it must have "
99 					"two elements", 0);
100 			RETURN_FALSE;
101 		}
102 
103 		idx = 0;
104 		while (idx < ht->nNumUsed) {
105 			z = &ht->arData[idx].val;
106 			if (Z_TYPE_P(z) != IS_UNDEF) {
107 				break;
108 			}
109 			idx++;
110 		}
111 		if (idx >= ht->nNumUsed || !valid_format(z)) {
112 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
113 					"datefmt_format_object: bad format; the date format (first "
114 					"element of the array) is not valid", 0);
115 			RETURN_FALSE;
116 		}
117 		dateStyle = (DateFormat::EStyle)Z_LVAL_P(z);
118 
119 		idx++;
120 		while (idx < ht->nNumUsed) {
121 			z = &ht->arData[idx].val;
122 			if (Z_TYPE_P(z) != IS_UNDEF) {
123 				break;
124 			}
125 			idx++;
126 		}
127 		if (idx >= ht->nNumUsed || !valid_format(z)) {
128 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
129 					"datefmt_format_object: bad format; the time format ("
130 					"second element of the array) is not valid", 0);
131 			RETURN_FALSE;
132 		}
133 		timeStyle = (DateFormat::EStyle)Z_LVAL_P(z);
134 	} else if (Z_TYPE_P(format) == IS_LONG) {
135 		if (!valid_format(format)) {
136 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
137 					"datefmt_format_object: the date/time format type is invalid",
138 					0);
139 			RETURN_FALSE;
140 		}
141 		dateStyle = timeStyle = (DateFormat::EStyle)Z_LVAL_P(format);
142 	} else {
143 		if (!try_convert_to_string(format)) {
144 			RETURN_THROWS();
145 		}
146 		if (Z_STRLEN_P(format) == 0) {
147 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
148 					"datefmt_format_object: the format is empty", 0);
149 			RETURN_FALSE;
150 		}
151 		pattern = true;
152 	}
153 
154 	//there's no support for relative time in ICU yet
155 	if (timeStyle != DateFormat::NONE) {
156 		timeStyle = (DateFormat::EStyle)(timeStyle & ~DateFormat::kRelative);
157 	}
158 
159 	zend_class_entry *instance_ce = Z_OBJCE_P(object);
160 	if (instanceof_function(instance_ce, Calendar_ce_ptr)) {
161 		Calendar *obj_cal = calendar_fetch_native_calendar(Z_OBJ_P(object));
162 		if (obj_cal == NULL) {
163 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
164 					"datefmt_format_object: bad IntlCalendar instance: "
165 					"not initialized properly", 0);
166 			RETURN_FALSE;
167 		}
168 		timeZone = obj_cal->getTimeZone().clone();
169 		date = obj_cal->getTime(status);
170 		if (U_FAILURE(status)) {
171 			intl_error_set(NULL, status,
172 					"datefmt_format_object: error obtaining instant from "
173 					"IntlCalendar", 0);
174 			RETVAL_FALSE;
175 			goto cleanup;
176 		}
177 		cal = obj_cal->clone();
178 	} else if (instanceof_function(instance_ce, php_date_get_interface_ce())) {
179 		if (intl_datetime_decompose(object, &date, &timeZone, NULL,
180 				"datefmt_format_object") == FAILURE) {
181 			RETURN_FALSE;
182 		}
183 		cal = new GregorianCalendar(Locale::createFromName(locale_str), status);
184 		if (U_FAILURE(status)) {
185 			intl_error_set(NULL, status,
186 					"datefmt_format_object: could not create GregorianCalendar",
187 					0);
188 			RETVAL_FALSE;
189 			goto cleanup;
190 		}
191 	} else {
192 		intl_error_set(NULL, status, "datefmt_format_object: the passed object "
193 				"must be an instance of either IntlCalendar or DateTimeInterface",
194 				0);
195 		RETURN_FALSE;
196 	}
197 
198 	if (pattern) {
199 		StringPiece sp(Z_STRVAL_P(format));
200 		df = new SimpleDateFormat(
201 			UnicodeString::fromUTF8(sp),
202 			Locale::createFromName(locale_str),
203 			status);
204 
205 		if (U_FAILURE(status)) {
206 			intl_error_set(NULL, status,
207 					"datefmt_format_object: could not create SimpleDateFormat",
208 					0);
209 			RETVAL_FALSE;
210 			goto cleanup;
211 		}
212 	} else {
213 		df = DateFormat::createDateTimeInstance(dateStyle, timeStyle,
214 				Locale::createFromName(locale_str));
215 
216 		if (df == NULL) { /* according to ICU sources, this should never happen */
217 			intl_error_set(NULL, status,
218 					"datefmt_format_object: could not create DateFormat",
219 					0);
220 			RETVAL_FALSE;
221 			goto cleanup;
222 		}
223 	}
224 
225 	//must be in this order (or have the cal adopt the tz)
226 	df->adoptCalendar(cal);
227 	cal = NULL;
228 	df->adoptTimeZone(timeZone);
229 	timeZone = NULL;
230 
231 	{
232 		zend_string *u8str;
233 		UnicodeString result = UnicodeString();
234 		df->format(date, result);
235 
236 		u8str = intl_charFromString(result, &status);
237 		if (!u8str) {
238 			intl_error_set(NULL, status,
239 					"datefmt_format_object: error converting result to UTF-8",
240 					0);
241 			RETVAL_FALSE;
242 			goto cleanup;
243 		}
244 		RETVAL_STR(u8str);
245 	}
246 
247 
248 cleanup:
249 	delete df;
250 	delete timeZone;
251 	delete cal;
252 }
253