xref: /plugin/siteexport/inc/functions.php (revision 6792d0cf58db367e1e6f5b779f1b1efd0e27751f)
1<?php
2
3if(!defined('DOKU_PLUGIN')) die('meh');
4require_once(DOKU_PLUGIN.'siteexport/inc/settings.php');
5require_once(DOKU_PLUGIN.'siteexport/inc/debug.php');
6
7class siteexport_functions extends DokuWiki_Plugin
8{
9    public $debug = null;
10    public $settings = null;
11
12    public function siteexport_functions($init=true, $isAJAX=false)
13    {
14        if ( $init )
15        {
16            $this->debug = new siteexport_debug();
17            $this->debug->isAJAX = $isAJAX;
18
19            $this->settings = new settings_plugin_siteexport_settings($this);
20            $this->debug->message("Settings completed: zipFile", $this->settings->zipFile, 1);
21        }
22    }
23
24    public function getPluginName()
25    {
26        return 'siteexport';
27    }
28
29    public function downloadURL()
30    {
31        $params = array('cache' => 'nocache', 'siteexport' => $this->settings->pattern);
32
33        if ( $this->debug->debugLevel() < 5 ) {
34            // If debug, then debug!
35            $params['debug'] = $this->debug->debugLevel();
36        }
37
38        return ml($this->settings->origZipFile, $params, true, '&');
39    }
40
41    public function checkIfCacheFileExistsForFileWithPattern($file, $pattern)
42    {
43        if ( !@file_exists($file) )
44        {
45            // If the cache File does not exist, move the newly created one over ...
46            $this->debug->message("'{$file}' does not exist. Checking original ZipFile", null, 3 );
47            $newCacheFile = mediaFN($this->getSpecialExportFileName($this->settings->origZipFile, $pattern));
48
49            if ( !@file_exists($newCacheFile) )
50            {
51                $this->debug->message("The export must have gone wrong. The cached file does not exist.", array("pattern" => $pattern, "original File" => $this->settings->origZipFile, "expected cached file" => $newCacheFile), 3);
52            }
53
54            $status = io_rename($newCacheFile, $file);
55            $this->debug->message("had to move another original file over. Did it work? " . ($status ? 'Yes, it did.' : 'No, it did not.'), null, 2 );
56        } else {
57            $this->debug->message("The file does exist!", $file, 2 );
58        }
59    }
60
61
62    /**
63     * Returns an utf8 encoded Namespace for a Page and input Namespace
64     * @param $NS
65     * @param $PAGE
66     */
67    function getNamespaceFromID($NS, &$PAGE) {
68        global $conf;
69        // Check current page - if its an NS add the startpage
70        $clean = true;
71        resolve_pageid(getNS($NS), $NS, $clean);
72        if ( ! page_exists($NS) && array_pop(explode(':', $NS)) != strtolower($conf['start'] )) { // Compare to lowercase since clean lowers it.
73            $NS .= ':' . $conf['start'];
74            resolve_pageid(getNS($NS), $NS, $clean);
75        }
76
77        $PAGE = noNS($NS);
78        $NS = getNS($NS);
79
80        return utf8_encodeFN(str_replace(':', '/', $NS));
81    }
82
83    /**
84     * create a file name for the page
85     **/
86    public function getSiteName($ID, $overrideRewrite=false) {
87        global $conf;
88
89        if ( empty($ID) ) return false;
90
91        $url = $this->wl($this->cleanID($ID), null, true, null, null, $overrideRewrite); // this must be done with rewriting set to override
92        //$url = $this->wl($this->cleanID($ID), null, true); // this must be done with rewriting set to override
93        $uri = @parse_url($url);
94        if ( $uri['path'][0] == '/' ) {
95            $uri['path'] = substr($uri['path'], 1);
96        }
97
98        return $this->shortenName($uri['path'] . '.' . $this->settings->fileType);
99    }
100
101    /**
102     * get the Title for the page
103     **/
104    public function getSiteTitle($ID) {
105        if (useHeading('content') && $ID) {
106            $heading = p_get_first_heading($ID,true);
107            if ($heading) {
108                return $this->xmlEntities($heading);
109            }
110        }
111        return ucwords($this->xmlEntities(array_pop(explode(':', $ID))));
112    }
113
114    /**
115     * Encoding ()taken from DW - but without needing the renderer
116     **/
117    public function xmlEntities($string) {
118        return htmlspecialchars($string,ENT_QUOTES,'UTF-8');
119    }
120
121    /**
122     * Create name for the file inside the zip and the replacements
123     **/
124    function shortenName($NAME)
125    {
126        $NS = $this->settings->exportNamespace;
127        $NAME = preg_replace("%^" . DOKU_BASE . "%", "", $NAME);
128        $NAME = preg_replace("%^(_media/)?(" . $NS . "/)?%", "", $NAME);
129
130        $this->debug->message("Shortening file to '$NAME'", null, 1);
131        return $NAME;
132    }
133
134    /**
135     * Remove unwanted chars from ID
136     *
137     * Cleans a given ID to only use allowed characters. Accented characters are
138     * converted to unaccented ones
139     *
140     * @author Andreas Gohr <andi@splitbrain.org>
141     * @param  string  $raw_id    The pageid to clean
142     * @param  boolean $ascii     Force ASCII
143     * @param  boolean $media     Allow leading or trailing _ for media files
144     */
145    function cleanID($raw_id,$ascii=false,$media=false){
146        global $conf;
147        global $lang;
148        static $sepcharpat = null;
149
150        global $cache_cleanid;
151        $cache = & $cache_cleanid;
152
153        // check if it's already in the memory cache
154        if (isset($cache[(string)$raw_id])) {
155            return $cache[(string)$raw_id];
156        }
157
158        $sepchar = $conf['sepchar'];
159        if($sepcharpat == null) // build string only once to save clock cycles
160        $sepcharpat = '#\\'.$sepchar.'+#';
161
162        $id = trim((string)$raw_id);
163        //        $id = utf8_strtolower($id); // NO LowerCase for us!
164
165        //alternative namespace seperator
166        $id = strtr($id,';',':');
167        if($conf['useslash']){
168            $id = strtr($id,'/',':');
169        }else{
170            $id = strtr($id,'/',$sepchar);
171        }
172
173        if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
174        if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
175
176        //remove specials
177        $id = utf8_stripspecials($id,$sepchar,'\*');
178
179        if($ascii) $id = utf8_strip($id);
180
181        //clean up
182        $id = preg_replace($sepcharpat,$sepchar,$id);
183        $id = preg_replace('#:+#',':',$id);
184        $id = ($media ? trim($id,':.-') : trim($id,':._-'));
185        $id = preg_replace('#:[:\._\-]+#',':',$id);
186
187        $cache[(string)$raw_id] = $id;
188        return($id);
189    }
190
191
192    /**
193     * This builds a link to a wikipage - changed for internal use here
194     *
195     * It handles URL rewriting and adds additional parameter if
196     * given in $more
197     *
198     * @author Andreas Gohr <andi@splitbrain.org>
199     */
200
201    function wl($id='',$more='',$abs=false,$sep='&amp;', $IDexists=true, $overrideRewrite=false, $hadBase=false){
202        global $conf;
203
204        $this->debug->message("Starting to build WL-URL for '$id'", $more, 1);
205
206        if(is_array($more)){
207
208        	$intermediateMore = '';
209        	foreach( $more as $key => $value) {
210
211        		if ( strlen($intermediateMore) > 0 ) {
212	        		$intermediateMore .= $sep;
213        		}
214
215	        	if ( !is_array($value) ) {
216		        	$intermediateMore .= rawurlencode($key) . '=';
217		        	$intermediateMore .= rawurlencode($value);
218		        	continue;
219	        	}
220
221	        	foreach( $value as $val ) {
222	        		if ( strlen($intermediateMore) > 0 ) {
223		        		$intermediateMore .= $sep;
224	        		}
225
226		        	$intermediateMore .= rawurlencode($key) . '[]=';
227		        	$intermediateMore .= rawurlencode($val);
228	        	}
229        	}
230
231            $more = $intermediateMore;
232        }else{
233            $more = str_replace(',',$sep,$more);
234        }
235
236        $id = idfilter($id);
237
238        if($abs){
239            $xlink = DOKU_URL;
240            if ( !$IDexists && !$hadBase ) { // If the file does not exist, we have to remove the base. This link my be one to an parallel BASE.
241                $xlink = preg_replace('#' . DOKU_BASE . '$#', '', $xlink);
242            }
243        }else if ($IDexists || $hadBase) { // if the ID does exist, we may add the base.
244            $xlink = DOKU_BASE;
245        } else{
246            $xlink = "";
247        }
248
249        // $this->debug->message("internal WL function Before Replacing: '$xlink'", array(DOKU_REL, DOKU_URL, DOKU_BASE, $xlink), 2);
250        $xlink = preg_replace('#(?<!http:|https:)//+#','/', ($abs ? '' : '/') . "$xlink/"); // ensure slashes at beginning and ending, but strip doubles
251        $this->debug->message("'$xlink'", array(DOKU_REL, DOKU_URL, DOKU_BASE, $xlink), 2);
252
253        if ( $overrideRewrite ) {
254            $this->debug->message("Override enabled.", null, 1);
255            $id = strtr($id,':','/');
256
257            $xlink .= $id;
258            if($more) $xlink .= '?'.$more;
259        } else {
260            if($conf['userewrite'] == 2 ){
261                $xlink .= DOKU_SCRIPT.'/'.$id;
262                if($more) $xlink .= '?'.$more;
263            }elseif($conf['userewrite'] ){
264                $xlink .= $id;
265                if($more) $xlink .= '?'.$more;
266            }elseif($id){
267                $xlink .= DOKU_SCRIPT.'?id='.$id;
268                if($more) $xlink .= $sep.$more;
269            }else{
270                $xlink .= DOKU_SCRIPT;
271                if($more) $xlink .= '?'.$more;
272            }
273        }
274
275        $this->debug->message("internal WL function result: '$xlink'", null, 2);
276
277        return $xlink;
278    }
279
280    /**
281     * Create the export file name - this is the file where everything is being stored
282     * @param $FILE
283     * @param $PATTERN - additional pattern for re-using old files
284     */
285    public function getSpecialExportFileName($FILE, $PATTERN=null) {
286
287        if ( empty($FILE) )
288        {
289            $FILE = $this->settings->origZipFile;
290        }
291
292        if ( empty($PATTERN) && empty($this->settings->pattern) ){
293            $this->debug->message("Generating an internal md5 pattern. This will go wrong - and won't cache properly.", null, 3);
294            $PATTERN = md5(microtime(false));
295        }
296
297        // Set Pattern Global for other stuff
298        if ( empty($this->settings->pattern) ) {
299            $this->settings->pattern = $PATTERN;
300        } else {
301            $PATTERN = $this->settings->pattern;
302        }
303
304        $FA = explode('.', $FILE);
305        $EXT = array_pop($FA);
306        array_push($FA, 'auto');
307        array_push($FA, $PATTERN);
308        array_push($FA, $EXT);
309
310        $fileName = implode('.', $FA);
311        $this->debug->message("Export Filename for '$FILE' will be: '$fileName'", null, 2);
312        return $fileName;
313    }
314
315    public function getCacheFileNameForPattern($PATTERN = null)
316    {
317        if ( $PATTERN == null ) {
318            $PATTERN = $this->settings->pattern;
319        }
320
321        return getCacheName($this->getSpecialExportFileName($this->settings->origZipFile, $PATTERN), '.' . basename(mediaFN($this->settings->origZipFile)) );
322    }
323
324    function startRedirctProcess($counter) {
325        global $ID;
326
327        $URL = wl($ID);
328
329        $additionalParameters = $_REQUEST;
330        $additionalParameters['startcounter'] = $counter;
331        $additionalParameters['pattern'] = $this->settings->pattern;
332
333        unset($additionalParameters['id']);
334        unset($additionalParameters['u']);
335        unset($additionalParameters['p']);
336        unset($additionalParameters['r']);
337        unset($additionalParameters['http_credentials']);
338
339        $this->addAdditionalParametersToURL($URL, $additionalParameters);
340        $this->debug->message("Redirecting to '$URL'", null, 2);
341
342        send_redirect($URL);
343        exit(0); // Should not be reached, but anyways
344    }
345
346    /**
347     * Builds additional Parameters into the URL given
348     * @param $URL
349     * @param $newAdditionalParameters
350     */
351    function addAdditionalParametersToURL(&$URL, $newAdditionalParameters) {
352
353        // Add additionalParameters
354        if ( !empty($newAdditionalParameters) ) {
355            foreach($newAdditionalParameters as $key => $value ) {
356                if ( empty($key) || empty($value) ) { continue; }
357
358                $append = '';
359
360                if ( is_array($value) ) {
361                    foreach( array_values($value) as $aValue ) { // Array Handling
362                        $URL .= (strstr($URL, '?') ? '&' : '?') . $key . "[]=$aValue";
363                    }
364                } else {
365                    $append = "$key=$value";
366                    $URL .= empty($append) || strstr($URL, $append)  ? '' : (strstr($URL, '?') ? '&' : '?') . $append;
367                }
368
369            }
370        }
371    }
372
373    /**
374     * Cleans the wiki variables and returns a rebuild URL that has the new variables at hand
375     * @param $data
376     */
377    function prepare_POSTData($data)
378    {
379        $NS = !empty($data['ns']) ? $data['ns'] : $data['id'];
380
381        $this->removeWikiVariables($data);
382        $data['do'] = 'siteexport';
383        $additionalKeys = '';
384
385        ksort($data);
386
387        $this->debug->message("Prepared POST data:", $data, 1);
388
389        foreach( $data as $key => $value ) {
390
391            if ( !is_array($value) ) { continue; }
392            $this->debug->message("Found inner Array:", $value, 1);
393
394            asort($value);
395            foreach ( $value as $innerKey => $aValue )
396            {
397                if ( is_numeric($innerKey))
398                {
399                    $innerKey = '';
400                }
401
402                $additionalKeys .= "&$key" . "[$innerKey]=$aValue";
403            }
404
405            unset($data[$key]);
406        }
407
408        return wl($NS, $data, true, '&') . $additionalKeys;
409    }
410
411    /**
412     * Parses a String into a $_REQUEST Like variant. You have to tell if a decode of the values is needed
413     * @param $inputArray
414     * @param $decode
415     */
416    public function parseStringToRequestArray($inputArray, $decode=false)
417    {
418        global $plugin_controller;
419
420        $outputArray = $inputArray;
421        if ( !is_array($inputArray) )
422        {
423            $intermediate = str_replace("&amp;", "&", $inputArray);
424
425            $outputArray = array();
426            foreach( explode("&", $intermediate) as $param ) {
427                list($key, $value) = explode("=", $param, 2);
428
429                // This is needed if we do want to calculate $_REQUEST for a non HTTP-Request
430                if ( $decode)
431                {
432                    $value = urldecode($value);
433                }
434
435                if ( empty($key) ) { continue; } // Don't check on Value, because there may be only the key that should be preserved
436
437                if ( substr($key, -2) == '[]' ) {
438	                $key = substr($key, 0, -2);
439	                if ( !is_array($outputArray[$key]) ) {
440		                $outputArray[$key] = array();
441	                }
442
443                    array_push($outputArray[$key], $value); // Array Handling
444                } else {
445                    $outputArray[$key] = $value;
446                }
447            }
448        }
449
450        if ( !empty($outputArray['diPlu']) ) {
451
452            $allPlugins = array();
453            foreach($plugin_controller->getList(null,true) as $plugin ) {
454                // check for CSS or JS
455                if ( !file_exists(DOKU_PLUGIN."$plugin/script.js") && !file_exists(DOKU_PLUGIN."$p/style.css") ) { continue; }
456                $allPlugins[] = $plugin;
457            }
458
459            if ( count($outputArray['diPlu']) > (count($allPlugins) / 2) ) {
460                $outputArray['diInv'] = 1;
461                $outputArray['diPlu'] = array_diff($allPlugins, $outputArray['diPlu']);
462            }
463        }
464
465        return $outputArray;
466    }
467
468    /**
469     * Remove certain fields from the list.
470     * @param $removeArray
471     * @param $advanced
472     * @param $isString
473     */
474    function removeWikiVariables(&$removeArray, $advanced=false, $isString=false) {
475
476        $removeArray = $this->parseStringToRequestArray($removeArray);
477
478        // 2010-08-23 - If there is still the media set, retain the id for e.g. detail.php
479        if ( !isset($removeArray['media']) ) {
480            unset($removeArray['id']);
481        }
482
483        unset($removeArray['do']);
484        unset($removeArray['ns']);
485        unset($removeArray['call']);
486        unset($removeArray['sectok']);
487        unset($removeArray['rndval']);
488        unset($removeArray['tseed']);
489        unset($removeArray['http_credentials']);
490        unset($removeArray['u']);
491        unset($removeArray['p']);
492        unset($removeArray['r']);
493        unset($removeArray['base']);
494        unset($removeArray['siteexport']);
495        unset($removeArray['DokuWiki']);
496        unset($removeArray['cronOverwriteExisting']);
497
498        if ( $removeArray['renderer'] == 'xhtml' ) {
499            $removeArray['do'] = 'export_' . $removeArray['renderer'];
500            unset($removeArray['renderer']);
501        }
502
503        // Keep custom options
504        if ( is_array($removeArray['customoptionname']) && is_array($removeArray['customoptionvalue']) && count($removeArray['customoptionname']) == count($removeArray['customoptionvalue']) )
505        {
506            for( $index=0; $index<count($removeArray['customoptionname']); $index++)
507            {
508                $removeArray[$removeArray['customoptionname'][$index]] = $removeArray['customoptionvalue'][$index];
509            }
510	        unset($removeArray['customoptionname']);
511	        unset($removeArray['customoptionvalue']);
512        }
513
514        if ( $advanced ) {
515            if ( $removeArray['renderer'] != 'xhtml' && !empty($removeArray['renderer']) ) {
516                $removeArray['do'] = 'export_' . $removeArray['renderer'];
517            }
518
519            // 2010-08-25 - Need fakeMedia for some _detail cases with rewrite = 2
520            if ( isset($removeArray['fakeMedia'])  ) {
521                unset($removeArray['media']);
522                unset($removeArray['fakeMedia']);
523            }
524
525            /* remove internal params */
526            unset($removeArray['ens']);
527            unset($removeArray['renderer']);
528            unset($removeArray['site']);
529            unset($removeArray['namespace']);
530            unset($removeArray['exportbody']);
531            unset($removeArray['addParams']);
532            unset($removeArray['template']);
533            unset($removeArray['eclipseDocZip']);
534            unset($removeArray['useTocFile']);
535            unset($removeArray['JavaHelpDocZip']);
536            unset($removeArray['depth']);
537            unset($removeArray['depthType']);
538            unset($removeArray['startcounter']);
539            unset($removeArray['pattern']);
540            unset($removeArray['TOCMapWithoutTranslation']);
541			// unset($removeArray['disableCache']);
542			unset($removeArray['debug']);
543        }
544
545        if ( $isString && is_array($removeArray) ) {
546            $intermediate = $removeArray;
547            $removeArray = array();
548
549            foreach ( $intermediate as $key => $value ) {
550                if ( is_array($value) ) {
551                    foreach( array_values($value) as $aValue ) { // Array Handling
552                        $removeArray[] = $key . "[]=$aValue";
553                    }
554                } else {
555                    $value = trim($value);
556
557                    $removeArray[] = "$key" . ( ((empty($value) && intval($value) !== 0)) || $value == '' ? '' : "=$value" ); // If the Value is empty, the Key must be preserved
558                }
559            }
560
561            //$removeArray = implode( ($this->settings->fileType == 'pdf' ? "&" : "&amp;"), $removeArray);
562            $removeArray = implode( "&", $removeArray); // The &amp; made problems with the HTTPClient / Apache. It should not be a problem to have &
563        }
564    }
565
566    /**
567     * returns a hashed name for the parameters
568     * @param $parameters
569     */
570    public function cronJobNameForParameters($parameters)
571    {
572        return md5($parameters);
573    }
574
575    /**
576     * Takes an URL and transforms it into the path+query part
577     * Used several times, e.g. for genering the hash for the cache file
578     * @param $url
579     */
580    public function urlToPathAndParams($url)
581    {
582        $query = parse_url($url, PHP_URL_QUERY);
583        $path = preg_replace(":^".DOKU_REL.":", "", parse_url($url, PHP_URL_PATH));
584        return "{$path}?{$query}";
585    }
586
587    /**
588     * Transforms an $_REQUEST into a Hash that can be used for cron and cache file
589     * @param $request
590     */
591    public function requestParametersToCacheHash($request)
592    {
593        $params = $this->urlToPathAndParams($this->prepare_POSTData($request));
594        $this->debug->message("Calculated the following Cache Hash URL: ", $params, 2);
595        return $this->cronJobNameForParameters($params);
596    }
597
598    /**
599     * Check a replaceID against a baseID - and make the replaceID relative against it
600     * @param $replaceID - ID which will be made relative if needed
601     * @param $baseID - ID which is the reference to be made relative against
602     */
603    public function getRelativeURL($replaceID, $baseID)
604    {
605        $origReplaceID = $replaceID;
606
607        $replaceTmp = cleanID($replaceID);
608        $file = noNS($replaceTmp);
609
610        $replaceID = getNS($replaceTmp);
611        $baseID = getNS($baseID);
612
613        $replaceParts = explode(':', $replaceID);
614        $baseParts = explode(':', $baseID);
615        $exportNSParts = explode(':', cleanID($this->settings->exportNamespace));
616
617        $newBase = array();
618
619        foreach($exportNSParts as $exportNS)
620        {
621            if ( $replaceParts[0] == $exportNS && $baseParts[0] == $exportNS )
622            {
623                array_shift($replaceParts);
624                array_shift($baseParts);
625                array_shift($exportNSParts);
626            }
627            else {
628                // Nothing is matching anymore
629                break;
630            }
631        }
632
633        $i = count($exportNSParts);
634        $this->debug->message("Checking", array('current extra removing amount'=>$i,'replace'=>$replaceParts,'base'=>$baseParts,'exportNSParts'=>$exportNSParts),1);
635
636        // Now if there is just one item in the ens left and it matches the base, but not the replace, this miiiiiight be the case we want.
637        if ( count($exportNSParts) == 1 && $exportNSParts[0] == $baseParts[0] )
638        {
639            array_shift($replaceParts);
640            $newBase = implode('/', $replaceParts);
641
642            if ( substr($newBase, -1) != '/' )
643            {
644                $newBase .= '/';
645            }
646
647            $this->debug->message("new Base: ", $newBase, 1);
648
649            // Now check from the beginning ...
650            return $newBase . $file;
651        }
652
653        return $origReplaceID;
654    }
655}
656
657?>