You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

557 lines
14 KiB

26 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998, 1999 Andi Gutmans, Zeev Suraski |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 0.91 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.zend.com/license/0_91.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_API.h"
  21. #include "zend_builtin_functions.h"
  22. #include "zend_operators.h"
  23. #include "zend_variables.h"
  24. #include "zend_constants.h"
  25. static ZEND_FUNCTION(zend_version);
  26. static ZEND_FUNCTION(func_num_args);
  27. static ZEND_FUNCTION(func_get_arg);
  28. static ZEND_FUNCTION(func_get_args);
  29. static ZEND_FUNCTION(strlen);
  30. static ZEND_FUNCTION(strcmp);
  31. static ZEND_FUNCTION(strcasecmp);
  32. static ZEND_FUNCTION(each);
  33. static ZEND_FUNCTION(error_reporting);
  34. static ZEND_FUNCTION(define);
  35. static ZEND_FUNCTION(defined);
  36. static ZEND_FUNCTION(get_class);
  37. static ZEND_FUNCTION(get_parent_class);
  38. static ZEND_FUNCTION(method_exists);
  39. static ZEND_FUNCTION(class_exists);
  40. static ZEND_FUNCTION(function_exists);
  41. static ZEND_FUNCTION(leak);
  42. static ZEND_FUNCTION(get_used_files);
  43. static ZEND_FUNCTION(get_imported_files);
  44. static ZEND_FUNCTION(is_subclass_of);
  45. extern unsigned char first_arg_force_ref[];
  46. static zend_function_entry builtin_functions[] = {
  47. ZEND_FE(zend_version, NULL)
  48. ZEND_FE(func_num_args, NULL)
  49. ZEND_FE(func_get_arg, NULL)
  50. ZEND_FE(func_get_args, NULL)
  51. ZEND_FE(strlen, NULL)
  52. ZEND_FE(strcmp, NULL)
  53. ZEND_FE(strcasecmp, NULL)
  54. ZEND_FE(each, first_arg_force_ref)
  55. ZEND_FE(error_reporting, NULL)
  56. ZEND_FE(define, NULL)
  57. ZEND_FE(defined, NULL)
  58. ZEND_FE(get_class, NULL)
  59. ZEND_FE(get_parent_class, NULL)
  60. ZEND_FE(method_exists, NULL)
  61. ZEND_FE(class_exists, NULL)
  62. ZEND_FE(function_exists, NULL)
  63. ZEND_FE(leak, NULL)
  64. ZEND_FE(get_used_files, NULL)
  65. ZEND_FE(get_imported_files, NULL)
  66. ZEND_FE(is_subclass_of, NULL)
  67. { NULL, NULL, NULL }
  68. };
  69. int zend_startup_builtin_functions()
  70. {
  71. return zend_register_functions(builtin_functions, NULL);
  72. }
  73. ZEND_FUNCTION(zend_version)
  74. {
  75. RETURN_STRINGL(ZEND_VERSION, sizeof(ZEND_VERSION)-1, 1);
  76. }
  77. ZEND_FUNCTION(func_num_args)
  78. {
  79. void **p;
  80. int arg_count;
  81. ELS_FETCH();
  82. p = EG(argument_stack).top_element-1;
  83. arg_count = (ulong) *p; /* this is the amount of arguments passed to func_num_args(); */
  84. p = EG(argument_stack).top_element-1-arg_count-1;
  85. if (p>=EG(argument_stack).elements) {
  86. RETURN_LONG((ulong) *p);
  87. } else {
  88. zend_error(E_WARNING, "func_num_args(): Called from the global scope - no function context");
  89. RETURN_LONG(-1);
  90. }
  91. }
  92. ZEND_FUNCTION(func_get_arg)
  93. {
  94. void **p;
  95. int arg_count;
  96. zval **z_requested_offset;
  97. zval *arg;
  98. long requested_offset;
  99. ELS_FETCH();
  100. if (ARG_COUNT(ht)!=1 || getParametersEx(1, &z_requested_offset)==FAILURE) {
  101. RETURN_FALSE;
  102. }
  103. convert_to_long_ex(z_requested_offset);
  104. requested_offset = (*z_requested_offset)->value.lval;
  105. p = EG(argument_stack).top_element-1;
  106. arg_count = (ulong) *p; /* this is the amount of arguments passed to func_num_args(); */
  107. p = EG(argument_stack).top_element-1-arg_count-1;
  108. if (p<EG(argument_stack).elements) {
  109. zend_error(E_WARNING, "func_get_arg(): Called from the global scope - no function context");
  110. RETURN_FALSE;
  111. }
  112. arg_count = (ulong) *p;
  113. if (requested_offset>arg_count) {
  114. zend_error(E_WARNING, "func_get_arg(): Only %d arguments passed to function (argument %d requested)", arg_count, requested_offset);
  115. RETURN_FALSE;
  116. }
  117. arg = *(p-(arg_count-requested_offset));
  118. *return_value = *arg;
  119. zval_copy_ctor(return_value);
  120. }
  121. ZEND_FUNCTION(func_get_args)
  122. {
  123. void **p;
  124. int arg_count;
  125. int i;
  126. ELS_FETCH();
  127. p = EG(argument_stack).top_element-1;
  128. arg_count = (ulong) *p; /* this is the amount of arguments passed to func_num_args(); */
  129. p = EG(argument_stack).top_element-1-arg_count-1;
  130. if (p<EG(argument_stack).elements) {
  131. zend_error(E_WARNING, "func_get_args(): Called from the global scope - no function context");
  132. RETURN_FALSE;
  133. }
  134. arg_count = (ulong) *p;
  135. array_init(return_value);
  136. for (i=0; i<arg_count; i++) {
  137. zval *element;
  138. element = (zval *) emalloc(sizeof(zval));
  139. *element = **((zval **) (p-(arg_count-i)));
  140. zval_copy_ctor(element);
  141. INIT_PZVAL(element);
  142. zend_hash_next_index_insert(return_value->value.ht, &element, sizeof(zval *), NULL);
  143. }
  144. }
  145. /* {{{ proto int strlen(string str)
  146. Get string length */
  147. ZEND_FUNCTION(strlen)
  148. {
  149. zval **str;
  150. if (ARG_COUNT(ht) != 1 || getParametersEx(1, &str) == FAILURE) {
  151. WRONG_PARAM_COUNT;
  152. }
  153. convert_to_string_ex(str);
  154. RETVAL_LONG((*str)->value.str.len);
  155. }
  156. /* }}} */
  157. /* {{{ proto int strcmp(string str1, string str2)
  158. Binary safe string comparison */
  159. ZEND_FUNCTION(strcmp)
  160. {
  161. zval **s1, **s2;
  162. if (ARG_COUNT(ht) != 2 || getParametersEx(2, &s1, &s2) == FAILURE) {
  163. WRONG_PARAM_COUNT;
  164. }
  165. convert_to_string_ex(s1);
  166. convert_to_string_ex(s2);
  167. RETURN_LONG(zend_binary_strcmp(*s1,*s2));
  168. }
  169. /* }}} */
  170. /* {{{ proto int strcasecmp(string str1, string str2)
  171. Binary safe case-insensitive string comparison */
  172. ZEND_FUNCTION(strcasecmp)
  173. {
  174. zval **s1, **s2;
  175. if (ARG_COUNT(ht)!=2 || getParametersEx(2, &s1, &s2) == FAILURE) {
  176. WRONG_PARAM_COUNT;
  177. }
  178. convert_to_string_ex(s1);
  179. convert_to_string_ex(s2);
  180. RETURN_LONG(zend_binary_strcasecmp(*s1, *s2));
  181. }
  182. /* }}} */
  183. ZEND_FUNCTION(each)
  184. {
  185. zval **array,*entry,**entry_ptr, *tmp;
  186. char *string_key;
  187. ulong num_key;
  188. zval **inserted_pointer;
  189. HashTable *target_hash;
  190. if (ARG_COUNT(ht) != 1 || getParametersEx(1, &array) == FAILURE) {
  191. WRONG_PARAM_COUNT;
  192. }
  193. target_hash = HASH_OF(*array);
  194. if (!target_hash) {
  195. zend_error(E_WARNING,"Variable passed to each() is not an array or object");
  196. return;
  197. }
  198. if (zend_hash_get_current_data(target_hash, (void **) &entry_ptr)==FAILURE) {
  199. RETURN_FALSE;
  200. }
  201. array_init(return_value);
  202. entry = *entry_ptr;
  203. /* add value elements */
  204. if (entry->is_ref) {
  205. tmp = (zval *)emalloc(sizeof(zval));
  206. *tmp = *entry;
  207. zval_copy_ctor(tmp);
  208. tmp->is_ref=0;
  209. tmp->refcount=0;
  210. entry=tmp;
  211. }
  212. zend_hash_index_update(return_value->value.ht, 1, &entry, sizeof(zval *), NULL);
  213. entry->refcount++;
  214. zend_hash_update_ptr(return_value->value.ht, "value", sizeof("value"), entry, sizeof(zval *), NULL);
  215. entry->refcount++;
  216. /* add the key elements */
  217. switch (zend_hash_get_current_key(target_hash, &string_key, &num_key)) {
  218. case HASH_KEY_IS_STRING:
  219. add_get_index_string(return_value,0,string_key,(void **) &inserted_pointer,0);
  220. break;
  221. case HASH_KEY_IS_LONG:
  222. add_get_index_long(return_value,0,num_key, (void **) &inserted_pointer);
  223. break;
  224. }
  225. zend_hash_update(return_value->value.ht, "key", sizeof("key"), inserted_pointer, sizeof(zval *), NULL);
  226. (*inserted_pointer)->refcount++;
  227. zend_hash_move_forward(target_hash);
  228. }
  229. ZEND_FUNCTION(error_reporting)
  230. {
  231. zval **arg;
  232. int old_error_reporting;
  233. ELS_FETCH();
  234. old_error_reporting = EG(error_reporting);
  235. switch (ARG_COUNT(ht)) {
  236. case 0:
  237. break;
  238. case 1:
  239. if (getParametersEx(1,&arg) == FAILURE) {
  240. RETURN_FALSE;
  241. }
  242. convert_to_long_ex(arg);
  243. EG(error_reporting)=(*arg)->value.lval;
  244. break;
  245. default:
  246. WRONG_PARAM_COUNT;
  247. break;
  248. }
  249. RETVAL_LONG(old_error_reporting);
  250. }
  251. ZEND_FUNCTION(define)
  252. {
  253. zval **var, **val, **non_cs;
  254. int case_sensitive;
  255. zend_constant c;
  256. ELS_FETCH();
  257. switch(ARG_COUNT(ht)) {
  258. case 2:
  259. if (getParametersEx(2, &var, &val)==FAILURE) {
  260. RETURN_FALSE;
  261. }
  262. case_sensitive = CONST_CS;
  263. break;
  264. case 3:
  265. if (getParametersEx(3, &var, &val, &non_cs)==FAILURE) {
  266. RETURN_FALSE;
  267. }
  268. convert_to_long_ex(non_cs);
  269. if ((*non_cs)->value.lval) {
  270. case_sensitive = 0;
  271. } else {
  272. case_sensitive = CONST_CS;
  273. }
  274. break;
  275. default:
  276. WRONG_PARAM_COUNT;
  277. break;
  278. }
  279. switch((*val)->type) {
  280. case IS_LONG:
  281. case IS_DOUBLE:
  282. case IS_STRING:
  283. case IS_BOOL:
  284. case IS_RESOURCE:
  285. break;
  286. default:
  287. zend_error(E_WARNING,"Constants may only evaluate to scalar values");
  288. RETURN_FALSE;
  289. break;
  290. }
  291. convert_to_string_ex(var);
  292. c.value = **val;
  293. zval_copy_ctor(&c.value);
  294. c.flags = case_sensitive | ~CONST_PERSISTENT; /* non persistent */
  295. c.name = zend_strndup((*var)->value.str.val, (*var)->value.str.len);
  296. c.name_len = (*var)->value.str.len+1;
  297. zend_register_constant(&c ELS_CC);
  298. RETURN_TRUE;
  299. }
  300. ZEND_FUNCTION(defined)
  301. {
  302. zval **var;
  303. zval c;
  304. if (ARG_COUNT(ht)!=1 || getParametersEx(1, &var)==FAILURE) {
  305. WRONG_PARAM_COUNT;
  306. }
  307. convert_to_string_ex(var);
  308. if (zend_get_constant((*var)->value.str.val, (*var)->value.str.len, &c)) {
  309. zval_dtor(&c);
  310. RETURN_LONG(1);
  311. } else {
  312. RETURN_LONG(0);
  313. }
  314. }
  315. /* {{{ proto string get_class(object object)
  316. Retrieves the class name ...
  317. */
  318. ZEND_FUNCTION(get_class)
  319. {
  320. zval **arg;
  321. if (ARG_COUNT(ht)!=1 || getParametersEx(1, &arg)==FAILURE) {
  322. RETURN_FALSE;
  323. }
  324. if ((*arg)->type != IS_OBJECT) {
  325. RETURN_FALSE;
  326. }
  327. RETURN_STRINGL((*arg)->value.obj.ce->name, (*arg)->value.obj.ce->name_length, 1);
  328. }
  329. /* }}} */
  330. /* {{{ proto string get_parent_class(object object)
  331. Retrieves the parent class name ...
  332. */
  333. ZEND_FUNCTION(get_parent_class)
  334. {
  335. zval **arg;
  336. if (ARG_COUNT(ht)!=1 || getParametersEx(1, &arg)==FAILURE) {
  337. RETURN_FALSE;
  338. }
  339. if (((*arg)->type != IS_OBJECT) || !(*arg)->value.obj.ce->parent) {
  340. RETURN_FALSE;
  341. }
  342. RETURN_STRINGL((*arg)->value.obj.ce->parent->name, (*arg)->value.obj.ce->parent->name_length, 1);
  343. }
  344. /* }}} */
  345. /* {{{ proto bool is_subclass_of(object object, string class_name)
  346. Returns true if the object has this class as one of its parents */
  347. ZEND_FUNCTION(is_subclass_of)
  348. {
  349. zval **obj, **class_name;
  350. char *lcname;
  351. zend_class_entry *parent_ce = NULL;
  352. CLS_FETCH();
  353. if (ARG_COUNT(ht) != 2 || getParametersEx(2, &obj, &class_name)==FAILURE) {
  354. RETURN_FALSE;
  355. }
  356. if ((*obj)->type != IS_OBJECT) {
  357. RETURN_FALSE;
  358. }
  359. convert_to_string_ex(class_name);
  360. lcname = estrndup((*class_name)->value.str.val, (*class_name)->value.str.len);
  361. zend_str_tolower(lcname, (*class_name)->value.str.len);
  362. for (parent_ce = (*obj)->value.obj.ce->parent; parent_ce != NULL; parent_ce = parent_ce->parent) {
  363. if (!strcmp(parent_ce->name, lcname)) {
  364. efree(lcname);
  365. RETURN_TRUE;
  366. }
  367. }
  368. efree(lcname);
  369. RETURN_FALSE;
  370. }
  371. /* }}} */
  372. /* {{{ proto bool method_exists(object object, string method)
  373. Checks if the class method exists ...
  374. */
  375. ZEND_FUNCTION(method_exists)
  376. {
  377. zval **klass, **method_name;
  378. char *lcname;
  379. if (ARG_COUNT(ht)!=2 || getParametersEx(2, &klass, &method_name)==FAILURE) {
  380. RETURN_FALSE;
  381. }
  382. if ((*klass)->type != IS_OBJECT) {
  383. RETURN_FALSE;
  384. }
  385. convert_to_string_ex(method_name);
  386. lcname = estrndup((*method_name)->value.str.val, (*method_name)->value.str.len);
  387. zend_str_tolower(lcname, (*method_name)->value.str.len);
  388. if(zend_hash_exists(&(*klass)->value.obj.ce->function_table, lcname, (*method_name)->value.str.len+1)) {
  389. efree(lcname);
  390. RETURN_TRUE;
  391. } else {
  392. efree(lcname);
  393. RETURN_FALSE;
  394. }
  395. }
  396. /* }}} */
  397. /* {{{ proto bool class_exists(string classname)
  398. Checks if the class exists ...
  399. */
  400. ZEND_FUNCTION(class_exists)
  401. {
  402. zval **class_name;
  403. char *lcname;
  404. CLS_FETCH();
  405. if (ARG_COUNT(ht) != 1 || getParametersEx(1, &class_name)==FAILURE) {
  406. RETURN_FALSE;
  407. }
  408. convert_to_string_ex(class_name);
  409. lcname = estrndup((*class_name)->value.str.val, (*class_name)->value.str.len);
  410. zend_str_tolower(lcname, (*class_name)->value.str.len);
  411. if (zend_hash_exists(CG(class_table), lcname, (*class_name)->value.str.len+1)) {
  412. efree(lcname);
  413. RETURN_TRUE;
  414. } else {
  415. efree(lcname);
  416. RETURN_FALSE;
  417. }
  418. }
  419. /* }}} */
  420. /* {{{ proto bool function_exists(string function_name)
  421. Checks if the function exists */
  422. ZEND_FUNCTION(function_exists)
  423. {
  424. zval **function_name;
  425. char *lcname;
  426. CLS_FETCH();
  427. if (ARG_COUNT(ht)!=1 || getParametersEx(1, &function_name)==FAILURE) {
  428. WRONG_PARAM_COUNT;
  429. }
  430. convert_to_string_ex(function_name);
  431. lcname = estrndup((*function_name)->value.str.val, (*function_name)->value.str.len);
  432. zend_str_tolower(lcname, (*function_name)->value.str.len);
  433. if (zend_hash_exists(CG(function_table), lcname, (*function_name)->value.str.len+1) == FAILURE) {
  434. efree(lcname);
  435. RETURN_FALSE;
  436. } else {
  437. efree(lcname);
  438. RETURN_TRUE;
  439. }
  440. }
  441. /* }}} */
  442. ZEND_FUNCTION(leak)
  443. {
  444. int leakbytes=3;
  445. zval **leak;
  446. if (ARG_COUNT(ht)>=1) {
  447. if (getParametersEx(1, &leak)==SUCCESS) {
  448. convert_to_long_ex(leak);
  449. leakbytes = (*leak)->value.lval;
  450. }
  451. }
  452. emalloc(leakbytes);
  453. }
  454. static int copy_import_use_file(zend_file_handle *fh, zval *array)
  455. {
  456. if (fh->filename) {
  457. char *extension_start;
  458. extension_start = strstr(fh->filename, zend_uv.import_use_extension);
  459. if (extension_start) {
  460. *extension_start = 0;
  461. if (fh->opened_path) {
  462. add_assoc_string(array, fh->filename, fh->opened_path, 1);
  463. } else {
  464. add_assoc_stringl(array, fh->filename, "N/A", sizeof("N/A")-1, 1);
  465. }
  466. *extension_start = zend_uv.import_use_extension[0];
  467. }
  468. }
  469. return 0;
  470. }
  471. ZEND_FUNCTION(get_used_files)
  472. {
  473. CLS_FETCH();
  474. array_init(return_value);
  475. zend_hash_apply_with_argument(&CG(used_files), (int (*)(void *, void *)) copy_import_use_file, return_value);
  476. }
  477. ZEND_FUNCTION(get_imported_files)
  478. {
  479. ELS_FETCH();
  480. array_init(return_value);
  481. zend_hash_apply_with_argument(&EG(imported_files), (int (*)(void *, void *)) copy_import_use_file, return_value);
  482. }