/// <summary> /// Write headers to ASP.NET HttpContext. Can be called multiple times, it will be flushed only once. /// </summary> public virtual void Flush(HttpContext ctx) { if (flushed) { return; } flushed = true; try { if (location != null) { ctx.Response.Redirect(location, false); } if (_contentEncoding != null) { contentEncoding.SetEncoding(ctx.Response); } if (contentType != null) { ctx.Response.ContentType = contentType; } foreach (KeyValuePair <string, string> pair in headers) { try { ctx.Response.AppendHeader(pair.Key, pair.Value); } catch (HttpException e) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_header", pair.Key + ": " + pair.Value, e.Message)); } } } catch (HttpException e) { PhpException.Throw(PhpError.Warning, e.Message); } }
/// <summary> /// Insert the filter into the filter chains. /// </summary> /// <param name="stream">Which stream's filter chains.</param> /// <param name="filter">What filter.</param> /// <param name="where">What position in the chains.</param> /// <param name="parameters">Additional parameters for the filter.</param> /// <returns>True if successful.</returns> public static bool AddToStream(PhpStream stream, string filter, FilterChainOptions where, object parameters) { PhpFilter readFilter, writeFilter; if ((stream.Options & StreamAccessOptions.Read) == 0) { where &= ~FilterChainOptions.Read; } if ((stream.Options & StreamAccessOptions.Write) == 0) { where &= ~FilterChainOptions.Write; } if ((where & FilterChainOptions.Read) > 0) { if (!GetFilter(filter, true, out readFilter, parameters)) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_filter_name", filter)); return(false); } stream.AddFilter(readFilter, where); readFilter.OnCreate(); // Add to chain, (filters buffers too). } if ((where & FilterChainOptions.Write) > 0) { if (!GetFilter(filter, true, out writeFilter, parameters)) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_filter_name", filter)); return(false); } stream.AddFilter(writeFilter, where); writeFilter.OnCreate(); // Add to chain. } return(true); }
/// <include file='Doc/Wrappers.xml' path='docs/method[@name="RemoveDirectory"]/*'/> public override bool RemoveDirectory(string path, StreamRemoveDirectoryOptions options, StreamContext context) { try { // Deletes the directory (but not the contents - must be empty) Directory.Delete(path, false); return(true); } catch (UnauthorizedAccessException) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_access_denied", FileSystemUtils.StripPassword(path))); } catch (IOException) { // Directory not empty. PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_rmdir_io_error", FileSystemUtils.StripPassword(path))); } return(false); }
public static bool CheckAssertion( object assertion, string code, ScriptContext context, string callerRelativeSourcePath, int line, int column, NamingContext namingContext) { // checks assertion: if (assertion != null && !PhpComparer./*Default.*/ CompareEq(assertion, false)) { return(true); } // calls user callback: if (context.Config.Assertion.Callback != null) { ApplicationConfiguration app_config = Configuration.Application; FullPath full_path = new FullPath(callerRelativeSourcePath, app_config.Compiler.SourceRoot); context.Config.Assertion.Callback.Invoke(full_path.FullFileName, line, code); } // reports a warning if required: if (context.Config.Assertion.ReportWarning) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("assertion_failed", code)); } // terminates script execution if required: if (context.Config.Assertion.Terminate) { throw new ScriptDiedException(0); } // assertion failed: return(false); }
/// <summary> /// Returns an unitialized instance of the specified type or <see cref="__PHP_Incomplete_Class"/>. /// </summary> /// <param name="typeName">The type name.</param> /// <param name="context">Current <see cref="ScriptContext"/>.</param> /// <returns>The newly created instance or <B>null</B> if <paramref name="typeName"/> denotes /// a primitive type.</returns> /// <remarks> /// If the <paramref name="typeName"/> denotes a CLR type, no constructor is executed. If the /// <paramref name="typeName"/> denotes a PHP type, no user constructor (e.g. <c>__construct</c>) /// is executed. /// </remarks> public static DObject GetUninitializedInstance(string /*!*/ typeName, ScriptContext /*!*/ context) { // resolve the specified type DTypeDesc type = context.ResolveType(typeName); if (type == null || type.IsAbstract) { PhpCallback callback = context.Config.Variables.DeserializationCallback; if (callback != null && !callback.IsInvalid) { callback.Invoke(typeName); type = context.ResolveType(typeName); if (type == null || type.IsAbstract) { // unserialize_callback_func failed PhpException.Throw(PhpError.Warning, CoreResources.GetString("unserialize_callback_failed", ((IPhpConvertible)callback).ToString())); } } } if (type == null || type.IsAbstract) { // type not found -> create __PHP_Incomplete_Class __PHP_Incomplete_Class pic = new __PHP_Incomplete_Class(context, false); pic.__PHP_Incomplete_Class_Name.Value = typeName; pic.__PHP_Incomplete_Class_Name.IsSet = true; return(pic); } else { // create the instance return(type.New(context) as DObject); } }
/// <summary> /// Retrieves the number of arguments passed to the current user-function. /// </summary> /// <returns><B>True</B> on success, <B>false</B> if called from outside of user-function context.</returns> /// <exception cref="PhpException">If called from outside of user-function context (Warning).</exception> public bool GetArgCount(out int argCount, out int typeArgCount) { // if stack is empty: if (Top == 0) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("no_function_context")); argCount = typeArgCount = 0; return(false); } // fetches the number of arguments from the stack's top; // each args-aware function should push it there by MakeArgsAware; // Items[Top] = null, Items[Top - 1] = <arg count>, Items[Top - 2] = <1st arg>, ... // <arg count> encodes type arg count and regular arg count {type #} << 16 | {regular #}; int encoded_result = (int)Items[Top - 1]; argCount = encoded_result & 0xffff; typeArgCount = encoded_result >> 16; Debug.Assert(argCount >= 0 && argCount <= Top); Debug.Assert(typeArgCount >= 0 && typeArgCount <= TypesTop); return(true); }
/// <summary> /// Try the new FileSystemInfo based operation and hamdle exceptions properly. /// </summary> /// <typeparam name="T">The return value type.</typeparam> /// <param name="invalid">Invalid value.</param> /// <param name="path">Path to the resource passed to the <paramref name="action"/>. Also used for error control.</param> /// <param name="action">Action to try. The first argument is the path.</param> /// <returns>The value of <paramref name="action"/>() or <paramref name="invalid"/>.</returns> public static T HandleNewFileSystemInfo <T>(T invalid, string path, Func <string, T> /*!*/ action) { try { return(action(path)); } catch (ArgumentException) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_stat_invalid_path", FileSystemUtils.StripPassword(path))); } catch (PathTooLongException) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_stat_invalid_path", FileSystemUtils.StripPassword(path))); } catch (Exception e) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_error", FileSystemUtils.StripPassword(path), e.Message)); } return(invalid); }
/// <include file='Doc/Wrappers.xml' path='docs/method[@name="Rename"]/*'/> public override bool Rename(string fromPath, string toPath, StreamRenameOptions options, StreamContext context) { try { File.Move(fromPath, toPath); return(true); } catch (UnauthorizedAccessException) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_access_denied", FileSystemUtils.StripPassword(fromPath))); } catch (IOException) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_rename_file_exists", FileSystemUtils.StripPassword(fromPath), FileSystemUtils.StripPassword(toPath))); } catch (Exception e) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_error", FileSystemUtils.StripPassword(fromPath), e.Message)); } return(false); }
public static void UnsupportedOperandTypes() { PhpException.Throw(PhpError.Error, CoreResources.GetString("unsupported_operand_types")); }
/// <include file='Doc/Wrappers.xml' path='docs/method[@name="Open"]/*'/> public override PhpStream Open(ref string path, string mode, StreamOpenOptions options, StreamContext context) { Debug.Assert(path != null); //Debug.Assert(PhpPath.IsLocalFile(path)); // Get the File.Open modes from the mode string FileMode fileMode; FileAccess fileAccess; StreamAccessOptions ao; if (!ParseMode(mode, options, out fileMode, out fileAccess, out ao)) { return(null); } // Open the native stream this.storageFile = IsolatedStorageFile.GetUserStoreForApplication(); FileStream stream = null; try { stream = new IsolatedStorageFileStream(path, fileMode, fileAccess, FileShare.ReadWrite | FileShare.Delete, storageFile); } catch (FileNotFoundException) { // Note: There may still be an URL in the path here. PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_not_exists", FileSystemUtils.StripPassword(path))); return(null); } catch (IOException e) { if ((ao & StreamAccessOptions.Exclusive) > 0) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_exists", FileSystemUtils.StripPassword(path))); } else { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_io_error", FileSystemUtils.StripPassword(path), PhpException.ToErrorMessage(e.Message))); } return(null); } catch (UnauthorizedAccessException) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_access_denied", FileSystemUtils.StripPassword(path))); return(null); } catch (Exception) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_invalid", FileSystemUtils.StripPassword(path))); return(null); } if ((ao & StreamAccessOptions.SeekEnd) > 0) { // Read/Write Append is not supported. Seek to the end of file manually. stream.Seek(0, SeekOrigin.End); } if ((ao & StreamAccessOptions.Temporary) > 0) { // Set the file attributes to Temporary too. File.SetAttributes(path, FileAttributes.Temporary); } return(new NativeStream(stream, this, ao, path, context)); }
public static void ThisUsedOutOfObjectContext() { PhpException.Throw(PhpError.Error, CoreResources.GetString("this_used_out_of_object")); }
/// <summary> /// Throws an error because <B>[]</B> cannot be used in read context. /// </summary> public override object Get(object var, ScriptContext context, DTypeDesc caller) { PhpException.Throw(PhpError.Error, CoreResources.operator_array_access_used_for_reading); return(null); }
public static void UndefinedMethodCalled(string className, string methodName) { PhpException.Throw(PhpError.Error, CoreResources.GetString("undefined_method_called", className, methodName)); }
/// <summary> /// Returns names and values of properties whose names have been returned by <c>__sleep</c>. /// </summary> /// <param name="instance">The instance being serialized.</param> /// <param name="sleepResult">The array returned by <c>__sleep</c>.</param> /// <param name="context">Current <see cref="ScriptContext"/>.</param> /// <returns>Name-value pairs. Names are properly formatted for serialization.</returns> /// <exception cref="PhpException">Property of the name returned from <c>__sleep</c> does not exist.</exception> /// <remarks> /// This method returns exactly <paramref name="sleepResult"/>'s <see cref="PhpHashtable.Count"/> items. /// </remarks> public static IEnumerable <KeyValuePair <string, object> > EnumerateSerializableProperties( DObject /*!*/ instance, PhpArray /*!*/ sleepResult, ScriptContext /*!*/ context) { foreach (object item in sleepResult.Values) { PhpMemberAttributes visibility; string name = PHP.Core.Convert.ObjectToString(item); string declaring_type_name; string property_name = ParsePropertyName(name, out declaring_type_name, out visibility); DTypeDesc declarer; if (declaring_type_name == null) { declarer = instance.TypeDesc; } else { declarer = context.ResolveType(declaring_type_name); if (declarer == null) { // property name refers to an unknown class -> value will be null yield return(new KeyValuePair <string, object>(name, null)); continue; } } // obtain the property desc and decorate the prop name according to its visibility and declaring class DPropertyDesc property; if (instance.TypeDesc.GetProperty(new VariableName(property_name), declarer, out property) == GetMemberResult.OK && !property.IsStatic) { if ((Enums.VisibilityEquals(visibility, property.MemberAttributes) && visibility != PhpMemberAttributes.Public) || (visibility == PhpMemberAttributes.Private && declarer != property.DeclaringType)) { // if certain conditions are met, serialize the property as null // (this is to precisely mimic the PHP behavior) yield return(new KeyValuePair <string, object>(name, null)); continue; } name = FormatPropertyName(property, property_name); } else { property = null; } // obtain the property value object val = null; if (property != null) { val = property.Get(instance); } else if (instance.RuntimeFields == null || !instance.RuntimeFields.TryGetValue(name, out val)) { // this is new in PHP 5.1 PhpException.Throw(PhpError.Notice, CoreResources.GetString("sleep_returned_bad_field", name)); } yield return(new KeyValuePair <string, object>(name, val)); } }
public static void PropertyTypeMismatch(string /*!*/ className, string /*!*/ propertyName) { PhpException.Throw(PhpError.Error, CoreResources.GetString("property_type_mismatch", className, propertyName)); }
public static void MethodNotAccessible(string className, string methodName, string context, bool isProtected) { PhpException.Throw(PhpError.Error, CoreResources.GetString( isProtected ? "protected_method_called" : "private_method_called", className, methodName, context)); }
public static void CannotInstantiateType(string typeName, bool isInterface) { PhpException.Throw(PhpError.Error, CoreResources.GetString( isInterface ? "interface_instantiated" : "abstract_class_instantiated", typeName)); }
public static void PropertyNotAccessible(string className, string fieldName, string context, bool isProtected) { PhpException.Throw(PhpError.Error, CoreResources.GetString( isProtected ? "protected_property_accessed" : "private_property_accessed", className, fieldName, context)); }
public static void ConstantNotAccessible(string className, string constName, string context, bool isProtected) { PhpException.Throw(PhpError.Error, CoreResources.GetString( isProtected ? "protected_constant_accessed" : "private_constant_accessed", className, constName, context)); }
public static void AbstractMethodCalled(string className, string methodName) { PhpException.Throw(PhpError.Error, CoreResources.GetString("abstract_method_called", className, methodName)); }
/// <summary> /// Performs all checks on a path passed to a PHP function. /// </summary> /// <remarks> /// <para> /// This method performs a check similar to <c>safe_mode.c: php_checkuid_ex()</c> /// together with <c>open_basedir</c> check. /// </para> /// <para> /// The <paramref name="filename"/> may be one of the following: /// <list type="bullet"> /// <item>A relative path. The path is resolved regarding the <c>include_path</c> too if required /// and checking continues as in the next case.</item> /// <item>An absolute path. The file or directory is checked for existence and for access permissions<sup>1</sup> /// according to the given <paramref name="mode"/>.</item> /// </list> /// <sup>1</sup> Regarding the <c>open_basedir</c> configuration option. /// File access permissions are checked at the time of file manipulation /// (opening, copying etc.). /// </para> /// </remarks> /// <param name="filename">A resolved path. Must be an absolute path to a local file.</param> /// <param name="mode">One of the <see cref="CheckAccessMode"/>.</param> /// <param name="options"><c>true</c> to suppress error messages.</param> /// <returns><c>true</c> if the function may continue with file access, /// <c>false</c>to fail.</returns> /// <exception cref="PhpException">If the file can not be accessed /// and the <see cref="CheckAccessOptions.Quiet"/> is not set.</exception> public static bool CheckAccess(string filename, CheckAccessMode mode, CheckAccessOptions options) { Debug.Assert(Path.IsPathRooted(filename)); string url = FileSystemUtils.StripPassword(filename); bool quiet = (options & CheckAccessOptions.Quiet) > 0; switch (mode) { case CheckAccessMode.FileMayExist: break; case CheckAccessMode.FileExists: if (!File.Exists(filename)) { if (!quiet) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_not_exists", url)); } return(false); } break; case CheckAccessMode.FileNotExists: if (File.Exists(filename)) { if (!quiet) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_exists", url)); } return(false); } break; case CheckAccessMode.FileOrDirectory: if ((!Directory.Exists(filename)) && (!File.Exists(filename))) { if (!quiet) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_path_not_exists", url)); } return(false); } break; case CheckAccessMode.Directory: if (!Directory.Exists(filename)) { if (!quiet) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_directory_not_exists", url)); } return(false); } break; default: Debug.Assert(false); return(false); } return(true); }
public static void UndeclaredStaticProperty(string className, string fieldName) { PhpException.Throw(PhpError.Error, CoreResources.GetString("undeclared_static_property_accessed", className, fieldName)); }
/// <summary> /// Merges the path with the current working directory /// to get a canonicalized absolute pathname representing the same file. /// </summary> /// <remarks> /// This method is an analogy of <c>main/safe_mode.c: php_checkuid</c>. /// Looks for the file in the <c>include_path</c> and checks for <c>open_basedir</c> restrictions. /// </remarks> /// <param name="path">An absolute or relative path to a file.</param> /// <param name="wrapper">The wrapper found for the specified file or <c>null</c> if the path resolution fails.</param> /// <param name="mode">The checking mode of the <see cref="CheckAccess"/> method (file, directory etc.).</param> /// <param name="options">Additional options for the <see cref="CheckAccess"/> method.</param> /// <returns><c>true</c> if all the resolution and checking passed without an error, <b>false</b> otherwise.</returns> /// <exception cref="PhpException">Security violation - when the target file /// lays outside the tree defined by <c>open_basedir</c> configuration option.</exception> public static bool ResolvePath(ref string path, out StreamWrapper wrapper, CheckAccessMode mode, CheckAccessOptions options) { // Path will contain the absolute path without file:// or the complete URL; filename is the relative path. string filename, scheme = GetSchemeInternal(path, out filename); wrapper = StreamWrapper.GetWrapper(scheme, (StreamOptions)options); if (wrapper == null) { return(false); } if (wrapper.IsUrl) { // Note: path contains the whole URL, filename the same without the scheme:// portion. // What to check more? } else if (scheme != "php") { try { // Filename contains the original path without the scheme:// portion, check for include path. bool isInclude = false; if ((options & CheckAccessOptions.UseIncludePath) > 0) { isInclude = CheckIncludePath(filename, ref path); } // Path will now contain an absolute path (either to an include or actual directory). if (!isInclude) { path = Path.GetFullPath(Path.Combine(ScriptContext.CurrentContext.WorkingDirectory, filename)); } } catch (Exception) { if ((options & CheckAccessOptions.Quiet) == 0) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_filename_invalid", FileSystemUtils.StripPassword(path))); } return(false); } GlobalConfiguration global_config = Configuration.Global; // Note: extensions check open_basedir too -> double check.. if (!global_config.SafeMode.IsPathAllowed(path)) { if ((options & CheckAccessOptions.Quiet) == 0) { PhpException.Throw(PhpError.Warning, CoreResources.GetString("open_basedir_effect", path, global_config.SafeMode.GetAllowedPathPrefixesJoin())); } return(false); } // Replace all '/' with '\'. // path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); Debug.Assert( path.IndexOf(Path.AltDirectorySeparatorChar) == -1 || (Path.AltDirectorySeparatorChar == Path.DirectorySeparatorChar), // on Mono, so ignore it string.Format("'{0}' should not contain '{1}' char.", path, Path.AltDirectorySeparatorChar)); // The file wrapper expects an absolute path w/o the scheme, others expect the scheme://url. if (scheme != "file") { path = String.Format("{0}://{1}", scheme, path); } } return(true); }
/// <include file='Doc/Wrappers.xml' path='docs/method[@name="Stat"]/*'/> /// <remarks> /// <seealso cref="StreamStatOptions"/> for the list of additional options. /// </remarks> public virtual StatStruct Stat(string path, StreamStatOptions options, StreamContext context, bool streamStat) { // int (*url_stat)(php_stream_wrapper *wrapper, char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC); PhpException.Throw(PhpError.Warning, CoreResources.GetString("wrapper_op_unsupported", "Stat")); return(new StatStruct()); }
public static void NoSuitableOverload(string className, string /*!*/ methodName) { PhpException.Throw(PhpError.Error, CoreResources.GetString( (className != null) ? "no_suitable_method_overload" : "no_suitable_function_overload", className, methodName)); }
public static void StaticPropertyUnset(string className, string fieldName) { PhpException.Throw(PhpError.Error, CoreResources.GetString("static_property_unset", className, fieldName)); }