xref: /plugin/siteexport/action/ajax.php (revision 6792d0cf58db367e1e6f5b779f1b1efd0e27751f)
17d101cc1SGerry Weißbach<?php
27d101cc1SGerry Weißbach/**
37d101cc1SGerry Weißbach * Site Export Plugin
47d101cc1SGerry Weißbach *
57d101cc1SGerry Weißbach * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
67d101cc1SGerry Weißbach * @author     i-net software <tools@inetsoftware.de>
77d101cc1SGerry Weißbach * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
87d101cc1SGerry Weißbach */
97d101cc1SGerry Weißbach
107d101cc1SGerry Weißbach// must be run within Dokuwiki
117d101cc1SGerry Weißbachif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../../').'/');
127d101cc1SGerry Weißbachif(!defined('DOKU_PLUGIN')) {
137d101cc1SGerry Weißbach    // Just for sanity
147d101cc1SGerry Weißbach    require_once(DOKU_INC.'inc/plugin.php');
157d101cc1SGerry Weißbach    define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
167d101cc1SGerry Weißbach}
177d101cc1SGerry Weißbach
187d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'action.php');
197d101cc1SGerry Weißbachrequire_once(DOKU_INC.'/inc/search.php');
207d101cc1SGerry Weißbach
217d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/functions.php');
227d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/httpproxy.php');
237d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/filewriter.php');
247d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/toc.php');
257d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/javahelp.php');
267d101cc1SGerry Weißbach
277d101cc1SGerry Weißbachclass action_plugin_siteexport_ajax extends DokuWiki_Action_Plugin
287d101cc1SGerry Weißbach{
297d101cc1SGerry Weißbach    /**
307d101cc1SGerry Weißbach     * New internal variables for better structure
317d101cc1SGerry Weißbach     */
327d101cc1SGerry Weißbach    private $filewriter = null;
337d101cc1SGerry Weißbach    public $functions = null;
347d101cc1SGerry Weißbach
357d101cc1SGerry Weißbach    // List of files that have already been checked
367d101cc1SGerry Weißbach    private $fileChecked = array();
377d101cc1SGerry Weißbach
387d101cc1SGerry Weißbach    // Namespace of the page to export
397d101cc1SGerry Weißbach    private $namespace = '';
407d101cc1SGerry Weißbach
417d101cc1SGerry Weißbach    /**
427d101cc1SGerry Weißbach     * Register Plugin in DW
437d101cc1SGerry Weißbach     **/
447d101cc1SGerry Weißbach    function register(&$controller) {
457d101cc1SGerry Weißbach        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax_siteexport_provider');
467d101cc1SGerry Weißbach        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'siteexport_action');
477d101cc1SGerry Weißbach    }
487d101cc1SGerry Weißbach
497d101cc1SGerry Weißbach    /**
507d101cc1SGerry Weißbach     * AJAX Provider - check what is going to be done
517d101cc1SGerry Weißbach     * @param $event
527d101cc1SGerry Weißbach     * @param $args
537d101cc1SGerry Weißbach     */
547d101cc1SGerry Weißbach    function ajax_siteexport_provider(&$event, $args) {
557d101cc1SGerry Weißbach
567d101cc1SGerry Weißbach        // If this is not a siteexport call, ignore it.
577d101cc1SGerry Weißbach        if ( !strstr($event->data, '__siteexport' ) )
587d101cc1SGerry Weißbach        {
597d101cc1SGerry Weißbach            return;
607d101cc1SGerry Weißbach        }
617d101cc1SGerry Weißbach
622270cdc5SGerry Weißbach        $this->__init_functions(true);
637d101cc1SGerry Weißbach
647d101cc1SGerry Weißbach        switch( $event->data ) {
657d101cc1SGerry Weißbach            case '__siteexport_getsitelist': $this->ajax_siteexport_getsitelist( $event ); break;
667d101cc1SGerry Weißbach            case '__siteexport_addsite': $this->ajax_siteexport_addsite( $event ); break;
677d101cc1SGerry Weißbach            case '__siteexport_generateurl': $this->ajax_siteexport_generateurl( $event ); break;
68*6792d0cfSGerry Weißbach            case '__siteexport_aggregate': $this->ajax_siteexport_aggregate( $event ); break;
697d101cc1SGerry Weißbach        }
707d101cc1SGerry Weißbach    }
717d101cc1SGerry Weißbach
727d101cc1SGerry Weißbach    /**
737d101cc1SGerry Weißbach     * Export from a URL - action
747d101cc1SGerry Weißbach     * @param $event
757d101cc1SGerry Weißbach     */
767d101cc1SGerry Weißbach    function siteexport_action( &$event ) {
777d101cc1SGerry Weißbach        global $ID;
787d101cc1SGerry Weißbach
797d101cc1SGerry Weißbach        // Check if the 'do' was siteexport
807793901eSGerry Weißbach	    $command = is_array($event->data) ? array_shift(array_keys($event->data)) : $event->data;
81fd385364SGerry Weißbach        if ( $command != 'siteexport' ) { return false; }
82fd385364SGerry Weißbach		$event->data = act_clean($event->data);
83fd385364SGerry Weißbach
847d101cc1SGerry Weißbach        if ( headers_sent() ) {
857d101cc1SGerry Weißbach            msg("The siteexport function has to be called prior to any header output.", -1);
867d101cc1SGerry Weißbach        }
877d101cc1SGerry Weißbach
887d101cc1SGerry Weißbach        $this->__init_functions();
897d101cc1SGerry Weißbach
907d101cc1SGerry Weißbach        $this->functions->debug->message("========================================", null, 1);
917d101cc1SGerry Weißbach        $this->functions->debug->message("Starting export from URL call", null, 1);
928da901a0SGerry Weißbach        $this->functions->debug->message("----------------------------------------", null, 1);
937d101cc1SGerry Weißbach
947d101cc1SGerry Weißbach        $event->preventDefault();
957d101cc1SGerry Weißbach        $event->stopPropagation();
967d101cc1SGerry Weißbach
977d101cc1SGerry Weißbach        // Fake security Token if none given
987d101cc1SGerry Weißbach        if ( empty( $_REQUEST['sectok'] ) ) {
997d101cc1SGerry Weißbach            $_REQUEST['sectok'] = getSecurityToken();
1007d101cc1SGerry Weißbach        }
1017d101cc1SGerry Weißbach
1027d101cc1SGerry Weißbach        // The timer will be used to do redirects if needed to prevent timeouts
1037d101cc1SGerry Weißbach        $starttimer = time();
1047d101cc1SGerry Weißbach        $timerdiff = $this->getConf('max_execution_time');
1057d101cc1SGerry Weißbach
1067d101cc1SGerry Weißbach        $data = $this->__get_siteexport_list_and_init_tocs($ID, !empty($_REQUEST['startcounter']));
1077d101cc1SGerry Weißbach
1087d101cc1SGerry Weißbach        if ( $data === false ) {
1097d101cc1SGerry Weißbach            header("HTTP/1.0 401 Unauthorized");
1107d101cc1SGerry Weißbach            print 'Unauthorized';
1117d101cc1SGerry Weißbach            exit;
1127d101cc1SGerry Weißbach        }
1137d101cc1SGerry Weißbach
1147d101cc1SGerry Weißbach        $counter = 0;
1157d101cc1SGerry Weißbach
1167d101cc1SGerry Weißbach        if ( count($data) == 0 && !$this->functions->settings->hasValidCacheFile ) {
1177d101cc1SGerry Weißbach            exit();
1187d101cc1SGerry Weißbach        }
1197d101cc1SGerry Weißbach
1207d101cc1SGerry Weißbach        foreach ( $data as $site ) {
1217d101cc1SGerry Weißbach
1227d101cc1SGerry Weißbach			if ( intval($site['exists']) == 1 || !isset($site['exists']) ) {
1237d101cc1SGerry Weißbach
1247d101cc1SGerry Weißbach	            // Skip over the amount of urls that have been exported already
1257d101cc1SGerry Weißbach	            if ( empty($_REQUEST['startcounter']) || $counter >= intval($_REQUEST['startcounter']) ) {
1267d101cc1SGerry Weißbach	                $status = $this->__siteexport_add_site($site['id']);
1278da901a0SGerry Weißbach
1288da901a0SGerry Weißbach			        if ( $status === false ) {
1298da901a0SGerry Weißbach				        $this->functions->debug->message("----------------------------------------", null, 1);
1308da901a0SGerry Weißbach				        $this->functions->debug->message("Errors during export from URL call", null, 1);
1318da901a0SGerry Weißbach				        $this->functions->debug->message("========================================", null, 1);
1328da901a0SGerry Weißbach						print $this->functions->debug->runtimeErrors;
1338da901a0SGerry Weißbach        			    exit(0); // We need to stop
1348da901a0SGerry Weißbach			        }
1357d101cc1SGerry Weißbach	            }
1367d101cc1SGerry Weißbach			}
1377d101cc1SGerry Weißbach
1387d101cc1SGerry Weißbach            $counter ++;
1397d101cc1SGerry Weißbach            if ( time() - $starttimer >= $timerdiff ) {
1407d101cc1SGerry Weißbach                $this->functions->debug->message("Will Redirect", null, 1);
1417d101cc1SGerry Weißbach                $this->handleRuntimeErrorOutput();
1427d101cc1SGerry Weißbach                $this->functions->startRedirctProcess($counter);
1437d101cc1SGerry Weißbach            }
1447d101cc1SGerry Weißbach        }
1457d101cc1SGerry Weißbach
1468da901a0SGerry Weißbach        $this->functions->debug->message("----------------------------------------", null, 1);
1477d101cc1SGerry Weißbach        $this->functions->debug->message("Finishing export from URL call", null, 1);
1487d101cc1SGerry Weißbach        $this->functions->debug->message("========================================", null, 1);
1497d101cc1SGerry Weißbach
1507d101cc1SGerry Weißbach        $this->cleanCacheFiles();
1517d101cc1SGerry Weißbach
1527d101cc1SGerry Weißbach        $URL = ml($this->functions->settings->origZipFile, array('cache' => 'nocache', 'siteexport' => $this->functions->settings->pattern, 'sectok' => getSecurityToken()), true, '&');
1537d101cc1SGerry Weißbach        $this->functions->debug->message("Redirecting to final file", $URL, 2);
1547d101cc1SGerry Weißbach
1557d101cc1SGerry Weißbach        $this->handleRuntimeErrorOutput();
1567d101cc1SGerry Weißbach        send_redirect($URL);
1577d101cc1SGerry Weißbach        exit(0); // Should not be reached, but anyways
1587d101cc1SGerry Weißbach    }
1597d101cc1SGerry Weißbach
1607d101cc1SGerry Weißbach    private function handleRuntimeErrorOutput()
1617d101cc1SGerry Weißbach    {
1627d101cc1SGerry Weißbach        if ( !empty($this->functions->debug->runtimeErrors) )
1637d101cc1SGerry Weißbach        {
1647d101cc1SGerry Weißbach            $this->filewriter->__moveDataToZip($this->functions->debug->runtimeErrors, '_runtime_error/' . time() . '.html');
1657d101cc1SGerry Weißbach        }
1667d101cc1SGerry Weißbach    }
1677d101cc1SGerry Weißbach
1682270cdc5SGerry Weißbach    public function __init_functions($isAJAX=false)
1697d101cc1SGerry Weißbach    {
1702270cdc5SGerry Weißbach        $this->functions = new siteexport_functions(true, $isAJAX);
1717d101cc1SGerry Weißbach        $this->filewriter = new siteexport_zipfilewriter($this->functions);
1727d101cc1SGerry Weißbach
1737d101cc1SGerry Weißbach        // Check for PDF Capabilities
1747d101cc1SGerry Weißbach        if ( $this->filewriter->canDoPDF() ) {
1757d101cc1SGerry Weißbach            $this->functions->settings->fileType = 'pdf';
1767d101cc1SGerry Weißbach        }
1777d101cc1SGerry Weißbach    }
1787d101cc1SGerry Weißbach
1797d101cc1SGerry Weißbach    /**
1807d101cc1SGerry Weißbach     * Prepares the generated URL for direct download access
1817d101cc1SGerry Weißbach     * Also gives back the parameters for this URL
1827d101cc1SGerry Weißbach     * @param $event init event of the ajax request
1837d101cc1SGerry Weißbach     */
1847d101cc1SGerry Weißbach    function ajax_siteexport_prepareURL_and_POSTData( &$event ) {
1857d101cc1SGerry Weißbach
1867d101cc1SGerry Weißbach        $event->preventDefault();
1877d101cc1SGerry Weißbach        $event->stopPropagation();
1887d101cc1SGerry Weißbach
1897d101cc1SGerry Weißbach        // Retrieve Information for download URL
190a609ae53SGerry Weißbach        $this->functions->debug->message("Prepared URL and POST from Request:", $_REQUEST, 2);
1917d101cc1SGerry Weißbach        $url = $this->functions->prepare_POSTData($_REQUEST);
1927d101cc1SGerry Weißbach        $combined = $this->functions->urlToPathAndParams($url);
1937d101cc1SGerry Weißbach        list($path, $query) = explode('?', $combined, 2);
1947d101cc1SGerry Weißbach        $return = array($url, $combined, $path, $query);
1957d101cc1SGerry Weißbach
1967d101cc1SGerry Weißbach        $this->functions->debug->message("Prepared URL and POST data:", $return, 2);
1977d101cc1SGerry Weißbach        return $return;
1987d101cc1SGerry Weißbach    }
1997d101cc1SGerry Weißbach
2007d101cc1SGerry Weißbach    /**
2017d101cc1SGerry Weißbach     * Executes a Cron Job Action
2027d101cc1SGerry Weißbach     * @param $event
2037d101cc1SGerry Weißbach     */
2047d101cc1SGerry Weißbach    function ajax_siteexport_cronaction( &$event )
2057d101cc1SGerry Weißbach    {
2067d101cc1SGerry Weißbach        $cronOverwriteExisting = intval($_REQUEST['cronOverwriteExisting']) == 1;
2077d101cc1SGerry Weißbach        list($url, $combined) = $this->ajax_siteexport_prepareURL_and_POSTData($event);
2087d101cc1SGerry Weißbach
2097d101cc1SGerry Weißbach        if ( !$function =& plugin_load('cron', 'siteexport' ) )
2107d101cc1SGerry Weißbach        {
2117d101cc1SGerry Weißbach            $this->functions->debug->message("Tried to do an action with siteexport/cron, but the cron plugin is missing.", null, 4);
2127d101cc1SGerry Weißbach        }
2137d101cc1SGerry Weißbach
2147d101cc1SGerry Weißbach        $status = null;
2157d101cc1SGerry Weißbach        switch( $event->data ) {
2167d101cc1SGerry Weißbach            case '__siteexport_savecron': $status = $function->saveCronDataWithParameters($combined, $cronOverwriteExisting); break;
2177d101cc1SGerry Weißbach            case '__siteexport_deletecron': $status = $function->deleteCronDataWithParameters($combined); break;
2187d101cc1SGerry Weißbach        }
2197d101cc1SGerry Weißbach
2207d101cc1SGerry Weißbach        if ( !empty($status) )
2217d101cc1SGerry Weißbach        {
2227d101cc1SGerry Weißbach            $this->functions->debug->message("Tried to do an action with siteexport/cron, but failed.", $status, 4);
2237d101cc1SGerry Weißbach        }
2247d101cc1SGerry Weißbach    }
2257d101cc1SGerry Weißbach
2267d101cc1SGerry Weißbach    /**
2277d101cc1SGerry Weißbach     * generate direct access URL
2287d101cc1SGerry Weißbach     **/
2297d101cc1SGerry Weißbach    function ajax_siteexport_generateurl( &$event ) {
2307d101cc1SGerry Weißbach
2317d101cc1SGerry Weißbach        list($url, $combined, $path, $POSTData) = $this->ajax_siteexport_prepareURL_and_POSTData($event);
2327d101cc1SGerry Weißbach
2337d101cc1SGerry Weißbach        // WGET Redirects - this is an option for wget only.
2347d101cc1SGerry Weißbach        // 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
2357d101cc1SGerry Weißbach        // Therefore we assume it takes about 5s for each page - that gives the freedom to have anough time for redirect.
2367d101cc1SGerry Weißbach        $maxRedirectNumber = ceil( ( count($this->__get_siteexport_list($NS, true)) * 5) / $this->getConf('max_execution_time') );
2377d101cc1SGerry Weißbach        $maxRedirect = $maxRedirectNumber > 0 ? '--max-redirect=' . ($maxRedirectNumber+3) . ' ' : '';
2387d101cc1SGerry Weißbach        $maxRedirs = $maxRedirectNumber > 0 ? '--max-redirs ' . ($maxRedirectNumber+3) . ' ' : '';
2397d101cc1SGerry Weißbach
2407d101cc1SGerry Weißbach        $this->functions->debug->message("Generating Direct Download URL", $url, 2);
2417d101cc1SGerry Weißbach
2427d101cc1SGerry Weißbach        // If there was a Runtime Exception
2437d101cc1SGerry Weißbach        if ( !$this->functions->debug->firstRE() ) {
2447d101cc1SGerry Weißbach            $this->functions->debug->message("There have been errors while generating the download URLs.", null, 4);
2457d101cc1SGerry Weißbach            return;
2467d101cc1SGerry Weißbach        }
2477d101cc1SGerry Weißbach
2487d101cc1SGerry Weißbach        echo $url;
2497d101cc1SGerry Weißbach        echo "\n";
2507d101cc1SGerry Weißbach        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';
2517d101cc1SGerry Weißbach        echo "\n";
2527d101cc1SGerry Weißbach        echo 'curl -L ' . $maxRedirs . '-o ' . array_pop(explode(":", ($this->getConf('zipfilename')))) . ' -d "' . $POSTData . '" ' . wl(cleanID($path), null, true) . ' --anyauth --user USER:PASSWD';
2537d101cc1SGerry Weißbach        echo "\n";
2547d101cc1SGerry Weißbach
2557d101cc1SGerry Weißbach        $this->functions->debug->message("Checking for Cron parameters: ", $combined, 1);
2567d101cc1SGerry Weißbach        if ( !$functions =& plugin_load('cron', 'siteexport' ) ||
2577d101cc1SGerry Weißbach        !$functions->hasCronJobForParameters($combined) ) {
2587d101cc1SGerry Weißbach            echo "false";
2597d101cc1SGerry Weißbach        } else
2607d101cc1SGerry Weißbach        {
2617d101cc1SGerry Weißbach            echo "true";
2627d101cc1SGerry Weißbach        }
2637d101cc1SGerry Weißbach
2647d101cc1SGerry Weißbach        return;
2657d101cc1SGerry Weißbach    }
2667d101cc1SGerry Weißbach
2677d101cc1SGerry Weißbach    /**
2687d101cc1SGerry Weißbach     * Get List of sites to be exported for AJAX (wrapper)
2697d101cc1SGerry Weißbach     **/
2707d101cc1SGerry Weißbach    function ajax_siteexport_getsitelist( &$event ) {
2717d101cc1SGerry Weißbach
2727d101cc1SGerry Weißbach        $event->preventDefault();
2737d101cc1SGerry Weißbach        $event->stopPropagation();
2747d101cc1SGerry Weißbach
2757d101cc1SGerry Weißbach        $data = $this->__get_siteexport_list_and_init_tocs($_REQUEST['ns']);
2767d101cc1SGerry Weißbach
2777d101cc1SGerry Weißbach        // Important for reconaisance of the session
2787d101cc1SGerry Weißbach
2797d101cc1SGerry Weißbach        if ( $data === false )
2807d101cc1SGerry Weißbach        {
2817d101cc1SGerry Weißbach            $this->functions->debug->runtimeException("No data generated. List of Files is 'false'.");
2827d101cc1SGerry Weißbach            return;
2837d101cc1SGerry Weißbach        }
2847d101cc1SGerry Weißbach
2857d101cc1SGerry Weißbach        if ( empty($data) && !$this->functions->settings->hasValidCacheFile )
2867d101cc1SGerry Weißbach        {
2877d101cc1SGerry Weißbach            $this->functions->debug->runtimeException("Generated list is empty.");
2887d101cc1SGerry Weißbach            return;
2897d101cc1SGerry Weißbach        }
2907d101cc1SGerry Weißbach
2917d101cc1SGerry Weißbach        // If there was a Runtime Exception
2927d101cc1SGerry Weißbach        if ( !$this->functions->debug->firstRE() )
2937d101cc1SGerry Weißbach        {
2947d101cc1SGerry Weißbach            $this->functions->debug->message("There have been errors while generating site list.", null, 4);
2957d101cc1SGerry Weißbach            return;
2967d101cc1SGerry Weißbach        }
2977d101cc1SGerry Weißbach
2987d101cc1SGerry Weißbach        echo "{$this->functions->settings->pattern}\n";
2997d101cc1SGerry Weißbach        echo $this->functions->downloadURL() . "\n";
3007d101cc1SGerry Weißbach        foreach($data as $line ){
3017d101cc1SGerry Weißbach            echo $line['id'] . "\n";
3027d101cc1SGerry Weißbach        }
3037d101cc1SGerry Weißbach
3047d101cc1SGerry Weißbach        return;
3057d101cc1SGerry Weißbach    }
3067d101cc1SGerry Weißbach
307*6792d0cfSGerry Weißbach    function ajax_siteexport_aggregate( &$event ) {
308*6792d0cfSGerry Weißbach
309*6792d0cfSGerry Weißbach        // Quick preparations for one page only
310*6792d0cfSGerry Weißbach        if ( $this->filewriter->hasValidCacheFile($_REQUEST, $data) ) {
311*6792d0cfSGerry Weißbach            $this->functions->debug->message("Had a valid cache file and will use it.", null, 2);
312*6792d0cfSGerry Weißbach            print $this->functions->downloadURL();
313*6792d0cfSGerry Weißbach        } else {
314*6792d0cfSGerry Weißbach            // Then go for it!
315*6792d0cfSGerry Weißbach            $this->functions->debug->message("Will create a new cache thing.", null, 2);
316*6792d0cfSGerry Weißbach            $this->ajax_siteexport_addsite( $event );
317*6792d0cfSGerry Weißbach        }
318*6792d0cfSGerry Weißbach
319*6792d0cfSGerry Weißbach    }
320*6792d0cfSGerry Weißbach
3217d101cc1SGerry Weißbach    /**
3227d101cc1SGerry Weißbach     * Add a page to the package (for AJAX calls - Wrapper)
3237d101cc1SGerry Weißbach     **/
3247d101cc1SGerry Weißbach    function ajax_siteexport_addsite( &$event ) {
3257d101cc1SGerry Weißbach
3267d101cc1SGerry Weißbach        $event->preventDefault();
3277d101cc1SGerry Weißbach        $event->stopPropagation();
3287d101cc1SGerry Weißbach
3297d101cc1SGerry Weißbach        $this->functions->debug->message("========================================", null, 1);
3307d101cc1SGerry Weißbach        $this->functions->debug->message("Starting export from AJAX call", null, 1);
3318da901a0SGerry Weißbach        $this->functions->debug->message("----------------------------------------", null, 1);
3327d101cc1SGerry Weißbach
3337d101cc1SGerry Weißbach        $status = $this->__siteexport_add_site($_REQUEST['site']);
3348da901a0SGerry Weißbach        if ( $status === false ) {
3358da901a0SGerry Weißbach	        $this->functions->debug->message("----------------------------------------", null, 1);
3368da901a0SGerry Weißbach	        $this->functions->debug->message("Errors during export from AJAX call", null, 1);
3378da901a0SGerry Weißbach	        $this->functions->debug->message("========================================", null, 1);
3388da901a0SGerry Weißbach        	return;
3398da901a0SGerry Weißbach        }
3407d101cc1SGerry Weißbach
3418da901a0SGerry Weißbach        $this->functions->debug->message("----------------------------------------", null, 1);
3427d101cc1SGerry Weißbach        $this->functions->debug->message("Finishing export from AJAX call", null, 1);
3437d101cc1SGerry Weißbach        $this->functions->debug->message("========================================", null, 1);
3447d101cc1SGerry Weißbach
3457d101cc1SGerry Weißbach        // Print the download zip-File
3467d101cc1SGerry Weißbach        $this->cleanCacheFiles();
3477d101cc1SGerry Weißbach
3487d101cc1SGerry Weißbach        // If there was a Runtime Exception
3497d101cc1SGerry Weißbach        if ( !$this->functions->debug->firstRE() ) {
3507d101cc1SGerry Weißbach            $this->functions->debug->message("There have been errors during the export.", null, 4);
3517d101cc1SGerry Weißbach            return;
3527d101cc1SGerry Weißbach        }
3537d101cc1SGerry Weißbach
3547d101cc1SGerry Weißbach        print $this->functions->downloadURL();
3557d101cc1SGerry Weißbach        return;
3567d101cc1SGerry Weißbach    }
3577d101cc1SGerry Weißbach
3587d101cc1SGerry Weißbach    /**
3597d101cc1SGerry Weißbach     * Fetch the list of pages to be exported
3607d101cc1SGerry Weißbach     **/
3617d101cc1SGerry Weißbach    function __get_siteexport_list($NS, $overrideCache=false) {
3627d101cc1SGerry Weißbach        global $conf;
3637d101cc1SGerry Weißbach
3647d101cc1SGerry Weißbach        $NS = $this->namespace = $this->functions->getNamespaceFromID($NS, $PAGE);
3657d101cc1SGerry Weißbach
3667d101cc1SGerry Weißbach        $depth = $this->getConf('depth');
3677d101cc1SGerry Weißbach        $query = '';
3687d101cc1SGerry Weißbach        $doSearch = 'search_allpages';
3697d101cc1SGerry Weißbach
3707d101cc1SGerry Weißbach        switch( intval($_REQUEST['depthType']) ) {
3717d101cc1SGerry Weißbach            case 0:
3727d101cc1SGerry Weißbach                $query = $this->functions->cleanID(str_replace(":", "/", $NS.':'.$PAGE));
3737d101cc1SGerry Weißbach                resolve_pageid($NS, $PAGE, $exists);
3747d101cc1SGerry Weißbach
3757d101cc1SGerry Weißbach                if ( $exists ) {
3767d101cc1SGerry Weißbach                    $data = array( array( 'id' => $PAGE) );
3777d101cc1SGerry Weißbach
3787d101cc1SGerry Weißbach                    $this->functions->debug->message("Checking for Cache", null, 2);
3797d101cc1SGerry Weißbach                    if ( !$overrideCache && $this->filewriter->hasValidCacheFile($_REQUEST, $data) )
3807d101cc1SGerry Weißbach                    {
3817d101cc1SGerry Weißbach                        return array();
3827d101cc1SGerry Weißbach                    }
3837d101cc1SGerry Weißbach
3847d101cc1SGerry Weißbach                    return $data;
3857d101cc1SGerry Weißbach                }
3867d101cc1SGerry Weißbach            case 1:	$depth = 0;
3877d101cc1SGerry Weißbach            break;
3887d101cc1SGerry Weißbach            case 2:	$depth = intval($_REQUEST['depth']);
3897d101cc1SGerry Weißbach            break;
3907d101cc1SGerry Weißbach        }
3917d101cc1SGerry Weißbach
3927d101cc1SGerry Weißbach        $opts = array( 'depth' => $depth, 'skipacl' => $this->getConf('skipacl'), 'query' => $query);
393a609ae53SGerry Weißbach        $this->functions->debug->message("Options", $opts, 2);
394a609ae53SGerry Weißbach
3957d101cc1SGerry Weißbach        $data = array();
3967d101cc1SGerry Weißbach        require_once (DOKU_INC.'inc/search.php');
3977d101cc1SGerry Weißbach
3987d101cc1SGerry Weißbach        // Check, which TOC to take
3997d101cc1SGerry Weißbach        if ( !$this->functions->settings->useTOCFile ) {
4007d101cc1SGerry Weißbach            search($data, $conf['datadir'], $doSearch, $opts, $this->namespace);
4017d101cc1SGerry Weißbach        } else {
4027d101cc1SGerry Weißbach            $this->functions->debug->message("Using TOC for data", null, 2);
4037d101cc1SGerry Weißbach
4047d101cc1SGerry Weißbach            $doSearch = 'search_pagename';
4057d101cc1SGerry Weißbach
4067d101cc1SGerry Weißbach            // Create Data of the TOC File should be used instead
4077d101cc1SGerry Weißbach            $opts['query'] = 'toc.txt';
4087d101cc1SGerry Weißbach
4097d101cc1SGerry Weißbach            $RAWdata = array();
4107d101cc1SGerry Weißbach            search($RAWdata, $conf['datadir'], $doSearch, $opts, $this->namespace);
4117d101cc1SGerry Weißbach
4127d101cc1SGerry Weißbach            // There may be more than one toc and all of them have to be merged.
4137d101cc1SGerry Weißbach            $data = array();
4147d101cc1SGerry Weißbach            foreach( $RAWdata as $entry )
4157d101cc1SGerry Weißbach            {
4167d101cc1SGerry Weißbach                $tmpData = p_get_metadata($entry['id'], 'sitetoc siteexportTOC', true);
4177d101cc1SGerry Weißbach
4187d101cc1SGerry Weißbach                if ( is_array($tmpData) )
4197d101cc1SGerry Weißbach                {
4207d101cc1SGerry Weißbach                    $data = array_merge($data, $tmpData);
4217d101cc1SGerry Weißbach                }
4227d101cc1SGerry Weißbach            }
4237d101cc1SGerry Weißbach        }
4247d101cc1SGerry Weißbach
4257d101cc1SGerry Weißbach        $this->functions->debug->message("Checking for Cache", null, 2);
4267d101cc1SGerry Weißbach        if ( !$overrideCache && $this->filewriter->hasValidCacheFile($_REQUEST, $data) )
4277d101cc1SGerry Weißbach        {
4287d101cc1SGerry Weißbach            return array();
4297d101cc1SGerry Weißbach        }
4307d101cc1SGerry Weißbach
4317d101cc1SGerry Weißbach        $this->functions->debug->message("Exporting the following sites: ", $data, 2);
4327d101cc1SGerry Weißbach        return $data;
4337d101cc1SGerry Weißbach    }
4347d101cc1SGerry Weißbach
4357d101cc1SGerry Weißbach    function __get_siteexport_list_and_init_tocs($NS, $isRedirected=false ) {
4367d101cc1SGerry Weißbach
4377d101cc1SGerry Weißbach        // Clean up if not redirected
4387d101cc1SGerry Weißbach        if ( !$isRedirected && !$this->__removeOldZip() ) {
4397d101cc1SGerry Weißbach            $this->functions->debug->runtimeException("Can't remove old files.");
4407d101cc1SGerry Weißbach            return false;
4417d101cc1SGerry Weißbach        }
4427d101cc1SGerry Weißbach
4437d101cc1SGerry Weißbach        $data = $this->__get_siteexport_list($NS, $isRedirected);
4447d101cc1SGerry Weißbach        if ( $isRedirected || empty($data) )
4457d101cc1SGerry Weißbach        {
4467d101cc1SGerry Weißbach            // if we have been redirected, simply return the data
4477d101cc1SGerry Weißbach            return $data;
4487d101cc1SGerry Weißbach        }
4497d101cc1SGerry Weißbach
4507d101cc1SGerry Weißbach        // Create Eclipse Documentation Pages - TOC.xml, Context.xml
4517d101cc1SGerry Weißbach        if ( !empty($_REQUEST['absolutePath']) ) $this->namespace = "";
4527d101cc1SGerry Weißbach//        $this->__removeOldZip( $this->functions->settings->eclipseZipFile );
4537d101cc1SGerry Weißbach
4547d101cc1SGerry Weißbach        if ( !empty($_REQUEST['eclipseDocZip']) )
4557d101cc1SGerry Weißbach        {
4567d101cc1SGerry Weißbach            $toc = new siteexport_toc($this->functions);
4577d101cc1SGerry Weißbach            $this->functions->debug->message("Generating eclipseDocZip", null, 2);
4587d101cc1SGerry Weißbach            $this->filewriter->__moveDataToZip($toc->__getTOCXML($data), 'toc.xml');
4597d101cc1SGerry Weißbach            $this->filewriter->__moveDataToZip($toc->__getContextXML($data), 'context.xml');
4607d101cc1SGerry Weißbach        } else  if ( !empty($_REQUEST['JavaHelpDocZip']) )
4617d101cc1SGerry Weißbach        {
4627d101cc1SGerry Weißbach            $toc = new siteexport_javahelp($this->functions, $this->filewriter);
4637d101cc1SGerry Weißbach            $toc->createTOCFiles($data);
4647d101cc1SGerry Weißbach
4657d101cc1SGerry Weißbach/*            $toc = new siteexport_toc($this->functions);
4667d101cc1SGerry Weißbach            list($tocData, $mapData) = $toc->__getJavaHelpTOCXML($data);
4677d101cc1SGerry Weißbach            $this->functions->debug->message("Generating JavaHelpDocZip", null, 2);
4687d101cc1SGerry Weißbach            $this->filewriter->__moveDataToZip($tocData, 'toc.xml');
4697d101cc1SGerry Weißbach            $this->filewriter->__moveDataToZip($mapData, 'map.xml');
4707d101cc1SGerry Weißbach*/        }
4717d101cc1SGerry Weißbach
4727d101cc1SGerry Weißbach        return $data;
4737d101cc1SGerry Weißbach    }
4747d101cc1SGerry Weißbach
4757d101cc1SGerry Weißbach    /**
4767d101cc1SGerry Weißbach     * Add page with ID to the package
4777d101cc1SGerry Weißbach     **/
4787d101cc1SGerry Weißbach    function __siteexport_add_site( $ID ) {
4797d101cc1SGerry Weißbach        global $conf, $currentID;
4807d101cc1SGerry Weißbach
4817d101cc1SGerry Weißbach        // Which is the current ID?
4827d101cc1SGerry Weißbach        $currentID = $ID;
4837d101cc1SGerry Weißbach
4847d101cc1SGerry Weißbach        $this->functions->debug->message("========================================", null, 2);
4857d101cc1SGerry Weißbach        $this->functions->debug->message("Adding Site: '$ID'", null, 2);
4864b7d84d7SGerry Weißbach        $this->functions->debug->message("----------------------------------------", $_REQUEST, 2);
4877d101cc1SGerry Weißbach
4887d101cc1SGerry Weißbach        $request = $this->functions->settings->additionalParameters;
4897d101cc1SGerry Weißbach        unset($request['diPlu']); // This will not be needed for the first request.
4907d101cc1SGerry Weißbach        unset($request['diInv']); // This will not be needed for the first request.
4917d101cc1SGerry Weißbach
4927d101cc1SGerry Weißbach        // say, what to export and Build URL
4937d101cc1SGerry Weißbach        // 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
4947d101cc1SGerry Weißbach
4957d101cc1SGerry Weißbach        $do = (intval($_REQUEST['exportbody']) == 1 ? (empty($_REQUEST['renderer']) ? $conf['renderer_xhtml'] : $_REQUEST['renderer'] ) : '' );
4967d101cc1SGerry Weißbach
4977d101cc1SGerry Weißbach        if ($do == 'pdf' && $this->filewriter->canDoPDF() )
4987d101cc1SGerry Weißbach        {
4997d101cc1SGerry Weißbach            $do = 'export_siteexport_pdf';
5007d101cc1SGerry Weißbach            $_REQUEST['origRenderer'] = (empty($_REQUEST['renderer']) ? $conf['renderer_xhtml'] : $_REQUEST['renderer'] );
5017d101cc1SGerry Weißbach        }
5027d101cc1SGerry Weißbach
5037d101cc1SGerry Weißbach        $do = ($do == $conf['renderer_xhtml'] && intval($_REQUEST['exportbody']) != 1) ? '' : 'export_' . $do;
5047d101cc1SGerry Weißbach
5057d101cc1SGerry Weißbach        if ( $do != 'export_' && !empty($do) )
5067d101cc1SGerry Weißbach        {
5077d101cc1SGerry Weißbach            $request['do'] = $do;
5087d101cc1SGerry Weißbach        }
5097d101cc1SGerry Weißbach
5107d101cc1SGerry Weißbach        // set Template
5117d101cc1SGerry Weißbach        if ( !empty( $_REQUEST['template'] ) ) {
5127d101cc1SGerry Weißbach            $request['template'] = $_REQUEST['template'];
5137d101cc1SGerry Weißbach        }
5147d101cc1SGerry Weißbach
5157d101cc1SGerry Weißbach        $this->functions->debug->message("REQUEST for add_site:", $request, 2);
5167d101cc1SGerry Weißbach
5177d101cc1SGerry Weißbach        $ID = $this->functions->cleanID($ID);
5187d101cc1SGerry Weißbach        $url = $this->functions->wl($ID, $request, true, '&');
5197d101cc1SGerry Weißbach
5207d101cc1SGerry Weißbach        // Parse URI PATH and add "html"
5217d101cc1SGerry Weißbach        $fileName = $this->functions->getSiteName($ID, true);
522*6792d0cfSGerry Weißbach        $this->functions->debug->message("Filename could be:", $fileName, 2);
5237d101cc1SGerry Weißbach
5247d101cc1SGerry Weißbach        $this->fileChecked[$url] = $fileName; // 2010-09-03 - One URL to one FileName
5257d101cc1SGerry Weißbach        $this->functions->settings->depth = str_repeat('../', count(explode('/', $fileName))-1);
5267d101cc1SGerry Weißbach
5277d101cc1SGerry Weißbach        // fetch URL and save it in temp file
5287d101cc1SGerry Weißbach        $tmpFile = $this->__getHTTPFile($url);
5297d101cc1SGerry Weißbach        if ( $tmpFile === false ) {
5308da901a0SGerry Weißbach        	// return $this->functions->debug->message("Creating temporary download file failed for '$url'. See log for more information.");
5318da901a0SGerry Weißbach        	$this->functions->debug->runtimeException("Creating temporary download file failed for '$url'. See log for more information.");
5328da901a0SGerry Weißbach        	return false;
5337d101cc1SGerry Weißbach        }
5347d101cc1SGerry Weißbach
535281ed919SGerry Weißbach        $dirname = dirname($fileName);
5367d101cc1SGerry Weißbach        // If a Filename was given that does not comply to the original name, use this one!
537281ed919SGerry Weißbach		if ( $this->filewriter->canDoPDF() ) {
5387d101cc1SGerry Weißbach
539281ed919SGerry Weißbach			$this->functions->debug->message("Will replace old filename '{$fileName}' with {$tmpFile[2]}", null, 1);
540281ed919SGerry Weißbach        	$extension = array_pop(explode('.', $fileName));
541281ed919SGerry Weißbach			$fileName = $dirname . '/' .  $this->functions->getSiteTitle($ID) . '.' . $extension;
542281ed919SGerry Weißbach        } else if ( !empty($tmpFile[1]) && !strstr($DATA[2], $tmpFile[1]) ) {
5437d101cc1SGerry Weißbach
544*6792d0cfSGerry Weißbach			$this->functions->debug->message("Will replace old filename '{$fileName}' with {$dirname}/{$tmpFile[1]}", null, 1);
545281ed919SGerry Weißbach			$fileName = $dirname . '/' . $tmpFile[1];
5467d101cc1SGerry Weißbach        }
5477d101cc1SGerry Weißbach
5487d101cc1SGerry Weißbach        // Add to zip
54984d65497SGerry Weißbach		$this->fileChecked[$url] = $fileName;
5507d101cc1SGerry Weißbach        $status = $this->filewriter->__addFileToZip($tmpFile[0], $fileName);
5517d101cc1SGerry Weißbach        @unlink($tmpFile[0]);
5527d101cc1SGerry Weißbach
5537d101cc1SGerry Weißbach        return $status;
5547d101cc1SGerry Weißbach    }
5557d101cc1SGerry Weißbach
556d3cbbad8SGerry Weißbach    function __preg_quote($input) {
557d3cbbad8SGerry Weißbach	    return preg_quote($input, '/');
558d3cbbad8SGerry Weißbach    }
559d3cbbad8SGerry Weißbach
5607d101cc1SGerry Weißbach    /**
5617d101cc1SGerry Weißbach     * Download the file via HTTP URL + recurse if this is not an image
5627d101cc1SGerry Weißbach     * The file will be saved as temporary file. The filename is the result.
5637d101cc1SGerry Weißbach     **/
5647d101cc1SGerry Weißbach    function __getHTTPFile($URL, $RECURSE=false, $newAdditionalParameters=null) {
5657d101cc1SGerry Weißbach        global $conf;
5667d101cc1SGerry Weißbach
567d3cbbad8SGerry Weißbach        $EXCLUDE = $this->getConf('exclude');
568d3cbbad8SGerry Weißbach        if ( !empty($EXCLUDE) ) {
569d3cbbad8SGerry Weißbach	        $PATTERN = "/(" . implode('|', explode(' ', preg_quote($EXCLUDE, '/'))) . ")/i";
5707d101cc1SGerry Weißbach
571d3cbbad8SGerry Weißbach	        $this->functions->debug->message("Checking for exclude: ", array(
572d3cbbad8SGerry Weißbach	        	"pattern" => $PATTERN,
573d3cbbad8SGerry Weißbach	        	"file" => $URL,
574d3cbbad8SGerry Weißbach	        	"matches" => preg_match($PATTERN, $URL) ? 'match' : 'no match'
575d3cbbad8SGerry Weißbach	        ), 2);
576d3cbbad8SGerry Weißbach
577d3cbbad8SGerry Weißbach			if ( preg_match($PATTERN, $URL) ) { return false; }
578d3cbbad8SGerry Weißbach        }
5797d101cc1SGerry Weißbach
5807d101cc1SGerry Weißbach        require_once( DOKU_INC . 'inc/HTTPClient.php');
5817d101cc1SGerry Weißbach
5828da901a0SGerry Weißbach        $http = new HTTPProxy($this->functions->debug, $this->functions->settings);
5837d101cc1SGerry Weißbach        $http->max_bodysize = $conf['fetchsize'];
5847d101cc1SGerry Weißbach        // $http->user = $_SERVER['PHP_AUTH_USER']; // Must not be set, or the files will be authenticated and have the edit thingies
5857d101cc1SGerry Weißbach        // $http->pass = $_SERVER['PHP_AUTH_PW']; // Must not be set, or the files will be authenticated and have the edit thingies
5867d101cc1SGerry Weißbach
5877d101cc1SGerry Weißbach        // Add additional Params
5887d101cc1SGerry Weißbach        $this->functions->addAdditionalParametersToURL($URL, $newAdditionalParameters);
5897d101cc1SGerry Weißbach
5907d101cc1SGerry Weißbach        $this->functions->debug->message("Fetching URL: '$URL'", null, 2);
5917d101cc1SGerry Weißbach        $getData = $http->get($URL);
5927d101cc1SGerry Weißbach
5937d101cc1SGerry Weißbach        if( $getData === false ) {
594cb168401SGerry Weißbach
595cb168401SGerry Weißbach        	if ( $this->functions->settings->ignoreNon200 ) {
596cb168401SGerry Weißbach	        	return null;
597cb168401SGerry Weißbach        	}
598cb168401SGerry Weißbach
5997d101cc1SGerry Weißbach            $this->functions->debug->message("Sending request failed with error, HTTP status was '{$http->status}'.", $URL, 4);
6007d101cc1SGerry Weißbach            return false;
6017d101cc1SGerry Weißbach        }
6027d101cc1SGerry Weißbach
6037d101cc1SGerry Weißbach        if( empty($getData) ) {
6047d101cc1SGerry Weißbach            $this->functions->debug->message("No data fetched.", null , 4);
6057d101cc1SGerry Weißbach            return false;
6067d101cc1SGerry Weißbach        }
6077d101cc1SGerry Weißbach
6087d101cc1SGerry Weißbach        $tmpFile = tempnam($this->functions->settings->tmpDir , 'siteexport__');
6097d101cc1SGerry Weißbach        $this->functions->debug->message("Temporary filename", $tmpFile, 1);
6107d101cc1SGerry Weißbach
6117d101cc1SGerry Weißbach        $fp = fopen( $tmpFile, "w");
6127d101cc1SGerry Weißbach        if(!$fp) {
6137d101cc1SGerry Weißbach            $this->functions->debug->message("Can't open temporary File '$tmpFile'.", null , 4);
6147d101cc1SGerry Weißbach            return false;
6157d101cc1SGerry Weißbach        }
6167d101cc1SGerry Weißbach
617281ed919SGerry Weißbach        $this->functions->debug->message("Headers received", $http->resp_headers, 2);
618281ed919SGerry Weißbach
6197d101cc1SGerry Weißbach        if ( !$RECURSE ) {
6207d101cc1SGerry Weißbach            // Parse URI PATH and add "html"
6217d101cc1SGerry Weißbach            $this->functions->debug->message("========================================", null, 1);
6227d101cc1SGerry Weißbach            $this->functions->debug->message("Starting to recurse file '$URL'", null , 1);
6238da901a0SGerry Weißbach			$this->functions->debug->message("----------------------------------------", null, 1);
6247d101cc1SGerry Weißbach            $this->__getInternalLinks($getData);
6258da901a0SGerry Weißbach			$this->functions->debug->message("----------------------------------------", null, 1);
6267d101cc1SGerry Weißbach            $this->functions->debug->message("Finished to recurse file '$URL'", null , 1);
6277d101cc1SGerry Weißbach            $this->functions->debug->message("========================================", null, 1);
6287d101cc1SGerry Weißbach        }
6297d101cc1SGerry Weißbach
6307d101cc1SGerry Weißbach        fwrite($fp,$getData);
6317d101cc1SGerry Weißbach        fclose($fp);
6327d101cc1SGerry Weißbach
6337d101cc1SGerry Weißbach        return array($tmpFile, preg_replace("/.*?filename=\"?(.*?)\"?;?$/", "$1", $http->resp_headers['content-disposition']));
6347d101cc1SGerry Weißbach    }
6357d101cc1SGerry Weißbach
6367d101cc1SGerry Weißbach    /**
6377d101cc1SGerry Weißbach     * Find internal links in the currently downloaded file. This also matches inside CSS files
6387d101cc1SGerry Weißbach     **/
6397d101cc1SGerry Weißbach    function __getInternalLinks(&$DATA) {
6407d101cc1SGerry Weißbach
6417d101cc1SGerry Weißbach        $PATTERN = '(href|src|action)="([^"]*)"';
6427d101cc1SGerry Weißbach        $CALLBACK = array($this, '__fetchAndReplaceLink');
6437d101cc1SGerry Weißbach        $DATA = preg_replace_callback("/$PATTERN/i", $CALLBACK, $DATA);
6447d101cc1SGerry Weißbach
6450fc5c0e0SGerry Weißbach        $PATTERNCSS = '(url\s*?)\(([^\)]*)\)';
6467d101cc1SGerry Weißbach        $DATA = preg_replace_callback("/$PATTERNCSS/i", $CALLBACK, $DATA);
6477d101cc1SGerry Weißbach    }
6487d101cc1SGerry Weißbach
6497d101cc1SGerry Weißbach    /**
6507d101cc1SGerry Weißbach     * Deep Fetch and replace of links inside the texts matched by __getInternalLinks
6517d101cc1SGerry Weißbach     **/
6527d101cc1SGerry Weißbach    function __fetchAndReplaceLink($DATA) {
6537d101cc1SGerry Weißbach        global $conf, $currentID;
6547d101cc1SGerry Weißbach
6557d101cc1SGerry Weißbach        $noDeepReplace = true;
6567d101cc1SGerry Weißbach        $newAdditionalParameters = $this->functions->settings->additionalParameters;
6577d101cc1SGerry Weißbach        $newDepth = $this->functions->settings->depth;
6587d101cc1SGerry Weißbach        $hadBase = false;
6597d101cc1SGerry Weißbach
660d3cbbad8SGerry Weißbach		// Clean data[2], remote ' and "
661d3cbbad8SGerry Weißbach		$DATA[2] = preg_replace("/^\s*?['\"]?(.*?)['\"]?\s*?$/", '\1', trim($DATA[2]));
662d3cbbad8SGerry Weißbach
6637d101cc1SGerry Weißbach        $this->functions->debug->message("Starting Link Replacement", $DATA, 2);
6647d101cc1SGerry Weißbach
6657d101cc1SGerry Weißbach        // $DATA[2] = urldecode($DATA[2]); // Leads to problems because it does not re-encode the url
6667d101cc1SGerry Weißbach        // External and mailto links
6677d101cc1SGerry Weißbach        if ( preg_match("%^(https?://|mailto:|javascript:|data:)%", $DATA[2]) ) {
6687d101cc1SGerry Weißbach            $this->functions->debug->message("Don't like http, mailto, data or javascript links here", null, 1);
6697d101cc1SGerry Weißbach            return $this->__rebuildLink($DATA, "");
6707d101cc1SGerry Weißbach        }
6717d101cc1SGerry Weißbach        //if ( preg_match("%^(https?://|mailto:|" . DOKU_BASE . "/_export/)%", $DATA[2]) ) { return $this->__rebuildLink($DATA, ""); }
6727d101cc1SGerry Weißbach        // External media - this is deep down in the link, so we have to grep it out
6737d101cc1SGerry Weißbach        if ( preg_match("%media=(https?://.*?$)%", $DATA[2], $matches) ) {
6747d101cc1SGerry Weißbach            $DATA[2] = $matches[1];
6757d101cc1SGerry Weißbach            $this->functions->debug->message("This is an HTTP like somewhere else", $DATA, 1);
6767d101cc1SGerry Weißbach            return $this->__rebuildLink($DATA, "");
6777d101cc1SGerry Weißbach        }
6787d101cc1SGerry Weißbach        // reference only links won't have to be rewritten
6797d101cc1SGerry Weißbach        if ( preg_match("%^#.*?$%", $DATA[2]) ) {
6807d101cc1SGerry Weißbach            $this->functions->debug->message("This is a refercence only", null, 1);
6817d101cc1SGerry Weißbach            return $this->__rebuildLink($DATA, "");
6827d101cc1SGerry Weißbach        }
6837d101cc1SGerry Weißbach
6847d101cc1SGerry Weißbach        // strip all things out
6857d101cc1SGerry Weißbach        // changed Data
6867d101cc1SGerry Weißbach        $PARAMS = @parse_url($DATA[2], PHP_URL_QUERY);
6877d101cc1SGerry Weißbach        $ANCHOR = @parse_url($DATA[2], PHP_URL_FRAGMENT);
6887d101cc1SGerry Weißbach        $DATA[2] = @parse_url($DATA[2], PHP_URL_PATH);
6897d101cc1SGerry Weißbach
6907d101cc1SGerry Weißbach        // 2010-08-25 - fix problem with relative movement in links ( "test/../test2" )
6917d101cc1SGerry Weißbach        $tmpData2 = '';
6927d101cc1SGerry Weißbach        while( $tmpData2 != $DATA[2] ) {
6937d101cc1SGerry Weißbach            $tmpData2 = $DATA[2];
6947d101cc1SGerry Weißbach            $DATA[2] = preg_replace("#/(?!\.\.)[^\/]*?/\.\./#", '/', $DATA[2]);
6957d101cc1SGerry Weißbach        }
6967d101cc1SGerry Weißbach
6977d101cc1SGerry Weißbach        $temp = preg_replace("%^" . DOKU_BASE . "%", "", $DATA[2]);
6987d101cc1SGerry Weißbach        if ( $temp != $DATA[2] ) {
6997d101cc1SGerry Weißbach            $DATA[2] = $temp;
7007d101cc1SGerry Weißbach            $hadBase = true; // 2010-08-23 Check if there has been a rewrite here that will have to be considered later on
7017d101cc1SGerry Weißbach        }
7027d101cc1SGerry Weißbach
7037d101cc1SGerry Weißbach        $this->functions->debug->message("URL before rewriting option for others than 1", array($DATA, $PARAMS, $hadBase), 1);
7047d101cc1SGerry Weißbach
705d3cbbad8SGerry Weißbach        // Handle rewrites other than 1 - just for non-lib-files
706d3cbbad8SGerry Weißbach        // if ( !preg_match('$^/?lib/$', $DATA[2]) ) {
707d3cbbad8SGerry Weißbach        if ( !preg_match('$^(' . DOKU_BASE . ')?lib/$', $DATA[2]) ) {
708d3cbbad8SGerry Weißbach		    $this->functions->debug->message("Did not match '$^(" . DOKU_BASE . ")?lib/$' userewrite == ", $conf['userewrite'], 2);
7097d101cc1SGerry Weißbach            if ( $conf['userewrite'] == 2 ) {
7107d101cc1SGerry Weißbach                $DATA[2] = $this->__getInternalRewriteURL($DATA[2]);
7117d101cc1SGerry Weißbach            } elseif ( $conf['userewrite'] == 0 ) {
7127d101cc1SGerry Weißbach                $this->__getParamsAndDataRewritten($DATA, $PARAMS);
7137d101cc1SGerry Weißbach            }
7140fc5c0e0SGerry Weißbach        } else {
7150fc5c0e0SGerry Weißbach	    	$this->functions->debug->message("This file must be inside lib ...", null, 2);
7167d101cc1SGerry Weißbach        }
7177d101cc1SGerry Weißbach
7187d101cc1SGerry Weißbach        $this->functions->debug->message("URL before rewriting option", array($DATA, $PARAMS), 2);
7197d101cc1SGerry Weißbach
7207d101cc1SGerry Weißbach        $ORIGDATA2 = $DATA;
7217d101cc1SGerry Weißbach        //        $ORIGDATA2 = $DATA[2]; // 08/10/2010 - this line required a $this->functions->wl which may mess up with the base URL
7227d101cc1SGerry Weißbach        $this->functions->debug->message("OrigDATA is:", $ORIGDATA2, 1);
7237d101cc1SGerry Weißbach
7247d101cc1SGerry Weißbach        // Generate ID
7257d101cc1SGerry Weißbach        $DATA[2] = str_replace('/', ':', $DATA[2]);
7267d101cc1SGerry Weißbach
7277d101cc1SGerry Weißbach        // If Data was empty this must be the same file!;
7287d101cc1SGerry Weißbach        if ( empty( $DATA[2] ) ) {
7297d101cc1SGerry Weißbach            $DATA[2] = $currentID;
7307d101cc1SGerry Weißbach        }
7317d101cc1SGerry Weißbach
7327d101cc1SGerry Weißbach        $ID = $DATA[2];
7337d101cc1SGerry Weißbach        $MEDIAMATCHER = "#(_media(/|:)|media=|_detail(/|:)|_export(/|:)|do=export_)#i"; // 2010-10-23 added "(/|:)" for the ID may not contain slashes anymore
7347d101cc1SGerry Weißbach        $ID = $this->functions->cleanID($DATA[2], null, preg_match($MEDIAMATCHER, $DATA[2]) );
7357d101cc1SGerry Weißbach        //        $ID = $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'media') ); // Export anpassung nun weiter unten
7367d101cc1SGerry Weißbach
7377d101cc1SGerry Weißbach        //        $IDexists = page_exists($ID); // 08/10/2010 - Not needed. This will be done in the next block.
7387d101cc1SGerry Weißbach        //        $this->functions->debug->message("Current ID: '$ID' exists: '" . ($IDexists ? 'true' : 'false') . "' (will be set to 'false' anyway)", null, 1);
7397d101cc1SGerry Weißbach
7407d101cc1SGerry Weißbach        $IDifIDnotExists = $ID; // 08/10/2010 - Save ID - with possible upper cases to preserve them
7417d101cc1SGerry Weißbach        $IDexists = false;
7427d101cc1SGerry Weißbach
7437d101cc1SGerry Weißbach        $this->functions->debug->message("Resolving ID: '$ID'", null, 2);
7447d101cc1SGerry Weißbach        if ( preg_match($MEDIAMATCHER, $DATA[2]) ) {
7457d101cc1SGerry Weißbach            resolve_mediaid(null, $ID, $IDexists);
7467d101cc1SGerry Weißbach
7477d101cc1SGerry Weißbach            $this->functions->debug->message("Current mediaID to filename: '" . mediaFN($ID) . "'", null, 2);
7487d101cc1SGerry Weißbach        } else {
7497d101cc1SGerry Weißbach            resolve_pageid(null, $ID, $IDexists);
7507d101cc1SGerry Weißbach            $this->functions->debug->message("Current ID to filename: '" . wikiFN($ID) . "'", null, 2);
7517d101cc1SGerry Weißbach        }
7527d101cc1SGerry Weißbach
7537d101cc1SGerry Weißbach        $this->functions->debug->message("Current ID after resolvement: '$ID' the ID does exist: '" . ($IDexists ? 'true' : 'false') . "'", null, 2);
7547d101cc1SGerry Weißbach        //        $ORIGDATA2 = @parse_url($this->functions->wl($ORIGDATA2, null, true)); // What was the next 2 line for? It did mess up with links from {{jdoc>}}
7557d101cc1SGerry Weißbach        //        $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
7567d101cc1SGerry Weißbach
7577d101cc1SGerry Weißbach        // 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!
7587d101cc1SGerry Weißbach        if ( !$IDexists ) {
7597d101cc1SGerry Weißbach            $ID = $IDifIDnotExists; // there may have been presevered Upper cases. We will need them!
7607d101cc1SGerry Weißbach        }
7617d101cc1SGerry Weißbach
7627d101cc1SGerry Weißbach        // $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'media') || strstr($DATA[2], 'export') );
7637d101cc1SGerry Weißbach        if ( substr($ID, -1) == ':' || empty($ID) ) $ID .= $conf['start'];
7647d101cc1SGerry Weißbach
7657d101cc1SGerry Weißbach        // Generate Download URL
7667d101cc1SGerry Weißbach        // $PARAMS = trim(str_replace('&amp;', '&', $PARAMS));
7677d101cc1SGerry Weißbach        $PARAMS = trim($PARAMS);
7687d101cc1SGerry Weißbach        $this->functions->removeWikiVariables($PARAMS, false, true);
7697d101cc1SGerry Weißbach
7707d101cc1SGerry Weißbach        $url = $this->functions->wl($ID, null, true, null, null, true, $hadBase) . ( !empty( $ANCHOR) ? '#' . $ANCHOR : '' ) . ( !empty( $PARAMS) ? '?' . $PARAMS : '' );
7717d101cc1SGerry Weißbach        $this->functions->debug->message("URL from ID: '$url'", null, 2);
7727d101cc1SGerry Weißbach
7737d101cc1SGerry Weißbach        // Parse URI PATH and add "html"
7747d101cc1SGerry Weißbach        $uri = @parse_url($url);
7757d101cc1SGerry Weißbach        $DATA[2] = $uri['path'];
7767d101cc1SGerry Weißbach        $DATA['ANCHOR'] = $ANCHOR;
7777d101cc1SGerry Weißbach        $DATA['PARAMS'] = $PARAMS;
7787d101cc1SGerry Weißbach
7797d101cc1SGerry Weißbach        $this->functions->debug->message("DATA after parsing.", $DATA, 2);
7807d101cc1SGerry Weißbach
7817d101cc1SGerry Weißbach        // Second Rewrite for UseRewrite = 2
7827d101cc1SGerry Weißbach        if ( $conf['userewrite'] == 2 ) {
7837d101cc1SGerry Weißbach            $DATA[2] = preg_replace( '$/lib/.*?fetch\.php$', '', $DATA[2]);
7847d101cc1SGerry Weißbach            $DATA[2] = preg_replace( '%(/lib/.*?detail\.php.*$)%', '\1' . '.' . $this->functions->settings->fileType, $DATA[2]);
7857d101cc1SGerry Weißbach
7867d101cc1SGerry Weißbach            if ( preg_match( '%/(lib/.*?detail|doku)\.php%', $DATA[2])) {
7877d101cc1SGerry Weißbach                $noDeepReplace = false;
7887d101cc1SGerry Weißbach                $fileName = $this->functions->getSiteName($ID);
7897d101cc1SGerry Weißbach                $newDepth = str_repeat('../', count(explode('/', $fileName))-1);
7907d101cc1SGerry Weißbach            }
7917d101cc1SGerry Weißbach
7927d101cc1SGerry Weißbach            $this->functions->debug->message("DATA after second rewrite with UseRewrite = 2", array($DATA, $noDeepReplace, $fileName, $newDepth), 1);
7937d101cc1SGerry Weißbach        }
7947d101cc1SGerry Weißbach
7957d101cc1SGerry Weißbach        switch ( array_pop(explode('/', $DATA[2])) ) {
7967d101cc1SGerry Weißbach            // CSS Extra Handling with extra rewrites
7977d101cc1SGerry Weißbach            case 'css.php'	:	// $DATA[2] .=  ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID(preg_replace("/(=|\?|&amp;)/", ".", $PARAMS))) . '.css';
7987d101cc1SGerry Weißbach                $DATA[2] .=  '.' . $this->functions->cleanID(preg_replace("/(=|\?|&amp;)/", ".", $PARAMS)) . '.css'; // allways put parameters behind
7997d101cc1SGerry Weißbach                // No paramters needed since they are rewritten.
8007d101cc1SGerry Weißbach                $DATA['PARAMS'] = "";
8017d101cc1SGerry Weißbach                $noDeepReplace = false;
8027d101cc1SGerry Weißbach                $fileName = $this->functions->getSiteName($ID);
8037d101cc1SGerry Weißbach                $newDepth = str_repeat('../', count(explode('/', $fileName))-1);
8047d101cc1SGerry Weißbach                $newAdditionalParameters['do'] = 'siteexport';
8057d101cc1SGerry Weißbach
8067d101cc1SGerry Weißbach                $this->functions->debug->message("This is CSS file", array($DATA, $noDeepReplace, $fileName, $newDepth, $newAdditionalParameters), 2);
8077d101cc1SGerry Weißbach
8087d101cc1SGerry Weißbach                break;
8097d101cc1SGerry Weißbach            case 'js.php'	:	// $DATA[2] .= ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID(preg_replace("/(=|\?|&amp;)/", ".", $PARAMS))) . '.js';
8107d101cc1SGerry Weißbach                $DATA[2] .=  '.t.' . $this->functions->cleanID($_REQUEST['template']) . '.js'; // allways put parameters behind
8117d101cc1SGerry Weißbach                // set Template
8127d101cc1SGerry Weißbach                if ( !empty( $_REQUEST['template'] ) ) {
8137d101cc1SGerry Weißbach                    $url .= ( strstr($url, '?') ? '&' : '?' ) . 'template=' . $_REQUEST['template'];
8147d101cc1SGerry Weißbach                }
8157d101cc1SGerry Weißbach                // No paramters needed since they are rewritten.
8167d101cc1SGerry Weißbach                $DATA['PARAMS'] = "";
8177d101cc1SGerry Weißbach                $newAdditionalParameters['do'] = 'siteexport';
8187d101cc1SGerry Weißbach
8197d101cc1SGerry Weißbach                $this->functions->debug->message("This is JS file", array($DATA, $url, $fileName, $newAdditionalParameters), 2);
8207d101cc1SGerry Weißbach
8217d101cc1SGerry Weißbach                break;
8227d101cc1SGerry Weißbach                // Detail Handling with extra Rewrites if Paramaters are available - otherwise this is just the fetch
8237d101cc1SGerry Weißbach            case 'indexer.php' :
8247d101cc1SGerry Weißbach                $this->functions->debug->message("Skipping indexer", null, 2);
8257d101cc1SGerry Weißbach                return "";
8267d101cc1SGerry Weißbach                break;
8277d101cc1SGerry Weißbach            case 'detail.php' :
8287d101cc1SGerry Weißbach                $fileName = $this->functions->getSiteName($ID, true); // 2010-09-03 - rewrite with override enabled
8297d101cc1SGerry Weißbach            case 'doku.php' :
8307d101cc1SGerry Weißbach                if ( $this->functions->settings->addParams ) {
8317d101cc1SGerry Weißbach                    $noDeepReplace = false;
8327d101cc1SGerry Weißbach
8337d101cc1SGerry Weißbach                    if ( empty($fileName) ) {
8347d101cc1SGerry Weißbach                        $fileName = $this->functions->getSiteName($ID); // 2010-09-03 - rewrite with override enabled
8357d101cc1SGerry Weißbach                    }
8367d101cc1SGerry Weißbach
8377d101cc1SGerry Weißbach                    $newDepth = str_repeat('../', count(explode('/', $fileName))-1);
8387d101cc1SGerry Weißbach                    $this->__rebuildDataForNormalFiles($DATA, $PARAMS);
8397d101cc1SGerry Weißbach
8407d101cc1SGerry Weißbach                    $this->functions->debug->message("This is doku.php or detail.php file with addParams", array($DATA, $fileName, $newDepth, $newAdditionalParameters), 2);
8417d101cc1SGerry Weißbach                    break;
8427d101cc1SGerry Weißbach                }
8437d101cc1SGerry Weißbach
8447d101cc1SGerry Weißbach                $url = str_replace('detail.php', 'fetch.php', $url);
8457d101cc1SGerry Weißbach                $this->functions->debug->message("This is doku.php or detail.php file '$url'", null, 2);
8467d101cc1SGerry Weißbach                // Fetch Handling for media - rewriting everything
8477d101cc1SGerry Weißbach            case 'fetch.php':
8487d101cc1SGerry Weißbach                $this->__getParamsAndDataRewritten($DATA, $PARAMS, 'media');
8497d101cc1SGerry Weißbach
8507d101cc1SGerry Weißbach                $DATA[2] = str_replace('/', ':', $DATA[2]);
8517d101cc1SGerry Weißbach                $ID = $this->functions->cleanID($DATA[2], null, strstr($DATA[2], 'media'));
8527d101cc1SGerry Weißbach
8537d101cc1SGerry Weißbach                $urlM = ml($ID, null, true);
8547d101cc1SGerry Weißbach                $uriM = @parse_url($urlM);
8557d101cc1SGerry Weißbach                $DATA[2] = $uriM['path'] . ( !empty( $ANCHOR) ? '#' . $ANCHOR : '' ) . ( !empty( $PARAMS) ? '?' . $PARAMS : '' );
8567d101cc1SGerry Weißbach
8577d101cc1SGerry Weißbach                $DATA['PARAMS'] = "";
8587d101cc1SGerry Weißbach                $newAdditionalParameters = array();
8597d101cc1SGerry Weißbach
8600b4abc9fSGerry Weißbach                $this->functions->debug->message("This is fetch.php file", array($DATA, $ID, $PARAMS), 2);
8617d101cc1SGerry Weißbach                break;
8627d101cc1SGerry Weißbach
8637d101cc1SGerry Weißbach                // default Handling for Pages
8647d101cc1SGerry Weißbach            default			:
8657d101cc1SGerry Weißbach                if ( preg_match("%" . DOKU_BASE . "_detail/%", $DATA[2]) ) {
8667d101cc1SGerry Weißbach
8677d101cc1SGerry Weißbach                    // GET ID Param from origdata2
8687d101cc1SGerry Weißbach                    preg_match("#id=(.*?)(&|\")#i", $DATA[0], $backlinkID);
8697d101cc1SGerry Weißbach                    $this->__rebuildDataForNormalFiles($DATA, $PARAMS);
8707d101cc1SGerry Weißbach
8717d101cc1SGerry Weißbach                    $fileIDPart = isset($backlinkID[1]) && !empty($backlinkID[1]) ? $this->functions->cleanID(urldecode($backlinkID[1])) : 'detail';
8727d101cc1SGerry Weißbach
8737d101cc1SGerry Weißbach                    $DATA[2] .= '/' . $fileIDPart . '.' . $this->functions->settings->fileType; // add namespace and subpage for back button and add filetype
8747d101cc1SGerry Weißbach
8757d101cc1SGerry Weißbach                    $noDeepReplace = false;
8767d101cc1SGerry Weißbach                    $fileName = $this->functions->shortenName($DATA[2]);
8777d101cc1SGerry Weißbach                    $newDepth = str_repeat('../', count(explode('/', $fileName))-1);
8787d101cc1SGerry Weißbach                    $url .= ( strstr($url, '?') ? '&' : '?' ) . 'id=' . $fileIDPart; // add id-part to URL for backlinks
8797d101cc1SGerry Weißbach
8807d101cc1SGerry Weißbach                    $DATA['PARAMS'] = "";
8817d101cc1SGerry Weißbach
8827d101cc1SGerry Weißbach                    $this->functions->debug->message("This is something with '_detail' file", array($DATA, $backlinkID, $newDepth, $url), 2);
8837d101cc1SGerry Weißbach                } else if ( preg_match("%" . DOKU_BASE . "_export/(.*?)/%", $DATA[2], $fileType) ) {
8847d101cc1SGerry Weißbach
8857d101cc1SGerry Weißbach                    // Fixes multiple codeblocks in one file
8867d101cc1SGerry Weißbach                    $this->__rebuildDataForNormalFiles($DATA, $PARAMS);
8877d101cc1SGerry Weißbach
8887d101cc1SGerry Weißbach                    // add the Params no matter what they are. This is export. We don't mess with other files
8897d101cc1SGerry Weißbach                    // adding the "/" fixes the usage of multiple codeblocks in the same namespace
8907d101cc1SGerry Weißbach                    $DATA[2] .= (empty( $PARAMS ) ? '' : '/' . $PARAMS) . '.'. $fileType[1];
8917d101cc1SGerry Weißbach
8927d101cc1SGerry Weißbach                    $DATA['PARAMS'] = "";
8937d101cc1SGerry Weißbach                    $this->functions->debug->message("This is something with '_export' file", $DATA, 2);
8947d101cc1SGerry Weißbach
8957d101cc1SGerry Weißbach                } else if ( $IDexists ) { // 08/10/2010 - was page_exists($ID) - but this should do as well.
8967d101cc1SGerry Weißbach                    // If this is a page ... skip it!
8977d101cc1SGerry Weißbach                    $DATA[2] .= ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID(preg_replace("/(=|\?|&amp;)/", ".", $PARAMS)))  . '.' . $this->functions->settings->fileType;
8987d101cc1SGerry Weißbach
8997d101cc1SGerry Weißbach                    // 2012-06-15 originally has an absolute path ... we might need a relative one if not in our namespace
9007d101cc1SGerry Weißbach                    $this->functions->debug->message("OK, this is to be absolute: " . (empty($_REQUEST['absolutePath'])?'false':'true'), null, 1);
9017d101cc1SGerry Weißbach                    if ( empty($_REQUEST['absolutePath']) )
9027d101cc1SGerry Weißbach                    {
9037d101cc1SGerry Weißbach                        $DATA[2] = $this->functions->getRelativeURL($DATA[2], $currentID);
9047d101cc1SGerry Weißbach                    }
9057d101cc1SGerry Weißbach
9067d101cc1SGerry Weißbach                    $DATA[2] = $this->functions->shortenName($DATA[2]);
9077d101cc1SGerry Weißbach
9087d101cc1SGerry Weißbach                    // If Parameters are to be included in the filename - they must not be added twice
9097d101cc1SGerry Weißbach                    if ( $this->functions->settings->addParams ) $DATA['PARAMS'] = "";
9107d101cc1SGerry Weißbach
9117d101cc1SGerry Weißbach                    $this->functions->debug->message("This page really exists", $DATA, 1);
9127d101cc1SGerry Weißbach
9137d101cc1SGerry Weißbach                    return $this->__rebuildLink($DATA);
9147d101cc1SGerry Weißbach                } else {
9157d101cc1SGerry Weißbach                    $this->__rebuildDataForNormalFiles($DATA, $PARAMS);
9167d101cc1SGerry Weißbach                }
9177d101cc1SGerry Weißbach
9187d101cc1SGerry Weißbach                unset($newAdditionalParameters['diPlu']);
9197d101cc1SGerry Weißbach        }
9207d101cc1SGerry Weißbach
9217d101cc1SGerry Weißbach
9227d101cc1SGerry Weißbach        $this->functions->debug->message("DATA after SWITCH CASE decision", array($DATA, $noDeepReplace, $fileName, $newDepth), 1);
9237d101cc1SGerry Weißbach
9247d101cc1SGerry Weißbach        if ( $this->filewriter->canDoPDF() ) {
9257d101cc1SGerry Weißbach            $this->functions->addAdditionalParametersToURL($url, $newAdditionalParameters);
9267d101cc1SGerry Weißbach            $DATA[2] = $url;
9277d101cc1SGerry Weißbach            unset($DATA['PARAMS']);
9287d101cc1SGerry Weißbach            $url = $this->__rebuildLink($DATA, '');
9297d101cc1SGerry Weißbach
9307d101cc1SGerry Weißbach            $this->functions->debug->message("Creating PDF with URL '$url'", null, 2);
9317d101cc1SGerry Weißbach
9327d101cc1SGerry Weißbach            return $url;
9337d101cc1SGerry Weißbach        }
9347d101cc1SGerry Weißbach
9357d101cc1SGerry Weißbach        // Create Name to save the file at
9367d101cc1SGerry Weißbach        $DATA[2] = str_replace(':', '_', $DATA[2]);
9377d101cc1SGerry Weißbach        $DATA[2] = $this->functions->shortenName($DATA[2]);
9387d101cc1SGerry Weißbach
9397d101cc1SGerry Weißbach
9407d101cc1SGerry Weißbach        // File already loaded?
9417d101cc1SGerry Weißbach        // 2010-10-23 - changes in_array from DATA[2] to $url - to check real URLs, the DATA[2] file will be checked with fileExistsInZip
9427d101cc1SGerry Weißbach        if ( in_array($url, array_keys($this->fileChecked)) ) {
9437d101cc1SGerry Weißbach            $DATA[2] = $this->fileChecked[$url];
9447d101cc1SGerry Weißbach            $this->functions->debug->message("File has been checked before.", array($DATA, $url), 2);
9457d101cc1SGerry Weißbach            return $this->__rebuildLink($DATA);
9467d101cc1SGerry Weißbach        }
9477d101cc1SGerry Weißbach
9487d101cc1SGerry Weißbach        // 2010-09-03 - second check if the file is in the ZIP already.
9497d101cc1SGerry Weißbach        if ( $this->filewriter->fileExistsInZip($DATA[2]) ) {
9507d101cc1SGerry Weißbach            $this->functions->debug->message("File with DATA exists in ZIP.", $DATA, 3);
9517d101cc1SGerry Weißbach            return $this->__rebuildLink($DATA);
9527d101cc1SGerry Weißbach        }
9537d101cc1SGerry Weißbach
9547d101cc1SGerry Weißbach        // 2010-10-23 - What if this is a fetch.php? than we produced an error.
9557d101cc1SGerry Weißbach        //        $this->fileChecked[] = $DATA[2];
9567d101cc1SGerry Weißbach
9577d101cc1SGerry Weißbach        // get tempFile and save it
9587d101cc1SGerry Weißbach        $origDepth = $this->functions->settings->depth;
9597d101cc1SGerry Weißbach        $this->functions->settings->depth = $newDepth;
9607d101cc1SGerry Weißbach
9617d101cc1SGerry Weißbach        $tmpID = $currentID;
9627d101cc1SGerry Weißbach        $tmpFile === false;
9637d101cc1SGerry Weißbach
9647d101cc1SGerry Weißbach        $this->functions->debug->message("Going to get the file", array($url, $noDeepReplace, $newAdditionalParameters), 2);
9657d101cc1SGerry Weißbach        $tmpFile = $this->__getHTTPFile($url, $noDeepReplace, $newAdditionalParameters);
9667d101cc1SGerry Weißbach        $this->functions->debug->message("This is the getHTTPFile result", $tmpFile, 2);
9677d101cc1SGerry Weißbach
9687d101cc1SGerry Weißbach        $currentID = $tmpID;
9697d101cc1SGerry Weißbach        $this->functions->settings->depth = $origDepth; // 2010-09-03 - Reset depth at the very end
9707d101cc1SGerry Weißbach
9717d101cc1SGerry Weißbach        if ( $tmpFile === false ) {
9727d101cc1SGerry Weißbach            // Keep an potentially extra link intact
9737d101cc1SGerry Weißbach
9747d101cc1SGerry Weißbach            $this->functions->debug->message("The fetched file '$url' is 'false'", null, 3);
9757d101cc1SGerry Weißbach            if ( $IDexists === false ) {
9767d101cc1SGerry Weißbach                $this->functions->debug->message("The file does not exist, fallback to ORIGDATA", $ORIGDATA2, 2);
9777d101cc1SGerry Weißbach                $DATA[2] = $this->functions->shortenName($ORIGDATA2[2]); // get Origdata Path
9787d101cc1SGerry Weißbach            }
9797d101cc1SGerry Weißbach
9807d101cc1SGerry Weißbach            $this->fileChecked[$url] = $DATA[2]; // 2010-09-03 - One URL to one FileName
9817d101cc1SGerry Weißbach            $link = $this->__rebuildLink($DATA);
9827d101cc1SGerry Weißbach            $this->functions->debug->message("Final Link after empty file from '$url'", null, 2);
9837d101cc1SGerry Weißbach
9847d101cc1SGerry Weißbach            return $link;
9857d101cc1SGerry Weißbach        }
9867d101cc1SGerry Weißbach
9877d101cc1SGerry Weißbach        $this->functions->debug->message("The fetched file looks good.", $tmpFile, 1);
988281ed919SGerry Weißbach        $dirname = dirname($DATA[2]);
9897d101cc1SGerry Weißbach
9907d101cc1SGerry Weißbach        // If a Filename was given that does not comply to the original name, us this one!
99184d65497SGerry Weißbach        // 2014-02-28 But only if we are on PDF Mode. Does this produce any other Problems?
99284d65497SGerry Weißbach        if ( $this->filewriter->canDoPDF() && !empty($tmpFile[1]) && !strstr($DATA[2], $tmpFile[1]) ) {
993281ed919SGerry Weißbach			$DATA[2] = $dirname . '/' . $tmpFile[1];
9947d101cc1SGerry Weißbach        }
9957d101cc1SGerry Weißbach
9967d101cc1SGerry Weißbach        // Add to zip
9977d101cc1SGerry Weißbach        $this->fileChecked[$url] = $DATA[2]; // 2010-09-03 - One URL to one FileName
9987d101cc1SGerry Weißbach
9997d101cc1SGerry Weißbach        $status = $this->filewriter->__addFileToZip($tmpFile[0], $DATA[2]);
10007d101cc1SGerry Weißbach        @unlink($tmpFile[0]);
10017d101cc1SGerry Weißbach
10027d101cc1SGerry Weißbach        $newURL = $this->__rebuildLink($DATA);
10037d101cc1SGerry Weißbach        $this->functions->debug->message("Returning final Link to document: '$newURL'", null, 2);
10047d101cc1SGerry Weißbach
10057d101cc1SGerry Weißbach        return $newURL;
10067d101cc1SGerry Weißbach    }
10077d101cc1SGerry Weißbach
10087d101cc1SGerry Weißbach    /**
10097d101cc1SGerry Weißbach     * build the new link to be put in place for the donwloaded site
10107d101cc1SGerry Weißbach     **/
10117d101cc1SGerry Weißbach    function __rebuildLink($DATA, $DEPTH = null) {
10127d101cc1SGerry Weißbach
10137d101cc1SGerry Weißbach        // depth is set, skip this one
10147d101cc1SGerry Weißbach        if ( is_null( $DEPTH ) ) $DEPTH = $this->functions->settings->depth;
101584d65497SGerry Weißbach        $DATA[2] .= ( !empty( $DATA['PARAMS']) && $this->functions->settings->addParams? '?' . $DATA['PARAMS'] : '' ) . ( !empty( $DATA['ANCHOR'] ) ? '#' . $DATA['ANCHOR'] : '' );
10167d101cc1SGerry Weißbach
10177d101cc1SGerry Weißbach        $newURL = $DATA[1] == 'url' ? $DATA[1] . '(' . $DEPTH . $DATA[2] . ')' : $DATA[1] . '="' . $DEPTH . $DATA[2] . '"';
10187d101cc1SGerry Weißbach        $this->functions->debug->message("Re-created URL: '$newURL'", null, 2);
10197d101cc1SGerry Weißbach
10207d101cc1SGerry Weißbach        return $newURL;
10217d101cc1SGerry Weißbach    }
10227d101cc1SGerry Weißbach
10237d101cc1SGerry Weißbach
10247d101cc1SGerry Weißbach    /**
10257d101cc1SGerry Weißbach     * remove an old zip file
10267d101cc1SGerry Weißbach     **/
10277d101cc1SGerry Weißbach    function __removeOldZip( $FILENAMEID=null, $checkForMore=true ) {
10287d101cc1SGerry Weißbach        global $INFO;
10297d101cc1SGerry Weißbach        global $conf;
10307d101cc1SGerry Weißbach
10317d101cc1SGerry Weißbach        $returnValue = true;
10327d101cc1SGerry Weißbach
10337d101cc1SGerry Weißbach        if ( empty($FILENAMEID) ) {
10347d101cc1SGerry Weißbach            $FILENAMEID = $this->functions->settings->origZipFile;
10357d101cc1SGerry Weißbach        }
10367d101cc1SGerry Weißbach
10378da901a0SGerry Weißbach        if ( !file_exists(mediaFN($FILENAMEID)) ) {
10388da901a0SGerry Weißbach            $returnValue = true;
10398da901a0SGerry Weißbach        } else {
10408da901a0SGerry Weißbach
10417d101cc1SGerry Weißbach            require_once( DOKU_INC . 'inc/media.php');
10427d101cc1SGerry Weißbach            if ( !media_delete($FILENAMEID, $INFO['perm']) ) {
10437d101cc1SGerry Weißbach                $returnValue = false;
10447d101cc1SGerry Weißbach            }
10457d101cc1SGerry Weißbach        }
10467d101cc1SGerry Weißbach
10477d101cc1SGerry Weißbach        if ( $checkForMore ) {
10487d101cc1SGerry Weißbach            // Try to remove more files.
10497d101cc1SGerry Weißbach            $ns = getNS($FILENAMEID);
10507d101cc1SGerry Weißbach            $fn = $this->functions->getSpecialExportFileName(noNS($FILENAMEID), '.+');
10517d101cc1SGerry Weißbach
10527d101cc1SGerry Weißbach            $data = array();
10537d101cc1SGerry Weißbach            search($data, $conf['mediadir'], 'search_media', array('pattern' => "/$fn$/i"), $ns);
10547d101cc1SGerry Weißbach
10557d101cc1SGerry Weißbach            if ( count($data > 0) ) {
10567d101cc1SGerry Weißbach
10577d101cc1SGerry Weißbach                // 30 Minuten Cache Zeit
1058f8fd18e7SGerry Weißbach                $cache = $this->functions->settings->cachetime;
10597d101cc1SGerry Weißbach                foreach ( $data as $media ) {
10607d101cc1SGerry Weißbach
10617d101cc1SGerry Weißbach                    //decide if has to be deleted needed:
10627d101cc1SGerry Weißbach                    if( $media['mtime'] < time()-$cache) {
10637d101cc1SGerry Weißbach                        $this->__removeOldZip($media['id'], false);
10647d101cc1SGerry Weißbach                    }
10657d101cc1SGerry Weißbach                }
10667d101cc1SGerry Weißbach            }
10677d101cc1SGerry Weißbach
10687d101cc1SGerry Weißbach        }
10697d101cc1SGerry Weißbach
10707d101cc1SGerry Weißbach        return $returnValue;
10717d101cc1SGerry Weißbach    }
10727d101cc1SGerry Weißbach
10737d101cc1SGerry Weißbach    /**
10747d101cc1SGerry Weißbach     * if confrewrite is set to internal rewrite, use this function - taken from a DW renderer
10757d101cc1SGerry Weißbach     **/
10767d101cc1SGerry Weißbach    function __getInternalRewriteURL($url) {
10777d101cc1SGerry Weißbach        global $conf;
10787d101cc1SGerry Weißbach
10797d101cc1SGerry Weißbach        //construct page id from request URI
10807d101cc1SGerry Weißbach        if( $conf['userewrite'] != 2) { return $url; }
10817d101cc1SGerry Weißbach
10827d101cc1SGerry Weißbach        //get the script URL
10837d101cc1SGerry Weißbach        if($conf['basedir']) {
10847d101cc1SGerry Weißbach            $relpath = '';
10857d101cc1SGerry Weißbach            $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']);
10867d101cc1SGerry Weißbach        } elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
10877d101cc1SGerry Weißbach            $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
10887d101cc1SGerry Weißbach            $_SERVER['SCRIPT_FILENAME']);
10897d101cc1SGerry Weißbach            $script = '/'.$script;
10907d101cc1SGerry Weißbach        }else{
10917d101cc1SGerry Weißbach            $script = $_SERVER['SCRIPT_NAME'];
10927d101cc1SGerry Weißbach        }
10937d101cc1SGerry Weißbach
10947d101cc1SGerry Weißbach        //clean script and request (fixes a windows problem)
10957d101cc1SGerry Weißbach        $script  = preg_replace('/\/\/+/','/',$script);
10967d101cc1SGerry Weißbach        $request = preg_replace('/\/\/+/','/',$url);
10977d101cc1SGerry Weißbach
10987d101cc1SGerry Weißbach        //remove script URL and Querystring to gain the id
10997d101cc1SGerry Weißbach        if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
11007d101cc1SGerry Weißbach            $id = preg_replace ('/\?.*/','',$match[1]);
11017d101cc1SGerry Weißbach        }
11027d101cc1SGerry Weißbach        $id = urldecode($id);
11037d101cc1SGerry Weißbach        //strip leading slashes
11047d101cc1SGerry Weißbach        $id = preg_replace('!^/+!','',$id);
11057d101cc1SGerry Weißbach
11067d101cc1SGerry Weißbach        return $id;
11077d101cc1SGerry Weißbach    }
11087d101cc1SGerry Weißbach
11097d101cc1SGerry Weißbach    /**
11107d101cc1SGerry Weißbach     * rewrite parameter calls
11117d101cc1SGerry Weißbach     **/
11127d101cc1SGerry Weißbach    function __getParamsAndDataRewritten(&$DATA, &$PARAMS, $IDKEY='id') {
11137d101cc1SGerry Weißbach
11147d101cc1SGerry Weißbach        $PARRAY = explode('&', str_replace('&amp;', '&', $PARAMS) );
11157d101cc1SGerry Weißbach        $PARAMS = "";
11167d101cc1SGerry Weißbach
11177d101cc1SGerry Weißbach        foreach ( $PARRAY as $item ) {
11187d101cc1SGerry Weißbach            list($key, $value) = explode('=', $item, 2);
11197d101cc1SGerry Weißbach            if ( empty($key) || empty($value) )
11207d101cc1SGerry Weißbach            continue;
11217d101cc1SGerry Weißbach
11227d101cc1SGerry Weißbach            if ( strtolower(trim($key)) == $IDKEY ) {
11237d101cc1SGerry Weißbach                $DATA[2] = preg_replace("%^" . DOKU_BASE . "%", "", $value);
11247d101cc1SGerry Weißbach                continue;
11257d101cc1SGerry Weißbach            }
11267d101cc1SGerry Weißbach
11277d101cc1SGerry Weißbach            if ( !empty( $PARAMS) ) {
11280b4abc9fSGerry Weißbach                $PARAMS .= '&';
11297d101cc1SGerry Weißbach            }
11307d101cc1SGerry Weißbach
11317d101cc1SGerry Weißbach            $PARAMS .= "$key=$value";
11327d101cc1SGerry Weißbach        }
11337d101cc1SGerry Weißbach    }
11347d101cc1SGerry Weißbach
11357d101cc1SGerry Weißbach    /**
11367d101cc1SGerry Weißbach     * rewrite detail.php calls
11377d101cc1SGerry Weißbach     **/
11387d101cc1SGerry Weißbach    function __rebuildDataForNormalFiles(&$DATA, &$PARAMS) {
11397d101cc1SGerry Weißbach        $PARTS = explode('.', $DATA[2]);
11407d101cc1SGerry Weißbach        if ( count($PARTS) > 1 ) {
11417d101cc1SGerry Weißbach            $EXT = '.' . array_pop($PARTS);
11427d101cc1SGerry Weißbach        }
11437d101cc1SGerry Weißbach
11447d101cc1SGerry Weißbach        $PARAMS = preg_replace("/(=|\?|&amp;)/", ".", $PARAMS);
11457d101cc1SGerry Weißbach        $DATA[2] = implode('.', $PARTS) . ( !$this->functions->settings->addParams || empty($PARAMS) ? '' : '.' . $this->functions->cleanID($PARAMS)) . ( $EXT == '.php' ? '.' . $this->functions->settings->fileType : $EXT );
11467d101cc1SGerry Weißbach        $DATA[2] = preg_replace("/\.+/", ".", $DATA[2]);
11477d101cc1SGerry Weißbach    }
11487d101cc1SGerry Weißbach
11497d101cc1SGerry Weißbach
11507d101cc1SGerry Weißbach
11517d101cc1SGerry Weißbach
11527d101cc1SGerry Weißbach    /*
11537d101cc1SGerry Weißbach     * Clean JS and CSS cache files
11547d101cc1SGerry Weißbach     */
11557d101cc1SGerry Weißbach    function cleanCacheFiles() {
11567d101cc1SGerry Weißbach
11577d101cc1SGerry Weißbach        $_SERVER['HTTP_HOST'] = preg_replace("/:?\d+$/", '', $_SERVER['HTTP_HOST']);
11587d101cc1SGerry Weißbach        $cache = getCacheName('scripts'.$_SERVER['HTTP_HOST'].'-siteexport-js-'.$_SERVER['SERVER_PORT'],'.js');
11597d101cc1SGerry Weißbach        $this->unlinkIfExists($cache);
11607d101cc1SGerry Weißbach
11617d101cc1SGerry Weißbach        $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['template']));
11627d101cc1SGerry Weißbach        if($tpl)
11637d101cc1SGerry Weißbach        {
11647d101cc1SGerry Weißbach            $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/';
11657d101cc1SGerry Weißbach            $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/';
11667d101cc1SGerry Weißbach        } else {
11677d101cc1SGerry Weißbach            $tplinc = DOKU_TPLINC;
11687d101cc1SGerry Weißbach            $tpldir = DOKU_TPL;
11697d101cc1SGerry Weißbach        }
11707d101cc1SGerry Weißbach
11717d101cc1SGerry Weißbach        // The generated script depends on some dynamic options
11727d101cc1SGerry Weißbach        $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].'-siteexport-js-'.$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$style,'.css');
11737d101cc1SGerry Weißbach        $this->unlinkIfExists($cache);
11747d101cc1SGerry Weißbach    }
11757d101cc1SGerry Weißbach
11767d101cc1SGerry Weißbach    function unlinkIfExists($cache) {
11777d101cc1SGerry Weißbach        if ( file_exists($cache) ) {
11787d101cc1SGerry Weißbach            @unlink($cache);
11797d101cc1SGerry Weißbach            if(function_exists('gzopen')) @unlink("$cache.gz");
11807d101cc1SGerry Weißbach        }
11817d101cc1SGerry Weißbach    }
11827d101cc1SGerry Weißbach
11837d101cc1SGerry Weißbach    // Private unset function
11847d101cc1SGerry Weißbach    private function clear(&$variable)
11857d101cc1SGerry Weißbach    {
11867d101cc1SGerry Weißbach        if ( isset($variable) )
11877d101cc1SGerry Weißbach        {
11887d101cc1SGerry Weißbach            unset($variable);
11897d101cc1SGerry Weißbach        }
11907d101cc1SGerry Weißbach    }
11917d101cc1SGerry Weißbach}