mod_php5.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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 at through the world-wide-web at |
  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: Rasmus Lerdorf <rasmus@php.net> |
  16. | (with helpful hints from Dean Gaudet <dgaudet@arctic.org> |
  17. | PHP 4.0 patches by Zeev Suraski <zeev@zend.com> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #include "php_apache_http.h"
  22. #ifdef NETWARE
  23. #define SIGPIPE SIGINT
  24. #endif
  25. #undef shutdown
  26. /* {{{ Prototypes
  27. */
  28. int apache_php_module_main(request_rec *r, int display_source_mode TSRMLS_DC);
  29. static void php_save_umask(void);
  30. static void php_restore_umask(void);
  31. static int sapi_apache_read_post(char *buffer, uint count_bytes TSRMLS_DC);
  32. static char *sapi_apache_read_cookies(TSRMLS_D);
  33. static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC);
  34. static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC);
  35. static int send_php(request_rec *r, int display_source_mode, char *filename);
  36. static int send_parsed_php(request_rec * r);
  37. static int send_parsed_php_source(request_rec * r);
  38. static int php_xbithack_handler(request_rec * r);
  39. static void php_init_handler(server_rec *s, pool *p);
  40. /* }}} */
  41. #if MODULE_MAGIC_NUMBER >= 19970728
  42. static void php_child_exit_handler(server_rec *s, pool *p);
  43. #endif
  44. #if MODULE_MAGIC_NUMBER > 19961007
  45. #define CONST_PREFIX const
  46. #else
  47. #define CONST_PREFIX
  48. #endif
  49. typedef struct _sapi_stack {
  50. int top, max, persistent;
  51. void **elements;
  52. } sapi_stack;
  53. typedef struct _php_per_dir_config {
  54. HashTable *ini_settings;
  55. sapi_stack headers_handlers;
  56. sapi_stack auth_handlers;
  57. sapi_stack access_handlers;
  58. sapi_stack type_handlers;
  59. sapi_stack fixup_handlers;
  60. sapi_stack logger_handlers;
  61. sapi_stack post_read_handlers;
  62. sapi_stack response_handlers;
  63. } php_per_dir_config;
  64. typedef struct _php_per_server_config {
  65. sapi_stack uri_handlers;
  66. sapi_stack requires;
  67. } php_per_server_config;
  68. static CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode);
  69. static CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2);
  70. static CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2);
  71. static CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2);
  72. static CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode);
  73. static CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2);
  74. /* ### these should be defined in mod_php5.h or somewhere else */
  75. #define USE_PATH 1
  76. #define IGNORE_URL 2
  77. module MODULE_VAR_EXPORT php5_module;
  78. int saved_umask;
  79. /* static int setup_env = 0; */
  80. static unsigned char apache_php_initialized;
  81. typedef struct _php_per_dir_entry {
  82. char *key;
  83. char *value;
  84. uint key_length;
  85. uint value_length;
  86. int type;
  87. } php_per_dir_entry;
  88. /* some systems are missing these from their header files */
  89. /* {{{ zend stack utility functions
  90. */
  91. /* This code is ripped part and parcel from zend_stack.[ch]. Assuming that the
  92. patch supporting zend_stack_init_ex is applied, all but the bottom two
  93. module-specific iterators will be removed
  94. */
  95. int sapi_stack_init_ex(sapi_stack *stack, int persistent)
  96. {
  97. stack->top = 0;
  98. stack->persistent = persistent;
  99. stack->elements = (void **) pemalloc(sizeof(void **) * STACK_BLOCK_SIZE, persistent);
  100. if (!stack->elements) {
  101. return FAILURE;
  102. } else {
  103. stack->max = STACK_BLOCK_SIZE;
  104. return SUCCESS;
  105. }
  106. }
  107. int sapi_stack_push(sapi_stack *stack, void *element)
  108. {
  109. if (stack->top >= stack->max) { /* we need to allocate more memory */
  110. stack->elements = (void **) perealloc(stack->elements,
  111. (sizeof(void **) * (stack->max += STACK_BLOCK_SIZE)), stack->persistent);
  112. if (!stack->elements) {
  113. return FAILURE;
  114. }
  115. }
  116. stack->elements[stack->top] = (void *) element;
  117. return stack->top++;
  118. }
  119. void* sapi_stack_pop(sapi_stack *stack) {
  120. if(stack->top == 0) {
  121. return NULL;
  122. }
  123. else {
  124. return stack->elements[--stack->top];
  125. }
  126. }
  127. int sapi_stack_destroy(sapi_stack *stack)
  128. {
  129. return SUCCESS;
  130. }
  131. int sapi_stack_apply_with_argument_all(sapi_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
  132. {
  133. int i, retval;
  134. switch (type) {
  135. case ZEND_STACK_APPLY_TOPDOWN:
  136. for (i=stack->top-1; i>=0; i--) {
  137. retval = apply_function(stack->elements[i], arg);
  138. }
  139. break;
  140. case ZEND_STACK_APPLY_BOTTOMUP:
  141. for (i=0; i<stack->top; i++) {
  142. retval = apply_function(stack->elements[i], arg);
  143. }
  144. break;
  145. }
  146. return retval;
  147. }
  148. int sapi_stack_apply_with_argument_stop_if_equals(sapi_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg, int stopval)
  149. {
  150. int i;
  151. int ret = DECLINED;
  152. switch (type) {
  153. case ZEND_STACK_APPLY_TOPDOWN:
  154. for (i=stack->top-1; i>=0; i--) {
  155. if ((ret = apply_function(stack->elements[i], arg)) == stopval) {
  156. break;
  157. }
  158. }
  159. break;
  160. case ZEND_STACK_APPLY_BOTTOMUP:
  161. for (i=0; i<stack->top; i++) {
  162. if ((ret = apply_function(stack->elements[i], arg)) == stopval) {
  163. break;
  164. }
  165. }
  166. break;
  167. }
  168. return ret;
  169. }
  170. int sapi_stack_apply_with_argument_stop_if_http_error(sapi_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
  171. {
  172. int i;
  173. int ret = DECLINED;
  174. switch (type) {
  175. case ZEND_STACK_APPLY_TOPDOWN:
  176. for (i=stack->top-1; i>=0; i--) {
  177. if ((ret = apply_function(stack->elements[i], arg)) > 0) {
  178. break;
  179. }
  180. }
  181. break;
  182. case ZEND_STACK_APPLY_BOTTOMUP:
  183. for (i=0; i<stack->top; i++) {
  184. if ((ret = apply_function(stack->elements[i], arg)) > 0) {
  185. break;
  186. }
  187. }
  188. break;
  189. }
  190. return ret;
  191. }
  192. void php_handler_stack_destroy(sapi_stack *stack)
  193. {
  194. php_handler *ph;
  195. while((ph = (php_handler *)sapi_stack_pop(stack)) != NULL) {
  196. free(ph->name);
  197. free(ph);
  198. }
  199. }
  200. /* }}} */
  201. /* {{{ php_save_umask
  202. */
  203. static void php_save_umask(void)
  204. {
  205. saved_umask = umask(077);
  206. umask(saved_umask);
  207. }
  208. /* }}} */
  209. /* {{{ sapi_apache_ub_write
  210. */
  211. static int sapi_apache_ub_write(const char *str, uint str_length TSRMLS_DC)
  212. {
  213. int ret=0;
  214. if (SG(server_context)) {
  215. ret = rwrite(str, str_length, (request_rec *) SG(server_context));
  216. }
  217. if (ret != str_length) {
  218. php_handle_aborted_connection();
  219. }
  220. return ret;
  221. }
  222. /* }}} */
  223. /* {{{ sapi_apache_flush
  224. */
  225. static void sapi_apache_flush(void *server_context)
  226. {
  227. if (server_context) {
  228. #if MODULE_MAGIC_NUMBER > 19970110
  229. rflush((request_rec *) server_context);
  230. #else
  231. bflush((request_rec *) server_context->connection->client);
  232. #endif
  233. }
  234. }
  235. /* }}} */
  236. /* {{{ sapi_apache_read_post
  237. */
  238. static int sapi_apache_read_post(char *buffer, uint count_bytes TSRMLS_DC)
  239. {
  240. uint total_read_bytes=0, read_bytes;
  241. request_rec *r = (request_rec *) SG(server_context);
  242. void (*handler)(int);
  243. /*
  244. * This handles the situation where the browser sends a Expect: 100-continue header
  245. * and needs to receive confirmation from the server on whether or not it can send
  246. * the rest of the request. RFC 2616
  247. *
  248. */
  249. if (!SG(read_post_bytes) && !ap_should_client_block(r)) {
  250. return total_read_bytes;
  251. }
  252. handler = signal(SIGPIPE, SIG_IGN);
  253. while (total_read_bytes<count_bytes) {
  254. hard_timeout("Read POST information", r); /* start timeout timer */
  255. read_bytes = get_client_block(r, buffer+total_read_bytes, count_bytes-total_read_bytes);
  256. reset_timeout(r);
  257. if (read_bytes<=0) {
  258. break;
  259. }
  260. total_read_bytes += read_bytes;
  261. }
  262. signal(SIGPIPE, handler);
  263. return total_read_bytes;
  264. }
  265. /* }}} */
  266. /* {{{ sapi_apache_read_cookies
  267. */
  268. static char *sapi_apache_read_cookies(TSRMLS_D)
  269. {
  270. return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, "HTTP_COOKIE");
  271. }
  272. /* }}} */
  273. /* {{{ sapi_apache_header_handler
  274. */
  275. static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC)
  276. {
  277. char *header_name, *header_content, *p;
  278. request_rec *r = (request_rec *) SG(server_context);
  279. if(!r) {
  280. return 0;
  281. }
  282. switch(op) {
  283. case SAPI_HEADER_DELETE_ALL:
  284. clear_table(r->headers_out);
  285. return 0;
  286. case SAPI_HEADER_DELETE:
  287. table_unset(r->headers_out, sapi_header->header);
  288. return 0;
  289. case SAPI_HEADER_ADD:
  290. case SAPI_HEADER_REPLACE:
  291. header_name = sapi_header->header;
  292. header_content = p = strchr(header_name, ':');
  293. if (!p) {
  294. return 0;
  295. }
  296. *p = 0;
  297. do {
  298. header_content++;
  299. } while (*header_content==' ');
  300. if (!strcasecmp(header_name, "Content-Type")) {
  301. r->content_type = pstrdup(r->pool, header_content);
  302. } else if (!strcasecmp(header_name, "Set-Cookie")) {
  303. table_add(r->headers_out, header_name, header_content);
  304. } else if (op == SAPI_HEADER_REPLACE) {
  305. table_set(r->headers_out, header_name, header_content);
  306. } else {
  307. table_add(r->headers_out, header_name, header_content);
  308. }
  309. *p = ':'; /* a well behaved header handler shouldn't change its original arguments */
  310. return SAPI_HEADER_ADD;
  311. default:
  312. return 0;
  313. }
  314. }
  315. /* }}} */
  316. /* {{{ sapi_apache_send_headers
  317. */
  318. static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
  319. {
  320. if(SG(server_context) == NULL) { /* server_context is not here anymore */
  321. return SAPI_HEADER_SEND_FAILED;
  322. }
  323. ((request_rec *) SG(server_context))->status = SG(sapi_headers).http_response_code;
  324. /* check that we haven't sent headers already, we use our own
  325. * headers_sent since we may send headers at anytime
  326. */
  327. if(!AP(headers_sent)) {
  328. send_http_header((request_rec *) SG(server_context));
  329. AP(headers_sent) = 1;
  330. }
  331. return SAPI_HEADER_SENT_SUCCESSFULLY;
  332. }
  333. /* }}} */
  334. /* {{{ sapi_apache_register_server_variables
  335. */
  336. static void sapi_apache_register_server_variables(zval *track_vars_array TSRMLS_DC)
  337. {
  338. register int i;
  339. array_header *arr = table_elts(((request_rec *) SG(server_context))->subprocess_env);
  340. table_entry *elts = (table_entry *) arr->elts;
  341. zval **path_translated;
  342. HashTable *symbol_table;
  343. for (i = 0; i < arr->nelts; i++) {
  344. char *val;
  345. if (elts[i].val) {
  346. val = elts[i].val;
  347. } else {
  348. val = "";
  349. }
  350. php_register_variable(elts[i].key, val, track_vars_array TSRMLS_CC);
  351. }
  352. /* If PATH_TRANSLATED doesn't exist, copy it from SCRIPT_FILENAME */
  353. if (track_vars_array) {
  354. symbol_table = track_vars_array->value.ht;
  355. } else {
  356. symbol_table = NULL;
  357. }
  358. if (symbol_table
  359. && !zend_hash_exists(symbol_table, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED"))
  360. && zend_hash_find(symbol_table, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &path_translated)==SUCCESS) {
  361. php_register_variable("PATH_TRANSLATED", Z_STRVAL_PP(path_translated), track_vars_array TSRMLS_CC);
  362. }
  363. php_register_variable("PHP_SELF", ((request_rec *) SG(server_context))->uri, track_vars_array TSRMLS_CC);
  364. }
  365. /* }}} */
  366. /* {{{ php_apache_startup
  367. */
  368. static int php_apache_startup(sapi_module_struct *sapi_module)
  369. {
  370. if (php_module_startup(sapi_module, &apache_module_entry, 1) == FAILURE) {
  371. return FAILURE;
  372. } else {
  373. return SUCCESS;
  374. }
  375. }
  376. /* }}} */
  377. /* {{{ php_apache_log_message
  378. */
  379. static void php_apache_log_message(char *message TSRMLS_DC)
  380. {
  381. if (SG(server_context)) {
  382. #if MODULE_MAGIC_NUMBER >= 19970831
  383. aplog_error(NULL, 0, APLOG_ERR | APLOG_NOERRNO, ((request_rec *) SG(server_context))->server, "%s", message);
  384. #else
  385. log_error(message, ((request_rec *) SG(server_context))->server);
  386. #endif
  387. } else {
  388. fprintf(stderr, "%s", message);
  389. fprintf(stderr, "\n");
  390. }
  391. }
  392. /* }}} */
  393. /* {{{ php_apache_request_shutdown
  394. */
  395. static void php_apache_request_shutdown(void *dummy)
  396. {
  397. TSRMLS_FETCH();
  398. AP(current_hook) = AP_CLEANUP;
  399. php_output_set_status(PHP_OUTPUT_DISABLED TSRMLS_CC);
  400. SG(server_context) = NULL; /* The server context (request) is invalid by the time run_cleanups() is called */
  401. if(SG(sapi_started)) {
  402. php_request_shutdown(dummy);
  403. SG(sapi_started) = 0;
  404. }
  405. AP(in_request) = 0;
  406. if(AP(setup_env)) {
  407. AP(setup_env) = 0;
  408. }
  409. AP(current_hook) = AP_WAITING_FOR_REQUEST;
  410. AP(headers_sent) = 0;
  411. }
  412. /* }}} */
  413. /* {{{ php_apache_sapi_activate
  414. */
  415. static int php_apache_sapi_activate(TSRMLS_D)
  416. {
  417. request_rec *r = (request_rec *) SG(server_context);
  418. /*
  419. * For the Apache module version, this bit of code registers a cleanup
  420. * function that gets triggered when our request pool is destroyed.
  421. * We need this because at any point in our code we can be interrupted
  422. * and that may happen before we have had time to free our memory.
  423. * The php_request_shutdown function needs to free all outstanding allocated
  424. * memory.
  425. */
  426. block_alarms();
  427. register_cleanup(r->pool, NULL, php_apache_request_shutdown, php_request_shutdown_for_exec);
  428. AP(in_request)=1;
  429. unblock_alarms();
  430. /* Override the default headers_only value - sometimes "GET" requests should actually only
  431. * send headers.
  432. */
  433. SG(request_info).headers_only = r->header_only;
  434. return SUCCESS;
  435. }
  436. /* }}} */
  437. /* {{{ php_apache_get_stat
  438. */
  439. static struct stat *php_apache_get_stat(TSRMLS_D)
  440. {
  441. return &((request_rec *) SG(server_context))->finfo;
  442. }
  443. /* }}} */
  444. /* {{{ php_apache_getenv
  445. */
  446. static char *php_apache_getenv(char *name, size_t name_len TSRMLS_DC)
  447. {
  448. return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, name);
  449. }
  450. /* }}} */
  451. /* {{{ sapi_module_struct apache_sapi_module
  452. */
  453. static sapi_module_struct apache_sapi_module = {
  454. "apache", /* name */
  455. "Apache", /* pretty name */
  456. php_apache_startup, /* startup */
  457. php_module_shutdown_wrapper, /* shutdown */
  458. php_apache_sapi_activate, /* activate */
  459. NULL, /* deactivate */
  460. sapi_apache_ub_write, /* unbuffered write */
  461. sapi_apache_flush, /* flush */
  462. php_apache_get_stat, /* get uid */
  463. php_apache_getenv, /* getenv */
  464. php_error, /* error handler */
  465. sapi_apache_header_handler, /* header handler */
  466. sapi_apache_send_headers, /* send headers handler */
  467. NULL, /* send header handler */
  468. sapi_apache_read_post, /* read POST data */
  469. sapi_apache_read_cookies, /* read Cookies */
  470. sapi_apache_register_server_variables, /* register server variables */
  471. php_apache_log_message, /* Log message */
  472. NULL, /* Get request time */
  473. NULL, /* child terminate */
  474. NULL, /* php.ini path override */
  475. #ifdef PHP_WIN32
  476. NULL,
  477. NULL,
  478. #else
  479. block_alarms, /* Block interruptions */
  480. unblock_alarms, /* Unblock interruptions */
  481. #endif
  482. NULL, /* default post reader */
  483. NULL, /* treat data */
  484. NULL, /* exe location */
  485. 0, /* ini ignore */
  486. NULL
  487. };
  488. /* }}} */
  489. /* {{{ php_restore_umask
  490. */
  491. static void php_restore_umask(void)
  492. {
  493. umask(saved_umask);
  494. }
  495. /* }}} */
  496. /* {{{ init_request_info
  497. */
  498. static void init_request_info(TSRMLS_D)
  499. {
  500. request_rec *r = ((request_rec *) SG(server_context));
  501. char *content_length = (char *) table_get(r->subprocess_env, "CONTENT_LENGTH");
  502. const char *authorization=NULL;
  503. char *tmp, *tmp_user;
  504. SG(request_info).query_string = r->args;
  505. SG(request_info).path_translated = r->filename;
  506. SG(request_info).request_uri = r->uri;
  507. SG(request_info).request_method = (char *)r->method;
  508. SG(request_info).proto_num = r->proto_num;
  509. SG(request_info).content_type = (char *) table_get(r->subprocess_env, "CONTENT_TYPE");
  510. SG(request_info).content_length = (content_length ? atol(content_length) : 0);
  511. SG(sapi_headers).http_response_code = r->status;
  512. if (r->headers_in) {
  513. authorization = table_get(r->headers_in, "Authorization");
  514. }
  515. SG(request_info).auth_user = NULL;
  516. SG(request_info).auth_password = NULL;
  517. if (authorization && !auth_type(r)) {
  518. if (!strcasecmp(getword(r->pool, &authorization, ' '), "Basic")) {
  519. tmp = uudecode(r->pool, authorization);
  520. tmp_user = getword_nulls_nc(r->pool, &tmp, ':');
  521. if (tmp_user) {
  522. r->connection->user = pstrdup(r->connection->pool, tmp_user);
  523. r->connection->ap_auth_type = "Basic";
  524. SG(request_info).auth_user = estrdup(tmp_user);
  525. }
  526. if (tmp) {
  527. SG(request_info).auth_password = estrdup(tmp);
  528. }
  529. } else if (!strcasecmp(getword(r->pool, &authorization, ' '), "Digest")) {
  530. r->connection->ap_auth_type = "Digest";
  531. SG(request_info).auth_digest = estrdup(authorization);
  532. }
  533. }
  534. }
  535. /* }}} */
  536. /* {{{ php_apache_alter_ini_entries
  537. */
  538. static int php_apache_alter_ini_entries(php_per_dir_entry *per_dir_entry TSRMLS_DC)
  539. {
  540. zend_alter_ini_entry(per_dir_entry->key, per_dir_entry->key_length+1, per_dir_entry->value, per_dir_entry->value_length, per_dir_entry->type, PHP_INI_STAGE_ACTIVATE);
  541. return 0;
  542. }
  543. /* }}} */
  544. /* {{{ php_apache_get_default_mimetype
  545. */
  546. static char *php_apache_get_default_mimetype(request_rec *r TSRMLS_DC)
  547. {
  548. char *mimetype;
  549. if (SG(default_mimetype) || SG(default_charset)) {
  550. /* Assume output will be of the default MIME type. Individual
  551. scripts may change this later. */
  552. char *tmpmimetype;
  553. tmpmimetype = sapi_get_default_content_type(TSRMLS_C);
  554. mimetype = pstrdup(r->pool, tmpmimetype);
  555. efree(tmpmimetype);
  556. } else {
  557. mimetype = SAPI_DEFAULT_MIMETYPE "; charset=" SAPI_DEFAULT_CHARSET;
  558. }
  559. return mimetype;
  560. }
  561. /* }}} */
  562. /* {{{ send_php
  563. */
  564. static int send_php(request_rec *r, int display_source_mode, char *filename)
  565. {
  566. int retval;
  567. php_per_dir_config *per_dir_conf;
  568. TSRMLS_FETCH();
  569. if (AP(in_request)) {
  570. zend_file_handle fh;
  571. fh.filename = r->filename;
  572. fh.opened_path = NULL;
  573. fh.free_filename = 0;
  574. fh.type = ZEND_HANDLE_FILENAME;
  575. zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &fh);
  576. return OK;
  577. }
  578. zend_first_try {
  579. /* Make sure file exists */
  580. if (filename == NULL && r->finfo.st_mode == 0) {
  581. return DECLINED;
  582. }
  583. per_dir_conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  584. if (per_dir_conf) {
  585. zend_hash_apply((HashTable *) per_dir_conf->ini_settings, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  586. }
  587. /* If PHP parser engine has been turned off with an "engine off"
  588. * directive, then decline to handle this request
  589. */
  590. if (!AP(engine)) {
  591. r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC);
  592. r->allowed |= (1 << METHODS) - 1;
  593. zend_try {
  594. zend_ini_deactivate(TSRMLS_C);
  595. } zend_end_try();
  596. return DECLINED;
  597. }
  598. if (filename == NULL) {
  599. filename = r->filename;
  600. }
  601. /* Apache 1.2 has a more complex mechanism for reading POST data */
  602. #if MODULE_MAGIC_NUMBER > 19961007
  603. if ((retval = setup_client_block(r, REQUEST_CHUNKED_ERROR))) {
  604. zend_try {
  605. zend_ini_deactivate(TSRMLS_C);
  606. } zend_end_try();
  607. return retval;
  608. }
  609. #endif
  610. if (AP(last_modified)) {
  611. #if MODULE_MAGIC_NUMBER < 19970912
  612. if ((retval = set_last_modified(r, r->finfo.st_mtime))) {
  613. zend_try {
  614. zend_ini_deactivate(TSRMLS_C);
  615. } zend_end_try();
  616. return retval;
  617. }
  618. #else
  619. update_mtime (r, r->finfo.st_mtime);
  620. set_last_modified(r);
  621. set_etag(r);
  622. #endif
  623. }
  624. /* Assume output will be of the default MIME type. Individual
  625. scripts may change this later in the request. */
  626. r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC);
  627. /* Init timeout */
  628. hard_timeout("send", r);
  629. SG(server_context) = r;
  630. php_save_umask();
  631. if(!AP(setup_env)) {
  632. AP(setup_env) = 1;
  633. add_common_vars(r);
  634. add_cgi_vars(r);
  635. }
  636. init_request_info(TSRMLS_C);
  637. apache_php_module_main(r, display_source_mode TSRMLS_CC);
  638. /* Done, restore umask, turn off timeout, close file and return */
  639. php_restore_umask();
  640. kill_timeout(r);
  641. } zend_end_try();
  642. return OK;
  643. }
  644. /* }}} */
  645. /* {{{ send_parsed_php
  646. */
  647. static int send_parsed_php(request_rec * r)
  648. {
  649. int result = send_php(r, 0, NULL);
  650. TSRMLS_FETCH();
  651. ap_table_setn(r->notes, "mod_php_memory_usage",
  652. ap_psprintf(r->pool, "%u", zend_memory_peak_usage(1 TSRMLS_CC)));
  653. return result;
  654. }
  655. /* }}} */
  656. /* {{{ send_parsed_php_source
  657. */
  658. static int send_parsed_php_source(request_rec * r)
  659. {
  660. return send_php(r, 1, NULL);
  661. }
  662. /* }}} */
  663. /* {{{ destroy_per_dir_entry
  664. */
  665. static void destroy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  666. {
  667. free(per_dir_entry->key);
  668. free(per_dir_entry->value);
  669. }
  670. /* }}} */
  671. /* {{{ copy_per_dir_entry
  672. */
  673. static void copy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  674. {
  675. php_per_dir_entry tmp = *per_dir_entry;
  676. per_dir_entry->key = (char *) malloc(tmp.key_length+1);
  677. memcpy(per_dir_entry->key, tmp.key, tmp.key_length);
  678. per_dir_entry->key[per_dir_entry->key_length] = 0;
  679. per_dir_entry->value = (char *) malloc(tmp.value_length+1);
  680. memcpy(per_dir_entry->value, tmp.value, tmp.value_length);
  681. per_dir_entry->value[per_dir_entry->value_length] = 0;
  682. }
  683. /* }}} */
  684. /* {{{ should_overwrite_per_dir_entry;
  685. */
  686. static zend_bool should_overwrite_per_dir_entry(HashTable *target_ht, php_per_dir_entry *orig_per_dir_entry, zend_hash_key *hash_key, void *pData)
  687. {
  688. php_per_dir_entry *new_per_dir_entry;
  689. if (zend_hash_find(target_ht, hash_key->arKey, hash_key->nKeyLength, (void **) &new_per_dir_entry)==FAILURE) {
  690. return 1; /* does not exist in dest, copy from source */
  691. }
  692. if (new_per_dir_entry->type==PHP_INI_SYSTEM
  693. && orig_per_dir_entry->type!=PHP_INI_SYSTEM) {
  694. return 1;
  695. } else {
  696. return 0;
  697. }
  698. }
  699. /* }}} */
  700. /* {{{ php_destroy_per_server_info
  701. */
  702. static void php_destroy_per_server_info(php_per_server_config *conf)
  703. {
  704. php_handler_stack_destroy(&conf->requires);
  705. php_handler_stack_destroy(&conf->uri_handlers);
  706. }
  707. /* }}} */
  708. /* {{{ php_destroy_per_dir_info
  709. */
  710. static void php_destroy_per_dir_info(php_per_dir_config *conf)
  711. {
  712. zend_hash_destroy(conf->ini_settings);
  713. php_handler_stack_destroy(&conf->response_handlers);
  714. php_handler_stack_destroy(&conf->auth_handlers);
  715. php_handler_stack_destroy(&conf->access_handlers);
  716. php_handler_stack_destroy(&conf->type_handlers);
  717. php_handler_stack_destroy(&conf->fixup_handlers);
  718. php_handler_stack_destroy(&conf->logger_handlers);
  719. php_handler_stack_destroy(&conf->post_read_handlers);
  720. php_handler_stack_destroy(&conf->headers_handlers);
  721. free(conf->ini_settings);
  722. }
  723. /* }}} */
  724. /* {{{ php_create_server
  725. */
  726. static void *php_create_server(pool *p, char *dummy)
  727. {
  728. php_per_server_config *conf;
  729. conf = (php_per_server_config *) malloc(sizeof(php_per_server_config));
  730. register_cleanup(p, (void *) conf, (void (*)(void *)) php_destroy_per_server_info, (void (*)(void *)) php_destroy_per_server_info);
  731. sapi_stack_init_ex(&conf->requires, 1);
  732. sapi_stack_init_ex(&conf->uri_handlers, 1);
  733. return conf;
  734. }
  735. /* }}} */
  736. /* {{{ php_create_dir
  737. */
  738. static void *php_create_dir(pool *p, char *dummy)
  739. {
  740. php_per_dir_config *conf;
  741. conf = (php_per_dir_config *) malloc(sizeof(php_per_dir_config));
  742. conf->ini_settings = (HashTable *) malloc(sizeof(HashTable));
  743. zend_hash_init_ex(conf->ini_settings, 5, NULL, (void (*)(void *)) destroy_per_dir_entry, 1, 0);
  744. sapi_stack_init_ex(&conf->response_handlers, 1);
  745. sapi_stack_init_ex(&conf->headers_handlers, 1);
  746. sapi_stack_init_ex(&conf->auth_handlers, 1);
  747. sapi_stack_init_ex(&conf->access_handlers, 1);
  748. sapi_stack_init_ex(&conf->type_handlers, 1);
  749. sapi_stack_init_ex(&conf->fixup_handlers, 1);
  750. sapi_stack_init_ex(&conf->logger_handlers, 1);
  751. sapi_stack_init_ex(&conf->post_read_handlers, 1);
  752. register_cleanup(p, (void *) conf, (void (*)(void *)) php_destroy_per_dir_info, (void (*)(void *)) php_destroy_per_dir_info);
  753. return conf;
  754. }
  755. /* }}} */
  756. /* {{{ php_merge_dir
  757. */
  758. static void *php_merge_dir(pool *p, void *basev, void *addv)
  759. {
  760. php_per_dir_config *a = (php_per_dir_config *) addv;
  761. php_per_dir_config *b = (php_per_dir_config *) basev;
  762. /* This function *must* return addv, and not modify basev */
  763. zend_hash_merge_ex((HashTable *) a->ini_settings, (HashTable *) b->ini_settings, (copy_ctor_func_t) copy_per_dir_entry, sizeof(php_per_dir_entry), (merge_checker_func_t) should_overwrite_per_dir_entry, NULL);
  764. a->headers_handlers = (a->headers_handlers.top)?a->headers_handlers:b->headers_handlers;
  765. a->auth_handlers = (a->auth_handlers.top)?a->auth_handlers:b->auth_handlers;
  766. a->access_handlers = (a->access_handlers.top)?a->access_handlers:b->access_handlers;
  767. a->type_handlers = (a->type_handlers.top)?a->type_handlers:b->type_handlers;
  768. a->fixup_handlers = (a->fixup_handlers.top)?a->fixup_handlers:b->fixup_handlers;
  769. a->logger_handlers = (a->logger_handlers.top)?a->logger_handlers:b->logger_handlers;
  770. a->post_read_handlers = (a->post_read_handlers.top)?a->post_read_handlers:b->post_read_handlers;
  771. a->response_handlers = (a->response_handlers.top)?a->response_handlers:b->response_handlers;
  772. return a;
  773. }
  774. /* }}} */
  775. /* {{{ php_apache_value_handler_ex
  776. */
  777. static CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  778. {
  779. php_per_dir_entry per_dir_entry;
  780. if (!apache_php_initialized) {
  781. apache_php_initialized = 1;
  782. #ifdef ZTS
  783. tsrm_startup(1, 1, 0, NULL);
  784. #endif
  785. sapi_startup(&apache_sapi_module);
  786. php_apache_startup(&apache_sapi_module);
  787. }
  788. per_dir_entry.type = mode;
  789. if (strcasecmp(arg2, "none") == 0) {
  790. arg2 = "";
  791. }
  792. per_dir_entry.key_length = strlen(arg1);
  793. per_dir_entry.value_length = strlen(arg2);
  794. per_dir_entry.key = (char *) malloc(per_dir_entry.key_length+1);
  795. memcpy(per_dir_entry.key, arg1, per_dir_entry.key_length);
  796. per_dir_entry.key[per_dir_entry.key_length] = 0;
  797. per_dir_entry.value = (char *) malloc(per_dir_entry.value_length+1);
  798. memcpy(per_dir_entry.value, arg2, per_dir_entry.value_length);
  799. per_dir_entry.value[per_dir_entry.value_length] = 0;
  800. zend_hash_update(conf, per_dir_entry.key, per_dir_entry.key_length, &per_dir_entry, sizeof(php_per_dir_entry), NULL);
  801. return NULL;
  802. }
  803. /* }}} */
  804. static CONST_PREFIX char *php_set_server_handler(server_rec *s, char *arg1, long handler_stage, long handler_type)
  805. {
  806. php_per_server_config *conf;
  807. php_handler *handler;
  808. handler = (php_handler *) malloc(sizeof(php_handler));
  809. handler->type = handler_type;
  810. handler->stage = handler_stage;
  811. handler->name = strdup(arg1);
  812. conf = get_module_config(s->module_config, &php5_module);
  813. switch(handler_stage) {
  814. case AP_URI_TRANS:
  815. sapi_stack_push(&conf->uri_handlers, handler);
  816. break;
  817. default:
  818. sapi_stack_push(&conf->requires, handler);
  819. break;
  820. }
  821. return NULL;
  822. }
  823. static CONST_PREFIX char *php_set_dir_handler(php_per_dir_config *conf, char *arg1, long handler_stage, long handler_type)
  824. {
  825. php_handler *handler;
  826. handler = (php_handler *) malloc(sizeof(php_handler));
  827. handler->type = handler_type;
  828. handler->stage = handler_stage;
  829. handler->name = strdup(arg1);
  830. switch(handler_stage) {
  831. case AP_POST_READ:
  832. sapi_stack_push(&conf->post_read_handlers, handler);
  833. break;
  834. case AP_HEADER_PARSE:
  835. sapi_stack_push(&conf->headers_handlers, handler);
  836. break;
  837. case AP_ACCESS_CONTROL:
  838. sapi_stack_push(&conf->access_handlers, handler);
  839. break;
  840. case AP_AUTHENTICATION:
  841. sapi_stack_push(&conf->auth_handlers, handler);
  842. break;
  843. case AP_AUTHORIZATION:
  844. break;
  845. case AP_TYPE_CHECKING:
  846. sapi_stack_push(&conf->type_handlers, handler);
  847. break;
  848. case AP_FIXUP:
  849. sapi_stack_push(&conf->fixup_handlers, handler);
  850. break;
  851. case AP_RESPONSE:
  852. sapi_stack_push(&conf->response_handlers, handler);
  853. break;
  854. case AP_LOGGING:
  855. sapi_stack_push(&conf->logger_handlers, handler);
  856. break;
  857. default:
  858. break;
  859. }
  860. return NULL;
  861. }
  862. /* {{{ php_set_uri_handler
  863. */
  864. static CONST_PREFIX char *php_set_uri_handler(cmd_parms *cmd, void *dummy, char *arg1)
  865. {
  866. return php_set_server_handler(cmd->server, arg1, AP_URI_TRANS, AP_HANDLER_TYPE_FILE);
  867. }
  868. /* }}} */
  869. /* {{{ php_set_uri_handler_code */
  870. static CONST_PREFIX char *php_set_uri_handler_code(cmd_parms *cmd, void *dummy, char *arg1)
  871. {
  872. return php_set_server_handler(cmd->server, arg1, AP_URI_TRANS, AP_HANDLER_TYPE_METHOD);
  873. }
  874. /* }}} */
  875. /* {{{ php_set_header_handler
  876. */
  877. static CONST_PREFIX char *php_set_header_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  878. {
  879. return php_set_dir_handler(conf, arg1, AP_HEADER_PARSE, AP_HANDLER_TYPE_FILE);
  880. }
  881. static CONST_PREFIX char *php_set_header_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  882. {
  883. return php_set_dir_handler(conf, arg1, AP_HEADER_PARSE, AP_HANDLER_TYPE_METHOD);
  884. }
  885. /* }}} */
  886. /* {{{ php_set_auth_handler
  887. */
  888. static CONST_PREFIX char *php_set_auth_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  889. {
  890. return php_set_dir_handler(conf, arg1, AP_AUTHENTICATION, AP_HANDLER_TYPE_FILE);
  891. }
  892. static CONST_PREFIX char *php_set_auth_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  893. {
  894. return php_set_dir_handler(conf, arg1, AP_AUTHENTICATION, AP_HANDLER_TYPE_METHOD);
  895. }
  896. /* }}} */
  897. /* {{{ php_set_access_handler
  898. */
  899. static CONST_PREFIX char *php_set_access_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  900. {
  901. return php_set_dir_handler(conf, arg1, AP_ACCESS_CONTROL, AP_HANDLER_TYPE_FILE);
  902. }
  903. static CONST_PREFIX char *php_set_access_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  904. {
  905. return php_set_dir_handler(conf, arg1, AP_ACCESS_CONTROL, AP_HANDLER_TYPE_METHOD);
  906. }
  907. /* }}} */
  908. /* {{{ php_set_type_handler
  909. */
  910. static CONST_PREFIX char *php_set_type_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  911. {
  912. return php_set_dir_handler(conf, arg1, AP_TYPE_CHECKING, AP_HANDLER_TYPE_FILE);
  913. }
  914. static CONST_PREFIX char *php_set_type_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  915. {
  916. return php_set_dir_handler(conf, arg1, AP_TYPE_CHECKING, AP_HANDLER_TYPE_METHOD);
  917. }
  918. /* }}} */
  919. /* {{{ php_set_fixup_handler
  920. */
  921. static CONST_PREFIX char *php_set_fixup_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  922. {
  923. return php_set_dir_handler(conf, arg1, AP_FIXUP, AP_HANDLER_TYPE_FILE);
  924. }
  925. static CONST_PREFIX char *php_set_fixup_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  926. {
  927. return php_set_dir_handler(conf, arg1, AP_FIXUP, AP_HANDLER_TYPE_METHOD);
  928. }
  929. /* }}} */
  930. /* {{{ php_set_logger_handler
  931. */
  932. static CONST_PREFIX char *php_set_logger_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  933. {
  934. return php_set_dir_handler(conf, arg1, AP_LOGGING, AP_HANDLER_TYPE_FILE);
  935. }
  936. static CONST_PREFIX char *php_set_logger_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  937. {
  938. return php_set_dir_handler(conf, arg1, AP_LOGGING, AP_HANDLER_TYPE_METHOD);
  939. }
  940. /* }}} */
  941. /* {{{ php_set_post_read_handler
  942. */
  943. static CONST_PREFIX char *php_set_post_read_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  944. {
  945. return php_set_dir_handler(conf, arg1, AP_POST_READ, AP_HANDLER_TYPE_FILE);
  946. }
  947. static CONST_PREFIX char *php_set_post_read_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  948. {
  949. return php_set_dir_handler(conf, arg1, AP_POST_READ, AP_HANDLER_TYPE_METHOD);
  950. }
  951. /* }}} */
  952. /* {{{ php_set_require
  953. */
  954. static CONST_PREFIX char *php_set_require(cmd_parms *cmd, void *dummy, char *arg1)
  955. {
  956. return php_set_server_handler(cmd->server, arg1, 0, AP_HANDLER_TYPE_FILE);
  957. }
  958. /* }}} */
  959. /* {{{ php_set_response_handler
  960. */
  961. static CONST_PREFIX char *php_set_response_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  962. {
  963. return php_set_dir_handler(conf, arg1, AP_RESPONSE, AP_HANDLER_TYPE_FILE);
  964. }
  965. static CONST_PREFIX char *php_set_response_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  966. {
  967. return php_set_dir_handler(conf, arg1, AP_RESPONSE, AP_HANDLER_TYPE_METHOD);
  968. }
  969. /* }}} */
  970. /* {{{ php_apache_value_handler
  971. */
  972. static CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  973. {
  974. return php_apache_value_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_PERDIR);
  975. }
  976. /* }}} */
  977. /* {{{ php_apache_admin_value_handler
  978. */
  979. static CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  980. {
  981. return php_apache_value_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_SYSTEM);
  982. }
  983. /* }}} */
  984. /* {{{ php_apache_flag_handler_ex
  985. */
  986. static CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  987. {
  988. char bool_val[2];
  989. if (!strcasecmp(arg2, "On")) {
  990. bool_val[0] = '1';
  991. } else {
  992. bool_val[0] = '0';
  993. }
  994. bool_val[1] = 0;
  995. return php_apache_value_handler_ex(cmd, conf, arg1, bool_val, mode);
  996. }
  997. /* }}} */
  998. /* {{{ php_apache_flag_handler
  999. */
  1000. static CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  1001. {
  1002. return php_apache_flag_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_PERDIR);
  1003. }
  1004. /* }}} */
  1005. /* {{{ php_apache_admin_flag_handler
  1006. */
  1007. static CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  1008. {
  1009. return php_apache_flag_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_SYSTEM);
  1010. }
  1011. /* }}} */
  1012. /* {{{ php_apache_phpini_set
  1013. */
  1014. static CONST_PREFIX char *php_apache_phpini_set(cmd_parms *cmd, HashTable *conf, char *arg)
  1015. {
  1016. if (apache_sapi_module.php_ini_path_override) {
  1017. return "Only first PHPINIDir directive honored per configuration tree - subsequent ones ignored";
  1018. }
  1019. apache_sapi_module.php_ini_path_override = ap_server_root_relative(cmd->pool, arg);
  1020. return NULL;
  1021. }
  1022. /* }}} */
  1023. /* {{{ int php_xbithack_handler(request_rec * r)
  1024. */
  1025. static int php_xbithack_handler(request_rec * r)
  1026. {
  1027. php_per_dir_config *conf;
  1028. TSRMLS_FETCH();
  1029. if (!(r->finfo.st_mode & S_IXUSR)) {
  1030. r->allowed |= (1 << METHODS) - 1;
  1031. return DECLINED;
  1032. }
  1033. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1034. if (conf) {
  1035. zend_hash_apply((HashTable *) conf->ini_settings, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  1036. }
  1037. if(!AP(xbithack)) {
  1038. r->allowed |= (1 << METHODS) - 1;
  1039. zend_try {
  1040. zend_ini_deactivate(TSRMLS_C);
  1041. } zend_end_try();
  1042. return DECLINED;
  1043. }
  1044. return send_parsed_php(r);
  1045. }
  1046. /* }}} */
  1047. /* {{{ apache_php_module_shutdown_wrapper
  1048. */
  1049. static void apache_php_module_shutdown_wrapper(void)
  1050. {
  1051. apache_php_initialized = 0;
  1052. apache_sapi_module.shutdown(&apache_sapi_module);
  1053. #if MODULE_MAGIC_NUMBER >= 19970728
  1054. /* This function is only called on server exit if the apache API
  1055. * child_exit handler exists, so shutdown globally
  1056. */
  1057. sapi_shutdown();
  1058. #endif
  1059. #ifdef ZTS
  1060. tsrm_shutdown();
  1061. #endif
  1062. }
  1063. /* }}} */
  1064. #if MODULE_MAGIC_NUMBER >= 19970728
  1065. /* {{{ php_child_exit_handler
  1066. */
  1067. static void php_child_exit_handler(server_rec *s, pool *p)
  1068. {
  1069. /* apache_php_initialized = 0; */
  1070. apache_sapi_module.shutdown(&apache_sapi_module);
  1071. #ifdef ZTS
  1072. tsrm_shutdown();
  1073. #endif
  1074. }
  1075. /* }}} */
  1076. #endif
  1077. /* {{{ void php_init_handler(server_rec *s, pool *p)
  1078. */
  1079. static void php_init_handler(server_rec *s, pool *p)
  1080. {
  1081. register_cleanup(p, NULL, (void (*)(void *))apache_php_module_shutdown_wrapper, (void (*)(void *))php_module_shutdown_for_exec);
  1082. if (!apache_php_initialized) {
  1083. apache_php_initialized = 1;
  1084. #ifdef ZTS
  1085. tsrm_startup(1, 1, 0, NULL);
  1086. #endif
  1087. sapi_startup(&apache_sapi_module);
  1088. php_apache_startup(&apache_sapi_module);
  1089. }
  1090. #if MODULE_MAGIC_NUMBER >= 19980527
  1091. {
  1092. TSRMLS_FETCH();
  1093. if (PG(expose_php)) {
  1094. ap_add_version_component("PHP/" PHP_VERSION);
  1095. }
  1096. }
  1097. #endif
  1098. }
  1099. /* }}} */
  1100. static int php_run_hook(php_handler *handler, request_rec *r)
  1101. {
  1102. zval *ret = NULL;
  1103. php_per_dir_config *conf;
  1104. TSRMLS_FETCH();
  1105. if(!AP(apache_config_loaded)) {
  1106. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1107. if (conf)
  1108. zend_hash_apply((HashTable *)conf->ini_settings, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  1109. AP(apache_config_loaded) = 1;
  1110. }
  1111. if (!handler->name) {
  1112. return DECLINED;
  1113. }
  1114. php_save_umask();
  1115. if (!AP(setup_env)) {
  1116. AP(setup_env) = 1;
  1117. add_common_vars(r);
  1118. add_cgi_vars(r);
  1119. }
  1120. SG(server_context) = r;
  1121. init_request_info(TSRMLS_C);
  1122. apache_php_module_hook(r, handler, &ret TSRMLS_CC);
  1123. php_restore_umask();
  1124. kill_timeout(r);
  1125. if (ret) {
  1126. convert_to_long(ret);
  1127. return Z_LVAL_P(ret);
  1128. }
  1129. return HTTP_INTERNAL_SERVER_ERROR;
  1130. }
  1131. static int php_uri_translation(request_rec *r)
  1132. {
  1133. php_per_server_config *conf;
  1134. TSRMLS_FETCH();
  1135. AP(current_hook) = AP_URI_TRANS;
  1136. conf = (php_per_server_config *) get_module_config(r->server->module_config, &php5_module);
  1137. return sapi_stack_apply_with_argument_stop_if_equals(&conf->uri_handlers,
  1138. ZEND_STACK_APPLY_BOTTOMUP,
  1139. (int (*)(void *element, void *)) php_run_hook, r, OK);
  1140. }
  1141. static int php_header_hook(request_rec *r)
  1142. {
  1143. php_per_dir_config *conf;
  1144. TSRMLS_FETCH();
  1145. AP(current_hook) = AP_HEADER_PARSE;
  1146. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1147. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->headers_handlers,
  1148. ZEND_STACK_APPLY_BOTTOMUP,
  1149. (int (*)(void *element, void *)) php_run_hook, r);
  1150. }
  1151. static int php_auth_hook(request_rec *r)
  1152. {
  1153. php_per_dir_config *conf;
  1154. TSRMLS_FETCH();
  1155. AP(current_hook) = AP_AUTHENTICATION;
  1156. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1157. return sapi_stack_apply_with_argument_stop_if_equals(&conf->auth_handlers,
  1158. ZEND_STACK_APPLY_BOTTOMUP,
  1159. (int (*)(void *element, void *)) php_run_hook, r, OK);
  1160. }
  1161. static int php_access_hook(request_rec *r)
  1162. {
  1163. php_per_dir_config *conf;
  1164. int status = DECLINED;
  1165. TSRMLS_FETCH();
  1166. AP(current_hook) = AP_ACCESS_CONTROL;
  1167. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1168. status = sapi_stack_apply_with_argument_stop_if_http_error(&conf->access_handlers,
  1169. ZEND_STACK_APPLY_BOTTOMUP,
  1170. (int (*)(void *element, void *)) php_run_hook, r);
  1171. return status;
  1172. }
  1173. static int php_type_hook(request_rec *r)
  1174. {
  1175. php_per_dir_config *conf;
  1176. TSRMLS_FETCH();
  1177. AP(current_hook) = AP_TYPE_CHECKING;
  1178. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1179. return sapi_stack_apply_with_argument_stop_if_equals(&conf->type_handlers,
  1180. ZEND_STACK_APPLY_BOTTOMUP,
  1181. (int (*)(void *element, void *)) php_run_hook,
  1182. r, OK);
  1183. }
  1184. static int php_fixup_hook(request_rec *r)
  1185. {
  1186. php_per_dir_config *conf;
  1187. TSRMLS_FETCH();
  1188. AP(current_hook) = AP_FIXUP;
  1189. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1190. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->fixup_handlers,
  1191. ZEND_STACK_APPLY_BOTTOMUP,
  1192. (int (*)(void *element, void *)) php_run_hook,
  1193. r);
  1194. }
  1195. static int php_logger_hook(request_rec *r)
  1196. {
  1197. php_per_dir_config *conf;
  1198. TSRMLS_FETCH();
  1199. AP(current_hook) = AP_LOGGING;
  1200. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1201. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->logger_handlers,
  1202. ZEND_STACK_APPLY_BOTTOMUP,
  1203. (int (*)(void *element, void *)) php_run_hook,
  1204. r);
  1205. }
  1206. static int php_post_read_hook(request_rec *r)
  1207. {
  1208. php_per_dir_config *conf;
  1209. php_per_server_config *svr;
  1210. TSRMLS_FETCH();
  1211. AP(current_hook) = AP_POST_READ;
  1212. svr = get_module_config(r->server->module_config, &php5_module);
  1213. if(ap_is_initial_req(r)) {
  1214. sapi_stack_apply_with_argument_all(&svr->requires, ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_run_hook, r);
  1215. }
  1216. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1217. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->post_read_handlers,
  1218. ZEND_STACK_APPLY_BOTTOMUP,
  1219. (int (*)(void *element, void *)) php_run_hook, r);
  1220. }
  1221. static int php_response_handler(request_rec *r)
  1222. {
  1223. php_per_dir_config *conf;
  1224. TSRMLS_FETCH();
  1225. AP(current_hook) = AP_RESPONSE;
  1226. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1227. return sapi_stack_apply_with_argument_all(&conf->response_handlers, ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_run_hook, r);
  1228. }
  1229. /* {{{ handler_rec php_handlers[]
  1230. */
  1231. handler_rec php_handlers[] =
  1232. {
  1233. {"application/x-httpd-php", send_parsed_php},
  1234. {"application/x-httpd-php-source", send_parsed_php_source},
  1235. {"text/html", php_xbithack_handler},
  1236. {"php-script", php_response_handler},
  1237. {NULL}
  1238. };
  1239. /* }}} */
  1240. /* {{{ command_rec php_commands[]
  1241. */
  1242. command_rec php_commands[] =
  1243. {
  1244. {"php_value", php_apache_value_handler, NULL, OR_OPTIONS, TAKE2, "PHP Value Modifier"},
  1245. {"phpUriHandler", php_set_uri_handler, NULL, RSRC_CONF, TAKE1, "PHP Value Modifier"},
  1246. {"phpUriHandlerMethod", php_set_uri_handler_code, NULL, RSRC_CONF, TAKE1, "PHP Value Modifier"},
  1247. #if MODULE_MAGIC_NUMBER >= 19970103
  1248. {"phpHeaderHandler", php_set_header_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1249. {"phpHeaderHandlerMethod", php_set_header_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1250. #endif
  1251. {"phpAuthHandler", php_set_auth_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1252. {"phpAuthHandlerMethod", php_set_auth_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1253. {"phpAccessHandler", php_set_access_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1254. {"phpAccessHandlerMethod", php_set_access_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1255. {"phpTypeHandler", php_set_type_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1256. {"phpTypeHandlerMethod", php_set_type_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1257. {"phpFixupHandler", php_set_fixup_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1258. {"phpFixupHandlerMethod", php_set_fixup_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1259. {"phpLoggerHandler", php_set_logger_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1260. {"phpLoggerHandlerMethod", php_set_logger_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1261. #if MODULE_MAGIC_NUMBER >= 19970902
  1262. {"phpPostReadHandler", php_set_post_read_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1263. {"phpPostReadHandlerMethod", php_set_post_read_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1264. {"phpRequire", php_set_require, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1265. {"phpResponseHandler", php_set_response_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1266. {"phpResponseHandlerMethod", php_set_response_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1267. #endif
  1268. {"php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, TAKE2, "PHP Flag Modifier"},
  1269. {"php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Value Modifier (Admin)"},
  1270. {"php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Flag Modifier (Admin)"},
  1271. {"PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, TAKE1, "Directory containing the php.ini file"},
  1272. {NULL}
  1273. };
  1274. /* }}} */
  1275. /* {{{ module MODULE_VAR_EXPORT php5_module
  1276. */
  1277. module MODULE_VAR_EXPORT php5_module =
  1278. {
  1279. STANDARD_MODULE_STUFF,
  1280. php_init_handler, /* initializer */
  1281. php_create_dir, /* per-directory config creator */
  1282. php_merge_dir, /* dir merger */
  1283. php_create_server, /* per-server config creator */
  1284. NULL, /* merge server config */
  1285. php_commands, /* command table */
  1286. php_handlers, /* handlers */
  1287. php_uri_translation, /* filename translation */
  1288. NULL, /* check_user_id */
  1289. php_auth_hook, /* check auth */
  1290. php_access_hook, /* check access */
  1291. php_type_hook, /* type_checker */
  1292. php_fixup_hook, /* fixups */
  1293. php_logger_hook /* logger */
  1294. #if MODULE_MAGIC_NUMBER >= 19970103
  1295. , php_header_hook /* header parser */
  1296. #endif
  1297. #if MODULE_MAGIC_NUMBER >= 19970719
  1298. , NULL /* child_init */
  1299. #endif
  1300. #if MODULE_MAGIC_NUMBER >= 19970728
  1301. , php_child_exit_handler /* child_exit */
  1302. #endif
  1303. #if MODULE_MAGIC_NUMBER >= 19970902
  1304. , php_post_read_hook /* post read-request */
  1305. #endif
  1306. };
  1307. /* }}} */
  1308. /*
  1309. * Local variables:
  1310. * tab-width: 4
  1311. * c-basic-offset: 4
  1312. * End:
  1313. * vim600: sw=4 ts=4 fdm=marker
  1314. * vim<600: sw=4 ts=4
  1315. */