Simule Edit Alt

Just the source


<?php if (!defined('PmWiki')) exit();

/*  Copyright 2012 Carlos A. Bonamigo (cabsec.pmwiki@gmail.com)
    This is a recipe 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.
*/

SDV($SimulEditAltFolder, "$WorkDir/");
SDV($SimulEditAltCookie, "SimEditAlt");
SDV($SimulEditAltCookieTime, time()+3600 );
SDV($SimulEditAltPrefix, ".SimEditAlt-" );
SDV($SimulEditAltRedirectFmt, '$PageUrl?action=edit&simedit=on' );
SDV($SimulEditAltColsRows,  'rows="23" cols="60"');

if($action == "edit")
  array_unshift($EditFunctions,'SimulEditAlt');

if($action == "edit" && isset($_GET['simedit']) 
         && isset($_COOKIE[$SimulEditAltCookie]) ){
  $SimulEditAltKey = $_COOKIE[$SimulEditAltCookie];
  $SimulEditAltKey = str_replace('.','',$SimulEditAltKey);
  $SimulEditAltKey = str_replace('/','',$SimulEditAltKey);
  $SimulEditAltFile = $SimulEditAltFolder
      .$SimulEditAltPrefix.$SimulEditAltKey;
  if(file_exists($SimulEditAltFile)){
    $SimuleEditAltText = file_get_contents($SimulEditAltFile);
    $MessagesFmt[] = "<textarea ".$SimulEditAltColsRows.">"
       .$SimuleEditAltText."</textarea>";
    setcookie($SimulEditAltCookie,'', $Now-3600,"/");
    unlink($SimulEditAltFile);
  }else{
    $MessagesFmt[] = "$[Sorry, we could not rescue your last edit.]";
  }
}

if(!function_exists('PHSC')) {
  function PHSC($x, $flags=ENT_COMPAT, $enc=null) { # for PHP 5.4
    if(is_null($enc)) $enc = $GLOBALS['Charset'];
    return htmlspecialchars($x, $flags, $enc);
  }
}

function SimulEditAlt($pagename,&$page,&$new) { 
  global $Now, $EnablePost, $MessagesFmt, $WorkDir,
    $SimulEditAltCookie, $SimulEditAltPrefix, 
    $SimulEditAltCookieTime, $SimulEditAltRedirectFmt,
    $SimulEditAltFolder;

  if (@!$_POST['basetime'] || !PageExists($pagename) 
      || $page['time'] >= $Now
      || $_POST['basetime']>=$page['time']
      || $page['text'] == $new['text']) return;

  $EnablePost = 0;
  $Redirect = FmtPageName($SimulEditAltRedirectFmt, $pagename);
  $filekey = substr(md5(time().rand(10,99)),0,8);
  $tmpfile = $SimulEditAltFolder.$SimulEditAltPrefix.$filekey ;
  $newtext = str_replace('$','&#036;',PHSC($new['text'],ENT_NOQUOTES));
  file_put_contents($tmpfile,$newtext);
  setcookie($SimulEditAltCookie,$filekey, $SimulEditAltCookieTime,"/");
  Redirect($pagename,$Redirect);
}