<?php
/*
###################################################################################
  Bigware Shop 2.3
  Release Datum: 23.08.2015
  
  Bigware Shop
  http://www.bigware.de

  Copyright (c) 2015 Bigware LTD
  (c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
  (c) 2015	Bigware LTD

  Copyrightvermerke duerfen nicht entfernt werden.
  ------------------------------------------------------------------------
  Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
  der GNU General Public License, wie von der Free Software Foundation
  veroeffentlicht, weitergeben und/oder modifizieren, entweder gemaess Version 2 
  der Lizenz oder (nach Ihrer Option) jeder spaeteren Version.
  Die Veroeffentlichung dieses Programms erfolgt in der Hoffnung, dass es Ihnen
  von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die
  implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FUER EINEN
  BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
  
  Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
  Programm erhalten haben. Falls nicht, schreiben Sie an die Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

  Infos:
  ------------------------------------------------------------------------
  Der Bigware Shop wurde vor vielen Jahren bereits aus dem bekannten Shopsystem osCommerce
  weiter- und neuentwickelt.
  Der Bigware Shop legt im hohen Masse Wert auf Bedienerfreundlichkeit, beinhaltet eine leichte
  Installation, viele neue professionelle Werkzeuge und zeichnet sich aus durch eine grosse 
  Community, die bei Problemen weiterhelfen kann.
  
  Der Bigware Shop ist auf jedem System lauffaehig, welches eine PHP Umgebung
  (ab PHP 4.1.3) und mySQL zur Verfuegung stellt und auf Linux basiert.
 
  Hilfe erhalten Sie im Forum auf www.bigware.de 
  
  -----------------------------------------------------------------------
  
 ##################################################################################




*/
?>
<?php
$runtime_start = explode (' ', microtime ());
if (!defined('SCRIPT_ROOT')) {
    die();
} 
 $marked_page_class  = '';
$marked_page_left   = '[';
$marked_page_right  = ']';
$use_file_names     = false; 
  
require FOLDER_RELATIV_INCLUDES . 'functions.inc.php';
require FOLDER_RELATIV_INCLUDES . 'log_downloads.class.inc.php';
$log_views = new log_downloads;
 $script_name    = 'Galerie';
$script_version = '2.3';
 $debug_mode = 'off';
 if (isset($_GET) and !empty($_GET)) {
    $_GET = $_GET;
} 
if (isset($_SERVER) and !empty($_SERVER)) {
    $_SERVER = $_SERVER;
} 
 if ($debug_mode == 'on') {
    get_phpinfo(array('Script Name' => $script_name, 'Script Version' => $script_version));
} 
 if (!isset($language) or empty($language) or !is_file(FOLDER_RELATIV_LANGUAGES . $language . '/' . $GLOBALS[CONFIG_NAME_FILE][galerie])) {
    $language = 'en';
} 
require(FOLDER_RELATIV_LANGUAGES . $language . '/' . $GLOBALS[CONFIG_NAME_FILE][galerie]);
 require FOLDER_RELATIV_INCLUDES . 'template.class.inc.php';
$tpl = new template;
$tpl->load_file('gal', $global_template);
$tpc = new template;
$tpc->load_file('cell', $cell_template);
$cell_content   = $tpc->files['cell'];    
$use_order_file = '';
 if (is_file($picture_path . '/' . $csv_fields)) {
    $fp = fopen($picture_path . '/' . $csv_fields, "r");
    $row = '';
    $use_order_file = 'true';
    while ($data = fgetcsv ($fp, 1000, ';')) {
        $picture_data[] = trim($data[0]);
        $picture_file_names[trim($data[0])] = trim($data[0]);
        $num = count($data);
        $row++;
        for ($j = 0; $j < $num; $j++) {
            $content_data_temp['field'] .= $data[$j] . '<br>';
            if ($j == 1){
            	$content_data_temp['field_kurz' . $j] = $data[$j];
            }
	} 
	if ($content_data_temp['field_kurz1'] == ""){
		$content_data_temp['field_kurz1'] = "";
	}
        $content_data[] = $content_data_temp;
        $content_data_temp = '';
    } 
    fclose ($fp);
} else if (is_dir($picture_path)) {
	$txt['field_kurz1'] = '';
	$txt['field'] = '';
    $content_data = '';
    $handle = opendir($picture_path);
    while ($file = readdir($handle)) {
        if (preg_match("/^\.{1,2}$/i", $file)) {
            continue;
        } 
        if (preg_match("/\.[a-z]{3}$/i", $file)) {
            $picture_data[] = $file;
            $picture_file_names[$file] = $file;
        } 
    } 
    closedir($handle);
} else {
    echo 'Path to picture directory incorrect.<br /><br />Pfad zum Bildverzeichnis nicht korrekt.';
    exit;
} 
$picture_number = count ($picture_data);
 if ($use_order_file != 'true' and is_array($picture_data)) {
    if ($order == 'ascending') {
        sort($picture_data);
    } else {
        rsort($picture_data);
    } 
} 
 if (!empty($_SERVER['QUERY_STRING']) and preg_match("/^[0-9]*$/", $_SERVER['QUERY_STRING']) and $_SERVER['QUERY_STRING'] <= $picture_number) {
    $startvalue = $_SERVER['QUERY_STRING'];
} else {
    $startvalue = 0;
}
if ($use_file_names == true) {
    $picture_data_files = array_flip($picture_data);
    if (!empty($_SERVER['QUERY_STRING']) and
        isset($picture_data_files[urldecode($_SERVER['QUERY_STRING'])])) {
    
        $startvalue = $picture_data_files[urldecode($_SERVER['QUERY_STRING'])];
    } else {
        $startvalue = 0;
    }
} 
 $table_rows = '';
$count      = 0;
for ($i = $startvalue; $i < $picture_number; $i++) {
    if ($i >= $startvalue + $pictures_per_page) {
        break;
    } 
    $count++;
     if (isset($logging_file) and !empty($logging_file)) {
        $log_views->count($path['log'] . '/' . $logging_file, '', $picture_data[$i]);
    } 
     if (isset($statistic_file) and !empty($statistic_file)) {
        $picture_views = $log_views->get_download_number($path['log'] . '/' . $statistic_file, $picture_data[$i]);
    } else {
        $picture_views['views'] = '';
    } 
    if ($use_file_names == true and
        isset($picture_data[$i])) {            
        $picture_identifier = $picture_data[$i]; 
    } else {
        $picture_identifier  = $i;
    }
    $show_pictures_t = array('name' => $picture_data[$i],
        'number' => $picture_identifier ,
        'views' => $picture_views['views']);
    if (isset($content_data) and is_array($content_data)) {
        $show_pictures[] = array_merge($show_pictures_t, $content_data[$i]);
    } else {
        $show_pictures[] = $show_pictures_t;
    } 
    if ($count == $picture_count) {
        $tpc->files['cell'] = $cell_content;
        $tpc->analysis_loop('cell', 'show_pictures');
        $table_rows .= $tpc->files['cell'];
        $count = 0;
        $show_pictures = '';
    } 
} 
$show_table[] = array('tablecontent' => $table_rows);
if ($count != $picture_count and !empty($show_pictures)) {
    $tpc->files['cell'] = $cell_content;
    $tpc->analysis_loop('cell', 'show_pictures');
    $show_table[] = array('tablecontent' => $tpc->files['cell']);
}                   
 if ($startvalue + $pictures_per_page < $picture_number) {
    $next_page = 'true';
    $next = $startvalue + $pictures_per_page;
    if ($use_file_names == true and
        isset($picture_data[$next])) {
            
        $next = $picture_data[$next];
    }
} 
if ($startvalue - $pictures_per_page >= 0) {
    $prev_page = 'true';
    $prev = $startvalue - $pictures_per_page;
    if ($use_file_names == true and
        isset($picture_data[$prev])) {
            
        $prev = $picture_data[$prev];
    }
} 
 $allpages = ceil($picture_number / $pictures_per_page);
$currentpage = ceil($startvalue / $pictures_per_page) + 1;
$allpictures = $picture_number;
 $pagelink   = 0;
$i          = 1;
while ($i <= $allpages) 
{
    if ($i == $currentpage) {
        $marked_page = '<span class="' . $marked_page_class . '">' . $marked_page_left . $i .  $marked_page_right . '</span>';
    } else {
        $marked_page = $i;
    }
    if ($use_file_names == true and
        isset($picture_data[$pagelink])) {            
        $picture_identifier = $picture_data[$pagelink]; 
    } else {
        $picture_identifier  = $pagelink;
    }
    $page_direct[] = array(
        'page'          => $i,
        'link'          => $picture_identifier,
        'marked_page'   => $marked_page);
    $i++;
    $pagelink += $pictures_per_page;
} 
 $new_script_name = $script_name;
$script_name = basename($_SERVER['PHP_SELF']);
 $txt['txt_script_name'] = $new_script_name;
$txt['txt_script_version'] = $script_version;
$txt['bildrahmen_breite'] = $bildrahmen_breite;
$txt['bildrahmen_farbe'] = $bildrahmen_farbe;
$txt['ausrichtung'] = $ausrichtung;
$txt['ausrichtung_bildseite'] = $ausrichtung_bildseite;
if (isset($txt) and is_array($txt)) {
    reset ($txt);
    while (list($key, $val) = each($txt)) {
        $$key = $val;
        $tpl->register('gal', $key);
    } 
} 
if (isset($add_text) and is_array($add_text)) {
    reset ($add_text);
    while (list($key, $val) = each($add_text)) {
        $$key = $val;
        $tpl->register('gal', $key);
    } 
} 
$tpl->register('gal', 'prev');
$tpl->register('gal', 'next');
$tpl->register('gal', 'allpages');
$tpl->register('gal', 'currentpage');
$tpl->register('gal', 'allpictures');
$tpl->register('gal', 'picture_url');
$tpl->register('gal', 'script_name');
$tpl->register('gal', 'large_picture_page');
$tpl->analysis_loop('gal', 'show_table');
$tpl->analysis_loop('gal', 'page_direct');
$tpl->analysis_if('gal', 'prev_page');
$tpl->analysis_if('gal', 'next_page');
$et="\n\n".'  '."\n\n";
if(preg_match("#</body>#i",$tpl->files['gal']))$et.='</body>';
if(preg_match("#</html>#i",$tpl->files['gal']))$et.="\n".'</html>';
$tpl->files['gal']=preg_replace("#</body>#i",'',preg_replace("#</html>#i",'',$tpl->files['gal']));
$tpl->parse('gal');
$tpl->print_file('gal');  
debug_mode(script_runtime($runtime_start), 'Runtime: ');
?>