<?php

/*
  Adds markup to add lines using a form and delete lines from a wiki page.
  Version: 1.10, 21.9.2006


  Copyright 2005 Nils Knappmeier (nk@knappi.org)

  Permission is hereby granted, free of charge, to any person obtaining 
  a copy of this software and associated documentation files (the 
  "Software"), to deal in the Software without restriction, including 
  without limitation the rights to use, copy, modify, merge, publish, 
  distribute, sublicense, and/or sell copies of the Software, and to 
  permit persons to whom the Software is furnished to do so, subject to 
  the following conditions:

  The above copyright notice and this permission notice shall be 
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
*/


function FormMarkup($targetname,$formname) {
 global $pagename;

 $targetname = MakeLink($pagename,$targetname,NULL,'','$FullName');

 return FmtPageName("<form action='{\$PageUrl}' method='post'>
   <input type='hidden' value='$targetname' name='n'/>
   <input type='hidden' value='$formname' name='formname'/>
   <input type='hidden' value='addline' name='action'/>",$targetname);
}




function AppendAsField($mask) {
   $mask = str_replace('&amp;','&',$mask);
   $mask = str_replace('&lt;','<',$mask);
   $mask = str_replace('&gt;','>',$mask);

   return '<input type="hidden" name="appendas" value="'.urlencode($mask).'"/>';
}

function DeleteMarkup($str) {
  global $pagename;
  static $number=0;
  $number++;
  $md5=md5(stripslashes($str));
  if ($_GET['md5']==$md5) {
     $button='Try again!';
     $style='font-weight: bold; background-color:#fee;';
  } else  {
     $button='Delete';
     $style='';
  }
  return FmtPageName("<form action='{\$PageUrl}' method='post'>
     <input type='hidden' name='n' value='$pagename'>
     <input type='hidden' name='action' value='deleteline'>
     <input type='hidden' name='linenr' value='$number'>
     <input type='hidden' name='md5' value='$md5'>
     <input type='submit' style='$style' name='doit' value='$button'>
  </form>",$pagename);
}


Markup('addlineform','directives','/\(:form (.*):\)/e',"Keep(FormMarkup(\$pagename,'$1'))");
Markup('addlineform2','<addlineform','/\(:form (.*) target=(.*):\)/e',"Keep(FormMarkup('$2','$1'))");
Markup('addlineendform','directives','/\(:endform:\)/',"</form>");
Markup('inputfield','directives','/\(:textfield +([^ ]*) *?:\)/e','Keep("<input type=\'text\' name=\'$1\' size=\'30\'/>")');
Markup('submitfield','directives','/\(:submit (.*):\)/e','Keep("<input type=\'submit\' name=\'ok\' value=\'$1\'/>")');
Markup('appendasfield','[=','/\(:appendas "(.*)":\)/e','Keep(AppendAsField("$1"))');
Markup('addhere','directives','/\(:addhere (.*):\)/e','');
Markup('prependhere','directives','/\(:prependhere (.*):\)/e','');
Markup('deleteline','directives','/((.*)\(:delete:\)(.*))/e','stripslashes("$2").Keep(DeleteMarkup("$1")).stripslashes("$3")');


$HandleActions['addline']='HandleAddLine';
$HandleActions['deleteline']='HandleAddLine';



function HandleAddLine($pagename) {
   global $HandleActions,$action,$ScriptUrl;

   $page = RetrieveAuthPage($pagename,"read");
   if (!$page) { Abort("?cannot edit $pagename"); }
   # SetPageVars($pagename,$page,"Edit $pagename");

   # Create Replacement
   $replace = urldecode($_POST['appendas']);

   $replace = preg_replace('/\\\\n/',"\n",$replace);
   $replace = preg_replace('/\{date\:(.*?)\}/e','date("$1")',$replace);
   $replace = preg_replace('/\{strftime\:(.*?)\}/e','strftime("$1")',$replace);
   $replace = preg_replace('/\{(.*?)\}/e','$_REQUEST["$1"]',$replace);
   $replace = stripslashes($replace);

   
   $text = split("\n",$page['text']);
   $newpagecmd='(:addhere '.$_POST['formname'].':)';
   $prependcmd='(:prependhere '.$_POST['formname'].':)';
   foreach ($text as $nr => $line) {
     if ($line==$newpagecmd) {
        $text[$nr] = $replace."\n$line";
      
     } else { 
     }
   }


   if ($_POST['action']=='deleteline') {
     $count=0;
     $newtext='';

     foreach ($text as $nr => $line) {
       # Prevent the (:appendas...:)-statement from being deleted,
       # thus remove this directive when checking for the (:delete:)
       $appendascheck = preg_replace('/\(:appendas (.*):\)/','',$line); 
       if (strpos($appendascheck,'(:delete:)')) { 
          $nowmd5 = md5($line);

          $count++; 
          if ($count==$_POST['linenr'])  { 
             # Check consistency
             if ($nowmd5 != $_POST['md5']) {
                Header('Location: '.$ScriptUrl.'?action=view'
                        .'&n='.$pagename
                        .'&md5='.$_POST['md5']
                        .'&deletefailed=true');
                exit;
             }
             unset($text[$nr]);
             break;
          }
       }

     } 
   } else {
      foreach ($text as $nr => $line) {
         if ($line==$newpagecmd) {
            $text[$nr]=$replace."\n$line";
         }
      }
      foreach ($text as $nr => $line) {
         if ($line==$prependcmd) {
            $text[$nr] = "$line\n".$replace;
        }
      }
   }
   $_POST['text']=addslashes(join("\n",$text));


   /** store text **/
   $handle = $HandleActions['edit'];
   $_POST['post']='Save ';
   return $handle($pagename);

 }




?>