xref: /PHP-7.3/ext/standard/pageinfo.c (revision 8d3f8ca1)
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 "pageinfo.h"
21 #include "SAPI.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #if HAVE_PWD_H
26 #ifdef PHP_WIN32
27 #include "win32/pwd.h"
28 #else
29 #include <pwd.h>
30 #endif
31 #endif
32 #if HAVE_GRP_H
33 # ifdef PHP_WIN32
34 #  include "win32/grp.h"
35 # else
36 #  include <grp.h>
37 # endif
38 #endif
39 #ifdef PHP_WIN32
40 #undef getgid
41 #define getgroups(a, b) 0
42 #define getgid() 1
43 #define getuid() 1
44 #endif
45 #if HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <sys/stat.h>
49 #include <sys/types.h>
50 #ifdef PHP_WIN32
51 #include <process.h>
52 #endif
53 
54 #include "ext/standard/basic_functions.h"
55 
56 /* {{{ php_statpage
57  */
php_statpage(void)58 PHPAPI void php_statpage(void)
59 {
60 	zend_stat_t *pstat;
61 
62 	pstat = sapi_get_stat();
63 
64 	if (BG(page_uid)==-1 || BG(page_gid)==-1) {
65 		if(pstat) {
66 			BG(page_uid)   = pstat->st_uid;
67 			BG(page_gid)   = pstat->st_gid;
68 			BG(page_inode) = pstat->st_ino;
69 			BG(page_mtime) = pstat->st_mtime;
70 		} else { /* handler for situations where there is no source file, ex. php -r */
71 			BG(page_uid) = getuid();
72 			BG(page_gid) = getgid();
73 		}
74 	}
75 }
76 /* }}} */
77 
78 /* {{{ php_getuid
79  */
php_getuid(void)80 zend_long php_getuid(void)
81 {
82 	php_statpage();
83 	return (BG(page_uid));
84 }
85 /* }}} */
86 
php_getgid(void)87 zend_long php_getgid(void)
88 {
89 	php_statpage();
90 	return (BG(page_gid));
91 }
92 
93 /* {{{ proto int getmyuid(void)
94    Get PHP script owner's UID */
PHP_FUNCTION(getmyuid)95 PHP_FUNCTION(getmyuid)
96 {
97 	zend_long uid;
98 
99 	if (zend_parse_parameters_none() == FAILURE) {
100 		return;
101 	}
102 
103 	uid = php_getuid();
104 	if (uid < 0) {
105 		RETURN_FALSE;
106 	} else {
107 		RETURN_LONG(uid);
108 	}
109 }
110 /* }}} */
111 
112 /* {{{ proto int getmygid(void)
113    Get PHP script owner's GID */
PHP_FUNCTION(getmygid)114 PHP_FUNCTION(getmygid)
115 {
116 	zend_long gid;
117 
118 	if (zend_parse_parameters_none() == FAILURE) {
119 		return;
120 	}
121 
122 	gid = php_getgid();
123 	if (gid < 0) {
124 		RETURN_FALSE;
125 	} else {
126 		RETURN_LONG(gid);
127 	}
128 }
129 /* }}} */
130 
131 /* {{{ proto int getmypid(void)
132    Get current process ID */
PHP_FUNCTION(getmypid)133 PHP_FUNCTION(getmypid)
134 {
135 	zend_long pid;
136 
137 	if (zend_parse_parameters_none() == FAILURE) {
138 		return;
139 	}
140 
141 	pid = getpid();
142 	if (pid < 0) {
143 		RETURN_FALSE;
144 	} else {
145 		RETURN_LONG(pid);
146 	}
147 }
148 /* }}} */
149 
150 /* {{{ proto int getmyinode(void)
151    Get the inode of the current script being parsed */
PHP_FUNCTION(getmyinode)152 PHP_FUNCTION(getmyinode)
153 {
154 	if (zend_parse_parameters_none() == FAILURE) {
155 		return;
156 	}
157 
158 	php_statpage();
159 	if (BG(page_inode) < 0) {
160 		RETURN_FALSE;
161 	} else {
162 		RETURN_LONG(BG(page_inode));
163 	}
164 }
165 /* }}} */
166 
php_getlastmod(void)167 PHPAPI time_t php_getlastmod(void)
168 {
169 	php_statpage();
170 	return BG(page_mtime);
171 }
172 
173 /* {{{ proto int getlastmod(void)
174    Get time of last page modification */
PHP_FUNCTION(getlastmod)175 PHP_FUNCTION(getlastmod)
176 {
177 	zend_long lm;
178 
179 	if (zend_parse_parameters_none() == FAILURE) {
180 		return;
181 	}
182 
183 	lm = php_getlastmod();
184 	if (lm < 0) {
185 		RETURN_FALSE;
186 	} else {
187 		RETURN_LONG(lm);
188 	}
189 }
190 /* }}} */
191 
192 /*nma
193  * Local variables:
194  * tab-width: 4
195  * c-basic-offset: 4
196  * End:
197  * vim600: sw=4 ts=4 fdm=marker
198  * vim<600: sw=4 ts=4
199  */
200