output.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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: Zeev Suraski <zeev@php.net> |
  16. | Thies C. Arntzen <thies@thieso.net> |
  17. | Marcus Boerger <helly@php.net> |
  18. | New API: Michael Wallner <mike@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #ifndef PHP_OUTPUT_DEBUG
  22. # define PHP_OUTPUT_DEBUG 0
  23. #endif
  24. #ifndef PHP_OUTPUT_NOINLINE
  25. # define PHP_OUTPUT_NOINLINE 0
  26. #endif
  27. #include "php.h"
  28. #include "ext/standard/head.h"
  29. #include "ext/standard/url_scanner_ex.h"
  30. #include "SAPI.h"
  31. #include "zend_stack.h"
  32. #include "php_output.h"
  33. PHPAPI ZEND_DECLARE_MODULE_GLOBALS(output);
  34. const char php_output_default_handler_name[sizeof("default output handler")] = "default output handler";
  35. const char php_output_devnull_handler_name[sizeof("null output handler")] = "null output handler";
  36. #if PHP_OUTPUT_NOINLINE || PHP_OUTPUT_DEBUG
  37. # undef inline
  38. # define inline
  39. #endif
  40. /* {{{ aliases, conflict and reverse conflict hash tables */
  41. static HashTable php_output_handler_aliases;
  42. static HashTable php_output_handler_conflicts;
  43. static HashTable php_output_handler_reverse_conflicts;
  44. /* }}} */
  45. /* {{{ forward declarations */
  46. static inline int php_output_lock_error(int op);
  47. static inline void php_output_op(int op, const char *str, size_t len);
  48. static inline php_output_handler *php_output_handler_init(zend_string *name, size_t chunk_size, int flags);
  49. static inline php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context);
  50. static inline int php_output_handler_append(php_output_handler *handler, const php_output_buffer *buf);
  51. static inline zval *php_output_handler_status(php_output_handler *handler, zval *entry);
  52. static inline php_output_context *php_output_context_init(php_output_context *context, int op);
  53. static inline void php_output_context_reset(php_output_context *context);
  54. static inline void php_output_context_swap(php_output_context *context);
  55. static inline void php_output_context_dtor(php_output_context *context);
  56. static inline int php_output_stack_pop(int flags);
  57. static int php_output_stack_apply_op(void *h, void *c);
  58. static int php_output_stack_apply_clean(void *h, void *c);
  59. static int php_output_stack_apply_list(void *h, void *z);
  60. static int php_output_stack_apply_status(void *h, void *z);
  61. static int php_output_handler_compat_func(void **handler_context, php_output_context *output_context);
  62. static int php_output_handler_default_func(void **handler_context, php_output_context *output_context);
  63. static int php_output_handler_devnull_func(void **handler_context, php_output_context *output_context);
  64. /* }}} */
  65. /* {{{ static void php_output_init_globals(zend_output_globals *G)
  66. * Initialize the module globals on MINIT */
  67. static inline void php_output_init_globals(zend_output_globals *G)
  68. {
  69. ZEND_TSRMLS_CACHE_UPDATE();
  70. memset(G, 0, sizeof(*G));
  71. }
  72. /* }}} */
  73. /* {{{ stderr/stdout writer if not PHP_OUTPUT_ACTIVATED */
  74. static size_t php_output_stdout(const char *str, size_t str_len)
  75. {
  76. fwrite(str, 1, str_len, stdout);
  77. return str_len;
  78. }
  79. static size_t php_output_stderr(const char *str, size_t str_len)
  80. {
  81. fwrite(str, 1, str_len, stderr);
  82. /* See http://support.microsoft.com/kb/190351 */
  83. #ifdef PHP_WIN32
  84. fflush(stderr);
  85. #endif
  86. return str_len;
  87. }
  88. static size_t (*php_output_direct)(const char *str, size_t str_len) = php_output_stderr;
  89. /* }}} */
  90. /* {{{ void php_output_header(void) */
  91. static void php_output_header(void)
  92. {
  93. if (!SG(headers_sent)) {
  94. if (!OG(output_start_filename)) {
  95. if (zend_is_compiling()) {
  96. OG(output_start_filename) = ZSTR_VAL(zend_get_compiled_filename());
  97. OG(output_start_lineno) = zend_get_compiled_lineno();
  98. } else if (zend_is_executing()) {
  99. OG(output_start_filename) = zend_get_executed_filename();
  100. OG(output_start_lineno) = zend_get_executed_lineno();
  101. }
  102. #if PHP_OUTPUT_DEBUG
  103. fprintf(stderr, "!!! output started at: %s (%d)\n", OG(output_start_filename), OG(output_start_lineno));
  104. #endif
  105. }
  106. if (!php_header()) {
  107. OG(flags) |= PHP_OUTPUT_DISABLED;
  108. }
  109. }
  110. }
  111. /* }}} */
  112. static void reverse_conflict_dtor(zval *zv)
  113. {
  114. HashTable *ht = Z_PTR_P(zv);
  115. zend_hash_destroy(ht);
  116. }
  117. /* {{{ void php_output_startup(void)
  118. * Set up module globals and initalize the conflict and reverse conflict hash tables */
  119. PHPAPI void php_output_startup(void)
  120. {
  121. ZEND_INIT_MODULE_GLOBALS(output, php_output_init_globals, NULL);
  122. zend_hash_init(&php_output_handler_aliases, 8, NULL, NULL, 1);
  123. zend_hash_init(&php_output_handler_conflicts, 8, NULL, NULL, 1);
  124. zend_hash_init(&php_output_handler_reverse_conflicts, 8, NULL, reverse_conflict_dtor, 1);
  125. php_output_direct = php_output_stdout;
  126. }
  127. /* }}} */
  128. /* {{{ void php_output_shutdown(void)
  129. * Destroy module globals and the conflict and reverse conflict hash tables */
  130. PHPAPI void php_output_shutdown(void)
  131. {
  132. php_output_direct = php_output_stderr;
  133. zend_hash_destroy(&php_output_handler_aliases);
  134. zend_hash_destroy(&php_output_handler_conflicts);
  135. zend_hash_destroy(&php_output_handler_reverse_conflicts);
  136. }
  137. /* }}} */
  138. /* {{{ SUCCESS|FAILURE php_output_activate(void)
  139. * Reset output globals and setup the output handler stack */
  140. PHPAPI int php_output_activate(void)
  141. {
  142. #ifdef ZTS
  143. memset((*((void ***) ZEND_TSRMLS_CACHE))[TSRM_UNSHUFFLE_RSRC_ID(output_globals_id)], 0, sizeof(zend_output_globals));
  144. #else
  145. memset(&output_globals, 0, sizeof(zend_output_globals));
  146. #endif
  147. zend_stack_init(&OG(handlers), sizeof(php_output_handler *));
  148. OG(flags) |= PHP_OUTPUT_ACTIVATED;
  149. return SUCCESS;
  150. }
  151. /* }}} */
  152. /* {{{ void php_output_deactivate(void)
  153. * Destroy the output handler stack */
  154. PHPAPI void php_output_deactivate(void)
  155. {
  156. php_output_handler **handler = NULL;
  157. if ((OG(flags) & PHP_OUTPUT_ACTIVATED)) {
  158. php_output_header();
  159. OG(flags) ^= PHP_OUTPUT_ACTIVATED;
  160. OG(active) = NULL;
  161. OG(running) = NULL;
  162. /* release all output handlers */
  163. if (OG(handlers).elements) {
  164. while ((handler = zend_stack_top(&OG(handlers)))) {
  165. php_output_handler_free(handler);
  166. zend_stack_del_top(&OG(handlers));
  167. }
  168. }
  169. zend_stack_destroy(&OG(handlers));
  170. }
  171. }
  172. /* }}} */
  173. /* {{{ void php_output_register_constants() */
  174. PHPAPI void php_output_register_constants(void)
  175. {
  176. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_START", PHP_OUTPUT_HANDLER_START, CONST_CS | CONST_PERSISTENT);
  177. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_WRITE", PHP_OUTPUT_HANDLER_WRITE, CONST_CS | CONST_PERSISTENT);
  178. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_FLUSH", PHP_OUTPUT_HANDLER_FLUSH, CONST_CS | CONST_PERSISTENT);
  179. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CLEAN", PHP_OUTPUT_HANDLER_CLEAN, CONST_CS | CONST_PERSISTENT);
  180. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_FINAL", PHP_OUTPUT_HANDLER_FINAL, CONST_CS | CONST_PERSISTENT);
  181. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CONT", PHP_OUTPUT_HANDLER_WRITE, CONST_CS | CONST_PERSISTENT);
  182. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_END", PHP_OUTPUT_HANDLER_FINAL, CONST_CS | CONST_PERSISTENT);
  183. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CLEANABLE", PHP_OUTPUT_HANDLER_CLEANABLE, CONST_CS | CONST_PERSISTENT);
  184. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_FLUSHABLE", PHP_OUTPUT_HANDLER_FLUSHABLE, CONST_CS | CONST_PERSISTENT);
  185. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_REMOVABLE", PHP_OUTPUT_HANDLER_REMOVABLE, CONST_CS | CONST_PERSISTENT);
  186. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_STDFLAGS", PHP_OUTPUT_HANDLER_STDFLAGS, CONST_CS | CONST_PERSISTENT);
  187. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_STARTED", PHP_OUTPUT_HANDLER_STARTED, CONST_CS | CONST_PERSISTENT);
  188. REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_DISABLED", PHP_OUTPUT_HANDLER_DISABLED, CONST_CS | CONST_PERSISTENT);
  189. }
  190. /* }}} */
  191. /* {{{ void php_output_set_status(int status)
  192. * Used by SAPIs to disable output */
  193. PHPAPI void php_output_set_status(int status)
  194. {
  195. OG(flags) = (OG(flags) & ~0xf) | (status & 0xf);
  196. }
  197. /* }}} */
  198. /* {{{ int php_output_get_status()
  199. * Get output control status */
  200. PHPAPI int php_output_get_status(void)
  201. {
  202. return (
  203. OG(flags)
  204. | (OG(active) ? PHP_OUTPUT_ACTIVE : 0)
  205. | (OG(running)? PHP_OUTPUT_LOCKED : 0)
  206. ) & 0xff;
  207. }
  208. /* }}} */
  209. /* {{{ int php_output_write_unbuffered(const char *str, size_t len)
  210. * Unbuffered write */
  211. PHPAPI size_t php_output_write_unbuffered(const char *str, size_t len)
  212. {
  213. if (OG(flags) & PHP_OUTPUT_ACTIVATED) {
  214. return sapi_module.ub_write(str, len);
  215. }
  216. return php_output_direct(str, len);
  217. }
  218. /* }}} */
  219. /* {{{ int php_output_write(const char *str, size_t len)
  220. * Buffered write */
  221. PHPAPI size_t php_output_write(const char *str, size_t len)
  222. {
  223. if (OG(flags) & PHP_OUTPUT_ACTIVATED) {
  224. php_output_op(PHP_OUTPUT_HANDLER_WRITE, str, len);
  225. return len;
  226. }
  227. if (OG(flags) & PHP_OUTPUT_DISABLED) {
  228. return 0;
  229. }
  230. return php_output_direct(str, len);
  231. }
  232. /* }}} */
  233. /* {{{ SUCCESS|FAILURE php_output_flush(void)
  234. * Flush the most recent output handlers buffer */
  235. PHPAPI int php_output_flush(void)
  236. {
  237. php_output_context context;
  238. if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_FLUSHABLE)) {
  239. php_output_context_init(&context, PHP_OUTPUT_HANDLER_FLUSH);
  240. php_output_handler_op(OG(active), &context);
  241. if (context.out.data && context.out.used) {
  242. zend_stack_del_top(&OG(handlers));
  243. php_output_write(context.out.data, context.out.used);
  244. zend_stack_push(&OG(handlers), &OG(active));
  245. }
  246. php_output_context_dtor(&context);
  247. return SUCCESS;
  248. }
  249. return FAILURE;
  250. }
  251. /* }}} */
  252. /* {{{ void php_output_flush_all()
  253. * Flush all output buffers subsequently */
  254. PHPAPI void php_output_flush_all(void)
  255. {
  256. if (OG(active)) {
  257. php_output_op(PHP_OUTPUT_HANDLER_FLUSH, NULL, 0);
  258. }
  259. }
  260. /* }}} */
  261. /* {{{ SUCCESS|FAILURE php_output_clean(void)
  262. * Cleans the most recent output handlers buffer if the handler is cleanable */
  263. PHPAPI int php_output_clean(void)
  264. {
  265. php_output_context context;
  266. if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_CLEANABLE)) {
  267. php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN);
  268. php_output_handler_op(OG(active), &context);
  269. php_output_context_dtor(&context);
  270. return SUCCESS;
  271. }
  272. return FAILURE;
  273. }
  274. /* }}} */
  275. /* {{{ void php_output_clean_all(void)
  276. * Cleans all output handler buffers, without regard whether the handler is cleanable */
  277. PHPAPI void php_output_clean_all(void)
  278. {
  279. php_output_context context;
  280. if (OG(active)) {
  281. php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN);
  282. zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_clean, &context);
  283. }
  284. }
  285. /* {{{ SUCCESS|FAILURE php_output_end(void)
  286. * Finalizes the most recent output handler at pops it off the stack if the handler is removable */
  287. PHPAPI int php_output_end(void)
  288. {
  289. if (php_output_stack_pop(PHP_OUTPUT_POP_TRY)) {
  290. return SUCCESS;
  291. }
  292. return FAILURE;
  293. }
  294. /* }}} */
  295. /* {{{ void php_output_end_all(void)
  296. * Finalizes all output handlers and ends output buffering without regard whether a handler is removable */
  297. PHPAPI void php_output_end_all(void)
  298. {
  299. while (OG(active) && php_output_stack_pop(PHP_OUTPUT_POP_FORCE));
  300. }
  301. /* }}} */
  302. /* {{{ SUCCESS|FAILURE php_output_discard(void)
  303. * Discards the most recent output handlers buffer and pops it off the stack if the handler is removable */
  304. PHPAPI int php_output_discard(void)
  305. {
  306. if (php_output_stack_pop(PHP_OUTPUT_POP_DISCARD|PHP_OUTPUT_POP_TRY)) {
  307. return SUCCESS;
  308. }
  309. return FAILURE;
  310. }
  311. /* }}} */
  312. /* {{{ void php_output_discard_all(void)
  313. * Discard all output handlers and buffers without regard whether a handler is removable */
  314. PHPAPI void php_output_discard_all(void)
  315. {
  316. while (OG(active)) {
  317. php_output_stack_pop(PHP_OUTPUT_POP_DISCARD|PHP_OUTPUT_POP_FORCE);
  318. }
  319. }
  320. /* }}} */
  321. /* {{{ int php_output_get_level(void)
  322. * Get output buffering level, ie. how many output handlers the stack contains */
  323. PHPAPI int php_output_get_level(void)
  324. {
  325. return OG(active) ? zend_stack_count(&OG(handlers)) : 0;
  326. }
  327. /* }}} */
  328. /* {{{ SUCCESS|FAILURE php_output_get_contents(zval *z)
  329. * Get the contents of the active output handlers buffer */
  330. PHPAPI int php_output_get_contents(zval *p)
  331. {
  332. if (OG(active)) {
  333. ZVAL_STRINGL(p, OG(active)->buffer.data, OG(active)->buffer.used);
  334. return SUCCESS;
  335. } else {
  336. ZVAL_NULL(p);
  337. return FAILURE;
  338. }
  339. }
  340. /* {{{ SUCCESS|FAILURE php_output_get_length(zval *z)
  341. * Get the length of the active output handlers buffer */
  342. PHPAPI int php_output_get_length(zval *p)
  343. {
  344. if (OG(active)) {
  345. ZVAL_LONG(p, OG(active)->buffer.used);
  346. return SUCCESS;
  347. } else {
  348. ZVAL_NULL(p);
  349. return FAILURE;
  350. }
  351. }
  352. /* }}} */
  353. /* {{{ php_output_handler* php_output_get_active_handler(void)
  354. * Get active output handler */
  355. PHPAPI php_output_handler* php_output_get_active_handler(void)
  356. {
  357. return OG(active);
  358. }
  359. /* }}} */
  360. /* {{{ SUCCESS|FAILURE php_output_handler_start_default(void)
  361. * Start a "default output handler" */
  362. PHPAPI int php_output_start_default(void)
  363. {
  364. php_output_handler *handler;
  365. handler = php_output_handler_create_internal(ZEND_STRL(php_output_default_handler_name), php_output_handler_default_func, 0, PHP_OUTPUT_HANDLER_STDFLAGS);
  366. if (SUCCESS == php_output_handler_start(handler)) {
  367. return SUCCESS;
  368. }
  369. php_output_handler_free(&handler);
  370. return FAILURE;
  371. }
  372. /* }}} */
  373. /* {{{ SUCCESS|FAILURE php_output_handler_start_devnull(void)
  374. * Start a "null output handler" */
  375. PHPAPI int php_output_start_devnull(void)
  376. {
  377. php_output_handler *handler;
  378. handler = php_output_handler_create_internal(ZEND_STRL(php_output_devnull_handler_name), php_output_handler_devnull_func, PHP_OUTPUT_HANDLER_DEFAULT_SIZE, 0);
  379. if (SUCCESS == php_output_handler_start(handler)) {
  380. return SUCCESS;
  381. }
  382. php_output_handler_free(&handler);
  383. return FAILURE;
  384. }
  385. /* }}} */
  386. /* {{{ SUCCESS|FAILURE php_output_start_user(zval *handler, size_t chunk_size, int flags)
  387. * Start a user level output handler */
  388. PHPAPI int php_output_start_user(zval *output_handler, size_t chunk_size, int flags)
  389. {
  390. php_output_handler *handler;
  391. if (output_handler) {
  392. handler = php_output_handler_create_user(output_handler, chunk_size, flags);
  393. } else {
  394. handler = php_output_handler_create_internal(ZEND_STRL(php_output_default_handler_name), php_output_handler_default_func, chunk_size, flags);
  395. }
  396. if (SUCCESS == php_output_handler_start(handler)) {
  397. return SUCCESS;
  398. }
  399. php_output_handler_free(&handler);
  400. return FAILURE;
  401. }
  402. /* }}} */
  403. /* {{{ SUCCESS|FAILURE php_output_start_internal(zval *name, php_output_handler_func_t handler, size_t chunk_size, int flags)
  404. * Start an internal output handler that does not have to maintain a non-global state */
  405. PHPAPI int php_output_start_internal(const char *name, size_t name_len, php_output_handler_func_t output_handler, size_t chunk_size, int flags)
  406. {
  407. php_output_handler *handler;
  408. handler = php_output_handler_create_internal(name, name_len, php_output_handler_compat_func, chunk_size, flags);
  409. php_output_handler_set_context(handler, output_handler, NULL);
  410. if (SUCCESS == php_output_handler_start(handler)) {
  411. return SUCCESS;
  412. }
  413. php_output_handler_free(&handler);
  414. return FAILURE;
  415. }
  416. /* }}} */
  417. /* {{{ php_output_handler *php_output_handler_create_user(zval *handler, size_t chunk_size, int flags)
  418. * Create a user level output handler */
  419. PHPAPI php_output_handler *php_output_handler_create_user(zval *output_handler, size_t chunk_size, int flags)
  420. {
  421. zend_string *handler_name = NULL;
  422. char *error = NULL;
  423. php_output_handler *handler = NULL;
  424. php_output_handler_alias_ctor_t alias = NULL;
  425. php_output_handler_user_func_t *user = NULL;
  426. switch (Z_TYPE_P(output_handler)) {
  427. case IS_NULL:
  428. handler = php_output_handler_create_internal(ZEND_STRL(php_output_default_handler_name), php_output_handler_default_func, chunk_size, flags);
  429. break;
  430. case IS_STRING:
  431. if (Z_STRLEN_P(output_handler) && (alias = php_output_handler_alias(Z_STRVAL_P(output_handler), Z_STRLEN_P(output_handler)))) {
  432. handler = alias(Z_STRVAL_P(output_handler), Z_STRLEN_P(output_handler), chunk_size, flags);
  433. break;
  434. }
  435. default:
  436. user = ecalloc(1, sizeof(php_output_handler_user_func_t));
  437. if (SUCCESS == zend_fcall_info_init(output_handler, 0, &user->fci, &user->fcc, &handler_name, &error)) {
  438. handler = php_output_handler_init(handler_name, chunk_size, (flags & ~0xf) | PHP_OUTPUT_HANDLER_USER);
  439. ZVAL_COPY(&user->zoh, output_handler);
  440. handler->func.user = user;
  441. } else {
  442. efree(user);
  443. }
  444. if (error) {
  445. php_error_docref("ref.outcontrol", E_WARNING, "%s", error);
  446. efree(error);
  447. }
  448. if (handler_name) {
  449. zend_string_release_ex(handler_name, 0);
  450. }
  451. }
  452. return handler;
  453. }
  454. /* }}} */
  455. /* {{{ php_output_handler *php_output_handler_create_internal(zval *name, php_output_handler_context_func_t handler, size_t chunk_size, int flags)
  456. * Create an internal output handler that can maintain a non-global state */
  457. PHPAPI php_output_handler *php_output_handler_create_internal(const char *name, size_t name_len, php_output_handler_context_func_t output_handler, size_t chunk_size, int flags)
  458. {
  459. php_output_handler *handler;
  460. zend_string *str = zend_string_init(name, name_len, 0);
  461. handler = php_output_handler_init(str, chunk_size, (flags & ~0xf) | PHP_OUTPUT_HANDLER_INTERNAL);
  462. handler->func.internal = output_handler;
  463. zend_string_release_ex(str, 0);
  464. return handler;
  465. }
  466. /* }}} */
  467. /* {{{ void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void*))
  468. * Set the context/state of an output handler. Calls the dtor of the previous context if there is one */
  469. PHPAPI void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void*))
  470. {
  471. if (handler->dtor && handler->opaq) {
  472. handler->dtor(handler->opaq);
  473. }
  474. handler->dtor = dtor;
  475. handler->opaq = opaq;
  476. }
  477. /* }}} */
  478. /* {{{ SUCCESS|FAILURE php_output_handler_start(php_output_handler *handler)
  479. * Starts the set up output handler and pushes it on top of the stack. Checks for any conflicts regarding the output handler to start */
  480. PHPAPI int php_output_handler_start(php_output_handler *handler)
  481. {
  482. HashTable *rconflicts;
  483. php_output_handler_conflict_check_t conflict;
  484. if (php_output_lock_error(PHP_OUTPUT_HANDLER_START) || !handler) {
  485. return FAILURE;
  486. }
  487. if (NULL != (conflict = zend_hash_find_ptr(&php_output_handler_conflicts, handler->name))) {
  488. if (SUCCESS != conflict(ZSTR_VAL(handler->name), ZSTR_LEN(handler->name))) {
  489. return FAILURE;
  490. }
  491. }
  492. if (NULL != (rconflicts = zend_hash_find_ptr(&php_output_handler_reverse_conflicts, handler->name))) {
  493. ZEND_HASH_FOREACH_PTR(rconflicts, conflict) {
  494. if (SUCCESS != conflict(ZSTR_VAL(handler->name), ZSTR_LEN(handler->name))) {
  495. return FAILURE;
  496. }
  497. } ZEND_HASH_FOREACH_END();
  498. }
  499. /* zend_stack_push returns stack level */
  500. handler->level = zend_stack_push(&OG(handlers), &handler);
  501. OG(active) = handler;
  502. return SUCCESS;
  503. }
  504. /* }}} */
  505. /* {{{ int php_output_handler_started(zval *name)
  506. * Check whether a certain output handler is in use */
  507. PHPAPI int php_output_handler_started(const char *name, size_t name_len)
  508. {
  509. php_output_handler **handlers;
  510. int i, count = php_output_get_level();
  511. if (count) {
  512. handlers = (php_output_handler **) zend_stack_base(&OG(handlers));
  513. for (i = 0; i < count; ++i) {
  514. if (name_len == ZSTR_LEN(handlers[i]->name) && !memcmp(ZSTR_VAL(handlers[i]->name), name, name_len)) {
  515. return 1;
  516. }
  517. }
  518. }
  519. return 0;
  520. }
  521. /* }}} */
  522. /* {{{ int php_output_handler_conflict(zval *handler_new, zval *handler_old)
  523. * Check whether a certain handler is in use and issue a warning that the new handler would conflict with the already used one */
  524. PHPAPI int php_output_handler_conflict(const char *handler_new, size_t handler_new_len, const char *handler_set, size_t handler_set_len)
  525. {
  526. if (php_output_handler_started(handler_set, handler_set_len)) {
  527. if (handler_new_len != handler_set_len || memcmp(handler_new, handler_set, handler_set_len)) {
  528. php_error_docref("ref.outcontrol", E_WARNING, "output handler '%s' conflicts with '%s'", handler_new, handler_set);
  529. } else {
  530. php_error_docref("ref.outcontrol", E_WARNING, "output handler '%s' cannot be used twice", handler_new);
  531. }
  532. return 1;
  533. }
  534. return 0;
  535. }
  536. /* }}} */
  537. /* {{{ SUCCESS|FAILURE php_output_handler_conflict_register(zval *name, php_output_handler_conflict_check_t check_func)
  538. * Register a conflict checking function on MINIT */
  539. PHPAPI int php_output_handler_conflict_register(const char *name, size_t name_len, php_output_handler_conflict_check_t check_func)
  540. {
  541. zend_string *str;
  542. if (!EG(current_module)) {
  543. zend_error(E_ERROR, "Cannot register an output handler conflict outside of MINIT");
  544. return FAILURE;
  545. }
  546. str = zend_string_init_interned(name, name_len, 1);
  547. zend_hash_update_ptr(&php_output_handler_conflicts, str, check_func);
  548. zend_string_release_ex(str, 1);
  549. return SUCCESS;
  550. }
  551. /* }}} */
  552. /* {{{ SUCCESS|FAILURE php_output_handler_reverse_conflict_register(zval *name, php_output_handler_conflict_check_t check_func)
  553. * Register a reverse conflict checking function on MINIT */
  554. PHPAPI int php_output_handler_reverse_conflict_register(const char *name, size_t name_len, php_output_handler_conflict_check_t check_func)
  555. {
  556. HashTable rev, *rev_ptr = NULL;
  557. if (!EG(current_module)) {
  558. zend_error(E_ERROR, "Cannot register a reverse output handler conflict outside of MINIT");
  559. return FAILURE;
  560. }
  561. if (NULL != (rev_ptr = zend_hash_str_find_ptr(&php_output_handler_reverse_conflicts, name, name_len))) {
  562. return zend_hash_next_index_insert_ptr(rev_ptr, check_func) ? SUCCESS : FAILURE;
  563. } else {
  564. zend_string *str;
  565. zend_hash_init(&rev, 8, NULL, NULL, 1);
  566. if (NULL == zend_hash_next_index_insert_ptr(&rev, check_func)) {
  567. zend_hash_destroy(&rev);
  568. return FAILURE;
  569. }
  570. str = zend_string_init_interned(name, name_len, 1);
  571. zend_hash_update_mem(&php_output_handler_reverse_conflicts, str, &rev, sizeof(HashTable));
  572. zend_string_release_ex(str, 1);
  573. return SUCCESS;
  574. }
  575. }
  576. /* }}} */
  577. /* {{{ php_output_handler_alias_ctor_t php_output_handler_alias(zval *name)
  578. * Get an internal output handler for a user handler if it exists */
  579. PHPAPI php_output_handler_alias_ctor_t php_output_handler_alias(const char *name, size_t name_len)
  580. {
  581. return zend_hash_str_find_ptr(&php_output_handler_aliases, name, name_len);
  582. }
  583. /* }}} */
  584. /* {{{ SUCCESS|FAILURE php_output_handler_alias_register(zval *name, php_output_handler_alias_ctor_t func)
  585. * Registers an internal output handler as alias for a user handler */
  586. PHPAPI int php_output_handler_alias_register(const char *name, size_t name_len, php_output_handler_alias_ctor_t func)
  587. {
  588. zend_string *str;
  589. if (!EG(current_module)) {
  590. zend_error(E_ERROR, "Cannot register an output handler alias outside of MINIT");
  591. return FAILURE;
  592. }
  593. str = zend_string_init_interned(name, name_len, 1);
  594. zend_hash_update_ptr(&php_output_handler_aliases, str, func);
  595. zend_string_release_ex(str, 1);
  596. return SUCCESS;
  597. }
  598. /* }}} */
  599. /* {{{ SUCCESS|FAILURE php_output_handler_hook(php_output_handler_hook_t type, void *arg)
  600. * Output handler hook for output handler functions to check/modify the current handlers abilities */
  601. PHPAPI int php_output_handler_hook(php_output_handler_hook_t type, void *arg)
  602. {
  603. if (OG(running)) {
  604. switch (type) {
  605. case PHP_OUTPUT_HANDLER_HOOK_GET_OPAQ:
  606. *(void ***) arg = &OG(running)->opaq;
  607. return SUCCESS;
  608. case PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS:
  609. *(int *) arg = OG(running)->flags;
  610. return SUCCESS;
  611. case PHP_OUTPUT_HANDLER_HOOK_GET_LEVEL:
  612. *(int *) arg = OG(running)->level;
  613. return SUCCESS;
  614. case PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE:
  615. OG(running)->flags &= ~(PHP_OUTPUT_HANDLER_REMOVABLE|PHP_OUTPUT_HANDLER_CLEANABLE);
  616. return SUCCESS;
  617. case PHP_OUTPUT_HANDLER_HOOK_DISABLE:
  618. OG(running)->flags |= PHP_OUTPUT_HANDLER_DISABLED;
  619. return SUCCESS;
  620. default:
  621. break;
  622. }
  623. }
  624. return FAILURE;
  625. }
  626. /* }}} */
  627. /* {{{ void php_output_handler_dtor(php_output_handler *handler)
  628. * Destroy an output handler */
  629. PHPAPI void php_output_handler_dtor(php_output_handler *handler)
  630. {
  631. if (handler->name) {
  632. zend_string_release_ex(handler->name, 0);
  633. }
  634. if (handler->buffer.data) {
  635. efree(handler->buffer.data);
  636. }
  637. if (handler->flags & PHP_OUTPUT_HANDLER_USER) {
  638. zval_ptr_dtor(&handler->func.user->zoh);
  639. efree(handler->func.user);
  640. }
  641. if (handler->dtor && handler->opaq) {
  642. handler->dtor(handler->opaq);
  643. }
  644. memset(handler, 0, sizeof(*handler));
  645. }
  646. /* }}} */
  647. /* {{{ void php_output_handler_free(php_output_handler **handler)
  648. * Destroy and free an output handler */
  649. PHPAPI void php_output_handler_free(php_output_handler **h)
  650. {
  651. if (*h) {
  652. php_output_handler_dtor(*h);
  653. efree(*h);
  654. *h = NULL;
  655. }
  656. }
  657. /* }}} */
  658. /* void php_output_set_implicit_flush(int enabled)
  659. * Enable or disable implicit flush */
  660. PHPAPI void php_output_set_implicit_flush(int flush)
  661. {
  662. if (flush) {
  663. OG(flags) |= PHP_OUTPUT_IMPLICITFLUSH;
  664. } else {
  665. OG(flags) &= ~PHP_OUTPUT_IMPLICITFLUSH;
  666. }
  667. }
  668. /* }}} */
  669. /* {{{ char *php_output_get_start_filename(void)
  670. * Get the file name where output has started */
  671. PHPAPI const char *php_output_get_start_filename(void)
  672. {
  673. return OG(output_start_filename);
  674. }
  675. /* }}} */
  676. /* {{{ int php_output_get_start_lineno(void)
  677. * Get the line number where output has started */
  678. PHPAPI int php_output_get_start_lineno(void)
  679. {
  680. return OG(output_start_lineno);
  681. }
  682. /* }}} */
  683. /* {{{ static int php_output_lock_error(int op)
  684. * Checks whether an unallowed operation is attempted from within the output handler and issues a fatal error */
  685. static inline int php_output_lock_error(int op)
  686. {
  687. /* if there's no ob active, ob has been stopped */
  688. if (op && OG(active) && OG(running)) {
  689. /* fatal error */
  690. php_output_deactivate();
  691. php_error_docref("ref.outcontrol", E_ERROR, "Cannot use output buffering in output buffering display handlers");
  692. return 1;
  693. }
  694. return 0;
  695. }
  696. /* }}} */
  697. /* {{{ static php_output_context *php_output_context_init(php_output_context *context, int op)
  698. * Initialize a new output context */
  699. static inline php_output_context *php_output_context_init(php_output_context *context, int op)
  700. {
  701. if (!context) {
  702. context = emalloc(sizeof(php_output_context));
  703. }
  704. memset(context, 0, sizeof(php_output_context));
  705. context->op = op;
  706. return context;
  707. }
  708. /* }}} */
  709. /* {{{ static void php_output_context_reset(php_output_context *context)
  710. * Reset an output context */
  711. static inline void php_output_context_reset(php_output_context *context)
  712. {
  713. int op = context->op;
  714. php_output_context_dtor(context);
  715. memset(context, 0, sizeof(php_output_context));
  716. context->op = op;
  717. }
  718. /* }}} */
  719. /* {{{ static void php_output_context_feed(php_output_context *context, char *, size_t, size_t)
  720. * Feed output contexts input buffer */
  721. static inline void php_output_context_feed(php_output_context *context, char *data, size_t size, size_t used, zend_bool free)
  722. {
  723. if (context->in.free && context->in.data) {
  724. efree(context->in.data);
  725. }
  726. context->in.data = data;
  727. context->in.used = used;
  728. context->in.free = free;
  729. context->in.size = size;
  730. }
  731. /* }}} */
  732. /* {{{ static void php_output_context_swap(php_output_context *context)
  733. * Swap output contexts buffers */
  734. static inline void php_output_context_swap(php_output_context *context)
  735. {
  736. if (context->in.free && context->in.data) {
  737. efree(context->in.data);
  738. }
  739. context->in.data = context->out.data;
  740. context->in.used = context->out.used;
  741. context->in.free = context->out.free;
  742. context->in.size = context->out.size;
  743. context->out.data = NULL;
  744. context->out.used = 0;
  745. context->out.free = 0;
  746. context->out.size = 0;
  747. }
  748. /* }}} */
  749. /* {{{ static void php_output_context_pass(php_output_context *context)
  750. * Pass input to output buffer */
  751. static inline void php_output_context_pass(php_output_context *context)
  752. {
  753. context->out.data = context->in.data;
  754. context->out.used = context->in.used;
  755. context->out.size = context->in.size;
  756. context->out.free = context->in.free;
  757. context->in.data = NULL;
  758. context->in.used = 0;
  759. context->in.free = 0;
  760. context->in.size = 0;
  761. }
  762. /* }}} */
  763. /* {{{ static void php_output_context_dtor(php_output_context *context)
  764. * Destroy the contents of an output context */
  765. static inline void php_output_context_dtor(php_output_context *context)
  766. {
  767. if (context->in.free && context->in.data) {
  768. efree(context->in.data);
  769. context->in.data = NULL;
  770. }
  771. if (context->out.free && context->out.data) {
  772. efree(context->out.data);
  773. context->out.data = NULL;
  774. }
  775. }
  776. /* }}} */
  777. /* {{{ static php_output_handler *php_output_handler_init(zval *name, size_t chunk_size, int flags)
  778. * Allocates and initializes a php_output_handler structure */
  779. static inline php_output_handler *php_output_handler_init(zend_string *name, size_t chunk_size, int flags)
  780. {
  781. php_output_handler *handler;
  782. handler = ecalloc(1, sizeof(php_output_handler));
  783. handler->name = zend_string_copy(name);
  784. handler->size = chunk_size;
  785. handler->flags = flags;
  786. handler->buffer.size = PHP_OUTPUT_HANDLER_INITBUF_SIZE(chunk_size);
  787. handler->buffer.data = emalloc(handler->buffer.size);
  788. return handler;
  789. }
  790. /* }}} */
  791. /* {{{ static int php_output_handler_appen(php_output_handler *handler, const php_output_buffer *buf)
  792. * Appends input to the output handlers buffer and indicates whether the buffer does not have to be processed by the output handler */
  793. static inline int php_output_handler_append(php_output_handler *handler, const php_output_buffer *buf)
  794. {
  795. if (buf->used) {
  796. OG(flags) |= PHP_OUTPUT_WRITTEN;
  797. /* store it away */
  798. if ((handler->buffer.size - handler->buffer.used) <= buf->used) {
  799. size_t grow_int = PHP_OUTPUT_HANDLER_INITBUF_SIZE(handler->size);
  800. size_t grow_buf = PHP_OUTPUT_HANDLER_INITBUF_SIZE(buf->used - (handler->buffer.size - handler->buffer.used));
  801. size_t grow_max = MAX(grow_int, grow_buf);
  802. handler->buffer.data = erealloc(handler->buffer.data, handler->buffer.size + grow_max);
  803. handler->buffer.size += grow_max;
  804. }
  805. memcpy(handler->buffer.data + handler->buffer.used, buf->data, buf->used);
  806. handler->buffer.used += buf->used;
  807. /* chunked buffering */
  808. if (handler->size && (handler->buffer.used >= handler->size)) {
  809. /* store away errors and/or any intermediate output */
  810. return OG(running) ? 1 : 0;
  811. }
  812. }
  813. return 1;
  814. }
  815. /* }}} */
  816. /* {{{ static php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context)
  817. * Output handler operation dispatcher, applying context op to the php_output_handler handler */
  818. static inline php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context)
  819. {
  820. php_output_handler_status_t status;
  821. int original_op = context->op;
  822. #if PHP_OUTPUT_DEBUG
  823. fprintf(stderr, ">>> op(%d, "
  824. "handler=%p, "
  825. "name=%s, "
  826. "flags=%d, "
  827. "buffer.data=%s, "
  828. "buffer.used=%zu, "
  829. "buffer.size=%zu, "
  830. "in.data=%s, "
  831. "in.used=%zu)\n",
  832. context->op,
  833. handler,
  834. handler->name,
  835. handler->flags,
  836. handler->buffer.used?handler->buffer.data:"",
  837. handler->buffer.used,
  838. handler->buffer.size,
  839. context->in.used?context->in.data:"",
  840. context->in.used
  841. );
  842. #endif
  843. if (php_output_lock_error(context->op)) {
  844. /* fatal error */
  845. return PHP_OUTPUT_HANDLER_FAILURE;
  846. }
  847. /* storable? */
  848. if (php_output_handler_append(handler, &context->in) && !context->op) {
  849. context->op = original_op;
  850. return PHP_OUTPUT_HANDLER_NO_DATA;
  851. } else {
  852. /* need to start? */
  853. if (!(handler->flags & PHP_OUTPUT_HANDLER_STARTED)) {
  854. context->op |= PHP_OUTPUT_HANDLER_START;
  855. }
  856. OG(running) = handler;
  857. if (handler->flags & PHP_OUTPUT_HANDLER_USER) {
  858. zval retval, ob_data, ob_mode;
  859. ZVAL_STRINGL(&ob_data, handler->buffer.data, handler->buffer.used);
  860. ZVAL_LONG(&ob_mode, (zend_long) context->op);
  861. zend_fcall_info_argn(&handler->func.user->fci, 2, &ob_data, &ob_mode);
  862. zval_ptr_dtor(&ob_data);
  863. #define PHP_OUTPUT_USER_SUCCESS(retval) ((Z_TYPE(retval) != IS_UNDEF) && !(Z_TYPE(retval) == IS_FALSE))
  864. if (SUCCESS == zend_fcall_info_call(&handler->func.user->fci, &handler->func.user->fcc, &retval, NULL) && PHP_OUTPUT_USER_SUCCESS(retval)) {
  865. /* user handler may have returned TRUE */
  866. status = PHP_OUTPUT_HANDLER_NO_DATA;
  867. if (Z_TYPE(retval) != IS_FALSE && Z_TYPE(retval) != IS_TRUE) {
  868. convert_to_string_ex(&retval);
  869. if (Z_STRLEN(retval)) {
  870. context->out.data = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
  871. context->out.used = Z_STRLEN(retval);
  872. context->out.free = 1;
  873. status = PHP_OUTPUT_HANDLER_SUCCESS;
  874. }
  875. }
  876. } else {
  877. /* call failed, pass internal buffer along */
  878. status = PHP_OUTPUT_HANDLER_FAILURE;
  879. }
  880. zend_fcall_info_argn(&handler->func.user->fci, 0);
  881. zval_ptr_dtor(&retval);
  882. } else {
  883. php_output_context_feed(context, handler->buffer.data, handler->buffer.size, handler->buffer.used, 0);
  884. if (SUCCESS == handler->func.internal(&handler->opaq, context)) {
  885. if (context->out.used) {
  886. status = PHP_OUTPUT_HANDLER_SUCCESS;
  887. } else {
  888. status = PHP_OUTPUT_HANDLER_NO_DATA;
  889. }
  890. } else {
  891. status = PHP_OUTPUT_HANDLER_FAILURE;
  892. }
  893. }
  894. handler->flags |= PHP_OUTPUT_HANDLER_STARTED;
  895. OG(running) = NULL;
  896. }
  897. switch (status) {
  898. case PHP_OUTPUT_HANDLER_FAILURE:
  899. /* disable this handler */
  900. handler->flags |= PHP_OUTPUT_HANDLER_DISABLED;
  901. /* discard any output */
  902. if (context->out.data && context->out.free) {
  903. efree(context->out.data);
  904. }
  905. /* returns handlers buffer */
  906. context->out.data = handler->buffer.data;
  907. context->out.used = handler->buffer.used;
  908. context->out.free = 1;
  909. handler->buffer.data = NULL;
  910. handler->buffer.used = 0;
  911. handler->buffer.size = 0;
  912. break;
  913. case PHP_OUTPUT_HANDLER_NO_DATA:
  914. /* handler ate all */
  915. php_output_context_reset(context);
  916. /* no break */
  917. case PHP_OUTPUT_HANDLER_SUCCESS:
  918. /* no more buffered data */
  919. handler->buffer.used = 0;
  920. handler->flags |= PHP_OUTPUT_HANDLER_PROCESSED;
  921. break;
  922. }
  923. context->op = original_op;
  924. return status;
  925. }
  926. /* }}} */
  927. /* {{{ static void php_output_op(int op, const char *str, size_t len)
  928. * Output op dispatcher, passes input and output handlers output through the output handler stack until it gets written to the SAPI */
  929. static inline void php_output_op(int op, const char *str, size_t len)
  930. {
  931. php_output_context context;
  932. php_output_handler **active;
  933. int obh_cnt;
  934. if (php_output_lock_error(op)) {
  935. return;
  936. }
  937. php_output_context_init(&context, op);
  938. /*
  939. * broken up for better performance:
  940. * - apply op to the one active handler; note that OG(active) might be popped off the stack on a flush
  941. * - or apply op to the handler stack
  942. */
  943. if (OG(active) && (obh_cnt = zend_stack_count(&OG(handlers)))) {
  944. context.in.data = (char *) str;
  945. context.in.used = len;
  946. if (obh_cnt > 1) {
  947. zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_op, &context);
  948. } else if ((active = zend_stack_top(&OG(handlers))) && (!((*active)->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
  949. php_output_handler_op(*active, &context);
  950. } else {
  951. php_output_context_pass(&context);
  952. }
  953. } else {
  954. context.out.data = (char *) str;
  955. context.out.used = len;
  956. }
  957. if (context.out.data && context.out.used) {
  958. php_output_header();
  959. if (!(OG(flags) & PHP_OUTPUT_DISABLED)) {
  960. #if PHP_OUTPUT_DEBUG
  961. fprintf(stderr, "::: sapi_write('%s', %zu)\n", context.out.data, context.out.used);
  962. #endif
  963. sapi_module.ub_write(context.out.data, context.out.used);
  964. if (OG(flags) & PHP_OUTPUT_IMPLICITFLUSH) {
  965. sapi_flush();
  966. }
  967. OG(flags) |= PHP_OUTPUT_SENT;
  968. }
  969. }
  970. php_output_context_dtor(&context);
  971. }
  972. /* }}} */
  973. /* {{{ static int php_output_stack_apply_op(void *h, void *c)
  974. * Operation callback for the stack apply function */
  975. static int php_output_stack_apply_op(void *h, void *c)
  976. {
  977. int was_disabled;
  978. php_output_handler_status_t status;
  979. php_output_handler *handler = *(php_output_handler **) h;
  980. php_output_context *context = (php_output_context *) c;
  981. if ((was_disabled = (handler->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
  982. status = PHP_OUTPUT_HANDLER_FAILURE;
  983. } else {
  984. status = php_output_handler_op(handler, context);
  985. }
  986. /*
  987. * handler ate all => break
  988. * handler returned data or failed resp. is disabled => continue
  989. */
  990. switch (status) {
  991. case PHP_OUTPUT_HANDLER_NO_DATA:
  992. return 1;
  993. case PHP_OUTPUT_HANDLER_SUCCESS:
  994. /* swap contexts buffers, unless this is the last handler in the stack */
  995. if (handler->level) {
  996. php_output_context_swap(context);
  997. }
  998. return 0;
  999. case PHP_OUTPUT_HANDLER_FAILURE:
  1000. default:
  1001. if (was_disabled) {
  1002. /* pass input along, if it's the last handler in the stack */
  1003. if (!handler->level) {
  1004. php_output_context_pass(context);
  1005. }
  1006. } else {
  1007. /* swap buffers, unless this is the last handler */
  1008. if (handler->level) {
  1009. php_output_context_swap(context);
  1010. }
  1011. }
  1012. return 0;
  1013. }
  1014. }
  1015. /* }}} */
  1016. /* {{{ static int php_output_stack_apply_clean(void *h, void *c)
  1017. * Clean callback for the stack apply function */
  1018. static int php_output_stack_apply_clean(void *h, void *c)
  1019. {
  1020. php_output_handler *handler = *(php_output_handler **) h;
  1021. php_output_context *context = (php_output_context *) c;
  1022. handler->buffer.used = 0;
  1023. php_output_handler_op(handler, context);
  1024. php_output_context_reset(context);
  1025. return 0;
  1026. }
  1027. /* }}} */
  1028. /* {{{ static int php_output_stack_apply_list(void *h, void *z)
  1029. * List callback for the stack apply function */
  1030. static int php_output_stack_apply_list(void *h, void *z)
  1031. {
  1032. php_output_handler *handler = *(php_output_handler **) h;
  1033. zval *array = (zval *) z;
  1034. add_next_index_str(array, zend_string_copy(handler->name));
  1035. return 0;
  1036. }
  1037. /* }}} */
  1038. /* {{{ static int php_output_stack_apply_status(void *h, void *z)
  1039. * Status callback for the stack apply function */
  1040. static int php_output_stack_apply_status(void *h, void *z)
  1041. {
  1042. php_output_handler *handler = *(php_output_handler **) h;
  1043. zval arr, *array = (zval *) z;
  1044. add_next_index_zval(array, php_output_handler_status(handler, &arr));
  1045. return 0;
  1046. }
  1047. /* {{{ static zval *php_output_handler_status(php_output_handler *handler, zval *entry)
  1048. * Returns an array with the status of the output handler */
  1049. static inline zval *php_output_handler_status(php_output_handler *handler, zval *entry)
  1050. {
  1051. ZEND_ASSERT(entry != NULL);
  1052. array_init(entry);
  1053. add_assoc_str(entry, "name", zend_string_copy(handler->name));
  1054. add_assoc_long(entry, "type", (zend_long) (handler->flags & 0xf));
  1055. add_assoc_long(entry, "flags", (zend_long) handler->flags);
  1056. add_assoc_long(entry, "level", (zend_long) handler->level);
  1057. add_assoc_long(entry, "chunk_size", (zend_long) handler->size);
  1058. add_assoc_long(entry, "buffer_size", (zend_long) handler->buffer.size);
  1059. add_assoc_long(entry, "buffer_used", (zend_long) handler->buffer.used);
  1060. return entry;
  1061. }
  1062. /* }}} */
  1063. /* {{{ static int php_output_stack_pop(int flags)
  1064. * Pops an output handler off the stack */
  1065. static inline int php_output_stack_pop(int flags)
  1066. {
  1067. php_output_context context;
  1068. php_output_handler **current, *orphan = OG(active);
  1069. if (!orphan) {
  1070. if (!(flags & PHP_OUTPUT_POP_SILENT)) {
  1071. php_error_docref("ref.outcontrol", E_NOTICE, "failed to %s buffer. No buffer to %s", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send");
  1072. }
  1073. return 0;
  1074. } else if (!(flags & PHP_OUTPUT_POP_FORCE) && !(orphan->flags & PHP_OUTPUT_HANDLER_REMOVABLE)) {
  1075. if (!(flags & PHP_OUTPUT_POP_SILENT)) {
  1076. php_error_docref("ref.outcontrol", E_NOTICE, "failed to %s buffer of %s (%d)", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send", ZSTR_VAL(orphan->name), orphan->level);
  1077. }
  1078. return 0;
  1079. } else {
  1080. php_output_context_init(&context, PHP_OUTPUT_HANDLER_FINAL);
  1081. /* don't run the output handler if it's disabled */
  1082. if (!(orphan->flags & PHP_OUTPUT_HANDLER_DISABLED)) {
  1083. /* didn't it start yet? */
  1084. if (!(orphan->flags & PHP_OUTPUT_HANDLER_STARTED)) {
  1085. context.op |= PHP_OUTPUT_HANDLER_START;
  1086. }
  1087. /* signal that we're cleaning up */
  1088. if (flags & PHP_OUTPUT_POP_DISCARD) {
  1089. context.op |= PHP_OUTPUT_HANDLER_CLEAN;
  1090. }
  1091. php_output_handler_op(orphan, &context);
  1092. }
  1093. /* pop it off the stack */
  1094. zend_stack_del_top(&OG(handlers));
  1095. if ((current = zend_stack_top(&OG(handlers)))) {
  1096. OG(active) = *current;
  1097. } else {
  1098. OG(active) = NULL;
  1099. }
  1100. /* pass output along */
  1101. if (context.out.data && context.out.used && !(flags & PHP_OUTPUT_POP_DISCARD)) {
  1102. php_output_write(context.out.data, context.out.used);
  1103. }
  1104. /* destroy the handler (after write!) */
  1105. php_output_handler_free(&orphan);
  1106. php_output_context_dtor(&context);
  1107. return 1;
  1108. }
  1109. }
  1110. /* }}} */
  1111. /* {{{ static SUCCESS|FAILURE php_output_handler_compat_func(void *ctx, php_output_context *)
  1112. * php_output_handler_context_func_t for php_output_handler_func_t output handlers */
  1113. static int php_output_handler_compat_func(void **handler_context, php_output_context *output_context)
  1114. {
  1115. php_output_handler_func_t func = *(php_output_handler_func_t *) handler_context;
  1116. if (func) {
  1117. char *out_str = NULL;
  1118. size_t out_len = 0;
  1119. func(output_context->in.data, output_context->in.used, &out_str, &out_len, output_context->op);
  1120. if (out_str) {
  1121. output_context->out.data = out_str;
  1122. output_context->out.used = out_len;
  1123. output_context->out.free = 1;
  1124. } else {
  1125. php_output_context_pass(output_context);
  1126. }
  1127. return SUCCESS;
  1128. }
  1129. return FAILURE;
  1130. }
  1131. /* }}} */
  1132. /* {{{ static SUCCESS|FAILURE php_output_handler_default_func(void *ctx, php_output_context *)
  1133. * Default output handler */
  1134. static int php_output_handler_default_func(void **handler_context, php_output_context *output_context)
  1135. {
  1136. php_output_context_pass(output_context);
  1137. return SUCCESS;
  1138. }
  1139. /* }}} */
  1140. /* {{{ static SUCCESS|FAILURE php_output_handler_devnull_func(void *ctx, php_output_context *)
  1141. * Null output handler */
  1142. static int php_output_handler_devnull_func(void **handler_context, php_output_context *output_context)
  1143. {
  1144. return SUCCESS;
  1145. }
  1146. /* }}} */
  1147. /*
  1148. * USERLAND (nearly 1:1 of old output.c)
  1149. */
  1150. /* {{{ proto bool ob_start([string|array user_function [, int chunk_size [, int flags]]])
  1151. Turn on Output Buffering (specifying an optional output handler). */
  1152. PHP_FUNCTION(ob_start)
  1153. {
  1154. zval *output_handler = NULL;
  1155. zend_long chunk_size = 0;
  1156. zend_long flags = PHP_OUTPUT_HANDLER_STDFLAGS;
  1157. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|zll", &output_handler, &chunk_size, &flags) == FAILURE) {
  1158. return;
  1159. }
  1160. if (chunk_size < 0) {
  1161. chunk_size = 0;
  1162. }
  1163. if (php_output_start_user(output_handler, chunk_size, flags) == FAILURE) {
  1164. php_error_docref("ref.outcontrol", E_NOTICE, "failed to create buffer");
  1165. RETURN_FALSE;
  1166. }
  1167. RETURN_TRUE;
  1168. }
  1169. /* }}} */
  1170. /* {{{ proto bool ob_flush(void)
  1171. Flush (send) contents of the output buffer. The last buffer content is sent to next buffer */
  1172. PHP_FUNCTION(ob_flush)
  1173. {
  1174. if (zend_parse_parameters_none() == FAILURE) {
  1175. return;
  1176. }
  1177. if (!OG(active)) {
  1178. php_error_docref("ref.outcontrol", E_NOTICE, "failed to flush buffer. No buffer to flush");
  1179. RETURN_FALSE;
  1180. }
  1181. if (SUCCESS != php_output_flush()) {
  1182. php_error_docref("ref.outcontrol", E_NOTICE, "failed to flush buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
  1183. RETURN_FALSE;
  1184. }
  1185. RETURN_TRUE;
  1186. }
  1187. /* }}} */
  1188. /* {{{ proto bool ob_clean(void)
  1189. Clean (delete) the current output buffer */
  1190. PHP_FUNCTION(ob_clean)
  1191. {
  1192. if (zend_parse_parameters_none() == FAILURE) {
  1193. return;
  1194. }
  1195. if (!OG(active)) {
  1196. php_error_docref("ref.outcontrol", E_NOTICE, "failed to delete buffer. No buffer to delete");
  1197. RETURN_FALSE;
  1198. }
  1199. if (SUCCESS != php_output_clean()) {
  1200. php_error_docref("ref.outcontrol", E_NOTICE, "failed to delete buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
  1201. RETURN_FALSE;
  1202. }
  1203. RETURN_TRUE;
  1204. }
  1205. /* }}} */
  1206. /* {{{ proto bool ob_end_flush(void)
  1207. Flush (send) the output buffer, and delete current output buffer */
  1208. PHP_FUNCTION(ob_end_flush)
  1209. {
  1210. if (zend_parse_parameters_none() == FAILURE) {
  1211. return;
  1212. }
  1213. if (!OG(active)) {
  1214. php_error_docref("ref.outcontrol", E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush");
  1215. RETURN_FALSE;
  1216. }
  1217. RETURN_BOOL(SUCCESS == php_output_end());
  1218. }
  1219. /* }}} */
  1220. /* {{{ proto bool ob_end_clean(void)
  1221. Clean the output buffer, and delete current output buffer */
  1222. PHP_FUNCTION(ob_end_clean)
  1223. {
  1224. if (zend_parse_parameters_none() == FAILURE) {
  1225. return;
  1226. }
  1227. if (!OG(active)) {
  1228. php_error_docref("ref.outcontrol", E_NOTICE, "failed to delete buffer. No buffer to delete");
  1229. RETURN_FALSE;
  1230. }
  1231. RETURN_BOOL(SUCCESS == php_output_discard());
  1232. }
  1233. /* }}} */
  1234. /* {{{ proto bool ob_get_flush(void)
  1235. Get current buffer contents, flush (send) the output buffer, and delete current output buffer */
  1236. PHP_FUNCTION(ob_get_flush)
  1237. {
  1238. if (zend_parse_parameters_none() == FAILURE) {
  1239. return;
  1240. }
  1241. if (php_output_get_contents(return_value) == FAILURE) {
  1242. php_error_docref("ref.outcontrol", E_NOTICE, "failed to delete and flush buffer. No buffer to delete or flush");
  1243. RETURN_FALSE;
  1244. }
  1245. if (SUCCESS != php_output_end()) {
  1246. php_error_docref("ref.outcontrol", E_NOTICE, "failed to delete buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
  1247. }
  1248. }
  1249. /* }}} */
  1250. /* {{{ proto bool ob_get_clean(void)
  1251. Get current buffer contents and delete current output buffer */
  1252. PHP_FUNCTION(ob_get_clean)
  1253. {
  1254. if (zend_parse_parameters_none() == FAILURE) {
  1255. return;
  1256. }
  1257. if(!OG(active)) {
  1258. RETURN_FALSE;
  1259. }
  1260. if (php_output_get_contents(return_value) == FAILURE) {
  1261. php_error_docref("ref.outcontrol", E_NOTICE, "failed to delete buffer. No buffer to delete");
  1262. RETURN_FALSE;
  1263. }
  1264. if (SUCCESS != php_output_discard()) {
  1265. php_error_docref("ref.outcontrol", E_NOTICE, "failed to delete buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
  1266. }
  1267. }
  1268. /* }}} */
  1269. /* {{{ proto string ob_get_contents(void)
  1270. Return the contents of the output buffer */
  1271. PHP_FUNCTION(ob_get_contents)
  1272. {
  1273. if (zend_parse_parameters_none() == FAILURE) {
  1274. return;
  1275. }
  1276. if (php_output_get_contents(return_value) == FAILURE) {
  1277. RETURN_FALSE;
  1278. }
  1279. }
  1280. /* }}} */
  1281. /* {{{ proto int ob_get_level(void)
  1282. Return the nesting level of the output buffer */
  1283. PHP_FUNCTION(ob_get_level)
  1284. {
  1285. if (zend_parse_parameters_none() == FAILURE) {
  1286. return;
  1287. }
  1288. RETURN_LONG(php_output_get_level());
  1289. }
  1290. /* }}} */
  1291. /* {{{ proto int ob_get_length(void)
  1292. Return the length of the output buffer */
  1293. PHP_FUNCTION(ob_get_length)
  1294. {
  1295. if (zend_parse_parameters_none() == FAILURE) {
  1296. return;
  1297. }
  1298. if (php_output_get_length(return_value) == FAILURE) {
  1299. RETURN_FALSE;
  1300. }
  1301. }
  1302. /* }}} */
  1303. /* {{{ proto false|array ob_list_handlers()
  1304. List all output_buffers in an array */
  1305. PHP_FUNCTION(ob_list_handlers)
  1306. {
  1307. if (zend_parse_parameters_none() == FAILURE) {
  1308. return;
  1309. }
  1310. array_init(return_value);
  1311. if (!OG(active)) {
  1312. return;
  1313. }
  1314. zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_list, return_value);
  1315. }
  1316. /* }}} */
  1317. /* {{{ proto false|array ob_get_status([bool full_status])
  1318. Return the status of the active or all output buffers */
  1319. PHP_FUNCTION(ob_get_status)
  1320. {
  1321. zend_bool full_status = 0;
  1322. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_status) == FAILURE) {
  1323. return;
  1324. }
  1325. if (!OG(active)) {
  1326. array_init(return_value);
  1327. return;
  1328. }
  1329. if (full_status) {
  1330. array_init(return_value);
  1331. zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_status, return_value);
  1332. } else {
  1333. php_output_handler_status(OG(active), return_value);
  1334. }
  1335. }
  1336. /* }}} */
  1337. /* {{{ proto void ob_implicit_flush([int flag])
  1338. Turn implicit flush on/off and is equivalent to calling flush() after every output call */
  1339. PHP_FUNCTION(ob_implicit_flush)
  1340. {
  1341. zend_long flag = 1;
  1342. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flag) == FAILURE) {
  1343. return;
  1344. }
  1345. php_output_set_implicit_flush(flag);
  1346. }
  1347. /* }}} */
  1348. /* {{{ proto bool output_reset_rewrite_vars(void)
  1349. Reset(clear) URL rewriter values */
  1350. PHP_FUNCTION(output_reset_rewrite_vars)
  1351. {
  1352. if (zend_parse_parameters_none() == FAILURE) {
  1353. return;
  1354. }
  1355. if (php_url_scanner_reset_vars() == SUCCESS) {
  1356. RETURN_TRUE;
  1357. } else {
  1358. RETURN_FALSE;
  1359. }
  1360. }
  1361. /* }}} */
  1362. /* {{{ proto bool output_add_rewrite_var(string name, string value)
  1363. Add URL rewriter values */
  1364. PHP_FUNCTION(output_add_rewrite_var)
  1365. {
  1366. char *name, *value;
  1367. size_t name_len, value_len;
  1368. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &name, &name_len, &value, &value_len) == FAILURE) {
  1369. return;
  1370. }
  1371. if (php_url_scanner_add_var(name, name_len, value, value_len, 1) == SUCCESS) {
  1372. RETURN_TRUE;
  1373. } else {
  1374. RETURN_FALSE;
  1375. }
  1376. }
  1377. /* }}} */
  1378. /*
  1379. * Local variables:
  1380. * tab-width: 4
  1381. * c-basic-offset: 4
  1382. * End:
  1383. * vim600: sw=4 ts=4 fdm=marker
  1384. * vim<600: sw=4 ts=4
  1385. */