/// <summary>
		/// Adds variables from one auto-global array to another.
		/// </summary>
		/// <param name="dst">The target array.</param>
		/// <param name="src">The source array.</param>
		/// <remarks>Variable values are deeply copied.</remarks>
		/// <exception cref="ArgumentNullException"><paramref name="dst"/> is a <B>null</B> reference.</exception>
		/// <exception cref="ArgumentNullException"><paramref name="src"/> is a <B>null</B> reference.</exception>
		private static void AddVariables(PhpArray/*!*/ dst, PhpArray/*!*/ src)
		{
			Debug.Assert(dst != null && src != null);

			foreach (KeyValuePair<IntStringKey, object> entry in src)
				dst[entry.Key] = PhpVariable.DeepCopy(entry.Value);
		}
示例#2
0
        public override void Load(string code = "")
        {
            try
            {
                context = ScriptContext.CurrentContext;
                context.Include(rpath  + "\\" + Name + ".php", true);
                Class = (PhpObject) context.NewObject(Name);
                PHPGlobals = context.GlobalVariables;
                context.GlobalVariables.Add("Commands", chatCommands);
                context.GlobalVariables.Add("DataStore", DataStore.GetInstance());
                context.GlobalVariables.Add("Find", Find.GetInstance());
                context.GlobalVariables.Add("GlobalData", GlobalData);
                context.GlobalVariables.Add("Plugin", this);
                context.GlobalVariables.Add("Server", Pluton.Server.GetInstance());
                context.GlobalVariables.Add("ServerConsoleCommands", consoleCommands);
                context.GlobalVariables.Add("Util", Util.GetInstance());
                context.GlobalVariables.Add("Web", Web.GetInstance());
                context.GlobalVariables.Add("World", World.GetInstance());
                foreach (var x in PHPGlobals)
                {
                    Globals.Add(x.Key.ToString());
                }

                State = PluginState.Loaded;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                State = PluginState.FailedToLoad;
            }

            PluginLoader.GetInstance().OnPluginLoaded(this);
        }
示例#3
0
        public object[] BindParams(MethodInfo mi, PhpArray parameters, bool wrappedArgs)
        {
            var resultParams = new List<object>();
            var parameterInfos = mi.GetParameters();
            object value;


            if (!wrappedArgs)
            {
                //TODO: make sure: When arguments are not wrapped soap method parameter is only one
                Debug.Assert(parameterInfos.Length == 1);

                resultParams.Add(Bind(parameters, parameterInfos[0].ParameterType));

            }
            else
            {
                foreach (var pi in parameterInfos)
                {
                    if (SetSpecifiedParameter(resultParams, pi))
                        continue;

                    if (parameters.TryGetValue(pi.Name, out value))
                    {
                        resultParams.Add(Bind(value, pi.ParameterType));
                    }
                }
            }

            lastPrimitive = false;

            return resultParams.ToArray();
        }
示例#4
0
        public static CurlForm Create(PhpArray arr)
        {
            string type = null;
            string filename;

            CurlForm form = new CurlForm();

            //go through items and if item starts with @ we have to treat it as file
            foreach (var item in arr)
            {
                var strValue = PHP.Core.Convert.ObjectToString(item.Value);
                if (!string.IsNullOrEmpty(strValue) && strValue[0] == '@')
                {
                    int index = strValue.IndexOf(";type=");
                    if (index != -1)
                        type = strValue.Substring(index + ";type=".Length);


                    index = strValue.IndexOf(";filename=");
                    if (index != -1)
                    {
                        filename = strValue.Substring(index + ";filename=".Length);
                        //filename = Path.Combine(ScriptContext.CurrentContext.WorkingDirectory, filename);
                    }
                    else
                    {
                        //filename = Path.Combine(ScriptContext.CurrentContext.WorkingDirectory, strValue.Substring(1));
                        filename = strValue.Substring(1);
                    }

                    form.AddFile(item.Key.String,
                        filename,
                        type != null ? type : "application/octet-stream",
                        item.Value
                        );
                    //Code from PHP CURL extension:
                    //error = curl_formadd(&first, &last,
                    //                CURLFORM_COPYNAME, string_key,
                    //                CURLFORM_NAMELENGTH, (long)string_key_len - 1,
                    //                CURLFORM_FILENAME, filename ? filename + sizeof(";filename=") - 1 : postval,
                    //                CURLFORM_CONTENTTYPE, type ? type + sizeof(";type=") - 1 : "application/octet-stream",
                    //                CURLFORM_FILE, postval,
                    //                CURLFORM_END);
                }
                else
                {
                    form.AddData(item.Key.String, item.Value);

                    //Code from PHP CURL extension:
                    //error = curl_formadd(&first, &last,
                    //                             CURLFORM_COPYNAME, string_key,
                    //                             CURLFORM_NAMELENGTH, (long)string_key_len - 1,
                    //                             CURLFORM_COPYCONTENTS, postval,
                    //                             CURLFORM_CONTENTSLENGTH, (long)Z_STRLEN_PP(current),
                    //                             CURLFORM_END);
                }
            }

            return form;
        }
示例#5
0
 public static PhpArray BuildErrorInfo(string sqlstate, object driver_error, string message)
 {
     PhpArray arr = new PhpArray();
     arr.Add(0, sqlstate);
     arr.Add(1, driver_error);
     arr.Add(2, message);
     return arr;
 }
示例#6
0
		public static int PrintFormatted(string format, PhpArray args)
		{
            string formattedString = PhpStrings.Format(format, args);

			ScriptContext.CurrentContext.Output.Write(formattedString);

            return formattedString.Length;
		}
示例#7
0
        public static PhpArray/*!*/SplClasses()
        {
            var array = new PhpArray(32);

            // TODO: (J) list SPL classes http://www.php.net/manual/en/function.spl-classes.php
            // array.Add( "class_name", "class_name" );

            return array;
        }
示例#8
0
        private static stdClass WrapToStdClass(object obj, string name)
        {
            var runtimeFields = new PhpArray(1);
            runtimeFields[name] = obj;

            return new stdClass()
            {
                RuntimeFields = runtimeFields
            };
        }
示例#9
0
        /// <summary>
        /// Constructor of PHP closure.
        /// </summary>
        /// <param name="context">Current <see cref="ScriptContext"/></param>
        /// <param name="lambda">Delegate to lambda function itself.</param>
        /// <param name="parameter"><see cref="PhpArray"/> of closure <c>parameter</c> field. Can be <c>null</c> if there are no parameters.</param>
        /// <param name="static"><see cref="PhpArray"/> of closure <c>parameter</c> field. Can be <c>null</c> if there is no <c>use</c> of scope variables.</param>
        public Closure(ScriptContext/*!*/context, RoutineDelegate/*!*/lambda, PhpArray parameter, PhpArray @static)
            :this(context, true)
        {
            Debug.Assert(context != null);
            Debug.Assert(lambda != null);

            this.lambda = lambda;
            this.parameter = parameter;
            this.@static = @static;
        }
示例#10
0
		public static PhpArray PrintFunctions()
		{
			PhpArray result = new PhpArray();

			result.Add("name", new PhpArray());
			result.Add("type", new PhpArray());
			result.Add("method", new PhpArray());

			Assembly assembly = typeof(PhpDocumentation).Assembly;
			//PhpLibraryModule.EnumerateFunctions(assembly, new PhpLibraryModule.FunctionsEnumCallback(FunctionsCallback), result);
			return result;
		}
示例#11
0
        public static object CallUserFunctionArray(DTypeDesc caller, PhpCallback function, PhpArray args)
		{
			object[] args_array;

            if (args != null)
            {
                args_array = new object[args.Count];
                args.CopyValuesTo(args_array, 0);
            }
            else
            {
                args_array = ArrayUtils.EmptyObjects;
            }

			return CallUserFunction(caller, function, args_array);
		}
示例#12
0
 internal static object BindServerParameters(object[] parameters, MethodInfo mi)
 {
     var runtimeFields = new PhpArray();
     //I can also just return CLR type and wrap it with PHP.Core.Reflection.ClrObject.WrapDynamic
     int i = 0;
     foreach (var p in mi.GetParameters().Where(a => !a.IsOut))
     {
         var name = WsdlHelper.GetParameterSoapName(p);
         var value = Bind(parameters[i++]);
         runtimeFields[name] = value;
     }
     return new stdClass()
     {
         RuntimeFields = runtimeFields
     };
 }
示例#13
0
        private static object BindObject(object obj, Type type)
        {
            FieldInfo[] fi = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
            var runtimeFields = new PhpArray(fi.Length);
            object value;
            bool specified = true;
            FieldInfo field;

            for (int i = 0; i < fi.Length; ++i)
            {
                field = fi[i];

                specified = true;

                if (i + 1 < fi.Length && Attribute.IsDefined(fi[i + 1], typeof(XmlIgnoreAttribute)))
                {
                    value = fi[i + 1].GetValue(obj);
                    if (value == null)
                        specified = false;
                    else
                        specified = (bool)value;

                    i++;
                }

                if (specified)
                {
                  var fieldValue = field.GetValue(obj);
                  value = Bind(fieldValue, field);
                    if (value != null)
                    {
                        if (fieldValue != null && fieldValue.GetType().IsArray && ((Array) fieldValue).Length > 0)
                            fieldValue = ((Array) fieldValue).GetValue(0);
                        var attr = Attribute.GetCustomAttributes(field, typeof(XmlElementAttribute)).Cast<XmlElementAttribute>().FirstOrDefault(a => a.Type == null || a.Type.IsInstanceOfType(fieldValue));
                        var name = field.Name;
                        if (attr != null && !string.IsNullOrWhiteSpace(attr.ElementName))
                            name = attr.ElementName;
                        runtimeFields.Add(name, value);
                    }
                }
            }

            return new stdClass()
            {
                RuntimeFields = runtimeFields
            };
        }
示例#14
0
        public static PhpArray GetFunctions()
        {
            var context = ScriptContext.CurrentContext;
            if (context.IsSplAutoloadEnabled)
            {
                PhpArray result = new PhpArray();
                foreach (var func in context.SplAutoloadFunctions)
                    result.Add(func.ToPhpRepresentation());
                
                return result;
            }
            else
            {
                return null;
            }

            
        }
示例#15
0
        /// <summary>
        /// Invokes the call.
        /// </summary>
        /// <returns></returns>
        public object InvokeCall(string methodName ,PhpArray parameters)
        {
            var soapProxy = (SoapHttpClientProtocolExtended)proxyInstance;
            var mi = WsdlHelper.GetMethodBySoapName(methodName, soapProxy.GetType());

            bool wrappedArgs = true;

            object[] attr = mi.GetCustomAttributes(typeof(System.Web.Services.Protocols.SoapDocumentMethodAttribute), false);
            if (attr.Length > 0 && attr[0].GetType() == typeof(System.Web.Services.Protocols.SoapDocumentMethodAttribute))
            {
                var soapMethodAttr = (System.Web.Services.Protocols.SoapDocumentMethodAttribute)attr[0];
                if (soapMethodAttr.ParameterStyle == System.Web.Services.Protocols.SoapParameterStyle.Bare)
                {
                    wrappedArgs = false;
                }
            }


            var paramBinder = new ParameterBinder();
            object[] transformedParameters = paramBinder.BindParams(mi, parameters, wrappedArgs);


            object[] resArray = soapProxy.Invoke(mi.Name, transformedParameters);

            if (resArray[0] != null)
            {
                var returnName = WsdlHelper.GetParameterSoapName(mi.ReturnParameter);
                resArray[0] = ResultBinder.BindResult( 
                    resArray[0],
                    returnName,
                    wrappedArgs);
            }

            //object result = mi.Invoke(proxyInstance, (object[])methodParams.ToArray(typeof(object)));

            //foreach (ParameterInfo pi in mi.GetParameters())
            //{
            //    if (pi.IsOut) outParams.Add(methodParams[i]);

            //    i++;
            //}
                
            return resArray[0];
        }
示例#16
0
        public static int Apply(ScriptContext/*!*/context, PHP.Core.Reflection.DTypeDesc caller, Iterator/*!*/iterator, PhpCallback function, PhpArray args)
        {
            // check parameters:
            Debug.Assert(context != null);
            Debug.Assert(iterator != null, "Phalanger should not pass a null here.");

            if (function == null)
            {
                PhpException.ArgumentNull("function");
                return -1;
            }

            // copy args into object array:
            object[] args_array;

            if (args != null)
            {
                args_array = new object[args.Count];
                args.Values.CopyTo(args_array, 0);
            }
            else
            {
                args_array = ArrayUtils.EmptyObjects;
            }

            // iterate through the iterator:
            int n = 0;
            
            iterator.rewind(context);
            
            while (PHP.Core.Convert.ObjectToBoolean(iterator.valid(context)))
            {
                if (!PHP.Core.Convert.ObjectToBoolean(function.Invoke(caller, args_array)))
                    break;
		        n++;

		        iterator.next(context);
	        }

            // return amount of iterated elements:
            return n;
        }
示例#17
0
        public object __call(string function_name, PhpArray arguments/*, PhpArray options, PhpArray input_headers, PhpArray output_headers*/)
        {
            try
            {
                if (arguments.Count > 0)
                {
                    var item = arguments.GetArrayItem(0, true);

                    if (item != null && item.GetType() == typeof(PhpArray))
                    {
                        PhpArray arr = (PhpArray)item;
                        return wsp.InvokeCall(function_name, arr);
                    }
                }
            }
            catch (Exception exception)
            {
                SoapFault.Throw(ScriptContext.CurrentContext, "SOAP-ERROR", exception.Message, exceptions);
            }

            return null;
        }
示例#18
0
        private static object BindObject(object obj, Type type)
        {
            FieldInfo[] fi = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
            var runtimeFields = new PhpArray(fi.Length);
            object value;
            bool specified = true;
            FieldInfo field;

            for (int i = 0; i < fi.Length; ++i)
            {
                field = fi[i];

                specified = true;

                if (i + 1 < fi.Length && Attribute.IsDefined(fi[i + 1], typeof(XmlIgnoreAttribute)))
                {
                    value = fi[i + 1].GetValue(obj);
                    if (value == null)
                        specified = false;
                    else
                        specified = (bool)value;

                    i++;
                }

                if (specified)
                {
                    value = Bind(field.GetValue(obj), field);
                    if (value != null)
                        runtimeFields.Add(field.Name, value);
                }
            }

            return new stdClass()
            {
                RuntimeFields = runtimeFields
            };
        }
示例#19
0
		public static bool KeyExists(object key, PhpArray array)
		{
			if (array == null)
			{
				PhpException.ArgumentNull("array");
				return false;
			}

			IntStringKey array_key;
			if (Core.Convert.ObjectToArrayKey(key, out array_key))
				return array.ContainsKey(array_key);
				
			PhpException.Throw(PhpError.Warning, CoreResources.GetString("illegal_offset_type")); 
			return false;
		}
示例#20
0
		public static PhpArray GetDefinedFunctions()
		{
			PhpArray result = new PhpArray(0, 2);
			PhpArray library = new PhpArray(0, 500);
			PhpArray user = new PhpArray();

			ScriptContext.CurrentContext.GetDeclaredFunctions(user, library);

			result["internal"] = library;
			result["user"] = user;
			return result;
		}
示例#21
0
		public static PhpArray GetGenericArgs()
		{
			DTypeDesc[] type_descs = ScriptContext.CurrentContext.Stack.GetTypeArguments();
			if (type_descs == null) return null;

			PhpArray result = new PhpArray(type_descs.Length, 0);

			foreach (DTypeDesc type_desc in type_descs)
				result.Add(type_desc.MakeFullName());

			result.InplaceCopyOnReturn = true;
			return result;
		}
示例#22
0
        /// <summary>
        /// Gets an array of modifier names contained in modifiers flags.
        /// </summary>
        //[ImplementsMethod]
        public static PhpArray getModifierNames(int modifiers)
        {
            PhpArray result = new PhpArray();
            Modifiers flags = (Modifiers)modifiers;

            if ((flags & (Modifiers.Abstract | Modifiers.AbstractClass)) != 0)
                result.Add("abstract");

            if ((flags & (Modifiers.Abstract | Modifiers.AbstractClass)) != 0)
                result.Add("final");

            switch (flags & Modifiers.VisibilityMask)
            {
                case Modifiers.Public: result.Add("public"); break;
                case Modifiers.Protected: result.Add("protected"); break;
                case Modifiers.Private: result.Add("private"); break;
            }

            if ((flags & Modifiers.Static) != 0)
                result.Add("static");

            return result;
        }
示例#23
0
		public static object Search(object needle, PhpArray haystack, bool strict)
		{
			// result needn't to be deeply copied because it is a key of an array //

			if (haystack == null)
			{
				PhpException.ArgumentNull("haystack");
				return false;
			}

			// using operator ===:
			if (strict)
			{
                using (var enumerator = haystack.GetFastEnumerator())
                    while (enumerator.MoveNext())
                    {
                        // dereferences value (because of StrictEquality operator):
                        object val = PhpVariable.Dereference(enumerator.CurrentValue);

                        if (Operators.StrictEquality(needle, val))
                            return enumerator.CurrentKey.Object;
                    }
			}
			else
			{
				// using operator ==:

                using (var enumerator = haystack.GetFastEnumerator())
                    while (enumerator.MoveNext())
                    {
                        // comparator manages references well:
                        if (PhpComparer.CompareEq(needle, enumerator.CurrentValue))
                            return enumerator.CurrentKey.Object;
                    }
			}

			// not found:
			return false;
		}
示例#24
0
		public static object Search(object needle, PhpArray haystack)
		{
			return Search(needle, haystack, false);
		}
示例#25
0
		public static bool InArray(object needle, PhpArray haystack, bool strict)
		{
			object b = Search(needle, haystack, strict);
			return !(b is bool) || (bool)b;
		}
示例#26
0
		public static bool InArray(object needle, PhpArray haystack)
		{
			object b = Search(needle, haystack, false);
			return !(b is bool) || (bool)b;
		}
示例#27
0
		public static bool KeyExistsObsolete(object key, PhpArray array)
		{
			return KeyExists(key, array);
		}
示例#28
0
		public static object RandomKeys(PhpArray array, int count)
		{
			if (count == 1)
			{
				ArrayList result = new ArrayList(1);
				return RandomSubset(((IDictionary)array).Keys, result, count, PhpMath.Generator) ? result[0] : null;
			}
			else
			{
				PhpArray result = new PhpArray(count > 0 ? count : 0, 0);
				if (RandomSubset(((IDictionary)array).Keys, result, count, PhpMath.Generator))
				{
					result.InplaceCopyOnReturn = true;
					return result;
				}
				else
				{
					return null;
				}
			}
		}
示例#29
0
		public static object RandomKeys(PhpArray array)
		{
			return RandomKeys(array, 1);
		}
示例#30
0
		/// <summary>
		/// Implementation of <see cref="Splice(PhpArray,int,int,object)"/> and <see cref="SpliceDc(PhpArray,int,int,object)"/>.
		/// </summary>
		/// <remarks>Whether to make a deep-copy of items in the replacement.</remarks>
		internal static PhpArray SpliceInternal(PhpArray array, int offset, int length, object replacement, bool deepCopy)
		{
			Debug.Assert(array != null);
			int count = array.Count;

			// converts offset and length to interval [first,last]:
			PhpMath.AbsolutizeRange(ref offset, ref length, count);

            PhpArray result = new PhpArray(length);
			PhpArray r_array = replacement as PhpArray;

			// replacement is an array:
			if (r_array != null)
			{
				// provides deep copies:
				IEnumerable<object> e;

				if (deepCopy)
					e = PhpVariable.EnumerateDeepCopies<object>(r_array.Values);
				else
					e = r_array.Values;

				// does replacement:
				array.ReindexAndReplace(offset, length, e, result);
			}
			else if (replacement != null)
			{
				// replacement is another type //

				// creates a deep copy:
				if (deepCopy) replacement = PhpVariable.DeepCopy(replacement);

				// does replacement:
				array.ReindexAndReplace(offset, length, new object[] { replacement }, result);
			}
			else
			{
				// replacement is null:

				array.ReindexAndReplace(offset, length, null, result);
			}

			return result;
		}