@ -2,19 +2,132 @@
/* For licensing terms, see /license.txt */
namespace ChamiloLMS\Component\Editor;
use \Entity\User;
use \Entity\Course;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Routing\Router;
use ChamiloLMS\Component\Editor\Driver\Driver;
use Symfony\Component\Security\Core\SecurityContext;
/**
* Class elfinder Connector - editor + Chamilo repository
* @package ChamiloLMS\Component\Editor
*/
class Connector
{
/** @var Course */
public $course;
/** @var User */
public $user;
public $drivers = array();
public $driverList = array();
/** @var Translator */
public $translator;
/** @var Router */
public $urlGenerator;
/** @var SecurityContext */
public $security;
public $paths;
public function __construct(
$paths,
Router $urlGenerator,
Translator $translator,
SecurityContext $security,
User $user,
$course = null
) {
$this->translator = $translator;
$this->user = $user;
$this->course = $course;
$this->urlGenerator = $urlGenerator;
$this->paths = $paths;
$this->security = $security;
$this->driverList = $this->getDefaultDriverList();
}
/**
*
* @param Driver $driver
*/
public function addDriver($driver)
{
if (!empty($driver)) {
$this->drivers[$driver->getName()] = $driver;
}
}
/**
* @return array
*/
public function getDrivers()
{
return $this->drivers;
}
/**
* @param string $driverName
* @return Driver $driver
*/
public function getDriver($driverName)
{
if (isset($this->drivers[$driverName])) {
return $this->drivers[$driverName];
}
return null;
}
/**
* @param bool $processDefaultValues
* @return array
*/
public function __construct()
public function getRoots($processDefaultValues = true )
{
$roots = array();
/** @var Driver $driver */
$drivers = $this->getDrivers();
foreach ($drivers as $driver) {
if ($processDefaultValues) {
$root = $driver->updateWithDefaultValues($driver->getConfiguration());
}
$roots[] = $root;
}
return $roots;
}
/**
* @return array
*/
public function getOperations()
{
//https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options-2.1
$opts = array(
//'debug' => true,
'bind' => array(
'upload rm' => array($this, 'manageCommands')
)
);
foreach ($this->getDriverList() as $driverName) {
$driverClass = $this->getDriverClass($driverName);
/** @var Driver $driver */
$driver = new $driverClass();
$driver->setName($driverName);
$driver->setConnector($this);
$this->addDriver($driver);
}
$opts['roots'] = $this->getRoots();
return $opts;
}
/**
* Simple function to demonstrate how to control file access using "accessControl" callback.
* This method will disable accessing files/folders starting from '.' (dot)
@ -27,260 +140,103 @@ class Connector
**/
public function access($attr, $path, $data, $volume)
{
//error_log($path); error_log($attr);
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
}
/**
* Simple callback catcher
*
* @param string $cmd command name
* @param array $result command result
* @param array $args command arguments from client
* @param \elFinder $elfinder elFinder instance
* @return void|true
**/
function logger($cmd, $result, $args, $elfinder)
* @return array
*/
public function getDriverList()
{
// do something here
// echo $cmd;
$courseInfo = api_get_course_info();
/*error_log($cmd);
error_log(print_r($result, 1));*/
//error_log(print_r($args,1));
//error_log(print_r($elfinder,1));
switch($cmd) {
case 'mkdir':
break;
case 'mkfile':
break;
case 'rename':
break;
case 'duplicate':
break;
case 'upload':
// Files added
if (isset($result['added'])) {
foreach ($result['added'] as $file) {
$name = $file['name'];
$webalized = \URLify::filter($file['name'], 80);
if (strcmp($name, $webalized) != 0) {
$arg = array('target' => $file['hash'], 'name' => $webalized);
$elfinder->exec('rename', $arg);
}
$realPath = $elfinder->realpath($file['hash']);
//error_log($realPath);
if (!empty($realPath)) {
// Getting file info
$info = $elfinder->exec('file', array('target' => $file['hash']));
/** @var elFinderVolumeLocalFileSystem $volume */
$volume = $info['volume'];
$root = $volume->root();
//var/www/chamilogits/data/courses/NEWONE/document
$realPathRoot = $elfinder->realpath($root);
// Removing course path
$realPath = str_replace($realPathRoot, '', $realPath);
\FileManager::add_document($courseInfo, $realPath, 'file', intval($file['size']), $file['name']);
}
}
}
break;
case 'rm':
if (isset($result['removed'])) {
foreach ($result['removed'] as $file) {
$realFilePath = $file['realpath'];
$filePath = str_replace($courseInfo['course_sys_data'].'document', '', $realFilePath);
\DocumentManager::delete_document($courseInfo, $filePath, $courseInfo['course_sys_data'].'document');
}
}
break;
case 'paste':
break;
}
return $this->driverList;
}
/**
* Available driver list.
* @return array
*/
public function getOperations( )
public function setDriverList($list)
{
$opts = array(
//'debug' => true,
'bind' => array(
'mkdir mkfile rename duplicate upload rm paste' => array($this, 'logger')
//'mkdir mkfile rename duplicate upload rm paste' => 'chamilo'
)
);
$courseInfo = api_get_course_info();
$userId = api_get_user_id();
$commonAttributes = array(
// Hiding dangerous files
array(
'pattern' => '/\.(php|py|pl|sh|xml)$/i',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false
),
// Hiding _DELETED_ files
array(
'pattern' => '/_DELETED_/',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false
),
// Hiding thumbnails
array(
'pattern' => '/.tmb/',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false
),
array(
'pattern' => '/.thumbs/',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false
),
array(
'pattern' => '/.quarantine/',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false
)
);
/*
var defaultCommands = [
'open', 'reload', 'home', 'up', 'back', 'forward', 'getfile', 'quicklook',
'download', 'rm', 'duplicate', 'rename', 'mkdir', 'mkfile', 'upload', 'copy',
'cut', 'paste', 'edit', 'extract', 'archive', 'search', 'info', 'view', 'help',
'resize', 'sort'
];
*/
$this->driverList = $list;
}
// for more options: https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options
$commonSettings = array(
'uploadOverwrite' => false, // Replace files on upload or give them new name if the same file was uploaded
//'acceptedName' =>
'uploadAllow' => array(
'image',
'audio',
'video',
'text/html',
'application/pdf',
'application/postscript',
'application/vnd.ms-word',
'application/vnd.ms-excel',
'application/vnd.ms-powerpoint',
'application/pdf',
'application/xml',
'application/vnd.oasis.opendocument.text',
'application/x-shockwave-flash'
), # allow files
//'uploadDeny' => array('text/x-php'),
'uploadOrder' => array('allow'), // only executes allow
'disabled' => array (
'duplicate', 'rename', 'mkdir', 'mkfile', 'copy', 'cut', 'paste', 'edit', 'extract', 'archive', 'help', 'resize'
)
/**
* Available driver list.
* @return array
*/
private function getDefaultDriverList()
{
return array(
'CourseDriver',
'CourseUserDriver',
'HomeDriver',
'PersonalDriver'
);
}
if (!empty($courseInfo)) {
// Adding course driver
$opts['roots'][] = array(
'driver' => 'LocalFileSystem',
'path' => api_get_path(SYS_DATA_PATH).'courses/'.$courseInfo['path'].'/document',
'startPath' => '/',
'URL' => api_get_path(REL_COURSE_PATH).$courseInfo['path'].'/document',
//'alias' => $courseInfo['code'].' documents',
'accessControl' => array($this, 'access'),
'attributes' => array(
// Hide shared_folder
array(
'pattern' => '/shared_folder/',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false
),
)
);
/**
* @param string $cmd
* @param array $result
* @param array $args
* @param Finder $elFinder
*/
public function manageCommands($cmd, $result, $args, $elFinder)
{
$cmd = ucfirst($cmd);
$cmd = 'after'.$cmd;
if (isset($args['target'])) {
$driverName = $elFinder->getVolumeDriverNameByTarget($args['target']);
}
// Adding course user file driver
if (isset($args['targets'])) {
foreach ($args['targets'] as $target) {
$driverName = $elFinder->getVolumeDriverNameByTarget($target);
break;
}
}
if (!empty($userId)) {
$opts['roots'][] = array(
'driver' => 'LocalFileSystem',
'path' => api_get_path(SYS_DATA_PATH).'courses/'.$courseInfo['path'].'/document/shared_folder/sf_user_'.$userId,
'startPath' => '/',
//'alias' => $courseInfo['code'].' personal documents',
'URL' => api_get_path(REL_COURSE_PATH).$courseInfo['path'].'/document/shared_folder/sf_user_'.$userId,
'accessControl' => 'access'
);
if (empty($driverName)) {
return false;
}
// Adding user personal files
if (!empty($result['error'])) {
}
$dir = \UserManager::get_user_picture_path_by_id($userId, 'system');
$dirWeb = \UserManager::get_user_picture_path_by_id($userId, 'web');
if (!empty($result['warning'])) {
}
$opts['roots'][] = array(
'driver' => 'LocalFileSystem',
'path' => $dir['dir'].'my_files',
'startPath' => '/',
//'alias' => 'Personal documents',
'URL' => $dirWeb['dir'].'my_files',
'accessControl' => 'access'
);
if (!empty($result['removed'])) {
foreach ($result['removed'] as $file) {
/** @var Driver $driver */
$driver = $this->getDriver($driverName);
$driver->$cmd($file, $args, $elFinder);
// removed file contain additional field "realpath"
//$log .= "\tREMOVED: ".$file['realpath']."\n";
}
} else {
// Adding user personal files
$dir = \UserManager::get_user_picture_path_by_id($userId, 'system');
$dirWeb = \UserManager::get_user_picture_path_by_id($userId, 'web');
$opts['roots'][] = array(
'driver' => 'LocalFileSystem',
'path' => $dir['dir'].'my_files',
'startPath' => '/',
'URL' => $dirWeb['dir'].'my_files',
'accessControl' => 'access'
);
}
// Add home portal
if (api_is_platform_admin()) {
$home = api_get_path(SYS_DATA_PATH).'home';
$opts['roots'][] = array(
'driver' => 'LocalFileSystem',
'path' => $home,
'startPath' => '/',
'URL' => api_get_path(WEB_DATA_PATH).'home',
'accessControl' => 'access'
);
if (!empty($result['added'])) {
foreach ($result['added'] as $file) {
$driver = $this->getDriver($driverName);
$driver->$cmd($file, $args, $elFinder);
}
}
// Injecting common file filters
foreach ($opts['roots'] as & $driver) {
$driver = array_merge($driver, $commonSettings);
if (isset($driver['attributes'])) {
$driver['attributes'] = array_merge($driver['attributes'], $commonAttributes);
} else {
$driver['attributes'] = $commonAttributes;
if (!empty($result['changed'])) {
foreach ($result['changed'] as $file) {
//$log .= "\tCHANGED: ".$elfinder->realpath($file['hash'])."\n";
}
}
}
return $opts;
/**
* @param string $driver
* @return string
*/
private function getDriverClass($driver)
{
return 'ChamiloLMS\Component\Editor\Driver\\'.$driver;
}
}