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