Update copyright years
[dokuwiki.git] / inc / io.php
blob32a6f7b8e0a07f32f0c1c7bd8db4dde18d97c774
1 <?php
2 /**
3 * File IO functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
9 if(!defined('DOKU_INC')) die('meh.');
10 require_once(DOKU_INC.'inc/common.php');
11 require_once(DOKU_INC.'inc/HTTPClient.php');
12 require_once(DOKU_INC.'inc/events.php');
13 require_once(DOKU_INC.'inc/utf8.php');
15 /**
16 * Removes empty directories
18 * Sends IO_NAMESPACE_DELETED events for 'pages' and 'media' namespaces.
19 * Event data:
20 * $data[0] ns: The colon separated namespace path minus the trailing page name.
21 * $data[1] ns_type: 'pages' or 'media' namespace tree.
23 * @todo use safemode hack
24 * @param string $id - a pageid, the namespace of that id will be tried to deleted
25 * @param string $basadir - the config name of the type to delete (datadir or mediadir usally)
26 * @returns bool - true if at least one namespace was deleted
27 * @author Andreas Gohr <andi@splitbrain.org>
28 * @author Ben Coburn <btcoburn@silicodon.net>
30 function io_sweepNS($id,$basedir='datadir'){
31 global $conf;
32 $types = array ('datadir'=>'pages', 'mediadir'=>'media');
33 $ns_type = (isset($types[$basedir])?$types[$basedir]:false);
35 $delone = false;
37 //scan all namespaces
38 while(($id = getNS($id)) !== false){
39 $dir = $conf[$basedir].'/'.utf8_encodeFN(str_replace(':','/',$id));
41 //try to delete dir else return
42 if(@rmdir($dir)) {
43 if ($ns_type!==false) {
44 $data = array($id, $ns_type);
45 $delone = true; // we deleted at least one dir
46 trigger_event('IO_NAMESPACE_DELETED', $data);
48 } else { return $delone; }
50 return $delone;
53 /**
54 * Used to read in a DokuWiki page from file, and send IO_WIKIPAGE_READ events.
56 * Generates the action event which delegates to io_readFile().
57 * Action plugins are allowed to modify the page content in transit.
58 * The file path should not be changed.
60 * Event data:
61 * $data[0] The raw arguments for io_readFile as an array.
62 * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns)
63 * $data[2] page_name: The wiki page name.
64 * $data[3] rev: The page revision, false for current wiki pages.
66 * @author Ben Coburn <btcoburn@silicodon.net>
68 function io_readWikiPage($file, $id, $rev=false) {
69 if (empty($rev)) { $rev = false; }
70 $data = array(array($file, false), getNS($id), noNS($id), $rev);
71 return trigger_event('IO_WIKIPAGE_READ', $data, '_io_readWikiPage_action', false);
74 /**
75 * Callback adapter for io_readFile().
76 * @author Ben Coburn <btcoburn@silicodon.net>
78 function _io_readWikiPage_action($data) {
79 if (is_array($data) && is_array($data[0]) && count($data[0])===2) {
80 return call_user_func_array('io_readFile', $data[0]);
81 } else {
82 return ''; //callback error
86 /**
87 * Returns content of $file as cleaned string.
89 * Uses gzip if extension is .gz
91 * If you want to use the returned value in unserialize
92 * be sure to set $clean to false!
94 * @author Andreas Gohr <andi@splitbrain.org>
96 function io_readFile($file,$clean=true){
97 $ret = '';
98 if(@file_exists($file)){
99 if(substr($file,-3) == '.gz'){
100 $ret = join('',gzfile($file));
101 }else if(substr($file,-4) == '.bz2'){
102 $ret = bzfile($file);
103 }else{
104 $ret = file_get_contents($file);
107 if($clean){
108 return cleanText($ret);
109 }else{
110 return $ret;
114 * Returns the content of a .bz2 compressed file as string
115 * @author marcel senf <marcel@rucksackreinigung.de>
118 function bzfile($file){
119 $bz = bzopen($file,"r");
120 while (!feof($bz)){
121 //8192 seems to be the maximum buffersize?
122 $str = $str . bzread($bz,8192);
124 bzclose($bz);
125 return $str;
130 * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events.
132 * This generates an action event and delegates to io_saveFile().
133 * Action plugins are allowed to modify the page content in transit.
134 * The file path should not be changed.
135 * (The append parameter is set to false.)
137 * Event data:
138 * $data[0] The raw arguments for io_saveFile as an array.
139 * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns)
140 * $data[2] page_name: The wiki page name.
141 * $data[3] rev: The page revision, false for current wiki pages.
143 * @author Ben Coburn <btcoburn@silicodon.net>
145 function io_writeWikiPage($file, $content, $id, $rev=false) {
146 if (empty($rev)) { $rev = false; }
147 if ($rev===false) { io_createNamespace($id); } // create namespaces as needed
148 $data = array(array($file, $content, false), getNS($id), noNS($id), $rev);
149 return trigger_event('IO_WIKIPAGE_WRITE', $data, '_io_writeWikiPage_action', false);
153 * Callback adapter for io_saveFile().
154 * @author Ben Coburn <btcoburn@silicodon.net>
156 function _io_writeWikiPage_action($data) {
157 if (is_array($data) && is_array($data[0]) && count($data[0])===3) {
158 return call_user_func_array('io_saveFile', $data[0]);
159 } else {
160 return false; //callback error
165 * Saves $content to $file.
167 * If the third parameter is set to true the given content
168 * will be appended.
170 * Uses gzip if extension is .gz
171 * and bz2 if extension is .bz2
173 * @author Andreas Gohr <andi@splitbrain.org>
174 * @return bool true on success
176 function io_saveFile($file,$content,$append=false){
177 global $conf;
178 $mode = ($append) ? 'ab' : 'wb';
180 $fileexists = @file_exists($file);
181 io_makeFileDir($file);
182 io_lock($file);
183 if(substr($file,-3) == '.gz'){
184 $fh = @gzopen($file,$mode.'9');
185 if(!$fh){
186 msg("Writing $file failed",-1);
187 io_unlock($file);
188 return false;
190 gzwrite($fh, $content);
191 gzclose($fh);
192 }else if(substr($file,-4) == '.bz2'){
193 $fh = @bzopen($file,$mode{0});
194 if(!$fh){
195 msg("Writing $file failed", -1);
196 io_unlock($file);
197 return false;
199 bzwrite($fh, $content);
200 bzclose($fh);
201 }else{
202 $fh = @fopen($file,$mode);
203 if(!$fh){
204 msg("Writing $file failed",-1);
205 io_unlock($file);
206 return false;
208 fwrite($fh, $content);
209 fclose($fh);
212 if(!$fileexists and !empty($conf['fperm'])) chmod($file, $conf['fperm']);
213 io_unlock($file);
214 return true;
218 * Delete exact linematch for $badline from $file.
220 * Be sure to include the trailing newline in $badline
222 * Uses gzip if extension is .gz
224 * 2005-10-14 : added regex option -- Christopher Smith <chris@jalakai.co.uk>
226 * @author Steven Danz <steven-danz@kc.rr.com>
227 * @return bool true on success
229 function io_deleteFromFile($file,$badline,$regex=false){
230 if (!@file_exists($file)) return true;
232 io_lock($file);
234 // load into array
235 if(substr($file,-3) == '.gz'){
236 $lines = gzfile($file);
237 }else{
238 $lines = file($file);
241 // remove all matching lines
242 if ($regex) {
243 $lines = preg_grep($badline,$lines,PREG_GREP_INVERT);
244 } else {
245 $pos = array_search($badline,$lines); //return null or false if not found
246 while(is_int($pos)){
247 unset($lines[$pos]);
248 $pos = array_search($badline,$lines);
252 if(count($lines)){
253 $content = join('',$lines);
254 if(substr($file,-3) == '.gz'){
255 $fh = @gzopen($file,'wb9');
256 if(!$fh){
257 msg("Removing content from $file failed",-1);
258 io_unlock($file);
259 return false;
261 gzwrite($fh, $content);
262 gzclose($fh);
263 }else{
264 $fh = @fopen($file,'wb');
265 if(!$fh){
266 msg("Removing content from $file failed",-1);
267 io_unlock($file);
268 return false;
270 fwrite($fh, $content);
271 fclose($fh);
273 }else{
274 @unlink($file);
277 io_unlock($file);
278 return true;
282 * Tries to lock a file
284 * Locking is only done for io_savefile and uses directories
285 * inside $conf['lockdir']
287 * It waits maximal 3 seconds for the lock, after this time
288 * the lock is assumed to be stale and the function goes on
290 * @author Andreas Gohr <andi@splitbrain.org>
292 function io_lock($file){
293 global $conf;
294 // no locking if safemode hack
295 if($conf['safemodehack']) return;
297 $lockDir = $conf['lockdir'].'/'.md5($file);
298 @ignore_user_abort(1);
300 $timeStart = time();
301 do {
302 //waited longer than 3 seconds? -> stale lock
303 if ((time() - $timeStart) > 3) break;
304 $locked = @mkdir($lockDir, $conf['dmode']);
305 if($locked){
306 if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']);
307 break;
309 usleep(50);
310 } while ($locked === false);
314 * Unlocks a file
316 * @author Andreas Gohr <andi@splitbrain.org>
318 function io_unlock($file){
319 global $conf;
320 // no locking if safemode hack
321 if($conf['safemodehack']) return;
323 $lockDir = $conf['lockdir'].'/'.md5($file);
324 @rmdir($lockDir);
325 @ignore_user_abort(0);
329 * Create missing namespace directories and send the IO_NAMESPACE_CREATED events
330 * in the order of directory creation. (Parent directories first.)
332 * Event data:
333 * $data[0] ns: The colon separated namespace path minus the trailing page name.
334 * $data[1] ns_type: 'pages' or 'media' namespace tree.
336 * @author Ben Coburn <btcoburn@silicodon.net>
338 function io_createNamespace($id, $ns_type='pages') {
339 // verify ns_type
340 $types = array('pages'=>'wikiFN', 'media'=>'mediaFN');
341 if (!isset($types[$ns_type])) {
342 trigger_error('Bad $ns_type parameter for io_createNamespace().');
343 return;
345 // make event list
346 $missing = array();
347 $ns_stack = explode(':', $id);
348 $ns = $id;
349 $tmp = dirname( $file = call_user_func($types[$ns_type], $ns) );
350 while (!@is_dir($tmp) && !(@file_exists($tmp) && !is_dir($tmp))) {
351 array_pop($ns_stack);
352 $ns = implode(':', $ns_stack);
353 if (strlen($ns)==0) { break; }
354 $missing[] = $ns;
355 $tmp = dirname(call_user_func($types[$ns_type], $ns));
357 // make directories
358 io_makeFileDir($file);
359 // send the events
360 $missing = array_reverse($missing); // inside out
361 foreach ($missing as $ns) {
362 $data = array($ns, $ns_type);
363 trigger_event('IO_NAMESPACE_CREATED', $data);
368 * Create the directory needed for the given file
370 * @author Andreas Gohr <andi@splitbrain.org>
372 function io_makeFileDir($file){
373 global $conf;
375 $dir = dirname($file);
376 if(!@is_dir($dir)){
377 io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
382 * Creates a directory hierachy.
384 * @link http://www.php.net/manual/en/function.mkdir.php
385 * @author <saint@corenova.com>
386 * @author Andreas Gohr <andi@splitbrain.org>
388 function io_mkdir_p($target){
389 global $conf;
390 if (@is_dir($target)||empty($target)) return 1; // best case check first
391 if (@file_exists($target) && !is_dir($target)) return 0;
392 //recursion
393 if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
394 if($conf['safemodehack']){
395 $dir = preg_replace('/^'.preg_quote(fullpath($conf['ftp']['root']),'/').'/','', $target);
396 return io_mkdir_ftp($dir);
397 }else{
398 $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree
399 if($ret && $conf['dperm']) chmod($target, $conf['dperm']);
400 return $ret;
403 return 0;
407 * Creates a directory using FTP
409 * This is used when the safemode workaround is enabled
411 * @author <andi@splitbrain.org>
413 function io_mkdir_ftp($dir){
414 global $conf;
416 if(!function_exists('ftp_connect')){
417 msg("FTP support not found - safemode workaround not usable",-1);
418 return false;
421 $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10);
422 if(!$conn){
423 msg("FTP connection failed",-1);
424 return false;
427 if(!@ftp_login($conn, $conf['ftp']['user'], conf_decodeString($conf['ftp']['pass']))){
428 msg("FTP login failed",-1);
429 return false;
432 //create directory
433 $ok = @ftp_mkdir($conn, $dir);
434 //set permissions
435 @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmode'],$dir));
437 @ftp_close($conn);
438 return $ok;
442 * Creates a unique temporary directory and returns
443 * its path.
445 * @author Michael Klier <chi@chimeric.de>
447 function io_mktmpdir() {
448 global $conf;
450 $base = $conf['tmpdir'];
451 $dir = md5(uniqid(mt_rand(), true));
452 $tmpdir = $base.'/'.$dir;
454 if(io_mkdir_p($tmpdir)) {
455 return($tmpdir);
456 } else {
457 return false;
462 * downloads a file from the net and saves it
464 * if $useAttachment is false,
465 * - $file is the full filename to save the file, incl. path
466 * - if successful will return true, false otherwise
468 * if $useAttachment is true,
469 * - $file is the directory where the file should be saved
470 * - if successful will return the name used for the saved file, false otherwise
472 * @author Andreas Gohr <andi@splitbrain.org>
473 * @author Chris Smith <chris@jalakai.co.uk>
475 function io_download($url,$file,$useAttachment=false,$defaultName='',$maxSize=2097152){
476 global $conf;
477 $http = new DokuHTTPClient();
478 $http->max_bodysize = $maxSize;
479 $http->timeout = 25; //max. 25 sec
481 $data = $http->get($url);
482 if(!$data) return false;
484 if ($useAttachment) {
485 $name = '';
486 if (isset($http->resp_headers['content-disposition'])) {
487 $content_disposition = $http->resp_headers['content-disposition'];
488 $match=array();
489 if (is_string($content_disposition) &&
490 preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) {
492 $name = basename($match[1]);
497 if (!$name) {
498 if (!$defaultName) return false;
499 $name = $defaultName;
502 $file = $file.$name;
505 $fileexists = @file_exists($file);
506 $fp = @fopen($file,"w");
507 if(!$fp) return false;
508 fwrite($fp,$data);
509 fclose($fp);
510 if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
511 if ($useAttachment) return $name;
512 return true;
516 * Windows compatible rename
518 * rename() can not overwrite existing files on Windows
519 * this function will use copy/unlink instead
521 function io_rename($from,$to){
522 global $conf;
523 if(!@rename($from,$to)){
524 if(@copy($from,$to)){
525 if($conf['fperm']) chmod($to, $conf['fperm']);
526 @unlink($from);
527 return true;
529 return false;
531 return true;
536 * Runs an external command and returns it's output as string
538 * @author Harry Brueckner <harry_b@eml.cc>
539 * @author Andreas Gohr <andi@splitbrain.org>
540 * @deprecated
542 function io_runcmd($cmd){
543 $fh = popen($cmd, "r");
544 if(!$fh) return false;
545 $ret = '';
546 while (!feof($fh)) {
547 $ret .= fread($fh, 8192);
549 pclose($fh);
550 return $ret;
554 * Search a file for matching lines
556 * This is probably not faster than file()+preg_grep() but less
557 * memory intensive because not the whole file needs to be loaded
558 * at once.
560 * @author Andreas Gohr <andi@splitbrain.org>
561 * @param string $file The file to search
562 * @param string $pattern PCRE pattern
563 * @param int $max How many lines to return (0 for all)
564 * @param bool $baxkref When true returns array with backreferences instead of lines
565 * @return matching lines or backref, false on error
567 function io_grep($file,$pattern,$max=0,$backref=false){
568 $fh = @fopen($file,'r');
569 if(!$fh) return false;
570 $matches = array();
572 $cnt = 0;
573 $line = '';
574 while (!feof($fh)) {
575 $line .= fgets($fh, 4096); // read full line
576 if(substr($line,-1) != "\n") continue;
578 // check if line matches
579 if(preg_match($pattern,$line,$match)){
580 if($backref){
581 $matches[] = $match;
582 }else{
583 $matches[] = $line;
585 $cnt++;
587 if($max && $max == $cnt) break;
588 $line = '';
590 fclose($fh);
591 return $matches;