View source
<?php
namespace Drupal\imce;
use Drupal\Component\Utility\Environment;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Render\BubbleableMetadata;
class Imce {
public static function access(AccountProxyInterface $user = NULL, $scheme = NULL) {
return (bool) static::userProfile($user, $scheme);
}
public static function response(Request $request, AccountProxyInterface $user = NULL, $scheme = NULL) {
return static::userFM($user, $scheme, $request)
->pageResponse();
}
public static function userFM(AccountProxyInterface $user = NULL, $scheme = NULL, Request $request = NULL) {
if ($conf = static::userConf($user, $scheme)) {
return new ImceFM($conf, $user, $request);
}
}
public static function userProfile(AccountProxyInterface $user = NULL, $scheme = NULL) {
$profiles =& drupal_static(__METHOD__, []);
$user = $user ?: \Drupal::currentUser();
$scheme = isset($scheme) ? $scheme : \Drupal::config('system.file')
->get('default_scheme');
$profile =& $profiles[$user
->id()][$scheme];
if (isset($profile)) {
return $profile;
}
$profile = FALSE;
if (\Drupal::service('stream_wrapper_manager')
->getViaScheme($scheme)) {
$storage = \Drupal::entityTypeManager()
->getStorage('imce_profile');
if ($user
->id() == 1 && ($profile = $storage
->load('admin'))) {
return $profile;
}
$imce_settings = \Drupal::config('imce.settings');
$roles_profiles = $imce_settings
->get('roles_profiles');
$user_roles = array_flip($user
->getRoles());
$roles = array_reverse(user_roles());
foreach ($roles as $rid => $role) {
if (isset($user_roles[$rid]) && !empty($roles_profiles[$rid][$scheme])) {
if ($profile = $storage
->load($roles_profiles[$rid][$scheme])) {
return $profile;
}
}
}
}
return $profile;
}
public static function userConf(AccountProxyInterface $user = NULL, $scheme = NULL) {
$user = $user ?: \Drupal::currentUser();
$scheme = isset($scheme) ? $scheme : \Drupal::config('system.file')
->get('default_scheme');
if ($profile = static::userProfile($user, $scheme)) {
$conf = $profile
->getConf();
$conf['pid'] = $profile
->id();
$conf['scheme'] = $scheme;
return static::processUserConf($conf, $user);
}
}
public static function processUserConf(array $conf, AccountProxyInterface $user) {
$conf['maxsize'] *= 1048576;
$conf['quota'] *= 1048576;
$phpmaxsize = Environment::getUploadMaxSize();
if ($phpmaxsize && (!$conf['maxsize'] || $phpmaxsize < $conf['maxsize'])) {
$conf['maxsize'] = $phpmaxsize;
}
$conf['root_uri'] = $conf['scheme'] . '://';
$conf['root_url'] = preg_replace('@/(?:%2E|\\.)$@i', '', file_create_url($conf['root_uri'] . '.'));
if (!\Drupal::config('imce.settings')
->get('abs_urls')) {
$conf['root_url'] = file_url_transform_relative($conf['root_url']);
}
$conf['token'] = $user
->isAnonymous() ? 'anon' : \Drupal::csrfToken()
->get('imce');
$conf['folders'] = static::processUserFolders($conf['folders'], $user);
\Drupal::service('plugin.manager.imce.plugin')
->processUserConf($conf, $user);
return $conf;
}
public static function processUserFolders(array $folders, AccountProxyInterface $user) {
$ret = [];
$token_service = \Drupal::token();
$meta = new BubbleableMetadata();
$token_data = [
'user' => $user,
];
foreach ($folders as $folder) {
$path = $token_service
->replace($folder['path'], $token_data, [], $meta);
if (static::regularPath($path)) {
$ret[$path] = $folder;
unset($ret[$path]['path']);
}
}
return $ret;
}
public static function permissionInConf($permission, array $conf) {
if (!empty($conf['folders'])) {
foreach ($conf['folders'] as $folder_conf) {
if (static::permissionInFolderConf($permission, $folder_conf)) {
return TRUE;
}
}
}
return FALSE;
}
public static function permissionInFolderConf($permission, $folder_conf) {
if ($folder_conf && !empty($folder_conf['permissions'])) {
$permissions = $folder_conf['permissions'];
return isset($permissions[$permission]) ? (bool) $permissions[$permission] : !empty($permissions['all']);
}
return FALSE;
}
public static function folderInConf($path, array $conf) {
if (isset($conf['folders'][$path])) {
return $conf['folders'][$path];
}
if (!empty($conf['folders']) && static::regularPath($path) && is_dir(static::joinPaths($conf['root_uri'], $path))) {
foreach ($conf['folders'] as $folder_path => $folder_conf) {
$is_root = $folder_path === '.';
if ($is_root || strpos($path . '/', $folder_path . '/') === 0) {
if (static::permissionInFolderConf('browse_subfolders', $folder_conf)) {
if ($filter = static::nameFilterInConf($conf)) {
$rest = $is_root ? $path : substr($path, strlen($folder_path) + 1);
foreach (explode('/', $rest) as $name) {
if (preg_match($filter, $name)) {
return;
}
}
}
return $folder_conf + [
'inherited' => TRUE,
];
}
}
}
}
}
public static function nameFilterInConf(array $conf) {
$filters = isset($conf['name_filters']) ? $conf['name_filters'] : [];
if (empty($conf['allow_dot_files'])) {
$filters[] = '^\\.|\\.$';
}
return $filters ? '/' . implode('|', $filters) . '/' : '';
}
public static function splitPath($path) {
if (is_string($path) && $path != '') {
$parts = explode('/', $path);
$filename = array_pop($parts);
$dirpath = implode('/', $parts);
if ($filename !== '') {
return [
$dirpath === '' ? '.' : $dirpath,
$filename,
];
}
}
}
public static function joinPaths($dirpath, $filename) {
if ($dirpath === '.') {
return $filename;
}
if ($filename === '.') {
return $dirpath;
}
if (substr($dirpath, -1) !== '/') {
$dirpath .= '/';
}
return $dirpath . $filename;
}
public static function regularPath($path) {
return is_string($path) && ($path === '.' || !preg_match('@\\\\|(^|/)\\.*(/|$)@', $path));
}
public static function scanDir($diruri, array $options = []) {
$content = [
'files' => [],
'subfolders' => [],
];
$browse_files = isset($options['browse_files']) ? $options['browse_files'] : TRUE;
$browse_subfolders = isset($options['browse_subfolders']) ? $options['browse_subfolders'] : TRUE;
if (!$browse_files && !$browse_subfolders) {
return $content;
}
if (!($opendir = opendir($diruri))) {
return $content + [
'error' => TRUE,
];
}
$name_filter = empty($options['name_filter']) ? FALSE : $options['name_filter'];
$callback = empty($options['filter_callback']) ? FALSE : $options['filter_callback'];
$uriprefix = substr($diruri, -1) === '/' ? $diruri : $diruri . '/';
while (($filename = readdir($opendir)) !== FALSE) {
if ($filename === '.' || $filename === '..') {
continue;
}
if ($name_filter && preg_match($name_filter, $filename)) {
continue;
}
$fileuri = $uriprefix . $filename;
$is_dir = is_dir($fileuri);
if ($is_dir ? !$browse_subfolders : !$browse_files) {
continue;
}
if ($callback) {
$result = $callback($filename, $is_dir, $fileuri, $options);
if ($result === 'continue') {
continue;
}
elseif ($result === 'break') {
break;
}
}
$content[$is_dir ? 'subfolders' : 'files'][$filename] = $fileuri;
}
closedir($opendir);
return $content;
}
public static function getFileEntity($uri, $create = FALSE, $save = FALSE) {
$file = FALSE;
if ($files = \Drupal::entityTypeManager()
->getStorage('file')
->loadByProperties([
'uri' => $uri,
])) {
$file = reset($files);
}
elseif ($create) {
$file = static::createFileEntity($uri, $save);
}
return $file;
}
public static function createFileEntity($uri, $save = FALSE) {
$values = [
'uri' => $uri,
'uid' => \Drupal::currentUser()
->id(),
'status' => 1,
'filesize' => filesize($uri),
'filename' => \Drupal::service('file_system')
->basename($uri),
'filemime' => \Drupal::service('file.mime_type.guesser')
->guess($uri),
];
$file = \Drupal::entityTypeManager()
->getStorage('file')
->create($values);
if ($save) {
$file
->save();
}
return $file;
}
public static function accessFilePaths(array $paths, AccountProxyInterface $user = NULL, $scheme = NULL) {
$ret = [];
if ($fm = static::userFM($user, $scheme)) {
foreach ($paths as $path) {
if ($fm
->checkFile($path)) {
$ret[] = $path;
}
}
}
return $ret;
}
public static function accessFileUri($uri, AccountProxyInterface $user = NULL) {
list($scheme, $path) = explode('://', $uri, 2);
return $scheme && $path && Imce::accessFilePaths([
$path,
], $user, $scheme);
}
}