Advertisement
Guest User

php embed test

a guest
Jan 16th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1.  
  2. #define TEST1 0
  3.  
  4. #include <iostream>
  5. #include <thread>
  6.  
  7. #include <main/php.h>
  8. #include <main/SAPI.h>
  9. #include <main/php_main.h>
  10. #if TEST1 == 1
  11. #include <php_embed.h>
  12. #endif
  13.  
  14. zend_module_entry ips_module_entry = {
  15. STANDARD_MODULE_HEADER,
  16. "XYZ",
  17. NULL,
  18. NULL,
  19. NULL,
  20. NULL,
  21. NULL,
  22. NULL,
  23. NO_VERSION_YET,
  24. STANDARD_MODULE_PROPERTIES
  25. };
  26.  
  27. static size_t php_embed_read_post(char *str, size_t str_length)
  28. {
  29. return 0;
  30. }
  31.  
  32. static char* php_embed_read_cookies()
  33. {
  34. return NULL;
  35. }
  36.  
  37. static size_t php_embed_ub_write(const char *str, size_t str_length)
  38. {
  39. std::cout << str;
  40. return str_length;
  41. }
  42.  
  43. static void php_embed_flush(void *server_context)
  44. {
  45. //
  46. }
  47.  
  48. static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context)
  49. {
  50. //
  51. }
  52.  
  53. static void php_embed_log_message(char *message, int a)
  54. {
  55. fprintf(stderr, "%s\n", message);
  56. }
  57.  
  58. static void php_embed_register_variables(zval *track_vars_array)
  59. {
  60. //
  61. }
  62.  
  63. static int php_embed_startup(sapi_module_struct *sapi_module)
  64. {
  65. if (php_module_startup(sapi_module, NULL, 0) == FAILURE) {
  66. return FAILURE;
  67. }
  68. return SUCCESS;
  69. }
  70.  
  71. #if TEST1 == 0
  72. sapi_module_struct php_embed_module = {
  73. "XYZ", /* name */
  74. "PHP for XYZ", /* pretty name */
  75.  
  76. php_embed_startup, /* startup */
  77. php_module_shutdown_wrapper, /* shutdown */
  78.  
  79. NULL, /* activate */
  80. NULL, /* deactivate */
  81.  
  82. php_embed_ub_write, /* unbuffered write */
  83. php_embed_flush, /* flush */
  84. NULL, /* get uid */
  85. NULL, /* getenv */
  86.  
  87. php_error, /* error handler */
  88.  
  89. NULL, /* header handler */
  90. NULL, /* send headers handler */
  91. php_embed_send_header, /* send header handler */
  92.  
  93. php_embed_read_post, /* read POST data */
  94. php_embed_read_cookies, /* read Cookies */
  95.  
  96. php_embed_register_variables, /* register server variables */
  97. php_embed_log_message, /* Log message */
  98. NULL, /* Get request time */
  99. NULL, /* Child terminate */
  100.  
  101. STANDARD_SAPI_MODULE_PROPERTIES
  102. };
  103. #endif
  104.  
  105. int main(int argc, const char * argv[]) {
  106.  
  107. tsrm_startup(128, 1, 0, NULL);
  108.  
  109. #if TEST1 == 0
  110. sapi_startup(&php_embed_module);
  111.  
  112. if (php_embed_module.startup(&php_embed_module) == FAILURE) {
  113. throw std::runtime_error("Could not start PHP!");
  114. }
  115. #endif
  116.  
  117. #if TEST1 == 1
  118. int argc2 = 1;
  119. char* text = "embed4";
  120. char *argv2[2] = { text, NULL };
  121. php_embed_init(argc2, argv2);
  122. #endif
  123.  
  124.  
  125. for(int i = 0; i < 10; i++) {
  126. std::thread([&argv]{
  127. ts_resource(0);
  128.  
  129. while(true){
  130. zend_file_handle file_handle;
  131. file_handle.type = ZEND_HANDLE_FILENAME;
  132. file_handle.filename = "/tmp/test1.php";
  133. file_handle.handle.fp = NULL;
  134. file_handle.opened_path = NULL;
  135. file_handle.free_filename = 0;
  136.  
  137. if (php_request_startup() == FAILURE) {
  138. //std::cout << "ERR" << std::endl;
  139. php_request_shutdown(NULL);
  140. continue;
  141. }
  142.  
  143. if(php_execute_script(&file_handle) == FAILURE)
  144. break;
  145.  
  146. //std::cout << "RUN" << std::endl;
  147. php_request_shutdown(NULL);
  148. }
  149.  
  150. std::cout << "DIED" << std::endl;
  151.  
  152. }).detach();
  153. }
  154.  
  155. while(true);
  156.  
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement