session.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Sascha Schumann <sascha@schumann.cx> |
  16. | Andrei Zmievski <andrei@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #ifdef PHP_WIN32
  24. # include "win32/winutil.h"
  25. # include "win32/time.h"
  26. #else
  27. # include <sys/time.h>
  28. #endif
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include "php_ini.h"
  32. #include "SAPI.h"
  33. #include "rfc1867.h"
  34. #include "php_variables.h"
  35. #include "php_session.h"
  36. #include "ext/standard/php_random.h"
  37. #include "ext/standard/php_var.h"
  38. #include "ext/date/php_date.h"
  39. #include "ext/standard/php_lcg.h"
  40. #include "ext/standard/url_scanner_ex.h"
  41. #include "ext/standard/info.h"
  42. #include "zend_smart_str.h"
  43. #include "ext/standard/url.h"
  44. #include "ext/standard/basic_functions.h"
  45. #include "ext/standard/head.h"
  46. #include "mod_files.h"
  47. #include "mod_user.h"
  48. #ifdef HAVE_LIBMM
  49. #include "mod_mm.h"
  50. #endif
  51. PHPAPI ZEND_DECLARE_MODULE_GLOBALS(ps)
  52. static int php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra);
  53. static int (*php_session_rfc1867_orig_callback)(unsigned int event, void *event_data, void **extra);
  54. static void php_session_track_init(void);
  55. /* SessionHandler class */
  56. zend_class_entry *php_session_class_entry;
  57. /* SessionHandlerInterface */
  58. zend_class_entry *php_session_iface_entry;
  59. /* SessionIdInterface */
  60. zend_class_entry *php_session_id_iface_entry;
  61. /* SessionUpdateTimestampHandler class */
  62. zend_class_entry *php_session_update_timestamp_class_entry;
  63. /* SessionUpdateTimestampInterface */
  64. zend_class_entry *php_session_update_timestamp_iface_entry;
  65. #define PS_MAX_SID_LENGTH 256
  66. /* ***********
  67. * Helpers *
  68. *********** */
  69. #define IF_SESSION_VARS() \
  70. if (Z_ISREF_P(&PS(http_session_vars)) && Z_TYPE_P(Z_REFVAL(PS(http_session_vars))) == IS_ARRAY)
  71. #define SESSION_CHECK_ACTIVE_STATE \
  72. if (PS(session_status) == php_session_active) { \
  73. php_error_docref(NULL, E_WARNING, "A session is active. You cannot change the session module's ini settings at this time"); \
  74. return FAILURE; \
  75. }
  76. #define SESSION_CHECK_OUTPUT_STATE \
  77. if (SG(headers_sent) && stage != ZEND_INI_STAGE_DEACTIVATE) { \
  78. php_error_docref(NULL, E_WARNING, "Headers already sent. You cannot change the session module's ini settings at this time"); \
  79. return FAILURE; \
  80. }
  81. #define APPLY_TRANS_SID (PS(use_trans_sid) && !PS(use_only_cookies))
  82. static int php_session_send_cookie(void);
  83. static int php_session_abort(void);
  84. /* Initialized in MINIT, readonly otherwise. */
  85. static int my_module_number = 0;
  86. /* Dispatched by RINIT and by php_session_destroy */
  87. static inline void php_rinit_session_globals(void) /* {{{ */
  88. {
  89. /* Do NOT init PS(mod_user_names) here! */
  90. /* TODO: These could be moved to MINIT and removed. These should be initialized by php_rshutdown_session_globals() always when execution is finished. */
  91. PS(id) = NULL;
  92. PS(session_status) = php_session_none;
  93. PS(in_save_handler) = 0;
  94. PS(set_handler) = 0;
  95. PS(mod_data) = NULL;
  96. PS(mod_user_is_open) = 0;
  97. PS(define_sid) = 1;
  98. PS(session_vars) = NULL;
  99. PS(module_number) = my_module_number;
  100. ZVAL_UNDEF(&PS(http_session_vars));
  101. }
  102. /* }}} */
  103. /* Dispatched by RSHUTDOWN and by php_session_destroy */
  104. static inline void php_rshutdown_session_globals(void) /* {{{ */
  105. {
  106. /* Do NOT destroy PS(mod_user_names) here! */
  107. if (!Z_ISUNDEF(PS(http_session_vars))) {
  108. zval_ptr_dtor(&PS(http_session_vars));
  109. ZVAL_UNDEF(&PS(http_session_vars));
  110. }
  111. if (PS(mod_data) || PS(mod_user_implemented)) {
  112. zend_try {
  113. PS(mod)->s_close(&PS(mod_data));
  114. } zend_end_try();
  115. }
  116. if (PS(id)) {
  117. zend_string_release_ex(PS(id), 0);
  118. PS(id) = NULL;
  119. }
  120. if (PS(session_vars)) {
  121. zend_string_release_ex(PS(session_vars), 0);
  122. PS(session_vars) = NULL;
  123. }
  124. /* User save handlers may end up directly here by misuse, bugs in user script, etc. */
  125. /* Set session status to prevent error while restoring save handler INI value. */
  126. PS(session_status) = php_session_none;
  127. }
  128. /* }}} */
  129. PHPAPI int php_session_destroy(void) /* {{{ */
  130. {
  131. int retval = SUCCESS;
  132. if (PS(session_status) != php_session_active) {
  133. php_error_docref(NULL, E_WARNING, "Trying to destroy uninitialized session");
  134. return FAILURE;
  135. }
  136. if (PS(id) && PS(mod)->s_destroy(&PS(mod_data), PS(id)) == FAILURE) {
  137. retval = FAILURE;
  138. php_error_docref(NULL, E_WARNING, "Session object destruction failed");
  139. }
  140. php_rshutdown_session_globals();
  141. php_rinit_session_globals();
  142. return retval;
  143. }
  144. /* }}} */
  145. PHPAPI void php_add_session_var(zend_string *name) /* {{{ */
  146. {
  147. IF_SESSION_VARS() {
  148. zval *sess_var = Z_REFVAL(PS(http_session_vars));
  149. SEPARATE_ARRAY(sess_var);
  150. if (!zend_hash_exists(Z_ARRVAL_P(sess_var), name)) {
  151. zval empty_var;
  152. ZVAL_NULL(&empty_var);
  153. zend_hash_update(Z_ARRVAL_P(sess_var), name, &empty_var);
  154. }
  155. }
  156. }
  157. /* }}} */
  158. PHPAPI zval* php_set_session_var(zend_string *name, zval *state_val, php_unserialize_data_t *var_hash) /* {{{ */
  159. {
  160. IF_SESSION_VARS() {
  161. zval *sess_var = Z_REFVAL(PS(http_session_vars));
  162. SEPARATE_ARRAY(sess_var);
  163. return zend_hash_update(Z_ARRVAL_P(sess_var), name, state_val);
  164. }
  165. return NULL;
  166. }
  167. /* }}} */
  168. PHPAPI zval* php_get_session_var(zend_string *name) /* {{{ */
  169. {
  170. IF_SESSION_VARS() {
  171. return zend_hash_find(Z_ARRVAL_P(Z_REFVAL(PS(http_session_vars))), name);
  172. }
  173. return NULL;
  174. }
  175. /* }}} */
  176. static void php_session_track_init(void) /* {{{ */
  177. {
  178. zval session_vars;
  179. zend_string *var_name = zend_string_init("_SESSION", sizeof("_SESSION") - 1, 0);
  180. /* Unconditionally destroy existing array -- possible dirty data */
  181. zend_delete_global_variable(var_name);
  182. if (!Z_ISUNDEF(PS(http_session_vars))) {
  183. zval_ptr_dtor(&PS(http_session_vars));
  184. }
  185. array_init(&session_vars);
  186. ZVAL_NEW_REF(&PS(http_session_vars), &session_vars);
  187. Z_ADDREF_P(&PS(http_session_vars));
  188. zend_hash_update_ind(&EG(symbol_table), var_name, &PS(http_session_vars));
  189. zend_string_release_ex(var_name, 0);
  190. }
  191. /* }}} */
  192. static zend_string *php_session_encode(void) /* {{{ */
  193. {
  194. IF_SESSION_VARS() {
  195. if (!PS(serializer)) {
  196. php_error_docref(NULL, E_WARNING, "Unknown session.serialize_handler. Failed to encode session object");
  197. return NULL;
  198. }
  199. return PS(serializer)->encode();
  200. } else {
  201. php_error_docref(NULL, E_WARNING, "Cannot encode non-existent session");
  202. }
  203. return NULL;
  204. }
  205. /* }}} */
  206. static int php_session_decode(zend_string *data) /* {{{ */
  207. {
  208. if (!PS(serializer)) {
  209. php_error_docref(NULL, E_WARNING, "Unknown session.serialize_handler. Failed to decode session object");
  210. return FAILURE;
  211. }
  212. if (PS(serializer)->decode(ZSTR_VAL(data), ZSTR_LEN(data)) == FAILURE) {
  213. php_session_destroy();
  214. php_session_track_init();
  215. php_error_docref(NULL, E_WARNING, "Failed to decode session object. Session has been destroyed");
  216. return FAILURE;
  217. }
  218. return SUCCESS;
  219. }
  220. /* }}} */
  221. /*
  222. * Note that we cannot use the BASE64 alphabet here, because
  223. * it contains "/" and "+": both are unacceptable for simple inclusion
  224. * into URLs.
  225. */
  226. static char hexconvtab[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,-";
  227. static void bin_to_readable(unsigned char *in, size_t inlen, char *out, size_t outlen, char nbits) /* {{{ */
  228. {
  229. unsigned char *p, *q;
  230. unsigned short w;
  231. int mask;
  232. int have;
  233. p = (unsigned char *)in;
  234. q = (unsigned char *)in + inlen;
  235. w = 0;
  236. have = 0;
  237. mask = (1 << nbits) - 1;
  238. while (outlen--) {
  239. if (have < nbits) {
  240. if (p < q) {
  241. w |= *p++ << have;
  242. have += 8;
  243. } else {
  244. /* Should never happen. Input must be large enough. */
  245. ZEND_ASSERT(0);
  246. break;
  247. }
  248. }
  249. /* consume nbits */
  250. *out++ = hexconvtab[w & mask];
  251. w >>= nbits;
  252. have -= nbits;
  253. }
  254. *out = '\0';
  255. }
  256. /* }}} */
  257. #define PS_EXTRA_RAND_BYTES 60
  258. PHPAPI zend_string *php_session_create_id(PS_CREATE_SID_ARGS) /* {{{ */
  259. {
  260. unsigned char rbuf[PS_MAX_SID_LENGTH + PS_EXTRA_RAND_BYTES];
  261. zend_string *outid;
  262. /* It would be enough to read ceil(sid_length * sid_bits_per_character / 8) bytes here.
  263. * We read sid_length bytes instead for simplicity. */
  264. /* Read additional PS_EXTRA_RAND_BYTES just in case CSPRNG is not safe enough */
  265. if (php_random_bytes_throw(rbuf, PS(sid_length) + PS_EXTRA_RAND_BYTES) == FAILURE) {
  266. return NULL;
  267. }
  268. outid = zend_string_alloc(PS(sid_length), 0);
  269. bin_to_readable(
  270. rbuf, PS(sid_length),
  271. ZSTR_VAL(outid), ZSTR_LEN(outid),
  272. (char)PS(sid_bits_per_character));
  273. return outid;
  274. }
  275. /* }}} */
  276. /* Default session id char validation function allowed by ps_modules.
  277. * If you change the logic here, please also update the error message in
  278. * ps_modules appropriately */
  279. PHPAPI int php_session_valid_key(const char *key) /* {{{ */
  280. {
  281. size_t len;
  282. const char *p;
  283. char c;
  284. int ret = SUCCESS;
  285. for (p = key; (c = *p); p++) {
  286. /* valid characters are a..z,A..Z,0..9 */
  287. if (!((c >= 'a' && c <= 'z')
  288. || (c >= 'A' && c <= 'Z')
  289. || (c >= '0' && c <= '9')
  290. || c == ','
  291. || c == '-')) {
  292. ret = FAILURE;
  293. break;
  294. }
  295. }
  296. len = p - key;
  297. /* Somewhat arbitrary length limit here, but should be way more than
  298. anyone needs and avoids file-level warnings later on if we exceed MAX_PATH */
  299. if (len == 0 || len > PS_MAX_SID_LENGTH) {
  300. ret = FAILURE;
  301. }
  302. return ret;
  303. }
  304. /* }}} */
  305. static zend_long php_session_gc(zend_bool immediate) /* {{{ */
  306. {
  307. int nrand;
  308. zend_long num = -1;
  309. /* GC must be done before reading session data. */
  310. if ((PS(mod_data) || PS(mod_user_implemented))) {
  311. if (immediate) {
  312. PS(mod)->s_gc(&PS(mod_data), PS(gc_maxlifetime), &num);
  313. return num;
  314. }
  315. nrand = (zend_long) ((float) PS(gc_divisor) * php_combined_lcg());
  316. if (PS(gc_probability) > 0 && nrand < PS(gc_probability)) {
  317. PS(mod)->s_gc(&PS(mod_data), PS(gc_maxlifetime), &num);
  318. }
  319. }
  320. return num;
  321. } /* }}} */
  322. static int php_session_initialize(void) /* {{{ */
  323. {
  324. zend_string *val = NULL;
  325. PS(session_status) = php_session_active;
  326. if (!PS(mod)) {
  327. PS(session_status) = php_session_disabled;
  328. php_error_docref(NULL, E_WARNING, "No storage module chosen - failed to initialize session");
  329. return FAILURE;
  330. }
  331. /* Open session handler first */
  332. if (PS(mod)->s_open(&PS(mod_data), PS(save_path), PS(session_name)) == FAILURE
  333. /* || PS(mod_data) == NULL */ /* FIXME: open must set valid PS(mod_data) with success */
  334. ) {
  335. php_session_abort();
  336. php_error_docref(NULL, E_WARNING, "Failed to initialize storage module: %s (path: %s)", PS(mod)->s_name, PS(save_path));
  337. return FAILURE;
  338. }
  339. /* If there is no ID, use session module to create one */
  340. if (!PS(id) || !ZSTR_VAL(PS(id))[0]) {
  341. if (PS(id)) {
  342. zend_string_release_ex(PS(id), 0);
  343. }
  344. PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
  345. if (!PS(id)) {
  346. php_session_abort();
  347. zend_throw_error(NULL, "Failed to create session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
  348. return FAILURE;
  349. }
  350. if (PS(use_cookies)) {
  351. PS(send_cookie) = 1;
  352. }
  353. } else if (PS(use_strict_mode) && PS(mod)->s_validate_sid &&
  354. PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == FAILURE) {
  355. if (PS(id)) {
  356. zend_string_release_ex(PS(id), 0);
  357. }
  358. PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
  359. if (!PS(id)) {
  360. PS(id) = php_session_create_id(NULL);
  361. }
  362. if (PS(use_cookies)) {
  363. PS(send_cookie) = 1;
  364. }
  365. }
  366. if (php_session_reset_id() == FAILURE) {
  367. php_session_abort();
  368. return FAILURE;
  369. }
  370. /* Read data */
  371. php_session_track_init();
  372. if (PS(mod)->s_read(&PS(mod_data), PS(id), &val, PS(gc_maxlifetime)) == FAILURE) {
  373. php_session_abort();
  374. /* FYI: Some broken save handlers return FAILURE for non-existent session ID, this is incorrect */
  375. php_error_docref(NULL, E_WARNING, "Failed to read session data: %s (path: %s)", PS(mod)->s_name, PS(save_path));
  376. return FAILURE;
  377. }
  378. /* GC must be done after read */
  379. php_session_gc(0);
  380. if (PS(session_vars)) {
  381. zend_string_release_ex(PS(session_vars), 0);
  382. PS(session_vars) = NULL;
  383. }
  384. if (val) {
  385. if (PS(lazy_write)) {
  386. PS(session_vars) = zend_string_copy(val);
  387. }
  388. php_session_decode(val);
  389. zend_string_release_ex(val, 0);
  390. }
  391. return SUCCESS;
  392. }
  393. /* }}} */
  394. static void php_session_save_current_state(int write) /* {{{ */
  395. {
  396. int ret = FAILURE;
  397. if (write) {
  398. IF_SESSION_VARS() {
  399. if (PS(mod_data) || PS(mod_user_implemented)) {
  400. zend_string *val;
  401. val = php_session_encode();
  402. if (val) {
  403. if (PS(lazy_write) && PS(session_vars)
  404. && PS(mod)->s_update_timestamp
  405. && PS(mod)->s_update_timestamp != php_session_update_timestamp
  406. && ZSTR_LEN(val) == ZSTR_LEN(PS(session_vars))
  407. && !memcmp(ZSTR_VAL(val), ZSTR_VAL(PS(session_vars)), ZSTR_LEN(val))
  408. ) {
  409. ret = PS(mod)->s_update_timestamp(&PS(mod_data), PS(id), val, PS(gc_maxlifetime));
  410. } else {
  411. ret = PS(mod)->s_write(&PS(mod_data), PS(id), val, PS(gc_maxlifetime));
  412. }
  413. zend_string_release_ex(val, 0);
  414. } else {
  415. ret = PS(mod)->s_write(&PS(mod_data), PS(id), ZSTR_EMPTY_ALLOC(), PS(gc_maxlifetime));
  416. }
  417. }
  418. if ((ret == FAILURE) && !EG(exception)) {
  419. if (!PS(mod_user_implemented)) {
  420. php_error_docref(NULL, E_WARNING, "Failed to write session data (%s). Please "
  421. "verify that the current setting of session.save_path "
  422. "is correct (%s)",
  423. PS(mod)->s_name,
  424. PS(save_path));
  425. } else {
  426. php_error_docref(NULL, E_WARNING, "Failed to write session data using user "
  427. "defined save handler. (session.save_path: %s)", PS(save_path));
  428. }
  429. }
  430. }
  431. }
  432. if (PS(mod_data) || PS(mod_user_implemented)) {
  433. PS(mod)->s_close(&PS(mod_data));
  434. }
  435. }
  436. /* }}} */
  437. static void php_session_normalize_vars() /* {{{ */
  438. {
  439. PS_ENCODE_VARS;
  440. IF_SESSION_VARS() {
  441. PS_ENCODE_LOOP(
  442. if (Z_TYPE_P(struc) == IS_PTR) {
  443. zval *zv = (zval *)Z_PTR_P(struc);
  444. ZVAL_COPY_VALUE(struc, zv);
  445. ZVAL_UNDEF(zv);
  446. }
  447. );
  448. }
  449. }
  450. /* }}} */
  451. /* *************************
  452. * INI Settings/Handlers *
  453. ************************* */
  454. static PHP_INI_MH(OnUpdateSaveHandler) /* {{{ */
  455. {
  456. const ps_module *tmp;
  457. SESSION_CHECK_ACTIVE_STATE;
  458. SESSION_CHECK_OUTPUT_STATE;
  459. tmp = _php_find_ps_module(ZSTR_VAL(new_value));
  460. if (PG(modules_activated) && !tmp) {
  461. int err_type;
  462. if (stage == ZEND_INI_STAGE_RUNTIME) {
  463. err_type = E_WARNING;
  464. } else {
  465. err_type = E_ERROR;
  466. }
  467. /* Do not output error when restoring ini options. */
  468. if (stage != ZEND_INI_STAGE_DEACTIVATE) {
  469. php_error_docref(NULL, err_type, "Cannot find save handler '%s'", ZSTR_VAL(new_value));
  470. }
  471. return FAILURE;
  472. }
  473. /* "user" save handler should not be set by user */
  474. if (!PS(set_handler) && tmp == ps_user_ptr) {
  475. php_error_docref(NULL, E_RECOVERABLE_ERROR, "Cannot set 'user' save handler by ini_set() or session_module_name()");
  476. return FAILURE;
  477. }
  478. PS(default_mod) = PS(mod);
  479. PS(mod) = tmp;
  480. return SUCCESS;
  481. }
  482. /* }}} */
  483. static PHP_INI_MH(OnUpdateSerializer) /* {{{ */
  484. {
  485. const ps_serializer *tmp;
  486. SESSION_CHECK_ACTIVE_STATE;
  487. SESSION_CHECK_OUTPUT_STATE;
  488. tmp = _php_find_ps_serializer(ZSTR_VAL(new_value));
  489. if (PG(modules_activated) && !tmp) {
  490. int err_type;
  491. if (stage == ZEND_INI_STAGE_RUNTIME) {
  492. err_type = E_WARNING;
  493. } else {
  494. err_type = E_ERROR;
  495. }
  496. /* Do not output error when restoring ini options. */
  497. if (stage != ZEND_INI_STAGE_DEACTIVATE) {
  498. php_error_docref(NULL, err_type, "Cannot find serialization handler '%s'", ZSTR_VAL(new_value));
  499. }
  500. return FAILURE;
  501. }
  502. PS(serializer) = tmp;
  503. return SUCCESS;
  504. }
  505. /* }}} */
  506. static PHP_INI_MH(OnUpdateTransSid) /* {{{ */
  507. {
  508. SESSION_CHECK_ACTIVE_STATE;
  509. SESSION_CHECK_OUTPUT_STATE;
  510. if (!strncasecmp(ZSTR_VAL(new_value), "on", sizeof("on"))) {
  511. PS(use_trans_sid) = (zend_bool) 1;
  512. } else {
  513. PS(use_trans_sid) = (zend_bool) atoi(ZSTR_VAL(new_value));
  514. }
  515. return SUCCESS;
  516. }
  517. /* }}} */
  518. static PHP_INI_MH(OnUpdateSaveDir) /* {{{ */
  519. {
  520. SESSION_CHECK_ACTIVE_STATE;
  521. SESSION_CHECK_OUTPUT_STATE;
  522. /* Only do the safemode/open_basedir check at runtime */
  523. if (stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) {
  524. char *p;
  525. if (memchr(ZSTR_VAL(new_value), '\0', ZSTR_LEN(new_value)) != NULL) {
  526. return FAILURE;
  527. }
  528. /* we do not use zend_memrchr() since path can contain ; itself */
  529. if ((p = strchr(ZSTR_VAL(new_value), ';'))) {
  530. char *p2;
  531. p++;
  532. if ((p2 = strchr(p, ';'))) {
  533. p = p2 + 1;
  534. }
  535. } else {
  536. p = ZSTR_VAL(new_value);
  537. }
  538. if (PG(open_basedir) && *p && php_check_open_basedir(p)) {
  539. return FAILURE;
  540. }
  541. }
  542. return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  543. }
  544. /* }}} */
  545. static PHP_INI_MH(OnUpdateName) /* {{{ */
  546. {
  547. SESSION_CHECK_ACTIVE_STATE;
  548. SESSION_CHECK_OUTPUT_STATE;
  549. /* Numeric session.name won't work at all */
  550. if ((!ZSTR_LEN(new_value) || is_numeric_string(ZSTR_VAL(new_value), ZSTR_LEN(new_value), NULL, NULL, 0))) {
  551. int err_type;
  552. if (stage == ZEND_INI_STAGE_RUNTIME || stage == ZEND_INI_STAGE_ACTIVATE || stage == ZEND_INI_STAGE_STARTUP) {
  553. err_type = E_WARNING;
  554. } else {
  555. err_type = E_ERROR;
  556. }
  557. /* Do not output error when restoring ini options. */
  558. if (stage != ZEND_INI_STAGE_DEACTIVATE) {
  559. php_error_docref(NULL, err_type, "session.name cannot be a numeric or empty '%s'", ZSTR_VAL(new_value));
  560. }
  561. return FAILURE;
  562. }
  563. return OnUpdateStringUnempty(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  564. }
  565. /* }}} */
  566. static PHP_INI_MH(OnUpdateCookieLifetime) /* {{{ */
  567. {
  568. SESSION_CHECK_ACTIVE_STATE;
  569. SESSION_CHECK_OUTPUT_STATE;
  570. if (atol(ZSTR_VAL(new_value)) < 0) {
  571. php_error_docref(NULL, E_WARNING, "CookieLifetime cannot be negative");
  572. return FAILURE;
  573. }
  574. return OnUpdateLongGEZero(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  575. }
  576. /* }}} */
  577. static PHP_INI_MH(OnUpdateSessionLong) /* {{{ */
  578. {
  579. SESSION_CHECK_ACTIVE_STATE;
  580. SESSION_CHECK_OUTPUT_STATE;
  581. return OnUpdateLong(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  582. }
  583. /* }}} */
  584. static PHP_INI_MH(OnUpdateSessionString) /* {{{ */
  585. {
  586. SESSION_CHECK_ACTIVE_STATE;
  587. SESSION_CHECK_OUTPUT_STATE;
  588. return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  589. }
  590. /* }}} */
  591. static PHP_INI_MH(OnUpdateSessionBool) /* {{{ */
  592. {
  593. SESSION_CHECK_OUTPUT_STATE;
  594. SESSION_CHECK_ACTIVE_STATE;
  595. return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  596. }
  597. /* }}} */
  598. static PHP_INI_MH(OnUpdateSidLength) /* {{{ */
  599. {
  600. zend_long val;
  601. char *endptr = NULL;
  602. SESSION_CHECK_OUTPUT_STATE;
  603. SESSION_CHECK_ACTIVE_STATE;
  604. val = ZEND_STRTOL(ZSTR_VAL(new_value), &endptr, 10);
  605. if (endptr && (*endptr == '\0')
  606. && val >= 22 && val <= PS_MAX_SID_LENGTH) {
  607. /* Numeric value */
  608. PS(sid_length) = val;
  609. return SUCCESS;
  610. }
  611. php_error_docref(NULL, E_WARNING, "session.configuration 'session.sid_length' must be between 22 and 256.");
  612. return FAILURE;
  613. }
  614. /* }}} */
  615. static PHP_INI_MH(OnUpdateSidBits) /* {{{ */
  616. {
  617. zend_long val;
  618. char *endptr = NULL;
  619. SESSION_CHECK_OUTPUT_STATE;
  620. SESSION_CHECK_ACTIVE_STATE;
  621. val = ZEND_STRTOL(ZSTR_VAL(new_value), &endptr, 10);
  622. if (endptr && (*endptr == '\0')
  623. && val >= 4 && val <=6) {
  624. /* Numeric value */
  625. PS(sid_bits_per_character) = val;
  626. return SUCCESS;
  627. }
  628. php_error_docref(NULL, E_WARNING, "session.configuration 'session.sid_bits_per_character' must be between 4 and 6.");
  629. return FAILURE;
  630. }
  631. /* }}} */
  632. static PHP_INI_MH(OnUpdateLazyWrite) /* {{{ */
  633. {
  634. SESSION_CHECK_ACTIVE_STATE;
  635. SESSION_CHECK_OUTPUT_STATE;
  636. return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  637. }
  638. /* }}} */
  639. static PHP_INI_MH(OnUpdateRfc1867Freq) /* {{{ */
  640. {
  641. int tmp;
  642. tmp = zend_atoi(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
  643. if(tmp < 0) {
  644. php_error_docref(NULL, E_WARNING, "session.upload_progress.freq must be greater than or equal to zero");
  645. return FAILURE;
  646. }
  647. if(ZSTR_LEN(new_value) > 0 && ZSTR_VAL(new_value)[ZSTR_LEN(new_value)-1] == '%') {
  648. if(tmp > 100) {
  649. php_error_docref(NULL, E_WARNING, "session.upload_progress.freq cannot be over 100%%");
  650. return FAILURE;
  651. }
  652. PS(rfc1867_freq) = -tmp;
  653. } else {
  654. PS(rfc1867_freq) = tmp;
  655. }
  656. return SUCCESS;
  657. } /* }}} */
  658. /* {{{ PHP_INI
  659. */
  660. PHP_INI_BEGIN()
  661. STD_PHP_INI_ENTRY("session.save_path", "", PHP_INI_ALL, OnUpdateSaveDir, save_path, php_ps_globals, ps_globals)
  662. STD_PHP_INI_ENTRY("session.name", "PHPSESSID", PHP_INI_ALL, OnUpdateName, session_name, php_ps_globals, ps_globals)
  663. PHP_INI_ENTRY("session.save_handler", "files", PHP_INI_ALL, OnUpdateSaveHandler)
  664. STD_PHP_INI_BOOLEAN("session.auto_start", "0", PHP_INI_PERDIR, OnUpdateBool, auto_start, php_ps_globals, ps_globals)
  665. STD_PHP_INI_ENTRY("session.gc_probability", "1", PHP_INI_ALL, OnUpdateSessionLong, gc_probability, php_ps_globals, ps_globals)
  666. STD_PHP_INI_ENTRY("session.gc_divisor", "100", PHP_INI_ALL, OnUpdateSessionLong, gc_divisor, php_ps_globals, ps_globals)
  667. STD_PHP_INI_ENTRY("session.gc_maxlifetime", "1440", PHP_INI_ALL, OnUpdateSessionLong, gc_maxlifetime, php_ps_globals, ps_globals)
  668. PHP_INI_ENTRY("session.serialize_handler", "php", PHP_INI_ALL, OnUpdateSerializer)
  669. STD_PHP_INI_ENTRY("session.cookie_lifetime", "0", PHP_INI_ALL, OnUpdateCookieLifetime,cookie_lifetime, php_ps_globals, ps_globals)
  670. STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateSessionString, cookie_path, php_ps_globals, ps_globals)
  671. STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateSessionString, cookie_domain, php_ps_globals, ps_globals)
  672. STD_PHP_INI_ENTRY("session.cookie_secure", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_secure, php_ps_globals, ps_globals)
  673. STD_PHP_INI_ENTRY("session.cookie_httponly", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_httponly, php_ps_globals, ps_globals)
  674. STD_PHP_INI_ENTRY("session.cookie_samesite", "", PHP_INI_ALL, OnUpdateString, cookie_samesite, php_ps_globals, ps_globals)
  675. STD_PHP_INI_ENTRY("session.use_cookies", "1", PHP_INI_ALL, OnUpdateSessionBool, use_cookies, php_ps_globals, ps_globals)
  676. STD_PHP_INI_ENTRY("session.use_only_cookies", "1", PHP_INI_ALL, OnUpdateSessionBool, use_only_cookies, php_ps_globals, ps_globals)
  677. STD_PHP_INI_ENTRY("session.use_strict_mode", "0", PHP_INI_ALL, OnUpdateSessionBool, use_strict_mode, php_ps_globals, ps_globals)
  678. STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateSessionString, extern_referer_chk, php_ps_globals, ps_globals)
  679. STD_PHP_INI_ENTRY("session.cache_limiter", "nocache", PHP_INI_ALL, OnUpdateSessionString, cache_limiter, php_ps_globals, ps_globals)
  680. STD_PHP_INI_ENTRY("session.cache_expire", "180", PHP_INI_ALL, OnUpdateSessionLong, cache_expire, php_ps_globals, ps_globals)
  681. PHP_INI_ENTRY("session.use_trans_sid", "0", PHP_INI_ALL, OnUpdateTransSid)
  682. PHP_INI_ENTRY("session.sid_length", "32", PHP_INI_ALL, OnUpdateSidLength)
  683. PHP_INI_ENTRY("session.sid_bits_per_character", "4", PHP_INI_ALL, OnUpdateSidBits)
  684. STD_PHP_INI_BOOLEAN("session.lazy_write", "1", PHP_INI_ALL, OnUpdateLazyWrite, lazy_write, php_ps_globals, ps_globals)
  685. /* Upload progress */
  686. STD_PHP_INI_BOOLEAN("session.upload_progress.enabled",
  687. "1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_enabled, php_ps_globals, ps_globals)
  688. STD_PHP_INI_BOOLEAN("session.upload_progress.cleanup",
  689. "1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_cleanup, php_ps_globals, ps_globals)
  690. STD_PHP_INI_ENTRY("session.upload_progress.prefix",
  691. "upload_progress_", ZEND_INI_PERDIR, OnUpdateString, rfc1867_prefix, php_ps_globals, ps_globals)
  692. STD_PHP_INI_ENTRY("session.upload_progress.name",
  693. "PHP_SESSION_UPLOAD_PROGRESS", ZEND_INI_PERDIR, OnUpdateString, rfc1867_name, php_ps_globals, ps_globals)
  694. STD_PHP_INI_ENTRY("session.upload_progress.freq", "1%", ZEND_INI_PERDIR, OnUpdateRfc1867Freq, rfc1867_freq, php_ps_globals, ps_globals)
  695. STD_PHP_INI_ENTRY("session.upload_progress.min_freq",
  696. "1", ZEND_INI_PERDIR, OnUpdateReal, rfc1867_min_freq,php_ps_globals, ps_globals)
  697. /* Commented out until future discussion */
  698. /* PHP_INI_ENTRY("session.encode_sources", "globals,track", PHP_INI_ALL, NULL) */
  699. PHP_INI_END()
  700. /* }}} */
  701. /* ***************
  702. * Serializers *
  703. *************** */
  704. PS_SERIALIZER_ENCODE_FUNC(php_serialize) /* {{{ */
  705. {
  706. smart_str buf = {0};
  707. php_serialize_data_t var_hash;
  708. IF_SESSION_VARS() {
  709. PHP_VAR_SERIALIZE_INIT(var_hash);
  710. php_var_serialize(&buf, Z_REFVAL(PS(http_session_vars)), &var_hash);
  711. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  712. }
  713. return buf.s;
  714. }
  715. /* }}} */
  716. PS_SERIALIZER_DECODE_FUNC(php_serialize) /* {{{ */
  717. {
  718. const char *endptr = val + vallen;
  719. zval session_vars;
  720. php_unserialize_data_t var_hash;
  721. int result;
  722. zend_string *var_name = zend_string_init("_SESSION", sizeof("_SESSION") - 1, 0);
  723. ZVAL_NULL(&session_vars);
  724. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  725. result = php_var_unserialize(
  726. &session_vars, (const unsigned char **)&val, (const unsigned char *)endptr, &var_hash);
  727. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  728. if (!result) {
  729. zval_ptr_dtor(&session_vars);
  730. ZVAL_NULL(&session_vars);
  731. }
  732. if (!Z_ISUNDEF(PS(http_session_vars))) {
  733. zval_ptr_dtor(&PS(http_session_vars));
  734. }
  735. if (Z_TYPE(session_vars) == IS_NULL) {
  736. array_init(&session_vars);
  737. }
  738. ZVAL_NEW_REF(&PS(http_session_vars), &session_vars);
  739. Z_ADDREF_P(&PS(http_session_vars));
  740. zend_hash_update_ind(&EG(symbol_table), var_name, &PS(http_session_vars));
  741. zend_string_release_ex(var_name, 0);
  742. return SUCCESS;
  743. }
  744. /* }}} */
  745. #define PS_BIN_NR_OF_BITS 8
  746. #define PS_BIN_UNDEF (1<<(PS_BIN_NR_OF_BITS-1))
  747. #define PS_BIN_MAX (PS_BIN_UNDEF-1)
  748. PS_SERIALIZER_ENCODE_FUNC(php_binary) /* {{{ */
  749. {
  750. smart_str buf = {0};
  751. php_serialize_data_t var_hash;
  752. PS_ENCODE_VARS;
  753. PHP_VAR_SERIALIZE_INIT(var_hash);
  754. PS_ENCODE_LOOP(
  755. if (ZSTR_LEN(key) > PS_BIN_MAX) continue;
  756. smart_str_appendc(&buf, (unsigned char)ZSTR_LEN(key));
  757. smart_str_appendl(&buf, ZSTR_VAL(key), ZSTR_LEN(key));
  758. php_var_serialize(&buf, struc, &var_hash);
  759. );
  760. smart_str_0(&buf);
  761. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  762. return buf.s;
  763. }
  764. /* }}} */
  765. PS_SERIALIZER_DECODE_FUNC(php_binary) /* {{{ */
  766. {
  767. const char *p;
  768. const char *endptr = val + vallen;
  769. int namelen;
  770. zend_string *name;
  771. php_unserialize_data_t var_hash;
  772. zval *current, rv;
  773. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  774. for (p = val; p < endptr; ) {
  775. namelen = ((unsigned char)(*p)) & (~PS_BIN_UNDEF);
  776. if (namelen < 0 || namelen > PS_BIN_MAX || (p + namelen) >= endptr) {
  777. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  778. return FAILURE;
  779. }
  780. name = zend_string_init(p + 1, namelen, 0);
  781. p += namelen + 1;
  782. current = var_tmp_var(&var_hash);
  783. if (php_var_unserialize(current, (const unsigned char **) &p, (const unsigned char *) endptr, &var_hash)) {
  784. ZVAL_PTR(&rv, current);
  785. php_set_session_var(name, &rv, &var_hash);
  786. } else {
  787. zend_string_release_ex(name, 0);
  788. php_session_normalize_vars();
  789. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  790. return FAILURE;
  791. }
  792. zend_string_release_ex(name, 0);
  793. }
  794. php_session_normalize_vars();
  795. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  796. return SUCCESS;
  797. }
  798. /* }}} */
  799. #define PS_DELIMITER '|'
  800. PS_SERIALIZER_ENCODE_FUNC(php) /* {{{ */
  801. {
  802. smart_str buf = {0};
  803. php_serialize_data_t var_hash;
  804. PS_ENCODE_VARS;
  805. PHP_VAR_SERIALIZE_INIT(var_hash);
  806. PS_ENCODE_LOOP(
  807. smart_str_appendl(&buf, ZSTR_VAL(key), ZSTR_LEN(key));
  808. if (memchr(ZSTR_VAL(key), PS_DELIMITER, ZSTR_LEN(key))) {
  809. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  810. smart_str_free(&buf);
  811. return NULL;
  812. }
  813. smart_str_appendc(&buf, PS_DELIMITER);
  814. php_var_serialize(&buf, struc, &var_hash);
  815. );
  816. smart_str_0(&buf);
  817. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  818. return buf.s;
  819. }
  820. /* }}} */
  821. PS_SERIALIZER_DECODE_FUNC(php) /* {{{ */
  822. {
  823. const char *p, *q;
  824. const char *endptr = val + vallen;
  825. ptrdiff_t namelen;
  826. zend_string *name;
  827. int retval = SUCCESS;
  828. php_unserialize_data_t var_hash;
  829. zval *current, rv;
  830. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  831. p = val;
  832. while (p < endptr) {
  833. q = p;
  834. while (*q != PS_DELIMITER) {
  835. if (++q >= endptr) goto break_outer_loop;
  836. }
  837. namelen = q - p;
  838. name = zend_string_init(p, namelen, 0);
  839. q++;
  840. current = var_tmp_var(&var_hash);
  841. if (php_var_unserialize(current, (const unsigned char **)&q, (const unsigned char *)endptr, &var_hash)) {
  842. ZVAL_PTR(&rv, current);
  843. php_set_session_var(name, &rv, &var_hash);
  844. } else {
  845. zend_string_release_ex(name, 0);
  846. retval = FAILURE;
  847. goto break_outer_loop;
  848. }
  849. zend_string_release_ex(name, 0);
  850. p = q;
  851. }
  852. break_outer_loop:
  853. php_session_normalize_vars();
  854. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  855. return retval;
  856. }
  857. /* }}} */
  858. #define MAX_SERIALIZERS 32
  859. #define PREDEFINED_SERIALIZERS 3
  860. static ps_serializer ps_serializers[MAX_SERIALIZERS + 1] = {
  861. PS_SERIALIZER_ENTRY(php_serialize),
  862. PS_SERIALIZER_ENTRY(php),
  863. PS_SERIALIZER_ENTRY(php_binary)
  864. };
  865. PHPAPI int php_session_register_serializer(const char *name, zend_string *(*encode)(PS_SERIALIZER_ENCODE_ARGS), int (*decode)(PS_SERIALIZER_DECODE_ARGS)) /* {{{ */
  866. {
  867. int ret = FAILURE;
  868. int i;
  869. for (i = 0; i < MAX_SERIALIZERS; i++) {
  870. if (ps_serializers[i].name == NULL) {
  871. ps_serializers[i].name = name;
  872. ps_serializers[i].encode = encode;
  873. ps_serializers[i].decode = decode;
  874. ps_serializers[i + 1].name = NULL;
  875. ret = SUCCESS;
  876. break;
  877. }
  878. }
  879. return ret;
  880. }
  881. /* }}} */
  882. /* *******************
  883. * Storage Modules *
  884. ******************* */
  885. #define MAX_MODULES 32
  886. #define PREDEFINED_MODULES 2
  887. static const ps_module *ps_modules[MAX_MODULES + 1] = {
  888. ps_files_ptr,
  889. ps_user_ptr
  890. };
  891. PHPAPI int php_session_register_module(const ps_module *ptr) /* {{{ */
  892. {
  893. int ret = FAILURE;
  894. int i;
  895. for (i = 0; i < MAX_MODULES; i++) {
  896. if (!ps_modules[i]) {
  897. ps_modules[i] = ptr;
  898. ret = SUCCESS;
  899. break;
  900. }
  901. }
  902. return ret;
  903. }
  904. /* }}} */
  905. /* Dummy PS module function */
  906. PHPAPI int php_session_validate_sid(PS_VALIDATE_SID_ARGS) {
  907. return SUCCESS;
  908. }
  909. /* Dummy PS module function */
  910. PHPAPI int php_session_update_timestamp(PS_UPDATE_TIMESTAMP_ARGS) {
  911. return SUCCESS;
  912. }
  913. /* ******************
  914. * Cache Limiters *
  915. ****************** */
  916. typedef struct {
  917. char *name;
  918. void (*func)(void);
  919. } php_session_cache_limiter_t;
  920. #define CACHE_LIMITER(name) _php_cache_limiter_##name
  921. #define CACHE_LIMITER_FUNC(name) static void CACHE_LIMITER(name)(void)
  922. #define CACHE_LIMITER_ENTRY(name) { #name, CACHE_LIMITER(name) },
  923. #define ADD_HEADER(a) sapi_add_header(a, strlen(a), 1);
  924. #define MAX_STR 512
  925. static char *month_names[] = {
  926. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  927. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  928. };
  929. static char *week_days[] = {
  930. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
  931. };
  932. static inline void strcpy_gmt(char *ubuf, time_t *when) /* {{{ */
  933. {
  934. char buf[MAX_STR];
  935. struct tm tm, *res;
  936. int n;
  937. res = php_gmtime_r(when, &tm);
  938. if (!res) {
  939. ubuf[0] = '\0';
  940. return;
  941. }
  942. n = slprintf(buf, sizeof(buf), "%s, %02d %s %d %02d:%02d:%02d GMT", /* SAFE */
  943. week_days[tm.tm_wday], tm.tm_mday,
  944. month_names[tm.tm_mon], tm.tm_year + 1900,
  945. tm.tm_hour, tm.tm_min,
  946. tm.tm_sec);
  947. memcpy(ubuf, buf, n);
  948. ubuf[n] = '\0';
  949. }
  950. /* }}} */
  951. static inline void last_modified(void) /* {{{ */
  952. {
  953. const char *path;
  954. zend_stat_t sb;
  955. char buf[MAX_STR + 1];
  956. path = SG(request_info).path_translated;
  957. if (path) {
  958. if (VCWD_STAT(path, &sb) == -1) {
  959. return;
  960. }
  961. #define LAST_MODIFIED "Last-Modified: "
  962. memcpy(buf, LAST_MODIFIED, sizeof(LAST_MODIFIED) - 1);
  963. strcpy_gmt(buf + sizeof(LAST_MODIFIED) - 1, &sb.st_mtime);
  964. ADD_HEADER(buf);
  965. }
  966. }
  967. /* }}} */
  968. #define EXPIRES "Expires: "
  969. CACHE_LIMITER_FUNC(public) /* {{{ */
  970. {
  971. char buf[MAX_STR + 1];
  972. struct timeval tv;
  973. time_t now;
  974. gettimeofday(&tv, NULL);
  975. now = tv.tv_sec + PS(cache_expire) * 60;
  976. memcpy(buf, EXPIRES, sizeof(EXPIRES) - 1);
  977. strcpy_gmt(buf + sizeof(EXPIRES) - 1, &now);
  978. ADD_HEADER(buf);
  979. snprintf(buf, sizeof(buf) , "Cache-Control: public, max-age=" ZEND_LONG_FMT, PS(cache_expire) * 60); /* SAFE */
  980. ADD_HEADER(buf);
  981. last_modified();
  982. }
  983. /* }}} */
  984. CACHE_LIMITER_FUNC(private_no_expire) /* {{{ */
  985. {
  986. char buf[MAX_STR + 1];
  987. snprintf(buf, sizeof(buf), "Cache-Control: private, max-age=" ZEND_LONG_FMT, PS(cache_expire) * 60); /* SAFE */
  988. ADD_HEADER(buf);
  989. last_modified();
  990. }
  991. /* }}} */
  992. CACHE_LIMITER_FUNC(private) /* {{{ */
  993. {
  994. ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
  995. CACHE_LIMITER(private_no_expire)();
  996. }
  997. /* }}} */
  998. CACHE_LIMITER_FUNC(nocache) /* {{{ */
  999. {
  1000. ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
  1001. /* For HTTP/1.1 conforming clients */
  1002. ADD_HEADER("Cache-Control: no-store, no-cache, must-revalidate");
  1003. /* For HTTP/1.0 conforming clients */
  1004. ADD_HEADER("Pragma: no-cache");
  1005. }
  1006. /* }}} */
  1007. static const php_session_cache_limiter_t php_session_cache_limiters[] = {
  1008. CACHE_LIMITER_ENTRY(public)
  1009. CACHE_LIMITER_ENTRY(private)
  1010. CACHE_LIMITER_ENTRY(private_no_expire)
  1011. CACHE_LIMITER_ENTRY(nocache)
  1012. {0}
  1013. };
  1014. static int php_session_cache_limiter(void) /* {{{ */
  1015. {
  1016. const php_session_cache_limiter_t *lim;
  1017. if (PS(cache_limiter)[0] == '\0') return 0;
  1018. if (PS(session_status) != php_session_active) return -1;
  1019. if (SG(headers_sent)) {
  1020. const char *output_start_filename = php_output_get_start_filename();
  1021. int output_start_lineno = php_output_get_start_lineno();
  1022. php_session_abort();
  1023. if (output_start_filename) {
  1024. php_error_docref(NULL, E_WARNING, "Cannot send session cache limiter - headers already sent (output started at %s:%d)", output_start_filename, output_start_lineno);
  1025. } else {
  1026. php_error_docref(NULL, E_WARNING, "Cannot send session cache limiter - headers already sent");
  1027. }
  1028. return -2;
  1029. }
  1030. for (lim = php_session_cache_limiters; lim->name; lim++) {
  1031. if (!strcasecmp(lim->name, PS(cache_limiter))) {
  1032. lim->func();
  1033. return 0;
  1034. }
  1035. }
  1036. return -1;
  1037. }
  1038. /* }}} */
  1039. /* *********************
  1040. * Cookie Management *
  1041. ********************* */
  1042. /*
  1043. * Remove already sent session ID cookie.
  1044. * It must be directly removed from SG(sapi_header) because sapi_add_header_ex()
  1045. * removes all of matching cookie. i.e. It deletes all of Set-Cookie headers.
  1046. */
  1047. static void php_session_remove_cookie(void) {
  1048. sapi_header_struct *header;
  1049. zend_llist *l = &SG(sapi_headers).headers;
  1050. zend_llist_element *next;
  1051. zend_llist_element *current;
  1052. char *session_cookie;
  1053. zend_string *e_session_name;
  1054. size_t session_cookie_len;
  1055. size_t len = sizeof("Set-Cookie")-1;
  1056. e_session_name = php_url_encode(PS(session_name), strlen(PS(session_name)));
  1057. spprintf(&session_cookie, 0, "Set-Cookie: %s=", ZSTR_VAL(e_session_name));
  1058. zend_string_free(e_session_name);
  1059. session_cookie_len = strlen(session_cookie);
  1060. current = l->head;
  1061. while (current) {
  1062. header = (sapi_header_struct *)(current->data);
  1063. next = current->next;
  1064. if (header->header_len > len && header->header[len] == ':'
  1065. && !strncmp(header->header, session_cookie, session_cookie_len)) {
  1066. if (current->prev) {
  1067. current->prev->next = next;
  1068. } else {
  1069. l->head = next;
  1070. }
  1071. if (next) {
  1072. next->prev = current->prev;
  1073. } else {
  1074. l->tail = current->prev;
  1075. }
  1076. sapi_free_header(header);
  1077. efree(current);
  1078. --l->count;
  1079. }
  1080. current = next;
  1081. }
  1082. efree(session_cookie);
  1083. }
  1084. static int php_session_send_cookie(void) /* {{{ */
  1085. {
  1086. smart_str ncookie = {0};
  1087. zend_string *date_fmt = NULL;
  1088. zend_string *e_session_name, *e_id;
  1089. if (SG(headers_sent)) {
  1090. const char *output_start_filename = php_output_get_start_filename();
  1091. int output_start_lineno = php_output_get_start_lineno();
  1092. if (output_start_filename) {
  1093. php_error_docref(NULL, E_WARNING, "Cannot send session cookie - headers already sent by (output started at %s:%d)", output_start_filename, output_start_lineno);
  1094. } else {
  1095. php_error_docref(NULL, E_WARNING, "Cannot send session cookie - headers already sent");
  1096. }
  1097. return FAILURE;
  1098. }
  1099. /* URL encode session_name and id because they might be user supplied */
  1100. e_session_name = php_url_encode(PS(session_name), strlen(PS(session_name)));
  1101. e_id = php_url_encode(ZSTR_VAL(PS(id)), ZSTR_LEN(PS(id)));
  1102. smart_str_appendl(&ncookie, "Set-Cookie: ", sizeof("Set-Cookie: ")-1);
  1103. smart_str_appendl(&ncookie, ZSTR_VAL(e_session_name), ZSTR_LEN(e_session_name));
  1104. smart_str_appendc(&ncookie, '=');
  1105. smart_str_appendl(&ncookie, ZSTR_VAL(e_id), ZSTR_LEN(e_id));
  1106. zend_string_release_ex(e_session_name, 0);
  1107. zend_string_release_ex(e_id, 0);
  1108. if (PS(cookie_lifetime) > 0) {
  1109. struct timeval tv;
  1110. time_t t;
  1111. gettimeofday(&tv, NULL);
  1112. t = tv.tv_sec + PS(cookie_lifetime);
  1113. if (t > 0) {
  1114. date_fmt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, t, 0);
  1115. smart_str_appends(&ncookie, COOKIE_EXPIRES);
  1116. smart_str_appendl(&ncookie, ZSTR_VAL(date_fmt), ZSTR_LEN(date_fmt));
  1117. zend_string_release_ex(date_fmt, 0);
  1118. smart_str_appends(&ncookie, COOKIE_MAX_AGE);
  1119. smart_str_append_long(&ncookie, PS(cookie_lifetime));
  1120. }
  1121. }
  1122. if (PS(cookie_path)[0]) {
  1123. smart_str_appends(&ncookie, COOKIE_PATH);
  1124. smart_str_appends(&ncookie, PS(cookie_path));
  1125. }
  1126. if (PS(cookie_domain)[0]) {
  1127. smart_str_appends(&ncookie, COOKIE_DOMAIN);
  1128. smart_str_appends(&ncookie, PS(cookie_domain));
  1129. }
  1130. if (PS(cookie_secure)) {
  1131. smart_str_appends(&ncookie, COOKIE_SECURE);
  1132. }
  1133. if (PS(cookie_httponly)) {
  1134. smart_str_appends(&ncookie, COOKIE_HTTPONLY);
  1135. }
  1136. if (PS(cookie_samesite)[0]) {
  1137. smart_str_appends(&ncookie, COOKIE_SAMESITE);
  1138. smart_str_appends(&ncookie, PS(cookie_samesite));
  1139. }
  1140. smart_str_0(&ncookie);
  1141. php_session_remove_cookie(); /* remove already sent session ID cookie */
  1142. /* 'replace' must be 0 here, else a previous Set-Cookie
  1143. header, probably sent with setcookie() will be replaced! */
  1144. sapi_add_header_ex(estrndup(ZSTR_VAL(ncookie.s), ZSTR_LEN(ncookie.s)), ZSTR_LEN(ncookie.s), 0, 0);
  1145. smart_str_free(&ncookie);
  1146. return SUCCESS;
  1147. }
  1148. /* }}} */
  1149. PHPAPI const ps_module *_php_find_ps_module(char *name) /* {{{ */
  1150. {
  1151. const ps_module *ret = NULL;
  1152. const ps_module **mod;
  1153. int i;
  1154. for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) {
  1155. if (*mod && !strcasecmp(name, (*mod)->s_name)) {
  1156. ret = *mod;
  1157. break;
  1158. }
  1159. }
  1160. return ret;
  1161. }
  1162. /* }}} */
  1163. PHPAPI const ps_serializer *_php_find_ps_serializer(char *name) /* {{{ */
  1164. {
  1165. const ps_serializer *ret = NULL;
  1166. const ps_serializer *mod;
  1167. for (mod = ps_serializers; mod->name; mod++) {
  1168. if (!strcasecmp(name, mod->name)) {
  1169. ret = mod;
  1170. break;
  1171. }
  1172. }
  1173. return ret;
  1174. }
  1175. /* }}} */
  1176. static void ppid2sid(zval *ppid) {
  1177. ZVAL_DEREF(ppid);
  1178. if (Z_TYPE_P(ppid) == IS_STRING) {
  1179. PS(id) = zend_string_init(Z_STRVAL_P(ppid), Z_STRLEN_P(ppid), 0);
  1180. PS(send_cookie) = 0;
  1181. } else {
  1182. PS(id) = NULL;
  1183. PS(send_cookie) = 1;
  1184. }
  1185. }
  1186. PHPAPI int php_session_reset_id(void) /* {{{ */
  1187. {
  1188. int module_number = PS(module_number);
  1189. zval *sid, *data, *ppid;
  1190. zend_bool apply_trans_sid;
  1191. if (!PS(id)) {
  1192. php_error_docref(NULL, E_WARNING, "Cannot set session ID - session ID is not initialized");
  1193. return FAILURE;
  1194. }
  1195. if (PS(use_cookies) && PS(send_cookie)) {
  1196. php_session_send_cookie();
  1197. PS(send_cookie) = 0;
  1198. }
  1199. /* If the SID constant exists, destroy it. */
  1200. /* We must not delete any items in EG(zend_constants) */
  1201. /* zend_hash_str_del(EG(zend_constants), "sid", sizeof("sid") - 1); */
  1202. sid = zend_get_constant_str("SID", sizeof("SID") - 1);
  1203. if (PS(define_sid)) {
  1204. smart_str var = {0};
  1205. smart_str_appends(&var, PS(session_name));
  1206. smart_str_appendc(&var, '=');
  1207. smart_str_appends(&var, ZSTR_VAL(PS(id)));
  1208. smart_str_0(&var);
  1209. if (sid) {
  1210. zval_ptr_dtor_str(sid);
  1211. ZVAL_NEW_STR(sid, var.s);
  1212. } else {
  1213. REGISTER_STRINGL_CONSTANT("SID", ZSTR_VAL(var.s), ZSTR_LEN(var.s), 0);
  1214. smart_str_free(&var);
  1215. }
  1216. } else {
  1217. if (sid) {
  1218. zval_ptr_dtor_str(sid);
  1219. ZVAL_EMPTY_STRING(sid);
  1220. } else {
  1221. REGISTER_STRINGL_CONSTANT("SID", "", 0, 0);
  1222. }
  1223. }
  1224. /* Apply trans sid if sid cookie is not set */
  1225. apply_trans_sid = 0;
  1226. if (APPLY_TRANS_SID) {
  1227. apply_trans_sid = 1;
  1228. if (PS(use_cookies) &&
  1229. (data = zend_hash_str_find(&EG(symbol_table), "_COOKIE", sizeof("_COOKIE") - 1))) {
  1230. ZVAL_DEREF(data);
  1231. if (Z_TYPE_P(data) == IS_ARRAY &&
  1232. (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), strlen(PS(session_name))))) {
  1233. ZVAL_DEREF(ppid);
  1234. apply_trans_sid = 0;
  1235. }
  1236. }
  1237. }
  1238. if (apply_trans_sid) {
  1239. zend_string *sname;
  1240. sname = zend_string_init(PS(session_name), strlen(PS(session_name)), 0);
  1241. php_url_scanner_reset_session_var(sname, 1); /* This may fail when session name has changed */
  1242. zend_string_release_ex(sname, 0);
  1243. php_url_scanner_add_session_var(PS(session_name), strlen(PS(session_name)), ZSTR_VAL(PS(id)), ZSTR_LEN(PS(id)), 1);
  1244. }
  1245. return SUCCESS;
  1246. }
  1247. /* }}} */
  1248. PHPAPI int php_session_start(void) /* {{{ */
  1249. {
  1250. zval *ppid;
  1251. zval *data;
  1252. char *p, *value;
  1253. size_t lensess;
  1254. switch (PS(session_status)) {
  1255. case php_session_active:
  1256. php_error(E_NOTICE, "A session had already been started - ignoring session_start()");
  1257. return FAILURE;
  1258. break;
  1259. case php_session_disabled:
  1260. value = zend_ini_string("session.save_handler", sizeof("session.save_handler") - 1, 0);
  1261. if (!PS(mod) && value) {
  1262. PS(mod) = _php_find_ps_module(value);
  1263. if (!PS(mod)) {
  1264. php_error_docref(NULL, E_WARNING, "Cannot find save handler '%s' - session startup failed", value);
  1265. return FAILURE;
  1266. }
  1267. }
  1268. value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler") - 1, 0);
  1269. if (!PS(serializer) && value) {
  1270. PS(serializer) = _php_find_ps_serializer(value);
  1271. if (!PS(serializer)) {
  1272. php_error_docref(NULL, E_WARNING, "Cannot find serialization handler '%s' - session startup failed", value);
  1273. return FAILURE;
  1274. }
  1275. }
  1276. PS(session_status) = php_session_none;
  1277. /* Fall through */
  1278. case php_session_none:
  1279. default:
  1280. /* Setup internal flags */
  1281. PS(define_sid) = !PS(use_only_cookies); /* SID constant is defined when non-cookie ID is used */
  1282. PS(send_cookie) = PS(use_cookies) || PS(use_only_cookies);
  1283. }
  1284. lensess = strlen(PS(session_name));
  1285. /*
  1286. * Cookies are preferred, because initially cookie and get
  1287. * variables will be available.
  1288. * URL/POST session ID may be used when use_only_cookies=Off.
  1289. * session.use_strice_mode=On prevents session adoption.
  1290. * Session based file upload progress uses non-cookie ID.
  1291. */
  1292. if (!PS(id)) {
  1293. if (PS(use_cookies) && (data = zend_hash_str_find(&EG(symbol_table), "_COOKIE", sizeof("_COOKIE") - 1))) {
  1294. ZVAL_DEREF(data);
  1295. if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), lensess))) {
  1296. ppid2sid(ppid);
  1297. PS(send_cookie) = 0;
  1298. PS(define_sid) = 0;
  1299. }
  1300. }
  1301. /* Initialize session ID from non cookie values */
  1302. if (!PS(use_only_cookies)) {
  1303. if (!PS(id) && (data = zend_hash_str_find(&EG(symbol_table), "_GET", sizeof("_GET") - 1))) {
  1304. ZVAL_DEREF(data);
  1305. if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), lensess))) {
  1306. ppid2sid(ppid);
  1307. }
  1308. }
  1309. if (!PS(id) && (data = zend_hash_str_find(&EG(symbol_table), "_POST", sizeof("_POST") - 1))) {
  1310. ZVAL_DEREF(data);
  1311. if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), lensess))) {
  1312. ppid2sid(ppid);
  1313. }
  1314. }
  1315. /* Check the REQUEST_URI symbol for a string of the form
  1316. * '<session-name>=<session-id>' to allow URLs of the form
  1317. * http://yoursite/<session-name>=<session-id>/script.php */
  1318. if (!PS(id) && zend_is_auto_global_str("_SERVER", sizeof("_SERVER") - 1) == SUCCESS &&
  1319. (data = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "REQUEST_URI", sizeof("REQUEST_URI") - 1)) &&
  1320. Z_TYPE_P(data) == IS_STRING &&
  1321. (p = strstr(Z_STRVAL_P(data), PS(session_name))) &&
  1322. p[lensess] == '='
  1323. ) {
  1324. char *q;
  1325. p += lensess + 1;
  1326. if ((q = strpbrk(p, "/?\\"))) {
  1327. PS(id) = zend_string_init(p, q - p, 0);
  1328. }
  1329. }
  1330. /* Check whether the current request was referred to by
  1331. * an external site which invalidates the previously found id. */
  1332. if (PS(id) && PS(extern_referer_chk)[0] != '\0' &&
  1333. !Z_ISUNDEF(PG(http_globals)[TRACK_VARS_SERVER]) &&
  1334. (data = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_REFERER", sizeof("HTTP_REFERER") - 1)) &&
  1335. Z_TYPE_P(data) == IS_STRING &&
  1336. Z_STRLEN_P(data) != 0 &&
  1337. strstr(Z_STRVAL_P(data), PS(extern_referer_chk)) == NULL
  1338. ) {
  1339. zend_string_release_ex(PS(id), 0);
  1340. PS(id) = NULL;
  1341. }
  1342. }
  1343. }
  1344. /* Finally check session id for dangerous characters
  1345. * Security note: session id may be embedded in HTML pages.*/
  1346. if (PS(id) && strpbrk(ZSTR_VAL(PS(id)), "\r\n\t <>'\"\\")) {
  1347. zend_string_release_ex(PS(id), 0);
  1348. PS(id) = NULL;
  1349. }
  1350. if (php_session_initialize() == FAILURE
  1351. || php_session_cache_limiter() == -2) {
  1352. PS(session_status) = php_session_none;
  1353. if (PS(id)) {
  1354. zend_string_release_ex(PS(id), 0);
  1355. PS(id) = NULL;
  1356. }
  1357. return FAILURE;
  1358. }
  1359. return SUCCESS;
  1360. }
  1361. /* }}} */
  1362. PHPAPI int php_session_flush(int write) /* {{{ */
  1363. {
  1364. if (PS(session_status) == php_session_active) {
  1365. php_session_save_current_state(write);
  1366. PS(session_status) = php_session_none;
  1367. return SUCCESS;
  1368. }
  1369. return FAILURE;
  1370. }
  1371. /* }}} */
  1372. static int php_session_abort(void) /* {{{ */
  1373. {
  1374. if (PS(session_status) == php_session_active) {
  1375. if (PS(mod_data) || PS(mod_user_implemented)) {
  1376. PS(mod)->s_close(&PS(mod_data));
  1377. }
  1378. PS(session_status) = php_session_none;
  1379. return SUCCESS;
  1380. }
  1381. return FAILURE;
  1382. }
  1383. /* }}} */
  1384. static int php_session_reset(void) /* {{{ */
  1385. {
  1386. if (PS(session_status) == php_session_active
  1387. && php_session_initialize() == SUCCESS) {
  1388. return SUCCESS;
  1389. }
  1390. return FAILURE;
  1391. }
  1392. /* }}} */
  1393. /* This API is not used by any PHP modules including session currently.
  1394. session_adapt_url() may be used to set Session ID to target url without
  1395. starting "URL-Rewriter" output handler. */
  1396. PHPAPI void session_adapt_url(const char *url, size_t urllen, char **new, size_t *newlen) /* {{{ */
  1397. {
  1398. if (APPLY_TRANS_SID && (PS(session_status) == php_session_active)) {
  1399. *new = php_url_scanner_adapt_single_url(url, urllen, PS(session_name), ZSTR_VAL(PS(id)), newlen, 1);
  1400. }
  1401. }
  1402. /* }}} */
  1403. /* ********************************
  1404. * Userspace exported functions *
  1405. ******************************** */
  1406. /* {{{ proto bool session_set_cookie_params(int lifetime [, string path [, string domain [, bool secure[, bool httponly]]]])
  1407. session_set_cookie_params(array options)
  1408. Set session cookie parameters */
  1409. static PHP_FUNCTION(session_set_cookie_params)
  1410. {
  1411. zval *lifetime_or_options = NULL;
  1412. zend_string *lifetime = NULL, *path = NULL, *domain = NULL, *samesite = NULL;
  1413. zend_bool secure = 0, secure_null = 1;
  1414. zend_bool httponly = 0, httponly_null = 1;
  1415. zend_string *ini_name;
  1416. int result;
  1417. int found = 0;
  1418. if (!PS(use_cookies)) {
  1419. return;
  1420. }
  1421. ZEND_PARSE_PARAMETERS_START(1, 5)
  1422. Z_PARAM_ZVAL(lifetime_or_options)
  1423. Z_PARAM_OPTIONAL
  1424. Z_PARAM_STR(path)
  1425. Z_PARAM_STR(domain)
  1426. Z_PARAM_BOOL_EX(secure, secure_null, 1, 0)
  1427. Z_PARAM_BOOL_EX(httponly, httponly_null, 1, 0)
  1428. ZEND_PARSE_PARAMETERS_END();
  1429. if (PS(session_status) == php_session_active) {
  1430. php_error_docref(NULL, E_WARNING, "Cannot change session cookie parameters when session is active");
  1431. RETURN_FALSE;
  1432. }
  1433. if (SG(headers_sent)) {
  1434. php_error_docref(NULL, E_WARNING, "Cannot change session cookie parameters when headers already sent");
  1435. RETURN_FALSE;
  1436. }
  1437. if (Z_TYPE_P(lifetime_or_options) == IS_ARRAY) {
  1438. zend_string *key;
  1439. zval *value;
  1440. if (path) {
  1441. path = NULL;
  1442. domain = NULL;
  1443. secure_null = 1;
  1444. httponly_null = 1;
  1445. php_error_docref(NULL, E_WARNING, "Cannot pass arguments after the options array");
  1446. RETURN_FALSE;
  1447. }
  1448. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(lifetime_or_options), key, value) {
  1449. if (key) {
  1450. ZVAL_DEREF(value);
  1451. if(!strcasecmp("lifetime", ZSTR_VAL(key))) {
  1452. lifetime = zval_get_string(value);
  1453. found++;
  1454. } else if(!strcasecmp("path", ZSTR_VAL(key))) {
  1455. path = zval_get_string(value);
  1456. found++;
  1457. } else if(!strcasecmp("domain", ZSTR_VAL(key))) {
  1458. domain = zval_get_string(value);
  1459. found++;
  1460. } else if(!strcasecmp("secure", ZSTR_VAL(key))) {
  1461. secure = zval_is_true(value);
  1462. secure_null = 0;
  1463. found++;
  1464. } else if(!strcasecmp("httponly", ZSTR_VAL(key))) {
  1465. httponly = zval_is_true(value);
  1466. httponly_null = 0;
  1467. found++;
  1468. } else if(!strcasecmp("samesite", ZSTR_VAL(key))) {
  1469. samesite = zval_get_string(value);
  1470. found++;
  1471. } else {
  1472. php_error_docref(NULL, E_WARNING, "Unrecognized key '%s' found in the options array", ZSTR_VAL(key));
  1473. }
  1474. } else {
  1475. php_error_docref(NULL, E_WARNING, "Numeric key found in the options array");
  1476. }
  1477. } ZEND_HASH_FOREACH_END();
  1478. if (found == 0) {
  1479. php_error_docref(NULL, E_WARNING, "No valid keys were found in the options array");
  1480. RETURN_FALSE;
  1481. }
  1482. } else {
  1483. lifetime = zval_get_string(lifetime_or_options);
  1484. }
  1485. if (lifetime) {
  1486. ini_name = zend_string_init("session.cookie_lifetime", sizeof("session.cookie_lifetime") - 1, 0);
  1487. result = zend_alter_ini_entry(ini_name, lifetime, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1488. zend_string_release(lifetime);
  1489. zend_string_release_ex(ini_name, 0);
  1490. if (result == FAILURE) {
  1491. RETURN_FALSE;
  1492. }
  1493. }
  1494. if (path) {
  1495. ini_name = zend_string_init("session.cookie_path", sizeof("session.cookie_path") - 1, 0);
  1496. result = zend_alter_ini_entry(ini_name, path, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1497. if (found > 0) {
  1498. zend_string_release(path);
  1499. }
  1500. zend_string_release_ex(ini_name, 0);
  1501. if (result == FAILURE) {
  1502. RETURN_FALSE;
  1503. }
  1504. }
  1505. if (domain) {
  1506. ini_name = zend_string_init("session.cookie_domain", sizeof("session.cookie_domain") - 1, 0);
  1507. result = zend_alter_ini_entry(ini_name, domain, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1508. if (found > 0) {
  1509. zend_string_release(domain);
  1510. }
  1511. zend_string_release_ex(ini_name, 0);
  1512. if (result == FAILURE) {
  1513. RETURN_FALSE;
  1514. }
  1515. }
  1516. if (!secure_null) {
  1517. ini_name = zend_string_init("session.cookie_secure", sizeof("session.cookie_secure") - 1, 0);
  1518. result = zend_alter_ini_entry_chars(ini_name, secure ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1519. zend_string_release_ex(ini_name, 0);
  1520. if (result == FAILURE) {
  1521. RETURN_FALSE;
  1522. }
  1523. }
  1524. if (!httponly_null) {
  1525. ini_name = zend_string_init("session.cookie_httponly", sizeof("session.cookie_httponly") - 1, 0);
  1526. result = zend_alter_ini_entry_chars(ini_name, httponly ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1527. zend_string_release_ex(ini_name, 0);
  1528. if (result == FAILURE) {
  1529. RETURN_FALSE;
  1530. }
  1531. }
  1532. if (samesite) {
  1533. ini_name = zend_string_init("session.cookie_samesite", sizeof("session.cookie_samesite") - 1, 0);
  1534. result = zend_alter_ini_entry(ini_name, samesite, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1535. if (found > 0) {
  1536. zend_string_release(samesite);
  1537. }
  1538. zend_string_release_ex(ini_name, 0);
  1539. if (result == FAILURE) {
  1540. RETURN_FALSE;
  1541. }
  1542. }
  1543. RETURN_TRUE;
  1544. }
  1545. /* }}} */
  1546. /* {{{ proto array session_get_cookie_params(void)
  1547. Return the session cookie parameters */
  1548. static PHP_FUNCTION(session_get_cookie_params)
  1549. {
  1550. if (zend_parse_parameters_none() == FAILURE) {
  1551. return;
  1552. }
  1553. array_init(return_value);
  1554. add_assoc_long(return_value, "lifetime", PS(cookie_lifetime));
  1555. add_assoc_string(return_value, "path", PS(cookie_path));
  1556. add_assoc_string(return_value, "domain", PS(cookie_domain));
  1557. add_assoc_bool(return_value, "secure", PS(cookie_secure));
  1558. add_assoc_bool(return_value, "httponly", PS(cookie_httponly));
  1559. add_assoc_string(return_value, "samesite", PS(cookie_samesite));
  1560. }
  1561. /* }}} */
  1562. /* {{{ proto string session_name([string newname])
  1563. Return the current session name. If newname is given, the session name is replaced with newname */
  1564. static PHP_FUNCTION(session_name)
  1565. {
  1566. zend_string *name = NULL;
  1567. zend_string *ini_name;
  1568. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S", &name) == FAILURE) {
  1569. return;
  1570. }
  1571. if (name && PS(session_status) == php_session_active) {
  1572. php_error_docref(NULL, E_WARNING, "Cannot change session name when session is active");
  1573. RETURN_FALSE;
  1574. }
  1575. if (name && SG(headers_sent)) {
  1576. php_error_docref(NULL, E_WARNING, "Cannot change session name when headers already sent");
  1577. RETURN_FALSE;
  1578. }
  1579. RETVAL_STRING(PS(session_name));
  1580. if (name) {
  1581. ini_name = zend_string_init("session.name", sizeof("session.name") - 1, 0);
  1582. zend_alter_ini_entry(ini_name, name, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1583. zend_string_release_ex(ini_name, 0);
  1584. }
  1585. }
  1586. /* }}} */
  1587. /* {{{ proto string session_module_name([string newname])
  1588. Return the current module name used for accessing session data. If newname is given, the module name is replaced with newname */
  1589. static PHP_FUNCTION(session_module_name)
  1590. {
  1591. zend_string *name = NULL;
  1592. zend_string *ini_name;
  1593. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S", &name) == FAILURE) {
  1594. return;
  1595. }
  1596. if (name && PS(session_status) == php_session_active) {
  1597. php_error_docref(NULL, E_WARNING, "Cannot change save handler module when session is active");
  1598. RETURN_FALSE;
  1599. }
  1600. if (name && SG(headers_sent)) {
  1601. php_error_docref(NULL, E_WARNING, "Cannot change save handler module when headers already sent");
  1602. RETURN_FALSE;
  1603. }
  1604. /* Set return_value to current module name */
  1605. if (PS(mod) && PS(mod)->s_name) {
  1606. RETVAL_STRING(PS(mod)->s_name);
  1607. } else {
  1608. RETVAL_EMPTY_STRING();
  1609. }
  1610. if (name) {
  1611. if (!_php_find_ps_module(ZSTR_VAL(name))) {
  1612. php_error_docref(NULL, E_WARNING, "Cannot find named PHP session module (%s)", ZSTR_VAL(name));
  1613. zval_ptr_dtor_str(return_value);
  1614. RETURN_FALSE;
  1615. }
  1616. if (PS(mod_data) || PS(mod_user_implemented)) {
  1617. PS(mod)->s_close(&PS(mod_data));
  1618. }
  1619. PS(mod_data) = NULL;
  1620. ini_name = zend_string_init("session.save_handler", sizeof("session.save_handler") - 1, 0);
  1621. zend_alter_ini_entry(ini_name, name, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1622. zend_string_release_ex(ini_name, 0);
  1623. }
  1624. }
  1625. /* }}} */
  1626. /* {{{ proto bool session_set_save_handler(string open, string close, string read, string write, string destroy, string gc, string create_sid)
  1627. Sets user-level functions */
  1628. static PHP_FUNCTION(session_set_save_handler)
  1629. {
  1630. zval *args = NULL;
  1631. int i, num_args, argc = ZEND_NUM_ARGS();
  1632. zend_string *ini_name, *ini_val;
  1633. if (PS(session_status) == php_session_active) {
  1634. php_error_docref(NULL, E_WARNING, "Cannot change save handler when session is active");
  1635. RETURN_FALSE;
  1636. }
  1637. if (SG(headers_sent)) {
  1638. php_error_docref(NULL, E_WARNING, "Cannot change save handler when headers already sent");
  1639. RETURN_FALSE;
  1640. }
  1641. if (argc > 0 && argc <= 2) {
  1642. zval *obj = NULL;
  1643. zend_string *func_name;
  1644. zend_function *current_mptr;
  1645. zend_bool register_shutdown = 1;
  1646. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &obj, php_session_iface_entry, &register_shutdown) == FAILURE) {
  1647. RETURN_FALSE;
  1648. }
  1649. /* For compatibility reason, implemeted interface is not checked */
  1650. /* Find implemented methods - SessionHandlerInterface */
  1651. i = 0;
  1652. ZEND_HASH_FOREACH_STR_KEY(&php_session_iface_entry->function_table, func_name) {
  1653. if ((current_mptr = zend_hash_find_ptr(&Z_OBJCE_P(obj)->function_table, func_name))) {
  1654. if (!Z_ISUNDEF(PS(mod_user_names).names[i])) {
  1655. zval_ptr_dtor(&PS(mod_user_names).names[i]);
  1656. }
  1657. array_init_size(&PS(mod_user_names).names[i], 2);
  1658. Z_ADDREF_P(obj);
  1659. add_next_index_zval(&PS(mod_user_names).names[i], obj);
  1660. add_next_index_str(&PS(mod_user_names).names[i], zend_string_copy(func_name));
  1661. } else {
  1662. php_error_docref(NULL, E_ERROR, "Session handler's function table is corrupt");
  1663. RETURN_FALSE;
  1664. }
  1665. ++i;
  1666. } ZEND_HASH_FOREACH_END();
  1667. /* Find implemented methods - SessionIdInterface (optional) */
  1668. ZEND_HASH_FOREACH_STR_KEY(&php_session_id_iface_entry->function_table, func_name) {
  1669. if ((current_mptr = zend_hash_find_ptr(&Z_OBJCE_P(obj)->function_table, func_name))) {
  1670. if (!Z_ISUNDEF(PS(mod_user_names).names[i])) {
  1671. zval_ptr_dtor(&PS(mod_user_names).names[i]);
  1672. }
  1673. array_init_size(&PS(mod_user_names).names[i], 2);
  1674. Z_ADDREF_P(obj);
  1675. add_next_index_zval(&PS(mod_user_names).names[i], obj);
  1676. add_next_index_str(&PS(mod_user_names).names[i], zend_string_copy(func_name));
  1677. } else {
  1678. if (!Z_ISUNDEF(PS(mod_user_names).names[i])) {
  1679. zval_ptr_dtor(&PS(mod_user_names).names[i]);
  1680. ZVAL_UNDEF(&PS(mod_user_names).names[i]);
  1681. }
  1682. }
  1683. ++i;
  1684. } ZEND_HASH_FOREACH_END();
  1685. /* Find implemented methods - SessionUpdateTimestampInterface (optional) */
  1686. ZEND_HASH_FOREACH_STR_KEY(&php_session_update_timestamp_iface_entry->function_table, func_name) {
  1687. if ((current_mptr = zend_hash_find_ptr(&Z_OBJCE_P(obj)->function_table, func_name))) {
  1688. if (!Z_ISUNDEF(PS(mod_user_names).names[i])) {
  1689. zval_ptr_dtor(&PS(mod_user_names).names[i]);
  1690. }
  1691. array_init_size(&PS(mod_user_names).names[i], 2);
  1692. Z_ADDREF_P(obj);
  1693. add_next_index_zval(&PS(mod_user_names).names[i], obj);
  1694. add_next_index_str(&PS(mod_user_names).names[i], zend_string_copy(func_name));
  1695. } else {
  1696. if (!Z_ISUNDEF(PS(mod_user_names).names[i])) {
  1697. zval_ptr_dtor(&PS(mod_user_names).names[i]);
  1698. ZVAL_UNDEF(&PS(mod_user_names).names[i]);
  1699. }
  1700. }
  1701. ++i;
  1702. } ZEND_HASH_FOREACH_END();
  1703. if (register_shutdown) {
  1704. /* create shutdown function */
  1705. php_shutdown_function_entry shutdown_function_entry;
  1706. shutdown_function_entry.arg_count = 1;
  1707. shutdown_function_entry.arguments = (zval *) safe_emalloc(sizeof(zval), 1, 0);
  1708. ZVAL_STRING(&shutdown_function_entry.arguments[0], "session_register_shutdown");
  1709. /* add shutdown function, removing the old one if it exists */
  1710. if (!register_user_shutdown_function("session_shutdown", sizeof("session_shutdown") - 1, &shutdown_function_entry)) {
  1711. zval_ptr_dtor(&shutdown_function_entry.arguments[0]);
  1712. efree(shutdown_function_entry.arguments);
  1713. php_error_docref(NULL, E_WARNING, "Unable to register session shutdown function");
  1714. RETURN_FALSE;
  1715. }
  1716. } else {
  1717. /* remove shutdown function */
  1718. remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") - 1);
  1719. }
  1720. if (PS(mod) && PS(session_status) != php_session_active && PS(mod) != &ps_mod_user) {
  1721. ini_name = zend_string_init("session.save_handler", sizeof("session.save_handler") - 1, 0);
  1722. ini_val = zend_string_init("user", sizeof("user") - 1, 0);
  1723. PS(set_handler) = 1;
  1724. zend_alter_ini_entry(ini_name, ini_val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1725. PS(set_handler) = 0;
  1726. zend_string_release_ex(ini_val, 0);
  1727. zend_string_release_ex(ini_name, 0);
  1728. }
  1729. RETURN_TRUE;
  1730. }
  1731. /* Set procedural save handler functions */
  1732. if (argc < 6 || PS_NUM_APIS < argc) {
  1733. WRONG_PARAM_COUNT;
  1734. }
  1735. if (zend_parse_parameters(argc, "+", &args, &num_args) == FAILURE) {
  1736. return;
  1737. }
  1738. /* remove shutdown function */
  1739. remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") - 1);
  1740. /* At this point argc can only be between 6 and PS_NUM_APIS */
  1741. for (i = 0; i < argc; i++) {
  1742. if (!zend_is_callable(&args[i], 0, NULL)) {
  1743. zend_string *name = zend_get_callable_name(&args[i]);
  1744. php_error_docref(NULL, E_WARNING, "Argument %d is not a valid callback", i+1);
  1745. zend_string_release_ex(name, 0);
  1746. RETURN_FALSE;
  1747. }
  1748. }
  1749. if (PS(mod) && PS(mod) != &ps_mod_user) {
  1750. ini_name = zend_string_init("session.save_handler", sizeof("session.save_handler") - 1, 0);
  1751. ini_val = zend_string_init("user", sizeof("user") - 1, 0);
  1752. PS(set_handler) = 1;
  1753. zend_alter_ini_entry(ini_name, ini_val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1754. PS(set_handler) = 0;
  1755. zend_string_release_ex(ini_val, 0);
  1756. zend_string_release_ex(ini_name, 0);
  1757. }
  1758. for (i = 0; i < argc; i++) {
  1759. if (!Z_ISUNDEF(PS(mod_user_names).names[i])) {
  1760. zval_ptr_dtor(&PS(mod_user_names).names[i]);
  1761. }
  1762. ZVAL_COPY(&PS(mod_user_names).names[i], &args[i]);
  1763. }
  1764. RETURN_TRUE;
  1765. }
  1766. /* }}} */
  1767. /* {{{ proto string session_save_path([string newname])
  1768. Return the current save path passed to module_name. If newname is given, the save path is replaced with newname */
  1769. static PHP_FUNCTION(session_save_path)
  1770. {
  1771. zend_string *name = NULL;
  1772. zend_string *ini_name;
  1773. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S", &name) == FAILURE) {
  1774. return;
  1775. }
  1776. if (name && PS(session_status) == php_session_active) {
  1777. php_error_docref(NULL, E_WARNING, "Cannot change save path when session is active");
  1778. RETURN_FALSE;
  1779. }
  1780. if (name && SG(headers_sent)) {
  1781. php_error_docref(NULL, E_WARNING, "Cannot change save path when headers already sent");
  1782. RETURN_FALSE;
  1783. }
  1784. RETVAL_STRING(PS(save_path));
  1785. if (name) {
  1786. if (memchr(ZSTR_VAL(name), '\0', ZSTR_LEN(name)) != NULL) {
  1787. php_error_docref(NULL, E_WARNING, "The save_path cannot contain NULL characters");
  1788. zval_ptr_dtor_str(return_value);
  1789. RETURN_FALSE;
  1790. }
  1791. ini_name = zend_string_init("session.save_path", sizeof("session.save_path") - 1, 0);
  1792. zend_alter_ini_entry(ini_name, name, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1793. zend_string_release_ex(ini_name, 0);
  1794. }
  1795. }
  1796. /* }}} */
  1797. /* {{{ proto string session_id([string newid])
  1798. Return the current session id. If newid is given, the session id is replaced with newid */
  1799. static PHP_FUNCTION(session_id)
  1800. {
  1801. zend_string *name = NULL;
  1802. int argc = ZEND_NUM_ARGS();
  1803. if (zend_parse_parameters(argc, "|S", &name) == FAILURE) {
  1804. return;
  1805. }
  1806. if (name && PS(use_cookies) && SG(headers_sent)) {
  1807. php_error_docref(NULL, E_WARNING, "Cannot change session id when headers already sent");
  1808. RETURN_FALSE;
  1809. }
  1810. if (name && PS(session_status) == php_session_active) {
  1811. php_error_docref(NULL, E_WARNING, "Cannot change session id when session is active");
  1812. RETURN_FALSE;
  1813. }
  1814. if (PS(id)) {
  1815. /* keep compatibility for "\0" characters ???
  1816. * see: ext/session/tests/session_id_error3.phpt */
  1817. size_t len = strlen(ZSTR_VAL(PS(id)));
  1818. if (UNEXPECTED(len != ZSTR_LEN(PS(id)))) {
  1819. RETVAL_NEW_STR(zend_string_init(ZSTR_VAL(PS(id)), len, 0));
  1820. } else {
  1821. RETVAL_STR_COPY(PS(id));
  1822. }
  1823. } else {
  1824. RETVAL_EMPTY_STRING();
  1825. }
  1826. if (name) {
  1827. if (PS(id)) {
  1828. zend_string_release_ex(PS(id), 0);
  1829. }
  1830. PS(id) = zend_string_copy(name);
  1831. }
  1832. }
  1833. /* }}} */
  1834. /* {{{ proto bool session_regenerate_id([bool delete_old_session])
  1835. Update the current session id with a newly generated one. If delete_old_session is set to true, remove the old session. */
  1836. static PHP_FUNCTION(session_regenerate_id)
  1837. {
  1838. zend_bool del_ses = 0;
  1839. zend_string *data;
  1840. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &del_ses) == FAILURE) {
  1841. return;
  1842. }
  1843. if (PS(session_status) != php_session_active) {
  1844. php_error_docref(NULL, E_WARNING, "Cannot regenerate session id - session is not active");
  1845. RETURN_FALSE;
  1846. }
  1847. if (SG(headers_sent)) {
  1848. php_error_docref(NULL, E_WARNING, "Cannot regenerate session id - headers already sent");
  1849. RETURN_FALSE;
  1850. }
  1851. /* Process old session data */
  1852. if (del_ses) {
  1853. if (PS(mod)->s_destroy(&PS(mod_data), PS(id)) == FAILURE) {
  1854. PS(mod)->s_close(&PS(mod_data));
  1855. PS(session_status) = php_session_none;
  1856. php_error_docref(NULL, E_WARNING, "Session object destruction failed. ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
  1857. RETURN_FALSE;
  1858. }
  1859. } else {
  1860. int ret;
  1861. data = php_session_encode();
  1862. if (data) {
  1863. ret = PS(mod)->s_write(&PS(mod_data), PS(id), data, PS(gc_maxlifetime));
  1864. zend_string_release_ex(data, 0);
  1865. } else {
  1866. ret = PS(mod)->s_write(&PS(mod_data), PS(id), ZSTR_EMPTY_ALLOC(), PS(gc_maxlifetime));
  1867. }
  1868. if (ret == FAILURE) {
  1869. PS(mod)->s_close(&PS(mod_data));
  1870. PS(session_status) = php_session_none;
  1871. php_error_docref(NULL, E_WARNING, "Session write failed. ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
  1872. RETURN_FALSE;
  1873. }
  1874. }
  1875. PS(mod)->s_close(&PS(mod_data));
  1876. /* New session data */
  1877. if (PS(session_vars)) {
  1878. zend_string_release_ex(PS(session_vars), 0);
  1879. PS(session_vars) = NULL;
  1880. }
  1881. zend_string_release_ex(PS(id), 0);
  1882. PS(id) = NULL;
  1883. if (PS(mod)->s_open(&PS(mod_data), PS(save_path), PS(session_name)) == FAILURE) {
  1884. PS(session_status) = php_session_none;
  1885. zend_throw_error(NULL, "Failed to open session: %s (path: %s)", PS(mod)->s_name, PS(save_path));
  1886. RETURN_FALSE;
  1887. }
  1888. PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
  1889. if (!PS(id)) {
  1890. PS(session_status) = php_session_none;
  1891. zend_throw_error(NULL, "Failed to create new session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
  1892. RETURN_FALSE;
  1893. }
  1894. if (PS(use_strict_mode) && PS(mod)->s_validate_sid &&
  1895. PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == SUCCESS) {
  1896. zend_string_release_ex(PS(id), 0);
  1897. PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
  1898. if (!PS(id)) {
  1899. PS(mod)->s_close(&PS(mod_data));
  1900. PS(session_status) = php_session_none;
  1901. zend_throw_error(NULL, "Failed to create session ID by collision: %s (path: %s)", PS(mod)->s_name, PS(save_path));
  1902. RETURN_FALSE;
  1903. }
  1904. }
  1905. /* Read is required to make new session data at this point. */
  1906. if (PS(mod)->s_read(&PS(mod_data), PS(id), &data, PS(gc_maxlifetime)) == FAILURE) {
  1907. PS(mod)->s_close(&PS(mod_data));
  1908. PS(session_status) = php_session_none;
  1909. zend_throw_error(NULL, "Failed to create(read) session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
  1910. RETURN_FALSE;
  1911. }
  1912. if (data) {
  1913. zend_string_release_ex(data, 0);
  1914. }
  1915. if (PS(use_cookies)) {
  1916. PS(send_cookie) = 1;
  1917. }
  1918. if (php_session_reset_id() == FAILURE) {
  1919. RETURN_FALSE;
  1920. }
  1921. RETURN_TRUE;
  1922. }
  1923. /* }}} */
  1924. /* {{{ proto string session_create_id([string prefix])
  1925. Generate new session ID. Intended for user save handlers. */
  1926. /* This is not used yet */
  1927. static PHP_FUNCTION(session_create_id)
  1928. {
  1929. zend_string *prefix = NULL, *new_id;
  1930. smart_str id = {0};
  1931. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S", &prefix) == FAILURE) {
  1932. return;
  1933. }
  1934. if (prefix && ZSTR_LEN(prefix)) {
  1935. if (php_session_valid_key(ZSTR_VAL(prefix)) == FAILURE) {
  1936. /* E_ERROR raised for security reason. */
  1937. php_error_docref(NULL, E_WARNING, "Prefix cannot contain special characters. Only aphanumeric, ',', '-' are allowed");
  1938. RETURN_FALSE;
  1939. } else {
  1940. smart_str_append(&id, prefix);
  1941. }
  1942. }
  1943. if (!PS(in_save_handler) && PS(session_status) == php_session_active) {
  1944. int limit = 3;
  1945. while (limit--) {
  1946. new_id = PS(mod)->s_create_sid(&PS(mod_data));
  1947. if (!PS(mod)->s_validate_sid) {
  1948. break;
  1949. } else {
  1950. /* Detect collision and retry */
  1951. if (PS(mod)->s_validate_sid(&PS(mod_data), new_id) == SUCCESS) {
  1952. zend_string_release_ex(new_id, 0);
  1953. new_id = NULL;
  1954. continue;
  1955. }
  1956. break;
  1957. }
  1958. }
  1959. } else {
  1960. new_id = php_session_create_id(NULL);
  1961. }
  1962. if (new_id) {
  1963. smart_str_append(&id, new_id);
  1964. zend_string_release_ex(new_id, 0);
  1965. } else {
  1966. smart_str_free(&id);
  1967. php_error_docref(NULL, E_WARNING, "Failed to create new ID");
  1968. RETURN_FALSE;
  1969. }
  1970. smart_str_0(&id);
  1971. RETVAL_NEW_STR(id.s);
  1972. }
  1973. /* }}} */
  1974. /* {{{ proto string session_cache_limiter([string new_cache_limiter])
  1975. Return the current cache limiter. If new_cache_limited is given, the current cache_limiter is replaced with new_cache_limiter */
  1976. static PHP_FUNCTION(session_cache_limiter)
  1977. {
  1978. zend_string *limiter = NULL;
  1979. zend_string *ini_name;
  1980. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S", &limiter) == FAILURE) {
  1981. return;
  1982. }
  1983. if (limiter && PS(session_status) == php_session_active) {
  1984. php_error_docref(NULL, E_WARNING, "Cannot change cache limiter when session is active");
  1985. RETURN_FALSE;
  1986. }
  1987. if (limiter && SG(headers_sent)) {
  1988. php_error_docref(NULL, E_WARNING, "Cannot change cache limiter when headers already sent");
  1989. RETURN_FALSE;
  1990. }
  1991. RETVAL_STRING(PS(cache_limiter));
  1992. if (limiter) {
  1993. ini_name = zend_string_init("session.cache_limiter", sizeof("session.cache_limiter") - 1, 0);
  1994. zend_alter_ini_entry(ini_name, limiter, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
  1995. zend_string_release_ex(ini_name, 0);
  1996. }
  1997. }
  1998. /* }}} */
  1999. /* {{{ proto int session_cache_expire([int new_cache_expire])
  2000. Return the current cache expire. If new_cache_expire is given, the current cache_expire is replaced with new_cache_expire */
  2001. static PHP_FUNCTION(session_cache_expire)
  2002. {
  2003. zval *expires = NULL;
  2004. zend_string *ini_name;
  2005. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &expires) == FAILURE) {
  2006. return;
  2007. }
  2008. if (expires && PS(session_status) == php_session_active) {
  2009. php_error_docref(NULL, E_WARNING, "Cannot change cache expire when session is active");
  2010. RETURN_LONG(PS(cache_expire));
  2011. }
  2012. if (expires && SG(headers_sent)) {
  2013. php_error_docref(NULL, E_WARNING, "Cannot change cache expire when headers already sent");
  2014. RETURN_FALSE;
  2015. }
  2016. RETVAL_LONG(PS(cache_expire));
  2017. if (expires) {
  2018. convert_to_string_ex(expires);
  2019. ini_name = zend_string_init("session.cache_expire", sizeof("session.cache_expire") - 1, 0);
  2020. zend_alter_ini_entry(ini_name, Z_STR_P(expires), ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
  2021. zend_string_release_ex(ini_name, 0);
  2022. }
  2023. }
  2024. /* }}} */
  2025. /* {{{ proto string session_encode(void)
  2026. Serializes the current setup and returns the serialized representation */
  2027. static PHP_FUNCTION(session_encode)
  2028. {
  2029. zend_string *enc;
  2030. if (zend_parse_parameters_none() == FAILURE) {
  2031. return;
  2032. }
  2033. enc = php_session_encode();
  2034. if (enc == NULL) {
  2035. RETURN_FALSE;
  2036. }
  2037. RETURN_STR(enc);
  2038. }
  2039. /* }}} */
  2040. /* {{{ proto bool session_decode(string data)
  2041. Deserializes data and reinitializes the variables */
  2042. static PHP_FUNCTION(session_decode)
  2043. {
  2044. zend_string *str = NULL;
  2045. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &str) == FAILURE) {
  2046. return;
  2047. }
  2048. if (PS(session_status) != php_session_active) {
  2049. php_error_docref(NULL, E_WARNING, "Session is not active. You cannot decode session data");
  2050. RETURN_FALSE;
  2051. }
  2052. if (php_session_decode(str) == FAILURE) {
  2053. RETURN_FALSE;
  2054. }
  2055. RETURN_TRUE;
  2056. }
  2057. /* }}} */
  2058. static int php_session_start_set_ini(zend_string *varname, zend_string *new_value) {
  2059. int ret;
  2060. smart_str buf ={0};
  2061. smart_str_appends(&buf, "session");
  2062. smart_str_appendc(&buf, '.');
  2063. smart_str_append(&buf, varname);
  2064. smart_str_0(&buf);
  2065. ret = zend_alter_ini_entry_ex(buf.s, new_value, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
  2066. smart_str_free(&buf);
  2067. return ret;
  2068. }
  2069. /* {{{ proto bool session_start([array options])
  2070. + Begin session */
  2071. static PHP_FUNCTION(session_start)
  2072. {
  2073. zval *options = NULL;
  2074. zval *value;
  2075. zend_ulong num_idx;
  2076. zend_string *str_idx;
  2077. zend_long read_and_close = 0;
  2078. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|a", &options) == FAILURE) {
  2079. RETURN_FALSE;
  2080. }
  2081. if (PS(session_status) == php_session_active) {
  2082. php_error_docref(NULL, E_NOTICE, "A session had already been started - ignoring");
  2083. RETURN_TRUE;
  2084. }
  2085. /*
  2086. * TODO: To prevent unusable session with trans sid, actual output started status is
  2087. * required. i.e. There shouldn't be any outputs in output buffer, otherwise session
  2088. * module is unable to rewrite output.
  2089. */
  2090. if (PS(use_cookies) && SG(headers_sent)) {
  2091. php_error_docref(NULL, E_WARNING, "Cannot start session when headers already sent");
  2092. RETURN_FALSE;
  2093. }
  2094. /* set options */
  2095. if (options) {
  2096. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(options), num_idx, str_idx, value) {
  2097. if (str_idx) {
  2098. switch(Z_TYPE_P(value)) {
  2099. case IS_STRING:
  2100. case IS_TRUE:
  2101. case IS_FALSE:
  2102. case IS_LONG:
  2103. if (zend_string_equals_literal(str_idx, "read_and_close")) {
  2104. read_and_close = zval_get_long(value);
  2105. } else {
  2106. zend_string *tmp_val;
  2107. zend_string *val = zval_get_tmp_string(value, &tmp_val);
  2108. if (php_session_start_set_ini(str_idx, val) == FAILURE) {
  2109. php_error_docref(NULL, E_WARNING, "Setting option '%s' failed", ZSTR_VAL(str_idx));
  2110. }
  2111. zend_tmp_string_release(tmp_val);
  2112. }
  2113. break;
  2114. default:
  2115. php_error_docref(NULL, E_WARNING, "Option(%s) value must be string, boolean or long", ZSTR_VAL(str_idx));
  2116. break;
  2117. }
  2118. }
  2119. (void) num_idx;
  2120. } ZEND_HASH_FOREACH_END();
  2121. }
  2122. php_session_start();
  2123. if (PS(session_status) != php_session_active) {
  2124. IF_SESSION_VARS() {
  2125. zval *sess_var = Z_REFVAL(PS(http_session_vars));
  2126. SEPARATE_ARRAY(sess_var);
  2127. /* Clean $_SESSION. */
  2128. zend_hash_clean(Z_ARRVAL_P(sess_var));
  2129. }
  2130. RETURN_FALSE;
  2131. }
  2132. if (read_and_close) {
  2133. php_session_flush(0);
  2134. }
  2135. RETURN_TRUE;
  2136. }
  2137. /* }}} */
  2138. /* {{{ proto bool session_destroy(void)
  2139. Destroy the current session and all data associated with it */
  2140. static PHP_FUNCTION(session_destroy)
  2141. {
  2142. if (zend_parse_parameters_none() == FAILURE) {
  2143. return;
  2144. }
  2145. RETURN_BOOL(php_session_destroy() == SUCCESS);
  2146. }
  2147. /* }}} */
  2148. /* {{{ proto bool session_unset(void)
  2149. Unset all registered variables */
  2150. static PHP_FUNCTION(session_unset)
  2151. {
  2152. if (zend_parse_parameters_none() == FAILURE) {
  2153. return;
  2154. }
  2155. if (PS(session_status) != php_session_active) {
  2156. RETURN_FALSE;
  2157. }
  2158. IF_SESSION_VARS() {
  2159. zval *sess_var = Z_REFVAL(PS(http_session_vars));
  2160. SEPARATE_ARRAY(sess_var);
  2161. /* Clean $_SESSION. */
  2162. zend_hash_clean(Z_ARRVAL_P(sess_var));
  2163. }
  2164. RETURN_TRUE;
  2165. }
  2166. /* }}} */
  2167. /* {{{ proto int session_gc(void)
  2168. Perform GC and return number of deleted sessions */
  2169. static PHP_FUNCTION(session_gc)
  2170. {
  2171. zend_long num;
  2172. if (zend_parse_parameters_none() == FAILURE) {
  2173. return;
  2174. }
  2175. if (PS(session_status) != php_session_active) {
  2176. php_error_docref(NULL, E_WARNING, "Session is not active");
  2177. RETURN_FALSE;
  2178. }
  2179. num = php_session_gc(1);
  2180. if (num < 0) {
  2181. RETURN_FALSE;
  2182. }
  2183. RETURN_LONG(num);
  2184. }
  2185. /* }}} */
  2186. /* {{{ proto bool session_write_close(void)
  2187. Write session data and end session */
  2188. static PHP_FUNCTION(session_write_close)
  2189. {
  2190. if (zend_parse_parameters_none() == FAILURE) {
  2191. return;
  2192. }
  2193. if (PS(session_status) != php_session_active) {
  2194. RETURN_FALSE;
  2195. }
  2196. php_session_flush(1);
  2197. RETURN_TRUE;
  2198. }
  2199. /* }}} */
  2200. /* {{{ proto bool session_abort(void)
  2201. Abort session and end session. Session data will not be written */
  2202. static PHP_FUNCTION(session_abort)
  2203. {
  2204. if (zend_parse_parameters_none() == FAILURE) {
  2205. return;
  2206. }
  2207. if (PS(session_status) != php_session_active) {
  2208. RETURN_FALSE;
  2209. }
  2210. php_session_abort();
  2211. RETURN_TRUE;
  2212. }
  2213. /* }}} */
  2214. /* {{{ proto bool session_reset(void)
  2215. Reset session data from saved session data */
  2216. static PHP_FUNCTION(session_reset)
  2217. {
  2218. if (zend_parse_parameters_none() == FAILURE) {
  2219. return;
  2220. }
  2221. if (PS(session_status) != php_session_active) {
  2222. RETURN_FALSE;
  2223. }
  2224. php_session_reset();
  2225. RETURN_TRUE;
  2226. }
  2227. /* }}} */
  2228. /* {{{ proto int session_status(void)
  2229. Returns the current session status */
  2230. static PHP_FUNCTION(session_status)
  2231. {
  2232. if (zend_parse_parameters_none() == FAILURE) {
  2233. return;
  2234. }
  2235. RETURN_LONG(PS(session_status));
  2236. }
  2237. /* }}} */
  2238. /* {{{ proto void session_register_shutdown(void)
  2239. Registers session_write_close() as a shutdown function */
  2240. static PHP_FUNCTION(session_register_shutdown)
  2241. {
  2242. php_shutdown_function_entry shutdown_function_entry;
  2243. /* This function is registered itself as a shutdown function by
  2244. * session_set_save_handler($obj). The reason we now register another
  2245. * shutdown function is in case the user registered their own shutdown
  2246. * function after calling session_set_save_handler(), which expects
  2247. * the session still to be available.
  2248. */
  2249. shutdown_function_entry.arg_count = 1;
  2250. shutdown_function_entry.arguments = (zval *) safe_emalloc(sizeof(zval), 1, 0);
  2251. ZVAL_STRING(&shutdown_function_entry.arguments[0], "session_write_close");
  2252. if (!append_user_shutdown_function(shutdown_function_entry)) {
  2253. zval_ptr_dtor(&shutdown_function_entry.arguments[0]);
  2254. efree(shutdown_function_entry.arguments);
  2255. /* Unable to register shutdown function, presumably because of lack
  2256. * of memory, so flush the session now. It would be done in rshutdown
  2257. * anyway but the handler will have had it's dtor called by then.
  2258. * If the user does have a later shutdown function which needs the
  2259. * session then tough luck.
  2260. */
  2261. php_session_flush(1);
  2262. php_error_docref(NULL, E_WARNING, "Unable to register session flush function");
  2263. }
  2264. }
  2265. /* }}} */
  2266. /* {{{ arginfo */
  2267. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_name, 0, 0, 0)
  2268. ZEND_ARG_INFO(0, name)
  2269. ZEND_END_ARG_INFO()
  2270. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_module_name, 0, 0, 0)
  2271. ZEND_ARG_INFO(0, module)
  2272. ZEND_END_ARG_INFO()
  2273. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_save_path, 0, 0, 0)
  2274. ZEND_ARG_INFO(0, path)
  2275. ZEND_END_ARG_INFO()
  2276. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_id, 0, 0, 0)
  2277. ZEND_ARG_INFO(0, id)
  2278. ZEND_END_ARG_INFO()
  2279. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_create_id, 0, 0, 0)
  2280. ZEND_ARG_INFO(0, prefix)
  2281. ZEND_END_ARG_INFO()
  2282. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_regenerate_id, 0, 0, 0)
  2283. ZEND_ARG_INFO(0, delete_old_session)
  2284. ZEND_END_ARG_INFO()
  2285. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_decode, 0, 0, 1)
  2286. ZEND_ARG_INFO(0, data)
  2287. ZEND_END_ARG_INFO()
  2288. ZEND_BEGIN_ARG_INFO(arginfo_session_void, 0)
  2289. ZEND_END_ARG_INFO()
  2290. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_save_handler, 0, 0, 1)
  2291. ZEND_ARG_INFO(0, open)
  2292. ZEND_ARG_INFO(0, close)
  2293. ZEND_ARG_INFO(0, read)
  2294. ZEND_ARG_INFO(0, write)
  2295. ZEND_ARG_INFO(0, destroy)
  2296. ZEND_ARG_INFO(0, gc)
  2297. ZEND_ARG_INFO(0, create_sid)
  2298. ZEND_ARG_INFO(0, validate_sid)
  2299. ZEND_ARG_INFO(0, update_timestamp)
  2300. ZEND_END_ARG_INFO()
  2301. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_limiter, 0, 0, 0)
  2302. ZEND_ARG_INFO(0, cache_limiter)
  2303. ZEND_END_ARG_INFO()
  2304. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_expire, 0, 0, 0)
  2305. ZEND_ARG_INFO(0, new_cache_expire)
  2306. ZEND_END_ARG_INFO()
  2307. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_cookie_params, 0, 0, 1)
  2308. ZEND_ARG_INFO(0, lifetime_or_options)
  2309. ZEND_ARG_INFO(0, path)
  2310. ZEND_ARG_INFO(0, domain)
  2311. ZEND_ARG_INFO(0, secure)
  2312. ZEND_ARG_INFO(0, httponly)
  2313. ZEND_END_ARG_INFO()
  2314. ZEND_BEGIN_ARG_INFO(arginfo_session_class_open, 0)
  2315. ZEND_ARG_INFO(0, save_path)
  2316. ZEND_ARG_INFO(0, session_name)
  2317. ZEND_END_ARG_INFO()
  2318. ZEND_BEGIN_ARG_INFO(arginfo_session_class_close, 0)
  2319. ZEND_END_ARG_INFO()
  2320. ZEND_BEGIN_ARG_INFO(arginfo_session_class_read, 0)
  2321. ZEND_ARG_INFO(0, key)
  2322. ZEND_END_ARG_INFO()
  2323. ZEND_BEGIN_ARG_INFO(arginfo_session_class_write, 0)
  2324. ZEND_ARG_INFO(0, key)
  2325. ZEND_ARG_INFO(0, val)
  2326. ZEND_END_ARG_INFO()
  2327. ZEND_BEGIN_ARG_INFO(arginfo_session_class_destroy, 0)
  2328. ZEND_ARG_INFO(0, key)
  2329. ZEND_END_ARG_INFO()
  2330. ZEND_BEGIN_ARG_INFO(arginfo_session_class_gc, 0)
  2331. ZEND_ARG_INFO(0, maxlifetime)
  2332. ZEND_END_ARG_INFO()
  2333. ZEND_BEGIN_ARG_INFO(arginfo_session_class_create_sid, 0)
  2334. ZEND_END_ARG_INFO()
  2335. ZEND_BEGIN_ARG_INFO(arginfo_session_class_validateId, 0)
  2336. ZEND_ARG_INFO(0, key)
  2337. ZEND_END_ARG_INFO()
  2338. ZEND_BEGIN_ARG_INFO(arginfo_session_class_updateTimestamp, 0)
  2339. ZEND_ARG_INFO(0, key)
  2340. ZEND_ARG_INFO(0, val)
  2341. ZEND_END_ARG_INFO()
  2342. ZEND_BEGIN_ARG_INFO_EX(arginfo_session_start, 0, 0, 0)
  2343. ZEND_ARG_INFO(0, options) /* array */
  2344. ZEND_END_ARG_INFO()
  2345. /* }}} */
  2346. /* {{{ session_functions[]
  2347. */
  2348. static const zend_function_entry session_functions[] = {
  2349. PHP_FE(session_name, arginfo_session_name)
  2350. PHP_FE(session_module_name, arginfo_session_module_name)
  2351. PHP_FE(session_save_path, arginfo_session_save_path)
  2352. PHP_FE(session_id, arginfo_session_id)
  2353. PHP_FE(session_create_id, arginfo_session_create_id)
  2354. PHP_FE(session_regenerate_id, arginfo_session_regenerate_id)
  2355. PHP_FE(session_decode, arginfo_session_decode)
  2356. PHP_FE(session_encode, arginfo_session_void)
  2357. PHP_FE(session_start, arginfo_session_start)
  2358. PHP_FE(session_destroy, arginfo_session_void)
  2359. PHP_FE(session_unset, arginfo_session_void)
  2360. PHP_FE(session_gc, arginfo_session_void)
  2361. PHP_FE(session_set_save_handler, arginfo_session_set_save_handler)
  2362. PHP_FE(session_cache_limiter, arginfo_session_cache_limiter)
  2363. PHP_FE(session_cache_expire, arginfo_session_cache_expire)
  2364. PHP_FE(session_set_cookie_params, arginfo_session_set_cookie_params)
  2365. PHP_FE(session_get_cookie_params, arginfo_session_void)
  2366. PHP_FE(session_write_close, arginfo_session_void)
  2367. PHP_FE(session_abort, arginfo_session_void)
  2368. PHP_FE(session_reset, arginfo_session_void)
  2369. PHP_FE(session_status, arginfo_session_void)
  2370. PHP_FE(session_register_shutdown, arginfo_session_void)
  2371. PHP_FALIAS(session_commit, session_write_close, arginfo_session_void)
  2372. PHP_FE_END
  2373. };
  2374. /* }}} */
  2375. /* {{{ SessionHandlerInterface functions[]
  2376. */
  2377. static const zend_function_entry php_session_iface_functions[] = {
  2378. PHP_ABSTRACT_ME(SessionHandlerInterface, open, arginfo_session_class_open)
  2379. PHP_ABSTRACT_ME(SessionHandlerInterface, close, arginfo_session_class_close)
  2380. PHP_ABSTRACT_ME(SessionHandlerInterface, read, arginfo_session_class_read)
  2381. PHP_ABSTRACT_ME(SessionHandlerInterface, write, arginfo_session_class_write)
  2382. PHP_ABSTRACT_ME(SessionHandlerInterface, destroy, arginfo_session_class_destroy)
  2383. PHP_ABSTRACT_ME(SessionHandlerInterface, gc, arginfo_session_class_gc)
  2384. PHP_FE_END
  2385. };
  2386. /* }}} */
  2387. /* {{{ SessionIdInterface functions[]
  2388. */
  2389. static const zend_function_entry php_session_id_iface_functions[] = {
  2390. PHP_ABSTRACT_ME(SessionIdInterface, create_sid, arginfo_session_class_create_sid)
  2391. PHP_FE_END
  2392. };
  2393. /* }}} */
  2394. /* {{{ SessionUpdateTimestampHandler functions[]
  2395. */
  2396. static const zend_function_entry php_session_update_timestamp_iface_functions[] = {
  2397. PHP_ABSTRACT_ME(SessionUpdateTimestampHandlerInterface, validateId, arginfo_session_class_validateId)
  2398. PHP_ABSTRACT_ME(SessionUpdateTimestampHandlerInterface, updateTimestamp, arginfo_session_class_updateTimestamp)
  2399. PHP_FE_END
  2400. };
  2401. /* }}} */
  2402. /* {{{ SessionHandler functions[]
  2403. */
  2404. static const zend_function_entry php_session_class_functions[] = {
  2405. PHP_ME(SessionHandler, open, arginfo_session_class_open, ZEND_ACC_PUBLIC)
  2406. PHP_ME(SessionHandler, close, arginfo_session_class_close, ZEND_ACC_PUBLIC)
  2407. PHP_ME(SessionHandler, read, arginfo_session_class_read, ZEND_ACC_PUBLIC)
  2408. PHP_ME(SessionHandler, write, arginfo_session_class_write, ZEND_ACC_PUBLIC)
  2409. PHP_ME(SessionHandler, destroy, arginfo_session_class_destroy, ZEND_ACC_PUBLIC)
  2410. PHP_ME(SessionHandler, gc, arginfo_session_class_gc, ZEND_ACC_PUBLIC)
  2411. PHP_ME(SessionHandler, create_sid, arginfo_session_class_create_sid, ZEND_ACC_PUBLIC)
  2412. PHP_FE_END
  2413. };
  2414. /* }}} */
  2415. /* ********************************
  2416. * Module Setup and Destruction *
  2417. ******************************** */
  2418. static int php_rinit_session(zend_bool auto_start) /* {{{ */
  2419. {
  2420. php_rinit_session_globals();
  2421. if (PS(mod) == NULL) {
  2422. char *value;
  2423. value = zend_ini_string("session.save_handler", sizeof("session.save_handler") - 1, 0);
  2424. if (value) {
  2425. PS(mod) = _php_find_ps_module(value);
  2426. }
  2427. }
  2428. if (PS(serializer) == NULL) {
  2429. char *value;
  2430. value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler") - 1, 0);
  2431. if (value) {
  2432. PS(serializer) = _php_find_ps_serializer(value);
  2433. }
  2434. }
  2435. if (PS(mod) == NULL || PS(serializer) == NULL) {
  2436. /* current status is unusable */
  2437. PS(session_status) = php_session_disabled;
  2438. return SUCCESS;
  2439. }
  2440. if (auto_start) {
  2441. php_session_start();
  2442. }
  2443. return SUCCESS;
  2444. } /* }}} */
  2445. static PHP_RINIT_FUNCTION(session) /* {{{ */
  2446. {
  2447. return php_rinit_session(PS(auto_start));
  2448. }
  2449. /* }}} */
  2450. static PHP_RSHUTDOWN_FUNCTION(session) /* {{{ */
  2451. {
  2452. int i;
  2453. if (PS(session_status) == php_session_active) {
  2454. zend_try {
  2455. php_session_flush(1);
  2456. } zend_end_try();
  2457. }
  2458. php_rshutdown_session_globals();
  2459. /* this should NOT be done in php_rshutdown_session_globals() */
  2460. for (i = 0; i < PS_NUM_APIS; i++) {
  2461. if (!Z_ISUNDEF(PS(mod_user_names).names[i])) {
  2462. zval_ptr_dtor(&PS(mod_user_names).names[i]);
  2463. ZVAL_UNDEF(&PS(mod_user_names).names[i]);
  2464. }
  2465. }
  2466. return SUCCESS;
  2467. }
  2468. /* }}} */
  2469. static PHP_GINIT_FUNCTION(ps) /* {{{ */
  2470. {
  2471. int i;
  2472. #if defined(COMPILE_DL_SESSION) && defined(ZTS)
  2473. ZEND_TSRMLS_CACHE_UPDATE();
  2474. #endif
  2475. ps_globals->save_path = NULL;
  2476. ps_globals->session_name = NULL;
  2477. ps_globals->id = NULL;
  2478. ps_globals->mod = NULL;
  2479. ps_globals->serializer = NULL;
  2480. ps_globals->mod_data = NULL;
  2481. ps_globals->session_status = php_session_none;
  2482. ps_globals->default_mod = NULL;
  2483. ps_globals->mod_user_implemented = 0;
  2484. ps_globals->mod_user_is_open = 0;
  2485. ps_globals->session_vars = NULL;
  2486. ps_globals->set_handler = 0;
  2487. for (i = 0; i < PS_NUM_APIS; i++) {
  2488. ZVAL_UNDEF(&ps_globals->mod_user_names.names[i]);
  2489. }
  2490. ZVAL_UNDEF(&ps_globals->http_session_vars);
  2491. }
  2492. /* }}} */
  2493. static PHP_MINIT_FUNCTION(session) /* {{{ */
  2494. {
  2495. zend_class_entry ce;
  2496. zend_register_auto_global(zend_string_init_interned("_SESSION", sizeof("_SESSION") - 1, 1), 0, NULL);
  2497. my_module_number = module_number;
  2498. PS(module_number) = module_number;
  2499. PS(session_status) = php_session_none;
  2500. REGISTER_INI_ENTRIES();
  2501. #ifdef HAVE_LIBMM
  2502. PHP_MINIT(ps_mm) (INIT_FUNC_ARGS_PASSTHRU);
  2503. #endif
  2504. php_session_rfc1867_orig_callback = php_rfc1867_callback;
  2505. php_rfc1867_callback = php_session_rfc1867_callback;
  2506. /* Register interfaces */
  2507. INIT_CLASS_ENTRY(ce, PS_IFACE_NAME, php_session_iface_functions);
  2508. php_session_iface_entry = zend_register_internal_class(&ce);
  2509. php_session_iface_entry->ce_flags |= ZEND_ACC_INTERFACE;
  2510. INIT_CLASS_ENTRY(ce, PS_SID_IFACE_NAME, php_session_id_iface_functions);
  2511. php_session_id_iface_entry = zend_register_internal_class(&ce);
  2512. php_session_id_iface_entry->ce_flags |= ZEND_ACC_INTERFACE;
  2513. INIT_CLASS_ENTRY(ce, PS_UPDATE_TIMESTAMP_IFACE_NAME, php_session_update_timestamp_iface_functions);
  2514. php_session_update_timestamp_iface_entry = zend_register_internal_class(&ce);
  2515. php_session_update_timestamp_iface_entry->ce_flags |= ZEND_ACC_INTERFACE;
  2516. /* Register base class */
  2517. INIT_CLASS_ENTRY(ce, PS_CLASS_NAME, php_session_class_functions);
  2518. php_session_class_entry = zend_register_internal_class(&ce);
  2519. zend_class_implements(php_session_class_entry, 1, php_session_iface_entry);
  2520. zend_class_implements(php_session_class_entry, 1, php_session_id_iface_entry);
  2521. REGISTER_LONG_CONSTANT("PHP_SESSION_DISABLED", php_session_disabled, CONST_CS | CONST_PERSISTENT);
  2522. REGISTER_LONG_CONSTANT("PHP_SESSION_NONE", php_session_none, CONST_CS | CONST_PERSISTENT);
  2523. REGISTER_LONG_CONSTANT("PHP_SESSION_ACTIVE", php_session_active, CONST_CS | CONST_PERSISTENT);
  2524. return SUCCESS;
  2525. }
  2526. /* }}} */
  2527. static PHP_MSHUTDOWN_FUNCTION(session) /* {{{ */
  2528. {
  2529. UNREGISTER_INI_ENTRIES();
  2530. #ifdef HAVE_LIBMM
  2531. PHP_MSHUTDOWN(ps_mm) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
  2532. #endif
  2533. /* reset rfc1867 callbacks */
  2534. php_session_rfc1867_orig_callback = NULL;
  2535. if (php_rfc1867_callback == php_session_rfc1867_callback) {
  2536. php_rfc1867_callback = NULL;
  2537. }
  2538. ps_serializers[PREDEFINED_SERIALIZERS].name = NULL;
  2539. memset(&ps_modules[PREDEFINED_MODULES], 0, (MAX_MODULES-PREDEFINED_MODULES)*sizeof(ps_module *));
  2540. return SUCCESS;
  2541. }
  2542. /* }}} */
  2543. static PHP_MINFO_FUNCTION(session) /* {{{ */
  2544. {
  2545. const ps_module **mod;
  2546. ps_serializer *ser;
  2547. smart_str save_handlers = {0};
  2548. smart_str ser_handlers = {0};
  2549. int i;
  2550. /* Get save handlers */
  2551. for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) {
  2552. if (*mod && (*mod)->s_name) {
  2553. smart_str_appends(&save_handlers, (*mod)->s_name);
  2554. smart_str_appendc(&save_handlers, ' ');
  2555. }
  2556. }
  2557. /* Get serializer handlers */
  2558. for (i = 0, ser = ps_serializers; i < MAX_SERIALIZERS; i++, ser++) {
  2559. if (ser && ser->name) {
  2560. smart_str_appends(&ser_handlers, ser->name);
  2561. smart_str_appendc(&ser_handlers, ' ');
  2562. }
  2563. }
  2564. php_info_print_table_start();
  2565. php_info_print_table_row(2, "Session Support", "enabled" );
  2566. if (save_handlers.s) {
  2567. smart_str_0(&save_handlers);
  2568. php_info_print_table_row(2, "Registered save handlers", ZSTR_VAL(save_handlers.s));
  2569. smart_str_free(&save_handlers);
  2570. } else {
  2571. php_info_print_table_row(2, "Registered save handlers", "none");
  2572. }
  2573. if (ser_handlers.s) {
  2574. smart_str_0(&ser_handlers);
  2575. php_info_print_table_row(2, "Registered serializer handlers", ZSTR_VAL(ser_handlers.s));
  2576. smart_str_free(&ser_handlers);
  2577. } else {
  2578. php_info_print_table_row(2, "Registered serializer handlers", "none");
  2579. }
  2580. php_info_print_table_end();
  2581. DISPLAY_INI_ENTRIES();
  2582. }
  2583. /* }}} */
  2584. static const zend_module_dep session_deps[] = { /* {{{ */
  2585. ZEND_MOD_OPTIONAL("hash")
  2586. ZEND_MOD_REQUIRED("spl")
  2587. ZEND_MOD_END
  2588. };
  2589. /* }}} */
  2590. /* ************************
  2591. * Upload hook handling *
  2592. ************************ */
  2593. static zend_bool early_find_sid_in(zval *dest, int where, php_session_rfc1867_progress *progress) /* {{{ */
  2594. {
  2595. zval *ppid;
  2596. if (Z_ISUNDEF(PG(http_globals)[where])) {
  2597. return 0;
  2598. }
  2599. if ((ppid = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[where]), PS(session_name), progress->sname_len))
  2600. && Z_TYPE_P(ppid) == IS_STRING) {
  2601. zval_ptr_dtor(dest);
  2602. ZVAL_COPY_DEREF(dest, ppid);
  2603. return 1;
  2604. }
  2605. return 0;
  2606. } /* }}} */
  2607. static void php_session_rfc1867_early_find_sid(php_session_rfc1867_progress *progress) /* {{{ */
  2608. {
  2609. if (PS(use_cookies)) {
  2610. sapi_module.treat_data(PARSE_COOKIE, NULL, NULL);
  2611. if (early_find_sid_in(&progress->sid, TRACK_VARS_COOKIE, progress)) {
  2612. progress->apply_trans_sid = 0;
  2613. return;
  2614. }
  2615. }
  2616. if (PS(use_only_cookies)) {
  2617. return;
  2618. }
  2619. sapi_module.treat_data(PARSE_GET, NULL, NULL);
  2620. early_find_sid_in(&progress->sid, TRACK_VARS_GET, progress);
  2621. } /* }}} */
  2622. static zend_bool php_check_cancel_upload(php_session_rfc1867_progress *progress) /* {{{ */
  2623. {
  2624. zval *progress_ary, *cancel_upload;
  2625. if ((progress_ary = zend_symtable_find(Z_ARRVAL_P(Z_REFVAL(PS(http_session_vars))), progress->key.s)) == NULL) {
  2626. return 0;
  2627. }
  2628. if (Z_TYPE_P(progress_ary) != IS_ARRAY) {
  2629. return 0;
  2630. }
  2631. if ((cancel_upload = zend_hash_str_find(Z_ARRVAL_P(progress_ary), "cancel_upload", sizeof("cancel_upload") - 1)) == NULL) {
  2632. return 0;
  2633. }
  2634. return Z_TYPE_P(cancel_upload) == IS_TRUE;
  2635. } /* }}} */
  2636. static void php_session_rfc1867_update(php_session_rfc1867_progress *progress, int force_update) /* {{{ */
  2637. {
  2638. if (!force_update) {
  2639. if (Z_LVAL_P(progress->post_bytes_processed) < progress->next_update) {
  2640. return;
  2641. }
  2642. #ifdef HAVE_GETTIMEOFDAY
  2643. if (PS(rfc1867_min_freq) > 0.0) {
  2644. struct timeval tv = {0};
  2645. double dtv;
  2646. gettimeofday(&tv, NULL);
  2647. dtv = (double) tv.tv_sec + tv.tv_usec / 1000000.0;
  2648. if (dtv < progress->next_update_time) {
  2649. return;
  2650. }
  2651. progress->next_update_time = dtv + PS(rfc1867_min_freq);
  2652. }
  2653. #endif
  2654. progress->next_update = Z_LVAL_P(progress->post_bytes_processed) + progress->update_step;
  2655. }
  2656. php_session_initialize();
  2657. PS(session_status) = php_session_active;
  2658. IF_SESSION_VARS() {
  2659. zval *sess_var = Z_REFVAL(PS(http_session_vars));
  2660. SEPARATE_ARRAY(sess_var);
  2661. progress->cancel_upload |= php_check_cancel_upload(progress);
  2662. Z_TRY_ADDREF(progress->data);
  2663. zend_hash_update(Z_ARRVAL_P(sess_var), progress->key.s, &progress->data);
  2664. }
  2665. php_session_flush(1);
  2666. } /* }}} */
  2667. static void php_session_rfc1867_cleanup(php_session_rfc1867_progress *progress) /* {{{ */
  2668. {
  2669. php_session_initialize();
  2670. PS(session_status) = php_session_active;
  2671. IF_SESSION_VARS() {
  2672. zval *sess_var = Z_REFVAL(PS(http_session_vars));
  2673. SEPARATE_ARRAY(sess_var);
  2674. zend_hash_del(Z_ARRVAL_P(sess_var), progress->key.s);
  2675. }
  2676. php_session_flush(1);
  2677. } /* }}} */
  2678. static int php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra) /* {{{ */
  2679. {
  2680. php_session_rfc1867_progress *progress;
  2681. int retval = SUCCESS;
  2682. if (php_session_rfc1867_orig_callback) {
  2683. retval = php_session_rfc1867_orig_callback(event, event_data, extra);
  2684. }
  2685. if (!PS(rfc1867_enabled)) {
  2686. return retval;
  2687. }
  2688. progress = PS(rfc1867_progress);
  2689. switch(event) {
  2690. case MULTIPART_EVENT_START: {
  2691. multipart_event_start *data = (multipart_event_start *) event_data;
  2692. progress = ecalloc(1, sizeof(php_session_rfc1867_progress));
  2693. progress->content_length = data->content_length;
  2694. progress->sname_len = strlen(PS(session_name));
  2695. PS(rfc1867_progress) = progress;
  2696. }
  2697. break;
  2698. case MULTIPART_EVENT_FORMDATA: {
  2699. multipart_event_formdata *data = (multipart_event_formdata *) event_data;
  2700. size_t value_len;
  2701. if (Z_TYPE(progress->sid) && progress->key.s) {
  2702. break;
  2703. }
  2704. /* orig callback may have modified *data->newlength */
  2705. if (data->newlength) {
  2706. value_len = *data->newlength;
  2707. } else {
  2708. value_len = data->length;
  2709. }
  2710. if (data->name && data->value && value_len) {
  2711. size_t name_len = strlen(data->name);
  2712. if (name_len == progress->sname_len && memcmp(data->name, PS(session_name), name_len) == 0) {
  2713. zval_ptr_dtor(&progress->sid);
  2714. ZVAL_STRINGL(&progress->sid, (*data->value), value_len);
  2715. } else if (name_len == strlen(PS(rfc1867_name)) && memcmp(data->name, PS(rfc1867_name), name_len + 1) == 0) {
  2716. smart_str_free(&progress->key);
  2717. smart_str_appends(&progress->key, PS(rfc1867_prefix));
  2718. smart_str_appendl(&progress->key, *data->value, value_len);
  2719. smart_str_0(&progress->key);
  2720. progress->apply_trans_sid = APPLY_TRANS_SID;
  2721. php_session_rfc1867_early_find_sid(progress);
  2722. }
  2723. }
  2724. }
  2725. break;
  2726. case MULTIPART_EVENT_FILE_START: {
  2727. multipart_event_file_start *data = (multipart_event_file_start *) event_data;
  2728. /* Do nothing when $_POST["PHP_SESSION_UPLOAD_PROGRESS"] is not set
  2729. * or when we have no session id */
  2730. if (!Z_TYPE(progress->sid) || !progress->key.s) {
  2731. break;
  2732. }
  2733. /* First FILE_START event, initializing data */
  2734. if (Z_ISUNDEF(progress->data)) {
  2735. if (PS(rfc1867_freq) >= 0) {
  2736. progress->update_step = PS(rfc1867_freq);
  2737. } else if (PS(rfc1867_freq) < 0) { /* % of total size */
  2738. progress->update_step = progress->content_length * -PS(rfc1867_freq) / 100;
  2739. }
  2740. progress->next_update = 0;
  2741. progress->next_update_time = 0.0;
  2742. array_init(&progress->data);
  2743. array_init(&progress->files);
  2744. add_assoc_long_ex(&progress->data, "start_time", sizeof("start_time") - 1, (zend_long)sapi_get_request_time());
  2745. add_assoc_long_ex(&progress->data, "content_length", sizeof("content_length") - 1, progress->content_length);
  2746. add_assoc_long_ex(&progress->data, "bytes_processed", sizeof("bytes_processed") - 1, data->post_bytes_processed);
  2747. add_assoc_bool_ex(&progress->data, "done", sizeof("done") - 1, 0);
  2748. add_assoc_zval_ex(&progress->data, "files", sizeof("files") - 1, &progress->files);
  2749. progress->post_bytes_processed = zend_hash_str_find(Z_ARRVAL(progress->data), "bytes_processed", sizeof("bytes_processed") - 1);
  2750. php_rinit_session(0);
  2751. PS(id) = zend_string_init(Z_STRVAL(progress->sid), Z_STRLEN(progress->sid), 0);
  2752. if (progress->apply_trans_sid) {
  2753. /* Enable trans sid by modifying flags */
  2754. PS(use_trans_sid) = 1;
  2755. PS(use_only_cookies) = 0;
  2756. }
  2757. PS(send_cookie) = 0;
  2758. }
  2759. array_init(&progress->current_file);
  2760. /* Each uploaded file has its own array. Trying to make it close to $_FILES entries. */
  2761. add_assoc_string_ex(&progress->current_file, "field_name", sizeof("field_name") - 1, data->name);
  2762. add_assoc_string_ex(&progress->current_file, "name", sizeof("name") - 1, *data->filename);
  2763. add_assoc_null_ex(&progress->current_file, "tmp_name", sizeof("tmp_name") - 1);
  2764. add_assoc_long_ex(&progress->current_file, "error", sizeof("error") - 1, 0);
  2765. add_assoc_bool_ex(&progress->current_file, "done", sizeof("done") - 1, 0);
  2766. add_assoc_long_ex(&progress->current_file, "start_time", sizeof("start_time") - 1, (zend_long)time(NULL));
  2767. add_assoc_long_ex(&progress->current_file, "bytes_processed", sizeof("bytes_processed") - 1, 0);
  2768. add_next_index_zval(&progress->files, &progress->current_file);
  2769. progress->current_file_bytes_processed = zend_hash_str_find(Z_ARRVAL(progress->current_file), "bytes_processed", sizeof("bytes_processed") - 1);
  2770. Z_LVAL_P(progress->current_file_bytes_processed) = data->post_bytes_processed;
  2771. php_session_rfc1867_update(progress, 0);
  2772. }
  2773. break;
  2774. case MULTIPART_EVENT_FILE_DATA: {
  2775. multipart_event_file_data *data = (multipart_event_file_data *) event_data;
  2776. if (!Z_TYPE(progress->sid) || !progress->key.s) {
  2777. break;
  2778. }
  2779. Z_LVAL_P(progress->current_file_bytes_processed) = data->offset + data->length;
  2780. Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
  2781. php_session_rfc1867_update(progress, 0);
  2782. }
  2783. break;
  2784. case MULTIPART_EVENT_FILE_END: {
  2785. multipart_event_file_end *data = (multipart_event_file_end *) event_data;
  2786. if (!Z_TYPE(progress->sid) || !progress->key.s) {
  2787. break;
  2788. }
  2789. if (data->temp_filename) {
  2790. add_assoc_string_ex(&progress->current_file, "tmp_name", sizeof("tmp_name") - 1, data->temp_filename);
  2791. }
  2792. add_assoc_long_ex(&progress->current_file, "error", sizeof("error") - 1, data->cancel_upload);
  2793. add_assoc_bool_ex(&progress->current_file, "done", sizeof("done") - 1, 1);
  2794. Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
  2795. php_session_rfc1867_update(progress, 0);
  2796. }
  2797. break;
  2798. case MULTIPART_EVENT_END: {
  2799. multipart_event_end *data = (multipart_event_end *) event_data;
  2800. if (Z_TYPE(progress->sid) && progress->key.s) {
  2801. if (PS(rfc1867_cleanup)) {
  2802. php_session_rfc1867_cleanup(progress);
  2803. } else {
  2804. if (!Z_ISUNDEF(progress->data)) {
  2805. SEPARATE_ARRAY(&progress->data);
  2806. add_assoc_bool_ex(&progress->data, "done", sizeof("done") - 1, 1);
  2807. Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
  2808. php_session_rfc1867_update(progress, 1);
  2809. }
  2810. }
  2811. php_rshutdown_session_globals();
  2812. }
  2813. if (!Z_ISUNDEF(progress->data)) {
  2814. zval_ptr_dtor(&progress->data);
  2815. }
  2816. zval_ptr_dtor(&progress->sid);
  2817. smart_str_free(&progress->key);
  2818. efree(progress);
  2819. progress = NULL;
  2820. PS(rfc1867_progress) = NULL;
  2821. }
  2822. break;
  2823. }
  2824. if (progress && progress->cancel_upload) {
  2825. return FAILURE;
  2826. }
  2827. return retval;
  2828. } /* }}} */
  2829. zend_module_entry session_module_entry = {
  2830. STANDARD_MODULE_HEADER_EX,
  2831. NULL,
  2832. session_deps,
  2833. "session",
  2834. session_functions,
  2835. PHP_MINIT(session), PHP_MSHUTDOWN(session),
  2836. PHP_RINIT(session), PHP_RSHUTDOWN(session),
  2837. PHP_MINFO(session),
  2838. PHP_SESSION_VERSION,
  2839. PHP_MODULE_GLOBALS(ps),
  2840. PHP_GINIT(ps),
  2841. NULL,
  2842. NULL,
  2843. STANDARD_MODULE_PROPERTIES_EX
  2844. };
  2845. #ifdef COMPILE_DL_SESSION
  2846. #ifdef ZTS
  2847. ZEND_TSRMLS_CACHE_DEFINE()
  2848. #endif
  2849. ZEND_GET_MODULE(session)
  2850. #endif
  2851. /*
  2852. * Local variables:
  2853. * tab-width: 4
  2854. * c-basic-offset: 4
  2855. * End:
  2856. * vim600: noet sw=4 ts=4 fdm=marker
  2857. * vim<600: sw=4 ts=4
  2858. */