<?php
/**
 * @package Flatnux_module_gallery
 * @author Alessandro Vernassa <speleoalex@gmail.com>
 * @author of modded flatnux module John D'Orazio <donjohn.fmmi@gmail.com>  
 * @copyright Copyright (c) 2003-2005
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
 */
defined('_FNEXEC') or die('Restricted access');
/**
 * ricava la lista delle foto presenti in una cartella
 * @param string directory
 * @param string extensions 
 */
function FNGALLERY_GetFileList($dir, $extensions)
{
    global $_FN;

    if (!is_dir($dir))
    {
        return false;
    }
    $handle_dir = opendir($dir);
    $list_images = $files = array();
    $exts = explode(",", strtolower($extensions));
    while (false !== ($file = readdir($handle_dir)))
    {
        if (!is_dir($dir . "/" . $file) && in_array(strtolower(FN_GetFileExtension($file)), $exts))
        {
            $files[] = $file;
        }
    }
    closedir($handle_dir);
    FN_NatSort($files);
    //dprint_r($files);
    foreach ($files as $file)
    {
        $tmp = array();
        $tmp['url'] = "";
        if (FN_GetFileExtension($file) == "link")
        {
            $tmp['url'] = file_get_contents($dir . "/" . $file);
        }
        $filetitle = $dir . "/" . $file . "." . $_FN['lang'] . ".title.txt";
        $filedesc = $dir . "/" . $file . "." . $_FN['lang'] . ".txt";
        $filethumb = FNGALLERY_GetThumb($dir . "/" . $file);
        $tmp['file'] = $dir . "/" . $file;
        $tmp['title'] = "";
        $tmp['description'] = "";
        if (file_exists($filetitle))
            $tmp['title'] = htmlspecialchars(file_get_contents($filetitle));
        if (file_exists($filedesc))
            $tmp['description'] = htmlspecialchars(file_get_contents($filedesc));
        $tmp['thumb'] = $filethumb;
        $list_images[] = $tmp;
    }
    return $list_images;
}
/**
 *
 * @global type $_FN
 * @param type $filename
 * @return string 
 */
function FNGALLERY_GetThumb($filename)
{
    global $_FN;
    $path = substr($filename, 0, strrpos($filename, "/"));
    $file_thumb = substr($filename, strrpos($filename, "/"), strlen($filename));
    $filenamethumb = $path . "/" . "thumbs" . "/" . $file_thumb;
    if (file_exists($filenamethumb))
    {
        return $filenamethumb;
    }
    if (file_exists($filenamethumb . ".gif"))
    {
        return $filenamethumb . ".gif";
    }
    if (file_exists($filenamethumb . ".png"))
    {
        return $filenamethumb . ".png";
    }
    if (file_exists($filenamethumb . ".jpg"))
    {
        return $filenamethumb . ".jpg";
    }
    if (file_exists($filenamethumb . ".jpeg"))
    {
        return $filenamethumb . ".jpeg";
    }
    if (file_exists($filenamethumb . ".GIF"))
    {
        return $filenamethumb . ".GIF";
    }
    if (file_exists($filenamethumb . ".PNG"))
    {
        return $filenamethumb . ".PNG";
    }
    if (file_exists($filenamethumb . ".JPG"))
    {
        return $filenamethumb . ".JPG";
    }
    if (file_exists($filenamethumb . ".JPEG"))
    {
        return $filenamethumb . ".JPEG";
    }
    if (FNGALLERY_IsVideo($filename))
        return ($_FN['siteurl'] . ("modules/gallery/images/video.png"));
    else
        return $filenamethumb . ".jpg";
}
/**
 *
 * @global string $_FN
 * @global type $photogroup
 * @return type 
 */
function FNGALLERY_IsGalleryAdmin()
{
    global $_FN;
    $config = FN_LoadConfig("modules/gallery/config.php");
    if (FN_IsAdmin() || ($config['photogroup'] != "" && FN_UserInGroup($_FN['user'], $config['photogroup'])))
        return true;
    else
        return false;
}
/**
 *
 * @param string $filename
 * @return bool 
 */
function FNGALLERY_IsVideo($filename)
{
//-----------------------------     config     -------------------------------->
    global $_FN;
    $config = FN_LoadConfig("modules/gallery/config.php");
    $photodir = $config['photodir'];
    $movie_extensions = $config['movie_extensions'];
    $ThumbSize = $config['ThumbSize'];
    $Num_Cols = $config['Num_Cols'];
    $photogroup = $config['photogroup'];
    $movie_extensions = $config['movie_extensions'];
    $create_thumbs_dir = $config['create_thumbs_dir'];
    $use_lightbox = $config['use_lightbox'];
//-----------------------------     config     --------------------------------<
    $exts = explode(",", strtolower($movie_extensions));
    if (in_array(strtolower(FN_GetFileExtension($filename)), $exts))
        return true;
    else
        return false;
}
/**
 *
 * @global type $_FN
 * @param type $filesource
 * @param type $filedest
 * @param type $max_size
 * @return type 
 */
function FNGALLERY_ResizeImage($filesource,$filedest,$max_size)
{
    global $_FN;
    if ($max_size <= 0 || !file_exists($filesource) || !getimagesize($filesource))
    {
        return false;
    }
    $max_w = $max_h = $max_size ;
    $new_height = $new_width = 0;
    list ( $width, $height, $type, $attr ) = getimagesize($filesource);
    if ($width <= $max_size && $height<= $max_size)
        return false;
    
    $new_height = $height;
    $new_width = $width;
    if ($width >= $max_w)
    {
        $new_width = $max_w;
        $new_height = intval($height * ($new_width / $width));
    }
    if ($new_height >= $max_h)
    {
        $new_height = $max_h;
        $new_width = intval($width * ($new_height / $height));
    }
    if ($width <= $max_w && $height <= $max_h)
    {
        $new_width = $width;
        $new_height = $height;
    }
    // Load
    $thumb = imagecreatetruecolor($new_width, $new_height);
    $white = imagecolorallocate($thumb, 255, 255, 255);
    $size = getimagesize($filesource);
    switch ($size [2])
    {
        case 1 :
            $source = ImageCreateFromGif($filesource);
            break;
        case 2 :
            $source = ImageCreateFromJpeg($filesource);
            break;
        case 3 :
            $source = ImageCreateFromPng($filesource);
            break;
        default :
            return false;
    }
    // Resize
    imagefilledrectangle($thumb, 0, 0, $width, $width, $white);
    imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    if (imagejpeg($thumb, $filedest))
        return true;
    return false;
}
?>