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