twig.c 26KB

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