<commit_before><?php

/*
MINIGAL NANO
- A PHP/HTML/CSS based image gallery script

This script and included files are subject to licensing from Creative Commons (http://creativecommons.org/licenses/by-sa/2.5/)
You may use, edit and redistribute this script, as long as you pay tribute to the original author by NOT removing the linkback to www.minigal.dk ("Powered by MiniGal Nano x.x.x")

MiniGal Nano is created by <NAME>

Copyright 2010 by <NAME>
Support: www.minigal.dk
Community: www.minigal.dk/forum

Please enjoy this free script!


Upgraded to https://github.com/sebsauvage/MinigalNano
by <NAME>.

*/

error_reporting(-1);

// Do not edit below this section unless you know what you are doing!

header('Content-Type: text/html; charset=UTF-8'); // We use UTF-8 for proper international characters handling.
$version = "0.3.7";
ini_set("memory_limit", "256M");

require("config-default.php");
include("config.php");

//-----------------------
// DEFINE VARIABLES
//-----------------------
$page_navigation = "";
$breadcrumb_navigation = "";
$thumbnails = "";
$new = "";
$images = "";
$exif_data = "";
$messages = "";
$comment = "";

//-----------------------
// PHP ENVIRONMENT CHECK
//-----------------------
if (!function_exists('exif_read_data') && $display_exif == 1) {
    $display_exif = 0;
    $messages = "Error: PHP EXIF is not available. Set &#36;display_exif = 0; in config.php to remove this message";
}

//-----------------------
// FUNCTIONS
//-----------------------
function padstring($name, $length)
{
    global $label_max_length;
    if (!isset($length)) {
        $length = $label_max_length;
    }
    if (strlen($name) > $length) {
        return substr($name, 0, $length) . "...";
    }
    return $name;
}

function getfirstImage($dirname)
{
    $imageName = false;
    $extensions = array("jpg", "png", "jpeg", "gif");
    if ($handle = opendir($dirname)) {
        while(false !== ($file = readdir($handle))) {
            if ($file[0] == '.') {
                continue;
            }
            $pathinfo = pathinfo($file);
            if (empty($pathinfo['extension'])) {
                continue;
            }
            $ext = strtolower($pathinfo['extension']);
            if (in_array($ext, $extensions)) {
                $imageName = $file;
                break;
            }
        }
        closedir($handle);
    }
    return $imageName;
}

function parse_fraction($v, $round=0)
{
    list($x, $y) = array_map('intval', explode('/', $v));
    if (empty($x) || empty($y)) {
        return $v;
    }
    if ($x % $y == 0) {
        return $x/$y;
    }
    if ($y % $x == 0) {
        return "1/" . $y/$x;
    }
    return round($x/$y, $round);
}

function readEXIF($file)
{
    $exif_arr = array();
    $exif_data = exif_read_data($file);

    $exif_val = $exif_data['Model'];
    if (!empty($exif_val)) {
        $exif_arr[] = $exif_val;
    }

    $exif_val = $exif_data['FocalLength'];
    if (!empty($exif_val)) {
        $exif_arr[] = parse_fraction($exif_val) . "mm";
    }

    $exif_val = $exif_data['ExposureTime'];
    if (!empty($exif_val)) {
        $exif_arr[] = parse_fraction($exif_val, 2) . "s";
    }

    $exif_val = $exif_data['FNumber'];
    if (!empty($exif_val)) {
        $exif_arr[] = "f" . parse_fraction($exif_val);
    }

    $exif_val = $exif_data['ISOSpeedRatings'];
    if (!empty($exif_val)) {
        $exif_arr[] = "ISO " . $exif_val;
    }

    if (count($exif_arr) > 0) {
        return "::" . implode(" | ", $exif_arr);
    }
}

function checkpermissions($file)
{
    global $messages;

    if (!is_readable($file)) {
        $messages = "At least one file or folder has wrong permissions. "
            . "Learn how to <a href='http://minigal.dk/faq-reader/items/"
            . "how-do-i-change-file-permissions-chmod.html' target='_blank'>"
            . "set file permissions</a>";
    }
}

if (!defined("GALLERY_ROOT"))
    define("GALLERY_ROOT", "");
$requestedDir = '';
if (!empty($_GET['dir']))
    $requestedDir = $_GET['dir'];
$thumbdir = rtrim('photos/' . $requestedDir, '/');

//$thumbdir = str_replace('/..', '', $thumbdir); // Prevent directory traversal attacks.
if (strstr($thumbdir, '..') !== false) {
    $requestedDir = '';
    $thumbdir = rtrim('photos/', '/');
}

$currentdir = GALLERY_ROOT . $thumbdir;

//-----------------------
// READ FILES AND FOLDERS
//-----------------------
$files = array();
$dirs = array();
if (is_dir($currentdir) && $handle = opendir($currentdir)) {
    while (false !== ($file = readdir($handle)) && !in_array($file, $SkipObjects)) {
        // 1. LOAD FOLDERS
        if (is_dir($currentdir . "/" . $file)) {
            if ($file != "." && $file != "..") {
                checkpermissions($currentdir . "/" . $file); // Check for correct file permission
                // Set thumbnail to folder.jpg if found:
                if (file_exists($currentdir. '/' . $file . '/folder.jpg')) {
                    $linkParams = http_build_query(
                        array('dir' => ltrim("$requestedDir/$file", '/')),
                        '',
                        '&amp;'
                    );
                    $linkUrl = "?$linkParams";

                    $imgParams = http_build_query(
                        array(
                            'filename' => "$currentdir/$file/folder.jpg",
                            'size' => $thumb_size
                        ),
                        '',
                        '&amp;'
                    );
                    $imgUrl = GALLERY_ROOT . "createthumb.php?$imgParams";

                    $dirs[] = array(
                        "name" => $file,
                        "date" => filemtime($currentdir . "/" . $file . "/folder.jpg"),
                        "html" => "<li><a href=\"{$linkUrl}\"><em>" . padstring($file, $label_max_length) . "</em><span></span><img src=\"{$imgUrl}\"  alt=\"$label_loading\" /></a></li>"
                    );
                } else {
                    // Set thumbnail to first image found (if any):
                    unset($firstimage);
                    $firstimage = getfirstImage("$currentdir/" . $file);

                    if ($firstimage != "") {
                        $linkParams = http_build_query(
                            array('dir' => ltrim("$requestedDir/$file", '/')),
                            '',
                            '&amp;'
                        );
                        $linkUrl = "?$linkParams";

                        $imgParams = http_build_query(
                            array(
                                'filename' => "$thumbdir/$file/$firstimage",
                                'size' => $thumb_size
                            ),
                            '',
                            '&amp;'
                        );
                        $imgUrl = GALLERY_ROOT . "createthumb.php?$imgParams";

                        $dirs[] = array(
                            "name" => $file,
                            "date" => filemtime($currentdir . "/" . $file),
                            "html" => "<li><a href=\"{$linkUrl}\"><em>" . padstring($file, $label_max_length) . "</em><span></span><img src=\"{$imgUrl}\"  alt='$label_loading' /></a></li>"
                        );
                    } else {
                        // If no folder.jpg or image is found, then display default icon:
                        $linkParams = http_build_query(
                            array('dir' => ltrim("$requestedDir/$file", '/')),
                            '',
                            '&amp;'
                        );
                        $linkUrl = "?$linkParams";
                        $imgUrl = GALLERY_ROOT . 'images/folder_' . strtolower($folder_color) . '.png';

                        $dirs[] = array(
                            "name" => $file,
                            "date" => filemtime($currentdir . "/" . $file),
                            "html" => "<li><a href=\"{$linkUrl}\"><em>" . padstring($file, $label_max_length) . "</em><span></span><img src=\"{$imgUrl}\" width='$thumb_size' height='$thumb_size' alt='$label_loading' /></a></li>"
                        );
                    }
                }
            }
        }

        // 2. LOAD CAPTIONS
        $img_captions[''] = '';
        if (file_exists($currentdir ."/captions.txt")) {
            $file_handle = fopen($currentdir ."/captions.txt", "rb");
            while (!feof($file_handle)) {
                $line_of_text = fgets($file_handle);
                if (empty($line_of_text))
                    continue;
                $parts = explode('/n', $line_of_text);
                foreach($parts as $img_capts) {
                    list($img_filename, $img_caption) = explode('|', $img_capts);
                    $img_captions[$img_filename] = $img_caption;
                }
            }
            fclose($file_handle);<commit_msg>parse captions.txt only once per dir, not once per file per dir<commit_after><?php

/*
MINIGAL NANO
- A PHP/HTML/CSS based image gallery script

This script and included files are subject to licensing from Creative Commons (http://creativecommons.org/licenses/by-sa/2.5/)
You may use, edit and redistribute this script, as long as you pay tribute to the original author by NOT removing the linkback to www.minigal.dk ("Powered by MiniGal Nano x.x.x")

MiniGal Nano is created by <NAME>

Copyright 2010 by <NAME>
Support: www.minigal.dk
Community: www.minigal.dk/forum

Please enjoy this free script!


Upgraded to https://github.com/sebsauvage/MinigalNano
by S<NAME>.

*/

error_reporting(-1);

// Do not edit below this section unless you know what you are doing!

header('Content-Type: text/html; charset=UTF-8'); // We use UTF-8 for proper international characters handling.
$version = "0.3.7";
ini_set("memory_limit", "256M");

require("config-default.php");
include("config.php");

//-----------------------
// DEFINE VARIABLES
//-----------------------
$page_navigation = "";
$breadcrumb_navigation = "";
$thumbnails = "";
$new = "";
$images = "";
$exif_data = "";
$messages = "";
$comment = "";

//-----------------------
// PHP ENVIRONMENT CHECK
//-----------------------
if (!function_exists('exif_read_data') && $display_exif == 1) {
    $display_exif = 0;
    $messages = "Error: PHP EXIF is not available. Set &#36;display_exif = 0; in config.php to remove this message";
}

//-----------------------
// FUNCTIONS
//-----------------------
function padstring($name, $length)
{
    global $label_max_length;
    if (!isset($length)) {
        $length = $label_max_length;
    }
    if (strlen($name) > $length) {
        return substr($name, 0, $length) . "...";
    }
    return $name;
}

function getfirstImage($dirname)
{
    $imageName = false;
    $extensions = array("jpg", "png", "jpeg", "gif");
    if ($handle = opendir($dirname)) {
        while(false !== ($file = readdir($handle))) {
            if ($file[0] == '.') {
                continue;
            }
            $pathinfo = pathinfo($file);
            if (empty($pathinfo['extension'])) {
                continue;
            }
            $ext = strtolower($pathinfo['extension']);
            if (in_array($ext, $extensions)) {
                $imageName = $file;
                break;
            }
        }
        closedir($handle);
    }
    return $imageName;
}

function parse_fraction($v, $round=0)
{
    list($x, $y) = array_map('intval', explode('/', $v));
    if (empty($x) || empty($y)) {
        return $v;
    }
    if ($x % $y == 0) {
        return $x/$y;
    }
    if ($y % $x == 0) {
        return "1/" . $y/$x;
    }
    return round($x/$y, $round);
}

function readEXIF($file)
{
    $exif_arr = array();
    $exif_data = exif_read_data($file);

    $exif_val = $exif_data['Model'];
    if (!empty($exif_val)) {
        $exif_arr[] = $exif_val;
    }

    $exif_val = $exif_data['FocalLength'];
    if (!empty($exif_val)) {
        $exif_arr[] = parse_fraction($exif_val) . "mm";
    }

    $exif_val = $exif_data['ExposureTime'];
    if (!empty($exif_val)) {
        $exif_arr[] = parse_fraction($exif_val, 2) . "s";
    }

    $exif_val = $exif_data['FNumber'];
    if (!empty($exif_val)) {
        $exif_arr[] = "f" . parse_fraction($exif_val);
    }

    $exif_val = $exif_data['ISOSpeedRatings'];
    if (!empty($exif_val)) {
        $exif_arr[] = "ISO " . $exif_val;
    }

    if (count($exif_arr) > 0) {
        return "::" . implode(" | ", $exif_arr);
    }
}

function checkpermissions($file)
{
    global $messages;

    if (!is_readable($file)) {
        $messages = "At least one file or folder has wrong permissions. "
            . "Learn how to <a href='http://minigal.dk/faq-reader/items/"
            . "how-do-i-change-file-permissions-chmod.html' target='_blank'>"
            . "set file permissions</a>";
    }
}

if (!defined("GALLERY_ROOT"))
    define("GALLERY_ROOT", "");
$requestedDir = '';
if (!empty($_GET['dir']))
    $requestedDir = $_GET['dir'];
$thumbdir = rtrim('photos/' . $requestedDir, '/');

//$thumbdir = str_replace('/..', '', $thumbdir); // Prevent directory traversal attacks.
if (strstr($thumbdir, '..') !== false) {
    $requestedDir = '';
    $thumbdir = rtrim('photos/', '/');
}

$currentdir = GALLERY_ROOT . $thumbdir;

//-----------------------
// READ FILES AND FOLDERS
//-----------------------
$files = array();
$dirs = array();
$img_captions = array();
if (is_dir($currentdir) && $handle = opendir($currentdir)) {
    // 1. LOAD CAPTIONS
    $caption_filename = "$currentdir/captions.txt";
    if (is_readable($caption_filename)) {
        $caption_handle = fopen($caption_filename, "rb");
        while (!feof($caption_handle)) {
            $caption_line = fgetss($caption_handle);
            if (empty($caption_line)) {
                continue;
            }
            list($img_file, $img_text) = explode('|', $caption_line);
            $img_captions[$img_file] = trim($img_text);
        }
        fclose($caption_handle);
    }

    while (false !== ($file = readdir($handle)) && !in_array($file, $SkipObjects)) {
        // 2. LOAD FOLDERS
        if (is_dir($currentdir . "/" . $file)) {
            if ($file != "." && $file != "..") {
                checkpermissions($currentdir . "/" . $file); // Check for correct file permission
                // Set thumbnail to folder.jpg if found:
                if (file_exists($currentdir. '/' . $file . '/folder.jpg')) {
                    $linkParams = http_build_query(
                        array('dir' => ltrim("$requestedDir/$file", '/')),
                        '',
                        '&amp;'
                    );
                    $linkUrl = "?$linkParams";

                    $imgParams = http_build_query(
                        array(
                            'filename' => "$currentdir/$file/folder.jpg",
                            'size' => $thumb_size
                        ),
                        '',
                        '&amp;'
                    );
                    $imgUrl = GALLERY_ROOT . "createthumb.php?$imgParams";

                    $dirs[] = array(
                        "name" => $file,
                        "date" => filemtime($currentdir . "/" . $file . "/folder.jpg"),
                        "html" => "<li><a href=\"{$linkUrl}\"><em>" . padstring($file, $label_max_length) . "</em><span></span><img src=\"{$imgUrl}\"  alt=\"$label_loading\" /></a></li>"
                    );
                } else {
                    // Set thumbnail to first image found (if any):
                    unset($firstimage);
                    $firstimage = getfirstImage("$currentdir/" . $file);

                    if ($firstimage != "") {
                        $linkParams = http_build_query(
                            array('dir' => ltrim("$requestedDir/$file", '/')),
                            '',
                            '&amp;'
                        );
                        $linkUrl = "?$linkParams";

                        $imgParams = http_build_query(
                            array(
                                'filename' => "$thumbdir/$file/$firstimage",
                                'size' => $thumb_size
                            ),
                            '',
                            '&amp;'
                        );
                        $imgUrl = GALLERY_ROOT . "createthumb.php?$imgParams";

                        $dirs[] = array(
                            "name" => $file,
                            "date" => filemtime($currentdir . "/" . $file),
                            "html" => "<li><a href=\"{$linkUrl}\"><em>" . padstring($file, $label_max_length) . "</em><span></span><img src=\"{$imgUrl}\"  alt='$label_loading' /></a></li>"
                        );
                    } else {
                        // If no folder.jpg or image is found, then display default icon:
                        $linkParams = http_build_query(
                            array('dir' => ltrim("$requestedDir/$file", '/')),
                            '',
                            '&amp;'
                        );
                        $linkUrl = "?$linkParams";
                        $imgUrl = GALLERY_ROOT . 'images/folder_' . strtolower($folder_color) . '.png';

                        $dirs[] = array(
                            "name" => $file,
                            "date" => filemtime($currentdir . "/" . $file),
                            "html" => "<li><a href=\"{$linkUrl}\"><em>" . padstring($file, $label_max_length) . "</em><span></span><img src=\"{$imgUrl}\" width='$thumb_size' height='$thumb_size' alt='$label_loading' /></a></li>"
                        );
                    }
                }
            }
{'fraction_non_alphanumeric': 0.18662780922490613, 'fraction_numerical': 0.003362663229277588, 'mean_word_length': 1.5469597487867541, 'pattern_counts': {'":': 2, '<': 69, '<?xml version=': 0, '>': 103, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3764974', 'n_tokens_mistral': 5457, 'n_tokens_neox': 5231, 'n_words': 1055}