output.c 48 KB

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