예제 #1
0
 public function __construct()
 {
     $themes = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Theme')->findAll();
     $choices = [];
     $finder = new Finder();
     // Extracting the PHP files from every Theme folder
     $iterator = $finder->followLinks()->files()->name('config.yml')->depth(1)->in(ROADIZ_ROOT . '/themes');
     // And storing it into an array, used in the form
     foreach ($iterator as $file) {
         $data = Yaml::parse($file->getPathname());
         $classname = '\\Themes\\' . $data['themeDir'] . "\\" . $data['themeDir'] . "App";
         /*
          * Parsed file is not or does not contain any PHP Class
          * Bad Theme !
          */
         $choices[$classname] = $data['name'];
     }
     foreach ($themes as $theme) {
         if (array_key_exists($theme->getClassName(), $choices)) {
             unset($choices[$theme->getClassName()]);
         }
         if (array_key_exists(Kernel::INSTALL_CLASSNAME, $choices)) {
             unset($choices[Kernel::INSTALL_CLASSNAME]);
         }
     }
     $this->choices = $choices;
 }
 /**
  * loadContext
  *
  * @return array
  */
 private function loadContext()
 {
     $this->files = new Finder();
     $this->baseDir = new SplFileInfo($this->get('spliced_cms.site_manager')->getCurrentAdminSite()->getRootDir(), '/', '/');
     if ($this->get('request')->query->has('dir')) {
         $path = $this->baseDir->getRealPath() . '/' . $this->get('request')->query->get('dir');
         $relativePath = preg_replace('/\\/{2,}/', '/', str_replace($this->baseDir->getRealPath(), '', $path));
         $this->dir = new SplFileInfo($path, $relativePath, $relativePath);
     } else {
         $this->dir = $this->baseDir;
     }
     $this->files->followLinks()->depth(0)->in($this->dir->getRealPath());
     $this->file = null;
     if ($this->get('request')->query->has('file') && $this->get('request')->query->get('file')) {
         $this->file = new SplFileInfo($this->dir->getRealPath() . '/' . $this->get('request')->query->get('file'), $this->dir->getRelativePath() . '/' . $this->get('request')->query->get('file'), $this->dir->getRelativePathname());
     }
     $this->loaded = true;
     return $this;
 }
예제 #3
0
/**
 * Retrieve directory content, directories and files
 *
 * @param Application $app Silex Application
 * @param Request $request Request parameters
 *
 * @return JsonResponse Array of objects
 */
function get_content(Application $app, Request $request)
{
    $dirpath = Utils\check_path($app['cakebox.root'], $request->get('path'));
    if (!isset($dirpath)) {
        $app->abort(400, "Missing parameters");
    }
    $finder = new Finder();
    $finder->followLinks()->depth('< 1')->in("{$app['cakebox.root']}/{$dirpath}")->ignoreVCS(true)->ignoreDotFiles($app['directory.ignoreDotFiles'])->notName($app["directory.ignore"])->sortByType();
    $dirContent = [];
    foreach ($finder as $file) {
        if ($file->isLink()) {
            $linkTo = readlink("{$app['cakebox.root']}/{$dirpath}/{$file->getBasename()}");
            if (file_exists($linkTo) == false) {
                continue;
            }
            $file = new \SplFileInfo($linkTo);
        }
        $pathInfo = [];
        $pathInfo["name"] = $file->getBasename();
        $pathInfo["type"] = $file->getType();
        $pathInfo["mtime"] = $file->getMTime();
        $pathInfo["size"] = Utils\get_size($file);
        $pathInfo["access"] = str_replace('%2F', '/', rawurlencode("{$app['cakebox.access']}/{$dirpath}/{$file->getBasename()}"));
        $pathInfo["extraType"] = "";
        $ext = strtolower($file->getExtension());
        if (in_array($ext, $app["extension.video"])) {
            $pathInfo["extraType"] = "video";
        } else {
            if (in_array($ext, $app["extension.audio"])) {
                $pathInfo["extraType"] = "audio";
            } else {
                if (in_array($ext, $app["extension.image"])) {
                    $pathInfo["extraType"] = "image";
                } else {
                    if (in_array($ext, $app["extension.archive"])) {
                        $pathInfo["extraType"] = "archive";
                    } else {
                        if (in_array($ext, $app["extension.subtitle"])) {
                            $pathInfo["extraType"] = "subtitle";
                        }
                    }
                }
            }
        }
        array_push($dirContent, $pathInfo);
    }
    return $app->json($dirContent);
}
예제 #4
0
 private function _findFiles()
 {
     $finder = new Finder();
     $finder->files();
     foreach ($this->_excludePatterns as $excludePattern) {
         $finder->notName($excludePattern);
     }
     foreach ($this->_paths as $p) {
         $finder->in($p);
     }
     if ($this->_followLinks) {
         $finder->followLinks();
     }
     $files = array();
     foreach ($finder as $f) {
         $files[$f->getRealpath()] = array('mtime' => $f->getMTime(), 'perms' => $f->getPerms(), 'owner' => $f->getOwner(), 'group' => $f->getGroup());
     }
     return $files;
 }
 /**
  * Returns a Finder instance for the files that will be included in the
  * backup.
  *
  * By default we ignore unreadable files and directories as well as, common
  * version control folders / files, "Dot" files and anything matching the
  * exclude rules.
  *
  * @uses Finder
  * @return Finder The Finder iterator of all files to be included
  */
 public function get_files()
 {
     $finder = new Finder();
     $finder->followLinks(true);
     $finder->ignoreDotFiles(false);
     $finder->ignoreVCS(true);
     $finder->ignoreUnreadableDirs(true);
     // Skip unreadable files too
     $finder->filter(function (\SplFileInfo $file) {
         if (!$file->isReadable()) {
             return false;
         }
     });
     // Finder expects exclude rules to be in a regex format
     $exclude_rules = $this->excludes->get_excludes_for_regex();
     // Skips folders/files that match default exclude patterns
     foreach ($exclude_rules as $exclude) {
         $finder->notPath($exclude);
     }
     return $finder->in(Path::get_root());
 }
예제 #6
0
 /**
  * Recursively scans a directory to calculate the total filesize
  *
  * Locks should be set by the caller with `set_transient( 'hmbkp_directory_filesizes_running', true, HOUR_IN_SECONDS );`
  *
  * @return array $directory_sizes    An array of directory paths => filesize sum of all files in directory
  */
 public function recursive_filesize_scanner()
 {
     /**
      * Raise the `memory_limit` and `max_execution time`
      *
      * Respects the WP_MAX_MEMORY_LIMIT Constant and the `admin_memory_limit`
      * filter.
      */
     @ini_set('memory_limit', apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT));
     @set_time_limit(0);
     // Use the cached array directory sizes if available
     $directory_sizes = $this->get_cached_filesizes();
     // If we do have it in cache then let's use it and also clear the lock
     if (is_array($directory_sizes)) {
         delete_transient('hmbkp_directory_filesizes_running');
         return $directory_sizes;
     }
     // If we don't have it cached then we'll need to re-calculate
     $finder = new Finder();
     $finder->followLinks();
     $finder->ignoreDotFiles(false);
     $finder->ignoreUnreadableDirs(true);
     $files = $finder->in(Path::get_root());
     foreach ($files as $file) {
         if ($file->isReadable()) {
             $directory_sizes[wp_normalize_path($file->getRealpath())] = $file->getSize();
         } else {
             $directory_sizes[wp_normalize_path($file->getRealpath())] = 0;
         }
     }
     file_put_contents(PATH::get_path() . '/.files', gzcompress(json_encode($directory_sizes)));
     // Remove the lock
     delete_transient('hmbkp_directory_filesizes_running');
     return $directory_sizes;
 }
예제 #7
0
 /**
  * Return an array of all files in the filesystem
  *
  * @return \RecursiveIteratorIterator
  */
 public function get_files()
 {
     $found = array();
     if (!empty($this->files)) {
         return $this->files;
     }
     $finder = new Finder();
     $finder->followLinks();
     $finder->ignoreDotFiles(false);
     $finder->ignoreUnreadableDirs();
     foreach ($this->default_excludes() as $exclude) {
         $finder->notPath($exclude);
     }
     foreach ($finder->in($this->get_root()) as $entry) {
         $this->files[] = $entry;
     }
     return $this->files;
 }
예제 #8
0
 /**
  * Return the single depth list of files and subdirectories in $directory ordered by total filesize
  *
  * Will schedule background threads to recursively calculate the filesize of subdirectories.
  * The total filesize of each directory and subdirectory is cached in a transient for 1 week.
  *
  * @param string $directory The directory to scan
  *
  * @return array returns an array of files ordered by filesize
  */
 public function list_directory_by_total_filesize($directory)
 {
     $files = $files_with_no_size = $empty_files = $files_with_size = $unreadable_files = array();
     if (!is_dir($directory)) {
         return $files;
     }
     $found = array();
     if (!empty($this->files)) {
         return $this->files;
     }
     $default_excludes = $this->backup->default_excludes();
     $finder = new Finder();
     $finder->ignoreDotFiles(false);
     $finder->ignoreUnreadableDirs();
     $finder->followLinks();
     $finder->depth('== 0');
     foreach ($default_excludes as $exclude) {
         $finder->notPath($exclude);
     }
     foreach ($finder->in($directory) as $entry) {
         $files[] = $entry;
         // Get the total filesize for each file and directory
         $filesize = $this->filesize($entry);
         if ($filesize) {
             // If there is already a file with exactly the same filesize then let's keep increasing the filesize of this one until we don't have a clash
             while (array_key_exists($filesize, $files_with_size)) {
                 $filesize++;
             }
             $files_with_size[$filesize] = $entry;
         } elseif (0 === $filesize) {
             $empty_files[] = $entry;
         } else {
             $files_with_no_size[] = $entry;
         }
     }
     // Add 0 byte files / directories to the bottom
     $files = $files_with_size + array_merge($empty_files, $unreadable_files);
     // Add directories that are still calculating to the top
     if ($files_with_no_size) {
         // We have to loop as merging or concatenating the array would re-flow the keys which we don't want because the filesize is stored in the key
         foreach ($files_with_no_size as $entry) {
             array_unshift($files, $entry);
         }
     }
     return $files;
 }
예제 #9
0
 /**
  * @param string $type
  * @param array  $items
  * @return array
  */
 public function collectFiles($type, $items)
 {
     $res = [];
     foreach ($items as $item) {
         $res[$item['exportName']] = [];
     }
     foreach ($items as $item) {
         $path = $item['path'];
         $exportName = $item['exportName'];
         if (is_file($path)) {
             if (($temp = strlen($path) - strlen($type)) >= 0 && strpos($path, $type, $temp) !== false) {
                 $res[$exportName][] = $path;
             }
         } else {
             $finder = new Finder();
             $finder->followLinks()->name('*.' . $type)->files()->in($path);
             foreach ($finder as $file) {
                 /** @var File $file */
                 $res[$exportName][] = ['realPath' => $file->getRealPath(), 'relativePathName' => $file->getRelativePathname(), 'relativePath' => $file->getRelativePath(), 'baseName' => $file->getBasename()];
             }
         }
     }
     return $res;
 }
예제 #10
0
$directoryIterator = $directoryFinder->in($path->path);
$directoryArray = array();
foreach ($directoryIterator as $directory) {
    $directoryArray[] = array("path" => $directory->getRelativePathname(), "name" => $directory->getBasename());
}
$fileFinder = new Finder();
$fileFinder->files()->ignoreUnreadableDirs()->depth(0);
$allowedImageTypes = loadPicFile("helpers/imagetypes.php");
foreach ($allowedImageTypes as $imageType) {
    $fileFinder->name("*.{$imageType}");
}
foreach (array_map("strtoupper", $allowedImageTypes) as $imageType) {
    $fileFinder->name("*.{$imageType}");
}
$fileFinder->sortByName();
if ($path->hasPermission("symlinks")) {
    $fileFinder->followLinks();
}
if (!empty($relpath)) {
    $fileFinder->path($relpath)->depth(substr_count($relpath, "/") + 1);
}
if ($path->hasPermission("nsfw") === false) {
    $fileFinder->notPath("/.*\\/NSFW\\/.*/")->notPath("/NSFW\\/.*/")->notPath("/.*\\/NSFW/");
}
$fileIterator = $fileFinder->in($path->path);
$fileArray = array();
foreach ($fileIterator as $file) {
    $fileArray[] = array("filename" => $file->getBasename(), "relpath" => $file->getRelativePathname(), "size" => humanFilesize($file->getSize()), "mtime" => date("Y-m-d H:i:s", $file->getMTime()));
}
header("Content-type: application/json");
echo json_encode(array("directories" => $directoryArray, "files" => $fileArray));
예제 #11
0
 /**
  * Return an array of all files in the filesystem.
  *
  * @param bool $ignore_default_exclude_rules If true then will return all files under root. Otherwise returns all files except those matching default exclude rules.
  *
  * @return array
  */
 public function get_files($ignore_default_exclude_rules = false)
 {
     $found = array();
     if (!empty($this->files)) {
         return $this->files;
     }
     $finder = new Finder();
     $finder->followLinks();
     $finder->ignoreDotFiles(false);
     $finder->ignoreUnreadableDirs();
     if (!$ignore_default_exclude_rules) {
         // Skips folders/files that match default exclude patterns
         foreach ($this->default_excludes() as $exclude) {
             $finder->notPath($exclude);
         }
     }
     foreach ($finder->in($this->get_root()) as $entry) {
         $this->files[] = $entry;
     }
     return $this->files;
 }
예제 #12
0
 /**
  *
  * @return Finder
  */
 protected function getFinder()
 {
     if (null === $this->finder) {
         $finder = new Finder();
         $settings = $this->getSettings();
         foreach ($settings->getSrc() as $source) {
             if (is_dir($source)) {
                 $finder->in($source);
             }
             if (is_file($source)) {
                 $finder->append(array($source));
             }
         }
         if (true === $settings->getFollowLinks()) {
             $finder->followLinks();
         }
         $finder->ignoreDotFiles($settings->getIgnoreDotFiles());
         $finder->name($settings->getFileSuffix());
         $finder->exclude($settings->getExclude());
         if (NULL !== $settings->getDate()) {
             $finder->date($settings->getDate());
         }
         $this->setFinder($finder);
     }
     return $this->finder;
 }
예제 #13
0
 private function scanFiles($path, $parent)
 {
     $finder = new Finder();
     $finder->in($path);
     $finder->files();
     $finder->ignoreDotFiles(true);
     $finder->depth(0);
     $finder->sortByName();
     $finder->followLinks();
     $items = [];
     $index = null;
     foreach ($finder as $entity) {
         /* @var $entity SplFileInfo */
         $name = $entity->getFilename();
         // Skip items starting with underscore
         if (preg_match('~^_~', $name)) {
             continue;
         }
         // Skip items starting with dot
         if (preg_match('~^\\.~', $name)) {
             continue;
         }
         $isIndex = false;
         if (preg_match('~^index\\.~', basename($name))) {
             $isIndex = true;
         }
         $item = new RequestItem();
         $path = $entity->getPathname();
         $url = $this->relativePath . str_replace($this->path, '', $path);
         if ($isIndex) {
             $url = dirname($url) . '/';
             $index = $item;
             $this->scanFolders(dirname($path), $index);
         }
         $renderer = $this->staple->getRenderer($path);
         // Skip assets
         if (!$renderer instanceof NavigableInterface) {
             continue;
         }
         $data = (object) (new PreProcessor())->getData($this, $path, $entity->getBasename());
         $item->url = $url;
         $item->title = !empty($data->title) ? $data->title : '* ' . ucfirst(basename($url));
         $parent->items[] = $item;
     }
     return $index;
 }
예제 #14
0
파일: Finder.php 프로젝트: stopsopa/utils
 /**
  * @return Finder
  */
 public function followLinks()
 {
     return parent::followLinks();
 }