/// <summary> /// Returns the value at specified offset. /// </summary> public PhpValue offsetGet(PhpValue offset) { var i = offset.ToLong(); IndexCheckHelper(i); return(_array[i]); }
/// <summary> /// Assigns a value to the specified offset. /// </summary> public void offsetSet(PhpValue offset, PhpValue value) { var i = offset.ToLong(); IndexCheckHelper(i); _array[i] = value; }
/// <summary> /// Converts to integer using a specified base. /// </summary> /// <param name="variable">The variable.</param> /// <param name="base">The base.</param> /// <returns>The converted value.</returns> public static long intval(PhpValue variable, int @base) { // TODO: base // The integer value of var on success, or 0 on failure. Empty arrays and objects return 0, non-empty arrays and objects return 1. // The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807. return(variable.ToLong()); }
/// <summary> /// Gets a contents of a specified cell from a specified query result resource. /// </summary> /// <param name="resultHandle">Query result resource.</param> /// <param name="row">Row index.</param> /// <param name="field">Column (field) integer index or string name.</param> /// <returns>The value of the cell or a <B>null</B> reference (<B>false</B> in PHP) on failure (invalid resource or row/field index/name).</returns> /// <remarks> /// Result is affected by run-time quoting. /// </remarks> public static PhpValue mysql_result(PhpResource resultHandle, int row, PhpValue field) { var result = MySqlResultResource.ValidResult(resultHandle); if (result == null) { return(PhpValue.False); } string field_name; object field_value; if (field.IsEmpty) { field_value = result.GetFieldValue(row, result.CurrentFieldIndex); } else if ((field_name = PhpVariable.AsString(field)) != null) { field_value = result.GetFieldValue(row, field_name); } else { field_value = result.GetFieldValue(row, (int)field.ToLong()); } return(PhpValue.FromClr(field_value)); // TODO: Core.Convert.Quote(field_value, context); }
/// <summary> /// Gets a contents of a specified cell from a specified query result resource. /// </summary> /// <param name="ctx">PHP context.</param> /// <param name="resultHandle">Query result resource.</param> /// <param name="rowIndex">Row index.</param> /// <param name="field">Column (field) integer index or string name.</param> /// <returns>The value of the cell or <B>false</B> on failure (invalid resource or row index).</returns> public static PhpValue mssql_result(Context ctx, PhpResource resultHandle, int rowIndex, PhpValue field) { var result = PhpSqlDbResult.ValidResult(resultHandle); if (result == null) { return(PhpValue.False); } string field_name; object field_value; if (field.IsNull) { field_value = result.GetFieldValue(rowIndex, result.CurrentFieldIndex); } else if ((field_name = PhpVariable.AsString(field)) != null) { field_value = result.GetFieldValue(rowIndex, field_name); } else { field_value = result.GetFieldValue(rowIndex, (int)field.ToLong()); } if (field_value == null) { return(PhpValue.False); } return(PhpValue.FromClr(field_value)); // Core.Convert.Quote(field_value, context); }
/// <summary> /// Whether an offset exists. /// </summary> /// <remarks>This method is executed when using isset() or empty().</remarks> public bool offsetExists(PhpValue offset) { var i = offset.ToLong(); var array = _array; return(array != null && i >= 0 && i < array.Length && array[i].HasValue); }
/// <summary> /// Gets or sets option. /// </summary> public static int GetSet(ref int option, int defaultValue, PhpValue newValue, IniAction action) { var oldValue = option; if (action == IniAction.Set) { option = (int)newValue.ToLong(); } return(oldValue); }
/// <summary> /// Sets variable's type. /// </summary> /// <param name="ctx">Current runtime context.</param> /// <param name="variable">The variable.</param> /// <param name="type">The string identifying a new type. See PHP manual for details.</param> /// <returns>Whether <paramref name="type"/> is valid type identifier.</returns> /// <exception cref="PhpException"><paramref name="type"/> has invalid value.</exception> public static bool settype(Context ctx, ref PhpValue variable, string type) { switch (type.ToLowerInvariant()) { case "bool": case "boolean": variable = PhpValue.Create(variable.ToBoolean()); return(true); case "int": case "integer": variable = PhpValue.Create(variable.ToLong()); return(true); case "float": case "double": variable = PhpValue.Create(variable.ToDouble()); return(true); case "string": if (variable.TypeCode != PhpTypeCode.MutableString) // already a string with possible binary data { variable = PhpValue.Create(variable.ToString(ctx)); } return(true); case "array": variable = PhpValue.Create(variable.ToArray()); return(true); case "object": variable = PhpValue.FromClass(variable.ToClass()); return(true); case "null": variable = PhpValue.Null; return(true); } PhpException.InvalidArgument(nameof(type), Resources.LibResources.invalid_type_name); return(false); }
/// <summary> /// Sets variable's type. /// </summary> /// <param name="ctx">Current runtime context.</param> /// <param name="variable">The variable.</param> /// <param name="type">The string identifying a new type. See PHP manual for details.</param> /// <returns>Whether <paramref name="type"/> is valid type identifier.</returns> /// <exception cref="PhpException"><paramref name="type"/> has invalid value.</exception> public static bool settype(Context ctx, ref PhpValue variable, string type) { switch (type.ToLowerInvariant()) { case "bool": case "boolean": variable = PhpValue.Create(variable.ToBoolean()); return(true); case "int": case "integer": variable = PhpValue.Create(variable.ToLong()); return(true); case "float": case "double": variable = PhpValue.Create(variable.ToDouble()); return(true); case "string": variable = PhpValue.Create(variable.ToString(ctx)); return(true); case "array": variable = PhpValue.Create(variable.AsArray()); return(true); case "object": variable = PhpValue.FromClass(variable.ToClass()); return(true); case "null": variable = PhpValue.Null; return(true); } //PhpException.InvalidArgument("type", LibResources.GetString("invalid_type_name")); //return false; throw new ArgumentException(nameof(type)); }
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 = Utilities.DbValueHelper.AsObject(Variable.ToBoolean()); p.DbType = DbType.Boolean; break; case PARAM.PARAM_INT: p.Value = Variable.ToLong(); 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 } }
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); }
public bool setAttribute(PDO_ATTR attribute, PhpValue value) { try { if ((int)attribute >= (int)PDO_ATTR.ATTR_DRIVER_SPECIFIC) { return(this.m_driver.TrySetAttribute(this.m_attributes, attribute, 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: this.m_attributes.Set(attribute, value.ToBoolean()); return(true); //strict positif integers case PDO_ATTR.ATTR_PREFETCH: case PDO_ATTR.ATTR_TIMEOUT: this.m_attributes.Set(attribute, (int)value.ToLong()); return(true); //remaining case PDO_ATTR.ATTR_ERRMODE: int errmodeValue = (int)value.ToLong(); if (Enum.IsDefined(typeof(PDO_ERRMODE), errmodeValue)) { this.m_attributes.Set(attribute, (PDO_ERRMODE)errmodeValue); return(true); } return(false); case PDO_ATTR.ATTR_CASE: int caseValue = (int)value.ToLong(); if (Enum.IsDefined(typeof(PDO_CASE), caseValue)) { this.m_attributes.Set(attribute, (PDO_CASE)caseValue); return(true); } return(false); case PDO_ATTR.ATTR_CURSOR: int cursorValue = (int)value.ToLong(); if (Enum.IsDefined(typeof(PDO_CURSOR), cursorValue)) { this.m_attributes.Set(attribute, (PDO_CURSOR)cursorValue); return(true); } return(false); case PDO_ATTR.ATTR_DEFAULT_FETCH_MODE: 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_ORACLE_NULLS: case PDO_ATTR.ATTR_PERSISTENT: case PDO_ATTR.ATTR_STATEMENT_CLASS: case PDO_ATTR.ATTR_STRINGIFY_FETCHES: throw new NotImplementedException(); //statement only case PDO_ATTR.ATTR_CURSOR_NAME: return(false); default: return(false); } } catch (System.Exception ex) { this.HandleError(ex); return(false); } }
/// <summary> /// Converts to integer. /// </summary> /// <param name="variable">The variable.</param> /// <returns>The converted value.</returns> public static long intval(PhpValue variable) => variable.ToLong();
/// <summary> /// Whether an offset exists. /// </summary> /// <remarks>This method is executed when using isset() or empty().</remarks> public bool offsetExists(PhpValue offset) { var i = offset.ToLong(); return(i >= 0 && _array != null && i < _array.Length && _array[i].IsSet); }
/// <summary> /// Set an attribute. /// </summary> /// <param name="attribute">The attribute.</param> /// <param name="value">The value.</param> /// <returns></returns> public virtual bool setAttribute(PDO_ATTR attribute, PhpValue value) { try { if (attribute >= PDO_ATTR.ATTR_DRIVER_SPECIFIC) { return(Driver.TrySetAttribute(m_attributes, attribute, value)); } } catch (System.Exception ex) { this.HandleError(ex); return(false); } 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: m_attributes[attribute] = value; return(true); //strict positif integers case PDO_ATTR.ATTR_PREFETCH: case PDO_ATTR.ATTR_TIMEOUT: m_attributes[attribute] = value; return(true); //remaining case PDO_ATTR.ATTR_ERRMODE: l = value.ToLong(); if (Enum.IsDefined(typeof(PDO_ERRMODE), (int)l)) { m_attributes[attribute] = l; return(true); } else { // Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error: invalid error mode PhpException.InvalidArgument(nameof(value)); return(false); } case PDO_ATTR.ATTR_CASE: l = value.ToLong(); if (Enum.IsDefined(typeof(PDO_CASE), (int)l)) { m_attributes[attribute] = l; return(true); } return(false); case PDO_ATTR.ATTR_CURSOR: l = value.ToLong(); if (Enum.IsDefined(typeof(PDO_CURSOR), (int)l)) { m_attributes[attribute] = l; return(true); } return(false); case PDO_ATTR.ATTR_DEFAULT_FETCH_MODE: l = value.ToLong(); if (Enum.IsDefined(typeof(PDO_FETCH), (int)l)) { m_attributes[attribute] = l; return(true); } return(false); case PDO_ATTR.ATTR_STATEMENT_CLASS: if (value.IsPhpArray(out var arr) && arr.Count != 0) { m_attributes[attribute] = arr.DeepCopy(); return(true); } 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_ORACLE_NULLS: case PDO_ATTR.ATTR_PERSISTENT: case PDO_ATTR.ATTR_STRINGIFY_FETCHES: throw new NotImplementedException(); //statement only case PDO_ATTR.ATTR_CURSOR_NAME: return(false); default: return(false); } }
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 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); } }