main.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
  17. | Zeev Suraski <zeev@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* {{{ includes
  21. */
  22. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  23. #include "php.h"
  24. #include <stdio.h>
  25. #include <fcntl.h>
  26. #ifdef PHP_WIN32
  27. #include "win32/time.h"
  28. #include "win32/signal.h"
  29. #include "win32/php_win32_globals.h"
  30. #include "win32/winutil.h"
  31. #include <process.h>
  32. #endif
  33. #if HAVE_SYS_TIME_H
  34. #include <sys/time.h>
  35. #endif
  36. #if HAVE_UNISTD_H
  37. #include <unistd.h>
  38. #endif
  39. #if HAVE_SIGNAL_H
  40. #include <signal.h>
  41. #endif
  42. #if HAVE_SETLOCALE
  43. #include <locale.h>
  44. #endif
  45. #include "zend.h"
  46. #include "zend_types.h"
  47. #include "zend_extensions.h"
  48. #include "php_ini.h"
  49. #include "php_globals.h"
  50. #include "php_main.h"
  51. #include "php_syslog.h"
  52. #include "fopen_wrappers.h"
  53. #include "ext/standard/php_standard.h"
  54. #include "ext/standard/php_string.h"
  55. #include "ext/date/php_date.h"
  56. #include "php_variables.h"
  57. #include "ext/standard/credits.h"
  58. #ifdef PHP_WIN32
  59. #include <io.h>
  60. #include "win32/php_registry.h"
  61. #include "ext/standard/flock_compat.h"
  62. #endif
  63. #include "php_syslog.h"
  64. #include "Zend/zend_exceptions.h"
  65. #if PHP_SIGCHILD
  66. #include <sys/types.h>
  67. #include <sys/wait.h>
  68. #endif
  69. #include "zend_compile.h"
  70. #include "zend_execute.h"
  71. #include "zend_highlight.h"
  72. #include "zend_extensions.h"
  73. #include "zend_ini.h"
  74. #include "zend_dtrace.h"
  75. #include "php_content_types.h"
  76. #include "php_ticks.h"
  77. #include "php_streams.h"
  78. #include "php_open_temporary_file.h"
  79. #include "SAPI.h"
  80. #include "rfc1867.h"
  81. #include "ext/standard/html_tables.h"
  82. #if HAVE_MMAP || defined(PHP_WIN32)
  83. # if HAVE_UNISTD_H
  84. # include <unistd.h>
  85. # if defined(_SC_PAGESIZE)
  86. # define REAL_PAGE_SIZE sysconf(_SC_PAGESIZE);
  87. # elif defined(_SC_PAGE_SIZE)
  88. # define REAL_PAGE_SIZE sysconf(_SC_PAGE_SIZE);
  89. # endif
  90. # endif
  91. # if HAVE_SYS_MMAN_H
  92. # include <sys/mman.h>
  93. # endif
  94. # ifndef REAL_PAGE_SIZE
  95. # ifdef PAGE_SIZE
  96. # define REAL_PAGE_SIZE PAGE_SIZE
  97. # else
  98. # define REAL_PAGE_SIZE 4096
  99. # endif
  100. # endif
  101. #endif
  102. /* }}} */
  103. PHPAPI int (*php_register_internal_extensions_func)(void) = php_register_internal_extensions;
  104. #ifndef ZTS
  105. php_core_globals core_globals;
  106. #else
  107. PHPAPI int core_globals_id;
  108. #endif
  109. #define SAFE_FILENAME(f) ((f)?(f):"-")
  110. static char *get_safe_charset_hint(void) {
  111. ZEND_TLS char *lastHint = NULL;
  112. ZEND_TLS char *lastCodeset = NULL;
  113. char *hint = SG(default_charset);
  114. size_t len = strlen(hint);
  115. size_t i = 0;
  116. if (lastHint == SG(default_charset)) {
  117. return lastCodeset;
  118. }
  119. lastHint = hint;
  120. lastCodeset = NULL;
  121. for (i = 0; i < sizeof(charset_map)/sizeof(charset_map[0]); i++) {
  122. if (len == charset_map[i].codeset_len
  123. && zend_binary_strcasecmp(hint, len, charset_map[i].codeset, len) == 0) {
  124. lastCodeset = (char*)charset_map[i].codeset;
  125. break;
  126. }
  127. }
  128. return lastCodeset;
  129. }
  130. /* {{{ PHP_INI_MH
  131. */
  132. static PHP_INI_MH(OnSetFacility)
  133. {
  134. const char *facility = ZSTR_VAL(new_value);
  135. #ifdef LOG_AUTH
  136. if (!strcmp(facility, "LOG_AUTH") || !strcmp(facility, "auth") || !strcmp(facility, "security")) {
  137. PG(syslog_facility) = LOG_AUTH;
  138. return SUCCESS;
  139. }
  140. #endif
  141. #ifdef LOG_AUTHPRIV
  142. if (!strcmp(facility, "LOG_AUTHPRIV") || !strcmp(facility, "authpriv")) {
  143. PG(syslog_facility) = LOG_AUTHPRIV;
  144. return SUCCESS;
  145. }
  146. #endif
  147. #ifdef LOG_CRON
  148. if (!strcmp(facility, "LOG_CRON") || !strcmp(facility, "cron")) {
  149. PG(syslog_facility) = LOG_CRON;
  150. return SUCCESS;
  151. }
  152. #endif
  153. #ifdef LOG_DAEMON
  154. if (!strcmp(facility, "LOG_DAEMON") || !strcmp(facility, "daemon")) {
  155. PG(syslog_facility) = LOG_DAEMON;
  156. return SUCCESS;
  157. }
  158. #endif
  159. #ifdef LOG_FTP
  160. if (!strcmp(facility, "LOG_FTP") || !strcmp(facility, "ftp")) {
  161. PG(syslog_facility) = LOG_FTP;
  162. return SUCCESS;
  163. }
  164. #endif
  165. #ifdef LOG_KERN
  166. if (!strcmp(facility, "LOG_KERN") || !strcmp(facility, "kern")) {
  167. PG(syslog_facility) = LOG_KERN;
  168. return SUCCESS;
  169. }
  170. #endif
  171. #ifdef LOG_LPR
  172. if (!strcmp(facility, "LOG_LPR") || !strcmp(facility, "lpr")) {
  173. PG(syslog_facility) = LOG_LPR;
  174. return SUCCESS;
  175. }
  176. #endif
  177. #ifdef LOG_MAIL
  178. if (!strcmp(facility, "LOG_MAIL") || !strcmp(facility, "mail")) {
  179. PG(syslog_facility) = LOG_MAIL;
  180. return SUCCESS;
  181. }
  182. #endif
  183. #ifdef LOG_INTERNAL_MARK
  184. if (!strcmp(facility, "LOG_INTERNAL_MARK") || !strcmp(facility, "mark")) {
  185. PG(syslog_facility) = LOG_INTERNAL_MARK;
  186. return SUCCESS;
  187. }
  188. #endif
  189. #ifdef LOG_NEWS
  190. if (!strcmp(facility, "LOG_NEWS") || !strcmp(facility, "news")) {
  191. PG(syslog_facility) = LOG_NEWS;
  192. return SUCCESS;
  193. }
  194. #endif
  195. #ifdef LOG_SYSLOG
  196. if (!strcmp(facility, "LOG_SYSLOG") || !strcmp(facility, "syslog")) {
  197. PG(syslog_facility) = LOG_SYSLOG;
  198. return SUCCESS;
  199. }
  200. #endif
  201. #ifdef LOG_USER
  202. if (!strcmp(facility, "LOG_USER") || !strcmp(facility, "user")) {
  203. PG(syslog_facility) = LOG_USER;
  204. return SUCCESS;
  205. }
  206. #endif
  207. #ifdef LOG_UUCP
  208. if (!strcmp(facility, "LOG_UUCP") || !strcmp(facility, "uucp")) {
  209. PG(syslog_facility) = LOG_UUCP;
  210. return SUCCESS;
  211. }
  212. #endif
  213. #ifdef LOG_LOCAL0
  214. if (!strcmp(facility, "LOG_LOCAL0") || !strcmp(facility, "local0")) {
  215. PG(syslog_facility) = LOG_LOCAL0;
  216. return SUCCESS;
  217. }
  218. #endif
  219. #ifdef LOG_LOCAL1
  220. if (!strcmp(facility, "LOG_LOCAL1") || !strcmp(facility, "local1")) {
  221. PG(syslog_facility) = LOG_LOCAL1;
  222. return SUCCESS;
  223. }
  224. #endif
  225. #ifdef LOG_LOCAL2
  226. if (!strcmp(facility, "LOG_LOCAL2") || !strcmp(facility, "local2")) {
  227. PG(syslog_facility) = LOG_LOCAL2;
  228. return SUCCESS;
  229. }
  230. #endif
  231. #ifdef LOG_LOCAL3
  232. if (!strcmp(facility, "LOG_LOCAL3") || !strcmp(facility, "local3")) {
  233. PG(syslog_facility) = LOG_LOCAL3;
  234. return SUCCESS;
  235. }
  236. #endif
  237. #ifdef LOG_LOCAL4
  238. if (!strcmp(facility, "LOG_LOCAL4") || !strcmp(facility, "local4")) {
  239. PG(syslog_facility) = LOG_LOCAL4;
  240. return SUCCESS;
  241. }
  242. #endif
  243. #ifdef LOG_LOCAL5
  244. if (!strcmp(facility, "LOG_LOCAL5") || !strcmp(facility, "local5")) {
  245. PG(syslog_facility) = LOG_LOCAL5;
  246. return SUCCESS;
  247. }
  248. #endif
  249. #ifdef LOG_LOCAL6
  250. if (!strcmp(facility, "LOG_LOCAL6") || !strcmp(facility, "local6")) {
  251. PG(syslog_facility) = LOG_LOCAL6;
  252. return SUCCESS;
  253. }
  254. #endif
  255. #ifdef LOG_LOCAL7
  256. if (!strcmp(facility, "LOG_LOCAL7") || !strcmp(facility, "local7")) {
  257. PG(syslog_facility) = LOG_LOCAL7;
  258. return SUCCESS;
  259. }
  260. #endif
  261. return FAILURE;
  262. }
  263. /* }}} */
  264. /* {{{ PHP_INI_MH
  265. */
  266. static PHP_INI_MH(OnSetPrecision)
  267. {
  268. zend_long i;
  269. ZEND_ATOL(i, ZSTR_VAL(new_value));
  270. if (i >= -1) {
  271. EG(precision) = i;
  272. return SUCCESS;
  273. } else {
  274. return FAILURE;
  275. }
  276. }
  277. /* }}} */
  278. /* {{{ PHP_INI_MH
  279. */
  280. static PHP_INI_MH(OnSetSerializePrecision)
  281. {
  282. zend_long i;
  283. ZEND_ATOL(i, ZSTR_VAL(new_value));
  284. if (i >= -1) {
  285. PG(serialize_precision) = i;
  286. return SUCCESS;
  287. } else {
  288. return FAILURE;
  289. }
  290. }
  291. /* }}} */
  292. /* {{{ PHP_INI_MH
  293. */
  294. static PHP_INI_MH(OnChangeMemoryLimit)
  295. {
  296. if (new_value) {
  297. PG(memory_limit) = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
  298. } else {
  299. PG(memory_limit) = Z_L(1)<<30; /* effectively, no limit */
  300. }
  301. return zend_set_memory_limit(PG(memory_limit));
  302. }
  303. /* }}} */
  304. /* {{{ PHP_INI_MH
  305. */
  306. static PHP_INI_MH(OnSetLogFilter)
  307. {
  308. const char *filter = ZSTR_VAL(new_value);
  309. if (!strcmp(filter, "all")) {
  310. PG(syslog_filter) = PHP_SYSLOG_FILTER_ALL;
  311. return SUCCESS;
  312. }
  313. if (!strcmp(filter, "no-ctrl")) {
  314. PG(syslog_filter) = PHP_SYSLOG_FILTER_NO_CTRL;
  315. return SUCCESS;
  316. }
  317. if (!strcmp(filter, "ascii")) {
  318. PG(syslog_filter) = PHP_SYSLOG_FILTER_ASCII;
  319. return SUCCESS;
  320. }
  321. if (!strcmp(filter, "raw")) {
  322. PG(syslog_filter) = PHP_SYSLOG_FILTER_RAW;
  323. return SUCCESS;
  324. }
  325. return FAILURE;
  326. }
  327. /* }}} */
  328. /* {{{ php_disable_functions
  329. */
  330. static void php_disable_functions(void)
  331. {
  332. char *s = NULL, *e;
  333. if (!*(INI_STR("disable_functions"))) {
  334. return;
  335. }
  336. e = PG(disable_functions) = strdup(INI_STR("disable_functions"));
  337. if (e == NULL) {
  338. return;
  339. }
  340. while (*e) {
  341. switch (*e) {
  342. case ' ':
  343. case ',':
  344. if (s) {
  345. *e = '\0';
  346. zend_disable_function(s, e-s);
  347. s = NULL;
  348. }
  349. break;
  350. default:
  351. if (!s) {
  352. s = e;
  353. }
  354. break;
  355. }
  356. e++;
  357. }
  358. if (s) {
  359. zend_disable_function(s, e-s);
  360. }
  361. }
  362. /* }}} */
  363. /* {{{ php_disable_classes
  364. */
  365. static void php_disable_classes(void)
  366. {
  367. char *s = NULL, *e;
  368. if (!*(INI_STR("disable_classes"))) {
  369. return;
  370. }
  371. e = PG(disable_classes) = strdup(INI_STR("disable_classes"));
  372. while (*e) {
  373. switch (*e) {
  374. case ' ':
  375. case ',':
  376. if (s) {
  377. *e = '\0';
  378. zend_disable_class(s, e-s);
  379. s = NULL;
  380. }
  381. break;
  382. default:
  383. if (!s) {
  384. s = e;
  385. }
  386. break;
  387. }
  388. e++;
  389. }
  390. if (s) {
  391. zend_disable_class(s, e-s);
  392. }
  393. }
  394. /* }}} */
  395. /* {{{ php_binary_init
  396. */
  397. static void php_binary_init(void)
  398. {
  399. char *binary_location = NULL;
  400. #ifdef PHP_WIN32
  401. binary_location = (char *)malloc(MAXPATHLEN);
  402. if (binary_location && GetModuleFileName(0, binary_location, MAXPATHLEN) == 0) {
  403. free(binary_location);
  404. PG(php_binary) = NULL;
  405. }
  406. #else
  407. if (sapi_module.executable_location) {
  408. binary_location = (char *)malloc(MAXPATHLEN);
  409. if (binary_location && !strchr(sapi_module.executable_location, '/')) {
  410. char *envpath, *path;
  411. int found = 0;
  412. if ((envpath = getenv("PATH")) != NULL) {
  413. char *search_dir, search_path[MAXPATHLEN];
  414. char *last = NULL;
  415. zend_stat_t s;
  416. path = estrdup(envpath);
  417. search_dir = php_strtok_r(path, ":", &last);
  418. while (search_dir) {
  419. snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location);
  420. if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK) && VCWD_STAT(binary_location, &s) == 0 && S_ISREG(s.st_mode)) {
  421. found = 1;
  422. break;
  423. }
  424. search_dir = php_strtok_r(NULL, ":", &last);
  425. }
  426. efree(path);
  427. }
  428. if (!found) {
  429. free(binary_location);
  430. binary_location = NULL;
  431. }
  432. } else if (!VCWD_REALPATH(sapi_module.executable_location, binary_location) || VCWD_ACCESS(binary_location, X_OK)) {
  433. free(binary_location);
  434. binary_location = NULL;
  435. }
  436. }
  437. #endif
  438. PG(php_binary) = binary_location;
  439. }
  440. /* }}} */
  441. /* {{{ PHP_INI_MH
  442. */
  443. static PHP_INI_MH(OnUpdateTimeout)
  444. {
  445. if (stage==PHP_INI_STAGE_STARTUP) {
  446. /* Don't set a timeout on startup, only per-request */
  447. ZEND_ATOL(EG(timeout_seconds), ZSTR_VAL(new_value));
  448. return SUCCESS;
  449. }
  450. zend_unset_timeout();
  451. ZEND_ATOL(EG(timeout_seconds), ZSTR_VAL(new_value));
  452. zend_set_timeout(EG(timeout_seconds), 0);
  453. return SUCCESS;
  454. }
  455. /* }}} */
  456. /* {{{ php_get_display_errors_mode() helper function
  457. */
  458. static int php_get_display_errors_mode(char *value, size_t value_length)
  459. {
  460. int mode;
  461. if (!value) {
  462. return PHP_DISPLAY_ERRORS_STDOUT;
  463. }
  464. if (value_length == 2 && !strcasecmp("on", value)) {
  465. mode = PHP_DISPLAY_ERRORS_STDOUT;
  466. } else if (value_length == 3 && !strcasecmp("yes", value)) {
  467. mode = PHP_DISPLAY_ERRORS_STDOUT;
  468. } else if (value_length == 4 && !strcasecmp("true", value)) {
  469. mode = PHP_DISPLAY_ERRORS_STDOUT;
  470. } else if (value_length == 6 && !strcasecmp(value, "stderr")) {
  471. mode = PHP_DISPLAY_ERRORS_STDERR;
  472. } else if (value_length == 6 && !strcasecmp(value, "stdout")) {
  473. mode = PHP_DISPLAY_ERRORS_STDOUT;
  474. } else {
  475. ZEND_ATOL(mode, value);
  476. if (mode && mode != PHP_DISPLAY_ERRORS_STDOUT && mode != PHP_DISPLAY_ERRORS_STDERR) {
  477. mode = PHP_DISPLAY_ERRORS_STDOUT;
  478. }
  479. }
  480. return mode;
  481. }
  482. /* }}} */
  483. /* {{{ PHP_INI_MH
  484. */
  485. static PHP_INI_MH(OnUpdateDisplayErrors)
  486. {
  487. PG(display_errors) = (zend_bool) php_get_display_errors_mode(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
  488. return SUCCESS;
  489. }
  490. /* }}} */
  491. /* {{{ PHP_INI_DISP
  492. */
  493. static PHP_INI_DISP(display_errors_mode)
  494. {
  495. int mode, cgi_or_cli;
  496. size_t tmp_value_length;
  497. char *tmp_value;
  498. if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) {
  499. tmp_value = (ini_entry->orig_value ? ZSTR_VAL(ini_entry->orig_value) : NULL );
  500. tmp_value_length = (ini_entry->orig_value? ZSTR_LEN(ini_entry->orig_value) : 0);
  501. } else if (ini_entry->value) {
  502. tmp_value = ZSTR_VAL(ini_entry->value);
  503. tmp_value_length = ZSTR_LEN(ini_entry->value);
  504. } else {
  505. tmp_value = NULL;
  506. tmp_value_length = 0;
  507. }
  508. mode = php_get_display_errors_mode(tmp_value, tmp_value_length);
  509. /* Display 'On' for other SAPIs instead of STDOUT or STDERR */
  510. cgi_or_cli = (!strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi") || !strcmp(sapi_module.name, "phpdbg"));
  511. switch (mode) {
  512. case PHP_DISPLAY_ERRORS_STDERR:
  513. if (cgi_or_cli ) {
  514. PUTS("STDERR");
  515. } else {
  516. PUTS("On");
  517. }
  518. break;
  519. case PHP_DISPLAY_ERRORS_STDOUT:
  520. if (cgi_or_cli ) {
  521. PUTS("STDOUT");
  522. } else {
  523. PUTS("On");
  524. }
  525. break;
  526. default:
  527. PUTS("Off");
  528. break;
  529. }
  530. }
  531. /* }}} */
  532. /* {{{ PHP_INI_MH
  533. */
  534. static PHP_INI_MH(OnUpdateDefaultCharset)
  535. {
  536. OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  537. if (new_value) {
  538. #ifdef PHP_WIN32
  539. php_win32_cp_do_update(ZSTR_VAL(new_value));
  540. #endif
  541. }
  542. return SUCCESS;
  543. }
  544. /* }}} */
  545. /* {{{ PHP_INI_MH
  546. */
  547. static PHP_INI_MH(OnUpdateInternalEncoding)
  548. {
  549. OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  550. if (new_value) {
  551. #ifdef PHP_WIN32
  552. php_win32_cp_do_update(ZSTR_VAL(new_value));
  553. #endif
  554. }
  555. return SUCCESS;
  556. }
  557. /* }}} */
  558. /* {{{ PHP_INI_MH
  559. */
  560. static PHP_INI_MH(OnUpdateInputEncoding)
  561. {
  562. OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  563. if (new_value) {
  564. #ifdef PHP_WIN32
  565. php_win32_cp_do_update(NULL);
  566. #endif
  567. }
  568. return SUCCESS;
  569. }
  570. /* }}} */
  571. /* {{{ PHP_INI_MH
  572. */
  573. static PHP_INI_MH(OnUpdateOutputEncoding)
  574. {
  575. OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  576. if (new_value) {
  577. #ifdef PHP_WIN32
  578. php_win32_cp_do_update(NULL);
  579. #endif
  580. }
  581. return SUCCESS;
  582. }
  583. /* }}} */
  584. /* {{{ PHP_INI_MH
  585. */
  586. static PHP_INI_MH(OnUpdateErrorLog)
  587. {
  588. /* Only do the safemode/open_basedir check at runtime */
  589. if ((stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) && new_value && strcmp(ZSTR_VAL(new_value), "syslog")) {
  590. if (PG(open_basedir) && php_check_open_basedir(ZSTR_VAL(new_value))) {
  591. return FAILURE;
  592. }
  593. }
  594. OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  595. return SUCCESS;
  596. }
  597. /* }}} */
  598. /* {{{ PHP_INI_MH
  599. */
  600. static PHP_INI_MH(OnUpdateMailLog)
  601. {
  602. /* Only do the safemode/open_basedir check at runtime */
  603. if ((stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) && new_value) {
  604. if (PG(open_basedir) && php_check_open_basedir(ZSTR_VAL(new_value))) {
  605. return FAILURE;
  606. }
  607. }
  608. OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  609. return SUCCESS;
  610. }
  611. /* }}} */
  612. /* {{{ PHP_INI_MH
  613. */
  614. static PHP_INI_MH(OnChangeMailForceExtra)
  615. {
  616. /* Don't allow changing it in htaccess */
  617. if (stage == PHP_INI_STAGE_HTACCESS) {
  618. return FAILURE;
  619. }
  620. return SUCCESS;
  621. }
  622. /* }}} */
  623. /* defined in browscap.c */
  624. PHP_INI_MH(OnChangeBrowscap);
  625. /* Need to be read from the environment (?):
  626. * PHP_AUTO_PREPEND_FILE
  627. * PHP_AUTO_APPEND_FILE
  628. * PHP_DOCUMENT_ROOT
  629. * PHP_USER_DIR
  630. * PHP_INCLUDE_PATH
  631. */
  632. /* Windows use the internal mail */
  633. #if defined(PHP_WIN32)
  634. # define DEFAULT_SENDMAIL_PATH NULL
  635. #else
  636. # define DEFAULT_SENDMAIL_PATH PHP_PROG_SENDMAIL " -t -i"
  637. #endif
  638. /* {{{ PHP_INI
  639. */
  640. PHP_INI_BEGIN()
  641. PHP_INI_ENTRY_EX("highlight.comment", HL_COMMENT_COLOR, PHP_INI_ALL, NULL, php_ini_color_displayer_cb)
  642. PHP_INI_ENTRY_EX("highlight.default", HL_DEFAULT_COLOR, PHP_INI_ALL, NULL, php_ini_color_displayer_cb)
  643. PHP_INI_ENTRY_EX("highlight.html", HL_HTML_COLOR, PHP_INI_ALL, NULL, php_ini_color_displayer_cb)
  644. PHP_INI_ENTRY_EX("highlight.keyword", HL_KEYWORD_COLOR, PHP_INI_ALL, NULL, php_ini_color_displayer_cb)
  645. PHP_INI_ENTRY_EX("highlight.string", HL_STRING_COLOR, PHP_INI_ALL, NULL, php_ini_color_displayer_cb)
  646. STD_PHP_INI_ENTRY_EX("display_errors", "1", PHP_INI_ALL, OnUpdateDisplayErrors, display_errors, php_core_globals, core_globals, display_errors_mode)
  647. STD_PHP_INI_BOOLEAN("display_startup_errors", "0", PHP_INI_ALL, OnUpdateBool, display_startup_errors, php_core_globals, core_globals)
  648. STD_PHP_INI_BOOLEAN("enable_dl", "1", PHP_INI_SYSTEM, OnUpdateBool, enable_dl, php_core_globals, core_globals)
  649. STD_PHP_INI_BOOLEAN("expose_php", "1", PHP_INI_SYSTEM, OnUpdateBool, expose_php, php_core_globals, core_globals)
  650. STD_PHP_INI_ENTRY("docref_root", "", PHP_INI_ALL, OnUpdateString, docref_root, php_core_globals, core_globals)
  651. STD_PHP_INI_ENTRY("docref_ext", "", PHP_INI_ALL, OnUpdateString, docref_ext, php_core_globals, core_globals)
  652. STD_PHP_INI_BOOLEAN("html_errors", "1", PHP_INI_ALL, OnUpdateBool, html_errors, php_core_globals, core_globals)
  653. STD_PHP_INI_BOOLEAN("xmlrpc_errors", "0", PHP_INI_SYSTEM, OnUpdateBool, xmlrpc_errors, php_core_globals, core_globals)
  654. STD_PHP_INI_ENTRY("xmlrpc_error_number", "0", PHP_INI_ALL, OnUpdateLong, xmlrpc_error_number, php_core_globals, core_globals)
  655. STD_PHP_INI_ENTRY("max_input_time", "-1", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLong, max_input_time, php_core_globals, core_globals)
  656. STD_PHP_INI_BOOLEAN("ignore_user_abort", "0", PHP_INI_ALL, OnUpdateBool, ignore_user_abort, php_core_globals, core_globals)
  657. STD_PHP_INI_BOOLEAN("implicit_flush", "0", PHP_INI_ALL, OnUpdateBool, implicit_flush, php_core_globals, core_globals)
  658. STD_PHP_INI_BOOLEAN("log_errors", "0", PHP_INI_ALL, OnUpdateBool, log_errors, php_core_globals, core_globals)
  659. STD_PHP_INI_ENTRY("log_errors_max_len", "1024", PHP_INI_ALL, OnUpdateLong, log_errors_max_len, php_core_globals, core_globals)
  660. STD_PHP_INI_BOOLEAN("ignore_repeated_errors", "0", PHP_INI_ALL, OnUpdateBool, ignore_repeated_errors, php_core_globals, core_globals)
  661. STD_PHP_INI_BOOLEAN("ignore_repeated_source", "0", PHP_INI_ALL, OnUpdateBool, ignore_repeated_source, php_core_globals, core_globals)
  662. STD_PHP_INI_BOOLEAN("report_memleaks", "1", PHP_INI_ALL, OnUpdateBool, report_memleaks, php_core_globals, core_globals)
  663. STD_PHP_INI_BOOLEAN("report_zend_debug", "1", PHP_INI_ALL, OnUpdateBool, report_zend_debug, php_core_globals, core_globals)
  664. STD_PHP_INI_ENTRY("output_buffering", "0", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateLong, output_buffering, php_core_globals, core_globals)
  665. STD_PHP_INI_ENTRY("output_handler", NULL, PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateString, output_handler, php_core_globals, core_globals)
  666. STD_PHP_INI_BOOLEAN("register_argc_argv", "1", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, register_argc_argv, php_core_globals, core_globals)
  667. STD_PHP_INI_BOOLEAN("auto_globals_jit", "1", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, auto_globals_jit, php_core_globals, core_globals)
  668. STD_PHP_INI_BOOLEAN("short_open_tag", DEFAULT_SHORT_OPEN_TAG, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, short_tags, zend_compiler_globals, compiler_globals)
  669. STD_PHP_INI_BOOLEAN("track_errors", "0", PHP_INI_ALL, OnUpdateBool, track_errors, php_core_globals, core_globals)
  670. STD_PHP_INI_ENTRY("unserialize_callback_func", NULL, PHP_INI_ALL, OnUpdateString, unserialize_callback_func, php_core_globals, core_globals)
  671. STD_PHP_INI_ENTRY("serialize_precision", "-1", PHP_INI_ALL, OnSetSerializePrecision, serialize_precision, php_core_globals, core_globals)
  672. STD_PHP_INI_ENTRY("arg_separator.output", "&", PHP_INI_ALL, OnUpdateStringUnempty, arg_separator.output, php_core_globals, core_globals)
  673. STD_PHP_INI_ENTRY("arg_separator.input", "&", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateStringUnempty, arg_separator.input, php_core_globals, core_globals)
  674. STD_PHP_INI_ENTRY("auto_append_file", NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateString, auto_append_file, php_core_globals, core_globals)
  675. STD_PHP_INI_ENTRY("auto_prepend_file", NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateString, auto_prepend_file, php_core_globals, core_globals)
  676. STD_PHP_INI_ENTRY("doc_root", NULL, PHP_INI_SYSTEM, OnUpdateStringUnempty, doc_root, php_core_globals, core_globals)
  677. STD_PHP_INI_ENTRY("default_charset", PHP_DEFAULT_CHARSET, PHP_INI_ALL, OnUpdateDefaultCharset, default_charset, sapi_globals_struct, sapi_globals)
  678. STD_PHP_INI_ENTRY("default_mimetype", SAPI_DEFAULT_MIMETYPE, PHP_INI_ALL, OnUpdateString, default_mimetype, sapi_globals_struct, sapi_globals)
  679. STD_PHP_INI_ENTRY("internal_encoding", NULL, PHP_INI_ALL, OnUpdateInternalEncoding, internal_encoding, php_core_globals, core_globals)
  680. STD_PHP_INI_ENTRY("input_encoding", NULL, PHP_INI_ALL, OnUpdateInputEncoding, input_encoding, php_core_globals, core_globals)
  681. STD_PHP_INI_ENTRY("output_encoding", NULL, PHP_INI_ALL, OnUpdateOutputEncoding, output_encoding, php_core_globals, core_globals)
  682. STD_PHP_INI_ENTRY("error_log", NULL, PHP_INI_ALL, OnUpdateErrorLog, error_log, php_core_globals, core_globals)
  683. STD_PHP_INI_ENTRY("extension_dir", PHP_EXTENSION_DIR, PHP_INI_SYSTEM, OnUpdateStringUnempty, extension_dir, php_core_globals, core_globals)
  684. STD_PHP_INI_ENTRY("sys_temp_dir", NULL, PHP_INI_SYSTEM, OnUpdateStringUnempty, sys_temp_dir, php_core_globals, core_globals)
  685. STD_PHP_INI_ENTRY("include_path", PHP_INCLUDE_PATH, PHP_INI_ALL, OnUpdateStringUnempty, include_path, php_core_globals, core_globals)
  686. PHP_INI_ENTRY("max_execution_time", "30", PHP_INI_ALL, OnUpdateTimeout)
  687. STD_PHP_INI_ENTRY("open_basedir", NULL, PHP_INI_ALL, OnUpdateBaseDir, open_basedir, php_core_globals, core_globals)
  688. STD_PHP_INI_BOOLEAN("file_uploads", "1", PHP_INI_SYSTEM, OnUpdateBool, file_uploads, php_core_globals, core_globals)
  689. STD_PHP_INI_ENTRY("upload_max_filesize", "2M", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLong, upload_max_filesize, php_core_globals, core_globals)
  690. STD_PHP_INI_ENTRY("post_max_size", "8M", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLong, post_max_size, sapi_globals_struct,sapi_globals)
  691. STD_PHP_INI_ENTRY("upload_tmp_dir", NULL, PHP_INI_SYSTEM, OnUpdateStringUnempty, upload_tmp_dir, php_core_globals, core_globals)
  692. STD_PHP_INI_ENTRY("max_input_nesting_level", "64", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLongGEZero, max_input_nesting_level, php_core_globals, core_globals)
  693. STD_PHP_INI_ENTRY("max_input_vars", "1000", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLongGEZero, max_input_vars, php_core_globals, core_globals)
  694. STD_PHP_INI_ENTRY("user_dir", NULL, PHP_INI_SYSTEM, OnUpdateString, user_dir, php_core_globals, core_globals)
  695. STD_PHP_INI_ENTRY("variables_order", "EGPCS", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateStringUnempty, variables_order, php_core_globals, core_globals)
  696. STD_PHP_INI_ENTRY("request_order", NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateString, request_order, php_core_globals, core_globals)
  697. STD_PHP_INI_ENTRY("error_append_string", NULL, PHP_INI_ALL, OnUpdateString, error_append_string, php_core_globals, core_globals)
  698. STD_PHP_INI_ENTRY("error_prepend_string", NULL, PHP_INI_ALL, OnUpdateString, error_prepend_string, php_core_globals, core_globals)
  699. PHP_INI_ENTRY("SMTP", "localhost",PHP_INI_ALL, NULL)
  700. PHP_INI_ENTRY("smtp_port", "25", PHP_INI_ALL, NULL)
  701. STD_PHP_INI_BOOLEAN("mail.add_x_header", "0", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, mail_x_header, php_core_globals, core_globals)
  702. STD_PHP_INI_ENTRY("mail.log", NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateMailLog, mail_log, php_core_globals, core_globals)
  703. PHP_INI_ENTRY("browscap", NULL, PHP_INI_SYSTEM, OnChangeBrowscap)
  704. PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit)
  705. PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision)
  706. PHP_INI_ENTRY("sendmail_from", NULL, PHP_INI_ALL, NULL)
  707. PHP_INI_ENTRY("sendmail_path", DEFAULT_SENDMAIL_PATH, PHP_INI_SYSTEM, NULL)
  708. PHP_INI_ENTRY("mail.force_extra_parameters",NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnChangeMailForceExtra)
  709. PHP_INI_ENTRY("disable_functions", "", PHP_INI_SYSTEM, NULL)
  710. PHP_INI_ENTRY("disable_classes", "", PHP_INI_SYSTEM, NULL)
  711. PHP_INI_ENTRY("max_file_uploads", "20", PHP_INI_SYSTEM|PHP_INI_PERDIR, NULL)
  712. STD_PHP_INI_BOOLEAN("allow_url_fopen", "1", PHP_INI_SYSTEM, OnUpdateBool, allow_url_fopen, php_core_globals, core_globals)
  713. STD_PHP_INI_BOOLEAN("allow_url_include", "0", PHP_INI_SYSTEM, OnUpdateBool, allow_url_include, php_core_globals, core_globals)
  714. STD_PHP_INI_BOOLEAN("enable_post_data_reading", "1", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, enable_post_data_reading, php_core_globals, core_globals)
  715. STD_PHP_INI_ENTRY("realpath_cache_size", "4096K", PHP_INI_SYSTEM, OnUpdateLong, realpath_cache_size_limit, virtual_cwd_globals, cwd_globals)
  716. STD_PHP_INI_ENTRY("realpath_cache_ttl", "120", PHP_INI_SYSTEM, OnUpdateLong, realpath_cache_ttl, virtual_cwd_globals, cwd_globals)
  717. STD_PHP_INI_ENTRY("user_ini.filename", ".user.ini", PHP_INI_SYSTEM, OnUpdateString, user_ini_filename, php_core_globals, core_globals)
  718. STD_PHP_INI_ENTRY("user_ini.cache_ttl", "300", PHP_INI_SYSTEM, OnUpdateLong, user_ini_cache_ttl, php_core_globals, core_globals)
  719. STD_PHP_INI_ENTRY("hard_timeout", "2", PHP_INI_SYSTEM, OnUpdateLong, hard_timeout, zend_executor_globals, executor_globals)
  720. #ifdef PHP_WIN32
  721. STD_PHP_INI_BOOLEAN("windows.show_crt_warning", "0", PHP_INI_ALL, OnUpdateBool, windows_show_crt_warning, php_core_globals, core_globals)
  722. #endif
  723. STD_PHP_INI_ENTRY("syslog.facility", "LOG_USER", PHP_INI_SYSTEM, OnSetFacility, syslog_facility, php_core_globals, core_globals)
  724. STD_PHP_INI_ENTRY("syslog.ident", "php", PHP_INI_SYSTEM, OnUpdateString, syslog_ident, php_core_globals, core_globals)
  725. STD_PHP_INI_ENTRY("syslog.filter", "no-ctrl", PHP_INI_ALL, OnSetLogFilter, syslog_filter, php_core_globals, core_globals)
  726. PHP_INI_END()
  727. /* }}} */
  728. /* True globals (no need for thread safety */
  729. /* But don't make them a single int bitfield */
  730. static int module_initialized = 0;
  731. static int module_startup = 1;
  732. static int module_shutdown = 0;
  733. /* {{{ php_during_module_startup */
  734. static int php_during_module_startup(void)
  735. {
  736. return module_startup;
  737. }
  738. /* }}} */
  739. /* {{{ php_during_module_shutdown */
  740. static int php_during_module_shutdown(void)
  741. {
  742. return module_shutdown;
  743. }
  744. /* }}} */
  745. /* {{{ php_get_module_initialized
  746. */
  747. PHPAPI int php_get_module_initialized(void)
  748. {
  749. return module_initialized;
  750. }
  751. /* }}} */
  752. /* {{{ php_log_err_with_severity
  753. */
  754. PHPAPI ZEND_COLD void php_log_err_with_severity(char *log_message, int syslog_type_int)
  755. {
  756. int fd = -1;
  757. time_t error_time;
  758. if (PG(in_error_log)) {
  759. /* prevent recursive invocation */
  760. return;
  761. }
  762. PG(in_error_log) = 1;
  763. /* Try to use the specified logging location. */
  764. if (PG(error_log) != NULL) {
  765. #ifdef HAVE_SYSLOG_H
  766. if (!strcmp(PG(error_log), "syslog")) {
  767. php_syslog(syslog_type_int, "%s", log_message);
  768. PG(in_error_log) = 0;
  769. return;
  770. }
  771. #endif
  772. fd = VCWD_OPEN_MODE(PG(error_log), O_CREAT | O_APPEND | O_WRONLY, 0644);
  773. if (fd != -1) {
  774. char *tmp;
  775. size_t len;
  776. zend_string *error_time_str;
  777. time(&error_time);
  778. #ifdef ZTS
  779. if (!php_during_module_startup()) {
  780. error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 1);
  781. } else {
  782. error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 0);
  783. }
  784. #else
  785. error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 1);
  786. #endif
  787. len = spprintf(&tmp, 0, "[%s] %s%s", ZSTR_VAL(error_time_str), log_message, PHP_EOL);
  788. #ifdef PHP_WIN32
  789. php_flock(fd, 2);
  790. /* XXX should eventually write in a loop if len > UINT_MAX */
  791. php_ignore_value(write(fd, tmp, (unsigned)len));
  792. #else
  793. php_ignore_value(write(fd, tmp, len));
  794. #endif
  795. efree(tmp);
  796. zend_string_free(error_time_str);
  797. close(fd);
  798. PG(in_error_log) = 0;
  799. return;
  800. }
  801. }
  802. /* Otherwise fall back to the default logging location, if we have one */
  803. if (sapi_module.log_message) {
  804. sapi_module.log_message(log_message, syslog_type_int);
  805. }
  806. PG(in_error_log) = 0;
  807. }
  808. /* }}} */
  809. /* {{{ php_write
  810. wrapper for modules to use PHPWRITE */
  811. PHPAPI size_t php_write(void *buf, size_t size)
  812. {
  813. return PHPWRITE(buf, size);
  814. }
  815. /* }}} */
  816. /* {{{ php_printf
  817. */
  818. PHPAPI size_t php_printf(const char *format, ...)
  819. {
  820. va_list args;
  821. size_t ret;
  822. char *buffer;
  823. size_t size;
  824. va_start(args, format);
  825. size = vspprintf(&buffer, 0, format, args);
  826. ret = PHPWRITE(buffer, size);
  827. efree(buffer);
  828. va_end(args);
  829. return ret;
  830. }
  831. /* }}} */
  832. /* {{{ php_verror */
  833. /* php_verror is called from php_error_docref<n> functions.
  834. * Its purpose is to unify error messages and automatically generate clickable
  835. * html error messages if correcponding ini setting (html_errors) is activated.
  836. * See: CODING_STANDARDS for details.
  837. */
  838. PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int type, const char *format, va_list args)
  839. {
  840. zend_string *replace_buffer = NULL, *replace_origin = NULL;
  841. char *buffer = NULL, *docref_buf = NULL, *target = NULL;
  842. char *docref_target = "", *docref_root = "";
  843. char *p;
  844. int buffer_len = 0;
  845. const char *space = "";
  846. const char *class_name = "";
  847. const char *function;
  848. int origin_len;
  849. char *origin;
  850. char *message;
  851. int is_function = 0;
  852. /* get error text into buffer and escape for html if necessary */
  853. buffer_len = (int)vspprintf(&buffer, 0, format, args);
  854. if (PG(html_errors)) {
  855. replace_buffer = php_escape_html_entities((unsigned char*)buffer, buffer_len, 0, ENT_COMPAT, get_safe_charset_hint());
  856. /* Retry with substituting invalid chars on fail. */
  857. if (!replace_buffer || ZSTR_LEN(replace_buffer) < 1) {
  858. replace_buffer = php_escape_html_entities((unsigned char*)buffer, buffer_len, 0, ENT_COMPAT | ENT_HTML_SUBSTITUTE_ERRORS, get_safe_charset_hint());
  859. }
  860. efree(buffer);
  861. if (replace_buffer) {
  862. buffer = ZSTR_VAL(replace_buffer);
  863. buffer_len = (int)ZSTR_LEN(replace_buffer);
  864. } else {
  865. buffer = "";
  866. buffer_len = 0;
  867. }
  868. }
  869. /* which function caused the problem if any at all */
  870. if (php_during_module_startup()) {
  871. function = "PHP Startup";
  872. } else if (php_during_module_shutdown()) {
  873. function = "PHP Shutdown";
  874. } else if (EG(current_execute_data) &&
  875. EG(current_execute_data)->func &&
  876. ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
  877. EG(current_execute_data)->opline &&
  878. EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL
  879. ) {
  880. switch (EG(current_execute_data)->opline->extended_value) {
  881. case ZEND_EVAL:
  882. function = "eval";
  883. is_function = 1;
  884. break;
  885. case ZEND_INCLUDE:
  886. function = "include";
  887. is_function = 1;
  888. break;
  889. case ZEND_INCLUDE_ONCE:
  890. function = "include_once";
  891. is_function = 1;
  892. break;
  893. case ZEND_REQUIRE:
  894. function = "require";
  895. is_function = 1;
  896. break;
  897. case ZEND_REQUIRE_ONCE:
  898. function = "require_once";
  899. is_function = 1;
  900. break;
  901. default:
  902. function = "Unknown";
  903. }
  904. } else {
  905. function = get_active_function_name();
  906. if (!function || !strlen(function)) {
  907. function = "Unknown";
  908. } else {
  909. is_function = 1;
  910. class_name = get_active_class_name(&space);
  911. }
  912. }
  913. /* if we still have memory then format the origin */
  914. if (is_function) {
  915. origin_len = (int)spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, params);
  916. } else {
  917. origin_len = (int)spprintf(&origin, 0, "%s", function);
  918. }
  919. if (PG(html_errors)) {
  920. replace_origin = php_escape_html_entities((unsigned char*)origin, origin_len, 0, ENT_COMPAT, get_safe_charset_hint());
  921. efree(origin);
  922. origin = ZSTR_VAL(replace_origin);
  923. }
  924. /* origin and buffer available, so lets come up with the error message */
  925. if (docref && docref[0] == '#') {
  926. docref_target = strchr(docref, '#');
  927. docref = NULL;
  928. }
  929. /* no docref given but function is known (the default) */
  930. if (!docref && is_function) {
  931. int doclen;
  932. while (*function == '_') {
  933. function++;
  934. }
  935. if (space[0] == '\0') {
  936. doclen = (int)spprintf(&docref_buf, 0, "function.%s", function);
  937. } else {
  938. doclen = (int)spprintf(&docref_buf, 0, "%s.%s", class_name, function);
  939. }
  940. while((p = strchr(docref_buf, '_')) != NULL) {
  941. *p = '-';
  942. }
  943. docref = php_strtolower(docref_buf, doclen);
  944. }
  945. /* we have a docref for a function AND
  946. * - we show errors in html mode AND
  947. * - the user wants to see the links
  948. */
  949. if (docref && is_function && PG(html_errors) && strlen(PG(docref_root))) {
  950. if (strncmp(docref, "http://", 7)) {
  951. /* We don't have 'http://' so we use docref_root */
  952. char *ref; /* temp copy for duplicated docref */
  953. docref_root = PG(docref_root);
  954. ref = estrdup(docref);
  955. if (docref_buf) {
  956. efree(docref_buf);
  957. }
  958. docref_buf = ref;
  959. /* strip of the target if any */
  960. p = strrchr(ref, '#');
  961. if (p) {
  962. target = estrdup(p);
  963. if (target) {
  964. docref_target = target;
  965. *p = '\0';
  966. }
  967. }
  968. /* add the extension if it is set in ini */
  969. if (PG(docref_ext) && strlen(PG(docref_ext))) {
  970. spprintf(&docref_buf, 0, "%s%s", ref, PG(docref_ext));
  971. efree(ref);
  972. }
  973. docref = docref_buf;
  974. }
  975. /* display html formatted or only show the additional links */
  976. if (PG(html_errors)) {
  977. spprintf(&message, 0, "%s [<a href='%s%s%s'>%s</a>]: %s", origin, docref_root, docref, docref_target, docref, buffer);
  978. } else {
  979. spprintf(&message, 0, "%s [%s%s%s]: %s", origin, docref_root, docref, docref_target, buffer);
  980. }
  981. if (target) {
  982. efree(target);
  983. }
  984. } else {
  985. spprintf(&message, 0, "%s: %s", origin, buffer);
  986. }
  987. if (replace_origin) {
  988. zend_string_free(replace_origin);
  989. } else {
  990. efree(origin);
  991. }
  992. if (docref_buf) {
  993. efree(docref_buf);
  994. }
  995. if (PG(track_errors) && module_initialized && EG(active) &&
  996. (Z_TYPE(EG(user_error_handler)) == IS_UNDEF || !(EG(user_error_handler_error_reporting) & type))) {
  997. zval tmp;
  998. ZVAL_STRINGL(&tmp, buffer, buffer_len);
  999. if (EG(current_execute_data)) {
  1000. if (zend_set_local_var_str("php_errormsg", sizeof("php_errormsg")-1, &tmp, 0) == FAILURE) {
  1001. zval_ptr_dtor(&tmp);
  1002. }
  1003. } else {
  1004. zend_hash_str_update_ind(&EG(symbol_table), "php_errormsg", sizeof("php_errormsg")-1, &tmp);
  1005. }
  1006. }
  1007. if (replace_buffer) {
  1008. zend_string_free(replace_buffer);
  1009. } else {
  1010. if (buffer_len > 0) {
  1011. efree(buffer);
  1012. }
  1013. }
  1014. php_error(type, "%s", message);
  1015. efree(message);
  1016. }
  1017. /* }}} */
  1018. /* {{{ php_error_docref0 */
  1019. /* See: CODING_STANDARDS for details. */
  1020. PHPAPI ZEND_COLD void php_error_docref0(const char *docref, int type, const char *format, ...)
  1021. {
  1022. va_list args;
  1023. va_start(args, format);
  1024. php_verror(docref, "", type, format, args);
  1025. va_end(args);
  1026. }
  1027. /* }}} */
  1028. /* {{{ php_error_docref1 */
  1029. /* See: CODING_STANDARDS for details. */
  1030. PHPAPI ZEND_COLD void php_error_docref1(const char *docref, const char *param1, int type, const char *format, ...)
  1031. {
  1032. va_list args;
  1033. va_start(args, format);
  1034. php_verror(docref, param1, type, format, args);
  1035. va_end(args);
  1036. }
  1037. /* }}} */
  1038. /* {{{ php_error_docref2 */
  1039. /* See: CODING_STANDARDS for details. */
  1040. PHPAPI ZEND_COLD void php_error_docref2(const char *docref, const char *param1, const char *param2, int type, const char *format, ...)
  1041. {
  1042. char *params;
  1043. va_list args;
  1044. spprintf(&params, 0, "%s,%s", param1, param2);
  1045. va_start(args, format);
  1046. php_verror(docref, params ? params : "...", type, format, args);
  1047. va_end(args);
  1048. if (params) {
  1049. efree(params);
  1050. }
  1051. }
  1052. /* }}} */
  1053. #ifdef PHP_WIN32
  1054. #define PHP_WIN32_ERROR_MSG_BUFFER_SIZE 512
  1055. PHPAPI ZEND_COLD void php_win32_docref2_from_error(DWORD error, const char *param1, const char *param2) {
  1056. if (error == 0) {
  1057. php_error_docref2(NULL, param1, param2, E_WARNING, "%s", strerror(errno));
  1058. } else {
  1059. char buf[PHP_WIN32_ERROR_MSG_BUFFER_SIZE + 1];
  1060. size_t buf_len;
  1061. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, buf, PHP_WIN32_ERROR_MSG_BUFFER_SIZE, NULL);
  1062. buf_len = strlen(buf);
  1063. if (buf_len >= 2) {
  1064. buf[buf_len - 1] = '\0';
  1065. buf[buf_len - 2] = '\0';
  1066. }
  1067. php_error_docref2(NULL, param1, param2, E_WARNING, "%s (code: %lu)", (char *)buf, error);
  1068. }
  1069. }
  1070. #undef PHP_WIN32_ERROR_MSG_BUFFER_SIZE
  1071. #endif
  1072. /* {{{ php_html_puts */
  1073. PHPAPI void php_html_puts(const char *str, size_t size)
  1074. {
  1075. zend_html_puts(str, size);
  1076. }
  1077. /* }}} */
  1078. /* {{{ php_error_cb
  1079. extended error handling function */
  1080. static ZEND_COLD void php_error_cb(int type, const char *error_filename, const uint32_t error_lineno, const char *format, va_list args)
  1081. {
  1082. char *buffer;
  1083. int buffer_len, display;
  1084. buffer_len = (int)vspprintf(&buffer, PG(log_errors_max_len), format, args);
  1085. /* check for repeated errors to be ignored */
  1086. if (PG(ignore_repeated_errors) && PG(last_error_message)) {
  1087. /* no check for PG(last_error_file) is needed since it cannot
  1088. * be NULL if PG(last_error_message) is not NULL */
  1089. if (strcmp(PG(last_error_message), buffer)
  1090. || (!PG(ignore_repeated_source)
  1091. && ((PG(last_error_lineno) != (int)error_lineno)
  1092. || strcmp(PG(last_error_file), error_filename)))) {
  1093. display = 1;
  1094. } else {
  1095. display = 0;
  1096. }
  1097. } else {
  1098. display = 1;
  1099. }
  1100. /* according to error handling mode, throw exception or show it */
  1101. if (EG(error_handling) == EH_THROW) {
  1102. switch (type) {
  1103. case E_ERROR:
  1104. case E_CORE_ERROR:
  1105. case E_COMPILE_ERROR:
  1106. case E_USER_ERROR:
  1107. case E_PARSE:
  1108. /* fatal errors are real errors and cannot be made exceptions */
  1109. break;
  1110. case E_STRICT:
  1111. case E_DEPRECATED:
  1112. case E_USER_DEPRECATED:
  1113. /* for the sake of BC to old damaged code */
  1114. break;
  1115. case E_NOTICE:
  1116. case E_USER_NOTICE:
  1117. /* notices are no errors and are not treated as such like E_WARNINGS */
  1118. break;
  1119. default:
  1120. /* throw an exception if we are in EH_THROW mode
  1121. * but DO NOT overwrite a pending exception
  1122. */
  1123. if (!EG(exception)) {
  1124. zend_throw_error_exception(EG(exception_class), buffer, 0, type);
  1125. }
  1126. efree(buffer);
  1127. return;
  1128. }
  1129. }
  1130. /* store the error if it has changed */
  1131. if (display) {
  1132. if (PG(last_error_message)) {
  1133. char *s = PG(last_error_message);
  1134. PG(last_error_message) = NULL;
  1135. free(s);
  1136. }
  1137. if (PG(last_error_file)) {
  1138. char *s = PG(last_error_file);
  1139. PG(last_error_file) = NULL;
  1140. free(s);
  1141. }
  1142. if (!error_filename) {
  1143. error_filename = "Unknown";
  1144. }
  1145. PG(last_error_type) = type;
  1146. PG(last_error_message) = strdup(buffer);
  1147. PG(last_error_file) = strdup(error_filename);
  1148. PG(last_error_lineno) = error_lineno;
  1149. }
  1150. /* display/log the error if necessary */
  1151. if (display && (EG(error_reporting) & type || (type & E_CORE))
  1152. && (PG(log_errors) || PG(display_errors) || (!module_initialized))) {
  1153. char *error_type_str;
  1154. int syslog_type_int = LOG_NOTICE;
  1155. switch (type) {
  1156. case E_ERROR:
  1157. case E_CORE_ERROR:
  1158. case E_COMPILE_ERROR:
  1159. case E_USER_ERROR:
  1160. error_type_str = "Fatal error";
  1161. syslog_type_int = LOG_ERR;
  1162. break;
  1163. case E_RECOVERABLE_ERROR:
  1164. error_type_str = "Recoverable fatal error";
  1165. syslog_type_int = LOG_ERR;
  1166. break;
  1167. case E_WARNING:
  1168. case E_CORE_WARNING:
  1169. case E_COMPILE_WARNING:
  1170. case E_USER_WARNING:
  1171. error_type_str = "Warning";
  1172. syslog_type_int = LOG_WARNING;
  1173. break;
  1174. case E_PARSE:
  1175. error_type_str = "Parse error";
  1176. syslog_type_int = LOG_ERR;
  1177. break;
  1178. case E_NOTICE:
  1179. case E_USER_NOTICE:
  1180. error_type_str = "Notice";
  1181. syslog_type_int = LOG_NOTICE;
  1182. break;
  1183. case E_STRICT:
  1184. error_type_str = "Strict Standards";
  1185. syslog_type_int = LOG_INFO;
  1186. break;
  1187. case E_DEPRECATED:
  1188. case E_USER_DEPRECATED:
  1189. error_type_str = "Deprecated";
  1190. syslog_type_int = LOG_INFO;
  1191. break;
  1192. default:
  1193. error_type_str = "Unknown error";
  1194. break;
  1195. }
  1196. if (!module_initialized || PG(log_errors)) {
  1197. char *log_buffer;
  1198. #ifdef PHP_WIN32
  1199. if (type == E_CORE_ERROR || type == E_CORE_WARNING) {
  1200. syslog(LOG_ALERT, "PHP %s: %s (%s)", error_type_str, buffer, GetCommandLine());
  1201. }
  1202. #endif
  1203. spprintf(&log_buffer, 0, "PHP %s: %s in %s on line %" PRIu32, error_type_str, buffer, error_filename, error_lineno);
  1204. php_log_err_with_severity(log_buffer, syslog_type_int);
  1205. efree(log_buffer);
  1206. }
  1207. if (PG(display_errors) && ((module_initialized && !PG(during_request_startup)) || (PG(display_startup_errors)))) {
  1208. if (PG(xmlrpc_errors)) {
  1209. php_printf("<?xml version=\"1.0\"?><methodResponse><fault><value><struct><member><name>faultCode</name><value><int>" ZEND_LONG_FMT "</int></value></member><member><name>faultString</name><value><string>%s:%s in %s on line %" PRIu32 "</string></value></member></struct></value></fault></methodResponse>", PG(xmlrpc_error_number), error_type_str, buffer, error_filename, error_lineno);
  1210. } else {
  1211. char *prepend_string = INI_STR("error_prepend_string");
  1212. char *append_string = INI_STR("error_append_string");
  1213. if (PG(html_errors)) {
  1214. if (type == E_ERROR || type == E_PARSE) {
  1215. zend_string *buf = php_escape_html_entities((unsigned char*)buffer, buffer_len, 0, ENT_COMPAT, get_safe_charset_hint());
  1216. php_printf("%s<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(buf), error_filename, error_lineno, STR_PRINT(append_string));
  1217. zend_string_free(buf);
  1218. } else {
  1219. php_printf("%s<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, buffer, error_filename, error_lineno, STR_PRINT(append_string));
  1220. }
  1221. } else {
  1222. /* Write CLI/CGI errors to stderr if display_errors = "stderr" */
  1223. if ((!strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi") || !strcmp(sapi_module.name, "phpdbg")) &&
  1224. PG(display_errors) == PHP_DISPLAY_ERRORS_STDERR
  1225. ) {
  1226. fprintf(stderr, "%s: %s in %s on line %" PRIu32 "\n", error_type_str, buffer, error_filename, error_lineno);
  1227. #ifdef PHP_WIN32
  1228. fflush(stderr);
  1229. #endif
  1230. } else {
  1231. php_printf("%s\n%s: %s in %s on line %" PRIu32 "\n%s", STR_PRINT(prepend_string), error_type_str, buffer, error_filename, error_lineno, STR_PRINT(append_string));
  1232. }
  1233. }
  1234. }
  1235. }
  1236. #if ZEND_DEBUG
  1237. if (PG(report_zend_debug)) {
  1238. zend_bool trigger_break;
  1239. switch (type) {
  1240. case E_ERROR:
  1241. case E_CORE_ERROR:
  1242. case E_COMPILE_ERROR:
  1243. case E_USER_ERROR:
  1244. trigger_break=1;
  1245. break;
  1246. default:
  1247. trigger_break=0;
  1248. break;
  1249. }
  1250. zend_output_debug_string(trigger_break, "%s(%" PRIu32 ") : %s - %s", error_filename, error_lineno, error_type_str, buffer);
  1251. }
  1252. #endif
  1253. }
  1254. /* Bail out if we can't recover */
  1255. switch (type) {
  1256. case E_CORE_ERROR:
  1257. if(!module_initialized) {
  1258. /* bad error in module startup - no way we can live with this */
  1259. exit(-2);
  1260. }
  1261. /* no break - intentionally */
  1262. case E_ERROR:
  1263. case E_RECOVERABLE_ERROR:
  1264. case E_PARSE:
  1265. case E_COMPILE_ERROR:
  1266. case E_USER_ERROR:
  1267. EG(exit_status) = 255;
  1268. if (module_initialized) {
  1269. if (!PG(display_errors) &&
  1270. !SG(headers_sent) &&
  1271. SG(sapi_headers).http_response_code == 200
  1272. ) {
  1273. sapi_header_line ctr = {0};
  1274. ctr.line = "HTTP/1.0 500 Internal Server Error";
  1275. ctr.line_len = sizeof("HTTP/1.0 500 Internal Server Error") - 1;
  1276. sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
  1277. }
  1278. /* the parser would return 1 (failure), we can bail out nicely */
  1279. if (type != E_PARSE) {
  1280. /* restore memory limit */
  1281. zend_set_memory_limit(PG(memory_limit));
  1282. efree(buffer);
  1283. zend_objects_store_mark_destructed(&EG(objects_store));
  1284. zend_bailout();
  1285. return;
  1286. }
  1287. }
  1288. break;
  1289. }
  1290. /* Log if necessary */
  1291. if (!display) {
  1292. efree(buffer);
  1293. return;
  1294. }
  1295. if (PG(track_errors) && module_initialized && EG(active)) {
  1296. zval tmp;
  1297. ZVAL_STRINGL(&tmp, buffer, buffer_len);
  1298. if (EG(current_execute_data)) {
  1299. if (zend_set_local_var_str("php_errormsg", sizeof("php_errormsg")-1, &tmp, 0) == FAILURE) {
  1300. zval_ptr_dtor(&tmp);
  1301. }
  1302. } else {
  1303. zend_hash_str_update_ind(&EG(symbol_table), "php_errormsg", sizeof("php_errormsg")-1, &tmp);
  1304. }
  1305. }
  1306. efree(buffer);
  1307. }
  1308. /* }}} */
  1309. /* {{{ php_get_current_user
  1310. */
  1311. PHPAPI char *php_get_current_user(void)
  1312. {
  1313. zend_stat_t *pstat;
  1314. if (SG(request_info).current_user) {
  1315. return SG(request_info).current_user;
  1316. }
  1317. /* FIXME: I need to have this somehow handled if
  1318. USE_SAPI is defined, because cgi will also be
  1319. interfaced in USE_SAPI */
  1320. pstat = sapi_get_stat();
  1321. if (!pstat) {
  1322. return "";
  1323. } else {
  1324. #ifdef PHP_WIN32
  1325. char *name = php_win32_get_username();
  1326. int len;
  1327. if (!name) {
  1328. return "";
  1329. }
  1330. len = (int)strlen(name);
  1331. name[len] = '\0';
  1332. SG(request_info).current_user_length = len;
  1333. SG(request_info).current_user = estrndup(name, len);
  1334. free(name);
  1335. return SG(request_info).current_user;
  1336. #else
  1337. struct passwd *pwd;
  1338. #if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
  1339. struct passwd _pw;
  1340. struct passwd *retpwptr = NULL;
  1341. int pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
  1342. char *pwbuf;
  1343. if (pwbuflen < 1) {
  1344. return "";
  1345. }
  1346. pwbuf = emalloc(pwbuflen);
  1347. if (getpwuid_r(pstat->st_uid, &_pw, pwbuf, pwbuflen, &retpwptr) != 0) {
  1348. efree(pwbuf);
  1349. return "";
  1350. }
  1351. if (retpwptr == NULL) {
  1352. efree(pwbuf);
  1353. return "";
  1354. }
  1355. pwd = &_pw;
  1356. #else
  1357. if ((pwd=getpwuid(pstat->st_uid))==NULL) {
  1358. return "";
  1359. }
  1360. #endif
  1361. SG(request_info).current_user_length = strlen(pwd->pw_name);
  1362. SG(request_info).current_user = estrndup(pwd->pw_name, SG(request_info).current_user_length);
  1363. #if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
  1364. efree(pwbuf);
  1365. #endif
  1366. return SG(request_info).current_user;
  1367. #endif
  1368. }
  1369. }
  1370. /* }}} */
  1371. /* {{{ proto bool set_time_limit(int seconds)
  1372. Sets the maximum time a script can run */
  1373. PHP_FUNCTION(set_time_limit)
  1374. {
  1375. zend_long new_timeout;
  1376. char *new_timeout_str;
  1377. int new_timeout_strlen;
  1378. zend_string *key;
  1379. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &new_timeout) == FAILURE) {
  1380. return;
  1381. }
  1382. new_timeout_strlen = (int)zend_spprintf(&new_timeout_str, 0, ZEND_LONG_FMT, new_timeout);
  1383. key = zend_string_init("max_execution_time", sizeof("max_execution_time")-1, 0);
  1384. if (zend_alter_ini_entry_chars_ex(key, new_timeout_str, new_timeout_strlen, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0) == SUCCESS) {
  1385. RETVAL_TRUE;
  1386. } else {
  1387. RETVAL_FALSE;
  1388. }
  1389. zend_string_release_ex(key, 0);
  1390. efree(new_timeout_str);
  1391. }
  1392. /* }}} */
  1393. /* {{{ php_fopen_wrapper_for_zend
  1394. */
  1395. static FILE *php_fopen_wrapper_for_zend(const char *filename, zend_string **opened_path)
  1396. {
  1397. return php_stream_open_wrapper_as_file((char *)filename, "rb", USE_PATH|IGNORE_URL_WIN|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE, opened_path);
  1398. }
  1399. /* }}} */
  1400. static void php_zend_stream_closer(void *handle) /* {{{ */
  1401. {
  1402. php_stream_close((php_stream*)handle);
  1403. }
  1404. /* }}} */
  1405. static void php_zend_stream_mmap_closer(void *handle) /* {{{ */
  1406. {
  1407. php_stream_mmap_unmap((php_stream*)handle);
  1408. php_zend_stream_closer(handle);
  1409. }
  1410. /* }}} */
  1411. static size_t php_zend_stream_fsizer(void *handle) /* {{{ */
  1412. {
  1413. php_stream_statbuf ssb;
  1414. if (php_stream_stat((php_stream*)handle, &ssb) == 0) {
  1415. return ssb.sb.st_size;
  1416. }
  1417. return 0;
  1418. }
  1419. /* }}} */
  1420. static int php_stream_open_for_zend(const char *filename, zend_file_handle *handle) /* {{{ */
  1421. {
  1422. return php_stream_open_for_zend_ex(filename, handle, USE_PATH|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE);
  1423. }
  1424. /* }}} */
  1425. PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *handle, int mode) /* {{{ */
  1426. {
  1427. char *p;
  1428. size_t len, mapped_len;
  1429. php_stream *stream = php_stream_open_wrapper((char *)filename, "rb", mode, &handle->opened_path);
  1430. if (stream) {
  1431. #if HAVE_MMAP || defined(PHP_WIN32)
  1432. size_t page_size = REAL_PAGE_SIZE;
  1433. #endif
  1434. handle->filename = (char*)filename;
  1435. handle->free_filename = 0;
  1436. handle->handle.stream.handle = stream;
  1437. handle->handle.stream.reader = (zend_stream_reader_t)_php_stream_read;
  1438. handle->handle.stream.fsizer = php_zend_stream_fsizer;
  1439. handle->handle.stream.isatty = 0;
  1440. /* can we mmap immediately? */
  1441. memset(&handle->handle.stream.mmap, 0, sizeof(handle->handle.stream.mmap));
  1442. len = php_zend_stream_fsizer(stream);
  1443. if (len != 0
  1444. #if HAVE_MMAP || defined(PHP_WIN32)
  1445. && ((len - 1) % page_size) <= page_size - ZEND_MMAP_AHEAD
  1446. #endif
  1447. && php_stream_mmap_possible(stream)
  1448. && (p = php_stream_mmap_range(stream, 0, len, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped_len)) != NULL) {
  1449. handle->handle.stream.closer = php_zend_stream_mmap_closer;
  1450. handle->handle.stream.mmap.buf = p;
  1451. handle->handle.stream.mmap.len = mapped_len;
  1452. handle->type = ZEND_HANDLE_MAPPED;
  1453. } else {
  1454. handle->handle.stream.closer = php_zend_stream_closer;
  1455. handle->type = ZEND_HANDLE_STREAM;
  1456. }
  1457. /* suppress warning if this stream is not explicitly closed */
  1458. php_stream_auto_cleanup(stream);
  1459. return SUCCESS;
  1460. }
  1461. return FAILURE;
  1462. }
  1463. /* }}} */
  1464. static zend_string *php_resolve_path_for_zend(const char *filename, size_t filename_len) /* {{{ */
  1465. {
  1466. return php_resolve_path(filename, filename_len, PG(include_path));
  1467. }
  1468. /* }}} */
  1469. /* {{{ php_get_configuration_directive_for_zend
  1470. */
  1471. static zval *php_get_configuration_directive_for_zend(zend_string *name)
  1472. {
  1473. return cfg_get_entry_ex(name);
  1474. }
  1475. /* }}} */
  1476. /* {{{ php_free_request_globals
  1477. */
  1478. static void php_free_request_globals(void)
  1479. {
  1480. if (PG(last_error_message)) {
  1481. free(PG(last_error_message));
  1482. PG(last_error_message) = NULL;
  1483. }
  1484. if (PG(last_error_file)) {
  1485. free(PG(last_error_file));
  1486. PG(last_error_file) = NULL;
  1487. }
  1488. if (PG(php_sys_temp_dir)) {
  1489. efree(PG(php_sys_temp_dir));
  1490. PG(php_sys_temp_dir) = NULL;
  1491. }
  1492. }
  1493. /* }}} */
  1494. /* {{{ php_message_handler_for_zend
  1495. */
  1496. static ZEND_COLD void php_message_handler_for_zend(zend_long message, const void *data)
  1497. {
  1498. switch (message) {
  1499. case ZMSG_FAILED_INCLUDE_FOPEN:
  1500. php_error_docref("function.include", E_WARNING, "Failed opening '%s' for inclusion (include_path='%s')", php_strip_url_passwd((char *) data), STR_PRINT(PG(include_path)));
  1501. break;
  1502. case ZMSG_FAILED_REQUIRE_FOPEN:
  1503. php_error_docref("function.require", E_COMPILE_ERROR, "Failed opening required '%s' (include_path='%s')", php_strip_url_passwd((char *) data), STR_PRINT(PG(include_path)));
  1504. break;
  1505. case ZMSG_FAILED_HIGHLIGHT_FOPEN:
  1506. php_error_docref(NULL, E_WARNING, "Failed opening '%s' for highlighting", php_strip_url_passwd((char *) data));
  1507. break;
  1508. case ZMSG_MEMORY_LEAK_DETECTED:
  1509. case ZMSG_MEMORY_LEAK_REPEATED:
  1510. #if ZEND_DEBUG
  1511. if (EG(error_reporting) & E_WARNING) {
  1512. char memory_leak_buf[1024];
  1513. if (message==ZMSG_MEMORY_LEAK_DETECTED) {
  1514. zend_leak_info *t = (zend_leak_info *) data;
  1515. snprintf(memory_leak_buf, 512, "%s(%" PRIu32 ") : Freeing " ZEND_ADDR_FMT " (%zu bytes), script=%s\n", t->filename, t->lineno, (size_t)t->addr, t->size, SAFE_FILENAME(SG(request_info).path_translated));
  1516. if (t->orig_filename) {
  1517. char relay_buf[512];
  1518. snprintf(relay_buf, 512, "%s(%" PRIu32 ") : Actual location (location was relayed)\n", t->orig_filename, t->orig_lineno);
  1519. strlcat(memory_leak_buf, relay_buf, sizeof(memory_leak_buf));
  1520. }
  1521. } else {
  1522. unsigned long leak_count = (zend_uintptr_t) data;
  1523. snprintf(memory_leak_buf, 512, "Last leak repeated %lu time%s\n", leak_count, (leak_count>1?"s":""));
  1524. }
  1525. # if defined(PHP_WIN32)
  1526. OutputDebugString(memory_leak_buf);
  1527. # else
  1528. fprintf(stderr, "%s", memory_leak_buf);
  1529. # endif
  1530. }
  1531. #endif
  1532. break;
  1533. case ZMSG_MEMORY_LEAKS_GRAND_TOTAL:
  1534. #if ZEND_DEBUG
  1535. if (EG(error_reporting) & E_WARNING) {
  1536. char memory_leak_buf[512];
  1537. snprintf(memory_leak_buf, 512, "=== Total %d memory leaks detected ===\n", *((uint32_t *) data));
  1538. # if defined(PHP_WIN32)
  1539. OutputDebugString(memory_leak_buf);
  1540. # else
  1541. fprintf(stderr, "%s", memory_leak_buf);
  1542. # endif
  1543. }
  1544. #endif
  1545. break;
  1546. case ZMSG_LOG_SCRIPT_NAME: {
  1547. struct tm *ta, tmbuf;
  1548. time_t curtime;
  1549. char *datetime_str, asctimebuf[52];
  1550. char memory_leak_buf[4096];
  1551. time(&curtime);
  1552. ta = php_localtime_r(&curtime, &tmbuf);
  1553. datetime_str = php_asctime_r(ta, asctimebuf);
  1554. if (datetime_str) {
  1555. datetime_str[strlen(datetime_str)-1]=0; /* get rid of the trailing newline */
  1556. snprintf(memory_leak_buf, sizeof(memory_leak_buf), "[%s] Script: '%s'\n", datetime_str, SAFE_FILENAME(SG(request_info).path_translated));
  1557. } else {
  1558. snprintf(memory_leak_buf, sizeof(memory_leak_buf), "[null] Script: '%s'\n", SAFE_FILENAME(SG(request_info).path_translated));
  1559. }
  1560. # if defined(PHP_WIN32)
  1561. OutputDebugString(memory_leak_buf);
  1562. # else
  1563. fprintf(stderr, "%s", memory_leak_buf);
  1564. # endif
  1565. }
  1566. break;
  1567. }
  1568. }
  1569. /* }}} */
  1570. void php_on_timeout(int seconds)
  1571. {
  1572. PG(connection_status) |= PHP_CONNECTION_TIMEOUT;
  1573. }
  1574. #if PHP_SIGCHILD
  1575. /* {{{ sigchld_handler
  1576. */
  1577. static void sigchld_handler(int apar)
  1578. {
  1579. int errno_save = errno;
  1580. while (waitpid(-1, NULL, WNOHANG) > 0);
  1581. signal(SIGCHLD, sigchld_handler);
  1582. errno = errno_save;
  1583. }
  1584. /* }}} */
  1585. #endif
  1586. /* {{{ php_request_startup
  1587. */
  1588. int php_request_startup(void)
  1589. {
  1590. int retval = SUCCESS;
  1591. zend_interned_strings_activate();
  1592. #ifdef HAVE_DTRACE
  1593. DTRACE_REQUEST_STARTUP(SAFE_FILENAME(SG(request_info).path_translated), SAFE_FILENAME(SG(request_info).request_uri), (char *)SAFE_FILENAME(SG(request_info).request_method));
  1594. #endif /* HAVE_DTRACE */
  1595. #ifdef PHP_WIN32
  1596. # if defined(ZTS)
  1597. _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
  1598. # endif
  1599. PG(com_initialized) = 0;
  1600. #endif
  1601. #if PHP_SIGCHILD
  1602. signal(SIGCHLD, sigchld_handler);
  1603. #endif
  1604. zend_try {
  1605. PG(in_error_log) = 0;
  1606. PG(during_request_startup) = 1;
  1607. php_output_activate();
  1608. /* initialize global variables */
  1609. PG(modules_activated) = 0;
  1610. PG(header_is_being_sent) = 0;
  1611. PG(connection_status) = PHP_CONNECTION_NORMAL;
  1612. PG(in_user_include) = 0;
  1613. zend_activate();
  1614. sapi_activate();
  1615. #ifdef ZEND_SIGNALS
  1616. zend_signal_activate();
  1617. #endif
  1618. if (PG(max_input_time) == -1) {
  1619. zend_set_timeout(EG(timeout_seconds), 1);
  1620. } else {
  1621. zend_set_timeout(PG(max_input_time), 1);
  1622. }
  1623. /* Disable realpath cache if an open_basedir is set */
  1624. if (PG(open_basedir) && *PG(open_basedir)) {
  1625. CWDG(realpath_cache_size_limit) = 0;
  1626. }
  1627. if (PG(expose_php)) {
  1628. sapi_add_header(SAPI_PHP_VERSION_HEADER, sizeof(SAPI_PHP_VERSION_HEADER)-1, 1);
  1629. }
  1630. if (PG(output_handler) && PG(output_handler)[0]) {
  1631. zval oh;
  1632. ZVAL_STRING(&oh, PG(output_handler));
  1633. php_output_start_user(&oh, 0, PHP_OUTPUT_HANDLER_STDFLAGS);
  1634. zval_ptr_dtor(&oh);
  1635. } else if (PG(output_buffering)) {
  1636. php_output_start_user(NULL, PG(output_buffering) > 1 ? PG(output_buffering) : 0, PHP_OUTPUT_HANDLER_STDFLAGS);
  1637. } else if (PG(implicit_flush)) {
  1638. php_output_set_implicit_flush(1);
  1639. }
  1640. /* We turn this off in php_execute_script() */
  1641. /* PG(during_request_startup) = 0; */
  1642. php_hash_environment();
  1643. zend_activate_modules();
  1644. PG(modules_activated)=1;
  1645. } zend_catch {
  1646. retval = FAILURE;
  1647. } zend_end_try();
  1648. SG(sapi_started) = 1;
  1649. return retval;
  1650. }
  1651. /* }}} */
  1652. /* {{{ php_request_shutdown_for_exec
  1653. */
  1654. void php_request_shutdown_for_exec(void *dummy)
  1655. {
  1656. /* used to close fd's in the 3..255 range here, but it's problematic
  1657. */
  1658. zend_interned_strings_deactivate();
  1659. shutdown_memory_manager(1, 1);
  1660. }
  1661. /* }}} */
  1662. /* {{{ php_request_shutdown
  1663. */
  1664. void php_request_shutdown(void *dummy)
  1665. {
  1666. zend_bool report_memleaks;
  1667. EG(flags) |= EG_FLAGS_IN_SHUTDOWN;
  1668. report_memleaks = PG(report_memleaks);
  1669. /* EG(current_execute_data) points into nirvana and therefore cannot be safely accessed
  1670. * inside zend_executor callback functions.
  1671. */
  1672. EG(current_execute_data) = NULL;
  1673. php_deactivate_ticks();
  1674. /* 1. Call all possible shutdown functions registered with register_shutdown_function() */
  1675. if (PG(modules_activated)) zend_try {
  1676. php_call_shutdown_functions();
  1677. } zend_end_try();
  1678. /* 2. Call all possible __destruct() functions */
  1679. zend_try {
  1680. zend_call_destructors();
  1681. } zend_end_try();
  1682. /* 3. Flush all output buffers */
  1683. zend_try {
  1684. zend_bool send_buffer = SG(request_info).headers_only ? 0 : 1;
  1685. if (CG(unclean_shutdown) && PG(last_error_type) == E_ERROR &&
  1686. (size_t)PG(memory_limit) < zend_memory_usage(1)
  1687. ) {
  1688. send_buffer = 0;
  1689. }
  1690. if (!send_buffer) {
  1691. php_output_discard_all();
  1692. } else {
  1693. php_output_end_all();
  1694. }
  1695. } zend_end_try();
  1696. /* 4. Reset max_execution_time (no longer executing php code after response sent) */
  1697. zend_try {
  1698. zend_unset_timeout();
  1699. } zend_end_try();
  1700. /* 5. Call all extensions RSHUTDOWN functions */
  1701. if (PG(modules_activated)) {
  1702. zend_deactivate_modules();
  1703. }
  1704. /* 6. Shutdown output layer (send the set HTTP headers, cleanup output handlers, etc.) */
  1705. zend_try {
  1706. php_output_deactivate();
  1707. } zend_end_try();
  1708. /* 7. Free shutdown functions */
  1709. if (PG(modules_activated)) {
  1710. php_free_shutdown_functions();
  1711. }
  1712. /* 8. Destroy super-globals */
  1713. zend_try {
  1714. int i;
  1715. for (i=0; i<NUM_TRACK_VARS; i++) {
  1716. zval_ptr_dtor(&PG(http_globals)[i]);
  1717. }
  1718. } zend_end_try();
  1719. /* 9. free request-bound globals */
  1720. php_free_request_globals();
  1721. /* 10. Shutdown scanner/executor/compiler and restore ini entries */
  1722. zend_deactivate();
  1723. /* 11. Call all extensions post-RSHUTDOWN functions */
  1724. zend_try {
  1725. zend_post_deactivate_modules();
  1726. } zend_end_try();
  1727. /* 12. SAPI related shutdown (free stuff) */
  1728. zend_try {
  1729. sapi_deactivate();
  1730. } zend_end_try();
  1731. /* 13. free virtual CWD memory */
  1732. virtual_cwd_deactivate();
  1733. /* 14. Destroy stream hashes */
  1734. zend_try {
  1735. php_shutdown_stream_hashes();
  1736. } zend_end_try();
  1737. /* 15. Free Willy (here be crashes) */
  1738. zend_interned_strings_deactivate();
  1739. zend_try {
  1740. shutdown_memory_manager(CG(unclean_shutdown) || !report_memleaks, 0);
  1741. } zend_end_try();
  1742. /* 16. Reset max_execution_time */
  1743. zend_try {
  1744. zend_unset_timeout();
  1745. } zend_end_try();
  1746. /* 17. Deactivate Zend signals */
  1747. #ifdef ZEND_SIGNALS
  1748. zend_signal_deactivate();
  1749. #endif
  1750. #ifdef PHP_WIN32
  1751. if (PG(com_initialized)) {
  1752. CoUninitialize();
  1753. PG(com_initialized) = 0;
  1754. }
  1755. #endif
  1756. #ifdef HAVE_DTRACE
  1757. DTRACE_REQUEST_SHUTDOWN(SAFE_FILENAME(SG(request_info).path_translated), SAFE_FILENAME(SG(request_info).request_uri), (char *)SAFE_FILENAME(SG(request_info).request_method));
  1758. #endif /* HAVE_DTRACE */
  1759. }
  1760. /* }}} */
  1761. /* {{{ php_com_initialize
  1762. */
  1763. PHPAPI void php_com_initialize(void)
  1764. {
  1765. #ifdef PHP_WIN32
  1766. if (!PG(com_initialized)) {
  1767. if (CoInitialize(NULL) == S_OK) {
  1768. PG(com_initialized) = 1;
  1769. }
  1770. }
  1771. #endif
  1772. }
  1773. /* }}} */
  1774. /* {{{ php_output_wrapper
  1775. */
  1776. static size_t php_output_wrapper(const char *str, size_t str_length)
  1777. {
  1778. return php_output_write(str, str_length);
  1779. }
  1780. /* }}} */
  1781. #ifdef ZTS
  1782. /* {{{ core_globals_ctor
  1783. */
  1784. static void core_globals_ctor(php_core_globals *core_globals)
  1785. {
  1786. memset(core_globals, 0, sizeof(*core_globals));
  1787. php_startup_ticks();
  1788. }
  1789. /* }}} */
  1790. #endif
  1791. /* {{{ core_globals_dtor
  1792. */
  1793. static void core_globals_dtor(php_core_globals *core_globals)
  1794. {
  1795. if (core_globals->last_error_message) {
  1796. free(core_globals->last_error_message);
  1797. }
  1798. if (core_globals->last_error_file) {
  1799. free(core_globals->last_error_file);
  1800. }
  1801. if (core_globals->disable_functions) {
  1802. free(core_globals->disable_functions);
  1803. }
  1804. if (core_globals->disable_classes) {
  1805. free(core_globals->disable_classes);
  1806. }
  1807. if (core_globals->php_binary) {
  1808. free(core_globals->php_binary);
  1809. }
  1810. php_shutdown_ticks();
  1811. }
  1812. /* }}} */
  1813. PHP_MINFO_FUNCTION(php_core) { /* {{{ */
  1814. php_info_print_table_start();
  1815. php_info_print_table_row(2, "PHP Version", PHP_VERSION);
  1816. php_info_print_table_end();
  1817. DISPLAY_INI_ENTRIES();
  1818. }
  1819. /* }}} */
  1820. /* {{{ php_register_extensions
  1821. */
  1822. int php_register_extensions(zend_module_entry * const * ptr, int count)
  1823. {
  1824. zend_module_entry * const * end = ptr + count;
  1825. while (ptr < end) {
  1826. if (*ptr) {
  1827. if (zend_register_internal_module(*ptr)==NULL) {
  1828. return FAILURE;
  1829. }
  1830. }
  1831. ptr++;
  1832. }
  1833. return SUCCESS;
  1834. }
  1835. /* A very long time ago php_module_startup() was refactored in a way
  1836. * which broke calling it with more than one additional module.
  1837. * This alternative to php_register_extensions() works around that
  1838. * by walking the shallower structure.
  1839. *
  1840. * See algo: https://bugs.php.net/bug.php?id=63159
  1841. */
  1842. static int php_register_extensions_bc(zend_module_entry *ptr, int count)
  1843. {
  1844. while (count--) {
  1845. if (zend_register_internal_module(ptr++) == NULL) {
  1846. return FAILURE;
  1847. }
  1848. }
  1849. return SUCCESS;
  1850. }
  1851. /* }}} */
  1852. #ifdef PHP_WIN32
  1853. static _invalid_parameter_handler old_invalid_parameter_handler;
  1854. void dummy_invalid_parameter_handler(
  1855. const wchar_t *expression,
  1856. const wchar_t *function,
  1857. const wchar_t *file,
  1858. unsigned int line,
  1859. uintptr_t pEwserved)
  1860. {
  1861. static int called = 0;
  1862. char buf[1024];
  1863. int len;
  1864. if (!called) {
  1865. if(PG(windows_show_crt_warning)) {
  1866. called = 1;
  1867. if (function) {
  1868. if (file) {
  1869. len = _snprintf(buf, sizeof(buf)-1, "Invalid parameter detected in CRT function '%ws' (%ws:%u)", function, file, line);
  1870. } else {
  1871. len = _snprintf(buf, sizeof(buf)-1, "Invalid parameter detected in CRT function '%ws'", function);
  1872. }
  1873. } else {
  1874. len = _snprintf(buf, sizeof(buf)-1, "Invalid CRT parameter detected (function not known)");
  1875. }
  1876. zend_error(E_WARNING, "%s", buf);
  1877. called = 0;
  1878. }
  1879. }
  1880. }
  1881. #endif
  1882. /* {{{ php_module_startup
  1883. */
  1884. int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_modules, uint32_t num_additional_modules)
  1885. {
  1886. zend_utility_functions zuf;
  1887. zend_utility_values zuv;
  1888. int retval = SUCCESS, module_number=0; /* for REGISTER_INI_ENTRIES() */
  1889. char *php_os;
  1890. zend_module_entry *module;
  1891. #ifdef PHP_WIN32
  1892. WORD wVersionRequested = MAKEWORD(2, 0);
  1893. WSADATA wsaData;
  1894. php_os = "WINNT";
  1895. old_invalid_parameter_handler =
  1896. _set_invalid_parameter_handler(dummy_invalid_parameter_handler);
  1897. if (old_invalid_parameter_handler != NULL) {
  1898. _set_invalid_parameter_handler(old_invalid_parameter_handler);
  1899. }
  1900. /* Disable the message box for assertions.*/
  1901. _CrtSetReportMode(_CRT_ASSERT, 0);
  1902. #else
  1903. php_os = PHP_OS;
  1904. #endif
  1905. #ifdef ZTS
  1906. (void)ts_resource(0);
  1907. #endif
  1908. #ifdef PHP_WIN32
  1909. if (!php_win32_init_random_bytes()) {
  1910. fprintf(stderr, "\ncrypt algorithm provider initialization failed\n");
  1911. return FAILURE;
  1912. }
  1913. #endif
  1914. module_shutdown = 0;
  1915. module_startup = 1;
  1916. sapi_initialize_empty_request();
  1917. sapi_activate();
  1918. if (module_initialized) {
  1919. return SUCCESS;
  1920. }
  1921. sapi_module = *sf;
  1922. php_output_startup();
  1923. #ifdef ZTS
  1924. ts_allocate_id(&core_globals_id, sizeof(php_core_globals), (ts_allocate_ctor) core_globals_ctor, (ts_allocate_dtor) core_globals_dtor);
  1925. #ifdef PHP_WIN32
  1926. ts_allocate_id(&php_win32_core_globals_id, sizeof(php_win32_core_globals), (ts_allocate_ctor) php_win32_core_globals_ctor, (ts_allocate_dtor) php_win32_core_globals_dtor);
  1927. #endif
  1928. #else
  1929. memset(&core_globals, 0, sizeof(core_globals));
  1930. php_startup_ticks();
  1931. #endif
  1932. gc_globals_ctor();
  1933. zuf.error_function = php_error_cb;
  1934. zuf.printf_function = php_printf;
  1935. zuf.write_function = php_output_wrapper;
  1936. zuf.fopen_function = php_fopen_wrapper_for_zend;
  1937. zuf.message_handler = php_message_handler_for_zend;
  1938. zuf.get_configuration_directive = php_get_configuration_directive_for_zend;
  1939. zuf.ticks_function = php_run_ticks;
  1940. zuf.on_timeout = php_on_timeout;
  1941. zuf.stream_open_function = php_stream_open_for_zend;
  1942. zuf.printf_to_smart_string_function = php_printf_to_smart_string;
  1943. zuf.printf_to_smart_str_function = php_printf_to_smart_str;
  1944. zuf.getenv_function = sapi_getenv;
  1945. zuf.resolve_path_function = php_resolve_path_for_zend;
  1946. zend_startup(&zuf, NULL);
  1947. #if HAVE_SETLOCALE
  1948. setlocale(LC_CTYPE, "");
  1949. zend_update_current_locale();
  1950. #endif
  1951. #if HAVE_TZSET
  1952. tzset();
  1953. #endif
  1954. #ifdef PHP_WIN32
  1955. /* start up winsock services */
  1956. if (WSAStartup(wVersionRequested, &wsaData) != 0) {
  1957. php_printf("\nwinsock.dll unusable. %d\n", WSAGetLastError());
  1958. return FAILURE;
  1959. }
  1960. #endif
  1961. le_index_ptr = zend_register_list_destructors_ex(NULL, NULL, "index pointer", 0);
  1962. /* Register constants */
  1963. REGISTER_MAIN_STRINGL_CONSTANT("PHP_VERSION", PHP_VERSION, sizeof(PHP_VERSION)-1, CONST_PERSISTENT | CONST_CS);
  1964. REGISTER_MAIN_LONG_CONSTANT("PHP_MAJOR_VERSION", PHP_MAJOR_VERSION, CONST_PERSISTENT | CONST_CS);
  1965. REGISTER_MAIN_LONG_CONSTANT("PHP_MINOR_VERSION", PHP_MINOR_VERSION, CONST_PERSISTENT | CONST_CS);
  1966. REGISTER_MAIN_LONG_CONSTANT("PHP_RELEASE_VERSION", PHP_RELEASE_VERSION, CONST_PERSISTENT | CONST_CS);
  1967. REGISTER_MAIN_STRINGL_CONSTANT("PHP_EXTRA_VERSION", PHP_EXTRA_VERSION, sizeof(PHP_EXTRA_VERSION) - 1, CONST_PERSISTENT | CONST_CS);
  1968. REGISTER_MAIN_LONG_CONSTANT("PHP_VERSION_ID", PHP_VERSION_ID, CONST_PERSISTENT | CONST_CS);
  1969. #ifdef ZTS
  1970. REGISTER_MAIN_LONG_CONSTANT("PHP_ZTS", 1, CONST_PERSISTENT | CONST_CS);
  1971. #else
  1972. REGISTER_MAIN_LONG_CONSTANT("PHP_ZTS", 0, CONST_PERSISTENT | CONST_CS);
  1973. #endif
  1974. REGISTER_MAIN_LONG_CONSTANT("PHP_DEBUG", PHP_DEBUG, CONST_PERSISTENT | CONST_CS);
  1975. REGISTER_MAIN_STRINGL_CONSTANT("PHP_OS", php_os, strlen(php_os), CONST_PERSISTENT | CONST_CS);
  1976. REGISTER_MAIN_STRINGL_CONSTANT("PHP_OS_FAMILY", PHP_OS_FAMILY, sizeof(PHP_OS_FAMILY)-1, CONST_PERSISTENT | CONST_CS);
  1977. REGISTER_MAIN_STRINGL_CONSTANT("PHP_SAPI", sapi_module.name, strlen(sapi_module.name), CONST_PERSISTENT | CONST_CS | CONST_NO_FILE_CACHE);
  1978. REGISTER_MAIN_STRINGL_CONSTANT("DEFAULT_INCLUDE_PATH", PHP_INCLUDE_PATH, sizeof(PHP_INCLUDE_PATH)-1, CONST_PERSISTENT | CONST_CS);
  1979. REGISTER_MAIN_STRINGL_CONSTANT("PEAR_INSTALL_DIR", PEAR_INSTALLDIR, sizeof(PEAR_INSTALLDIR)-1, CONST_PERSISTENT | CONST_CS);
  1980. REGISTER_MAIN_STRINGL_CONSTANT("PEAR_EXTENSION_DIR", PHP_EXTENSION_DIR, sizeof(PHP_EXTENSION_DIR)-1, CONST_PERSISTENT | CONST_CS);
  1981. REGISTER_MAIN_STRINGL_CONSTANT("PHP_EXTENSION_DIR", PHP_EXTENSION_DIR, sizeof(PHP_EXTENSION_DIR)-1, CONST_PERSISTENT | CONST_CS);
  1982. REGISTER_MAIN_STRINGL_CONSTANT("PHP_PREFIX", PHP_PREFIX, sizeof(PHP_PREFIX)-1, CONST_PERSISTENT | CONST_CS);
  1983. REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINDIR", PHP_BINDIR, sizeof(PHP_BINDIR)-1, CONST_PERSISTENT | CONST_CS);
  1984. #ifndef PHP_WIN32
  1985. REGISTER_MAIN_STRINGL_CONSTANT("PHP_MANDIR", PHP_MANDIR, sizeof(PHP_MANDIR)-1, CONST_PERSISTENT | CONST_CS);
  1986. #endif
  1987. REGISTER_MAIN_STRINGL_CONSTANT("PHP_LIBDIR", PHP_LIBDIR, sizeof(PHP_LIBDIR)-1, CONST_PERSISTENT | CONST_CS);
  1988. REGISTER_MAIN_STRINGL_CONSTANT("PHP_DATADIR", PHP_DATADIR, sizeof(PHP_DATADIR)-1, CONST_PERSISTENT | CONST_CS);
  1989. REGISTER_MAIN_STRINGL_CONSTANT("PHP_SYSCONFDIR", PHP_SYSCONFDIR, sizeof(PHP_SYSCONFDIR)-1, CONST_PERSISTENT | CONST_CS);
  1990. REGISTER_MAIN_STRINGL_CONSTANT("PHP_LOCALSTATEDIR", PHP_LOCALSTATEDIR, sizeof(PHP_LOCALSTATEDIR)-1, CONST_PERSISTENT | CONST_CS);
  1991. REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_PATH", PHP_CONFIG_FILE_PATH, strlen(PHP_CONFIG_FILE_PATH), CONST_PERSISTENT | CONST_CS);
  1992. REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_SCAN_DIR", PHP_CONFIG_FILE_SCAN_DIR, sizeof(PHP_CONFIG_FILE_SCAN_DIR)-1, CONST_PERSISTENT | CONST_CS);
  1993. REGISTER_MAIN_STRINGL_CONSTANT("PHP_SHLIB_SUFFIX", PHP_SHLIB_SUFFIX, sizeof(PHP_SHLIB_SUFFIX)-1, CONST_PERSISTENT | CONST_CS);
  1994. REGISTER_MAIN_STRINGL_CONSTANT("PHP_EOL", PHP_EOL, sizeof(PHP_EOL)-1, CONST_PERSISTENT | CONST_CS);
  1995. REGISTER_MAIN_LONG_CONSTANT("PHP_MAXPATHLEN", MAXPATHLEN, CONST_PERSISTENT | CONST_CS);
  1996. REGISTER_MAIN_LONG_CONSTANT("PHP_INT_MAX", ZEND_LONG_MAX, CONST_PERSISTENT | CONST_CS);
  1997. REGISTER_MAIN_LONG_CONSTANT("PHP_INT_MIN", ZEND_LONG_MIN, CONST_PERSISTENT | CONST_CS);
  1998. REGISTER_MAIN_LONG_CONSTANT("PHP_INT_SIZE", SIZEOF_ZEND_LONG, CONST_PERSISTENT | CONST_CS);
  1999. REGISTER_MAIN_LONG_CONSTANT("PHP_FD_SETSIZE", FD_SETSIZE, CONST_PERSISTENT | CONST_CS);
  2000. REGISTER_MAIN_LONG_CONSTANT("PHP_FLOAT_DIG", DBL_DIG, CONST_PERSISTENT | CONST_CS);
  2001. REGISTER_MAIN_DOUBLE_CONSTANT("PHP_FLOAT_EPSILON", DBL_EPSILON, CONST_PERSISTENT | CONST_CS);
  2002. REGISTER_MAIN_DOUBLE_CONSTANT("PHP_FLOAT_MAX", DBL_MAX, CONST_PERSISTENT | CONST_CS);
  2003. REGISTER_MAIN_DOUBLE_CONSTANT("PHP_FLOAT_MIN", DBL_MIN, CONST_PERSISTENT | CONST_CS);
  2004. #ifdef PHP_WIN32
  2005. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_MAJOR", EG(windows_version_info).dwMajorVersion, CONST_PERSISTENT | CONST_CS);
  2006. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_MINOR", EG(windows_version_info).dwMinorVersion, CONST_PERSISTENT | CONST_CS);
  2007. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_BUILD", EG(windows_version_info).dwBuildNumber, CONST_PERSISTENT | CONST_CS);
  2008. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_PLATFORM", EG(windows_version_info).dwPlatformId, CONST_PERSISTENT | CONST_CS);
  2009. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_SP_MAJOR", EG(windows_version_info).wServicePackMajor, CONST_PERSISTENT | CONST_CS);
  2010. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_SP_MINOR", EG(windows_version_info).wServicePackMinor, CONST_PERSISTENT | CONST_CS);
  2011. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_SUITEMASK", EG(windows_version_info).wSuiteMask, CONST_PERSISTENT | CONST_CS);
  2012. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_PRODUCTTYPE", EG(windows_version_info).wProductType, CONST_PERSISTENT | CONST_CS);
  2013. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_DOMAIN_CONTROLLER", VER_NT_DOMAIN_CONTROLLER, CONST_PERSISTENT | CONST_CS);
  2014. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_SERVER", VER_NT_SERVER, CONST_PERSISTENT | CONST_CS);
  2015. REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_WORKSTATION", VER_NT_WORKSTATION, CONST_PERSISTENT | CONST_CS);
  2016. #endif
  2017. php_binary_init();
  2018. if (PG(php_binary)) {
  2019. REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", PG(php_binary), strlen(PG(php_binary)), CONST_PERSISTENT | CONST_CS | CONST_NO_FILE_CACHE);
  2020. } else {
  2021. REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", "", 0, CONST_PERSISTENT | CONST_CS | CONST_NO_FILE_CACHE);
  2022. }
  2023. php_output_register_constants();
  2024. php_rfc1867_register_constants();
  2025. /* this will read in php.ini, set up the configuration parameters,
  2026. load zend extensions and register php function extensions
  2027. to be loaded later */
  2028. if (php_init_config() == FAILURE) {
  2029. return FAILURE;
  2030. }
  2031. /* Register PHP core ini entries */
  2032. REGISTER_INI_ENTRIES();
  2033. /* Register Zend ini entries */
  2034. zend_register_standard_ini_entries();
  2035. #ifdef ZEND_WIN32
  2036. /* Until the current ini values was setup, the current cp is 65001.
  2037. If the actual ini vaues are different, some stuff needs to be updated.
  2038. It concerns at least main_cwd_state and there might be more. As we're
  2039. still in the startup phase, lets use the chance and reinit the relevant
  2040. item according to the current codepage. Still, if ini_set() is used
  2041. later on, a more intelligent way to update such stuff is needed.
  2042. Startup/shutdown routines could involve touching globals and thus
  2043. can't always be used on demand. */
  2044. if (!php_win32_cp_use_unicode()) {
  2045. virtual_cwd_main_cwd_init(1);
  2046. }
  2047. #endif
  2048. /* Disable realpath cache if an open_basedir is set */
  2049. if (PG(open_basedir) && *PG(open_basedir)) {
  2050. CWDG(realpath_cache_size_limit) = 0;
  2051. }
  2052. PG(have_called_openlog) = 0;
  2053. /* initialize stream wrappers registry
  2054. * (this uses configuration parameters from php.ini)
  2055. */
  2056. if (php_init_stream_wrappers(module_number) == FAILURE) {
  2057. php_printf("PHP: Unable to initialize stream url wrappers.\n");
  2058. return FAILURE;
  2059. }
  2060. zuv.html_errors = 1;
  2061. zuv.import_use_extension = ".php";
  2062. zuv.import_use_extension_length = (uint32_t)strlen(zuv.import_use_extension);
  2063. php_startup_auto_globals();
  2064. zend_set_utility_values(&zuv);
  2065. php_startup_sapi_content_types();
  2066. /* startup extensions statically compiled in */
  2067. if (php_register_internal_extensions_func() == FAILURE) {
  2068. php_printf("Unable to start builtin modules\n");
  2069. return FAILURE;
  2070. }
  2071. /* start additional PHP extensions */
  2072. php_register_extensions_bc(additional_modules, num_additional_modules);
  2073. /* load and startup extensions compiled as shared objects (aka DLLs)
  2074. as requested by php.ini entries
  2075. these are loaded after initialization of internal extensions
  2076. as extensions *might* rely on things from ext/standard
  2077. which is always an internal extension and to be initialized
  2078. ahead of all other internals
  2079. */
  2080. php_ini_register_extensions();
  2081. zend_startup_modules();
  2082. /* start Zend extensions */
  2083. zend_startup_extensions();
  2084. zend_collect_module_handlers();
  2085. /* register additional functions */
  2086. if (sapi_module.additional_functions) {
  2087. if ((module = zend_hash_str_find_ptr(&module_registry, "standard", sizeof("standard")-1)) != NULL) {
  2088. EG(current_module) = module;
  2089. zend_register_functions(NULL, sapi_module.additional_functions, NULL, MODULE_PERSISTENT);
  2090. EG(current_module) = NULL;
  2091. }
  2092. }
  2093. /* disable certain classes and functions as requested by php.ini */
  2094. php_disable_functions();
  2095. php_disable_classes();
  2096. /* make core report what it should */
  2097. if ((module = zend_hash_str_find_ptr(&module_registry, "core", sizeof("core")-1)) != NULL) {
  2098. module->version = PHP_VERSION;
  2099. module->info_func = PHP_MINFO(php_core);
  2100. }
  2101. if (zend_post_startup() != SUCCESS) {
  2102. return FAILURE;
  2103. }
  2104. module_initialized = 1;
  2105. /* Check for deprecated directives */
  2106. /* NOTE: If you add anything here, remember to add it to Makefile.global! */
  2107. {
  2108. struct {
  2109. const long error_level;
  2110. const char *phrase;
  2111. const char *directives[17]; /* Remember to change this if the number of directives change */
  2112. } directives[2] = {
  2113. {
  2114. E_DEPRECATED,
  2115. "Directive '%s' is deprecated",
  2116. {
  2117. "track_errors",
  2118. NULL
  2119. }
  2120. },
  2121. {
  2122. E_CORE_ERROR,
  2123. "Directive '%s' is no longer available in PHP",
  2124. {
  2125. "allow_call_time_pass_reference",
  2126. "asp_tags",
  2127. "define_syslog_variables",
  2128. "highlight.bg",
  2129. "magic_quotes_gpc",
  2130. "magic_quotes_runtime",
  2131. "magic_quotes_sybase",
  2132. "register_globals",
  2133. "register_long_arrays",
  2134. "safe_mode",
  2135. "safe_mode_gid",
  2136. "safe_mode_include_dir",
  2137. "safe_mode_exec_dir",
  2138. "safe_mode_allowed_env_vars",
  2139. "safe_mode_protected_env_vars",
  2140. "zend.ze1_compatibility_mode",
  2141. NULL
  2142. }
  2143. }
  2144. };
  2145. unsigned int i;
  2146. zend_try {
  2147. /* 2 = Count of deprecation structs */
  2148. for (i = 0; i < 2; i++) {
  2149. const char **p = directives[i].directives;
  2150. while(*p) {
  2151. zend_long value;
  2152. if (cfg_get_long((char*)*p, &value) == SUCCESS && value) {
  2153. zend_error(directives[i].error_level, directives[i].phrase, *p);
  2154. }
  2155. ++p;
  2156. }
  2157. }
  2158. } zend_catch {
  2159. retval = FAILURE;
  2160. } zend_end_try();
  2161. }
  2162. virtual_cwd_deactivate();
  2163. sapi_deactivate();
  2164. module_startup = 0;
  2165. shutdown_memory_manager(1, 0);
  2166. virtual_cwd_activate();
  2167. zend_interned_strings_switch_storage(1);
  2168. #if ZEND_RC_DEBUG
  2169. zend_rc_debug = 1;
  2170. #endif
  2171. /* we're done */
  2172. return retval;
  2173. }
  2174. /* }}} */
  2175. void php_module_shutdown_for_exec(void)
  2176. {
  2177. /* used to close fd's in the range 3.255 here, but it's problematic */
  2178. }
  2179. /* {{{ php_module_shutdown_wrapper
  2180. */
  2181. int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals)
  2182. {
  2183. php_module_shutdown();
  2184. return SUCCESS;
  2185. }
  2186. /* }}} */
  2187. /* {{{ php_module_shutdown
  2188. */
  2189. void php_module_shutdown(void)
  2190. {
  2191. int module_number=0; /* for UNREGISTER_INI_ENTRIES() */
  2192. module_shutdown = 1;
  2193. if (!module_initialized) {
  2194. return;
  2195. }
  2196. zend_interned_strings_switch_storage(0);
  2197. #ifdef ZTS
  2198. ts_free_worker_threads();
  2199. #endif
  2200. #if ZEND_RC_DEBUG
  2201. zend_rc_debug = 0;
  2202. #endif
  2203. #ifdef PHP_WIN32
  2204. (void)php_win32_shutdown_random_bytes();
  2205. #endif
  2206. sapi_flush();
  2207. zend_shutdown();
  2208. #ifdef PHP_WIN32
  2209. /*close winsock */
  2210. WSACleanup();
  2211. #endif
  2212. /* Destroys filter & transport registries too */
  2213. php_shutdown_stream_wrappers(module_number);
  2214. UNREGISTER_INI_ENTRIES();
  2215. /* close down the ini config */
  2216. php_shutdown_config();
  2217. #ifndef ZTS
  2218. zend_ini_shutdown();
  2219. shutdown_memory_manager(CG(unclean_shutdown), 1);
  2220. #else
  2221. zend_ini_global_shutdown();
  2222. #endif
  2223. php_output_shutdown();
  2224. #ifndef ZTS
  2225. zend_interned_strings_dtor();
  2226. #endif
  2227. module_initialized = 0;
  2228. #ifndef ZTS
  2229. core_globals_dtor(&core_globals);
  2230. gc_globals_dtor();
  2231. #else
  2232. ts_free_id(core_globals_id);
  2233. #endif
  2234. #ifdef PHP_WIN32
  2235. if (old_invalid_parameter_handler == NULL) {
  2236. _set_invalid_parameter_handler(old_invalid_parameter_handler);
  2237. }
  2238. #endif
  2239. }
  2240. /* }}} */
  2241. /* {{{ php_execute_script
  2242. */
  2243. PHPAPI int php_execute_script(zend_file_handle *primary_file)
  2244. {
  2245. zend_file_handle *prepend_file_p, *append_file_p;
  2246. zend_file_handle prepend_file = {{0}, NULL, NULL, 0, 0}, append_file = {{0}, NULL, NULL, 0, 0};
  2247. #if HAVE_BROKEN_GETCWD
  2248. volatile int old_cwd_fd = -1;
  2249. #else
  2250. char *old_cwd;
  2251. ALLOCA_FLAG(use_heap)
  2252. #endif
  2253. int retval = 0;
  2254. EG(exit_status) = 0;
  2255. #ifndef HAVE_BROKEN_GETCWD
  2256. # define OLD_CWD_SIZE 4096
  2257. old_cwd = do_alloca(OLD_CWD_SIZE, use_heap);
  2258. old_cwd[0] = '\0';
  2259. #endif
  2260. zend_try {
  2261. char realfile[MAXPATHLEN];
  2262. #ifdef PHP_WIN32
  2263. if(primary_file->filename) {
  2264. UpdateIniFromRegistry((char*)primary_file->filename);
  2265. }
  2266. #endif
  2267. PG(during_request_startup) = 0;
  2268. if (primary_file->filename && !(SG(options) & SAPI_OPTION_NO_CHDIR)) {
  2269. #if HAVE_BROKEN_GETCWD
  2270. /* this looks nasty to me */
  2271. old_cwd_fd = open(".", 0);
  2272. #else
  2273. php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
  2274. #endif
  2275. VCWD_CHDIR_FILE(primary_file->filename);
  2276. }
  2277. /* Only lookup the real file path and add it to the included_files list if already opened
  2278. * otherwise it will get opened and added to the included_files list in zend_execute_scripts
  2279. */
  2280. if (primary_file->filename &&
  2281. strcmp("Standard input code", primary_file->filename) &&
  2282. primary_file->opened_path == NULL &&
  2283. primary_file->type != ZEND_HANDLE_FILENAME
  2284. ) {
  2285. if (expand_filepath(primary_file->filename, realfile)) {
  2286. primary_file->opened_path = zend_string_init(realfile, strlen(realfile), 0);
  2287. zend_hash_add_empty_element(&EG(included_files), primary_file->opened_path);
  2288. }
  2289. }
  2290. if (PG(auto_prepend_file) && PG(auto_prepend_file)[0]) {
  2291. prepend_file.filename = PG(auto_prepend_file);
  2292. prepend_file.opened_path = NULL;
  2293. prepend_file.free_filename = 0;
  2294. prepend_file.type = ZEND_HANDLE_FILENAME;
  2295. prepend_file_p = &prepend_file;
  2296. } else {
  2297. prepend_file_p = NULL;
  2298. }
  2299. if (PG(auto_append_file) && PG(auto_append_file)[0]) {
  2300. append_file.filename = PG(auto_append_file);
  2301. append_file.opened_path = NULL;
  2302. append_file.free_filename = 0;
  2303. append_file.type = ZEND_HANDLE_FILENAME;
  2304. append_file_p = &append_file;
  2305. } else {
  2306. append_file_p = NULL;
  2307. }
  2308. if (PG(max_input_time) != -1) {
  2309. #ifdef PHP_WIN32
  2310. zend_unset_timeout();
  2311. #endif
  2312. zend_set_timeout(INI_INT("max_execution_time"), 0);
  2313. }
  2314. /*
  2315. If cli primary file has shabang line and there is a prepend file,
  2316. the `start_lineno` will be used by prepend file but not primary file,
  2317. save it and restore after prepend file been executed.
  2318. */
  2319. if (CG(start_lineno) && prepend_file_p) {
  2320. int orig_start_lineno = CG(start_lineno);
  2321. CG(start_lineno) = 0;
  2322. if (zend_execute_scripts(ZEND_REQUIRE, NULL, 1, prepend_file_p) == SUCCESS) {
  2323. CG(start_lineno) = orig_start_lineno;
  2324. retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 2, primary_file, append_file_p) == SUCCESS);
  2325. }
  2326. } else {
  2327. retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS);
  2328. }
  2329. } zend_end_try();
  2330. if (EG(exception)) {
  2331. zend_try {
  2332. zend_exception_error(EG(exception), E_ERROR);
  2333. } zend_end_try();
  2334. }
  2335. #if HAVE_BROKEN_GETCWD
  2336. if (old_cwd_fd != -1) {
  2337. fchdir(old_cwd_fd);
  2338. close(old_cwd_fd);
  2339. }
  2340. #else
  2341. if (old_cwd[0] != '\0') {
  2342. php_ignore_value(VCWD_CHDIR(old_cwd));
  2343. }
  2344. free_alloca(old_cwd, use_heap);
  2345. #endif
  2346. return retval;
  2347. }
  2348. /* }}} */
  2349. /* {{{ php_execute_simple_script
  2350. */
  2351. PHPAPI int php_execute_simple_script(zend_file_handle *primary_file, zval *ret)
  2352. {
  2353. char *old_cwd;
  2354. ALLOCA_FLAG(use_heap)
  2355. EG(exit_status) = 0;
  2356. #define OLD_CWD_SIZE 4096
  2357. old_cwd = do_alloca(OLD_CWD_SIZE, use_heap);
  2358. old_cwd[0] = '\0';
  2359. zend_try {
  2360. #ifdef PHP_WIN32
  2361. if(primary_file->filename) {
  2362. UpdateIniFromRegistry((char*)primary_file->filename);
  2363. }
  2364. #endif
  2365. PG(during_request_startup) = 0;
  2366. if (primary_file->filename && !(SG(options) & SAPI_OPTION_NO_CHDIR)) {
  2367. php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
  2368. VCWD_CHDIR_FILE(primary_file->filename);
  2369. }
  2370. zend_execute_scripts(ZEND_REQUIRE, ret, 1, primary_file);
  2371. } zend_end_try();
  2372. if (old_cwd[0] != '\0') {
  2373. php_ignore_value(VCWD_CHDIR(old_cwd));
  2374. }
  2375. free_alloca(old_cwd, use_heap);
  2376. return EG(exit_status);
  2377. }
  2378. /* }}} */
  2379. /* {{{ php_handle_aborted_connection
  2380. */
  2381. PHPAPI void php_handle_aborted_connection(void)
  2382. {
  2383. PG(connection_status) = PHP_CONNECTION_ABORTED;
  2384. php_output_set_status(PHP_OUTPUT_DISABLED);
  2385. if (!PG(ignore_user_abort)) {
  2386. zend_bailout();
  2387. }
  2388. }
  2389. /* }}} */
  2390. /* {{{ php_handle_auth_data
  2391. */
  2392. PHPAPI int php_handle_auth_data(const char *auth)
  2393. {
  2394. int ret = -1;
  2395. if (auth && auth[0] != '\0' && strncmp(auth, "Basic ", 6) == 0) {
  2396. char *pass;
  2397. zend_string *user;
  2398. user = php_base64_decode((const unsigned char*)auth + 6, strlen(auth) - 6);
  2399. if (user) {
  2400. pass = strchr(ZSTR_VAL(user), ':');
  2401. if (pass) {
  2402. *pass++ = '\0';
  2403. SG(request_info).auth_user = estrndup(ZSTR_VAL(user), ZSTR_LEN(user));
  2404. SG(request_info).auth_password = estrdup(pass);
  2405. ret = 0;
  2406. }
  2407. zend_string_free(user);
  2408. }
  2409. }
  2410. if (ret == -1) {
  2411. SG(request_info).auth_user = SG(request_info).auth_password = NULL;
  2412. } else {
  2413. SG(request_info).auth_digest = NULL;
  2414. }
  2415. if (ret == -1 && auth && auth[0] != '\0' && strncmp(auth, "Digest ", 7) == 0) {
  2416. SG(request_info).auth_digest = estrdup(auth + 7);
  2417. ret = 0;
  2418. }
  2419. if (ret == -1) {
  2420. SG(request_info).auth_digest = NULL;
  2421. }
  2422. return ret;
  2423. }
  2424. /* }}} */
  2425. /* {{{ php_lint_script
  2426. */
  2427. PHPAPI int php_lint_script(zend_file_handle *file)
  2428. {
  2429. zend_op_array *op_array;
  2430. int retval = FAILURE;
  2431. zend_try {
  2432. op_array = zend_compile_file(file, ZEND_INCLUDE);
  2433. zend_destroy_file_handle(file);
  2434. if (op_array) {
  2435. destroy_op_array(op_array);
  2436. efree(op_array);
  2437. retval = SUCCESS;
  2438. }
  2439. } zend_end_try();
  2440. if (EG(exception)) {
  2441. zend_exception_error(EG(exception), E_ERROR);
  2442. }
  2443. return retval;
  2444. }
  2445. /* }}} */
  2446. /*
  2447. * Local variables:
  2448. * tab-width: 4
  2449. * c-basic-offset: 4
  2450. * End:
  2451. * vim600: sw=4 ts=4 fdm=marker
  2452. * vim<600: sw=4 ts=4
  2453. */