<?php if (!defined('PmWiki')) exit();
/**
  PPDonate redirector for PmWiki.org
  Written by (c) Petko Yotov 2015-2022 www.pmwiki.org/petko

  This text is 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 3 of the License, or
  (at your option) any later version. See pmwiki.php for full details
  and lack of warranty.
*/
# Version date
$RecipeInfo['PPDonate']['Version'] = '20221029';

SDVA($HandleActions, array('ppdonate'=> 'HandlePPDonate'));
SDVA($HandleAuth, array('ppdonate'=> 'read'));

SDVA($PPDonate, array(
  'DefaultMaintainer' => '',
//   'PPUrl' => 'https://www.paypal.com/cgi-bin/webscr?cmd=_donations&',
  'PPUrl' => 'https://www.paypal.com/donate/?',
  'Thanks' => '<p class="note frame" style="font-size: 1.5em; margin: 1em 0; padding: 1em;">' . XL('Thank you for your support!') . '</p>',
));
SDVA($PPDonate['parameters'], array(
  'business' => '',
  'hosted_button_id' => '',
  'currency_code' => 'USD',
  'no_note' => '0',
  'no_shipping' => '1',
  'lc' => 'US',
));

function HandlePPDonate($pagename, $auth='read') {
  global $AuthorGroup, $PPDonate, $PageDonateFmt,
    $HandleBrowseFmt, $PageStartFmt, $PageEndFmt;

  if (@$_GET['pp']==1) {
    $PageDonateFmt = $PPDonate['Thanks'];
    $HandleBrowseFmt = array(&$PageStartFmt,
      &$PageDonateFmt, '$PageText', &$PageEndFmt);
    HandleBrowse($pagename);
    exit;
  }

  $pn = MakeBaseName($pagename);
  $cpage = RetrieveAuthPage($pn, $auth, true, READPAGE_CURRENT);
  if (! $cpage) Abort("Cannot $auth $n.");
  
  if( preg_match("/^$AuthorGroup\\./", $pn) ) {
    $profilepage = $pn;
  }
  else {

    if (!preg_match('/^Maintainer: *(\\S+.*\\S) *$/m', $cpage['text'], $m)) {
      $m = array();
      if ($PPDonate['DefaultMaintainer']) $m[1] = "[[~{$PPDonate['DefaultMaintainer']}]]]";
      else Abort("No maintainer found for $pn.");
    }

    $maintainer = $m[1];
    if (! preg_match("/\\[\\[ *(~|$AuthorGroup *\\/) *(\\S.*?) *(\\|.+)?\\]\\]/", $maintainer, $m))
      Abort("No maintainer found for $pn.");

    $mname = $m[2];

    $profilepage = MakePageName("$AuthorGroup.$AuthorGroup", "$AuthorGroup.$mname");
  }

  if ( (!$profilepage) || ! PageExists($profilepage))
    Abort("No profile found for the maintainer of $pn.");

  $param = PageVar($profilepage, '$:PPDonate');

  if (! trim(@$param)) // Maintainer has not configured donations.
    Redirect($profilepage); // will exit

  $opt = ParseArgs($param);
  
  if (! @$opt['business'] && ! @$opt['hosted_button_id'])
    Abort("Maintainer has not configured donations.");

  $PageUrl = PageVar($pagename, '$PageUrl');
  $WikiTitle = PageVar($pagename, '$WikiTitle');

  $p = array();
  foreach ($PPDonate['parameters'] as $k=>$v) {
    if(!isset($opt[$k])) continue;
    $v = str_replace('$', '', $opt[$k]);
    $p[] = "$k=" . rawurldecode($v);
  }
  $p[] = 'item_name='.rawurlencode("$WikiTitle ($pagename)");
  $p[] = 'rm=1';
  $p[] = 'return='. rawurlencode("$PageUrl?action=ppdonate&pp=1");
  $p[] = 'cancel_return='. rawurlencode($PageUrl);

  $url = $PPDonate['PPUrl'] . implode('&', $p);

  Redirect($pagename, $url);

}