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.

548 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. p = EG(argument_stack).top_element-1;
  82. arg_count = (ulong) *p; /* this is the amount of arguments passed to func_num_args(); */
  83. p = EG(argument_stack).top_element-1-arg_count-1;
  84. if (p>=EG(argument_stack).elements) {
  85. RETURN_LONG((ulong) *p);
  86. } else {
  87. zend_error(E_WARNING, "func_num_args(): Called from the global scope - no function context");
  88. RETURN_LONG(-1);
  89. }
  90. }
  91. ZEND_FUNCTION(func_get_arg)
  92. {
  93. void **p;
  94. int arg_count;
  95. zval **z_requested_offset;
  96. zval *arg;
  97. long requested_offset;
  98. if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &z_requested_offset)==FAILURE) {
  99. RETURN_FALSE;
  100. }
  101. convert_to_long_ex(z_requested_offset);
  102. requested_offset = (*z_requested_offset)->value.lval;
  103. p = EG(argument_stack).top_element-1;
  104. arg_count = (ulong) *p; /* this is the amount of arguments passed to func_num_args(); */
  105. p = EG(argument_stack).top_element-1-arg_count-1;
  106. if (p<EG(argument_stack).elements) {
  107. zend_error(E_WARNING, "func_get_arg(): Called from the global scope - no function context");
  108. RETURN_FALSE;
  109. }
  110. arg_count = (ulong) *p;
  111. if (requested_offset>arg_count) {
  112. zend_error(E_WARNING, "func_get_arg(): Only %d arguments passed to function (argument %d requested)", arg_count, requested_offset);
  113. RETURN_FALSE;
  114. }
  115. arg = *(p-(arg_count-requested_offset));
  116. *return_value = *arg;
  117. zval_copy_ctor(return_value);
  118. }
  119. ZEND_FUNCTION(func_get_args)
  120. {
  121. void **p;
  122. int arg_count;
  123. int i;
  124. p = EG(argument_stack).top_element-1;
  125. arg_count = (ulong) *p; /* this is the amount of arguments passed to func_num_args(); */
  126. p = EG(argument_stack).top_element-1-arg_count-1;
  127. if (p<EG(argument_stack).elements) {
  128. zend_error(E_WARNING, "func_get_args(): Called from the global scope - no function context");
  129. RETURN_FALSE;
  130. }
  131. arg_count = (ulong) *p;
  132. array_init(return_value);
  133. for (i=0; i<arg_count; i++) {
  134. zval *element;
  135. ALLOC_ZVAL(element);
  136. *element = **((zval **) (p-(arg_count-i)));
  137. zval_copy_ctor(element);
  138. INIT_PZVAL(element);
  139. zend_hash_next_index_insert(return_value->value.ht, &element, sizeof(zval *), NULL);
  140. }
  141. }
  142. /* {{{ proto int strlen(string str)
  143. Get string length */
  144. ZEND_FUNCTION(strlen)
  145. {
  146. zval **str;
  147. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) {
  148. WRONG_PARAM_COUNT;
  149. }
  150. convert_to_string_ex(str);
  151. RETVAL_LONG((*str)->value.str.len);
  152. }
  153. /* }}} */
  154. /* {{{ proto int strcmp(string str1, string str2)
  155. Binary safe string comparison */
  156. ZEND_FUNCTION(strcmp)
  157. {
  158. zval **s1, **s2;
  159. if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &s1, &s2) == FAILURE) {
  160. WRONG_PARAM_COUNT;
  161. }
  162. convert_to_string_ex(s1);
  163. convert_to_string_ex(s2);
  164. RETURN_LONG(zend_binary_zval_strcmp(*s1,*s2));
  165. }
  166. /* }}} */
  167. /* {{{ proto int strcasecmp(string str1, string str2)
  168. Binary safe case-insensitive string comparison */
  169. ZEND_FUNCTION(strcasecmp)
  170. {
  171. zval **s1, **s2;
  172. if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &s1, &s2) == FAILURE) {
  173. WRONG_PARAM_COUNT;
  174. }
  175. convert_to_string_ex(s1);
  176. convert_to_string_ex(s2);
  177. RETURN_LONG(zend_binary_zval_strcasecmp(*s1, *s2));
  178. }
  179. /* }}} */
  180. ZEND_FUNCTION(each)
  181. {
  182. zval **array,*entry,**entry_ptr, *tmp;
  183. char *string_key;
  184. ulong num_key;
  185. zval **inserted_pointer;
  186. HashTable *target_hash;
  187. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
  188. WRONG_PARAM_COUNT;
  189. }
  190. target_hash = HASH_OF(*array);
  191. if (!target_hash) {
  192. zend_error(E_WARNING,"Variable passed to each() is not an array or object");
  193. return;
  194. }
  195. if (zend_hash_get_current_data(target_hash, (void **) &entry_ptr)==FAILURE) {
  196. RETURN_FALSE;
  197. }
  198. array_init(return_value);
  199. entry = *entry_ptr;
  200. /* add value elements */
  201. if (entry->is_ref) {
  202. ALLOC_ZVAL(tmp);
  203. *tmp = *entry;
  204. zval_copy_ctor(tmp);
  205. tmp->is_ref=0;
  206. tmp->refcount=0;
  207. entry=tmp;
  208. }
  209. zend_hash_index_update(return_value->value.ht, 1, &entry, sizeof(zval *), NULL);
  210. entry->refcount++;
  211. zend_hash_update_ptr(return_value->value.ht, "value", sizeof("value"), entry, sizeof(zval *), NULL);
  212. entry->refcount++;
  213. /* add the key elements */
  214. switch (zend_hash_get_current_key(target_hash, &string_key, &num_key)) {
  215. case HASH_KEY_IS_STRING:
  216. add_get_index_string(return_value,0,string_key,(void **) &inserted_pointer,0);
  217. break;
  218. case HASH_KEY_IS_LONG:
  219. add_get_index_long(return_value,0,num_key, (void **) &inserted_pointer);
  220. break;
  221. }
  222. zend_hash_update(return_value->value.ht, "key", sizeof("key"), inserted_pointer, sizeof(zval *), NULL);
  223. (*inserted_pointer)->refcount++;
  224. zend_hash_move_forward(target_hash);
  225. }
  226. ZEND_FUNCTION(error_reporting)
  227. {
  228. zval **arg;
  229. int old_error_reporting;
  230. old_error_reporting = EG(error_reporting);
  231. switch (ZEND_NUM_ARGS()) {
  232. case 0:
  233. break;
  234. case 1:
  235. if (zend_get_parameters_ex(1,&arg) == FAILURE) {
  236. RETURN_FALSE;
  237. }
  238. convert_to_long_ex(arg);
  239. EG(error_reporting)=(*arg)->value.lval;
  240. break;
  241. default:
  242. WRONG_PARAM_COUNT;
  243. break;
  244. }
  245. RETVAL_LONG(old_error_reporting);
  246. }
  247. ZEND_FUNCTION(define)
  248. {
  249. zval **var, **val, **non_cs;
  250. int case_sensitive;
  251. zend_constant c;
  252. switch(ZEND_NUM_ARGS()) {
  253. case 2:
  254. if (zend_get_parameters_ex(2, &var, &val)==FAILURE) {
  255. RETURN_FALSE;
  256. }
  257. case_sensitive = CONST_CS;
  258. break;
  259. case 3:
  260. if (zend_get_parameters_ex(3, &var, &val, &non_cs)==FAILURE) {
  261. RETURN_FALSE;
  262. }
  263. convert_to_long_ex(non_cs);
  264. if ((*non_cs)->value.lval) {
  265. case_sensitive = 0;
  266. } else {
  267. case_sensitive = CONST_CS;
  268. }
  269. break;
  270. default:
  271. WRONG_PARAM_COUNT;
  272. break;
  273. }
  274. switch((*val)->type) {
  275. case IS_LONG:
  276. case IS_DOUBLE:
  277. case IS_STRING:
  278. case IS_BOOL:
  279. case IS_RESOURCE:
  280. case IS_NULL:
  281. break;
  282. default:
  283. zend_error(E_WARNING,"Constants may only evaluate to scalar values");
  284. RETURN_FALSE;
  285. break;
  286. }
  287. convert_to_string_ex(var);
  288. c.value = **val;
  289. zval_copy_ctor(&c.value);
  290. c.flags = case_sensitive | ~CONST_PERSISTENT; /* non persistent */
  291. c.name = zend_strndup((*var)->value.str.val, (*var)->value.str.len);
  292. c.name_len = (*var)->value.str.len+1;
  293. zend_register_constant(&c ELS_CC);
  294. RETURN_TRUE;
  295. }
  296. ZEND_FUNCTION(defined)
  297. {
  298. zval **var;
  299. zval c;
  300. if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &var)==FAILURE) {
  301. WRONG_PARAM_COUNT;
  302. }
  303. convert_to_string_ex(var);
  304. if (zend_get_constant((*var)->value.str.val, (*var)->value.str.len, &c)) {
  305. zval_dtor(&c);
  306. RETURN_LONG(1);
  307. } else {
  308. RETURN_LONG(0);
  309. }
  310. }
  311. /* {{{ proto string get_class(object object)
  312. Retrieves the class name ...
  313. */
  314. ZEND_FUNCTION(get_class)
  315. {
  316. zval **arg;
  317. if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &arg)==FAILURE) {
  318. RETURN_FALSE;
  319. }
  320. if ((*arg)->type != IS_OBJECT) {
  321. RETURN_FALSE;
  322. }
  323. RETURN_STRINGL((*arg)->value.obj.ce->name, (*arg)->value.obj.ce->name_length, 1);
  324. }
  325. /* }}} */
  326. /* {{{ proto string get_parent_class(object object)
  327. Retrieves the parent class name ...
  328. */
  329. ZEND_FUNCTION(get_parent_class)
  330. {
  331. zval **arg;
  332. if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &arg)==FAILURE) {
  333. RETURN_FALSE;
  334. }
  335. if (((*arg)->type != IS_OBJECT) || !(*arg)->value.obj.ce->parent) {
  336. RETURN_FALSE;
  337. }
  338. RETURN_STRINGL((*arg)->value.obj.ce->parent->name, (*arg)->value.obj.ce->parent->name_length, 1);
  339. }
  340. /* }}} */
  341. /* {{{ proto bool is_subclass_of(object object, string class_name)
  342. Returns true if the object has this class as one of its parents */
  343. ZEND_FUNCTION(is_subclass_of)
  344. {
  345. zval **obj, **class_name;
  346. char *lcname;
  347. zend_class_entry *parent_ce = NULL;
  348. if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &obj, &class_name)==FAILURE) {
  349. RETURN_FALSE;
  350. }
  351. if ((*obj)->type != IS_OBJECT) {
  352. RETURN_FALSE;
  353. }
  354. convert_to_string_ex(class_name);
  355. lcname = estrndup((*class_name)->value.str.val, (*class_name)->value.str.len);
  356. zend_str_tolower(lcname, (*class_name)->value.str.len);
  357. for (parent_ce = (*obj)->value.obj.ce->parent; parent_ce != NULL; parent_ce = parent_ce->parent) {
  358. if (!strcmp(parent_ce->name, lcname)) {
  359. efree(lcname);
  360. RETURN_TRUE;
  361. }
  362. }
  363. efree(lcname);
  364. RETURN_FALSE;
  365. }
  366. /* }}} */
  367. /* {{{ proto bool method_exists(object object, string method)
  368. Checks if the class method exists ...
  369. */
  370. ZEND_FUNCTION(method_exists)
  371. {
  372. zval **klass, **method_name;
  373. char *lcname;
  374. if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &klass, &method_name)==FAILURE) {
  375. RETURN_FALSE;
  376. }
  377. if ((*klass)->type != IS_OBJECT) {
  378. RETURN_FALSE;
  379. }
  380. convert_to_string_ex(method_name);
  381. lcname = estrndup((*method_name)->value.str.val, (*method_name)->value.str.len);
  382. zend_str_tolower(lcname, (*method_name)->value.str.len);
  383. if(zend_hash_exists(&(*klass)->value.obj.ce->function_table, lcname, (*method_name)->value.str.len+1)) {
  384. efree(lcname);
  385. RETURN_TRUE;
  386. } else {
  387. efree(lcname);
  388. RETURN_FALSE;
  389. }
  390. }
  391. /* }}} */
  392. /* {{{ proto bool class_exists(string classname)
  393. Checks if the class exists ...
  394. */
  395. ZEND_FUNCTION(class_exists)
  396. {
  397. zval **class_name;
  398. char *lcname;
  399. CLS_FETCH();
  400. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &class_name)==FAILURE) {
  401. RETURN_FALSE;
  402. }
  403. convert_to_string_ex(class_name);
  404. lcname = estrndup((*class_name)->value.str.val, (*class_name)->value.str.len);
  405. zend_str_tolower(lcname, (*class_name)->value.str.len);
  406. if (zend_hash_exists(CG(class_table), lcname, (*class_name)->value.str.len+1)) {
  407. efree(lcname);
  408. RETURN_TRUE;
  409. } else {
  410. efree(lcname);
  411. RETURN_FALSE;
  412. }
  413. }
  414. /* }}} */
  415. /* {{{ proto bool function_exists(string function_name)
  416. Checks if the function exists */
  417. ZEND_FUNCTION(function_exists)
  418. {
  419. zval **function_name;
  420. char *lcname;
  421. int retval;
  422. if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &function_name)==FAILURE) {
  423. WRONG_PARAM_COUNT;
  424. }
  425. convert_to_string_ex(function_name);
  426. lcname = estrndup((*function_name)->value.str.val, (*function_name)->value.str.len);
  427. zend_str_tolower(lcname, (*function_name)->value.str.len);
  428. retval = zend_hash_exists(EG(function_table), lcname, (*function_name)->value.str.len+1);
  429. efree(lcname);
  430. RETURN_BOOL(retval);
  431. }
  432. /* }}} */
  433. ZEND_FUNCTION(leak)
  434. {
  435. int leakbytes=3;
  436. zval **leak;
  437. if (ZEND_NUM_ARGS()>=1) {
  438. if (zend_get_parameters_ex(1, &leak)==SUCCESS) {
  439. convert_to_long_ex(leak);
  440. leakbytes = (*leak)->value.lval;
  441. }
  442. }
  443. emalloc(leakbytes);
  444. }
  445. static int copy_import_use_file(zend_file_handle *fh, zval *array)
  446. {
  447. if (fh->filename) {
  448. char *extension_start;
  449. extension_start = strstr(fh->filename, zend_uv.import_use_extension);
  450. if (extension_start) {
  451. *extension_start = 0;
  452. if (fh->opened_path) {
  453. add_assoc_string(array, fh->filename, fh->opened_path, 1);
  454. } else {
  455. add_assoc_stringl(array, fh->filename, "N/A", sizeof("N/A")-1, 1);
  456. }
  457. *extension_start = zend_uv.import_use_extension[0];
  458. }
  459. }
  460. return 0;
  461. }
  462. ZEND_FUNCTION(get_used_files)
  463. {
  464. CLS_FETCH();
  465. array_init(return_value);
  466. zend_hash_apply_with_argument(&CG(used_files), (int (*)(void *, void *)) copy_import_use_file, return_value);
  467. }
  468. ZEND_FUNCTION(get_imported_files)
  469. {
  470. array_init(return_value);
  471. zend_hash_apply_with_argument(&EG(imported_files), (int (*)(void *, void *)) copy_import_use_file, return_value);
  472. }