diff --git a/file.php b/file.php index ce8452a..55981ef 100644 --- a/file.php +++ b/file.php @@ -41,6 +41,80 @@ class file return true; } + /** Change the group for a file/dir... + * @param $filename string The file/directory to change + * @param $group mixed The group name or group GID + * @throws If filename not exists, or the directory is not RW + */ + public function chgrp ($filename, $group) + { + $this->debug (2, "chgrp ($filename, $group)"); + $filename = $this->realpath ($filename); + $this->checkPathRW (dirname ($filename)); + if (! file_exists ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' to chmod doesn't exists"), + $filename), 404); + if (! is_writeable ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' to chmod is not writeable"), + $filename), 500); + $rc = chgrp ($filename, $group); + $this->debug (1, "chgrp ($filename, $group) => $rc"); + return $rc; + } + + /** Change the rights mode for a file/dir... + * @param $filename string The file/directory to change + * @throws If filename not exists, or the directory is not RW + */ + public function chmod ($filename, $mode) + { + $this->debug (2, "chmod ($filename, $mode)"); + $filename = $this->realpath ($filename); + $this->checkPathRW (dirname ($filename)); + if (! file_exists ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' to chmod doesn't exists"), + $filename), 404); + if (! is_writeable ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' to chmod is not writeable"), + $filename), 500); + $rc = chmod ($filename, $mode); + $this->debug (1, "chmod ($filename, $mode) => $rc"); + return $rc; + } + + /** Change the owner for a file/dir... + * @param $filename string The file/directory to change + * @param $user mixed The user name or user UID + * @throws If filename not exists, or the directory is not RW + */ + public function chown ($filename, $user) + { + $this->debug (2, "chown ($filename, $user)"); + if (posix_getuid () !== 0 && + posix_getuid () !== $user && + posix_getpwuid (posix_getuid()) !== $user) + throw new \Exception (sprintf (dgettext ("domframework", + "Only root user can change the file owner for '%s'"), + $filename), 500); + $filename = $this->realpath ($filename); + $this->checkPathRW (dirname ($filename)); + if (! file_exists ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' to chmod doesn't exists"), + $filename), 404); + if (! is_writeable ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' to chmod is not writeable"), + $filename), 500); + $rc = chown ($filename, $user); + $this->debug (1, "chown ($filename, $user) => $rc"); + return $rc; + } + /** Chroot in the provided directory * @param $directory string The directory to chroot * @return true if the chroot is done, false if there is a failure