예제 #1
0
 /// <summary>
 /// Converts to boolean.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>The converted value.</returns>
 public static bool boolval(PhpValue variable) => variable.ToBoolean();
예제 #2
0
            public void InitializeParameter(DbParameter p)
            {
                p.Direction = (Type.HasValue && (Type.Value & PARAM.PARAM_INPUT_OUTPUT) != 0)
                    ? ParameterDirection.InputOutput
                    : ParameterDirection.Input;

                if (Variable.IsNull)
                {
                    p.Value = DBNull.Value;
                    // TODO: p.DbType
                }
                else if (Type.HasValue)
                {
                    switch (Type.Value & ~PARAM.PARAM_INPUT_OUTPUT)
                    {
                    case PARAM.PARAM_BOOL:
                        p.Value  = Variable.ToBoolean();    // TODO: boxed value
                        p.DbType = DbType.Boolean;
                        break;

                    case PARAM.PARAM_INT:
                        p.Value  = Variable.ToLongOrThrow();
                        p.DbType = DbType.Int64;
                        break;

                    case PARAM.PARAM_STR:
                        if (Variable.IsUnicodeString(out var str))
                        {
                            p.Value  = str;
                            p.DbType = DbType.String;
                        }
                        else if (Variable.IsBinaryString(out var bin))
                        {
                            p.Value  = bin.ToBytes(Encoding.UTF8);
                            p.DbType = DbType.Binary;
                        }
                        else
                        {
                            p.Value  = Variable.ToString();
                            p.DbType = DbType.String;
                        }
                        break;

                    case PARAM.PARAM_NULL:
                        p.Value = DBNull.Value;
                        // TODO: p.DbType
                        break;

                    case PARAM.PARAM_ZVAL:
                        p.Value = Variable.ToClr();
                        // TODO: p.DbType
                        break;

                    default:
                        throw new NotImplementedException(Type.Value.ToString());
                    }
                }
                else
                {
                    p.Value = Variable.ToClr();
                    // TODO: p.DbType
                }
            }
예제 #3
0
        /// <inheritDoc />
        public bool bindValue(PhpValue parameter, PhpValue value, PDO.PARAM data_type = (PDO.PARAM) PDO.PARAM_STR)
        {
            Debug.Assert(this.m_cmd != null);

            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);
                }

                param = m_cmd.Parameters[key];

                //rewrite the bounded params dictionary
                if (HasParamsBound)
                {
                    if (m_boundParams.ContainsKey(key))
                    {
                        m_boundParams.Remove(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;

                if (paramIndex < m_positionalPlaceholders.Count)
                {
                    param = m_cmd.Parameters[paramIndex];
                }

                //rewrite the bounded params dictionary
                if (HasParamsBound)
                {
                    if (m_boundParams.ContainsKey(paramIndex.ToString()))
                    {
                        m_boundParams.Remove(paramIndex.ToString());
                    }
                }
            }
            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 (value.IsInteger())
                {
                    param.DbType = DbType.Int32;
                    param.Value  = (int)value;
                }
                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 = value.ToStringOrNull()) != null)
                {
                    param.DbType = DbType.String;
                    param.Value  = str;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_BOOL:
                if (value.IsBoolean())
                {
                    param.DbType = DbType.Boolean;
                    param.Value  = value.ToBoolean();
                }
                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 = value.ToBytesOrNull()) != null)
                {
                    param.DbType = DbType.Binary;
                    param.Value  = bytes;
                }
                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:
            default:
                throw new NotImplementedException();
            }

            return(true);
        }
예제 #4
0
        internal static bool TrySetOption(this CURLResource ch, int option, PhpValue value)
        {
            switch (option)
            {
            case CURLOPT_URL: return((ch.Url = value.AsString()) != null);

            case CURLOPT_DEFAULT_PROTOCOL: return((ch.DefaultSheme = value.AsString()) != null);

            case CURLOPT_HTTPGET: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Get;
                }
                break;

            case CURLOPT_POST: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Post;
                }
                break;

            case CURLOPT_PUT: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Put;
                }
                break;

            case CURLOPT_NOBODY: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Head;
                }
                break;

            case CURLOPT_CUSTOMREQUEST: return((ch.Method = value.AsString()) != null);

            case CURLOPT_POSTFIELDS: ch.PostFields = value.GetValue().DeepCopy(); break;

            case CURLOPT_FOLLOWLOCATION: ch.FollowLocation = value.ToBoolean(); break;

            case CURLOPT_MAXREDIRS: ch.MaxRedirects = (int)value.ToLong(); break;

            case CURLOPT_REFERER: return((ch.Referer = value.AsString()) != null);

            case CURLOPT_RETURNTRANSFER: ch.ReturnTransfer = value.ToBoolean(); break;

            case CURLOPT_HEADER: ch.OutputHeader = value.ToBoolean(); break;

            case CURLOPT_HTTPHEADER: ch.Headers = value.GetValue().DeepCopy().ToArray(); break;

            case CURLOPT_COOKIE: return((ch.CookieHeader = value.AsString()) != null);

            case CURLOPT_COOKIEFILE: ch.CookieFileSet = true; break;

            case CURLOPT_FILE: return((ch.OutputTransfer = value.Object as PhpStream) != null);

            case CURLOPT_INFILE: return((ch.PutStream = value.Object as PhpStream) != null);

            case CURLOPT_USERAGENT: return((ch.UserAgent = value.AsString()) != null);

            case CURLOPT_BINARYTRANSFER: return(true);      // no effect

            case CURLOPT_PRIVATE: ch.Private = value.GetValue().DeepCopy(); return(true);

            default:
                PhpException.ArgumentValueNotSupported(nameof(option), option);
                return(false);
            }

            return(true);
        }
예제 #5
0
        static PhpValue GsrCore(Context ctx, IPhpConfigurationService config, string option, PhpValue value, IniAction action)
        {
            switch (option)
            {
            case "precision":
                // CONSIDER: this can be set in our Context.InvariantNumberFormatInfo ?
                AssertGet(option, action);
                return(15);       // default Double precision in .NET

            case "register_globals":
                AssertGet(option, action);
                return(PhpValue.False);     // always Off

            case "allow_url_fopen":
                return(GetSet(ref config.Core.AllowUrlFopen, true, value, action));

            case "include_path":
                return(GetSet(ref config.Core.IncludePaths, ".", value, action));

            case "allow_url_include":
                AssertGet(option, action);
                return(PhpValue.False);

            case "disable_functions":
            case "disable_classes":
                AssertGet(option, action);
                return(string.Empty);

            case "file_uploads":
                AssertGet(option, action);
                return(PhpValue.True);

            //case "max_file_uploads":
            //    return 20;

            case "memory_limit":
                //AssertGet(option, action); // ignore
                return(-1);    // no memory limit

            case "post_max_size":
            case "upload_max_filesize":
                AssertGet(option, action);
                return(ctx.HttpPhpContext != null ? ctx.HttpPhpContext.MaxRequestSize : (16_000_000 /*cli mode, just return something*/));

            case "docref_root":
                return(GetSet(ref config.Core.docref_root, "", value, action));

            case "docref_ext":
                return(GetSet(ref config.Core.docref_ext, "", value, action));

            case "open_basedir":
                AssertGet(option, action);
                return(string.Empty);

            case "max_execution_time":
                var max_execution_time = config.Core.ExecutionTimeout;
                if (action == IniAction.Set)
                {
                    ctx.ApplyExecutionTimeout(config.Core.ExecutionTimeout = Math.Max(0, value.ToInt()));
                }
                return(max_execution_time);

            case "display_startup_errors":
                var display_startup_errors = config.Core.DisplayStartupErrors;
                if (action == IniAction.Set)
                {
                    config.Core.DisplayStartupErrors = value.ToBoolean();
                }
                return(display_startup_errors);

            default:
                throw new ArgumentException();
            }
        }
예제 #6
0
        internal static bool TrySetOption(this CURLResource ch, int option, PhpValue value)
        {
            switch (option)
            {
            case CURLOPT_URL: return((ch.Url = value.AsString()) != null);

            case CURLOPT_DEFAULT_PROTOCOL: return((ch.DefaultSheme = value.AsString()) != null);

            case CURLOPT_HTTPGET: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Get;
                }
                break;

            case CURLOPT_POST: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Post;
                }
                break;

            case CURLOPT_PUT: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Put;
                }
                break;

            case CURLOPT_NOBODY: if (value.ToBoolean())
                {
                    ch.Method = WebRequestMethods.Http.Head;
                }
                break;

            case CURLOPT_CUSTOMREQUEST: return((ch.Method = value.AsString()) != null);

            case CURLOPT_POSTFIELDS: ch.PostFields = value.GetValue().DeepCopy(); break;

            case CURLOPT_FOLLOWLOCATION: ch.FollowLocation = value.ToBoolean(); break;

            case CURLOPT_MAXREDIRS: ch.MaxRedirects = (int)value.ToLong(); break;

            case CURLOPT_REFERER: return((ch.Referer = value.AsString()) != null);

            case CURLOPT_RETURNTRANSFER:
                ch.ProcessingResponse = value.ToBoolean()
                        ? ProcessMethod.Return
                        : ProcessMethod.StdOut;
                break;

            case CURLOPT_HEADER:
                ch.ProcessingHeaders = value.ToBoolean()
                        ? ProcessMethod.StdOut // NOTE: if ProcessingResponse is RETURN, RETURN headers as well
                        : ProcessMethod.Ignore;
                break;

            case CURLOPT_HTTPHEADER: ch.Headers = value.GetValue().DeepCopy().ToArray(); break;

            case CURLOPT_COOKIE: return((ch.CookieHeader = value.AsString()) != null);

            case CURLOPT_COOKIEFILE: ch.CookieFileSet = true; break;

            case CURLOPT_FILE: return(TryProcessMethodFromStream(value, ProcessMethod.StdOut, ref ch.ProcessingResponse));

            case CURLOPT_INFILE: return(TryProcessMethodFromStream(value, ProcessMethod.Ignore, ref ch.ProcessingRequest, readable: true));

            case CURLOPT_WRITEHEADER: return(TryProcessMethodFromStream(value, ProcessMethod.Ignore, ref ch.ProcessingHeaders));

            //case CURLOPT_STDERR: return TryProcessMethodFromStream(value, ProcessMethod.Ignore, ref ch.ProcessingErr);

            case CURLOPT_HEADERFUNCTION: return(TryProcessMethodFromCallable(value, ProcessMethod.Ignore, ref ch.ProcessingHeaders));

            case CURLOPT_WRITEFUNCTION: return(TryProcessMethodFromCallable(value, ProcessMethod.StdOut, ref ch.ProcessingResponse));

            //case CURLOPT_READFUNCTION:
            //case CURLOPT_PROGRESSFUNCTION:

            case CURLOPT_USERAGENT: return((ch.UserAgent = value.AsString()) != null);

            case CURLOPT_BINARYTRANSFER: break;       // no effect

            case CURLOPT_PRIVATE: ch.Private = value.GetValue().DeepCopy(); break;

            case CURLOPT_TIMEOUT: { if (value.IsLong(out long l))
                                    {
                                        ch.Timeout = (int)l * 1000;
                                    }
                                    break; }

            case CURLOPT_TIMEOUT_MS: { if (value.IsLong(out long l))
                                       {
                                           ch.Timeout = (int)l;
                                       }
                                       break; }

            case CURLOPT_CONNECTTIMEOUT: return(false);         // TODO: is there an alternative in .NET ?

            case CURLOPT_CONNECTTIMEOUT_MS: return(false);      // TODO: is there an alternative in .NET ?

            case CURLOPT_BUFFERSIZE:
            {
                if (value.IsLong(out long l) && l < int.MaxValue && l >= 0)
                {
                    ch.BufferSize = (int)l;
                    return(true);
                }
                return(false);
            }

            case CURLOPT_EXPECT_100_TIMEOUT_MS: { if (value.IsLong(out long l))
                                                  {
                                                      ch.ContinueTimeout = (int)l;
                                                  }
                                                  break; }

            case CURLOPT_HTTP_VERSION:
                switch ((int)value.ToLong())
                {
                case CURL_HTTP_VERSION_NONE: ch.ProtocolVersion = null; break;

                case CURL_HTTP_VERSION_1_0: ch.ProtocolVersion = HttpVersion.Version10; break;

                case CURL_HTTP_VERSION_1_1: ch.ProtocolVersion = HttpVersion.Version11; break;

                //case CURL_HTTP_VERSION_2:
                case CURL_HTTP_VERSION_2_0:
                case CURL_HTTP_VERSION_2TLS: ch.ProtocolVersion = new Version(2, 0); break;         // HttpVersion.Version20

                default: return(false);
                }
                break;

            case CURLOPT_USERNAME: ch.Username = value.ToString(); break;

            case CURLOPT_USERPWD: (ch.Username, ch.Password) = SplitUserPwd(value.ToString()); break;

            default:
                PhpException.ArgumentValueNotSupported(nameof(option), TryGetOptionName(option));
                return(false);
            }

            return(true);
        }
예제 #7
0
        public virtual bool setAttribute(PDO_ATTR attribute, PhpValue value)
        {
            long l; // temp value

            switch (attribute)
            {
            //readonly
            case PDO_ATTR.ATTR_SERVER_INFO:
            case PDO_ATTR.ATTR_SERVER_VERSION:
            case PDO_ATTR.ATTR_CLIENT_VERSION:
            case PDO_ATTR.ATTR_CONNECTION_STATUS:
            case PDO_ATTR.ATTR_DRIVER_NAME:
                return(false);

            //boolean

            case PDO_ATTR.ATTR_AUTOCOMMIT:
            case PDO_ATTR.ATTR_EMULATE_PREPARES:
                GetOrCreateAttributes()[attribute] = value.ToBoolean();
                return(true);

            case PDO_ATTR.ATTR_STRINGIFY_FETCHES:
                Driver.TrySetStringifyFetches(this, value.ToBoolean());
                return(true);    // always returns TRUE

            //strict positif integers

            case PDO_ATTR.ATTR_PREFETCH:
            case PDO_ATTR.ATTR_TIMEOUT:
                // TODO: strict positif integers
                GetOrCreateAttributes()[attribute] = value.GetValue().DeepCopy();
                return(true);

            //remaining

            case PDO_ATTR.ATTR_ERRMODE:
                l = value.ToLong();
                if (Enum.IsDefined(typeof(PDO_ERRMODE), (int)l))
                {
                    GetOrCreateAttributes()[attribute] = l;
                    return(true);
                }
                else
                {
                    // Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error: invalid error mode
                    // TODO: this.RaiseError( ... ) ?
                    PhpException.InvalidArgument(nameof(value));
                    return(false);
                }

            case PDO_ATTR.ATTR_CASE:
                l = value.ToLong();
                if (Enum.IsDefined(typeof(PDO_CASE), (int)l))
                {
                    GetOrCreateAttributes()[attribute] = l;
                    return(true);
                }
                return(false);

            case PDO_ATTR.ATTR_CURSOR:
                l = value.ToLong();
                if (Enum.IsDefined(typeof(PDO_CURSOR), (int)l))
                {
                    GetOrCreateAttributes()[attribute] = l;
                    return(true);
                }
                return(false);

            case PDO_ATTR.ATTR_DEFAULT_FETCH_MODE:
                l = value.ToLong();
                if (Enum.IsDefined(typeof(PDO_FETCH), (int)l))
                {
                    GetOrCreateAttributes()[attribute] = l;
                    return(true);
                }
                return(false);

            case PDO_ATTR.ATTR_STATEMENT_CLASS:
                if (value.IsPhpArray(out var arr) && arr != null && arr.Count != 0)
                {
                    GetOrCreateAttributes()[attribute] = arr.DeepCopy();
                    return(true);
                }
                return(false);

            case PDO_ATTR.ATTR_ORACLE_NULLS:
                if (value.IsLong(out l))
                {
                    Debug.Assert(l == (long)PDO_NULL.NULL_NATURAL, "nonstandard ATTR_ORACLE_NULLS is not yet supported");
                    _oracle_nulls = (PDO_NULL)l;
                    return(true);
                }
                else
                {
                    HandleError("attribute value must be an integer");
                    return(false);
                }

            case PDO_ATTR.ATTR_FETCH_CATALOG_NAMES:
            case PDO_ATTR.ATTR_FETCH_TABLE_NAMES:
            case PDO_ATTR.ATTR_MAX_COLUMN_LEN:
            case PDO_ATTR.ATTR_PERSISTENT:
                throw new NotImplementedException($"setAttribute({attribute})");

            //statement only
            case PDO_ATTR.ATTR_CURSOR_NAME:
                return(false);

            default:

                // driver specific
                try
                {
                    if (attribute >= PDO_ATTR.ATTR_DRIVER_SPECIFIC)
                    {
                        return(Driver.TrySetAttribute(this, GetOrCreateAttributes(), (int)attribute, value));
                    }
                }
                catch (System.Exception ex)
                {
                    this.HandleError(ex);
                    return(false);
                }

                // invalid attribute:
                Debug.WriteLine($"PDO_ATTR {attribute} is not known.");
                return(false);
            }
        }