<?php if(!defined('PmWiki'))exit;
/**
  Simple picture rotate function for PmWiki
  Written by (c) 2006-2020 Petko Yotov 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.

  This text is partly based on the Mini picture gallery.

  Copyright 2006-2020 Petko Yotov www.pmwiki.org/petko
*/
$RecipeInfo['RotatePicture']['Version'] = '20200116';

SDVA($HandleActions, array('rotatepic'=>'HandleRotatePicture'));
SDVA($HandleAuth, array('rotatepic'=>'upload'));

SDVA($RotatePicture, array(
  'ImTypes' => array(1=>"gif",2=>"jpeg",3=>"png",15=>"wbmp",16=>"xbm"),
  'ImRx' => "/\\.(gif|png|jpe|jpe?g|wbmp|xbm)$/i",
  'SkipRx' => "/^th\\d+---/i", // thumbnails
  'ListFmt' => '[[Attach:%1$s | Attach:%1$s ]] | '
    . ' \'\'\'rotate %1$s\'\'\': [[{$FullName}?upname=%1$s&action=rotatepic&deg=90|left]] '
    . ' [[{$FullName}?upname=%1$s&action=rotatepic&deg=-90|right]] '
    . ' [[{$FullName}?upname=%1$s&action=rotatepic&deg=180|upside down]] '."\n"
    ,
));

Markup('(:rotatelist:)', 'directives', '/\\(:rotatelist:\\)/', 'FmtRotateList');

function FmtRotateList($m) {
  global $UploadFileFmt, $RotatePicture, $MarkupToHTML;
  extract($MarkupToHTML);
  
  $uploaddir = FmtPageName($UploadFileFmt, $pagename);
  
  $dirp = @opendir($uploaddir);
  if (!$dirp) return '';
  $flist = array();
  while (($fname=readdir($dirp)) !== false) {
    if ($fname[0] == '.') continue;
    if (! preg_match($RotatePicture['ImRx'], $fname)) continue;
    if (preg_match($RotatePicture['SkipRx'], $fname)) continue;
    
    $flist[] = $fname;
  }
  closedir($dirp);
  natcasesort($flist);
  
  $out = "";
  foreach($flist as $fname) {
    $out .= sprintf($RotatePicture['ListFmt'], $fname);
  
  }
  
  return PRR($out);
  
}


function HandleRotatePicture($pagename, $auth = 'upload') {
  global $UploadFileFmt, $Now, $RotatePicture;
  
  $degrees = floatval(@$_REQUEST['deg']);
  if(!$degrees) Abort("? Nothing to do (deg).");
  $upname = stripmagic(@$_REQUEST['upname']);
  if(!$upname) Abort("? Nothing to do (upname).");


  $page = RetrieveAuthPage($pagename, $auth, true, READPAGE_CURRENT);
  if(!$page) Abort("?No permissions to $auth @ $pagename");
  
  
  
  
  
  $upname = MakeUploadName($pagename, $upname);
  $fpath = FmtPageName("$UploadFileFmt/$upname", $pagename);
  
  if(!file_exists($fpath)){Abort("? file '$fpath' not found."); exit;}
  
  list($W, $H, $T) = @getimagesize($fpath);
  
  if(!isset($RotatePicture['ImTypes'][$T])){Abort("? format $T not supported."); exit;}
  
  $fcreate = "imagecreatefrom".$RotatePicture['ImTypes'][$T];
  if (!function_exists($fcreate)){Abort("? No such function $fcreate.");}
  
  $fsave = "image".$RotatePicture['ImTypes'][$T];
  if (!function_exists($fsave)){Abort("? No such function $fsave.");}
  
  $img = $fcreate($fpath);
  if (!@$img){Abort("? Could not load picture from $fpath.");}
  
  $rotated = imagerotate($img, $degrees, 0);
  if(!$rotated) {
    Abort("? could not rotate $fpath.");
  }
  rename($fpath, "$fpath,$Now");
  
  $fsave($rotated, $fpath);
  clearstatcache();
  
  HandleDispatch($pagename, 'download');
//   Redirect($pagename);
}