xref: /dokuwiki/inc/io.php (revision 198564ab361cd134dce0bcc8d07863157d1ae7a2)
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 */
815fae107Sandi
9*198564abSMichael Großeuse dokuwiki\HTTPClient\DokuHTTPClient;
10*198564abSMichael Große
11f3f0262cSandi/**
1253d6ccfeSandi * Removes empty directories
1353d6ccfeSandi *
14cc7d0c94SBen Coburn * Sends IO_NAMESPACE_DELETED events for 'pages' and 'media' namespaces.
15cc7d0c94SBen Coburn * Event data:
16cc7d0c94SBen Coburn * $data[0]    ns: The colon separated namespace path minus the trailing page name.
17cc7d0c94SBen Coburn * $data[1]    ns_type: 'pages' or 'media' namespace tree.
18cc7d0c94SBen Coburn *
19d186898bSAndreas Gohr * @param string $id      - a pageid, the namespace of that id will be tried to deleted
20cd2f903bSMichael Hamann * @param string $basedir - the config name of the type to delete (datadir or mediadir usally)
21cd2f903bSMichael Hamann * @return bool - true if at least one namespace was deleted
2242ea7f44SGerrit Uitslag *
2353d6ccfeSandi * @author  Andreas Gohr <andi@splitbrain.org>
24cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
2553d6ccfeSandi */
26755f1e03SAndreas Gohrfunction io_sweepNS($id,$basedir='datadir'){
2753d6ccfeSandi    global $conf;
28cc7d0c94SBen Coburn    $types = array ('datadir'=>'pages', 'mediadir'=>'media');
29cc7d0c94SBen Coburn    $ns_type = (isset($types[$basedir])?$types[$basedir]:false);
3053d6ccfeSandi
31d186898bSAndreas Gohr    $delone = false;
32d186898bSAndreas Gohr
3353d6ccfeSandi    //scan all namespaces
3453d6ccfeSandi    while(($id = getNS($id)) !== false){
35755f1e03SAndreas Gohr        $dir = $conf[$basedir].'/'.utf8_encodeFN(str_replace(':','/',$id));
3653d6ccfeSandi
3753d6ccfeSandi        //try to delete dir else return
38cc7d0c94SBen Coburn        if(@rmdir($dir)) {
39cc7d0c94SBen Coburn            if ($ns_type!==false) {
40cc7d0c94SBen Coburn                $data = array($id, $ns_type);
41d186898bSAndreas Gohr                $delone = true; // we deleted at least one dir
42cc7d0c94SBen Coburn                trigger_event('IO_NAMESPACE_DELETED', $data);
43cc7d0c94SBen Coburn            }
44d186898bSAndreas Gohr        } else { return $delone; }
45cc7d0c94SBen Coburn    }
46d186898bSAndreas Gohr    return $delone;
47cc7d0c94SBen Coburn}
48cc7d0c94SBen Coburn
49cc7d0c94SBen Coburn/**
50cc7d0c94SBen Coburn * Used to read in a DokuWiki page from file, and send IO_WIKIPAGE_READ events.
51cc7d0c94SBen Coburn *
52cc7d0c94SBen Coburn * Generates the action event which delegates to io_readFile().
53cc7d0c94SBen Coburn * Action plugins are allowed to modify the page content in transit.
54cc7d0c94SBen Coburn * The file path should not be changed.
55cc7d0c94SBen Coburn *
56cc7d0c94SBen Coburn * Event data:
57cc7d0c94SBen Coburn * $data[0]    The raw arguments for io_readFile as an array.
58cc7d0c94SBen Coburn * $data[1]    ns: The colon separated namespace path minus the trailing page name. (false if root ns)
59cc7d0c94SBen Coburn * $data[2]    page_name: The wiki page name.
60cc7d0c94SBen Coburn * $data[3]    rev: The page revision, false for current wiki pages.
61cc7d0c94SBen Coburn *
62cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
6342ea7f44SGerrit Uitslag *
6442ea7f44SGerrit Uitslag * @param string   $file filename
6542ea7f44SGerrit Uitslag * @param string   $id page id
6642ea7f44SGerrit Uitslag * @param bool|int $rev revision timestamp
6742ea7f44SGerrit Uitslag * @return string
68cc7d0c94SBen Coburn */
69cc7d0c94SBen Coburnfunction io_readWikiPage($file, $id, $rev=false) {
70cc7d0c94SBen Coburn    if (empty($rev)) { $rev = false; }
717651b376SAndreas Gohr    $data = array(array($file, true), getNS($id), noNS($id), $rev);
72cc7d0c94SBen Coburn    return trigger_event('IO_WIKIPAGE_READ', $data, '_io_readWikiPage_action', false);
73cc7d0c94SBen Coburn}
74cc7d0c94SBen Coburn
75cc7d0c94SBen Coburn/**
76cc7d0c94SBen Coburn * Callback adapter for io_readFile().
7742ea7f44SGerrit Uitslag *
78cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
7942ea7f44SGerrit Uitslag *
8042ea7f44SGerrit Uitslag * @param array $data event data
8142ea7f44SGerrit Uitslag * @return string
82cc7d0c94SBen Coburn */
83cc7d0c94SBen Coburnfunction _io_readWikiPage_action($data) {
84cc7d0c94SBen Coburn    if (is_array($data) && is_array($data[0]) && count($data[0])===2) {
85cc7d0c94SBen Coburn        return call_user_func_array('io_readFile', $data[0]);
86cc7d0c94SBen Coburn    } else {
87cc7d0c94SBen Coburn        return ''; //callback error
8853d6ccfeSandi    }
8953d6ccfeSandi}
9053d6ccfeSandi
9153d6ccfeSandi/**
9215fae107Sandi * Returns content of $file as cleaned string.
9315fae107Sandi *
9415fae107Sandi * Uses gzip if extension is .gz
9515fae107Sandi *
96ee4c4a1bSAndreas Gohr * If you want to use the returned value in unserialize
97ee4c4a1bSAndreas Gohr * be sure to set $clean to false!
98ee4c4a1bSAndreas Gohr *
9915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
10042ea7f44SGerrit Uitslag *
10142ea7f44SGerrit Uitslag * @param string $file  filename
10242ea7f44SGerrit Uitslag * @param bool   $clean
103d387bf5eSAndreas Gohr * @return string|bool the file contents or false on error
104f3f0262cSandi */
105e34c0709SAndreas Gohrfunction io_readFile($file,$clean=true){
106f3f0262cSandi    $ret = '';
10779e79377SAndreas Gohr    if(file_exists($file)){
108f3f0262cSandi        if(substr($file,-3) == '.gz'){
10913c37900SAndreas Gohr            if(!DOKU_HAS_GZIP) return false;
1102ad45addSAndreas Gohr            $ret = gzfile($file);
1112ad45addSAndreas Gohr            if(is_array($ret)) $ret = join('', $ret);
112ff3ed99fSmarcel        }else if(substr($file,-4) == '.bz2'){
11313c37900SAndreas Gohr            if(!DOKU_HAS_BZIP) return false;
114ff3ed99fSmarcel            $ret = bzfile($file);
115f3f0262cSandi        }else{
11643078d10SAndreas Gohr            $ret = file_get_contents($file);
117f3f0262cSandi        }
118f3f0262cSandi    }
1192ad45addSAndreas Gohr    if($ret === null) return false;
120d387bf5eSAndreas Gohr    if($ret !== false && $clean){
121f3f0262cSandi        return cleanText($ret);
122e34c0709SAndreas Gohr    }else{
123e34c0709SAndreas Gohr        return $ret;
124e34c0709SAndreas Gohr    }
125f3f0262cSandi}
126ff3ed99fSmarcel/**
127ff3ed99fSmarcel * Returns the content of a .bz2 compressed file as string
12842ea7f44SGerrit Uitslag *
129ff3ed99fSmarcel * @author marcel senf <marcel@rucksackreinigung.de>
130d387bf5eSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
13142ea7f44SGerrit Uitslag *
13242ea7f44SGerrit Uitslag * @param string $file filename
133cfb71e37SPatrick Brown * @param bool   $array return array of lines
134cfb71e37SPatrick Brown * @return string|array|bool content or false on error
135ff3ed99fSmarcel */
136cfb71e37SPatrick Brownfunction bzfile($file, $array=false) {
137ff3ed99fSmarcel    $bz = bzopen($file,"r");
138d387bf5eSAndreas Gohr    if($bz === false) return false;
139d387bf5eSAndreas Gohr
140cfb71e37SPatrick Brown    if($array) $lines = array();
141cd2f903bSMichael Hamann    $str = '';
142ff3ed99fSmarcel    while (!feof($bz)) {
143ff3ed99fSmarcel        //8192 seems to be the maximum buffersize?
144d387bf5eSAndreas Gohr        $buffer = bzread($bz,8192);
145d387bf5eSAndreas Gohr        if(($buffer === false) || (bzerrno($bz) !== 0)) {
146d387bf5eSAndreas Gohr            return false;
147d387bf5eSAndreas Gohr        }
148d387bf5eSAndreas Gohr        $str = $str . $buffer;
149cfb71e37SPatrick Brown        if($array) {
150cfb71e37SPatrick Brown            $pos = strpos($str, "\n");
151cfb71e37SPatrick Brown            while($pos !== false) {
152cfb71e37SPatrick Brown                $lines[] = substr($str, 0, $pos+1);
153cfb71e37SPatrick Brown                $str = substr($str, $pos+1);
154cfb71e37SPatrick Brown                $pos = strpos($str, "\n");
155cfb71e37SPatrick Brown            }
156cfb71e37SPatrick Brown        }
157ff3ed99fSmarcel    }
158ff3ed99fSmarcel    bzclose($bz);
159cfb71e37SPatrick Brown    if($array) {
160cfb71e37SPatrick Brown        if($str !== '') $lines[] = $str;
161cfb71e37SPatrick Brown        return $lines;
162cfb71e37SPatrick Brown    }
163ff3ed99fSmarcel    return $str;
164ff3ed99fSmarcel}
165ff3ed99fSmarcel
166f3f0262cSandi/**
167cc7d0c94SBen Coburn * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events.
168cc7d0c94SBen Coburn *
169cc7d0c94SBen Coburn * This generates an action event and delegates to io_saveFile().
170cc7d0c94SBen Coburn * Action plugins are allowed to modify the page content in transit.
171cc7d0c94SBen Coburn * The file path should not be changed.
172cc7d0c94SBen Coburn * (The append parameter is set to false.)
173cc7d0c94SBen Coburn *
174cc7d0c94SBen Coburn * Event data:
175cc7d0c94SBen Coburn * $data[0]    The raw arguments for io_saveFile as an array.
176cc7d0c94SBen Coburn * $data[1]    ns: The colon separated namespace path minus the trailing page name. (false if root ns)
177cc7d0c94SBen Coburn * $data[2]    page_name: The wiki page name.
178cc7d0c94SBen Coburn * $data[3]    rev: The page revision, false for current wiki pages.
179cc7d0c94SBen Coburn *
180cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
18142ea7f44SGerrit Uitslag *
18242ea7f44SGerrit Uitslag * @param string $file      filename
18342ea7f44SGerrit Uitslag * @param string $content
18442ea7f44SGerrit Uitslag * @param string $id        page id
18542ea7f44SGerrit Uitslag * @param int|bool $rev timestamp of revision
18642ea7f44SGerrit Uitslag * @return bool
187cc7d0c94SBen Coburn */
188cc7d0c94SBen Coburnfunction io_writeWikiPage($file, $content, $id, $rev=false) {
189cc7d0c94SBen Coburn    if (empty($rev)) { $rev = false; }
190cc7d0c94SBen Coburn    if ($rev===false) { io_createNamespace($id); } // create namespaces as needed
191cc7d0c94SBen Coburn    $data = array(array($file, $content, false), getNS($id), noNS($id), $rev);
192cc7d0c94SBen Coburn    return trigger_event('IO_WIKIPAGE_WRITE', $data, '_io_writeWikiPage_action', false);
193cc7d0c94SBen Coburn}
194cc7d0c94SBen Coburn
195cc7d0c94SBen Coburn/**
196cc7d0c94SBen Coburn * Callback adapter for io_saveFile().
197cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
19842ea7f44SGerrit Uitslag *
19942ea7f44SGerrit Uitslag * @param array $data event data
20042ea7f44SGerrit Uitslag * @return bool
201cc7d0c94SBen Coburn */
202cc7d0c94SBen Coburnfunction _io_writeWikiPage_action($data) {
203cc7d0c94SBen Coburn    if (is_array($data) && is_array($data[0]) && count($data[0])===3) {
204a4306b74SAndreas Gohr        $ok = call_user_func_array('io_saveFile', $data[0]);
205a4306b74SAndreas Gohr        // for attic files make sure the file has the mtime of the revision
206a4306b74SAndreas Gohr        if($ok && is_int($data[3]) && $data[3] > 0) {
207a4306b74SAndreas Gohr            @touch($data[0][0], $data[3]);
208a4306b74SAndreas Gohr        }
209a4306b74SAndreas Gohr        return $ok;
210cc7d0c94SBen Coburn    } else {
211cc7d0c94SBen Coburn        return false; //callback error
212cc7d0c94SBen Coburn    }
213cc7d0c94SBen Coburn}
214cc7d0c94SBen Coburn
215cc7d0c94SBen Coburn/**
2161bd6bbdeSPatrick Brown * Internal function to save contents to a file.
2171bd6bbdeSPatrick Brown *
2181bd6bbdeSPatrick Brown * @author  Andreas Gohr <andi@splitbrain.org>
2191bd6bbdeSPatrick Brown *
2201bd6bbdeSPatrick Brown * @param string $file filename path to file
2211bd6bbdeSPatrick Brown * @param string $content
2221bd6bbdeSPatrick Brown * @param bool   $append
2231bd6bbdeSPatrick Brown * @return bool true on success, otherwise false
2241bd6bbdeSPatrick Brown */
2251bd6bbdeSPatrick Brownfunction _io_saveFile($file, $content, $append) {
2261bd6bbdeSPatrick Brown    global $conf;
2271bd6bbdeSPatrick Brown    $mode = ($append) ? 'ab' : 'wb';
2281bd6bbdeSPatrick Brown    $fileexists = file_exists($file);
2291bd6bbdeSPatrick Brown
2301bd6bbdeSPatrick Brown    if(substr($file,-3) == '.gz'){
23113c37900SAndreas Gohr        if(!DOKU_HAS_GZIP) return false;
2321bd6bbdeSPatrick Brown        $fh = @gzopen($file,$mode.'9');
2331bd6bbdeSPatrick Brown        if(!$fh) return false;
2341bd6bbdeSPatrick Brown        gzwrite($fh, $content);
2351bd6bbdeSPatrick Brown        gzclose($fh);
2361bd6bbdeSPatrick Brown    }else if(substr($file,-4) == '.bz2'){
23713c37900SAndreas Gohr        if(!DOKU_HAS_BZIP) return false;
2381bd6bbdeSPatrick Brown        if($append) {
2391bd6bbdeSPatrick Brown            $bzcontent = bzfile($file);
2401bd6bbdeSPatrick Brown            if($bzcontent === false) return false;
2411bd6bbdeSPatrick Brown            $content = $bzcontent.$content;
2421bd6bbdeSPatrick Brown        }
2431bd6bbdeSPatrick Brown        $fh = @bzopen($file,'w');
2441bd6bbdeSPatrick Brown        if(!$fh) return false;
2451bd6bbdeSPatrick Brown        bzwrite($fh, $content);
2461bd6bbdeSPatrick Brown        bzclose($fh);
2471bd6bbdeSPatrick Brown    }else{
2481bd6bbdeSPatrick Brown        $fh = @fopen($file,$mode);
2491bd6bbdeSPatrick Brown        if(!$fh) return false;
2501bd6bbdeSPatrick Brown        fwrite($fh, $content);
2511bd6bbdeSPatrick Brown        fclose($fh);
2521bd6bbdeSPatrick Brown    }
2531bd6bbdeSPatrick Brown
2541bd6bbdeSPatrick Brown    if(!$fileexists and !empty($conf['fperm'])) chmod($file, $conf['fperm']);
2551bd6bbdeSPatrick Brown    return true;
2561bd6bbdeSPatrick Brown}
2571bd6bbdeSPatrick Brown
2581bd6bbdeSPatrick Brown/**
25915fae107Sandi * Saves $content to $file.
260f3f0262cSandi *
2611380fc45SAndreas Gohr * If the third parameter is set to true the given content
2621380fc45SAndreas Gohr * will be appended.
2631380fc45SAndreas Gohr *
26415fae107Sandi * Uses gzip if extension is .gz
265ff3ed99fSmarcel * and bz2 if extension is .bz2
26615fae107Sandi *
26715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
26842ea7f44SGerrit Uitslag *
26942ea7f44SGerrit Uitslag * @param string $file filename path to file
27042ea7f44SGerrit Uitslag * @param string $content
27142ea7f44SGerrit Uitslag * @param bool   $append
27242ea7f44SGerrit Uitslag * @return bool true on success, otherwise false
273f3f0262cSandi */
2741380fc45SAndreas Gohrfunction io_saveFile($file, $content, $append=false) {
275f3f0262cSandi    io_makeFileDir($file);
27690eb8392Sandi    io_lock($file);
2771bd6bbdeSPatrick Brown    if(!_io_saveFile($file, $content, $append)) {
278f3f0262cSandi        msg("Writing $file failed",-1);
279fb7125eeSAndreas Gohr        io_unlock($file);
280f3f0262cSandi        return false;
281f3f0262cSandi    }
28290eb8392Sandi    io_unlock($file);
283f3f0262cSandi    return true;
284f3f0262cSandi}
285f3f0262cSandi
286f3f0262cSandi/**
2871bd6bbdeSPatrick Brown * Replace one or more occurrences of a line in a file.
2881380fc45SAndreas Gohr *
289d93ba631SPatrick Brown * The default, when $maxlines is 0 is to delete all matching lines then append a single line.
290d93ba631SPatrick Brown * A regex that matches any part of the line will remove the entire line in this mode.
291d93ba631SPatrick Brown * Captures in $newline are not available.
2921bd6bbdeSPatrick Brown *
293d93ba631SPatrick Brown * Otherwise each line is matched and replaced individually, up to the first $maxlines lines
294d93ba631SPatrick Brown * or all lines if $maxlines is -1. If $regex is true then captures can be used in $newline.
295d93ba631SPatrick Brown *
296d93ba631SPatrick Brown * Be sure to include the trailing newline in $oldline when replacing entire lines.
297b158d625SSteven Danz *
298b158d625SSteven Danz * Uses gzip if extension is .gz
2991bd6bbdeSPatrick Brown * and bz2 if extension is .bz2
3008b06d178Schris *
301b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
3021bd6bbdeSPatrick Brown * @author Christopher Smith <chris@jalakai.co.uk>
3031bd6bbdeSPatrick Brown * @author Patrick Brown <ptbrown@whoopdedo.org>
30442ea7f44SGerrit Uitslag *
30542ea7f44SGerrit Uitslag * @param string $file     filename
3061bd6bbdeSPatrick Brown * @param string $oldline  exact linematch to remove
3071bd6bbdeSPatrick Brown * @param string $newline  new line to insert
30842ea7f44SGerrit Uitslag * @param bool   $regex    use regexp?
3091bd6bbdeSPatrick Brown * @param int    $maxlines number of occurrences of the line to replace
310b158d625SSteven Danz * @return bool true on success
311b158d625SSteven Danz */
3121bd6bbdeSPatrick Brownfunction io_replaceInFile($file, $oldline, $newline, $regex=false, $maxlines=0) {
313dc4a4eb0SPatrick Brown    if ((string)$oldline === '') {
314dc4a4eb0SPatrick Brown        trigger_error('$oldline parameter cannot be empty in io_replaceInFile()', E_USER_WARNING);
315dc4a4eb0SPatrick Brown        return false;
316dc4a4eb0SPatrick Brown    }
317dc4a4eb0SPatrick Brown
31879e79377SAndreas Gohr    if (!file_exists($file)) return true;
3191380fc45SAndreas Gohr
320b158d625SSteven Danz    io_lock($file);
3211380fc45SAndreas Gohr
3221380fc45SAndreas Gohr    // load into array
323b158d625SSteven Danz    if(substr($file,-3) == '.gz'){
32413c37900SAndreas Gohr        if(!DOKU_HAS_GZIP) return false;
3251380fc45SAndreas Gohr        $lines = gzfile($file);
326cfb71e37SPatrick Brown    }else if(substr($file,-4) == '.bz2'){
32713c37900SAndreas Gohr        if(!DOKU_HAS_BZIP) return false;
328cfb71e37SPatrick Brown        $lines = bzfile($file, true);
329b158d625SSteven Danz    }else{
3301380fc45SAndreas Gohr        $lines = file($file);
331b158d625SSteven Danz    }
332b158d625SSteven Danz
3339a734b7aSChristopher Smith    // make non-regexes into regexes
3343dfe7d64SChristopher Smith    $pattern = $regex ? $oldline : '/^'.preg_quote($oldline,'/').'$/';
3359a734b7aSChristopher Smith    $replace = $regex ? $newline : addcslashes($newline, '\$');
3369a734b7aSChristopher Smith
3379a734b7aSChristopher Smith    // remove matching lines
3386c000204SPatrick Brown    if ($maxlines > 0) {
3396c000204SPatrick Brown        $count = 0;
3409a734b7aSChristopher Smith        $matched = 0;
341a93ad676SAndreas Gohr        foreach($lines as $i => $line) {
342a93ad676SAndreas Gohr            if($count >= $maxlines) break;
3439a734b7aSChristopher Smith            // $matched will be set to 0|1 depending on whether pattern is matched and line replaced
3449a734b7aSChristopher Smith            $lines[$i] = preg_replace($pattern, $replace, $line, -1, $matched);
3459a734b7aSChristopher Smith            if ($matched) $count++;
3466c000204SPatrick Brown        }
347e12c5ac7SChristopher Smith    } else if ($maxlines == 0) {
348e12c5ac7SChristopher Smith        $lines = preg_grep($pattern, $lines, PREG_GREP_INVERT);
349b158d625SSteven Danz
350e12c5ac7SChristopher Smith        if ((string)$newline !== ''){
3511bd6bbdeSPatrick Brown            $lines[] = $newline;
3521bd6bbdeSPatrick Brown        }
353e12c5ac7SChristopher Smith    } else {
354e12c5ac7SChristopher Smith        $lines = preg_replace($pattern, $replace, $lines);
355e12c5ac7SChristopher Smith    }
3561bd6bbdeSPatrick Brown
3571380fc45SAndreas Gohr    if(count($lines)){
3581bd6bbdeSPatrick Brown        if(!_io_saveFile($file, join('',$lines), false)) {
359b158d625SSteven Danz            msg("Removing content from $file failed",-1);
360fb7125eeSAndreas Gohr            io_unlock($file);
361b158d625SSteven Danz            return false;
362b158d625SSteven Danz        }
363b158d625SSteven Danz    }else{
364b158d625SSteven Danz        @unlink($file);
365b158d625SSteven Danz    }
366b158d625SSteven Danz
367b158d625SSteven Danz    io_unlock($file);
368b158d625SSteven Danz    return true;
369b158d625SSteven Danz}
370b158d625SSteven Danz
371b158d625SSteven Danz/**
3721bd6bbdeSPatrick Brown * Delete lines that match $badline from $file.
3731bd6bbdeSPatrick Brown *
3741bd6bbdeSPatrick Brown * Be sure to include the trailing newline in $badline
3751bd6bbdeSPatrick Brown *
3761bd6bbdeSPatrick Brown * @author Patrick Brown <ptbrown@whoopdedo.org>
3771bd6bbdeSPatrick Brown *
3781bd6bbdeSPatrick Brown * @param string $file    filename
3791bd6bbdeSPatrick Brown * @param string $badline exact linematch to remove
3801bd6bbdeSPatrick Brown * @param bool   $regex   use regexp?
3811bd6bbdeSPatrick Brown * @return bool true on success
3821bd6bbdeSPatrick Brown */
3831bd6bbdeSPatrick Brownfunction io_deleteFromFile($file,$badline,$regex=false){
3841bd6bbdeSPatrick Brown    return io_replaceInFile($file,$badline,null,$regex,0);
3851bd6bbdeSPatrick Brown}
3861bd6bbdeSPatrick Brown
3871bd6bbdeSPatrick Brown/**
38890eb8392Sandi * Tries to lock a file
38990eb8392Sandi *
39090eb8392Sandi * Locking is only done for io_savefile and uses directories
39190eb8392Sandi * inside $conf['lockdir']
39290eb8392Sandi *
39390eb8392Sandi * It waits maximal 3 seconds for the lock, after this time
39490eb8392Sandi * the lock is assumed to be stale and the function goes on
39590eb8392Sandi *
39690eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org>
39742ea7f44SGerrit Uitslag *
39842ea7f44SGerrit Uitslag * @param string $file filename
39990eb8392Sandi */
40090eb8392Sandifunction io_lock($file){
40190eb8392Sandi    global $conf;
40290eb8392Sandi
40390eb8392Sandi    $lockDir = $conf['lockdir'].'/'.md5($file);
40490eb8392Sandi    @ignore_user_abort(1);
40590eb8392Sandi
40690eb8392Sandi    $timeStart = time();
40790eb8392Sandi    do {
40890eb8392Sandi        //waited longer than 3 seconds? -> stale lock
40990eb8392Sandi        if ((time() - $timeStart) > 3) break;
41044881d27STroels Liebe Bentsen        $locked = @mkdir($lockDir, $conf['dmode']);
41177b98903SAndreas Gohr        if($locked){
412bb4866bdSchris            if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']);
41377b98903SAndreas Gohr            break;
41477b98903SAndreas Gohr        }
41577b98903SAndreas Gohr        usleep(50);
41690eb8392Sandi    } while ($locked === false);
41790eb8392Sandi}
41890eb8392Sandi
41990eb8392Sandi/**
42090eb8392Sandi * Unlocks a file
42190eb8392Sandi *
42290eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org>
42342ea7f44SGerrit Uitslag *
42442ea7f44SGerrit Uitslag * @param string $file filename
42590eb8392Sandi */
42690eb8392Sandifunction io_unlock($file){
42790eb8392Sandi    global $conf;
42890eb8392Sandi
42990eb8392Sandi    $lockDir = $conf['lockdir'].'/'.md5($file);
43090eb8392Sandi    @rmdir($lockDir);
43190eb8392Sandi    @ignore_user_abort(0);
43290eb8392Sandi}
43390eb8392Sandi
43490eb8392Sandi/**
435cc7d0c94SBen Coburn * Create missing namespace directories and send the IO_NAMESPACE_CREATED events
436cc7d0c94SBen Coburn * in the order of directory creation. (Parent directories first.)
437cc7d0c94SBen Coburn *
438cc7d0c94SBen Coburn * Event data:
439cc7d0c94SBen Coburn * $data[0]    ns: The colon separated namespace path minus the trailing page name.
440cc7d0c94SBen Coburn * $data[1]    ns_type: 'pages' or 'media' namespace tree.
441cc7d0c94SBen Coburn *
442cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
44342ea7f44SGerrit Uitslag *
44442ea7f44SGerrit Uitslag * @param string $id page id
44542ea7f44SGerrit Uitslag * @param string $ns_type 'pages' or 'media'
446cc7d0c94SBen Coburn */
447cc7d0c94SBen Coburnfunction io_createNamespace($id, $ns_type='pages') {
448cc7d0c94SBen Coburn    // verify ns_type
449cc7d0c94SBen Coburn    $types = array('pages'=>'wikiFN', 'media'=>'mediaFN');
450cc7d0c94SBen Coburn    if (!isset($types[$ns_type])) {
451cc7d0c94SBen Coburn        trigger_error('Bad $ns_type parameter for io_createNamespace().');
452cc7d0c94SBen Coburn        return;
453cc7d0c94SBen Coburn    }
454cc7d0c94SBen Coburn    // make event list
455cc7d0c94SBen Coburn    $missing = array();
456cc7d0c94SBen Coburn    $ns_stack = explode(':', $id);
457cc7d0c94SBen Coburn    $ns = $id;
458cc7d0c94SBen Coburn    $tmp = dirname( $file = call_user_func($types[$ns_type], $ns) );
45979e79377SAndreas Gohr    while (!@is_dir($tmp) && !(file_exists($tmp) && !is_dir($tmp))) {
460cc7d0c94SBen Coburn        array_pop($ns_stack);
461cc7d0c94SBen Coburn        $ns = implode(':', $ns_stack);
462cc7d0c94SBen Coburn        if (strlen($ns)==0) { break; }
463cc7d0c94SBen Coburn        $missing[] = $ns;
464cc7d0c94SBen Coburn        $tmp = dirname(call_user_func($types[$ns_type], $ns));
465cc7d0c94SBen Coburn    }
466cc7d0c94SBen Coburn    // make directories
467cc7d0c94SBen Coburn    io_makeFileDir($file);
468cc7d0c94SBen Coburn    // send the events
469cc7d0c94SBen Coburn    $missing = array_reverse($missing); // inside out
470cc7d0c94SBen Coburn    foreach ($missing as $ns) {
471cc7d0c94SBen Coburn        $data = array($ns, $ns_type);
472cc7d0c94SBen Coburn        trigger_event('IO_NAMESPACE_CREATED', $data);
473cc7d0c94SBen Coburn    }
474cc7d0c94SBen Coburn}
475cc7d0c94SBen Coburn
476cc7d0c94SBen Coburn/**
477f3f0262cSandi * Create the directory needed for the given file
47815fae107Sandi *
47915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
48042ea7f44SGerrit Uitslag *
48142ea7f44SGerrit Uitslag * @param string $file file name
482f3f0262cSandi */
483f3f0262cSandifunction io_makeFileDir($file){
484f3f0262cSandi    $dir = dirname($file);
4850d8850c4SAndreas Gohr    if(!@is_dir($dir)){
486f3f0262cSandi        io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
487f3f0262cSandi    }
488f3f0262cSandi}
489f3f0262cSandi
490f3f0262cSandi/**
491f3f0262cSandi * Creates a directory hierachy.
492f3f0262cSandi *
49359752844SAnders Sandblad * @link    http://php.net/manual/en/function.mkdir.php
494f3f0262cSandi * @author  <saint@corenova.com>
4953dc3a5f1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
49642ea7f44SGerrit Uitslag *
49742ea7f44SGerrit Uitslag * @param string $target filename
49842ea7f44SGerrit Uitslag * @return bool|int|string
499f3f0262cSandi */
500f3f0262cSandifunction io_mkdir_p($target){
5013dc3a5f1Sandi    global $conf;
5020d8850c4SAndreas Gohr    if (@is_dir($target)||empty($target)) return 1; // best case check first
50379e79377SAndreas Gohr    if (file_exists($target) && !is_dir($target)) return 0;
5043dc3a5f1Sandi    //recursion
5053dc3a5f1Sandi    if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
50644881d27STroels Liebe Bentsen        $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree
507443e135dSChristopher Smith        if($ret && !empty($conf['dperm'])) chmod($target, $conf['dperm']);
50844881d27STroels Liebe Bentsen        return $ret;
5093dc3a5f1Sandi    }
510f3f0262cSandi    return 0;
511f3f0262cSandi}
512f3f0262cSandi
513f3f0262cSandi/**
5144d47e8e3SAndreas Gohr * Recursively delete a directory
5154d47e8e3SAndreas Gohr *
5164d47e8e3SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5174d47e8e3SAndreas Gohr * @param string $path
5184d47e8e3SAndreas Gohr * @param bool   $removefiles defaults to false which will delete empty directories only
5194d47e8e3SAndreas Gohr * @return bool
5204d47e8e3SAndreas Gohr */
5214d47e8e3SAndreas Gohrfunction io_rmdir($path, $removefiles = false) {
5224d47e8e3SAndreas Gohr    if(!is_string($path) || $path == "") return false;
523d8cf4dd4SAndreas Gohr    if(!file_exists($path)) return true; // it's already gone or was never there, count as success
5244d47e8e3SAndreas Gohr
5254d47e8e3SAndreas Gohr    if(is_dir($path) && !is_link($path)) {
5264d47e8e3SAndreas Gohr        $dirs  = array();
5274d47e8e3SAndreas Gohr        $files = array();
5284d47e8e3SAndreas Gohr
5294d47e8e3SAndreas Gohr        if(!$dh = @opendir($path)) return false;
5308426a3eeSAndreas Gohr        while(false !== ($f = readdir($dh))) {
5314d47e8e3SAndreas Gohr            if($f == '..' || $f == '.') continue;
5324d47e8e3SAndreas Gohr
5334d47e8e3SAndreas Gohr            // collect dirs and files first
5344d47e8e3SAndreas Gohr            if(is_dir("$path/$f") && !is_link("$path/$f")) {
5354d47e8e3SAndreas Gohr                $dirs[] = "$path/$f";
5364d47e8e3SAndreas Gohr            } else if($removefiles) {
5374d47e8e3SAndreas Gohr                $files[] = "$path/$f";
5384d47e8e3SAndreas Gohr            } else {
5394d47e8e3SAndreas Gohr                return false; // abort when non empty
5404d47e8e3SAndreas Gohr            }
5414d47e8e3SAndreas Gohr
5424d47e8e3SAndreas Gohr        }
5434d47e8e3SAndreas Gohr        closedir($dh);
5444d47e8e3SAndreas Gohr
5454d47e8e3SAndreas Gohr        // now traverse into  directories first
5464d47e8e3SAndreas Gohr        foreach($dirs as $dir) {
5474d47e8e3SAndreas Gohr            if(!io_rmdir($dir, $removefiles)) return false; // abort on any error
5484d47e8e3SAndreas Gohr        }
5494d47e8e3SAndreas Gohr
5504d47e8e3SAndreas Gohr        // now delete files
5514d47e8e3SAndreas Gohr        foreach($files as $file) {
5524d47e8e3SAndreas Gohr            if(!@unlink($file)) return false; //abort on any error
5534d47e8e3SAndreas Gohr        }
5544d47e8e3SAndreas Gohr
5554d47e8e3SAndreas Gohr        // remove self
5564d47e8e3SAndreas Gohr        return @rmdir($path);
5574d47e8e3SAndreas Gohr    } else if($removefiles) {
5584d47e8e3SAndreas Gohr        return @unlink($path);
5594d47e8e3SAndreas Gohr    }
5604d47e8e3SAndreas Gohr    return false;
5614d47e8e3SAndreas Gohr}
5624d47e8e3SAndreas Gohr
5634d47e8e3SAndreas Gohr/**
564de862555SMichael Klier * Creates a unique temporary directory and returns
565de862555SMichael Klier * its path.
566de862555SMichael Klier *
567de862555SMichael Klier * @author Michael Klier <chi@chimeric.de>
56842ea7f44SGerrit Uitslag *
56942ea7f44SGerrit Uitslag * @return false|string path to new directory or false
570de862555SMichael Klier */
571de862555SMichael Klierfunction io_mktmpdir() {
572de862555SMichael Klier    global $conf;
573de862555SMichael Klier
574da1e1077SChris Smith    $base = $conf['tmpdir'];
575da1e1077SChris Smith    $dir  = md5(uniqid(mt_rand(), true));
576287f35bdSAndreas Gohr    $tmpdir = $base.'/'.$dir;
577de862555SMichael Klier
578de862555SMichael Klier    if(io_mkdir_p($tmpdir)) {
579de862555SMichael Klier        return($tmpdir);
580de862555SMichael Klier    } else {
581de862555SMichael Klier        return false;
582de862555SMichael Klier    }
583de862555SMichael Klier}
584de862555SMichael Klier
585de862555SMichael Klier/**
58673ccfcb9Schris * downloads a file from the net and saves it
58773ccfcb9Schris *
58873ccfcb9Schris * if $useAttachment is false,
58973ccfcb9Schris * - $file is the full filename to save the file, incl. path
59073ccfcb9Schris * - if successful will return true, false otherwise
591db959ae3SAndreas Gohr *
59273ccfcb9Schris * if $useAttachment is true,
59373ccfcb9Schris * - $file is the directory where the file should be saved
59473ccfcb9Schris * - if successful will return the name used for the saved file, false otherwise
595b625487dSandi *
596b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
59773ccfcb9Schris * @author Chris Smith <chris@jalakai.co.uk>
59842ea7f44SGerrit Uitslag *
59942ea7f44SGerrit Uitslag * @param string $url           url to download
60042ea7f44SGerrit Uitslag * @param string $file          path to file or directory where to save
60164159a61SAndreas Gohr * @param bool   $useAttachment true: try to use name of download, uses otherwise $defaultName
60264159a61SAndreas Gohr *                              false: uses $file as path to file
60342ea7f44SGerrit Uitslag * @param string $defaultName   fallback for if using $useAttachment
60442ea7f44SGerrit Uitslag * @param int    $maxSize       maximum file size
60542ea7f44SGerrit Uitslag * @return bool|string          if failed false, otherwise true or the name of the file in the given dir
606b625487dSandi */
607847b8298SAndreas Gohrfunction io_download($url,$file,$useAttachment=false,$defaultName='',$maxSize=2097152){
608ac9115b0STroels Liebe Bentsen    global $conf;
6099b307a83SAndreas Gohr    $http = new DokuHTTPClient();
610847b8298SAndreas Gohr    $http->max_bodysize = $maxSize;
6119b307a83SAndreas Gohr    $http->timeout = 25; //max. 25 sec
612a5951419SAndreas Gohr    $http->keep_alive = false; // we do single ops here, no need for keep-alive
6139b307a83SAndreas Gohr
6149b307a83SAndreas Gohr    $data = $http->get($url);
6159b307a83SAndreas Gohr    if(!$data) return false;
6169b307a83SAndreas Gohr
61773ccfcb9Schris    $name = '';
618cd2f903bSMichael Hamann    if ($useAttachment) {
61973ccfcb9Schris        if (isset($http->resp_headers['content-disposition'])) {
62073ccfcb9Schris            $content_disposition = $http->resp_headers['content-disposition'];
621ce070a9fSchris            $match=array();
62273ccfcb9Schris            if (is_string($content_disposition) &&
623ce070a9fSchris                    preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) {
62473ccfcb9Schris
6253009a773SAndreas Gohr                $name = utf8_basename($match[1]);
62673ccfcb9Schris            }
62773ccfcb9Schris
62873ccfcb9Schris        }
62973ccfcb9Schris
63073ccfcb9Schris        if (!$name) {
63173ccfcb9Schris            if (!$defaultName) return false;
63273ccfcb9Schris            $name = $defaultName;
63373ccfcb9Schris        }
63473ccfcb9Schris
63573ccfcb9Schris        $file = $file.$name;
63673ccfcb9Schris    }
63773ccfcb9Schris
63879e79377SAndreas Gohr    $fileexists = file_exists($file);
6399b307a83SAndreas Gohr    $fp = @fopen($file,"w");
640b625487dSandi    if(!$fp) return false;
6419b307a83SAndreas Gohr    fwrite($fp,$data);
642b625487dSandi    fclose($fp);
6431ca31cfeSAndreas Gohr    if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
64473ccfcb9Schris    if ($useAttachment) return $name;
645b625487dSandi    return true;
646b625487dSandi}
647b625487dSandi
648b625487dSandi/**
649ac9115b0STroels Liebe Bentsen * Windows compatible rename
650bf5e5a5bSAndreas Gohr *
651bf5e5a5bSAndreas Gohr * rename() can not overwrite existing files on Windows
652bf5e5a5bSAndreas Gohr * this function will use copy/unlink instead
65342ea7f44SGerrit Uitslag *
65442ea7f44SGerrit Uitslag * @param string $from
65542ea7f44SGerrit Uitslag * @param string $to
65642ea7f44SGerrit Uitslag * @return bool succes or fail
657bf5e5a5bSAndreas Gohr */
658bf5e5a5bSAndreas Gohrfunction io_rename($from,$to){
659ac9115b0STroels Liebe Bentsen    global $conf;
660bf5e5a5bSAndreas Gohr    if(!@rename($from,$to)){
661bf5e5a5bSAndreas Gohr        if(@copy($from,$to)){
6628e0b019fSAndreas Gohr            if($conf['fperm']) chmod($to, $conf['fperm']);
663bf5e5a5bSAndreas Gohr            @unlink($from);
664bf5e5a5bSAndreas Gohr            return true;
665bf5e5a5bSAndreas Gohr        }
666bf5e5a5bSAndreas Gohr        return false;
667bf5e5a5bSAndreas Gohr    }
668bf5e5a5bSAndreas Gohr    return true;
669bf5e5a5bSAndreas Gohr}
670bf5e5a5bSAndreas Gohr
671420edfd6STom N Harris/**
672420edfd6STom N Harris * Runs an external command with input and output pipes.
673420edfd6STom N Harris * Returns the exit code from the process.
674420edfd6STom N Harris *
675420edfd6STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org>
67642ea7f44SGerrit Uitslag *
67742ea7f44SGerrit Uitslag * @param string $cmd
67842ea7f44SGerrit Uitslag * @param string $input  input pipe
67942ea7f44SGerrit Uitslag * @param string $output output pipe
68042ea7f44SGerrit Uitslag * @return int exit code from process
681420edfd6STom N Harris */
682420edfd6STom N Harrisfunction io_exec($cmd, $input, &$output){
6836c528220STom N Harris    $descspec = array(
6846c528220STom N Harris            0=>array("pipe","r"),
6856c528220STom N Harris            1=>array("pipe","w"),
6866c528220STom N Harris            2=>array("pipe","w"));
6876c528220STom N Harris    $ph = proc_open($cmd, $descspec, $pipes);
6886c528220STom N Harris    if(!$ph) return -1;
6896c528220STom N Harris    fclose($pipes[2]); // ignore stderr
6906c528220STom N Harris    fwrite($pipes[0], $input);
6916c528220STom N Harris    fclose($pipes[0]);
6926c528220STom N Harris    $output = stream_get_contents($pipes[1]);
6936c528220STom N Harris    fclose($pipes[1]);
6946c528220STom N Harris    return proc_close($ph);
695f3f0262cSandi}
696f3f0262cSandi
6977421c3ccSAndreas Gohr/**
6987421c3ccSAndreas Gohr * Search a file for matching lines
6997421c3ccSAndreas Gohr *
7007421c3ccSAndreas Gohr * This is probably not faster than file()+preg_grep() but less
7017421c3ccSAndreas Gohr * memory intensive because not the whole file needs to be loaded
7027421c3ccSAndreas Gohr * at once.
7037421c3ccSAndreas Gohr *
7047421c3ccSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
7057421c3ccSAndreas Gohr * @param  string $file    The file to search
7067421c3ccSAndreas Gohr * @param  string $pattern PCRE pattern
7077421c3ccSAndreas Gohr * @param  int    $max     How many lines to return (0 for all)
708cd2f903bSMichael Hamann * @param  bool   $backref When true returns array with backreferences instead of lines
709cd2f903bSMichael Hamann * @return array matching lines or backref, false on error
7107421c3ccSAndreas Gohr */
7117421c3ccSAndreas Gohrfunction io_grep($file,$pattern,$max=0,$backref=false){
7127421c3ccSAndreas Gohr    $fh = @fopen($file,'r');
7137421c3ccSAndreas Gohr    if(!$fh) return false;
7147421c3ccSAndreas Gohr    $matches = array();
7157421c3ccSAndreas Gohr
7167421c3ccSAndreas Gohr    $cnt  = 0;
7177421c3ccSAndreas Gohr    $line = '';
7187421c3ccSAndreas Gohr    while (!feof($fh)) {
7197421c3ccSAndreas Gohr        $line .= fgets($fh, 4096);  // read full line
7207421c3ccSAndreas Gohr        if(substr($line,-1) != "\n") continue;
7217421c3ccSAndreas Gohr
7227421c3ccSAndreas Gohr        // check if line matches
7237421c3ccSAndreas Gohr        if(preg_match($pattern,$line,$match)){
7247421c3ccSAndreas Gohr            if($backref){
7257421c3ccSAndreas Gohr                $matches[] = $match;
7267421c3ccSAndreas Gohr            }else{
7277421c3ccSAndreas Gohr                $matches[] = $line;
7287421c3ccSAndreas Gohr            }
7297421c3ccSAndreas Gohr            $cnt++;
7307421c3ccSAndreas Gohr        }
7317421c3ccSAndreas Gohr        if($max && $max == $cnt) break;
7327421c3ccSAndreas Gohr        $line = '';
7337421c3ccSAndreas Gohr    }
7347421c3ccSAndreas Gohr    fclose($fh);
7357421c3ccSAndreas Gohr    return $matches;
7367421c3ccSAndreas Gohr}
7377421c3ccSAndreas Gohr
738f549be3dSGerrit Uitslag
739f549be3dSGerrit Uitslag/**
740f549be3dSGerrit Uitslag * Get size of contents of a file, for a compressed file the uncompressed size
741f549be3dSGerrit Uitslag * Warning: reading uncompressed size of content of bz-files requires uncompressing
742f549be3dSGerrit Uitslag *
743f549be3dSGerrit Uitslag * @author  Gerrit Uitslag <klapinklapin@gmail.com>
744f549be3dSGerrit Uitslag *
745f549be3dSGerrit Uitslag * @param string $file filename path to file
746f549be3dSGerrit Uitslag * @return int size of file
747f549be3dSGerrit Uitslag */
748f549be3dSGerrit Uitslagfunction io_getSizeFile($file) {
749f549be3dSGerrit Uitslag    if (!file_exists($file)) return 0;
750f549be3dSGerrit Uitslag
751f549be3dSGerrit Uitslag    if(substr($file,-3) == '.gz'){
752f549be3dSGerrit Uitslag        $fp = @fopen($file, "rb");
753f549be3dSGerrit Uitslag        if($fp === false) return 0;
754f549be3dSGerrit Uitslag
755f549be3dSGerrit Uitslag        fseek($fp, -4, SEEK_END);
756f549be3dSGerrit Uitslag        $buffer = fread($fp, 4);
757f549be3dSGerrit Uitslag        fclose($fp);
758f549be3dSGerrit Uitslag        $array = unpack("V", $buffer);
759f549be3dSGerrit Uitslag        $uncompressedsize = end($array);
760f549be3dSGerrit Uitslag    }else if(substr($file,-4) == '.bz2'){
761f549be3dSGerrit Uitslag        if(!DOKU_HAS_BZIP) return 0;
762f549be3dSGerrit Uitslag
763f549be3dSGerrit Uitslag        $bz = bzopen($file,"r");
764f549be3dSGerrit Uitslag        if($bz === false) return 0;
765f549be3dSGerrit Uitslag
766f549be3dSGerrit Uitslag        $uncompressedsize = 0;
767f549be3dSGerrit Uitslag        while (!feof($bz)) {
768f549be3dSGerrit Uitslag            //8192 seems to be the maximum buffersize?
769f549be3dSGerrit Uitslag            $buffer = bzread($bz,8192);
770f549be3dSGerrit Uitslag            if(($buffer === false) || (bzerrno($bz) !== 0)) {
771f549be3dSGerrit Uitslag                return 0;
772f549be3dSGerrit Uitslag            }
773f549be3dSGerrit Uitslag            $uncompressedsize += strlen($buffer);
774f549be3dSGerrit Uitslag        }
775f549be3dSGerrit Uitslag    }else{
776f549be3dSGerrit Uitslag        $uncompressedsize = filesize($file);
777f549be3dSGerrit Uitslag    }
778f549be3dSGerrit Uitslag
779f549be3dSGerrit Uitslag    return $uncompressedsize;
780f549be3dSGerrit Uitslag }
781