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