/// <summary>Initialize $_REQUEST global variable.</summary>
        protected PhpArray InitRequestVariable(PhpArray get, PhpArray post, PhpArray cookie, string gpcOrder)
        {
            Debug.Assert(get != null && post != null && cookie != null && gpcOrder != null);

            if (IsWebApplication)
            {
                var requestArray = new PhpArray(get.Count + post.Count + cookie.Count);

                // adds items from GET, POST, COOKIE arrays in the order specified by RegisteringOrder config option:
                for (int i = 0; i < gpcOrder.Length; i++)
                {
                    switch (char.ToUpperInvariant(gpcOrder[i]))
                    {
                    case 'G': Superglobals.AddVariables(requestArray, get); break;

                    case 'P': Superglobals.AddVariables(requestArray, post); break;

                    case 'C': Superglobals.AddVariables(requestArray, cookie); break;
                    }
                }

                return(requestArray);
            }
            else
            {
                return(PhpArray.NewEmpty());
            }
        }
Beispiel #2
0
 /// <summary>
 /// Creates <see cref="PhpArray"/> from object's properties.
 /// </summary>
 /// <param name="obj">Object instance.</param>
 /// <returns>Array containing given object properties keyed according to PHP specifications.</returns>
 public static PhpArray ClassToArray(object obj)
 {
     if (object.ReferenceEquals(obj, null))
     {
         return(PhpArray.NewEmpty());
     }
     else if (obj.GetType() == typeof(stdClass))
     {
         // special case,
         // object is stdClass, we can simply copy its runtime fields
         return(((stdClass)obj).GetRuntimeFields().DeepCopy());
     }
     else
     {
         if (obj is Array)
         {
             // [] -> array
             return(new PhpArray((Array)obj));
         }
         // TODO: IList
         else
         {
             // obj -> array
             var arr = new PhpArray();
             TypeMembersUtils.InstanceFieldsToPhpArray(obj, arr);
             return(arr);
         }
     }
 }
Beispiel #3
0
        void InitSuperglobals(ref Superglobals superglobals)
        {
            var var_order = this.Configuration.Core.VariablesOrder; // TODO
            var egpcs     = this.Configuration.Core.RegisteringOrder;

            superglobals.env     = Superglobals.StaticEnv.DeepCopy();
            superglobals.get     = InitGetVariable();
            superglobals.post    = InitPostVariable();
            superglobals.cookie  = InitCookieVariable();
            superglobals.server  = InitServerVariable();
            superglobals.files   = InitFilesVariable();
            superglobals.session = PhpArray.NewEmpty();
            superglobals.request = InitRequestVariable(superglobals.get, superglobals.post, superglobals.cookie, egpcs);   // after get, post, cookie
            superglobals.globals = InitGlobals(egpcs);
        }
Beispiel #4
0
        /// <summary>
        /// Starts the session if it is not started yet.
        /// </summary>
        public virtual bool StartSession(Context ctx, IHttpPhpContext webctx)
        {
            // checks and changes session state:
            if (webctx.SessionState != PhpSessionState.Closed)
            {
                return(false);
            }
            webctx.SessionState = PhpSessionState.InProgress;

            try
            {
                // ensures session and reads session data
                var session_array = this.Load(webctx) ?? PhpArray.NewEmpty();

                // sets the auto-global variable (the previous content of $_SESSION array is discarded):
                ctx.Session = session_array;
                ctx.Globals[SESSION_Variable] = session_array;

                //if (ctx.Configuration.Core.RegisterGlobals)
                //{
                //    // ctx.RegisterSessionGlobals();
                //}

                // adds/updates a SID constant:
                if (!ctx.DefineConstant(SID_Constant, GetSessionId(webctx), true))
                {
                    throw new InvalidOperationException("SID already set.");    // TODO: allow overwriting
                }
            }
            catch
            {
                webctx.SessionState = PhpSessionState.Closed;
                return(false);
            }
            finally
            {
                //
                webctx.SessionState = PhpSessionState.Started;
            }

            //
            return(true);
        }
Beispiel #5
0
 /// <summary>
 /// Creates <see cref="PhpArray"/> from object's properties.
 /// </summary>
 /// <param name="obj">Object instance.</param>
 /// <returns>Array containing given object properties keyed according to PHP specifications.</returns>
 public static PhpArray ClassToArray(object obj)
 {
     if (object.ReferenceEquals(obj, null))
     {
         return(PhpArray.NewEmpty());
     }
     else if (obj.GetType() == typeof(stdClass))
     {
         // special case,
         // object is stdClass, we can simply copy its runtime fields
         var runtime_fields = ((stdClass)obj).GetRuntimeFields();
         return((runtime_fields != null) ? runtime_fields.DeepCopy() : PhpArray.NewEmpty());
     }
     else
     {
         if (obj is IPhpConvertible conv)
         {
             return(ToArray(conv));
         }
         else if (obj is Array)
         {
             // [] -> array
             return(new PhpArray((Array)obj));
         }
         else if (obj is System.Collections.IEnumerable)
         {
             // the same behavior as foreach for CLR enumerators
             return(PhpArray.Create(Operators.GetForeachEnumerator((System.Collections.IEnumerable)obj)));
         }
         else
         {
             // obj -> array
             var arr = new PhpArray();
             TypeMembersUtils.InstanceFieldsToPhpArray(obj, arr);
             return(arr);
         }
     }
 }
 /// <summary>Initialize $_COOKIE global variable.</summary>
 protected virtual PhpArray InitCookieVariable() => PhpArray.NewEmpty();
 /// <summary>Initialize $_FILES global variable.</summary>
 protected virtual PhpArray InitFilesVariable() => PhpArray.NewEmpty();
 /// <summary>Initialize $_POST global variable.</summary>
 protected virtual PhpArray InitPostVariable() => PhpArray.NewEmpty();
 /// <summary>Initialize $_SERVER global variable.</summary>
 protected virtual PhpArray InitServerVariable() => PhpArray.NewEmpty();
 public override PhpArray ToArray(ref PhpValue me) => PhpArray.NewEmpty();