Base class for PHP stream filters.
Inheritance: IFilter
Example #1
0
        /// <summary>Adds <paramref name="filter"/> to the list of filters attached to <paramref name="stream"/>.</summary>
        /// <param name="stream">The target stream.</param>
        /// <param name="filter">The filter name.</param>
        /// <param name="read_write">Combination of the <see cref="FilterChainOptions"/> flags.</param>
        /// <param name="parameters">Additional parameters for a user filter.</param>
        public static bool stream_filter_prepend(PhpResource stream, string filter, FilterChainOptions read_write, PhpValue parameters)
        {
            var s = PhpStream.GetValid(stream);

            if (s == null)
            {
                return(false);
            }

            var where = (FilterChainOptions)read_write & FilterChainOptions.ReadWrite;
            return(PhpFilter.AddToStream(s, filter, where | FilterChainOptions.Head, parameters));
        }
Example #2
0
        /// <summary>
        /// Insert the filter into the filter chains.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="stream">Which stream's filter chains.</param>
        /// <param name="filter">What filter.</param>
        /// <param name="where">What position in the chains.</param>
        /// <param name="parameters">Additional parameters for the filter.</param>
        /// <returns>Filters that have been added.</returns>
        internal static (PhpFilter readFilter, PhpFilter writeFilter) AddToStream(Context ctx, PhpStream stream, string filter, FilterChainOptions where, PhpValue parameters)
        {
            if ((stream.Options & StreamAccessOptions.Read) == 0)
            {
                where &= ~FilterChainOptions.Read;
            }
            if ((stream.Options & StreamAccessOptions.Write) == 0)
            {
                where &= ~FilterChainOptions.Write;
            }

            PhpFilter readFilter = null, writeFilter = null;

            if ((where & FilterChainOptions.Read) != 0)
            {
                if (GetFilter(ctx, filter, true, out readFilter, parameters))
                {
                    stream.AddFilter(readFilter, where);
                    readFilter.OnCreate();
                    // Add to chain, (filters buffers too).
                }
                else
                {
                    PhpException.Throw(PhpError.Warning, Core.Resources.ErrResources.invalid_filter_name, filter);
                    //return false;
                    throw new ArgumentException(nameof(filter));
                }
            }

            if ((where & FilterChainOptions.Write) != 0)
            {
                if (GetFilter(ctx, filter, true, out writeFilter, parameters))
                {
                    stream.AddFilter(writeFilter, where);
                    writeFilter.OnCreate();
                    // Add to chain.
                }
                else
                {
                    PhpException.Throw(PhpError.Warning, Core.Resources.ErrResources.invalid_filter_name, filter);
                    //return false;
                    throw new ArgumentException(nameof(filter));
                }
            }

            return(readFilter, writeFilter);
        }
Example #3
0
        public static PhpResource stream_filter_prepend(Context ctx, PhpResource stream, string filter, FilterChainOptions read_write = FilterChainOptions.ReadWrite, PhpValue parameters = default)
        {
            var s = PhpStream.GetValid(stream);
            if (s == null) return null; // false;

            var where = read_write & FilterChainOptions.ReadWrite;
            var added = PhpFilter.AddToStream(ctx, s, filter, where | FilterChainOptions.Head, parameters);

            //
            if (added.readFilter != null || added.writeFilter != null)
            {
                return new StreamFilterResource(s, added.writeFilter, added.readFilter);
            }
            else
            {
                return null; // false
            }
        }
Example #4
0
        /// <summary>
        /// Searches for a filter implementation in the known <see cref="PhpFilter"/> descendants.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="filter">The name of the filter (may contain wildcards).</param>
        /// <param name="instantiate"><c>true</c> to fille <paramref name="instance"/> with a new instance of that filter.</param>
        /// <param name="instance">Filled with a new instance of an implemented filter if <paramref name="instantiate"/>.</param>
        /// <param name="parameters">Additional parameters for the filter.</param>
        /// <returns><c>true</c> if a filter with the given name was found.</returns>
        internal static bool GetFilter(Context ctx, string filter, bool instantiate, out PhpFilter instance, PhpValue parameters)
        {
            foreach (var factory in _filterFactories)
            {
                if (factory.GetImplementedFilter(ctx, filter, instantiate, out instance, parameters))
                {
                    if (instance != null)
                    {
                        instance.filtername = filter;
                        instance.@params    = parameters.DeepCopy();
                    }

                    return(true);
                }
            }

            instance = null;
            return(false);
        }
        public bool GetImplementedFilter(Context ctx, string name, bool instantiate, out PhpFilter instance, PhpValue parameters)
        {
            instance = null;

            switch (name)
            {
            case "zlib.deflate":
                if (instantiate)
                {
                    instance = new DeflateFilter(-1, DeflateFilterMode.Normal);
                }
                return(true);

            case "zlib.inflate":
                if (instantiate)
                {
                    instance = new InflateFilter();
                }
                return(true);
            }

            return(false);
        }
Example #6
0
 public StreamFilterResource(PhpStream stream, PhpFilter writeFilter, PhpFilter readFilter) : base("stream filter")
 {
     this.Stream      = stream;
     this.WriteFilter = writeFilter;
     this.ReadFilter  = readFilter;
 }
Example #7
0
 /// <summary>
 /// Retrieves the list of registered filters.
 /// </summary>
 /// <returns>A <see cref="PhpArray"/> containing the names of available filters. Cannot be <c>null</c>.</returns>
 public static PhpArray stream_get_filters()
 {
     return(new PhpArray(PhpFilter.GetFilterNames().Select(PhpValue.Create)));
 }
Example #8
0
        /// <summary>
        /// Registers a user stream filter.
        /// </summary>
        /// <param name="filter">The name of the filter (may contain wildcards).</param>
        /// <param name="classname">The PHP user class (derived from <c>php_user_filter</c>) implementing the filter.</param>
        /// <returns><c>true</c> if the filter was succesfully added, <c>false</c> if the filter of such name already exists.</returns>
        public static bool stream_filter_register(string filter, string classname)
        {
            // EX: [stream_filter_register]

            return(PhpFilter.AddUserFilter(filter, classname));
        }
Example #9
0
            public bool GetImplementedFilter(Context ctx, string name, bool instantiate, out PhpFilter instance, PhpValue parameters)
            {
                instance = null;

                for (int i = 0; i < _filters.Count; i++)
                {
                    var pair = _filters[i];

                    // TODO: wildcard
                    if (pair.Key.EqualsOrdinalIgnoreCase(name))
                    {
                        if (instantiate)
                        {
                            var tinfo = pair.Value as PhpTypeInfo;
                            if (tinfo == null)
                            {
                                Debug.Assert(pair.Value is string);
                                tinfo = ctx.GetDeclaredTypeOrThrow((string)pair.Value, autoload: true);

                                if (tinfo != null) // always true
                                {
                                    _filters[i] = new KeyValuePair <string, object>(pair.Key, tinfo);
                                }
                                else
                                {
                                    throw null; // unreachable
                                }
                            }

                            instance = (php_user_filter)tinfo.Creator(ctx);
                        }

                        return(true);
                    }
                }

                return(false);
            }
Example #10
0
        ///// <summary>
        ///// Insert the filter into the filter chains.
        ///// </summary>
        ///// <param name="stream">Which stream's filter chains.</param>
        ///// <param name="filter">What filter.</param>
        ///// <param name="where">What position in the chains.</param>
        ///// <param name="parameters">Additional parameters for the filter.</param>
        ///// <returns>True if successful.</returns>
        //public static bool AddToStream(PhpStream stream, string filter, FilterChainOptions where, object parameters)
        //{
        //    PhpFilter readFilter, writeFilter;

        //    if ((stream.Options & StreamAccessOptions.Read) == 0) where &= ~FilterChainOptions.Read;
        //    if ((stream.Options & StreamAccessOptions.Write) == 0) where &= ~FilterChainOptions.Write;

        //    if ((where & FilterChainOptions.Read) > 0)
        //    {
        //        if (!GetFilter(filter, true, out readFilter, parameters))
        //        {
        //            //PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_filter_name", filter));
        //            //return false;
        //            throw new ArgumentException(nameof(filter));
        //        }

        //        stream.AddFilter(readFilter, where);
        //        readFilter.OnCreate();
        //        // Add to chain, (filters buffers too).
        //    }

        //    if ((where & FilterChainOptions.Write) > 0)
        //    {
        //        if (!GetFilter(filter, true, out writeFilter, parameters))
        //        {
        //            //PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_filter_name", filter));
        //            //return false;
        //            throw new ArgumentException(nameof(filter));
        //        }

        //        stream.AddFilter(writeFilter, where);
        //        writeFilter.OnCreate();
        //        // Add to chain.
        //    }

        //    return true;
        //}

        #endregion

        #region Implemented Filters

        /// <summary>
        /// Searches for a filter implementation in the known <see cref="PhpFilter"/> descendants.
        /// </summary>
        /// <param name="filter">The name of the filter (may contain wildcards).</param>
        /// <param name="instantiate"><c>true</c> to fille <paramref name="instance"/> with a new instance of that filter.</param>
        /// <param name="instance">Filled with a new instance of an implemented filter if <paramref name="instantiate"/>.</param>
        /// <param name="parameters">Additional parameters for the filter.</param>
        /// <returns><c>true</c> if a filter with the given name was found.</returns>
        internal static bool GetFilter(string filter, bool instantiate, out PhpFilter instance, object parameters)
        {
            instance = null;

            foreach (IFilterFactory factory in systemFilters)
                if (factory.GetImplementedFilter(filter, instantiate, out instance, parameters))
                {
                    if (instance != null)
                        instance.FilterName = filter;

                    return true;
                }

            // TODO: the registered filter names may be wildcards - use fnmatch.
            string classname;
            if ((UserFilters != null) && (UserFilters.TryGetValue(filter, out classname)))
            {
                if (instantiate)
                {
                    // EX: [PhpFilter.GetFilter] create a new user filter; and support the WILDCARD naming too.
                }
                return true;
            }
            return false;
        }
Example #11
0
 /// <summary>
 /// Retrieves the list of registered filters.
 /// </summary>
 /// <returns>A <see cref="PhpArray"/> containing the names of available filters. Cannot be <c>null</c>.</returns>
 public static PhpArray stream_get_filters(Context ctx)
 {
     return(new PhpArray(PhpFilter.GetFilterNames(ctx)));
 }