Blame view

php/downloadPlot.php 2.39 KB
37e81bff   Benjamin Renard   Direct save of pl...
1
2
3
4
5
6
7
8
9
10
11
12
<?php

require_once 'config.php';

if (!isset($_POST['sessionId']))
{
	header("HTTP/1.0 400 Bad Request");
	echo json_encode(array("success" => false, "error" => "Unknown session Id"));
	exit;
}
$sessionId = $_POST['sessionId'];

6abb8fd7   Benjamin Renard   Fix save plot
13
if (!isset($_POST['interactiveId']))
37e81bff   Benjamin Renard   Direct save of pl...
14
15
{
	header("HTTP/1.0 400 Bad Request");
6abb8fd7   Benjamin Renard   Fix save plot
16
	echo json_encode(array("success" => false, "error" => "Unknown interactive Id"));
37e81bff   Benjamin Renard   Direct save of pl...
17
18
	exit;
}
6abb8fd7   Benjamin Renard   Fix save plot
19
$interactiveId     = $_POST['interactiveId'];
37e81bff   Benjamin Renard   Direct save of pl...
20

6e1e1bf4   Benjamin Renard   Fix some bugs wit...
21
$preview = empty($_POST['preview']) ? FALSE : ($_POST['preview'] == "true");
37e81bff   Benjamin Renard   Direct save of pl...
22

6e1e1bf4   Benjamin Renard   Fix some bugs wit...
23
$preview_function = empty($_POST['function']) ? FALSE : ($_POST['function'] == "true");
ebafe59e   Benjamin Renard   Add the possibili...
24

6e1e1bf4   Benjamin Renard   Fix some bugs wit...
25
26
27
download_plot($sessionId, $interactiveId, $preview, $preview_function);

function download_plot($sessionId, $interactiveId, $preview, $preview_function)
37e81bff   Benjamin Renard   Direct save of pl...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
	// Must be fresh start
	if( headers_sent() )
	{
		header("HTTP/1.0 400 Bad Request");
		echo json_encode(array("success" => false, "error" => "Headers Sent"));
		return;
	}

	// Required for some browsers
	if(ini_get('zlib.output_compression'))
		ini_set('zlib.output_compression', 'Off');

	//Build file path
ebafe59e   Benjamin Renard   Add the possibili...
42
	if (!$preview) {
6abb8fd7   Benjamin Renard   Fix save plot
43
		$fullPath = USERPATH."/".$sessionId."/RES/Plot_/".$interactiveId.".png";
ebafe59e   Benjamin Renard   Add the possibili...
44
	}
6e1e1bf4   Benjamin Renard   Fix some bugs wit...
45
	else if (!$preview_function) {
6abb8fd7   Benjamin Renard   Fix save plot
46
		$fullPath = USERPATH."/".$sessionId."/RES/Plot_/".str_replace('plot_','instant',$interactiveId).".png";
ebafe59e   Benjamin Renard   Add the possibili...
47
	}
6e1e1bf4   Benjamin Renard   Fix some bugs wit...
48
49
50
51
	else {
		$fullPath = USERPATH."/".$sessionId."/RES/Plot_/".str_replace('plot_','plotFunction',$interactiveId).".png";
	}

37e81bff   Benjamin Renard   Direct save of pl...
52
53
54
55
56
57
58
59
60
61
62
63
64
	// File Exists?
	if( file_exists($fullPath) ){

		// Parse Info / Get Extension
		$fsize = filesize($fullPath);
		$path_parts = pathinfo($fullPath);
		$ctype="image/png";
		
		header("Pragma: public"); // required
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: private",false); // required for certain browsers
		header("Content-Type: $ctype");
c1d7bcd8   Erdogan Furkan   11287 - Done.
65
66
67
68
69
70
		$newName ;
		preg_match('/(\d+)/', basename($fullPath), $matches);
		if (isset($matches[0])) {
			$newName = str_replace('.png', '_'.date('YmdHis').'.png', str_replace($matches[0], (string)$matches[0]+1, basename($fullPath)));
		}
		header("Content-Disposition: attachment; filename=\"".$newName."\";" );
37e81bff   Benjamin Renard   Direct save of pl...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: ".$fsize);
		ob_clean();
		flush();
		readfile( $fullPath );

	} else
	{
		header("HTTP/1.0 400 Bad Request");
		echo json_encode(array("success" => false, "error" => "No existing plot"));
		return;
	}
}

ebafe59e   Benjamin Renard   Add the possibili...
85
?>