Mercurial > hg > mgallery
diff mgallery.inc.php @ 0:ac688606ec4b
Initial import of code.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 13 May 2015 07:27:40 +0300 |
parents | |
children | dec931d9e860 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mgallery.inc.php Wed May 13 07:27:40 2015 +0300 @@ -0,0 +1,301 @@ +<?php +// +// Yet Another Image Gallery +// (C) Copyright 2015 Tecnic Software productions (TNSP) +// + +$mgProgVersion = "v0.4"; +$mgProgInfo = "Programmed by Matti 'ccr' Hamalainen"; +$mgProgEmail = "<ccr@tnsp.org>"; +$mgProgCopyright = "2015 Tecnic Software productions (TNSP)"; + + +// +// 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); + + +// +// Navigation control defines +// +define("GNAV_TOP" , 0x01); +define("GNAV_BOTTOM" , 0x02); +define("GNAV_IMG" , 0x04); +define("GNAV_TEXT" , 0x08); +define("GNAV_SIDE_IMG" , 0x10); +define("GNAV_JAVASCRIPT" , 0x20); +define("GNAV_BREADCRUMBS", 0x40); + + +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"), + + "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> © Copyright ".$mgProgCopyright), + "css_select" => array(MG_BOOL, FALSE), + "css" => array(MG_STR, NULL), + "album_icon" => array(MG_STR, "album_sm.png"), + + "image_navigation" => array(MG_INT, GNAV_TOP | GNAV_BOTTOM | GNAV_SIDE_IMG | GNAV_TEXT | GNAV_JAVASCRIPT | GNAV_BREADCRUMBS), + "album_navigation" => array(MG_INT, GNAV_BREADCRUMBS), + + "album_row_limit" => array(MG_INT, 5), + "album_page_limit" => array(MG_INT, 100), + + "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, 80), // JPEG quality percent + + "med_width" => array(MG_INT, 800), + "med_height" => array(MG_INT, 600), + "med_quality" => array(MG_INT, 95), +); + + +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 mgGetArr($data, $skeys, $sfmt1 = "%1", $sfmt2 = "", $func = NULL) +{ + 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_callable($func)) + $val = call_user_func($func, $val); + + $str = str_replace("%".$i, $val, $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; + } +} + + +?> \ No newline at end of file