<?php
/*
    CREATIVE COMMONS - Attribution-No Derivative Works 3.0 Germany:
    - You may not alter, transform, or build upon this work.
    - Dieses Werk darf nicht bearbeitet oder in anderer Weise verändert werden.
*/
$ScriptPath = dirname(__FILE__)."/";
include ($ScriptPath.'/../calendar/functions_artists.php');
include ($ScriptPath.'/../calendar/functions_organizers.php');
include ($ScriptPath.'/../calendar/functions_venues.php');

function editSetupGallery ( $SetupGalleryAlbumsOrder, $SetupGalleryAlbumsThumbsHeight,
  $SetupGalleryAlbumsThumbsRandomly, $SetupGalleryAlbumsThumbsWidth, $SetupFrontendGalleryURL )
{
  global $DB_PREFIX;
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $SetupGalleryAlbumsThumbsHeight = mysql_real_escape_string($SetupGalleryAlbumsThumbsHeight);
    $SetupGalleryAlbumsThumbsWidth = mysql_real_escape_string($SetupGalleryAlbumsThumbsWidth);
    $SetupFrontendGalleryURL = mysql_real_escape_string($SetupFrontendGalleryURL);
  // Leere numerische Variablen als NULL definieren
    if ( $SetupGalleryAlbumsThumbsHeight=='' ) $SetupGalleryAlbumsThumbsHeight="NULL";
    if ( $SetupGalleryAlbumsThumbsWidth=='' ) $SetupGalleryAlbumsThumbsWidth="NULL";
  $sql = "UPDATE ".DB_PREFIX."setup SET
    SetupGalleryAlbumsOrder = '$SetupGalleryAlbumsOrder',
    SetupGalleryAlbumsThumbsHeight = $SetupGalleryAlbumsThumbsHeight,
    SetupGalleryAlbumsThumbsRandomly = '$SetupGalleryAlbumsThumbsRandomly,
    SetupGalleryAlbumsThumbsWidth = $SetupGalleryAlbumsThumbsWidth',
    SetupFrontendGalleryURL = '$SetupFrontendGalleryURL'";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
  else return false;
}

function editSetupNews ( $SetupNewsEntriesPerPage, $SetupNewsChannelTitle, $SetupNewsChannelURL,
  $SetupNewsChannelDescription, $SetupNewsChannelLanguage, $SetupNewsChannelCopyright,
  $SetupFrontendNewsDateFormat )
{
  global $DB_PREFIX;
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $SetupNewsEntriesPerPage = mysql_real_escape_string($SetupNewsEntriesPerPage);
    $SetupNewsChannelTitle = mysql_real_escape_string($SetupNewsChannelTitle);
    $SetupNewsChannelURL = mysql_real_escape_string($SetupNewsChannelURL);
    $SetupNewsChannelDescription = mysql_real_escape_string($SetupNewsChannelDescription);
    $SetupNewsChannelLanguage = mysql_real_escape_string($SetupNewsChannelLanguage);
    $SetupNewsChannelCopyright = mysql_real_escape_string($SetupNewsChannelCopyright);
    $SetupFrontendNewsDateFormat = mysql_real_escape_string($SetupFrontendNewsDateFormat);
  // Leere numerische Variablen als NULL definieren
    if ( $SetupNewsEntriesPerPage=='' ) $SetupNewsEntriesPerPage="NULL";
  $sql = "UPDATE ".DB_PREFIX."setup SET
    SetupNewsEntriesPerPage = $SetupNewsEntriesPerPage,
    SetupNewsChannelTitle = '$SetupNewsChannelTitle',
    SetupNewsChannelURL = '$SetupNewsChannelURL',
    SetupNewsChannelDescription = '$SetupNewsChannelDescription',
    SetupNewsChannelLanguage = '$SetupNewsChannelLanguage',
    SetupNewsChannelCopyright = '$SetupNewsChannelCopyright',
    SetupFrontendNewsDateFormat = '$SetupFrontendNewsDateFormat'";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
  else return false;
}

function editSetupGuestbook ( $SetupGuestbookEntriesPerPage, $SetupGuestbookMail,
  $SetupGuestbookEntriesUnlock, $SetupGuestbookSpamDomains, $SetupGuestbookSpamIPs,
  $SetupGuestbookSpamWords, $SetupGuestbookWriteSpamLog, $SetupGuestbookURL, $SetupGuestbookFormURL,
  $SetupGuestbookEmailURL, $SetupFrontendGuestbookDateFormat )
{
  global $DB_PREFIX;
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $SetupGuestbookEntriesPerPage = mysql_real_escape_string($SetupNewsEntriesPerPage);
    $SetupGuestbookMail = mysql_real_escape_string($SetupGuestbookMail);
    $SetupGuestbookSpamDomains = mysql_real_escape_string($SetupGuestbookSpamDomains);
    $SetupGuestbookSpamIPs = mysql_real_escape_string($SetupGuestbookSpamIPs);
    $SetupGuestbookSpamWords = mysql_real_escape_string($SetupGuestbookSpamWords);
    $SetupGuestbookURL = mysql_real_escape_string($SetupGuestbookURL);
    $SetupGuestbookFormURL = mysql_real_escape_string($SetupGuestbookFormURL);
    $SetupGuestbookEmailURL = mysql_real_escape_string($SetupGuestbookEmailURL);
    $SetupFrontendGuestbookDateFormat = mysql_real_escape_string($SetupFrontendGuestbookDateFormat);
  // Leere numerische Variablen als NULL definieren
    if ( $SetupGuestbookEntriesPerPage=='' ) $SetupGuestbookEntriesPerPage="NULL";
    if ( $SetupGuestbookEntriesUnlock=='' ) $SetupGuestbookEntriesUnlock=1;
    if ( $SetupGuestbookWriteSpamLog=='' ) $SetupGuestbookWriteSpamLog=0;
  $sql = "UPDATE ".DB_PREFIX."setup SET
    SetupGuestbookEntriesPerPage = $SetupGuestbookEntriesPerPage,
    SetupGuestbookMail = '$SetupGuestbookMail',
    SetupGuestbookEntriesUnlock = $SetupGuestbookEntriesUnlock,
    SetupGuestbookSpamDomains = '$SetupGuestbookSpamDomains',
    SetupGuestbookSpamIPs = '$SetupGuestbookSpamIPs',
    SetupGuestbookSpamWords = '$SetupGuestbookSpamWords',
    SetupGuestbookWriteSpamLog = $SetupGuestbookWriteSpamLog,
    SetupGuestbookURL = '$SetupGuestbookURL',
    SetupGuestbookFormURL = '$SetupGuestbookFormURL',
    SetupGuestbookEmailURL = '$SetupGuestbookEmailURL',
    SetupFrontendGuestbookDateFormat = '$SetupFrontendGuestbookDateFormat'";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
  else return false;
}

function editSetupCalendar ( $SetupAllowDoubleDates, $SetupCalendarAttributeInAddGigForm,
  $SetupShowEditorInAddGigForm, $SetupRowsTextareas, $SetupFrontendCalendarShowMonthlySeparator,
  $SetupFrontendCalendarShowTableHeads, $SetupFrontendAllowOptionalGigs,
  $SetupFrontendAllowPrivateGigs, $SetupFrontendCalendarCurrentMonthShowPastGigs,
  $SetupFrontendCalendarGigDetailsShowEmptyAttributes, $SetupFrontendCalendarSelector,
  $SetupFrontendFirstDayOfWeek, $SetupFrontendCalendarPeriodSelector,
  $SetupFrontendCalendarCharsInFields, $SetupFrontendCalendarMaxAmountOfGigs,
  $SetupFrontendCalendarMaxAmountOfGigsPast, $SetupFrontendCalendarDateFormat,
  $SetupFrontendCalendarGigDetailsDateFormat, $SetupFrontendCalendarCurrentMonthLimitFuture,
  $SetupFrontendCalendarCurrentMonthLimitPast, $SetupFrontendCalendarURL,
  $SetupFrontendCalendarGigDetailsURL, $SetupFrontendCalendarCurrentMonthURL,
  $SetupFrontendAttributes, $SetupFrontendDetailAttributes )
{
  global $DB_PREFIX;
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $SetupRowsTextareas = mysql_real_escape_string($SetupRowsTextareas);
    $SetupFrontendCalendarCharsInFields = mysql_real_escape_string($SetupFrontendCalendarCharsInFields);
    $SetupFrontendCalendarMaxAmountOfGigs = mysql_real_escape_string($SetupFrontendCalendarMaxAmountOfGigs);
    $SetupFrontendCalendarMaxAmountOfGigsPast = mysql_real_escape_string($SetupFrontendCalendarMaxAmountOfGigsPast);
    $SetupFrontendCalendarDateFormat = mysql_real_escape_string($SetupFrontendCalendarDateFormat);
    $SetupFrontendCalendarGigDetailsDateFormat = mysql_real_escape_string($SetupFrontendCalendarGigDetailsDateFormat);
    $SetupFrontendCalendarCurrentMonthLimitFuture = mysql_real_escape_string($SetupFrontendCalendarCurrentMonthLimitFuture);
    $SetupFrontendCalendarCurrentMonthLimitPast = mysql_real_escape_string($SetupFrontendCalendarCurrentMonthLimitPast);
    $SetupFrontendCalendarURL = mysql_real_escape_string($SetupFrontendCalendarURL);
    $SetupFrontendCalendarGigDetailsURL = mysql_real_escape_string($SetupFrontendCalendarGigDetailsURL);
    $SetupFrontendCalendarCurrentMonthURL = mysql_real_escape_string($SetupFrontendCalendarCurrentMonthURL);
  // Leere numerische Variablen als NULL definieren
    if ( $SetupAllowDoubleDates=='' ) $SetupAllowDoubleDates=1;
    if ( $SetupShowEditorInAddGigForm=='' ) $SetupShowEditorInAddGigForm=1;
    if ( $SetupRowsTextareas=='' ) $SetupRowsTextareas=5;
    if ( $SetupFrontendCalendarShowMonthlySeparator=='' ) $SetupFrontendCalendarShowMonthlySeparator=1;
    if ( $SetupFrontendCalendarShowTableHeads=='' ) $SetupFrontendCalendarShowTableHeads=1;
    if ( $SetupFrontendAllowOptionalGigs=='' ) $SetupFrontendAllowOptionalGigs=0;
    if ( $SetupFrontendAllowPrivateGigs=='' ) $SetupFrontendAllowPrivateGigs=0;
    if ( $SetupFrontendCalendarCurrentMonthShowPastGigs=='' ) $SetupFrontendCalendarCurrentMonthShowPastGigs=true;
    if ( $SetupFrontendCalendarGigDetailsShowEmptyAttributes=='' ) $SetupFrontendCalendarGigDetailsShowEmptyAttributes=false;
    if ( $SetupFrontendCalendarCharsInFields=='' ) $SetupFrontendCalendarCharsInFields=20;
    if ( $SetupFrontendCalendarMaxAmountOfGigs=='' ) $SetupFrontendCalendarMaxAmountOfGigs=999;
    if ( $SetupFrontendCalendarMaxAmountOfGigsPast=='' ) $SetupFrontendCalendarMaxAmountOfGigsPast=999;
    if ( $SetupFrontendCalendarCurrentMonthLimitFuture=='' ) $SetupFrontendCalendarCurrentMonthLimitFuture=12;
    if ( $SetupFrontendCalendarCurrentMonthLimitPast=='' ) $SetupFrontendCalendarCurrentMonthLimitPast=6;
  $sql = "UPDATE ".DB_PREFIX."setup SET
    SetupAllowDoubleDates = $SetupAllowDoubleDates,
    SetupCalendarAttributeInAddGigForm = '$SetupCalendarAttributeInAddGigForm',
    SetupShowEditorInAddGigForm = $SetupShowEditorInAddGigForm,
    SetupRowsTextareas = $SetupRowsTextareas,
    SetupFrontendCalendarShowMonthlySeparator = $SetupFrontendCalendarShowMonthlySeparator,
    SetupFrontendCalendarShowTableHeads = $SetupFrontendCalendarShowTableHeads,
    SetupFrontendAllowOptionalGigs = $SetupFrontendAllowOptionalGigs,
    SetupFrontendAllowPrivateGigs = $SetupFrontendAllowPrivateGigs,
    SetupFrontendCalendarCurrentMonthShowPastGigs = $SetupFrontendCalendarCurrentMonthShowPastGigs,
    SetupFrontendCalendarGigDetailsShowEmptyAttributes = $SetupFrontendCalendarGigDetailsShowEmptyAttributes,
    SetupFrontendCalendarSelector = '$SetupFrontendCalendarSelector',
    SetupFrontendFirstDayOfWeek = '$SetupFrontendFirstDayOfWeek',
    SetupFrontendCalendarPeriodSelector = '$SetupFrontendCalendarPeriodSelector',
    SetupFrontendCalendarCharsInFields = $SetupFrontendCalendarCharsInFields,
    SetupFrontendCalendarMaxAmountOfGigs = $SetupFrontendCalendarMaxAmountOfGigs,
    SetupFrontendCalendarMaxAmountOfGigsPast = $SetupFrontendCalendarMaxAmountOfGigsPast,
    SetupFrontendCalendarDateFormat = '$SetupFrontendCalendarDateFormat',
    SetupFrontendCalendarGigDetailsDateFormat = '$SetupFrontendCalendarGigDetailsDateFormat',
    SetupFrontendCalendarCurrentMonthLimitFuture = $SetupFrontendCalendarCurrentMonthLimitFuture,
    SetupFrontendCalendarCurrentMonthLimitPast = $SetupFrontendCalendarCurrentMonthLimitPast,
    SetupFrontendCalendarURL = '$SetupFrontendCalendarURL',
    SetupFrontendCalendarGigDetailsURL = '$SetupFrontendCalendarGigDetailsURL',
    SetupFrontendCalendarCurrentMonthURL = '$SetupFrontendCalendarCurrentMonthURL',
    SetupFrontendAttributes = '$SetupFrontendAttributes',
    SetupFrontendDetailAttributes = '$SetupFrontendDetailAttributes'";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
  else return false;
}

function editSetupFrontend (  )
{
  global $DB_PREFIX;
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    // AKTIVIEREN; STAMMT AUS 1.2!!!
    //$xxx = mysql_real_escape_string($xxx);
  // Leere numerische Variablen als NULL definieren
    // später anpassen und erweitern: if ( $SetupAllowDoubleDates=='' ) $SetupAllowDoubleDates=1;
  $sql = "UPDATE ".DB_PREFIX."setup SET";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
  else return false;
}

function editSetupGeneral ( $SetupGeneralRefreshtime, $SetupGeneralBackendCSVSeparator,
  $SetupGeneralBackendInactivityTime, $SetupGeneralBackendDateFormat, $SetupGeneralBackendTimeFormat,
  $SetupGeneralBackendUseCache, $SetupGeneralFrontendLanguage, $SetupGeneralFrontendCssFileURL )
{
  global $DB_PREFIX;
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $SetupGeneralRefreshtime = mysql_real_escape_string($SetupGeneralRefreshtime);
    $SetupGeneralBackendCSVSeparator = mysql_real_escape_string($SetupGeneralBackendCSVSeparator);
    $SetupGeneralBackendInactivityTime = mysql_real_escape_string($SetupGeneralBackendInactivityTime);
    $SetupGeneralBackendDateFormat = mysql_real_escape_string($SetupGeneralBackendDateFormat);
    $SetupGeneralBackendTimeFormat = mysql_real_escape_string($SetupGeneralBackendTimeFormat);
    $SetupGeneralFrontendLanguage = mysql_real_escape_string($SetupGeneralFrontendLanguage);
    $SetupGeneralFrontendCssFileURL = mysql_real_escape_string($SetupGeneralFrontendCssFileURL);
  // Leere numerische Variablen als NULL definieren
    if ( $SetupGeneralRefreshtime=='' ) $SetupGeneralRefreshtime=1;
    if ( $SetupGeneralBackendCSVSeparator=='' ) $SetupGeneralBackendCSVSeparator=';';
    if ( $SetupGeneralBackendInactivityTime=='' ) $SetupGeneralBackendInactivityTime=60;
  $sql = "UPDATE ".DB_PREFIX."setup SET
    SetupGeneralRefreshtime = $SetupGeneralRefreshtime,
    SetupGeneralBackendCSVSeparator = '$SetupGeneralBackendCSVSeparator',
    SetupGeneralBackendInactivityTime = $SetupGeneralBackendInactivityTime,
    SetupGeneralBackendDateFormat = '$SetupGeneralBackendDateFormat',
    SetupGeneralBackendTimeFormat = '$SetupGeneralBackendTimeFormat',
    SetupGeneralBackendUseCache = $SetupGeneralBackendUseCache,
    SetupGeneralFrontendLanguage = '$SetupGeneralFrontendLanguage',
    SetupGeneralFrontendCssFileURL = '$SetupGeneralFrontendCssFileURL'";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
  else return false;
}

function getURLWithoutFinalSlash ($url)
{
  if (substr( $url, strlen($url)-1 )=="/" AND $url!='http://') return substr( $url, 0, strlen($url)-1 );
    else return $url;
}

function isEmailDomainValid ($email)
{
  if (function_exists('checkdnsrr'))
  {
    if (checkdnsrr(array_pop(explode("@",$email)),"MX")) return true;
  }
  else return true;
}

function isEmailTLDValid($email)
{
  $tld = array('ac', 'ad', 'ae', 'aero', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar', 'as',
    'at', 'au', 'aw', 'az', 'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh', 'bi', 'biz', 'bj', 'bm', 'bn',
    'bo', 'br', 'bs', 'bt', 'bv', 'bw', 'by', 'bz', 'ca', 'cat', 'cc', 'cd', 'cf', 'cg', 'ch', 'ci',
    'ck', 'cl', 'cm', 'cn', 'co', 'com', 'coop', 'cr', 'cu', 'cv', 'cx', 'cy', 'cz', 'de', 'dj',
    'dk', 'dm', 'do', 'dz', 'ec', 'edu', 'ee', 'eg', 'er', 'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm',
    'fo', 'fr', 'ga', 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gl', 'gm', 'gn', 'gov', 'gp', 'gq',
    'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk', 'hm', 'hn', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im',
    'in', 'info', 'int', 'io', 'iq', 'ir', 'is', 'it', 'je', 'jm', 'jo', 'jobs', 'jp', 'ke', 'kg',
    'kh', 'ki', 'km', 'kn', 'kr', 'kw', 'ky', 'kz', 'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt',
    'lu', 'lv', 'ly', 'ma', 'mc', 'md', 'mg', 'mh', 'mil', 'mk', 'ml', 'mm', 'mn', 'mo', 'mobi',
    'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'museum', 'mv', 'mw', 'mx', 'my', 'mz', 'na', 'name', 'nc',
    'ne', 'net', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu', 'nz', 'om', 'org', 'pa', 'pe',
    'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'pro', 'ps', 'pt', 'pw', 'py', 'qa', 're', 'ro',
    'ru', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl', 'sm', 'sn', 'so',
    'sr', 'st', 'su', 'sv', 'sy', 'sz', 'tc', 'td', 'tf', 'tg', 'th', 'tj', 'tk', 'tl', 'tm', 'tn',
    'to', 'tp', 'tr', 'travel', 'tt', 'tv', 'tw', 'tz', 'ua', 'ug', 'uk', 'um', 'us', 'uy', 'uz',
    'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf', 'ws', 'ye', 'yt', 'yu', 'za', 'zm');
  $email = rtrim($email);
  $email = substr($email, strrpos($email, '.') + 1);
  if (!$email) return false;
  if (in_array(strtolower($email), $tld)) return true;
  return false;
}
function deleteFileInCache ( $Timestamp )
{
  $Year = date('Y', $Timestamp);
  $Month = date('n', $Timestamp);
  $FileName1 = 'admin-'.$Year.'-'.$Month.'.php';
  $FileName2 = 'user-'.$Year.'-'.$Month.'.php';
  $Path = 'cache/';
  if ( !is_link( $Path.$FileName1 ) AND is_file ( $Path.$FileName1 ) )
    if ( @ !unlink ( $Path.$FileName1 ) )
      print '<span class="ERROR">ERROR: Cache NOT cleared! ('.$FileName1.')</span><br />';
  if ( !is_link( $Path.$FileName2 ) AND is_file ( $Path.$FileName2 ) )
    if ( @ !unlink ( $Path.$FileName2 ) )
      print '<span class="ERROR">ERROR: Cache NOT cleared! ('.$FileName2.')</span><br />';
}
function emptyDirectory ($strDir)
{
  //echo $strDir.'<br />';
  $oDir = dir( $strDir );
  while (false!==($strFile=$oDir->read()))
  {
    if ($strFile!='.' && $strFile!='..' && !is_link($strDir.$strFile))
    {
      if (is_file($strDir.$strFile))
      {
        if (@unlink($strDir.$strFile))
        {
        }
        else
        {
          print '<span class="ErrorMessage">ERROR: Cache NOT cleared! ('.$strFile.')</span><br />';
        }
      }
      else
      {
        if(is_dir($strDir.$strFile))
        {
          //echo $strDir.'<br />';
          emptyDirectory($strDir.$strFile.'/');
          rmdir($strDir.$strFile);
        }
      }
    }
  }
  $oDir->close();
}
function clearCache ()
{
  $Path = str_replace('\\','/', __FILE__);
  $Path = str_replace('functions/standard.php', '', $Path);
  emptyDirectory($Path.'cache/');
}
function writeToCache ( $Filename, $Content)
{
  echo $Content;
  $path = 'cache/';
  $datei = $path.$Filename;
  $fp=fopen($datei,"w+");
  fwrite($fp,$Content);
}
function getArrayForAutoSuggest ( $Field )
{
  global $DB_PREFIX;
  $sql = "SELECT DISTINCT `".$Field."` FROM `".DB_PREFIX."gigs` ORDER BY `".$Field."` ASC";
  $result = mysql_query($sql);
  $ArrayForAutoSuggest = array();
  while ($daten = mysql_fetch_assoc($result))
  {
    $temp = $daten[$Field];
    if ($temp!='' OR $temp!=0) array_push($ArrayForAutoSuggest, $temp);
  }
  $String = implode (", ", $ArrayForAutoSuggest);
  $String = str_replace('"', '\"',$String);
  return $String;
}
function getFrontendURL ( $Var )
{
  global $Timestamp;
  global $GuestbookID;
  global $SetupFrontendCalendarCurrentMonthURL;
  global $SetupFrontendCalendarGigDetailsURL;
  global $SetupGuestbookEmailURL;
  global $SetupGalleryURL;
  $URL = ${$Var};
  if (isset($_SERVER['REQUEST_URI'])) $REQUEST_URI=$_SERVER['REQUEST_URI'];
    else $REQUEST_URI = '';
  if ( $URL=='' ) $URL = $REQUEST_URI;
    else $URL = $URL;
  $URL = str_replace("&","&amp;",$URL);
  $URLArray = parse_url ( $URL );
  if (isset($URLArray['query'])) $Query = $URLArray['query'];
    else $Query='';
  parse_str ( $Query );
  if ( $Var=='SetupFrontendCalendarGigDetailsURL' )
    $URL = $URLArray['path'].'?Timestamp='.$Timestamp;
  if ( $Var=='SetupFrontendCalendarCurrentMonthURL' )
  {
    if (!isset($year)) $year='';
    if ($year!='') $temp = strstr ( $Query, 'year' );
      else $temp = '';
    $Query = str_replace('&amp;'.$temp,"",$Query);
    $Query = str_replace($temp,"",$Query);
    $Query = str_replace('&amp;'.$temp,"",$Query);
    $Query = str_replace($temp,"",$Query);
    if ($Query=='') $URL = $URLArray['path'].'?';
      else $URL = $URLArray['path'].'?'.$Query.'&amp;';
  }
  if ( $Var=='SetupGuestbookEmailURL' )
  {
    if ( preg_match ( '|\?|', $URL ) ) $URL = $URL.'&amp;';
      else $URL = $URL.'?';
    $URL = $URL.'id='.$GuestbookID.'&amp;page=mail';
  }
  if ( $Var=='SetupGalleryURL' ) $URL = str_replace('?'.$Query,"",$URL);
  return $URL;
}
 
function getGoogleMapsURL ( $GigCountry, $GigDistrict, $GigPostalCode, $GigCity )
{
  global $BrowserLanguageShort;
  if ($GigPostalCode == "0") $GigPostalCode = "";
  $Location = "";
  if ( $GigCountry!='') $Location .= $GigCountry;
  if ( $GigCountry!='' AND $GigPostalCode!='') $Location .= '-';
  if ( $GigPostalCode!='') $Location .= $GigPostalCode;
  if ( $GigCountry!='' AND $GigPostalCode=='' && $GigCity!='' ) $Location .= ' ';
  if ( $GigPostalCode!='' AND $GigCity!='') $Location .= ' ';
  if ( $GigCity!='') $Location .= $GigCity;
  if ( $GigDistrict!='' AND $GigCity=='') $Location .= $GigDistrict;
  // READ http://mapki.com/wiki/Google_Map_Parameters
  return $Location = 'http://maps.google.com/maps?f=q&amp;hl='.$BrowserLanguageShort.'&amp;source=s_q&amp;geocode=&amp;q='.rawurlencode($Location);
}
 
function translateLanguageVarString ( $String )
{
  $Array = explode( ', ', $String);
  $amountArrayItems = count($Array);
  for ($i=0; $i<$amountArrayItems; $i++)
  {
    global ${'L_'.$Array[$i]};
    $Array[$i] = ${'L_'.$Array[$i]};
  }
  return implode( ', ', $Array);
}
 
function setSetupVar ( $Var, $Value )
{
  global $DB_PREFIX;
  $sql = "UPDATE ".DB_PREFIX."setup SET $Var = '$Value'";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
  else return false;
}
 
function setUserVar ( $Var, $Value )
{
  global $DB_PREFIX;
  $sql = "UPDATE ".DB_PREFIX."user SET $Var = '$Value'";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
  else return false;
}
 
function publishedGigOnTimestamp ( $Timestamp )
{
  global $DB_PREFIX;
  $SQL = "SELECT * FROM ".DB_PREFIX."gigs WHERE GigDate = ".$Timestamp." AND (GigStatus = 'Gig'";
  if ( getSetup('SetupFrontendAllowOptionalGigs') ) $SQL .= " OR GigStatus = 'Option'";
  $SQL .= ")";
  if ( !getSetup('SetupFrontendAllowPrivateGigs') ) $SQL .= " AND GigIsPrivate = 0";
  $SQL .= " AND GigPublish = 1";
  $SQL .= " LIMIT 1";
  $resultat = mysql_query($SQL);
  if ( mysql_num_rows($resultat)==1) return true;
  else return false;
}

function convertISO8601ToTimestamp ($ISO8601)
{
  global $L_ERROR_Invalid_date;
  if ( $ISO8601>0)
  {
    $eregStr =  '/([0-9]{4})-'. // centuries & years CCYY-
                '([0-9]{2})-'. // months MM-
                '([0-9]{2})'.  // days DD
                'T'.           // separator T
                '([0-9]{2}):'. // hours hh:
                '([0-9]{2}):'. // minutes mm:
                '([0-9]{2})(\.[0-9]+)?'. // seconds ss.ss...
                '(Z|[+\-][0-9]{2}:?[0-9]{2})?/'; // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
              if(preg_match($eregStr,$ISO8601,$regs))
              {
              $temp = strtotime("$regs[1]-$regs[2]-$regs[3] $regs[4]:$regs[5]:$regs[6]Z");
              // not utc
                if($regs[8] != 'Z')
                {
                  $op = substr($regs[8],0,1);
                  $h = substr($regs[8],1,2);
                  $m = substr($regs[8],strlen($regs[8])-2,2);
                  if($op == '-')
                  {
                    $temp = $temp+($h*3600+$m*60);
                  }
                  elseif($op == '+')
                  {
                    $temp = $temp-($h*3600+$m*60);
                  }
                }
              return $temp;
              }
            else // OpenOffice JJJJ-MM-TT HH:MM:SSZ
            {
              $eregStr =  '/([0-9]{4})-'. // centuries & years CCYY-
                          '([0-9]{2})-'. // months MM-
                          '([0-9]{2})'.  // days DD
                          ' '.           // separator ' '
                          '([0-9]{2}):'. // hours hh:
                          '([0-9]{2}):'. // minutes mm:
                          '([0-9]{2})(\.[0-9]+)?'. // seconds ss.ss...
                          '(Z|[+\-][0-9]{2}:?[0-9]{2})?/'; // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
              if(preg_match($eregStr,$ISO8601,$regs))
              {

              $temp = strtotime("$regs[1]-$regs[2]-$regs[3] $regs[4]:$regs[5]:$regs[6]Z");
              // not utc
                if($regs[8] != 'Z')
                {
                  $op = substr($regs[8],0,1);
                  $h = substr($regs[8],1,2);
                  $m = substr($regs[8],strlen($regs[8])-2,2);
                  if($op == '-')
                  {
                    $temp = $temp+($h*3600+$m*60);
                  }
                  elseif($op == '+')
                  {
                    $temp = $temp-($h*3600+$m*60);
                  }
                }
              return $temp;
              }
            else // OpenOffice JJJJ-MM-TT
            {
              $eregStr =  '/([0-9]{4})-'. // centuries & years CCYY-
                          '([0-9]{2})-'. // months MM-
                          '([0-9]{2})/';  // days DD
              if(preg_match($eregStr,$ISO8601,$regs))
              {
                $temp = strtotime("$regs[1]-$regs[2]-$regs[3] 0:0:0Z");
                return $temp;
              }
              else // Timestamp
              {
                $eregStr =  '(/[0-9]{9,10})/'; // Timestamp
                if(preg_match($eregStr,$ISO8601,$regs))
                {
                  return $regs[0];
                }
                else { echo '<span class="ErrorMessage">'.$L_ERROR_Invalid_date.' ('.$ISO8601.')</span><br />'; return false; }
              }
            }
          }
  }
  else return '';
}
 
function convertTimestampToISO8601 ($Timestamp)
{ 
  if ( $Timestamp>0 )
  {
    // PHP4
      $temp = date('Y-m-d\TH:i:sO', $Timestamp);
      return substr ( $temp, 0, 22 ).':'.substr ( $temp, 22 );
    // PHP5
      // return date('c',$Timestamp);
  }
  else return '';
}

function getNumerOfEventsFromTimestamp ($Timestamp)
{
  global $DB_PREFIX;
  // Diese Funktion optimieren, so dass sie sofort Ergebniss ausgibt, wenn zahl>1
  $sql = "SELECT count(*) as number FROM ".DB_PREFIX."gigs WHERE GigDate=".$Timestamp;
  $result = mysql_query($sql);
  $row = mysql_fetch_array($result);
  return $row['number'];
}
 
function getGigVarFromGigDate ($GigDate, $Var)
{
  global $DB_PREFIX;
  $SQL = "SELECT ".$Var." FROM ".DB_PREFIX."gigs WHERE GigDate='".$GigDate."' LIMIT 1";
  $result = mysql_query($SQL);
  if ( mysql_num_rows($result)==1)
  {
    $temp=mysql_fetch_assoc($result);
    return $temp[$Var];
  }
  else return false;
}
 
function getGigVarFromGigID ($GigID, $Var)
{
  global $DB_PREFIX;
  $SQL = "SELECT ".$Var." FROM ".DB_PREFIX."gigs WHERE GigID='".$GigID."' LIMIT 1";
  $result = mysql_query($SQL);
  if ( mysql_num_rows($result)==1)
  {
    $temp=mysql_fetch_assoc($result);
    return $temp[$Var];
  }
  else return false;
}
 
function changeGigIsPrivate ( $GigID )
{
  global $DB_PREFIX;
  // aktuellen GigStatus abfragen
  $sql = "SELECT GigIsPrivate FROM ".DB_PREFIX."gigs WHERE GigID='".$GigID."' LIMIT 1";
  $result = mysql_query($sql);
  if ( mysql_num_rows($result)==1)
  {
    $temp=mysql_fetch_assoc($result);
    $aktuellerGigStatus = $temp['GigIsPrivate'];
  }
  // neuen GigStatus setzen
  if ( $aktuellerGigStatus=='0' ) $neuerGigStatus = '1';
  if ( $aktuellerGigStatus=='1' ) $neuerGigStatus = '0';
  $DateOfLastEdit = date("U");
  $sql = "UPDATE ".DB_PREFIX."gigs SET GigIsPrivate = '".$neuerGigStatus."',
    GigDateOfLastEdit = '".$DateOfLastEdit."', GigLastEditor = '".getCurrentUserVar('UserName')."'
    WHERE GigID = '".$GigID."' LIMIT 1";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
    else return false;
}
 
function changeGigPublishing ( $GigID )
{
  global $DB_PREFIX;
  // aktuellen GigStatus abfragen
  $sql = "SELECT GigPublish FROM ".DB_PREFIX."gigs WHERE GigID='".$GigID."' LIMIT 1";
  $result = mysql_query($sql);
  if ( mysql_num_rows($result)==1)
  {
    $temp=mysql_fetch_assoc($result);
    $aktuellerGigStatus = $temp['GigPublish'];
  }
  // neuen GigStatus setzen
  if ( $aktuellerGigStatus=='0' ) $neuerGigStatus = '1';
  if ( $aktuellerGigStatus=='1' ) $neuerGigStatus = '0';
  $DateOfLastEdit = date("U");
  $sql = "UPDATE ".DB_PREFIX."gigs SET GigPublish = '".$neuerGigStatus."',
    GigDateOfLastEdit = '".$DateOfLastEdit."', GigLastEditor = '".getCurrentUserVar('UserName')."'
    WHERE GigID = '".$GigID."' LIMIT 1";
  $resultat = mysql_query($sql);
  if ($resultat) return true;
    else return false;
}
 
function getEventIsPublished ($GigID)
{
  global $DB_PREFIX;
  //$GigPublish = "";
  //$GigStatus = "";
  $SQL = "SELECT * FROM ".DB_PREFIX."gigs WHERE GigID = ".$GigID." LIMIT 1";
  $resultat = mysql_query($SQL);
  while ($row = mysql_fetch_array($resultat)) 
    extract($row);
  if  (
        ( 
          $GigStatus == 'Gig' 
          OR 
          ( $GigStatus == 'Option' AND getSetup('SetupFrontendAllowOptionalGigs') )
        ) 
        AND 
        ( !$GigIsPrivate OR getSetup('SetupFrontendAllowPrivateGigs') )
        AND
        $GigPublish == '1' 
     ) 
    return true;
  else return false;
} 
 
function getDateAndTimeFromTimestamp ($Timestamp)
{ 
  if ( $Timestamp != '0' AND $Timestamp != '' )
    return date(getSetup('SetupBackendDateFormat'), $Timestamp).' - '
      .date(getSetup('SetupBackendTimeFormat'), $Timestamp);
  else return '-';
} 
 
function checkICQNumber ($ICQNumber)
{ 
  if (preg_match('/^([0-9]{5,9}$)/', $ICQNumber)) return true;
    else return false;
} 
 
function logoutInactiveUser ()
{
  global $DB_PREFIX;
  $SetupUserMaximumInactivityTime = getSetup('SetupUserMaximumInactivityTime');
  if ( $SetupUserMaximumInactivityTime<1 ) $SetupUserMaximumInactivityTime = 1;
  $Diff = time()-($SetupUserMaximumInactivityTime*60);
  $sql="UPDATE ".DB_PREFIX."user SET UserSession=NULL WHERE UserLastActivityTimestamp<'".$Diff."'";
  mysql_query($sql);
}  

function updateUserActivity ()
{
  global $DB_PREFIX;
  $sql="UPDATE ".DB_PREFIX."user SET UserLastActivityTimestamp=".time()."
    WHERE UserName='".getCurrentUser()."' LIMIT 1";
  mysql_query($sql);
} 
 
function getNumberOfEventsInDatabase ()
{
  global $DB_PREFIX;
  $sql = "SELECT count(*) as number  FROM ".DB_PREFIX."gigs";
  $result = mysql_query($sql);
  $row = mysql_fetch_array($result);
  return $row['number'];
} 
 
function getNumberOfEventsInPeriod ( $Period, $GigStatus )
{
  global $DB_PREFIX;
  $TimestampFromToday = strtotime('today');
  $sql = "SELECT count(*) as number  FROM ".DB_PREFIX."gigs";
  if ( $Period!='all' OR $GigStatus!='all' ) $sql .= " WHERE";
  if ( $Period =="future" ) $sql .= " GigDate>=".$TimestampFromToday;
  if ( $Period =="past" ) $sql .= " GigDate<=".$TimestampFromToday;
  if ( $Period!='all' && $GigStatus!='all' ) $sql .= " AND";
  if ( $GigStatus!='all' ) $zweiterFilter = " GigStatus='".$GigStatus."'";
    else $zweiterFilter = "";
  $sql .= $zweiterFilter;
  $result = mysql_query($sql);
  $row = mysql_fetch_array($result);
  return $row['number'];
}
 
function getUserVar ($UserName, $Var)
{
  global $DB_PREFIX;
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $UserName = mysql_real_escape_string($UserName);
  $sql = "SELECT ".$Var." FROM ".DB_PREFIX."user WHERE UserName='".$UserName."' LIMIT 1";
  $result = mysql_query($sql);
  if ( mysql_num_rows($result)==1)
  {
    $temp=mysql_fetch_assoc($result);
    return $temp[$Var];
  }
  else return false;
}

function checkURL ($url)
{ 
  if ( !stristr($url, "http://") ) $url = "http://".$url;
  if ( preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url)
    OR ($url == "http://") )
    return true;
}

function getLocationString ( $GigCountry, $GigDistrict, $GigPostalCode, $GigCity )
{
  if ($GigPostalCode == "0") $GigPostalCode = "";
  $Location = "";
  if ( $GigCountry!='') $Location .= $GigCountry;
  if ( $GigCountry!='' && $GigPostalCode!='') $Location .= '-';
  if ( $GigPostalCode!='') $Location .= $GigPostalCode;
  if ( $GigCountry!='' && $GigPostalCode=='' && $GigCity!='' ) $Location .= ' ';
  if ( $GigPostalCode!='' && $GigCity!='') $Location .= ' ';
  if ( $GigCity!='') $Location .= $GigCity;
  if ( $GigDistrict!='') $Location .= ' ('.$GigDistrict.')';
  return $Location;
}

function getCurrentUserVar ($Var)
{
  global $DB_PREFIX;
  if ($Var>0 OR $Var!='')
  {
    $SQL = "SELECT ".$Var." FROM ".DB_PREFIX."user WHERE UserSession='".session_id()."' LIMIT 1";
    $result = mysql_query($SQL);
    if ($result)
    {
      if ( mysql_num_rows($result)==1)
      {
        $temp=mysql_fetch_assoc($result);
        return $temp[$Var];
      }
      else return false;
    }
    else return false;
  }
  else return false;
}

function checkGigDatum ($GigDateDay, $GigDateMonth, $GigDateYear)
{
  if ( checkdate($GigDateMonth, $GigDateDay, $GigDateYear) ) return true;
}

function startTime()
{
  $timeExplode = explode(" ", microtime());
  return $timeExplode[1] + $timeExplode[0];
}

function endTime($timer)
{
  $timeExplode = explode(" ", microtime());
  $time = $timeExplode[1] + $timeExplode[0];
  $finish = $time - $timer;
  return sprintf("%4.3f", $finish);
}

function checkEmail ($email)
{
  $email = strtolower($email);
  if (preg_match('/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/', $email)
    AND isEmailTLDValid($email)
    AND isEmailDomainValid($email))
    return true;
}

function GetTooltip($Timestamp)
{
  global $DB_PREFIX;
  $Tooltip = "";
  $SQL = "SELECT * FROM ".DB_PREFIX."gigs WHERE GigDate = ".$Timestamp;
  $resultat = mysql_query($SQL);
  while ($row = mysql_fetch_array($resultat))
  {
    extract($row);
    $VenueCountry = getVenueVarFromVenueID ($GigVenueID, 'VenueCountry');
    $VenueDistrict = getVenueVarFromVenueID ($GigVenueID, 'VenueDistrict');
    $VenuePostalCode = getVenueVarFromVenueID ($GigVenueID, 'VenuePostalCode');
    $VenueCity = getVenueVarFromVenueID ($GigVenueID, 'VenueCity');
    $ArtistName = getArtistVarFromArtistID ($GigArtistID, 'ArtistName');
    $Tooltip = getLocationString($VenueCountry, $VenueDistrict, $VenuePostalCode, $VenueCity);
    if ( $ArtistName != '') $Tooltip .= '&#10;'.$ArtistName;
    if ( $GigEvent != '') $Tooltip .= '&#10;'.$GigEvent;
  }
  $Tooltip = str_replace('"', '&quot;', $Tooltip);
  $Tooltip = str_replace('<p>', '&#10;', $Tooltip);
  $Tooltip = preg_replace("/\r|\n/s", "", $Tooltip);
  return strip_tags($Tooltip);
}

function GigsPublished( $Zeitraum )
{
  global $DB_PREFIX;
  $TimestampFromToday = strtotime('today');
  $SQL = "SELECT * FROM ".DB_PREFIX."gigs WHERE GigStatus='Gig' OR GigStatus='Option' AND
    GigPublish=1";
  $resultat = mysql_query($SQL);
  $temp=0;
  while ($row = mysql_fetch_array($resultat))
  {
    extract($row);
    if ( $Zeitraum == 'future' AND $GigDate >= $TimestampFromToday ) $temp=1;
    elseif ( $Zeitraum == 'past' AND $GigDate < $TimestampFromToday ) $temp=1;
    elseif ( $Zeitraum == 'all' AND $GigDate > '0' ) $temp=1;
  }
  if ($temp==1) return true;
  else return false;
}

function getSetup ($Var)
{
  global $DB_PREFIX;
  $SQL="SELECT ".$Var." FROM ".DB_PREFIX."setup WHERE SetupID=1 LIMIT 1";
  $result = mysql_query($SQL);
  if ($result)
  {
    if ( mysql_num_rows($result)==1)
    {
      $temp=mysql_fetch_assoc($result);
      return $temp[$Var];
    }
    else return false;
  }
  else return false;
}

function transformPassword ($Password)
{
  $anzahlZeichen = strlen($Password);
  for ($i=0; $i<$anzahlZeichen; $i++) echo "*";
}

function testDoppelteGigs ($GigDateDay, $GigDateMonth, $GigDateYear)
{
  global $DB_PREFIX;
  $Timestamp = strtotime($GigDateYear.'-'.$GigDateMonth.'-'.$GigDateDay);
  $doppelteVorhanden = false;
  //$db_select = @mysql_select_db($DB_NAME);
  $sql = 'SELECT * FROM ' .$prefix."gigs";
  $result = mysql_query($sql);
  while($row = mysql_fetch_row($result))
    if (strtolower($row[1]) == $Timestamp) $doppelteVorhanden = true;
  if ($doppelteVorhanden) return true;
  else return false;
}

function IsUserUser ()
{
  global $DB_PREFIX;
  $sql="SELECT UserStatus FROM ".DB_PREFIX."user WHERE UserSession='".session_id()."'";
  $result= mysql_query($sql);
  $temp = mysql_fetch_assoc($result);
  if ($temp['UserStatus'] == "User") return true;
  else return false;
}

function IsUserAdmin ()
{
  global $DB_PREFIX;
  $sql='SELECT UserStatus FROM '.DB_PREFIX.'user WHERE UserSession="'.session_id().'"';
  $result = mysql_query($sql);
  $temp = mysql_fetch_assoc($result);
  if ($temp['UserStatus'] == "Admin") return true;
  else return false;
}

function logout()
{
  global $DB_PREFIX;
  $sql="UPDATE ".DB_PREFIX."user SET UserSession=NULL WHERE UserSession='".session_id()."'";
  mysql_query($sql);
  session_name('GigKalender');
  session_destroy();
}

function login ( $userID )
{
  global $DB_PREFIX;
  $sql="UPDATE ".DB_PREFIX."user SET UserSession='".session_id()."' WHERE UserId=".$userID;
  mysql_query($sql);
}

function loggedIn()
{
  global $DB_PREFIX;
  $sql="SELECT UserID FROM ".DB_PREFIX."user WHERE UserSession='".session_id()."' LIMIT 1";
  $result= mysql_query($sql);
  if ($result) return ( mysql_num_rows($result)==1);
    else return false;
}

function getUID ( $UserName, $UserCurrentPassword )
{
  global $DB_PREFIX;
  $SQL="SELECT UserId FROM ".DB_PREFIX."user WHERE UserName='".$UserName."'
    AND UserCurrentPassword='".$UserCurrentPassword."' LIMIT 1";
  $result = mysql_query($SQL);
  if ($result)
  {
    if ( mysql_num_rows($result)==1)
    {
      $user=mysql_fetch_assoc($result);
      return $user['UserId'];
    }
    else return false;
  }
  else return false;
}

function getCurrentUser ()
{
  global $DB_PREFIX;
  $SQL="SELECT UserName FROM ".DB_PREFIX."user WHERE UserSession='".session_id()."' LIMIT 1";
  $result = mysql_query($SQL);
  if ( mysql_num_rows($result)==1)
  {
    $temp = mysql_fetch_assoc($result);
    return $temp['UserName'];
  }
  else return false;  
}

function addUser ($UserName, $UserStatus, $UserCurrentPassword, $UserEmailadress, $UserSession,
  $UserLanguage, $UserLastActivityTimestamp, $UserBackendCalenderListCols, $UserRightEmail,
  $UserRightAddNews, $UserRightEditNews, $UserRightDeleteNews, $UserCharsCalenderListFields,
  $UserCharsGuestbookListFields, $UserEmailCopyToOwnAdress, $UserFirstDayOfWeek)
{
  global $DB_PREFIX;
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $UserName = mysql_real_escape_string($UserName);
    $UserCurrentPassword = mysql_real_escape_string($UserCurrentPassword);
    $UserEmailadress = mysql_real_escape_string($UserEmailadress);
  // Leere numerische Variablen als NULL definieren
    if ( $UserLastActivityTimestamp=='' ) $UserLastActivityTimestamp="NULL";
    if ( $UserRightEmail=='' ) $UserRightEmail=0;
    if ( $UserRightAddNews=='' ) $UserRightAddNews=0;
    if ( $UserRightEditNews=='' ) $UserRightEditNews=0;
    if ( $UserRightDeleteNews=='' ) $UserRightDeleteNews=0;
    if ( $UserCharsCalenderListFields=='' ) $UserCharsCalenderListFields=10;
    if ( $UserEmailCopyToOwnAdress=='' ) $UserEmailCopyToOwnAdress=1;
  if (!getUserAlreadyKnown($UserName))
  {
    $SQL = "INSERT INTO ".DB_PREFIX."user (UserName, UserStatus, UserCurrentPassword,
      UserEmailadress, UserSession, UserLanguage, UserLastActivityTimestamp,
      UserBackendCalenderListCols, UserRightEmail, UserRightAddNews, UserRightEditNews,
      UserRightDeleteNews, UserCharsCalenderListFields, UserCharsGuestbookListFields,
      UserEmailCopyToOwnAdress, UserFirstDayOfWeek) VALUES ('$UserName', '$UserStatus',
      '$UserCurrentPassword', '$UserEmailadress', '$UserSession', '$UserLanguage',
      $UserLastActivityTimestamp, '$UserBackendCalenderListCols', $UserRightEmail,
      $UserRightAddNews, $UserRightEditNews, $UserRightDeleteNews,
      '$UserCharsCalenderListFields', $UserCharsGuestbookListFields,
      $UserEmailCopyToOwnAdress, '$UserFirstDayOfWeek')";
    $resultat = mysql_query($SQL);
    if ($resultat) return true;
    else return false;
  }
  else return false;
}

function getUserAlreadyKnown ($Username)
{
  global $DB_PREFIX;
  $string = strtolower($Username);
  $db_select = @mysql_select_db($DB_NAME);
  $sql = 'SELECT * FROM ' .DB_PREFIX."user";
  $result = mysql_query($sql);
  $doppelteVorhanden = false;
  $result = mysql_query($sql);
  $menge = mysql_num_rows($result);
  while($row = mysql_fetch_row($result))
    if (strtolower($row[1]) == $string) $doppelteVorhanden = true;
  if ($doppelteVorhanden) return true;
  else return false;
}

function getDatabasePresent ()
{
  global $DB_NAME;
  if (mysql_select_db(DB_NAME)) return true;
    else return false;
}

function addGig ( $Gig, $Job )
{
  global $DB_PREFIX;
  global $L_042;
  global $L_107;
  $amountAttributes = count($Gig);
  for ($i=0; $i<$amountAttributes; $i++)
  {
    if ( $i!=13 ) // Notizen werden ausgeklammert
      $Gig[$i]=strip_tags($Gig[$i]);
    // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
      $Gig[$i] = mysql_real_escape_string($Gig[$i]);
  }
  list( $GigID, $GigDateDay, $GigDateMonth, $GigDateYear, $GigEvent, $GigEventURL, $GigEntranceFee,
        $GigLineup, $GigVisitors, $GigStageBuilding, $GigBeginning, $GigDuration, $GigEnding,
        $GigNotes, $GigStatus, $GigPublish, $GigIsPrivate, $GigDateOfLastEdit, $GigLastEditor,
        $GigAddedFrom, $GigDateOfAdding, $GigVenueID, $GigOrganizerID, $GigArtistID )
        = $Gig;
  if ( $GigEventURL=='http://' ) $GigEventURL='';
  if ( $GigEventURL!="" AND !stristr( $GigEventURL, 'http://' ) )
    $GigEventURL = 'http://'.$GigEventURL;
  // Abschließendes Slash bei URLs entfernen
    $GigEventURL = getURLWithoutFinalSlash ($GigEventURL);
  // Leere numerische Variablen als NULL definieren
    if ( $GigPublish=='' ) $GigPublish=0;
    if ( $GigIsPrivate=='' ) $GigIsPrivate=0;
    if ( $GigDateOfLastEdit=='' ) $GigDateOfLastEdit="NULL";
    if ( $GigDateOfAdding=='' ) $GigDateOfAdding="NULL";
    if ( $GigVenueID=='' ) $GigVenueID="NULL";
    if ( $GigOrganizerID=='' ) $GigOrganizerID="NULL";
    if ( $GigArtistID=='' ) $GigArtistID="NULL";
  $GigDateNew = strtotime($GigDateYear.'-'.$GigDateMonth.'-'.$GigDateDay);
  if ( $Job=="add" )
  {
    if ($GigDateNew != -1)
    {
        $SQL = "INSERT INTO ".DB_PREFIX."gigs
        (
          GigDate, GigEvent, GigEventURL, GigEntranceFee, GigLineup, GigVisitors, GigStageBuilding,
          GigBeginning, GigDuration, GigEnding, GigNotes, GigStatus, GigPublish, GigIsPrivate,
          GigDateOfLastEdit, GigLastEditor, GigAddedFrom, GigDateOfAdding, GigVenueID,
          GigOrganizerID, GigArtistID
        )
        VALUES
        (
          '$GigDateNew', '$GigEvent', '$GigEventURL', '$GigEntranceFee', '$GigLineup',
          '$GigVisitors', '$GigStageBuilding', '$GigBeginning', '$GigDuration', '$GigEnding',
          '$GigNotes', '$GigStatus', $GigPublish, $GigIsPrivate, $GigDateOfLastEdit,
          '$GigLastEditor', '$GigAddedFrom', $GigDateOfAdding, $GigVenueID, $GigOrganizerID,
          $GigArtistID)";
        $resultat = mysql_query($SQL);
        if ($resultat) { deleteFileInCache($GigDateNew); return true; }
        else return false;
    }
    else
    {
      echo "ERROR: The date is wrong! (Timestamp -1) ".$GigDateYear.'-'.$GigDateMonth.'-'.$GigDateDay."<br />";
      return false;
    }
  }
  if ( $Job=="edit" )
  {
    if (  getGigVarFromGigID ($GigID, 'GigDate') == $GigDateNew)
    {
      $GigLastEditor = getCurrentUserVar("UserName");
      $GigDateOfLastEdit = date("U");
      $SQL = "UPDATE ".DB_PREFIX."gigs SET
        GigEvent = '$GigEvent', GigEventURL = '$GigEventURL', GigEntranceFee = '$GigEntranceFee',
        GigLineup = '$GigLineup', GigVisitors = '$GigVisitors',
        GigStageBuilding = '$GigStageBuilding', GigBeginning = '$GigBeginning',
        GigDuration = '$GigDuration', GigEnding = '$GigEnding', GigNotes = '$GigNotes',
        GigStatus = '$GigStatus', GigPublish = $GigPublish, GigIsPrivate = $GigIsPrivate,
        GigDateOfLastEdit = $GigDateOfLastEdit, GigLastEditor = '".getCurrentUserVar('UserName')."',
        GigVenueID = $GigVenueID, GigOrganizerID = $GigOrganizerID, GigArtistID = 'GigArtistID
        WHERE GigID = $GigID LIMIT 1";
      $resultat = mysql_query($SQL);
      if ($resultat) { deleteFileInCache($GigDateNew); return true; }
      else return false;
    }
    else
    {
      $GigDateOld = getGigVarFromGigID ($GigID, 'GigDate');
      $SQL = "UPDATE ".DB_PREFIX."gigs SET
        GigDate = $GigDateNew, GigEvent = '$GigEvent', GigEventURL = '$GigEventURL',
        GigEntranceFee = '$GigEntranceFee', GigLineup = '$GigLineup', GigVisitors = '$GigVisitors',
        GigStageBuilding = '$GigStageBuilding', GigBeginning = '$GigBeginning',
        GigEnding = '$GigEnding', GigNotes = '$GigNotes', GigStatus = '$GigStatus',
        GigPublish = $GigPublish, GigIsPrivate = $GigIsPrivate,
        GigDateOfLastEdit = $GigDateOfLastEdit, GigVenueID = $GigVenueID,
        GigOrganizerID = $GigOrganizerID, GigArtistID = $GigArtistID
        WHERE GigID = $GigID LIMIT 1";
      $resultat = mysql_query($SQL);
      if ($resultat)
      {
        deleteFileInCache($GigDateOld);
        deleteFileInCache($GigDateNew);
        echo $L_107."<br />";
        return true;
      }
      else return false;
    }
  }
}

function editUser ($UserName, $UserStatus, $UserCurrentPassword, $UserEmailadress, $UserNameOld,
  $UserLanguage, $UserBackendCalenderListCols, $UserRightEmail, $UserRightAddNews,
  $UserRightEditNews, $UserRightDeleteNews, $UserFirstDayOfWeek)
{
  global $DB_PREFIX;
  // HTML-Tags entfernen
    $UserName = strip_tags($UserName);
    $UserCurrentPassword = strip_tags($UserCurrentPassword);
    $UserEmailadress = strip_tags($UserEmailadress);
    $UserNameOld = strip_tags($UserNameOld);
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $UserName = mysql_real_escape_string($UserName);
    $UserCurrentPassword = mysql_real_escape_string($UserCurrentPassword);
    $UserEmailadress = mysql_real_escape_string($UserEmailadress);
    $UserNameOld = mysql_real_escape_string($UserNameOld);
  // Leere numerische Variablen als NULL definieren
    if ( $UserRightEmail=='' ) $UserRightEmail=0;
    if ( $UserRightAddNews=='' ) $UserRightAddNews=0;
    if ( $UserRightEditNews=='' ) $UserRightEditNews=0;
    if ( $UserRightDeleteNews=='' ) $UserRightDeleteNews=0;
    // später aktivieren: if ( $UserEmailCopyToOwnAdress=='' ) $UserEmailCopyToOwnAdress=1;
  $SQL = "UPDATE ".DB_PREFIX."user SET
    UserName = '$UserName', UserStatus = '$UserStatus',
    UserCurrentPassword = '$UserCurrentPassword', UserEmailadress = '$UserEmailadress',
    UserLanguage = '$UserLanguage', UserBackendCalenderListCols = '$UserBackendCalenderListCols',
    UserRightEmail = $UserRightEmail, UserRightAddNews = $UserRightAddNews, UserRightEditNews = $UserRightEditNews,
    UserRightDeleteNews = $UserRightDeleteNews, UserFirstDayOfWeek = '$UserFirstDayOfWeek'
    WHERE UserName = '$UserNameOld'
    LIMIT 1";
  $resultat = mysql_query($SQL);
  if ($resultat) return true;
  else return false;
}

function editCurrentUser ($UserCurrentPassword, $UserEmailadress, $UserLanguage, $UserFirstDayOfWeek )
{
  global $DB_PREFIX;
  // Cache löschen
    if ( $UserFirstDayOfWeek!=getCurrentUserVar('UserFirstDayOfWeek')) echo 'Cache leeren<br />';
  // HTML- und PHP-Tags entfernen
    $UserCurrentPassword = strip_tags($UserCurrentPassword);
    $UserEmailadress = strip_tags($UserEmailadress);
  // Maskieren spezieller Zeichen für die Verwendung in einer SQL-Anweisung
    $UserCurrentPassword = mysql_real_escape_string($UserCurrentPassword);
    $UserEmailadress = mysql_real_escape_string($UserEmailadress);
  $SQL = "UPDATE ".DB_PREFIX."user SET UserCurrentPassword = '$UserCurrentPassword',
    UserEmailadress = '$UserEmailadress', UserLanguage = '$UserLanguage',
    UserFirstDayOfWeek = '$UserFirstDayOfWeek' WHERE UserName = '".getCurrentUser()."'
    LIMIT 1";
  $resultat = mysql_query($SQL);
  if ($resultat) return true;
  else return false;
}

function getDateIsInFuture ($Timestamp)
{
  if ($Timestamp >= strtotime('today')) return true;
    else return false;
}

//function getGigTypeOfTimestamp ($Timestamp)
//{
//  global $DB_PREFIX;
//  $GigStatus = "";
//  $SQL = "SELECT * FROM ".DB_PREFIX."gigs WHERE GigDate = ".$Timestamp." LIMIT 1";
//  $resultat = mysql_query($SQL);
//  while ($row = mysql_fetch_array($resultat))
//    extract($row);
//  if ($GigStatus!='') return $GigStatus;
//    else return 'Normal';
//}

function getNumberOfDaysInMonth ($Month, $Year)
{
  return date('t', mktime(0, 0, 0, $Month, 1 , $Year));
}

function getWeekdayNameFromDate ($Day, $Month, $Year)
{
  return date('l', mktime(0, 0, 0, $Month, $Day, $Year));
}

function getWeekdayFromDate ($Day, $Month, $Year)
{
  $weekday = date('w', mktime(0, 0, 0, $Month, $Day, $Year));
  if($weekday == 0) $weekday = 7;
  return $weekday;
}

function getMonthNameFromMonthNumber ($MonthNumber)
{
  $var = 'L_'.date('F', strtotime('2011-'.$MonthNumber.'-1'));
  global ${$var};
  return ${$var};
}
?>