<?php if (!defined('PmWiki')) exit();
/*  ZipExport -- export PmWiki pages in the default pagefile format
    Written by Petko Yotov (c) 2009-2020 pmwiki.org/petko

    This file written for PmWiki; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  See pmwiki.php for full details.
*/

$RecipeInfo['ZipExport']['Version'] = '20200319a';

SDVA($HandleActions, array('zipexport'=>'HandleZipExport'));
SDVA($HandleAuth, array('zipexport'=>'read'));

SDVA($SearchPatterns['normal+self'], array(
  'recent' => '!\.(All)?Recent(Changes|Uploads)$!',
  'group' => '!\.Group(Print)?(Header|Footer|Attributes)$!',
  ));

# privacy & version stuff
SDVA($ZipExport, array(
  'WhiteListPages' => 'PmWiki*.*',
  'SkipPageAttributes' => array('version', 'newline', 'host', 'agent'),
  'ListOpt' => array('list' => 'normal+self', 'self'=>1),
));

function HandleZipExport($pagename, $auth = 'read') {
  global $ZipExport, $CategoryGroup, $Charset, $Version;
  
  $page = RetrieveAuthPage($pagename, $auth, true, READPAGE_CURRENT);
  if (!$page) Abort("?insufficient permissions");
  
  SDVA($ZipExport['CategoryOpt'], array('link' => $pagename));
  
  # determine list of pages to display
  if (@($_REQUEST['trail'] || $_REQUEST['group'] || $_REQUEST['link'] 
        || $_REQUEST['name'])) 
    $opt = $ZipExport['ListOpt'];
  else if (preg_match("/^$CategoryGroup\\./", $pagename))
    $opt = $ZipExport['CategoryOpt'];
  else { 
    PCache($pagename, $page);
    $pagelist = array($pagename); 
  }
  if (!@$pagelist) {
    $opt = array_merge($opt, @$_REQUEST);
    $pagelist = MakePageList($pagename, $opt, 0);
  }
  $pagelist = MatchPageNames($pagelist, $ZipExport['WhiteListPages']);

  if(! count($pagelist)) Abort('?No whitelisted pages match the requested specification.');

  $r0 = array('%', "\n", '<');
  $r1 = array('%25', '%0a', '%3c');
  
  $zip = new ZipArchive();
  $zfile = @tempnam('tmp', 'pmzip');
  $zip->open($zfile, ZipArchive::OVERWRITE);
  
  $cnt = 0;
  foreach($pagelist as $pn) {
    $page = RetrieveAuthPage($pn, 'read', false, READPAGE_CURRENT);
    if(!$page) continue;

    foreach($ZipExport['SkipPageAttributes'] as $v) unset($page[$v]);
    $page['charset'] = $Charset; # PmWiki automatically converts to $Charset
    uksort($page, 'CmpPageAttr');
    $x = "version=$Version ordered=1 urlencoded=1\n";

    foreach($page as $k=>$v)
      if ($k > '' && $v>'' && $k[0] != '=') {
        $x .= str_replace($r0, $r1, "$k=$v") . "\n";
      }
    $zip->addFromString("wiki.d/$pn", $x);
    $cnt++;
  }
  
  $zip->close(); 

  if($cnt) {
    $downname = preg_replace('/\\..*$/', '', $pagename) .'.zip';
    
    header("Content-Type: application/zip");
    header("Content-Length: " . filesize($zfile));
    header("Content-Disposition: attachment; filename=\"$downname\"");
    readfile($zfile);
    unlink($zfile); 
  }
  else {
    unlink($zfile);
    Abort('?No permissions to access the requested list specification.');
  }
  exit;
}