17d101cc1SGerry Weißbach<?php 27d101cc1SGerry Weißbach/** 37d101cc1SGerry Weißbach * Site Export Plugin 47d101cc1SGerry Weißbach * 57d101cc1SGerry Weißbach * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 67d101cc1SGerry Weißbach * @author i-net software <tools@inetsoftware.de> 77d101cc1SGerry Weißbach * @author Gerry Weissbach <gweissbach@inetsoftware.de> 87d101cc1SGerry Weißbach */ 97d101cc1SGerry Weißbach 107d101cc1SGerry Weißbach// must be run within Dokuwiki 117d101cc1SGerry Weißbachif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../../').'/'); 127d101cc1SGerry Weißbachif(!defined('DOKU_PLUGIN')) { 137d101cc1SGerry Weißbach // Just for sanity 147d101cc1SGerry Weißbach require_once(DOKU_INC.'inc/plugin.php'); 157d101cc1SGerry Weißbach define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 167d101cc1SGerry Weißbach} 177d101cc1SGerry Weißbach 187d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'action.php'); 197d101cc1SGerry Weißbachrequire_once(DOKU_INC.'/inc/search.php'); 207d101cc1SGerry Weißbach 217d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/functions.php'); 227d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/httpproxy.php'); 237d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/filewriter.php'); 247d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/toc.php'); 257d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/javahelp.php'); 267d101cc1SGerry Weißbach 277d101cc1SGerry Weißbachclass action_plugin_siteexport_ajax extends DokuWiki_Action_Plugin 287d101cc1SGerry Weißbach{ 297d101cc1SGerry Weißbach /** 307d101cc1SGerry Weißbach * New internal variables for better structure 317d101cc1SGerry Weißbach */ 327d101cc1SGerry Weißbach private $filewriter = null; 337d101cc1SGerry Weißbach public $functions = null; 347d101cc1SGerry Weißbach 357d101cc1SGerry Weißbach // List of files that have already been checked 367d101cc1SGerry Weißbach private $fileChecked = array(); 377d101cc1SGerry Weißbach 387d101cc1SGerry Weißbach // Namespace of the page to export 397d101cc1SGerry Weißbach private $namespace = ''; 407d101cc1SGerry Weißbach 417d101cc1SGerry Weißbach /** 427d101cc1SGerry Weißbach * Register Plugin in DW 437d101cc1SGerry Weißbach **/ 447d101cc1SGerry Weißbach function register(&$controller) { 457d101cc1SGerry Weißbach $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax_siteexport_provider'); 467d101cc1SGerry Weißbach $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'siteexport_action'); 477d101cc1SGerry Weißbach } 487d101cc1SGerry Weißbach 497d101cc1SGerry Weißbach /** 507d101cc1SGerry Weißbach * AJAX Provider - check what is going to be done 517d101cc1SGerry Weißbach * @param $event 527d101cc1SGerry Weißbach * @param $args 537d101cc1SGerry Weißbach */ 547d101cc1SGerry Weißbach function ajax_siteexport_provider(&$event, $args) { 557d101cc1SGerry Weißbach 567d101cc1SGerry Weißbach // If this is not a siteexport call, ignore it. 577d101cc1SGerry Weißbach if ( !strstr($event->data, '__siteexport' ) ) 587d101cc1SGerry Weißbach { 597d101cc1SGerry Weißbach return; 607d101cc1SGerry Weißbach } 617d101cc1SGerry Weißbach 622270cdc5SGerry Weißbach $this->__init_functions(true); 637d101cc1SGerry Weißbach 647d101cc1SGerry Weißbach switch( $event->data ) { 657d101cc1SGerry Weißbach case '__siteexport_getsitelist': $this->ajax_siteexport_getsitelist( $event ); break; 667d101cc1SGerry Weißbach case '__siteexport_addsite': $this->ajax_siteexport_addsite( $event ); break; 677d101cc1SGerry Weißbach case '__siteexport_generateurl': $this->ajax_siteexport_generateurl( $event ); break; 686792d0cfSGerry Weißbach case '__siteexport_aggregate': $this->ajax_siteexport_aggregate( $event ); break; 697d101cc1SGerry Weißbach } 707d101cc1SGerry Weißbach } 717d101cc1SGerry Weißbach 727d101cc1SGerry Weißbach /** 737d101cc1SGerry Weißbach * Export from a URL - action 747d101cc1SGerry Weißbach * @param $event 757d101cc1SGerry Weißbach */ 767d101cc1SGerry Weißbach function siteexport_action( &$event ) { 777d101cc1SGerry Weißbach global $ID; 787d101cc1SGerry Weißbach 797d101cc1SGerry Weißbach // Check if the 'do' was siteexport 807793901eSGerry Weißbach $command = is_array($event->data) ? array_shift(array_keys($event->data)) : $event->data; 81fd385364SGerry Weißbach if ( $command != 'siteexport' ) { return false; } 82fd385364SGerry Weißbach $event->data = act_clean($event->data); 83fd385364SGerry Weißbach 847d101cc1SGerry Weißbach if ( headers_sent() ) { 857d101cc1SGerry Weißbach msg("The siteexport function has to be called prior to any header output.", -1); 867d101cc1SGerry Weißbach } 877d101cc1SGerry Weißbach 887d101cc1SGerry Weißbach $this->__init_functions(); 897d101cc1SGerry Weißbach 907d101cc1SGerry Weißbach $this->functions->debug->message("========================================", null, 1); 917d101cc1SGerry Weißbach $this->functions->debug->message("Starting export from URL call", null, 1); 928da901a0SGerry Weißbach $this->functions->debug->message("----------------------------------------", null, 1); 937d101cc1SGerry Weißbach 947d101cc1SGerry Weißbach $event->preventDefault(); 957d101cc1SGerry Weißbach $event->stopPropagation(); 967d101cc1SGerry Weißbach 977d101cc1SGerry Weißbach // Fake security Token if none given 987d101cc1SGerry Weißbach if ( empty( $_REQUEST['sectok'] ) ) { 997d101cc1SGerry Weißbach $_REQUEST['sectok'] = getSecurityToken(); 1007d101cc1SGerry Weißbach } 1017d101cc1SGerry Weißbach 1027d101cc1SGerry Weißbach // The timer will be used to do redirects if needed to prevent timeouts 1037d101cc1SGerry Weißbach $starttimer = time(); 1047d101cc1SGerry Weißbach $timerdiff = $this->getConf('max_execution_time'); 1057d101cc1SGerry Weißbach 1067d101cc1SGerry Weißbach $data = $this->__get_siteexport_list_and_init_tocs($ID, !empty($_REQUEST['startcounter'])); 1077d101cc1SGerry Weißbach 1087d101cc1SGerry Weißbach if ( $data === false ) { 1097d101cc1SGerry Weißbach header("HTTP/1.0 401 Unauthorized"); 1107d101cc1SGerry Weißbach print 'Unauthorized'; 1117d101cc1SGerry Weißbach exit; 1127d101cc1SGerry Weißbach } 1137d101cc1SGerry Weißbach 1147d101cc1SGerry Weißbach $counter = 0; 1157d101cc1SGerry Weißbach 1167d101cc1SGerry Weißbach if ( count($data) == 0 && !$this->functions->settings->hasValidCacheFile ) { 1177d101cc1SGerry Weißbach exit(); 1187d101cc1SGerry Weißbach } 1197d101cc1SGerry Weißbach 1207d101cc1SGerry Weißbach foreach ( $data as $site ) { 1217d101cc1SGerry Weißbach 1227d101cc1SGerry Weißbach if ( intval($site['exists']) == 1 || !isset($site['exists']) ) { 1237d101cc1SGerry Weißbach 1247d101cc1SGerry Weißbach // Skip over the amount of urls that have been exported already 1257d101cc1SGerry Weißbach if ( empty($_REQUEST['startcounter']) || $counter >= intval($_REQUEST['startcounter']) ) { 1267d101cc1SGerry Weißbach $status = $this->__siteexport_add_site($site['id']); 1278da901a0SGerry Weißbach 1288da901a0SGerry Weißbach if ( $status === false ) { 1298da901a0SGerry Weißbach $this->functions->debug->message("----------------------------------------", null, 1); 1308da901a0SGerry Weißbach $this->functions->debug->message("Errors during export from URL call", null, 1); 1318da901a0SGerry Weißbach $this->functions->debug->message("========================================", null, 1); 1328da901a0SGerry Weißbach print $this->functions->debug->runtimeErrors; 1338da901a0SGerry Weißbach exit(0); // We need to stop 1348da901a0SGerry Weißbach } 1357d101cc1SGerry Weißbach } 1367d101cc1SGerry Weißbach } 1377d101cc1SGerry Weißbach 1387d101cc1SGerry Weißbach $counter ++; 1397d101cc1SGerry Weißbach if ( time() - $starttimer >= $timerdiff ) { 1407d101cc1SGerry Weißbach $this->functions->debug->message("Will Redirect", null, 1); 1417d101cc1SGerry Weißbach $this->handleRuntimeErrorOutput(); 1427d101cc1SGerry Weißbach $this->functions->startRedirctProcess($counter); 1437d101cc1SGerry Weißbach } 1447d101cc1SGerry Weißbach } 1457d101cc1SGerry Weißbach 1468da901a0SGerry Weißbach $this->functions->debug->message("----------------------------------------", null, 1); 1477d101cc1SGerry Weißbach $this->functions->debug->message("Finishing export from URL call", null, 1); 1487d101cc1SGerry Weißbach $this->functions->debug->message("========================================", null, 1); 1497d101cc1SGerry Weißbach 1507d101cc1SGerry Weißbach $this->cleanCacheFiles(); 1517d101cc1SGerry Weißbach 1527d101cc1SGerry Weißbach $URL = ml($this->functions->settings->origZipFile, array('cache' => 'nocache', 'siteexport' => $this->functions->settings->pattern, 'sectok' => getSecurityToken()), true, '&'); 1537d101cc1SGerry Weißbach $this->functions->debug->message("Redirecting to final file", $URL, 2); 1547d101cc1SGerry Weißbach 1557d101cc1SGerry Weißbach $this->handleRuntimeErrorOutput(); 1567d101cc1SGerry Weißbach send_redirect($URL); 1577d101cc1SGerry Weißbach exit(0); // Should not be reached, but anyways 1587d101cc1SGerry Weißbach } 1597d101cc1SGerry Weißbach 1607d101cc1SGerry Weißbach private function handleRuntimeErrorOutput() 1617d101cc1SGerry Weißbach { 1627d101cc1SGerry Weißbach if ( !empty($this->functions->debug->runtimeErrors) ) 1637d101cc1SGerry Weißbach { 1647d101cc1SGerry Weißbach $this->filewriter->__moveDataToZip($this->functions->debug->runtimeErrors, '_runtime_error/' . time() . '.html'); 1657d101cc1SGerry Weißbach } 1667d101cc1SGerry Weißbach } 1677d101cc1SGerry Weißbach 1682270cdc5SGerry Weißbach public function __init_functions($isAJAX=false) 1697d101cc1SGerry Weißbach { 1702270cdc5SGerry Weißbach $this->functions = new siteexport_functions(true, $isAJAX); 1717d101cc1SGerry Weißbach $this->filewriter = new siteexport_zipfilewriter($this->functions); 1727d101cc1SGerry Weißbach 1737d101cc1SGerry Weißbach // Check for PDF Capabilities 1747d101cc1SGerry Weißbach if ( $this->filewriter->canDoPDF() ) { 1757d101cc1SGerry Weißbach $this->functions->settings->fileType = 'pdf'; 1767d101cc1SGerry Weißbach } 1777d101cc1SGerry Weißbach } 1787d101cc1SGerry Weißbach 1797d101cc1SGerry Weißbach /** 1807d101cc1SGerry Weißbach * Prepares the generated URL for direct download access 1817d101cc1SGerry Weißbach * Also gives back the parameters for this URL 1827d101cc1SGerry Weißbach * @param $event init event of the ajax request 1837d101cc1SGerry Weißbach */ 1847d101cc1SGerry Weißbach function ajax_siteexport_prepareURL_and_POSTData( &$event ) { 1857d101cc1SGerry Weißbach 1867d101cc1SGerry Weißbach $event->preventDefault(); 1877d101cc1SGerry Weißbach $event->stopPropagation(); 1887d101cc1SGerry Weißbach 1897d101cc1SGerry Weißbach // Retrieve Information for download URL 190a609ae53SGerry Weißbach $this->functions->debug->message("Prepared URL and POST from Request:", $_REQUEST, 2); 1917d101cc1SGerry Weißbach $url = $this->functions->prepare_POSTData($_REQUEST); 1927d101cc1SGerry Weißbach $combined = $this->functions->urlToPathAndParams($url); 1937d101cc1SGerry Weißbach list($path, $query) = explode('?', $combined, 2); 1947d101cc1SGerry Weißbach $return = array($url, $combined, $path, $query); 1957d101cc1SGerry Weißbach 1967d101cc1SGerry Weißbach $this->functions->debug->message("Prepared URL and POST data:", $return, 2); 1977d101cc1SGerry Weißbach return $return; 1987d101cc1SGerry Weißbach } 1997d101cc1SGerry Weißbach 2007d101cc1SGerry Weißbach /** 2017d101cc1SGerry Weißbach * Executes a Cron Job Action 2027d101cc1SGerry Weißbach * @param $event 2037d101cc1SGerry Weißbach */ 2047d101cc1SGerry Weißbach function ajax_siteexport_cronaction( &$event ) 2057d101cc1SGerry Weißbach { 2067d101cc1SGerry Weißbach $cronOverwriteExisting = intval($_REQUEST['cronOverwriteExisting']) == 1; 2077d101cc1SGerry Weißbach list($url, $combined) = $this->ajax_siteexport_prepareURL_and_POSTData($event); 2087d101cc1SGerry Weißbach 2097d101cc1SGerry Weißbach if ( !$function =& plugin_load('cron', 'siteexport' ) ) 2107d101cc1SGerry Weißbach { 2117d101cc1SGerry Weißbach $this->functions->debug->message("Tried to do an action with siteexport/cron, but the cron plugin is missing.", null, 4); 2127d101cc1SGerry Weißbach } 2137d101cc1SGerry Weißbach 2147d101cc1SGerry Weißbach $status = null; 2157d101cc1SGerry Weißbach switch( $event->data ) { 2167d101cc1SGerry Weißbach case '__siteexport_savecron': $status = $function->saveCronDataWithParameters($combined, $cronOverwriteExisting); break; 2177d101cc1SGerry Weißbach case '__siteexport_deletecron': $status = $function->deleteCronDataWithParameters($combined); break; 2187d101cc1SGerry Weißbach } 2197d101cc1SGerry Weißbach 2207d101cc1SGerry Weißbach if ( !empty($status) ) 2217d101cc1SGerry Weißbach { 2227d101cc1SGerry Weißbach $this->functions->debug->message("Tried to do an action with siteexport/cron, but failed.", $status, 4); 2237d101cc1SGerry Weißbach } 2247d101cc1SGerry Weißbach } 2257d101cc1SGerry Weißbach 2267d101cc1SGerry Weißbach /** 2277d101cc1SGerry Weißbach * generate direct access URL 2287d101cc1SGerry Weißbach **/ 2297d101cc1SGerry Weißbach function ajax_siteexport_generateurl( &$event ) { 2307d101cc1SGerry Weißbach 2317d101cc1SGerry Weißbach list($url, $combined, $path, $POSTData) = $this->ajax_siteexport_prepareURL_and_POSTData($event); 2327d101cc1SGerry Weißbach 2337d101cc1SGerry Weißbach // WGET Redirects - this is an option for wget only. 2347d101cc1SGerry Weißbach // Calculate the maximum redirects that we want to allow. A Problem is that we don't know how long it will take to fetch one page 2357d101cc1SGerry Weißbach // Therefore we assume it takes about 5s for each page - that gives the freedom to have anough time for redirect. 2367d101cc1SGerry Weißbach $maxRedirectNumber = ceil( ( count($this->__get_siteexport_list($NS, true)) * 5) / $this->getConf('max_execution_time') ); 2377d101cc1SGerry Weißbach $maxRedirect = $maxRedirectNumber > 0 ? '--max-redirect=' . ($maxRedirectNumber+3) . ' ' : ''; 2387d101cc1SGerry Weißbach $maxRedirs = $maxRedirectNumber > 0 ? '--max-redirs ' . ($maxRedirectNumber+3) . ' ' : ''; 2397d101cc1SGerry Weißbach 2407d101cc1SGerry Weißbach $this->functions->debug->message("Generating Direct Download URL", $url, 2); 2417d101cc1SGerry Weißbach 2427d101cc1SGerry Weißbach // If there was a Runtime Exception 2437d101cc1SGerry Weißbach if ( !$this->functions->debug->firstRE() ) { 2447d101cc1SGerry Weißbach $this->functions->debug->message("There have been errors while generating the download URLs.", null, 4); 2457d101cc1SGerry Weißbach return; 2467d101cc1SGerry Weißbach } 2477d101cc1SGerry Weißbach 2487d101cc1SGerry Weißbach echo $url; 2497d101cc1SGerry Weißbach echo "\n"; 2507d101cc1SGerry Weißbach echo 'wget ' . $maxRedirect . '--output-document=' . array_pop(explode(":", ($this->getConf('zipfilename')))) . ' --post-data="' . $POSTData . '" ' . wl(cleanID($path), null, true) . ' --http-user=USER --http-passwd=PASSWD'; 2517d101cc1SGerry Weißbach echo "\n"; 2527d101cc1SGerry Weißbach echo 'curl -L ' . $maxRedirs . '-o ' . array_pop(explode(":", ($this->getConf('zipfilename')))) . ' -d "' . $POSTData . '" ' . wl(cleanID($path), null, true) . ' --anyauth --user USER:PASSWD'; 2537d101cc1SGerry Weißbach echo "\n"; 2547d101cc1SGerry Weißbach 2557d101cc1SGerry Weißbach $this->functions->debug->message("Checking for Cron parameters: ", $combined, 1); 2567d101cc1SGerry Weißbach if ( !$functions =& plugin_load('cron', 'siteexport' ) || 2577d101cc1SGerry Weißbach !$functions->hasCronJobForParameters($combined) ) { 2587d101cc1SGerry Weißbach echo "false"; 2597d101cc1SGerry Weißbach } else 2607d101cc1SGerry Weißbach { 2617d101cc1SGerry Weißbach echo "true"; 2627d101cc1SGerry Weißbach } 2637d101cc1SGerry Weißbach 2647d101cc1SGerry Weißbach return; 2657d101cc1SGerry Weißbach } 2667d101cc1SGerry Weißbach 2677d101cc1SGerry Weißbach /** 2687d101cc1SGerry Weißbach * Get List of sites to be exported for AJAX (wrapper) 2697d101cc1SGerry Weißbach **/ 2707d101cc1SGerry Weißbach function ajax_siteexport_getsitelist( &$event ) { 2717d101cc1SGerry Weißbach 2727d101cc1SGerry Weißbach $event->preventDefault(); 2737d101cc1SGerry Weißbach $event->stopPropagation(); 2747d101cc1SGerry Weißbach 2757d101cc1SGerry Weißbach $data = $this->__get_siteexport_list_and_init_tocs($_REQUEST['ns']); 2767d101cc1SGerry Weißbach 2777d101cc1SGerry Weißbach // Important for reconaisance of the session 2787d101cc1SGerry Weißbach 2797d101cc1SGerry Weißbach if ( $data === false ) 2807d101cc1SGerry Weißbach { 2817d101cc1SGerry Weißbach $this->functions->debug->runtimeException("No data generated. List of Files is 'false'."); 2827d101cc1SGerry Weißbach return; 2837d101cc1SGerry Weißbach } 2847d101cc1SGerry Weißbach 2857d101cc1SGerry Weißbach if ( empty($data) && !$this->functions->settings->hasValidCacheFile ) 2867d101cc1SGerry Weißbach { 2877d101cc1SGerry Weißbach $this->functions->debug->runtimeException("Generated list is empty."); 2887d101cc1SGerry Weißbach return; 2897d101cc1SGerry Weißbach } 2907d101cc1SGerry Weißbach 2917d101cc1SGerry Weißbach // If there was a Runtime Exception 2927d101cc1SGerry Weißbach if ( !$this->functions->debug->firstRE() ) 2937d101cc1SGerry Weißbach { 2947d101cc1SGerry Weißbach $this->functions->debug->message("There have been errors while generating site list.", null, 4); 2957d101cc1SGerry Weißbach return; 2967d101cc1SGerry Weißbach } 2977d101cc1SGerry Weißbach 2987d101cc1SGerry Weißbach echo "{$this->functions->settings->pattern}\n"; 2997d101cc1SGerry Weißbach echo $this->functions->downloadURL() . "\n"; 3007d101cc1SGerry Weißbach foreach($data as $line ){ 3017d101cc1SGerry Weißbach echo $line['id'] . "\n"; 3027d101cc1SGerry Weißbach } 3037d101cc1SGerry Weißbach 3047d101cc1SGerry Weißbach return; 3057d101cc1SGerry Weißbach } 3067d101cc1SGerry Weißbach 3076792d0cfSGerry Weißbach function ajax_siteexport_aggregate( &$event ) { 3086792d0cfSGerry Weißbach 3096792d0cfSGerry Weißbach // Quick preparations for one page only 3106792d0cfSGerry Weißbach if ( $this->filewriter->hasValidCacheFile($_REQUEST, $data) ) { 3116792d0cfSGerry Weißbach $this->functions->debug->message("Had a valid cache file and will use it.", null, 2); 3126792d0cfSGerry Weißbach print $this->functions->downloadURL(); 313b88e443eSGerry Weißbach 314b88e443eSGerry Weißbach $event->preventDefault(); 315b88e443eSGerry Weißbach $event->stopPropagation(); 3166792d0cfSGerry Weißbach } else { 3176792d0cfSGerry Weißbach // Then go for it! 3186792d0cfSGerry Weißbach $this->functions->debug->message("Will create a new cache thing.", null, 2); 3196792d0cfSGerry Weißbach $this->ajax_siteexport_addsite( $event ); 3206792d0cfSGerry Weißbach } 3216792d0cfSGerry Weißbach 3226792d0cfSGerry Weißbach } 3236792d0cfSGerry Weißbach 3247d101cc1SGerry Weißbach /** 3257d101cc1SGerry Weißbach * Add a page to the package (for AJAX calls - Wrapper) 3267d101cc1SGerry Weißbach **/ 3277d101cc1SGerry Weißbach function ajax_siteexport_addsite( &$event ) { 3287d101cc1SGerry Weißbach 3297d101cc1SGerry Weißbach $event->preventDefault(); 3307d101cc1SGerry Weißbach $event->stopPropagation(); 3317d101cc1SGerry Weißbach 3327d101cc1SGerry Weißbach $this->functions->debug->message("========================================", null, 1); 3337d101cc1SGerry Weißbach $this->functions->debug->message("Starting export from AJAX call", null, 1); 3348da901a0SGerry Weißbach $this->functions->debug->message("----------------------------------------", null, 1); 3357d101cc1SGerry Weißbach 3367d101cc1SGerry Weißbach $status = $this->__siteexport_add_site($_REQUEST['site']); 3378da901a0SGerry Weißbach if ( $status === false ) { 3388da901a0SGerry Weißbach $this->functions->debug->message("----------------------------------------", null, 1); 3398da901a0SGerry Weißbach $this->functions->debug->message("Errors during export from AJAX call", null, 1); 3408da901a0SGerry Weißbach $this->functions->debug->message("========================================", null, 1); 3418da901a0SGerry Weißbach return; 3428da901a0SGerry Weißbach } 3437d101cc1SGerry Weißbach 3448da901a0SGerry Weißbach $this->functions->debug->message("----------------------------------------", null, 1); 3457d101cc1SGerry Weißbach $this->functions->debug->message("Finishing export from AJAX call", null, 1); 3467d101cc1SGerry Weißbach $this->functions->debug->message("========================================", null, 1); 3477d101cc1SGerry Weißbach 3487d101cc1SGerry Weißbach // Print the download zip-File 3497d101cc1SGerry Weißbach $this->cleanCacheFiles(); 3507d101cc1SGerry Weißbach 3517d101cc1SGerry Weißbach // If there was a Runtime Exception 3527d101cc1SGerry Weißbach if ( !$this->functions->debug->firstRE() ) { 3537d101cc1SGerry Weißbach $this->functions->debug->message("There have been errors during the export.", null, 4); 3547d101cc1SGerry Weißbach return; 3557d101cc1SGerry Weißbach } 3567d101cc1SGerry Weißbach 3577d101cc1SGerry Weißbach print $this->functions->downloadURL(); 3587d101cc1SGerry Weißbach return; 3597d101cc1SGerry Weißbach } 3607d101cc1SGerry Weißbach 3617d101cc1SGerry Weißbach /** 3627d101cc1SGerry Weißbach * Fetch the list of pages to be exported 3637d101cc1SGerry Weißbach **/ 3647d101cc1SGerry Weißbach function __get_siteexport_list($NS, $overrideCache=false) { 3657d101cc1SGerry Weißbach global $conf; 3667d101cc1SGerry Weißbach 3677d101cc1SGerry Weißbach $NS = $this->namespace = $this->functions->getNamespaceFromID($NS, $PAGE); 3687d101cc1SGerry Weißbach 3697d101cc1SGerry Weißbach $depth = $this->getConf('depth'); 3707d101cc1SGerry Weißbach $query = ''; 3717d101cc1SGerry Weißbach $doSearch = 'search_allpages'; 3727d101cc1SGerry Weißbach 3737d101cc1SGerry Weißbach switch( intval($_REQUEST['depthType']) ) { 3747d101cc1SGerry Weißbach case 0: 3757d101cc1SGerry Weißbach $query = $this->functions->cleanID(str_replace(":", "/", $NS.':'.$PAGE)); 3767d101cc1SGerry Weißbach resolve_pageid($NS, $PAGE, $exists); 3777d101cc1SGerry Weißbach 3787d101cc1SGerry Weißbach if ( $exists ) { 3797d101cc1SGerry Weißbach $data = array( array( 'id' => $PAGE) ); 3807d101cc1SGerry Weißbach 3817d101cc1SGerry Weißbach $this->functions->debug->message("Checking for Cache", null, 2); 3827d101cc1SGerry Weißbach if ( !$overrideCache && $this->filewriter->hasValidCacheFile($_REQUEST, $data) ) 3837d101cc1SGerry Weißbach { 3847d101cc1SGerry Weißbach return array(); 3857d101cc1SGerry Weißbach } 3867d101cc1SGerry Weißbach 3877d101cc1SGerry Weißbach return $data; 3887d101cc1SGerry Weißbach } 3897d101cc1SGerry Weißbach case 1: $depth = 0; 3907d101cc1SGerry Weißbach break; 3917d101cc1SGerry Weißbach case 2: $depth = intval($_REQUEST['depth']); 3927d101cc1SGerry Weißbach break; 3937d101cc1SGerry Weißbach } 3947d101cc1SGerry Weißbach 3957d101cc1SGerry Weißbach $opts = array( 'depth' => $depth, 'skipacl' => $this->getConf('skipacl'), 'query' => $query); 396a609ae53SGerry Weißbach $this->functions->debug->message("Options", $opts, 2); 397a609ae53SGerry Weißbach 3987d101cc1SGerry Weißbach $data = array(); 3997d101cc1SGerry Weißbach require_once (DOKU_INC.'inc/search.php'); 4007d101cc1SGerry Weißbach 4017d101cc1SGerry Weißbach // Check, which TOC to take 4027d101cc1SGerry Weißbach if ( !$this->functions->settings->useTOCFile ) { 4037d101cc1SGerry Weißbach search($data, $conf['datadir'], $doSearch, $opts, $this->namespace); 4047d101cc1SGerry Weißbach } else { 4057d101cc1SGerry Weißbach $this->functions->debug->message("Using TOC for data", null, 2); 4067d101cc1SGerry Weißbach 4077d101cc1SGerry Weißbach $doSearch = 'search_pagename'; 4087d101cc1SGerry Weißbach 4097d101cc1SGerry Weißbach // Create Data of the TOC File should be used instead 4107d101cc1SGerry Weißbach $opts['query'] = 'toc.txt'; 4117d101cc1SGerry Weißbach 4127d101cc1SGerry Weißbach $RAWdata = array(); 4137d101cc1SGerry Weißbach search($RAWdata, $conf['datadir'], $doSearch, $opts, $this->namespace); 4147d101cc1SGerry Weißbach 4157d101cc1SGerry Weißbach // There may be more than one toc and all of them have to be merged. 4167d101cc1SGerry Weißbach $data = array(); 4177d101cc1SGerry Weißbach foreach( $RAWdata as $entry ) 4187d101cc1SGerry Weißbach { 4197d101cc1SGerry Weißbach $tmpData = p_get_metadata($entry['id'], 'sitetoc siteexportTOC', true); 4207d101cc1SGerry Weißbach 4217d101cc1SGerry Weißbach if ( is_array($tmpData) ) 4227d101cc1SGerry Weißbach { 4237d101cc1SGerry Weißbach $data = array_merge($data, $tmpData); 4247d101cc1SGerry Weißbach } 4257d101cc1SGerry Weißbach } 4267d101cc1SGerry Weißbach } 4277d101cc1SGerry Weißbach 4287d101cc1SGerry Weißbach $this->functions->debug->message("Checking for Cache", null, 2); 4297d101cc1SGerry Weißbach if ( !$overrideCache && $this->filewriter->hasValidCacheFile($_REQUEST, $data) ) 4307d101cc1SGerry Weißbach { 4317d101cc1SGerry Weißbach return array(); 4327d101cc1SGerry Weißbach } 4337d101cc1SGerry Weißbach 4347d101cc1SGerry Weißbach $this->functions->debug->message("Exporting the following sites: ", $data, 2); 4357d101cc1SGerry Weißbach return $data; 4367d101cc1SGerry Weißbach } 4377d101cc1SGerry Weißbach 4387d101cc1SGerry Weißbach function __get_siteexport_list_and_init_tocs($NS, $isRedirected=false ) { 4397d101cc1SGerry Weißbach 4407d101cc1SGerry Weißbach // Clean up if not redirected 4417d101cc1SGerry Weißbach if ( !$isRedirected && !$this->__removeOldZip() ) { 4427d101cc1SGerry Weißbach $this->functions->debug->runtimeException("Can't remove old files."); 4437d101cc1SGerry Weißbach return false; 4447d101cc1SGerry Weißbach } 4457d101cc1SGerry Weißbach 4467d101cc1SGerry Weißbach $data = $this->__get_siteexport_list($NS, $isRedirected); 4477d101cc1SGerry Weißbach if ( $isRedirected || empty($data) ) 4487d101cc1SGerry Weißbach { 4497d101cc1SGerry Weißbach // if we have been redirected, simply return the data 4507d101cc1SGerry Weißbach return $data; 4517d101cc1SGerry Weißbach } 4527d101cc1SGerry Weißbach 4537d101cc1SGerry Weißbach // Create Eclipse Documentation Pages - TOC.xml, Context.xml 4547d101cc1SGerry Weißbach if ( !empty($_REQUEST['absolutePath']) ) $this->namespace = ""; 4557d101cc1SGerry Weißbach// $this->__removeOldZip( $this->functions->settings->eclipseZipFile ); 4567d101cc1SGerry Weißbach 4577d101cc1SGerry Weißbach if ( !empty($_REQUEST['eclipseDocZip']) ) 4587d101cc1SGerry Weißbach { 4597d101cc1SGerry Weißbach $toc = new siteexport_toc($this->functions); 4607d101cc1SGerry Weißbach $this->functions->debug->message("Generating eclipseDocZip", null, 2); 4617d101cc1SGerry Weißbach $this->filewriter->__moveDataToZip($toc->__getTOCXML($data), 'toc.xml'); 4627d101cc1SGerry Weißbach $this->filewriter->__moveDataToZip($toc->__getContextXML($data), 'context.xml'); 4637d101cc1SGerry Weißbach } else if ( !empty($_REQUEST['JavaHelpDocZip']) ) 4647d101cc1SGerry Weißbach { 4657d101cc1SGerry Weißbach $toc = new siteexport_javahelp($this->functions, $this->filewriter); 4667d101cc1SGerry Weißbach $toc->createTOCFiles($data); 4677d101cc1SGerry Weißbach 4687d101cc1SGerry Weißbach/* $toc = new siteexport_toc($this->functions); 4697d101cc1SGerry Weißbach list($tocData, $mapData) = $toc->__getJavaHelpTOCXML($data); 4707d101cc1SGerry Weißbach $this->functions->debug->message("Generating JavaHelpDocZip", null, 2); 4717d101cc1SGerry Weißbach $this->filewriter->__moveDataToZip($tocData, 'toc.xml'); 4727d101cc1SGerry Weißbach $this->filewriter->__moveDataToZip($mapData, 'map.xml'); 4737d101cc1SGerry Weißbach*/ } 4747d101cc1SGerry Weißbach 4757d101cc1SGerry Weißbach return $data; 4767d101cc1SGerry Weißbach } 4777d101cc1SGerry Weißbach 4787d101cc1SGerry Weißbach /** 4797d101cc1SGerry Weißbach * Add page with ID to the package 4807d101cc1SGerry Weißbach **/ 4817d101cc1SGerry Weißbach function __siteexport_add_site( $ID ) { 4827d101cc1SGerry Weißbach global $conf, $currentID; 4837d101cc1SGerry Weißbach 4847d101cc1SGerry Weißbach // Which is the current ID? 4857d101cc1SGerry Weißbach $currentID = $ID; 4867d101cc1SGerry Weißbach 4877d101cc1SGerry Weißbach $this->functions->debug->message("========================================", null, 2); 4887d101cc1SGerry Weißbach $this->functions->debug->message("Adding Site: '$ID'", null, 2); 4894b7d84d7SGerry Weißbach $this->functions->debug->message("----------------------------------------", $_REQUEST, 2); 4907d101cc1SGerry Weißbach 4917d101cc1SGerry Weißbach $request = $this->functions->settings->additionalParameters; 4927d101cc1SGerry Weißbach unset($request['diPlu']); // This will not be needed for the first request. 4937d101cc1SGerry Weißbach unset($request['diInv']); // This will not be needed for the first request. 4947d101cc1SGerry Weißbach 4957d101cc1SGerry Weißbach // say, what to export and Build URL 4967d101cc1SGerry Weißbach // http://documentation:81/helpdesk/de/hds/getting-started?depthType=0&do=siteexport&ens=helpdesk%3Ade%3Ahds%3Agetting-started&pdfExport=1&renderer=siteexport_siteexportpdf&template=helpdesk 4977d101cc1SGerry Weißbach 4987d101cc1SGerry Weißbach $do = (intval($_REQUEST['exportbody']) == 1 ? (empty($_REQUEST['renderer']) ? $conf['renderer_xhtml'] : $_REQUEST['renderer'] ) : '' ); 4997d101cc1SGerry Weißbach 5007d101cc1SGerry Weißbach if ($do == 'pdf' && $this->filewriter->canDoPDF() ) 5017d101cc1SGerry Weißbach { 5027d101cc1SGerry Weißbach $do = 'export_siteexport_pdf'; 5037d101cc1SGerry Weißbach $_REQUEST['origRenderer'] = (empty($_REQUEST['renderer']) ? $conf['renderer_xhtml'] : $_REQUEST['renderer'] ); 5047d101cc1SGerry Weißbach } 5057d101cc1SGerry Weißbach 5067d101cc1SGerry Weißbach $do = ($do == $conf['renderer_xhtml'] && intval($_REQUEST['exportbody']) != 1) ? '' : 'export_' . $do; 5077d101cc1SGerry Weißbach 5087d101cc1SGerry Weißbach if ( $do != 'export_' && !empty($do) ) 5097d101cc1SGerry Weißbach { 5107d101cc1SGerry Weißbach $request['do'] = $do; 5117d101cc1SGerry Weißbach } 5127d101cc1SGerry Weißbach 5137d101cc1SGerry Weißbach // set Template 5147d101cc1SGerry Weißbach if ( !empty( $_REQUEST['template'] ) ) { 5157d101cc1SGerry Weißbach $request['template'] = $_REQUEST['template']; 5167d101cc1SGerry Weißbach } 5177d101cc1SGerry Weißbach 5187d101cc1SGerry Weißbach $this->functions->debug->message("REQUEST for add_site:", $request, 2); 5197d101cc1SGerry Weißbach 5207d101cc1SGerry Weißbach $ID = $this->functions->cleanID($ID); 5217d101cc1SGerry Weißbach $url = $this->functions->wl($ID, $request, true, '&'); 5227d101cc1SGerry Weißbach 5237d101cc1SGerry Weißbach // Parse URI PATH and add "html" 5247d101cc1SGerry Weißbach $fileName = $this->functions->getSiteName($ID, true); 5256792d0cfSGerry Weißbach $this->functions->debug->message("Filename could be:", $fileName, 2); 5267d101cc1SGerry Weißbach 5277d101cc1SGerry Weißbach $this->fileChecked[$url] = $fileName; // 2010-09-03 - One URL to one FileName 5287d101cc1SGerry Weißbach $this->functions->settings->depth = str_repeat('../', count(explode('/', $fileName))-1); 5297d101cc1SGerry Weißbach 5307d101cc1SGerry Weißbach // fetch URL and save it in temp file 5317d101cc1SGerry Weißbach $tmpFile = $this->__getHTTPFile($url); 5327d101cc1SGerry Weißbach if ( $tmpFile === false ) { 5338da901a0SGerry Weißbach // return $this->functions->debug->message("Creating temporary download file failed for '$url'. See log for more information."); 5348da901a0SGerry Weißbach $this->functions->debug->runtimeException("Creating temporary download file failed for '$url'. See log for more information."); 5358da901a0SGerry Weißbach return false; 5367d101cc1SGerry Weißbach } 5377d101cc1SGerry Weißbach 538281ed919SGerry Weißbach $dirname = dirname($fileName); 5397d101cc1SGerry Weißbach // If a Filename was given that does not comply to the original name, use this one! 540281ed919SGerry Weißbach if ( $this->filewriter->canDoPDF() ) { 5417d101cc1SGerry Weißbach 542281ed919SGerry Weißbach $this->functions->debug->message("Will replace old filename '{$fileName}' with {$tmpFile[2]}", null, 1); 543281ed919SGerry Weißbach $extension = array_pop(explode('.', $fileName)); 54446ccacefSGerry Weißbach 54546ccacefSGerry Weißbach // 2014-04-29 added cleanID to ensure that links are generated consistently when using [[this>...]] or another local, relativ linking 54646ccacefSGerry Weißbach $fileName = $dirname . '/' . $this->functions->cleanID($this->functions->getSiteTitle($ID)) . '.' . $extension; 547281ed919SGerry Weißbach } else if ( !empty($tmpFile[1]) && !strstr($DATA[2], $tmpFile[1]) ) { 5487d101cc1SGerry Weißbach 5496792d0cfSGerry Weißbach $this->functions->debug->message("Will replace old filename '{$fileName}' with {$dirname}/{$tmpFile[1]}", null, 1); 550281ed919SGerry Weißbach $fileName = $dirname . '/' . $tmpFile[1]; 5517d101cc1SGerry Weißbach } 5527d101cc1SGerry Weißbach 5537d101cc1SGerry Weißbach // Add to zip 55484d65497SGerry Weißbach $this->fileChecked[$url] = $fileName; 5557d101cc1SGerry Weißbach $status = $this->filewriter->__addFileToZip($tmpFile[0], $fileName); 5567d101cc1SGerry Weißbach @unlink($tmpFile[0]); 5577d101cc1SGerry Weißbach 5587d101cc1SGerry Weißbach return $status; 5597d101cc1SGerry Weißbach } 5607d101cc1SGerry Weißbach 561d3cbbad8SGerry Weißbach function __preg_quote($input) { 562d3cbbad8SGerry Weißbach return preg_quote($input, '/'); 563d3cbbad8SGerry Weißbach } 564d3cbbad8SGerry Weißbach 5657d101cc1SGerry Weißbach /** 5667d101cc1SGerry Weißbach * Download the file via HTTP URL + recurse if this is not an image 5677d101cc1SGerry Weißbach * The file will be saved as temporary file. The filename is the result. 5687d101cc1SGerry Weißbach **/ 5697d101cc1SGerry Weißbach function __getHTTPFile($URL, $RECURSE=false, $newAdditionalParameters=null) { 5707d101cc1SGerry Weißbach global $conf; 5717d101cc1SGerry Weißbach 572d3cbbad8SGerry Weißbach $EXCLUDE = $this->getConf('exclude'); 573d3cbbad8SGerry Weißbach if ( !empty($EXCLUDE) ) { 574d3cbbad8SGerry Weißbach $PATTERN = "/(" . implode('|', explode(' ', preg_quote($EXCLUDE, '/'))) . ")/i"; 5757d101cc1SGerry Weißbach 576d3cbbad8SGerry Weißbach $this->functions->debug->message("Checking for exclude: ", array( 577d3cbbad8SGerry Weißbach "pattern" => $PATTERN, 578d3cbbad8SGerry Weißbach "file" => $URL, 579d3cbbad8SGerry Weißbach "matches" => preg_match($PATTERN, $URL) ? 'match' : 'no match' 580d3cbbad8SGerry Weißbach ), 2); 581d3cbbad8SGerry Weißbach 582d3cbbad8SGerry Weißbach if ( preg_match($PATTERN, $URL) ) { return false; } 583d3cbbad8SGerry Weißbach } 5847d101cc1SGerry Weißbach 5857d101cc1SGerry Weißbach require_once( DOKU_INC . 'inc/HTTPClient.php'); 5867d101cc1SGerry Weißbach 5878da901a0SGerry Weißbach $http = new HTTPProxy($this->functions->debug, $this->functions->settings); 5887d101cc1SGerry Weißbach $http->max_bodysize = $conf['fetchsize']; 5897d101cc1SGerry Weißbach // $http->user = $_SERVER['PHP_AUTH_USER']; // Must not be set, or the files will be authenticated and have the edit thingies 5907d101cc1SGerry Weißbach // $http->pass = $_SERVER['PHP_AUTH_PW']; // Must not be set, or the files will be authenticated and have the edit thingies 5917d101cc1SGerry Weißbach 5927d101cc1SGerry Weißbach // Add additional Params 5937d101cc1SGerry Weißbach $this->functions->addAdditionalParametersToURL($URL, $newAdditionalParameters); 5947d101cc1SGerry Weißbach 5957d101cc1SGerry Weißbach $this->functions->debug->message("Fetching URL: '$URL'", null, 2); 596*9d84786fSGerry Weißbach $getData = $http->get($URL, true); // true == sloopy, get 304 body as well. 5977d101cc1SGerry Weißbach 5987d101cc1SGerry Weißbach if( $getData === false ) { 599cb168401SGerry Weißbach 600cb168401SGerry Weißbach if ( $this->functions->settings->ignoreNon200 ) { 601cb168401SGerry Weißbach return null; 602cb168401SGerry Weißbach } 603cb168401SGerry Weißbach 6047d101cc1SGerry Weißbach $this->functions->debug->message("Sending request failed with error, HTTP status was '{$http->status}'.", $URL, 4); 6057d101cc1SGerry Weißbach return false; 6067d101cc1SGerry Weißbach } 6077d101cc1SGerry Weißbach 6087d101cc1SGerry Weißbach if( empty($getData) ) { 6097d101cc1SGerry Weißbach $this->functions->debug->message("No data fetched.", null , 4); 6107d101cc1SGerry Weißbach return false; 6117d101cc1SGerry Weißbach } 6127d101cc1SGerry Weißbach 613281ed919SGerry Weißbach $this->functions->debug->message("Headers received", $http->resp_headers, 2); 614281ed919SGerry Weißbach 6157d101cc1SGerry Weißbach if ( !$RECURSE ) { 6167d101cc1SGerry Weißbach // Parse URI PATH and add "html" 6177d101cc1SGerry Weißbach $this->functions->debug->message("========================================", null, 1); 6187d101cc1SGerry Weißbach $this->functions->debug->message("Starting to recurse file '$URL'", null , 1); 6198da901a0SGerry Weißbach $this->functions->debug->message("----------------------------------------", null, 1); 6207d101cc1SGerry Weißbach $this->__getInternalLinks($getData); 6218da901a0SGerry Weißbach $this->functions->debug->message("----------------------------------------", null, 1); 6227d101cc1SGerry Weißbach $this->functions->debug->message("Finished to recurse file '$URL'", null , 1); 6237d101cc1SGerry Weißbach $this->functions->debug->message("========================================", null, 1); 6247d101cc1SGerry Weißbach } 6257d101cc1SGerry Weißbach 626*9d84786fSGerry Weißbach $tmpFile = tempnam($this->functions->settings->tmpDir , 'siteexport__'); 627*9d84786fSGerry Weißbach $this->functions->debug->message("Temporary filename", $tmpFile, 1); 628*9d84786fSGerry Weißbach 629*9d84786fSGerry Weißbach $fp = fopen( $tmpFile, "w"); 630*9d84786fSGerry Weißbach if(!$fp) { 631*9d84786fSGerry Weißbach $this->functions->debug->message("Can't open temporary File '$tmpFile'.", null , 4); 632*9d84786fSGerry Weißbach return false; 633*9d84786fSGerry Weißbach } 634*9d84786fSGerry Weißbach 6357d101cc1SGerry Weißbach fwrite($fp,$getData); 6367d101cc1SGerry Weißbach fclose($fp); 6377d101cc1SGerry Weißbach 638*9d84786fSGerry Weißbach return array($tmpFile, preg_replace("/.*?filename=\"?(.*?)\"?;?$/", "$1", $http->resp_headers['content-disposition'], strrev(array_shift(explode('/', strrev($http->resp_headers['content-type']), 2))))); 6397d101cc1SGerry Weißbach } 6407d101cc1SGerry Weißbach 6417d101cc1SGerry Weißbach /** 6427d101cc1SGerry Weißbach * Find internal links in the currently downloaded file. This also matches inside CSS files 6437d101cc1SGerry Weißbach **/ 6447d101cc1SGerry Weißbach function __getInternalLinks(&$DATA) { 6457d101cc1SGerry Weißbach 6467d101cc1SGerry Weißbach $PATTERN = '(href|src|action)="([^"]*)"'; 6477d101cc1SGerry Weißbach $CALLBACK = array($this, '__fetchAndReplaceLink'); 6487d101cc1SGerry Weißbach $DATA = preg_replace_callback("/$PATTERN/i", $CALLBACK, $DATA); 6497d101cc1SGerry Weißbach 6500fc5c0e0SGerry Weißbach $PATTERNCSS = '(url\s*?)\(([^\)]*)\)'; 6517d101cc1SGerry Weißbach $DATA = preg_replace_callback("/$PATTERNCSS/i", $CALLBACK, $DATA); 6527d101cc1SGerry Weißbach } 6537d101cc1SGerry Weißbach 6547d101cc1SGerry Weißbach /** 6557d101cc1SGerry Weißbach * Deep Fetch and replace of links inside the texts matched by __getInternalLinks 6567d101cc1SGerry Weißbach **/ 6577d101cc1SGerry Weißbach function __fetchAndReplaceLink($DATA) { 6587d101cc1SGerry Weißbach global $conf, $currentID; 6597d101cc1SGerry Weißbach 6607d101cc1SGerry Weißbach $noDeepReplace = true; 6617d101cc1SGerry Weißbach $newAdditionalParameters = $this->functions->settings->additionalParameters; 6627d101cc1SGerry Weißbach $newDepth = $this->functions->settings->depth; 6637d101cc1SGerry Weißbach $hadBase = false; 6647d101cc1SGerry Weißbach 665d3cbbad8SGerry Weißbach // Clean data[2], remote ' and " 666d3cbbad8SGerry Weißbach $DATA[2] = preg_replace("/^\s*?['\"]?(.*?)['\"]?\s*?$/", '\1', trim($DATA[2])); 667d3cbbad8SGerry Weißbach 66867f4f6e5SGerry Weißbach $this->functions->debug->message("Starting Link Replacement", array( 'data' => $DATA, 'additional Params' => $newAdditionalParameters, 'newDepth' => $newDepth, 'currentID' => $currentID), 2); 6697d101cc1SGerry Weißbach 6707d101cc1SGerry Weißbach // $DATA[2] = urldecode($DATA[2]); // Leads to problems because it does not re-encode the url 6717d101cc1SGerry Weißbach // External and mailto links 6727d101cc1SGerry Weißbach if ( preg_match("%^(https?://|mailto:|javascript:|data:)%", $DATA[2]) ) { 6737d101cc1SGerry Weißbach $this->functions->debug->message("Don't like http, mailto, data or javascript links here", null, 1); 6747d101cc1SGerry Weißbach return $this->__rebuildLink($DATA, ""); 6757d101cc1SGerry Weißbach } 6767d101cc1SGerry Weißbach //if ( preg_match("%^(https?://|mailto:|" . DOKU_BASE . "/_export/)%", $DATA[2]) ) { return $this->__rebuildLink($DATA, ""); } 6777d101cc1SGerry Weißbach // External media - this is deep down in the link, so we have to grep it out 6787d101cc1SGerry Weißbach if ( preg_match("%media=(https?://.*?$)%", $DATA[2], $matches) ) { 6797d101cc1SGerry Weißbach $DATA[2] = $matches[1]; 6807d101cc1SGerry Weißbach $this->functions->debug->message("This is an HTTP like somewhere else", $DATA, 1); 6817d101cc1SGerry Weißbach return $this->__rebuildLink($DATA, ""); 6827d101cc1SGerry Weißbach } 6837d101cc1SGerry Weißbach // reference only links won't have to be rewritten 6847d101cc1SGerry Weißbach if ( preg_match("%^#.*?$%", $DATA[2]) ) { 6857d101cc1SGerry Weißbach $this->functions->debug->message("This is a refercence only", null, 1); 6867d101cc1SGerry Weißbach return $this->__rebuildLink($DATA, ""); 6877d101cc1SGerry Weißbach } 6887d101cc1SGerry Weißbach 6897d101cc1SGerry Weißbach // strip all things out 6907d101cc1SGerry Weißbach // changed Data 6917d101cc1SGerry Weißbach $PARAMS = @parse_url($DATA[2], PHP_URL_QUERY); 6927d101cc1SGerry Weißbach $ANCHOR = @parse_url($DATA[2], PHP_URL_FRAGMENT); 6937d101cc1SGerry Weißbach $DATA[2] = @parse_url($DATA[2], PHP_URL_PATH); 6947d101cc1SGerry Weißbach 69567f4f6e5SGerry Weißbach // 2014-05-12 - fix problem with URLs starting with a ./ or ../ ... they seem to need the current IDs root 69667f4f6e5SGerry Weißbach if ( preg_match("#^..?/#", $DATA[2])) { 69767f4f6e5SGerry Weißbach $DATA[2] = getNS($currentID) . ':' . $DATA[2]; 69867f4f6e5SGerry Weißbach } 69967f4f6e5SGerry Weißbach 7007d101cc1SGerry Weißbach // 2010-08-25 - fix problem with relative movement in links ( "test/../test2" ) 7017d101cc1SGerry Weißbach $tmpData2 = ''; 7027d101cc1SGerry Weißbach while( $tmpData2 != $DATA[2] ) { 7037d101cc1SGerry Weißbach $tmpData2 = $DATA[2]; 7047d101cc1SGerry Weißbach $DATA[2] = preg_replace("#/(?!\.\.)[^\/]*?/\.\./#", '/', $DATA[2]); 7057d101cc1SGerry Weißbach } 7067d101cc1SGerry Weißbach 7077d101cc1SGerry Weißbach $temp = preg_replace("%^" . DOKU_BASE . "%", "", $DATA[2]); 7087d101cc1SGerry Weißbach if ( $temp != $DATA[2] ) { 7097d101cc1SGerry Weißbach $DATA[2] = $temp; 7107d101cc1SGerry Weißbach $hadBase = true; // 2010-08-23 Check if there has been a rewrite here that will have to be considered later on 7117d101cc1SGerry Weißbach } 7127d101cc1SGerry Weißbach 7137d101cc1SGerry Weißbach $this->functions->debug->message("URL before rewriting option for others than 1", array($DATA, $PARAMS, $hadBase), 1); 7147d101cc1SGerry Weißbach 715d3cbbad8SGerry Weißbach // Handle rewrites other than 1 - just for non-lib-files 716d3cbbad8SGerry Weißbach // if ( !preg_match('$^/?lib/$', $DATA[2]) ) { 717d3cbbad8SGerry Weißbach if ( !preg_match('$^(' . DOKU_BASE . ')?lib/$', $DATA[2]) ) { 71867f4f6e5SGerry Weißbach $this->functions->debug->message("Did not match '$^(" . DOKU_BASE . ")?lib/$' userewrite == {$conf['userewrite']}", null, 2); 7197d101cc1SGerry Weißbach if ( $conf['userewrite'] == 2 ) { 7207d101cc1SGerry Weißbach $DATA[2] = $this->__getInternalRewriteURL($DATA[2]); 7217d101cc1SGerry Weißbach } elseif ( $conf['userewrite'] == 0 ) { 7227d101cc1SGerry Weißbach $this->__getParamsAndDataRewritten($DATA, $PARAMS); 7237d101cc1SGerry Weißbach } 7240fc5c0e0SGerry Weißbach } else { 7250fc5c0e0SGerry Weißbach $this->functions->debug->message("This file must be inside lib ...", null, 2); 7267d101cc1SGerry Weißbach } 7277d101cc1SGerry Weißbach 7287d101cc1SGerry Weißbach $this->functions->debug->message("URL before rewriting option", array($DATA, $PARAMS), 2); 7297d101cc1SGerry Weißbach 7307d101cc1SGerry Weißbach $ORIGDATA2 = $DATA; 7317d101cc1SGerry Weißbach // $ORIGDATA2 = $DATA[2]; // 08/10/2010 - this line required a $this->functions->wl which may mess up with the base URL 7327d101cc1SGerry Weißbach $this->functions->debug->message("OrigDATA is:", $ORIGDATA2, 1); 7337d101cc1SGerry Weißbach 7347d101cc1SGerry Weißbach // Generate ID 7357d101cc1SGerry Weißbach $DATA[2] = str_replace('/', ':', $DATA[2]); 7367d101cc1SGerry Weißbach 7377d101cc1SGerry Weißbach // If Data was empty this must be the same file!; 7387d101cc1SGerry Weißbach if ( empty( $DATA[2] ) ) { 7397d101cc1SGerry Weißbach $DATA[2] = $currentID; 7407d101cc1SGerry Weißbach } 7417d101cc1SGerry Weißbach 7427d101cc1SGerry Weißbach $ID = $DATA[2]; 7437d101cc1SGerry Weißbach $MEDIAMATCHER = "#(_media(/|:)|media=|_detail(/|:)|_export(/|:)|do=export_)#i"; // 2010-10-23 added "(/|:)" for the ID may not contain slashes anymore 7447d101cc1SGerry Weißbach $ID = $this->functions->cleanID($DATA[2], null, preg_match($MEDIAMATCHER, $DATA[2]) ); 7457d101cc1SGerry Weißbach // $ID = $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'media') ); // Export anpassung nun weiter unten 7467d101cc1SGerry Weißbach 7477d101cc1SGerry Weißbach // $IDexists = page_exists($ID); // 08/10/2010 - Not needed. This will be done in the next block. 7487d101cc1SGerry Weißbach // $this->functions->debug->message("Current ID: '$ID' exists: '" . ($IDexists ? 'true' : 'false') . "' (will be set to 'false' anyway)", null, 1); 7497d101cc1SGerry Weißbach 7507d101cc1SGerry Weißbach $IDifIDnotExists = $ID; // 08/10/2010 - Save ID - with possible upper cases to preserve them 7517d101cc1SGerry Weißbach $IDexists = false; 7527d101cc1SGerry Weißbach 7537d101cc1SGerry Weißbach $this->functions->debug->message("Resolving ID: '$ID'", null, 2); 7547d101cc1SGerry Weißbach if ( preg_match($MEDIAMATCHER, $DATA[2]) ) { 7557d101cc1SGerry Weißbach resolve_mediaid(null, $ID, $IDexists); 7567d101cc1SGerry Weißbach 7577d101cc1SGerry Weißbach $this->functions->debug->message("Current mediaID to filename: '" . mediaFN($ID) . "'", null, 2); 7587d101cc1SGerry Weißbach } else { 7597d101cc1SGerry Weißbach resolve_pageid(null, $ID, $IDexists); 7607d101cc1SGerry Weißbach $this->functions->debug->message("Current ID to filename: '" . wikiFN($ID) . "'", null, 2); 7617d101cc1SGerry Weißbach } 7627d101cc1SGerry Weißbach 7637d101cc1SGerry Weißbach $this->functions->debug->message("Current ID after resolvement: '$ID' the ID does exist: '" . ($IDexists ? 'true' : 'false') . "'", null, 2); 7647d101cc1SGerry Weißbach // $ORIGDATA2 = @parse_url($this->functions->wl($ORIGDATA2, null, true)); // What was the next 2 line for? It did mess up with links from {{jdoc>}} 7657d101cc1SGerry Weißbach // $this->functions->debug->message("OrigData ID after parse:", $ORIGDATA2, 1); // 08/10/2010 - The lines are obsolete when the $ORIGDATA2 = $DATA. $ORIGDATA is only for fallback 7667d101cc1SGerry Weißbach 7677d101cc1SGerry Weißbach // 08/10/2010 - If the ID does not exist, we may have a problem here with upper cases - they will all be lower by now! 7687d101cc1SGerry Weißbach if ( !$IDexists ) { 7697d101cc1SGerry Weißbach $ID = $IDifIDnotExists; // there may have been presevered Upper cases. We will need them! 7707d101cc1SGerry Weißbach } 7717d101cc1SGerry Weißbach 7727d101cc1SGerry Weißbach // $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'media') || strstr($DATA[2], 'export') ); 7737d101cc1SGerry Weißbach if ( substr($ID, -1) == ':' || empty($ID) ) $ID .= $conf['start']; 7747d101cc1SGerry Weißbach 7757d101cc1SGerry Weißbach // Generate Download URL 7767d101cc1SGerry Weißbach // $PARAMS = trim(str_replace('&', '&', $PARAMS)); 7777d101cc1SGerry Weißbach $PARAMS = trim($PARAMS); 7787d101cc1SGerry Weißbach $this->functions->removeWikiVariables($PARAMS, false, true); 7797d101cc1SGerry Weißbach 7807d101cc1SGerry Weißbach $url = $this->functions->wl($ID, null, true, null, null, true, $hadBase) . ( !empty( $ANCHOR) ? '#' . $ANCHOR : '' ) . ( !empty( $PARAMS) ? '?' . $PARAMS : '' ); 7817d101cc1SGerry Weißbach $this->functions->debug->message("URL from ID: '$url'", null, 2); 7827d101cc1SGerry Weißbach 7837d101cc1SGerry Weißbach // Parse URI PATH and add "html" 7847d101cc1SGerry Weißbach $uri = @parse_url($url); 7857d101cc1SGerry Weißbach $DATA[2] = $uri['path']; 7867d101cc1SGerry Weißbach $DATA['ANCHOR'] = $ANCHOR; 7877d101cc1SGerry Weißbach $DATA['PARAMS'] = $PARAMS; 7887d101cc1SGerry Weißbach 7897d101cc1SGerry Weißbach $this->functions->debug->message("DATA after parsing.", $DATA, 2); 7907d101cc1SGerry Weißbach 7917d101cc1SGerry Weißbach // Second Rewrite for UseRewrite = 2 7927d101cc1SGerry Weißbach if ( $conf['userewrite'] == 2 ) { 7937d101cc1SGerry Weißbach $DATA[2] = preg_replace( '$/lib/.*?fetch\.php$', '', $DATA[2]); 7947d101cc1SGerry Weißbach $DATA[2] = preg_replace( '%(/lib/.*?detail\.php.*$)%', '\1' . '.' . $this->functions->settings->fileType, $DATA[2]); 7957d101cc1SGerry Weißbach 7967d101cc1SGerry Weißbach if ( preg_match( '%/(lib/.*?detail|doku)\.php%', $DATA[2])) { 7977d101cc1SGerry Weißbach $noDeepReplace = false; 7987d101cc1SGerry Weißbach $fileName = $this->functions->getSiteName($ID); 7997d101cc1SGerry Weißbach $newDepth = str_repeat('../', count(explode('/', $fileName))-1); 8007d101cc1SGerry Weißbach } 8017d101cc1SGerry Weißbach 8027d101cc1SGerry Weißbach $this->functions->debug->message("DATA after second rewrite with UseRewrite = 2", array($DATA, $noDeepReplace, $fileName, $newDepth), 1); 8037d101cc1SGerry Weißbach } 8047d101cc1SGerry Weißbach 8057d101cc1SGerry Weißbach switch ( array_pop(explode('/', $DATA[2])) ) { 8067d101cc1SGerry Weißbach // CSS Extra Handling with extra rewrites 8077d101cc1SGerry Weißbach case 'css.php' : // $DATA[2] .= ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID(preg_replace("/(=|\?|&)/", ".", $PARAMS))) . '.css'; 8087d101cc1SGerry Weißbach $DATA[2] .= '.' . $this->functions->cleanID(preg_replace("/(=|\?|&)/", ".", $PARAMS)) . '.css'; // allways put parameters behind 8097d101cc1SGerry Weißbach // No paramters needed since they are rewritten. 8107d101cc1SGerry Weißbach $DATA['PARAMS'] = ""; 8117d101cc1SGerry Weißbach $noDeepReplace = false; 8127d101cc1SGerry Weißbach $fileName = $this->functions->getSiteName($ID); 8137d101cc1SGerry Weißbach $newDepth = str_repeat('../', count(explode('/', $fileName))-1); 8147d101cc1SGerry Weißbach $newAdditionalParameters['do'] = 'siteexport'; 8157d101cc1SGerry Weißbach 8167d101cc1SGerry Weißbach $this->functions->debug->message("This is CSS file", array($DATA, $noDeepReplace, $fileName, $newDepth, $newAdditionalParameters), 2); 8177d101cc1SGerry Weißbach 8187d101cc1SGerry Weißbach break; 8197d101cc1SGerry Weißbach case 'js.php' : // $DATA[2] .= ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID(preg_replace("/(=|\?|&)/", ".", $PARAMS))) . '.js'; 8207d101cc1SGerry Weißbach $DATA[2] .= '.t.' . $this->functions->cleanID($_REQUEST['template']) . '.js'; // allways put parameters behind 8217d101cc1SGerry Weißbach // set Template 8227d101cc1SGerry Weißbach if ( !empty( $_REQUEST['template'] ) ) { 8237d101cc1SGerry Weißbach $url .= ( strstr($url, '?') ? '&' : '?' ) . 'template=' . $_REQUEST['template']; 8247d101cc1SGerry Weißbach } 8257d101cc1SGerry Weißbach // No paramters needed since they are rewritten. 8267d101cc1SGerry Weißbach $DATA['PARAMS'] = ""; 8277d101cc1SGerry Weißbach $newAdditionalParameters['do'] = 'siteexport'; 8287d101cc1SGerry Weißbach 8297d101cc1SGerry Weißbach $this->functions->debug->message("This is JS file", array($DATA, $url, $fileName, $newAdditionalParameters), 2); 8307d101cc1SGerry Weißbach 8317d101cc1SGerry Weißbach break; 8327d101cc1SGerry Weißbach // Detail Handling with extra Rewrites if Paramaters are available - otherwise this is just the fetch 8337d101cc1SGerry Weißbach case 'indexer.php' : 8347d101cc1SGerry Weißbach $this->functions->debug->message("Skipping indexer", null, 2); 8357d101cc1SGerry Weißbach return ""; 8367d101cc1SGerry Weißbach break; 8377d101cc1SGerry Weißbach case 'detail.php' : 8387d101cc1SGerry Weißbach $fileName = $this->functions->getSiteName($ID, true); // 2010-09-03 - rewrite with override enabled 839*9d84786fSGerry Weißbach $noDeepReplace = true; 8407d101cc1SGerry Weißbach 841*9d84786fSGerry Weißbach $this->__getParamsAndDataRewritten($DATA, $PARAMS, 'id'); 842*9d84786fSGerry Weißbach $ID = $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'id')); 8437d101cc1SGerry Weißbach 8447d101cc1SGerry Weißbach $newDepth = str_repeat('../', count(explode('/', $fileName))-1); 8457d101cc1SGerry Weißbach $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 846*9d84786fSGerry Weißbach $DATA[2] .= '.' . array_pop(explode('/', $fileName)); 8477d101cc1SGerry Weißbach 848*9d84786fSGerry Weißbach $this->functions->debug->message("This is detail.php file with addParams", array($DATA, $ID, $fileName, $newDepth, $newAdditionalParameters), 2); 8497d101cc1SGerry Weißbach break; 850*9d84786fSGerry Weißbach case 'doku.php' : 8517d101cc1SGerry Weißbach 852*9d84786fSGerry Weißbach $noDeepReplace = false; 853*9d84786fSGerry Weißbach $this->__getParamsAndDataRewritten($DATA, $PARAMS, 'id'); 854*9d84786fSGerry Weißbach $ID = $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'id')); 855*9d84786fSGerry Weißbach 856*9d84786fSGerry Weißbach $this->functions->debug->message("Current ID to filename (doku.php): '" . wikiFN($ID) . "'", null, 2); 857*9d84786fSGerry Weißbach 858*9d84786fSGerry Weißbach $fileName = $this->functions->getSiteName($ID); // 2010-09-03 - rewrite with override enabled 859*9d84786fSGerry Weißbach 860*9d84786fSGerry Weißbach $newDepth = str_repeat('../', count(explode('/', $fileName))-1); 861*9d84786fSGerry Weißbach $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 862*9d84786fSGerry Weißbach $DATA[2] .= '.' . array_pop(explode('/', $fileName)); 863*9d84786fSGerry Weißbach 864*9d84786fSGerry Weißbach $this->functions->debug->message("This is doku.php file with addParams", array($DATA, $ID, $fileName, $newDepth, $newAdditionalParameters), 2); 865*9d84786fSGerry Weißbach return $this->__rebuildLink($DATA); 866*9d84786fSGerry Weißbach break; 867*9d84786fSGerry Weißbach 8687d101cc1SGerry Weißbach // Fetch Handling for media - rewriting everything 8697d101cc1SGerry Weißbach case 'fetch.php': 8707d101cc1SGerry Weißbach $this->__getParamsAndDataRewritten($DATA, $PARAMS, 'media'); 8717d101cc1SGerry Weißbach 8727d101cc1SGerry Weißbach $DATA[2] = str_replace('/', ':', $DATA[2]); 8737d101cc1SGerry Weißbach $ID = $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'media')); 874*9d84786fSGerry Weißbach resolve_mediaid(null, $ID, $IDexists); 8757d101cc1SGerry Weißbach 876*9d84786fSGerry Weißbach $DATA[2] = $this->functions->wl($ID, null, null, null, $IDexists, true); 877*9d84786fSGerry Weißbach $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 8787d101cc1SGerry Weißbach 8797d101cc1SGerry Weißbach $DATA['PARAMS'] = ""; 8807d101cc1SGerry Weißbach $newAdditionalParameters = array(); 8817d101cc1SGerry Weißbach 8820b4abc9fSGerry Weißbach $this->functions->debug->message("This is fetch.php file", array($DATA, $ID, $PARAMS), 2); 8837d101cc1SGerry Weißbach break; 8847d101cc1SGerry Weißbach 8857d101cc1SGerry Weißbach // default Handling for Pages 886*9d84786fSGerry Weißbach case 'feed.php': 887*9d84786fSGerry Weißbach return ""; // Ignore. Has no sense to export. 888*9d84786fSGerry Weißbach break; 8897d101cc1SGerry Weißbach default: 8907d101cc1SGerry Weißbach if ( preg_match("%" . DOKU_BASE . "_detail/%", $DATA[2]) ) { 8917d101cc1SGerry Weißbach 8927d101cc1SGerry Weißbach // GET ID Param from origdata2 8937d101cc1SGerry Weißbach preg_match("#id=(.*?)(&|\")#i", $DATA[0], $backlinkID); 8947d101cc1SGerry Weißbach $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 8957d101cc1SGerry Weißbach 8967d101cc1SGerry Weißbach $fileIDPart = isset($backlinkID[1]) && !empty($backlinkID[1]) ? $this->functions->cleanID(urldecode($backlinkID[1])) : 'detail'; 8977d101cc1SGerry Weißbach 8987d101cc1SGerry Weißbach $DATA[2] .= '/' . $fileIDPart . '.' . $this->functions->settings->fileType; // add namespace and subpage for back button and add filetype 8997d101cc1SGerry Weißbach 9007d101cc1SGerry Weißbach $noDeepReplace = false; 9017d101cc1SGerry Weißbach $fileName = $this->functions->shortenName($DATA[2]); 9027d101cc1SGerry Weißbach $newDepth = str_repeat('../', count(explode('/', $fileName))-1); 9037d101cc1SGerry Weißbach $url .= ( strstr($url, '?') ? '&' : '?' ) . 'id=' . $fileIDPart; // add id-part to URL for backlinks 9047d101cc1SGerry Weißbach 9057d101cc1SGerry Weißbach $DATA['PARAMS'] = ""; 9067d101cc1SGerry Weißbach 9077d101cc1SGerry Weißbach $this->functions->debug->message("This is something with '_detail' file", array($DATA, $backlinkID, $newDepth, $url), 2); 9087d101cc1SGerry Weißbach } else if ( preg_match("%" . DOKU_BASE . "_export/(.*?)/%", $DATA[2], $fileType) ) { 9097d101cc1SGerry Weißbach 9107d101cc1SGerry Weißbach // Fixes multiple codeblocks in one file 9117d101cc1SGerry Weißbach $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 9127d101cc1SGerry Weißbach 9137d101cc1SGerry Weißbach // add the Params no matter what they are. This is export. We don't mess with other files 9147d101cc1SGerry Weißbach // adding the "/" fixes the usage of multiple codeblocks in the same namespace 9157d101cc1SGerry Weißbach $DATA[2] .= (empty( $PARAMS ) ? '' : '/' . $PARAMS) . '.'. $fileType[1]; 9167d101cc1SGerry Weißbach 9177d101cc1SGerry Weißbach $DATA['PARAMS'] = ""; 9187d101cc1SGerry Weißbach $this->functions->debug->message("This is something with '_export' file", $DATA, 2); 9197d101cc1SGerry Weißbach 9207d101cc1SGerry Weißbach } else if ( $IDexists ) { // 08/10/2010 - was page_exists($ID) - but this should do as well. 9217d101cc1SGerry Weißbach // If this is a page ... skip it! 9227d101cc1SGerry Weißbach $DATA[2] .= ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID(preg_replace("/(=|\?|&)/", ".", $PARAMS))) . '.' . $this->functions->settings->fileType; 9237d101cc1SGerry Weißbach 9247d101cc1SGerry Weißbach // 2012-06-15 originally has an absolute path ... we might need a relative one if not in our namespace 9257d101cc1SGerry Weißbach $this->functions->debug->message("OK, this is to be absolute: " . (empty($_REQUEST['absolutePath'])?'false':'true'), null, 1); 9267d101cc1SGerry Weißbach if ( empty($_REQUEST['absolutePath']) ) 9277d101cc1SGerry Weißbach { 9287d101cc1SGerry Weißbach $DATA[2] = $this->functions->getRelativeURL($DATA[2], $currentID); 9297d101cc1SGerry Weißbach } 9307d101cc1SGerry Weißbach 9317d101cc1SGerry Weißbach $DATA[2] = $this->functions->shortenName($DATA[2]); 9327d101cc1SGerry Weißbach 9337d101cc1SGerry Weißbach // If Parameters are to be included in the filename - they must not be added twice 9347d101cc1SGerry Weißbach if ( $this->functions->settings->addParams ) $DATA['PARAMS'] = ""; 9357d101cc1SGerry Weißbach 9367d101cc1SGerry Weißbach $this->functions->debug->message("This page really exists", $DATA, 1); 9377d101cc1SGerry Weißbach 9387d101cc1SGerry Weißbach return $this->__rebuildLink($DATA); 9397d101cc1SGerry Weißbach } else { 940*9d84786fSGerry Weißbach $this->__rebuildDataForNormalFiles($DATA, $PARAMS, true); 941*9d84786fSGerry Weißbach $newAdditionalParameters = null; // 2014-06-27 - when using the "normal" files way we will not need any additional stuff. 942*9d84786fSGerry Weißbach // This would make problems with e.g. ditaa plugin 9437d101cc1SGerry Weißbach } 9447d101cc1SGerry Weißbach 9457d101cc1SGerry Weißbach unset($newAdditionalParameters['diPlu']); 9467d101cc1SGerry Weißbach } 9477d101cc1SGerry Weißbach 9487d101cc1SGerry Weißbach 9497d101cc1SGerry Weißbach $this->functions->debug->message("DATA after SWITCH CASE decision", array($DATA, $noDeepReplace, $fileName, $newDepth), 1); 9507d101cc1SGerry Weißbach 9517d101cc1SGerry Weißbach if ( $this->filewriter->canDoPDF() ) { 9527d101cc1SGerry Weißbach $this->functions->addAdditionalParametersToURL($url, $newAdditionalParameters); 9537d101cc1SGerry Weißbach $DATA[2] = $url; 9547d101cc1SGerry Weißbach unset($DATA['PARAMS']); 9557d101cc1SGerry Weißbach $url = $this->__rebuildLink($DATA, ''); 9567d101cc1SGerry Weißbach 9577d101cc1SGerry Weißbach $this->functions->debug->message("Creating PDF with URL '$url'", null, 2); 9587d101cc1SGerry Weißbach 9597d101cc1SGerry Weißbach return $url; 9607d101cc1SGerry Weißbach } 9617d101cc1SGerry Weißbach 9627d101cc1SGerry Weißbach // Create Name to save the file at 9637d101cc1SGerry Weißbach $DATA[2] = str_replace(':', '_', $DATA[2]); 9647d101cc1SGerry Weißbach $DATA[2] = $this->functions->shortenName($DATA[2]); 9657d101cc1SGerry Weißbach 9667d101cc1SGerry Weißbach 9677d101cc1SGerry Weißbach // File already loaded? 9687d101cc1SGerry Weißbach // 2010-10-23 - changes in_array from DATA[2] to $url - to check real URLs, the DATA[2] file will be checked with fileExistsInZip 9697d101cc1SGerry Weißbach if ( in_array($url, array_keys($this->fileChecked)) ) { 9707d101cc1SGerry Weißbach $DATA[2] = $this->fileChecked[$url]; 9717d101cc1SGerry Weißbach $this->functions->debug->message("File has been checked before.", array($DATA, $url), 2); 9727d101cc1SGerry Weißbach return $this->__rebuildLink($DATA); 9737d101cc1SGerry Weißbach } 9747d101cc1SGerry Weißbach 9757d101cc1SGerry Weißbach // 2010-09-03 - second check if the file is in the ZIP already. 9767d101cc1SGerry Weißbach if ( $this->filewriter->fileExistsInZip($DATA[2]) ) { 9777d101cc1SGerry Weißbach $this->functions->debug->message("File with DATA exists in ZIP.", $DATA, 3); 9787d101cc1SGerry Weißbach return $this->__rebuildLink($DATA); 9797d101cc1SGerry Weißbach } 9807d101cc1SGerry Weißbach 9817d101cc1SGerry Weißbach // 2010-10-23 - What if this is a fetch.php? than we produced an error. 9827d101cc1SGerry Weißbach // $this->fileChecked[] = $DATA[2]; 9837d101cc1SGerry Weißbach 9847d101cc1SGerry Weißbach // get tempFile and save it 9857d101cc1SGerry Weißbach $origDepth = $this->functions->settings->depth; 9867d101cc1SGerry Weißbach $this->functions->settings->depth = $newDepth; 9877d101cc1SGerry Weißbach 9887d101cc1SGerry Weißbach $tmpID = $currentID; 989*9d84786fSGerry Weißbach $tmpFile = false; 9907d101cc1SGerry Weißbach 9917d101cc1SGerry Weißbach $this->functions->debug->message("Going to get the file", array($url, $noDeepReplace, $newAdditionalParameters), 2); 9927d101cc1SGerry Weißbach $tmpFile = $this->__getHTTPFile($url, $noDeepReplace, $newAdditionalParameters); 993*9d84786fSGerry Weißbach $this->functions->debug->message("The getHTTPFile result is still empty", $tmpFile === false ? 'YES' : 'NO', 2); 9947d101cc1SGerry Weißbach 9957d101cc1SGerry Weißbach $currentID = $tmpID; 9967d101cc1SGerry Weißbach $this->functions->settings->depth = $origDepth; // 2010-09-03 - Reset depth at the very end 9977d101cc1SGerry Weißbach 9987d101cc1SGerry Weißbach if ( $tmpFile === false ) { 9997d101cc1SGerry Weißbach // Keep an potentially extra link intact 10007d101cc1SGerry Weißbach 10017d101cc1SGerry Weißbach $this->functions->debug->message("The fetched file '$url' is 'false'", null, 3); 10027d101cc1SGerry Weißbach if ( $IDexists === false ) { 10037d101cc1SGerry Weißbach $this->functions->debug->message("The file does not exist, fallback to ORIGDATA", $ORIGDATA2, 2); 10047d101cc1SGerry Weißbach $DATA[2] = $this->functions->shortenName($ORIGDATA2[2]); // get Origdata Path 10057d101cc1SGerry Weißbach } 10067d101cc1SGerry Weißbach 10077d101cc1SGerry Weißbach $this->fileChecked[$url] = $DATA[2]; // 2010-09-03 - One URL to one FileName 10087d101cc1SGerry Weißbach $link = $this->__rebuildLink($DATA); 10097d101cc1SGerry Weißbach $this->functions->debug->message("Final Link after empty file from '$url'", null, 2); 10107d101cc1SGerry Weißbach 10117d101cc1SGerry Weißbach return $link; 10127d101cc1SGerry Weißbach } 10137d101cc1SGerry Weißbach 10147d101cc1SGerry Weißbach $this->functions->debug->message("The fetched file looks good.", $tmpFile, 1); 1015281ed919SGerry Weißbach $dirname = dirname($DATA[2]); 10167d101cc1SGerry Weißbach 10177d101cc1SGerry Weißbach // If a Filename was given that does not comply to the original name, us this one! 101884d65497SGerry Weißbach // 2014-02-28 But only if we are on PDF Mode. Does this produce any other Problems? 101984d65497SGerry Weißbach if ( $this->filewriter->canDoPDF() && !empty($tmpFile[1]) && !strstr($DATA[2], $tmpFile[1]) ) { 1020281ed919SGerry Weißbach $DATA[2] = $dirname . '/' . $tmpFile[1]; 10217d101cc1SGerry Weißbach } 10227d101cc1SGerry Weißbach 10237d101cc1SGerry Weißbach // Add to zip 10247d101cc1SGerry Weißbach $this->fileChecked[$url] = $DATA[2]; // 2010-09-03 - One URL to one FileName 10257d101cc1SGerry Weißbach 10267d101cc1SGerry Weißbach $status = $this->filewriter->__addFileToZip($tmpFile[0], $DATA[2]); 10277d101cc1SGerry Weißbach @unlink($tmpFile[0]); 10287d101cc1SGerry Weißbach 10297d101cc1SGerry Weißbach $newURL = $this->__rebuildLink($DATA); 10307d101cc1SGerry Weißbach $this->functions->debug->message("Returning final Link to document: '$newURL'", null, 2); 10317d101cc1SGerry Weißbach 10327d101cc1SGerry Weißbach return $newURL; 10337d101cc1SGerry Weißbach } 10347d101cc1SGerry Weißbach 10357d101cc1SGerry Weißbach /** 10367d101cc1SGerry Weißbach * build the new link to be put in place for the donwloaded site 10377d101cc1SGerry Weißbach **/ 10387d101cc1SGerry Weißbach function __rebuildLink($DATA, $DEPTH = null) { 10395ae1a484SGerry Weißbach global $currentID; 10407d101cc1SGerry Weißbach 10417d101cc1SGerry Weißbach // depth is set, skip this one 10427d101cc1SGerry Weißbach if ( is_null( $DEPTH ) ) $DEPTH = $this->functions->settings->depth; 104384d65497SGerry Weißbach $DATA[2] .= ( !empty( $DATA['PARAMS']) && $this->functions->settings->addParams? '?' . $DATA['PARAMS'] : '' ) . ( !empty( $DATA['ANCHOR'] ) ? '#' . $DATA['ANCHOR'] : '' ); 10447d101cc1SGerry Weißbach 104567f4f6e5SGerry Weißbach $intermediateURL = $DEPTH . $DATA[2]; 104667f4f6e5SGerry Weißbach 10475ae1a484SGerry Weißbach // Check if the URL has a ../../something/somethingelse 10485ae1a484SGerry Weißbach // and basically goes back to our current page or something in parallel 10495ae1a484SGerry Weißbach // 1) remove all ../ at begining 10505ae1a484SGerry Weißbach $checkURL = preg_replace("#^(\.\./)+#", '', $intermediateURL); 10515ae1a484SGerry Weißbach if ( $checkURL != $intermediateURL ) { 10525ae1a484SGerry Weißbach 10535ae1a484SGerry Weißbach // 2) check if the URLs next parts match the current ENS to all NS parts of the current ID 10545ae1a484SGerry Weißbach // $this->functions->debug->message("Found ENS: '{$this->functions->settings->exportNamespace}', currentID: {$currentID}'", null, 2); 10555ae1a484SGerry Weißbach $currentIDPart = preg_replace("#^{$this->functions->settings->exportNamespace}/#", "", str_replace(':', '/', getNS($currentID) . '/')); 10565ae1a484SGerry Weißbach 10575ae1a484SGerry Weißbach $this->functions->debug->message("Found ../: '$checkURL' / currentIDPart: '{$currentIDPart}'", null, 2); 10585ae1a484SGerry Weißbach if ( ($newURL = preg_replace("#^{$currentIDPart}#", "./", $checkURL)) != $checkURL ) { 10595ae1a484SGerry Weißbach // 3) if so, remove these parts 10605ae1a484SGerry Weißbach $intermediateURL = $newURL; 10615ae1a484SGerry Weißbach $this->functions->debug->message("Found ./ URL: '$newURL'", null, 2); 10625ae1a484SGerry Weißbach } 10635ae1a484SGerry Weißbach } 10645ae1a484SGerry Weißbach 106567f4f6e5SGerry Weißbach $newURL = $DATA[1] == 'url' ? $DATA[1] . '(' . $intermediateURL . ')' : $DATA[1] . '="' . $intermediateURL . '"'; 10667d101cc1SGerry Weißbach $this->functions->debug->message("Re-created URL: '$newURL'", null, 2); 10677d101cc1SGerry Weißbach 10687d101cc1SGerry Weißbach return $newURL; 10697d101cc1SGerry Weißbach } 10707d101cc1SGerry Weißbach 10717d101cc1SGerry Weißbach 10727d101cc1SGerry Weißbach /** 10737d101cc1SGerry Weißbach * remove an old zip file 10747d101cc1SGerry Weißbach **/ 10757d101cc1SGerry Weißbach function __removeOldZip( $FILENAMEID=null, $checkForMore=true ) { 10767d101cc1SGerry Weißbach global $INFO; 10777d101cc1SGerry Weißbach global $conf; 10787d101cc1SGerry Weißbach 10797d101cc1SGerry Weißbach $returnValue = true; 10807d101cc1SGerry Weißbach 10817d101cc1SGerry Weißbach if ( empty($FILENAMEID) ) { 10827d101cc1SGerry Weißbach $FILENAMEID = $this->functions->settings->origZipFile; 10837d101cc1SGerry Weißbach } 10847d101cc1SGerry Weißbach 10858da901a0SGerry Weißbach if ( !file_exists(mediaFN($FILENAMEID)) ) { 10868da901a0SGerry Weißbach $returnValue = true; 10878da901a0SGerry Weißbach } else { 10888da901a0SGerry Weißbach 10897d101cc1SGerry Weißbach require_once( DOKU_INC . 'inc/media.php'); 10907d101cc1SGerry Weißbach if ( !media_delete($FILENAMEID, $INFO['perm']) ) { 10917d101cc1SGerry Weißbach $returnValue = false; 10927d101cc1SGerry Weißbach } 10937d101cc1SGerry Weißbach } 10947d101cc1SGerry Weißbach 10957d101cc1SGerry Weißbach if ( $checkForMore ) { 10967d101cc1SGerry Weißbach // Try to remove more files. 10977d101cc1SGerry Weißbach $ns = getNS($FILENAMEID); 10987d101cc1SGerry Weißbach $fn = $this->functions->getSpecialExportFileName(noNS($FILENAMEID), '.+'); 10997d101cc1SGerry Weißbach 11007d101cc1SGerry Weißbach $data = array(); 11017d101cc1SGerry Weißbach search($data, $conf['mediadir'], 'search_media', array('pattern' => "/$fn$/i"), $ns); 11027d101cc1SGerry Weißbach 11037d101cc1SGerry Weißbach if ( count($data > 0) ) { 11047d101cc1SGerry Weißbach 11057d101cc1SGerry Weißbach // 30 Minuten Cache Zeit 1106f8fd18e7SGerry Weißbach $cache = $this->functions->settings->cachetime; 11077d101cc1SGerry Weißbach foreach ( $data as $media ) { 11087d101cc1SGerry Weißbach 11097d101cc1SGerry Weißbach //decide if has to be deleted needed: 11107d101cc1SGerry Weißbach if( $media['mtime'] < time()-$cache) { 11117d101cc1SGerry Weißbach $this->__removeOldZip($media['id'], false); 11127d101cc1SGerry Weißbach } 11137d101cc1SGerry Weißbach } 11147d101cc1SGerry Weißbach } 11157d101cc1SGerry Weißbach 11167d101cc1SGerry Weißbach } 11177d101cc1SGerry Weißbach 11187d101cc1SGerry Weißbach return $returnValue; 11197d101cc1SGerry Weißbach } 11207d101cc1SGerry Weißbach 11217d101cc1SGerry Weißbach /** 11227d101cc1SGerry Weißbach * if confrewrite is set to internal rewrite, use this function - taken from a DW renderer 11237d101cc1SGerry Weißbach **/ 11247d101cc1SGerry Weißbach function __getInternalRewriteURL($url) { 11257d101cc1SGerry Weißbach global $conf; 11267d101cc1SGerry Weißbach 11277d101cc1SGerry Weißbach //construct page id from request URI 11287d101cc1SGerry Weißbach if( $conf['userewrite'] != 2) { return $url; } 11297d101cc1SGerry Weißbach 11307d101cc1SGerry Weißbach //get the script URL 11317d101cc1SGerry Weißbach if($conf['basedir']) { 11327d101cc1SGerry Weißbach $relpath = ''; 11337d101cc1SGerry Weißbach $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']); 11347d101cc1SGerry Weißbach } elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 11357d101cc1SGerry Weißbach $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 11367d101cc1SGerry Weißbach $_SERVER['SCRIPT_FILENAME']); 11377d101cc1SGerry Weißbach $script = '/'.$script; 11387d101cc1SGerry Weißbach }else{ 11397d101cc1SGerry Weißbach $script = $_SERVER['SCRIPT_NAME']; 11407d101cc1SGerry Weißbach } 11417d101cc1SGerry Weißbach 11427d101cc1SGerry Weißbach //clean script and request (fixes a windows problem) 11437d101cc1SGerry Weißbach $script = preg_replace('/\/\/+/','/',$script); 11447d101cc1SGerry Weißbach $request = preg_replace('/\/\/+/','/',$url); 11457d101cc1SGerry Weißbach 11467d101cc1SGerry Weißbach //remove script URL and Querystring to gain the id 11477d101cc1SGerry Weißbach if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ 11487d101cc1SGerry Weißbach $id = preg_replace ('/\?.*/','',$match[1]); 11497d101cc1SGerry Weißbach } 11507d101cc1SGerry Weißbach $id = urldecode($id); 11517d101cc1SGerry Weißbach //strip leading slashes 11527d101cc1SGerry Weißbach $id = preg_replace('!^/+!','',$id); 11537d101cc1SGerry Weißbach 11547d101cc1SGerry Weißbach return $id; 11557d101cc1SGerry Weißbach } 11567d101cc1SGerry Weißbach 11577d101cc1SGerry Weißbach /** 11587d101cc1SGerry Weißbach * rewrite parameter calls 11597d101cc1SGerry Weißbach **/ 11607d101cc1SGerry Weißbach function __getParamsAndDataRewritten(&$DATA, &$PARAMS, $IDKEY='id') { 11617d101cc1SGerry Weißbach 11627d101cc1SGerry Weißbach $PARRAY = explode('&', str_replace('&', '&', $PARAMS) ); 1163*9d84786fSGerry Weißbach $PARAMS = array(); 11647d101cc1SGerry Weißbach 11657d101cc1SGerry Weißbach foreach ( $PARRAY as $item ) { 11667d101cc1SGerry Weißbach list($key, $value) = explode('=', $item, 2); 11677d101cc1SGerry Weißbach if ( empty($key) || empty($value) ) 11687d101cc1SGerry Weißbach continue; 11697d101cc1SGerry Weißbach 11707d101cc1SGerry Weißbach if ( strtolower(trim($key)) == $IDKEY ) { 11717d101cc1SGerry Weißbach $DATA[2] = preg_replace("%^" . DOKU_BASE . "%", "", $value); 11727d101cc1SGerry Weißbach continue; 11737d101cc1SGerry Weißbach } 11747d101cc1SGerry Weißbach 1175*9d84786fSGerry Weißbach $PARAMS[] = "$key=$value"; 11767d101cc1SGerry Weißbach } 11777d101cc1SGerry Weißbach 1178*9d84786fSGerry Weißbach $PARAMS = implode('&', $PARAMS); 11797d101cc1SGerry Weißbach } 11807d101cc1SGerry Weißbach 11817d101cc1SGerry Weißbach /** 11827d101cc1SGerry Weißbach * rewrite detail.php calls 11837d101cc1SGerry Weißbach **/ 1184*9d84786fSGerry Weißbach function __rebuildDataForNormalFiles(&$DATA, &$PARAMS, $addHash=false) { 11857d101cc1SGerry Weißbach $PARTS = explode('.', $DATA[2]); 11867d101cc1SGerry Weißbach if ( count($PARTS) > 1 ) { 11877d101cc1SGerry Weißbach $EXT = '.' . array_pop($PARTS); 11887d101cc1SGerry Weißbach } 11897d101cc1SGerry Weißbach 1190*9d84786fSGerry Weißbach $internalParams = $PARAMS = preg_replace("/(=|\?|&)/", ".", $PARAMS); 1191*9d84786fSGerry Weißbach 1192*9d84786fSGerry Weißbach // add anyways - if on overridde 1193*9d84786fSGerry Weißbach if ( !$this->functions->settings->addParams && !empty($PARAMS) && $addHash ) { 1194*9d84786fSGerry Weißbach $internalParams = md5($PARAMS); 1195*9d84786fSGerry Weißbach } else if ( !$this->functions->settings->addParams ){ 1196*9d84786fSGerry Weißbach $internalParams = null; 1197*9d84786fSGerry Weißbach } 1198*9d84786fSGerry Weißbach 1199*9d84786fSGerry Weißbach $DATA[2] = implode('.', $PARTS) . ( empty($internalParams) ? '' : '.' . $this->functions->cleanID($internalParams)) . ( $EXT == '.php' ? '.' . $this->functions->settings->fileType : $EXT ); 12007d101cc1SGerry Weißbach $DATA[2] = preg_replace("/\.+/", ".", $DATA[2]); 1201*9d84786fSGerry Weißbach $this->functions->debug->message("Rebuilding Data for normal file.", $DATA[2], 1); 12027d101cc1SGerry Weißbach } 12037d101cc1SGerry Weißbach 12047d101cc1SGerry Weißbach 12057d101cc1SGerry Weißbach 12067d101cc1SGerry Weißbach 12077d101cc1SGerry Weißbach /* 12087d101cc1SGerry Weißbach * Clean JS and CSS cache files 12097d101cc1SGerry Weißbach */ 12107d101cc1SGerry Weißbach function cleanCacheFiles() { 12117d101cc1SGerry Weißbach 12127d101cc1SGerry Weißbach $_SERVER['HTTP_HOST'] = preg_replace("/:?\d+$/", '', $_SERVER['HTTP_HOST']); 12137d101cc1SGerry Weißbach $cache = getCacheName('scripts'.$_SERVER['HTTP_HOST'].'-siteexport-js-'.$_SERVER['SERVER_PORT'],'.js'); 12147d101cc1SGerry Weißbach $this->unlinkIfExists($cache); 12157d101cc1SGerry Weißbach 12167d101cc1SGerry Weißbach $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['template'])); 12177d101cc1SGerry Weißbach if($tpl) 12187d101cc1SGerry Weißbach { 12197d101cc1SGerry Weißbach $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/'; 12207d101cc1SGerry Weißbach $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/'; 12217d101cc1SGerry Weißbach } else { 12227d101cc1SGerry Weißbach $tplinc = DOKU_TPLINC; 12237d101cc1SGerry Weißbach $tpldir = DOKU_TPL; 12247d101cc1SGerry Weißbach } 12257d101cc1SGerry Weißbach 12267d101cc1SGerry Weißbach // The generated script depends on some dynamic options 12277d101cc1SGerry Weißbach $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].'-siteexport-js-'.$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$style,'.css'); 12287d101cc1SGerry Weißbach $this->unlinkIfExists($cache); 12297d101cc1SGerry Weißbach } 12307d101cc1SGerry Weißbach 12317d101cc1SGerry Weißbach function unlinkIfExists($cache) { 12327d101cc1SGerry Weißbach if ( file_exists($cache) ) { 12337d101cc1SGerry Weißbach @unlink($cache); 12347d101cc1SGerry Weißbach if(function_exists('gzopen')) @unlink("$cache.gz"); 12357d101cc1SGerry Weißbach } 12367d101cc1SGerry Weißbach } 12377d101cc1SGerry Weißbach 12387d101cc1SGerry Weißbach // Private unset function 12397d101cc1SGerry Weißbach private function clear(&$variable) 12407d101cc1SGerry Weißbach { 12417d101cc1SGerry Weißbach if ( isset($variable) ) 12427d101cc1SGerry Weißbach { 12437d101cc1SGerry Weißbach unset($variable); 12447d101cc1SGerry Weißbach } 12457d101cc1SGerry Weißbach } 12467d101cc1SGerry Weißbach}