By default, scanning unreadable directories content throws an AccessDeniedException.
public ignoreUnreadableDirs ( boolean $ignore = true ) : |
||
$ignore | boolean | |
return | The current Finder instance |
/** * @inheritdoc */ public function searchString($stringToSearch, $searchFolder) { $this->finder = $this->finderFactory->create(); $result = new FileSearchResult($searchFolder); $this->finder->ignoreUnreadableDirs()->in($searchFolder); foreach ($this->finder->files()->contains($stringToSearch) as $file) { /** @var \Symfony\Component\Finder\SplFileInfo $file */ $fileLocation = new FileLocation(); $fileLocation->setFolder($file->getPath()); $fileLocation->setName($file->getFilename()); $fileLocation->setExtension($file->getExtension()); $result->add($fileLocation); } return $result; }
/** * Install external adapters * @return void * @throws Exception */ private function installAdapters(AdaptersEvent $event) { $fs = new Filesystem(); $finder = new Finder(); try { $pluginsDir = __DIR__ . '/../../../'; $iterator = $finder->ignoreUnreadableDirs()->files()->name('*Adapter.php')->in($pluginsDir . '*/*/Adapter/PaywallAdapters/')->in(__DIR__ . '/../Adapter/'); foreach ($iterator as $file) { $classNamespace = str_replace(realpath($pluginsDir), '', substr($file->getRealPath(), 0, -4)); $namespace = str_replace('/', '\\', $classNamespace); $adapterName = substr($file->getFilename(), 0, -4); $oneAdapter = $this->em->getRepository('Newscoop\\PaywallBundle\\Entity\\Settings')->findOneBy(array('value' => $adapterName)); $event->registerAdapter($adapterName, array('class' => $namespace)); if (!$oneAdapter) { $adapter = new Settings(); $adapter->setName(str_replace("Adapter.php", "", $file->getFilename())); $adapter->setValue($adapterName); if ($adapterName !== 'PaypalAdapter') { $adapter->setIsActive(false); } $this->em->persist($adapter); } } $this->em->flush(); } catch (\Exception $e) { } }
/** * @return array */ public function findFiles() { $files = array(); $finder = new Finder(); $iterate = false; $finder->ignoreUnreadableDirs(); foreach ($this->items as $item) { if (!is_file($item)) { $finder->in($item); $iterate = true; } else { $files[] = realpath($item); } } foreach ($this->excludes as $exclude) { $finder->exclude($exclude); } foreach ($this->names as $name) { $finder->name($name); } foreach ($this->notNames as $notName) { $finder->notName($notName); } foreach ($this->regularExpressionsExcludes as $regularExpressionExclude) { $finder->notPath($regularExpressionExclude); } if ($iterate) { foreach ($finder as $file) { $files[] = $file->getRealpath(); } } return $files; }
public function generatedirList() { $path = $this->container->getParameter('kernel.root_dir') . "/../"; $finder = new Finder(); $iterator = $finder->ignoreUnreadableDirs()->directories()->depth('< 2')->in($path); $dirList = array(); foreach ($iterator as $key => $value) { $dirList["{$value}"] = "{$value}"; } return $dirList; }
public function clear() { #ToDO prueba de concepto sacar a un servicio $manager = $this->orphanManager->get('gallery'); $fs = new Filesystem(); $finder = new Finder(); if ($fs->exists($this->config['directory'] . '/' . $this->session->getId())) { $files = $finder->ignoreUnreadableDirs()->in($this->config['directory'] . '/' . $this->session->getId()); $fs->remove($files); } }
/** * Function that generates the directories list * which is used to fill the drop down options for Search Path * * @return array */ public function generateDirsList() { $path = $this->get('kernel')->getRootDir(); $finder = new Finder(); $iterator = $finder->ignoreUnreadableDirs()->directories()->depth('< 2')->in($path); $dirsList = array(); foreach ($iterator as $key => $value) { $dirsList["{$value}"] = "{$value}"; } return $dirsList; }
protected function generateSecret($password) { $finder = new Finder(); $root = $this->container->get('kernel')->getRootDir(); $files = $finder->ignoreUnreadableDirs()->in($root . 'key/')->files()->name('uipubkey.pem'); foreach ($files as $file) { $private_key_text = $file->getContent(); break; } if (!$private_key_text) { throw new FileNotFoundException('Encryption key not found'); } $rsa = new RSA(); $rsa->loadKey($private_key_text); return base64_encode($rsa->encrypt($password)); }
public static function tearDownAfterClass() { $dirs = array(); $finder = new Finder(); //許可がありませんDIR対応 $finder->ignoreUnreadableDirs(true); $iterator = $finder->in(sys_get_temp_dir())->name('FileController*')->directories(); foreach ($iterator as $dir) { $dirs[] = $dir->getPathName(); } foreach ($dirs as $dir) { // プロセスが掴んでいるためか、確実に削除できない場合がある try { $f = new Filesystem(); $f->remove($dir); } catch (\Exception $e) { // queit. } } }
/** * 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()); }
/** * Install external parsers * * @return void * * @throws Exception */ private function installParsers(IngestParsersEvent $event) { $fs = new Filesystem(); $finder = new Finder(); try { $pluginsDir = __DIR__ . '/../../../'; $namespaces = array(); $iterator = $finder->ignoreUnreadableDirs()->files()->name('*Parser.php')->notName('AbstractParser.php')->in(__DIR__ . '/../Parsers/'); try { $iterator = $iterator->in($pluginsDir . '*/*/Parsers/IngestAdapters/'); } catch (\Exception $e) { // Catch exception if no such directory exists } foreach ($iterator as $file) { $classNamespace = str_replace(realpath($pluginsDir), '', substr($file->getRealPath(), 0, -4)); $namespace = str_replace('/', '\\', $classNamespace); $namespaces[] = (string) $namespace; $parserName = substr($file->getFilename(), 0, -4); $parser = $this->em->getRepository('Newscoop\\IngestPluginBundle\\Entity\\Parser')->findOneByNamespace($namespace); $event->registerParser($parserName, array('class' => $namespace)); if (!$parser) { $parser = new Parser(); } $parser->setName($namespace::getParserName())->setDescription($namespace::getParserDescription())->setDomain($namespace::getParserDomain())->setRequiresUrl($namespace::getRequiresUrl())->setSectionHandling($namespace::getHandlesSection())->setNamespace($namespace); $this->em->persist($parser); } // Remove parser which we didn't find anymore $parsersToRemove = $this->em->createQuery(' DELETE FROM Newscoop\\IngestPluginBundle\\Entity\\Parser AS p WHERE p.namespace NOT IN (:namespaces) ')->setParameter('namespaces', $namespaces)->getResult(); $this->em->flush(); } catch (\Exception $e) { throw new \Exception($e->getMessage()); } }
/** * 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; }
/** * 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; }
/** * 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; }
/** * @param string $directoryPath * @param array $settings * @param string $virtualHost */ protected function setUmask($directoryPath, array $settings, $virtualHost) { $fileUmask = !empty($settings[$virtualHost]['umask']['file']) ? $settings[$virtualHost]['umask']['file'] : !empty($settings['default']['umask']['file']) ? $settings['default']['umask']['file'] : 0; $folderUmask = !empty($settings[$virtualHost]['umask']['folder']) ? $settings[$virtualHost]['umask']['folder'] : (!empty($settings['default']['umask']['folder']) ? $settings['default']['umask']['folder'] : 0); if (empty($fileUmask) && empty($folderUmask)) { return; } $repositoryFinder = new Finder(); $repositoryFinder->ignoreUnreadableDirs(true)->ignoreDotFiles(false)->ignoreVCS(true)->in($directoryPath); if (empty($fileUmask)) { $repositoryFinder->directories(); } elseif (empty($folderUmask)) { $repositoryFinder->files(); } /** @var SplFileInfo $repositoryItem */ foreach ($repositoryFinder as $repositoryItem) { if (!empty($fileUmask) && $repositoryItem->isFile()) { chmod($repositoryItem->getPathname(), octdec($fileUmask)); } elseif (!empty($folderUmask) && $repositoryItem->isDir()) { chmod($repositoryItem->getPathname(), octdec($folderUmask)); } } }
/** * @return Finder */ public function ignoreUnreadableDirs($ignore = true) { return parent::ignoreUnreadableDirs($ignore); }
/** * @param OutputInterface $output * @return void */ protected function cleanCustom(OutputInterface $output) { if (empty($this->options['custom'])) { return; } foreach ($this->options['custom'] as $options) { try { $finder = new Finder(); $finder->ignoreDotFiles(false); $finder->ignoreVCS(false); $finder->ignoreUnreadableDirs(true); $in = []; if (!is_array($options['in'])) { $options['in'] = [$options['in']]; } foreach ($options['in'] as $dir) { $in[] = $this->baseDir . $dir; } $finder->in($in); if (!empty($options['type'])) { switch ($options['type']) { case 'files': $finder->files(); break; case 'dirs': case 'directories': $finder->directories(); break; } } if (!empty($options['notName'])) { if (!is_array($options['notName'])) { $options['notName'] = [$options['notName']]; } foreach ($options['notName'] as $notName) { $finder->notName($notName); } } if (!empty($options['name'])) { if (!is_array($options['name'])) { $options['name'] = [$options['name']]; } foreach ($options['name'] as $notName) { $finder->name($notName); } } if (!empty($options['depth'])) { $finder->depth($options['depth']); } foreach ($finder as $file) { $this->files[] = $file->getRealPath(); } } catch (\Exception $e) { // ignore errors } } }
/** * Get suite files with tests. * * @return \ArrayObject */ public function getFiles() { if ($this->files === null) { $files = new \ArrayObject(); foreach ($this->config['source'] as $source) { $finder = new Finder(); $finder->ignoreUnreadableDirs()->files(); $finder->in($source['in'])->name($source['name']); if (!empty($source['notName'])) { foreach ($source['notName'] as $name) { $finder->notName($name); } } if (!empty($source['exclude'])) { $finder->exclude($source['exclude']); } /** @var \SplFileInfo $file */ foreach ($finder as $file) { $realPath = $file->getRealPath(); if (!$files->offsetExists($realPath) && substr($file->getFilename(), -8) === 'Test.php') { $files[$realPath] = $file; } } } $this->files = $files; } return $this->files; }
/** * Get list of files from a bucket / directory. * @return (array) * @todo Pass in destination as querystring parameter * @todo Figure out sorting, alpha natural sort * @todo Breadcrumbs */ public function fileclerk__list() { // Force AJAX, except in dev if (!Request::isAjax() && FILECLERK_ENV != 'dev') { echo FILECLERK_AJAX_WARNING; exit; } // Destination config parameter $destination = Request::get('destination'); $destination = is_null($destination) ? 0 : $destination; // Merge configs before we proceed $this->config = $this->tasks->merge_configs($destination); // Setup client self::load_s3(); // Set default error to false $error = false; // Do some werk to setup paths $bucket = $this->config['bucket']; $directory = $this->config['directory']; $uri = Request::get('uri'); $uri = explode('?', $uri); $uri = reset($uri); $s3_url = Url::tidy('s3://' . join('/', array($bucket, $directory, $uri))); // Let's make sure we have a valid URL before movin' on if (Url::isValid($s3_url)) { // Just messing around native SDK methods get objects if (FALSE) { // Trying out listIterator $iterator = $this->client->getIterator('ListObjects', array('Bucket' => $this->config['bucket'], 'Delimiter' => '/'), array('limit' => 1000, 'return_prefixes' => true)); //dd($iterator); echo '<pre>'; foreach ($iterator as $object) { //if( ! isset($object['Prefix']) ) continue; // Skip non-directories echo var_dump($object) . '<br>'; //echo var_dump($object) . '<br>'; } echo '</pre>'; exit; } // Finder instance $finder = new Finder(); // Finder call try { $finder->ignoreUnreadableDirs()->ignoreDotFiles(true)->in($s3_url)->depth('== 0'); $results = iterator_to_array($finder); } catch (Exception $e) { $error = $e->getMessage(); header('Content-Type', 'application/json'); echo self::build_response_json(false, true, FILECLERK_S3_ERROR, $error, 'error', null, null, null); exit; } // Data array for building out view $data = array('crumbs' => explode('/', $uri), 'destination' => $destination, 'list' => array()); // Prepare breadcrumbs foreach ($data['crumbs'] as $key => $value) { $path = explode('/', $uri, $key + 1 - count($data['crumbs'])); $path = implode('/', $path); //$path = Url::tidy( $path . '/?' . $querystring ); $path = Url::tidy($path); $data['crumbs'][$key] = array('name' => $value, 'path' => $path); } // Build breadcrumb $breadcrumb = Parse::template(self::get_view('_list-breadcrumb'), $data); /** * Let's make sure we've got somethin' up in this mutha. */ if ($finder->count() > 0) { foreach ($finder as $file) { // Get the filename $filename = $file->getFilename(); // Set the S3 key string for the objet $key = Url::tidy(join('/', array($directory, $uri, $filename))); // File / directory attributes $this->data = $this->tasks->get_file_data_array(); // Set some file data $file_data = array('basename' => $file->getBasename('.' . $file->getExtension()), 'destination' => $destination, 'extension' => $file->getExtension(), 'file' => $file->getPathname(), 'filename' => $file->getFilename(), 'last_modified' => $file->isDir() ? '--' : $file->getMTime(), 'is_file' => $file->isFile(), 'is_directory' => $file->isDir(), 'size' => $file->isDir() ? '--' : File::getHumanSize($file->getSize()), 'size_bytes' => $file->getSize(), 'uri' => $uri); /** * Need to set the uri value */ if ($file->isFile()) { // Check if file is an iamge $file_data['is_image'] = self::is_image($file_data['extension']); // Set filesize data $file_data['size_kilobytes'] = $this->tasks->get_size_kilobytes($file_data['size_bytes']); $file_data['size_megabytes'] = $this->tasks->get_size_megabytes($file_data['size_bytes']); $file_data['size_gigabytes'] = $this->tasks->get_size_gigabytes($file_data['size_bytes']); } elseif ($file->isDir()) { if (is_null($uri)) { $file_data['uri'] = Url::tidy('/' . join('/', array($file_data['filename']))); } else { $file_data['uri'] = Url::tidy('/' . join('/', array($uri, $file_data['filename']))); } } else { // Keep on movin' on. continue; } // Set the URL $file_data['url'] = Url::tidy(self::get_url_prefix($uri) . '/' . $filename); // Push file data to a new array with the filename as the key for sorting. $data['list'][$filename] = $file_data; unset($file_data); } // Nothing returned from Finder } else { /** * Return an error of type dialog that should show a message to the user * that there are is nothing to see her. Doh! * @return [array] JSON * @todo See `self::set_json_return`. */ // echo self::build_response_json(false, true, FILECLERK_LIST_NO_RESULTS, 'No results returned.'); // exit; $no_results_template = File::get(__DIR__ . '/views/error-no-results.html'); end($data['crumbs']); $previous_directory = prev($data['crumbs']); echo json_encode(array('error' => TRUE, 'type' => 'dialog', 'code' => FILECLERK_LIST_NO_RESULTS, 'breadcrumb' => $breadcrumb, 'html' => Parse::template($no_results_template, array('previous_directory' => $previous_directory['path'])))); exit; } } // Need to pass in the destination for root requests. $data['destination'] = $destination; // Sort this f*****g multi-dimensional array already. uksort($data['list'], 'strcasecmp'); //array_multisort( array_keys($data['list']), SORT_FLAG_CASE, $data['list'] ); /** * THIS ONLY WORKS IN PHP 5.4+. F**K! */ // array_multisort( array_keys($data['list']), SORT_NATURAL | SORT_FLAG_CASE, $data['list'] ); // Now we need to tweak the array for parsing. foreach ($data['list'] as $filename => $filedata) { $data['list'][] = $filedata; unset($data['list'][$filename]); } // We're basically parsing template partials here to build out the larger view. $parsed_data = array('list' => Parse::template(self::get_view('_list'), $data)); // Put it all together $ft_template = File::get(__DIR__ . '/views/list.html'); // Return JASON header('Content-Type', 'application/json'); echo self::build_response_json(true, false, FILECLERK_LIST_SUCCESS, null, null, $data, $breadcrumb, Parse::template($ft_template, $parsed_data)); exit; }
/** * 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; }
/** * Search all php binaries from the passed paths. * * @param string[] $paths The paths to scan for binaries. * * @param string[] $fileNames Optional names of files to search for. * * @return string[] */ private function findBinaries($paths, $fileNames = ['php', 'php.exe']) { // We have to work around the problem that the symfony Finder will try to follow the symlink when a file // i.e. /var/bin/foo is symlinked to /usr/bin/foo and therefore raise a warning that /var/bin is not in // the open_basedir locations. // Therefore we can not use the Finder component when open_basedir has been set. if ($baseDirs = array_filter(array_map('trim', explode(PATH_SEPARATOR, ini_get('open_basedir'))))) { $foundBinaries = []; foreach ($this->filterBaseDir($paths, $baseDirs) as $path) { foreach (scandir($path) as $file) { if (in_array(basename($file), $fileNames)) { $foundBinaries[] = new \SplFileInfo($path . DIRECTORY_SEPARATOR . $file); } } } return $foundBinaries; } if (empty($paths)) { return []; } $finder = new Finder(); $finder->ignoreUnreadableDirs()->in($paths); foreach ($fileNames as $name) { $finder->name($name); } $foundBinaries = []; foreach ($finder as $file) { /** @var \SplFileInfo $file */ $foundBinaries[] = $file->getPathname(); } return $foundBinaries; }