<commit_before><?php

class KurogoWebBridge
{
    protected $platform = 'unknown';
    protected $module = 'unknown';
    protected $page = 'index';
    protected $path = '';
    protected $pathExists = false;
    
    const NATIVE_PLATFORM_PARAMETER = 'nativePlatform';
    const ASSET_CHECK_PARAMETER     = 'nativeAssetCheck';

    const INTERNAL_LINK_SCHEME = 'kgolink://';
    const CONFIG_LINK_SCHEME   = 'kgoconfig://';
    
    const FILE_TYPE_HTML       = 'html';
    const FILE_TYPE_CSS        = 'css';
    const FILE_TYPE_JAVASCRIPT = 'js';
    const FILE_TYPE_ASSET      = 'asset';
    
    // This global could be removed by using closures (ie php 5.3+)
    static protected $currentInstance = null;
    protected $processingFileType = self::FILE_TYPE_HTML;
    
    public function __construct($platform, $module) {
        $this->platform = $platform;
        $this->module = $module;
        $this->path = WEB_BRIDGE_DIR.DIRECTORY_SEPARATOR.$platform.DIRECTORY_SEPARATOR.$module;
    }

    public function setPage($page) {
        $this->page = $page;
    }

    protected function rmPath($path) {
        if (is_file($path)) {
            if (!is_writeable($path)) { chmod($path, 0666); }
            
            unlink($path);
            return !is_file($path);
            
        } else if (is_dir($path)) {
            if (!is_writeable($path)) { chmod($path, 0777); }
            
            $handle = opendir($path);
            while ($entry = readdir($handle)) {
                if ($entry != '..' && $entry != '.' && $entry != '') {
                    $this->rmPath($path.DIRECTORY_SEPARATOR.$entry);
                }
            }
            closedir($handle);
            
            rmdir($path);
            return !is_dir($path);
        }
        return true; // never existed
    }

    protected function _addPathToZip($path, $zip, $zipPath='') {
        $zipPath .= DIRECTORY_SEPARATOR.basename($path);
        
        if (is_file($path)) {
            $zip->addFile($path, $zipPath);
            
        } else if (is_dir($path)) {
            $zip->addEmptyDir($zipPath);
            
            $handle = opendir($path);
            while ($entry = readdir($handle)) {
                if ($entry != '..' && $entry != '.' && $entry != '') {
                    $this->_addPathToZip($path.DIRECTORY_SEPARATOR.$entry, $zip, $zipPath);
                }
            }
            closedir($handle);
        }
    }

    protected function zipPath($path, $zipFile) {
        if (!class_exists('ZipArchive')) {
            throw new KurogoException("class ZipArchive (php-zip) not available");
        }
        
        if (!is_dir($path) && !is_file($path)) {
            throw new KurogoException("$path not found");
        }
        
        $pathToZip = realpath($path);
        
        $zip = new ZipArchive();
        $zip->open($zipFile, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
        $this->_addPathToZip($path, $zip);
        $zip->close();
    }

    protected static function getFileType($file) {
        $parts = explode('.', $file);
        $ext = strtolower(end($parts));
        if (count($parts) < 2 || in_array($ext, array('html', 'php'))) {
            return self::FILE_TYPE_HTML;
            
        } else if (count($parts) > 1 && $ext == 'css') {
            return self::FILE_TYPE_CSS;
            
        } else if (count($parts) > 1 && $ext == 'js') {
            return self::FILE_TYPE_JAVASCRIPT;
            
        } else {
            return self::FILE_TYPE_ASSET;
        }
    }

    //
    // Helper functions to avoid code duplication
    //
    protected function getAsset($urlSuffix) {
        $url = FULL_URL_PREFIX.$urlSuffix.(stripos($urlSuffix, '?') ? '&' : '?').
            http_build_query(array(self::NATIVE_PLATFORM_PARAMETER => $this->platform));
        //error_log($url);
        $contents = @file_get_contents($url);
        if (!$contents) {
            Kurogo::log(LOG_NOTICE, "Failed to load asset $url", 'api');
        }
        return $contents;
    }

    protected function saveAsset($contents, $file) {
        $filePath = "{$this->path}/$file";
        
        $dir = dirname($filePath);
        if (!file_exists($dir)) {
            if (!mkdir($dir, 0700, true)) {
                throw new KurogoDataException("Could not create $dir");
            }
        }
        
        if (!file_put_contents($filePath, $contents)) {
            throw new KurogoDataException("Unable to write to $filePath");
        }
    }

    protected function urlSuffixToFile($urlSuffix) {
        return str_replace('/', '_',
            preg_replace(
                array(
                    '@device/native-[^/]+/@',
                    '@^min/\?g=file-/([^&]+)(&.+|)$@',
                    '@^min/g=([^-]+)-([^&]+)(&.+|)$@',
                    '@/(images|javascript|css)/@',
                    '@^modules/([^/]+)/@',
                    '@^common/@',
                ),
                array(
                    '',
                    '$1',
                    $this->page.'_min.$1',
                    '/',
                    '$1_',
                    '',
                ),
                ltrim($urlSuffix, '/')
            )
        );
    }

    protected static function getPartsForMatches($matches) {
        $urlSuffix = html_entity_decode($matches[4]);
        $file = self::$currentInstance->urlSuffixToFile($urlSuffix);
        
        if ($file) {
            $replacement = $matches[1].$file.$matches[5];
        } else {
            Kurogo::log(LOG_NOTICE, "Unable to determine file name for '{$matches[0]}'", 'api');
            $replacement = $matches[0];
        }
        
        return array($urlSuffix, $file, $replacement);
    }

    //
    // Callbacks for preg_replace_callback
    //

    protected static function rewriteURLsToFilePathsCallback($matches) {
        list($urlSuffix, $file, $replacement) = self::getPartsForMatches($matches);
        
        return $replacement;
    }<commit_msg>Do not overwrite fileloader urls<commit_after><?php

class KurogoWebBridge
{
    protected $platform = 'unknown';
    protected $module = 'unknown';
    protected $page = 'index';
    protected $path = '';
    protected $pathExists = false;
    
    const NATIVE_PLATFORM_PARAMETER = 'nativePlatform';
    const ASSET_CHECK_PARAMETER     = 'nativeAssetCheck';

    const INTERNAL_LINK_SCHEME = 'kgolink://';
    const CONFIG_LINK_SCHEME   = 'kgoconfig://';
    
    const FILE_TYPE_HTML       = 'html';
    const FILE_TYPE_CSS        = 'css';
    const FILE_TYPE_JAVASCRIPT = 'js';
    const FILE_TYPE_ASSET      = 'asset';
    
    // This global could be removed by using closures (ie php 5.3+)
    static protected $currentInstance = null;
    protected $processingFileType = self::FILE_TYPE_HTML;
    
    public function __construct($platform, $module) {
        $this->platform = $platform;
        $this->module = $module;
        $this->path = WEB_BRIDGE_DIR.DIRECTORY_SEPARATOR.$platform.DIRECTORY_SEPARATOR.$module;
    }

    public function setPage($page) {
        $this->page = $page;
    }

    protected function rmPath($path) {
        if (is_file($path)) {
            if (!is_writeable($path)) { chmod($path, 0666); }
            
            unlink($path);
            return !is_file($path);
            
        } else if (is_dir($path)) {
            if (!is_writeable($path)) { chmod($path, 0777); }
            
            $handle = opendir($path);
            while ($entry = readdir($handle)) {
                if ($entry != '..' && $entry != '.' && $entry != '') {
                    $this->rmPath($path.DIRECTORY_SEPARATOR.$entry);
                }
            }
            closedir($handle);
            
            rmdir($path);
            return !is_dir($path);
        }
        return true; // never existed
    }

    protected function _addPathToZip($path, $zip, $zipPath='') {
        $zipPath .= DIRECTORY_SEPARATOR.basename($path);
        
        if (is_file($path)) {
            $zip->addFile($path, $zipPath);
            
        } else if (is_dir($path)) {
            $zip->addEmptyDir($zipPath);
            
            $handle = opendir($path);
            while ($entry = readdir($handle)) {
                if ($entry != '..' && $entry != '.' && $entry != '') {
                    $this->_addPathToZip($path.DIRECTORY_SEPARATOR.$entry, $zip, $zipPath);
                }
            }
            closedir($handle);
        }
    }

    protected function zipPath($path, $zipFile) {
        if (!class_exists('ZipArchive')) {
            throw new KurogoException("class ZipArchive (php-zip) not available");
        }
        
        if (!is_dir($path) && !is_file($path)) {
            throw new KurogoException("$path not found");
        }
        
        $pathToZip = realpath($path);
        
        $zip = new ZipArchive();
        $zip->open($zipFile, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
        $this->_addPathToZip($path, $zip);
        $zip->close();
    }

    protected static function getFileType($file) {
        $parts = explode('.', $file);
        $ext = strtolower(end($parts));
        if (count($parts) < 2 || in_array($ext, array('html', 'php'))) {
            return self::FILE_TYPE_HTML;
            
        } else if (count($parts) > 1 && $ext == 'css') {
            return self::FILE_TYPE_CSS;
            
        } else if (count($parts) > 1 && $ext == 'js') {
            return self::FILE_TYPE_JAVASCRIPT;
            
        } else {
            return self::FILE_TYPE_ASSET;
        }
    }

    //
    // Helper functions to avoid code duplication
    //
    protected function getAsset($urlSuffix) {
        $url = FULL_URL_PREFIX.$urlSuffix.(stripos($urlSuffix, '?') ? '&' : '?').
            http_build_query(array(self::NATIVE_PLATFORM_PARAMETER => $this->platform));
        //error_log($url);
        $contents = @file_get_contents($url);
        if (!$contents) {
            Kurogo::log(LOG_NOTICE, "Failed to load asset $url", 'api');
        }
        return $contents;
    }

    protected function saveAsset($contents, $file) {
        $filePath = "{$this->path}/$file";
        
        $dir = dirname($filePath);
        if (!file_exists($dir)) {
            if (!mkdir($dir, 0700, true)) {
                throw new KurogoDataException("Could not create $dir");
            }
        }
        
        if (!file_put_contents($filePath, $contents)) {
            throw new KurogoDataException("Unable to write to $filePath");
        }
    }

    protected function urlSuffixToFile($urlSuffix) {
        return str_replace('/', '_',
            preg_replace(
                array(
                    '@device/native-[^/]+/@',
                    '@^min/\?g=file-/([^&]+)(&.+|)$@',
                    '@^min/g=([^-]+)-([^&]+)(&.+|)$@',
                    '@/(images|javascript|css)/@',
                    '@^modules/([^/]+)/@',
                    '@^common/@',
                ),
                array(
                    '',
                    '$1',
                    $this->page.'_min.$1',
                    '/',
                    '$1_',
                    '',
                ),
                ltrim($urlSuffix, '/')
            )
        );
    }

    protected static function getPartsForMatches($matches) {
        $urlSuffix = html_entity_decode($matches[4]);
        $file = self::$currentInstance->urlSuffixToFile($urlSuffix);
        
        if (strpos($matches[0], '/'.FileLoader::fileDir().'/') !== FALSE) {
            $file = '';                  // do not rewrite fileloader urls
            $replacement = $matches[0];
            
        } else if ($file) {
            $replacement = $matches[1].$file.$matches[5];
            
        } else {
            Kurogo::log(LOG_NOTICE, "Unable to determine file name for '{$matches[0]}'", 'api');
            $replacement = $matches[0];
        }
        
        return array($urlSuffix, $file, $replacement);
    }

    //
    // Callbacks for preg_replace_callback
    //

    protected static function rewriteURLsToFilePathsCallback($matches) {
        list($urlSuffix, $file, $replacement) = self::getPartsForMatches($matches);
        
        return $replacement;
    }
{'fraction_non_alphanumeric': 0.15382116462275644, 'fraction_numerical': 0.0042231787541622675, 'mean_word_length': 1.7123348017621145, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3913779', 'n_tokens_mistral': 3722, 'n_tokens_neox': 3580, 'n_words': 663}