private function _checkDir($filepath, $dirs)
    {
        $directory = dirname($filepath) . DS;
        $_directory = array();
        while (true) {
            // remember the directory to add it to _resource_dir in case we're successful
            $_directory[$directory] = true;
            // test if the directory is trusted
            if (isset($dirs[$directory])) {
                // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
                $dirs = array_merge($dirs, $_directory);

                return $dirs;
            }
            // abort if we've reached root
            if (!preg_match('#[\\\/][^\\\/]+[\\\/]$#', $directory)) {
                break;
            }
            // bubble up one level
            $directory = preg_replace('#[\\\/][^\\\/]+[\\\/]$#', DS, $directory);
        }

        // give up
        throw new SmartyException("directory '{$filepath}' not allowed by security setting");
    }