1ed7b5f09Sandi<?php 2ed7b5f09Sandi/** 3ed7b5f09Sandi * Initialize some defaults needed for DokuWiki 4ed7b5f09Sandi */ 5ed7b5f09Sandi 6a609a9ccSBen Coburn // start timing Dokuwiki execution 7a609a9ccSBen Coburn function delta_time($start=0) { 8a609a9ccSBen Coburn list($usec, $sec) = explode(" ", microtime()); 9a609a9ccSBen Coburn return ((float)$usec+(float)$sec)-((float)$start); 10a609a9ccSBen Coburn } 11a609a9ccSBen Coburn define('DOKU_START_TIME', delta_time()); 12a609a9ccSBen Coburn 1348beefecSAndreas Gohr // if available load a preload config file 1448beefecSAndreas Gohr @include(fullpath(dirname(__FILE__)).'/preload.php'); 1548beefecSAndreas Gohr 16ed7b5f09Sandi // define the include path 1700976812SAndreas Gohr if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 18ad15db82Sandi 19e7cb32dcSAndreas Gohr // define config path (packagers may want to change this to /etc/dokuwiki/) 20b7551a6dSEsther Brunner if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/'); 21e7cb32dcSAndreas Gohr 22bad905f1SBen Coburn // check for error reporting override or set error reporting to sane values 23d8186216SBen Coburn if (!defined('DOKU_E_LEVEL') && @file_exists(DOKU_CONF.'report_e_all')) { 24bad905f1SBen Coburn define('DOKU_E_LEVEL', E_ALL); 25bad905f1SBen Coburn } 26fc80ed59SAndreas Gohr if (!defined('DOKU_E_LEVEL')) { 27fc80ed59SAndreas Gohr if(defined('E_DEPRECATED')){ // since php 5.3 28fc80ed59SAndreas Gohr error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 29fc80ed59SAndreas Gohr }else{ 30fc80ed59SAndreas Gohr error_reporting(E_ALL ^ E_NOTICE); 31fc80ed59SAndreas Gohr } 32fc80ed59SAndreas Gohr } else { 33fc80ed59SAndreas Gohr error_reporting(DOKU_E_LEVEL); 34fc80ed59SAndreas Gohr } 35c53ea5f2Sandi 3650602150SBen Coburn // init memory caches 37bc3e97beSAndreas Gohr global $cache_revinfo; $cache_revinfo = array(); 38bc3e97beSAndreas Gohr global $cache_wikifn; $cache_wikifn = array(); 39a424cd8eSchris global $cache_cleanid; $cache_cleanid = array(); 40a424cd8eSchris global $cache_authname; $cache_authname = array(); 410a7e3bceSchris global $cache_metadata; $cache_metadata = array(); 4250602150SBen Coburn 434724a577Sandi //prepare config array() 44ee20e7d1Sandi global $conf; 454724a577Sandi $conf = array(); 464724a577Sandi 47ad15db82Sandi // load the config file(s) 48e7cb32dcSAndreas Gohr require_once(DOKU_CONF.'dokuwiki.php'); 490a6ead41SAndreas Gohr if(@file_exists(DOKU_CONF.'local.php')){ 500a6ead41SAndreas Gohr require_once(DOKU_CONF.'local.php'); 510a6ead41SAndreas Gohr } 52ad15db82Sandi 53ad15db82Sandi //prepare language array 54ee20e7d1Sandi global $lang; 55ad15db82Sandi $lang = array(); 56ed7b5f09Sandi 5716521111Sandi //load the language files 58bc3b6aecSandi require_once(DOKU_INC.'inc/lang/en/lang.php'); 59f949a01cSAndreas Gohr if ( $conf['lang'] && $conf['lang'] != 'en' ) { 60bc3b6aecSandi require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); 61fc1c55b1Shfuecks } 6216521111Sandi 63ed7b5f09Sandi // define baseURL 644b1a4e04SAndreas Gohr if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false)); 65ed7b5f09Sandi if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true)); 664b1a4e04SAndreas Gohr if(!defined('DOKU_BASE')){ 674b1a4e04SAndreas Gohr if($conf['canonical']){ 684b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_URL); 694b1a4e04SAndreas Gohr }else{ 704b1a4e04SAndreas Gohr define('DOKU_BASE',DOKU_REL); 714b1a4e04SAndreas Gohr } 724b1a4e04SAndreas Gohr } 734b1a4e04SAndreas Gohr 74b8595a66SAndreas Gohr // define whitespace 75b8595a66SAndreas Gohr if(!defined('DOKU_LF')) define ('DOKU_LF',"\n"); 76b8595a66SAndreas Gohr if(!defined('DOKU_TAB')) define ('DOKU_TAB',"\t"); 77ed7b5f09Sandi 78e71ce681SAndreas Gohr // define cookie and session id 798b11f0a5SAndreas Gohr if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5(DOKU_REL)); 80e71ce681SAndreas Gohr 81ee20e7d1Sandi // define Plugin dir 82f62ea8a1Sandi if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 83ee20e7d1Sandi 84ed7b5f09Sandi // define main script 85ed7b5f09Sandi if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php'); 86ed7b5f09Sandi 876b13307fSandi // define Template baseURL 886b13307fSandi if(!defined('DOKU_TPL')) define('DOKU_TPL', 89f62ea8a1Sandi DOKU_BASE.'lib/tpl/'.$conf['template'].'/'); 906b13307fSandi 9178a6aeb1SAndreas Gohr // define real Template directory 9278a6aeb1SAndreas Gohr if(!defined('DOKU_TPLINC')) define('DOKU_TPLINC', 9378a6aeb1SAndreas Gohr DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 9478a6aeb1SAndreas Gohr 95ed7b5f09Sandi // make session rewrites XHTML compliant 963fc74836Sandi @ini_set('arg_separator.output', '&'); 97ed7b5f09Sandi 98d7e6bba9SAndreas Gohr // make sure global zlib does not interfere FS#1132 99d7e6bba9SAndreas Gohr @ini_set('zlib.output_compression', 'off'); 100d7e6bba9SAndreas Gohr 1016deb5405SAndreas Gohr // increase PCRE backtrack limit 1026deb5405SAndreas Gohr @ini_set('pcre.backtrack_limit', '20971520'); 1036deb5405SAndreas Gohr 1043138b5c7SAndreas Gohr // enable gzip compression 1053138b5c7SAndreas Gohr if ($conf['gzip_output'] && 1063138b5c7SAndreas Gohr !defined('DOKU_DISABLE_GZIP_OUTPUT') && 1073138b5c7SAndreas Gohr function_exists('ob_gzhandler') && 1083138b5c7SAndreas Gohr preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) { 1093138b5c7SAndreas Gohr ob_start('ob_gzhandler'); 1103138b5c7SAndreas Gohr } 1113138b5c7SAndreas Gohr 112ed7b5f09Sandi // init session 1136534245aSAndreas Gohr if (!headers_sent() && !defined('NOSESSION')){ 114ed7b5f09Sandi session_name("DokuWiki"); 115f5c6743cSAndreas Gohr if (version_compare(PHP_VERSION, '5.2.0', '>')) { 116f5c6743cSAndreas Gohr session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true); 117f5c6743cSAndreas Gohr }else{ 118f5c6743cSAndreas Gohr session_set_cookie_params(0,DOKU_REL,'',($conf['securecookie'] && is_ssl())); 119f5c6743cSAndreas Gohr } 120bad31ae9SAndreas Gohr session_start(); 121bad31ae9SAndreas Gohr } 122ed7b5f09Sandi 123ed7b5f09Sandi // kill magic quotes 124e55eb89cSAndreas Gohr if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')) { 125ed7b5f09Sandi if (!empty($_GET)) remove_magic_quotes($_GET); 126ed7b5f09Sandi if (!empty($_POST)) remove_magic_quotes($_POST); 127ed7b5f09Sandi if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 128ed7b5f09Sandi if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 1293fc74836Sandi @ini_set('magic_quotes_gpc', 0); 130e55eb89cSAndreas Gohr define('MAGIC_QUOTES_STRIPPED',1); 131ed7b5f09Sandi } 1323fc74836Sandi @set_magic_quotes_runtime(0); 1333fc74836Sandi @ini_set('magic_quotes_sybase',0); 134ed7b5f09Sandi 135a1637ffdSAndreas Gohr // don't let cookies ever interfere with request vars 136a1637ffdSAndreas Gohr $_REQUEST = array_merge($_GET,$_POST); 137a1637ffdSAndreas Gohr 138ed7b5f09Sandi // disable gzip if not available 1398a447e5cSAndreas Gohr if($conf['compression'] == 'bz2' && !function_exists('bzopen')){ 140fe893490SAndreas Gohr $conf['compression'] = 'gz'; 141501252a5SAndreas Gohr } 142fe893490SAndreas Gohr if($conf['compression'] == 'gz' && !function_exists('gzopen')){ 143501252a5SAndreas Gohr $conf['compression'] = 0; 144ed7b5f09Sandi } 145ed7b5f09Sandi 146e656dcd4SAndreas Gohr // fix dateformat for upgraders 147e656dcd4SAndreas Gohr if(strpos($conf['dformat'],'%') === false){ 148e656dcd4SAndreas Gohr $conf['dformat'] = '%Y/%m/%d %H:%M'; 149e656dcd4SAndreas Gohr } 150e656dcd4SAndreas Gohr 1511ca31cfeSAndreas Gohr // precalculate file creation modes 1521ca31cfeSAndreas Gohr init_creationmodes(); 153ed7b5f09Sandi 1543dc3a5f1Sandi // make real paths and check them 15598407a7aSandi init_paths(); 1567367b368SAndreas Gohr init_files(); 157ed7b5f09Sandi 1588c4f28e8Sjan // automatic upgrade to script versions of certain files 159e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'users.auth'); 160e7cb32dcSAndreas Gohr scriptify(DOKU_CONF.'acl.auth'); 161f62ea8a1Sandi 162f62ea8a1Sandi 163f62ea8a1Sandi/** 16498407a7aSandi * Checks paths from config file 16598407a7aSandi */ 16698407a7aSandifunction init_paths(){ 16798407a7aSandi global $conf; 16898407a7aSandi 16998407a7aSandi $paths = array('datadir' => 'pages', 17098407a7aSandi 'olddir' => 'attic', 17198407a7aSandi 'mediadir' => 'media', 17298407a7aSandi 'metadir' => 'meta', 17398407a7aSandi 'cachedir' => 'cache', 174579b0f7eSTNHarris 'indexdir' => 'index', 175de33a58fSMichael Klier 'lockdir' => 'locks', 176de33a58fSMichael Klier 'tmpdir' => 'tmp'); 17798407a7aSandi 17898407a7aSandi foreach($paths as $c => $p){ 179bb4866bdSchris if(empty($conf[$c])) $conf[$c] = $conf['savedir'].'/'.$p; 18098407a7aSandi $conf[$c] = init_path($conf[$c]); 1811983acc2SGuy Brand if(empty($conf[$c])) nice_die("The $c ('$p') does not exist, isn't accessible or writable. 18269dc3177SAndreas Gohr You should check your config and permission settings. 18369dc3177SAndreas Gohr Or maybe you want to <a href=\"install.php\">run the 18469dc3177SAndreas Gohr installer</a>?"); 18598407a7aSandi } 18671726d78SBen Coburn 18771726d78SBen Coburn // path to old changelog only needed for upgrading 18871726d78SBen Coburn $conf['changelog_old'] = init_path((isset($conf['changelog']))?($conf['changelog']):($conf['savedir'].'/changes.log')); 18971726d78SBen Coburn if ($conf['changelog_old']=='') { unset($conf['changelog_old']); } 19071726d78SBen Coburn // hardcoded changelog because it is now a cache that lives in meta 19171726d78SBen Coburn $conf['changelog'] = $conf['metadir'].'/_dokuwiki.changes'; 19298407a7aSandi} 19398407a7aSandi 19498407a7aSandi/** 1950d8850c4SAndreas Gohr * Checks the existance of certain files and creates them if missing. 1967367b368SAndreas Gohr */ 1977367b368SAndreas Gohrfunction init_files(){ 1987367b368SAndreas Gohr global $conf; 1990d8850c4SAndreas Gohr 200579b0f7eSTNHarris $files = array( $conf['indexdir'].'/page.idx'); 2017367b368SAndreas Gohr 2027367b368SAndreas Gohr foreach($files as $file){ 2037367b368SAndreas Gohr if(!@file_exists($file)){ 2040d8850c4SAndreas Gohr $fh = @fopen($file,'a'); 2050d8850c4SAndreas Gohr if($fh){ 2067367b368SAndreas Gohr fclose($fh); 2071ca31cfeSAndreas Gohr if($conf['fperm']) chmod($file, $conf['fperm']); 2080d8850c4SAndreas Gohr }else{ 2093816dcbcSAndreas Gohr nice_die("$file is not writable. Check your permissions settings!"); 2100d8850c4SAndreas Gohr } 2117367b368SAndreas Gohr } 2127367b368SAndreas Gohr } 2137367b368SAndreas Gohr} 2147367b368SAndreas Gohr 2157367b368SAndreas Gohr/** 2160d8850c4SAndreas Gohr * Returns absolute path 217f62ea8a1Sandi * 2180d8850c4SAndreas Gohr * This tries the given path first, then checks in DOKU_INC. 2190d8850c4SAndreas Gohr * Check for accessability on directories as well. 2200d8850c4SAndreas Gohr * 2210d8850c4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 222f62ea8a1Sandi */ 223f62ea8a1Sandifunction init_path($path){ 2240d8850c4SAndreas Gohr // check existance 22500976812SAndreas Gohr $p = fullpath($path); 2260d8850c4SAndreas Gohr if(!@file_exists($p)){ 22700976812SAndreas Gohr $p = fullpath(DOKU_INC.$path); 2280d8850c4SAndreas Gohr if(!@file_exists($p)){ 2298fc4e739Sandi return ''; 230f62ea8a1Sandi } 2310d8850c4SAndreas Gohr } 2320d8850c4SAndreas Gohr 2330d8850c4SAndreas Gohr // check writability 2340d8850c4SAndreas Gohr if(!@is_writable($p)){ 2350d8850c4SAndreas Gohr return ''; 2360d8850c4SAndreas Gohr } 2370d8850c4SAndreas Gohr 2380d8850c4SAndreas Gohr // check accessability (execute bit) for directories 2390d8850c4SAndreas Gohr if(@is_dir($p) && !@file_exists("$p/.")){ 2400d8850c4SAndreas Gohr return ''; 2410d8850c4SAndreas Gohr } 2420d8850c4SAndreas Gohr 2430d8850c4SAndreas Gohr return $p; 2440d8850c4SAndreas Gohr} 2458c4f28e8Sjan 246ed7b5f09Sandi/** 2471ca31cfeSAndreas Gohr * Sets the internal config values fperm and dperm which, when set, 2481ca31cfeSAndreas Gohr * will be used to change the permission of a newly created dir or 2491ca31cfeSAndreas Gohr * file with chmod. Considers the influence of the system's umask 2501ca31cfeSAndreas Gohr * setting the values only if needed. 2511ca31cfeSAndreas Gohr */ 2521ca31cfeSAndreas Gohrfunction init_creationmodes(){ 2531ca31cfeSAndreas Gohr global $conf; 2541ca31cfeSAndreas Gohr 2551ca31cfeSAndreas Gohr // Legacy support for old umask/dmask scheme 2561ca31cfeSAndreas Gohr unset($conf['dmask']); 2571ca31cfeSAndreas Gohr unset($conf['fmask']); 2581ca31cfeSAndreas Gohr unset($conf['umask']); 2591ca31cfeSAndreas Gohr unset($conf['fperm']); 2601ca31cfeSAndreas Gohr unset($conf['dperm']); 2611ca31cfeSAndreas Gohr 2629f3cdec3SAndreas Gohr // get system umask, fallback to 0 if none available 2639f3cdec3SAndreas Gohr $umask = @umask(); 2649f3cdec3SAndreas Gohr if(!$umask) $umask = 0000; 2651ca31cfeSAndreas Gohr 2661ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 2671ca31cfeSAndreas Gohr // and set the fperm param if it's not what we want 2681ca31cfeSAndreas Gohr $auto_fmode = 0666 & ~$umask; 2691ca31cfeSAndreas Gohr if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; 2701ca31cfeSAndreas Gohr 2711ca31cfeSAndreas Gohr // check what is set automatically by the system on file creation 2721ca31cfeSAndreas Gohr // and set the dperm param if it's not what we want 2731ca31cfeSAndreas Gohr $auto_dmode = $conf['dmode'] & ~$umask; 2741ca31cfeSAndreas Gohr if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; 2751ca31cfeSAndreas Gohr} 2761ca31cfeSAndreas Gohr 2771ca31cfeSAndreas Gohr/** 278ed7b5f09Sandi * remove magic quotes recursivly 279ed7b5f09Sandi * 280ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 281ed7b5f09Sandi */ 282ed7b5f09Sandifunction remove_magic_quotes(&$array) { 283ed7b5f09Sandi foreach (array_keys($array) as $key) { 28481c54349SAndreas Gohr // handle magic quotes in keynames (breaks order) 28581c54349SAndreas Gohr $sk = stripslashes($key); 28681c54349SAndreas Gohr if($sk != $key){ 28781c54349SAndreas Gohr $array[$sk] = $array[$key]; 28881c54349SAndreas Gohr unset($array[$key]); 28981c54349SAndreas Gohr $key = $sk; 29081c54349SAndreas Gohr } 29181c54349SAndreas Gohr 29281c54349SAndreas Gohr // do recursion if needed 293ed7b5f09Sandi if (is_array($array[$key])) { 294ed7b5f09Sandi remove_magic_quotes($array[$key]); 295ed7b5f09Sandi }else { 296ed7b5f09Sandi $array[$key] = stripslashes($array[$key]); 297ed7b5f09Sandi } 298ed7b5f09Sandi } 299ed7b5f09Sandi} 300ed7b5f09Sandi 301ed7b5f09Sandi/** 302ed7b5f09Sandi * Returns the full absolute URL to the directory where 303ed7b5f09Sandi * DokuWiki is installed in (includes a trailing slash) 304ed7b5f09Sandi * 305ed7b5f09Sandi * @author Andreas Gohr <andi@splitbrain.org> 306ed7b5f09Sandi */ 3074b1a4e04SAndreas Gohrfunction getBaseURL($abs=null){ 308ed7b5f09Sandi global $conf; 309ed7b5f09Sandi //if canonical url enabled always return absolute 3104b1a4e04SAndreas Gohr if(is_null($abs)) $abs = $conf['canonical']; 311ed7b5f09Sandi 31292b83b77Sandi if($conf['basedir']){ 31346c73e01SChris Smith $dir = $conf['basedir']; 31489aa05dbSAndreas Gohr }elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){ 31546c73e01SChris Smith $dir = dirname($_SERVER['SCRIPT_NAME']); 31689aa05dbSAndreas Gohr }elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){ 31746c73e01SChris Smith $dir = dirname($_SERVER['PHP_SELF']); 318093ec9e4Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 319093ec9e4Sandi $dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 320093ec9e4Sandi $_SERVER['SCRIPT_FILENAME']); 32146c73e01SChris Smith $dir = dirname('/'.$dir); 32292b83b77Sandi }else{ 32346c73e01SChris Smith $dir = '.'; //probably wrong 32492b83b77Sandi } 325ed7b5f09Sandi 32646c73e01SChris Smith $dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour 32746c73e01SChris Smith $dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes 328ed7b5f09Sandi 329f62ea8a1Sandi //handle script in lib/exe dir 330f62ea8a1Sandi $dir = preg_replace('!lib/exe/$!','',$dir); 331f62ea8a1Sandi 332488d5fa0SMichael Klier chi@chimeric.de //handle script in lib/plugins dir 333488d5fa0SMichael Klier chi@chimeric.de $dir = preg_replace('!lib/plugins/.*$!','',$dir); 334488d5fa0SMichael Klier chi@chimeric.de 335ed7b5f09Sandi //finish here for relative URLs 336ed7b5f09Sandi if(!$abs) return $dir; 337ed7b5f09Sandi 33846c73e01SChris Smith //use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path 33946c73e01SChris Smith if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir; 340ef7b3ecdSAndreas Gohr 341e82e3526SAndreas Gohr //split hostheader into host and port 342e82e3526SAndreas Gohr list($host,$port) = explode(':',$_SERVER['HTTP_HOST']); 343e82e3526SAndreas Gohr if(!$port) $port = $_SERVER['SERVER_PORT']; 344e82e3526SAndreas Gohr if(!$port) $port = 80; 345ed7b5f09Sandi 346f5c6743cSAndreas Gohr if(!is_ssl()){ 347ed7b5f09Sandi $proto = 'http://'; 348e82e3526SAndreas Gohr if ($port == '80') { 349ed7b5f09Sandi $port=''; 350ed7b5f09Sandi } 351ed7b5f09Sandi }else{ 352ed7b5f09Sandi $proto = 'https://'; 353e82e3526SAndreas Gohr if ($port == '443') { 354ed7b5f09Sandi $port=''; 355ed7b5f09Sandi } 356ed7b5f09Sandi } 357ed7b5f09Sandi 358e82e3526SAndreas Gohr if($port) $port = ':'.$port; 359e82e3526SAndreas Gohr 360ed7b5f09Sandi return $proto.$host.$port.$dir; 361ed7b5f09Sandi} 362ed7b5f09Sandi 363b000c6d4Sandi/** 364f5c6743cSAndreas Gohr * Check if accessed via HTTPS 365f5c6743cSAndreas Gohr * 366f5c6743cSAndreas Gohr * Apache leaves ,$_SERVER['HTTPS'] empty when not available, IIS sets it to 'off'. 367f5c6743cSAndreas Gohr * 'false' and 'disabled' are just guessing 368f5c6743cSAndreas Gohr * 369f5c6743cSAndreas Gohr * @returns bool true when SSL is active 370f5c6743cSAndreas Gohr */ 371f5c6743cSAndreas Gohrfunction is_ssl(){ 372f5c6743cSAndreas Gohr if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 373f5c6743cSAndreas Gohr return false; 374f5c6743cSAndreas Gohr }else{ 375f5c6743cSAndreas Gohr return true; 376f5c6743cSAndreas Gohr } 377f5c6743cSAndreas Gohr} 378f5c6743cSAndreas Gohr 379f5c6743cSAndreas Gohr/** 380b000c6d4Sandi * Append a PHP extension to a given file and adds an exit call 381b000c6d4Sandi * 382b000c6d4Sandi * This is used to migrate some old configfiles. An added PHP extension 383b000c6d4Sandi * ensures the contents are not shown to webusers even if .htaccess files 384b000c6d4Sandi * do not work 385b000c6d4Sandi * 386b000c6d4Sandi * @author Jan Decaluwe <jan@jandecaluwe.com> 387b000c6d4Sandi */ 3888c4f28e8Sjanfunction scriptify($file) { 3898c4f28e8Sjan // checks 3908c4f28e8Sjan if (!is_readable($file)) { 3918c4f28e8Sjan return; 3928c4f28e8Sjan } 3938c4f28e8Sjan $fn = $file.'.php'; 3948c4f28e8Sjan if (@file_exists($fn)) { 3958c4f28e8Sjan return; 3968c4f28e8Sjan } 3978c4f28e8Sjan $fh = fopen($fn, 'w'); 3988c4f28e8Sjan if (!$fh) { 3993816dcbcSAndreas Gohr nice_die($fn.' is not writable. Check your permission settings!'); 4008c4f28e8Sjan } 4018c4f28e8Sjan // write php exit hack first 4028c4f28e8Sjan fwrite($fh, "# $fn\n"); 4038c4f28e8Sjan fwrite($fh, '# <?php exit()?>'."\n"); 4048c4f28e8Sjan fwrite($fh, "# Don't modify the lines above\n"); 4058c4f28e8Sjan fwrite($fh, "#\n"); 4068c4f28e8Sjan // copy existing lines 4078c4f28e8Sjan $lines = file($file); 4088c4f28e8Sjan foreach ($lines as $line){ 4098c4f28e8Sjan fwrite($fh, $line); 4108c4f28e8Sjan } 4113ba793e2Sandi fclose($fh); 412b000c6d4Sandi //try to rename the old file 4133aee4c27SAndreas Gohr io_rename($file,"$file.old"); 4148c4f28e8Sjan} 4158c4f28e8Sjan 4163816dcbcSAndreas Gohr/** 4173816dcbcSAndreas Gohr * print a nice message even if no styles are loaded yet. 4183816dcbcSAndreas Gohr */ 4193816dcbcSAndreas Gohrfunction nice_die($msg){ 4203816dcbcSAndreas Gohr echo<<<EOT 4213816dcbcSAndreas Gohr <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 4223816dcbcSAndreas Gohr "http://www.w3.org/TR/html4/loose.dtd"> 4233816dcbcSAndreas Gohr <html> 4243816dcbcSAndreas Gohr <head><title>DokuWiki Setup Error</title></head> 4253816dcbcSAndreas Gohr <body style="font-family: Arial, sans-serif"> 4263816dcbcSAndreas Gohr <div style="width:60%; margin: auto; background-color: #fcc; 4273816dcbcSAndreas Gohr border: 1px solid #faa; padding: 0.5em 1em;"> 4283816dcbcSAndreas Gohr <h1 style="font-size: 120%">DokuWiki Setup Error</h1> 4293816dcbcSAndreas Gohr <p>$msg</p> 4303816dcbcSAndreas Gohr </div> 4313816dcbcSAndreas Gohr </body> 4323816dcbcSAndreas Gohr </html> 4333816dcbcSAndreas GohrEOT; 4343816dcbcSAndreas Gohr exit; 4353816dcbcSAndreas Gohr} 4363816dcbcSAndreas Gohr 437ed7b5f09Sandi 43800976812SAndreas Gohr/** 43900976812SAndreas Gohr * A realpath() replacement 44000976812SAndreas Gohr * 44100976812SAndreas Gohr * This function behaves similar to PHP's realpath() but does not resolve 44200976812SAndreas Gohr * symlinks or accesses upper directories 44300976812SAndreas Gohr * 444*4761d30cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 44500976812SAndreas Gohr * @author <richpageau at yahoo dot co dot uk> 44600976812SAndreas Gohr * @link http://de3.php.net/manual/en/function.realpath.php#75992 44700976812SAndreas Gohr */ 44800976812SAndreas Gohrfunction fullpath($path){ 449*4761d30cSAndreas Gohr static $run = 0; 450*4761d30cSAndreas Gohr $root = ''; 451*4761d30cSAndreas Gohr $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || $GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']); 45200976812SAndreas Gohr 453*4761d30cSAndreas Gohr // find the (indestructable) root of the path - keeps windows stuff intact 454*4761d30cSAndreas Gohr if($path{0} == '/'){ 455*4761d30cSAndreas Gohr $root = '/'; 456*4761d30cSAndreas Gohr }elseif($iswin){ 457*4761d30cSAndreas Gohr // match drive letter and UNC paths 458*4761d30cSAndreas Gohr if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)){ 459*4761d30cSAndreas Gohr $root = $match[1]; 460*4761d30cSAndreas Gohr $path = $match[2]; 461*4761d30cSAndreas Gohr }else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)){ 462*4761d30cSAndreas Gohr $root = $match[1]; 463*4761d30cSAndreas Gohr $path = $match[2]; 46400976812SAndreas Gohr } 465*4761d30cSAndreas Gohr } 466*4761d30cSAndreas Gohr $path = str_replace('\\','/',$path); 467*4761d30cSAndreas Gohr 468*4761d30cSAndreas Gohr // if the given path wasn't absolute already, prepend the script path and retry 469*4761d30cSAndreas Gohr if(!$root){ 470*4761d30cSAndreas Gohr $base = dirname($_SERVER['SCRIPT_FILENAME']); 471*4761d30cSAndreas Gohr $path = $base.'/'.$path; 472*4761d30cSAndreas Gohr if($run == 0){ // avoid endless recursion when base isn't absolute for some reason 473*4761d30cSAndreas Gohr $run++; 474*4761d30cSAndreas Gohr return fullpath($path,1); 475*4761d30cSAndreas Gohr } 476*4761d30cSAndreas Gohr } 477*4761d30cSAndreas Gohr $run = 0; 47800976812SAndreas Gohr 47900976812SAndreas Gohr // canonicalize 48000976812SAndreas Gohr $path=explode('/', $path); 48100976812SAndreas Gohr $newpath=array(); 482ef38bfe8SAndreas Gohr foreach($path as $p) { 483ef38bfe8SAndreas Gohr if ($p === '' || $p === '.') continue; 484ef38bfe8SAndreas Gohr if ($p==='..') { 48500976812SAndreas Gohr array_pop($newpath); 48600976812SAndreas Gohr continue; 48700976812SAndreas Gohr } 488ef38bfe8SAndreas Gohr array_push($newpath, $p); 48900976812SAndreas Gohr } 490*4761d30cSAndreas Gohr $finalpath = $root.implode('/', $newpath); 49100976812SAndreas Gohr 492*4761d30cSAndreas Gohr // check for existance (except when unit testing) 493*4761d30cSAndreas Gohr if(!defined('DOKU_UNITTEST') && !@file_exists($finalpath)) { 494*4761d30cSAndreas Gohr return false; 49500976812SAndreas Gohr } 496*4761d30cSAndreas Gohr return $finalpath; 49700976812SAndreas Gohr} 49800976812SAndreas Gohr 49900976812SAndreas Gohr 50000976812SAndreas Gohr 501340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 502