<?php
/*=======================================================================
 *
 * download.php
 *
 =======================================================================*/


include_once ("../include/mc_gen_include.inc.php");
require_once "DB.php";


// DB-Config
$dbhost = "172.16.0.111:3711";  // EXTERNAL_DB_METAOBJECT
$dblogin = "phoenix";
$dbpassword = "AdAdgkS13";
$dbname = "phoenix";
$dsn = "mysql://$dblogin:$dbpassword@$dbhost/$dbname";
$db = DB::connect($dsn, false);
if (DB::isError($db))
    die ("$PHP_SELF: " . $db->getMessage());
$db->setFetchMode(DB_FETCHMODE_ASSOC);
$db->query('SET NAMES latin1');


// Current timestamp
$currentTime = date("Y-m-d H:i:s");

$clientIP = trim($_SERVER['REMOTE_ADDR']);

$maxDownloads = 2;

// DEFINE VERSIONS HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$arr1 = array(); $arr2 = array(); $arr3 = array();
$arr1[0] = "HTM_333";        $arr2[0] = "htm__3.33.apk";     $arr3[0] = "POCO App (=TPS), V3.33";
$arr1[1] = "HTMV_330";       $arr2[1] = "htmv_.0330.apk";    $arr3[1] = "IKEA App (=votian), V3.30";
$arr1[2] = "HTMV_332d";      $arr2[2] = "htmv__3.32d.apk";   $arr3[2] = "IKEA App (=votian), V3.32d";
$arr1[3] = "SB_HTGT_334a";   $arr2[3] = "sb_3.34a.apk";      $arr3[3] = "SB / HTGT, V3.34a";

$arr1Len = count($arr1);


$access = true;
$isMobile = false;
if (substr($clientIP,0,7) == "10.243.") :

   $access = false;
   $isMobile = true;

   // Check individual counter
   $clientArray = getColVectorFromDB2ArrayByClause("temp.app_download", "appd_cnt", "appd_ip = '" . $clientIP . "'", "appd_ver", "", "");

   if ($arr1Len == 0) :
      $access = true;
   else :
     for ($i = 0; $i < $arr1Len; $i++) :
         if ($clientArray[$arr1[$i]] < $maxDownloads) :
            $access = true;
            break;
         endif;
     endfor;
   endif;
endif;
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="de">
<head>
<title>App-Download</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content=""> <meta name="keywords" content="">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
<link rel="stylesheet" type="text/css" href="../css/phoenix.css"> 

<?php include_once ("../include/js_framework.inc.php"); ?>
<script src="../js/checkFormTags.js" type="text/javascript"></script>

<script type="text/javascript">
<!--

// JQuery.Request (GET)
function ajaxRequestGet(url, data) {
  $.ajax({
     type: "GET",
     url: url,
     data: data,
     async: false,
     cache: false,
     success: function(msg){eval(msg);}
  });
}

function versionURL(db_item,filename) {
	<?php if ($isMobile) : ?>
	  ajaxRequestGet('../web_download/ajaxDownloader.php', 'mode=0&clientIP=<?php echo $clientIP ?>&clientVersion=' + db_item);
	  if (cnt < <?php echo $maxDownloads ?>) {
       ajaxRequestGet('../web_download/ajaxDownloader.php', 'mode=1&clientIP=<?php echo $clientIP ?>&clientVersion=' + db_item);
       $('#' + db_item).click(function(e) { e.preventDefault(); });
       var specialURL = filename;
       document.location.href= '../' + specialURL;
    }
  <?php else : ?>
       ajaxRequestGet('../web_download/ajaxDownloader.php', 'mode=1&clientIP=<?php echo $clientIP ?>&clientVersion=' + db_item);
       $('#' + db_item).click(function(e) { e.preventDefault(); });
       var specialURL = filename;
       document.location.href= '../' + specialURL;
  <?php endif; ?>
}

-->
</script>


</head>

<body>
	<div style="width:400px; height:50px;">
		&nbsp;&nbsp;&nbsp;&nbsp;
  </div>
  	
	<div style="float:left; width:70px;">
		&nbsp;
  </div>

  <div style="float:left; width:320px;">
  	
    <div style="width:320px; height:100px; text-align:center;">
       <img src="../logo.png" height=100px width=100px alt="Logo">
    </div>
    
    <?php if (!$isMobile || $access) : ?>

      <?php for ($i = 0; $i < $arr1Len; $i++) : ?>
    
              <?php if (!$isMobile || $clientArray[$arr1[$i]] == "" || $clientArray[$arr1[$i]] < $maxDownloads) : ?>  
              <div style="width:320px; height:50px; text-align:center;">
              <h2><a id="<?php echo $arr1[$i] ?>" href="javascript:versionURL('<?php echo $arr1[$i] ?>','<?php echo $arr2[$i] ?>');"><?php echo $arr3[$i] ?></a></h2>
              </div>
            <?php endif; ?>
    
      <?php endfor; ?>
        
    <?php else :  ?>
    
	    <div style="width:320px; height:50px; text-align:center;">
	      <h2>Bitte nutzen Sie einen</h2><br>
	      <h2>WLAN-Zugang zum Download!</h2><br><br>
	      <h2>Vielen Dank!</h2>
	    </div>
    
    <?php endif;  ?>

  </div>
</body>
</html>
