PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/standard/dir.c

https://github.com/php/php-src
C | 581 lines | 422 code | 97 blank | 62 comment | 77 complexity | 2643521ab63dade3139a1efd51c04605 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Thies C. Arntzen <thies@thieso.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. /* {{{ includes/startup/misc */
  17. #include "php.h"
  18. #include "fopen_wrappers.h"
  19. #include "file.h"
  20. #include "php_dir.h"
  21. #include "php_string.h"
  22. #include "php_scandir.h"
  23. #include "basic_functions.h"
  24. #include "dir_arginfo.h"
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <errno.h>
  29. #ifdef PHP_WIN32
  30. #include "win32/readdir.h"
  31. #endif
  32. #ifdef HAVE_GLOB
  33. #ifndef PHP_WIN32
  34. #include <glob.h>
  35. #else
  36. #include "win32/glob.h"
  37. #endif
  38. #endif
  39. typedef struct {
  40. zend_resource *default_dir;
  41. } php_dir_globals;
  42. #ifdef ZTS
  43. #define DIRG(v) ZEND_TSRMG(dir_globals_id, php_dir_globals *, v)
  44. int dir_globals_id;
  45. #else
  46. #define DIRG(v) (dir_globals.v)
  47. php_dir_globals dir_globals;
  48. #endif
  49. static zend_class_entry *dir_class_entry_ptr;
  50. #define Z_DIRECTORY_PATH_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 0)
  51. #define Z_DIRECTORY_HANDLE_P(zv) OBJ_PROP_NUM(Z_OBJ_P(zv), 1)
  52. #define FETCH_DIRP() \
  53. myself = getThis(); \
  54. if (!myself) { \
  55. ZEND_PARSE_PARAMETERS_START(0, 1) \
  56. Z_PARAM_OPTIONAL \
  57. Z_PARAM_RESOURCE_OR_NULL(id) \
  58. ZEND_PARSE_PARAMETERS_END(); \
  59. if (id) { \
  60. if ((dirp = (php_stream *)zend_fetch_resource(Z_RES_P(id), "Directory", php_file_le_stream())) == NULL) { \
  61. RETURN_THROWS(); \
  62. } \
  63. } else { \
  64. if (!DIRG(default_dir)) { \
  65. zend_type_error("No resource supplied"); \
  66. RETURN_THROWS(); \
  67. } \
  68. if ((dirp = (php_stream *)zend_fetch_resource(DIRG(default_dir), "Directory", php_file_le_stream())) == NULL) { \
  69. RETURN_THROWS(); \
  70. } \
  71. } \
  72. } else { \
  73. ZEND_PARSE_PARAMETERS_NONE(); \
  74. zval *handle_zv = Z_DIRECTORY_HANDLE_P(myself); \
  75. if (Z_TYPE_P(handle_zv) != IS_RESOURCE) { \
  76. zend_throw_error(NULL, "Unable to find my handle property"); \
  77. RETURN_THROWS(); \
  78. } \
  79. if ((dirp = (php_stream *)zend_fetch_resource_ex(handle_zv, "Directory", php_file_le_stream())) == NULL) { \
  80. RETURN_THROWS(); \
  81. } \
  82. }
  83. static void php_set_default_dir(zend_resource *res)
  84. {
  85. if (DIRG(default_dir)) {
  86. zend_list_delete(DIRG(default_dir));
  87. }
  88. if (res) {
  89. GC_ADDREF(res);
  90. }
  91. DIRG(default_dir) = res;
  92. }
  93. PHP_RINIT_FUNCTION(dir)
  94. {
  95. DIRG(default_dir) = NULL;
  96. return SUCCESS;
  97. }
  98. PHP_MINIT_FUNCTION(dir)
  99. {
  100. static char dirsep_str[2], pathsep_str[2];
  101. dir_class_entry_ptr = register_class_Directory();
  102. #ifdef ZTS
  103. ts_allocate_id(&dir_globals_id, sizeof(php_dir_globals), NULL, NULL);
  104. #endif
  105. dirsep_str[0] = DEFAULT_SLASH;
  106. dirsep_str[1] = '\0';
  107. REGISTER_STRING_CONSTANT("DIRECTORY_SEPARATOR", dirsep_str, CONST_CS|CONST_PERSISTENT);
  108. pathsep_str[0] = ZEND_PATHS_SEPARATOR;
  109. pathsep_str[1] = '\0';
  110. REGISTER_STRING_CONSTANT("PATH_SEPARATOR", pathsep_str, CONST_CS|CONST_PERSISTENT);
  111. REGISTER_LONG_CONSTANT("SCANDIR_SORT_ASCENDING", PHP_SCANDIR_SORT_ASCENDING, CONST_CS | CONST_PERSISTENT);
  112. REGISTER_LONG_CONSTANT("SCANDIR_SORT_DESCENDING", PHP_SCANDIR_SORT_DESCENDING, CONST_CS | CONST_PERSISTENT);
  113. REGISTER_LONG_CONSTANT("SCANDIR_SORT_NONE", PHP_SCANDIR_SORT_NONE, CONST_CS | CONST_PERSISTENT);
  114. #ifdef HAVE_GLOB
  115. #ifdef GLOB_BRACE
  116. REGISTER_LONG_CONSTANT("GLOB_BRACE", GLOB_BRACE, CONST_CS | CONST_PERSISTENT);
  117. #else
  118. # define GLOB_BRACE 0
  119. #endif
  120. #ifdef GLOB_MARK
  121. REGISTER_LONG_CONSTANT("GLOB_MARK", GLOB_MARK, CONST_CS | CONST_PERSISTENT);
  122. #else
  123. # define GLOB_MARK 0
  124. #endif
  125. #ifdef GLOB_NOSORT
  126. REGISTER_LONG_CONSTANT("GLOB_NOSORT", GLOB_NOSORT, CONST_CS | CONST_PERSISTENT);
  127. #else
  128. # define GLOB_NOSORT 0
  129. #endif
  130. #ifdef GLOB_NOCHECK
  131. REGISTER_LONG_CONSTANT("GLOB_NOCHECK", GLOB_NOCHECK, CONST_CS | CONST_PERSISTENT);
  132. #else
  133. # define GLOB_NOCHECK 0
  134. #endif
  135. #ifdef GLOB_NOESCAPE
  136. REGISTER_LONG_CONSTANT("GLOB_NOESCAPE", GLOB_NOESCAPE, CONST_CS | CONST_PERSISTENT);
  137. #else
  138. # define GLOB_NOESCAPE 0
  139. #endif
  140. #ifdef GLOB_ERR
  141. REGISTER_LONG_CONSTANT("GLOB_ERR", GLOB_ERR, CONST_CS | CONST_PERSISTENT);
  142. #else
  143. # define GLOB_ERR 0
  144. #endif
  145. #ifndef GLOB_ONLYDIR
  146. # define GLOB_ONLYDIR (1<<30)
  147. # define GLOB_EMULATE_ONLYDIR
  148. # define GLOB_FLAGMASK (~GLOB_ONLYDIR)
  149. #else
  150. # define GLOB_FLAGMASK (~0)
  151. #endif
  152. /* This is used for checking validity of passed flags (passing invalid flags causes segfault in glob()!! */
  153. #define GLOB_AVAILABLE_FLAGS (0 | GLOB_BRACE | GLOB_MARK | GLOB_NOSORT | GLOB_NOCHECK | GLOB_NOESCAPE | GLOB_ERR | GLOB_ONLYDIR)
  154. REGISTER_LONG_CONSTANT("GLOB_ONLYDIR", GLOB_ONLYDIR, CONST_CS | CONST_PERSISTENT);
  155. REGISTER_LONG_CONSTANT("GLOB_AVAILABLE_FLAGS", GLOB_AVAILABLE_FLAGS, CONST_CS | CONST_PERSISTENT);
  156. #endif /* HAVE_GLOB */
  157. return SUCCESS;
  158. }
  159. /* }}} */
  160. /* {{{ internal functions */
  161. static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject)
  162. {
  163. char *dirname;
  164. size_t dir_len;
  165. zval *zcontext = NULL;
  166. php_stream_context *context = NULL;
  167. php_stream *dirp;
  168. ZEND_PARSE_PARAMETERS_START(1, 2)
  169. Z_PARAM_PATH(dirname, dir_len)
  170. Z_PARAM_OPTIONAL
  171. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  172. ZEND_PARSE_PARAMETERS_END();
  173. context = php_stream_context_from_zval(zcontext, 0);
  174. dirp = php_stream_opendir(dirname, REPORT_ERRORS, context);
  175. if (dirp == NULL) {
  176. RETURN_FALSE;
  177. }
  178. dirp->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
  179. php_set_default_dir(dirp->res);
  180. if (createobject) {
  181. object_init_ex(return_value, dir_class_entry_ptr);
  182. ZVAL_STRINGL(Z_DIRECTORY_PATH_P(return_value), dirname, dir_len);
  183. ZVAL_RES(Z_DIRECTORY_HANDLE_P(return_value), dirp->res);
  184. php_stream_auto_cleanup(dirp); /* so we don't get warnings under debug */
  185. } else {
  186. php_stream_to_zval(dirp, return_value);
  187. }
  188. }
  189. /* }}} */
  190. /* {{{ Open a directory and return a dir_handle */
  191. PHP_FUNCTION(opendir)
  192. {
  193. _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  194. }
  195. /* }}} */
  196. /* {{{ Directory class with properties, handle and class and methods read, rewind and close */
  197. PHP_FUNCTION(dir)
  198. {
  199. _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  200. }
  201. /* }}} */
  202. /* {{{ Close directory connection identified by the dir_handle */
  203. PHP_FUNCTION(closedir)
  204. {
  205. zval *id = NULL, *myself;
  206. php_stream *dirp;
  207. zend_resource *res;
  208. FETCH_DIRP();
  209. if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) {
  210. zend_argument_type_error(1, "must be a valid Directory resource");
  211. RETURN_THROWS();
  212. }
  213. res = dirp->res;
  214. zend_list_close(dirp->res);
  215. if (res == DIRG(default_dir)) {
  216. php_set_default_dir(NULL);
  217. }
  218. }
  219. /* }}} */
  220. #if defined(HAVE_CHROOT) && !defined(ZTS) && ENABLE_CHROOT_FUNC
  221. /* {{{ Change root directory */
  222. PHP_FUNCTION(chroot)
  223. {
  224. char *str;
  225. int ret;
  226. size_t str_len;
  227. ZEND_PARSE_PARAMETERS_START(1, 1)
  228. Z_PARAM_PATH(str, str_len)
  229. ZEND_PARSE_PARAMETERS_END();
  230. ret = chroot(str);
  231. if (ret != 0) {
  232. php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
  233. RETURN_FALSE;
  234. }
  235. php_clear_stat_cache(1, NULL, 0);
  236. ret = chdir("/");
  237. if (ret != 0) {
  238. php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
  239. RETURN_FALSE;
  240. }
  241. RETURN_TRUE;
  242. }
  243. /* }}} */
  244. #endif
  245. /* {{{ Change the current directory */
  246. PHP_FUNCTION(chdir)
  247. {
  248. char *str;
  249. int ret;
  250. size_t str_len;
  251. ZEND_PARSE_PARAMETERS_START(1, 1)
  252. Z_PARAM_PATH(str, str_len)
  253. ZEND_PARSE_PARAMETERS_END();
  254. if (php_check_open_basedir(str)) {
  255. RETURN_FALSE;
  256. }
  257. ret = VCWD_CHDIR(str);
  258. if (ret != 0) {
  259. php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
  260. RETURN_FALSE;
  261. }
  262. if (BG(CurrentStatFile) && !IS_ABSOLUTE_PATH(ZSTR_VAL(BG(CurrentStatFile)), ZSTR_LEN(BG(CurrentStatFile)))) {
  263. zend_string_release(BG(CurrentStatFile));
  264. BG(CurrentStatFile) = NULL;
  265. }
  266. if (BG(CurrentLStatFile) && !IS_ABSOLUTE_PATH(ZSTR_VAL(BG(CurrentLStatFile)), ZSTR_LEN(BG(CurrentLStatFile)))) {
  267. zend_string_release(BG(CurrentLStatFile));
  268. BG(CurrentLStatFile) = NULL;
  269. }
  270. RETURN_TRUE;
  271. }
  272. /* }}} */
  273. /* {{{ Gets the current directory */
  274. PHP_FUNCTION(getcwd)
  275. {
  276. char path[MAXPATHLEN];
  277. char *ret=NULL;
  278. ZEND_PARSE_PARAMETERS_NONE();
  279. #if HAVE_GETCWD
  280. ret = VCWD_GETCWD(path, MAXPATHLEN);
  281. #elif HAVE_GETWD
  282. ret = VCWD_GETWD(path);
  283. #endif
  284. if (ret) {
  285. RETURN_STRING(path);
  286. } else {
  287. RETURN_FALSE;
  288. }
  289. }
  290. /* }}} */
  291. /* {{{ Rewind dir_handle back to the start */
  292. PHP_FUNCTION(rewinddir)
  293. {
  294. zval *id = NULL, *myself;
  295. php_stream *dirp;
  296. FETCH_DIRP();
  297. if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) {
  298. zend_argument_type_error(1, "must be a valid Directory resource");
  299. RETURN_THROWS();
  300. }
  301. php_stream_rewinddir(dirp);
  302. }
  303. /* }}} */
  304. /* {{{ Read directory entry from dir_handle */
  305. PHP_FUNCTION(readdir)
  306. {
  307. zval *id = NULL, *myself;
  308. php_stream *dirp;
  309. php_stream_dirent entry;
  310. FETCH_DIRP();
  311. if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) {
  312. zend_argument_type_error(1, "must be a valid Directory resource");
  313. RETURN_THROWS();
  314. }
  315. if (php_stream_readdir(dirp, &entry)) {
  316. RETURN_STRINGL(entry.d_name, strlen(entry.d_name));
  317. }
  318. RETURN_FALSE;
  319. }
  320. /* }}} */
  321. #ifdef HAVE_GLOB
  322. /* {{{ Find pathnames matching a pattern */
  323. PHP_FUNCTION(glob)
  324. {
  325. size_t cwd_skip = 0;
  326. #ifdef ZTS
  327. char cwd[MAXPATHLEN];
  328. char work_pattern[MAXPATHLEN];
  329. char *result;
  330. #endif
  331. char *pattern = NULL;
  332. size_t pattern_len;
  333. zend_long flags = 0;
  334. glob_t globbuf;
  335. size_t n;
  336. int ret;
  337. bool basedir_limit = 0;
  338. zval tmp;
  339. ZEND_PARSE_PARAMETERS_START(1, 2)
  340. Z_PARAM_PATH(pattern, pattern_len)
  341. Z_PARAM_OPTIONAL
  342. Z_PARAM_LONG(flags)
  343. ZEND_PARSE_PARAMETERS_END();
  344. if (pattern_len >= MAXPATHLEN) {
  345. php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
  346. RETURN_FALSE;
  347. }
  348. if ((GLOB_AVAILABLE_FLAGS & flags) != flags) {
  349. php_error_docref(NULL, E_WARNING, "At least one of the passed flags is invalid or not supported on this platform");
  350. RETURN_FALSE;
  351. }
  352. #ifdef ZTS
  353. if (!IS_ABSOLUTE_PATH(pattern, pattern_len)) {
  354. result = VCWD_GETCWD(cwd, MAXPATHLEN);
  355. if (!result) {
  356. cwd[0] = '\0';
  357. }
  358. #ifdef PHP_WIN32
  359. if (IS_SLASH(*pattern)) {
  360. cwd[2] = '\0';
  361. }
  362. #endif
  363. cwd_skip = strlen(cwd)+1;
  364. snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, pattern);
  365. pattern = work_pattern;
  366. }
  367. #endif
  368. memset(&globbuf, 0, sizeof(glob_t));
  369. globbuf.gl_offs = 0;
  370. if (0 != (ret = glob(pattern, flags & GLOB_FLAGMASK, NULL, &globbuf))) {
  371. #ifdef GLOB_NOMATCH
  372. if (GLOB_NOMATCH == ret) {
  373. /* Some glob implementation simply return no data if no matches
  374. were found, others return the GLOB_NOMATCH error code.
  375. We don't want to treat GLOB_NOMATCH as an error condition
  376. so that PHP glob() behaves the same on both types of
  377. implementations and so that 'foreach (glob() as ...'
  378. can be used for simple glob() calls without further error
  379. checking.
  380. */
  381. goto no_results;
  382. }
  383. #endif
  384. RETURN_FALSE;
  385. }
  386. /* now catch the FreeBSD style of "no matches" */
  387. if (!globbuf.gl_pathc || !globbuf.gl_pathv) {
  388. #ifdef GLOB_NOMATCH
  389. no_results:
  390. #endif
  391. #ifndef PHP_WIN32
  392. /* Paths containing '*', '?' and some other chars are
  393. illegal on Windows but legit on other platforms. For
  394. this reason the direct basedir check against the glob
  395. query is senseless on windows. For instance while *.txt
  396. is a pretty valid filename on EXT3, it's invalid on NTFS. */
  397. if (PG(open_basedir) && *PG(open_basedir)) {
  398. if (php_check_open_basedir_ex(pattern, 0)) {
  399. RETURN_FALSE;
  400. }
  401. }
  402. #endif
  403. array_init(return_value);
  404. return;
  405. }
  406. array_init(return_value);
  407. for (n = 0; n < (size_t)globbuf.gl_pathc; n++) {
  408. if (PG(open_basedir) && *PG(open_basedir)) {
  409. if (php_check_open_basedir_ex(globbuf.gl_pathv[n], 0)) {
  410. basedir_limit = 1;
  411. continue;
  412. }
  413. }
  414. /* we need to do this every time since GLOB_ONLYDIR does not guarantee that
  415. * all directories will be filtered. GNU libc documentation states the
  416. * following:
  417. * If the information about the type of the file is easily available
  418. * non-directories will be rejected but no extra work will be done to
  419. * determine the information for each file. I.e., the caller must still be
  420. * able to filter directories out.
  421. */
  422. if (flags & GLOB_ONLYDIR) {
  423. zend_stat_t s;
  424. if (0 != VCWD_STAT(globbuf.gl_pathv[n], &s)) {
  425. continue;
  426. }
  427. if (S_IFDIR != (s.st_mode & S_IFMT)) {
  428. continue;
  429. }
  430. }
  431. ZVAL_STRING(&tmp, globbuf.gl_pathv[n]+cwd_skip);
  432. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
  433. }
  434. globfree(&globbuf);
  435. if (basedir_limit && !zend_hash_num_elements(Z_ARRVAL_P(return_value))) {
  436. zend_array_destroy(Z_ARR_P(return_value));
  437. RETURN_FALSE;
  438. }
  439. }
  440. /* }}} */
  441. #endif
  442. /* {{{ List files & directories inside the specified path */
  443. PHP_FUNCTION(scandir)
  444. {
  445. char *dirn;
  446. size_t dirn_len;
  447. zend_long flags = PHP_SCANDIR_SORT_ASCENDING;
  448. zend_string **namelist;
  449. int n, i;
  450. zval *zcontext = NULL;
  451. php_stream_context *context = NULL;
  452. ZEND_PARSE_PARAMETERS_START(1, 3)
  453. Z_PARAM_PATH(dirn, dirn_len)
  454. Z_PARAM_OPTIONAL
  455. Z_PARAM_LONG(flags)
  456. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  457. ZEND_PARSE_PARAMETERS_END();
  458. if (dirn_len < 1) {
  459. zend_argument_value_error(1, "cannot be empty");
  460. RETURN_THROWS();
  461. }
  462. if (zcontext) {
  463. context = php_stream_context_from_zval(zcontext, 0);
  464. }
  465. if (flags == PHP_SCANDIR_SORT_ASCENDING) {
  466. n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasort);
  467. } else if (flags == PHP_SCANDIR_SORT_NONE) {
  468. n = php_stream_scandir(dirn, &namelist, context, NULL);
  469. } else {
  470. n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
  471. }
  472. if (n < 0) {
  473. php_error_docref(NULL, E_WARNING, "(errno %d): %s", errno, strerror(errno));
  474. RETURN_FALSE;
  475. }
  476. array_init(return_value);
  477. for (i = 0; i < n; i++) {
  478. add_next_index_str(return_value, namelist[i]);
  479. }
  480. if (n) {
  481. efree(namelist);
  482. }
  483. }
  484. /* }}} */