Exemplo n.º 1
0
 /**
  * @depends test_ext_bz2
  */
 function test_bzfiles()
 {
     $this->assertEquals("The\nTest\n", io_readFile(__DIR__ . '/io_readfile/test.txt.bz2'));
     $this->assertEquals("The\r\nTest\r\n", io_readFile(__DIR__ . '/io_readfile/test.txt.bz2', false));
     $this->assertEquals(false, io_readFile(__DIR__ . '/io_readfile/nope.txt.bz2'));
     $this->assertEquals(false, io_readFile(__DIR__ . '/io_readfile/corrupt.txt.bz2'));
     // internal bzfile function
     $this->assertEquals(array("The\r\n", "Test\r\n"), bzfile(__DIR__ . '/io_readfile/test.txt.bz2', true));
     $this->assertEquals(array_fill(0, 120, str_repeat('a', 80) . "\n"), bzfile(__DIR__ . '/io_readfile/large.txt.bz2', true));
     $line = str_repeat('a', 8888) . "\n";
     $this->assertEquals(array($line, "\n", $line, "!"), bzfile(__DIR__ . '/io_readfile/long.txt.bz2', true));
 }
Exemplo n.º 2
0
/**
 * Returns content of $file as cleaned string.
 *
 * Uses gzip if extension is .gz
 *
 * If you want to use the returned value in unserialize
 * be sure to set $clean to false!
 *
 * @author  Andreas Gohr <*****@*****.**>
 */
function io_readFile($file, $clean = true)
{
    $ret = '';
    if (@file_exists($file)) {
        if (substr($file, -3) == '.gz') {
            $ret = join('', gzfile($file));
        } else {
            if (substr($file, -4) == '.bz2') {
                $ret = bzfile($file);
            } else {
                $ret = file_get_contents($file);
            }
        }
    }
    if ($clean) {
        return cleanText($ret);
    } else {
        return $ret;
    }
}
Exemplo n.º 3
0
/**
 * Replace one or more occurrences of a line in a file.
 *
 * The default, when $maxlines is 0 is to delete all matching lines then append a single line.
 * A regex that matches any part of the line will remove the entire line in this mode.
 * Captures in $newline are not available.
 *
 * Otherwise each line is matched and replaced individually, up to the first $maxlines lines
 * or all lines if $maxlines is -1. If $regex is true then captures can be used in $newline.
 *
 * Be sure to include the trailing newline in $oldline when replacing entire lines.
 *
 * Uses gzip if extension is .gz
 * and bz2 if extension is .bz2
 *
 * @author Steven Danz <*****@*****.**>
 * @author Christopher Smith <*****@*****.**>
 * @author Patrick Brown <*****@*****.**>
 *
 * @param string $file     filename
 * @param string $oldline  exact linematch to remove
 * @param string $newline  new line to insert
 * @param bool   $regex    use regexp?
 * @param int    $maxlines number of occurrences of the line to replace
 * @return bool true on success
 */
function io_replaceInFile($file, $oldline, $newline, $regex = false, $maxlines = 0)
{
    if ((string) $oldline === '') {
        trigger_error('$oldline parameter cannot be empty in io_replaceInFile()', E_USER_WARNING);
        return false;
    }
    if (!file_exists($file)) {
        return true;
    }
    io_lock($file);
    // load into array
    if (substr($file, -3) == '.gz') {
        if (!DOKU_HAS_GZIP) {
            return false;
        }
        $lines = gzfile($file);
    } else {
        if (substr($file, -4) == '.bz2') {
            if (!DOKU_HAS_BZIP) {
                return false;
            }
            $lines = bzfile($file, true);
        } else {
            $lines = file($file);
        }
    }
    // make non-regexes into regexes
    $pattern = $regex ? $oldline : '/^' . preg_quote($oldline, '/') . '$/';
    $replace = $regex ? $newline : addcslashes($newline, '\\$');
    // remove matching lines
    if ($maxlines > 0) {
        $count = 0;
        $matched = 0;
        while ($count < $maxlines && (list($i, $line) = each($lines))) {
            // $matched will be set to 0|1 depending on whether pattern is matched and line replaced
            $lines[$i] = preg_replace($pattern, $replace, $line, -1, $matched);
            if ($matched) {
                $count++;
            }
        }
    } else {
        if ($maxlines == 0) {
            $lines = preg_grep($pattern, $lines, PREG_GREP_INVERT);
            if ((string) $newline !== '') {
                $lines[] = $newline;
            }
        } else {
            $lines = preg_replace($pattern, $replace, $lines);
        }
    }
    if (count($lines)) {
        if (!_io_saveFile($file, join('', $lines), false)) {
            msg("Removing content from {$file} failed", -1);
            io_unlock($file);
            return false;
        }
    } else {
        @unlink($file);
    }
    io_unlock($file);
    return true;
}