Пример #1
0
        /// <summary>
        /// Adds a parameter to variable binding.
        /// </summary>
        /// <param name="parameter">SQL parameter.</param>
        /// <param name="variable">Passed PHP variable.</param>
        /// <param name="type">Parameter type specified by user.</param>
        /// <returns><B>true</B> if the binding succeeded.</returns>
        public bool AddBinding(IDataParameter /*!*/ parameter, PhpAlias /*!*/ variable, ParameterType type)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            if (type < ParameterType.String || type > ParameterType.Infer)
            {
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            if (Bindings.ContainsKey(parameter.ParameterName))
            {
                return(false);
            }

            Bindings.Add(parameter.ParameterName, new Binding(variable, parameter, type));
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Binds a PHP variable to an SQL parameter of a statement.
        /// </summary>
        /// <param name="statement">Statement resource.</param>
        /// <param name="parameterName">Parameter name starting with '@' character.</param>
        /// <param name="variable">PHP variable to bind to the parameter.</param>
        /// <param name="type">SQL type of the parameter.</param>
        /// <param name="isOutput">Whether the parameter is an output parameter.</param>
        /// <param name="isNullable">Whether the parameter accepts <B>null</B> values.</param>
        /// <param name="maxLength">Maximum size of input data.</param>
        /// <returns>Whether binding succeeded.</returns>
        public static bool mssql_bind(PhpResource statement, string parameterName, PhpAlias variable, VariableType type,
                                      bool isOutput = false, bool isNullable = false, int maxLength = -1)
        {
            PhpSqlDbProcedure procedure = PhpSqlDbProcedure.ValidProcedure(statement);

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

            if (parameterName == null)
            {
                PhpException.ArgumentNull(nameof(parameterName));
                return(false);
            }

            var param_type = PhpSqlDbProcedure.VariableTypeToParamType(type);

            if (param_type == PhpSqlDbProcedure.ParameterType.Invalid)
            {
                PhpException.ArgumentValueNotSupported("type", (int)type);
                return(false);
            }

            SqlParameter parameter = new SqlParameter();

            parameter.ParameterName = parameterName;

            // it is necessary to set size for in-out params as the results are truncated to this size;
            // 8000 is maximal size of the data according to the doc:
            if (maxLength >= 0)
            {
                parameter.Size = maxLength;
            }
            else
            {
                parameter.Size = 8000;
            }

            if (string.Equals(parameterName, "RETVAL", StringComparison.OrdinalIgnoreCase))
            {
                parameter.Direction = ParameterDirection.ReturnValue;
            }
            else if (isOutput)
            {
                parameter.Direction = ParameterDirection.InputOutput;
            }
            else
            {
                parameter.Direction = ParameterDirection.Input;
            }

            if (!procedure.AddBinding(parameter, variable, param_type))
            {
                PhpException.Throw(PhpError.Notice, Resources.parameter_already_bound, parameterName);
                return(false);
            }

            return(true);
        }
Пример #3
0
        /// <summary>
        /// This function parses an XML string into 2 parallel array structures, one (index) containing
        /// pointers to the location of the appropriate values in the values array. These last two
        /// parameters must be passed by reference.
        /// </summary>
        /// <param name="parser">A reference to the XML parser. </param>
        /// <param name="data">A string containing the XML data. </param>
        /// <param name="values">An array containing the values of the XML data.</param>
        /// <param name="index">
        /// An array containing pointers to the location of the appropriate values in the $values.
        /// </param>
        /// <returns>
        /// Returns 0 for failure and 1 for success. This is not the same as FALSE and TRUE, be careful
        /// with operators such as ===.
        /// </returns>
        public static int xml_parse_into_struct(PhpResource parser, string data, PhpAlias values, PhpAlias index = null)
        {
            if (values == null)
            {
                PhpException.Throw(PhpError.Warning, "values argument should not be null");
                return(0);
            }

            var xmlParser = XmlParserResource.ValidResource(parser);

            if (xmlParser != null)
            {
                var values_arr = new PhpArray();
                values.Value = (PhpValue)values_arr;

                PhpArray index_arr;
                if (index != null)
                {
                    index.Value = (PhpValue)(index_arr = new PhpArray());
                }
                else
                {
                    index_arr = null;
                }

                return(xmlParser.ParseIntoStruct(data, values_arr, index_arr) ? 1 : 0);
            }

            PhpException.Throw(PhpError.Warning, "parser argument should contain valid XML parser");
            return(0);
        }
Пример #4
0
                public override void Accept(PhpAlias value)
                {
                    ++_seq;

                    if (value.ReferenceCount == 0)
                    {
                        value.Value.Accept(this);
                    }
                    else
                    {
                        if (serializedRefs.TryGetValue(value, out var seq))
                        {
                            // this reference has already been serialized -> write out its seq. number
                            Write(Tokens.Reference);
                            Write(Tokens.Colon);
                            Write(seq.ToString());
                            Write(Tokens.Semicolon);
                        }
                        else
                        {
                            serializedRefs[value] = _seq;
                            value.Value.Accept(this);
                        }
                    }
                }
Пример #5
0
 public static int fscanf(PhpResource handle, string format, PhpAlias arg, params PhpAlias[] arguments)
 {
     //PhpStream stream = PhpStream.GetValid(handle);
     //if (stream == null) return -1;
     //string line = stream.ReadLine(-1, null);
     //return Strings.sscanf(line, format, arg, arguments);
     throw new NotImplementedException();
 }
Пример #6
0
            public Binding(PhpAlias /*!*/ variable, IDataParameter /*!*/ parameter, ParameterType type)
            {
                Debug.Assert(variable != null && parameter != null && type != ParameterType.Invalid);

                this.Variable  = variable;
                this.Parameter = parameter;
                this.Type      = type;
            }
Пример #7
0
            public override void Accept(PhpAlias obj)
            {
                if (Enter(obj))
                {
                    _output.Append("&");
                    base.Accept(obj);

                    //
                    Leave(obj);
                }
                else
                {
                    // *RECURSION*
                    _output.Append(RECURSION);
                }
            }
Пример #8
0
        public sealed override TextElement /*PhpFilter.*/ Filter(IEncodingProvider enc, TextElement input, bool closing)
        {
            var @in = new UserFilterBucketBrigade()
            {
                bucket = input.ToPhpString()
            };
            var @out     = new UserFilterBucketBrigade();
            var consumed = new PhpAlias(0L);

            switch ((PhpFilters.FilterStatus)filter(@in, @out, consumed, closing))
            {
            case PhpFilters.FilterStatus.OK:
                return(new TextElement(@out.bucket, enc.StringEncoding));

            case PhpFilters.FilterStatus.MoreData:
                return(TextElement.Empty);

            case PhpFilters.FilterStatus.FatalError:
            default:
                // silently stop feeding this filter
                return(TextElement.Null);
            }
        }
Пример #9
0
 /// <summary>
 /// Binds a parameter to the specified variable name
 /// </summary>
 /// <param name="parameter">Parameter identifier.
 /// For a prepared statement using named placeholders, this will be a parameter name of the form <c>:name</c>.
 /// For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.</param>
 /// <param name="variable">Name of the PHP variable to bind to the SQL statement parameter.</param>
 /// <param name="data_type">Explicit data type for the parameter using the PDO::PARAM_* constants. To return an INOUT parameter from a stored procedure, use the bitwise OR operator to set the PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter.</param>
 /// <param name="length">Length of the data type. To indicate that a parameter is an OUT parameter from a stored procedure, you must explicitly set the length.</param>
 /// <param name="driver_options"></param>
 /// <returns>Returns TRUE on success or FALSE on failure.</returns>
 public virtual bool bindParam(IntStringKey parameter, PhpAlias variable, PARAM data_type = PARAM.PARAM_STR, int?length = default, PhpValue driver_options = default)
 {
     return(StoreParameter(ref bound_params, parameter, variable, data_type));
 }
Пример #10
0
 /// <summary>
 /// Bind a column to a PHP variable.
 /// </summary>
 /// <param name="column">
 /// Number of the column (1-indexed) or name of the column in the result set.
 /// If using the column name, be aware that the name should match the case of the column, as returned by the driver</param>
 /// <param name="param">PHP variable to which the column will be bound.</param>
 /// <param name="type">Data type of the parameter, specified by the PDO::PARAM_* constants.</param>
 /// <param name="maxlen">A hint for pre-allocation.</param>
 /// <param name="driverdata">Optional parameter(s) for the driver.</param>
 /// <returns>Returns TRUE on success or FALSE on failure</returns>
 public virtual bool bindColumn(IntStringKey column, PhpAlias param, PARAM?type = default, int maxlen = 0, PhpValue driverdata = default)
 {
     return(StoreParameter(ref bound_columns, column, param, type));
 }
Пример #11
0
        public static PhpString exif_thumbnail(Context ctx, string filename, PhpAlias width = null, PhpAlias height = null, PhpAlias imagetype = null)
        {
            if (string.IsNullOrEmpty(filename))
            {
                PhpException.Throw(PhpError.Warning, Resources.filename_cannot_be_empty);
                return(default(PhpString));
            }

            Image <Rgba32> thumbnail = null;

            IImageFormat format = null;

            byte[] result;

            var bytes = Utils.ReadPhpBytes(ctx, filename);

            if (bytes == null)
            {
                return(default(PhpString));
            }

            // get thumbnail from <filename>'s content:
            using (var ms = new MemoryStream(bytes))
            {
                try
                {
                    // TODO: Image.Identify needs a new overload that returns the format.
                    using (var image = Image.Load(ms, out format))
                    {
                        // return byte[] ~ image.MetaData.ExifProfile{ this.data, this.thumbnailOffset, this.thumbnailLength }
                        thumbnail = image.Metadata.ExifProfile.CreateThumbnail <Rgba32>();
                    }
                }
                catch
                {
                    return(default(PhpString));
                }
            }

            if (thumbnail == null)
            {
                return(default(PhpString));
            }

            //
            if (width != null)
            {
                width.Value = (PhpValue)thumbnail.Width;
            }

            if (height != null)
            {
                height.Value = (PhpValue)thumbnail.Height;
            }

            if (imagetype != null)  // TODO: get thumbnail image format
            {
                imagetype.Value = (PhpValue)(format == JpegFormat.Instance ? PhpImage.IMAGETYPE_JPEG : PhpImage.IMAGETYPE_TIFF_II);
            }

            using (var ms2 = new MemoryStream())
            {
                thumbnail.Save(ms2, new PngEncoder());
                result = ms2.ToArray();
            }

            thumbnail.Dispose();

            //
            return(new PhpString(result));
        }
Пример #12
0
                PhpAlias /*!*/ AddSeq()
                {
                    _lazyObjects ??= new Dictionary <int, PhpAlias>();

                    return(_lazyObjects[_lazyObjects.Count + 1] = PhpAlias.Create(PhpValue.Null));
                }
Пример #13
0
 public override void Accept(PhpAlias obj)
 {
     _output.Append("&");
     base.Accept(obj);
 }
Пример #14
0
        public void setStaticPropertyValue(Context ctx, string name, PhpAlias def_value)
        {
            var prop = _tinfo.GetDeclaredProperty(name) ?? throw new ReflectionException();

            prop.SetValue(ctx, null, def_value);
        }
Пример #15
0
 private ReflectionReference(PhpAlias alias)
 {
     _alias = alias ?? throw new ArgumentNullException(nameof(alias));
 }
Пример #16
0
 public virtual PhpValue fscanf(string format, PhpAlias arg, params PhpAlias[] args) => PhpPath.fscanf(_stream, format, arg, args);
Пример #17
0
        public static PhpString exif_thumbnail(Context ctx, string filename, PhpAlias width = null, PhpAlias height = null, PhpAlias imagetype = null)
        {
            if (string.IsNullOrEmpty(filename))
            {
                PhpException.Throw(PhpError.Warning, Resources.filename_cannot_be_empty);
                return(null);
            }

            Image <Color> thumbnail = null;

            byte[] result;

            var bytes = Utils.ReadPhpBytes(ctx, filename);

            if (bytes == null)
            {
                return(null);
            }

            // get thumbnail from <filename>'s content:
            using (var ms = new MemoryStream(bytes))
            {
                try
                {
                    using (var image = Image.Load(ms))
                    {
                        // return byte[] ~ image.MetaData.ExifProfile{ this.data, this.thumbnailOffset, this.thumbnailLength }
                        thumbnail = image.MetaData.ExifProfile.CreateThumbnail <Color>();
                    }
                }
                catch
                {
                    return(null);
                }
            }

            if (thumbnail == null)
            {
                return(null);
            }

            //
            if (width != null)
            {
                width.Value = (PhpValue)thumbnail.Width;
            }

            if (height != null)
            {
                height.Value = (PhpValue)thumbnail.Height;
            }

            if (imagetype != null)
            {
                imagetype.Value = (PhpValue)((thumbnail.CurrentImageFormat.Decoder is ImageSharp.Formats.JpegDecoder)
                    ? PhpImage.IMAGETYPE_JPEG : PhpImage.IMAGETYPE_TIFF_II);
            }

            using (var ms2 = new MemoryStream())
            {
                thumbnail.Save(ms2, new ImageSharp.Formats.PngEncoder());
                result = ms2.ToArray();
            }

            thumbnail.Dispose();

            //
            return(new PhpString(result));
        }
Пример #18
0
        /// <inheritDoc />
        public bool bindParam(PhpValue parameter, PhpAlias variable, PDO.PARAM data_type = PDO.PARAM.PARAM_STR, int length = -1, PhpValue driver_options = default(PhpValue))
        {
            Debug.Assert(this.m_cmd != null);

            // lazy instantization
            if (m_boundParams == null)
            {
                m_boundParams = new Dictionary <string, PhpAlias>();
            }

            IDbDataParameter param = null;

            if (m_namedAttr)
            {
                // Mixed parameters not allowed
                if (m_positionalAttr)
                {
                    m_pdo.HandleError(new PDOException("Mixed parameters mode not allowed. Use either only positional, or only named parameters."));
                    return(false);
                }

                string key = parameter.AsString();
                if (key == null)
                {
                    m_pdo.HandleError(new PDOException("Supplied parameter name must be a string."));
                    return(false);
                }

                if (key.Length > 0 && key[0] == ':')
                {
                    key = key.Substring(1);
                }

                //Store the bounded variable reference in the dictionary
                m_boundParams.Add(key, variable);

                param = m_cmd.Parameters[key];
            }
            else if (m_positionalAttr)
            {
                if (!parameter.IsInteger())
                {
                    m_pdo.HandleError(new PDOException("Supplied parameter index must be an integer."));
                    return(false);
                }
                int paramIndex = (int)parameter;

                //Store the bounded variable.Value reference in the dictionary
                m_boundParams.Add(paramIndex.ToString(), variable);

                if (paramIndex < m_positionalPlaceholders.Count)
                {
                    param = m_cmd.Parameters[paramIndex];
                }
            }
            else
            {
                m_pdo.HandleError(new PDOException("No parameter mode set yet for this Statement. Possibly no parameters required?"));
                return(false);
            }

            if (param == null)
            {
                m_pdo.HandleError(new PDOException("No matching parameter found."));
                return(false);
            }

            switch (data_type)
            {
            case PDO.PARAM.PARAM_INT:
                if (variable.Value.IsInteger())
                {
                    param.DbType = DbType.Int32;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_STR:
                string str = null;
                if ((str = variable.Value.ToStringOrNull()) != null)
                {
                    param.DbType = DbType.String;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_BOOL:
                if (variable.Value.IsBoolean())
                {
                    param.DbType = DbType.Boolean;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_LOB:
                byte[] bytes = null;
                if ((bytes = variable.Value.ToBytesOrNull()) != null)
                {
                    param.DbType = DbType.Binary;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            // Currently not supported by any drivers
            case PDO.PARAM.PARAM_NULL:
            case PDO.PARAM.PARAM_STMT:
                throw new NotImplementedException();
            }

            return(true);
        }
Пример #19
0
 /// <summary>
 /// Called when applying the filter.
 /// </summary>
 public virtual long filter(PhpResource @in, PhpResource @out, PhpAlias consumed, bool closing) => 0;
Пример #20
0
 public PhpValue getStaticPropertyValue(string name, PhpAlias def_value)
 {
     throw new NotImplementedException();
 }
 public override bool index(string query, string remote, string name, PhpAlias c)
 {
     c.Value.Array.Add("server", "SharpFlashDetector Server");
     return(true);
 }