/** * Return an array of files and folders at this current level in the directory structure * * @param string $path * @return array */ public function getContainerList($path) { $path = $this->fileHandler->postfixSlash($path); $bases = $this->getBases($path); if (empty($bases['pathAbsolute'])) { return array(); } $fullPath = $bases['pathAbsolute'] . ltrim($path, '/'); $useMultibyte = $this->getOption('use_multibyte', $this->properties, false); $encoding = $this->getOption('modx_charset', $this->properties, 'UTF-8'); $hideFiles = !empty($this->properties['hideFiles']) && $this->properties['hideFiles'] != 'false' ? true : false; $editAction = $this->getEditActionId(); $imagesExts = $this->getOption('imageExtensions', $this->properties, 'jpg,jpeg,png,gif'); $imagesExts = explode(',', $imagesExts); $skipFiles = $this->getOption('skipFiles', $this->properties, '.svn,.git,_notes,.DS_Store,nbproject,.idea'); $skipFiles = explode(',', $skipFiles); $skipFiles[] = '.'; $skipFiles[] = '..'; $directories = array(); $files = array(); if (!is_dir($fullPath)) { return array(); } /* iterate through directories */ /** @var DirectoryIterator $file */ foreach (new DirectoryIterator($fullPath) as $file) { if (in_array($file, $skipFiles)) { continue; } if (!$file->isReadable()) { continue; } $fileName = $file->getFilename(); $filePathName = $file->getPathname(); $octalPerms = substr(sprintf('%o', $file->getPerms()), -4); /* handle dirs */ $cls = array(); if ($file->isDir() && $this->hasPermission('directory_list')) { $cls[] = 'folder'; if ($this->hasPermission('directory_chmod')) { $cls[] = 'pchmod'; } if ($this->hasPermission('directory_create')) { $cls[] = 'pcreate'; } if ($this->hasPermission('directory_remove')) { $cls[] = 'premove'; } if ($this->hasPermission('directory_update')) { $cls[] = 'pupdate'; } if ($this->hasPermission('file_upload')) { $cls[] = 'pupload'; } if ($this->hasPermission('file_create')) { $cls[] = 'pcreate'; } $directories[$fileName] = array('id' => $bases['urlRelative'] . rtrim($fileName, '/') . '/', 'text' => $fileName, 'cls' => implode(' ', $cls), 'type' => 'dir', 'leaf' => false, 'path' => $bases['pathAbsolute'] . $fileName, 'pathRelative' => $bases['pathRelative'] . $fileName, 'perms' => $octalPerms, 'menu' => array()); $directories[$fileName]['menu'] = array('items' => $this->getListContextMenu($file, $directories[$fileName])); } /* get files in current dir */ if ($file->isFile() && !$hideFiles && $this->hasPermission('file_list')) { $ext = pathinfo($filePathName, PATHINFO_EXTENSION); $ext = $useMultibyte ? mb_strtolower($ext, $encoding) : strtolower($ext); $cls = array(); $cls[] = 'icon-file'; $cls[] = 'icon-' . $ext; if (!empty($this->properties['currentFile']) && rawurldecode($this->properties['currentFile']) == $fullPath . $fileName && $this->properties['currentAction'] == $editAction) { $cls[] = 'active-node'; } if ($this->hasPermission('file_remove')) { $cls[] = 'premove'; } if ($this->hasPermission('file_update')) { $cls[] = 'pupdate'; } if (!$file->isWritable()) { $cls[] = 'icon-lock'; } $encFile = rawurlencode($fullPath . $fileName); $page = !empty($editAction) ? '?a=' . $editAction . '&file=' . $bases['urlRelative'] . $fileName . '&wctx=' . $this->ctx->get('key') . '&source=' . $this->get('id') : null; $url = ($bases['urlIsRelative'] ? $bases['urlRelative'] : $bases['url']) . ($path != './' ? $path : '') . $fileName; /* get relative url from manager/ */ $fromManagerUrl = $bases['url'] . trim(str_replace('//', '/', $path . $fileName), '/'); $fromManagerUrl = ($bases['urlIsRelative'] ? '../' : '') . $fromManagerUrl; $files[$fileName] = array('id' => $bases['urlRelative'] . $fileName, 'text' => $fileName, 'cls' => implode(' ', $cls), 'type' => 'file', 'leaf' => true, 'qtip' => in_array($ext, $imagesExts) ? '<img src="' . $fromManagerUrl . '" alt="' . $fileName . '" />' : '', 'page' => $this->fileHandler->isBinary($filePathName) ? $page : null, 'perms' => $octalPerms, 'path' => $bases['pathAbsolute'] . $fileName, 'pathRelative' => $bases['pathRelative'] . $fileName, 'directory' => $bases['path'], 'url' => $url, 'file' => $encFile, 'menu' => array()); $files[$fileName]['menu'] = array('items' => $this->getListContextMenu($file, $files[$fileName])); } } $ls = array(); /* now sort files/directories */ ksort($directories); foreach ($directories as $dir) { $ls[] = $dir; } ksort($files); foreach ($files as $file) { $ls[] = $file; } return $ls; }
/** * Get a list of files in a specific directory. * * @param string $path * @return array */ public function getObjectsInContainer($path) { $properties = $this->getPropertyList(); $dir = $this->fileHandler->postfixSlash($path); $bases = $this->getBases($dir); if (empty($bases['pathAbsolute'])) { return array(); } $fullPath = $bases['pathAbsolute'] . $dir; $modAuth = $this->xpdo->user->getUserToken($this->xpdo->context->get('key')); /* get default settings */ $imageExtensions = $this->getOption('imageExtensions', $properties, 'jpg,jpeg,png,gif'); $imageExtensions = explode(',', $imageExtensions); $use_multibyte = $this->ctx->getOption('use_multibyte', false); $encoding = $this->ctx->getOption('modx_charset', 'UTF-8'); $allowedFileTypes = $this->getOption('allowedFileTypes', $properties, ''); $editAction = $this->getEditActionId(); if (is_string($allowedFileTypes)) { if (empty($allowedFileTypes)) { $allowedFileTypes = array(); } else { $allowedFileTypes = explode(',', $allowedFileTypes); } } $thumbnailType = $this->getOption('thumbnailType', $properties, 'png'); $thumbnailQuality = $this->getOption('thumbnailQuality', $properties, 90); $skipFiles = $this->getOption('skipFiles', $properties, '.svn,.git,_notes,nbproject,.idea,.DS_Store'); $skipFiles = explode(',', $skipFiles); $skipFiles[] = '.'; $skipFiles[] = '..'; /* iterate */ $files = array(); $filenames = array(); if (!is_dir($fullPath)) { $this->addError('dir', $this->xpdo->lexicon('file_folder_err_ns') . $fullPath); return array(); } /** @var DirectoryIterator $file */ foreach (new DirectoryIterator($fullPath) as $file) { if (in_array($file, $skipFiles)) { continue; } if (!$file->isReadable()) { continue; } $fileName = $file->getFilename(); $filePathName = $file->getPathname(); if (!$file->isDir()) { $fileExtension = pathinfo($filePathName, PATHINFO_EXTENSION); $fileExtension = $use_multibyte ? mb_strtolower($fileExtension, $encoding) : strtolower($fileExtension); if (!empty($allowedFileTypes) && !in_array($fileExtension, $allowedFileTypes)) { continue; } $filesize = @filesize($filePathName); $url = urlencode(ltrim($dir . $fileName, '/')); $page = !empty($editAction) ? '?a=' . $editAction . '&file=' . $bases['urlRelative'] . $fileName . '&wctx=' . $this->ctx->get('key') . '&source=' . $this->get('id') : null; /* get thumbnail */ if (in_array($fileExtension, $imageExtensions)) { $preview = 1; $imageWidth = $this->ctx->getOption('filemanager_image_width', 400); $imageHeight = $this->ctx->getOption('filemanager_image_height', 300); $thumbWidth = $this->ctx->getOption('filemanager_thumb_width', 100); $thumbHeight = $this->ctx->getOption('filemanager_thumb_height', 80); $size = @getimagesize($filePathName); if (is_array($size)) { $imageWidth = $size[0] > 800 ? 800 : $size[0]; $imageHeight = $size[1] > 600 ? 600 : $size[1]; } /* ensure max h/w */ if ($thumbWidth > $imageWidth) { $thumbWidth = $imageWidth; } if ($thumbHeight > $imageHeight) { $thumbHeight = $imageHeight; } /* generate thumb/image URLs */ $thumbQuery = http_build_query(array('src' => $url, 'w' => $thumbWidth, 'h' => $thumbHeight, 'f' => $thumbnailType, 'q' => $thumbnailQuality, 'far' => '1', 'HTTP_MODAUTH' => $modAuth, 'wctx' => $this->ctx->get('key'), 'source' => $this->get('id'))); $imageQuery = http_build_query(array('src' => $url, 'w' => $imageWidth, 'h' => $imageHeight, 'HTTP_MODAUTH' => $modAuth, 'f' => $thumbnailType, 'q' => $thumbnailQuality, 'wctx' => $this->ctx->get('key'), 'source' => $this->get('id'))); $thumb = $this->ctx->getOption('connectors_url', MODX_CONNECTORS_URL) . 'system/phpthumb.php?' . urldecode($thumbQuery); $image = $this->ctx->getOption('connectors_url', MODX_CONNECTORS_URL) . 'system/phpthumb.php?' . urldecode($imageQuery); } else { $preview = 0; $size = null; $thumb = $image = $this->ctx->getOption('manager_url', MODX_MANAGER_URL) . 'templates/default/images/restyle/nopreview.jpg'; $thumbWidth = $imageWidth = $this->ctx->getOption('filemanager_thumb_width', 100); $thumbHeight = $imageHeight = $this->ctx->getOption('filemanager_thumb_height', 80); } $octalPerms = substr(sprintf('%o', $file->getPerms()), -4); $filenames[] = strtoupper($fileName); $files[$fileName] = array('id' => $bases['urlAbsoluteWithPath'] . $fileName, 'name' => $fileName, 'cls' => 'icon-' . $fileExtension, 'image' => $image, 'image_width' => is_array($size) ? $size[0] : $imageWidth, 'image_height' => is_array($size) ? $size[1] : $imageHeight, 'thumb' => $thumb, 'thumb_width' => $thumbWidth, 'thumb_height' => $thumbHeight, 'url' => ltrim($dir . $fileName, '/'), 'relativeUrl' => ltrim($dir . $fileName, '/'), 'fullRelativeUrl' => rtrim($bases['url']) . ltrim($dir . $fileName, '/'), 'ext' => $fileExtension, 'pathname' => str_replace('//', '/', $filePathName), 'pathRelative' => $bases['pathRelative'] . $fileName, 'lastmod' => $file->getMTime(), 'preview' => $preview, 'disabled' => false, 'perms' => $octalPerms, 'leaf' => true, 'page' => $this->fileHandler->isBinary($filePathName) ? $page : null, 'size' => $filesize, 'menu' => array()); $files[$fileName]['menu'] = $this->getListContextMenu($file, $files[$fileName]); } } $ls = array(); array_multisort($filenames, SORT_ASC, SORT_STRING, $files); foreach ($files as $file) { $ls[] = $file; } return $ls; }