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