<?php
/**
 * @version     backend/classes/recursiveListing.php 2015-07-28 14:14:00 UTC Ch
 * @authorUrl   https://watchful.li
 * @copyright   Copyright (c) 2012-2015 watchful.li
 * @license     GNU/GPL v3 or later
 * @copyright   Copyright (c) 2012-2015 watchful.li
 * @license     GNU/GPL v3 or later
 */
defined('_JEXEC') or die('Restricted access');
class WatchfulliRecursiveListing
{

    /**
     *
     * @param string $path
     * @return \stdClass
     */
    public static function getStructure($path)
    {
        $structure = new stdClass();
        $structure->dirs = array();
        $structure->files = array();
        $iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

        foreach ($iterator as $file)
        {
            if ($file->isDir())
            {
                $structure->dirs[] = $file->getRealpath();
            }
            else
            {
                $structure->files[] = $file->getRealpath();
            }
        }

        $structure->files = array_unique($structure->files);
        $structure->files = array_values($structure->files);

        $structure->dirs = array_unique($structure->dirs);
        $structure->dirs = array_values($structure->dirs);

        return $structure;
    }

    /**
     *
     * @param \stdClass $structure
     * @param array $hashes
     * @return \stdClass
     */
    public static function getNonCoreFiles($structure, $hashes)
    {
        $hashPaths = array();
        $nonCoreFiles = array();

        foreach ($hashes as $hash)
        {
            $hashPaths[] = $hash[0];
        }

        foreach ($structure->files as $file)
        {
            //Rremove path base
            $hashBasePath = str_replace(JPATH_BASE . '/', '', $file);
            if (!in_array($hashBasePath, $hashPaths))
            {
                $nonCoreFiles[] = $file;
            }
        }

        return $nonCoreFiles;
    }

}