<?php $fileinfo = "index.php created 05 Apr 06 16:17:00 by krauss
      Time-stamp: <2018-10-27 17:26:19 thk>";
  $LastChangeTime = $fileinfo;
  $LastChangeTime = preg_replace("/^[^<]+<([0-9-]+ [0-9:]+).*$/i","\\1",$LastChangeTime);
  $Creator = "Thomas Krauß";
  $Titel = "Artikel";
  include("global.php");
  $Error = "";
  
  // new Flags:
  // - VIEWER=koenige.php - use an other viewer-plugin for this article
  // - IMGLIST - article is an image list, format: <dir>\n<imgfile> <description>\n... set also VIEWER

  $Imagesizes = array("I"=>"c64x64","T"=>"c128x128","S"=>"s300x200","M"=>"s600x400","L"=>"s1200x800");
  if (isset($_GET["a"]) && infiledb($_GET["a"])) { if (showimg($_GET["a"])) return; }
  if (!isset($artread)) include("inc/articles.php");
  if (isset($_GET["show"])) $aargs["show"] = $_GET["show"];
  else if (isset($_GET["a"])) $aargs["show"] = $_GET["a"];
  if (isset($aargs["show"])) $LeftMenu = getarticlemenu($aargs["show"]);
  include("header.php");

  if ($Error!="")
  { print "<p class=error>$Error</p>\n";
  } else
  { if (isset($_GET["lists"])) $aargs["lists"] = $_GET["lists"];
    else if (preg_match("/\/sgs\//",$_SERVER["SCRIPT_URL"])) $aargs["lists"] = "sgs boeller vl";
    else if (preg_match("/\/pfarrei\//",$_SERVER["SCRIPT_URL"])) $aargs["lists"] = "pfarrei";
    else $aargs["lists"] = "ort"; // "ort smue";
    
    $aargs["showarticletitleurl"] = "$myname?a=";
    
    // Elements: d Date    t Title   l Logos   h Head   c Text    E (make edit-link)
    //           i Image   ( <p>     ) </p>    B <br>   H <hrule> | table-column
    if (!isset($aargs["show"]))
    { $aargs["class"]   = "articlelist";
      $aargs["order"]   = "Ed|>l|C|Th"; // use full title path (T instead of t)
      $aargs["predata"] = "<h3>Aktuelle Meldungen</h3>";
      $aargs["nav"] = 1;
    } else
    { $aargs["class"] = "article";
      if (isset($user) && $user!="") $aargs["order"] = "tl(h)ic"; else $aargs["order"]  = "t(h)ic";
      if (isset($_GET["nav"])) $aargs["nav"] = 1;
    }
    if (isset($_GET["articleshowyear"]) && $_GET["articleshowyear"]>=0) $aargs["articleshowyear"] = $_GET["articleshowyear"];
    if (isset($_GET["showown"]) && isset($_SESSION["USERID"])) $aargs["user"] = $_SESSION["USERID"];
    if (isset($_GET["edit"])) $aargs["editarticle"] = $_GET["edit"];
    if (isset($_GET["del"]))  $aargs["delarticle"]  = $_GET["del"];
    $aargs["editurl"] = "$myname?edit=";
    $aargs["delurl"]  = "$myname?del=";
    if (isset($_POST["changeArticle"]) || isset($_POST["changehistArticle"]) || isset($_POST["newArticle"])) $args["show"] = changearticle();
    if (isset($_POST["delArticle"])) delarticle();
    foreach ($_GET as $k=>$v) if (!isset($aargs[$k])) $aargs[$k]=$v;
    getarticles($aargs);
  }
  include("footer.php");

function infiledb($name)
{ $db = new dbclass;
  $name = html_entity_decode($name);
  if (preg_match("/^(.*)@([A-Z]+)$/",$name,$m)) { $name = $m[1]; $size=$m[2]; }
  $db->query("select ID from sgdata where File='$name' limit 1");
  return($db->NumRows>0);
}

function showimg($name) // in fact deliver any file if rights are OK
{ global $datadir, $Recht, $Error, $Imagesizes, $usergroups;
  $db = new dbclass;

  $uploaddir = $datadir."/uploads/";
  $name = html_entity_decode($name);
  $size="";
  if (preg_match("/^(.*)@([A-Z]+)$/",$name,$m)) { $name = $m[1]; if (isset($Imagesizes[$m[2]])) $size=$Imagesizes[$m[2]]; }
  $db->query("select sgdata.*,sglists.GroupR from sgdata,sglists where File='$name' and Cat=ListID order by ID desc limit 1");
  // [ID] = '1' [File] = 'sgs_farb_klein_trans_1.gif' [Description] = 'SGS Logo klein' [UserID] = '1' [UploadDate] = '20180222.1019'
  // [OldVersion] = '-1' [Lists] = 'sgs' [Cat] = 'verein' [Flags] = '' [Rights] = ''
  if ($db->NumRows>0)
  { $db->getrecord();
    // foreach ($db->Record as $k=>$v) print "data[$k] = '$v'<br>\n";
    if (preg_match("/ ".$db->Record["GroupR"]." /",$usergroups))
    { $type = mime_content_type($uploaddir.$name);
      header("Content-Type: $type");
      if ($db->Record["Width"]>-1 && $db->Record["Height"]>-1 && preg_match("/([sc])([0-9]+)x([0-9]+)/",$size,$m))
      { $imgmode=$m[1]; $imgw=$m[2]; $imgh=$m[3];
        $scalex = 1.0*$imgw/$db->Record["Width"];
        $scaley = 1.0*$imgh/$db->Record["Height"];
        if (preg_match("/\.([a-z]+)$/",$name,$m)) $imgtype=$m[1]; else $imgtype="jpg";
        // convert -crop 800x800+0+133 -scale 64x64 sgs_farb_klein_trans_2.gif pnm:- | head -3
      	$opt = "";
       	if ($imgmode=="c") // crop the image
       	{ // 800x1000 -> 100x100 -> scalex,y=1/8,1/10 -> use max(scale)=1/8 cut=800,800 off=0,100
       	  $scale = ($scalex>$scaley ? $scalex:$scaley); // image higher than wide, use width, crop height -> cut crop l/r
       	  $cutw = intval($imgw/$scale);  $cuth = intval($imgh/$scale);
       	  $offx = intval( ($db->Record["Width"]-$cutw)/2 );
       	  $offy = intval( ($db->Record["Height"]-$cuth)/2 );
       	  $opt = sprintf(" -crop %dx%d+%d+%d -scale %dx%d",$cutw,$cuth,$offx,$offy,$imgw,$imgh);
       	} else // scale the image to fit in $imgw,$imgh
       	{ // 800x1000 -> 100x100 -> scalex,y=1/8,1/10 -> use min(scale)=1/10 scw,h=80,100
       	  $scale = ($scalex<$scaley ? $scalex:$scaley);
       	  $scw = intval( ($scale*$db->Record["Width"] ) );
       	  $sch = intval( ($scale*$db->Record["Height"]) );
	  if ($scw<$db->Record["Width"] && $sch<$db->Record["Height"]) $opt=sprintf(" -scale %dx%d",$scw,$sch);
       	}
	// print sprintf("convert %s %s %s:-",$opt,$uploaddir.$name,$imgtype);
	// return(0);
	if ($opt=="") readfile($uploaddir.$name);
	else
        { $fp=popen(sprintf("convert %s %s %s:-",$opt,$uploaddir.$name,$imgtype), "r");
          while(!feof($fp)) { print fread($fp, 1024); flush(); }
          fclose($fp);
	}
      } else readfile($uploaddir.$name);
      return(1);
      // print sprintf("recht %s = %d<br>\n",$db->Record["Rights"],isset($Recht[$db->Record["Rights"]]));
      // $r=""; foreach ($Recht as $k=>$v) $r.= " $k=$v";
      // print "file $uploaddir$name type $type cat '".$db->Record["Cat"]."' rights '".$db->Record["Cat"]."' rechte ($r)<br>\n";
    } else $Error = "Sie haben kein Recht die Datei anzusehen";
  }
  // $imgdir = $datadir."/wikiimg";
  // $name = preg_replace("/['\";]+/","",$name);
  // $name = preg_replace("/(\/[.]*\/)+/","/",$name);
  // $path = explode("/",$name);
  // foreach ($path as $k)
  // { $imgdir.="/$k";
  //   if (is_dir($imgdir))
  //   { if (is_file($imgdir."/.rights"))
  //     { if (! checkrights($imgdir."/.rights")) return(0);
  //     }
  //   } else
  //   { foreach (array("jpg","gif","png") as $suff)
  //     { if (is_file($imgdir.".".$suff))
  // 	{ header("Content-Type: image/$suff");
  // 	  readfile($imgdir.".".$suff);
  // 	  return(1);
  // 	}
  //     }
  //   }
  // }	
  return(0);
}


function checkrights($file)
{ global $Recht;

  if ($a=file($file))
  { foreach (explode(" ",implode(" ",$a)) as $r)
    { if (isset($Recht[$r])) return(1); }
    return(0);
  }
  return(1);
}

?>
