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 // Remove extensions 92 if ( $overrideRewrite ) { 93 $ID = preg_replace("#\.[^\.]+$#", '', $ID); 94 } 95 96 $url = $this->wl($this->cleanID($ID), null, true, null, null, $overrideRewrite); // this must be done with rewriting set to override 97 //$url = $this->wl($this->cleanID($ID), null, true); // this must be done with rewriting set to override 98 $uri = @parse_url($url); 99 if ( $uri['path'][0] == '/' ) { 100 $uri['path'] = substr($uri['path'], 1); 101 } 102 103 return $this->shortenName($uri['path'] . '.' . $this->settings->fileType); 104 } 105 106 /** 107 * get the Title for the page 108 **/ 109 public function getSiteTitle($ID) { 110 if (useHeading('content') && $ID) { 111 $heading = p_get_first_heading($ID,true); 112 if ($heading) { 113 return $this->xmlEntities($heading); 114 } 115 } 116 return ucwords($this->xmlEntities(array_pop(explode(':', $ID)))); 117 } 118 119 /** 120 * Encoding ()taken from DW - but without needing the renderer 121 **/ 122 public function xmlEntities($string) { 123 return htmlspecialchars($string,ENT_QUOTES,'UTF-8'); 124 } 125 126 /** 127 * Create name for the file inside the zip and the replacements 128 **/ 129 function shortenName($NAME) 130 { 131 $NS = $this->settings->exportNamespace; 132 $NAME = preg_replace("%^" . DOKU_BASE . "%", "", $NAME); 133 $NAME = preg_replace("%^(_media/)?(" . $NS . "/)?%", "", $NAME); 134 135 $this->debug->message("Shortening file to '$NAME'", null, 1); 136 return $NAME; 137 } 138 139 /** 140 * Remove unwanted chars from ID 141 * 142 * Cleans a given ID to only use allowed characters. Accented characters are 143 * converted to unaccented ones 144 * 145 * @author Andreas Gohr <andi@splitbrain.org> 146 * @param string $raw_id The pageid to clean 147 * @param boolean $ascii Force ASCII 148 * @param boolean $media Allow leading or trailing _ for media files 149 */ 150 function cleanID($raw_id,$ascii=false,$media=false){ 151 global $conf; 152 global $lang; 153 static $sepcharpat = null; 154 155 global $cache_cleanid; 156 $cache = & $cache_cleanid; 157 158 // check if it's already in the memory cache 159 if (isset($cache[(string)$raw_id])) { 160 return $cache[(string)$raw_id]; 161 } 162 163 $sepchar = $conf['sepchar']; 164 if($sepcharpat == null) // build string only once to save clock cycles 165 $sepcharpat = '#\\'.$sepchar.'+#'; 166 167 $id = trim((string)$raw_id); 168 // $id = utf8_strtolower($id); // NO LowerCase for us! 169 170 //alternative namespace seperator 171 $id = strtr($id,';',':'); 172 if($conf['useslash']){ 173 $id = strtr($id,'/',':'); 174 }else{ 175 $id = strtr($id,'/',$sepchar); 176 } 177 178 if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); 179 if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); 180 181 // We want spaces to be preserved when they are in the link. 182 global $UTF8_SPECIAL_CHARS2; 183 $UTF8_SPECIAL_CHARS2_SAVE = (string)$UTF8_SPECIAL_CHARS2; 184 $UTF8_SPECIAL_CHARS2 = str_replace(' ', '', $UTF8_SPECIAL_CHARS2); 185 186 //remove specials 187 $id = utf8_stripspecials($id,$sepchar,'\*'); 188 $UTF8_SPECIAL_CHARS2 = $UTF8_SPECIAL_CHARS2_SAVE; 189 190 if($ascii) $id = utf8_strip($id); 191 192 //clean up 193 $id = preg_replace($sepcharpat,$sepchar,$id); 194 $id = preg_replace('#:+#',':',$id); 195 $id = ($media ? trim($id,':.-') : trim($id,':._-')); 196 $id = preg_replace('#:[:\._\-]+#',':',$id); 197 198 $cache[(string)$raw_id] = $id; 199 return($id); 200 } 201 202 203 /** 204 * This builds a link to a wikipage - changed for internal use here 205 * 206 * It handles URL rewriting and adds additional parameter if 207 * given in $more 208 * 209 * @author Andreas Gohr <andi@splitbrain.org> 210 */ 211 212 function wl($id='',$more='',$abs=false,$sep='&', $IDexists=true, $overrideRewrite=false, $hadBase=false){ 213 global $conf; 214 215 $this->debug->message("Starting to build WL-URL for '$id'", $more, 1); 216 217 if(is_array($more)){ 218 219 $intermediateMore = ''; 220 foreach( $more as $key => $value) { 221 222 if ( strlen($intermediateMore) > 0 ) { 223 $intermediateMore .= $sep; 224 } 225 226 if ( !is_array($value) ) { 227 $intermediateMore .= rawurlencode($key) . '='; 228 $intermediateMore .= rawurlencode($value); 229 continue; 230 } 231 232 foreach( $value as $val ) { 233 if ( strlen($intermediateMore) > 0 ) { 234 $intermediateMore .= $sep; 235 } 236 237 $intermediateMore .= rawurlencode($key) . '[]='; 238 $intermediateMore .= rawurlencode($val); 239 } 240 } 241 242 $more = $intermediateMore; 243 }else{ 244 $more = str_replace(',',$sep,$more); 245 } 246 247 $id = idfilter($id); 248 249 if($abs){ 250 $xlink = DOKU_URL; 251 if ( !$IDexists && !$hadBase ) { // If the file does not exist, we have to remove the base. This link my be one to an parallel BASE. 252 $xlink = preg_replace('#' . DOKU_BASE . '$#', '', $xlink); 253 } 254 }else if ($IDexists || $hadBase) { // if the ID does exist, we may add the base. 255 $xlink = DOKU_BASE; 256 } else{ 257 $xlink = ""; 258 } 259 260 // $this->debug->message("internal WL function Before Replacing: '$xlink'", array(DOKU_REL, DOKU_URL, DOKU_BASE, $xlink), 2); 261 $xlink = preg_replace('#(?<!http:|https:)//+#','/', ($abs ? '' : '/') . "$xlink/"); // ensure slashes at beginning and ending, but strip doubles 262 $this->debug->message("'$xlink'", array(DOKU_REL, DOKU_URL, DOKU_BASE, $xlink), 2); 263 264 if ( $overrideRewrite ) { 265 $this->debug->message("Override enabled.", null, 1); 266 $id = strtr($id,':','/'); 267 268 $xlink .= $id; 269 if($more) $xlink .= '?'.$more; 270 } else { 271 if($conf['userewrite'] == 2 ){ 272 $xlink .= DOKU_SCRIPT.'/'.$id; 273 if($more) $xlink .= '?'.$more; 274 }elseif($conf['userewrite'] ){ 275 $xlink .= $id; 276 if($more) $xlink .= '?'.$more; 277 }elseif($id){ 278 $xlink .= DOKU_SCRIPT.'?id='.$id; 279 if($more) $xlink .= $sep.$more; 280 }else{ 281 $xlink .= DOKU_SCRIPT; 282 if($more) $xlink .= '?'.$more; 283 } 284 } 285 286 $this->debug->message("internal WL function result: '$xlink'", null, 2); 287 288 return $xlink; 289 } 290 291 /** 292 * Create the export file name - this is the file where everything is being stored 293 * @param $FILE 294 * @param $PATTERN - additional pattern for re-using old files 295 */ 296 public function getSpecialExportFileName($FILE, $PATTERN=null) { 297 298 if ( empty($FILE) ) 299 { 300 $FILE = $this->settings->origZipFile; 301 } 302 303 if ( empty($PATTERN) && empty($this->settings->pattern) ){ 304 $this->debug->message("Generating an internal md5 pattern. This will go wrong - and won't cache properly.", null, 3); 305 $PATTERN = md5(microtime(false)); 306 } 307 308 // Set Pattern Global for other stuff 309 if ( empty($this->settings->pattern) ) { 310 $this->settings['pattern'] = $PATTERN; 311 } else { 312 $PATTERN = $this->settings->pattern; 313 } 314 315 $FA = explode('.', $FILE); 316 $EXT = array_pop($FA); 317 array_push($FA, 'auto'); 318 array_push($FA, $PATTERN); 319 array_push($FA, $EXT); 320 321 $fileName = implode('.', $FA); 322 $this->debug->message("Export Filename for '$FILE' will be: '$fileName'", null, 2); 323 return $fileName; 324 } 325 326 public function getCacheFileNameForPattern($PATTERN = null) 327 { 328 if ( $PATTERN == null ) { 329 $PATTERN = $this->settings->pattern; 330 } 331 332 return getCacheName($this->getSpecialExportFileName($this->settings->origZipFile, $PATTERN), '.' . basename(mediaFN($this->settings->origZipFile)) ); 333 } 334 335 function startRedirctProcess($counter) { 336 global $ID; 337 338 $URL = wl($ID); 339 340 $additionalParameters = $_REQUEST; 341 $additionalParameters['startcounter'] = $counter; 342 $additionalParameters['pattern'] = $this->settings->pattern; 343 344 unset($additionalParameters['id']); 345 unset($additionalParameters['u']); 346 unset($additionalParameters['p']); 347 unset($additionalParameters['r']); 348 unset($additionalParameters['http_credentials']); 349 350 $this->addAdditionalParametersToURL($URL, $additionalParameters); 351 $this->debug->message("Redirecting to '$URL'", null, 2); 352 353 send_redirect($URL); 354 exit(0); // Should not be reached, but anyways 355 } 356 357 /** 358 * Builds additional Parameters into the URL given 359 * @param $URL 360 * @param $newAdditionalParameters 361 */ 362 function addAdditionalParametersToURL(&$URL, $newAdditionalParameters) { 363 364 // Add additionalParameters 365 if ( !empty($newAdditionalParameters) ) { 366 foreach($newAdditionalParameters as $key => $value ) { 367 if ( empty($key) || empty($value) ) { continue; } 368 369 $append = ''; 370 371 if ( is_array($value) ) { 372 foreach( array_values($value) as $aValue ) { // Array Handling 373 $URL .= (strstr($URL, '?') ? '&' : '?') . $key . "[]=$aValue"; 374 } 375 } else { 376 $append = "$key=$value"; 377 $URL .= empty($append) || strstr($URL, $append) ? '' : (strstr($URL, '?') ? '&' : '?') . $append; 378 } 379 380 } 381 } 382 } 383 384 /** 385 * Cleans the wiki variables and returns a rebuild URL that has the new variables at hand 386 * @param $data 387 */ 388 function prepare_POSTData($data) 389 { 390 $NS = !empty($data['ns']) ? $data['ns'] : $data['id']; 391 392 $this->removeWikiVariables($data); 393 $data['do'] = 'siteexport'; 394 $additionalKeys = ''; 395 396 ksort($data); 397 398 $this->debug->message("Prepared POST data:", $data, 1); 399 400 foreach( $data as $key => $value ) { 401 402 if ( !is_array($value) ) { continue; } 403 $this->debug->message("Found inner Array:", $value, 1); 404 405 asort($value); 406 foreach ( $value as $innerKey => $aValue ) 407 { 408 if ( is_numeric($innerKey)) 409 { 410 $innerKey = ''; 411 } 412 413 $additionalKeys .= "&$key" . "[$innerKey]=$aValue"; 414 } 415 416 unset($data[$key]); 417 } 418 419 return wl($NS, $data, true, '&') . $additionalKeys; 420 } 421 422 /** 423 * Parses a String into a $_REQUEST Like variant. You have to tell if a decode of the values is needed 424 * @param $inputArray 425 * @param $decode 426 */ 427 public function parseStringToRequestArray($inputArray, $decode=false) 428 { 429 global $plugin_controller; 430 431 $outputArray = $inputArray; 432 if ( !is_array($inputArray) ) 433 { 434 $intermediate = str_replace("&", "&", $inputArray); 435 436 $outputArray = array(); 437 foreach( explode("&", $intermediate) as $param ) { 438 list($key, $value) = explode("=", $param, 2); 439 440 // This is needed if we do want to calculate $_REQUEST for a non HTTP-Request 441 if ( $decode) 442 { 443 $value = urldecode($value); 444 } 445 446 if ( empty($key) ) { continue; } // Don't check on Value, because there may be only the key that should be preserved 447 448 if ( substr($key, -2) == '[]' ) { 449 $key = substr($key, 0, -2); 450 if ( !is_array($outputArray[$key]) ) { 451 $outputArray[$key] = array(); 452 } 453 454 array_push($outputArray[$key], $value); // Array Handling 455 } else { 456 $outputArray[$key] = $value; 457 } 458 } 459 } 460 461 if ( !empty($outputArray['diPlu']) ) { 462 463 $allPlugins = array(); 464 foreach($plugin_controller->getList(null,true) as $plugin ) { 465 // check for CSS or JS 466 if ( !file_exists(DOKU_PLUGIN."$plugin/script.js") && !file_exists(DOKU_PLUGIN."$p/style.css") ) { continue; } 467 $allPlugins[] = $plugin; 468 } 469 470 if ( count($outputArray['diPlu']) > (count($allPlugins) / 2) ) { 471 $outputArray['diInv'] = 1; 472 $outputArray['diPlu'] = array_diff($allPlugins, $outputArray['diPlu']); 473 } 474 } 475 476 return $outputArray; 477 } 478 479 /** 480 * Remove certain fields from the list. 481 * @param $removeArray 482 * @param $advanced 483 * @param $isString 484 */ 485 function removeWikiVariables(&$removeArray, $advanced=false, $isString=false) { 486 487 $removeArray = $this->parseStringToRequestArray($removeArray); 488 489 // 2010-08-23 - If there is still the media set, retain the id for e.g. detail.php 490 if ( !isset($removeArray['media']) ) { 491 unset($removeArray['id']); 492 } 493 494 unset($removeArray['do']); 495 unset($removeArray['ns']); 496 unset($removeArray['call']); 497 unset($removeArray['sectok']); 498 unset($removeArray['rndval']); 499 unset($removeArray['tseed']); 500 unset($removeArray['http_credentials']); 501 unset($removeArray['u']); 502 unset($removeArray['p']); 503 unset($removeArray['r']); 504 unset($removeArray['base']); 505 unset($removeArray['siteexport']); 506 unset($removeArray['DokuWiki']); 507 unset($removeArray['cronOverwriteExisting']); 508 509 if ( $removeArray['renderer'] == 'xhtml' ) { 510 $removeArray['do'] = 'export_' . $removeArray['renderer']; 511 unset($removeArray['renderer']); 512 } 513 514 // Keep custom options 515 if ( is_array($removeArray['customoptionname']) && is_array($removeArray['customoptionvalue']) && count($removeArray['customoptionname']) == count($removeArray['customoptionvalue']) ) 516 { 517 for( $index=0; $index<count($removeArray['customoptionname']); $index++) 518 { 519 $removeArray[$removeArray['customoptionname'][$index]] = $removeArray['customoptionvalue'][$index]; 520 } 521 unset($removeArray['customoptionname']); 522 unset($removeArray['customoptionvalue']); 523 } 524 525 if ( $advanced ) { 526 if ( $removeArray['renderer'] != 'xhtml' && !empty($removeArray['renderer']) ) { 527 $removeArray['do'] = 'export_' . $removeArray['renderer']; 528 } 529 530 // 2010-08-25 - Need fakeMedia for some _detail cases with rewrite = 2 531 if ( isset($removeArray['fakeMedia']) ) { 532 unset($removeArray['media']); 533 unset($removeArray['fakeMedia']); 534 } 535 536 /* remove internal params */ 537 unset($removeArray['ens']); 538 unset($removeArray['renderer']); 539 unset($removeArray['site']); 540 unset($removeArray['namespace']); 541 unset($removeArray['exportbody']); 542 unset($removeArray['addParams']); 543 unset($removeArray['template']); 544 unset($removeArray['eclipseDocZip']); 545 unset($removeArray['useTocFile']); 546 unset($removeArray['JavaHelpDocZip']); 547 unset($removeArray['depth']); 548 unset($removeArray['depthType']); 549 unset($removeArray['startcounter']); 550 unset($removeArray['pattern']); 551 unset($removeArray['TOCMapWithoutTranslation']); 552 // unset($removeArray['disableCache']); 553 unset($removeArray['debug']); 554 } 555 556 if ( $isString && is_array($removeArray) ) { 557 $intermediate = $removeArray; 558 $removeArray = array(); 559 560 foreach ( $intermediate as $key => $value ) { 561 if ( is_array($value) ) { 562 foreach( array_values($value) as $aValue ) { // Array Handling 563 $removeArray[] = $key . "[]=$aValue"; 564 } 565 } else { 566 $value = trim($value); 567 568 $removeArray[] = "$key" . ( ((empty($value) && intval($value) !== 0)) || $value == '' ? '' : "=$value" ); // If the Value is empty, the Key must be preserved 569 } 570 } 571 572 //$removeArray = implode( ($this->settings->fileType == 'pdf' ? "&" : "&"), $removeArray); 573 $removeArray = implode( "&", $removeArray); // The & made problems with the HTTPClient / Apache. It should not be a problem to have & 574 } 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, $separator=':') 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($separator, $replaceID); 625 $baseParts = explode($separator, $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($separator, $replaceParts); 652 653 if ( substr($newBase, -1) != $separator ) 654 { 655 $newBase .= $separator; 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?>