twig.c 27KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Twig Extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2011 Derick Rethans |
  6. +----------------------------------------------------------------------+
  7. | Redistribution and use in source and binary forms, with or without |
  8. | modification, are permitted provided that the conditions mentioned |
  9. | in the accompanying LICENSE file are met (BSD, revised). |
  10. +----------------------------------------------------------------------+
  11. | Author: Derick Rethans <derick@derickrethans.nl> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include "php.h"
  18. #include "php_ini.h"
  19. #include "ext/standard/info.h"
  20. #include "php_twig.h"
  21. #include "ext/standard/php_string.h"
  22. #include "ext/standard/php_smart_str.h"
  23. #include "Zend/zend_object_handlers.h"
  24. #include "Zend/zend_interfaces.h"
  25. #include "Zend/zend_exceptions.h"
  26. ZEND_BEGIN_ARG_INFO_EX(twig_template_get_attribute_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 6)
  27. ZEND_ARG_INFO(0, template)
  28. ZEND_ARG_INFO(0, object)
  29. ZEND_ARG_INFO(0, item)
  30. ZEND_ARG_INFO(0, arguments)
  31. ZEND_ARG_INFO(0, type)
  32. ZEND_ARG_INFO(0, isDefinedTest)
  33. ZEND_END_ARG_INFO()
  34. zend_function_entry twig_functions[] = {
  35. PHP_FE(twig_template_get_attributes, twig_template_get_attribute_args)
  36. {NULL, NULL, NULL}
  37. };
  38. zend_module_entry twig_module_entry = {
  39. #if ZEND_MODULE_API_NO >= 20010901
  40. STANDARD_MODULE_HEADER,
  41. #endif
  42. "twig",
  43. twig_functions,
  44. PHP_MINIT(twig),
  45. PHP_MSHUTDOWN(twig),
  46. PHP_RINIT(twig),
  47. PHP_RSHUTDOWN(twig),
  48. PHP_MINFO(twig),
  49. #if ZEND_MODULE_API_NO >= 20010901
  50. PHP_TWIG_VERSION,
  51. #endif
  52. STANDARD_MODULE_PROPERTIES
  53. };
  54. #ifdef COMPILE_DL_TWIG
  55. ZEND_GET_MODULE(twig)
  56. #endif
  57. PHP_INI_BEGIN()
  58. PHP_INI_END()
  59. PHP_MINIT_FUNCTION(twig)
  60. {
  61. REGISTER_INI_ENTRIES();
  62. return SUCCESS;
  63. }
  64. PHP_MSHUTDOWN_FUNCTION(twig)
  65. {
  66. UNREGISTER_INI_ENTRIES();
  67. return SUCCESS;
  68. }
  69. PHP_RINIT_FUNCTION(twig)
  70. {
  71. return SUCCESS;
  72. }
  73. PHP_RSHUTDOWN_FUNCTION(twig)
  74. {
  75. return SUCCESS;
  76. }
  77. PHP_MINFO_FUNCTION(twig)
  78. {
  79. php_info_print_table_start();
  80. php_info_print_table_header(2, "Twig support", "enabled");
  81. php_info_print_table_row(2, "Version", PHP_TWIG_VERSION);
  82. php_info_print_table_end();
  83. DISPLAY_INI_ENTRIES();
  84. }
  85. int TWIG_ARRAY_KEY_EXISTS(zval *array, char* key, int key_len)
  86. {
  87. if (Z_TYPE_P(array) != IS_ARRAY) {
  88. return 0;
  89. }
  90. return zend_symtable_exists(Z_ARRVAL_P(array), key, key_len + 1);
  91. }
  92. int TWIG_INSTANCE_OF(zval *object, zend_class_entry *interface TSRMLS_DC)
  93. {
  94. if (Z_TYPE_P(object) != IS_OBJECT) {
  95. return 0;
  96. }
  97. return instanceof_function(Z_OBJCE_P(object), interface TSRMLS_CC);
  98. }
  99. int TWIG_INSTANCE_OF_USERLAND(zval *object, char *interface TSRMLS_DC)
  100. {
  101. zend_class_entry **pce;
  102. if (Z_TYPE_P(object) != IS_OBJECT) {
  103. return 0;
  104. }
  105. if (zend_lookup_class(interface, strlen(interface), &pce TSRMLS_CC) == FAILURE) {
  106. return 0;
  107. }
  108. return instanceof_function(Z_OBJCE_P(object), *pce TSRMLS_CC);
  109. }
  110. zval *TWIG_GET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset TSRMLS_DC)
  111. {
  112. zend_class_entry *ce = Z_OBJCE_P(object);
  113. zval *retval;
  114. if (Z_TYPE_P(object) == IS_OBJECT) {
  115. SEPARATE_ARG_IF_REF(offset);
  116. zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
  117. zval_ptr_dtor(&offset);
  118. if (!retval) {
  119. if (!EG(exception)) {
  120. zend_error(E_ERROR, "Undefined offset for object of type %s used as array", ce->name);
  121. }
  122. return NULL;
  123. }
  124. return retval;
  125. }
  126. return NULL;
  127. }
  128. int TWIG_ISSET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset TSRMLS_DC)
  129. {
  130. zend_class_entry *ce = Z_OBJCE_P(object);
  131. zval *retval;
  132. if (Z_TYPE_P(object) == IS_OBJECT) {
  133. SEPARATE_ARG_IF_REF(offset);
  134. zend_call_method_with_1_params(&object, ce, NULL, "offsetexists", &retval, offset);
  135. zval_ptr_dtor(&offset);
  136. if (!retval) {
  137. if (!EG(exception)) {
  138. zend_error(E_ERROR, "Undefined offset for object of type %s used as array", ce->name);
  139. }
  140. return 0;
  141. }
  142. return (retval && Z_TYPE_P(retval) == IS_BOOL && Z_LVAL_P(retval));
  143. }
  144. return 0;
  145. }
  146. char *TWIG_STRTOLOWER(const char *str, int str_len)
  147. {
  148. char *item_dup;
  149. item_dup = estrndup(str, str_len);
  150. php_strtolower(item_dup, str_len);
  151. return item_dup;
  152. }
  153. zval *TWIG_CALL_USER_FUNC_ARRAY(zval *object, char *function, zval *arguments TSRMLS_DC)
  154. {
  155. zend_fcall_info fci;
  156. zval ***args = NULL;
  157. int arg_count = 0;
  158. HashTable *table;
  159. HashPosition pos;
  160. int i = 0;
  161. zval *retval_ptr;
  162. zval *zfunction;
  163. if (arguments) {
  164. table = HASH_OF(arguments);
  165. args = safe_emalloc(sizeof(zval **), table->nNumOfElements, 0);
  166. zend_hash_internal_pointer_reset_ex(table, &pos);
  167. while (zend_hash_get_current_data_ex(table, (void **)&args[i], &pos) == SUCCESS) {
  168. i++;
  169. zend_hash_move_forward_ex(table, &pos);
  170. }
  171. arg_count = table->nNumOfElements;
  172. }
  173. MAKE_STD_ZVAL(zfunction);
  174. ZVAL_STRING(zfunction, function, 1);
  175. fci.size = sizeof(fci);
  176. fci.function_table = EG(function_table);
  177. fci.function_name = zfunction;
  178. fci.symbol_table = NULL;
  179. #if PHP_VERSION_ID >= 50300
  180. fci.object_ptr = object;
  181. #else
  182. fci.object_pp = &object;
  183. #endif
  184. fci.retval_ptr_ptr = &retval_ptr;
  185. fci.param_count = arg_count;
  186. fci.params = args;
  187. fci.no_separation = 0;
  188. if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) {
  189. zval_dtor(zfunction);
  190. efree(zfunction);
  191. zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Could not execute %s::%s()", zend_get_class_entry(object TSRMLS_CC)->name, function TSRMLS_CC);
  192. }
  193. if (args) {
  194. efree(fci.params);
  195. }
  196. zval_dtor(zfunction);
  197. efree(zfunction);
  198. return retval_ptr;
  199. }
  200. int TWIG_CALL_BOOLEAN(zval *object, char *functionName TSRMLS_DC)
  201. {
  202. zval *ret;
  203. int res;
  204. ret = TWIG_CALL_USER_FUNC_ARRAY(object, functionName, NULL TSRMLS_CC);
  205. res = Z_LVAL_P(ret);
  206. zval_ptr_dtor(&ret);
  207. return res;
  208. }
  209. zval *TWIG_GET_STATIC_PROPERTY(zval *class, char *prop_name TSRMLS_DC)
  210. {
  211. zval **tmp_zval;
  212. zend_class_entry *ce;
  213. if (class == NULL || Z_TYPE_P(class) != IS_OBJECT) {
  214. return NULL;
  215. }
  216. ce = zend_get_class_entry(class TSRMLS_CC);
  217. #if PHP_VERSION_ID >= 50400
  218. tmp_zval = zend_std_get_static_property(ce, prop_name, strlen(prop_name), 0, NULL TSRMLS_CC);
  219. #else
  220. tmp_zval = zend_std_get_static_property(ce, prop_name, strlen(prop_name), 0 TSRMLS_CC);
  221. #endif
  222. return *tmp_zval;
  223. }
  224. zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name TSRMLS_DC)
  225. {
  226. zval **tmp_zval;
  227. char *tmp_name;
  228. if (class == NULL || Z_TYPE_P(class) != IS_ARRAY || Z_TYPE_P(prop_name) != IS_STRING) {
  229. if (class != NULL && Z_TYPE_P(class) == IS_OBJECT && TWIG_INSTANCE_OF(class, zend_ce_arrayaccess TSRMLS_CC)) {
  230. // array access object
  231. return TWIG_GET_ARRAYOBJECT_ELEMENT(class, prop_name TSRMLS_CC);
  232. }
  233. return NULL;
  234. }
  235. convert_to_string(prop_name);
  236. tmp_name = Z_STRVAL_P(prop_name);
  237. if (zend_symtable_find(HASH_OF(class), tmp_name, strlen(tmp_name)+1, (void**) &tmp_zval) == SUCCESS) {
  238. return *tmp_zval;
  239. }
  240. return NULL;
  241. }
  242. zval *TWIG_GET_ARRAY_ELEMENT(zval *class, char *prop_name, int prop_name_length TSRMLS_DC)
  243. {
  244. zval **tmp_zval;
  245. if (class == NULL/* || Z_TYPE_P(class) != IS_ARRAY*/) {
  246. return NULL;
  247. }
  248. if (class != NULL && Z_TYPE_P(class) == IS_OBJECT && TWIG_INSTANCE_OF(class, zend_ce_arrayaccess TSRMLS_CC)) {
  249. // array access object
  250. zval *tmp_name_zval;
  251. zval *tmp_ret_zval;
  252. ALLOC_INIT_ZVAL(tmp_name_zval);
  253. ZVAL_STRING(tmp_name_zval, prop_name, 1);
  254. tmp_ret_zval = TWIG_GET_ARRAYOBJECT_ELEMENT(class, tmp_name_zval TSRMLS_CC);
  255. zval_dtor(tmp_name_zval);
  256. efree(tmp_name_zval);
  257. return tmp_ret_zval;
  258. }
  259. if (zend_symtable_find(HASH_OF(class), prop_name, prop_name_length+1, (void**)&tmp_zval) == SUCCESS) {
  260. return *tmp_zval;
  261. }
  262. return NULL;
  263. }
  264. zval *TWIG_PROPERTY(zval *object, zval *propname TSRMLS_DC)
  265. {
  266. char *prot_name;
  267. int prot_name_length;
  268. zval *tmp = NULL;
  269. tmp = TWIG_GET_ARRAY_ELEMENT(object, Z_STRVAL_P(propname), Z_STRLEN_P(propname) TSRMLS_CC);
  270. if (tmp) {
  271. return tmp;
  272. }
  273. zend_mangle_property_name(&prot_name, &prot_name_length, "*", 1, Z_STRVAL_P(propname), Z_STRLEN_P(propname), 0);
  274. tmp = TWIG_GET_ARRAY_ELEMENT(object, prot_name, prot_name_length TSRMLS_CC);
  275. efree(prot_name);
  276. if (tmp) {
  277. return tmp;
  278. }
  279. if (Z_OBJ_HT_P(object)->read_property) {
  280. #if PHP_VERSION_ID >= 50400
  281. tmp = Z_OBJ_HT_P(object)->read_property(object, propname, BP_VAR_IS, NULL TSRMLS_CC);
  282. #else
  283. tmp = Z_OBJ_HT_P(object)->read_property(object, propname, BP_VAR_IS TSRMLS_CC);
  284. #endif
  285. if (tmp != EG(uninitialized_zval_ptr)) {
  286. return tmp;
  287. } else {
  288. return NULL;
  289. }
  290. }
  291. return tmp;
  292. }
  293. int TWIG_HAS_PROPERTY(zval *object, zval *propname TSRMLS_DC)
  294. {
  295. if (Z_OBJ_HT_P(object)->has_property) {
  296. #if PHP_VERSION_ID >= 50400
  297. return Z_OBJ_HT_P(object)->has_property(object, propname, 0, NULL TSRMLS_CC);
  298. #else
  299. return Z_OBJ_HT_P(object)->has_property(object, propname, 0 TSRMLS_CC);
  300. #endif
  301. }
  302. return 0;
  303. }
  304. zval *TWIG_PROPERTY_CHAR(zval *object, char *propname TSRMLS_DC)
  305. {
  306. zval *tmp_name_zval, *tmp;
  307. ALLOC_INIT_ZVAL(tmp_name_zval);
  308. ZVAL_STRING(tmp_name_zval, propname, 1);
  309. tmp = TWIG_PROPERTY(object, tmp_name_zval TSRMLS_CC);
  310. zval_dtor(tmp_name_zval);
  311. efree(tmp_name_zval);
  312. return tmp;
  313. }
  314. int TWIG_CALL_B_0(zval *object, char *method)
  315. {
  316. return 0;
  317. }
  318. zval *TWIG_CALL_S(zval *object, char *method, char *arg0 TSRMLS_DC)
  319. {
  320. zend_fcall_info fci;
  321. zval **args[1];
  322. zval *argument;
  323. zval *zfunction;
  324. zval *retval_ptr;
  325. MAKE_STD_ZVAL(argument);
  326. ZVAL_STRING(argument, arg0, 1);
  327. args[0] = &argument;
  328. MAKE_STD_ZVAL(zfunction);
  329. ZVAL_STRING(zfunction, method, 1);
  330. fci.size = sizeof(fci);
  331. fci.function_table = EG(function_table);
  332. fci.function_name = zfunction;
  333. fci.symbol_table = NULL;
  334. #if PHP_VERSION_ID >= 50300
  335. fci.object_ptr = object;
  336. #else
  337. fci.object_pp = &object;
  338. #endif
  339. fci.retval_ptr_ptr = &retval_ptr;
  340. fci.param_count = 1;
  341. fci.params = args;
  342. fci.no_separation = 0;
  343. if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) {
  344. zval_dtor(argument);
  345. return 0;
  346. }
  347. zval_dtor(zfunction);
  348. efree(zfunction);
  349. zval_dtor(argument);
  350. efree(argument);
  351. return retval_ptr;
  352. }
  353. int TWIG_CALL_SB(zval *object, char *method, char *arg0 TSRMLS_DC)
  354. {
  355. zval *retval_ptr;
  356. int success;
  357. retval_ptr = TWIG_CALL_S(object, method, arg0 TSRMLS_CC);
  358. success = (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr));
  359. if (retval_ptr) {
  360. zval_ptr_dtor(&retval_ptr);
  361. }
  362. return success;
  363. }
  364. int TWIG_CALL_Z(zval *object, char *method, zval *arg1 TSRMLS_DC)
  365. {
  366. zend_fcall_info fci;
  367. zval **args[1];
  368. zval *zfunction;
  369. zval *retval_ptr;
  370. int success;
  371. args[0] = &arg1;
  372. MAKE_STD_ZVAL(zfunction);
  373. ZVAL_STRING(zfunction, method, 1);
  374. fci.size = sizeof(fci);
  375. fci.function_table = EG(function_table);
  376. fci.function_name = zfunction;
  377. fci.symbol_table = NULL;
  378. #if PHP_VERSION_ID >= 50300
  379. fci.object_ptr = object;
  380. #else
  381. fci.object_pp = &object;
  382. #endif
  383. fci.retval_ptr_ptr = &retval_ptr;
  384. fci.param_count = 1;
  385. fci.params = args;
  386. fci.no_separation = 0;
  387. if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) {
  388. zval_dtor(zfunction);
  389. efree(zfunction);
  390. if (retval_ptr) {
  391. zval_ptr_dtor(&retval_ptr);
  392. }
  393. return 0;
  394. }
  395. zval_dtor(zfunction);
  396. efree(zfunction);
  397. success = (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr));
  398. if (retval_ptr) {
  399. zval_ptr_dtor(&retval_ptr);
  400. }
  401. return success;
  402. }
  403. int TWIG_CALL_ZZ(zval *object, char *method, zval *arg1, zval *arg2 TSRMLS_DC)
  404. {
  405. zend_fcall_info fci;
  406. zval **args[2];
  407. zval *zfunction;
  408. zval *retval_ptr;
  409. int success;
  410. args[0] = &arg1;
  411. args[1] = &arg2;
  412. MAKE_STD_ZVAL(zfunction);
  413. ZVAL_STRING(zfunction, method, 1);
  414. fci.size = sizeof(fci);
  415. fci.function_table = EG(function_table);
  416. fci.function_name = zfunction;
  417. fci.symbol_table = NULL;
  418. #if PHP_VERSION_ID >= 50300
  419. fci.object_ptr = object;
  420. #else
  421. fci.object_pp = &object;
  422. #endif
  423. fci.retval_ptr_ptr = &retval_ptr;
  424. fci.param_count = 2;
  425. fci.params = args;
  426. fci.no_separation = 0;
  427. if (zend_call_function(&fci, NULL TSRMLS_CC) == FAILURE) {
  428. zval_dtor(zfunction);
  429. return 0;
  430. }
  431. zval_dtor(zfunction);
  432. success = (retval_ptr && (Z_TYPE_P(retval_ptr) == IS_BOOL) && Z_LVAL_P(retval_ptr));
  433. if (retval_ptr) {
  434. zval_ptr_dtor(&retval_ptr);
  435. }
  436. return success;
  437. }
  438. #ifndef Z_SET_REFCOUNT_P
  439. # define Z_SET_REFCOUNT_P(pz, rc) pz->refcount = rc
  440. # define Z_UNSET_ISREF_P(pz) pz->is_ref = 0
  441. #endif
  442. void TWIG_NEW(zval *object, char *class, zval *arg0 TSRMLS_DC, zval *arg1 TSRMLS_DC)
  443. {
  444. zend_class_entry **pce;
  445. if (zend_lookup_class(class, strlen(class), &pce TSRMLS_CC) == FAILURE) {
  446. return;
  447. }
  448. Z_TYPE_P(object) = IS_OBJECT;
  449. object_init_ex(object, *pce);
  450. Z_SET_REFCOUNT_P(object, 1);
  451. Z_UNSET_ISREF_P(object);
  452. TWIG_CALL_ZZ(object, "__construct", arg0 TSRMLS_CC, arg1 TSRMLS_CC);
  453. }
  454. static int twig_add_array_key_to_string(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
  455. {
  456. smart_str *buf;
  457. char *joiner;
  458. buf = va_arg(args, smart_str*);
  459. joiner = va_arg(args, char*);
  460. if (buf->len != 0) {
  461. smart_str_appends(buf, joiner);
  462. }
  463. if (hash_key->nKeyLength == 0) {
  464. smart_str_append_long(buf, (long) hash_key->h);
  465. } else {
  466. char *key, *tmp_str;
  467. int key_len, tmp_len;
  468. key = php_addcslashes(hash_key->arKey, hash_key->nKeyLength - 1, &key_len, 0, "'\\", 2 TSRMLS_CC);
  469. tmp_str = php_str_to_str_ex(key, key_len, "\0", 1, "' . \"\\0\" . '", 12, &tmp_len, 0, NULL);
  470. smart_str_appendl(buf, tmp_str, tmp_len);
  471. efree(key);
  472. efree(tmp_str);
  473. }
  474. return 0;
  475. }
  476. char *TWIG_IMPLODE_ARRAY_KEYS(char *joiner, zval *array TSRMLS_DC)
  477. {
  478. smart_str collector = { 0, 0, 0 };
  479. smart_str_appendl(&collector, "", 0);
  480. zend_hash_apply_with_arguments(HASH_OF(array) TSRMLS_CC, twig_add_array_key_to_string, 2, &collector, joiner);
  481. smart_str_0(&collector);
  482. return collector.c;
  483. }
  484. static void TWIG_THROW_EXCEPTION(char *exception_name TSRMLS_DC, char *message, ...)
  485. {
  486. char *buffer;
  487. va_list args;
  488. zend_class_entry **pce;
  489. if (zend_lookup_class(exception_name, strlen(exception_name), &pce TSRMLS_CC) == FAILURE)
  490. {
  491. return;
  492. }
  493. va_start(args, message);
  494. vspprintf(&buffer, 0, message, args);
  495. va_end(args);
  496. zend_throw_exception_ex(*pce, 0 TSRMLS_CC, buffer);
  497. }
  498. char *TWIG_GET_CLASS_NAME(zval *object TSRMLS_DC)
  499. {
  500. char *class_name;
  501. zend_uint class_name_len;
  502. if (Z_TYPE_P(object) != IS_OBJECT) {
  503. return "";
  504. }
  505. zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC);
  506. return class_name;
  507. }
  508. static int twig_add_method_to_class(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
  509. {
  510. zval *retval;
  511. char *item;
  512. size_t item_len;
  513. zend_function *mptr = (zend_function *) pDest;
  514. if (!(mptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
  515. return 0;
  516. }
  517. retval = va_arg(args, zval*);
  518. item_len = strlen(mptr->common.function_name);
  519. item = estrndup(mptr->common.function_name, item_len);
  520. php_strtolower(item, item_len);
  521. add_assoc_stringl_ex(retval, item, item_len+1, item, item_len, 0);
  522. return 0;
  523. }
  524. static int twig_add_property_to_class(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
  525. {
  526. zend_class_entry *ce;
  527. zval *retval;
  528. char *class_name, *prop_name;
  529. zend_property_info *pptr = (zend_property_info *) pDest;
  530. if (!(pptr->flags & ZEND_ACC_PUBLIC)) {
  531. return 0;
  532. }
  533. ce = *va_arg(args, zend_class_entry**);
  534. retval = va_arg(args, zval*);
  535. zend_unmangle_property_name(pptr->name, pptr->name_length, &class_name, &prop_name);
  536. add_assoc_string(retval, prop_name, prop_name, 1);
  537. return 0;
  538. }
  539. /* {{{ _adddynproperty */
  540. static int twig_add_dyn_property_to_class(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
  541. {
  542. zend_class_entry *ce = *va_arg(args, zend_class_entry**);
  543. zval *retval = va_arg(args, zval*), member;
  544. char *class_name, *prop_name;
  545. if (hash_key->nKeyLength < 1 || hash_key->arKey[0] == '\0') {
  546. return 0; /* non public cannot be dynamic */
  547. }
  548. ZVAL_STRINGL(&member, hash_key->arKey, hash_key->nKeyLength-1, 0);
  549. if (zend_get_property_info(ce, &member, 1 TSRMLS_CC) == &EG(std_property_info)) {
  550. zend_unmangle_property_name((&EG(std_property_info))->name, (&EG(std_property_info))->name_length, &class_name, &prop_name);
  551. add_assoc_string(retval, prop_name, prop_name, 1);
  552. }
  553. return 0;
  554. }
  555. static void twig_add_class_to_cache(zval *cache, zval *object, char *class_name TSRMLS_DC)
  556. {
  557. zval *class_info, *class_methods, *class_properties;
  558. zend_class_entry *class_ce;
  559. class_ce = zend_get_class_entry(object TSRMLS_CC);
  560. ALLOC_INIT_ZVAL(class_info);
  561. ALLOC_INIT_ZVAL(class_methods);
  562. ALLOC_INIT_ZVAL(class_properties);
  563. array_init(class_info);
  564. array_init(class_methods);
  565. array_init(class_properties);
  566. // add all methods to self::cache[$class]['methods']
  567. zend_hash_apply_with_arguments(&class_ce->function_table TSRMLS_CC, twig_add_method_to_class, 1, class_methods);
  568. zend_hash_apply_with_arguments(&class_ce->properties_info TSRMLS_CC, twig_add_property_to_class, 2, &class_ce, class_properties);
  569. if (object && Z_OBJ_HT_P(object)->get_properties) {
  570. HashTable *properties = Z_OBJ_HT_P(object)->get_properties(object TSRMLS_CC);
  571. zend_hash_apply_with_arguments(properties TSRMLS_CC, twig_add_dyn_property_to_class, 2, &class_ce, class_properties);
  572. }
  573. add_assoc_zval(class_info, "methods", class_methods);
  574. add_assoc_zval(class_info, "properties", class_properties);
  575. add_assoc_zval(cache, class_name, class_info);
  576. }
  577. /* {{{ proto mixed twig_template_get_attributes(TwigTemplate template, mixed object, mixed item, array arguments, string type, boolean isDefinedTest, boolean ignoreStrictCheck)
  578. A C implementation of TwigTemplate::getAttribute() */
  579. PHP_FUNCTION(twig_template_get_attributes)
  580. {
  581. zval *template;
  582. zval *object;
  583. char *item;
  584. int item_len;
  585. zval zitem;
  586. zval *arguments = NULL;
  587. zval *ret = NULL;
  588. char *type = NULL;
  589. int type_len = 0;
  590. zend_bool isDefinedTest = 0;
  591. zend_bool ignoreStrictCheck = 0;
  592. int free_ret = 0;
  593. zval *tmp_self_cache;
  594. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozs|asbb", &template, &object, &item, &item_len, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) {
  595. return;
  596. }
  597. INIT_PZVAL(&zitem);
  598. ZVAL_STRINGL(&zitem, item, item_len, 0);
  599. if (!type) {
  600. type = "any";
  601. }
  602. /*
  603. // array
  604. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  605. if ((is_array($object) && array_key_exists($item, $object))
  606. || ($object instanceof ArrayAccess && isset($object[$item]))
  607. ) {
  608. if ($isDefinedTest) {
  609. return true;
  610. }
  611. return $object[$item];
  612. }
  613. */
  614. if (strcmp("method", type) != 0) {
  615. // printf("XXXmethod: %s\n", type);
  616. if ((TWIG_ARRAY_KEY_EXISTS(object, item, item_len))
  617. || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess TSRMLS_CC) && TWIG_ISSET_ARRAYOBJECT_ELEMENT(object, &zitem TSRMLS_CC))
  618. ) {
  619. zval *ret;
  620. if (isDefinedTest) {
  621. RETURN_TRUE;
  622. }
  623. ret = TWIG_GET_ARRAY_ELEMENT(object, item, item_len TSRMLS_CC);
  624. RETVAL_ZVAL(ret, 1, 0);
  625. if (free_ret) {
  626. zval_ptr_dtor(&ret);
  627. }
  628. return;
  629. }
  630. /*
  631. if (Twig_TemplateInterface::ARRAY_CALL === $type) {
  632. if ($isDefinedTest) {
  633. return false;
  634. }
  635. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  636. return null;
  637. }
  638. */
  639. if (strcmp("array", type) == 0) {
  640. if (isDefinedTest) {
  641. RETURN_FALSE;
  642. }
  643. if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) {
  644. return;
  645. }
  646. /*
  647. if (is_object($object)) {
  648. throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
  649. // array
  650. } else {
  651. throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
  652. }
  653. }
  654. }
  655. */
  656. if (Z_TYPE_P(object) == IS_OBJECT) {
  657. TWIG_THROW_EXCEPTION("Twig_Error_Runtime" TSRMLS_CC, "Key \"%s\" in object (with ArrayAccess) of type \"%s\" does not exist", item, TWIG_GET_CLASS_NAME(object TSRMLS_CC));
  658. } else {
  659. TWIG_THROW_EXCEPTION("Twig_Error_Runtime" TSRMLS_CC, "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE_ARRAY_KEYS(", ", object TSRMLS_CC));
  660. }
  661. return;
  662. }
  663. }
  664. /*
  665. if (!is_object($object)) {
  666. if ($isDefinedTest) {
  667. return false;
  668. }
  669. */
  670. if (Z_TYPE_P(object) != IS_OBJECT) {
  671. if (isDefinedTest) {
  672. RETURN_FALSE;
  673. }
  674. /*
  675. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  676. return null;
  677. }
  678. throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, implode(', ', array_keys($object))));
  679. }
  680. */
  681. if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) {
  682. RETURN_FALSE;
  683. }
  684. if (Z_TYPE_P(object) == IS_ARRAY) {
  685. TWIG_THROW_EXCEPTION("Twig_Error_Runtime" TSRMLS_CC, "Item \"%s\" for \"Array\" does not exist", item);
  686. } else {
  687. Z_ADDREF_P(object);
  688. convert_to_string_ex(&object);
  689. TWIG_THROW_EXCEPTION("Twig_Error_Runtime" TSRMLS_CC, "Item \"%s\" for \"%s\" does not exist", item, Z_STRVAL_P(object));
  690. zval_ptr_dtor(&object);
  691. }
  692. return;
  693. }
  694. /*
  695. // get some information about the object
  696. $class = get_class($object);
  697. if (!isset(self::$cache[$class])) {
  698. $r = new ReflectionClass($class);
  699. self::$cache[$class] = array('methods' => array(), 'properties' => array());
  700. foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  701. self::$cache[$class]['methods'][strtolower($method->getName())] = true;
  702. }
  703. foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
  704. self::$cache[$class]['properties'][$property->getName()] = true;
  705. }
  706. }
  707. */
  708. if (Z_TYPE_P(object) == IS_OBJECT) {
  709. char *class_name = NULL;
  710. class_name = TWIG_GET_CLASS_NAME(object TSRMLS_CC);
  711. tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache" TSRMLS_CC);
  712. if (!TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC)) {
  713. twig_add_class_to_cache(tmp_self_cache, object, class_name TSRMLS_CC);
  714. }
  715. efree(class_name);
  716. }
  717. /*
  718. // object property
  719. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  720. if (isset(self::$cache[$class]['properties'][$item])
  721. || isset($object->$item) || array_key_exists($item, $object)
  722. ) {
  723. if ($isDefinedTest) {
  724. return true;
  725. }
  726. if ($this->env->hasExtension('sandbox')) {
  727. $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
  728. }
  729. return $object->$item;
  730. }
  731. }
  732. */
  733. if (strcmp("method", type) != 0) {
  734. zval *tmp_class, *tmp_properties, *tmp_item;
  735. char *class_name = NULL;
  736. class_name = TWIG_GET_CLASS_NAME(object TSRMLS_CC);
  737. tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC);
  738. tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties", strlen("properties") TSRMLS_CC);
  739. tmp_item = TWIG_GET_ARRAY_ELEMENT(tmp_properties, item, item_len TSRMLS_CC);
  740. efree(class_name);
  741. if (tmp_item || TWIG_HAS_PROPERTY(object, &zitem TSRMLS_CC) || TWIG_ARRAY_KEY_EXISTS(object, item, item_len) // FIXME: Array key? is that array access here?
  742. ) {
  743. if (isDefinedTest) {
  744. RETURN_TRUE;
  745. }
  746. if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "hasExtension", "sandbox" TSRMLS_CC)) {
  747. TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getExtension", "sandbox" TSRMLS_CC), "checkPropertyAllowed", object, &zitem TSRMLS_CC);
  748. }
  749. if (EG(exception)) {
  750. return;
  751. }
  752. ret = TWIG_PROPERTY(object, &zitem TSRMLS_CC);
  753. RETURN_ZVAL(ret, 1, 0);
  754. }
  755. }
  756. /*
  757. // object method
  758. $lcItem = strtolower($item);
  759. if (isset(self::$cache[$class]['methods'][$lcItem])) {
  760. $method = $item;
  761. } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
  762. $method = 'get'.$item;
  763. } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
  764. $method = 'is'.$item;
  765. } elseif (isset(self::$cache[$class]['methods']['__call'])) {
  766. $method = $item;
  767. */
  768. {
  769. char *lcItem = TWIG_STRTOLOWER(item, item_len);
  770. int lcItem_length;
  771. char *method = NULL;
  772. char *tmp_method_name_get;
  773. char *tmp_method_name_is;
  774. zval *tmp_class, *tmp_methods;
  775. char *class_name = NULL;
  776. class_name = TWIG_GET_CLASS_NAME(object TSRMLS_CC);
  777. lcItem_length = strlen(lcItem);
  778. tmp_method_name_get = emalloc(4 + lcItem_length);
  779. tmp_method_name_is = emalloc(3 + lcItem_length);
  780. sprintf(tmp_method_name_get, "get%s", lcItem);
  781. sprintf(tmp_method_name_is, "is%s", lcItem);
  782. tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC);
  783. tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods", strlen("methods") TSRMLS_CC);
  784. efree(class_name);
  785. if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, lcItem, lcItem_length TSRMLS_CC)) {
  786. method = item;
  787. } else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, tmp_method_name_get, lcItem_length + 3 TSRMLS_CC)) {
  788. method = tmp_method_name_get;
  789. } else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, tmp_method_name_is, lcItem_length + 2 TSRMLS_CC)) {
  790. method = tmp_method_name_is;
  791. } else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, "__call", 6 TSRMLS_CC)) {
  792. method = item;
  793. /*
  794. } else {
  795. if ($isDefinedTest) {
  796. return false;
  797. }
  798. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  799. return null;
  800. }
  801. throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
  802. }
  803. if ($isDefinedTest) {
  804. return true;
  805. }
  806. */
  807. } else {
  808. if (isDefinedTest) {
  809. RETURN_FALSE;
  810. }
  811. if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) {
  812. return;
  813. }
  814. TWIG_THROW_EXCEPTION("Twig_Error_Runtime" TSRMLS_CC, "Method \"%s\" for object \"%s\" does not exist", item, TWIG_GET_CLASS_NAME(object TSRMLS_CC));
  815. return;
  816. }
  817. if (isDefinedTest) {
  818. efree(tmp_method_name_get);
  819. efree(tmp_method_name_is);
  820. efree(lcItem);
  821. RETURN_TRUE;
  822. }
  823. /*
  824. if ($this->env->hasExtension('sandbox')) {
  825. $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
  826. }
  827. */
  828. if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "hasExtension", "sandbox" TSRMLS_CC)) {
  829. TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getExtension", "sandbox" TSRMLS_CC), "checkMethodAllowed", object, &zitem TSRMLS_CC);
  830. }
  831. if (EG(exception)) {
  832. return;
  833. }
  834. /*
  835. $ret = call_user_func_array(array($object, $method), $arguments);
  836. */
  837. if (Z_TYPE_P(object) == IS_OBJECT) {
  838. ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments TSRMLS_CC);
  839. free_ret = 1;
  840. }
  841. efree(tmp_method_name_get);
  842. efree(tmp_method_name_is);
  843. efree(lcItem);
  844. }
  845. /*
  846. if ($object instanceof Twig_TemplateInterface) {
  847. return new Twig_Markup($ret, $this->env->getCharset());
  848. }
  849. */
  850. if (TWIG_INSTANCE_OF_USERLAND(object, "Twig_TemplateInterface" TSRMLS_CC)) {
  851. zval *charset = TWIG_CALL_USER_FUNC_ARRAY(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getCharset" TSRMLS_CC, NULL TSRMLS_CC);
  852. TWIG_NEW(return_value, "Twig_Markup", ret TSRMLS_CC, charset TSRMLS_CC);
  853. zval_ptr_dtor(&charset);
  854. if (ret) {
  855. zval_ptr_dtor(&ret);
  856. }
  857. return;
  858. }
  859. /*
  860. return $ret;
  861. */
  862. if (ret) {
  863. RETVAL_ZVAL(ret, 1, 0);
  864. if (free_ret) {
  865. zval_ptr_dtor(&ret);
  866. }
  867. }
  868. }