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