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