xref: /PHP-8.3/ext/standard/pageinfo.c (revision 265c88b9)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Jim Winstead <jimw@php.net>                                  |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "php.h"
18 #include "pageinfo.h"
19 #include "SAPI.h"
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #ifdef HAVE_PWD_H
24 #ifdef PHP_WIN32
25 #include "win32/pwd.h"
26 #else
27 #include <pwd.h>
28 #endif
29 #endif
30 #if HAVE_GRP_H
31 # include <grp.h>
32 #endif
33 #ifdef PHP_WIN32
34 #undef getgid
35 #define getgroups(a, b) 0
36 #define getgid() 1
37 #define getuid() 1
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #ifdef PHP_WIN32
45 #include <process.h>
46 #endif
47 
48 #include "ext/standard/basic_functions.h"
49 
50 /* {{{ php_statpage */
php_statpage(void)51 PHPAPI void php_statpage(void)
52 {
53 	zend_stat_t *pstat = NULL;
54 
55 	pstat = sapi_get_stat();
56 
57 	if (BG(page_uid)==-1 || BG(page_gid)==-1) {
58 		if(pstat) {
59 			BG(page_uid)   = pstat->st_uid;
60 			BG(page_gid)   = pstat->st_gid;
61 			BG(page_inode) = pstat->st_ino;
62 			BG(page_mtime) = pstat->st_mtime;
63 		} else { /* handler for situations where there is no source file, ex. php -r */
64 			BG(page_uid) = getuid();
65 			BG(page_gid) = getgid();
66 		}
67 	}
68 }
69 /* }}} */
70 
71 /* {{{ php_getuid */
php_getuid(void)72 zend_long php_getuid(void)
73 {
74 	php_statpage();
75 	return (BG(page_uid));
76 }
77 /* }}} */
78 
php_getgid(void)79 zend_long php_getgid(void)
80 {
81 	php_statpage();
82 	return (BG(page_gid));
83 }
84 
85 /* {{{ Get PHP script owner's UID */
PHP_FUNCTION(getmyuid)86 PHP_FUNCTION(getmyuid)
87 {
88 	zend_long uid;
89 
90 	ZEND_PARSE_PARAMETERS_NONE();
91 
92 	uid = php_getuid();
93 	if (uid < 0) {
94 		RETURN_FALSE;
95 	} else {
96 		RETURN_LONG(uid);
97 	}
98 }
99 /* }}} */
100 
101 /* {{{ Get PHP script owner's GID */
PHP_FUNCTION(getmygid)102 PHP_FUNCTION(getmygid)
103 {
104 	zend_long gid;
105 
106 	ZEND_PARSE_PARAMETERS_NONE();
107 
108 	gid = php_getgid();
109 	if (gid < 0) {
110 		RETURN_FALSE;
111 	} else {
112 		RETURN_LONG(gid);
113 	}
114 }
115 /* }}} */
116 
117 /* {{{ Get current process ID */
PHP_FUNCTION(getmypid)118 PHP_FUNCTION(getmypid)
119 {
120 	zend_long pid;
121 
122 	ZEND_PARSE_PARAMETERS_NONE();
123 
124 	pid = getpid();
125 	if (pid < 0) {
126 		RETURN_FALSE;
127 	} else {
128 		RETURN_LONG(pid);
129 	}
130 }
131 /* }}} */
132 
133 /* {{{ Get the inode of the current script being parsed */
PHP_FUNCTION(getmyinode)134 PHP_FUNCTION(getmyinode)
135 {
136 	ZEND_PARSE_PARAMETERS_NONE();
137 
138 	php_statpage();
139 	if (BG(page_inode) < 0) {
140 		RETURN_FALSE;
141 	} else {
142 		RETURN_LONG(BG(page_inode));
143 	}
144 }
145 /* }}} */
146 
php_getlastmod(void)147 PHPAPI time_t php_getlastmod(void)
148 {
149 	php_statpage();
150 	return BG(page_mtime);
151 }
152 
153 /* {{{ Get time of last page modification */
PHP_FUNCTION(getlastmod)154 PHP_FUNCTION(getlastmod)
155 {
156 	zend_long lm;
157 
158 	ZEND_PARSE_PARAMETERS_NONE();
159 
160 	lm = php_getlastmod();
161 	if (lm < 0) {
162 		RETURN_FALSE;
163 	} else {
164 		RETURN_LONG(lm);
165 	}
166 }
167 /* }}} */
168