Created
April 11, 2017 07:05
-
-
Save mungurs/6f01ae5c00f6b2db19354b709b135217 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class fileManagerFacade { | |
private $fileSync = null; | |
private $elFinder = null; | |
private $connector = null; | |
private $rootDirName = 'files-manager'; | |
private $visibleFileTypes = []; | |
private $fileTypes = ['images']; | |
public function __construct($visibleFileTypes = []) | |
{ | |
$this->setVisibleFileTypes($visibleFileTypes); | |
$this->createRootDir(); | |
$this->fileSync = new leafFileSync(); | |
$this->elFinder = new elFinder($this->getSettings()); | |
$this->connector = new elFinderConnector($this->elFinder); | |
} | |
/** | |
* Outputs api response | |
*/ | |
public function run(){ | |
$this->connector->run(); | |
} | |
public function createFileRelation($hash, $ownerId, $ownerClass){ | |
$file = $this->getFile($hash); | |
$fileRelation = new leafFileRelation([ | |
'ownerClass' => $ownerClass, | |
'ownerObjectId' => $ownerId, | |
'fileId' => $file->id | |
]); | |
$fileRelation->save(); | |
return $fileRelation; | |
} | |
/** | |
* Returns leafFile from filehash | |
* | |
* @param $hash | |
* @return leafFile | |
* @throws Exception | |
*/ | |
public function getFile($hash){ | |
$absPath = $this->elFinder->realpath($hash); | |
if($absPath == false){ | |
throw new Exception('Invalid file hash'); | |
} | |
$relativePath = self::getRelativeFilePath($absPath); | |
/** @var leafFile $file */ | |
$file = leafFile::getByPath($relativePath); | |
if(empty($file)) { | |
throw new Exception('Missing leafFile for requested path'); | |
} | |
return $file; | |
} | |
private function getSettings(){ | |
$settings = [ | |
'locale' => '', | |
'debug' => false, //TODO: disable! | |
'bind' => [ | |
'mkdir.pre mkfile.pre rename.pre' => [ | |
'Plugin.Sanitizer.cmdPreprocess' | |
], | |
'upload.presave' => [ | |
'Plugin.Sanitizer.onUpLoadPreSave' | |
], | |
// sync with leafFile table | |
'mkfile rename duplicate upload rm paste' => [ | |
[$this->fileSync, 'sync'] | |
] | |
], | |
'plugin' => [ | |
'Sanitizer' => [ | |
'enable' => true, | |
'targets' => ['\\','/',':','*','?','"','<','>','|'], // target chars | |
'replace' => '_' // replace to this | |
] | |
], | |
'roots' => [ | |
[ | |
'id' => 'r', // https://github.com/Studio-42/elFinder/issues/1781 | |
'alias' => $this->rootDirName, | |
'accessControl' => [$this, 'access'], | |
'driver' => 'LocalFileSystem', //default | |
'path' => $this->getRootPath(), | |
'URL' => $this->getRootUrl(), | |
'attributes' => $this->getAttributes(), | |
'plugin' => [ | |
'Sanitizer' => [ | |
'enable' => true, | |
'targets' => ['\\','/',':','*','?','"','<','>','|'], // target chars | |
'replace' => '_' // replace to this | |
] | |
] | |
] | |
] | |
]; | |
return $settings; | |
} | |
private function setVisibleFileTypes($types = []){ | |
foreach($types as $type){ | |
if(!in_array($type, $this->fileTypes)){ | |
throw new Exception('Non-existent file group '); | |
} | |
$this->visibleFileTypes[] = $type; | |
} | |
} | |
/** | |
* This method will disable accessing files/folders starting from '.' (dot) | |
* | |
* @param string $attr attribute name (read|write|locked|hidden) | |
* @param string $path file path relative to volume root directory started with directory separator | |
* @return bool|null | |
**/ | |
public function access($attr, $path, $data, $volume) { | |
return strpos(basename($path), '.') === 0 // if file/folder begins with '.' (dot) | |
? !($attr == 'read' || $attr == 'write') // set read+write to false, other (locked+hidden) set to true | |
: null; // else elFinder decide it itself | |
} | |
/** | |
* @return string file managers absolute root directory path | |
*/ | |
private function getRootPath(){ | |
return LEAF_FILE_ROOT . $this->rootDirName . DIRECTORY_SEPARATOR; | |
} | |
/** | |
* @return string file managers root directory url | |
*/ | |
private function getRootUrl(){ | |
return LEAF_FILE_ROOT_WWW . $this->rootDirName; | |
} | |
/** | |
* Generates attributes for specific file group/s | |
* @param array $reqAttributes | |
* @return array https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options#attributes | |
*/ | |
private function getAttributes($reqAttributes = []){ | |
$attributes = []; | |
$reqTypes = $this->visibleFileTypes; | |
if($reqTypes && in_array('images', $reqTypes)){ | |
$attributes[] = [ // hide all files/folders with a dot (.) inside of them | |
'pattern' => '/.*[.]+.*/', | |
'hidden' => true | |
]; | |
// show files with image extensions | |
$attributes[] = [ | |
'pattern' => '/([^\s]+(\.(?i)(jpg|png|gif|bmp|svg))$)/', //You can add extra image extensions here | |
'read' => true, | |
'write' => true, | |
'locked' => false, | |
'hidden' => false | |
]; | |
} | |
//Extra groups or attributes add here using images as example | |
return $attributes; | |
} | |
/** | |
* Creates root dir if it does not exist | |
*/ | |
private function createRootDir(){ | |
if(!file_exists($this->getRootPath())){ | |
mkdir($this->getRootPath(), 0755, true); | |
} | |
} | |
public static function normalizePath($path){ | |
return str_replace('\\', '/', $path); | |
} | |
public static function getRelativeFilePath($fileAbsPath){ | |
$absPath = fileManagerFacade::normalizePath($fileAbsPath); | |
$rootPath = fileManagerFacade::normalizePath(LEAF_FILE_ROOT); | |
$relativePath = str_replace($rootPath, '', $absPath ); | |
return $relativePath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment