Exemple #1
0
        /// <summary>
        /// Deletes a file using a StreamWrapper corresponding to the given URL.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="path">An URL of a file to be deleted.</param>
        /// <param name="context">StreamContext.</param>
        /// <returns>True in case of success.</returns>
        public static bool unlink(Context ctx, string path, PhpResource context = null)
        {
            if (string.IsNullOrEmpty(path))
            {
                //PhpException.Throw(PhpError.Warning, LibResources.GetString("arg_empty", "path"));
                //return false;
                throw new ArgumentException(nameof(path));
            }

            var sc = StreamContext.GetValid(context, true);

            if (sc == null) // PHP warning is thrown by StreamContext.GetValid
            {
                return(false);
            }

            StreamWrapper wrapper;

            if (!PhpStream.ResolvePath(ctx, ref path, out wrapper, CheckAccessMode.FileExists, CheckAccessOptions.Empty))
            {
                return(false);
            }

            // Clear the cache (the currently deleted file may have been cached)
            clearstatcache();

            //
            return(wrapper.Unlink(path, 0, sc));
        }
 /// <summary>
 /// Makes a directory or a branch of directories using the specified wrapper.
 /// </summary>
 /// <param name="ctx">Runtime context.</param>
 /// <param name="pathname">The path to create.</param>
 /// <param name="mode">A combination of <see cref="StreamMakeDirectoryOptions"/>.</param>
 /// <param name="recursive">Create recursively.</param>
 /// <param name="context">Stream context, can be <c>null</c> to use default context.</param>
 /// <returns><c>true</c> if successful, <c>false</c> otherwise.</returns>
 public static bool mkdir(Context ctx, string pathname, int mode = (int)FileModeFlags.ReadWriteExecute, bool recursive = false, PhpResource context = null)
 {
     return(PhpStream.ResolvePath(ctx, ref pathname, out var wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty) &&
            wrapper.MakeDirectory(pathname, mode,
                                  recursive ? StreamMakeDirectoryOptions.Recursive : StreamMakeDirectoryOptions.Empty,
                                  (context as StreamContext) ?? StreamContext.Default));
 }
        /// <summary>
        /// Removes a directory.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="dirname"></param>
        /// <param name="context">Stream context. Can be <c>null</c> to use default context.</param>
        /// <returns><c>true</c> if successful, <c>false</c> otherwise.</returns>
        public static bool rmdir(Context ctx, string dirname, StreamContext context = null)
        {
            StreamWrapper wrapper;

            return(PhpStream.ResolvePath(ctx, ref dirname, out wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty) &&
                   wrapper.RemoveDirectory(dirname, StreamRemoveDirectoryOptions.Empty, (context as StreamContext) ?? StreamContext.Default));
        }
        public static PhpArray scandir(Context ctx, string directory, ScanDirSortOrder sorting_order = ScanDirSortOrder.Ascending)
        {
            if (PhpStream.ResolvePath(ctx, ref directory, out var wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty))
            {
                var listing = wrapper.Listing(ctx.RootPath, directory, 0, null);
                if (listing != null)
                {
                    switch (sorting_order)
                    {
                    case ScanDirSortOrder.Ascending:
                        listing.Sort();
                        break;

                    case ScanDirSortOrder.Descending:
                        listing.Sort();
                        listing.Reverse();
                        break;
                    }

                    //
                    return(new PhpArray(listing));
                }
            }
            return(null); // false
        }
Exemple #5
0
        public static PhpArray Scan(string directory, int sorting_order)
        {
            StreamWrapper wrapper;

            if (!PhpStream.ResolvePath(ref directory, out wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty))
            {
                return(null);
            }

            string[] listing = wrapper.Listing(directory, 0, null);

            if (listing != null)
            {
                PhpArray ret = new PhpArray(listing);                 // create the array from the system one
                if (sorting_order == 1)
                {
                    PhpArrays.ReverseSort(ret, ComparisonMethod.String);
                }
                else
                {
                    PhpArrays.Sort(ret, ComparisonMethod.String);
                }
                return(ret);
            }
            return(null);            // false
        }
Exemple #6
0
        public static bool ChangeFileMode(string path, int mode)
        {
            StreamWrapper wrapper;

            if (!PhpStream.ResolvePath(ref path, out wrapper, CheckAccessMode.FileOrDirectory, CheckAccessOptions.Empty))
            {
                return(false);
            }

            bool           isDir = PhpFile.IsDirectory(path);
            FileSystemInfo fInfo = isDir ? (FileSystemInfo) new DirectoryInfo(path) : new FileInfo(path);

            if (!fInfo.Exists)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_path", path));
                return(false);
            }

            //Directories has no equivalent of a readonly flag,
            //instead, their content permission should be adjusted accordingly
            //[http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.directorysecurity.aspx]
            if (isDir)
            {
                //DirectoryInfo dInfo = (DirectoryInfo)fInfo;
                //DirectorySecurity dSecurity = dInfo.GetAccessControl();

                //foreach(FileSystemAccessRule rule in ResolveAccessRules(mode))
                //    dSecurity.AddAccessRule(rule);
                //try
                //{
                //    dInfo.SetAccessControl(dSecurity);
                //}
                //catch
                {
                    return(false);
                }
            }
            else
            {
                // according to <io.h> and <chmod.c> from C libraries in Visual Studio 2008
                // and PHP 5.3 source codes, which are using standard _chmod() function in C
                // on Windows it only changes the ReadOnly flag of the file
                //
                // see <chmod.c> for more details

                /*
                 #define _S_IREAD        0x0100          // read permission, owner
                 #define _S_IWRITE       0x0080          // write permission, owner
                 #define _S_IEXEC        0x0040          // execute/search permission, owner
                 */

                ((FileInfo)fInfo).IsReadOnly = ((mode & 0x0080) == 0);
            }

            return(true);
        }
Exemple #7
0
        public static bool RemoveDirectory(string dirname, StreamContext context)
        {
            StreamWrapper wrapper;

            if (!PhpStream.ResolvePath(ref dirname, out wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty))
            {
                return(false);
            }

            return(wrapper.RemoveDirectory(dirname, StreamRemoveDirectoryOptions.Empty, StreamContext.Default));
        }
Exemple #8
0
        /// <summary>
        /// Check input parameters, resolves absolute path and corresponding stream wrapper.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="path">The path passed to stat().</param>
        /// <param name="quiet">Wheter to suppress warning message if argument is empty.</param>
        /// <param name="wrapper">If passed, it will contain valid StremWrapper to the given <paramref name="path"/>.</param>
        /// <returns>True if check passed.</returns>
        internal static bool ResolvePath(Context ctx, ref string path, bool quiet, out StreamWrapper wrapper)
        {
            if (string.IsNullOrEmpty(path))
            {
                wrapper = null;
                PhpException.Throw(PhpError.Warning, Resources.LibResources.arg_empty, nameof(path));
                return(false);
            }

            return(PhpStream.ResolvePath(ctx, ref path, out wrapper, CheckAccessMode.FileOrDirectory, quiet ? CheckAccessOptions.Quiet : CheckAccessOptions.Empty));
        }
Exemple #9
0
        /// <summary>
        /// Check input parameters, resolves absolute path and corresponding stream wrapper.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="path">The path passed to stat().</param>
        /// <param name="quiet">Wheter to suppress warning message if argument is empty.</param>
        /// <param name="wrapper">If passed, it will contain valid StremWrapper to the given <paramref name="path"/>.</param>
        /// <returns>True if check passed.</returns>
        static bool ResolvePath(Context ctx, ref string path, bool quiet, out StreamWrapper wrapper)
        {
            if (string.IsNullOrEmpty(path))
            {
                wrapper = null;
                //PhpException.Throw(PhpError.Warning, LibResources.GetString("arg:empty", "path"));
                //return false;
                throw new ArgumentException(nameof(path));
            }

            return(PhpStream.ResolvePath(ctx, ref path, out wrapper, CheckAccessMode.FileOrDirectory, quiet ? CheckAccessOptions.Quiet : CheckAccessOptions.Empty));
        }
Exemple #10
0
        public static bool MakeDirectory(string pathname, int mode, bool recursive, PhpResource context)
        {
            StreamWrapper wrapper;

            if (!PhpStream.ResolvePath(ref pathname, out wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty))
            {
                return(false);
            }

            return(wrapper.MakeDirectory(pathname, mode, recursive ?
                                         StreamMakeDirectoryOptions.Recursive : StreamMakeDirectoryOptions.Empty, StreamContext.Default));
        }
Exemple #11
0
        public static PhpResource tmpfile(Context ctx)
        {
            string path = tempnam(string.Empty, "php");

            StreamWrapper wrapper;

            if (!PhpStream.ResolvePath(ctx, ref path, out wrapper, CheckAccessMode.FileMayExist, CheckAccessOptions.Empty))
            {
                return(null);
            }

            return(wrapper.Open(ctx, ref path, "w+b", StreamOpenOptions.Temporary, StreamContext.Default));
        }
Exemple #12
0
        public static PhpResource Open(string directory)
        {
            lastDirHandle = null;

            StreamWrapper wrapper;

            if (!PhpStream.ResolvePath(ref directory, out wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty))
            {
                return(null);
            }

            string[] listing = wrapper.Listing(directory, 0, null);
            return((listing != null) ? (lastDirHandle = new DirectoryListing(listing)) : null);
        }
        public static PhpResource opendir(Context ctx, string directory)
        {
            var dirctx = PhpDirectoryContext.GetContext(ctx);

            if (PhpStream.ResolvePath(ctx, ref directory, out var wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty))
            {
                var listing = wrapper.Listing(ctx.RootPath, directory, StreamListingOptions.Empty, null);
                if (listing != null)
                {
                    return(dirctx.LastDirHandle = new DirectoryListing(dirctx, listing));
                }
            }

            //
            dirctx.LastDirHandle = null;
            return(null);
        }
Exemple #14
0
        public static bool Touch(string path, int mtime, int atime)
        {
            // Create the file if it does not already exist (performs all checks).
            //PhpStream file = (PhpStream)Open(path, "ab");
            //if (file == null) return false;
            StreamWrapper wrapper;

            if (!PhpStream.ResolvePath(ref path, out wrapper, CheckAccessMode.FileMayExist, CheckAccessOptions.Quiet))
            {
                return(false);
            }

            if (!Exists(path))
            {
                // Open and close => create new.
                Close(wrapper.Open(ref path, "wb", StreamOpenOptions.Empty, StreamContext.Default));
            }

            DateTime access_time       = (atime > 0) ? DateTimeUtils.UnixTimeStampToUtc(atime) : DateTime.UtcNow;
            DateTime modification_time = (mtime > 0) ? DateTimeUtils.UnixTimeStampToUtc(mtime) : DateTime.UtcNow;

            access_time       -= DateTimeUtils.GetDaylightTimeDifference(access_time, DateTime.UtcNow);
            modification_time -= DateTimeUtils.GetDaylightTimeDifference(modification_time, DateTime.UtcNow);

            try
            {
                File.SetLastWriteTimeUtc(path, modification_time);
                File.SetLastAccessTimeUtc(path, access_time);

                // Clear the cached stat values
                ClearStatCache();
                return(true);
            }
            catch (UnauthorizedAccessException)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_access_denied",
                                                                             FileSystemUtils.StripPassword(path)));
            }
            catch (System.Exception e)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_error",
                                                                             FileSystemUtils.StripPassword(path), e.Message));
            }
            return(false);
        }
Exemple #15
0
        /// <summary>
        /// Copies a file (even accross different stream wrappers).
        /// </summary>
        /// <remarks>
        /// If the destination file already exists, it will be overwritten.
        /// <para>
        /// Note: As of PHP 4.3.0, both source and dest may be URLs if the
        /// "fopen wrappers" have been enabled. See <c>fopen()</c> for more details.
        /// If dest is an URL, the copy operation may fail if the wrapper does
        /// not support overwriting of existing files.
        /// </para>
        /// </remarks>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="source">Source URL.</param>
        /// <param name="dest">Destination URL.</param>
        /// <returns><c>true</c> on success or <c>false</c> on failure.</returns>
        public static bool copy(Context ctx, string source, string dest)
        {
            StreamWrapper reader, writer;

            if ((!PhpStream.ResolvePath(ctx, ref source, out reader, CheckAccessMode.FileExists, CheckAccessOptions.Empty)) ||
                (!PhpStream.ResolvePath(ctx, ref dest, out writer, CheckAccessMode.FileExists, CheckAccessOptions.Empty)))
            {
                return(false);
            }

            if ((reader.Scheme == "file") && (writer.Scheme == "file"))
            {
                // Copy the file.
                try
                {
                    File.Copy(source, dest, true);
                    return(true);
                }
                catch (System.Exception)
                {
                    return(false);
                }
            }
            else
            {
                // Copy the two files using the appropriate stream wrappers.
                using (PhpResource from = reader.Open(ctx, ref source, "rb", StreamOpenOptions.Empty, StreamContext.Default))
                {
                    if (from == null)
                    {
                        return(false);
                    }
                    using (PhpResource to = writer.Open(ctx, ref dest, "wb", StreamOpenOptions.Empty, StreamContext.Default))
                    {
                        if (to == null)
                        {
                            return(false);
                        }

                        int copied = PhpStreams.stream_copy_to_stream(from, to);
                        return(copied >= 0);
                    }
                }
            }
        }
Exemple #16
0
        /// <summary>
        /// Renames a file.
        /// </summary>
        /// <remarks>
        /// Both the <paramref name="oldpath"/> and the <paramref name="newpath"/> must be handled by the same wrapper.
        /// </remarks>
        public static bool rename(Context ctx, string oldpath, string newpath)
        {
            StreamWrapper oldwrapper, newwrapper;

            if ((!PhpStream.ResolvePath(ctx, ref oldpath, out oldwrapper, CheckAccessMode.FileExists, CheckAccessOptions.Empty)) ||
                (!PhpStream.ResolvePath(ctx, ref newpath, out newwrapper, CheckAccessMode.FileMayExist, CheckAccessOptions.Empty)))
            {
                return(false);
            }

            if (oldwrapper != newwrapper)
            {
                //PhpException.Throw(PhpError.Warning, LibResources.GetString("wrappers_must_match"));
                throw new ArgumentException("wrappers_must_match"); // TODO: Err
            }

            return(oldwrapper.Rename(oldpath, newpath, StreamRenameOptions.Empty, StreamContext.Default));
        }
Exemple #17
0
        /// <summary>
        /// Sets access and modification time of file.
        /// </summary>
        /// <remarks>
        /// Attempts to set the access and modification time of the file named by
        /// path to the value given by time. If the option time is not given,
        /// uses the present time. If the third option atime is present, the access
        /// time of the given path is set to the value of atime. Note that
        /// the access time is always modified, regardless of the number of parameters.
        /// If the file does not exist, it is created.
        /// </remarks>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="path">The file to touch.</param>
        /// <param name="mtime">The new modification time.</param>
        /// <param name="atime">The desired access time.</param>
        /// <returns><c>true</c> on success, <c>false</c> on failure.</returns>
        public static bool touch(Context ctx, string path, int mtime = 0, int atime = 0)
        {
            // Create the file if it does not already exist (performs all checks).
            //PhpStream file = (PhpStream)Open(path, "ab");
            //if (file == null) return false;
            if (!PhpStream.ResolvePath(ctx, ref path, out var wrapper, CheckAccessMode.FileMayExist, CheckAccessOptions.Quiet))
            {
                return(false);
            }

            if (!file_exists(ctx, path))
            {
                // Open and close => create new.
                wrapper.Open(ctx, ref path, "wb", StreamOpenOptions.Empty, StreamContext.Default)
                ?.Dispose();
            }

            var access_time       = (atime > 0) ? DateTimeUtils.UnixTimeStampToUtc(atime) : System.DateTime.UtcNow;
            var modification_time = (mtime > 0) ? DateTimeUtils.UnixTimeStampToUtc(mtime) : System.DateTime.UtcNow;

            //access_time -= DateTimeUtils.GetDaylightTimeDifference(access_time, System.DateTime.UtcNow);
            //modification_time -= DateTimeUtils.GetDaylightTimeDifference(modification_time, System.DateTime.UtcNow);

            try
            {
                File.SetLastWriteTimeUtc(path, modification_time);
                File.SetLastAccessTimeUtc(path, access_time);

                // Clear the cached stat values
                clearstatcache();
                return(true);
            }
            catch (UnauthorizedAccessException)
            {
                PhpException.Throw(PhpError.Warning, ErrResources.stream_file_access_denied, FileSystemUtils.StripPassword(path));
            }
            catch (System.Exception e)
            {
                PhpException.Throw(PhpError.Warning, ErrResources.stream_error, FileSystemUtils.StripPassword(path), e.Message);
            }
            return(false);
        }
        public static PhpResource opendir(Context ctx, string directory)
        {
            var dirctx = PhpDirectoryContext.GetContext(ctx);

            StreamWrapper wrapper;

            if (PhpStream.ResolvePath(ctx, ref directory, out wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty))
            {
                string[] listing = wrapper.Listing(directory, 0, null);
                if (listing != null)
                {
                    var res = dirctx.LastDirHandle = new DirectoryListing(ctx, listing);
                    return(res);
                }
            }

            //
            dirctx.LastDirHandle = null;
            return(null);
        }
 public static PhpArray scandir(Context ctx, string directory, int sorting_order = 0)
 {
     if (PhpStream.ResolvePath(ctx, ref directory, out var wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty))
     {
         var listing = wrapper.Listing(ctx.RootPath, directory, 0, null);
         if (listing != null)
         {
             var ret = new PhpArray(listing); // create the array from the system one
             if (sorting_order == 1)
             {
                 Arrays.rsort(ctx, ret, ComparisonMethod.String);
             }
             else
             {
                 Arrays.sort(ctx, ret, ComparisonMethod.String);
             }
             return(ret);
         }
     }
     return(null); // false
 }
Exemple #20
0
        public static PhpArray scandir(Context ctx, string directory, ScanDirSortOrder sorting_order = ScanDirSortOrder.Ascending)
        {
            if (PhpStream.ResolvePath(ctx, ref directory, out var wrapper, CheckAccessMode.Directory, CheckAccessOptions.Empty))
            {
                var listing = wrapper.Listing(ctx.RootPath, directory, 0, null);
                if (listing != null)
                {
                    var ret = new PhpArray(listing); // create the array from the system one
                    switch (sorting_order)
                    {
                    case ScanDirSortOrder.Ascending:
                        Arrays.sort(ctx, ret, ComparisonMethod.String);
                        break;

                    case ScanDirSortOrder.Descending:
                        Arrays.rsort(ctx, ret, ComparisonMethod.String);
                        break;
                    }
                    return(ret);
                }
            }
            return(null); // false
        }
Exemple #21
0
        /// <summary>
        /// Check StatInternal input parameters.
        /// </summary>
        /// <param name="path">The path passed to stat().</param>
        /// <param name="quiet">Wheter to suppress warning message if argument is empty.</param>
        /// <param name="wrapper">If passed, it will contain valid StremWrapper to the given <paramref name="path"/>.</param>
        /// <returns>True if check passed.</returns>
        private static bool StatInternalCheck(ref string path, bool quiet, out StreamWrapper wrapper)
        {
            wrapper = null;

            if (String.IsNullOrEmpty(path))
            {
                PhpException.Throw(PhpError.Warning, LibResources.GetString("arg:empty", "path"));
                return(false);
            }

            CheckAccessOptions options = CheckAccessOptions.Empty;

            if (quiet)
            {
                options |= CheckAccessOptions.Quiet;
            }
            if (!PhpStream.ResolvePath(ref path, out wrapper, CheckAccessMode.FileOrDirectory, options))
            {
                return(false);
            }

            // check passed
            return(true);
        }
Exemple #22
0
 public static string stream_resolve_include_path(Context ctx, string filename)
 {
     if (PhpStream.ResolvePath(ctx, ref filename, out var wrapper, CheckAccessMode.FileExists, CheckAccessOptions.Quiet | CheckAccessOptions.UseIncludePath))
     {
         return(filename);
     }