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 ZEND_PARSE_PARAMETERS_START(1, 1)
190 Z_PARAM_PATH(path, path_len)
191 ZEND_PARSE_PARAMETERS_END();
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 bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bavail));
265 #endif
266
267 *space = bytesfree;
268 return SUCCESS;
269 }
270 #endif
271 /* }}} */
272 /* }}} */
273
274 /* {{{ proto float disk_free_space(string path)
275 Get free disk space for filesystem that path is on */
PHP_FUNCTION(disk_free_space)276 PHP_FUNCTION(disk_free_space)
277 {
278 double bytesfree;
279 char *path;
280 size_t path_len;
281
282 ZEND_PARSE_PARAMETERS_START(1, 1)
283 Z_PARAM_PATH(path, path_len)
284 ZEND_PARSE_PARAMETERS_END();
285
286 if (php_check_open_basedir(path)) {
287 RETURN_FALSE;
288 }
289
290 if (php_disk_free_space(path, &bytesfree) == SUCCESS) {
291 RETURN_DOUBLE(bytesfree);
292 }
293 RETURN_FALSE;
294 }
295 /* }}} */
296
297 #ifndef PHP_WIN32
php_get_gid_by_name(const char * name,gid_t * gid)298 PHPAPI int php_get_gid_by_name(const char *name, gid_t *gid)
299 {
300 #if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
301 struct group gr;
302 struct group *retgrptr;
303 long grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
304 char *grbuf;
305
306 if (grbuflen < 1) {
307 return FAILURE;
308 }
309
310 grbuf = emalloc(grbuflen);
311 if (getgrnam_r(name, &gr, grbuf, grbuflen, &retgrptr) != 0 || retgrptr == NULL) {
312 efree(grbuf);
313 return FAILURE;
314 }
315 efree(grbuf);
316 *gid = gr.gr_gid;
317 #else
318 struct group *gr = getgrnam(name);
319
320 if (!gr) {
321 return FAILURE;
322 }
323 *gid = gr->gr_gid;
324 #endif
325 return SUCCESS;
326 }
327 #endif
328
php_do_chgrp(INTERNAL_FUNCTION_PARAMETERS,int do_lchgrp)329 static void php_do_chgrp(INTERNAL_FUNCTION_PARAMETERS, int do_lchgrp) /* {{{ */
330 {
331 char *filename;
332 size_t filename_len;
333 zval *group;
334 #if !defined(WINDOWS)
335 gid_t gid;
336 int ret;
337 #endif
338 php_stream_wrapper *wrapper;
339
340 ZEND_PARSE_PARAMETERS_START(2, 2)
341 Z_PARAM_PATH(filename, filename_len)
342 Z_PARAM_ZVAL(group)
343 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
344
345 wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
346 if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
347 if(wrapper && wrapper->wops->stream_metadata) {
348 int option;
349 void *value;
350 if (Z_TYPE_P(group) == IS_LONG) {
351 option = PHP_STREAM_META_GROUP;
352 value = &Z_LVAL_P(group);
353 } else if (Z_TYPE_P(group) == IS_STRING) {
354 option = PHP_STREAM_META_GROUP_NAME;
355 value = Z_STRVAL_P(group);
356 } else {
357 php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(group));
358 RETURN_FALSE;
359 }
360 if(wrapper->wops->stream_metadata(wrapper, filename, option, value, NULL)) {
361 RETURN_TRUE;
362 } else {
363 RETURN_FALSE;
364 }
365 } else {
366 #if !defined(WINDOWS)
367 /* On Windows, we expect regular chgrp to fail silently by default */
368 php_error_docref(NULL, E_WARNING, "Can not call chgrp() for a non-standard stream");
369 #endif
370 RETURN_FALSE;
371 }
372 }
373
374 #if defined(WINDOWS)
375 /* We have no native chgrp on Windows, nothing left to do if stream doesn't have own implementation */
376 RETURN_FALSE;
377 #else
378 if (Z_TYPE_P(group) == IS_LONG) {
379 gid = (gid_t)Z_LVAL_P(group);
380 } else if (Z_TYPE_P(group) == IS_STRING) {
381 if(php_get_gid_by_name(Z_STRVAL_P(group), &gid) != SUCCESS) {
382 php_error_docref(NULL, E_WARNING, "Unable to find gid for %s", Z_STRVAL_P(group));
383 RETURN_FALSE;
384 }
385 } else {
386 php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(group));
387 RETURN_FALSE;
388 }
389
390 /* Check the basedir */
391 if (php_check_open_basedir(filename)) {
392 RETURN_FALSE;
393 }
394
395 if (do_lchgrp) {
396 #if HAVE_LCHOWN
397 ret = VCWD_LCHOWN(filename, -1, gid);
398 #endif
399 } else {
400 ret = VCWD_CHOWN(filename, -1, gid);
401 }
402 if (ret == -1) {
403 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
404 RETURN_FALSE;
405 }
406 RETURN_TRUE;
407 #endif
408 }
409 /* }}} */
410
411 /* {{{ proto bool chgrp(string filename, mixed group)
412 Change file group */
PHP_FUNCTION(chgrp)413 PHP_FUNCTION(chgrp)
414 {
415 php_do_chgrp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
416 }
417 /* }}} */
418
419 /* {{{ proto bool lchgrp(string filename, mixed group)
420 Change symlink group */
421 #if HAVE_LCHOWN
PHP_FUNCTION(lchgrp)422 PHP_FUNCTION(lchgrp)
423 {
424 # if !defined(WINDOWS)
425 php_do_chgrp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
426 # else
427 RETURN_FALSE;
428 # endif
429 }
430 #endif
431 /* }}} */
432
433 #ifndef PHP_WIN32
php_get_uid_by_name(const char * name,uid_t * uid)434 PHPAPI uid_t php_get_uid_by_name(const char *name, uid_t *uid)
435 {
436 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
437 struct passwd pw;
438 struct passwd *retpwptr = NULL;
439 long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
440 char *pwbuf;
441
442 if (pwbuflen < 1) {
443 return FAILURE;
444 }
445
446 pwbuf = emalloc(pwbuflen);
447 if (getpwnam_r(name, &pw, pwbuf, pwbuflen, &retpwptr) != 0 || retpwptr == NULL) {
448 efree(pwbuf);
449 return FAILURE;
450 }
451 efree(pwbuf);
452 *uid = pw.pw_uid;
453 #else
454 struct passwd *pw = getpwnam(name);
455
456 if (!pw) {
457 return FAILURE;
458 }
459 *uid = pw->pw_uid;
460 #endif
461 return SUCCESS;
462 }
463 #endif
464
php_do_chown(INTERNAL_FUNCTION_PARAMETERS,int do_lchown)465 static void php_do_chown(INTERNAL_FUNCTION_PARAMETERS, int do_lchown) /* {{{ */
466 {
467 char *filename;
468 size_t filename_len;
469 zval *user;
470 #if !defined(WINDOWS)
471 uid_t uid;
472 int ret;
473 #endif
474 php_stream_wrapper *wrapper;
475
476 ZEND_PARSE_PARAMETERS_START(2, 2)
477 Z_PARAM_PATH(filename, filename_len)
478 Z_PARAM_ZVAL(user)
479 ZEND_PARSE_PARAMETERS_END();
480
481 wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
482 if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
483 if(wrapper && wrapper->wops->stream_metadata) {
484 int option;
485 void *value;
486 if (Z_TYPE_P(user) == IS_LONG) {
487 option = PHP_STREAM_META_OWNER;
488 value = &Z_LVAL_P(user);
489 } else if (Z_TYPE_P(user) == IS_STRING) {
490 option = PHP_STREAM_META_OWNER_NAME;
491 value = Z_STRVAL_P(user);
492 } else {
493 php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(user));
494 RETURN_FALSE;
495 }
496 if(wrapper->wops->stream_metadata(wrapper, filename, option, value, NULL)) {
497 RETURN_TRUE;
498 } else {
499 RETURN_FALSE;
500 }
501 } else {
502 #if !defined(WINDOWS)
503 /* On Windows, we expect regular chown to fail silently by default */
504 php_error_docref(NULL, E_WARNING, "Can not call chown() for a non-standard stream");
505 #endif
506 RETURN_FALSE;
507 }
508 }
509
510 #if defined(WINDOWS)
511 /* We have no native chown on Windows, nothing left to do if stream doesn't have own implementation */
512 RETURN_FALSE;
513 #else
514
515 if (Z_TYPE_P(user) == IS_LONG) {
516 uid = (uid_t)Z_LVAL_P(user);
517 } else if (Z_TYPE_P(user) == IS_STRING) {
518 if(php_get_uid_by_name(Z_STRVAL_P(user), &uid) != SUCCESS) {
519 php_error_docref(NULL, E_WARNING, "Unable to find uid for %s", Z_STRVAL_P(user));
520 RETURN_FALSE;
521 }
522 } else {
523 php_error_docref(NULL, E_WARNING, "parameter 2 should be string or integer, %s given", zend_zval_type_name(user));
524 RETURN_FALSE;
525 }
526
527 /* Check the basedir */
528 if (php_check_open_basedir(filename)) {
529 RETURN_FALSE;
530 }
531
532 if (do_lchown) {
533 #if HAVE_LCHOWN
534 ret = VCWD_LCHOWN(filename, uid, -1);
535 #endif
536 } else {
537 ret = VCWD_CHOWN(filename, uid, -1);
538 }
539 if (ret == -1) {
540 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
541 RETURN_FALSE;
542 }
543 RETURN_TRUE;
544 #endif
545 }
546 /* }}} */
547
548
549 /* {{{ proto bool chown (string filename, mixed user)
550 Change file owner */
PHP_FUNCTION(chown)551 PHP_FUNCTION(chown)
552 {
553 php_do_chown(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
554 }
555 /* }}} */
556
557 /* {{{ proto bool chown (string filename, mixed user)
558 Change file owner */
559 #if HAVE_LCHOWN
PHP_FUNCTION(lchown)560 PHP_FUNCTION(lchown)
561 {
562 # if !defined(WINDOWS)
563 RETVAL_TRUE;
564 php_do_chown(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
565 # else
566 RETURN_FALSE;
567 # endif
568 }
569 #endif
570 /* }}} */
571
572 /* {{{ proto bool chmod(string filename, int mode)
573 Change file mode */
PHP_FUNCTION(chmod)574 PHP_FUNCTION(chmod)
575 {
576 char *filename;
577 size_t filename_len;
578 zend_long mode;
579 int ret;
580 mode_t imode;
581 php_stream_wrapper *wrapper;
582
583 ZEND_PARSE_PARAMETERS_START(2, 2)
584 Z_PARAM_PATH(filename, filename_len)
585 Z_PARAM_LONG(mode)
586 ZEND_PARSE_PARAMETERS_END();
587
588 wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
589 if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
590 if(wrapper && wrapper->wops->stream_metadata) {
591 if(wrapper->wops->stream_metadata(wrapper, filename, PHP_STREAM_META_ACCESS, &mode, NULL)) {
592 RETURN_TRUE;
593 } else {
594 RETURN_FALSE;
595 }
596 } else {
597 php_error_docref(NULL, E_WARNING, "Can not call chmod() for a non-standard stream");
598 RETURN_FALSE;
599 }
600 }
601
602 /* Check the basedir */
603 if (php_check_open_basedir(filename)) {
604 RETURN_FALSE;
605 }
606
607 imode = (mode_t) mode;
608
609 ret = VCWD_CHMOD(filename, imode);
610 if (ret == -1) {
611 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
612 RETURN_FALSE;
613 }
614 RETURN_TRUE;
615 }
616 /* }}} */
617
618 #if HAVE_UTIME
619 /* {{{ proto bool touch(string filename [, int time [, int atime]])
620 Set modification time of file */
PHP_FUNCTION(touch)621 PHP_FUNCTION(touch)
622 {
623 char *filename;
624 size_t filename_len;
625 zend_long filetime = 0, fileatime = 0;
626 int ret, argc = ZEND_NUM_ARGS();
627 FILE *file;
628 struct utimbuf newtimebuf;
629 struct utimbuf *newtime = &newtimebuf;
630 php_stream_wrapper *wrapper;
631
632 ZEND_PARSE_PARAMETERS_START(1, 3)
633 Z_PARAM_PATH(filename, filename_len)
634 Z_PARAM_OPTIONAL
635 Z_PARAM_LONG(filetime)
636 Z_PARAM_LONG(fileatime)
637 ZEND_PARSE_PARAMETERS_END();
638
639 if (!filename_len) {
640 RETURN_FALSE;
641 }
642
643 switch (argc) {
644 case 1:
645 #ifdef HAVE_UTIME_NULL
646 newtime = NULL;
647 #else
648 newtime->modtime = newtime->actime = time(NULL);
649 #endif
650 break;
651 case 2:
652 newtime->modtime = newtime->actime = filetime;
653 break;
654 case 3:
655 newtime->modtime = filetime;
656 newtime->actime = fileatime;
657 break;
658 default:
659 /* Never reached */
660 WRONG_PARAM_COUNT;
661 }
662
663 wrapper = php_stream_locate_url_wrapper(filename, NULL, 0);
664 if(wrapper != &php_plain_files_wrapper || strncasecmp("file://", filename, 7) == 0) {
665 if(wrapper && wrapper->wops->stream_metadata) {
666 if(wrapper->wops->stream_metadata(wrapper, filename, PHP_STREAM_META_TOUCH, newtime, NULL)) {
667 RETURN_TRUE;
668 } else {
669 RETURN_FALSE;
670 }
671 } else {
672 php_stream *stream;
673 if(argc > 1) {
674 php_error_docref(NULL, E_WARNING, "Can not call touch() for a non-standard stream");
675 RETURN_FALSE;
676 }
677 stream = php_stream_open_wrapper_ex(filename, "c", REPORT_ERRORS, NULL, NULL);
678 if(stream != NULL) {
679 php_stream_close(stream);
680 RETURN_TRUE;
681 } else {
682 RETURN_FALSE;
683 }
684 }
685 }
686
687 /* Check the basedir */
688 if (php_check_open_basedir(filename)) {
689 RETURN_FALSE;
690 }
691
692 /* create the file if it doesn't exist already */
693 if (VCWD_ACCESS(filename, F_OK) != 0) {
694 file = VCWD_FOPEN(filename, "w");
695 if (file == NULL) {
696 php_error_docref(NULL, E_WARNING, "Unable to create file %s because %s", filename, strerror(errno));
697 RETURN_FALSE;
698 }
699 fclose(file);
700 }
701
702 ret = VCWD_UTIME(filename, newtime);
703 if (ret == -1) {
704 php_error_docref(NULL, E_WARNING, "Utime failed: %s", strerror(errno));
705 RETURN_FALSE;
706 }
707 RETURN_TRUE;
708 }
709 /* }}} */
710 #endif
711
712 /* {{{ php_clear_stat_cache()
713 */
php_clear_stat_cache(zend_bool clear_realpath_cache,const char * filename,size_t filename_len)714 PHPAPI void php_clear_stat_cache(zend_bool clear_realpath_cache, const char *filename, size_t filename_len)
715 {
716 /* always clear CurrentStatFile and CurrentLStatFile even if filename is not NULL
717 * as it may contain outdated data (e.g. "nlink" for a directory when deleting a file
718 * in this directory, as shown by lstat_stat_variation9.phpt) */
719 if (BG(CurrentStatFile)) {
720 efree(BG(CurrentStatFile));
721 BG(CurrentStatFile) = NULL;
722 }
723 if (BG(CurrentLStatFile)) {
724 efree(BG(CurrentLStatFile));
725 BG(CurrentLStatFile) = NULL;
726 }
727 if (clear_realpath_cache) {
728 if (filename != NULL) {
729 realpath_cache_del(filename, filename_len);
730 } else {
731 realpath_cache_clean();
732 }
733 }
734 }
735 /* }}} */
736
737 /* {{{ proto void clearstatcache([bool clear_realpath_cache[, string filename]])
738 Clear file stat cache */
PHP_FUNCTION(clearstatcache)739 PHP_FUNCTION(clearstatcache)
740 {
741 zend_bool clear_realpath_cache = 0;
742 char *filename = NULL;
743 size_t filename_len = 0;
744
745 ZEND_PARSE_PARAMETERS_START(0, 2)
746 Z_PARAM_OPTIONAL
747 Z_PARAM_BOOL(clear_realpath_cache)
748 Z_PARAM_PATH(filename, filename_len)
749 ZEND_PARSE_PARAMETERS_END();
750
751 php_clear_stat_cache(clear_realpath_cache, filename, 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,size_t filename_length,int type,zval * return_value)762 PHPAPI void php_stat(const char *filename, size_t 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 if (type >= FS_IS_W && type <= FS_IS_X) {
830 if(ssb.sb.st_uid==getuid()) {
831 rmask=S_IRUSR;
832 wmask=S_IWUSR;
833 xmask=S_IXUSR;
834 } else if(ssb.sb.st_gid==getgid()) {
835 rmask=S_IRGRP;
836 wmask=S_IWGRP;
837 xmask=S_IXGRP;
838 } else {
839 int groups, n, i;
840 gid_t *gids;
841
842 groups = getgroups(0, NULL);
843 if(groups > 0) {
844 gids=(gid_t *)safe_emalloc(groups, sizeof(gid_t), 0);
845 n=getgroups(groups, gids);
846 for(i=0;i<n;i++){
847 if(ssb.sb.st_gid==gids[i]) {
848 rmask=S_IRGRP;
849 wmask=S_IWGRP;
850 xmask=S_IXGRP;
851 break;
852 }
853 }
854 efree(gids);
855 }
856 }
857 }
858
859 if (IS_ABLE_CHECK(type) && getuid() == 0) {
860 /* root has special perms on plain_wrapper */
861 if (wrapper == &php_plain_files_wrapper) {
862 if (type == FS_IS_X) {
863 xmask = S_IXROOT;
864 } else {
865 RETURN_TRUE;
866 }
867 }
868 }
869
870 switch (type) {
871 case FS_PERMS:
872 RETURN_LONG((zend_long)ssb.sb.st_mode);
873 case FS_INODE:
874 RETURN_LONG((zend_long)ssb.sb.st_ino);
875 case FS_SIZE:
876 RETURN_LONG((zend_long)ssb.sb.st_size);
877 case FS_OWNER:
878 RETURN_LONG((zend_long)ssb.sb.st_uid);
879 case FS_GROUP:
880 RETURN_LONG((zend_long)ssb.sb.st_gid);
881 case FS_ATIME:
882 RETURN_LONG((zend_long)ssb.sb.st_atime);
883 case FS_MTIME:
884 RETURN_LONG((zend_long)ssb.sb.st_mtime);
885 case FS_CTIME:
886 RETURN_LONG((zend_long)ssb.sb.st_ctime);
887 case FS_TYPE:
888 if (S_ISLNK(ssb.sb.st_mode)) {
889 RETURN_STRING("link");
890 }
891 switch(ssb.sb.st_mode & S_IFMT) {
892 case S_IFIFO: RETURN_STRING("fifo");
893 case S_IFCHR: RETURN_STRING("char");
894 case S_IFDIR: RETURN_STRING("dir");
895 case S_IFBLK: RETURN_STRING("block");
896 case S_IFREG: RETURN_STRING("file");
897 #if defined(S_IFSOCK) && !defined(ZEND_WIN32)&&!defined(__BEOS__)
898 case S_IFSOCK: RETURN_STRING("socket");
899 #endif
900 }
901 php_error_docref(NULL, E_NOTICE, "Unknown file type (%d)", ssb.sb.st_mode&S_IFMT);
902 RETURN_STRING("unknown");
903 case FS_IS_W:
904 RETURN_BOOL((ssb.sb.st_mode & wmask) != 0);
905 case FS_IS_R:
906 RETURN_BOOL((ssb.sb.st_mode&rmask)!=0);
907 case FS_IS_X:
908 RETURN_BOOL((ssb.sb.st_mode&xmask)!=0 && !S_ISDIR(ssb.sb.st_mode));
909 case FS_IS_FILE:
910 RETURN_BOOL(S_ISREG(ssb.sb.st_mode));
911 case FS_IS_DIR:
912 RETURN_BOOL(S_ISDIR(ssb.sb.st_mode));
913 case FS_IS_LINK:
914 RETURN_BOOL(S_ISLNK(ssb.sb.st_mode));
915 case FS_EXISTS:
916 RETURN_TRUE; /* the false case was done earlier */
917 case FS_LSTAT:
918 /* FALLTHROUGH */
919 case FS_STAT:
920 array_init(return_value);
921
922 ZVAL_LONG(&stat_dev, stat_sb->st_dev);
923 ZVAL_LONG(&stat_ino, stat_sb->st_ino);
924 ZVAL_LONG(&stat_mode, stat_sb->st_mode);
925 ZVAL_LONG(&stat_nlink, stat_sb->st_nlink);
926 ZVAL_LONG(&stat_uid, stat_sb->st_uid);
927 ZVAL_LONG(&stat_gid, stat_sb->st_gid);
928 #ifdef HAVE_ST_RDEV
929 # ifdef PHP_WIN32
930 /* It is unsigned, so if a negative came from userspace, it'll
931 convert to UINT_MAX, but we wan't to keep the userspace value.
932 Almost the same as in php_if_fstat. */
933 if ((int)stat_sb->st_rdev < 0) {
934 ZVAL_LONG(&stat_rdev, (int)stat_sb->st_rdev);
935 } else {
936 ZVAL_LONG(&stat_rdev, stat_sb->st_rdev);
937 }
938 # else
939 ZVAL_LONG(&stat_rdev, stat_sb->st_rdev);
940 # endif
941 #else
942 ZVAL_LONG(&stat_rdev, -1);
943 #endif
944 ZVAL_LONG(&stat_size, stat_sb->st_size);
945 ZVAL_LONG(&stat_atime, stat_sb->st_atime);
946 ZVAL_LONG(&stat_mtime, stat_sb->st_mtime);
947 ZVAL_LONG(&stat_ctime, stat_sb->st_ctime);
948 #ifdef HAVE_ST_BLKSIZE
949 ZVAL_LONG(&stat_blksize, stat_sb->st_blksize);
950 #else
951 ZVAL_LONG(&stat_blksize,-1);
952 #endif
953 #ifdef HAVE_ST_BLOCKS
954 ZVAL_LONG(&stat_blocks, stat_sb->st_blocks);
955 #else
956 ZVAL_LONG(&stat_blocks,-1);
957 #endif
958 /* Store numeric indexes in proper order */
959 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_dev);
960 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ino);
961 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mode);
962 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_nlink);
963 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_uid);
964 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_gid);
965
966 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_rdev);
967 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_size);
968 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_atime);
969 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_mtime);
970 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_ctime);
971 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blksize);
972 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &stat_blocks);
973
974 /* Store string indexes referencing the same zval*/
975 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[0], strlen(stat_sb_names[0]), &stat_dev);
976 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[1], strlen(stat_sb_names[1]), &stat_ino);
977 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[2], strlen(stat_sb_names[2]), &stat_mode);
978 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[3], strlen(stat_sb_names[3]), &stat_nlink);
979 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[4], strlen(stat_sb_names[4]), &stat_uid);
980 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[5], strlen(stat_sb_names[5]), &stat_gid);
981 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[6], strlen(stat_sb_names[6]), &stat_rdev);
982 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[7], strlen(stat_sb_names[7]), &stat_size);
983 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[8], strlen(stat_sb_names[8]), &stat_atime);
984 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[9], strlen(stat_sb_names[9]), &stat_mtime);
985 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[10], strlen(stat_sb_names[10]), &stat_ctime);
986 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[11], strlen(stat_sb_names[11]), &stat_blksize);
987 zend_hash_str_update(Z_ARRVAL_P(return_value), stat_sb_names[12], strlen(stat_sb_names[12]), &stat_blocks);
988
989 return;
990 }
991 php_error_docref(NULL, E_WARNING, "Didn't understand stat call");
992 RETURN_FALSE;
993 }
994 /* }}} */
995
996 /* another quickie macro to make defining similar functions easier */
997 /* {{{ FileFunction(name, funcnum) */
998 #define FileFunction(name, funcnum) \
999 ZEND_NAMED_FUNCTION(name) { \
1000 char *filename; \
1001 size_t filename_len; \
1002 \
1003 ZEND_PARSE_PARAMETERS_START(1, 1) \
1004 Z_PARAM_PATH(filename, filename_len) \
1005 ZEND_PARSE_PARAMETERS_END(); \
1006 \
1007 php_stat(filename, filename_len, funcnum, return_value); \
1008 }
1009 /* }}} */
1010
1011 /* {{{ proto int fileperms(string filename)
1012 Get file permissions */
FileFunction(PHP_FN (fileperms),FS_PERMS)1013 FileFunction(PHP_FN(fileperms), FS_PERMS)
1014 /* }}} */
1015
1016 /* {{{ proto int fileinode(string filename)
1017 Get file inode */
1018 FileFunction(PHP_FN(fileinode), FS_INODE)
1019 /* }}} */
1020
1021 /* {{{ proto int filesize(string filename)
1022 Get file size */
1023 FileFunction(PHP_FN(filesize), FS_SIZE)
1024 /* }}} */
1025
1026 /* {{{ proto int fileowner(string filename)
1027 Get file owner */
1028 FileFunction(PHP_FN(fileowner), FS_OWNER)
1029 /* }}} */
1030
1031 /* {{{ proto int filegroup(string filename)
1032 Get file group */
1033 FileFunction(PHP_FN(filegroup), FS_GROUP)
1034 /* }}} */
1035
1036 /* {{{ proto int fileatime(string filename)
1037 Get last access time of file */
1038 FileFunction(PHP_FN(fileatime), FS_ATIME)
1039 /* }}} */
1040
1041 /* {{{ proto int filemtime(string filename)
1042 Get last modification time of file */
1043 FileFunction(PHP_FN(filemtime), FS_MTIME)
1044 /* }}} */
1045
1046 /* {{{ proto int filectime(string filename)
1047 Get inode modification time of file */
1048 FileFunction(PHP_FN(filectime), FS_CTIME)
1049 /* }}} */
1050
1051 /* {{{ proto string filetype(string filename)
1052 Get file type */
1053 FileFunction(PHP_FN(filetype), FS_TYPE)
1054 /* }}} */
1055
1056 /* {{{ proto bool is_writable(string filename)
1057 Returns true if file can be written */
1058 FileFunction(PHP_FN(is_writable), FS_IS_W)
1059 /* }}} */
1060
1061 /* {{{ proto bool is_readable(string filename)
1062 Returns true if file can be read */
1063 FileFunction(PHP_FN(is_readable), FS_IS_R)
1064 /* }}} */
1065
1066 /* {{{ proto bool is_executable(string filename)
1067 Returns true if file is executable */
1068 FileFunction(PHP_FN(is_executable), FS_IS_X)
1069 /* }}} */
1070
1071 /* {{{ proto bool is_file(string filename)
1072 Returns true if file is a regular file */
1073 FileFunction(PHP_FN(is_file), FS_IS_FILE)
1074 /* }}} */
1075
1076 /* {{{ proto bool is_dir(string filename)
1077 Returns true if file is directory */
1078 FileFunction(PHP_FN(is_dir), FS_IS_DIR)
1079 /* }}} */
1080
1081 /* {{{ proto bool is_link(string filename)
1082 Returns true if file is symbolic link */
1083 FileFunction(PHP_FN(is_link), FS_IS_LINK)
1084 /* }}} */
1085
1086 /* {{{ proto bool file_exists(string filename)
1087 Returns true if filename exists */
1088 FileFunction(PHP_FN(file_exists), FS_EXISTS)
1089 /* }}} */
1090
1091 /* {{{ proto array lstat(string filename)
1092 Give information about a file or symbolic link */
1093 FileFunction(php_if_lstat, FS_LSTAT)
1094 /* }}} */
1095
1096 /* {{{ proto array stat(string filename)
1097 Give information about a file */
1098 FileFunction(php_if_stat, FS_STAT)
1099 /* }}} */
1100
1101 /* {{{ proto bool realpath_cache_size()
1102 Get current size of realpath cache */
1103 PHP_FUNCTION(realpath_cache_size)
1104 {
1105 if (zend_parse_parameters_none() == FAILURE) {
1106 return;
1107 }
1108 RETURN_LONG(realpath_cache_size());
1109 }
1110
1111 /* {{{ proto bool realpath_cache_get()
1112 Get current size of realpath cache */
PHP_FUNCTION(realpath_cache_get)1113 PHP_FUNCTION(realpath_cache_get)
1114 {
1115 realpath_cache_bucket **buckets = realpath_cache_get_buckets(), **end = buckets + realpath_cache_max_buckets();
1116
1117 if (zend_parse_parameters_none() == FAILURE) {
1118 return;
1119 }
1120
1121 array_init(return_value);
1122 while(buckets < end) {
1123 realpath_cache_bucket *bucket = *buckets;
1124 while(bucket) {
1125 zval entry;
1126
1127 array_init(&entry);
1128
1129 /* bucket->key is unsigned long */
1130 if (ZEND_LONG_MAX >= bucket->key) {
1131 add_assoc_long_ex(&entry, "key", sizeof("key") - 1, bucket->key);
1132 } else {
1133 add_assoc_double_ex(&entry, "key", sizeof("key") - 1, (double)bucket->key);
1134 }
1135 add_assoc_bool_ex(&entry, "is_dir", sizeof("is_dir") - 1, bucket->is_dir);
1136 add_assoc_stringl_ex(&entry, "realpath", sizeof("realpath") - 1, bucket->realpath, bucket->realpath_len);
1137 add_assoc_long_ex(&entry, "expires", sizeof("expires") - 1, bucket->expires);
1138 #ifdef PHP_WIN32
1139 add_assoc_bool_ex(&entry, "is_rvalid", sizeof("is_rvalid") - 1, bucket->is_rvalid);
1140 add_assoc_bool_ex(&entry, "is_wvalid", sizeof("is_wvalid") - 1, bucket->is_wvalid);
1141 add_assoc_bool_ex(&entry, "is_readable", sizeof("is_readable") - 1, bucket->is_readable);
1142 add_assoc_bool_ex(&entry, "is_writable", sizeof("is_writable") - 1, bucket->is_writable);
1143 #endif
1144 zend_hash_str_update(Z_ARRVAL_P(return_value), bucket->path, bucket->path_len, &entry);
1145 bucket = bucket->next;
1146 }
1147 buckets++;
1148 }
1149 }
1150
1151 /*
1152 * Local variables:
1153 * tab-width: 4
1154 * c-basic-offset: 4
1155 * End:
1156 * vim600: sw=4 ts=4 fdm=marker
1157 * vim<600: sw=4 ts=4
1158 */
1159