1<?php 2/** 3 * Site Export Plugin 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author i-net software <tools@inetsoftware.de> 7 * @author Gerry Weissbach <gweissbach@inetsoftware.de> 8 */ 9 10// must be run within Dokuwiki 11if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../../').'/'); 12if(!defined('DOKU_PLUGIN')) { 13 // Just for sanity 14 require_once(DOKU_INC.'inc/plugin.php'); 15 define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 16} 17 18require_once(DOKU_PLUGIN.'action.php'); 19require_once(DOKU_INC.'/inc/search.php'); 20 21require_once(DOKU_PLUGIN.'siteexport/inc/functions.php'); 22require_once(DOKU_PLUGIN.'siteexport/inc/httpproxy.php'); 23require_once(DOKU_PLUGIN.'siteexport/inc/filewriter.php'); 24require_once(DOKU_PLUGIN.'siteexport/inc/toc.php'); 25require_once(DOKU_PLUGIN.'siteexport/inc/javahelp.php'); 26 27class action_plugin_siteexport_ajax extends DokuWiki_Action_Plugin 28{ 29 /** 30 * New internal variables for better structure 31 */ 32 private $filewriter = null; 33 public $functions = null; 34 35 // List of files that have already been checked 36 private $fileChecked = array(); 37 38 // Namespace of the page to export 39 private $namespace = ''; 40 41 /** 42 * Register Plugin in DW 43 **/ 44 public function register(Doku_Event_Handler $controller) { 45 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax_siteexport_provider'); 46 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'siteexport_action'); 47 } 48 49 /** 50 * AJAX Provider - check what is going to be done 51 * @param $event 52 * @param $args 53 */ 54 function ajax_siteexport_provider(&$event, $args) { 55 56 // If this is not a siteexport call, ignore it. 57 if ( !strstr($event->data, '__siteexport' ) ) 58 { 59 return; 60 } 61 62 $this->__init_functions(true); 63 64 switch( $event->data ) { 65 case '__siteexport_getsitelist': $this->ajax_siteexport_getsitelist( $event ); break; 66 case '__siteexport_addsite': $this->ajax_siteexport_addsite( $event ); break; 67 case '__siteexport_generateurl': $this->ajax_siteexport_generateurl( $event ); break; 68 case '__siteexport_aggregate': $this->ajax_siteexport_aggregate( $event ); break; 69 } 70 } 71 72 /** 73 * Export from a URL - action 74 * @param $event 75 */ 76 function siteexport_action( &$event ) { 77 global $ID; 78 79 // Check if the 'do' was siteexport 80 $command = is_array($event->data) ? array_shift(array_keys($event->data)) : $event->data; 81 if ( $command != 'siteexport' ) { return false; } 82 $event->data = act_clean($event->data); 83 84 if ( headers_sent() ) { 85 msg("The siteexport function has to be called prior to any header output.", -1); 86 } 87 88 $this->__init_functions(); 89 90 $this->functions->debug->message("========================================", null, 1); 91 $this->functions->debug->message("Starting export from URL call", null, 1); 92 $this->functions->debug->message("----------------------------------------", null, 1); 93 94 $event->preventDefault(); 95 $event->stopPropagation(); 96 97 // Fake security Token if none given 98 if ( empty( $_REQUEST['sectok'] ) ) { 99 $_REQUEST['sectok'] = getSecurityToken(); 100 } 101 102 // The timer will be used to do redirects if needed to prevent timeouts 103 $starttimer = time(); 104 $timerdiff = $this->getConf('max_execution_time'); 105 106 $data = $this->__get_siteexport_list_and_init_tocs($ID, !empty($_REQUEST['startcounter'])); 107 108 if ( $data === false ) { 109 header("HTTP/1.0 401 Unauthorized"); 110 print 'Unauthorized'; 111 exit; 112 } 113 114 $counter = 0; 115 116 if ( count($data) == 0 && !$this->functions->settings->hasValidCacheFile ) { 117 exit( "No Data to export" ); 118 } 119 120 foreach ( $data as $site ) { 121 122 if ( intval($site['exists']) == 1 || !isset($site['exists']) ) { 123 124 // Skip over the amount of urls that have been exported already 125 if ( empty($_REQUEST['startcounter']) || $counter >= intval($_REQUEST['startcounter']) ) { 126 $status = $this->__siteexport_add_site($site['id']); 127 128 if ( $status === false ) { 129 $this->functions->debug->message("----------------------------------------", null, 1); 130 $this->functions->debug->message("Errors during export from URL call", null, 1); 131 $this->functions->debug->message("========================================", null, 1); 132 print $this->functions->debug->runtimeErrors; 133 exit(0); // We need to stop 134 } 135 } 136 } 137 138 $counter ++; 139 if ( time() - $starttimer >= $timerdiff ) { 140 $this->functions->debug->message("Will Redirect", null, 1); 141 $this->handleRuntimeErrorOutput(); 142 $this->functions->startRedirctProcess($counter); 143 } 144 } 145 146 $this->functions->debug->message("----------------------------------------", null, 1); 147 $this->functions->debug->message("Finishing export from URL call", null, 1); 148 $this->functions->debug->message("========================================", null, 1); 149 150 $this->cleanCacheFiles(); 151 152 $URL = ml($this->functions->settings->origZipFile, array('cache' => 'nocache', 'siteexport' => $this->functions->settings->pattern, 'sectok' => getSecurityToken()), true, '&'); 153 $this->functions->debug->message("Redirecting to final file", $URL, 2); 154 155 $this->handleRuntimeErrorOutput(); 156 send_redirect($URL); 157 exit(0); // Should not be reached, but anyways 158 } 159 160 private function handleRuntimeErrorOutput() 161 { 162 if ( !empty($this->functions->debug->runtimeErrors) ) 163 { 164 $this->filewriter->__moveDataToZip($this->functions->debug->runtimeErrors, '_runtime_error/' . time() . '.html'); 165 } 166 } 167 168 public function __init_functions($isAJAX=false) 169 { 170 global $conf; 171 172 $conf['useslash'] = 1; 173 174 $this->functions = new siteexport_functions(true, $isAJAX); 175 $this->filewriter = new siteexport_zipfilewriter($this->functions); 176 177 // Check for PDF Capabilities 178 if ( $this->filewriter->canDoPDF() ) { 179 $this->functions->settings->fileType = 'pdf'; 180 } 181 } 182 183 /** 184 * Prepares the generated URL for direct download access 185 * Also gives back the parameters for this URL 186 * @param $event init event of the ajax request 187 */ 188 function ajax_siteexport_prepareURL_and_POSTData( &$event ) { 189 190 $event->preventDefault(); 191 $event->stopPropagation(); 192 193 // Retrieve Information for download URL 194 $this->functions->debug->message("Prepared URL and POST from Request:", $_REQUEST, 2); 195 $url = $this->functions->prepare_POSTData($_REQUEST); 196 $combined = $this->functions->urlToPathAndParams($url); 197 list($path, $query) = explode('?', $combined, 2); 198 $return = array($url, $combined, $path, $query); 199 200 $this->functions->debug->message("Prepared URL and POST data:", $return, 2); 201 return $return; 202 } 203 204 /** 205 * Executes a Cron Job Action 206 * @param $event 207 */ 208 function ajax_siteexport_cronaction( &$event ) 209 { 210 $cronOverwriteExisting = intval($_REQUEST['cronOverwriteExisting']) == 1; 211 list($url, $combined) = $this->ajax_siteexport_prepareURL_and_POSTData($event); 212 213 if ( !$function =& plugin_load('cron', 'siteexport' ) ) 214 { 215 $this->functions->debug->message("Tried to do an action with siteexport/cron, but the cron plugin is missing.", null, 4); 216 } 217 218 $status = null; 219 switch( $event->data ) { 220 case '__siteexport_savecron': $status = $function->saveCronDataWithParameters($combined, $cronOverwriteExisting); break; 221 case '__siteexport_deletecron': $status = $function->deleteCronDataWithParameters($combined); break; 222 } 223 224 if ( !empty($status) ) 225 { 226 $this->functions->debug->message("Tried to do an action with siteexport/cron, but failed.", $status, 4); 227 } 228 } 229 230 /** 231 * generate direct access URL 232 **/ 233 function ajax_siteexport_generateurl( &$event ) { 234 235 list($url, $combined, $path, $POSTData) = $this->ajax_siteexport_prepareURL_and_POSTData($event); 236 237 // WGET Redirects - this is an option for wget only. 238 // 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 239 // Therefore we assume it takes about 5s for each page - that gives the freedom to have anough time for redirect. 240 $maxRedirectNumber = ceil( ( count($this->__get_siteexport_list($NS, true)) * 5) / $this->getConf('max_execution_time') ); 241 $maxRedirect = $maxRedirectNumber > 0 ? '--max-redirect=' . ($maxRedirectNumber+3) . ' ' : ''; 242 $maxRedirs = $maxRedirectNumber > 0 ? '--max-redirs ' . ($maxRedirectNumber+3) . ' ' : ''; 243 244 $this->functions->debug->message("Generating Direct Download URL", $url, 2); 245 246 // If there was a Runtime Exception 247 if ( !$this->functions->debug->firstRE() ) { 248 $this->functions->debug->message("There have been errors while generating the download URLs.", null, 4); 249 return; 250 } 251 252 echo $url; 253 echo "\n"; 254 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'; 255 echo "\n"; 256 echo 'curl -L ' . $maxRedirs . '-o ' . array_pop(explode(":", ($this->getConf('zipfilename')))) . ' -d "' . $POSTData . '" ' . wl(cleanID($path), null, true) . ' --anyauth --user USER:PASSWD'; 257 echo "\n"; 258 259 $this->functions->debug->message("Checking for Cron parameters: ", $combined, 1); 260 if ( !$functions =& plugin_load('cron', 'siteexport' ) || 261 !$functions->hasCronJobForParameters($combined) ) { 262 echo "false"; 263 } else 264 { 265 echo "true"; 266 } 267 268 return; 269 } 270 271 /** 272 * Get List of sites to be exported for AJAX (wrapper) 273 **/ 274 function ajax_siteexport_getsitelist( &$event ) { 275 276 $event->preventDefault(); 277 $event->stopPropagation(); 278 279 $data = $this->__get_siteexport_list_and_init_tocs($_REQUEST['ns']); 280 281 // Important for reconaisance of the session 282 283 if ( $data === false ) 284 { 285 $this->functions->debug->runtimeException("No data generated. List of Files is 'false'."); 286 return; 287 } 288 289 if ( empty($data) && !$this->functions->settings->hasValidCacheFile ) 290 { 291 $this->functions->debug->runtimeException("Generated list is empty."); 292 return; 293 } 294 295 // If there was a Runtime Exception 296 if ( !$this->functions->debug->firstRE() ) 297 { 298 $this->functions->debug->message("There have been errors while generating site list.", null, 4); 299 return; 300 } 301 302 echo "{$this->functions->settings->pattern}\n"; 303 echo $this->functions->downloadURL() . "\n"; 304 foreach($data as $line ){ 305 echo $line['id'] . "\n"; 306 } 307 308 return; 309 } 310 311 function ajax_siteexport_aggregate( &$event ) { 312 313 // Quick preparations for one page only 314 if ( $this->filewriter->hasValidCacheFile($_REQUEST, $data) ) { 315 $this->functions->debug->message("Had a valid cache file and will use it.", null, 2); 316 print $this->functions->downloadURL(); 317 318 $event->preventDefault(); 319 $event->stopPropagation(); 320 } else { 321 // Then go for it! 322 $this->functions->debug->message("Will create a new cache thing.", null, 2); 323 $this->ajax_siteexport_addsite( $event ); 324 } 325 326 } 327 328 /** 329 * Add a page to the package (for AJAX calls - Wrapper) 330 **/ 331 function ajax_siteexport_addsite( &$event ) { 332 333 $event->preventDefault(); 334 $event->stopPropagation(); 335 336 $this->functions->debug->message("========================================", null, 1); 337 $this->functions->debug->message("Starting export from AJAX call", null, 1); 338 $this->functions->debug->message("----------------------------------------", null, 1); 339 340 $status = $this->__siteexport_add_site($_REQUEST['site']); 341 if ( $status === false ) { 342 $this->functions->debug->message("----------------------------------------", null, 1); 343 $this->functions->debug->message("Errors during export from AJAX call", null, 1); 344 $this->functions->debug->message("========================================", null, 1); 345 return; 346 } 347 348 $this->functions->debug->message("----------------------------------------", null, 1); 349 $this->functions->debug->message("Finishing export from AJAX call", null, 1); 350 $this->functions->debug->message("========================================", null, 1); 351 352 // Print the download zip-File 353 $this->cleanCacheFiles(); 354 355 // If there was a Runtime Exception 356 if ( !$this->functions->debug->firstRE() ) { 357 $this->functions->debug->message("There have been errors during the export.", null, 4); 358 return; 359 } 360 361 print $this->functions->downloadURL(); 362 return; 363 } 364 365 /** 366 * Fetch the list of pages to be exported 367 **/ 368 function __get_siteexport_list($NS, $overrideCache=false) { 369 global $conf; 370 371 $NS = $this->namespace = $this->functions->getNamespaceFromID($NS, $PAGE); 372 $this->functions->debug->message("ROOT Namespace to export from: '{$NS}' / {$this->namespace}", null, 1); 373 374 $depth = $this->getConf('depth'); 375 $query = ''; 376 $doSearch = 'search_allpages'; 377 378 switch( intval($_REQUEST['depthType']) ) { 379 case 0: 380 $query = $this->functions->cleanID(str_replace(":", "/", $NS.':'.$PAGE)); 381 resolve_pageid($NS, $PAGE, $exists); 382 383 if ( $exists ) { 384 $data = array( array( 'id' => $PAGE) ); 385 386 $this->functions->debug->message("Checking for Cache", null, 2); 387 if ( !$overrideCache && $this->filewriter->hasValidCacheFile($_REQUEST, $data) ) 388 { 389 return array(); 390 } 391 392 return $data; 393 } 394 case 1: $depth = 0; 395 break; 396 case 2: $depth = intval($_REQUEST['depth']); 397 break; 398 } 399 400 $opts = array( 'depth' => $depth, 'skipacl' => $this->getConf('skipacl'), 'query' => $query); 401 $this->functions->debug->message("Options", $opts, 2); 402 403 $data = array(); 404 require_once (DOKU_INC.'inc/search.php'); 405 406 // Check, which TOC to take 407 if ( !$this->functions->settings->useTOCFile ) { 408 search($data, $conf['datadir'], $doSearch, $opts, $this->namespace); 409 } else { 410 $this->functions->debug->message("Using TOC for data", null, 2); 411 412 $doSearch = 'search_pagename'; 413 414 // Create Data of the TOC File should be used instead 415 $opts['query'] = 'toc.txt'; 416 417 $RAWdata = array(); 418 search($RAWdata, $conf['datadir'], $doSearch, $opts, $this->namespace); 419 420 // There may be more than one toc and all of them have to be merged. 421 $data = array(); 422 foreach( $RAWdata as $entry ) 423 { 424 $tmpData = p_get_metadata($entry['id'], 'sitetoc siteexportTOC', true); 425 426 if ( is_array($tmpData) ) 427 { 428 $data = array_merge($data, $tmpData); 429 } 430 } 431 } 432 433 $this->functions->debug->message("Checking for Cache", null, 2); 434 if ( !$overrideCache && $this->filewriter->hasValidCacheFile($_REQUEST, $data) ) 435 { 436 return array(); 437 } 438 439 $this->functions->debug->message("Exporting the following sites: ", $data, 2); 440 return $data; 441 } 442 443 function __get_siteexport_list_and_init_tocs($NS, $isRedirected=false ) { 444 445 // Clean up if not redirected 446 if ( !$isRedirected && !$this->__removeOldZip() ) { 447 $this->functions->debug->runtimeException("Can't remove old files."); 448 return false; 449 } 450 451 $data = $this->__get_siteexport_list($NS, $isRedirected); 452 if ( $isRedirected || empty($data) ) 453 { 454 // if we have been redirected, simply return the data 455 $this->functions->debug->message("List is empty I guess. Used NS: '{$NS}' ", null, 1); 456 return $data; 457 } 458 459 // Create Eclipse Documentation Pages - TOC.xml, Context.xml 460 if ( !empty($_REQUEST['absolutePath']) ) $this->namespace = ""; 461// $this->__removeOldZip( $this->functions->settings->eclipseZipFile ); 462 463 if ( !empty($_REQUEST['eclipseDocZip']) ) 464 { 465 $toc = new siteexport_toc($this->functions); 466 $this->functions->debug->message("Generating eclipseDocZip", null, 2); 467 $this->filewriter->__moveDataToZip($toc->__getTOCXML($data), 'toc.xml'); 468 $this->filewriter->__moveDataToZip($toc->__getContextXML($data), 'context.xml'); 469 } else if ( !empty($_REQUEST['JavaHelpDocZip']) ) 470 { 471 $toc = new siteexport_javahelp($this->functions, $this->filewriter); 472 $toc->createTOCFiles($data); 473 474/* $toc = new siteexport_toc($this->functions); 475 list($tocData, $mapData) = $toc->__getJavaHelpTOCXML($data); 476 $this->functions->debug->message("Generating JavaHelpDocZip", null, 2); 477 $this->filewriter->__moveDataToZip($tocData, 'toc.xml'); 478 $this->filewriter->__moveDataToZip($mapData, 'map.xml'); 479*/ } 480 481 return $data; 482 } 483 484 /** 485 * Add page with ID to the package 486 **/ 487 function __siteexport_add_site( $ID ) { 488 global $conf, $currentID, $currentParent; 489 490 // Which is the current ID? 491 $currentID = $ID; 492 493 $this->functions->debug->message("========================================", null, 2); 494 $this->functions->debug->message("Adding Site: '$ID'", null, 2); 495 $this->functions->debug->message("----------------------------------------", $_REQUEST, 2); 496 497 $request = $this->functions->settings->additionalParameters; 498 unset($request['diPlu']); // This will not be needed for the first request. 499 unset($request['diInv']); // This will not be needed for the first request. 500 501 // say, what to export and Build URL 502 // 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 503 504 $do = (intval($_REQUEST['exportbody']) == 1 ? (empty($_REQUEST['renderer']) ? $conf['renderer_xhtml'] : $_REQUEST['renderer'] ) : '' ); 505 506 if ($do == 'pdf' && $this->filewriter->canDoPDF() ) 507 { 508 $do = 'export_siteexport_pdf'; 509 $_REQUEST['origRenderer'] = (empty($_REQUEST['renderer']) ? $conf['renderer_xhtml'] : $_REQUEST['renderer'] ); 510 } else if ( $_REQUEST['renderer'] == 'dw2pdf' ) { 511 $do = 'pdf'; 512 } 513 514 $do = ($do == $conf['renderer_xhtml'] && intval($_REQUEST['exportbody']) != 1) ? '' : 'export_' . $do; 515 516 if ( $do != 'export_' && !empty($do) ) 517 { 518 $request['do'] = $do; 519 } 520 521 // set Template 522 if ( !empty( $_REQUEST['template'] ) ) { 523 $request['template'] = $_REQUEST['template']; 524 } 525 526 $this->functions->debug->message("REQUEST for add_site:", $request, 2); 527 528 $ID = $this->functions->cleanID($ID); 529 $url = $this->functions->wl($ID, $request, true, '&'); 530 531 // Parse URI PATH and add "html" 532 $currentParent = $fileName = $this->functions->getSiteName($ID, true); 533 $this->functions->debug->message("Filename could be:", $fileName, 2); 534 535 $this->fileChecked[$url] = $fileName; // 2010-09-03 - One URL to one FileName 536 $this->functions->settings->depth = str_repeat('../', count(explode('/', $fileName))-1); 537 538 // fetch URL and save it in temp file 539 $tmpFile = $this->__getHTTPFile($url); 540 if ( $tmpFile === false ) { 541 // return $this->functions->debug->message("Creating temporary download file failed for '$url'. See log for more information."); 542 $this->functions->debug->runtimeException("Creating temporary download file failed for '$url'. See log for more information."); 543 return false; 544 } 545 546 $dirname = dirname($fileName); 547 // If a Filename was given that does not comply to the original name, use this one! 548 if ( $this->filewriter->canDoPDF() ) { 549 550 $this->functions->debug->message("Will replace old filename '{$fileName}' with {$ID}", null, 1); 551 $extension = explode('.', $fileName); 552 $extension = array_pop($extension); 553 554 // 2014-04-29 added cleanID to ensure that links are generated consistently when using [[this>...]] or another local, relativ linking 555 $fileName = $dirname . '/' . $this->functions->cleanID($this->functions->getSiteTitle($ID)) . '.' . $extension; 556 } else if ( !empty($tmpFile[1]) && !strstr($DATA[2], $tmpFile[1]) ) { 557 558 $this->functions->debug->message("Will replace old filename '{$fileName}' with {$dirname}/{$tmpFile[1]}", null, 1); 559 $fileName = $dirname . '/' . $tmpFile[1]; 560 } 561 562 // Add to zip 563 $this->fileChecked[$url] = $fileName; 564 $status = $this->filewriter->__addFileToZip($tmpFile[0], $fileName); 565 @unlink($tmpFile[0]); 566 567 return $status; 568 } 569 570 function __preg_quote($input) { 571 return preg_quote($input, '/'); 572 } 573 574 /** 575 * Download the file via HTTP URL + recurse if this is not an image 576 * The file will be saved as temporary file. The filename is the result. 577 **/ 578 function __getHTTPFile($URL, $RECURSE=false, $newAdditionalParameters=null) { 579 global $conf; 580 581 $EXCLUDE = $this->getConf('exclude'); 582 if ( !empty($EXCLUDE) ) { 583 $PATTERN = "/(" . implode('|', explode(' ', preg_quote($EXCLUDE, '/'))) . ")/i"; 584 585 $this->functions->debug->message("Checking for exclude: ", array( 586 "pattern" => $PATTERN, 587 "file" => $URL, 588 "matches" => preg_match($PATTERN, $URL) ? 'match' : 'no match' 589 ), 2); 590 591 if ( preg_match($PATTERN, $URL) ) { return false; } 592 } 593 594 $http = new HTTPProxy($this->functions->debug, $this->functions->settings); 595 $http->max_bodysize = $conf['fetchsize']; 596 // $http->user = $_SERVER['PHP_AUTH_USER']; // Must not be set, or the files will be authenticated and have the edit thingies 597 // $http->pass = $_SERVER['PHP_AUTH_PW']; // Must not be set, or the files will be authenticated and have the edit thingies 598 599 // Add additional Params 600 $this->functions->addAdditionalParametersToURL($URL, $newAdditionalParameters); 601 602 $this->functions->debug->message("Fetching URL: '$URL'", null, 2); 603 $getData = $http->get($URL, true); // true == sloopy, get 304 body as well. 604 605 if( $getData === false ) { // || ($http->status != 200 && !$this->functions->settings->ignoreNon200) ) { 606 607 if ( $http->status != 200 && $this->functions->settings->ignoreNon200 ) { 608 $this->functions->debug->message("HTTP status was '{$http->status}' - but I was told to ignore it by the settings.", $URL, 3); 609 return true; 610 } 611 612 $this->functions->debug->message("Sending request failed with error, HTTP status was '{$http->status}'.", $URL, 4); 613 return false; 614 } 615 616 if( empty($getData) ) { 617 $this->functions->debug->message("No data fetched", $URL, 4); 618 return false; 619 } 620 621 $this->functions->debug->message("Headers received", $http->resp_headers, 2); 622 623 if ( !$RECURSE ) { 624 // Parse URI PATH and add "html" 625 $this->functions->debug->message("========================================", null, 1); 626 $this->functions->debug->message("Starting to recurse file '$URL'", null , 1); 627 $this->functions->debug->message("----------------------------------------", null, 1); 628 $this->__getInternalLinks($getData); 629 $this->functions->debug->message("----------------------------------------", null, 1); 630 $this->functions->debug->message("Finished to recurse file '$URL'", null , 1); 631 $this->functions->debug->message("========================================", null, 1); 632 } 633 634 $tmpFile = tempnam($this->functions->settings->tmpDir , 'siteexport__'); 635 $this->functions->debug->message("Temporary filename", $tmpFile, 1); 636 637 $fp = fopen( $tmpFile, "w"); 638 if(!$fp) { 639 $this->functions->debug->message("Can't open temporary File '$tmpFile'.", null , 4); 640 return false; 641 } 642 643 fwrite($fp,$getData); 644 fclose($fp); 645 646 // plain/text; ... 647 $extension = explode(';', $http->resp_headers['content-type'], 2); 648 $extension = array_shift($extension); 649 $extension = explode('/', $extension, 2); 650 if ( $extension[0] == 'image' && preg_match("/^[a-zA-Z0-9]{3,}$/", $extension[1]) ) { 651 $extension = strtolower($extension[1]); 652 $this->functions->debug->message("Found new image extension:", $extension, 2); 653 } else { 654 unset($extension); 655 } 656 657 return array($tmpFile, preg_replace("/.*?filename=\"?(.*?)\"?;?$/", "$1", $http->resp_headers['content-disposition']), $extension); 658 } 659 660 /** 661 * Find internal links in the currently downloaded file. This also matches inside CSS files 662 **/ 663 function __getInternalLinks(&$DATA) { 664 665 $PATTERN = '(href|src|action)="([^"]*)"'; 666 $CALLBACK = array($this, '__fetchAndReplaceLink'); 667 $DATA = preg_replace_callback("/$PATTERN/i", $CALLBACK, $DATA); 668 669 $PATTERNCSS = '(url\s*?)\(([^\)]*)\)'; 670 $DATA = preg_replace_callback("/$PATTERNCSS/i", $CALLBACK, $DATA); 671 } 672 673 /** 674 * Deep Fetch and replace of links inside the texts matched by __getInternalLinks 675 **/ 676 function __fetchAndReplaceLink($DATA) { 677 global $conf, $currentID, $currentParent; 678 679 $noDeepReplace = true; 680 $newAdditionalParameters = $this->functions->settings->additionalParameters; 681 $newDepth = $this->functions->settings->depth; 682 $hadBase = false; 683 684 // Clean data[2], remote ' and " 685 $DATA[2] = preg_replace("/^\s*?['\"]?(.*?)['\"]?\s*?$/", '\1', trim($DATA[2])); 686 687 $this->functions->debug->message("Starting Link Replacement", array( 'data' => $DATA, 'additional Params' => $newAdditionalParameters, 'newDepth' => $newDepth, 'currentID' => $currentID, 'currentParent' => $currentParent), 2); 688 689 // $DATA[2] = urldecode($DATA[2]); // Leads to problems because it does not re-encode the url 690 // External and mailto links 691 if ( preg_match("%^(https?://|mailto:|javascript:|data:)%", $DATA[2]) ) { 692 $this->functions->debug->message("Don't like http, mailto, data or javascript links here", null, 1); 693 return $this->__rebuildLink($DATA, ""); 694 } 695 //if ( preg_match("%^(https?://|mailto:|" . DOKU_BASE . "/_export/)%", $DATA[2]) ) { return $this->__rebuildLink($DATA, ""); } 696 // External media - this is deep down in the link, so we have to grep it out 697 if ( preg_match("%media=(https?://.*?$)%", $DATA[2], $matches) ) { 698 $DATA[2] = $matches[1]; 699 $this->functions->debug->message("This is an HTTP like somewhere else", $DATA, 1); 700 return $this->__rebuildLink($DATA, ""); 701 } 702 // reference only links won't have to be rewritten 703 if ( preg_match("%^#.*?$%", $DATA[2]) ) { 704 $this->functions->debug->message("This is a refercence only", null, 1); 705 return $this->__rebuildLink($DATA, ""); 706 } 707 708 // 2014-07-21: Origdata before anything else - or it will be missing some things. 709 $ORIGDATA2 = $DATA; 710 // $ORIGDATA2 = $DATA[2]; // 08/10/2010 - this line required a $this->functions->wl which may mess up with the base URL 711 $this->functions->debug->message("OrigDATA is:", $ORIGDATA2, 1); 712 713 // strip all things out 714 // changed Data 715 $PARAMS = @parse_url($DATA[2], PHP_URL_QUERY); 716 $ANCHOR = @parse_url($DATA[2], PHP_URL_FRAGMENT); 717 $DATA[2] = @parse_url($DATA[2], PHP_URL_PATH); 718 719 // 2014-05-12 - fix problem with URLs starting with a ./ or ../ ... they seem to need the current IDs root 720 if ( preg_match("#^\.\.?/#", $DATA[2])) { 721 $DATA[2] = getNS($currentID) . ':' . $DATA[2]; 722 } 723 724 // 2010-08-25 - fix problem with relative movement in links ( "test/../test2" ) 725 // 2014-06-30 - what? to what will this end relatively? 726 $tmpData2 = ''; 727 while( $tmpData2 != $DATA[2] ) { 728 $tmpData2 = $DATA[2]; 729 $DATA[2] = preg_replace("#/(?!\.\.)[^\/]*?/\.\./#", '/', $DATA[2]); 730 } 731 732 $temp = preg_replace("%^" . preg_quote(DOKU_BASE, '%') . "%", "", $DATA[2]); 733 if ( $temp != $DATA[2] ) { 734 $DATA[2] = $temp; 735 $hadBase = true; // 2010-08-23 Check if there has been a rewrite here that will have to be considered later on 736 } 737 738 $this->functions->debug->message("URL before rewriting option for others than 1", array($DATA, $PARAMS, $hadBase), 1); 739 740 // Handle rewrites other than 1 - just for non-lib-files 741 // if ( !preg_match('$^/?lib/$', $DATA[2]) ) { 742 if ( !preg_match('$^(' . DOKU_BASE . ')?lib/$', $DATA[2]) ) { 743 $this->functions->debug->message("Did not match '$^(" . DOKU_BASE . ")?lib/$' userewrite == {$conf['userewrite']}", null, 2); 744 if ( $conf['userewrite'] == 2 ) { 745 $DATA[2] = $this->__getInternalRewriteURL($DATA[2]); 746 } elseif ( $conf['userewrite'] == 0 ) { 747 $this->__getParamsAndDataRewritten($DATA, $PARAMS); 748 } 749 } else { 750 $this->functions->debug->message("This file must be inside lib ...", null, 2); 751 } 752 753 $this->functions->debug->message("URL before rewriting option", array($DATA, $PARAMS), 2); 754 755 // Generate ID 756 $DATA[2] = str_replace('/', ':', $DATA[2]); 757 758 // If Data was empty this must be the same file!; 759 if ( empty( $DATA[2] ) ) { 760 $DATA[2] = $currentID; 761 } 762 763 $ID = $DATA[2]; 764 $MEDIAMATCHER = "#(_media(/|:)|media=|_detail(/|:)|_export(/|:)|do=export_)#i"; // 2010-10-23 added "(/|:)" for the ID may not contain slashes anymore 765 $ISMEDIA = preg_match($MEDIAMATCHER, $DATA[2]); 766 if ( $ISMEDIA && $conf['userewrite'] == 1) { 767 //$DATA[2] = preg_replace($MEDIAMATCHER, "", $DATA[2]); 768 $ID = preg_replace("#^_(detail|media)(/|:)#", "", $ID); 769 } 770 771 $ID = $this->functions->cleanID($DATA[2], null, $ISMEDIA ); 772 // $ID = $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'media') ); // Export anpassung nun weiter unten 773 774 // $IDexists = page_exists($ID); // 08/10/2010 - Not needed. This will be done in the next block. 775 // $this->functions->debug->message("Current ID: '$ID' exists: '" . ($IDexists ? 'true' : 'false') . "' (will be set to 'false' anyway)", null, 1); 776 777 $IDifIDnotExists = $ID; // 08/10/2010 - Save ID - with possible upper cases to preserve them 778 $IDexists = false; 779 780 $this->functions->debug->message("Resolving ID: '$ID'", null, 2); 781 if ( $ISMEDIA ) { 782 resolve_mediaid(null, $ID, $IDexists); 783 784 $this->functions->debug->message("Current mediaID to filename: '" . mediaFN($ID) . "'", null, 2); 785 } else { 786 resolve_pageid(null, $ID, $IDexists); 787 $this->functions->debug->message("Current ID to filename: '" . wikiFN($ID) . "'", null, 2); 788 } 789 790 $this->functions->debug->message("Current ID after resolvement: '$ID' the ID does exist: '" . ($IDexists ? 'true' : 'false') . "'", null, 2); 791 // $ORIGDATA2 = @parse_url($this->functions->wl($ORIGDATA2, null, true)); // What was the next 2 line for? It did mess up with links from {{jdoc>}} 792 // $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 793 794 // 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! 795 if ( !$IDexists ) { 796 $ID = $IDifIDnotExists; // there may have been presevered Upper cases. We will need them! 797 } 798 799 // $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'media') || strstr($DATA[2], 'export') ); 800 if ( substr($ID, -1) == ':' || empty($ID) ) $ID .= $conf['start']; 801 802 // Generate Download URL 803 // $PARAMS = trim(str_replace('&', '&', $PARAMS)); 804 $PARAMS = trim($PARAMS); 805 $this->functions->removeWikiVariables($PARAMS, false, true); 806 807 $url = $this->functions->wl($ID, null, true, null, null, true, $hadBase) . ( !empty( $ANCHOR) ? '#' . $ANCHOR : '' ) . ( !empty( $PARAMS) ? '?' . $PARAMS : '' ); 808 $this->functions->debug->message("URL from ID: '$url'", null, 2); 809 810 // Parse URI PATH and add "html" 811 $uri = @parse_url($url); 812 $DATA[2] = $uri['path']; 813 814 $this->functions->debug->message("DATA after parsing.", $DATA, 2); 815 816 // Second Rewrite for UseRewrite = 2 817 if ( $conf['userewrite'] == 2 && preg_match("%((/lib/exe/(fetch|detail|indexer)|feed|doku)\.php)/?(.*?)$%", $DATA[2], $matches)) { 818 819 820 // The actual file in lib 821 $DATA[2] = $matches[1]; 822 $PARAMS .= '&' . (in_array($matches[3], array('fetch', 'detail')) ? 'media' : 'id') . '=' . cleanID(str_replace('/', ':', $matches[4])); 823 824/* $DATA[2] = preg_replace( '$/lib/.*?fetch\.php$', '', $DATA[2]); 825 $DATA[2] = preg_replace( '%(/lib/.*?detail\.php.*$)%', '\1' . '.' . $this->functions->settings->fileType, $DATA[2]); 826 827 if ( preg_match( '%/(lib/.*?detail|doku)\.php%', $DATA[2])) { 828 $noDeepReplace = false; 829 $fileName = $this->functions->getSiteName($ID); 830 $newDepth = str_repeat('../', count(explode('/', $fileName))-1); 831 } 832 $this->functions->debug->message("DATA after second rewrite with UseRewrite = 2", array($DATA, $noDeepReplace, $fileName, $newDepth), 1); 833*/ 834 $this->functions->debug->message("DATA after second rewrite with UseRewrite = 2", array($DATA, $matches, $PARAMS), 1); 835 } 836 837 $DATA['ANCHOR'] = $ANCHOR; 838 $DATA['PARAMS'] = $PARAMS; 839 $elements = explode('/', $DATA[2]); 840 841 switch ( array_pop($elements) ) { 842 // CSS Extra Handling with extra rewrites 843 case 'css.php' : // $DATA[2] .= ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID(preg_replace("/(=|\?|&)/", ".", $PARAMS))) . '.css'; 844 $DATA[2] .= '.' . $this->functions->cleanID(preg_replace("/(=|\?|&)/", ".", $PARAMS)) . '.css'; // allways put parameters behind 845 // No paramters needed since they are rewritten. 846 $DATA['PARAMS'] = ""; 847 $noDeepReplace = false; 848 $fileName = $this->functions->getSiteName($ID, true); 849 850 // NewDepth has to be relative to the css file itself ... 851 $newDepth = './' . str_repeat('../', count(explode('/', $fileName))-1); // it is an ID at this point. 852 $newAdditionalParameters['do'] = 'siteexport'; 853 854 $this->functions->debug->message("This is CSS file", array($DATA, $noDeepReplace, $fileName, $newDepth, $newAdditionalParameters), 2); 855 856 break; 857 case 'js.php' : // $DATA[2] .= ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID(preg_replace("/(=|\?|&)/", ".", $PARAMS))) . '.js'; 858 $DATA[2] .= '.t.' . $this->functions->cleanID($_REQUEST['template']) . '.js'; // allways put parameters behind 859 // set Template 860 if ( !empty( $_REQUEST['template'] ) ) { 861 $url .= ( strstr($url, '?') ? '&' : '?' ) . 'template=' . $_REQUEST['template']; 862 } 863 // No paramters needed since they are rewritten. 864 $DATA['PARAMS'] = ""; 865 $newAdditionalParameters['do'] = 'siteexport'; 866 867 $this->functions->debug->message("This is JS file", array($DATA, $url, $fileName, $newAdditionalParameters), 2); 868 869 break; 870 // Detail Handling with extra Rewrites if Paramaters are available - otherwise this is just the fetch 871 case 'indexer.php' : 872 $this->functions->debug->message("Skipping indexer", null, 2); 873 return ""; 874 break; 875 case 'detail.php' : 876 $noDeepReplace = false; 877 878 $this->__getParamsAndDataRewritten($DATA, $PARAMS, 'media'); 879 $ID = $this->functions->cleanID(str_replace('/', ':', $DATA[2]), null, strstr($DATA[2], 'media')); 880 $fileName = $this->functions->getSiteName($ID, true); // 2010-09-03 - rewrite with override enabled 881 882 $newDepth = str_repeat('../', count(explode('/', $fileName))-1); 883 $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 884 $DATA[2] .= '.detail.html'; 885 886 $this->functions->debug->message("This is detail.php file with addParams", array($DATA, $ID, $fileName, $newDepth, $newAdditionalParameters), 2); 887 break; 888 case 'doku.php' : 889 890 $noDeepReplace = false; 891 $this->__getParamsAndDataRewritten($DATA, $PARAMS, 'id'); 892 $ID = $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'id')); 893 894 $this->functions->debug->message("Current ID to filename (doku.php): '" . wikiFN($ID) . "'", null, 2); 895 896 $fileName = $this->functions->getSiteName($ID); // 2010-09-03 - rewrite with override enabled 897 898 $newDepth = str_repeat('../', count(explode('/', $fileName))-1); 899 $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 900 $DATA[2] .= '.' . array_pop(explode('/', $fileName)); 901 902 $this->functions->debug->message("This is doku.php file with addParams", array($DATA, $ID, $fileName, $newDepth, $newAdditionalParameters), 2); 903 return $this->__rebuildLink($DATA); 904 break; 905 906 // Fetch Handling for media - rewriting everything 907 case 'fetch.php': 908 $this->__getParamsAndDataRewritten($DATA, $PARAMS, 'media'); 909 910 $DATA[2] = str_replace('/', ':', $DATA[2]); 911 $ID = $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'media')); 912 resolve_mediaid(null, $ID, $IDexists); 913 914 $DATA[2] = $this->functions->wl($ID, null, null, null, $IDexists, true); 915 $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 916 917 $DATA['PARAMS'] = ""; 918 $newAdditionalParameters = array(); 919 920 $this->functions->debug->message("This is fetch.php file", array($DATA, $ID, $PARAMS), 2); 921 break; 922 923 // default Handling for Pages 924 case 'feed.php': 925 return ""; // Ignore. Has no sense to export. 926 break; 927 default: 928 if ( preg_match("%" . preg_quote(DOKU_BASE, '%') . "_detail/%", $DATA[2]) ) { 929 930 // GET ID Param from origdata2 931 preg_match("#id=(.*?)(&|\")#i", $DATA[0], $backlinkID); 932 $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 933 934 $fileIDPart = isset($backlinkID[1]) && !empty($backlinkID[1]) ? $this->functions->cleanID(urldecode($backlinkID[1])) : 'detail'; 935 936 $ID = preg_replace("#^_detail(/|:)#", "", $ID); 937 $DATA[2] .= ':' . $fileIDPart . '.' . $this->functions->settings->fileType; // add namespace and subpage for back button and add filetype 938 939 $noDeepReplace = false; 940 $fileName = $this->functions->shortenName($DATA[2]); 941 $newDepth = str_repeat('../', count(explode('/', $fileName))-1); 942 $url .= ( strstr($url, '?') ? '&' : '?' ) . 'id=' . $fileIDPart; // add id-part to URL for backlinks 943 944 $DATA['PARAMS'] = ""; 945 946 $this->functions->debug->message("This is something with '_detail' file", array($DATA, $backlinkID, $newDepth, $url, $ID), 2); 947 } else if ( preg_match("%" . preg_quote(DOKU_BASE, '%') . "_export/(.*?)/%", $DATA[2], $fileType) ) { 948 949 // Fixes multiple codeblocks in one file 950 $this->__rebuildDataForNormalFiles($DATA, $PARAMS); 951 952 // add the Params no matter what they are. This is export. We don't mess with other files 953 // adding the "/" fixes the usage of multiple codeblocks in the same namespace 954 $DATA[2] .= (empty( $PARAMS ) ? '' : '/' . $PARAMS) . '.'. $fileType[1]; 955 956 $DATA['PARAMS'] = ""; 957 $this->functions->debug->message("This is something with '_export' file", $DATA, 2); 958 959 } else if ( $IDexists ) { // 08/10/2010 - was page_exists($ID) - but this should do as well. 960 // If this is a page ... skip it! 961 $DATA[2] .= ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID(preg_replace("/(=|\?|&)/", ".", $PARAMS))) . '.' . $this->functions->settings->fileType; 962 963 $DATA[2] = $this->functions->shortenName($DATA[2]); 964 965 // If Parameters are to be included in the filename - they must not be added twice 966 if ( $this->functions->settings->addParams ) $DATA['PARAMS'] = ""; 967 968 $this->functions->debug->message("This page really exists", $DATA, 1); 969 970 return $this->__rebuildLink($DATA); 971 } else { 972 $this->__rebuildDataForNormalFiles($DATA, $PARAMS, true); 973 $newAdditionalParameters = null; // 2014-06-27 - when using the "normal" files way we will not need any additional stuff. 974 // This would make problems with e.g. ditaa plugin 975 } 976 977 unset($newAdditionalParameters['diPlu']); 978 } 979 980 $this->functions->debug->message("DATA after SWITCH CASE decision", array($DATA, $noDeepReplace, $fileName, $newDepth), 1); 981 982 if ( $this->filewriter->canDoPDF() ) { 983 $this->functions->addAdditionalParametersToURL($url, $newAdditionalParameters); 984 $DATA[2] = $url; 985 unset($DATA['PARAMS']); 986 $url = $this->__rebuildLink($DATA, ''); 987 988 $this->functions->debug->message("Creating PDF with URL '$url'", null, 2); 989 990 return $url; 991 } 992 993 // Create Name to save the file at 994 $DATA[2] = str_replace(':', '_', $DATA[2]); 995 $DATA[2] = $this->functions->shortenName($DATA[2]); 996 997 998 // File already loaded? 999 // 2010-10-23 - changes in_array from DATA[2] to $url - to check real URLs, the DATA[2] file will be checked with fileExistsInZip 1000 if ( in_array($url, array_keys($this->fileChecked)) ) { 1001 $DATA[2] = $this->fileChecked[$url]; 1002 $this->functions->debug->message("File has been checked before.", array($DATA, $url), 2); 1003 return $this->__rebuildLink($DATA); 1004 } 1005 1006 // 2010-09-03 - second check if the file is in the ZIP already. 1007 if ( $this->filewriter->fileExistsInZip($DATA[2]) ) { 1008 $this->functions->debug->message("File with DATA exists in ZIP.", $DATA, 3); 1009 return $this->__rebuildLink($DATA); 1010 } 1011 1012 // 2010-10-23 - What if this is a fetch.php? than we produced an error. 1013 // $this->fileChecked[] = $DATA[2]; 1014 1015 // get tempFile and save it 1016 $origDepth = $this->functions->settings->depth; 1017 $this->functions->settings->depth = $newDepth; 1018 1019 $tmpID = $currentID; 1020 $tmpParent = $currentParent; 1021 $tmpFile = false; 1022 1023 $currentParent = $fileName; 1024 $this->functions->debug->message("Going to get the file", array($url, $noDeepReplace, $newAdditionalParameters), 2); 1025 $tmpFile = $this->__getHTTPFile($url, $noDeepReplace, $newAdditionalParameters); 1026 $this->functions->debug->message("The getHTTPFile result is still empty", $tmpFile === false ? 'YES' : 'NO', 2); 1027 1028 $currentParent = $tmpParent; 1029 $currentID = $tmpID; 1030 $this->functions->settings->depth = $origDepth; // 2010-09-03 - Reset depth at the very end 1031 1032 if ( $tmpFile === false ) { 1033 // Keep an potentially extra link intact 1034 1035 $this->functions->debug->message("The fetched file '$url' is 'false'", null, 3); 1036 if ( $IDexists === false ) { 1037 $this->functions->debug->message("The file does not exist, fallback to ORIGDATA", $ORIGDATA2, 2); 1038 $DATA[2] = $this->functions->shortenName($ORIGDATA2[2]); // get Origdata Path 1039 } 1040 1041 $this->fileChecked[$url] = $DATA[2]; // 2010-09-03 - One URL to one FileName 1042 $link = $this->__rebuildLink($DATA); 1043 $this->functions->debug->message("Final Link after empty file from '$url'", null, 2); 1044 1045 return $link; 1046 } 1047 1048 $this->functions->debug->message("The fetched file looks good.", $tmpFile, 2); 1049 $dirname = dirname($DATA[2]); 1050 1051 // If a Filename was given that does not comply to the original name, us this one! 1052 // 2014-02-28 But only if we are on PDF Mode. Does this produce any other Problems? 1053 if ( $this->filewriter->canDoPDF() && !empty($tmpFile[1]) && !strstr($DATA[2], $tmpFile[1]) ) { 1054 $DATA[2] = $dirname . '/' . $tmpFile[1]; 1055 $this->functions->debug->message("Changed filename.", $DATA[2], 2); 1056 } 1057 1058 // Custom extension if not set already - 2014-07-02 1059 if ( !empty($tmpFile[2]) && !preg_match("#\.{$tmpFile[2]}$#", $DATA[2]) ) { 1060 $DATA[2] = preg_match("#(\.[^\.]+)$#", $DATA[2]) ? preg_replace("#(\.[^\.]+)$#", '.' . $tmpFile[2], $DATA[2]) : $DATA[2] . '.' . $tmpFile[2]; 1061 $this->functions->debug->message("Added extension provided from Server.", $DATA[2], 2); 1062 } 1063 1064 // Add to zip 1065 $this->fileChecked[$url] = $DATA[2]; // 2010-09-03 - One URL to one FileName 1066 1067 $status = $this->filewriter->__addFileToZip($tmpFile[0], $DATA[2]); 1068 @unlink($tmpFile[0]); 1069 1070 $newURL = $this->__rebuildLink($DATA); 1071 $this->functions->debug->message("Returning final Link to document: '$newURL'", null, 2); 1072 1073 return $newURL; 1074 } 1075 1076 /** 1077 * build the new link to be put in place for the donwloaded site 1078 **/ 1079 function __rebuildLink($DATA, $DEPTH = null) { 1080 global $currentID, $currentParent; 1081 1082 // depth is set, skip this one 1083 if ( is_null( $DEPTH ) ) $DEPTH = $this->functions->settings->depth; 1084 $DATA[2] .= ( !empty( $DATA['PARAMS']) && $this->functions->settings->addParams? '?' . $DATA['PARAMS'] : '' ) . ( !empty( $DATA['ANCHOR'] ) ? '#' . $DATA['ANCHOR'] : '' ); 1085 1086 $intermediateURL = $DEPTH . $DATA[2]; 1087 1088//* 1089 // 2012-06-15 originally has an absolute path ... we might need a relative one if not in our namespace 1090 if ( empty($_REQUEST['absolutePath']) && preg_match("#^(\.\./)+#", $intermediateURL) ) { 1091 1092 $this->functions->debug->message("OK, this is not to be absolute: ", array($intermediateURL, $currentParent), 1); 1093 // Experimental 1094 $intermediateURL = $this->functions->getRelativeURL($intermediateURL, $currentParent); 1095 } 1096/*/ 1097 // Check if the URL has a ../../something/somethingelse 1098 // and basically goes back to our current page or something in parallel 1099 // 1) remove all ../ at begining 1100 1101 $this->functions->debug->message("currentID: '{$currentID}'", null, 1); 1102 $checkURL = preg_replace("#^(\.\./)+#", '', $intermediateURL); 1103 if ( $checkURL != $intermediateURL ) { 1104 $this->functions->debug->message("Found ../: '$checkURL' / currentIDPart: '{$currentIDPart}'", null, 2); 1105 1106 // 2) check if the URLs next parts match the current ENS to all NS parts of the current ID 1107 // $this->functions->debug->message("Found ENS: '{$this->functions->settings->exportNamespace}', currentID: {$currentID}'", null, 2); 1108 $currentIDPart = preg_replace("#^{$this->functions->settings->exportNamespace}/#", "", str_replace(':', '/', getNS($currentID) . '/')); 1109 1110 if ( ($newURL = preg_replace("#^{$currentIDPart}#", "./", $checkURL)) != $checkURL ) { 1111 // 3) if so, remove these parts 1112 $intermediateURL = $newURL; 1113 $this->functions->debug->message("Found ./ URL: '$newURL'", null, 2); 1114 } 1115 } 1116//*/ 1117 $newURL = $DATA[1] == 'url' ? $DATA[1] . '(' . $intermediateURL . ')' : $DATA[1] . '="' . $intermediateURL . '"'; 1118 $this->functions->debug->message("Re-created URL: '$newURL'", $DEPTH, 2); 1119 1120 return $newURL; 1121 } 1122 1123 1124 /** 1125 * remove an old zip file 1126 **/ 1127 function __removeOldZip( $FILENAMEID=null, $checkForMore=true ) { 1128 global $INFO; 1129 global $conf; 1130 1131 $returnValue = true; 1132 1133 if ( empty($FILENAMEID) ) { 1134 $FILENAMEID = $this->functions->settings->origZipFile; 1135 } 1136 1137 if ( !file_exists(mediaFN($FILENAMEID)) ) { 1138 $returnValue = true; 1139 } else { 1140 1141 require_once( DOKU_INC . 'inc/media.php'); 1142 if ( !media_delete($FILENAMEID, $INFO['perm']) ) { 1143 $returnValue = false; 1144 } 1145 } 1146 1147 if ( $checkForMore ) { 1148 // Try to remove more files. 1149 $ns = getNS($FILENAMEID); 1150 $fn = $this->functions->getSpecialExportFileName(noNS($FILENAMEID), '.+'); 1151 1152 $data = array(); 1153 search($data, $conf['mediadir'], 'search_media', array('pattern' => "/$fn$/i"), $ns); 1154 1155 if ( count($data > 0) ) { 1156 1157 // 30 Minuten Cache Zeit 1158 $cache = $this->functions->settings->cachetime; 1159 foreach ( $data as $media ) { 1160 1161 //decide if has to be deleted needed: 1162 if( $media['mtime'] < time()-$cache) { 1163 $this->__removeOldZip($media['id'], false); 1164 } 1165 } 1166 } 1167 1168 } 1169 1170 return $returnValue; 1171 } 1172 1173 /** 1174 * if confrewrite is set to internal rewrite, use this function - taken from a DW renderer 1175 **/ 1176 function __getInternalRewriteURL($url) { 1177 global $conf; 1178 1179 //construct page id from request URI 1180 if( $conf['userewrite'] != 2) { return $url; } 1181 1182 //get the script URL 1183 if($conf['basedir']) { 1184 $relpath = ''; 1185 $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']); 1186 } elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 1187 $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 1188 $_SERVER['SCRIPT_FILENAME']); 1189 $script = '/'.$script; 1190 }else{ 1191 $script = $_SERVER['SCRIPT_NAME']; 1192 } 1193 1194 //clean script and request (fixes a windows problem) 1195 $script = preg_replace('/\/\/+/','/',$script); 1196 $request = preg_replace('/\/\/+/','/',$url); 1197 1198 //remove script URL and Querystring to gain the id 1199 if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ 1200 $id = preg_replace ('/\?.*/','',$match[1]); 1201 } 1202 $id = urldecode($id); 1203 //strip leading slashes 1204 $id = preg_replace('!^/+!','',$id); 1205 1206 return $id; 1207 } 1208 1209 /** 1210 * rewrite parameter calls 1211 **/ 1212 function __getParamsAndDataRewritten(&$DATA, &$PARAMS, $IDKEY='id') { 1213 1214 $PARRAY = explode('&', str_replace('&', '&', $PARAMS) ); 1215 $PARAMS = array(); 1216 1217 foreach ( $PARRAY as $item ) { 1218 list($key, $value) = explode('=', $item, 2); 1219 if ( empty($key) || empty($value) ) 1220 continue; 1221 1222 if ( strtolower(trim($key)) == $IDKEY ) { 1223 $DATA[2] = preg_replace("%^" . preg_quote(DOKU_BASE, '%') . "%", "", str_replace(':', '/', $value)); 1224 continue; 1225 } 1226 1227 $PARAMS[] = "$key=$value"; 1228 } 1229 1230 sort($PARAMS); 1231 1232 $PARAMS = implode('&', $PARAMS); 1233 } 1234 1235 /** 1236 * rewrite detail.php calls 1237 **/ 1238 function __rebuildDataForNormalFiles(&$DATA, &$PARAMS, $addHash=false) { 1239 $PARTS = explode('.', $DATA[2]); 1240 if ( count($PARTS) > 1 ) { 1241 $EXT = '.' . array_pop($PARTS); 1242 } 1243 1244 $internalParams = $PARAMS = preg_replace("/(=|\?|&)/", ".", $PARAMS); 1245 1246 // add anyways - if on overridde 1247 if ( !$this->functions->settings->addParams && !empty($PARAMS) && $addHash ) { 1248 $internalParams = md5($PARAMS); 1249 } else if ( !$this->functions->settings->addParams ){ 1250 $internalParams = null; 1251 } 1252 1253 $DATA[2] = implode('.', $PARTS) . ( empty($internalParams) ? '' : '.' . $this->functions->cleanID($internalParams)) . ( $EXT == '.php' ? '.' . $this->functions->settings->fileType : $EXT ); 1254 $DATA[2] = preg_replace("/\.+/", ".", $DATA[2]); 1255 $this->functions->debug->message("Rebuilding Data for normal file.", $DATA[2], 1); 1256 } 1257 1258 /* 1259 * Clean JS and CSS cache files 1260 */ 1261 function cleanCacheFiles() { 1262 1263 $_SERVER['HTTP_HOST'] = preg_replace("/:?\d+$/", '', $_SERVER['HTTP_HOST']); 1264 $cache = getCacheName('scripts'.$_SERVER['HTTP_HOST'].'-siteexport-js-'.$_SERVER['SERVER_PORT'],'.js'); 1265 $this->unlinkIfExists($cache); 1266 1267 $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['template'])); 1268 if($tpl) 1269 { 1270 $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/'; 1271 $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/'; 1272 } else { 1273 $tplinc = DOKU_TPLINC; 1274 $tpldir = DOKU_TPL; 1275 } 1276 1277 // The generated script depends on some dynamic options 1278 $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].'-siteexport-js-'.$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$style,'.css'); 1279 $this->unlinkIfExists($cache); 1280 } 1281 1282 function unlinkIfExists($cache) { 1283 if ( file_exists($cache) ) { 1284 @unlink($cache); 1285 if(function_exists('gzopen')) @unlink("$cache.gz"); 1286 } 1287 } 1288 1289 // Private unset function 1290 private function clear(&$variable) 1291 { 1292 if ( isset($variable) ) 1293 { 1294 unset($variable); 1295 } 1296 } 1297}