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 9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 10f3f0262cSandi 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 * 1953d6ccfeSandi * @todo use safemode hack 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 */ 27755f1e03SAndreas Gohrfunction io_sweepNS($id,$basedir='datadir'){ 2853d6ccfeSandi global $conf; 29cc7d0c94SBen Coburn $types = array ('datadir'=>'pages', 'mediadir'=>'media'); 30cc7d0c94SBen Coburn $ns_type = (isset($types[$basedir])?$types[$basedir]:false); 3153d6ccfeSandi 32d186898bSAndreas Gohr $delone = false; 33d186898bSAndreas Gohr 3453d6ccfeSandi //scan all namespaces 3553d6ccfeSandi while(($id = getNS($id)) !== false){ 36755f1e03SAndreas Gohr $dir = $conf[$basedir].'/'.utf8_encodeFN(str_replace(':','/',$id)); 3753d6ccfeSandi 3853d6ccfeSandi //try to delete dir else return 39cc7d0c94SBen Coburn if(@rmdir($dir)) { 40cc7d0c94SBen Coburn if ($ns_type!==false) { 41cc7d0c94SBen Coburn $data = array($id, $ns_type); 42d186898bSAndreas Gohr $delone = true; // we deleted at least one dir 43cc7d0c94SBen Coburn trigger_event('IO_NAMESPACE_DELETED', $data); 44cc7d0c94SBen Coburn } 45d186898bSAndreas Gohr } else { return $delone; } 46cc7d0c94SBen Coburn } 47d186898bSAndreas Gohr return $delone; 48cc7d0c94SBen Coburn} 49cc7d0c94SBen Coburn 50cc7d0c94SBen Coburn/** 51cc7d0c94SBen Coburn * Used to read in a DokuWiki page from file, and send IO_WIKIPAGE_READ events. 52cc7d0c94SBen Coburn * 53cc7d0c94SBen Coburn * Generates the action event which delegates to io_readFile(). 54cc7d0c94SBen Coburn * Action plugins are allowed to modify the page content in transit. 55cc7d0c94SBen Coburn * The file path should not be changed. 56cc7d0c94SBen Coburn * 57cc7d0c94SBen Coburn * Event data: 58cc7d0c94SBen Coburn * $data[0] The raw arguments for io_readFile as an array. 59cc7d0c94SBen Coburn * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns) 60cc7d0c94SBen Coburn * $data[2] page_name: The wiki page name. 61cc7d0c94SBen Coburn * $data[3] rev: The page revision, false for current wiki pages. 62cc7d0c94SBen Coburn * 63cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 6442ea7f44SGerrit Uitslag * 6542ea7f44SGerrit Uitslag * @param string $file filename 6642ea7f44SGerrit Uitslag * @param string $id page id 6742ea7f44SGerrit Uitslag * @param bool|int $rev revision timestamp 6842ea7f44SGerrit Uitslag * @return string 69cc7d0c94SBen Coburn */ 70cc7d0c94SBen Coburnfunction io_readWikiPage($file, $id, $rev=false) { 71cc7d0c94SBen Coburn if (empty($rev)) { $rev = false; } 727651b376SAndreas Gohr $data = array(array($file, true), getNS($id), noNS($id), $rev); 73cc7d0c94SBen Coburn return trigger_event('IO_WIKIPAGE_READ', $data, '_io_readWikiPage_action', false); 74cc7d0c94SBen Coburn} 75cc7d0c94SBen Coburn 76cc7d0c94SBen Coburn/** 77cc7d0c94SBen Coburn * Callback adapter for io_readFile(). 7842ea7f44SGerrit Uitslag * 79cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 8042ea7f44SGerrit Uitslag * 8142ea7f44SGerrit Uitslag * @param array $data event data 8242ea7f44SGerrit Uitslag * @return string 83cc7d0c94SBen Coburn */ 84cc7d0c94SBen Coburnfunction _io_readWikiPage_action($data) { 85cc7d0c94SBen Coburn if (is_array($data) && is_array($data[0]) && count($data[0])===2) { 86cc7d0c94SBen Coburn return call_user_func_array('io_readFile', $data[0]); 87cc7d0c94SBen Coburn } else { 88cc7d0c94SBen Coburn return ''; //callback error 8953d6ccfeSandi } 9053d6ccfeSandi} 9153d6ccfeSandi 9253d6ccfeSandi/** 9315fae107Sandi * Returns content of $file as cleaned string. 9415fae107Sandi * 9515fae107Sandi * Uses gzip if extension is .gz 9615fae107Sandi * 97ee4c4a1bSAndreas Gohr * If you want to use the returned value in unserialize 98ee4c4a1bSAndreas Gohr * be sure to set $clean to false! 99ee4c4a1bSAndreas Gohr * 10015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 10142ea7f44SGerrit Uitslag * 10242ea7f44SGerrit Uitslag * @param string $file filename 10342ea7f44SGerrit Uitslag * @param bool $clean 104*d387bf5eSAndreas Gohr * @return string|bool the file contents or false on error 105f3f0262cSandi */ 106e34c0709SAndreas Gohrfunction io_readFile($file,$clean=true){ 107f3f0262cSandi $ret = ''; 10879e79377SAndreas Gohr if(file_exists($file)){ 109f3f0262cSandi if(substr($file,-3) == '.gz'){ 110f3f0262cSandi $ret = join('',gzfile($file)); 111ff3ed99fSmarcel }else if(substr($file,-4) == '.bz2'){ 112ff3ed99fSmarcel $ret = bzfile($file); 113f3f0262cSandi }else{ 11443078d10SAndreas Gohr $ret = file_get_contents($file); 115f3f0262cSandi } 116f3f0262cSandi } 117*d387bf5eSAndreas Gohr if($ret !== false && $clean){ 118f3f0262cSandi return cleanText($ret); 119e34c0709SAndreas Gohr }else{ 120e34c0709SAndreas Gohr return $ret; 121e34c0709SAndreas Gohr } 122f3f0262cSandi} 123ff3ed99fSmarcel/** 124ff3ed99fSmarcel * Returns the content of a .bz2 compressed file as string 12542ea7f44SGerrit Uitslag * 126ff3ed99fSmarcel * @author marcel senf <marcel@rucksackreinigung.de> 127*d387bf5eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12842ea7f44SGerrit Uitslag * 12942ea7f44SGerrit Uitslag * @param string $file filename 130*d387bf5eSAndreas Gohr * @return string|bool content or false on error 131ff3ed99fSmarcel */ 132ff3ed99fSmarcelfunction bzfile($file){ 133ff3ed99fSmarcel $bz = bzopen($file,"r"); 134*d387bf5eSAndreas Gohr if($bz === false) return false; 135*d387bf5eSAndreas Gohr 136cd2f903bSMichael Hamann $str = ''; 137ff3ed99fSmarcel while (!feof($bz)){ 138ff3ed99fSmarcel //8192 seems to be the maximum buffersize? 139*d387bf5eSAndreas Gohr $buffer = bzread($bz,8192); 140*d387bf5eSAndreas Gohr if(($buffer === false) || (bzerrno($bz) !== 0)) { 141*d387bf5eSAndreas Gohr return false; 142*d387bf5eSAndreas Gohr } 143*d387bf5eSAndreas Gohr $str = $str . $buffer; 144ff3ed99fSmarcel } 145ff3ed99fSmarcel bzclose($bz); 146ff3ed99fSmarcel return $str; 147ff3ed99fSmarcel} 148ff3ed99fSmarcel 149f3f0262cSandi/** 150cc7d0c94SBen Coburn * Used to write out a DokuWiki page to file, and send IO_WIKIPAGE_WRITE events. 151cc7d0c94SBen Coburn * 152cc7d0c94SBen Coburn * This generates an action event and delegates to io_saveFile(). 153cc7d0c94SBen Coburn * Action plugins are allowed to modify the page content in transit. 154cc7d0c94SBen Coburn * The file path should not be changed. 155cc7d0c94SBen Coburn * (The append parameter is set to false.) 156cc7d0c94SBen Coburn * 157cc7d0c94SBen Coburn * Event data: 158cc7d0c94SBen Coburn * $data[0] The raw arguments for io_saveFile as an array. 159cc7d0c94SBen Coburn * $data[1] ns: The colon separated namespace path minus the trailing page name. (false if root ns) 160cc7d0c94SBen Coburn * $data[2] page_name: The wiki page name. 161cc7d0c94SBen Coburn * $data[3] rev: The page revision, false for current wiki pages. 162cc7d0c94SBen Coburn * 163cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 16442ea7f44SGerrit Uitslag * 16542ea7f44SGerrit Uitslag * @param string $file filename 16642ea7f44SGerrit Uitslag * @param string $content 16742ea7f44SGerrit Uitslag * @param string $id page id 16842ea7f44SGerrit Uitslag * @param int|bool $rev timestamp of revision 16942ea7f44SGerrit Uitslag * @return bool 170cc7d0c94SBen Coburn */ 171cc7d0c94SBen Coburnfunction io_writeWikiPage($file, $content, $id, $rev=false) { 172cc7d0c94SBen Coburn if (empty($rev)) { $rev = false; } 173cc7d0c94SBen Coburn if ($rev===false) { io_createNamespace($id); } // create namespaces as needed 174cc7d0c94SBen Coburn $data = array(array($file, $content, false), getNS($id), noNS($id), $rev); 175cc7d0c94SBen Coburn return trigger_event('IO_WIKIPAGE_WRITE', $data, '_io_writeWikiPage_action', false); 176cc7d0c94SBen Coburn} 177cc7d0c94SBen Coburn 178cc7d0c94SBen Coburn/** 179cc7d0c94SBen Coburn * Callback adapter for io_saveFile(). 180cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 18142ea7f44SGerrit Uitslag * 18242ea7f44SGerrit Uitslag * @param array $data event data 18342ea7f44SGerrit Uitslag * @return bool 184cc7d0c94SBen Coburn */ 185cc7d0c94SBen Coburnfunction _io_writeWikiPage_action($data) { 186cc7d0c94SBen Coburn if (is_array($data) && is_array($data[0]) && count($data[0])===3) { 187cc7d0c94SBen Coburn return call_user_func_array('io_saveFile', $data[0]); 188cc7d0c94SBen Coburn } else { 189cc7d0c94SBen Coburn return false; //callback error 190cc7d0c94SBen Coburn } 191cc7d0c94SBen Coburn} 192cc7d0c94SBen Coburn 193cc7d0c94SBen Coburn/** 19415fae107Sandi * Saves $content to $file. 195f3f0262cSandi * 1961380fc45SAndreas Gohr * If the third parameter is set to true the given content 1971380fc45SAndreas Gohr * will be appended. 1981380fc45SAndreas Gohr * 19915fae107Sandi * Uses gzip if extension is .gz 200ff3ed99fSmarcel * and bz2 if extension is .bz2 20115fae107Sandi * 20215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 20342ea7f44SGerrit Uitslag * 20442ea7f44SGerrit Uitslag * @param string $file filename path to file 20542ea7f44SGerrit Uitslag * @param string $content 20642ea7f44SGerrit Uitslag * @param bool $append 20742ea7f44SGerrit Uitslag * @return bool true on success, otherwise false 208f3f0262cSandi */ 2091380fc45SAndreas Gohrfunction io_saveFile($file,$content,$append=false){ 210ac9115b0STroels Liebe Bentsen global $conf; 2111380fc45SAndreas Gohr $mode = ($append) ? 'ab' : 'wb'; 2121380fc45SAndreas Gohr 21379e79377SAndreas Gohr $fileexists = file_exists($file); 214f3f0262cSandi io_makeFileDir($file); 21590eb8392Sandi io_lock($file); 216f3f0262cSandi if(substr($file,-3) == '.gz'){ 2171380fc45SAndreas Gohr $fh = @gzopen($file,$mode.'9'); 218f3f0262cSandi if(!$fh){ 219f3f0262cSandi msg("Writing $file failed",-1); 220fb7125eeSAndreas Gohr io_unlock($file); 221f3f0262cSandi return false; 222f3f0262cSandi } 223f3f0262cSandi gzwrite($fh, $content); 224f3f0262cSandi gzclose($fh); 225ff3ed99fSmarcel }else if(substr($file,-4) == '.bz2'){ 226ece639c7SAndreas Gohr $fh = @bzopen($file,$mode{0}); 227ff3ed99fSmarcel if(!$fh){ 228ff3ed99fSmarcel msg("Writing $file failed", -1); 229fb7125eeSAndreas Gohr io_unlock($file); 230ff3ed99fSmarcel return false; 231ff3ed99fSmarcel } 232ff3ed99fSmarcel bzwrite($fh, $content); 233ff3ed99fSmarcel bzclose($fh); 234f3f0262cSandi }else{ 2351380fc45SAndreas Gohr $fh = @fopen($file,$mode); 236f3f0262cSandi if(!$fh){ 237f3f0262cSandi msg("Writing $file failed",-1); 238fb7125eeSAndreas Gohr io_unlock($file); 239f3f0262cSandi return false; 240f3f0262cSandi } 241f3f0262cSandi fwrite($fh, $content); 242f3f0262cSandi fclose($fh); 243f3f0262cSandi } 244ac9115b0STroels Liebe Bentsen 245bb4866bdSchris if(!$fileexists and !empty($conf['fperm'])) chmod($file, $conf['fperm']); 24690eb8392Sandi io_unlock($file); 247f3f0262cSandi return true; 248f3f0262cSandi} 249f3f0262cSandi 250f3f0262cSandi/** 2511380fc45SAndreas Gohr * Delete exact linematch for $badline from $file. 2521380fc45SAndreas Gohr * 2531380fc45SAndreas Gohr * Be sure to include the trailing newline in $badline 254b158d625SSteven Danz * 255b158d625SSteven Danz * Uses gzip if extension is .gz 256b158d625SSteven Danz * 2578b06d178Schris * 2005-10-14 : added regex option -- Christopher Smith <chris@jalakai.co.uk> 2588b06d178Schris * 259b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 26042ea7f44SGerrit Uitslag * 26142ea7f44SGerrit Uitslag * @param string $file filename 26242ea7f44SGerrit Uitslag * @param string $badline exact linematch to remove 26342ea7f44SGerrit Uitslag * @param bool $regex use regexp? 264b158d625SSteven Danz * @return bool true on success 265b158d625SSteven Danz */ 2668b06d178Schrisfunction io_deleteFromFile($file,$badline,$regex=false){ 26779e79377SAndreas Gohr if (!file_exists($file)) return true; 2681380fc45SAndreas Gohr 269b158d625SSteven Danz io_lock($file); 2701380fc45SAndreas Gohr 2711380fc45SAndreas Gohr // load into array 272b158d625SSteven Danz if(substr($file,-3) == '.gz'){ 2731380fc45SAndreas Gohr $lines = gzfile($file); 274b158d625SSteven Danz }else{ 2751380fc45SAndreas Gohr $lines = file($file); 276b158d625SSteven Danz } 277b158d625SSteven Danz 2781380fc45SAndreas Gohr // remove all matching lines 2798b06d178Schris if ($regex) { 2808b06d178Schris $lines = preg_grep($badline,$lines,PREG_GREP_INVERT); 2818b06d178Schris } else { 2821380fc45SAndreas Gohr $pos = array_search($badline,$lines); //return null or false if not found 2831380fc45SAndreas Gohr while(is_int($pos)){ 2841380fc45SAndreas Gohr unset($lines[$pos]); 2851380fc45SAndreas Gohr $pos = array_search($badline,$lines); 286b158d625SSteven Danz } 2878b06d178Schris } 288b158d625SSteven Danz 2891380fc45SAndreas Gohr if(count($lines)){ 2901380fc45SAndreas Gohr $content = join('',$lines); 291b158d625SSteven Danz if(substr($file,-3) == '.gz'){ 292b158d625SSteven Danz $fh = @gzopen($file,'wb9'); 293b158d625SSteven Danz if(!$fh){ 294b158d625SSteven Danz msg("Removing content from $file failed",-1); 295fb7125eeSAndreas Gohr io_unlock($file); 296b158d625SSteven Danz return false; 297b158d625SSteven Danz } 298b158d625SSteven Danz gzwrite($fh, $content); 299b158d625SSteven Danz gzclose($fh); 300b158d625SSteven Danz }else{ 301b158d625SSteven Danz $fh = @fopen($file,'wb'); 302b158d625SSteven Danz if(!$fh){ 303b158d625SSteven Danz msg("Removing content from $file failed",-1); 304fb7125eeSAndreas Gohr io_unlock($file); 305b158d625SSteven Danz return false; 306b158d625SSteven Danz } 307b158d625SSteven Danz fwrite($fh, $content); 308b158d625SSteven Danz fclose($fh); 309b158d625SSteven Danz } 310b158d625SSteven Danz }else{ 311b158d625SSteven Danz @unlink($file); 312b158d625SSteven Danz } 313b158d625SSteven Danz 314b158d625SSteven Danz io_unlock($file); 315b158d625SSteven Danz return true; 316b158d625SSteven Danz} 317b158d625SSteven Danz 318b158d625SSteven Danz/** 31990eb8392Sandi * Tries to lock a file 32090eb8392Sandi * 32190eb8392Sandi * Locking is only done for io_savefile and uses directories 32290eb8392Sandi * inside $conf['lockdir'] 32390eb8392Sandi * 32490eb8392Sandi * It waits maximal 3 seconds for the lock, after this time 32590eb8392Sandi * the lock is assumed to be stale and the function goes on 32690eb8392Sandi * 32790eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org> 32842ea7f44SGerrit Uitslag * 32942ea7f44SGerrit Uitslag * @param string $file filename 33090eb8392Sandi */ 33190eb8392Sandifunction io_lock($file){ 33290eb8392Sandi global $conf; 33390eb8392Sandi // no locking if safemode hack 33490eb8392Sandi if($conf['safemodehack']) return; 33590eb8392Sandi 33690eb8392Sandi $lockDir = $conf['lockdir'].'/'.md5($file); 33790eb8392Sandi @ignore_user_abort(1); 33890eb8392Sandi 33990eb8392Sandi $timeStart = time(); 34090eb8392Sandi do { 34190eb8392Sandi //waited longer than 3 seconds? -> stale lock 34290eb8392Sandi if ((time() - $timeStart) > 3) break; 34344881d27STroels Liebe Bentsen $locked = @mkdir($lockDir, $conf['dmode']); 34477b98903SAndreas Gohr if($locked){ 345bb4866bdSchris if(!empty($conf['dperm'])) chmod($lockDir, $conf['dperm']); 34677b98903SAndreas Gohr break; 34777b98903SAndreas Gohr } 34877b98903SAndreas Gohr usleep(50); 34990eb8392Sandi } while ($locked === false); 35090eb8392Sandi} 35190eb8392Sandi 35290eb8392Sandi/** 35390eb8392Sandi * Unlocks a file 35490eb8392Sandi * 35590eb8392Sandi * @author Andreas Gohr <andi@splitbrain.org> 35642ea7f44SGerrit Uitslag * 35742ea7f44SGerrit Uitslag * @param string $file filename 35890eb8392Sandi */ 35990eb8392Sandifunction io_unlock($file){ 36090eb8392Sandi global $conf; 36190eb8392Sandi // no locking if safemode hack 36290eb8392Sandi if($conf['safemodehack']) return; 36390eb8392Sandi 36490eb8392Sandi $lockDir = $conf['lockdir'].'/'.md5($file); 36590eb8392Sandi @rmdir($lockDir); 36690eb8392Sandi @ignore_user_abort(0); 36790eb8392Sandi} 36890eb8392Sandi 36990eb8392Sandi/** 370cc7d0c94SBen Coburn * Create missing namespace directories and send the IO_NAMESPACE_CREATED events 371cc7d0c94SBen Coburn * in the order of directory creation. (Parent directories first.) 372cc7d0c94SBen Coburn * 373cc7d0c94SBen Coburn * Event data: 374cc7d0c94SBen Coburn * $data[0] ns: The colon separated namespace path minus the trailing page name. 375cc7d0c94SBen Coburn * $data[1] ns_type: 'pages' or 'media' namespace tree. 376cc7d0c94SBen Coburn * 377cc7d0c94SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 37842ea7f44SGerrit Uitslag * 37942ea7f44SGerrit Uitslag * @param string $id page id 38042ea7f44SGerrit Uitslag * @param string $ns_type 'pages' or 'media' 381cc7d0c94SBen Coburn */ 382cc7d0c94SBen Coburnfunction io_createNamespace($id, $ns_type='pages') { 383cc7d0c94SBen Coburn // verify ns_type 384cc7d0c94SBen Coburn $types = array('pages'=>'wikiFN', 'media'=>'mediaFN'); 385cc7d0c94SBen Coburn if (!isset($types[$ns_type])) { 386cc7d0c94SBen Coburn trigger_error('Bad $ns_type parameter for io_createNamespace().'); 387cc7d0c94SBen Coburn return; 388cc7d0c94SBen Coburn } 389cc7d0c94SBen Coburn // make event list 390cc7d0c94SBen Coburn $missing = array(); 391cc7d0c94SBen Coburn $ns_stack = explode(':', $id); 392cc7d0c94SBen Coburn $ns = $id; 393cc7d0c94SBen Coburn $tmp = dirname( $file = call_user_func($types[$ns_type], $ns) ); 39479e79377SAndreas Gohr while (!@is_dir($tmp) && !(file_exists($tmp) && !is_dir($tmp))) { 395cc7d0c94SBen Coburn array_pop($ns_stack); 396cc7d0c94SBen Coburn $ns = implode(':', $ns_stack); 397cc7d0c94SBen Coburn if (strlen($ns)==0) { break; } 398cc7d0c94SBen Coburn $missing[] = $ns; 399cc7d0c94SBen Coburn $tmp = dirname(call_user_func($types[$ns_type], $ns)); 400cc7d0c94SBen Coburn } 401cc7d0c94SBen Coburn // make directories 402cc7d0c94SBen Coburn io_makeFileDir($file); 403cc7d0c94SBen Coburn // send the events 404cc7d0c94SBen Coburn $missing = array_reverse($missing); // inside out 405cc7d0c94SBen Coburn foreach ($missing as $ns) { 406cc7d0c94SBen Coburn $data = array($ns, $ns_type); 407cc7d0c94SBen Coburn trigger_event('IO_NAMESPACE_CREATED', $data); 408cc7d0c94SBen Coburn } 409cc7d0c94SBen Coburn} 410cc7d0c94SBen Coburn 411cc7d0c94SBen Coburn/** 412f3f0262cSandi * Create the directory needed for the given file 41315fae107Sandi * 41415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 41542ea7f44SGerrit Uitslag * 41642ea7f44SGerrit Uitslag * @param string $file file name 417f3f0262cSandi */ 418f3f0262cSandifunction io_makeFileDir($file){ 419f3f0262cSandi $dir = dirname($file); 4200d8850c4SAndreas Gohr if(!@is_dir($dir)){ 421f3f0262cSandi io_mkdir_p($dir) || msg("Creating directory $dir failed",-1); 422f3f0262cSandi } 423f3f0262cSandi} 424f3f0262cSandi 425f3f0262cSandi/** 426f3f0262cSandi * Creates a directory hierachy. 427f3f0262cSandi * 42815fae107Sandi * @link http://www.php.net/manual/en/function.mkdir.php 429f3f0262cSandi * @author <saint@corenova.com> 4303dc3a5f1Sandi * @author Andreas Gohr <andi@splitbrain.org> 43142ea7f44SGerrit Uitslag * 43242ea7f44SGerrit Uitslag * @param string $target filename 43342ea7f44SGerrit Uitslag * @return bool|int|string 434f3f0262cSandi */ 435f3f0262cSandifunction io_mkdir_p($target){ 4363dc3a5f1Sandi global $conf; 4370d8850c4SAndreas Gohr if (@is_dir($target)||empty($target)) return 1; // best case check first 43879e79377SAndreas Gohr if (file_exists($target) && !is_dir($target)) return 0; 4393dc3a5f1Sandi //recursion 4403dc3a5f1Sandi if (io_mkdir_p(substr($target,0,strrpos($target,'/')))){ 4413dc3a5f1Sandi if($conf['safemodehack']){ 44200976812SAndreas Gohr $dir = preg_replace('/^'.preg_quote(fullpath($conf['ftp']['root']),'/').'/','', $target); 443034138e2SRainer Weinhold return io_mkdir_ftp($dir); 4443dc3a5f1Sandi }else{ 44544881d27STroels Liebe Bentsen $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree 446443e135dSChristopher Smith if($ret && !empty($conf['dperm'])) chmod($target, $conf['dperm']); 44744881d27STroels Liebe Bentsen return $ret; 4483dc3a5f1Sandi } 4493dc3a5f1Sandi } 450f3f0262cSandi return 0; 451f3f0262cSandi} 452f3f0262cSandi 453f3f0262cSandi/** 4544d47e8e3SAndreas Gohr * Recursively delete a directory 4554d47e8e3SAndreas Gohr * 4564d47e8e3SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 4574d47e8e3SAndreas Gohr * @param string $path 4584d47e8e3SAndreas Gohr * @param bool $removefiles defaults to false which will delete empty directories only 4594d47e8e3SAndreas Gohr * @return bool 4604d47e8e3SAndreas Gohr */ 4614d47e8e3SAndreas Gohrfunction io_rmdir($path, $removefiles = false) { 4624d47e8e3SAndreas Gohr if(!is_string($path) || $path == "") return false; 463d8cf4dd4SAndreas Gohr if(!file_exists($path)) return true; // it's already gone or was never there, count as success 4644d47e8e3SAndreas Gohr 4654d47e8e3SAndreas Gohr if(is_dir($path) && !is_link($path)) { 4664d47e8e3SAndreas Gohr $dirs = array(); 4674d47e8e3SAndreas Gohr $files = array(); 4684d47e8e3SAndreas Gohr 4694d47e8e3SAndreas Gohr if(!$dh = @opendir($path)) return false; 4708426a3eeSAndreas Gohr while(false !== ($f = readdir($dh))) { 4714d47e8e3SAndreas Gohr if($f == '..' || $f == '.') continue; 4724d47e8e3SAndreas Gohr 4734d47e8e3SAndreas Gohr // collect dirs and files first 4744d47e8e3SAndreas Gohr if(is_dir("$path/$f") && !is_link("$path/$f")) { 4754d47e8e3SAndreas Gohr $dirs[] = "$path/$f"; 4764d47e8e3SAndreas Gohr } else if($removefiles) { 4774d47e8e3SAndreas Gohr $files[] = "$path/$f"; 4784d47e8e3SAndreas Gohr } else { 4794d47e8e3SAndreas Gohr return false; // abort when non empty 4804d47e8e3SAndreas Gohr } 4814d47e8e3SAndreas Gohr 4824d47e8e3SAndreas Gohr } 4834d47e8e3SAndreas Gohr closedir($dh); 4844d47e8e3SAndreas Gohr 4854d47e8e3SAndreas Gohr // now traverse into directories first 4864d47e8e3SAndreas Gohr foreach($dirs as $dir) { 4874d47e8e3SAndreas Gohr if(!io_rmdir($dir, $removefiles)) return false; // abort on any error 4884d47e8e3SAndreas Gohr } 4894d47e8e3SAndreas Gohr 4904d47e8e3SAndreas Gohr // now delete files 4914d47e8e3SAndreas Gohr foreach($files as $file) { 4924d47e8e3SAndreas Gohr if(!@unlink($file)) return false; //abort on any error 4934d47e8e3SAndreas Gohr } 4944d47e8e3SAndreas Gohr 4954d47e8e3SAndreas Gohr // remove self 4964d47e8e3SAndreas Gohr return @rmdir($path); 4974d47e8e3SAndreas Gohr } else if($removefiles) { 4984d47e8e3SAndreas Gohr return @unlink($path); 4994d47e8e3SAndreas Gohr } 5004d47e8e3SAndreas Gohr return false; 5014d47e8e3SAndreas Gohr} 5024d47e8e3SAndreas Gohr 5034d47e8e3SAndreas Gohr/** 5043dc3a5f1Sandi * Creates a directory using FTP 5053dc3a5f1Sandi * 5063dc3a5f1Sandi * This is used when the safemode workaround is enabled 5073dc3a5f1Sandi * 5083dc3a5f1Sandi * @author <andi@splitbrain.org> 50942ea7f44SGerrit Uitslag * 51042ea7f44SGerrit Uitslag * @param string $dir name of the new directory 51142ea7f44SGerrit Uitslag * @return false|string 5123dc3a5f1Sandi */ 5133dc3a5f1Sandifunction io_mkdir_ftp($dir){ 5143dc3a5f1Sandi global $conf; 5153dc3a5f1Sandi 5163dc3a5f1Sandi if(!function_exists('ftp_connect')){ 5173dc3a5f1Sandi msg("FTP support not found - safemode workaround not usable",-1); 5183dc3a5f1Sandi return false; 5193dc3a5f1Sandi } 5203dc3a5f1Sandi 5213dc3a5f1Sandi $conn = @ftp_connect($conf['ftp']['host'],$conf['ftp']['port'],10); 5223dc3a5f1Sandi if(!$conn){ 5233dc3a5f1Sandi msg("FTP connection failed",-1); 5243dc3a5f1Sandi return false; 5253dc3a5f1Sandi } 5263dc3a5f1Sandi 5273994772aSChris Smith if(!@ftp_login($conn, $conf['ftp']['user'], conf_decodeString($conf['ftp']['pass']))){ 5283dc3a5f1Sandi msg("FTP login failed",-1); 5293dc3a5f1Sandi return false; 5303dc3a5f1Sandi } 5313dc3a5f1Sandi 5323dc3a5f1Sandi //create directory 533034138e2SRainer Weinhold $ok = @ftp_mkdir($conn, $dir); 5341ca31cfeSAndreas Gohr //set permissions 5351ca31cfeSAndreas Gohr @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmode'],$dir)); 5363dc3a5f1Sandi 537034138e2SRainer Weinhold @ftp_close($conn); 5383dc3a5f1Sandi return $ok; 5393dc3a5f1Sandi} 5403dc3a5f1Sandi 5413dc3a5f1Sandi/** 542de862555SMichael Klier * Creates a unique temporary directory and returns 543de862555SMichael Klier * its path. 544de862555SMichael Klier * 545de862555SMichael Klier * @author Michael Klier <chi@chimeric.de> 54642ea7f44SGerrit Uitslag * 54742ea7f44SGerrit Uitslag * @return false|string path to new directory or false 548de862555SMichael Klier */ 549de862555SMichael Klierfunction io_mktmpdir() { 550de862555SMichael Klier global $conf; 551de862555SMichael Klier 552da1e1077SChris Smith $base = $conf['tmpdir']; 553da1e1077SChris Smith $dir = md5(uniqid(mt_rand(), true)); 554287f35bdSAndreas Gohr $tmpdir = $base.'/'.$dir; 555de862555SMichael Klier 556de862555SMichael Klier if(io_mkdir_p($tmpdir)) { 557de862555SMichael Klier return($tmpdir); 558de862555SMichael Klier } else { 559de862555SMichael Klier return false; 560de862555SMichael Klier } 561de862555SMichael Klier} 562de862555SMichael Klier 563de862555SMichael Klier/** 56473ccfcb9Schris * downloads a file from the net and saves it 56573ccfcb9Schris * 56673ccfcb9Schris * if $useAttachment is false, 56773ccfcb9Schris * - $file is the full filename to save the file, incl. path 56873ccfcb9Schris * - if successful will return true, false otherwise 569db959ae3SAndreas Gohr * 57073ccfcb9Schris * if $useAttachment is true, 57173ccfcb9Schris * - $file is the directory where the file should be saved 57273ccfcb9Schris * - if successful will return the name used for the saved file, false otherwise 573b625487dSandi * 574b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 57573ccfcb9Schris * @author Chris Smith <chris@jalakai.co.uk> 57642ea7f44SGerrit Uitslag * 57742ea7f44SGerrit Uitslag * @param string $url url to download 57842ea7f44SGerrit Uitslag * @param string $file path to file or directory where to save 57942ea7f44SGerrit Uitslag * @param bool $useAttachment if true: try to use name of download, uses otherwise $defaultName, false: uses $file as path to file 58042ea7f44SGerrit Uitslag * @param string $defaultName fallback for if using $useAttachment 58142ea7f44SGerrit Uitslag * @param int $maxSize maximum file size 58242ea7f44SGerrit Uitslag * @return bool|string if failed false, otherwise true or the name of the file in the given dir 583b625487dSandi */ 584847b8298SAndreas Gohrfunction io_download($url,$file,$useAttachment=false,$defaultName='',$maxSize=2097152){ 585ac9115b0STroels Liebe Bentsen global $conf; 5869b307a83SAndreas Gohr $http = new DokuHTTPClient(); 587847b8298SAndreas Gohr $http->max_bodysize = $maxSize; 5889b307a83SAndreas Gohr $http->timeout = 25; //max. 25 sec 589a5951419SAndreas Gohr $http->keep_alive = false; // we do single ops here, no need for keep-alive 5909b307a83SAndreas Gohr 5919b307a83SAndreas Gohr $data = $http->get($url); 5929b307a83SAndreas Gohr if(!$data) return false; 5939b307a83SAndreas Gohr 59473ccfcb9Schris $name = ''; 595cd2f903bSMichael Hamann if ($useAttachment) { 59673ccfcb9Schris if (isset($http->resp_headers['content-disposition'])) { 59773ccfcb9Schris $content_disposition = $http->resp_headers['content-disposition']; 598ce070a9fSchris $match=array(); 59973ccfcb9Schris if (is_string($content_disposition) && 600ce070a9fSchris preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match)) { 60173ccfcb9Schris 6023009a773SAndreas Gohr $name = utf8_basename($match[1]); 60373ccfcb9Schris } 60473ccfcb9Schris 60573ccfcb9Schris } 60673ccfcb9Schris 60773ccfcb9Schris if (!$name) { 60873ccfcb9Schris if (!$defaultName) return false; 60973ccfcb9Schris $name = $defaultName; 61073ccfcb9Schris } 61173ccfcb9Schris 61273ccfcb9Schris $file = $file.$name; 61373ccfcb9Schris } 61473ccfcb9Schris 61579e79377SAndreas Gohr $fileexists = file_exists($file); 6169b307a83SAndreas Gohr $fp = @fopen($file,"w"); 617b625487dSandi if(!$fp) return false; 6189b307a83SAndreas Gohr fwrite($fp,$data); 619b625487dSandi fclose($fp); 6201ca31cfeSAndreas Gohr if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']); 62173ccfcb9Schris if ($useAttachment) return $name; 622b625487dSandi return true; 623b625487dSandi} 624b625487dSandi 625b625487dSandi/** 626ac9115b0STroels Liebe Bentsen * Windows compatible rename 627bf5e5a5bSAndreas Gohr * 628bf5e5a5bSAndreas Gohr * rename() can not overwrite existing files on Windows 629bf5e5a5bSAndreas Gohr * this function will use copy/unlink instead 63042ea7f44SGerrit Uitslag * 63142ea7f44SGerrit Uitslag * @param string $from 63242ea7f44SGerrit Uitslag * @param string $to 63342ea7f44SGerrit Uitslag * @return bool succes or fail 634bf5e5a5bSAndreas Gohr */ 635bf5e5a5bSAndreas Gohrfunction io_rename($from,$to){ 636ac9115b0STroels Liebe Bentsen global $conf; 637bf5e5a5bSAndreas Gohr if(!@rename($from,$to)){ 638bf5e5a5bSAndreas Gohr if(@copy($from,$to)){ 6398e0b019fSAndreas Gohr if($conf['fperm']) chmod($to, $conf['fperm']); 640bf5e5a5bSAndreas Gohr @unlink($from); 641bf5e5a5bSAndreas Gohr return true; 642bf5e5a5bSAndreas Gohr } 643bf5e5a5bSAndreas Gohr return false; 644bf5e5a5bSAndreas Gohr } 645bf5e5a5bSAndreas Gohr return true; 646bf5e5a5bSAndreas Gohr} 647bf5e5a5bSAndreas Gohr 648420edfd6STom N Harris/** 649420edfd6STom N Harris * Runs an external command with input and output pipes. 650420edfd6STom N Harris * Returns the exit code from the process. 651420edfd6STom N Harris * 652420edfd6STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 65342ea7f44SGerrit Uitslag * 65442ea7f44SGerrit Uitslag * @param string $cmd 65542ea7f44SGerrit Uitslag * @param string $input input pipe 65642ea7f44SGerrit Uitslag * @param string $output output pipe 65742ea7f44SGerrit Uitslag * @return int exit code from process 658420edfd6STom N Harris */ 659420edfd6STom N Harrisfunction io_exec($cmd, $input, &$output){ 6606c528220STom N Harris $descspec = array( 6616c528220STom N Harris 0=>array("pipe","r"), 6626c528220STom N Harris 1=>array("pipe","w"), 6636c528220STom N Harris 2=>array("pipe","w")); 6646c528220STom N Harris $ph = proc_open($cmd, $descspec, $pipes); 6656c528220STom N Harris if(!$ph) return -1; 6666c528220STom N Harris fclose($pipes[2]); // ignore stderr 6676c528220STom N Harris fwrite($pipes[0], $input); 6686c528220STom N Harris fclose($pipes[0]); 6696c528220STom N Harris $output = stream_get_contents($pipes[1]); 6706c528220STom N Harris fclose($pipes[1]); 6716c528220STom N Harris return proc_close($ph); 672f3f0262cSandi} 673f3f0262cSandi 6747421c3ccSAndreas Gohr/** 6757421c3ccSAndreas Gohr * Search a file for matching lines 6767421c3ccSAndreas Gohr * 6777421c3ccSAndreas Gohr * This is probably not faster than file()+preg_grep() but less 6787421c3ccSAndreas Gohr * memory intensive because not the whole file needs to be loaded 6797421c3ccSAndreas Gohr * at once. 6807421c3ccSAndreas Gohr * 6817421c3ccSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6827421c3ccSAndreas Gohr * @param string $file The file to search 6837421c3ccSAndreas Gohr * @param string $pattern PCRE pattern 6847421c3ccSAndreas Gohr * @param int $max How many lines to return (0 for all) 685cd2f903bSMichael Hamann * @param bool $backref When true returns array with backreferences instead of lines 686cd2f903bSMichael Hamann * @return array matching lines or backref, false on error 6877421c3ccSAndreas Gohr */ 6887421c3ccSAndreas Gohrfunction io_grep($file,$pattern,$max=0,$backref=false){ 6897421c3ccSAndreas Gohr $fh = @fopen($file,'r'); 6907421c3ccSAndreas Gohr if(!$fh) return false; 6917421c3ccSAndreas Gohr $matches = array(); 6927421c3ccSAndreas Gohr 6937421c3ccSAndreas Gohr $cnt = 0; 6947421c3ccSAndreas Gohr $line = ''; 6957421c3ccSAndreas Gohr while (!feof($fh)) { 6967421c3ccSAndreas Gohr $line .= fgets($fh, 4096); // read full line 6977421c3ccSAndreas Gohr if(substr($line,-1) != "\n") continue; 6987421c3ccSAndreas Gohr 6997421c3ccSAndreas Gohr // check if line matches 7007421c3ccSAndreas Gohr if(preg_match($pattern,$line,$match)){ 7017421c3ccSAndreas Gohr if($backref){ 7027421c3ccSAndreas Gohr $matches[] = $match; 7037421c3ccSAndreas Gohr }else{ 7047421c3ccSAndreas Gohr $matches[] = $line; 7057421c3ccSAndreas Gohr } 7067421c3ccSAndreas Gohr $cnt++; 7077421c3ccSAndreas Gohr } 7087421c3ccSAndreas Gohr if($max && $max == $cnt) break; 7097421c3ccSAndreas Gohr $line = ''; 7107421c3ccSAndreas Gohr } 7117421c3ccSAndreas Gohr fclose($fh); 7127421c3ccSAndreas Gohr return $matches; 7137421c3ccSAndreas Gohr} 7147421c3ccSAndreas Gohr 715