1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2005 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Jim Winstead <jimw@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php.h"
#include "safe_mode.h"
#include "fopen_wrappers.h"
#include "php_globals.h"
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#if HAVE_SYS_VFS_H
# include <sys/vfs.h>
#endif
#ifdef OS2
# define INCL_DOS
# include <os2.h>
#endif
#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
# include <sys/statvfs.h>
#elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_STATFS)
# include <sys/statfs.h>
#elif defined(HAVE_SYS_MOUNT_H) && defined(HAVE_STATFS)
# include <sys/mount.h>
#endif
#if HAVE_PWD_H
# ifdef PHP_WIN32
# include "win32/pwd.h"
# else
# include <pwd.h>
# endif
#endif
#if HAVE_GRP_H
# ifdef PHP_WIN32
# include "win32/grp.h"
# else
# include <grp.h>
# endif
#endif
#if HAVE_UTIME
# ifdef PHP_WIN32
# include <sys/utime.h>
# else
# include <utime.h>
# endif
#endif
#include "basic_functions.h"
#include "php_filestat.h"
#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
#endif
#ifndef S_ISREG
#define S_ISREG(mode) (((mode)&S_IFMT) == S_IFREG)
#endif
#ifndef S_ISLNK
#define S_ISLNK(mode) (((mode)&S_IFMT) == S_IFLNK)
#endif
#define S_IXROOT ( S_IXUSR | S_IXGRP | S_IXOTH )
PHP_RINIT_FUNCTION(filestat)
{
BG(CurrentStatFile)=NULL;
BG(CurrentLStatFile)=NULL;
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(filestat)
{
if (BG(CurrentStatFile)) {
efree (BG(CurrentStatFile));
BG(CurrentStatFile) = NULL;
}
if (BG(CurrentLStatFile)) {
efree (BG(CurrentLStatFile));
BG(CurrentLStatFile) = NULL;
}
return SUCCESS;
}
/* {{{ proto float disk_total_space(string path)
Get total disk space for filesystem that path is on */
PHP_FUNCTION(disk_total_space)
{
zval **path;
#ifdef WINDOWS
double bytestotal;
HINSTANCE kernel32;
FARPROC gdfse;
typedef BOOL (WINAPI *gdfse_func)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
gdfse_func func;
/* These are used by GetDiskFreeSpaceEx, if available. */
ULARGE_INTEGER FreeBytesAvailableToCaller;
ULARGE_INTEGER TotalNumberOfBytes;
ULARGE_INTEGER TotalNumberOfFreeBytes;
/* These are used by GetDiskFreeSpace otherwise. */
DWORD SectorsPerCluster;
DWORD BytesPerSector;
DWORD NumberOfFreeClusters;
DWORD TotalNumberOfClusters;
#else /* not - WINDOWS */
#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
struct statvfs buf;
#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
struct statfs buf;
#endif
double bytestotal = 0;
#endif /* WINDOWS */
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &path)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(path);
if (php_check_open_basedir(Z_STRVAL_PP(path) TSRMLS_CC)) {
RETURN_FALSE;
}
#ifdef WINDOWS
/* GetDiskFreeSpaceEx is only available in NT and Win95 post-OSR2,
so we have to jump through some hoops to see if the function
exists. */
kernel32 = LoadLibrary("kernel32.dll");
if (kernel32) {
gdfse = GetProcAddress(kernel32, "GetDiskFreeSpaceExA");
/* It's available, so we can call it. */
if (gdfse) {
func = (gdfse_func)gdfse;
if (func(Z_STRVAL_PP(path),
&FreeBytesAvailableToCaller,
&TotalNumberOfBytes,
&TotalNumberOfFreeBytes) == 0) RETURN_FALSE;
/* i know - this is ugly, but i works <thies@thieso.net> */
bytestotal = TotalNumberOfBytes.HighPart *
(double) (((unsigned long)1) << 31) * 2.0 +
TotalNumberOfBytes.LowPart;
}
/* If it's not available, we just use GetDiskFreeSpace */
else {
if (GetDiskFreeSpace(Z_STRVAL_PP(path),
&SectorsPerCluster, &BytesPerSector,
&NumberOfFreeClusters, &TotalNumberOfClusters) == 0) RETURN_FALSE;
bytestotal = (double)TotalNumberOfClusters * (double)SectorsPerCluster * (double)BytesPerSector;
}
}
else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load kernel32.dll");
RETURN_FALSE;
}
#elif defined(OS2)
{
FSALLOCATE fsinfo;
char drive = Z_STRVAL_PP(path)[0] & 95;
if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0)
bytestotal = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnit;
}
#else /* WINDOWS, OS/2 */
#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
if (statvfs(Z_STRVAL_PP(path), &buf)) RETURN_FALSE;
if (buf.f_frsize) {
bytestotal = (((double)buf.f_blocks) * ((double)buf.f_frsize));
} else {
bytestotal = (((double)buf.f_blocks) * ((double)buf.f_bsize));
}
#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
if (statfs(Z_STRVAL_PP(path), &buf)) RETURN_FALSE;
bytestotal = (((double)buf.f_bsize) * ((double)buf.f_blocks));
#endif
#endif /* WINDOWS */
RETURN_DOUBLE(bytestotal);
}
/* }}} */
/* {{{ proto float disk_free_space(string path)
Get free disk space for filesystem that path is on */
PHP_FUNCTION(disk_free_space)
{
zval **path;
#ifdef WINDOWS
double bytesfree;
HINSTANCE kernel32;
FARPROC gdfse;
typedef BOOL (WINAPI *gdfse_func)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
gdfse_func func;
/* These are used by GetDiskFreeSpaceEx, if available. */
ULARGE_INTEGER FreeBytesAvailableToCaller;
ULARGE_INTEGER TotalNumberOfBytes;
ULARGE_INTEGER TotalNumberOfFreeBytes;
/* These are used by GetDiskFreeSpace otherwise. */
DWORD SectorsPerCluster;
DWORD BytesPerSector;
DWORD NumberOfFreeClusters;
DWORD TotalNumberOfClusters;
#else /* not - WINDOWS */
#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
struct statvfs buf;
#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
struct statfs buf;
#endif
double bytesfree = 0;
#endif /* WINDOWS */
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &path)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(path);
if (php_check_open_basedir(Z_STRVAL_PP(path) TSRMLS_CC)) {
RETURN_FALSE;
}
#ifdef WINDOWS
/* GetDiskFreeSpaceEx is only available in NT and Win95 post-OSR2,
so we have to jump through some hoops to see if the function
exists. */
kernel32 = LoadLibrary("kernel32.dll");
if (kernel32) {
gdfse = GetProcAddress(kernel32, "GetDiskFreeSpaceExA");
/* It's available, so we can call it. */
if (gdfse) {
func = (gdfse_func)gdfse;
if (func(Z_STRVAL_PP(path),
&FreeBytesAvailableToCaller,
&TotalNumberOfBytes,
&TotalNumberOfFreeBytes) == 0) RETURN_FALSE;
/* i know - this is ugly, but i works <thies@thieso.net> */
bytesfree = FreeBytesAvailableToCaller.HighPart *
(double) (((unsigned long)1) << 31) * 2.0 +
FreeBytesAvailableToCaller.LowPart;
}
/* If it's not available, we just use GetDiskFreeSpace */
else {
if (GetDiskFreeSpace(Z_STRVAL_PP(path),
&SectorsPerCluster, &BytesPerSector,
&NumberOfFreeClusters, &TotalNumberOfClusters) == 0) RETURN_FALSE;
bytesfree = (double)NumberOfFreeClusters * (double)SectorsPerCluster * (double)BytesPerSector;
}
}
else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load kernel32.dll");
RETURN_FALSE;
}
#elif defined(OS2)
{
FSALLOCATE fsinfo;
char drive = Z_STRVAL_PP(path)[0] & 95;
if (DosQueryFSInfo( drive ? drive - 64 : 0, FSIL_ALLOC, &fsinfo, sizeof( fsinfo ) ) == 0)
bytesfree = (double)fsinfo.cbSector * fsinfo.cSectorUnit * fsinfo.cUnitAvail;
}
#else /* WINDOWS, OS/2 */
#if defined(HAVE_SYS_STATVFS_H) && defined(HAVE_STATVFS)
if (statvfs(Z_STRVAL_PP(path), &buf)) RETURN_FALSE;
if (buf.f_frsize) {
bytesfree = (((double)buf.f_bavail) * ((double)buf.f_frsize));
} else {
bytesfree = (((double)buf.f_bavail) * ((double)buf.f_bsize));
}
#elif (defined(HAVE_SYS_STATFS_H) || defined(HAVE_SYS_MOUNT_H)) && defined(HAVE_STATFS)
if (statfs(Z_STRVAL_PP(path), &buf)) RETURN_FALSE;
#ifdef NETWARE
bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bfree));
#else
bytesfree = (((double)buf.f_bsize) * ((double)buf.f_bavail));
#endif
#endif
#endif /* WINDOWS */
RETURN_DOUBLE(bytesfree);
}
/* }}} */
/* {{{ proto bool chgrp(string filename, mixed group)
Change file group */
#ifndef NETWARE
PHP_FUNCTION(chgrp)
{
#if !defined(WINDOWS)
zval **filename, **group;
gid_t gid;
struct group *gr=NULL;
int ret;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &filename, &group)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(filename);
if (Z_TYPE_PP(group) == IS_STRING) {
gr = getgrnam(Z_STRVAL_PP(group));
if (!gr) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find gid for %s",
Z_STRVAL_PP(group));
RETURN_FALSE;
}
gid = gr->gr_gid;
} else {
convert_to_long_ex(group);
gid = Z_LVAL_PP(group);
}
if (PG(safe_mode) &&(!php_checkuid(Z_STRVAL_PP(filename), NULL, CHECKUID_ALLOW_FILE_NOT_EXISTS))) {
RETURN_FALSE;
}
/* Check the basedir */
if (php_check_open_basedir(Z_STRVAL_PP(filename) TSRMLS_CC)) {
RETURN_FALSE;
}
ret = VCWD_CHOWN(Z_STRVAL_PP(filename), -1, gid);
if (ret == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
RETURN_TRUE;
#else
RETURN_FALSE;
#endif
}
#endif
/* }}} */
/* {{{ proto bool chown (string filename, mixed user)
Change file owner */
#ifndef NETWARE
PHP_FUNCTION(chown)
{
#if !defined(WINDOWS)
zval **filename, **user;
int ret;
uid_t uid;
struct passwd *pw = NULL;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &filename, &user)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(filename);
if (Z_TYPE_PP(user) == IS_STRING) {
pw = getpwnam(Z_STRVAL_PP(user));
if (!pw) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find uid for %s",
Z_STRVAL_PP(user));
RETURN_FALSE;
}
uid = pw->pw_uid;
} else {
convert_to_long_ex(user);
uid = Z_LVAL_PP(user);
}
if (PG(safe_mode) &&(!php_checkuid(Z_STRVAL_PP(filename), NULL, CHECKUID_ALLOW_FILE_NOT_EXISTS))) {
RETURN_FALSE;
}
/* Check the basedir */
if (php_check_open_basedir(Z_STRVAL_PP(filename) TSRMLS_CC)) {
RETURN_FALSE;
}
ret = VCWD_CHOWN(Z_STRVAL_PP(filename), uid, -1);
if (ret == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
#endif
RETURN_TRUE;
}
#endif
/* }}} */
/* {{{ proto bool chmod(string filename, int mode)
Change file mode */
PHP_FUNCTION(chmod)
{
zval **filename, **mode;
int ret;
mode_t imode;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &filename, &mode)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(filename);
convert_to_long_ex(mode);
if (PG(safe_mode) &&(!php_checkuid(Z_STRVAL_PP(filename), NULL, CHECKUID_ALLOW_FILE_NOT_EXISTS))) {
RETURN_FALSE;
}
/* Check the basedir */
if (php_check_open_basedir(Z_STRVAL_PP(filename) TSRMLS_CC)) {
RETURN_FALSE;
}
imode = (mode_t) Z_LVAL_PP(mode);
/* in safe mode, do not allow to setuid files.
Setuiding files could allow users to gain privileges
that safe mode doesn't give them.
*/
if(PG(safe_mode))
imode &= 0777;
ret = VCWD_CHMOD(Z_STRVAL_PP(filename), imode);
if (ret == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#if HAVE_UTIME
/* {{{ proto bool touch(string filename [, int time [, int atime]])
Set modification time of file */
PHP_FUNCTION(touch)
{
zval **filename, **filetime, **fileatime;
int ret;
struct stat sb;
FILE *file;
struct utimbuf newtimebuf;
struct utimbuf *newtime = NULL;
int ac = ZEND_NUM_ARGS();
if (ac == 1 && zend_get_parameters_ex(1, &filename) != FAILURE) {
#ifndef HAVE_UTIME_NULL
newtime = &newtimebuf;
newtime->modtime = newtime->actime = time(NULL);
#endif
} else if (ac == 2 && zend_get_parameters_ex(2, &filename, &filetime) != FAILURE) {
convert_to_long_ex(filetime);
newtime = &newtimebuf;
newtime->modtime = newtime->actime = Z_LVAL_PP(filetime);
} else if (ac == 3 && zend_get_parameters_ex(3, &filename, &filetime, &fileatime) != FAILURE) {
convert_to_long_ex(fileatime);
convert_to_long_ex(filetime);
newtime = &newtimebuf;
newtime->actime = Z_LVAL_PP(fileatime);
newtime->modtime = Z_LVAL_PP(filetime);
} else {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(filename);
if (PG(safe_mode) &&(!php_checkuid(Z_STRVAL_PP(filename), NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
RETURN_FALSE;
}
/* Check the basedir */
if (php_check_open_basedir(Z_STRVAL_PP(filename) TSRMLS_CC)) {
RETURN_FALSE;
}
/* create the file if it doesn't exist already */
ret = VCWD_STAT(Z_STRVAL_PP(filename), &sb);
if (ret == -1) {
file = VCWD_FOPEN(Z_STRVAL_PP(filename), "w");
if (file == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create file %s because %s", Z_STRVAL_PP(filename), strerror(errno));
RETURN_FALSE;
}
fclose(file);
}
ret = VCWD_UTIME(Z_STRVAL_PP(filename), newtime);
if (ret == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Utime failed: %s", strerror(errno));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#endif
/* {{{ proto void clearstatcache(void)
Clear file stat cache */
PHP_FUNCTION(clearstatcache)
{
if (BG(CurrentStatFile)) {
efree(BG(CurrentStatFile));
BG(CurrentStatFile) = NULL;
}
if (BG(CurrentLStatFile)) {
efree(BG(CurrentLStatFile));
BG(CurrentLStatFile) = NULL;
}
}
/* }}} */
#define IS_LINK_OPERATION(__t) ((__t) == FS_TYPE || (__t) == FS_IS_LINK || (__t) == FS_LSTAT)
#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)
#define IS_ABLE_CHECK(__t) ((__t) == FS_IS_R || (__t) == FS_IS_W || (__t) == FS_IS_X)
#define IS_ACCESS_CHECK(__t) (IS_ABLE_CHECK(type) || (__t) == FS_EXISTS)
/* {{{ php_stat
*/
PHPAPI void php_stat(const char *filename, php_stat_len filename_length, int type, zval *return_value TSRMLS_DC)
{
zval *stat_dev, *stat_ino, *stat_mode, *stat_nlink, *stat_uid, *stat_gid, *stat_rdev,
*stat_size, *stat_atime, *stat_mtime, *stat_ctime, *stat_blksize, *stat_blocks;
struct stat *stat_sb;
php_stream_statbuf ssb;
int flags = 0, rmask=S_IROTH, wmask=S_IWOTH, xmask=S_IXOTH; /* access rights defaults to other */
char *stat_sb_names[13]={"dev", "ino", "mode", "nlink", "uid", "gid", "rdev",
"size", "atime", "mtime", "ctime", "blksize", "blocks"};
if (!filename_length) {
RETURN_FALSE;
}
if (IS_ACCESS_CHECK(type)) {
char *local;
if (php_stream_locate_url_wrapper(filename, &local, 0 TSRMLS_CC) == &php_plain_files_wrapper) {
switch (type) {
#ifdef F_OK
case FS_EXISTS:
RETURN_BOOL(VCWD_ACCESS(local, F_OK) == 0);
break;
#endif
#ifdef W_OK
case FS_IS_W:
RETURN_BOOL(VCWD_ACCESS(local, W_OK) == 0);
break;
#endif
#ifdef R_OK
case FS_IS_R:
RETURN_BOOL(VCWD_ACCESS(local, R_OK) == 0);
break;
#endif
#ifdef X_OK
case FS_IS_X:
RETURN_BOOL(VCWD_ACCESS(local, X_OK) == 0);
break;
#endif
}
}
}
if (IS_LINK_OPERATION(type)) {
flags |= PHP_STREAM_URL_STAT_LINK;
}
if (IS_EXISTS_CHECK(type)) {
flags |= PHP_STREAM_URL_STAT_QUIET;
}
if (php_stream_stat_path_ex((char *)filename, flags, &ssb, NULL)) {
/* Error Occured */
if (!IS_EXISTS_CHECK(type)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%sstat failed for %s", IS_LINK_OPERATION(type) ? "L" : "", filename);
}
RETURN_FALSE;
}
stat_sb = &ssb.sb;
#ifndef NETWARE
if (type >= FS_IS_W && type <= FS_IS_X) {
if(ssb.sb.st_uid==getuid()) {
rmask=S_IRUSR;
wmask=S_IWUSR;
xmask=S_IXUSR;
} else if(ssb.sb.st_gid==getgid()) {
rmask=S_IRGRP;
wmask=S_IWGRP;
xmask=S_IXGRP;
} else {
int groups, n, i;
gid_t *gids;
groups = getgroups(0, NULL);
if(groups) {
gids=(gid_t *)safe_emalloc(groups, sizeof(gid_t), 0);
n=getgroups(groups, gids);
for(i=0;i<n;i++){
if(ssb.sb.st_gid==gids[i]) {
rmask=S_IRGRP;
wmask=S_IWGRP;
xmask=S_IXGRP;
break;
}
}
efree(gids);
}
}
}
#endif
#ifndef NETWARE
if (IS_ABLE_CHECK(type) && getuid() == 0) {
/* root has special perms on plain_wrapper
But we don't know about root under Netware */
php_stream_wrapper *wrapper;
wrapper = php_stream_locate_url_wrapper(filename, NULL, 0 TSRMLS_CC);
if (wrapper == &php_plain_files_wrapper) {
if (type == FS_IS_X) {
xmask = S_IXROOT;
} else {
RETURN_TRUE;
}
}
}
#endif
switch (type) {
case FS_PERMS:
RETURN_LONG((long)ssb.sb.st_mode);
case FS_INODE:
RETURN_LONG((long)ssb.sb.st_ino);
case FS_SIZE:
RETURN_LONG((long)ssb.sb.st_size);
case FS_OWNER:
RETURN_LONG((long)ssb.sb.st_uid);
case FS_GROUP:
RETURN_LONG((long)ssb.sb.st_gid);
case FS_ATIME:
#ifdef NETWARE
RETURN_LONG((long)ssb.sb.st_atime.tv_sec);
#else
RETURN_LONG((long)ssb.sb.st_atime);
#endif
case FS_MTIME:
#ifdef NETWARE
RETURN_LONG((long)ssb.sb.st_mtime.tv_sec);
#else
RETURN_LONG((long)ssb.sb.st_mtime);
#endif
case FS_CTIME:
#ifdef NETWARE
RETURN_LONG((long)ssb.sb.st_ctime.tv_sec);
#else
RETURN_LONG((long)ssb.sb.st_ctime);
#endif
case FS_TYPE:
if (S_ISLNK(ssb.sb.st_mode)) {
RETURN_ASCII_STRING("link", 1);
}
switch(ssb.sb.st_mode & S_IFMT) {
case S_IFIFO: RETURN_ASCII_STRING("fifo", 1);
case S_IFCHR: RETURN_ASCII_STRING("char", 1);
case S_IFDIR: RETURN_ASCII_STRING("dir", 1);
case S_IFBLK: RETURN_ASCII_STRING("block", 1);
case S_IFREG: RETURN_ASCII_STRING("file", 1);
#if defined(S_IFSOCK) && !defined(ZEND_WIN32)&&!defined(__BEOS__)
case S_IFSOCK: RETURN_ASCII_STRING("socket", 1);
#endif
}
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown file type (%d)", ssb.sb.st_mode&S_IFMT);
RETURN_ASCII_STRING("unknown", 1);
case FS_IS_W:
RETURN_BOOL((ssb.sb.st_mode & wmask) != 0);
case FS_IS_R:
RETURN_BOOL((ssb.sb.st_mode&rmask)!=0);
case FS_IS_X:
RETURN_BOOL((ssb.sb.st_mode&xmask)!=0 && !S_ISDIR(ssb.sb.st_mode));
case FS_IS_FILE:
RETURN_BOOL(S_ISREG(ssb.sb.st_mode));
case FS_IS_DIR:
RETURN_BOOL(S_ISDIR(ssb.sb.st_mode));
case FS_IS_LINK:
RETURN_BOOL(S_ISLNK(ssb.sb.st_mode));
case FS_EXISTS:
RETURN_TRUE; /* the false case was done earlier */
case FS_LSTAT:
/* FALLTHROUGH */
case FS_STAT:
array_init(return_value);
MAKE_LONG_ZVAL_INCREF(stat_dev, stat_sb->st_dev);
MAKE_LONG_ZVAL_INCREF(stat_ino, stat_sb->st_ino);
MAKE_LONG_ZVAL_INCREF(stat_mode, stat_sb->st_mode);
MAKE_LONG_ZVAL_INCREF(stat_nlink, stat_sb->st_nlink);
MAKE_LONG_ZVAL_INCREF(stat_uid, stat_sb->st_uid);
MAKE_LONG_ZVAL_INCREF(stat_gid, stat_sb->st_gid);
#ifdef HAVE_ST_RDEV
MAKE_LONG_ZVAL_INCREF(stat_rdev, stat_sb->st_rdev);
#else
MAKE_LONG_ZVAL_INCREF(stat_rdev, -1);
#endif
MAKE_LONG_ZVAL_INCREF(stat_size, stat_sb->st_size);
#ifdef NETWARE
MAKE_LONG_ZVAL_INCREF(stat_atime, (stat_sb->st_atime).tv_sec);
MAKE_LONG_ZVAL_INCREF(stat_mtime, (stat_sb->st_mtime).tv_sec);
MAKE_LONG_ZVAL_INCREF(stat_ctime, (stat_sb->st_ctime).tv_sec);
#else
MAKE_LONG_ZVAL_INCREF(stat_atime, stat_sb->st_atime);
MAKE_LONG_ZVAL_INCREF(stat_mtime, stat_sb->st_mtime);
MAKE_LONG_ZVAL_INCREF(stat_ctime, stat_sb->st_ctime);
#endif
#ifdef HAVE_ST_BLKSIZE
MAKE_LONG_ZVAL_INCREF(stat_blksize, stat_sb->st_blksize);
#else
MAKE_LONG_ZVAL_INCREF(stat_blksize,-1);
#endif
#ifdef HAVE_ST_BLOCKS
MAKE_LONG_ZVAL_INCREF(stat_blocks, stat_sb->st_blocks);
#else
MAKE_LONG_ZVAL_INCREF(stat_blocks,-1);
#endif
/* Store numeric indexes in propper order */
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_dev, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_ino, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_mode, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_nlink, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_uid, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_gid, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_rdev, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_size, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_atime, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_mtime, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_ctime, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_blksize, sizeof(zval *), NULL);
zend_hash_next_index_insert(HASH_OF(return_value), (void *)&stat_blocks, sizeof(zval *), NULL);
/* Store string indexes referencing the same zval*/
zend_hash_update(HASH_OF(return_value), stat_sb_names[0], strlen(stat_sb_names[0])+1, (void *) &stat_dev, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[1], strlen(stat_sb_names[1])+1, (void *) &stat_ino, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[2], strlen(stat_sb_names[2])+1, (void *) &stat_mode, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[3], strlen(stat_sb_names[3])+1, (void *) &stat_nlink, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[4], strlen(stat_sb_names[4])+1, (void *) &stat_uid, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[5], strlen(stat_sb_names[5])+1, (void *) &stat_gid, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[6], strlen(stat_sb_names[6])+1, (void *) &stat_rdev, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[7], strlen(stat_sb_names[7])+1, (void *) &stat_size, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[8], strlen(stat_sb_names[8])+1, (void *) &stat_atime, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[9], strlen(stat_sb_names[9])+1, (void *) &stat_mtime, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[10], strlen(stat_sb_names[10])+1, (void *) &stat_ctime, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[11], strlen(stat_sb_names[11])+1, (void *) &stat_blksize, sizeof(zval *), NULL);
zend_hash_update(HASH_OF(return_value), stat_sb_names[12], strlen(stat_sb_names[12])+1, (void *) &stat_blocks, sizeof(zval *), NULL);
return;
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Didn't understand stat call");
RETURN_FALSE;
}
/* }}} */
/* another quickie macro to make defining similar functions easier */
#define FileFunction(name, funcnum) \
void name(INTERNAL_FUNCTION_PARAMETERS) { \
zval **filename; \
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &filename) == FAILURE) { \
WRONG_PARAM_COUNT; \
} \
convert_to_string_ex(filename); \
php_stat(Z_STRVAL_PP(filename), (php_stat_len) Z_STRLEN_PP(filename), funcnum, return_value TSRMLS_CC); \
}
/* {{{ proto int fileperms(string filename)
Get file permissions */
FileFunction(PHP_FN(fileperms), FS_PERMS)
/* }}} */
/* {{{ proto int fileinode(string filename)
Get file inode */
FileFunction(PHP_FN(fileinode), FS_INODE)
/* }}} */
/* {{{ proto int filesize(string filename)
Get file size */
FileFunction(PHP_FN(filesize), FS_SIZE)
/* }}} */
/* {{{ proto int fileowner(string filename)
Get file owner */
FileFunction(PHP_FN(fileowner), FS_OWNER)
/* }}} */
/* {{{ proto int filegroup(string filename)
Get file group */
FileFunction(PHP_FN(filegroup), FS_GROUP)
/* }}} */
/* {{{ proto int fileatime(string filename)
Get last access time of file */
FileFunction(PHP_FN(fileatime), FS_ATIME)
/* }}} */
/* {{{ proto int filemtime(string filename)
Get last modification time of file */
FileFunction(PHP_FN(filemtime), FS_MTIME)
/* }}} */
/* {{{ proto int filectime(string filename)
Get inode modification time of file */
FileFunction(PHP_FN(filectime), FS_CTIME)
/* }}} */
/* {{{ proto string filetype(string filename)
Get file type */
FileFunction(PHP_FN(filetype), FS_TYPE)
/* }}} */
/* {{{ proto bool is_writable(string filename)
Returns true if file can be written */
FileFunction(PHP_FN(is_writable), FS_IS_W)
/* }}} */
/* {{{ proto bool is_readable(string filename)
Returns true if file can be read */
FileFunction(PHP_FN(is_readable), FS_IS_R)
/* }}} */
/* {{{ proto bool is_executable(string filename)
Returns true if file is executable */
FileFunction(PHP_FN(is_executable), FS_IS_X)
/* }}} */
/* {{{ proto bool is_file(string filename)
Returns true if file is a regular file */
FileFunction(PHP_FN(is_file), FS_IS_FILE)
/* }}} */
/* {{{ proto bool is_dir(string filename)
Returns true if file is directory */
FileFunction(PHP_FN(is_dir), FS_IS_DIR)
/* }}} */
/* {{{ proto bool is_link(string filename)
Returns true if file is symbolic link */
FileFunction(PHP_FN(is_link), FS_IS_LINK)
/* }}} */
/* {{{ proto bool file_exists(string filename)
Returns true if filename exists */
FileFunction(PHP_FN(file_exists), FS_EXISTS)
/* }}} */
/* {{{ proto array lstat(string filename)
Give information about a file or symbolic link */
FileFunction(php_if_lstat, FS_LSTAT)
/* }}} */
/* {{{ proto array stat(string filename)
Give information about a file */
FileFunction(php_if_stat, FS_STAT)
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|