/** Datei: library/ARUNOX/API/Filesystem/Filesystem.php **/ path, $rootUrl, $filename ); } public static function basename( string $filename ) : bool|string { return pathinfo( $filename, PATHINFO_BASENAME ); } public static function extension( string $filename ) : bool|string { return pathinfo( $filename, PATHINFO_EXTENSION ); } public static function filesize( string $filename ) : bool|string { return filesize( $filename ); } public static function filesizes( string $filename ) : array { $info = getimagesize( $filename ); return [ "w" => $info[0], "h" => $info[1] ]; } public static function filehash( string $filename ) : bool|string { return hash_file("sha256", $filename ); } public static function created( string $filename ) : int { $stat = stat( $filename ); return $stat['ctime'] ?? 0; } public static function modified( string $filename ) : int { $stat = stat( $filename ); return $stat['mtime'] ?? 0; } public static function permissions( string $filename ) : int { $stat = stat( $filename ); return $stat['mode'] ?? 0; } public static function upload( array $file, string $destination, null|string $filename = NULL ) : null|File { if( !isset( $file["tmp_name"], $file["name"]) || !is_uploaded_file( $file["tmp_name"] ) ) { return NULL; } $extension = static::extension( $file["name"] ); $filename = static::filename( $filename ?? $file["name"] ); $destination = rtrim( $destination, DIRECTORY_SEPARATOR ); $target = $destination . DIRECTORY_SEPARATOR . $filename . "." . $extension; if( !static::existsDir( $destination ) ) static::createDir( $destination ); // show( $filename, $extension, $destination, $target, "STOP "); if( move_uploaded_file( $file["tmp_name"], $target ) ) { return static::getFile( $target ); } return NULL; } /* ### PUBLIC STATIC DIRECTORY FUNCTIONS ######################## # */ public static function isDir( string $directory ) : bool { return is_dir( $directory ); } public static function existsDir( string $directory ) : bool { return file_exists( $directory ); } public static function createDir( string $directory, int $mode = 0777, bool $recursive = TRUE ) : bool { return @mkdir( $directory, $mode, $recursive ) !== FALSE; } public static function readDir( string $directory ) : array { $list = []; foreach( new DirectoryIterator( $directory ) as $item ) { if( !$item->isDot() ) { $list[] = $directory . DIRECTORY_SEPARATOR . $item->getFilename(); } } return $list; } public static function listDirs( string $directory, bool $recursive = FALSE ) : array { $list = []; foreach( new DirectoryIterator( $directory ) as $item ) { if( !$item->isDot() && $item->isDir()) { $list[] = $directory . DIRECTORY_SEPARATOR . $item->getFilename(); } } return $list; } public static function listFiles( string $directory, bool $recursive = FALSE ) : array { $list = []; foreach( new DirectoryIterator( $directory ) as $item ) { if( !$item->isDot() && $item->isFile() ) { $list[] = $directory . DIRECTORY_SEPARATOR . $item->getFilename(); } if( $recursive && !$item->isDot() && $item->isDir() ) { $files = static::listFiles( $directory . DIRECTORY_SEPARATOR . $item->getFilename(), TRUE ); array_push( $list, ...$files ); } } return $list; } public static function removeDir( string $directory ) : bool { return rmdir( $directory ); } public static function moveDir( string $directory, string $destination ) : bool { return rename( $directory, $destination ); } public static function copyDir( string $directory, string $destination ) : bool { return copy( $directory, $destination ); } public static function getDir( string $directory ) : null|Directory { return file_exists( $directory ) ? new Directory( realpath( $directory ), static::class ) : NULL; } public static function putDir( string $directory, int $mode = 0777, bool $recursive = TRUE ) : bool|Directory { if( static::createDir( $directory, $mode, $recursive ) !== FALSE ) { return static::getDir( $directory ); } return FALSE; } public static function dirname( string $directory ) : bool|string { return pathinfo( $directory, PATHINFO_DIRNAME ); } } /** Datei: library/ARUNOX/API/Filesystem/Directory.php **/ files(), $this->directories() ) ); } public function files( bool $recursive = FALSE ) : array { if( $this->files === NULL || $this->fileRecursive != $recursive ) { $this->fileRecursive = $recursive; $this->loadFiles( $recursive ); } return $this->files; } public function directories( bool $recursive = FALSE ) : array { if( $this->directories === NULL || $this->dirsRecursive != $recursive ) { $this->dirsRecursive = $recursive; $this->loadDirs( $recursive ); } return $this->directories; } /* ### PROTECTED FUNCTIONS ###################################### # */ protected function loadFiles( bool $recursive = FALSE ) : void { $this->files = $this->filesystem::listFiles( $this->path, $recursive ); foreach( $this->files as &$file ) { $file = $this->filesystem::getFile( $file ); } } protected function loadDirs( bool $recursive = FALSE ) : void { $this->directories = $this->filesystem::listDirs( $this->path, $recursive ); foreach( $this->directories as &$dir ) { $dir = $this->filesystem::getDir( $dir ); } } /* ### PUBLIC MAGIC FUNCTIONS ################################### # */ public function __construct( string $directory, string $filesystem ) { if( !class_exists( $filesystem ) ) throw new RuntimeException( "Filesystem `{$filesystem}` ist missing." ); if( !$filesystem::isDir( $directory ) ) throw new RuntimeException( "Directory `{$directory}` not found." ); $this->path = $directory; $this->filesystem = $filesystem; $this->name = $filesystem::basename( $directory ); } public function __get( string $name ) { if( method_exists( $this, $name ) ) { return $this->{$name}(); } } public function __toString() : string { return $this->path; } public static function __callStatic( string $name, array $args ) { if( method_exists( Filesystem::class, "{$name}Dir" ) ) { return Filesystem::{"{$name}Dir"}( ...$args ); } } } /** Datei: library/ARUNOX/API/Filesystem/File.php **/ content === NULL ) { $this->content = $this->filesystem::readFile( $this->path ); } return $this->content; } public function versioned() : string { return "{$this->url}?_={$this->modified()}"; } public function size() : int { if( $this->size === NULL ) { $this->size = $this->filesystem::filesize( $this->path ); } return $this->size; } public function sizes() : array { if( $this->sizes === NULL ) { $this->sizes = $this->filesystem::filesizes( $this->path ); } return $this->sizes; } public function mime() : string { if( $this->mime === NULL ) { $this->mime = $this->filesystem::mimetype( $this->path ); } return $this->mime; } public function hash() : string { if( $this->hash === NULL ) { $this->hash = $this->filesystem::filehash( $this->path ); } return $this->hash; } public function created() : int { if( $this->created === NULL ) { $this->created = $this->filesystem::created( $this->path ); } return $this->created; } public function modified() : int { if( $this->modified === NULL ) { $this->modified = $this->filesystem::modified( $this->path ); } return $this->modified; } public function permissions() : int { if( $this->permissions === NULL ) { $this->permissions = $this->filesystem::permissions( $this->path ); } return $this->permissions; } /* ### PROTECTED FUNCTIONS ###################################### # */ /* ### PUBLIC MAGIC FUNCTIONS ################################### # */ public function __construct( string $filename, string $filesystem ) { if( !class_exists( $filesystem ) ) throw new RuntimeException( "Filesystem `{$filesystem}` ist missing." ); if( !$filesystem::isFile( $filename ) ) throw new RuntimeException( "File `{$filename}` not found." ); $this->path = $filename; $this->filesystem = $filesystem; $this->basename = $filesystem::basename( $filename ); $this->extension = $filesystem::extension( $filename ); $this->filename = $filesystem::filename( $filename ); $this->dirname = $filesystem::dirname( $filename ); $this->url = $filesystem::fileurl( $filename ); } public function __get( string $name ) { if( method_exists( $this, $name ) ) { return $this->{$name}(); } } }