pageinfo.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include "php.h"
  17. #include "pageinfo.h"
  18. #include "SAPI.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #if HAVE_PWD_H
  22. #ifdef PHP_WIN32
  23. #include "win32/pwd.h"
  24. #else
  25. #include <pwd.h>
  26. #endif
  27. #endif
  28. #if HAVE_GRP_H
  29. # include <grp.h>
  30. #endif
  31. #ifdef PHP_WIN32
  32. #undef getgid
  33. #define getgroups(a, b) 0
  34. #define getgid() 1
  35. #define getuid() 1
  36. #endif
  37. #if HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #include <sys/stat.h>
  41. #include <sys/types.h>
  42. #ifdef PHP_WIN32
  43. #include <process.h>
  44. #endif
  45. #include "ext/standard/basic_functions.h"
  46. /* {{{ php_statpage */
  47. PHPAPI void php_statpage(void)
  48. {
  49. zend_stat_t *pstat;
  50. pstat = sapi_get_stat();
  51. if (BG(page_uid)==-1 || BG(page_gid)==-1) {
  52. if(pstat) {
  53. BG(page_uid) = pstat->st_uid;
  54. BG(page_gid) = pstat->st_gid;
  55. BG(page_inode) = pstat->st_ino;
  56. BG(page_mtime) = pstat->st_mtime;
  57. } else { /* handler for situations where there is no source file, ex. php -r */
  58. BG(page_uid) = getuid();
  59. BG(page_gid) = getgid();
  60. }
  61. }
  62. }
  63. /* }}} */
  64. /* {{{ php_getuid */
  65. zend_long php_getuid(void)
  66. {
  67. php_statpage();
  68. return (BG(page_uid));
  69. }
  70. /* }}} */
  71. zend_long php_getgid(void)
  72. {
  73. php_statpage();
  74. return (BG(page_gid));
  75. }
  76. /* {{{ Get PHP script owner's UID */
  77. PHP_FUNCTION(getmyuid)
  78. {
  79. zend_long uid;
  80. ZEND_PARSE_PARAMETERS_NONE();
  81. uid = php_getuid();
  82. if (uid < 0) {
  83. RETURN_FALSE;
  84. } else {
  85. RETURN_LONG(uid);
  86. }
  87. }
  88. /* }}} */
  89. /* {{{ Get PHP script owner's GID */
  90. PHP_FUNCTION(getmygid)
  91. {
  92. zend_long gid;
  93. ZEND_PARSE_PARAMETERS_NONE();
  94. gid = php_getgid();
  95. if (gid < 0) {
  96. RETURN_FALSE;
  97. } else {
  98. RETURN_LONG(gid);
  99. }
  100. }
  101. /* }}} */
  102. /* {{{ Get current process ID */
  103. PHP_FUNCTION(getmypid)
  104. {
  105. zend_long pid;
  106. ZEND_PARSE_PARAMETERS_NONE();
  107. pid = getpid();
  108. if (pid < 0) {
  109. RETURN_FALSE;
  110. } else {
  111. RETURN_LONG(pid);
  112. }
  113. }
  114. /* }}} */
  115. /* {{{ Get the inode of the current script being parsed */
  116. PHP_FUNCTION(getmyinode)
  117. {
  118. ZEND_PARSE_PARAMETERS_NONE();
  119. php_statpage();
  120. if (BG(page_inode) < 0) {
  121. RETURN_FALSE;
  122. } else {
  123. RETURN_LONG(BG(page_inode));
  124. }
  125. }
  126. /* }}} */
  127. PHPAPI time_t php_getlastmod(void)
  128. {
  129. php_statpage();
  130. return BG(page_mtime);
  131. }
  132. /* {{{ Get time of last page modification */
  133. PHP_FUNCTION(getlastmod)
  134. {
  135. zend_long lm;
  136. ZEND_PARSE_PARAMETERS_NONE();
  137. lm = php_getlastmod();
  138. if (lm < 0) {
  139. RETURN_FALSE;
  140. } else {
  141. RETURN_LONG(lm);
  142. }
  143. }
  144. /* }}} */