1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Jim Winstead <jimw@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #include "php.h"
22 #include "fopen_wrappers.h"
23 #include "php_globals.h"
24
25 #include <stdlib.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <time.h>
31
32 #if HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35
36 #if HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39
40 #if HAVE_SYS_VFS_H
41 # include <sys/vfs.h>
42 #endif
43
44 #ifdef OS2
45 # define INCL_DOS
46 # include <os2.h>
47 #endif
48
49 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
50 # include <sys/statvfs.h>
51 #elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_STATFS)
52 # include <sys/statfs.h>
53 #elif defined(HAVE_SYS_MOUNT_H) && defined(HAVE_STATFS)
54 # include <sys/mount.h>
55 #endif
56
57 #if HAVE_PWD_H
58 # ifdef PHP_WIN32
59 # include "win32/pwd.h"
60 # else
61 # include <pwd.h>
62 # endif
63 #endif
64
65 #if HAVE_GRP_H
66 # ifdef PHP_WIN32
67 # include "win32/grp.h"
68 # else
69 # include <grp.h>
70 # endif
71 #endif
72
73 #if HAVE_UTIME
74 # ifdef PHP_WIN32
75 # include <sys/utime.h>
76 # else
77 # include <utime.h>
78 # endif
79 #endif
80
81 #ifdef PHP_WIN32
82 #include "win32/winutil.h"
83 #endif
84
85 #include "basic_functions.h"
86 #include "php_filestat.h"
87
PHP_RINIT_FUNCTION(filestat)88 PHP_RINIT_FUNCTION(filestat) /* {{{ */
89 {
90 BG(CurrentStatFile)=NULL;
91 BG(CurrentLStatFile)=NULL;
92 return SUCCESS;
93 }
94 /* }}} */
95
PHP_RSHUTDOWN_FUNCTION(filestat)96 PHP_RSHUTDOWN_FUNCTION(filestat) /* {{{ */
97 {
98 if (BG(CurrentStatFile)) {
99 efree (BG(CurrentStatFile));
100 BG(CurrentStatFile) = NULL;
101 }
102 if (BG(CurrentLStatFile)) {
103 efree (BG(CurrentLStatFile));
104 BG(CurrentLStatFile) = NULL;
105 }
106 return SUCCESS;
107 }
108 /* }}} */
109
php_disk_total_space(char * path,double * space)110 static int php_disk_total_space(char *path, double *space) /* {{{ */
111 #if defined(WINDOWS) /* {{{ */
112 {
113 ULARGE_INTEGER FreeBytesAvailableToCaller;
114 ULARGE_INTEGER TotalNumberOfBytes;
115 ULARGE_INTEGER TotalNumberOfFreeBytes;
116 PHP_WIN32_IOUTIL_INIT_W(path)
117
118 if (GetDiskFreeSpaceExW(pathw, &FreeBytesAvailableToCaller, &TotalNumberOfBytes, &TotalNumberOfFreeBytes) == 0) {
119 php_error_docref(NULL, E_WARNING, "%s", php_win_err());
120 PHP_WIN32_IOUTIL_CLEANUP_W()
121 return FAILURE;
122 }
123
124 /* i know - this is ugly, but i works <thies@thieso.net> */
125 *space = TotalNumberOfBytes.HighPart * (double) (((zend_ulong)1) << 31) * 2.0 + TotalNumberOfBytes.LowPart;
126
127 PHP_WIN32_IOUTIL_CLEANUP_W()
128
129 return SUCCESS;
130 }
131 /* }}} */
132 #elif defined(OS2) /* {{{ */
133 {
134 double bytestotal = 0;
135 FSALLOCATE fsinfo;
136 char drive = path[0] & 95;
137
138 if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) {
139 bytestotal = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnit;
140 *space = bytestotal;
141 return SUCCESS;
142 }
143 return FAILURE;
144 }
145 /* }}} */
146 #else /* {{{ if !defined(OS2) && !defined(WINDOWS) */
147 {
148 double bytestotal = 0;
149 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
150 struct statvfs buf;
151 #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
152 struct statfs buf;
153 #endif
154
155 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
156 if (statvfs(path, &buf)) {
157 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
158 return FAILURE;
159 }
160 if (buf.f_frsize) {
161 bytestotal = (((double)buf.f_blocks) * ((double)buf.f_frsize));
162 } else {
163 bytestotal = (((double)buf.f_blocks) * ((double)buf.f_bsize));
164 }
165
166 #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
167 if (statfs(path, &buf)) {
168 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
169 return FAILURE;
170 }
171 bytestotal = (((double)buf.f_bsize) * ((double)buf.f_blocks));
172 #endif
173
174 *space = bytestotal;
175 return SUCCESS;
176 }
177 #endif
178 /* }}} */
179 /* }}} */
180
181 /* {{{ proto float disk_total_space(string path)
182 Get total disk space for filesystem that path is on */
PHP_FUNCTION(disk_total_space)183 PHP_FUNCTION(disk_total_space)
184 {
185 double bytestotal;
186 char *path;
187 size_t path_len;
188
189 if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &path, &path_len) == FAILURE) {
190 return;
191 }
192
193 if (php_check_open_basedir(path)) {
194 RETURN_FALSE;
195 }
196
197 if (php_disk_total_space(path, &bytestotal) == SUCCESS) {
198 RETURN_DOUBLE(bytestotal);
199 }
200 RETURN_FALSE;
201 }
202 /* }}} */
203
php_disk_free_space(char * path,double * space)204 static int php_disk_free_space(char *path, double *space) /* {{{ */
205 #if defined(WINDOWS) /* {{{ */
206 {
207 ULARGE_INTEGER FreeBytesAvailableToCaller;
208 ULARGE_INTEGER TotalNumberOfBytes;
209 ULARGE_INTEGER TotalNumberOfFreeBytes;
210 PHP_WIN32_IOUTIL_INIT_W(path)
211
212 if (GetDiskFreeSpaceExW(pathw, &FreeBytesAvailableToCaller, &TotalNumberOfBytes, &TotalNumberOfFreeBytes) == 0) {
213 php_error_docref(NULL, E_WARNING, "%s", php_win_err());
214 PHP_WIN32_IOUTIL_CLEANUP_W()
215 return FAILURE;
216 }
217
218 /* i know - this is ugly, but i works <thies@thieso.net> */
219 *space = FreeBytesAvailableToCaller.HighPart * (double) (((zend_ulong)1) << 31) * 2.0 + FreeBytesAvailableToCaller.LowPart;
220
221 PHP_WIN32_IOUTIL_CLEANUP_W()
222
223 return SUCCESS;
224 }
225 /* }}} */
226 #elif defined(OS2) /* {{{ */
227 {
228 double bytesfree = 0;
229 FSALLOCATE fsinfo;
230 char drive = path[0] & 95;
231
232 if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0) {
233 bytesfree = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnitAvail;
234 *space = bytesfree;
235 return SUCCESS;
236 }
237 return FAILURE;
238 }
239 /* }}} */
240 #else /* {{{ if !defined(OS2) && !defined(WINDOWS) */
241 {
242 double bytesfree = 0;
243 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
244 struct statvfs buf;
245 #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
246 struct statfs buf;
247 #endif
248
249 #if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
250 if (statvfs(path, &buf)) {
251 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
252 return FAILURE;
253 }
254 if (buf.f_frsize) {
255 bytesfree = (((double)buf.f_bavail) * ((double)buf.f_frsize));
256 } else {
257 bytesfree = (((double)buf.f_bavail) * ((double)buf.f_bsize));
258 }
259 #elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
260 if (statfs(path, &buf)) {
261 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
262 return FAILURE;
263 }
264 #ifdef NETWARE
265 bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bfree));
266 #else
267 bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bavail));
268 #endif
269 #endif
270
271 *space = bytesfree;
272 return SUCCESS;
273 }
274 #endif
275 /* }}} */
276 /* }}} */
277
278 /* {{{ proto float disk_free_space(string path)
279 Get free disk space for filesystem that path is on */
PHP_FUNCTION(disk_free_space)280 PHP_FUNCTION(disk_free_space)
281 {
282 double bytesfree;
283 char *path;
284 size_t path_len;
285
286 if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &path, &path_len) == FAILURE) {
287 return;
288 }
289
290 if (php_check_open_basedir(path)) {
291 RETURN_FALSE;
292 }
293
294 if (php_disk_free_space(path, &bytesfree) == SUCCESS) {
295 RETURN_DOUBLE(bytesfree);
296 }
297 RETURN_FALSE;
298 }
299 /* }}} */
300
301 #if !defined(WINDOWS) && !defined(NETWARE)
php_get_gid_by_name(const char * name,gid_t * gid)302 PHPAPI int php_get_gid_by_name(const char *name, gid_t *gid)
303 {
304 #if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
305 struct group gr;
306 struct group *retgrptr;
307 long grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
308 char *grbuf;
309
310 if (grbuflen < 1) {
311 return FAILURE;
312 }
313
314 grbuf = emalloc(grbuflen);
315 if (getgrnam_r(name, &gr, grbuf, grbuflen, &retgrptr) != 0 || retgrptr == NULL) {
316 efree(grbuf);
317 return FAILURE;
318 }
319 efree(grbuf);
320 *gid = gr.gr_gid;
321 #else
322 struct group *gr = getgrnam(name);
323
324 if (!gr) {
325 return FAILURE;
326 }
327 *gid = gr->gr_gid;
328 #endif
329 return SUCCESS;
330 }
331 #endif
332
php_do_chgrp(INTERNAL_FUNCTION_PARAMETERS,int do_lchgrp)333 static void php_do_chgrp(INTERNAL_FUNCTION_PARAMETERS, int do_lchgrp) /* {{{ */
334 {
335 char *filename;
336 size_t filename_len;
337 zval *group;
338 #if !defined(WINDOWS)
339 gid_t gid;
340 int ret;
341 #endif
342 php_stream_wrapper *wrapper;
343
344 if (zend_parse_parameters(ZEND_NUM_ARGS(), "pz/", &filename, &filename_len, &group) == FAILURE) {
345 RETURN_FALSE;
346 }
347
348 wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
349 if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
350 if(wrapper && wrapper->wops->stream_metadata) {
351 int option;
352 void *value;
353 if (Z_TYPE_P(group) == IS_LONG) {
354 option = PHP_STREAM_META_GROUP;
355 value = &Z_LVAL_P(group);
356 } else if (Z_TYPE_P(group) == IS_STRING) {
357 option = PHP_STREAM_META_GROUP_NAME;
358 value = Z_STRVAL_P(group);
359 } else {
360 php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(group));
361 RETURN_FALSE;
362 }
363 if(wrapper->wops->stream_metadata(wrapper, filename, option, value, NULL)) {
364 RETURN_TRUE;
365 } else {
366 RETURN_FALSE;
367 }
368 } else {
369 #if !defined(WINDOWS)
370 /* On Windows, we expect regular chgrp to fail silently by default */
371 php_error_docref(NULL, E_WARNING, "Can not call chgrp() for a non-standard stream");
372 #endif
373 RETURN_FALSE;
374 }
375 }
376
377 #if defined(WINDOWS)
378 /* We have no native chgrp on Windows, nothing left to do if stream doesn't have own implementation */
379 RETURN_FALSE;
380 #else
381 if (Z_TYPE_P(group) == IS_LONG) {
382 gid = (gid_t)Z_LVAL_P(group);
383 } else if (Z_TYPE_P(group) == IS_STRING) {
384 if(php_get_gid_by_name(Z_STRVAL_P(group), &gid) != SUCCESS) {
385 php_error_docref(NULL, E_WARNING, "Unable to find gid for %s", Z_STRVAL_P(group));
386 RETURN_FALSE;
387 }
388 } else {
389 php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(group));
390 RETURN_FALSE;
391 }
392
393 /* Check the basedir */
394 if (php_check_open_basedir(filename)) {
395 RETURN_FALSE;
396 }
397
398 if (do_lchgrp) {
399 #if HAVE_LCHOWN
400 ret = VCWD_LCHOWN(filename, -1, gid);
401 #endif
402 } else {
403 ret = VCWD_CHOWN(filename, -1, gid);
404 }
405 if (ret == -1) {
406 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
407 RETURN_FALSE;
408 }
409 RETURN_TRUE;
410 #endif
411 }
412 /* }}} */
413
414 #ifndef NETWARE
415 /* {{{ proto bool chgrp(string filename, mixed group)
416 Change file group */
PHP_FUNCTION(chgrp)417 PHP_FUNCTION(chgrp)
418 {
419 php_do_chgrp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
420 }
421 /* }}} */
422
423 /* {{{ proto bool lchgrp(string filename, mixed group)
424 Change symlink group */
425 #if HAVE_LCHOWN
PHP_FUNCTION(lchgrp)426 PHP_FUNCTION(lchgrp)
427 {
428 # if !defined(WINDOWS)
429 php_do_chgrp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
430 # else
431 RETURN_FALSE;
432 # endif
433 }
434 #endif
435 /* }}} */
436 #endif /* !NETWARE */
437
438 #if !defined(WINDOWS) && !defined(NETWARE)
php_get_uid_by_name(const char * name,uid_t * uid)439 PHPAPI uid_t php_get_uid_by_name(const char *name, uid_t *uid)
440 {
441 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
442 struct passwd pw;
443 struct passwd *retpwptr = NULL;
444 long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
445 char *pwbuf;
446
447 if (pwbuflen < 1) {
448 return FAILURE;
449 }
450
451 pwbuf = emalloc(pwbuflen);
452 if (getpwnam_r(name, &pw, pwbuf, pwbuflen, &retpwptr) != 0 || retpwptr == NULL) {
453 efree(pwbuf);
454 return FAILURE;
455 }
456 efree(pwbuf);
457 *uid = pw.pw_uid;
458 #else
459 struct passwd *pw = getpwnam(name);
460
461 if (!pw) {
462 return FAILURE;
463 }
464 *uid = pw->pw_uid;
465 #endif
466 return SUCCESS;
467 }
468 #endif
469
php_do_chown(INTERNAL_FUNCTION_PARAMETERS,int do_lchown)470 static void php_do_chown(INTERNAL_FUNCTION_PARAMETERS, int do_lchown) /* {{{ */
471 {
472 char *filename;
473 size_t filename_len;
474 zval *user;
475 #if !defined(WINDOWS)
476 uid_t uid;
477 int ret;
478 #endif
479 php_stream_wrapper *wrapper;
480
481 if (zend_parse_parameters(ZEND_NUM_ARGS(), "pz/", &filename, &filename_len, &user) == FAILURE) {
482 return;
483 }
484
485 wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
486 if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
487 if(wrapper && wrapper->wops->stream_metadata) {
488 int option;
489 void *value;
490 if (Z_TYPE_P(user) == IS_LONG) {
491 option = PHP_STREAM_META_OWNER;
492 value = &Z_LVAL_P(user);
493 } else if (Z_TYPE_P(user) == IS_STRING) {
494 option = PHP_STREAM_META_OWNER_NAME;
495 value = Z_STRVAL_P(user);
496 } else {
497 php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(user));
498 RETURN_FALSE;
499 }
500 if(wrapper->wops->stream_metadata(wrapper, filename, option, value, NULL)) {
501 RETURN_TRUE;
502 } else {
503 RETURN_FALSE;
504 }
505 } else {
506 #if !defined(WINDOWS)
507 /* On Windows, we expect regular chown to fail silently by default */
508 php_error_docref(NULL, E_WARNING, "Can not call chown() for a non-standard stream");
509 #endif
510 RETURN_FALSE;
511 }
512 }
513
514 #if defined(WINDOWS)
515 /* We have no native chown on Windows, nothing left to do if stream doesn't have own implementation */
516 RETURN_FALSE;
517 #else
518
519 if (Z_TYPE_P(user) == IS_LONG) {
520 uid = (uid_t)Z_LVAL_P(user);
521 } else if (Z_TYPE_P(user) == IS_STRING) {
522 if(php_get_uid_by_name(Z_STRVAL_P(user), &uid) != SUCCESS) {
523 php_error_docref(NULL, E_WARNING, "Unable to find uid for %s", Z_STRVAL_P(user));
524 RETURN_FALSE;
525 }
526 } else {
527 php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(user));
528 RETURN_FALSE;
529 }
530
531 /* Check the basedir */
532 if (php_check_open_basedir(filename)) {
533 RETURN_FALSE;
534 }
535
536 if (do_lchown) {
537 #if HAVE_LCHOWN
538 ret = VCWD_LCHOWN(filename, uid, -1);
539 #endif
540 } else {
541 ret = VCWD_CHOWN(filename, uid, -1);
542 }
543 if (ret == -1) {
544 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
545 RETURN_FALSE;
546 }
547 RETURN_TRUE;
548 #endif
549 }
550 /* }}} */
551
552
553 #ifndef NETWARE
554 /* {{{ proto bool chown (string filename, mixed user)
555 Change file owner */
PHP_FUNCTION(chown)556 PHP_FUNCTION(chown)
557 {
558 php_do_chown(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
559 }
560 /* }}} */
561
562 /* {{{ proto bool chown (string filename, mixed user)
563 Change file owner */
564 #if HAVE_LCHOWN
PHP_FUNCTION(lchown)565 PHP_FUNCTION(lchown)
566 {
567 # if !defined(WINDOWS)
568 RETVAL_TRUE;
569 php_do_chown(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
570 # else
571 RETURN_FALSE;
572 # endif
573 }
574 #endif
575 /* }}} */
576 #endif /* !NETWARE */
577
578 /* {{{ proto bool chmod(string filename, int mode)
579 Change file mode */
PHP_FUNCTION(chmod)580 PHP_FUNCTION(chmod)
581 {
582 char *filename;
583 size_t filename_len;
584 zend_long mode;
585 int ret;
586 mode_t imode;
587 php_stream_wrapper *wrapper;
588
589 if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl", &filename, &filename_len, &mode) == FAILURE) {
590 return;
591 }
592
593 wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
594 if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
595 if(wrapper && wrapper->wops->stream_metadata) {
596 if(wrapper->wops->stream_metadata(wrapper, filename, PHP_STREAM_META_ACCESS, &mode, NULL)) {
597 RETURN_TRUE;
598 } else {
599 RETURN_FALSE;
600 }
601 } else {
602 php_error_docref(NULL, E_WARNING, "Can not call chmod() for a non-standard stream");
603 RETURN_FALSE;
604 }
605 }
606
607 /* Check the basedir */
608 if (php_check_open_basedir(filename)) {
609 RETURN_FALSE;
610 }
611
612 imode = (mode_t) mode;
613
614 ret = VCWD_CHMOD(filename, imode);
615 if (ret == -1) {
616 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
617 RETURN_FALSE;
618 }
619 RETURN_TRUE;
620 }
621 /* }}} */
622
623 #if HAVE_UTIME
624 /* {{{ proto bool touch(string filename [, int time [, int atime]])
625 Set modification time of file */
PHP_FUNCTION(touch)626 PHP_FUNCTION(touch)
627 {
628 char *filename;
629 size_t filename_len;
630 zend_long filetime = 0, fileatime = 0;
631 int ret, argc = ZEND_NUM_ARGS();
632 FILE *file;
633 struct utimbuf newtimebuf;
634 struct utimbuf *newtime = &newtimebuf;
635 php_stream_wrapper *wrapper;
636
637 if (zend_parse_parameters(argc, "p|ll", &filename, &filename_len, &filetime, &fileatime) == FAILURE) {
638 return;
639 }
640
641 if (!filename_len) {
642 RETURN_FALSE;
643 }
644
645 switch (argc) {
646 case 1:
647 #ifdef HAVE_UTIME_NULL
648 newtime = NULL;
649 #else
650 newtime->modtime = newtime->actime = time(NULL);
651 #endif
652 break;
653 case 2:
654 newtime->modtime = newtime->actime = filetime;
655 break;
656 case 3:
657 newtime->modtime = filetime;
658 newtime->actime = fileatime;
659 break;
660 default:
661 /* Never reached */
662 WRONG_PARAM_COUNT;
663 }
664
665 wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
666 if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
667 if(wrapper && wrapper->wops->stream_metadata) {
668 if(wrapper->wops->stream_metadata(wrapper, filename, PHP_STREAM_META_TOUCH, newtime, NULL)) {
669 RETURN_TRUE;
670 } else {
671 RETURN_FALSE;
672 }
673 } else {
674 php_stream *stream;
675 if(argc > 1) {
676 php_error_docref(NULL, E_WARNING, "Can not call touch() for a non-standard stream");
677 RETURN_FALSE;
678 }
679 stream = php_stream_open_wrapper_ex(filename, "c", REPORT_ERRORS, NULL, NULL);
680 if(stream != NULL) {
681 php_stream_close(stream);
682 RETURN_TRUE;
683 } else {
684 RETURN_FALSE;
685 }
686 }
687 }
688
689 /* Check the basedir */
690 if (php_check_open_basedir(filename)) {
691 RETURN_FALSE;
692 }
693
694 /* create the file if it doesn't exist already */
695 if (VCWD_ACCESS(filename, F_OK) != 0) {
696 file = VCWD_FOPEN(filename, "w");
697 if (file == NULL) {
698 php_error_docref(NULL, E_WARNING, "Unable to create file %s because %s", filename, strerror(errno));
699 RETURN_FALSE;
700 }
701 fclose(file);
702 }
703
704 ret = VCWD_UTIME(filename, newtime);
705 if (ret == -1) {
706 php_error_docref(NULL, E_WARNING, "Utime failed: %s", strerror(errno));
707 RETURN_FALSE;
708 }
709 RETURN_TRUE;
710 }
711 /* }}} */
712 #endif
713
714 /* {{{ php_clear_stat_cache()
715 */
php_clear_stat_cache(zend_bool clear_realpath_cache,const char * filename,int filename_len)716 PHPAPI void php_clear_stat_cache(zend_bool clear_realpath_cache, const char *filename, int filename_len)
717 {
718 /* always clear CurrentStatFile and CurrentLStatFile even if filename is not NULL
719 * as it may contain outdated data (e.g. "nlink" for a directory when deleting a file
720 * in this directory, as shown by lstat_stat_variation9.phpt) */
721 if (BG(CurrentStatFile)) {
722 efree(BG(CurrentStatFile));
723 BG(CurrentStatFile) = NULL;
724 }
725 if (BG(CurrentLStatFile)) {
726 efree(BG(CurrentLStatFile));
727 BG(CurrentLStatFile) = NULL;
728 }
729 if (clear_realpath_cache) {
730 if (filename != NULL) {
731 realpath_cache_del(filename, filename_len);
732 } else {
733 realpath_cache_clean();
734 }
735 }
736 }
737 /* }}} */
738
739 /* {{{ proto void clearstatcache([bool clear_realpath_cache[, string filename]])
740 Clear file stat cache */
PHP_FUNCTION(clearstatcache)741 PHP_FUNCTION(clearstatcache)
742 {
743 zend_bool clear_realpath_cache = 0;
744 char *filename = NULL;
745 size_t filename_len = 0;
746
747 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|bp", &clear_realpath_cache, &filename, &filename_len) == FAILURE) {
748 return;
749 }
750
751 php_clear_stat_cache(clear_realpath_cache, filename, (int)filename_len);
752 }
753 /* }}} */
754
755 #define IS_LINK_OPERATION(__t) ((__t) == FS_TYPE || (__t) == FS_IS_LINK || (__t) == FS_LSTAT)
756 #define IS_EXISTS_CHECK(__t) ((__t) == FS_EXISTS || (__t) == FS_IS_W || (__t) == FS_IS_R || (__t) == FS_IS_X || (__t) == FS_IS_FILE || (__t) == FS_IS_DIR || (__t) == FS_IS_LINK)
757 #define IS_ABLE_CHECK(__t) ((__t) == FS_IS_R || (__t) == FS_IS_W || (__t) == FS_IS_X)
758 #define IS_ACCESS_CHECK(__t) (IS_ABLE_CHECK(type) || (__t) == FS_EXISTS)
759
760 /* {{{ php_stat
761 */
php_stat(const char * filename,php_stat_len filename_length,int type,zval * return_value)762 PHPAPI void php_stat(const char *filename, php_stat_len filename_length, int type, zval *return_value)
763 {
764 zval stat_dev, stat_ino, stat_mode, stat_nlink, stat_uid, stat_gid, stat_rdev,
765 stat_size, stat_atime, stat_mtime, stat_ctime, stat_blksize, stat_blocks;
766 zend_stat_t *stat_sb;
767 php_stream_statbuf ssb;
768 int flags = 0, rmask=S_IROTH, wmask=S_IWOTH, xmask=S_IXOTH; /* access rights defaults to other */
769 char *stat_sb_names[13] = {
770 "dev", "ino", "mode", "nlink", "uid", "gid", "rdev",
771 "size", "atime", "mtime", "ctime", "blksize", "blocks"
772 };
773 const char *local;
774 php_stream_wrapper *wrapper;
775
776 if (!filename_length) {
777 RETURN_FALSE;
778 }
779
780 if ((wrapper = php_stream_locate_url_wrapper(filename, &local, 0)) == &php_plain_files_wrapper && php_check_open_basedir(local)) {
781 RETURN_FALSE;
782 }
783
784 if (IS_ACCESS_CHECK(type)) {
785 if (wrapper == &php_plain_files_wrapper) {
786
787 switch (type) {
788 #ifdef F_OK
789 case FS_EXISTS:
790 RETURN_BOOL(VCWD_ACCESS(local, F_OK) == 0);
791 break;
792 #endif
793 #ifdef W_OK
794 case FS_IS_W:
795 RETURN_BOOL(VCWD_ACCESS(local, W_OK) == 0);
796 break;
797 #endif
798 #ifdef R_OK
799 case FS_IS_R:
800 RETURN_BOOL(VCWD_ACCESS(local, R_OK) == 0);
801 break;
802 #endif
803 #ifdef X_OK
804 case FS_IS_X:
805 RETURN_BOOL(VCWD_ACCESS(local, X_OK) == 0);
806 break;
807 #endif
808 }
809 }
810 }
811
812 if (IS_LINK_OPERATION(type)) {
813 flags |= PHP_STREAM_URL_STAT_LINK;
814 }
815 if (IS_EXISTS_CHECK(type)) {
816 flags |= PHP_STREAM_URL_STAT_QUIET;
817 }
818
819 if (php_stream_stat_path_ex((char *)filename, flags, &ssb, NULL)) {
820 /* Error Occurred */
821 if (!IS_EXISTS_CHECK(type)) {
822 php_error_docref(NULL, E_WARNING, "%sstat failed for %s", IS_LINK_OPERATION(type) ? "L" : "", filename);
823 }
824 RETURN_FALSE;
825 }
826
827 stat_sb = &ssb.sb;
828
829
830 #ifndef NETWARE
831 if (type >= FS_IS_W && type <= FS_IS_X) {
832 if(ssb.sb.st_uid==getuid()) {
833 rmask=S_IRUSR;
834 wmask=S_IWUSR;
835 xmask=S_IXUSR;
836 } else if(ssb.sb.st_gid==getgid()) {
837 rmask=S_IRGRP;
838 wmask=S_IWGRP;
839 xmask=S_IXGRP;
840 } else {
841 int groups, n, i;
842 gid_t *gids;
843
844 groups = getgroups(0, NULL);
845 if(groups > 0) {
846 gids=(gid_t *)safe_emalloc(groups, sizeof(gid_t), 0);
847 n=getgroups(groups, gids);
848 for(i=0;i<n;i++){
849 if(ssb.sb.st_gid==gids[i]) {
850 rmask=S_IRGRP;
851 wmask=S_IWGRP;
852 xmask=S_IXGRP;
853 break;
854 }
855 }
856 efree(gids);
857 }
858 }
859 }
860 #endif
861
862 #ifndef NETWARE
863 if (IS_ABLE_CHECK(type) && getuid() == 0) {
864 /* root has special perms on plain_wrapper
865 But we don't know about root under Netware */
866 if (wrapper == &php_plain_files_wrapper) {
867 if (type == FS_IS_X) {
868 xmask = S_IXROOT;
869 } else {
870 RETURN_TRUE;
871 }
872 }
873 }
874 #endif
875
876 switch (type) {
877 case FS_PERMS:
878 RETURN_LONG((zend_long)ssb.sb.st_mode);
879 case FS_INODE:
880 RETURN_LONG((zend_long)ssb.sb.st_ino);
881 case FS_SIZE:
882 RETURN_LONG((zend_long)ssb.sb.st_size);
883 case FS_OWNER:
884 RETURN_LONG((zend_long)ssb.sb.st_uid);
885 case FS_GROUP:
886 RETURN_LONG((zend_long)ssb.sb.st_gid);
887 case FS_ATIME:
888 RETURN_LONG((zend_long)ssb.sb.st_atime);
889 case FS_MTIME:
890 RETURN_LONG((zend_long)ssb.sb.st_mtime);
891 case FS_CTIME:
892 RETURN_LONG((zend_long)ssb.sb.st_ctime);
893 case FS_TYPE:
894 if (S_ISLNK(ssb.sb.st_mode)) {
895 RETURN_STRING("link");
896 }
897 switch(ssb.sb.st_mode & S_IFMT) {
898 case S_IFIFO: RETURN_STRING("fifo");
899 case S_IFCHR: RETURN_STRING("char");
900 case S_IFDIR: RETURN_STRING("dir");
901 case S_IFBLK: RETURN_STRING("block");
902 case S_IFREG: RETURN_STRING("file");
903 #if defined(S_IFSOCK) && !defined(ZEND_WIN32)&&!defined(__BEOS__)
904 case S_IFSOCK: RETURN_STRING("socket");
905 #endif
906 }
907 php_error_docref(NULL, E_NOTICE, "Unknown file type (%d)", ssb.sb.st_mode&S_IFMT);
908 RETURN_STRING("unknown");
909 case FS_IS_W:
910 RETURN_BOOL((ssb.sb.st_mode & wmask) != 0);
911 case FS_IS_R:
912 RETURN_BOOL((ssb.sb.st_mode&rmask)!=0);
913 case FS_IS_X:
914 RETURN_BOOL((ssb.sb.st_mode&xmask)!=0 && !S_ISDIR(ssb.sb.st_mode));
915 case FS_IS_FILE:
916 RETURN_BOOL(S_ISREG(ssb.sb.st_mode));
917 case FS_IS_DIR:
918 RETURN_BOOL(S_ISDIR(ssb.sb.st_mode));
919 case FS_IS_LINK:
920 RETURN_BOOL(S_ISLNK(ssb.sb.st_mode));
921 case FS_EXISTS:
922 RETURN_TRUE; /* the false case was done earlier */
923 case FS_LSTAT:
924 /* FALLTHROUGH */
925 case FS_STAT:
926 array_init(return_value);
927
928 ZVAL_LONG(&stat_dev, stat_sb->st_dev);
929 ZVAL_LONG(&stat_ino, stat_sb->st_ino);
930 ZVAL_LONG(&stat_mode, stat_sb->st_mode);
931 ZVAL_LONG(&stat_nlink, stat_sb->st_nlink);
932 ZVAL_LONG(&stat_uid, stat_sb->st_uid);
933 ZVAL_LONG(&stat_gid, stat_sb->st_gid);
934 #ifdef HAVE_ST_RDEV
935 # ifdef PHP_WIN32
936 /* It is unsigned, so if a negative came from userspace, it'll
937 convert to UINT_MAX, but we wan't to keep the userspace value.
938 Almost the same as in php_if_fstat. */
939 if ((int)stat_sb->st_rdev < 0) {
940 ZVAL_LONG(&stat_rdev, (int)stat_sb->st_rdev);
941 } else {
942 ZVAL_LONG(&stat_rdev, stat_sb->st_rdev);
943 }
944 # else
945 ZVAL_LONG(&stat_rdev, stat_sb->st_rdev);
946 # endif
947 #else
948 ZVAL_LONG(&stat_rdev, -1);
949 #endif
950 ZVAL_LONG(&stat_size, stat_sb->st_size);
951 ZVAL_LONG(&stat_atime, stat_sb->st_atime);
952 ZVAL_LONG(&stat_mtime, stat_sb->st_mtime);
953 ZVAL_LONG(&stat_ctime, stat_sb->st_ctime);
954 #ifdef HAVE_ST_BLKSIZE
955 ZVAL_LONG(&stat_blksize, stat_sb->st_blksize);
956 #else
957 ZVAL_LONG(&stat_blksize,-1);
958 #endif
959 #ifdef HAVE_ST_BLOCKS
960 ZVAL_LONG(&stat_blocks, stat_sb->st_blocks);
961 #else
962 ZVAL_LONG(&stat_blocks,-1);
963 #endif
964 /* Store numeric indexes in proper order */
965 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_dev);
966 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ino);
967 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mode);
968 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_nlink);
969 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_uid);
970 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_gid);
971
972 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_rdev);
973 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_size);
974 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_atime);
975 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mtime);
976 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ctime);
977 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blksize);
978 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blocks);
979
980 /* Store string indexes referencing the same zval*/
981 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[0], strlen(stat_sb_names[0]), &stat_dev);
982 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[1], strlen(stat_sb_names[1]), &stat_ino);
983 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[2], strlen(stat_sb_names[2]), &stat_mode);
984 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[3], strlen(stat_sb_names[3]), &stat_nlink);
985 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[4], strlen(stat_sb_names[4]), &stat_uid);
986 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[5], strlen(stat_sb_names[5]), &stat_gid);
987 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[6], strlen(stat_sb_names[6]), &stat_rdev);
988 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[7], strlen(stat_sb_names[7]), &stat_size);
989 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[8], strlen(stat_sb_names[8]), &stat_atime);
990 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[9], strlen(stat_sb_names[9]), &stat_mtime);
991 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[10], strlen(stat_sb_names[10]), &stat_ctime);
992 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[11], strlen(stat_sb_names[11]), &stat_blksize);
993 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[12], strlen(stat_sb_names[12]), &stat_blocks);
994
995 return;
996 }
997 php_error_docref(NULL, E_WARNING, "Didn't understand stat call");
998 RETURN_FALSE;
999 }
1000 /* }}} */
1001
1002 /* another quickie macro to make defining similar functions easier */
1003 /* {{{ FileFunction(name, funcnum) */
1004 #define FileFunction(name, funcnum) \
1005 void name(INTERNAL_FUNCTION_PARAMETERS) { \
1006 char *filename; \
1007 size_t filename_len; \
1008 \
1009 ZEND_PARSE_PARAMETERS_START(1, 1) \
1010 Z_PARAM_PATH(filename, filename_len) \
1011 ZEND_PARSE_PARAMETERS_END(); \
1012 \
1013 php_stat(filename, (php_stat_len) filename_len, funcnum, return_value); \
1014 }
1015 /* }}} */
1016
1017 /* {{{ proto int fileperms(string filename)
1018 Get file permissions */
FileFunction(PHP_FN (fileperms),FS_PERMS)1019 FileFunction(PHP_FN(fileperms), FS_PERMS)
1020 /* }}} */
1021
1022 /* {{{ proto int fileinode(string filename)
1023 Get file inode */
1024 FileFunction(PHP_FN(fileinode), FS_INODE)
1025 /* }}} */
1026
1027 /* {{{ proto int filesize(string filename)
1028 Get file size */
1029 FileFunction(PHP_FN(filesize), FS_SIZE)
1030 /* }}} */
1031
1032 /* {{{ proto int fileowner(string filename)
1033 Get file owner */
1034 FileFunction(PHP_FN(fileowner), FS_OWNER)
1035 /* }}} */
1036
1037 /* {{{ proto int filegroup(string filename)
1038 Get file group */
1039 FileFunction(PHP_FN(filegroup), FS_GROUP)
1040 /* }}} */
1041
1042 /* {{{ proto int fileatime(string filename)
1043 Get last access time of file */
1044 FileFunction(PHP_FN(fileatime), FS_ATIME)
1045 /* }}} */
1046
1047 /* {{{ proto int filemtime(string filename)
1048 Get last modification time of file */
1049 FileFunction(PHP_FN(filemtime), FS_MTIME)
1050 /* }}} */
1051
1052 /* {{{ proto int filectime(string filename)
1053 Get inode modification time of file */
1054 FileFunction(PHP_FN(filectime), FS_CTIME)
1055 /* }}} */
1056
1057 /* {{{ proto string filetype(string filename)
1058 Get file type */
1059 FileFunction(PHP_FN(filetype), FS_TYPE)
1060 /* }}} */
1061
1062 /* {{{ proto bool is_writable(string filename)
1063 Returns true if file can be written */
1064 FileFunction(PHP_FN(is_writable), FS_IS_W)
1065 /* }}} */
1066
1067 /* {{{ proto bool is_readable(string filename)
1068 Returns true if file can be read */
1069 FileFunction(PHP_FN(is_readable), FS_IS_R)
1070 /* }}} */
1071
1072 /* {{{ proto bool is_executable(string filename)
1073 Returns true if file is executable */
1074 FileFunction(PHP_FN(is_executable), FS_IS_X)
1075 /* }}} */
1076
1077 /* {{{ proto bool is_file(string filename)
1078 Returns true if file is a regular file */
1079 FileFunction(PHP_FN(is_file), FS_IS_FILE)
1080 /* }}} */
1081
1082 /* {{{ proto bool is_dir(string filename)
1083 Returns true if file is directory */
1084 FileFunction(PHP_FN(is_dir), FS_IS_DIR)
1085 /* }}} */
1086
1087 /* {{{ proto bool is_link(string filename)
1088 Returns true if file is symbolic link */
1089 FileFunction(PHP_FN(is_link), FS_IS_LINK)
1090 /* }}} */
1091
1092 /* {{{ proto bool file_exists(string filename)
1093 Returns true if filename exists */
1094 FileFunction(PHP_FN(file_exists), FS_EXISTS)
1095 /* }}} */
1096
1097 /* {{{ proto array lstat(string filename)
1098 Give information about a file or symbolic link */
1099 FileFunction(php_if_lstat, FS_LSTAT)
1100 /* }}} */
1101
1102 /* {{{ proto array stat(string filename)
1103 Give information about a file */
1104 FileFunction(php_if_stat, FS_STAT)
1105 /* }}} */
1106
1107 /* {{{ proto bool realpath_cache_size()
1108 Get current size of realpath cache */
1109 PHP_FUNCTION(realpath_cache_size)
1110 {
1111 if (zend_parse_parameters_none() == FAILURE) {
1112 return;
1113 }
1114 RETURN_LONG(realpath_cache_size());
1115 }
1116
1117 /* {{{ proto bool realpath_cache_get()
1118 Get current size of realpath cache */
PHP_FUNCTION(realpath_cache_get)1119 PHP_FUNCTION(realpath_cache_get)
1120 {
1121 realpath_cache_bucket **buckets = realpath_cache_get_buckets(), **end = buckets + realpath_cache_max_buckets();
1122
1123 if (zend_parse_parameters_none() == FAILURE) {
1124 return;
1125 }
1126
1127 array_init(return_value);
1128 while(buckets < end) {
1129 realpath_cache_bucket *bucket = *buckets;
1130 while(bucket) {
1131 zval entry;
1132
1133 array_init(&entry);
1134
1135 /* bucket->key is unsigned long */
1136 if (ZEND_LONG_MAX >= bucket->key) {
1137 add_assoc_long_ex(&entry, "key", sizeof("key") - 1, bucket->key);
1138 } else {
1139 add_assoc_double_ex(&entry, "key", sizeof("key") - 1, (double)bucket->key);
1140 }
1141 add_assoc_bool_ex(&entry, "is_dir", sizeof("is_dir") - 1, bucket->is_dir);
1142 add_assoc_stringl_ex(&entry, "realpath", sizeof("realpath") - 1, bucket->realpath, bucket->realpath_len);
1143 add_assoc_long_ex(&entry, "expires", sizeof("expires") - 1, bucket->expires);
1144 #ifdef PHP_WIN32
1145 add_assoc_bool_ex(&entry, "is_rvalid", sizeof("is_rvalid") - 1, bucket->is_rvalid);
1146 add_assoc_bool_ex(&entry, "is_wvalid", sizeof("is_wvalid") - 1, bucket->is_wvalid);
1147 add_assoc_bool_ex(&entry, "is_readable", sizeof("is_readable") - 1, bucket->is_readable);
1148 add_assoc_bool_ex(&entry, "is_writable", sizeof("is_writable") - 1, bucket->is_writable);
1149 #endif
1150 zend_hash_str_update(Z_ARRVAL_P(return_value), bucket->path, bucket->path_len, &entry);
1151 bucket = bucket->next;
1152 }
1153 buckets++;
1154 }
1155 }
1156
1157 /*
1158 * Local variables:
1159 * tab-width: 4
1160 * c-basic-offset: 4
1161 * End:
1162 * vim600: sw=4 ts=4 fdm=marker
1163 * vim<600: sw=4 ts=4
1164 */
1165