main.c 81 KB

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