xref: /dokuwiki/inc/io.php (revision d868eb89f182718a31113373a6272670bd7f8012)
1ed7b5f09Sandi<?php
215fae107Sandi/**
315fae107Sandi * File IO functions
415fae107Sandi *
515fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
615fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
715fae107Sandi */
824870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
95a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient;
10cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
11198564abSMichael Große
12f3f0262cSandi/**
1353d6ccfeSandi * Removes empty directories
1453d6ccfeSandi *
15cc7d0c94SBen Coburn * Sends IO_NAMESPACE_DELETED events for 'pages' and 'media' namespaces.
16cc7d0c94SBen Coburn * Event data:
17cc7d0c94SBen Coburn * $data[0]    ns: The colon separated namespace path minus the trailing page name.
18cc7d0c94SBen Coburn * $data[1]    ns_type: 'pages' or 'media' namespace tree.
19cc7d0c94SBen Coburn *
20d186898bSAndreas Gohr * @param string $id      - a pageid, the namespace of that id will be tried to deleted
21cd2f903bSMichael Hamann * @param string $basedir - the config name of the type to delete (datadir or mediadir usally)
22cd2f903bSMichael Hamann * @return bool - true if at least one namespace was deleted
2342ea7f44SGerrit Uitslag *
2453d6ccfeSandi * @author  Andreas Gohr <andi@splitbrain.org>
25cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
2653d6ccfeSandi */
27*d868eb89SAndreas Gohrfunction io_sweepNS($id, $basedir = 'datadir')
28*d868eb89SAndreas Gohr{
2953d6ccfeSandi    global $conf;
3024870174SAndreas Gohr    $types = ['datadir'=>'pages', 'mediadir'=>'media'];
3124870174SAndreas Gohr    $ns_type = ($types[$basedir] ?? false);
3253d6ccfeSandi
33d186898bSAndreas Gohr    $delone = false;
34d186898bSAndreas Gohr
3553d6ccfeSandi    //scan all namespaces
3653d6ccfeSandi    while(($id = getNS($id)) !== false){
37755f1e03SAndreas Gohr        $dir = $conf[$basedir].'/'.utf8_encodeFN(str_replace(':', '/', $id));
3853d6ccfeSandi
3953d6ccfeSandi        //try to delete dir else return
40cc7d0c94SBen Coburn        if(@rmdir($dir)) {
41cc7d0c94SBen Coburn            if ($ns_type!==false) {
4224870174SAndreas Gohr                $data = [$id, $ns_type];
43d186898bSAndreas Gohr                $delone = true; // we deleted at least one dir
44cbb44eabSAndreas Gohr                Event::createAndTrigger('IO_NAMESPACE_DELETED', $data);
45cc7d0c94SBen Coburn            }
46d186898bSAndreas Gohr        } else { return $delone; }
47cc7d0c94SBen Coburn    }
48d186898bSAndreas Gohr    return $delone;
49cc7d0c94SBen Coburn}
50cc7d0c94SBen Coburn
51cc7d0c94SBen Coburn/**
52cc7d0c94SBen Coburn * Used to read in a DokuWiki page from file, and send IO_WIKIPAGE_READ events.
53cc7d0c94SBen Coburn *
54cc7d0c94SBen Coburn * Generates the action event which delegates to io_readFile().
55cc7d0c94SBen Coburn * Action plugins are allowed to modify the page content in transit.
56cc7d0c94SBen Coburn * The file path should not be changed.
57cc7d0c94SBen Coburn *
58cc7d0c94SBen Coburn * Event data:
59cc7d0c94SBen Coburn * $data[0]    The raw arguments for io_readFile as an array.
60cc7d0c94SBen Coburn * $data[1]    ns: The colon separated namespace path minus the trailing page name. (false if root ns)
61cc7d0c94SBen Coburn * $data[2]    page_name: The wiki page name.
62cc7d0c94SBen Coburn * $data[3]    rev: The page revision, false for current wiki pages.
63cc7d0c94SBen Coburn *
64cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
6542ea7f44SGerrit Uitslag *
6642ea7f44SGerrit Uitslag * @param string   $file filename
6742ea7f44SGerrit Uitslag * @param string   $id page id
6842ea7f44SGerrit Uitslag * @param bool|int $rev revision timestamp
6942ea7f44SGerrit Uitslag * @return string
70cc7d0c94SBen Coburn */
71*d868eb89SAndreas Gohrfunction io_readWikiPage($file, $id, $rev = false)
72*d868eb89SAndreas Gohr{
73cc7d0c94SBen Coburn    if (empty($rev)) { $rev = false; }
7424870174SAndreas Gohr    $data = [[$file, true], getNS($id), noNS($id), $rev];
75cbb44eabSAndreas Gohr    return Event::createAndTrigger('IO_WIKIPAGE_READ', $data, '_io_readWikiPage_action', false);
76cc7d0c94SBen Coburn}
77cc7d0c94SBen Coburn
78cc7d0c94SBen Coburn/**
79cc7d0c94SBen Coburn * Callback adapter for io_readFile().
8042ea7f44SGerrit Uitslag *
81cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
8242ea7f44SGerrit Uitslag *
8342ea7f44SGerrit Uitslag * @param array $data event data
8442ea7f44SGerrit Uitslag * @return string
85cc7d0c94SBen Coburn */
86*d868eb89SAndreas Gohrfunction _io_readWikiPage_action($data)
87*d868eb89SAndreas Gohr{
88cc7d0c94SBen Coburn    if (is_array($data) && is_array($data[0]) && count($data[0])===2) {
8924870174SAndreas Gohr        return io_readFile(...$data[0]);
90cc7d0c94SBen Coburn    } else {
91cc7d0c94SBen Coburn        return ''; //callback error
9253d6ccfeSandi    }
9353d6ccfeSandi}
9453d6ccfeSandi
9553d6ccfeSandi/**
9615fae107Sandi * Returns content of $file as cleaned string.
9715fae107Sandi *
9815fae107Sandi * Uses gzip if extension is .gz
9915fae107Sandi *
100ee4c4a1bSAndreas Gohr * If you want to use the returned value in unserialize
101ee4c4a1bSAndreas Gohr * be sure to set $clean to false!
102ee4c4a1bSAndreas Gohr *
10315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
10442ea7f44SGerrit Uitslag *
10542ea7f44SGerrit Uitslag * @param string $file  filename
10642ea7f44SGerrit Uitslag * @param bool   $clean
107d387bf5eSAndreas Gohr * @return string|bool the file contents or false on error
108f3f0262cSandi */
109*d868eb89SAndreas Gohrfunction io_readFile($file, $clean = true)
110*d868eb89SAndreas Gohr{
111f3f0262cSandi    $ret = '';
11279e79377SAndreas Gohr    if(file_exists($file)){
113f3f0262cSandi        if (substr($file, -3) == '.gz') {
11413c37900SAndreas Gohr            if(!DOKU_HAS_GZIP) return false;
1152ad45addSAndreas Gohr            $ret = gzfile($file);
11624870174SAndreas Gohr            if(is_array($ret)) $ret = implode('', $ret);
117ff3ed99fSmarcel        } elseif (substr($file, -4) == '.bz2') {
11813c37900SAndreas Gohr            if(!DOKU_HAS_BZIP) return false;
119ff3ed99fSmarcel            $ret = bzfile($file);
120f3f0262cSandi        } else{
12143078d10SAndreas Gohr            $ret = file_get_contents($file);
122f3f0262cSandi        }
123f3f0262cSandi    }
1242ad45addSAndreas Gohr    if($ret === null) return false;
125d387bf5eSAndreas Gohr    if($ret !== false && $clean){
126f3f0262cSandi        return cleanText($ret);
127e34c0709SAndreas Gohr    }else{
128e34c0709SAndreas Gohr        return $ret;
129e34c0709SAndreas Gohr    }
130f3f0262cSandi}
131ff3ed99fSmarcel/**
132ff3ed99fSmarcel * Returns the content of a .bz2 compressed file as string
13342ea7f44SGerrit Uitslag *
134ff3ed99fSmarcel * @author marcel senf <marcel@rucksackreinigung.de>
135d387bf5eSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
13642ea7f44SGerrit Uitslag *
13742ea7f44SGerrit Uitslag * @param string $file filename
138cfb71e37SPatrick Brown * @param bool   $array return array of lines
139cfb71e37SPatrick Brown * @return string|array|bool content or false on error
140ff3ed99fSmarcel */
141*d868eb89SAndreas Gohrfunction bzfile($file, $array = false)
142*d868eb89SAndreas Gohr{
143ff3ed99fSmarcel    $bz = bzopen($file, "r");
144d387bf5eSAndreas Gohr    if($bz === false) return false;
145d387bf5eSAndreas Gohr
14624870174SAndreas Gohr    if($array) $lines = [];
147cd2f903bSMichael Hamann    $str = '';
148ff3ed99fSmarcel    while (!feof($bz)) {
149ff3ed99fSmarcel        //8192 seems to be the maximum buffersize?
150d387bf5eSAndreas Gohr        $buffer = bzread($bz, 8192);
151d387bf5eSAndreas Gohr        if(($buffer === false) || (bzerrno($bz) !== 0)) {
152d387bf5eSAndreas Gohr            return false;
153d387bf5eSAndreas Gohr        }
15424870174SAndreas Gohr        $str .= $buffer;
155cfb71e37SPatrick Brown        if($array) {
156cfb71e37SPatrick Brown            $pos = strpos($str, "\n");
157cfb71e37SPatrick Brown            while($pos !== false) {
158cfb71e37SPatrick Brown                $lines[] = substr($str, 0, $pos+1);
159cfb71e37SPatrick Brown                $str = substr($str, $pos+1);
160cfb71e37SPatrick Brown                $pos = strpos($str, "\n");
161cfb71e37SPatrick Brown            }
162cfb71e37SPatrick Brown        }
163ff3ed99fSmarcel    }
164ff3ed99fSmarcel    bzclose($bz);
165cfb71e37SPatrick Brown    if($array) {
166cfb71e37SPatrick Brown        if($str !== '') $lines[] = $str;
167cfb71e37SPatrick Brown        return $lines;
168cfb71e37SPatrick Brown    }
169ff3ed99fSmarcel    return $str;
170ff3ed99fSmarcel}
171ff3ed99fSmarcel
172f3f0262cSandi/**
173cc7d0c94SBen Coburn * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events.
174cc7d0c94SBen Coburn *
175cc7d0c94SBen Coburn * This generates an action event and delegates to io_saveFile().
176cc7d0c94SBen Coburn * Action plugins are allowed to modify the page content in transit.
177cc7d0c94SBen Coburn * The file path should not be changed.
178cc7d0c94SBen Coburn * (The append parameter is set to false.)
179cc7d0c94SBen Coburn *
180cc7d0c94SBen Coburn * Event data:
181cc7d0c94SBen Coburn * $data[0]    The raw arguments for io_saveFile as an array.
182cc7d0c94SBen Coburn * $data[1]    ns: The colon separated namespace path minus the trailing page name. (false if root ns)
183cc7d0c94SBen Coburn * $data[2]    page_name: The wiki page name.
184cc7d0c94SBen Coburn * $data[3]    rev: The page revision, false for current wiki pages.
185cc7d0c94SBen Coburn *
186cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
18742ea7f44SGerrit Uitslag *
18842ea7f44SGerrit Uitslag * @param string $file      filename
18942ea7f44SGerrit Uitslag * @param string $content
19042ea7f44SGerrit Uitslag * @param string $id        page id
19142ea7f44SGerrit Uitslag * @param int|bool $rev timestamp of revision
19242ea7f44SGerrit Uitslag * @return bool
193cc7d0c94SBen Coburn */
194*d868eb89SAndreas Gohrfunction io_writeWikiPage($file, $content, $id, $rev = false)
195*d868eb89SAndreas Gohr{
196cc7d0c94SBen Coburn    if (empty($rev)) { $rev = false; }
197cc7d0c94SBen Coburn    if ($rev===false) { io_createNamespace($id); } // create namespaces as needed
19824870174SAndreas Gohr    $data = [[$file, $content, false], getNS($id), noNS($id), $rev];
199cbb44eabSAndreas Gohr    return Event::createAndTrigger('IO_WIKIPAGE_WRITE', $data, '_io_writeWikiPage_action', false);
200cc7d0c94SBen Coburn}
201cc7d0c94SBen Coburn
202cc7d0c94SBen Coburn/**
203cc7d0c94SBen Coburn * Callback adapter for io_saveFile().
204cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
20542ea7f44SGerrit Uitslag *
20642ea7f44SGerrit Uitslag * @param array $data event data
20742ea7f44SGerrit Uitslag * @return bool
208cc7d0c94SBen Coburn */
209*d868eb89SAndreas Gohrfunction _io_writeWikiPage_action($data)
210*d868eb89SAndreas Gohr{
211cc7d0c94SBen Coburn    if (is_array($data) && is_array($data[0]) && count($data[0])===3) {
21224870174SAndreas Gohr        $ok = io_saveFile(...$data[0]);
213a4306b74SAndreas Gohr        // for attic files make sure the file has the mtime of the revision
214a4306b74SAndreas Gohr        if($ok && is_int($data[3]) && $data[3] > 0) {
215a4306b74SAndreas Gohr            @touch($data[0][0], $data[3]);
216a4306b74SAndreas Gohr        }
217a4306b74SAndreas Gohr        return $ok;
218cc7d0c94SBen Coburn    } else {
219cc7d0c94SBen Coburn        return false; //callback error
220cc7d0c94SBen Coburn    }
221cc7d0c94SBen Coburn}
222cc7d0c94SBen Coburn
223cc7d0c94SBen Coburn/**
2241bd6bbdeSPatrick Brown * Internal function to save contents to a file.
2251bd6bbdeSPatrick Brown *
2261bd6bbdeSPatrick Brown * @author  Andreas Gohr <andi@splitbrain.org>
2271bd6bbdeSPatrick Brown *
2281bd6bbdeSPatrick Brown * @param string $file filename path to file
2291bd6bbdeSPatrick Brown * @param string $content
2301bd6bbdeSPatrick Brown * @param bool   $append
2311bd6bbdeSPatrick Brown * @return bool true on success, otherwise false
2321bd6bbdeSPatrick Brown */
233*d868eb89SAndreas Gohrfunction _io_saveFile($file, $content, $append)
234*d868eb89SAndreas Gohr{
2351bd6bbdeSPatrick Brown    global $conf;
2361bd6bbdeSPatrick Brown    $mode = ($append) ? 'ab' : 'wb';
2371bd6bbdeSPatrick Brown    $fileexists = file_exists($file);
2381bd6bbdeSPatrick Brown
2391bd6bbdeSPatrick Brown    if (substr($file, -3) == '.gz') {
24013c37900SAndreas Gohr        if(!DOKU_HAS_GZIP) return false;
2411bd6bbdeSPatrick Brown        $fh = @gzopen($file, $mode.'9');
2421bd6bbdeSPatrick Brown        if(!$fh) return false;
2431bd6bbdeSPatrick Brown        gzwrite($fh, $content);
2441bd6bbdeSPatrick Brown        gzclose($fh);
2451bd6bbdeSPatrick Brown    } elseif (substr($file, -4) == '.bz2') {
24613c37900SAndreas Gohr        if(!DOKU_HAS_BZIP) return false;
2471bd6bbdeSPatrick Brown        if($append) {
2481bd6bbdeSPatrick Brown            $bzcontent = bzfile($file);
2491bd6bbdeSPatrick Brown            if($bzcontent === false) return false;
2501bd6bbdeSPatrick Brown            $content = $bzcontent.$content;
2511bd6bbdeSPatrick Brown        }
2521bd6bbdeSPatrick Brown        $fh = @bzopen($file, 'w');
2531bd6bbdeSPatrick Brown        if(!$fh) return false;
2541bd6bbdeSPatrick Brown        bzwrite($fh, $content);
2551bd6bbdeSPatrick Brown        bzclose($fh);
2561bd6bbdeSPatrick Brown    } else{
2571bd6bbdeSPatrick Brown        $fh = @fopen($file, $mode);
2581bd6bbdeSPatrick Brown        if(!$fh) return false;
2591bd6bbdeSPatrick Brown        fwrite($fh, $content);
2601bd6bbdeSPatrick Brown        fclose($fh);
2611bd6bbdeSPatrick Brown    }
2621bd6bbdeSPatrick Brown
26324870174SAndreas Gohr    if(!$fileexists && $conf['fperm']) chmod($file, $conf['fperm']);
2641bd6bbdeSPatrick Brown    return true;
2651bd6bbdeSPatrick Brown}
2661bd6bbdeSPatrick Brown
2671bd6bbdeSPatrick Brown/**
26815fae107Sandi * Saves $content to $file.
269f3f0262cSandi *
2701380fc45SAndreas Gohr * If the third parameter is set to true the given content
2711380fc45SAndreas Gohr * will be appended.
2721380fc45SAndreas Gohr *
27315fae107Sandi * Uses gzip if extension is .gz
274ff3ed99fSmarcel * and bz2 if extension is .bz2
27515fae107Sandi *
27615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
27742ea7f44SGerrit Uitslag *
27842ea7f44SGerrit Uitslag * @param string $file filename path to file
27942ea7f44SGerrit Uitslag * @param string $content
28042ea7f44SGerrit Uitslag * @param bool   $append
28142ea7f44SGerrit Uitslag * @return bool true on success, otherwise false
282f3f0262cSandi */
283*d868eb89SAndreas Gohrfunction io_saveFile($file, $content, $append = false)
284*d868eb89SAndreas Gohr{
285f3f0262cSandi    io_makeFileDir($file);
28690eb8392Sandi    io_lock($file);
2871bd6bbdeSPatrick Brown    if(!_io_saveFile($file, $content, $append)) {
288f3f0262cSandi        msg("Writing $file failed", -1);
289fb7125eeSAndreas Gohr        io_unlock($file);
290f3f0262cSandi        return false;
291f3f0262cSandi    }
29290eb8392Sandi    io_unlock($file);
293f3f0262cSandi    return true;
294f3f0262cSandi}
295f3f0262cSandi
296f3f0262cSandi/**
2971bd6bbdeSPatrick Brown * Replace one or more occurrences of a line in a file.
2981380fc45SAndreas Gohr *
299d93ba631SPatrick Brown * The default, when $maxlines is 0 is to delete all matching lines then append a single line.
300d93ba631SPatrick Brown * A regex that matches any part of the line will remove the entire line in this mode.
301d93ba631SPatrick Brown * Captures in $newline are not available.
3021bd6bbdeSPatrick Brown *
303d93ba631SPatrick Brown * Otherwise each line is matched and replaced individually, up to the first $maxlines lines
304d93ba631SPatrick Brown * or all lines if $maxlines is -1. If $regex is true then captures can be used in $newline.
305d93ba631SPatrick Brown *
306d93ba631SPatrick Brown * Be sure to include the trailing newline in $oldline when replacing entire lines.
307b158d625SSteven Danz *
308b158d625SSteven Danz * Uses gzip if extension is .gz
3091bd6bbdeSPatrick Brown * and bz2 if extension is .bz2
3108b06d178Schris *
311b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
3121bd6bbdeSPatrick Brown * @author Christopher Smith <chris@jalakai.co.uk>
3131bd6bbdeSPatrick Brown * @author Patrick Brown <ptbrown@whoopdedo.org>
31442ea7f44SGerrit Uitslag *
31542ea7f44SGerrit Uitslag * @param string $file     filename
3161bd6bbdeSPatrick Brown * @param string $oldline  exact linematch to remove
3171bd6bbdeSPatrick Brown * @param string $newline  new line to insert
31842ea7f44SGerrit Uitslag * @param bool   $regex    use regexp?
3191bd6bbdeSPatrick Brown * @param int    $maxlines number of occurrences of the line to replace
320b158d625SSteven Danz * @return bool true on success
321b158d625SSteven Danz */
322*d868eb89SAndreas Gohrfunction io_replaceInFile($file, $oldline, $newline, $regex = false, $maxlines = 0)
323*d868eb89SAndreas Gohr{
324dc4a4eb0SPatrick Brown    if ((string)$oldline === '') {
325dc4a4eb0SPatrick Brown        trigger_error('$oldline parameter cannot be empty in io_replaceInFile()', E_USER_WARNING);
326dc4a4eb0SPatrick Brown        return false;
327dc4a4eb0SPatrick Brown    }
328dc4a4eb0SPatrick Brown
32979e79377SAndreas Gohr    if (!file_exists($file)) return true;
3301380fc45SAndreas Gohr
331b158d625SSteven Danz    io_lock($file);
3321380fc45SAndreas Gohr
3331380fc45SAndreas Gohr    // load into array
334b158d625SSteven Danz    if (substr($file, -3) == '.gz') {
33513c37900SAndreas Gohr        if(!DOKU_HAS_GZIP) return false;
3361380fc45SAndreas Gohr        $lines = gzfile($file);
337cfb71e37SPatrick Brown    } elseif (substr($file, -4) == '.bz2') {
33813c37900SAndreas Gohr        if(!DOKU_HAS_BZIP) return false;
339cfb71e37SPatrick Brown        $lines = bzfile($file, true);
340b158d625SSteven Danz    } else{
3411380fc45SAndreas Gohr        $lines = file($file);
342b158d625SSteven Danz    }
343b158d625SSteven Danz
3449a734b7aSChristopher Smith    // make non-regexes into regexes
3453dfe7d64SChristopher Smith    $pattern = $regex ? $oldline : '/^'.preg_quote($oldline, '/').'$/';
3469a734b7aSChristopher Smith    $replace = $regex ? $newline : addcslashes($newline, '\$');
3479a734b7aSChristopher Smith
3489a734b7aSChristopher Smith    // remove matching lines
3496c000204SPatrick Brown    if ($maxlines > 0) {
3506c000204SPatrick Brown        $count = 0;
3519a734b7aSChristopher Smith        $matched = 0;
352a93ad676SAndreas Gohr        foreach($lines as $i => $line) {
353a93ad676SAndreas Gohr            if($count >= $maxlines) break;
3549a734b7aSChristopher Smith            // $matched will be set to 0|1 depending on whether pattern is matched and line replaced
3559a734b7aSChristopher Smith            $lines[$i] = preg_replace($pattern, $replace, $line, -1, $matched);
3569a734b7aSChristopher Smith            if ($matched) $count++;
3576c000204SPatrick Brown        }
358e12c5ac7SChristopher Smith    } elseif ($maxlines == 0) {
359e12c5ac7SChristopher Smith        $lines = preg_grep($pattern, $lines, PREG_GREP_INVERT);
360e12c5ac7SChristopher Smith        if ((string)$newline !== ''){
3611bd6bbdeSPatrick Brown            $lines[] = $newline;
3621bd6bbdeSPatrick Brown        }
363e12c5ac7SChristopher Smith    } else {
364e12c5ac7SChristopher Smith        $lines = preg_replace($pattern, $replace, $lines);
365e12c5ac7SChristopher Smith    }
3661bd6bbdeSPatrick Brown
3671380fc45SAndreas Gohr    if(count($lines)){
36824870174SAndreas Gohr        if(!_io_saveFile($file, implode('', $lines), false)) {
369b158d625SSteven Danz            msg("Removing content from $file failed", -1);
370fb7125eeSAndreas Gohr            io_unlock($file);
371b158d625SSteven Danz            return false;
372b158d625SSteven Danz        }
373b158d625SSteven Danz    }else{
374b158d625SSteven Danz        @unlink($file);
375b158d625SSteven Danz    }
376b158d625SSteven Danz
377b158d625SSteven Danz    io_unlock($file);
378b158d625SSteven Danz    return true;
379b158d625SSteven Danz}
380b158d625SSteven Danz
381b158d625SSteven Danz/**
3821bd6bbdeSPatrick Brown * Delete lines that match $badline from $file.
3831bd6bbdeSPatrick Brown *
3841bd6bbdeSPatrick Brown * Be sure to include the trailing newline in $badline
3851bd6bbdeSPatrick Brown *
3861bd6bbdeSPatrick Brown * @author Patrick Brown <ptbrown@whoopdedo.org>
3871bd6bbdeSPatrick Brown *
3881bd6bbdeSPatrick Brown * @param string $file    filename
3891bd6bbdeSPatrick Brown * @param string $badline exact linematch to remove
3901bd6bbdeSPatrick Brown * @param bool   $regex   use regexp?
3911bd6bbdeSPatrick Brown * @return bool true on success
3921bd6bbdeSPatrick Brown */
393*d868eb89SAndreas Gohrfunction io_deleteFromFile($file, $badline, $regex = false)
394*d868eb89SAndreas Gohr{
3951bd6bbdeSPatrick Brown    return io_replaceInFile($file, $badline, null, $regex, 0);
3961bd6bbdeSPatrick Brown}
3971bd6bbdeSPatrick Brown
3981bd6bbdeSPatrick Brown/**
39990eb8392Sandi * Tries to lock a file
40090eb8392Sandi *
40190eb8392Sandi * Locking is only done for io_savefile and uses directories
40290eb8392Sandi * inside $conf['lockdir']
40390eb8392Sandi *
40490eb8392Sandi * It waits maximal 3 seconds for the lock, after this time
40590eb8392Sandi * the lock is assumed to be stale and the function goes on
40690eb8392Sandi *
40790eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org>
40842ea7f44SGerrit Uitslag *
40942ea7f44SGerrit Uitslag * @param string $file filename
41090eb8392Sandi */
411*d868eb89SAndreas Gohrfunction io_lock($file)
412*d868eb89SAndreas Gohr{
41390eb8392Sandi    global $conf;
41490eb8392Sandi
41590eb8392Sandi    $lockDir = $conf['lockdir'].'/'.md5($file);
41690eb8392Sandi    @ignore_user_abort(1);
41790eb8392Sandi
41890eb8392Sandi    $timeStart = time();
41990eb8392Sandi    do {
42090eb8392Sandi        //waited longer than 3 seconds? -> stale lock
42190eb8392Sandi        if ((time() - $timeStart) > 3) break;
422bd539124SAndreas Gohr        $locked = @mkdir($lockDir);
42377b98903SAndreas Gohr        if($locked){
42423420346SDamien Regad            if($conf['dperm']) chmod($lockDir, $conf['dperm']);
42577b98903SAndreas Gohr            break;
42677b98903SAndreas Gohr        }
42777b98903SAndreas Gohr        usleep(50);
42890eb8392Sandi    } while ($locked === false);
42990eb8392Sandi}
43090eb8392Sandi
43190eb8392Sandi/**
43290eb8392Sandi * Unlocks a file
43390eb8392Sandi *
43490eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org>
43542ea7f44SGerrit Uitslag *
43642ea7f44SGerrit Uitslag * @param string $file filename
43790eb8392Sandi */
438*d868eb89SAndreas Gohrfunction io_unlock($file)
439*d868eb89SAndreas Gohr{
44090eb8392Sandi    global $conf;
44190eb8392Sandi
44290eb8392Sandi    $lockDir = $conf['lockdir'].'/'.md5($file);
44390eb8392Sandi    @rmdir($lockDir);
44490eb8392Sandi    @ignore_user_abort(0);
44590eb8392Sandi}
44690eb8392Sandi
44790eb8392Sandi/**
448cc7d0c94SBen Coburn * Create missing namespace directories and send the IO_NAMESPACE_CREATED events
449cc7d0c94SBen Coburn * in the order of directory creation. (Parent directories first.)
450cc7d0c94SBen Coburn *
451cc7d0c94SBen Coburn * Event data:
452cc7d0c94SBen Coburn * $data[0]    ns: The colon separated namespace path minus the trailing page name.
453cc7d0c94SBen Coburn * $data[1]    ns_type: 'pages' or 'media' namespace tree.
454cc7d0c94SBen Coburn *
455cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
45642ea7f44SGerrit Uitslag *
45742ea7f44SGerrit Uitslag * @param string $id page id
45842ea7f44SGerrit Uitslag * @param string $ns_type 'pages' or 'media'
459cc7d0c94SBen Coburn */
460*d868eb89SAndreas Gohrfunction io_createNamespace($id, $ns_type = 'pages')
461*d868eb89SAndreas Gohr{
462cc7d0c94SBen Coburn    // verify ns_type
46324870174SAndreas Gohr    $types = ['pages'=>'wikiFN', 'media'=>'mediaFN'];
464cc7d0c94SBen Coburn    if (!isset($types[$ns_type])) {
465cc7d0c94SBen Coburn        trigger_error('Bad $ns_type parameter for io_createNamespace().');
466cc7d0c94SBen Coburn        return;
467cc7d0c94SBen Coburn    }
468cc7d0c94SBen Coburn    // make event list
46924870174SAndreas Gohr    $missing = [];
470cc7d0c94SBen Coburn    $ns_stack = explode(':', $id);
471cc7d0c94SBen Coburn    $ns = $id;
472cc7d0c94SBen Coburn    $tmp = dirname( $file = call_user_func($types[$ns_type], $ns) );
47379e79377SAndreas Gohr    while (!@is_dir($tmp) && !(file_exists($tmp) && !is_dir($tmp))) {
474cc7d0c94SBen Coburn        array_pop($ns_stack);
475cc7d0c94SBen Coburn        $ns = implode(':', $ns_stack);
476cc7d0c94SBen Coburn        if (strlen($ns)==0) { break; }
477cc7d0c94SBen Coburn        $missing[] = $ns;
478cc7d0c94SBen Coburn        $tmp = dirname(call_user_func($types[$ns_type], $ns));
479cc7d0c94SBen Coburn    }
480cc7d0c94SBen Coburn    // make directories
481cc7d0c94SBen Coburn    io_makeFileDir($file);
482cc7d0c94SBen Coburn    // send the events
483cc7d0c94SBen Coburn    $missing = array_reverse($missing); // inside out
484cc7d0c94SBen Coburn    foreach ($missing as $ns) {
48524870174SAndreas Gohr        $data = [$ns, $ns_type];
486cbb44eabSAndreas Gohr        Event::createAndTrigger('IO_NAMESPACE_CREATED', $data);
487cc7d0c94SBen Coburn    }
488cc7d0c94SBen Coburn}
489cc7d0c94SBen Coburn
490cc7d0c94SBen Coburn/**
491f3f0262cSandi * Create the directory needed for the given file
49215fae107Sandi *
49315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
49442ea7f44SGerrit Uitslag *
49542ea7f44SGerrit Uitslag * @param string $file file name
496f3f0262cSandi */
497*d868eb89SAndreas Gohrfunction io_makeFileDir($file)
498*d868eb89SAndreas Gohr{
499f3f0262cSandi    $dir = dirname($file);
5000d8850c4SAndreas Gohr    if(!@is_dir($dir)){
501f3f0262cSandi        io_mkdir_p($dir) || msg("Creating directory $dir failed", -1);
502f3f0262cSandi    }
503f3f0262cSandi}
504f3f0262cSandi
505f3f0262cSandi/**
506f3f0262cSandi * Creates a directory hierachy.
507f3f0262cSandi *
50859752844SAnders Sandblad * @link    http://php.net/manual/en/function.mkdir.php
509f3f0262cSandi * @author  <saint@corenova.com>
5103dc3a5f1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
51142ea7f44SGerrit Uitslag *
51242ea7f44SGerrit Uitslag * @param string $target filename
51342ea7f44SGerrit Uitslag * @return bool|int|string
514f3f0262cSandi */
515*d868eb89SAndreas Gohrfunction io_mkdir_p($target)
516*d868eb89SAndreas Gohr{
5173dc3a5f1Sandi    global $conf;
5180d8850c4SAndreas Gohr    if (@is_dir($target)||empty($target)) return 1; // best case check first
51979e79377SAndreas Gohr    if (file_exists($target) && !is_dir($target)) return 0;
5203dc3a5f1Sandi    //recursion
5213dc3a5f1Sandi    if (io_mkdir_p(substr($target, 0, strrpos($target, '/')))){
522bd539124SAndreas Gohr        $ret = @mkdir($target); // crawl back up & create dir tree
523443e135dSChristopher Smith        if($ret && !empty($conf['dperm'])) chmod($target, $conf['dperm']);
52444881d27STroels Liebe Bentsen        return $ret;
5253dc3a5f1Sandi    }
526f3f0262cSandi    return 0;
527f3f0262cSandi}
528f3f0262cSandi
529f3f0262cSandi/**
5304d47e8e3SAndreas Gohr * Recursively delete a directory
5314d47e8e3SAndreas Gohr *
5324d47e8e3SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5334d47e8e3SAndreas Gohr * @param string $path
5344d47e8e3SAndreas Gohr * @param bool   $removefiles defaults to false which will delete empty directories only
5354d47e8e3SAndreas Gohr * @return bool
5364d47e8e3SAndreas Gohr */
537*d868eb89SAndreas Gohrfunction io_rmdir($path, $removefiles = false)
538*d868eb89SAndreas Gohr{
5394d47e8e3SAndreas Gohr    if(!is_string($path) || $path == "") return false;
540d8cf4dd4SAndreas Gohr    if(!file_exists($path)) return true; // it's already gone or was never there, count as success
5414d47e8e3SAndreas Gohr
5424d47e8e3SAndreas Gohr    if (is_dir($path) && !is_link($path)) {
54324870174SAndreas Gohr        $dirs  = [];
54424870174SAndreas Gohr        $files = [];
5454d47e8e3SAndreas Gohr        if(!$dh = @opendir($path)) return false;
5468426a3eeSAndreas Gohr        while(false !== ($f = readdir($dh))) {
5474d47e8e3SAndreas Gohr            if($f == '..' || $f == '.') continue;
5484d47e8e3SAndreas Gohr
5494d47e8e3SAndreas Gohr            // collect dirs and files first
5504d47e8e3SAndreas Gohr            if (is_dir("$path/$f") && !is_link("$path/$f")) {
5514d47e8e3SAndreas Gohr                $dirs[] = "$path/$f";
5524d47e8e3SAndreas Gohr            } elseif ($removefiles) {
5534d47e8e3SAndreas Gohr                $files[] = "$path/$f";
5544d47e8e3SAndreas Gohr            } else {
5554d47e8e3SAndreas Gohr                return false; // abort when non empty
5564d47e8e3SAndreas Gohr            }
5574d47e8e3SAndreas Gohr
5584d47e8e3SAndreas Gohr        }
5594d47e8e3SAndreas Gohr        closedir($dh);
5604d47e8e3SAndreas Gohr        // now traverse into  directories first
5614d47e8e3SAndreas Gohr        foreach($dirs as $dir) {
5624d47e8e3SAndreas Gohr            if(!io_rmdir($dir, $removefiles)) return false; // abort on any error
5634d47e8e3SAndreas Gohr        }
5644d47e8e3SAndreas Gohr        // now delete files
5654d47e8e3SAndreas Gohr        foreach($files as $file) {
5664d47e8e3SAndreas Gohr            if(!@unlink($file)) return false; //abort on any error
5674d47e8e3SAndreas Gohr        }
5684d47e8e3SAndreas Gohr        // remove self
5694d47e8e3SAndreas Gohr        return @rmdir($path);
5704d47e8e3SAndreas Gohr    } elseif ($removefiles) {
5714d47e8e3SAndreas Gohr        return @unlink($path);
5724d47e8e3SAndreas Gohr    }
5734d47e8e3SAndreas Gohr    return false;
5744d47e8e3SAndreas Gohr}
5754d47e8e3SAndreas Gohr
5764d47e8e3SAndreas Gohr/**
577de862555SMichael Klier * Creates a unique temporary directory and returns
578de862555SMichael Klier * its path.
579de862555SMichael Klier *
580de862555SMichael Klier * @author Michael Klier <chi@chimeric.de>
58142ea7f44SGerrit Uitslag *
58242ea7f44SGerrit Uitslag * @return false|string path to new directory or false
583de862555SMichael Klier */
584*d868eb89SAndreas Gohrfunction io_mktmpdir()
585*d868eb89SAndreas Gohr{
586de862555SMichael Klier    global $conf;
587de862555SMichael Klier
588da1e1077SChris Smith    $base = $conf['tmpdir'];
58924870174SAndreas Gohr    $dir  = md5(uniqid(random_int(0, mt_getrandmax()), true));
590287f35bdSAndreas Gohr    $tmpdir = $base.'/'.$dir;
591de862555SMichael Klier
592de862555SMichael Klier    if(io_mkdir_p($tmpdir)) {
593de862555SMichael Klier        return($tmpdir);
594de862555SMichael Klier    } else {
595de862555SMichael Klier        return false;
596de862555SMichael Klier    }
597de862555SMichael Klier}
598de862555SMichael Klier
599de862555SMichael Klier/**
60073ccfcb9Schris * downloads a file from the net and saves it
60173ccfcb9Schris *
60273ccfcb9Schris * if $useAttachment is false,
60373ccfcb9Schris * - $file is the full filename to save the file, incl. path
60473ccfcb9Schris * - if successful will return true, false otherwise
605db959ae3SAndreas Gohr *
60673ccfcb9Schris * if $useAttachment is true,
60773ccfcb9Schris * - $file is the directory where the file should be saved
60873ccfcb9Schris * - if successful will return the name used for the saved file, false otherwise
609b625487dSandi *
610b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
61173ccfcb9Schris * @author Chris Smith <chris@jalakai.co.uk>
61242ea7f44SGerrit Uitslag *
61342ea7f44SGerrit Uitslag * @param string $url           url to download
61442ea7f44SGerrit Uitslag * @param string $file          path to file or directory where to save
61564159a61SAndreas Gohr * @param bool   $useAttachment true: try to use name of download, uses otherwise $defaultName
61664159a61SAndreas Gohr *                              false: uses $file as path to file
61742ea7f44SGerrit Uitslag * @param string $defaultName   fallback for if using $useAttachment
61842ea7f44SGerrit Uitslag * @param int    $maxSize       maximum file size
61942ea7f44SGerrit Uitslag * @return bool|string          if failed false, otherwise true or the name of the file in the given dir
620b625487dSandi */
621*d868eb89SAndreas Gohrfunction io_download($url, $file, $useAttachment = false, $defaultName = '', $maxSize = 2_097_152)
622*d868eb89SAndreas Gohr{
623ac9115b0STroels Liebe Bentsen    global $conf;
6249b307a83SAndreas Gohr    $http = new DokuHTTPClient();
625847b8298SAndreas Gohr    $http->max_bodysize = $maxSize;
6269b307a83SAndreas Gohr    $http->timeout = 25; //max. 25 sec
627a5951419SAndreas Gohr    $http->keep_alive = false; // we do single ops here, no need for keep-alive
6289b307a83SAndreas Gohr
6299b307a83SAndreas Gohr    $data = $http->get($url);
6309b307a83SAndreas Gohr    if(!$data) return false;
6319b307a83SAndreas Gohr
63273ccfcb9Schris    $name = '';
633cd2f903bSMichael Hamann    if ($useAttachment) {
63473ccfcb9Schris        if (isset($http->resp_headers['content-disposition'])) {
63573ccfcb9Schris            $content_disposition = $http->resp_headers['content-disposition'];
63624870174SAndreas Gohr            $match=[];
63773ccfcb9Schris            if (is_string($content_disposition) &&
638ce070a9fSchris                    preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) {
63973ccfcb9Schris
64024870174SAndreas Gohr                $name = PhpString::basename($match[1]);
64173ccfcb9Schris            }
64273ccfcb9Schris
64373ccfcb9Schris        }
64473ccfcb9Schris
64573ccfcb9Schris        if (!$name) {
64673ccfcb9Schris            if (!$defaultName) return false;
64773ccfcb9Schris            $name = $defaultName;
64873ccfcb9Schris        }
64973ccfcb9Schris
65024870174SAndreas Gohr        $file .= $name;
65173ccfcb9Schris    }
65273ccfcb9Schris
65379e79377SAndreas Gohr    $fileexists = file_exists($file);
6549b307a83SAndreas Gohr    $fp = @fopen($file, "w");
655b625487dSandi    if(!$fp) return false;
6569b307a83SAndreas Gohr    fwrite($fp, $data);
657b625487dSandi    fclose($fp);
65824870174SAndreas Gohr    if(!$fileexists && $conf['fperm']) chmod($file, $conf['fperm']);
65973ccfcb9Schris    if ($useAttachment) return $name;
660b625487dSandi    return true;
661b625487dSandi}
662b625487dSandi
663b625487dSandi/**
664ac9115b0STroels Liebe Bentsen * Windows compatible rename
665bf5e5a5bSAndreas Gohr *
666bf5e5a5bSAndreas Gohr * rename() can not overwrite existing files on Windows
667bf5e5a5bSAndreas Gohr * this function will use copy/unlink instead
66842ea7f44SGerrit Uitslag *
66942ea7f44SGerrit Uitslag * @param string $from
67042ea7f44SGerrit Uitslag * @param string $to
67142ea7f44SGerrit Uitslag * @return bool succes or fail
672bf5e5a5bSAndreas Gohr */
673*d868eb89SAndreas Gohrfunction io_rename($from, $to)
674*d868eb89SAndreas Gohr{
675ac9115b0STroels Liebe Bentsen    global $conf;
676bf5e5a5bSAndreas Gohr    if(!@rename($from, $to)){
677bf5e5a5bSAndreas Gohr        if(@copy($from, $to)){
6788e0b019fSAndreas Gohr            if($conf['fperm']) chmod($to, $conf['fperm']);
679bf5e5a5bSAndreas Gohr            @unlink($from);
680bf5e5a5bSAndreas Gohr            return true;
681bf5e5a5bSAndreas Gohr        }
682bf5e5a5bSAndreas Gohr        return false;
683bf5e5a5bSAndreas Gohr    }
684bf5e5a5bSAndreas Gohr    return true;
685bf5e5a5bSAndreas Gohr}
686bf5e5a5bSAndreas Gohr
687420edfd6STom N Harris/**
688420edfd6STom N Harris * Runs an external command with input and output pipes.
689420edfd6STom N Harris * Returns the exit code from the process.
690420edfd6STom N Harris *
691420edfd6STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org>
69242ea7f44SGerrit Uitslag *
69342ea7f44SGerrit Uitslag * @param string $cmd
69442ea7f44SGerrit Uitslag * @param string $input  input pipe
69542ea7f44SGerrit Uitslag * @param string $output output pipe
69642ea7f44SGerrit Uitslag * @return int exit code from process
697420edfd6STom N Harris */
698*d868eb89SAndreas Gohrfunction io_exec($cmd, $input, &$output)
699*d868eb89SAndreas Gohr{
70024870174SAndreas Gohr    $descspec = [
70124870174SAndreas Gohr        0=>["pipe", "r"],
70224870174SAndreas Gohr        1=>["pipe", "w"],
70324870174SAndreas Gohr        2=>["pipe", "w"]
70424870174SAndreas Gohr    ];
7056c528220STom N Harris    $ph = proc_open($cmd, $descspec, $pipes);
7066c528220STom N Harris    if(!$ph) return -1;
7076c528220STom N Harris    fclose($pipes[2]); // ignore stderr
7086c528220STom N Harris    fwrite($pipes[0], $input);
7096c528220STom N Harris    fclose($pipes[0]);
7106c528220STom N Harris    $output = stream_get_contents($pipes[1]);
7116c528220STom N Harris    fclose($pipes[1]);
7126c528220STom N Harris    return proc_close($ph);
713f3f0262cSandi}
714f3f0262cSandi
7157421c3ccSAndreas Gohr/**
7167421c3ccSAndreas Gohr * Search a file for matching lines
7177421c3ccSAndreas Gohr *
7187421c3ccSAndreas Gohr * This is probably not faster than file()+preg_grep() but less
7197421c3ccSAndreas Gohr * memory intensive because not the whole file needs to be loaded
7207421c3ccSAndreas Gohr * at once.
7217421c3ccSAndreas Gohr *
7227421c3ccSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
7237421c3ccSAndreas Gohr * @param  string $file    The file to search
7247421c3ccSAndreas Gohr * @param  string $pattern PCRE pattern
7257421c3ccSAndreas Gohr * @param  int    $max     How many lines to return (0 for all)
726cd2f903bSMichael Hamann * @param  bool   $backref When true returns array with backreferences instead of lines
727cd2f903bSMichael Hamann * @return array matching lines or backref, false on error
7287421c3ccSAndreas Gohr */
729*d868eb89SAndreas Gohrfunction io_grep($file, $pattern, $max = 0, $backref = false)
730*d868eb89SAndreas Gohr{
7317421c3ccSAndreas Gohr    $fh = @fopen($file, 'r');
7327421c3ccSAndreas Gohr    if(!$fh) return false;
73324870174SAndreas Gohr    $matches = [];
7347421c3ccSAndreas Gohr
7357421c3ccSAndreas Gohr    $cnt  = 0;
7367421c3ccSAndreas Gohr    $line = '';
7377421c3ccSAndreas Gohr    while (!feof($fh)) {
7387421c3ccSAndreas Gohr        $line .= fgets($fh, 4096);  // read full line
7397421c3ccSAndreas Gohr        if(substr($line, -1) != "\n") continue;
7407421c3ccSAndreas Gohr
7417421c3ccSAndreas Gohr        // check if line matches
7427421c3ccSAndreas Gohr        if(preg_match($pattern, $line, $match)){
7437421c3ccSAndreas Gohr            if($backref){
7447421c3ccSAndreas Gohr                $matches[] = $match;
7457421c3ccSAndreas Gohr            }else{
7467421c3ccSAndreas Gohr                $matches[] = $line;
7477421c3ccSAndreas Gohr            }
7487421c3ccSAndreas Gohr            $cnt++;
7497421c3ccSAndreas Gohr        }
7507421c3ccSAndreas Gohr        if($max && $max == $cnt) break;
7517421c3ccSAndreas Gohr        $line = '';
7527421c3ccSAndreas Gohr    }
7537421c3ccSAndreas Gohr    fclose($fh);
7547421c3ccSAndreas Gohr    return $matches;
7557421c3ccSAndreas Gohr}
7567421c3ccSAndreas Gohr
757f549be3dSGerrit Uitslag
758f549be3dSGerrit Uitslag/**
759f549be3dSGerrit Uitslag * Get size of contents of a file, for a compressed file the uncompressed size
760f549be3dSGerrit Uitslag * Warning: reading uncompressed size of content of bz-files requires uncompressing
761f549be3dSGerrit Uitslag *
762f549be3dSGerrit Uitslag * @author  Gerrit Uitslag <klapinklapin@gmail.com>
763f549be3dSGerrit Uitslag *
764f549be3dSGerrit Uitslag * @param string $file filename path to file
765f549be3dSGerrit Uitslag * @return int size of file
766f549be3dSGerrit Uitslag */
767*d868eb89SAndreas Gohrfunction io_getSizeFile($file)
768*d868eb89SAndreas Gohr{
769f549be3dSGerrit Uitslag    if (!file_exists($file)) return 0;
770f549be3dSGerrit Uitslag
771f549be3dSGerrit Uitslag    if (substr($file, -3) == '.gz') {
772f549be3dSGerrit Uitslag        $fp = @fopen($file, "rb");
773f549be3dSGerrit Uitslag        if($fp === false) return 0;
774f549be3dSGerrit Uitslag        fseek($fp, -4, SEEK_END);
775f549be3dSGerrit Uitslag        $buffer = fread($fp, 4);
776f549be3dSGerrit Uitslag        fclose($fp);
777f549be3dSGerrit Uitslag        $array = unpack("V", $buffer);
778f549be3dSGerrit Uitslag        $uncompressedsize = end($array);
779f549be3dSGerrit Uitslag    } elseif (substr($file, -4) == '.bz2') {
780f549be3dSGerrit Uitslag        if(!DOKU_HAS_BZIP) return 0;
781f549be3dSGerrit Uitslag        $bz = bzopen($file, "r");
782f549be3dSGerrit Uitslag        if($bz === false) return 0;
783f549be3dSGerrit Uitslag        $uncompressedsize = 0;
784f549be3dSGerrit Uitslag        while (!feof($bz)) {
785f549be3dSGerrit Uitslag            //8192 seems to be the maximum buffersize?
786f549be3dSGerrit Uitslag            $buffer = bzread($bz, 8192);
787f549be3dSGerrit Uitslag            if(($buffer === false) || (bzerrno($bz) !== 0)) {
788f549be3dSGerrit Uitslag                return 0;
789f549be3dSGerrit Uitslag            }
790f549be3dSGerrit Uitslag            $uncompressedsize += strlen($buffer);
791f549be3dSGerrit Uitslag        }
792f549be3dSGerrit Uitslag    } else{
793f549be3dSGerrit Uitslag        $uncompressedsize = filesize($file);
794f549be3dSGerrit Uitslag    }
795f549be3dSGerrit Uitslag
796f549be3dSGerrit Uitslag    return $uncompressedsize;
797f549be3dSGerrit Uitslag }
798