OsmAnd/config/site/download.php

132 lines
3.6 KiB
PHP
Raw Normal View History

2011-05-06 12:33:45 +02:00
<?php
2012-01-14 01:24:22 +01:00
include 'autoload.php';
use UnitedPrototype\GoogleAnalytics;
2012-01-14 00:59:59 +01:00
2011-05-06 12:33:45 +02:00
function downloadFile($filename) {
if (!file_exists($filename)) {
header('HTTP/1.0 404 Not Found');
//die('File doesn\'t exist');
die(1);
}
2011-05-06 12:33:45 +02:00
$from=0;
$cr=NULL;
$to=filesize($filename) - 1;
if (isset($_SERVER['HTTP_RANGE'])) {
list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
list($range) = explode(",",$range,2);
list($from, $range_end) = explode("-", $range);
$from=intval($from);
if($range_end) {
$to=intval($range_end);
}
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $from . '-' . $to.'/'.filesize($filename));
} else {
header('HTTP/1.1 200 Ok');
}
$size= $to - $from + 1;
header('Accept-Ranges: bytes');
header('Content-Length: ' . $size);
header('Connection: close');
header('Content-Type: application/octet-stream');
header('Last-Modified: ' . gmdate('r', filemtime($filename)));
$f=fopen($filename, 'r');
header('Content-Disposition: attachment; filename="' . basename($filename) . '";');
if ($from) fseek($f, $from, SEEK_SET);
$downloaded=0;
while(!feof($f) and !connection_status() and ($downloaded<$size)) {
$part = min(512000, $size-$downloaded);
echo fread($f, $part);
$downloaded+=$part;
flush();
}
fclose($f);
}
function url_exists($url) {
$hdrs = @get_headers($url);
return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false;
}
function update_count_of_downloads($file) {
try {
$xml = simplexml_load_file("download_stat.xml");
$res = $xml->xpath('//download[@name="'.$file.'"]');
if (count($res) > 0) {
$node = $res[0];
$node["count"] = intval($node["count"]) + 1;
} else {
$obj = $xml-> addChild("download");
$obj -> addAttribute("name", $file);
$obj -> addAttribute("count", "1");
}
$xml->asXML("download_stat.xml");
//fclose($xml);
} catch(Exception $e) {
}
}
if(!isset($_GET['file']) ) {
header('HTTP/1.0 404 Not Found');
die(1);
}
2011-05-06 12:33:45 +02:00
$file = $_GET['file'];
2012-01-08 14:04:42 +01:00
// not used now
2012-01-14 00:17:28 +01:00
if(!isset($_SERVER['HTTP_RANGE']) ) {
// old version
// update_count_of_downloads($file) ;
2012-04-20 11:00:05 +02:00
if (!isset($_GET['event'])) {
$eventno = 1;
} else {
$eventno = $_GET['event'];
}
2012-04-20 11:00:05 +02:00
if (isset($_GET['osmandver'])) {
$app = $_GET['osmandver'];
} else {
$app = 'Download '.$_SERVER['HTTP_USER_AGENT'];
}
2012-01-14 00:59:59 +01:00
2012-01-14 00:18:32 +01:00
$tracker = new GoogleAnalytics\Tracker('UA-28342846-1', 'download.osmand.net');
2012-01-14 00:17:28 +01:00
$visitor = new GoogleAnalytics\Visitor();
2012-01-14 01:24:22 +01:00
$visitor->setIpAddress($_SERVER['REMOTE_ADDR']);
2012-01-14 00:17:28 +01:00
$visitor->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$visitor->setScreenResolution('1024x768');
// Assemble Session information
// (could also get unserialized from PHP session)
$session = new GoogleAnalytics\Session();
// Assemble Page information
$page = new GoogleAnalytics\Page('/download.php?'.$file);
$page->setTitle('Download file '.$file);
// Track page view
$tracker->trackPageview($page, $session, $visitor);
$event = new GoogleAnalytics\Event($app, 'App', $file, $eventno);
$tracker->trackEvent($event, $session, $visitor);
2012-01-14 00:17:28 +01:00
}
2011-05-06 12:33:45 +02:00
set_time_limit(0);
2012-01-08 14:04:42 +01:00
$xml = simplexml_load_file("indexes.xml");
2012-01-08 14:53:47 +01:00
$res = $xml->xpath('//region[@name="'.$file.'"]');
2012-01-08 14:04:42 +01:00
if (count($res) > 0) {
$node = $res[0];
if($node["local"]) {
downloadFile('indexes/'.$file);
} else {
header('HTTP/1.1 302 Found');
header('Location: http://osmand.googlecode.com/files/'.$file);
}
2011-05-06 12:33:45 +02:00
} else {
header('HTTP/1.1 302 Found');
header('Location: http://osmand.googlecode.com/files/'.$file);
}
?>