xref: /dokuwiki/inc/io.php (revision 6cd2b1e4b7588c56b52418c91396c264538d5a54)
1<?php
2/**
3 * File IO functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
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');
14
15/**
16 * Removes empty directories
17 *
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.
22 *
23 * @todo use safemode hack
24 * @author  Andreas Gohr <andi@splitbrain.org>
25 * @author Ben Coburn <btcoburn@silicodon.net>
26 */
27function io_sweepNS($id,$basedir='datadir'){
28  global $conf;
29  $types = array ('datadir'=>'pages', 'mediadir'=>'media');
30  $ns_type = (isset($types[$basedir])?$types[$basedir]:false);
31
32  //scan all namespaces
33  while(($id = getNS($id)) !== false){
34    $dir = $conf[$basedir].'/'.utf8_encodeFN(str_replace(':','/',$id));
35
36    //try to delete dir else return
37    if(@rmdir($dir)) {
38      if ($ns_type!==false) {
39        $data = array($id, $ns_type);
40        trigger_event('IO_NAMESPACE_DELETED', $data);
41      }
42    } else { return; }
43  }
44}
45
46/**
47 * Used to read in a DokuWiki page from file, and send IO_WIKIPAGE_READ events.
48 *
49 * Generates the action event which delegates to io_readFile().
50 * Action plugins are allowed to modify the page content in transit.
51 * The file path should not be changed.
52 *
53 * Event data:
54 * $data[0]    The raw arguments for io_readFile as an array.
55 * $data[1]    ns: The colon separated namespace path minus the trailing page name. (false if root ns)
56 * $data[2]    page_name: The wiki page name.
57 * $data[3]    rev: The page revision, false for current wiki pages.
58 *
59 * @author Ben Coburn <btcoburn@silicodon.net>
60 */
61function io_readWikiPage($file, $id, $rev=false) {
62    if (empty($rev)) { $rev = false; }
63    $data = array(array($file, false), getNS($id), noNS($id), $rev);
64    return trigger_event('IO_WIKIPAGE_READ', $data, '_io_readWikiPage_action', false);
65}
66
67/**
68 * Callback adapter for io_readFile().
69 * @author Ben Coburn <btcoburn@silicodon.net>
70 */
71function _io_readWikiPage_action($data) {
72    if (is_array($data) && is_array($data[0]) && count($data[0])===2) {
73        return call_user_func_array('io_readFile', $data[0]);
74    } else {
75        return ''; //callback error
76    }
77}
78
79/**
80 * Returns content of $file as cleaned string.
81 *
82 * Uses gzip if extension is .gz
83 *
84 * If you want to use the returned value in unserialize
85 * be sure to set $clean to false!
86 *
87 * @author  Andreas Gohr <andi@splitbrain.org>
88 */
89function io_readFile($file,$clean=true){
90  $ret = '';
91  if(@file_exists($file)){
92    if(substr($file,-3) == '.gz'){
93      $ret = join('',gzfile($file));
94    }else if(substr($file,-4) == '.bz2'){
95      $ret = bzfile($file);
96    }else{
97      $ret = join('',file($file));
98    }
99  }
100  if($clean){
101    return cleanText($ret);
102  }else{
103    return $ret;
104  }
105}
106/**
107* Returns the content of a .bz2 compressed file as string
108* @author marcel senf <marcel@rucksackreinigung.de>
109*/
110
111function bzfile($file){
112  $bz = bzopen($file,"r");
113  while (!feof($bz)){
114    //8192 seems to be the maximum buffersize?
115	  $str = $str . bzread($bz,8192);
116  }
117  bzclose($bz);
118  return $str;
119}
120
121
122/**
123 * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events.
124 *
125 * This generates an action event and delegates to io_saveFile().
126 * Action plugins are allowed to modify the page content in transit.
127 * The file path should not be changed.
128 * (The append parameter is set to false.)
129 *
130 * Event data:
131 * $data[0]    The raw arguments for io_saveFile as an array.
132 * $data[1]    ns: The colon separated namespace path minus the trailing page name. (false if root ns)
133 * $data[2]    page_name: The wiki page name.
134 * $data[3]    rev: The page revision, false for current wiki pages.
135 *
136 * @author Ben Coburn <btcoburn@silicodon.net>
137 */
138function io_writeWikiPage($file, $content, $id, $rev=false) {
139    if (empty($rev)) { $rev = false; }
140    if ($rev===false) { io_createNamespace($id); } // create namespaces as needed
141    $data = array(array($file, $content, false), getNS($id), noNS($id), $rev);
142    return trigger_event('IO_WIKIPAGE_WRITE', $data, '_io_writeWikiPage_action', false);
143}
144
145/**
146 * Callback adapter for io_saveFile().
147 * @author Ben Coburn <btcoburn@silicodon.net>
148 */
149function _io_writeWikiPage_action($data) {
150    if (is_array($data) && is_array($data[0]) && count($data[0])===3) {
151        return call_user_func_array('io_saveFile', $data[0]);
152    } else {
153        return false; //callback error
154    }
155}
156
157/**
158 * Saves $content to $file.
159 *
160 * If the third parameter is set to true the given content
161 * will be appended.
162 *
163 * Uses gzip if extension is .gz
164 * and bz2 if extension is .bz2
165 *
166 * @author  Andreas Gohr <andi@splitbrain.org>
167 * @return bool true on success
168 */
169function io_saveFile($file,$content,$append=false){
170  global $conf;
171  $mode = ($append) ? 'ab' : 'wb';
172
173  $fileexists = @file_exists($file);
174  io_makeFileDir($file);
175  io_lock($file);
176  if(substr($file,-3) == '.gz'){
177    $fh = @gzopen($file,$mode.'9');
178    if(!$fh){
179      msg("Writing $file failed",-1);
180      return false;
181    }
182    gzwrite($fh, $content);
183    gzclose($fh);
184  }else if(substr($file,-4) == '.bz2'){
185    $fh = @bzopen($file,$mode);
186    if(!$fh){
187      msg("Writing $file failed", -1);
188      return false;
189    }
190    bzwrite($fh, $content);
191    bzclose($fh);
192  }else{
193    $fh = @fopen($file,$mode);
194    if(!$fh){
195      msg("Writing $file failed",-1);
196      return false;
197    }
198    fwrite($fh, $content);
199    fclose($fh);
200  }
201
202  if(!$fileexists and !empty($conf['fperm'])) chmod($file, $conf['fperm']);
203  io_unlock($file);
204  return true;
205}
206
207/**
208 * Delete exact linematch for $badline from $file.
209 *
210 * Be sure to include the trailing newline in $badline
211 *
212 * Uses gzip if extension is .gz
213 *
214 * 2005-10-14 : added regex option -- Christopher Smith <chris@jalakai.co.uk>
215 *
216 * @author Steven Danz <steven-danz@kc.rr.com>
217 * @return bool true on success
218 */
219function io_deleteFromFile($file,$badline,$regex=false){
220  if (!@file_exists($file)) return true;
221
222  io_lock($file);
223
224  // load into array
225  if(substr($file,-3) == '.gz'){
226    $lines = gzfile($file);
227  }else{
228    $lines = file($file);
229  }
230
231  // remove all matching lines
232  if ($regex) {
233    $lines = preg_grep($badline,$lines,PREG_GREP_INVERT);
234  } else {
235    $pos = array_search($badline,$lines); //return null or false if not found
236    while(is_int($pos)){
237      unset($lines[$pos]);
238      $pos = array_search($badline,$lines);
239    }
240  }
241
242  if(count($lines)){
243    $content = join('',$lines);
244    if(substr($file,-3) == '.gz'){
245      $fh = @gzopen($file,'wb9');
246      if(!$fh){
247        msg("Removing content from $file failed",-1);
248        return false;
249      }
250      gzwrite($fh, $content);
251      gzclose($fh);
252    }else{
253      $fh = @fopen($file,'wb');
254      if(!$fh){
255        msg("Removing content from $file failed",-1);
256        return false;
257      }
258      fwrite($fh, $content);
259      fclose($fh);
260    }
261  }else{
262    @unlink($file);
263  }
264
265  io_unlock($file);
266  return true;
267}
268
269/**
270 * Tries to lock a file
271 *
272 * Locking is only done for io_savefile and uses directories
273 * inside $conf['lockdir']
274 *
275 * It waits maximal 3 seconds for the lock, after this time
276 * the lock is assumed to be stale and the function goes on
277 *
278 * @author Andreas Gohr <andi@splitbrain.org>
279 */
280function io_lock($file){
281  global $conf;
282  // no locking if safemode hack
283  if($conf['safemodehack']) return;
284
285  $lockDir = $conf['lockdir'].'/'.md5($file);
286  @ignore_user_abort(1);
287
288  $timeStart = time();
289  do {
290    //waited longer than 3 seconds? -> stale lock
291    if ((time() - $timeStart) > 3) break;
292    $locked = @mkdir($lockDir, $conf['dmode']);
293    if($locked){
294      if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']);
295      break;
296    }
297    usleep(50);
298  } while ($locked === false);
299}
300
301/**
302 * Unlocks a file
303 *
304 * @author Andreas Gohr <andi@splitbrain.org>
305 */
306function io_unlock($file){
307  global $conf;
308  // no locking if safemode hack
309  if($conf['safemodehack']) return;
310
311  $lockDir = $conf['lockdir'].'/'.md5($file);
312  @rmdir($lockDir);
313  @ignore_user_abort(0);
314}
315
316/**
317 * Create missing namespace directories and send the IO_NAMESPACE_CREATED events
318 * in the order of directory creation. (Parent directories first.)
319 *
320 * Event data:
321 * $data[0]    ns: The colon separated namespace path minus the trailing page name.
322 * $data[1]    ns_type: 'pages' or 'media' namespace tree.
323 *
324 * @author Ben Coburn <btcoburn@silicodon.net>
325 */
326function io_createNamespace($id, $ns_type='pages') {
327    // verify ns_type
328    $types = array('pages'=>'wikiFN', 'media'=>'mediaFN');
329    if (!isset($types[$ns_type])) {
330        trigger_error('Bad $ns_type parameter for io_createNamespace().');
331        return;
332    }
333    // make event list
334    $missing = array();
335    $ns_stack = explode(':', $id);
336    $ns = $id;
337    $tmp = dirname( $file = call_user_func($types[$ns_type], $ns) );
338    while (!@is_dir($tmp) && !(@file_exists($tmp) && !is_dir($tmp))) {
339        array_pop($ns_stack);
340        $ns = implode(':', $ns_stack);
341        if (strlen($ns)==0) { break; }
342        $missing[] = $ns;
343        $tmp = dirname(call_user_func($types[$ns_type], $ns));
344    }
345    // make directories
346    io_makeFileDir($file);
347    // send the events
348    $missing = array_reverse($missing); // inside out
349    foreach ($missing as $ns) {
350        $data = array($ns, $ns_type);
351        trigger_event('IO_NAMESPACE_CREATED', $data);
352    }
353}
354
355/**
356 * Create the directory needed for the given file
357 *
358 * @author  Andreas Gohr <andi@splitbrain.org>
359 */
360function io_makeFileDir($file){
361  global $conf;
362
363  $dir = dirname($file);
364  if(!@is_dir($dir)){
365    io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
366  }
367}
368
369/**
370 * Creates a directory hierachy.
371 *
372 * @link    http://www.php.net/manual/en/function.mkdir.php
373 * @author  <saint@corenova.com>
374 * @author  Andreas Gohr <andi@splitbrain.org>
375 */
376function io_mkdir_p($target){
377  global $conf;
378  if (@is_dir($target)||empty($target)) return 1; // best case check first
379  if (@file_exists($target) && !is_dir($target)) return 0;
380  //recursion
381  if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
382    if($conf['safemodehack']){
383      $dir = preg_replace('/^'.preg_quote(realpath($conf['ftp']['root']),'/').'/','', $target);
384      return io_mkdir_ftp($dir);
385    }else{
386      $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree
387      if($ret && $conf['dperm']) chmod($target, $conf['dperm']);
388      return $ret;
389    }
390  }
391  return 0;
392}
393
394/**
395 * Creates a directory using FTP
396 *
397 * This is used when the safemode workaround is enabled
398 *
399 * @author <andi@splitbrain.org>
400 */
401function io_mkdir_ftp($dir){
402  global $conf;
403
404  if(!function_exists('ftp_connect')){
405    msg("FTP support not found - safemode workaround not usable",-1);
406    return false;
407  }
408
409  $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10);
410  if(!$conn){
411    msg("FTP connection failed",-1);
412    return false;
413  }
414
415  if(!@ftp_login($conn, $conf['ftp']['user'], $conf['ftp']['pass'])){
416    msg("FTP login failed",-1);
417    return false;
418  }
419
420  //create directory
421  $ok = @ftp_mkdir($conn, $dir);
422  //set permissions
423  @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmode'],$dir));
424
425  @ftp_close($conn);
426  return $ok;
427}
428
429/**
430 * downloads a file from the net and saves it
431 *
432 * if $useAttachment is false,
433 * - $file is the full filename to save the file, incl. path
434 * - if successful will return true, false otherwise
435
436 * if $useAttachment is true,
437 * - $file is the directory where the file should be saved
438 * - if successful will return the name used for the saved file, false otherwise
439 *
440 * @author Andreas Gohr <andi@splitbrain.org>
441 * @author Chris Smith <chris@jalakai.co.uk>
442 */
443function io_download($url,$file,$useAttachment=false,$defaultName='',$maxSize=2097152){
444  global $conf;
445  $http = new DokuHTTPClient();
446  $http->max_bodysize = $maxSize;
447  $http->timeout = 25; //max. 25 sec
448
449  $data = $http->get($url);
450  if(!$data) return false;
451
452  if ($useAttachment) {
453    $name = '';
454      if (isset($http->resp_headers['content-disposition'])) {
455      $content_disposition = $http->resp_headers['content-disposition'];
456      $match=array();
457      if (is_string($content_disposition) &&
458          preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) {
459
460          $name = basename($match[1]);
461      }
462
463    }
464
465    if (!$name) {
466        if (!$defaultName) return false;
467        $name = $defaultName;
468    }
469
470    $file = $file.$name;
471  }
472
473  $fileexists = @file_exists($file);
474  $fp = @fopen($file,"w");
475  if(!$fp) return false;
476  fwrite($fp,$data);
477  fclose($fp);
478  if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
479  if ($useAttachment) return $name;
480  return true;
481}
482
483/**
484 * Windows compatible rename
485 *
486 * rename() can not overwrite existing files on Windows
487 * this function will use copy/unlink instead
488 */
489function io_rename($from,$to){
490  global $conf;
491  if(!@rename($from,$to)){
492    if(@copy($from,$to)){
493      if($conf['fperm']) chmod($to, $conf['fperm']);
494      @unlink($from);
495      return true;
496    }
497    return false;
498  }
499  return true;
500}
501
502
503/**
504 * Runs an external command and returns it's output as string
505 *
506 * @author Harry Brueckner <harry_b@eml.cc>
507 * @author Andreas Gohr <andi@splitbrain.org>
508 * @deprecated
509 */
510function io_runcmd($cmd){
511  $fh = popen($cmd, "r");
512  if(!$fh) return false;
513  $ret = '';
514  while (!feof($fh)) {
515    $ret .= fread($fh, 8192);
516  }
517  pclose($fh);
518  return $ret;
519}
520
521/**
522 * Search a file for matching lines
523 *
524 * This is probably not faster than file()+preg_grep() but less
525 * memory intensive because not the whole file needs to be loaded
526 * at once.
527 *
528 * @author Andreas Gohr <andi@splitbrain.org>
529 * @param  string $file    The file to search
530 * @param  string $pattern PCRE pattern
531 * @param  int    $max     How many lines to return (0 for all)
532 * @param  bool   $baxkref When true returns array with backreferences instead of lines
533 * @return matching lines or backref, false on error
534 */
535function io_grep($file,$pattern,$max=0,$backref=false){
536  $fh = @fopen($file,'r');
537  if(!$fh) return false;
538  $matches = array();
539
540  $cnt  = 0;
541  $line = '';
542  while (!feof($fh)) {
543    $line .= fgets($fh, 4096);  // read full line
544    if(substr($line,-1) != "\n") continue;
545
546    // check if line matches
547    if(preg_match($pattern,$line,$match)){
548      if($backref){
549        $matches[] = $match;
550      }else{
551        $matches[] = $line;
552      }
553      $cnt++;
554    }
555    if($max && $max == $cnt) break;
556    $line = '';
557  }
558  fclose($fh);
559  return $matches;
560}
561
562//Setup VIM: ex: et ts=2 enc=utf-8 :
563