README 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. This is very beta documentation. Clearly better stuff can and will follow.
  2. INTRO:
  3. apache_hooks is a full super-set enhancement of the apache 1.3 sapi that allows for
  4. php code to be run on the apache request object at every stage of the apache
  5. request. It supports all of the apache 1.3 sapi commands and configurations, and
  6. additionally supports the following httpd.conf directives:
  7. HTTPD.CONF DIRECTIEVS:
  8. phpRequire /path/to/file = requires a file at the beginning of an
  9. initial apache request
  10. phpUriHandler /path/to/file = registers a hook that will run the
  11. specified file at the uri translation stage of the apache request
  12. phpUriHandler Class::Method = registers a hook to run Class::Method at
  13. the uri translation stage of the apache request
  14. phpPostReadHandler /path/to/file = hook for post-read phase
  15. phpPostReadHandlerMethod Class::Method
  16. phpHeaderHandler = hook for header parsing phase
  17. phpHeaderHandlerMethod
  18. phpAuthHandler = hook for authentication phase
  19. phpAuthHandlerMethod
  20. phpAccessHandler = hook for access control phase
  21. phpAccessHandlerMethod
  22. phpTypeHandler = hook for Type Checking phase
  23. phpTypeHandlerMethod
  24. phpFixupHandler = hook for 'fixup' phase
  25. phpFixupHandlerMethod
  26. phpLoggerHandler = hook for logging phase
  27. phpLoggerHandlerMethod
  28. AddHandler php-script = set's up a special type handler
  29. phpResponseHandler /path/to/file = sets file to be called to handle
  30. response phase
  31. phpResponseHandlerMethod Class::Method
  32. All handlers may be stacked, i.e. you can list multiple handler directives
  33. in a single scope and they will be run in order.
  34. EXAMPLES:
  35. So, to set up a 'hello world' location handler (so that any request to
  36. /hello/* returns hello world) you can:
  37. phpRequire /tmp/setup.php
  38. <Location /hello>
  39. AddHandler php-script
  40. phpResponseHandlerMethod Hello::World
  41. </Location>
  42. with
  43. #/tmp/setup.php
  44. <?
  45. class Hello {
  46. function World() {
  47. global $request;
  48. $request->send_http_header();
  49. echo "Hello World";
  50. }
  51. }
  52. ?>
  53. $request is the apache request. It is instantiated at all stages
  54. automatically. The methods of that class are:
  55. getallheaders
  56. args
  57. boundary
  58. content_encoding
  59. content_type
  60. filename
  61. handler
  62. hostname
  63. method
  64. path_info
  65. protocol
  66. status_line
  67. the_request
  68. unparsed_uri
  69. uri
  70. allowed
  71. bytes_sent
  72. chunked
  73. content_length
  74. header_only
  75. method_number
  76. mtime
  77. no_cache
  78. no_local_copy
  79. proto_num
  80. proxyreq
  81. read_body
  82. remaining
  83. request_time
  84. status
  85. headers_in
  86. headers_out
  87. err_headers_out
  88. auth_name
  89. auth_type
  90. basic_auth_pw
  91. discard_request_body
  92. is_initial_req
  93. meets_conditions
  94. remote_host
  95. satisfies
  96. server_port
  97. set_etag
  98. set_last_modified
  99. some_auth_required
  100. update_mtime
  101. send_http_header
  102. basic_http_header
  103. send_header_field
  104. send_http_trace
  105. send_http_options
  106. send_error_response
  107. set_content_length
  108. set_keepalive
  109. rputs
  110. log_error
  111. lookup_uri
  112. lookup_file
  113. method_uri
  114. run
  115. internal_redirect
  116. These all wrap the ap_* apache EXPORT_API functions using the same
  117. semantics (and are also the same as the Apache::Request methods in
  118. mod_perl if you are familiar with that)
  119. So, a uri handler to redirect all non-local traffic to /404.php (an
  120. error page) would be
  121. phpUriHandler /tmp/uri.php
  122. #/tmp/uri.php
  123. <?
  124. if($REMOTE_ADDR != '127.0.0.1') {
  125. $request->uri('/404.php');
  126. }
  127. return OK;
  128. ?>
  129. It's important to note that since this is called from the uri
  130. translations phase, this validation is performed for every request to
  131. the server, not just for php pages.
  132. Also, scope is shared between all the hooks. So in the above, we could
  133. merge the two and do something like:
  134. #/tmp/uri.php
  135. <?
  136. if($REMOTE_ADDR != '127.0.0.1') {
  137. $whoami = 'Stranger';
  138. }
  139. else {
  140. $whoami = 'Friend';
  141. }
  142. return DECLINED; # because we're not redirecting, just messing around
  143. ?>
  144. and then:
  145. #/tmp/setup.php
  146. <?
  147. class Hello {
  148. function World() {
  149. global $request;
  150. global $whoami;
  151. $request->send_http_header();
  152. echo "Hello $whoami";
  153. }
  154. }
  155. ?>
  156. These variables are also in the same scope as a script if your script is
  157. being handled by the standard application/x-httpd-php handler.
  158. This allows you to make decisions and pass data between your handlers
  159. and scripts at all stages.
  160. The above are clearly trite examples, but hopefully give you a starting
  161. point.
  162. One note: all handlers can be validly re-entered 'in sub-requests'.
  163. For this reason you should not define functions/classes here without
  164. anti-redefinition guards (I would just recommend putting them in an
  165. include and using include_one). This is not true for phpRequire, which
  166. is only entered once, at the main request, and so it is safe to make
  167. function/class declarations there (in fact that's what it's for).
  168. Hope that helps!