lsapi_main.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at 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. | Author: George Wang <gwang@litespeedtech.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "SAPI.h"
  20. #include "php_main.h"
  21. #include "php_ini.h"
  22. #include "php_variables.h"
  23. #include "zend_highlight.h"
  24. #include "zend.h"
  25. #include "lsapilib.h"
  26. #include <stdio.h>
  27. #if HAVE_STDLIB_H
  28. #include <stdlib.h>
  29. #endif
  30. #if HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #ifdef PHP_WIN32
  34. #include <io.h>
  35. #include <fcntl.h>
  36. #include "win32/php_registry.h"
  37. #else
  38. #include <sys/wait.h>
  39. #endif
  40. #include <sys/stat.h>
  41. #if HAVE_SYS_TYPES_H
  42. #include <sys/types.h>
  43. #endif
  44. #if HAVE_SIGNAL_H
  45. #include <signal.h>
  46. #endif
  47. #include <sys/socket.h>
  48. #include <arpa/inet.h>
  49. #include <netinet/in.h>
  50. #define SAPI_LSAPI_MAX_HEADER_LENGTH 2048
  51. /* Key for each cache entry is dirname(PATH_TRANSLATED).
  52. *
  53. * NOTE: Each cache entry config_hash contains the combination from all user ini files found in
  54. * the path starting from doc_root throught to dirname(PATH_TRANSLATED). There is no point
  55. * storing per-file entries as it would not be possible to detect added / deleted entries
  56. * between separate files.
  57. */
  58. typedef struct _user_config_cache_entry {
  59. time_t expires;
  60. HashTable user_config;
  61. } user_config_cache_entry;
  62. static HashTable user_config_cache;
  63. static int lsapi_mode = 0;
  64. static char *php_self = "";
  65. static char *script_filename = "";
  66. static int source_highlight = 0;
  67. static int ignore_php_ini = 0;
  68. static char * argv0 = NULL;
  69. static int engine = 1;
  70. static int parse_user_ini = 0;
  71. #ifdef ZTS
  72. zend_compiler_globals *compiler_globals;
  73. zend_executor_globals *executor_globals;
  74. php_core_globals *core_globals;
  75. sapi_globals_struct *sapi_globals;
  76. void ***tsrm_ls;
  77. #endif
  78. zend_module_entry litespeed_module_entry;
  79. static void init_sapi_from_env(sapi_module_struct *sapi_module)
  80. {
  81. char *p;
  82. p = getenv("LSPHPRC");
  83. if (p)
  84. sapi_module->php_ini_path_override = p;
  85. }
  86. /* {{{ php_lsapi_startup
  87. */
  88. static int php_lsapi_startup(sapi_module_struct *sapi_module)
  89. {
  90. if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
  91. return FAILURE;
  92. }
  93. argv0 = sapi_module->executable_location;
  94. return SUCCESS;
  95. }
  96. /* }}} */
  97. /* {{{ sapi_lsapi_ini_defaults */
  98. /* overwriteable ini defaults must be set in sapi_cli_ini_defaults() */
  99. #define INI_DEFAULT(name,value)\
  100. ZVAL_STRING(tmp, value, 0);\
  101. zend_hash_update(configuration_hash, name, sizeof(name), tmp, sizeof(zval), (void**)&entry);\
  102. Z_STRVAL_P(entry) = zend_strndup(Z_STRVAL_P(entry), Z_STRLEN_P(entry))
  103. static void sapi_lsapi_ini_defaults(HashTable *configuration_hash)
  104. {
  105. zval *tmp, *entry;
  106. #if PHP_MAJOR_VERSION > 4
  107. /*
  108. MAKE_STD_ZVAL(tmp);
  109. INI_DEFAULT("register_long_arrays", "0");
  110. FREE_ZVAL(tmp);
  111. */
  112. #endif
  113. }
  114. /* }}} */
  115. /* {{{ sapi_lsapi_ub_write
  116. */
  117. #if PHP_MAJOR_VERSION >= 7
  118. static size_t sapi_lsapi_ub_write(const char *str, size_t str_length TSRMLS_DC)
  119. #else
  120. static int sapi_lsapi_ub_write(const char *str, uint str_length TSRMLS_DC)
  121. #endif
  122. {
  123. int ret;
  124. int remain;
  125. if ( lsapi_mode ) {
  126. ret = LSAPI_Write( str, str_length );
  127. if ( ret < str_length ) {
  128. php_handle_aborted_connection();
  129. return str_length - ret;
  130. }
  131. } else {
  132. remain = str_length;
  133. while( remain > 0 ) {
  134. ret = write( 1, str, remain );
  135. if ( ret <= 0 ) {
  136. php_handle_aborted_connection();
  137. return str_length - remain;
  138. }
  139. str += ret;
  140. remain -= ret;
  141. }
  142. }
  143. return str_length;
  144. }
  145. /* }}} */
  146. /* {{{ sapi_lsapi_flush
  147. */
  148. static void sapi_lsapi_flush(void * server_context)
  149. {
  150. if ( lsapi_mode ) {
  151. if ( LSAPI_Flush() == -1) {
  152. php_handle_aborted_connection();
  153. }
  154. }
  155. }
  156. /* }}} */
  157. /* {{{ sapi_lsapi_deactivate
  158. */
  159. static int sapi_lsapi_deactivate(TSRMLS_D)
  160. {
  161. if ( SG(request_info).path_translated )
  162. {
  163. efree( SG(request_info).path_translated );
  164. }
  165. return SUCCESS;
  166. }
  167. /* }}} */
  168. /* {{{ sapi_lsapi_getenv
  169. */
  170. static char *sapi_lsapi_getenv( char * name, size_t name_len TSRMLS_DC )
  171. {
  172. if ( lsapi_mode ) {
  173. return LSAPI_GetEnv( name );
  174. } else {
  175. return getenv( name );
  176. }
  177. }
  178. /* }}} */
  179. #if PHP_MAJOR_VERSION > 4
  180. static int add_variable( const char * pKey, int keyLen, const char * pValue, int valLen,
  181. void * arg TSRMLS_DC)
  182. {
  183. #if PHP_MAJOR_VERSION >= 7
  184. int filter_arg = (Z_ARR_P((zval *)arg) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV]))
  185. ? PARSE_ENV : PARSE_SERVER;
  186. size_t new_val_len;
  187. #else
  188. int filter_arg = (arg == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER;
  189. unsigned int new_val_len;
  190. #endif
  191. char * new_val = (char *) pValue;
  192. if (sapi_module.input_filter(filter_arg, (char *)pKey, &new_val, valLen, &new_val_len TSRMLS_CC)) {
  193. php_register_variable_safe((char *)pKey, new_val, new_val_len, (zval *)arg TSRMLS_CC);
  194. }
  195. return 1;
  196. }
  197. #else
  198. static int add_variable( const char * pKey, int keyLen, const char * pValue, int valLen,
  199. void * arg )
  200. {
  201. zval * gpc_element, **gpc_element_p;
  202. HashTable * symtable1 = Z_ARRVAL_P((zval * )arg);
  203. register char * pKey1 = (char *)pKey;
  204. MAKE_STD_ZVAL(gpc_element);
  205. Z_STRLEN_P( gpc_element ) = valLen;
  206. Z_STRVAL_P( gpc_element ) = estrndup(pValue, valLen);
  207. Z_TYPE_P( gpc_element ) = IS_STRING;
  208. zend_hash_update( symtable1, pKey1, keyLen + 1, &gpc_element, sizeof( zval *), (void **) &gpc_element_p );
  209. return 1;
  210. }
  211. #endif
  212. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  213. struct param_zts
  214. {
  215. void *arg;
  216. TSRMLS_D ;
  217. };
  218. static int add_variable_zts(const char * pKey, int keyLen, const char * pValue, int valLen,
  219. void * arg )
  220. {
  221. struct param_zts * params = (struct param_zts *)arg;
  222. return add_variable(pKey, keyLen, pValue, valLen, params->arg, params->TSRMLS_C);
  223. }
  224. #endif
  225. static void litespeed_php_import_environment_variables(zval *array_ptr TSRMLS_DC)
  226. {
  227. char buf[128];
  228. char **env, *p, *t = buf;
  229. size_t alloc_size = sizeof(buf);
  230. unsigned long nlen; /* ptrdiff_t is not portable */
  231. #if PHP_MAJOR_VERSION >= 7
  232. if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
  233. Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV]) &&
  234. zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV])) > 0
  235. ) {
  236. zval_dtor(array_ptr);
  237. ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_ENV]);
  238. return;
  239. } else if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
  240. Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]) &&
  241. zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER])) > 0
  242. ) {
  243. zval_dtor(array_ptr);
  244. ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_SERVER]);
  245. return;
  246. }
  247. #else
  248. if (PG(http_globals)[TRACK_VARS_ENV] &&
  249. array_ptr != PG(http_globals)[TRACK_VARS_ENV] &&
  250. Z_TYPE_P(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
  251. zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_ENV])) > 0
  252. ) {
  253. zval_dtor(array_ptr);
  254. *array_ptr = *PG(http_globals)[TRACK_VARS_ENV];
  255. INIT_PZVAL(array_ptr);
  256. zval_copy_ctor(array_ptr);
  257. return;
  258. } else if (PG(http_globals)[TRACK_VARS_SERVER] &&
  259. array_ptr != PG(http_globals)[TRACK_VARS_SERVER] &&
  260. Z_TYPE_P(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
  261. zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER])) > 0
  262. ) {
  263. zval_dtor(array_ptr);
  264. *array_ptr = *PG(http_globals)[TRACK_VARS_SERVER];
  265. INIT_PZVAL(array_ptr);
  266. zval_copy_ctor(array_ptr);
  267. return;
  268. }
  269. #endif
  270. for (env = environ; env != NULL && *env != NULL; env++) {
  271. p = strchr(*env, '=');
  272. if (!p) { /* malformed entry? */
  273. continue;
  274. }
  275. nlen = p - *env;
  276. if (nlen >= alloc_size) {
  277. alloc_size = nlen + 64;
  278. t = (t == buf ? emalloc(alloc_size): erealloc(t, alloc_size));
  279. }
  280. memcpy(t, *env, nlen);
  281. t[nlen] = '\0';
  282. add_variable(t, nlen, p + 1, strlen( p + 1 ), array_ptr TSRMLS_CC);
  283. }
  284. if (t != buf && t != NULL) {
  285. efree(t);
  286. }
  287. }
  288. #if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || PHP_MAJOR_VERSION < 5)
  289. static int add_variable_magic_quote( const char * pKey, int keyLen, const char * pValue, int valLen,
  290. void * arg )
  291. {
  292. zval * gpc_element, **gpc_element_p;
  293. HashTable * symtable1 = Z_ARRVAL_P((zval * )arg);
  294. register char * pKey1 = (char *)pKey;
  295. MAKE_STD_ZVAL(gpc_element);
  296. Z_STRLEN_P( gpc_element ) = valLen;
  297. Z_STRVAL_P( gpc_element ) = php_addslashes((char *)pValue, valLen, &Z_STRLEN_P( gpc_element ), 0 );
  298. Z_TYPE_P( gpc_element ) = IS_STRING;
  299. #if PHP_MAJOR_VERSION > 4
  300. zend_symtable_update( symtable1, pKey1, keyLen + 1, &gpc_element, sizeof( zval *), (void **) &gpc_element_p );
  301. #else
  302. zend_hash_update( symtable1, pKey1, keyLen + 1, &gpc_element, sizeof( zval *), (void **) &gpc_element_p );
  303. #endif
  304. return 1;
  305. }
  306. #endif
  307. /* {{{ sapi_lsapi_register_variables
  308. */
  309. static void sapi_lsapi_register_variables(zval *track_vars_array TSRMLS_DC)
  310. {
  311. char * php_self = "";
  312. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  313. struct param_zts params;
  314. params.arg = track_vars_array;
  315. params.TSRMLS_C = TSRMLS_C;
  316. #endif
  317. if ( lsapi_mode ) {
  318. if ( (SG(request_info).request_uri ) )
  319. php_self = (SG(request_info).request_uri );
  320. litespeed_php_import_environment_variables(track_vars_array TSRMLS_CC);
  321. #if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || PHP_MAJOR_VERSION < 5)
  322. if (!PG(magic_quotes_gpc)) {
  323. #endif
  324. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  325. LSAPI_ForeachHeader( add_variable_zts, &params );
  326. LSAPI_ForeachEnv( add_variable_zts, &params );
  327. #else
  328. LSAPI_ForeachHeader( add_variable, track_vars_array );
  329. LSAPI_ForeachEnv( add_variable, track_vars_array );
  330. #endif
  331. add_variable("PHP_SELF", 8, php_self, strlen( php_self ),
  332. track_vars_array TSRMLS_CC);
  333. #if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || PHP_MAJOR_VERSION < 5)
  334. } else {
  335. LSAPI_ForeachHeader( add_variable_magic_quote, track_vars_array );
  336. LSAPI_ForeachEnv( add_variable_magic_quote, track_vars_array );
  337. add_variable_magic_quote("PHP_SELF", 8, php_self, strlen( php_self ), track_vars_array );
  338. }
  339. #endif
  340. } else {
  341. php_import_environment_variables(track_vars_array TSRMLS_CC);
  342. php_register_variable("PHP_SELF", php_self, track_vars_array TSRMLS_CC);
  343. php_register_variable("SCRIPT_NAME", php_self, track_vars_array TSRMLS_CC);
  344. php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array TSRMLS_CC);
  345. php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array TSRMLS_CC);
  346. php_register_variable("DOCUMENT_ROOT", "", track_vars_array TSRMLS_CC);
  347. }
  348. }
  349. /* }}} */
  350. /* {{{ sapi_lsapi_read_post
  351. */
  352. #if PHP_MAJOR_VERSION >= 7
  353. static size_t sapi_lsapi_read_post(char *buffer, size_t count_bytes TSRMLS_DC)
  354. #else
  355. static int sapi_lsapi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
  356. #endif
  357. {
  358. if ( lsapi_mode ) {
  359. return LSAPI_ReadReqBody( buffer, (unsigned long long)count_bytes );
  360. } else {
  361. return 0;
  362. }
  363. }
  364. /* }}} */
  365. /* {{{ sapi_lsapi_read_cookies
  366. */
  367. static char *sapi_lsapi_read_cookies(TSRMLS_D)
  368. {
  369. if ( lsapi_mode ) {
  370. return LSAPI_GetHeader( H_COOKIE );
  371. } else {
  372. return NULL;
  373. }
  374. }
  375. /* }}} */
  376. /* {{{ sapi_lsapi_send_headers
  377. */
  378. static int sapi_lsapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
  379. {
  380. sapi_header_struct *h;
  381. zend_llist_position pos;
  382. if ( lsapi_mode ) {
  383. LSAPI_SetRespStatus( SG(sapi_headers).http_response_code );
  384. h = zend_llist_get_first_ex(&sapi_headers->headers, &pos);
  385. while (h) {
  386. if ( h->header_len > 0 ) {
  387. LSAPI_AppendRespHeader(h->header, h->header_len);
  388. }
  389. h = zend_llist_get_next_ex(&sapi_headers->headers, &pos);
  390. }
  391. if (SG(sapi_headers).send_default_content_type) {
  392. char *hd;
  393. int len;
  394. char headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
  395. hd = sapi_get_default_content_type(TSRMLS_C);
  396. len = snprintf( headerBuf, SAPI_LSAPI_MAX_HEADER_LENGTH - 1,
  397. "Content-type: %s", hd );
  398. efree(hd);
  399. LSAPI_AppendRespHeader( headerBuf, len );
  400. }
  401. }
  402. LSAPI_FinalizeRespHeaders();
  403. return SAPI_HEADER_SENT_SUCCESSFULLY;
  404. }
  405. /* }}} */
  406. /* {{{ sapi_lsapi_send_headers
  407. */
  408. static void sapi_lsapi_log_message(char *message
  409. #if PHP_MAJOR_VERSION > 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION >= 1)
  410. , int syslog_type_int /* unused */
  411. #endif
  412. TSRMLS_DC)
  413. {
  414. char buf[8192];
  415. int len = strlen( message );
  416. if ( *(message + len - 1 ) != '\n' )
  417. {
  418. snprintf( buf, 8191, "%s\n", message );
  419. message = buf;
  420. ++len;
  421. }
  422. LSAPI_Write_Stderr( message, len);
  423. }
  424. /* }}} */
  425. /* Set to 1 to turn on log messages useful during development:
  426. */
  427. #if 0
  428. static void log_message (const char *fmt, ...)
  429. {
  430. va_list ap;
  431. va_start(ap, fmt);
  432. char buf[0x100];
  433. vsnprintf(buf, sizeof(buf), fmt, ap);
  434. va_end(ap);
  435. sapi_lsapi_log_message(buf
  436. #if PHP_MAJOR_VERSION > 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION >= 1)
  437. , 0
  438. #endif
  439. );
  440. }
  441. #define DEBUG_MESSAGE(fmt, ...) log_message("LS:%d " fmt "\n", __LINE__, ##__VA_ARGS__)
  442. #else
  443. #define DEBUG_MESSAGE(fmt, ...)
  444. #endif
  445. /* {{{ sapi_module_struct cgi_sapi_module
  446. */
  447. static sapi_module_struct lsapi_sapi_module =
  448. {
  449. "litespeed",
  450. "LiteSpeed V6.10",
  451. php_lsapi_startup, /* startup */
  452. php_module_shutdown_wrapper, /* shutdown */
  453. NULL, /* activate */
  454. sapi_lsapi_deactivate, /* deactivate */
  455. sapi_lsapi_ub_write, /* unbuffered write */
  456. sapi_lsapi_flush, /* flush */
  457. NULL, /* get uid */
  458. sapi_lsapi_getenv, /* getenv */
  459. php_error, /* error handler */
  460. NULL, /* header handler */
  461. sapi_lsapi_send_headers, /* send headers handler */
  462. NULL, /* send header handler */
  463. sapi_lsapi_read_post, /* read POST data */
  464. sapi_lsapi_read_cookies, /* read Cookies */
  465. sapi_lsapi_register_variables, /* register server variables */
  466. sapi_lsapi_log_message, /* Log message */
  467. #if PHP_MAJOR_VERSION > 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION >= 1)
  468. NULL, /* Get request time */
  469. NULL, /* Child terminate */
  470. #else
  471. NULL, /* php.ini path override */
  472. NULL, /* block interruptions */
  473. NULL, /* unblock interruptions */
  474. NULL, /* default post reader */
  475. NULL, /* treat data */
  476. NULL, /* executable location */
  477. 0, /* php.ini ignore */
  478. #endif
  479. STANDARD_SAPI_MODULE_PROPERTIES
  480. };
  481. /* }}} */
  482. static void init_request_info( TSRMLS_D )
  483. {
  484. char * pContentType = LSAPI_GetHeader( H_CONTENT_TYPE );
  485. char * pAuth;
  486. SG(request_info).content_type = pContentType ? pContentType : "";
  487. SG(request_info).request_method = LSAPI_GetRequestMethod();
  488. SG(request_info).query_string = LSAPI_GetQueryString();
  489. SG(request_info).request_uri = LSAPI_GetScriptName();
  490. SG(request_info).content_length = LSAPI_GetReqBodyLen();
  491. SG(request_info).path_translated = estrdup( LSAPI_GetScriptFileName());
  492. /* It is not reset by zend engine, set it to 200. */
  493. SG(sapi_headers).http_response_code = 200;
  494. pAuth = LSAPI_GetHeader( H_AUTHORIZATION );
  495. php_handle_auth_data(pAuth TSRMLS_CC);
  496. }
  497. static char s_cur_chdir[4096] = "";
  498. static int lsapi_chdir_primary_script( zend_file_handle * file_handle )
  499. {
  500. #if PHP_MAJOR_VERSION > 4
  501. char * p;
  502. char ch;
  503. SG(options) |= SAPI_OPTION_NO_CHDIR;
  504. getcwd( s_cur_chdir, sizeof( s_cur_chdir ) );
  505. p = strrchr( file_handle->filename, '/' );
  506. if ( *p )
  507. {
  508. *p = 0;
  509. if ( strcmp( file_handle->filename, s_cur_chdir ) != 0 ) {
  510. chdir( file_handle->filename );
  511. }
  512. *p++ = '/';
  513. ch = *p;
  514. *p = 0;
  515. if ( !CWDG(cwd).cwd ||
  516. ( strcmp( file_handle->filename, CWDG(cwd).cwd ) != 0 ) ) {
  517. CWDG(cwd).cwd_length = p - file_handle->filename;
  518. CWDG(cwd).cwd = (char *) realloc(CWDG(cwd).cwd, CWDG(cwd).cwd_length+1);
  519. memmove( CWDG(cwd).cwd, file_handle->filename, CWDG(cwd).cwd_length+1 );
  520. }
  521. *p = ch;
  522. }
  523. /* virtual_file_ex(&CWDG(cwd), file_handle->filename, NULL, CWD_REALPATH); */
  524. #else
  525. VCWD_CHDIR_FILE( file_handle->filename );
  526. #endif
  527. return 0;
  528. }
  529. static int lsapi_fopen_primary_script( zend_file_handle * file_handle )
  530. {
  531. FILE * fp;
  532. char * p;
  533. fp = fopen( SG(request_info).path_translated, "rb" );
  534. if ( !fp )
  535. {
  536. return -1;
  537. }
  538. file_handle->type = ZEND_HANDLE_FP;
  539. file_handle->handle.fp = fp;
  540. file_handle->filename = SG(request_info).path_translated;
  541. file_handle->free_filename = 0;
  542. file_handle->opened_path = NULL;
  543. lsapi_chdir_primary_script( file_handle );
  544. return 0;
  545. }
  546. static int lsapi_execute_script( zend_file_handle * file_handle TSRMLS_DC)
  547. {
  548. char *p;
  549. int len;
  550. file_handle->type = ZEND_HANDLE_FILENAME;
  551. file_handle->handle.fd = 0;
  552. file_handle->filename = SG(request_info).path_translated;
  553. file_handle->free_filename = 0;
  554. file_handle->opened_path = NULL;
  555. p = argv0;
  556. *p++ = ':';
  557. len = strlen( SG(request_info).path_translated );
  558. if ( len > 45 )
  559. len = len - 45;
  560. else
  561. len = 0;
  562. memccpy( p, SG(request_info).path_translated + len, 0, 46 );
  563. php_execute_script(file_handle TSRMLS_CC);
  564. return 0;
  565. }
  566. static int lsapi_activate_user_ini(TSRMLS_D);
  567. static int lsapi_module_main(int show_source TSRMLS_DC)
  568. {
  569. zend_file_handle file_handle = {0};
  570. if (php_request_startup(TSRMLS_C) == FAILURE ) {
  571. return -1;
  572. }
  573. #if PHP_MAJOR_VERSION > 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3)
  574. if (parse_user_ini && lsapi_activate_user_ini(TSRMLS_C) == FAILURE) {
  575. return -1;
  576. }
  577. #endif
  578. if (show_source) {
  579. zend_syntax_highlighter_ini syntax_highlighter_ini;
  580. php_get_highlight_struct(&syntax_highlighter_ini);
  581. highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini TSRMLS_CC);
  582. } else {
  583. lsapi_execute_script( &file_handle TSRMLS_CC);
  584. }
  585. zend_try {
  586. php_request_shutdown(NULL);
  587. memset( argv0, 0, 46 );
  588. } zend_end_try();
  589. return 0;
  590. }
  591. static int alter_ini( const char * pKey, int keyLen, const char * pValue, int valLen,
  592. void * arg )
  593. {
  594. #if PHP_MAJOR_VERSION >= 7
  595. zend_string * psKey;
  596. #endif
  597. int type = ZEND_INI_PERDIR;
  598. if ( '\001' == *pKey ) {
  599. ++pKey;
  600. if ( *pKey == 4 ) {
  601. type = ZEND_INI_SYSTEM;
  602. }
  603. ++pKey;
  604. --keyLen;
  605. if (( keyLen == 7 )&&( strncasecmp( pKey, "engine", 6 )== 0 ))
  606. {
  607. if ( *pValue == '0' )
  608. engine = 0;
  609. }
  610. else
  611. {
  612. #if PHP_MAJOR_VERSION >= 7
  613. --keyLen;
  614. psKey = zend_string_init( pKey, keyLen, 1 );
  615. zend_alter_ini_entry_chars(psKey,
  616. (char *)pValue, valLen,
  617. type, PHP_INI_STAGE_ACTIVATE);
  618. zend_string_release( psKey );
  619. #else
  620. zend_alter_ini_entry((char *)pKey, keyLen,
  621. (char *)pValue, valLen,
  622. type, PHP_INI_STAGE_ACTIVATE);
  623. #endif
  624. }
  625. }
  626. return 1;
  627. }
  628. static void user_config_cache_entry_dtor(
  629. #if PHP_MAJOR_VERSION >= 7
  630. zval
  631. #else
  632. void
  633. #endif
  634. *el)
  635. {
  636. user_config_cache_entry *entry =
  637. #if PHP_MAJOR_VERSION >= 7
  638. (user_config_cache_entry *)Z_PTR_P(el)
  639. #else
  640. *(user_config_cache_entry **)el
  641. #endif
  642. ;
  643. zend_hash_destroy(&entry->user_config);
  644. free(entry);
  645. }
  646. static void user_config_cache_init()
  647. {
  648. zend_hash_init(&user_config_cache, 0, NULL, user_config_cache_entry_dtor, 1);
  649. }
  650. static int pathlen_without_trailing_slash(char *path)
  651. {
  652. int len = (int)strlen(path);
  653. while (len > 1 && /* mind "/" as root dir */
  654. path[len-1] == DEFAULT_SLASH)
  655. {
  656. --len;
  657. }
  658. return len;
  659. }
  660. static inline char* skip_slash(char *s)
  661. {
  662. while (*s == DEFAULT_SLASH) {
  663. ++s;
  664. }
  665. return s;
  666. }
  667. /**
  668. * Walk down the path_stop starting at path_start.
  669. *
  670. * If path_start = "/path1" and path_stop = "/path1/path2/path3"
  671. * the callback will be called 3 times with the next args:
  672. *
  673. * 1. "/path1/path2/path3"
  674. * ^ end
  675. * ^ start
  676. * 2. "/path1/path2/path3"
  677. * ^ end
  678. * ^ start
  679. * 3. "/path1/path2/path3"
  680. * ^ end
  681. * ^ start
  682. *
  683. * path_stop has to be a subdir of path_start
  684. * or to be path_start itself.
  685. *
  686. * Both path args have to be absolute.
  687. * Trailing slashes are allowed.
  688. * NULL or empty string args are not allowed.
  689. */
  690. static void walk_down_the_path(char* path_start,
  691. char* path_stop,
  692. void (*cb)(char* begin,
  693. char* end,
  694. void* data),
  695. void* data)
  696. {
  697. char *pos = path_stop + pathlen_without_trailing_slash(path_start);
  698. cb(path_stop, pos, data);
  699. while ((pos = skip_slash(pos))[0]) {
  700. pos = strchr(pos, DEFAULT_SLASH);
  701. if (!pos) {
  702. /* The last token without trailing slash
  703. */
  704. cb(path_stop, path_stop + strlen(path_stop), data);
  705. return;
  706. }
  707. cb(path_stop, pos, data);
  708. }
  709. }
  710. #if PHP_MAJOR_VERSION > 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3)
  711. typedef struct {
  712. char *path;
  713. uint path_len;
  714. char *doc_root;
  715. user_config_cache_entry *entry;
  716. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  717. TSRMLS_D ;
  718. #endif
  719. } _lsapi_activate_user_ini_ctx;
  720. typedef int (*fn_activate_user_ini_chain_t)
  721. (_lsapi_activate_user_ini_ctx *ctx, void* next);
  722. static int lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx *ctx,
  723. void* next)
  724. {
  725. int rc = SUCCESS;
  726. fn_activate_user_ini_chain_t *fn_next = next;
  727. if (!PG(user_ini_filename) || !*PG(user_ini_filename)) {
  728. return SUCCESS;
  729. }
  730. /* PATH_TRANSLATED should be defined at this stage */
  731. ctx->path = SG(request_info).path_translated;
  732. if (!ctx->path || !*ctx->path) {
  733. return FAILURE;
  734. }
  735. ctx->doc_root = sapi_lsapi_getenv("DOCUMENT_ROOT", 0
  736. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  737. , ctx->TSRMLS_C
  738. #endif
  739. );
  740. DEBUG_MESSAGE("doc_root: %s", ctx->doc_root);
  741. if (*fn_next) {
  742. rc = (*fn_next)(ctx, fn_next + 1);
  743. }
  744. return rc;
  745. }
  746. static int lsapi_activate_user_ini_mk_path(_lsapi_activate_user_ini_ctx *ctx,
  747. void* next)
  748. {
  749. char *path;
  750. int rc = SUCCESS;
  751. fn_activate_user_ini_chain_t *fn_next = next;
  752. /* Extract dir name from path_translated * and store it in 'path' */
  753. ctx->path_len = strlen(ctx->path);
  754. path = ctx->path = estrndup(SG(request_info).path_translated, ctx->path_len);
  755. if (!path)
  756. return FAILURE;
  757. ctx->path_len = zend_dirname(path, ctx->path_len);
  758. DEBUG_MESSAGE("dirname: %s", ctx->path);
  759. if (*fn_next) {
  760. rc = (*fn_next)(ctx, fn_next + 1);
  761. }
  762. efree(path);
  763. return rc;
  764. }
  765. static int lsapi_activate_user_ini_mk_realpath(_lsapi_activate_user_ini_ctx *ctx,
  766. void* next)
  767. {
  768. char *real_path;
  769. int rc = SUCCESS;
  770. fn_activate_user_ini_chain_t *fn_next = next;
  771. if (!IS_ABSOLUTE_PATH(ctx->path, ctx->path_len)) {
  772. real_path = tsrm_realpath(ctx->path, NULL
  773. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  774. , ctx->TSRMLS_C
  775. #endif
  776. );
  777. if (!real_path) {
  778. return SUCCESS;
  779. }
  780. ctx->path = real_path;
  781. ctx->path_len = strlen(ctx->path);
  782. DEBUG_MESSAGE("calculated tsrm realpath: %s", real_path);
  783. } else {
  784. DEBUG_MESSAGE("%s is an absolute path", ctx->path);
  785. real_path = NULL;
  786. }
  787. if (*fn_next) {
  788. rc = (*fn_next)(ctx, fn_next + 1);
  789. }
  790. if (real_path)
  791. efree(real_path);
  792. return rc;
  793. }
  794. static int lsapi_activate_user_ini_mk_user_config(_lsapi_activate_user_ini_ctx *ctx,
  795. void* next)
  796. {
  797. fn_activate_user_ini_chain_t *fn_next = next;
  798. /* Find cached config entry: If not found, create one */
  799. #if PHP_MAJOR_VERSION >= 7
  800. ctx->entry = zend_hash_str_find_ptr(&user_config_cache, ctx->path, ctx->path_len);
  801. #else
  802. {
  803. user_config_cache_entry **entry_pp;
  804. if (SUCCESS == zend_hash_find(&user_config_cache, ctx->path,
  805. ctx->path_len + 1, (void **) &entry_pp))
  806. ctx->entry = *entry_pp;
  807. else
  808. ctx->entry = NULL;
  809. }
  810. #endif
  811. if (ctx->entry) {
  812. DEBUG_MESSAGE("found entry for %s", ctx->path);
  813. } else {
  814. DEBUG_MESSAGE("entry for %s not found, creating new entry", ctx->path);
  815. ctx->entry = pemalloc(sizeof(user_config_cache_entry), 1);
  816. ctx->entry->expires = 0;
  817. zend_hash_init(&ctx->entry->user_config, 0, NULL,
  818. #if PHP_MAJOR_VERSION <= 5
  819. (dtor_func_t)
  820. #endif
  821. config_zval_dtor, 1);
  822. #if PHP_MAJOR_VERSION >= 7
  823. zend_hash_str_update_ptr(&user_config_cache, ctx->path,
  824. ctx->path_len, ctx->entry);
  825. #else
  826. zend_hash_update(&user_config_cache, ctx->path, ctx->path_len + 1,
  827. &ctx->entry, sizeof(&ctx->entry), NULL);
  828. #endif
  829. }
  830. if (*fn_next) {
  831. return (*fn_next)(ctx, fn_next + 1);
  832. } else {
  833. return SUCCESS;
  834. }
  835. }
  836. static void walk_down_the_path_callback(char* begin,
  837. char* end,
  838. void* data)
  839. {
  840. _lsapi_activate_user_ini_ctx *ctx = data;
  841. char tmp = end[0];
  842. end[0] = 0;
  843. DEBUG_MESSAGE("parsing %s%c%s", begin, DEFAULT_SLASH, PG(user_ini_filename));
  844. php_parse_user_ini_file(begin, PG(user_ini_filename), &ctx->entry->user_config
  845. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  846. , ctx->TSRMLS_C
  847. #endif
  848. );
  849. end[0] = tmp;
  850. }
  851. static int lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_ctx *ctx,
  852. void* next)
  853. {
  854. time_t request_time = sapi_get_request_time(
  855. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  856. ctx->TSRMLS_C
  857. #endif
  858. );
  859. uint path_len, docroot_len;
  860. int rc = SUCCESS;
  861. fn_activate_user_ini_chain_t *fn_next = next;
  862. if (!ctx->entry->expires || request_time > ctx->entry->expires)
  863. {
  864. docroot_len = ctx->doc_root && ctx->doc_root[0]
  865. ? pathlen_without_trailing_slash(ctx->doc_root)
  866. : 0;
  867. int is_outside_of_docroot = !docroot_len ||
  868. ctx->path_len < docroot_len ||
  869. strncmp(ctx->path, ctx->doc_root, docroot_len) != 0;
  870. if (is_outside_of_docroot) {
  871. php_parse_user_ini_file(ctx->path, PG(user_ini_filename), &ctx->entry->user_config
  872. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  873. , ctx->TSRMLS_C
  874. #endif
  875. );
  876. } else {
  877. walk_down_the_path(ctx->doc_root, ctx->path,
  878. &walk_down_the_path_callback, ctx);
  879. }
  880. ctx->entry->expires = request_time + PG(user_ini_cache_ttl);
  881. }
  882. if (*fn_next) {
  883. rc = (*fn_next)(ctx, fn_next + 1);
  884. }
  885. return rc;
  886. }
  887. static int lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx *ctx,
  888. void* next)
  889. {
  890. int rc = SUCCESS;
  891. fn_activate_user_ini_chain_t *fn_next = next;
  892. DEBUG_MESSAGE("calling php_ini_activate_config()");
  893. php_ini_activate_config(&ctx->entry->user_config, PHP_INI_PERDIR, PHP_INI_STAGE_HTACCESS
  894. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  895. , ctx->TSRMLS_C
  896. #endif
  897. );
  898. if (*fn_next) {
  899. rc = (*fn_next)(ctx, fn_next + 1);
  900. }
  901. return rc;
  902. }
  903. static int lsapi_activate_user_ini(TSRMLS_D)
  904. {
  905. _lsapi_activate_user_ini_ctx ctx;
  906. /**
  907. * The reason to have this function list stacked
  908. * is each function now can define a scoped destructor.
  909. *
  910. * Passing control via function pointer is a sign of low coupling,
  911. * which means dependencies between these functions are to be
  912. * controlled from a single place
  913. * (here below, by modifying this function list order)
  914. */
  915. static const fn_activate_user_ini_chain_t fn_chain[] = {
  916. &lsapi_activate_user_ini_basic_checks,
  917. &lsapi_activate_user_ini_mk_path,
  918. &lsapi_activate_user_ini_mk_realpath,
  919. &lsapi_activate_user_ini_mk_user_config,
  920. &lsapi_activate_user_ini_walk_down_the_path,
  921. &lsapi_activate_user_ini_finally,
  922. NULL
  923. };
  924. #if defined(ZTS) && PHP_MAJOR_VERSION < 7
  925. ctx.TSRMLS_C = TSRMLS_C;
  926. #endif
  927. return fn_chain[0](&ctx, (fn_activate_user_ini_chain_t*)(fn_chain + 1));
  928. }
  929. #endif
  930. static void override_ini()
  931. {
  932. LSAPI_ForeachSpecialEnv( alter_ini, NULL );
  933. }
  934. static int processReq( TSRMLS_D )
  935. {
  936. int ret = 0;
  937. zend_first_try {
  938. /* avoid server_context==NULL checks */
  939. SG(server_context) = (void *) 1;
  940. engine = 1;
  941. override_ini();
  942. if ( engine ) {
  943. init_request_info( TSRMLS_C );
  944. if ( lsapi_module_main( source_highlight TSRMLS_CC ) == -1 ) {
  945. ret = -1;
  946. }
  947. } else {
  948. LSAPI_AppendRespHeader( "status: 403", 11 );
  949. LSAPI_AppendRespHeader( "content-type: text/html", 23 );
  950. LSAPI_Write( "Forbidden: PHP engine is disable.\n", 34 );
  951. }
  952. } zend_end_try();
  953. return ret;
  954. }
  955. static void cli_usage( TSRMLS_D )
  956. {
  957. static const char * usage =
  958. "Usage: php\n"
  959. " php -[b|c|n|h|i|q|s|v|?] [<file>] [args...]\n"
  960. " Run in LSAPI mode, only '-b', '-s' and '-c' are effective\n"
  961. " Run in Command Line Interpreter mode when parameters are specified\n"
  962. "\n"
  963. " -b <address:port>|<port> Bind Path for external LSAPI Server mode\n"
  964. " -c <path>|<file> Look for php.ini file in this directory\n"
  965. " -n No php.ini file will be used\n"
  966. " -h This help\n"
  967. " -i PHP information\n"
  968. " -l Syntax check\n"
  969. " -q Quiet-mode. Suppress HTTP Header output.\n"
  970. " -s Display colour syntax highlighted source.\n"
  971. " -v Version number\n"
  972. " -? This help\n"
  973. "\n"
  974. " args... Arguments passed to script.\n";
  975. php_output_startup();
  976. php_output_activate(TSRMLS_C);
  977. php_printf( "%s", usage );
  978. #ifdef PHP_OUTPUT_NEWAPI
  979. php_output_end_all(TSRMLS_C);
  980. #else
  981. php_end_ob_buffers(1 TSRMLS_CC);
  982. #endif
  983. }
  984. static int parse_opt( int argc, char * argv[], int *climode,
  985. char **php_ini_path, char ** php_bind )
  986. {
  987. char ** p = &argv[1];
  988. char ** argend= &argv[argc];
  989. int c;
  990. while (( p < argend )&&(**p == '-' )) {
  991. c = *((*p)+1);
  992. ++p;
  993. switch( c ) {
  994. case 'b':
  995. if ( p >= argend ) {
  996. fprintf( stderr, "TCP or socket address must be specified following '-b' option.\n");
  997. return -1;
  998. }
  999. *php_bind = strdup(*p++);
  1000. break;
  1001. case 'c':
  1002. if ( p >= argend ) {
  1003. fprintf( stderr, "<path> or <file> must be specified following '-c' option.\n");
  1004. return -1;
  1005. }
  1006. *php_ini_path = strdup( *p++ );
  1007. break;
  1008. case 's':
  1009. source_highlight = 1;
  1010. break;
  1011. case 'n':
  1012. ignore_php_ini = 1;
  1013. break;
  1014. case '?':
  1015. if ( *((*(p-1))+2) == 's' )
  1016. exit( 99 );
  1017. case 'h':
  1018. case 'i':
  1019. case 'l':
  1020. case 'q':
  1021. case 'v':
  1022. default:
  1023. *climode = 1;
  1024. break;
  1025. }
  1026. }
  1027. if ( p - argv < argc ) {
  1028. *climode = 1;
  1029. }
  1030. return 0;
  1031. }
  1032. static int cli_main( int argc, char * argv[] )
  1033. {
  1034. static const char * ini_defaults[] = {
  1035. "report_zend_debug", "0",
  1036. "display_errors", "1",
  1037. "register_argc_argv", "1",
  1038. "html_errors", "0",
  1039. "implicit_flush", "1",
  1040. "output_buffering", "0",
  1041. "max_execution_time", "0",
  1042. "max_input_time", "-1",
  1043. NULL
  1044. };
  1045. const char ** ini;
  1046. char ** p = &argv[1];
  1047. char ** argend= &argv[argc];
  1048. int ret = -1;
  1049. int c;
  1050. #if PHP_MAJOR_VERSION >= 7
  1051. zend_string * psKey;
  1052. #endif
  1053. lsapi_mode = 0; /* enter CLI mode */
  1054. #ifdef PHP_WIN32
  1055. _fmode = _O_BINARY; /*sets default for file streams to binary */
  1056. setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
  1057. setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
  1058. setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
  1059. #endif
  1060. zend_first_try {
  1061. SG(server_context) = (void *) 1;
  1062. zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */
  1063. CG(in_compilation) = 0; /* not initialized but needed for several options */
  1064. SG(options) |= SAPI_OPTION_NO_CHDIR;
  1065. #if PHP_MAJOR_VERSION < 7
  1066. EG(uninitialized_zval_ptr) = NULL;
  1067. #endif
  1068. for( ini = ini_defaults; *ini; ini+=2 ) {
  1069. #if PHP_MAJOR_VERSION >= 7
  1070. psKey = zend_string_init( *ini, strlen( *ini ), 1 );
  1071. zend_alter_ini_entry_chars( psKey,
  1072. (char *)*(ini+1), strlen( *(ini+1) ),
  1073. PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  1074. zend_string_release( psKey );
  1075. #else
  1076. zend_alter_ini_entry( (char *)*ini, strlen( *ini )+1,
  1077. (char *)*(ini+1), strlen( *(ini+1) ),
  1078. PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  1079. #endif
  1080. }
  1081. while (( p < argend )&&(**p == '-' )) {
  1082. c = *((*p)+1);
  1083. ++p;
  1084. switch( c ) {
  1085. case 'q':
  1086. break;
  1087. case 'i':
  1088. if (php_request_startup(TSRMLS_C) != FAILURE) {
  1089. php_print_info(0xFFFFFFFF TSRMLS_CC);
  1090. #ifdef PHP_OUTPUT_NEWAPI
  1091. php_output_end_all(TSRMLS_C);
  1092. #else
  1093. php_end_ob_buffers(1 TSRMLS_CC);
  1094. #endif
  1095. php_request_shutdown( NULL );
  1096. ret = 0;
  1097. }
  1098. break;
  1099. case 'v':
  1100. if (php_request_startup(TSRMLS_C) != FAILURE) {
  1101. #if ZEND_DEBUG
  1102. php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
  1103. #else
  1104. php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
  1105. #endif
  1106. #ifdef PHP_OUTPUT_NEWAPI
  1107. php_output_end_all(TSRMLS_C);
  1108. #else
  1109. php_end_ob_buffers(1 TSRMLS_CC);
  1110. #endif
  1111. php_request_shutdown( NULL );
  1112. ret = 0;
  1113. }
  1114. break;
  1115. case 'c':
  1116. ++p;
  1117. /* fall through */
  1118. case 's':
  1119. break;
  1120. case 'l':
  1121. source_highlight = 2;
  1122. break;
  1123. case 'h':
  1124. case '?':
  1125. default:
  1126. cli_usage(TSRMLS_C);
  1127. ret = 0;
  1128. break;
  1129. }
  1130. }
  1131. if ( ret == -1 ) {
  1132. if ( *p ) {
  1133. zend_file_handle file_handle = {0};
  1134. file_handle.type = ZEND_HANDLE_FP;
  1135. file_handle.handle.fp = VCWD_FOPEN(*p, "rb");
  1136. if ( file_handle.handle.fp ) {
  1137. script_filename = *p;
  1138. php_self = *p;
  1139. SG(request_info).path_translated = estrdup(*p);
  1140. SG(request_info).argc = argc - (p - argv);
  1141. SG(request_info).argv = p;
  1142. if (php_request_startup(TSRMLS_C) == FAILURE ) {
  1143. fclose( file_handle.handle.fp );
  1144. ret = 2;
  1145. } else {
  1146. if (source_highlight == 1) {
  1147. zend_syntax_highlighter_ini syntax_highlighter_ini;
  1148. php_get_highlight_struct(&syntax_highlighter_ini);
  1149. highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini TSRMLS_CC);
  1150. } else if (source_highlight == 2) {
  1151. file_handle.filename = *p;
  1152. file_handle.free_filename = 0;
  1153. file_handle.opened_path = NULL;
  1154. ret = php_lint_script(&file_handle TSRMLS_CC);
  1155. if (ret==SUCCESS) {
  1156. zend_printf("No syntax errors detected in %s\n", file_handle.filename);
  1157. } else {
  1158. zend_printf("Errors parsing %s\n", file_handle.filename);
  1159. }
  1160. } else {
  1161. file_handle.filename = *p;
  1162. file_handle.free_filename = 0;
  1163. file_handle.opened_path = NULL;
  1164. php_execute_script(&file_handle TSRMLS_CC);
  1165. ret = EG(exit_status);
  1166. }
  1167. php_request_shutdown( NULL );
  1168. }
  1169. } else {
  1170. php_printf("Could not open input file: %s.\n", *p);
  1171. }
  1172. } else {
  1173. cli_usage(TSRMLS_C);
  1174. }
  1175. }
  1176. }zend_end_try();
  1177. php_module_shutdown(TSRMLS_C);
  1178. #ifdef ZTS
  1179. tsrm_shutdown();
  1180. #endif
  1181. return ret;
  1182. }
  1183. static int s_stop;
  1184. void litespeed_cleanup(int signal)
  1185. {
  1186. s_stop = signal;
  1187. }
  1188. void start_children( int children )
  1189. {
  1190. struct sigaction act, old_term, old_quit, old_int, old_usr1;
  1191. int running = 0;
  1192. int status;
  1193. pid_t pid;
  1194. /* Create a process group */
  1195. setsid();
  1196. /* Set up handler to kill children upon exit */
  1197. act.sa_flags = 0;
  1198. act.sa_handler = litespeed_cleanup;
  1199. if( sigaction( SIGTERM, &act, &old_term ) ||
  1200. sigaction( SIGINT, &act, &old_int ) ||
  1201. sigaction( SIGUSR1, &act, &old_usr1 ) ||
  1202. sigaction( SIGQUIT, &act, &old_quit )) {
  1203. perror( "Can't set signals" );
  1204. exit( 1 );
  1205. }
  1206. s_stop = 0;
  1207. while( 1 ) {
  1208. while((!s_stop )&&( running < children )) {
  1209. pid = fork();
  1210. switch( pid ) {
  1211. case 0: /* children process */
  1212. /* don't catch our signals */
  1213. sigaction( SIGTERM, &old_term, 0 );
  1214. sigaction( SIGQUIT, &old_quit, 0 );
  1215. sigaction( SIGINT, &old_int, 0 );
  1216. sigaction( SIGUSR1, &old_usr1, 0 );
  1217. return ;
  1218. case -1:
  1219. perror( "php (pre-forking)" );
  1220. exit( 1 );
  1221. break;
  1222. default: /* parent process */
  1223. running++;
  1224. break;
  1225. }
  1226. }
  1227. if ( s_stop ) {
  1228. break;
  1229. }
  1230. pid = wait( &status );
  1231. running--;
  1232. }
  1233. kill( -getpgrp(), SIGUSR1 );
  1234. exit( 0 );
  1235. }
  1236. void setArgv0( int argc, char * argv[] )
  1237. {
  1238. char * p;
  1239. int i;
  1240. argv0 = argv[0] + strlen( argv[0] );
  1241. p = argv0;
  1242. while(( p > argv[0] )&&( p[-1] != '/'))
  1243. --p;
  1244. if ( p > argv[0] )
  1245. {
  1246. memmove( argv[0], p, argv0 - p );
  1247. memset( argv[0] + ( argv0 - p ), 0, p - argv[0] );
  1248. argv0 = argv[0] + (argv0 - p);
  1249. }
  1250. for( i = 1; i < argc; ++i )
  1251. {
  1252. memset( argv[i], 0, strlen( argv[i] ) );
  1253. }
  1254. }
  1255. #include <fcntl.h>
  1256. int main( int argc, char * argv[] )
  1257. {
  1258. int ret;
  1259. int bindFd;
  1260. char * php_ini_path = NULL;
  1261. char * php_bind = NULL;
  1262. int n;
  1263. int climode = 0;
  1264. struct timeval tv_req_begin;
  1265. struct timeval tv_req_end;
  1266. int slow_script_msec = 0;
  1267. char time_buf[40];
  1268. #ifdef HAVE_SIGNAL_H
  1269. #if defined(SIGPIPE) && defined(SIG_IGN)
  1270. signal(SIGPIPE, SIG_IGN);
  1271. #endif
  1272. #endif
  1273. #ifdef ZTS
  1274. tsrm_startup(1, 1, 0, NULL);
  1275. #endif
  1276. if (argc > 1 ) {
  1277. if ( parse_opt( argc, argv, &climode,
  1278. &php_ini_path, &php_bind ) == -1 ) {
  1279. return 1;
  1280. }
  1281. }
  1282. if ( climode ) {
  1283. lsapi_sapi_module.phpinfo_as_text = 1;
  1284. } else {
  1285. setArgv0(argc, argv );
  1286. }
  1287. sapi_startup(&lsapi_sapi_module);
  1288. #ifdef ZTS
  1289. compiler_globals = ts_resource(compiler_globals_id);
  1290. executor_globals = ts_resource(executor_globals_id);
  1291. core_globals = ts_resource(core_globals_id);
  1292. sapi_globals = ts_resource(sapi_globals_id);
  1293. tsrm_ls = ts_resource(0);
  1294. SG(request_info).path_translated = NULL;
  1295. #endif
  1296. lsapi_sapi_module.executable_location = argv[0];
  1297. /* Initialize from environment variables before processing command-line
  1298. * options: the latter override the former.
  1299. */
  1300. init_sapi_from_env(&lsapi_sapi_module);
  1301. if ( ignore_php_ini )
  1302. lsapi_sapi_module.php_ini_ignore = 1;
  1303. if ( php_ini_path ) {
  1304. lsapi_sapi_module.php_ini_path_override = php_ini_path;
  1305. }
  1306. lsapi_sapi_module.ini_defaults = sapi_lsapi_ini_defaults;
  1307. if (php_module_startup(&lsapi_sapi_module, &litespeed_module_entry, 1) == FAILURE) {
  1308. #ifdef ZTS
  1309. tsrm_shutdown();
  1310. #endif
  1311. return FAILURE;
  1312. }
  1313. if ( climode ) {
  1314. return cli_main(argc, argv);
  1315. }
  1316. if ( php_bind ) {
  1317. bindFd = LSAPI_CreateListenSock( php_bind, 10 );
  1318. if ( bindFd == -1 ) {
  1319. fprintf( stderr,
  1320. "Failed to bind socket [%s]: %s\n", php_bind, strerror( errno ) );
  1321. exit( 2 );
  1322. }
  1323. if ( bindFd != 0 ) {
  1324. dup2( bindFd, 0 );
  1325. close( bindFd );
  1326. }
  1327. }
  1328. LSAPI_Init();
  1329. LSAPI_Init_Env_Parameters( NULL );
  1330. lsapi_mode = 1;
  1331. slow_script_msec = LSAPI_Get_Slow_Req_Msecs();
  1332. if ( php_bind ) {
  1333. LSAPI_No_Check_ppid();
  1334. free( php_bind );
  1335. php_bind = NULL;
  1336. }
  1337. while( LSAPI_Prefork_Accept_r( &g_req ) >= 0 ) {
  1338. if ( slow_script_msec ) {
  1339. gettimeofday( &tv_req_begin, NULL );
  1340. }
  1341. ret = processReq(TSRMLS_C);
  1342. if ( slow_script_msec ) {
  1343. gettimeofday( &tv_req_end, NULL );
  1344. n = ((long) tv_req_end.tv_sec - tv_req_begin.tv_sec ) * 1000
  1345. + (tv_req_end.tv_usec - tv_req_begin.tv_usec) / 1000;
  1346. if ( n > slow_script_msec )
  1347. {
  1348. strftime( time_buf, 30, "%d/%b/%Y:%H:%M:%S", localtime( &tv_req_end.tv_sec ) );
  1349. fprintf( stderr, "[%s] Slow PHP script: %d ms\n URL: %s %s\n Query String: %s\n Script: %s\n",
  1350. time_buf, n, LSAPI_GetRequestMethod(),
  1351. LSAPI_GetScriptName(), LSAPI_GetQueryString(),
  1352. LSAPI_GetScriptFileName() );
  1353. }
  1354. }
  1355. LSAPI_Finish();
  1356. if ( ret ) {
  1357. break;
  1358. }
  1359. }
  1360. php_module_shutdown(TSRMLS_C);
  1361. #ifdef ZTS
  1362. tsrm_shutdown();
  1363. #endif
  1364. return ret;
  1365. }
  1366. /* LiteSpeed PHP module starts here */
  1367. #if PHP_MAJOR_VERSION > 4
  1368. /* {{{ arginfo */
  1369. ZEND_BEGIN_ARG_INFO(arginfo_litespeed__void, 0)
  1370. ZEND_END_ARG_INFO()
  1371. /* }}} */
  1372. #else
  1373. #define arginfo_litespeed__void NULL
  1374. #endif
  1375. PHP_FUNCTION(litespeed_request_headers);
  1376. PHP_FUNCTION(litespeed_response_headers);
  1377. PHP_FUNCTION(apache_get_modules);
  1378. PHP_MINFO_FUNCTION(litespeed);
  1379. zend_function_entry litespeed_functions[] = {
  1380. PHP_FE(litespeed_request_headers, arginfo_litespeed__void)
  1381. PHP_FE(litespeed_response_headers, arginfo_litespeed__void)
  1382. PHP_FE(apache_get_modules, arginfo_litespeed__void)
  1383. PHP_FALIAS(getallheaders, litespeed_request_headers, arginfo_litespeed__void)
  1384. PHP_FALIAS(apache_request_headers, litespeed_request_headers, arginfo_litespeed__void)
  1385. PHP_FALIAS(apache_response_headers, litespeed_response_headers, arginfo_litespeed__void)
  1386. {NULL, NULL, NULL}
  1387. };
  1388. static PHP_MINIT_FUNCTION(litespeed)
  1389. {
  1390. user_config_cache_init();
  1391. const char *p = getenv("LSPHP_ENABLE_USER_INI");
  1392. if (p && 0 == strcasecmp(p, "on"))
  1393. parse_user_ini = 1;
  1394. /* REGISTER_INI_ENTRIES(); */
  1395. return SUCCESS;
  1396. }
  1397. static PHP_MSHUTDOWN_FUNCTION(litespeed)
  1398. {
  1399. zend_hash_destroy(&user_config_cache);
  1400. /* UNREGISTER_INI_ENTRIES(); */
  1401. return SUCCESS;
  1402. }
  1403. zend_module_entry litespeed_module_entry = {
  1404. STANDARD_MODULE_HEADER,
  1405. "litespeed",
  1406. litespeed_functions,
  1407. PHP_MINIT(litespeed),
  1408. PHP_MSHUTDOWN(litespeed),
  1409. NULL,
  1410. NULL,
  1411. NULL,
  1412. NO_VERSION_YET,
  1413. STANDARD_MODULE_PROPERTIES
  1414. };
  1415. static int add_associate_array( const char * pKey, int keyLen, const char * pValue, int valLen,
  1416. void * arg )
  1417. {
  1418. #if PHP_MAJOR_VERSION < 7
  1419. add_assoc_string_ex((zval *)arg, (char *)pKey, keyLen+1, (char *)pValue, 1);
  1420. #else
  1421. add_assoc_string_ex((zval *)arg, (char *)pKey, keyLen, (char *)pValue);
  1422. #endif
  1423. return 1;
  1424. }
  1425. /* {{{ proto array litespeed_request_headers(void)
  1426. Fetch all HTTP request headers */
  1427. PHP_FUNCTION(litespeed_request_headers)
  1428. {
  1429. /* TODO: */
  1430. if (ZEND_NUM_ARGS() > 0) {
  1431. WRONG_PARAM_COUNT;
  1432. }
  1433. array_init(return_value);
  1434. LSAPI_ForeachOrgHeader( add_associate_array, return_value );
  1435. }
  1436. /* }}} */
  1437. /* {{{ proto array litespeed_response_headers(void)
  1438. Fetch all HTTP response headers */
  1439. PHP_FUNCTION(litespeed_response_headers)
  1440. {
  1441. sapi_header_struct *h;
  1442. zend_llist_position pos;
  1443. char * p;
  1444. int len;
  1445. char headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
  1446. if (ZEND_NUM_ARGS() > 0) {
  1447. WRONG_PARAM_COUNT;
  1448. }
  1449. if (!&SG(sapi_headers).headers) {
  1450. RETURN_FALSE;
  1451. }
  1452. array_init(return_value);
  1453. h = zend_llist_get_first_ex(&SG(sapi_headers).headers, &pos);
  1454. while (h) {
  1455. if ( h->header_len > 0 ) {
  1456. p = strchr( h->header, ':' );
  1457. len = p - h->header;
  1458. if (( p )&&( len > 0 )) {
  1459. memmove( headerBuf, h->header, len );
  1460. while( len > 0 && (isspace( headerBuf[len-1])) ) {
  1461. --len;
  1462. }
  1463. headerBuf[len] = 0;
  1464. if ( len ) {
  1465. while( isspace(*++p));
  1466. #if PHP_MAJOR_VERSION < 7
  1467. add_assoc_string_ex(return_value, headerBuf, len+1, p, 1);
  1468. #else
  1469. add_assoc_string_ex(return_value, headerBuf, len, p);
  1470. #endif
  1471. }
  1472. }
  1473. }
  1474. h = zend_llist_get_next_ex(&SG(sapi_headers).headers, &pos);
  1475. }
  1476. }
  1477. /* }}} */
  1478. /* {{{ proto array apache_get_modules(void)
  1479. Fetch all loaded module names */
  1480. PHP_FUNCTION(apache_get_modules)
  1481. {
  1482. static const char * mod_names[] =
  1483. {
  1484. "mod_rewrite", "mod_mime", "mod_headers", "mod_expires", "mod_auth_basic", NULL
  1485. };
  1486. const char **name = mod_names;
  1487. /* TODO: */
  1488. if (ZEND_NUM_ARGS() > 0) {
  1489. WRONG_PARAM_COUNT;
  1490. }
  1491. array_init(return_value);
  1492. while( *name )
  1493. {
  1494. add_next_index_string(return_value, *name
  1495. #if PHP_MAJOR_VERSION < 7
  1496. , 1
  1497. #endif
  1498. );
  1499. ++name;
  1500. }
  1501. }
  1502. /* }}} */
  1503. /*
  1504. * Local variables:
  1505. * tab-width: 4
  1506. * c-basic-offset: 4
  1507. * End:
  1508. * vim600: sw=4 ts=4 fdm=marker
  1509. * vim<600: sw=4 ts=4
  1510. */