xref: /PHP-5.3/sapi/apache_hooks/sapi_apache.c (revision a2045ff3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 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: Rasmus Lerdorf <rasmus@php.net>                             |
16    | (with helpful hints from Dean Gaudet <dgaudet@arctic.org>            |
17    | PHP 4.0 patches by:                                                  |
18    | Zeev Suraski <zeev@zend.com>                                         |
19    | Stig Bakken <ssb@php.net>                                            |
20    +----------------------------------------------------------------------+
21  */
22 /* $Id$ */
23 
24 #include "php_apache_http.h"
25 
26 /* {{{ apache_php_module_main
27  */
apache_php_module_main(request_rec * r,int display_source_mode TSRMLS_DC)28 int apache_php_module_main(request_rec *r, int display_source_mode TSRMLS_DC)
29 {
30 	zend_file_handle file_handle;
31 
32 	if (php_request_startup(TSRMLS_C) == FAILURE) {
33 		return FAILURE;
34 	}
35 	/* sending a file handle to another dll is not working
36 	   so let zend open it. */
37 
38 	if (display_source_mode) {
39 		zend_syntax_highlighter_ini syntax_highlighter_ini;
40 
41 		php_get_highlight_struct(&syntax_highlighter_ini);
42 		if (highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini TSRMLS_CC)){
43 			return OK;
44 		} else {
45 			return NOT_FOUND;
46 		}
47 	} else {
48 		file_handle.type = ZEND_HANDLE_FILENAME;
49 		file_handle.handle.fd = 0;
50 		file_handle.filename = SG(request_info).path_translated;
51 		file_handle.opened_path = NULL;
52 		file_handle.free_filename = 0;
53 		(void) php_execute_script(&file_handle TSRMLS_CC);
54 	}
55 	AP(in_request) = 0;
56 
57 	return (OK);
58 }
59 /* }}} */
60 
61 /* {{{ apache_php_module_hook
62  */
apache_php_module_hook(request_rec * r,php_handler * handler,zval ** ret TSRMLS_DC)63 int apache_php_module_hook(request_rec *r, php_handler *handler, zval **ret TSRMLS_DC)
64 {
65 	zend_file_handle file_handle;
66 	zval *req;
67     char *tmp;
68 
69 #if PHP_SIGCHILD
70 	signal(SIGCHLD, sigchld_handler);
71 #endif
72     if(AP(current_hook) == AP_RESPONSE) {
73         if (php_request_startup_for_hook(TSRMLS_C) == FAILURE)
74             return FAILURE;
75     }
76     else {
77         if (php_request_startup_for_hook(TSRMLS_C) == FAILURE)
78             return FAILURE;
79     }
80 
81 	req = php_apache_request_new(r);
82     if(PG(register_globals)) {
83         php_register_variable_ex("request", req, NULL TSRMLS_CC);
84     }
85     else {
86         php_register_variable_ex("request", req, PG(http_globals)[TRACK_VARS_SERVER] TSRMLS_CC);
87     }
88     switch(handler->type) {
89         case AP_HANDLER_TYPE_FILE:
90             php_register_variable("PHP_SELF_HOOK", handler->name, PG(http_globals)[TRACK_VARS_SERVER] TSRMLS_CC);
91 	        memset(&file_handle, 0, sizeof(file_handle));
92 	        file_handle.type = ZEND_HANDLE_FILENAME;
93 	        file_handle.filename = handler->name;
94 	        (void) php_execute_simple_script(&file_handle, ret TSRMLS_CC);
95             break;
96         case AP_HANDLER_TYPE_METHOD:
97             if( (tmp = strstr(handler->name, "::")) != NULL &&  *(tmp+2) != '\0' ) {
98                 zval *class;
99                 zval *method;
100                 *tmp = '\0';
101                 ALLOC_ZVAL(class);
102                 ZVAL_STRING(class, handler->name, 1);
103                 ALLOC_ZVAL(method);
104                 ZVAL_STRING(method, tmp +2, 1);
105                 *tmp = ':';
106                 call_user_function_ex(EG(function_table), &class, method, ret, 0, NULL, 0, NULL TSRMLS_CC);
107                 zval_dtor(class);
108                 zval_dtor(method);
109             }
110             else {
111                 php_error(E_ERROR, "Unable to call %s - not a Class::Method\n", handler->name);
112                 /* not a class::method */
113             }
114             break;
115         default:
116             /* not a valid type */
117             assert(0);
118             break;
119     }
120 	zval_dtor(req);
121 	AP(in_request) = 0;
122 
123 	return OK;
124 }
125 
126 /* }}} */
127 
128 /*
129  * Local variables:
130  * tab-width: 4
131  * c-basic-offset: 4
132  * End:
133  * vim600: sw=4 ts=4 fdm=marker
134  * vim<600: sw=4 ts=4
135  */
136