phpdbg_rinit_hook.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. | Authors: Bob Weinand <bwoebi@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "phpdbg_rinit_hook.h"
  19. #include "php_ini.h"
  20. #include <errno.h>
  21. ZEND_DECLARE_MODULE_GLOBALS(phpdbg_webhelper);
  22. PHP_INI_BEGIN()
  23. STD_PHP_INI_ENTRY("phpdbg.auth", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, auth, zend_phpdbg_webhelper_globals, phpdbg_webhelper_globals)
  24. STD_PHP_INI_ENTRY("phpdbg.path", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, path, zend_phpdbg_webhelper_globals, phpdbg_webhelper_globals)
  25. PHP_INI_END()
  26. static inline void php_phpdbg_webhelper_globals_ctor(zend_phpdbg_webhelper_globals *pg) /* {{{ */
  27. {
  28. } /* }}} */
  29. static PHP_MINIT_FUNCTION(phpdbg_webhelper) /* {{{ */
  30. {
  31. if (!strcmp(sapi_module.name, PHPDBG_NAME)) {
  32. return SUCCESS;
  33. }
  34. ZEND_INIT_MODULE_GLOBALS(phpdbg_webhelper, php_phpdbg_webhelper_globals_ctor, NULL);
  35. REGISTER_INI_ENTRIES();
  36. return SUCCESS;
  37. } /* }}} */
  38. static PHP_RINIT_FUNCTION(phpdbg_webhelper) /* {{{ */
  39. {
  40. zval cookies = PG(http_globals)[TRACK_VARS_COOKIE];
  41. zval *auth;
  42. if (Z_TYPE(cookies) == IS_ARRAY || (auth = zend_hash_str_find(Z_ARRVAL(cookies), PHPDBG_NAME "_AUTH_COOKIE", sizeof(PHPDBG_NAME "_AUTH_COOKIE"))) || Z_STRLEN_P(auth) != strlen(PHPDBG_WG(auth)) || strcmp(Z_STRVAL_P(auth), PHPDBG_WG(auth))) {
  43. return SUCCESS;
  44. }
  45. #ifndef _WIN32
  46. {
  47. struct sockaddr_un sock;
  48. int s = socket(AF_UNIX, SOCK_STREAM, 0);
  49. size_t len = strlen(PHPDBG_WG(path)) + sizeof(sock.sun_family);
  50. char buf[(1 << 8) + 1];
  51. ssize_t buflen;
  52. sock.sun_family = AF_UNIX;
  53. strcpy(sock.sun_path, PHPDBG_WG(path));
  54. if (connect(s, (struct sockaddr *)&sock, len) == -1) {
  55. zend_error(E_ERROR, "Unable to connect to UNIX domain socket at %s defined by phpdbg.path ini setting. Reason: %s", PHPDBG_WG(path), strerror(errno));
  56. }
  57. char *msg = NULL;
  58. size_t msglen = 0;
  59. phpdbg_webdata_compress(&msg, &msglen);
  60. buf[0] = (msglen >> 0) & 0xff;
  61. buf[1] = (msglen >> 8) & 0xff;
  62. buf[2] = (msglen >> 16) & 0xff;
  63. buf[3] = (msglen >> 24) & 0xff;
  64. send(s, buf, 4, 0);
  65. send(s, msg, msglen, 0);
  66. while ((buflen = recv(s, buf, sizeof(buf) - 1, 0)) > 0) {
  67. php_write(buf, buflen);
  68. }
  69. close(s);
  70. php_output_flush_all();
  71. zend_bailout();
  72. }
  73. #endif
  74. return SUCCESS;
  75. } /* }}} */
  76. zend_module_entry phpdbg_webhelper_module_entry = {
  77. STANDARD_MODULE_HEADER,
  78. "phpdbg_webhelper",
  79. NULL,
  80. PHP_MINIT(phpdbg_webhelper),
  81. NULL,
  82. PHP_RINIT(phpdbg_webhelper),
  83. NULL,
  84. NULL,
  85. PHPDBG_VERSION,
  86. STANDARD_MODULE_PROPERTIES
  87. };
  88. #ifdef COMPILE_DL_PHPDBG_WEBHELPER
  89. ZEND_GET_MODULE(phpdbg_webhelper)
  90. #endif