/// <summary> /// Tests whether <paramref name="value"/>'s class is derived from a class given by <paramref name="class_name"/>. /// </summary> /// <param name="ctx">Runtime context.</param> /// <param name="value">The object to test.</param> /// <param name="class_name">The name of the class.</param> /// <param name="allow_string">If this parameter set to FALSE, string class name as object is not allowed. This also prevents from calling autoloader if the class doesn't exist.</param> /// <returns><B>true</B> if the object <paramref name="value"/> belongs to <paramref name="class_name"/> class or /// a class which is a subclass of <paramref name="class_name"/>, <B>false</B> otherwise.</returns> public static bool is_a(Context ctx, PhpValue value, string class_name, bool allow_string) { // first load type of {value} PhpTypeInfo tvalue; var obj = value.AsObject(); if (obj != null) { tvalue = obj.GetPhpTypeInfo(); } else { var tname = value.AsString(); if (tname != null && allow_string) { // value can be a string specifying a class name tvalue = ctx.GetDeclaredType(tname, true); } else { // invalid argument type ignored return(false); } } // second, load type of {class_name} var ctype = ctx.GetDeclaredType(class_name, false); // check is_a return(tvalue != null && ctype != null && tvalue.Type.IsSubclassOf(ctype.Type.AsType())); }
/// <summary> /// Gets or sets option. /// </summary> public static string GetSet(ref string option, string defaultValue, PhpValue newValue, IniAction action) { var oldValue = option; if (action == IniAction.Set) { option = newValue.AsString(); } return(oldValue); }
public PhpValue offsetGet(PhpValue offset) { if (offsetExists(offset)) { return(_styles[offset.AsString()]); } else { return(PhpValue.False); } }
public PhpValue offsetGet(PhpValue offset) { if (attributes.ContainsKey(offset)) { return(attributes[offset].AsPhpAlias()); } else if (offset.AsString() == "style") { styles ??= new CssBuilder(); return(PhpValue.FromClass(styles).AsPhpAlias()); } else if (offset.AsString() == "class") { classes ??= new ClassBuilder(); return(PhpValue.FromClass(classes).AsPhpAlias()); } else { throw new ArgumentException(); } }
public void __construct(Context ctx, PhpValue @class, string name) { if (name != null) { _tinfo = ReflectionUtils.ResolvePhpTypeInfo(ctx, @class); _routine = _tinfo.RuntimeMethods[name] ?? throw new ReflectionException(string.Format(Resources.Resources.method_does_not_exist, _tinfo.Name, name)); } else { __construct(ctx, @class.AsString(ctx)); } }
public virtual void __construct(Context ctx, PhpValue /*string|array*/ function, PhpValue /*string|int*/ parameter) { // resolve RoutineInfo: PhpTypeInfo declaringclass = null; RoutineInfo routine = null; var function_str = function.AsString(); if (function_str != null) { routine = ctx.GetDeclaredFunction(function_str); } else { var function_arr = function.AsArray(); if (function_arr != null && function_arr.Count == 2) { declaringclass = ReflectionUtils.ResolvePhpTypeInfo(ctx, function_arr[0]); // cannot be null routine = declaringclass.RuntimeMethods[function_arr[1].ToStringOrThrow(ctx)]; } } if (routine != null) { var func = (declaringclass == null) ? (ReflectionFunctionAbstract) new ReflectionFunction(routine) : new ReflectionMethod(declaringclass, routine); // resolve parameter: var parameters = ReflectionUtils.ResolveReflectionParameters(func, routine.Methods); var pstr = parameter.AsString(); if (pstr != null) { SetParameter(parameters.First(p => p._name == pstr)); return; } else { if (parameter.IsLong(out long index) && index < parameters.Count && index >= 0) { SetParameter(parameters[(int)index]); return; } } } throw new ReflectionException(); }
/// <inheritDoc /> public bool bindValue(PhpValue parameter, PhpValue value, int data_type = 2) { Debug.Assert(this.m_cmd != null); string key = parameter.String; if (key.Length > 0 && key[0] == ':') { key = key.Substring(1); } IDataParameter param; param = this.m_cmd.Parameters[key]; param.Value = value.AsString(); return(true); }
/// <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) - 1; 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); }
/// <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); }
/// <summary> /// Converts the character encoding of <paramref name="str"/> to <paramref name="to_encoding"/>. /// </summary> /// <param name="ctx">Runtime context.</param> /// <param name="str">Input string.</param> /// <param name="to_encoding">Target encoding.</param> /// <param name="from_encoding"> /// Encodings to try for decoding <paramref name="str"/>. /// It is either an array, or a comma separated enumerated list. If from_encoding is not specified, the internal encoding will be used. /// </param> /// <returns>Converted string.</returns> public static PhpString mb_convert_encoding(Context ctx, PhpString str, string to_encoding, PhpValue from_encoding = default(PhpValue)) { string decoded; if (str.ContainsBinaryData) { // source encoding Encoding from_enc = null; IEnumerable <Encoding> from_encs = null; if (Operators.IsSet(from_encoding)) { PhpArray from_arr; var from_str = from_encoding.AsString(); if (from_str != null) { // TODO: "auto" if (from_str.IndexOf(',') >= 0) { // comma separated list (string) from_encs = from_str.Split(',').Select(name => name.Trim()).Select(GetEncoding); } else { // string from_enc = GetEncoding(from_str); } } else if ((from_arr = from_encoding.AsArray()) != null) { // array from_encs = from_arr.Values.Select(val => val.ToString().Trim()).Select(GetEncoding); } else { throw new ArgumentException(nameof(from_encoding)); } } else { // from_encoding is default or NULL: from_enc = GetConfig(ctx).InternalEncoding ?? ctx.StringEncoding; } if (from_enc != null) { decoded = str.ToString(from_enc); } else if (from_encs != null) { decoded = null; // autodetect encoding foreach (var enc in from_encs) { if (enc != null) { try { decoded = str.ToString(Encoding.GetEncoding(enc.CodePage, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback)); break; } catch { // continue; } } } if (decoded == null) { throw new ArgumentException(); } } else { throw new InvalidOperationException(); } } else { // already in UTF16 decoded = str.ToString(); } // target encoding: var target_enc = GetEncoding(to_encoding); if (target_enc == null) { return(new PhpString(decoded)); } else { return(new PhpString(target_enc.GetBytes(decoded))); } }
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); }
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); }
public void offsetUnset(PhpValue offset) { _styles.Remove(offset.AsString()); }
public void offsetSet(PhpValue offset, PhpValue value) { _styles[offset.AsString()] = value.AsString(); }
public bool offsetExists(PhpValue offset) { return(_styles.ContainsKey(offset.AsString())); }