summaryrefslogtreecommitdiff
path: root/inc/io.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/io.php')
-rw-r--r--inc/io.php525
1 files changed, 262 insertions, 263 deletions
diff --git a/inc/io.php b/inc/io.php
index 2eb94db0c..32a6f7b8e 100644
--- a/inc/io.php
+++ b/inc/io.php
@@ -28,26 +28,26 @@ require_once(DOKU_INC.'inc/utf8.php');
* @author Ben Coburn <btcoburn@silicodon.net>
*/
function io_sweepNS($id,$basedir='datadir'){
- global $conf;
- $types = array ('datadir'=>'pages', 'mediadir'=>'media');
- $ns_type = (isset($types[$basedir])?$types[$basedir]:false);
-
- $delone = false;
-
- //scan all namespaces
- while(($id = getNS($id)) !== false){
- $dir = $conf[$basedir].'/'.utf8_encodeFN(str_replace(':','/',$id));
-
- //try to delete dir else return
- if(@rmdir($dir)) {
- if ($ns_type!==false) {
- $data = array($id, $ns_type);
- $delone = true; // we deleted at least one dir
- trigger_event('IO_NAMESPACE_DELETED', $data);
- }
- } else { return $delone; }
- }
- return $delone;
+ global $conf;
+ $types = array ('datadir'=>'pages', 'mediadir'=>'media');
+ $ns_type = (isset($types[$basedir])?$types[$basedir]:false);
+
+ $delone = false;
+
+ //scan all namespaces
+ while(($id = getNS($id)) !== false){
+ $dir = $conf[$basedir].'/'.utf8_encodeFN(str_replace(':','/',$id));
+
+ //try to delete dir else return
+ if(@rmdir($dir)) {
+ if ($ns_type!==false) {
+ $data = array($id, $ns_type);
+ $delone = true; // we deleted at least one dir
+ trigger_event('IO_NAMESPACE_DELETED', $data);
+ }
+ } else { return $delone; }
+ }
+ return $delone;
}
/**
@@ -94,35 +94,35 @@ function _io_readWikiPage_action($data) {
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_readFile($file,$clean=true){
- $ret = '';
- if(@file_exists($file)){
- if(substr($file,-3) == '.gz'){
- $ret = join('',gzfile($file));
- }else if(substr($file,-4) == '.bz2'){
- $ret = bzfile($file);
+ $ret = '';
+ if(@file_exists($file)){
+ if(substr($file,-3) == '.gz'){
+ $ret = join('',gzfile($file));
+ }else if(substr($file,-4) == '.bz2'){
+ $ret = bzfile($file);
+ }else{
+ $ret = file_get_contents($file);
+ }
+ }
+ if($clean){
+ return cleanText($ret);
}else{
- $ret = file_get_contents($file);
+ return $ret;
}
- }
- if($clean){
- return cleanText($ret);
- }else{
- return $ret;
- }
}
/**
-* Returns the content of a .bz2 compressed file as string
-* @author marcel senf <marcel@rucksackreinigung.de>
-*/
+ * Returns the content of a .bz2 compressed file as string
+ * @author marcel senf <marcel@rucksackreinigung.de>
+ */
function bzfile($file){
- $bz = bzopen($file,"r");
- while (!feof($bz)){
- //8192 seems to be the maximum buffersize?
- $str = $str . bzread($bz,8192);
- }
- bzclose($bz);
- return $str;
+ $bz = bzopen($file,"r");
+ while (!feof($bz)){
+ //8192 seems to be the maximum buffersize?
+ $str = $str . bzread($bz,8192);
+ }
+ bzclose($bz);
+ return $str;
}
@@ -174,44 +174,44 @@ function _io_writeWikiPage_action($data) {
* @return bool true on success
*/
function io_saveFile($file,$content,$append=false){
- global $conf;
- $mode = ($append) ? 'ab' : 'wb';
-
- $fileexists = @file_exists($file);
- io_makeFileDir($file);
- io_lock($file);
- if(substr($file,-3) == '.gz'){
- $fh = @gzopen($file,$mode.'9');
- if(!$fh){
- msg("Writing $file failed",-1);
- io_unlock($file);
- return false;
- }
- gzwrite($fh, $content);
- gzclose($fh);
- }else if(substr($file,-4) == '.bz2'){
- $fh = @bzopen($file,$mode{0});
- if(!$fh){
- msg("Writing $file failed", -1);
- io_unlock($file);
- return false;
- }
- bzwrite($fh, $content);
- bzclose($fh);
- }else{
- $fh = @fopen($file,$mode);
- if(!$fh){
- msg("Writing $file failed",-1);
- io_unlock($file);
- return false;
+ global $conf;
+ $mode = ($append) ? 'ab' : 'wb';
+
+ $fileexists = @file_exists($file);
+ io_makeFileDir($file);
+ io_lock($file);
+ if(substr($file,-3) == '.gz'){
+ $fh = @gzopen($file,$mode.'9');
+ if(!$fh){
+ msg("Writing $file failed",-1);
+ io_unlock($file);
+ return false;
+ }
+ gzwrite($fh, $content);
+ gzclose($fh);
+ }else if(substr($file,-4) == '.bz2'){
+ $fh = @bzopen($file,$mode{0});
+ if(!$fh){
+ msg("Writing $file failed", -1);
+ io_unlock($file);
+ return false;
+ }
+ bzwrite($fh, $content);
+ bzclose($fh);
+ }else{
+ $fh = @fopen($file,$mode);
+ if(!$fh){
+ msg("Writing $file failed",-1);
+ io_unlock($file);
+ return false;
+ }
+ fwrite($fh, $content);
+ fclose($fh);
}
- fwrite($fh, $content);
- fclose($fh);
- }
- if(!$fileexists and !empty($conf['fperm'])) chmod($file, $conf['fperm']);
- io_unlock($file);
- return true;
+ if(!$fileexists and !empty($conf['fperm'])) chmod($file, $conf['fperm']);
+ io_unlock($file);
+ return true;
}
/**
@@ -227,55 +227,55 @@ function io_saveFile($file,$content,$append=false){
* @return bool true on success
*/
function io_deleteFromFile($file,$badline,$regex=false){
- if (!@file_exists($file)) return true;
-
- io_lock($file);
-
- // load into array
- if(substr($file,-3) == '.gz'){
- $lines = gzfile($file);
- }else{
- $lines = file($file);
- }
-
- // remove all matching lines
- if ($regex) {
- $lines = preg_grep($badline,$lines,PREG_GREP_INVERT);
- } else {
- $pos = array_search($badline,$lines); //return null or false if not found
- while(is_int($pos)){
- unset($lines[$pos]);
- $pos = array_search($badline,$lines);
- }
- }
+ if (!@file_exists($file)) return true;
+
+ io_lock($file);
- if(count($lines)){
- $content = join('',$lines);
+ // load into array
if(substr($file,-3) == '.gz'){
- $fh = @gzopen($file,'wb9');
- if(!$fh){
- msg("Removing content from $file failed",-1);
- io_unlock($file);
- return false;
- }
- gzwrite($fh, $content);
- gzclose($fh);
+ $lines = gzfile($file);
}else{
- $fh = @fopen($file,'wb');
- if(!$fh){
- msg("Removing content from $file failed",-1);
- io_unlock($file);
- return false;
- }
- fwrite($fh, $content);
- fclose($fh);
+ $lines = file($file);
}
- }else{
- @unlink($file);
- }
- io_unlock($file);
- return true;
+ // remove all matching lines
+ if ($regex) {
+ $lines = preg_grep($badline,$lines,PREG_GREP_INVERT);
+ } else {
+ $pos = array_search($badline,$lines); //return null or false if not found
+ while(is_int($pos)){
+ unset($lines[$pos]);
+ $pos = array_search($badline,$lines);
+ }
+ }
+
+ if(count($lines)){
+ $content = join('',$lines);
+ if(substr($file,-3) == '.gz'){
+ $fh = @gzopen($file,'wb9');
+ if(!$fh){
+ msg("Removing content from $file failed",-1);
+ io_unlock($file);
+ return false;
+ }
+ gzwrite($fh, $content);
+ gzclose($fh);
+ }else{
+ $fh = @fopen($file,'wb');
+ if(!$fh){
+ msg("Removing content from $file failed",-1);
+ io_unlock($file);
+ return false;
+ }
+ fwrite($fh, $content);
+ fclose($fh);
+ }
+ }else{
+ @unlink($file);
+ }
+
+ io_unlock($file);
+ return true;
}
/**
@@ -290,24 +290,24 @@ function io_deleteFromFile($file,$badline,$regex=false){
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_lock($file){
- global $conf;
- // no locking if safemode hack
- if($conf['safemodehack']) return;
-
- $lockDir = $conf['lockdir'].'/'.md5($file);
- @ignore_user_abort(1);
-
- $timeStart = time();
- do {
- //waited longer than 3 seconds? -> stale lock
- if ((time() - $timeStart) > 3) break;
- $locked = @mkdir($lockDir, $conf['dmode']);
- if($locked){
- if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']);
- break;
- }
- usleep(50);
- } while ($locked === false);
+ global $conf;
+ // no locking if safemode hack
+ if($conf['safemodehack']) return;
+
+ $lockDir = $conf['lockdir'].'/'.md5($file);
+ @ignore_user_abort(1);
+
+ $timeStart = time();
+ do {
+ //waited longer than 3 seconds? -> stale lock
+ if ((time() - $timeStart) > 3) break;
+ $locked = @mkdir($lockDir, $conf['dmode']);
+ if($locked){
+ if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']);
+ break;
+ }
+ usleep(50);
+ } while ($locked === false);
}
/**
@@ -316,13 +316,13 @@ function io_lock($file){
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_unlock($file){
- global $conf;
- // no locking if safemode hack
- if($conf['safemodehack']) return;
+ global $conf;
+ // no locking if safemode hack
+ if($conf['safemodehack']) return;
- $lockDir = $conf['lockdir'].'/'.md5($file);
- @rmdir($lockDir);
- @ignore_user_abort(0);
+ $lockDir = $conf['lockdir'].'/'.md5($file);
+ @rmdir($lockDir);
+ @ignore_user_abort(0);
}
/**
@@ -370,12 +370,12 @@ function io_createNamespace($id, $ns_type='pages') {
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_makeFileDir($file){
- global $conf;
+ global $conf;
- $dir = dirname($file);
- if(!@is_dir($dir)){
- io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
- }
+ $dir = dirname($file);
+ if(!@is_dir($dir)){
+ io_mkdir_p($dir) || msg("Creating directory $dir failed",-1);
+ }
}
/**
@@ -386,21 +386,21 @@ function io_makeFileDir($file){
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_mkdir_p($target){
- global $conf;
- if (@is_dir($target)||empty($target)) return 1; // best case check first
- if (@file_exists($target) && !is_dir($target)) return 0;
- //recursion
- if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
- if($conf['safemodehack']){
- $dir = preg_replace('/^'.preg_quote(fullpath($conf['ftp']['root']),'/').'/','', $target);
- return io_mkdir_ftp($dir);
- }else{
- $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree
- if($ret && $conf['dperm']) chmod($target, $conf['dperm']);
- return $ret;
+ global $conf;
+ if (@is_dir($target)||empty($target)) return 1; // best case check first
+ if (@file_exists($target) && !is_dir($target)) return 0;
+ //recursion
+ if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){
+ if($conf['safemodehack']){
+ $dir = preg_replace('/^'.preg_quote(fullpath($conf['ftp']['root']),'/').'/','', $target);
+ return io_mkdir_ftp($dir);
+ }else{
+ $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree
+ if($ret && $conf['dperm']) chmod($target, $conf['dperm']);
+ return $ret;
+ }
}
- }
- return 0;
+ return 0;
}
/**
@@ -411,31 +411,31 @@ function io_mkdir_p($target){
* @author <andi@splitbrain.org>
*/
function io_mkdir_ftp($dir){
- global $conf;
-
- if(!function_exists('ftp_connect')){
- msg("FTP support not found - safemode workaround not usable",-1);
- return false;
- }
-
- $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10);
- if(!$conn){
- msg("FTP connection failed",-1);
- return false;
- }
-
- if(!@ftp_login($conn, $conf['ftp']['user'], conf_decodeString($conf['ftp']['pass']))){
- msg("FTP login failed",-1);
- return false;
- }
-
- //create directory
- $ok = @ftp_mkdir($conn, $dir);
- //set permissions
- @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmode'],$dir));
-
- @ftp_close($conn);
- return $ok;
+ global $conf;
+
+ if(!function_exists('ftp_connect')){
+ msg("FTP support not found - safemode workaround not usable",-1);
+ return false;
+ }
+
+ $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10);
+ if(!$conn){
+ msg("FTP connection failed",-1);
+ return false;
+ }
+
+ if(!@ftp_login($conn, $conf['ftp']['user'], conf_decodeString($conf['ftp']['pass']))){
+ msg("FTP login failed",-1);
+ return false;
+ }
+
+ //create directory
+ $ok = @ftp_mkdir($conn, $dir);
+ //set permissions
+ @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmode'],$dir));
+
+ @ftp_close($conn);
+ return $ok;
}
/**
@@ -464,7 +464,7 @@ function io_mktmpdir() {
* if $useAttachment is false,
* - $file is the full filename to save the file, incl. path
* - if successful will return true, false otherwise
-
+ *
* if $useAttachment is true,
* - $file is the directory where the file should be saved
* - if successful will return the name used for the saved file, false otherwise
@@ -473,43 +473,43 @@ function io_mktmpdir() {
* @author Chris Smith <chris@jalakai.co.uk>
*/
function io_download($url,$file,$useAttachment=false,$defaultName='',$maxSize=2097152){
- global $conf;
- $http = new DokuHTTPClient();
- $http->max_bodysize = $maxSize;
- $http->timeout = 25; //max. 25 sec
+ global $conf;
+ $http = new DokuHTTPClient();
+ $http->max_bodysize = $maxSize;
+ $http->timeout = 25; //max. 25 sec
- $data = $http->get($url);
- if(!$data) return false;
+ $data = $http->get($url);
+ if(!$data) return false;
- if ($useAttachment) {
- $name = '';
- if (isset($http->resp_headers['content-disposition'])) {
- $content_disposition = $http->resp_headers['content-disposition'];
- $match=array();
- if (is_string($content_disposition) &&
- preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) {
+ if ($useAttachment) {
+ $name = '';
+ if (isset($http->resp_headers['content-disposition'])) {
+ $content_disposition = $http->resp_headers['content-disposition'];
+ $match=array();
+ if (is_string($content_disposition) &&
+ preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) {
- $name = basename($match[1]);
- }
+ $name = basename($match[1]);
+ }
- }
+ }
+
+ if (!$name) {
+ if (!$defaultName) return false;
+ $name = $defaultName;
+ }
- if (!$name) {
- if (!$defaultName) return false;
- $name = $defaultName;
+ $file = $file.$name;
}
- $file = $file.$name;
- }
-
- $fileexists = @file_exists($file);
- $fp = @fopen($file,"w");
- if(!$fp) return false;
- fwrite($fp,$data);
- fclose($fp);
- if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
- if ($useAttachment) return $name;
- return true;
+ $fileexists = @file_exists($file);
+ $fp = @fopen($file,"w");
+ if(!$fp) return false;
+ fwrite($fp,$data);
+ fclose($fp);
+ if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
+ if ($useAttachment) return $name;
+ return true;
}
/**
@@ -519,16 +519,16 @@ function io_download($url,$file,$useAttachment=false,$defaultName='',$maxSize=20
* this function will use copy/unlink instead
*/
function io_rename($from,$to){
- global $conf;
- if(!@rename($from,$to)){
- if(@copy($from,$to)){
- if($conf['fperm']) chmod($to, $conf['fperm']);
- @unlink($from);
- return true;
+ global $conf;
+ if(!@rename($from,$to)){
+ if(@copy($from,$to)){
+ if($conf['fperm']) chmod($to, $conf['fperm']);
+ @unlink($from);
+ return true;
+ }
+ return false;
}
- return false;
- }
- return true;
+ return true;
}
@@ -540,14 +540,14 @@ function io_rename($from,$to){
* @deprecated
*/
function io_runcmd($cmd){
- $fh = popen($cmd, "r");
- if(!$fh) return false;
- $ret = '';
- while (!feof($fh)) {
- $ret .= fread($fh, 8192);
- }
- pclose($fh);
- return $ret;
+ $fh = popen($cmd, "r");
+ if(!$fh) return false;
+ $ret = '';
+ while (!feof($fh)) {
+ $ret .= fread($fh, 8192);
+ }
+ pclose($fh);
+ return $ret;
}
/**
@@ -565,30 +565,29 @@ function io_runcmd($cmd){
* @return matching lines or backref, false on error
*/
function io_grep($file,$pattern,$max=0,$backref=false){
- $fh = @fopen($file,'r');
- if(!$fh) return false;
- $matches = array();
-
- $cnt = 0;
- $line = '';
- while (!feof($fh)) {
- $line .= fgets($fh, 4096); // read full line
- if(substr($line,-1) != "\n") continue;
-
- // check if line matches
- if(preg_match($pattern,$line,$match)){
- if($backref){
- $matches[] = $match;
- }else{
- $matches[] = $line;
- }
- $cnt++;
- }
- if($max && $max == $cnt) break;
+ $fh = @fopen($file,'r');
+ if(!$fh) return false;
+ $matches = array();
+
+ $cnt = 0;
$line = '';
- }
- fclose($fh);
- return $matches;
+ while (!feof($fh)) {
+ $line .= fgets($fh, 4096); // read full line
+ if(substr($line,-1) != "\n") continue;
+
+ // check if line matches
+ if(preg_match($pattern,$line,$match)){
+ if($backref){
+ $matches[] = $match;
+ }else{
+ $matches[] = $line;
+ }
+ $cnt++;
+ }
+ if($max && $max == $cnt) break;
+ $line = '';
+ }
+ fclose($fh);
+ return $matches;
}
-//Setup VIM: ex: et ts=2 enc=utf-8 :