Esempio n. 1
10
function file_write($path, $data, $simple = false, $skip_purge = false)
{
    global $config, $debug;
    if (preg_match('/^remote:\\/\\/(.+)\\:(.+)$/', $path, $m)) {
        if (isset($config['remote'][$m[1]])) {
            require_once 'inc/remote.php';
            $remote = new Remote($config['remote'][$m[1]]);
            $remote->write($data, $m[2]);
            return;
        } else {
            error('Invalid remote server: ' . $m[1]);
        }
    }
    if (!function_exists("dio_truncate")) {
        if (!($fp = fopen($path, $simple ? 'w' : 'c'))) {
            error('Unable to open file for writing: ' . $path);
        }
        // File locking
        if (!$simple && !flock($fp, LOCK_EX)) {
            error('Unable to lock file: ' . $path);
        }
        // Truncate file
        if (!$simple && !ftruncate($fp, 0)) {
            error('Unable to truncate file: ' . $path);
        }
        // Write data
        if (($bytes = fwrite($fp, $data)) === false) {
            error('Unable to write to file: ' . $path);
        }
        // Unlock
        if (!$simple) {
            flock($fp, LOCK_UN);
        }
        // Close
        if (!fclose($fp)) {
            error('Unable to close file: ' . $path);
        }
    } else {
        if (!($fp = dio_open($path, O_WRONLY | O_CREAT, 0644))) {
            error('Unable to open file for writing: ' . $path);
        }
        // File locking
        if (dio_fcntl($fp, F_SETLKW, array('type' => F_WRLCK)) === -1) {
            error('Unable to lock file: ' . $path);
        }
        // Truncate file
        if (!dio_truncate($fp, 0)) {
            error('Unable to truncate file: ' . $path);
        }
        // Write data
        if (($bytes = dio_write($fp, $data)) === false) {
            error('Unable to write to file: ' . $path);
        }
        // Unlock
        dio_fcntl($fp, F_SETLK, array('type' => F_UNLCK));
        // Close
        dio_close($fp);
    }
    /**
     * Create gzipped file.
     *
     * When writing into a file foo.bar and the size is larger or equal to 1
     * KiB, this also produces the gzipped version foo.bar.gz
     *
     * This is useful with nginx with gzip_static on.
     */
    if ($config['gzip_static']) {
        $gzpath = "{$path}.gz";
        if ($bytes & ~0x3ff) {
            // if ($bytes >= 1024)
            if (file_put_contents($gzpath, gzencode($data), $simple ? 0 : LOCK_EX) === false) {
                error("Unable to write to file: {$gzpath}");
            }
            if (!touch($gzpath, filemtime($path), fileatime($path))) {
                error("Unable to touch file: {$gzpath}");
            }
        } else {
            @unlink($gzpath);
        }
    }
    if (!$skip_purge && isset($config['purge'])) {
        // Purge cache
        if (basename($path) == $config['file_index']) {
            // Index file (/index.html); purge "/" as well
            $uri = dirname($path);
            // root
            if ($uri == '.') {
                $uri = '';
            } else {
                $uri .= '/';
            }
            purge($uri);
        }
        purge($path);
    }
    if ($config['debug']) {
        $debug['write'][] = $path . ': ' . $bytes . ' bytes';
    }
    event('write', $path);
}
Esempio n. 2
0
function file_write($path, $data, $simple = false, $skip_purge = false)
{
    global $config, $debug;
    //echo "file_write($path, ", strlen($data), ", $simple, $skip_purge)<br>\n";
    $board = '';
    if (strpos($path, '/') !== false) {
        $parts = explode('/', $path, 2);
        $board = $parts[0];
    }
    $useCache = true;
    $useFile = false;
    if ($path == 'main.js') {
        $useCache = false;
        $useFile = true;
    }
    if ($board && $useCache) {
        if (!isset($config['cache']['odiliMagicBoards'][$board])) {
            $useCache = false;
            $useFile = true;
        } else {
            $type = strtolower($config['cache']['odiliMagicBoards'][$board]);
            if ($type === 'hybrid') {
                $useFile = true;
                $useCache = true;
            } elseif ($type === 'memory') {
                // defaults will be fine
                $useCache = true;
                $useFile = false;
            } else {
                $useCache = false;
                $useFile = true;
            }
        }
    }
    if ($useCache) {
        Cache::store('vichan_filecache_' . $path, $data, -1);
        if ($config['gzip_static']) {
            $bytes = strlen($data);
            if ($bytes & ~0x3ff) {
                Cache::store('vichan_filecache_' . $path . '.gz', gzencode($data), -1);
            } else {
                Cache::delete('vichan_filecache_' . $path . '.gz');
            }
        }
    }
    if (preg_match('/^remote:\\/\\/(.+)\\:(.+)$/', $path, $m)) {
        if (isset($config['remote'][$m[1]])) {
            require_once 'inc/remote.php';
            $remote = new Remote($config['remote'][$m[1]]);
            $remote->write($data, $m[2]);
            return;
        } else {
            error('Invalid remote server: ' . $m[1]);
        }
    }
    if ($useFile) {
        if (!function_exists("dio_truncate")) {
            if (!($fp = fopen($path, $simple ? 'w' : 'c'))) {
                error('Unable to open file for writing: ' . $path);
            }
            // File locking
            if (!$simple && !flock($fp, LOCK_EX)) {
                error('Unable to lock file: ' . $path);
            }
            // Truncate file
            if (!$simple && !ftruncate($fp, 0)) {
                error('Unable to truncate file: ' . $path);
            }
            // Write data
            if (($bytes = fwrite($fp, $data)) === false) {
                error('Unable to write to file: ' . $path);
            }
            // Unlock
            if (!$simple) {
                flock($fp, LOCK_UN);
            }
            // Close
            if (!fclose($fp)) {
                error('Unable to close file: ' . $path);
            }
        } else {
            if (!($fp = dio_open($path, O_WRONLY | O_CREAT, 0644))) {
                error('Unable to open file for writing: ' . $path);
            }
            // File locking
            if (dio_fcntl($fp, F_SETLKW, array('type' => F_WRLCK)) === -1) {
                error('Unable to lock file: ' . $path);
            }
            // Truncate file
            if (!dio_truncate($fp, 0)) {
                error('Unable to truncate file: ' . $path);
            }
            // Write data
            if (($bytes = dio_write($fp, $data)) === false) {
                error('Unable to write to file: ' . $path);
            }
            // Unlock
            dio_fcntl($fp, F_SETLK, array('type' => F_UNLCK));
            // Close
            dio_close($fp);
        }
        /**
         * Create gzipped file.
         *
         * When writing into a file foo.bar and the size is larger or equal to 1
         * KiB, this also produces the gzipped version foo.bar.gz
         *
         * This is useful with nginx with gzip_static on.
         */
        if ($config['gzip_static']) {
            $gzpath = "{$path}.gz";
            if ($bytes & ~0x3ff) {
                // if ($bytes >= 1024)
                if (file_put_contents($gzpath, gzencode($data), $simple ? 0 : LOCK_EX) === false) {
                    error("Unable to write to file: {$gzpath}");
                }
                if (!touch($gzpath, filemtime($path), fileatime($path))) {
                    error("Unable to touch file: {$gzpath}");
                }
            } else {
                @unlink($gzpath);
            }
        }
    }
    if (!$skip_purge && isset($config['purge'])) {
        // Purge cache
        if (basename($path) == $config['file_index']) {
            // Index file (/index.html); purge "/" as well
            $uri = dirname($path);
            // root
            if ($uri == '.') {
                $uri = '';
            } else {
                $uri .= '/';
            }
            purge($uri);
        }
        purge($path);
    }
    if ($config['debug']) {
        $bytes = strlen($data);
        $debug['write'][] = $path . ': ' . $bytes . ' bytes';
    }
    event('write', $path);
}
Esempio n. 3
0
function file_write($path, $data, $simple = false, $skip_purge = false)
{
    global $config, $debug;
    if (preg_match('/^remote:\\/\\/(.+)\\:(.+)$/', $path, $m)) {
        if (isset($config['remote'][$m[1]])) {
            require_once 'inc/remote.php';
            $remote = new Remote($config['remote'][$m[1]]);
            $remote->write($data, $m[2]);
            return;
        } else {
            error('Invalid remote server: ' . $m[1]);
        }
    }
    if (!($fp = fopen($path, $simple ? 'w' : 'c'))) {
        error('Unable to open file for writing: ' . $path);
    }
    // File locking
    if (!$simple && !flock($fp, LOCK_EX)) {
        error('Unable to lock file: ' . $path);
    }
    // Truncate file
    if (!$simple && !ftruncate($fp, 0)) {
        error('Unable to truncate file: ' . $path);
    }
    // Write data
    if (($bytes = fwrite($fp, $data)) === false) {
        error('Unable to write to file: ' . $path);
    }
    // Unlock
    if (!$simple) {
        flock($fp, LOCK_UN);
    }
    // Close
    if (!fclose($fp)) {
        error('Unable to close file: ' . $path);
    }
    if (!$skip_purge && isset($config['purge'])) {
        // Purge cache
        if (basename($path) == $config['file_index']) {
            // Index file (/index.html); purge "/" as well
            $uri = dirname($path);
            // root
            if ($uri == '.') {
                $uri = '';
            } else {
                $uri .= '/';
            }
            purge($uri);
        }
        purge($path);
    }
    if ($config['debug']) {
        $debug['write'][] = $path . ': ' . $bytes . ' bytes';
    }
    event('write', $path);
}
Esempio n. 4
0
function file_write($path, $data, $simple = false, $skip_purge = false)
{
    global $config, $debug;
    //echo "file_write($path, ", strlen($data), ", $simple, $skip_purge)<br>\n";
    Cache::store('vichan_filecache_' . $path, $data, -1);
    if ($config['gzip_static']) {
        $bytes = strlen($data);
        if ($bytes & ~0x3ff) {
            Cache::store('vichan_filecache_' . $path . '.gz', gzencode($data), -1);
        } else {
            Cache::delete('vichan_filecache_' . $path . '.gz');
        }
    }
    if (preg_match('/^remote:\\/\\/(.+)\\:(.+)$/', $path, $m)) {
        if (isset($config['remote'][$m[1]])) {
            require_once 'inc/remote.php';
            $remote = new Remote($config['remote'][$m[1]]);
            $remote->write($data, $m[2]);
            return;
        } else {
            error('Invalid remote server: ' . $m[1]);
        }
    }
    /*
    	if (!$fp = fopen($path, $simple ? 'w' : 'c'))
    		error('Unable to open file for writing: ' . $path);
    
    	// File locking
    	if (!$simple && !flock($fp, LOCK_EX)) {
    		error('Unable to lock file: ' . $path);
    	}
    
    	// Truncate file
    	if (!$simple && !ftruncate($fp, 0))
    		error('Unable to truncate file: ' . $path);
    
    	// Write data
    	if (($bytes = fwrite($fp, $data)) === false)
    		error('Unable to write to file: ' . $path);
    
    	// Unlock
    	if (!$simple)
    		flock($fp, LOCK_UN);
    
    	// Close
    	if (!fclose($fp))
    		error('Unable to close file: ' . $path);
    */
    /**
     * Create gzipped file.
     *
     * When writing into a file foo.bar and the size is larger or equal to 1
     * KiB, this also produces the gzipped version foo.bar.gz
     *
     * This is useful with nginx with gzip_static on.
     */
    /*
    	if ($config['gzip_static']) {
    		$gzpath = "$path.gz";
    
    		if ($bytes & ~0x3ff) {  // if ($bytes >= 1024)
    			if (file_put_contents($gzpath, gzencode($data), $simple ? 0 : LOCK_EX) === false)
    				error("Unable to write to file: $gzpath");
    			//if (!touch($gzpath, filemtime($path), fileatime($path)))
    			//	error("Unable to touch file: $gzpath");
    		}
    		else {
    			@unlink($gzpath);
    		}
    	}
    */
    if (!$skip_purge && isset($config['purge'])) {
        // Purge cache
        if (basename($path) == $config['file_index']) {
            // Index file (/index.html); purge "/" as well
            $uri = dirname($path);
            // root
            if ($uri == '.') {
                $uri = '';
            } else {
                $uri .= '/';
            }
            purge($uri);
        }
        purge($path);
    }
    if ($config['debug']) {
        $bytes = strlen($data);
        $debug['write'][] = $path . ': ' . $bytes . ' bytes';
    }
    event('write', $path);
}