PageRenderTime 140ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Source.DLR/Core/GlobalVariables.CLR.cs

#
C# | 784 lines | 462 code | 117 blank | 205 comment | 99 complexity | 83d56c919bd87e9c35f039478a85da64 MD5 | raw file
Possible License(s): CPL-1.0, GPL-2.0, CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. /*
  2. Copyright (c) 2004-2006 Tomas Matousek.
  3. The use and distribution terms for this software are contained in the file named License.txt,
  4. which can be found in the root of the Phalanger distribution. By using this software
  5. in any fashion, you are agreeing to be bound by the terms of this license.
  6. You must not remove this notice from this software.
  7. */
  8. using System;
  9. using System.IO;
  10. using System.Diagnostics;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Web;
  14. using System.Web.SessionState;
  15. using System.Collections.Specialized;
  16. using System.Reflection;
  17. using PHP.Core.Emit;
  18. namespace PHP.Core
  19. {
  20. /// <summary>
  21. /// Declares auto-global variables stored in the script context.
  22. /// </summary>
  23. public sealed class AutoGlobals
  24. {
  25. internal const int EgpcsCount = 5;
  26. internal const int MaxCount = 9;
  27. internal const int EstimatedUserGlobalVariableCount = 15;
  28. #region Enumeration
  29. /// <summary>
  30. /// File upload errors.
  31. /// </summary>
  32. public enum PostedFileError
  33. {
  34. /// <summary>
  35. /// No error.
  36. /// </summary>
  37. None,
  38. /// <summary>
  39. /// The uploaded file exceeds the "upload_max_filesize" configuration option. Not supported.
  40. /// Request is not processed when exceeding maximal size of posted file set in ASP.NET config.
  41. /// </summary>
  42. SizeExceededOnServer,
  43. /// <summary>
  44. /// The uploaded file exceeds the "MAX_FILE_SIZE" value specified in the form. Not supported.
  45. /// </summary>
  46. SizeExceededOnClient,
  47. /// <summary>
  48. /// The uploaded file was only partially uploaded. Not supported.
  49. /// </summary>
  50. Partial,
  51. /// <summary>
  52. /// No file was uploaded.
  53. /// </summary>
  54. NoFile
  55. }
  56. #endregion
  57. #region Fields
  58. /// <summary>
  59. /// <para>
  60. /// If server context is available contains server variables ($_SERVER).
  61. /// Moreover, it contains <c>PHP_SELF</c> - a virtual path to the executing script and
  62. /// if <see cref="GlobalConfiguration.GlobalVariablesSection.RegisterArgcArgv"/> is set it contains also
  63. /// <c>argv</c> (an array containing a query string as its one and only element) and
  64. /// <c>argc</c> which is set to zero.
  65. /// </para>
  66. /// <para>
  67. /// If server context is not available contains empty array (unlike PHP which does fill it with <see cref="Env"/>
  68. /// and then adds some empty items).
  69. /// </para>
  70. /// </summary>
  71. public PhpReference/*!*/ Server = new PhpReference();
  72. public const string ServerName = "_SERVER";
  73. /// <summary>
  74. /// Environment variables ($_ENV).
  75. /// </summary>
  76. public PhpReference/*!*/ Env = new PhpReference();
  77. public const string EnvName = "_ENV";
  78. /// <summary>
  79. /// Global variables ($GLOBALS).
  80. /// </summary>
  81. public PhpReference/*!*/ Globals = new PhpReference();
  82. public const string GlobalsName = "GLOBALS";
  83. /// <summary>
  84. /// Request variables ($_REQUEST) copied from $_GET, $_POST and $_COOKIE arrays.
  85. /// </summary>
  86. public PhpReference/*!*/ Request = new PhpReference();
  87. public const string RequestName = "_REQUEST";
  88. /// <summary>
  89. /// Variables passed by HTTP GET method ($_GET).
  90. /// </summary>
  91. public PhpReference/*!*/ Get = new PhpReference();
  92. public const string GetName = "_GET";
  93. /// <summary>
  94. /// Variables passed by HTTP POST method ($_POST).
  95. /// </summary>
  96. public PhpReference/*!*/ Post = new PhpReference();
  97. public const string PostName = "_POST";
  98. /// <summary>
  99. /// Cookies ($_COOKIE).
  100. /// </summary>
  101. public PhpReference/*!*/ Cookie = new PhpReference();
  102. public const string CookieName = "_COOKIE";
  103. /// <summary>
  104. /// Uploaded files information ($_FILES).
  105. /// </summary>
  106. public PhpReference/*!*/ Files = new PhpReference();
  107. public const string FilesName = "_FILES";
  108. /// <summary>
  109. /// Session variables ($_SESSION). Initialized on session start.
  110. /// </summary>
  111. public PhpReference/*!*/ Session = new PhpReference();
  112. public const string SessionName = "_SESSION";
  113. #endregion
  114. #region IsAutoGlobal
  115. /// <summary>
  116. /// Checks whether a specified name is the name of an auto-global variable.
  117. /// </summary>
  118. /// <param name="name">The name.</param>
  119. /// <returns>Whether <paramref name="name"/> is auto-global.</returns>
  120. public static bool IsAutoGlobal(string name)
  121. {
  122. switch (name)
  123. {
  124. case "GLOBALS":
  125. case "_SERVER":
  126. case "_ENV":
  127. case "_COOKIE":
  128. case "_FILES":
  129. case "_REQUEST":
  130. case "_GET":
  131. case "_POST":
  132. case "_SESSION":
  133. return true;
  134. }
  135. return false;
  136. }
  137. #endregion
  138. #region Variable Addition
  139. /// <summary>
  140. /// Adds a variable to auto-global array.
  141. /// </summary>
  142. /// <param name="array">The array.</param>
  143. /// <param name="name">A unparsed name of variable.</param>
  144. /// <param name="value">A value to be added.</param>
  145. /// <param name="isGpc">Whether the array is GET, POST or COOKIE (value will be slashed then).</param>
  146. /// <param name="config">A configuration record.</param>
  147. /// <param name="subname">A name of intermediate array inserted before the value.</param>
  148. private static void AddVariable(
  149. PhpArray/*!*/ array,
  150. string name,
  151. object value,
  152. string subname,
  153. bool isGpc,
  154. LocalConfiguration/*!*/ config)
  155. {
  156. if (config == null)
  157. throw new ArgumentNullException("config");
  158. if (array == null)
  159. throw new ArgumentNullException("array");
  160. if (name == null)
  161. name = String.Empty;
  162. value = GpcEncodeValue(value, isGpc, config);
  163. string key;
  164. // current left and right square brace positions:
  165. int left, right;
  166. // checks pattern {var_name}[{key1}][{key2}]...[{keyn}] where var_name is [^[]* and keys are [^]]*:
  167. left = name.IndexOf('[');
  168. if (left > 0 && left < name.Length - 1 && (right = name.IndexOf(']', left + 1)) >= 0)
  169. {
  170. // the variable name is a key to the "array", dots are replaced by underscores in top-level name:
  171. key = name.Substring(0, left).Replace('.', '_');
  172. // ensures that all [] operators in the chain except for the last one are applied on an array:
  173. for (;;)
  174. {
  175. // adds a level keyed by "key":
  176. array = Operators.EnsureItemIsArraySimple(array, key);
  177. // adds a level keyed by "subname" (once only):
  178. if (subname != null)
  179. {
  180. array = Operators.EnsureItemIsArraySimple(array, subname);
  181. subname = null;
  182. }
  183. // next key:
  184. key = name.Substring(left + 1, right - left - 1);
  185. // breaks if ']' is not followed by '[':
  186. left = right + 1;
  187. if (left == name.Length || name[left] != '[') break;
  188. // the next right brace:
  189. right = name.IndexOf(']', left + 1);
  190. }
  191. if (key.Length > 0)
  192. array.SetArrayItem(key, value);
  193. else
  194. array.Add(value);
  195. }
  196. else
  197. {
  198. // no array pattern in variable name, "name" is a top-level key:
  199. name = name.Replace('.', '_');
  200. // inserts a subname on the next level:
  201. if (subname != null)
  202. Operators.EnsureItemIsArraySimple(array, name)[subname] = value;
  203. else
  204. array[name] = value;
  205. }
  206. }
  207. private static object GpcEncodeValue(object value, bool isGpc, LocalConfiguration config)
  208. {
  209. string svalue = value as string;
  210. if (svalue != null && isGpc)
  211. {
  212. // url-decodes the values:
  213. svalue = HttpUtility.UrlDecode(svalue, Configuration.Application.Globalization.PageEncoding);
  214. // quotes the values:
  215. if (Configuration.Global.GlobalVariables.QuoteGpcVariables)
  216. {
  217. if (config.Variables.QuoteInDbManner)
  218. svalue = StringUtils.AddDbSlashes(svalue);
  219. svalue = StringUtils.AddCSlashes(svalue, true, true);
  220. }
  221. value = svalue;
  222. }
  223. return value;
  224. }
  225. /// <summary>
  226. /// Adds variables from one auto-global array to another.
  227. /// </summary>
  228. /// <param name="dst">The target array.</param>
  229. /// <param name="src">The source array.</param>
  230. /// <remarks>Variable values are deeply copied.</remarks>
  231. /// <exception cref="ArgumentNullException"><paramref name="dst"/> is a <B>null</B> reference.</exception>
  232. /// <exception cref="ArgumentNullException"><paramref name="src"/> is a <B>null</B> reference.</exception>
  233. private static void AddVariables(PhpArray/*!*/ dst, PhpArray/*!*/ src)
  234. {
  235. Debug.Assert(dst != null && src != null);
  236. foreach (KeyValuePair<IntStringKey, object> entry in src)
  237. dst[entry.Key] = PhpVariable.DeepCopy(entry.Value);
  238. }
  239. /// <summary>
  240. /// Adds variables from one auto-global array to another.
  241. /// </summary>
  242. /// <param name="dst">A PHP reference to the target array.</param>
  243. /// <param name="src">A PHP reference to the source array.</param>
  244. /// <remarks>
  245. /// Variable values are deeply copied.
  246. /// If either reference is a <B>null</B> reference or doesn't contain an array, no copying takes place.
  247. /// </remarks>
  248. internal static void AddVariables(PhpReference/*!*/ dst, PhpReference/*!*/ src)
  249. {
  250. if (dst != null && src != null)
  251. {
  252. PhpArray adst = dst.Value as PhpArray;
  253. PhpArray asrc = src.Value as PhpArray;
  254. if (adst != null && asrc != null)
  255. AddVariables(adst, asrc);
  256. }
  257. }
  258. /// <summary>
  259. /// Loads variables from a collection.
  260. /// </summary>
  261. /// <param name="result">An array where to add variables stored in the collection.</param>
  262. /// <param name="collection">The collection.</param>
  263. /// <param name="isGpc">Whether the array is GET, POST or COOKIE.</param>
  264. /// <param name="config">A configuration record.</param>
  265. private static void LoadFromCollection(PhpArray result, NameValueCollection collection, bool isGpc, LocalConfiguration config)
  266. {
  267. foreach (string name in collection)
  268. {
  269. // gets all values associated with the name:
  270. string[] values = collection.GetValues(name);
  271. // adds all items:
  272. if (name != null)
  273. {
  274. for (int i = 0; i < values.Length; i++)
  275. AddVariable(result, name, values[i] as string, null, false, config);
  276. }
  277. else
  278. {
  279. // if name is null, only name of the variable is stated:
  280. // e.g. for GET variables, URL looks like this: ...&test&...
  281. // we add the name of the variable and an emtpy string to get what PHP gets:
  282. for (int i = 0; i < values.Length; i++)
  283. AddVariable(result, values[i], String.Empty, null, false, config);
  284. }
  285. }
  286. }
  287. #endregion
  288. #region Initialization
  289. /// <summary>
  290. /// Initializes all auto-global variables.
  291. /// </summary>
  292. internal void Initialize(LocalConfiguration config/*!*/, HttpContext context)
  293. {
  294. Debug.Assert(config != null);
  295. HttpRequest request = (context != null) ? context.Request : null;
  296. // $_ENV:
  297. InitializeEnvironmentVariables(config);
  298. // $_SERVER:
  299. InitializeServerVariables(config, context);
  300. // $_GET, $_POST, $_COOKIE, $_REQUEST:
  301. InitializeGetPostCookieRequestVariables(config, request);
  302. // $_SESSION (initialized by session_start)
  303. // $_FILE:
  304. InitializeFileVariables(config, request);
  305. // $GLOBALS:
  306. InitializeGlobals(config, request);
  307. }
  308. /// <summary>
  309. /// Loads $_ENV from Environment.GetEnvironmentVariables().
  310. /// </summary>
  311. private void InitializeEnvironmentVariables(LocalConfiguration/*!*/ config)
  312. {
  313. Debug.Assert(config != null);
  314. IDictionary env_vars = Environment.GetEnvironmentVariables();
  315. PhpArray array = new PhpArray(0, env_vars.Count);
  316. foreach (DictionaryEntry entry in env_vars)
  317. AddVariable(array, entry.Key as string, entry.Value as string, null, false, config);
  318. Env.Value = array;
  319. }
  320. /// <summary>
  321. /// Loads $_SERVER from HttpRequest.ServerVariables.
  322. /// </summary>
  323. private void InitializeServerVariables(LocalConfiguration/*!*/ config, HttpContext context)
  324. {
  325. Debug.Assert(config != null);
  326. PhpArray array, argv;
  327. if (context != null)
  328. {
  329. array = new PhpArray(0, context.Request.ServerVariables.Count);
  330. // adds variables defined by ASP.NET and IIS:
  331. LoadFromCollection(array, context.Request.ServerVariables, false, config);
  332. // adds argv, argc variables:
  333. if (Configuration.Global.GlobalVariables.RegisterArgcArgv)
  334. {
  335. array["argv"] = argv = new PhpArray();
  336. argv.Add(context.Request.ServerVariables["QUERY_STRING"]);
  337. array["argc"] = 0;
  338. }
  339. // additional variables defined in PHP manual:
  340. array["PHP_SELF"] = context.Request.Path;
  341. try
  342. {
  343. array["DOCUMENT_ROOT"] = context.Request.MapPath("/"); // throws exception under mod_aspdotnet
  344. }
  345. catch (Exception)
  346. {
  347. array["DOCUMENT_ROOT"] = null;
  348. }
  349. array["SERVER_ADDR"] = context.Request.ServerVariables["LOCAL_ADDR"];
  350. array["REQUEST_URI"] = context.Request.RawUrl;
  351. array["REQUEST_TIME"] = DateTimeUtils.UtcToUnixTimeStamp(context.Timestamp.ToUniversalTime());
  352. array["SCRIPT_FILENAME"] = context.Request.PhysicalPath;
  353. }
  354. else
  355. {
  356. array = new PhpArray(0, 0);
  357. }
  358. Server.Value = array;
  359. }
  360. /// <summary>
  361. /// Loads $_GET, $_POST, $_COOKIE, and $_REQUEST arrays.
  362. /// </summary>
  363. private void InitializeGetPostCookieRequestVariables(LocalConfiguration/*!*/ config, HttpRequest request)
  364. {
  365. Debug.Assert(config != null);
  366. PhpArray get_array, post_array, cookie_array, request_array;
  367. InitializeGetPostVariables(config, request, out get_array, out post_array);
  368. InitializeCookieVariables(config, request, out cookie_array);
  369. InitializeRequestVariables(request, config.Variables.RegisteringOrder,
  370. get_array, post_array, cookie_array, out request_array);
  371. Get.Value = get_array;
  372. Post.Value = post_array;
  373. Cookie.Value = cookie_array;
  374. Request.Value = request_array;
  375. }
  376. /// <summary>
  377. /// Loads $_GET, $_POST arrays from HttpRequest.QueryString and HttpRequest.Form.
  378. /// </summary>
  379. /// <param name="config">Local configuration.</param>
  380. /// <param name="request">HTTP request instance or a <B>null</B> reference.</param>
  381. /// <param name="getArray">Resulting $_GET array.</param>
  382. /// <param name="postArray">Resulting $_POST array.</param>
  383. /// <exception cref="ArgumentNullException"><paranref name="config"/> is a <B>null</B> reference.</exception>
  384. public static void InitializeGetPostVariables(LocalConfiguration/*!*/ config, HttpRequest request,
  385. out PhpArray getArray, out PhpArray postArray)
  386. {
  387. if (config == null)
  388. throw new ArgumentNullException("config");
  389. if (request != null)
  390. {
  391. if (request.RequestType == "GET")
  392. {
  393. getArray = new PhpArray(0, request.QueryString.Count + request.Form.Count);
  394. postArray = new PhpArray(0, 0);
  395. // loads Form variables to GET array:
  396. LoadFromCollection(getArray, request.Form, true, config);
  397. }
  398. else
  399. {
  400. getArray = new PhpArray(0, request.QueryString.Count);
  401. postArray = new PhpArray(0, request.Form.Count);
  402. // loads Form variables to POST array:
  403. LoadFromCollection(postArray, request.Form, true, config);
  404. }
  405. // loads Query variables to GET array:
  406. LoadFromCollection(getArray, request.QueryString, true, config);
  407. }
  408. else
  409. {
  410. getArray = new PhpArray(0, 0);
  411. postArray = new PhpArray(0, 0);
  412. }
  413. }
  414. /// <summary>
  415. /// Loads $_COOKIE arrays from HttpRequest.Cookies.
  416. /// </summary>
  417. /// <param name="config">Local configuration.</param>
  418. /// <param name="request">HTTP request instance or a <B>null</B> reference.</param>
  419. /// <param name="cookieArray">Resulting $_COOKIE array.</param>
  420. /// <exception cref="ArgumentNullException"><paranref name="config"/> is a <B>null</B> reference.</exception>
  421. public static void InitializeCookieVariables(LocalConfiguration/*!*/ config, HttpRequest request,
  422. out PhpArray cookieArray)
  423. {
  424. if (config == null)
  425. throw new ArgumentNullException("config");
  426. if (request != null)
  427. {
  428. cookieArray = new PhpArray(0, request.Cookies.Count);
  429. foreach (string cookie_name in request.Cookies)
  430. {
  431. HttpCookie cookie = request.Cookies[cookie_name];
  432. AddVariable(cookieArray, cookie.Name, cookie.Value, null, true, config);
  433. // adds a copy of cookie with the same key as the session name;
  434. // the name gets encoded and so $_COOKIE[session_name()] doesn't work then:
  435. if (cookie.Name == AspNetSessionHandler.AspNetSessionName)
  436. cookieArray[AspNetSessionHandler.AspNetSessionName] = GpcEncodeValue(cookie.Value, true, config);
  437. }
  438. }
  439. else
  440. {
  441. cookieArray = new PhpArray(0, 0);
  442. }
  443. }
  444. /// <summary>
  445. /// Loads $_REQUEST from $_GET, $_POST and $_COOKIE arrays.
  446. /// </summary>
  447. private static void InitializeRequestVariables(HttpRequest request, string/*!*/ gpcOrder,
  448. PhpArray/*!*/ getArray, PhpArray/*!*/ postArray, PhpArray/*!*/ cookieArray, out PhpArray requestArray)
  449. {
  450. Debug.Assert(gpcOrder != null && getArray != null && postArray != null && cookieArray != null);
  451. if (request != null)
  452. {
  453. requestArray = new PhpArray(0, getArray.Count + postArray.Count + cookieArray.Count);
  454. // adds items from GET, POST, COOKIE arrays in the order specified by RegisteringOrder config option:
  455. for (int i = 0; i < gpcOrder.Length; i++)
  456. {
  457. switch (Char.ToUpper(gpcOrder[i]))
  458. {
  459. case 'G': AddVariables(requestArray, getArray); break;
  460. case 'P': AddVariables(requestArray, postArray); break;
  461. case 'C': AddVariables(requestArray, cookieArray); break;
  462. }
  463. }
  464. }
  465. else
  466. {
  467. requestArray = new PhpArray(0, 0);
  468. }
  469. }
  470. /// <summary>
  471. /// Loads $_FILES from HttpRequest.Files.
  472. /// </summary>
  473. /// <remarks>
  474. /// <list type="bullet">
  475. /// <item>$_FILES[{var_name}]['name'] - The original name of the file on the client machine.</item>
  476. /// <item>$_FILES[{var_name}]['type'] - The mime type of the file, if the browser provided this information. An example would be "image/gif".</item>
  477. /// <item>$_FILES[{var_name}]['size'] - The size, in bytes, of the uploaded file.</item>
  478. /// <item>$_FILES[{var_name}]['tmp_name'] - The temporary filename of the file in which the uploaded file was stored on the server.</item>
  479. /// <item>$_FILES[{var_name}]['error'] - The error code associated with this file upload.</item>
  480. /// </list>
  481. /// </remarks>
  482. private void InitializeFileVariables(LocalConfiguration/*!*/ config, HttpRequest request)
  483. {
  484. Debug.Assert(config != null);
  485. PhpArray files;
  486. string path;
  487. int count;
  488. GlobalConfiguration global_config = Configuration.Global;
  489. if (request != null && global_config.PostedFiles.Accept && (count = request.Files.Count) > 0)
  490. {
  491. files = new PhpArray(0, count);
  492. // gets a path where temporary files are stored:
  493. path = global_config.PostedFiles.GetTempPath(global_config.SafeMode);
  494. for (int i = 0; i < count; i++)
  495. {
  496. string name = request.Files.GetKey(i);
  497. string file_path, type, file_name;
  498. HttpPostedFile file = request.Files[i];
  499. PostedFileError error = PostedFileError.None;
  500. if (file.FileName != String.Empty)
  501. {
  502. type = file.ContentType;
  503. file_path = Path.Combine(path, RequestContext.GetTempFileName());
  504. file_name = Path.GetFileName(file.FileName);
  505. // registers the temporary file for deletion at request end:
  506. RequestContext.AddTemporaryFile(file_path);
  507. // saves uploaded content to the temporary file:
  508. file.SaveAs(file_path);
  509. }
  510. else
  511. {
  512. file_path = type = file_name = String.Empty;
  513. error = PostedFileError.NoFile;
  514. }
  515. AddVariable(files, name, file_name, "name", false, config);
  516. AddVariable(files, name, type, "type", false, config);
  517. AddVariable(files, name, file_path, "tmp_name", false, config);
  518. AddVariable(files, name, (int)error, "error", false, config);
  519. AddVariable(files, name, file.ContentLength, "size", false, config);
  520. }
  521. }
  522. else
  523. {
  524. files = new PhpArray(0, 0);
  525. }
  526. Files.Value = files;
  527. }
  528. /// <summary>
  529. /// Adds file variables from $_FILE array to $GLOBALS array.
  530. /// </summary>
  531. /// <param name="globals">$GLOBALS array.</param>
  532. /// <param name="files">$_FILES array.</param>
  533. private void AddFileVariablesToGlobals(PhpArray/*!*/ globals, PhpArray/*!*/ files)
  534. {
  535. foreach (KeyValuePair<IntStringKey, object> entry in files)
  536. {
  537. PhpArray file_info = (PhpArray)entry.Value;
  538. globals[entry.Key] = file_info["tmp_name"];
  539. globals[entry.Key.ToString() + "_name"] = file_info["name"];
  540. globals[entry.Key.ToString() + "_type"] = file_info["type"];
  541. globals[entry.Key.ToString() + "_size"] = file_info["size"];
  542. }
  543. }
  544. /// <summary>
  545. /// Loads $GLOBALS from $_ENV, $_REQUEST, $_SERVER and $_FILES.
  546. /// </summary>
  547. private void InitializeGlobals(LocalConfiguration/*!*/ config, HttpRequest/*!*/ request)
  548. {
  549. Debug.Assert(config != null && Request.Value != null && Env.Value != null && Server.Value != null && Files.Value != null);
  550. PhpArray globals;
  551. GlobalConfiguration global = Configuration.Global;
  552. // estimates the initial capacity of $GLOBALS array:
  553. int count = EstimatedUserGlobalVariableCount + AutoGlobals.MaxCount;
  554. if (global.GlobalVariables.RegisterLongArrays) count += AutoGlobals.MaxCount;
  555. // adds EGPCS variables as globals:
  556. if (global.GlobalVariables.RegisterGlobals)
  557. {
  558. PhpArray env_array = (PhpArray)Env.Value;
  559. PhpArray get_array = (PhpArray)Get.Value;
  560. PhpArray post_array = (PhpArray)Post.Value;
  561. PhpArray files_array = (PhpArray)Files.Value;
  562. PhpArray cookie_array = (PhpArray)Cookie.Value;
  563. PhpArray server_array = (PhpArray)Server.Value;
  564. PhpArray request_array = (PhpArray)Request.Value;
  565. if (request != null)
  566. {
  567. globals = new PhpArray(0, count + env_array.Count + request_array.Count + server_array.Count + files_array.Count * 4);
  568. // adds items in the order specified by RegisteringOrder config option (overwrites existing):
  569. string order = config.Variables.RegisteringOrder;
  570. for (int i = 0; i < order.Length; i++)
  571. {
  572. switch (order[i])
  573. {
  574. case 'E': AddVariables(globals, env_array); break;
  575. case 'G': AddVariables(globals, get_array); break;
  576. case 'P':
  577. AddVariables(globals, post_array);
  578. AddFileVariablesToGlobals(globals, files_array);
  579. break;
  580. case 'C': AddVariables(globals, cookie_array); break;
  581. case 'S': AddVariables(globals, server_array); break;
  582. }
  583. }
  584. }
  585. else
  586. {
  587. globals = new PhpArray(0, count + env_array.Count);
  588. AddVariables(globals, env_array);
  589. }
  590. }
  591. else
  592. {
  593. globals = new PhpArray(0, count);
  594. }
  595. // command line argc, argv:
  596. if (request == null)
  597. {
  598. string[] args = Environment.GetCommandLineArgs();
  599. PhpArray argv = new PhpArray(0, args.Length);
  600. // adds all arguments to the array (the 0-th argument is not '-' as in PHP but the program file):
  601. for (int i = 0; i < args.Length; i++)
  602. argv.Add(i, args[i]);
  603. globals["argv"] = argv;
  604. globals["argc"] = args.Length;
  605. }
  606. // adds auto-global variables (overwrites potential existing variables in $GLOBALS):
  607. globals[GlobalsName] = Globals;
  608. globals[EnvName] = Env;
  609. globals[GetName] = Get;
  610. globals[PostName] = Post;
  611. globals[CookieName] = Cookie;
  612. globals[RequestName] = Request;
  613. globals[ServerName] = Server;
  614. globals[FilesName] = Files;
  615. globals[SessionName] = Session;
  616. // adds long arrays:
  617. if (Configuration.Global.GlobalVariables.RegisterLongArrays)
  618. {
  619. globals.Add("HTTP_ENV_VARS", new PhpReference(((PhpArray)Env.Value).DeepCopy()));
  620. globals.Add("HTTP_GET_VARS", new PhpReference(((PhpArray)Get.Value).DeepCopy()));
  621. globals.Add("HTTP_POST_VARS", new PhpReference(((PhpArray)Post.Value).DeepCopy()));
  622. globals.Add("HTTP_COOKIE_VARS", new PhpReference(((PhpArray)Cookie.Value).DeepCopy()));
  623. globals.Add("HTTP_SERVER_VARS", new PhpReference(((PhpArray)Server.Value).DeepCopy()));
  624. globals.Add("HTTP_POST_FILES", new PhpReference(((PhpArray)Files.Value).DeepCopy()));
  625. // both session array references the same array:
  626. globals.Add("HTTP_SESSION_VARS", Session);
  627. }
  628. Globals.Value = globals;
  629. }
  630. #endregion
  631. #region Emit Support
  632. /// <summary>
  633. /// Returns 'FieldInfo' representing field in AutoGlobals for given global variable name.
  634. /// </summary>
  635. internal static FieldInfo GetFieldForVariable(VariableName name)
  636. {
  637. switch (name.ToString())
  638. {
  639. case AutoGlobals.CookieName:
  640. return Fields.AutoGlobals.Cookie;
  641. case AutoGlobals.EnvName:
  642. return Fields.AutoGlobals.Env;
  643. case AutoGlobals.FilesName:
  644. return Fields.AutoGlobals.Files;
  645. case AutoGlobals.GetName:
  646. return Fields.AutoGlobals.Get;
  647. case AutoGlobals.GlobalsName:
  648. return Fields.AutoGlobals.Globals;
  649. case AutoGlobals.PostName:
  650. return Fields.AutoGlobals.Post;
  651. case AutoGlobals.RequestName:
  652. return Fields.AutoGlobals.Request;
  653. case AutoGlobals.ServerName:
  654. return Fields.AutoGlobals.Server;
  655. case AutoGlobals.SessionName:
  656. return Fields.AutoGlobals.Session;
  657. default:
  658. return null;
  659. }
  660. }
  661. #endregion
  662. }
  663. }