示例#1
0
        /// <summary>
        /// Attempts to change the owner of the <paramref name="filename"/> to <paramref name="user"/>.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="filename">Path to the file to change owner.</param>
        /// <param name="user">A <see cref="string"/> or <see cref="int"/> identifier of the target group.</param>
        /// <returns>Whether the function succeeded.</returns>
        public static bool chown(Context ctx, string filename, PhpValue user)
        {
            if (PhpPath.ResolvePath(ctx, ref filename, false, out var wrapper))
            {
                var fs = new FileSecurity(filename, AccessControlSections.Owner);   // throws if file does not exist or no permissions
                IdentityReference identity;
                if (user.IsString(out var uname))
                {
                    var sepidx      = uname.IndexOf('/');
                    var domain_user = sepidx >= 0
                        ? (uname.Remove(sepidx), uname.Substring(sepidx + 1))
                        : (null, uname);

                    identity = new NTAccount(domain_user.Item1, domain_user.Item2);
                }
                //else if (user.IsLong(out var uid))
                //{

                //}
                else
                {
                    PhpException.InvalidArgumentType(nameof(user), PhpVariable.TypeNameString);
                    return(false);
                }

                //var identity = user.IsString(out var uname) ? new NTAccount(uname) : user.IsLong(out var uid) ? new IdentityReference(...) : null;
                fs.SetOwner(identity);  // throws if no permission or error
                return(true);
            }

            //
            return(false);
        }
 /// <summary>Changes the virtual working directory for the current script.</summary>
 /// <param name="ctx">Runtime context.</param>
 /// <param name="directory">Absolute or relative path to the new working directory.</param>
 /// <returns>Returns <c>true</c> on success or <c>false</c> on failure.</returns>
 /// <exception cref="PhpException">If the specified directory does not exist.</exception>
 public static bool chdir(Context ctx, string directory)
 {
     if (directory != null)
     {
         string newPath = PhpPath.AbsolutePath(ctx, directory);
         if (System.IO.Directory.Exists(newPath))
         {
             // Note: open_basedir not applied here, URL will not pass through
             ctx.WorkingDirectory = newPath;
             return(true);
         }
     }
     PhpException.Throw(PhpError.Warning, string.Format(Resources.LibResources.directory_not_found, directory));
     return(false);
 }
示例#3
0
        /// <summary>
        /// Changes file permissions.
        /// </summary>
        /// <remarks>
        /// On Windows platform this function supports only the
        /// <c>_S_IREAD (0400)</c> and <c>_S_IWRITE (0200)</c>
        /// options (set read / write permissions for the file owner).
        /// Note that the constants are octal numbers.
        /// </remarks>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="path">Path to the file to change group.</param>
        /// <param name="mode">New file permissions (see remarks).</param>
        /// <returns><c>true</c> on success, <c>false</c> on failure.</returns>
        public static bool chmod(Context ctx, string path, int mode)
        {
            StreamWrapper wrapper;

            if (!PhpPath.ResolvePath(ctx, ref path, false, out wrapper))
            {
                return(false);
            }

            bool           isDir = PhpPath.is_dir(ctx, path);
            FileSystemInfo fInfo = isDir
                ? (FileSystemInfo) new DirectoryInfo(path)
                : new FileInfo(path);

            if (!fInfo.Exists)
            {
                //PhpException.Throw(PhpError.Warning, CoreResources.GetString("invalid_path", path));
                // TODO: Err invalid_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)
            {
                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);
        }
示例#4
0
 /// <summary>
 /// Opens a new zip archive for reading.
 /// </summary>
 /// <param name="ctx">Current runtime context.</param>
 /// <param name="filename">The file name of the ZIP archive to open.</param>
 /// <returns>Returns a resource handle for later use with <see cref="zip_read(ZipArchiveResource)"/>
 /// and <see cref="zip_close(ZipArchiveResource)"/> or returns the number of error if
 /// <paramref name="filename"/> does not exist or in case of other error.</returns>
 public static PhpValue zip_open(Context ctx, string filename)
 {
     try
     {
         string fullPath   = PhpPath.AbsolutePath(ctx, filename);
         var    fileStream = File.Open(fullPath, FileMode.Open);
         var    archive    = new ZipArchive(fileStream, ZipArchiveMode.Read);
         return(PhpValue.FromClass(new ZipArchiveResource(archive)));
     }
     catch (FileNotFoundException e)
     {
         PhpException.Throw(PhpError.Warning, e.Message);
         return(ER_NOENT);
     }
     catch (IOException e)
     {
         PhpException.Throw(PhpError.Warning, e.Message);
         return(ER_OPEN);
     }
 }
示例#5
0
        /// <summary>
        /// Moves an uploaded file to a new location.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="path">Temporary path of the uploaded file.</param>
        /// <param name="destination">Destination.</param>
        /// <returns>Value indicating the move succeeded.</returns>
        public static bool move_uploaded_file(Context ctx, string path, string destination)
        {
            if (path == null || !ctx.IsTemporaryFile(path))
            {
                return(false);
            }

            // overwrite destination unconditionally:
            if (PhpPath.file_exists(ctx, destination))
            {
                PhpPath.unlink(ctx, destination);
            }

            // move temp file to destination:
            if (PhpPath.rename(ctx, path, destination))
            {
                ctx.RemoveTemporaryFile(path);
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#6
0
 /// <summary>
 /// Writes the contents of string to the given gz-file.
 /// </summary>
 /// <param name="ctx">Runtime context.</param>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <param name="str">The string to write.</param>
 /// <param name="length">
 ///     The number of uncompressed bytes to write. If supplied, writing will stop after length (uncompressed) bytes have been
 ///     written or the end of string is reached, whichever comes first.
 /// </param>
 /// <returns>Returns the number of (uncompressed) bytes written to the given gz-file stream.</returns>
 public static int gzwrite(Context ctx, PhpResource zp, PhpString str, int length = -1)
 {
     return(PhpPath.fwrite(ctx, zp, str, length));
 }
示例#7
0
 /// <summary>
 /// Reads up to length bytes from the given gz-file pointer. Reading stops when length (uncompressed) bytes
 /// have been read or EOF is reached, whichever comes first.
 /// </summary>
 /// <param name="ctx">Runtime context.</param>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <param name="length">The number of bytes to read.</param>
 /// <returns>The data that have been read.</returns>
 public static PhpString gzread(Context ctx, PhpResource zp, int length)
 {
     return(PhpPath.fread(ctx, zp, length));
 }
示例#8
0
 /// <summary>
 /// Reads to EOF on the given gz-file pointer from the current position and writes the (uncompressed) results to standard output.
 /// </summary>
 /// <param name="ctx">Runtime context.</param>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>The number of uncompressed characters read from gz and passed through to the input, or FALSE on error.</returns>
 public static int gzpassthru(Context ctx, PhpResource zp)
 {
     return(PhpPath.fpassthru(ctx, zp));
 }
示例#9
0
 public static PhpString gzgets(PhpResource zp, int length = -1)
 {
     return((length == -1) ? PhpPath.fgets(zp) : PhpPath.fgets(zp, length));
 }
示例#10
0
 public static PhpString gzgetc(Context ctx, PhpResource zp)
 {
     return(PhpPath.fgetc(ctx, zp));
 }
示例#11
0
 /// <summary>
 /// Gets the position of the given file pointer; i.e., its offset into the uncompressed file stream.
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>The position of the file pointer or FALSE if an error occurs.</returns>
 public static int gztell(PhpResource zp)
 {
     return(PhpPath.ftell(zp));
 }
示例#12
0
 /// <summary>
 ///     <para>
 ///         Sets the file position indicator for the given file pointer to the given offset byte into the file stream. Equivalent
 ///         to calling (in C) gzseek(zp, offset, SEEK_SET).
 ///     </para>
 ///     <para>
 ///         If the file is opened for reading, this function is emulated but can be extremely slow. If the file is opened for
 ///         writing, only forward seeks are supported; gzseek() then compresses a sequence of zeroes up to the new starting position.
 ///     </para>
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <param name="offset">The seeked offset.</param>
 /// <param name="whence">
 ///     whence values are: SEEK_SET (relative to origin), SEEK_CUR (relative to current position).
 /// </param>
 /// <returns>Upon success, returns 0; otherwise, returns -1. Note that seeking past EOF is not considered an error.</returns>
 public static int gzseek(PhpResource zp, int offset, int whence = PhpStreams.SEEK_SET)
 {
     return(PhpPath.fseek(zp, offset, whence));
 }
示例#13
0
 /// <summary>
 /// Sets the file position indicator of the given gz-file pointer to the beginning of the file stream.
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>Returns TRUE on success or FALSE on failure.</returns>
 public static bool gzrewind(PhpResource zp)
 {
     return(PhpPath.rewind(zp));
 }
示例#14
0
 /// <summary>
 /// Tests the given GZ file pointer for EOF.
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>Returns TRUE if the gz-file pointer is at EOF or an error occurs; otherwise returns FALSE.</returns>
 public static bool gzeof(PhpResource zp)
 {
     return(PhpPath.feof(zp));
 }
示例#15
0
 /// <summary>
 /// Closes the given gz-file pointer.
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>Returns TRUE on success or FALSE on failure.</returns>
 public static bool gzclose(PhpResource zp)
 {
     return(PhpPath.fclose(zp));
 }
示例#16
0
        /// <summary>
        /// Extracts part(s) from a specified path.
        /// </summary>
        /// <param name="path">The path to be parsed.</param>
        /// <param name="options">Flags determining the result.</param>
        /// <returns>
        /// If <paramref name="options"/> is <see cref="PathInfoOptions.All"/> then returns array
        /// keyed by <c>"dirname"</c>, <c>"basename"</c>, and <c>"extension"</c>. Otherwise,
        /// it returns string value containing a single part of the path.
        /// </returns>
        public static PhpValue pathinfo(string path, PathInfoOptions options = PathInfoOptions.All)
        {
            // collect strings
            string dirname = null, basename = null, extension = null, filename = null;

            if ((options & PathInfoOptions.BaseName) != 0 ||
                (options & PathInfoOptions.Extension) != 0 ||
                (options & PathInfoOptions.FileName) != 0)
            {
                basename = PhpPath.basename(path);
            }

            if ((options & PathInfoOptions.DirName) != 0)
            {
                dirname = PhpPath.dirname(path);
            }

            if ((options & PathInfoOptions.Extension) != 0)
            {
                int last_dot = basename.LastIndexOf('.');
                if (last_dot >= 0)
                {
                    extension = basename.Substring(last_dot + 1);
                }
            }

            if ((options & PathInfoOptions.FileName) != 0)
            {
                int last_dot = basename.LastIndexOf('.');
                if (last_dot >= 0)
                {
                    filename = basename.Substring(0, last_dot);
                }
                else
                {
                    filename = basename;
                }
            }

            // return requested value or all of them in an associative array
            if (options == PathInfoOptions.All)
            {
                var result = new PhpArray(4);
                result.Add("dirname", dirname);
                result.Add("basename", basename);
                result.Add("extension", extension);
                result.Add("filename", filename);
                return(PhpValue.Create(result));
            }

            if ((options & PathInfoOptions.DirName) != 0)
            {
                return(PhpValue.Create(dirname));
            }

            if ((options & PathInfoOptions.BaseName) != 0)
            {
                return(PhpValue.Create(basename));
            }

            if ((options & PathInfoOptions.Extension) != 0)
            {
                return(PhpValue.Create(extension));
            }

            if ((options & PathInfoOptions.FileName) != 0)
            {
                return(PhpValue.Create(filename));
            }

            return(PhpValue.Null);
        }
示例#17
0
 public static string gzgetss(PhpResource zp, int length = -1, string allowable_tags = null)
 {
     return(length < 0
         ? PhpPath.fgetss(zp)
         : PhpPath.fgetss(zp, length, allowable_tags));
 }
示例#18
0
 public static object gzgetss(PhpResource zp, int length = -1, string allowable_tags = null)
 {
     return((length == -1)
         ? PhpPath.fgetss(zp)
         : PhpPath.fgetss(zp, length, allowable_tags.ToString()));
 }