view mgallery.inc.php @ 109:c8cfc6cc161a

Adjust image scaling to be delayed and not being done on each resize event (for example Firefox fullscreen switching animates by default and triggers LOTS of resize events, which makes things slow.)
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 30 Oct 2016 15:22:03 +0200
parents 71de97240799
children 9da8bab49711
line wrap: on
line source

<?php
//
// Yet Another Image Gallery
// -- Common functions and data include
// Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org>
// (C) Copyright 2015-2016 Tecnic Software productions (TNSP)
//

$mgProgVersion = "v0.8.0";
$mgProgInfo = "Programmed by Matti 'ccr' Hamalainen";
$mgProgEmail = "<ccr@tnsp.org>";
$mgProgCopyright = "2015-2016 Tecnic Software productions (TNSP)";


//
// Navigation control defines
//
define("MGF_JAVASCRIPT"   , 0x01);
define("MGF_BREADCRUMBS"  , 0x10);

//
// Constants for different value types
//
define("MG_STR", 1);
define("MG_INT", 2);
define("MG_DVA", 3);
define("MG_BOOL", 4);


define("yes", 1);
define("no", 0);


function mgPathName($path)
{
  $tmp = mgCleanPathArray(TRUE, 0, func_num_args(), func_get_args());
  if (count($tmp) > 0)
    return implode("/", array_splice($tmp, 0, -1))."/";
  else
    return $path;
}

//
// Configuration settings and their default values
//
$mgDefaults = array(
  "base_path"        => array(MG_STR, mgPathName(mgRealPath($_SERVER["SCRIPT_FILENAME"]))),
  "base_url"         => array(MG_STR, mgPathName($_SERVER["PHP_SELF"])),
  "image_url"        => array(MG_STR, mgPathName($_SERVER["PHP_SELF"])),
  "mgallery_php"     => array(MG_STR, "mgallery.php"),
  "format_exts"      => array(MG_STR, "\.jpg|\.png|\.gif|\.jpeg"),
  "captions_file"    => array(MG_STR, "captions.txt"),
  "header_file"      => array(MG_STR, "header.txt"),
  "info_file"        => array(MG_STR, "gallery.info"),
  "cache_file"       => array(MG_STR, ".mgallery.cache"),

  "cover_images"     => array(MG_BOOL, TRUE),
  "use_tables"       => array(MG_BOOL, TRUE),
  "clean_urls"       => array(MG_BOOL, FALSE),

  "title_prefix"     => array(MG_STR, ""),
  "title_sep"        => array(MG_STR, " - "),
  "page_info"        => array(MG_STR, "<b>MGallery ".$mgProgVersion."</b> &copy; Copyright ".$mgProgCopyright),
  "css_select"       => array(MG_BOOL, FALSE),
  "css"              => array(MG_STR, NULL),
  "urchin_file"      => array(MG_STR, FALSE),
  "album_icon"       => array(MG_STR, "album_sm.png"),

  "image_flags"      => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS),
  "album_flags"      => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS),
  "album_row_limit"  => array(MG_INT, 5),

  "med_suffix"       => array(MG_STR, ".med"),
  "tn_path"          => array(MG_STR, "tn/"),

  "tn_width"         => array(MG_INT, 140),  // In pixels, applies as bounding box for w/h
  "tn_height"        => array(MG_INT, 100),
  "tn_quality"       => array(MG_INT, 77),   // JPEG quality percent

  "med_width"        => array(MG_INT, 960),
  "med_height"       => array(MG_INT, 640),
  "med_quality"      => array(MG_INT, 88),
);


function mgDebug($msg)
{
//  echo "MGAL[debug]: ".$msg;
}


function mgFatal($msg)
{
  die("MGAL[fatal]: ".$msg);
}


function mgError($msg)
{
  echo "MGAL[error]: ".$msg;
  return FALSE;
}


function mgCArg($index, $clip = FALSE)
{
  global $argc, $argv;
  if ($index < $argc)
  {
    $str = $argv[$index];
    return ($clip !== FALSE) ? substr($str, 0, $clip) : $str;
  }
  else
    return FALSE;
}


function mgCArgLC($index, $clip = FALSE)
{
  global $argc, $argv;
  if ($index < $argc)
  {
    $str = strtolower($argv[$index]);
    return ($clip !== FALSE) ? substr($str, 0, $clip) : $str;
  }
  else
    return FALSE;
}


function mgGetSetting($key, $default = NULL)
{
  global $mgSettings, $mgDefaults;

  if (!array_key_exists($key, $mgDefaults))
    mgFatal("Setting '".$key."' does not exist.\n");

  if (array_key_exists($key, $mgSettings))
    $val = $mgSettings[$key];
  else
    $val = $mgDefaults[$key][1];

  if (!isset($val) || $val === NULL)
  {
    if ($default !== NULL)
      $val = $default;
    else
      mgFatal("Setting '".$key."' is not set, but is required to be configured.\n");
  }

  return $val;
}


function mgGetPath($path, $key)
{
  $val = mgGetSetting($key);
  return ($val !== FALSE) ? $path."/".$val : FALSE;
}


function mgReadSettings($filename = "mgallery.cfg")
{
  global $mgSettings, $mgDefaults;

  $spaths = array();
  $spaths[] = getcwd()."/";
  if (($tmp = getenv("HOME")) !== FALSE && strlen($tmp) > 0)
  {
    $spaths[] = $tmp."/.config/mgallery/";
    $spaths[] = $tmp."/.";
  }
  else
  {
    $data = posix_getpwuid(posix_getuid());
    if ($data !== FALSE && isset($data["dir"]))
    {
      $tmp = $data["dir"];
      $spaths[] = $tmp."/.config/mgallery/";
      $spaths[] = $tmp."/.";
    }
  }
  $spaths[] = dirname(__FILE__)."/";

  foreach (array_unique($spaths) as $path)
  {
    $file = $path.$filename;
    mgDebug("Checking '".$file."' for configuration ..\n");
    if (file_exists($file) &&
      ($mgSettings = parse_ini_file($file, FALSE)) !== FALSE)
    {
      mgDebug("Found '".$file."' config.\n");

      // Validate settings
      $ok = TRUE;
      foreach ($mgSettings as $setting => $val)
      {
        if (!array_key_exists($setting, $mgDefaults))
        {
          mgError("Setting '".$setting."' does not exist.\n");
          $ok = FALSE;
        }
      }
      return $ok;
    }
  }

  $mgSettings = array();
  return FALSE;
}


function mgRealPath($path)
{
  return realpath($path);
}


function mgCleanPathArray($refs, $start, $argc, $argv)
{
  $path = array();
  $first = TRUE;
  for ($n = $start; $n < $argc; $n++)
  {
    foreach (explode("/", $argv[$n]) as $piece)
    {
      switch ($piece)
      {
        case ".":
        case "":
          if ($first)
            $path[] = $piece;
          break;

        case "..":
          if ($refs && count($path) > 0)
            array_pop($path);
          break;

        default:
          $path[] = $piece;
          break;
      }
      $first = FALSE;
    }
  }
  return $path;
}


function mgCleanPath($refs)
{
  return implode("/", mgCleanPathArray($refs, 1, func_num_args(), func_get_args()));
}


function mgGetTrans($val, $che = FALSE)
{
  global $pageLang;

  if (is_array($val))
    $str = array_key_exists($pageLang, $val) ? $val[$pageLang] : reset($val);
  else
    $str = $val;

  return $che ? chentities($str) : $str;
}


function mgGetArr($data, $skeys, $sfmt1 = "%1", $sfmt2 = "", $func = NULL)
{
  global $pageLang;

  if (!is_array($skeys))
    $skeys = array($skeys);

  foreach ($skeys as $skey)
  if (!array_key_exists($skey, $data))
    return $sfmt2;

  $str = $sfmt1;
  for ($i = 1; $i <= sizeof($skeys); $i++)
  {
    $val = $data[$skeys[$i - 1]];
    if (is_array($val))
      $vtmp = array_key_exists($pageLang, $val) ? $val[$pageLang] : reset($val);
    else
      $vtmp = $val;

    if (is_callable($func))
      $vtmp = call_user_func($func, $vtmp);

    $str = str_replace("%".$i, $vtmp, $str);
  }

  return $str;
}


function mgGetVal($skeys, $sfmt1 = "%1", $sfmt2 = "", $func = NULL)
{
}


function mgGetDValStr($type, $val)
{
  switch ($type)
  {
    case MG_STR  : return "\"".$val."\"";
    case MG_BOOL : return $val ? "yes" : "no";
    case MG_INT  :
    default      : return (string) $val;
  }
}


?>