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