protected static function getValidwwwDir( $phpSelf, $scriptFileName, $index )
    {
        if ( !isset( $phpSelf[1] ) || empty($index) || strpos( $phpSelf, $index ) === false )
            return false;

        // validate $index straight away
        if ( strpos( $scriptFileName, $index ) === false )
            return null;

        // optimize '/index.php' pattern
        if ( $phpSelf === "/{$index}" )
            return '';

        $phpSelfParts = explode( $index, $phpSelf );
        $validateDir = $phpSelfParts[0];
        // remove first path if home dir
        if ( $phpSelf[1] === '~' )
        {
            $uri = explode( '/', ltrim( $validateDir, '/' ) );
            $userName = ltrim( array_shift( $uri ), "~" );

            // validating that we have user/ in path
            if ( !strpos( $scriptFileName, $userName ."/" ) )
            {
                return null;
            }

            $validateDir = '/' . implode( '/', $uri );
        }

        if ( !empty( $validateDir ) )
        {
            // validate directly with phpself part
            if ( strpos( $scriptFileName, $validateDir ) !== false )
                return trim( $phpSelfParts[0], '/' );

            // validate with windows path
            if ( strpos( $scriptFileName, str_replace( '/', '\\', $validateDir ) ) !== false )
                return trim( $phpSelfParts[0], '/' );
        }

        return null;
    }