squeezr/lib/Tollwerk/Squeezr/Cleaner.php
/**
     * Recursively clean a subdirectory
     *
     * @param string $path Subdirectory path
     * @return int                                        Number of contained resources
     */
Recursively clean a subdirectory
@param string $path Subdirectory path
@return int                                        Number of contained resources
Recursively clean a subdirectory @param string $path Subdirectory path @return int Number of contained resources
private function _cleanDirectory($path = '')
    {
        $resources = scandir($this->_base.$path);
        $resourceCount = count($resources);
        $links =
        $files = array();
        foreach ($resources as $resource) {
            $absResource = $this->_base.$path.DIRECTORY_SEPARATOR.$resource;
            $origResource = SQUEEZR_DOCROOT.$path.DIRECTORY_SEPARATOR.$resource;
            if (@is_dir($absResource) && (($resource == '.') || ($resource == '..'))) {
                --$resourceCount;
                continue;
            }
            if (@is_dir($absResource)) {
                if (!@is_dir($origResource) || !$this->_cleanDirectory(ltrim($path.DIRECTORY_SEPARATOR.$resource,
                        DIRECTORY_SEPARATOR))
                ) {
                    @exec('rm -R '.escapeshellarg($absResource));
                    --$resourceCount;
                }
            } elseif (@is_link($absResource)) {
                $links[] = $resource;
            } elseif (@is_file($absResource)) {
                $files[] = $resource;
            } else {
                if (@unlink($absResource)) {
                    --$resourceCount;
                }
            }
        }
        foreach ($files as $file) {
            $absResource = $this->_base.$path.DIRECTORY_SEPARATOR.$file;
            switch (strtolower(pathinfo($file, PATHINFO_EXTENSION))) {
                case 'css':
                    if (preg_match("%^(.+)\-\d+\.css$%i", $file, $breakpointCssFile)) {
                        $origResource = SQUEEZR_DOCROOT.$path.DIRECTORY_SEPARATOR.$breakpointCssFile[1].'.css';
                    } else {
                        if (@unlink($absResource)) {
                            --$resourceCount;
                        }
                        continue 2;
                    }
                    break;
                case 'php':
                    if (preg_match("%^(.+)\-squeezr\.css\.php$%i", $file, $phpCacheFile)) {
                        $origResource = SQUEEZR_DOCROOT.$path.DIRECTORY_SEPARATOR.$phpCacheFile[1].'.css';
                    } else {
                        if (@unlink($absResource)) {
                            --$resourceCount;
                        }
                        continue 2;
                    }
                    break;
                case 'png':
                case 'jpg':
                case 'jpeg':
                case 'gif':
                    $origResource = SQUEEZR_DOCROOT.$path.DIRECTORY_SEPARATOR.$file;
                    break;
                default:
                    continue 2;
                    break;
            }
            if (!@is_file($origResource) || (@filemtime($origResource) > @filemtime($absResource))) {
                if (@unlink($absResource)) {
                    --$resourceCount;
                }
            }
        }
        foreach ($links as $link) {
            $absResource = $this->_base.$path.DIRECTORY_SEPARATOR.$link;
            $origResource = @readlink($absResource);
            if ((!$origResource || !@is_file($origResource)) && @unlink($absResource)) {
                --$resourceCount;
            }
        }
        return $resourceCount;
    }
private function _cleanDirectory ( $ path = '' ) { $ resources = scandir ( $ this -> _base . $ path ) ; $ resourceCount = count ( $ resources ) ; $ links = $ files = array ( ) ; foreach ( $ resources as $ resource ) { $ absResource = $ this -> _base . $ path . DIRECTORY_SEPARATOR . $ resource ; $ origResource = SQUEEZR_DOCROOT . $ path . DIRECTORY_SEPARATOR . $ resource ; if ( @ is_dir ( $ absResource ) && ( ( $ resource == '.' ) || ( $ resource == '..' ) ) ) { -- $ resourceCount ; continue ; } if ( @ is_dir ( $ absResource ) ) { if ( ! @ is_dir ( $ origResource ) || ! $ this -> _cleanDirectory ( ltrim ( $ path . DIRECTORY_SEPARATOR . $ resource , DIRECTORY_SEPARATOR ) ) ) { @ exec ( 'rm -R ' . escapeshellarg ( $ absResource ) ) ; -- $ resourceCount ; } } elseif ( @ is_link ( $ absResource ) ) { $ links [ ] = $ resource ; } elseif ( @ is_file ( $ absResource ) ) { $ files [ ] = $ resource ; } else { if ( @ unlink ( $ absResource ) ) { -- $ resourceCount ; } } } foreach ( $ files as $ file ) { $ absResource = $ this -> _base . $ path . DIRECTORY_SEPARATOR . $ file ; switch ( strtolower ( pathinfo ( $ file , PATHINFO_EXTENSION ) ) ) { case 'css' : if ( preg_match ( " %^(.+)\-\d+\.css$%i " , $ file , $ breakpointCssFile ) ) { $ origResource = SQUEEZR_DOCROOT . $ path . DIRECTORY_SEPARATOR . $ breakpointCssFile [ 1 ] . '.css' ; } else { if ( @ unlink ( $ absResource ) ) { -- $ resourceCount ; } continue 2 ; } break ; case 'php' : if ( preg_match ( " %^(.+)\-squeezr\.css\.php$%i " , $ file , $ phpCacheFile ) ) { $ origResource = SQUEEZR_DOCROOT . $ path . DIRECTORY_SEPARATOR . $ phpCacheFile [ 1 ] . '.css' ; } else { if ( @ unlink ( $ absResource ) ) { -- $ resourceCount ; } continue 2 ; } break ; case 'png' : case 'jpg' : case 'jpeg' : case 'gif' : $ origResource = SQUEEZR_DOCROOT . $ path . DIRECTORY_SEPARATOR . $ file ; break ; default : continue 2 ; break ; } if ( ! @ is_file ( $ origResource ) || ( @ filemtime ( $ origResource ) > @ filemtime ( $ absResource ) ) ) { if ( @ unlink ( $ absResource ) ) { -- $ resourceCount ; } } } foreach ( $ links as $ link ) { $ absResource = $ this -> _base . $ path . DIRECTORY_SEPARATOR . $ link ; $ origResource = @ readlink ( $ absResource ) ; if ( ( ! $ origResource || ! @ is_file ( $ origResource ) ) && @ unlink ( $ absResource ) ) { -- $ resourceCount ; } } return $ resourceCount ; }
Recursively clean a subdirectory
 @param string $path Subdirectory path
 @return int                                        Number of contained resources
Recursively clean a subdirectory @param string $path Subdirectory path @return int Number of contained resources
// Run through the directory contents // Ignore symbolic links to current and parent directory // If it's a directory ... // Clean recursively and remove it alltogether in case it remains empty // Else if it's a symbolic link: register it // Else if it's a regular file: register it // Else: No idea ... delete it! // Check & clean regular files // Proceed based on file extension // CSS file // If it's a breakpoint CSS file // Else: No idea ... delete it! // PHP cache file // If it's a PHP cache file // Else: No idea ... delete it! // Image files // Other extensions: Ignore // Remove the cache file if the original file doesn't exist anymore or is younger // Check & clean symbolic links // Delete the symbolic link if the original file doesn't exist anymore