예제 #1
0
        /// <summary>
        /// Returns Razor view rendered into a string
        /// </summary>
        public PhpValue RenderRazor(PhpValue name, PhpArray data)
        {
            object httpCtx = ctx.GetType().GetProperty("HttpContext").GetValue(ctx, null);

            if (httpCtx is HttpContext)
            {
                HttpContext        httpContext        = (HttpContext)httpCtx;
                object             viewRenderSvc      = httpContext.RequestServices.GetService(typeof(IRazorRenderService));
                ViewDataDictionary viewDataDictionary = GetData(data);
                string             partialName;

                if (name.GetValue().IsString(out partialName))
                {
                    var partial = ((IRazorRenderService)viewRenderSvc).RenderToString(partialName, viewDataDictionary);

                    if (partial != null)
                    {
                        return(partial);
                    }
                }
            }
            return("");
        }
예제 #2
0
 /// <summary>
 /// Binds a value to a parameter.
 /// </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="value">The value to bind to the parameter.</param>
 /// <param name="data_type">Explicit data type for the parameter using the PDO::PARAM_* constants.</param>
 /// <returns>Returns TRUE on success or FALSE on failure</returns>
 public virtual bool bindValue(IntStringKey parameter, PhpValue value, PARAM data_type = PARAM.PARAM_STR)
 {
     return(StoreParameter(ref bound_params, parameter, value.GetValue().DeepCopy(), data_type));
 }
예제 #3
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(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);
            }
        }
예제 #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.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_0:                                                 // == CURL_HTTP_VERSION_2:
                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;

            case CURLOPT_PROTOCOLS: ch.Protocols = (int)value.ToLong(); break;

            case CURLOPT_REDIR_PROTOCOLS:
                PhpException.ArgumentValueNotSupported(nameof(option), nameof(CURLOPT_REDIR_PROTOCOLS));
                break;

            case CURLOPT_SSL_VERIFYHOST:
            case CURLOPT_SSL_VERIFYPEER:
            case CURLOPT_SSL_VERIFYSTATUS:
                // always enabled
                break;

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

            return(true);
        }
예제 #5
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_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

            default:
                return(false);
            }

            return(true);
        }