~jstys-z/helioviewer.org/timeline

« back to all changes in this revision

Viewing changes to timeline/Highstock-1.3.10/exporting-server/php/php-batik/index.php

  • Committer: Jeff Stys
  • Date: 2014-04-21 12:46:26 UTC
  • Revision ID: jstys@sesda3.com-20140421124626-2332pb2dyjc33jxi
Proof-of-concept version of Data Coverage Timeline using Highchart/Highstock javascript library.  Changes to getDataCoverage API in order to feed the necessary data to the Timeline

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * This file is part of the exporting module for Highcharts JS.
 
4
 * www.highcharts.com/license
 
5
 * 
 
6
 *  
 
7
 * Available POST variables:
 
8
 *
 
9
 * $filename  string   The desired filename without extension
 
10
 * $type      string   The MIME type for export. 
 
11
 * $width     int      The pixel width of the exported raster image. The height is calculated.
 
12
 * $svg       string   The SVG source code to convert.
 
13
 */
 
14
 
 
15
 
 
16
// Options
 
17
define ('BATIK_PATH', 'batik-rasterizer.jar');
 
18
 
 
19
///////////////////////////////////////////////////////////////////////////////
 
20
ini_set('magic_quotes_gpc', 'off');
 
21
 
 
22
$type = $_POST['type'];
 
23
$svg = (string) $_POST['svg'];
 
24
$filename = (string) $_POST['filename'];
 
25
 
 
26
// prepare variables
 
27
if (!$filename or !preg_match('/^[A-Za-z0-9\-_ ]+$/', $filename)) {
 
28
        $filename = 'chart';
 
29
}
 
30
if (get_magic_quotes_gpc()) {
 
31
        $svg = stripslashes($svg);      
 
32
}
 
33
 
 
34
// check for malicious attack in SVG
 
35
if(strpos($svg,"<!ENTITY") !== false || strpos($svg,"<!DOCTYPE") !== false){
 
36
        exit("Execution is topped, the posted SVG could contain code for a malicious attack");
 
37
}
 
38
 
 
39
$tempName = md5(rand());
 
40
 
 
41
// allow no other than predefined types
 
42
if ($type == 'image/png') {
 
43
        $typeString = '-m image/png';
 
44
        $ext = 'png';
 
45
        
 
46
} elseif ($type == 'image/jpeg') {
 
47
        $typeString = '-m image/jpeg';
 
48
        $ext = 'jpg';
 
49
 
 
50
} elseif ($type == 'application/pdf') {
 
51
        $typeString = '-m application/pdf';
 
52
        $ext = 'pdf';
 
53
 
 
54
} elseif ($type == 'image/svg+xml') {
 
55
        $ext = 'svg';
 
56
 
 
57
} else { // prevent fallthrough from global variables
 
58
        $ext = 'txt';
 
59
}
 
60
 
 
61
$outfile = "temp/$tempName.$ext";
 
62
 
 
63
if (isset($typeString)) {
 
64
        
 
65
        // size
 
66
        $width = '';
 
67
        if ($_POST['width']) {
 
68
                $width = (int)$_POST['width'];
 
69
                if ($width) $width = "-w $width";
 
70
        }
 
71
 
 
72
        // generate the temporary file
 
73
        if (!file_put_contents("temp/$tempName.svg", $svg)) { 
 
74
                die("Couldn't create temporary file. Check that the directory permissions for
 
75
                        the /temp directory are set to 777.");
 
76
        }
 
77
        
 
78
        // do the conversion
 
79
        $output = shell_exec("java -jar ". BATIK_PATH ." $typeString -d $outfile $width temp/$tempName.svg");
 
80
        
 
81
        // catch error
 
82
        if (!is_file($outfile) || filesize($outfile) < 10) {
 
83
                echo "<pre>$output</pre>";
 
84
                echo "Error while converting SVG. ";
 
85
                
 
86
                if (strpos($output, 'SVGConverter.error.while.rasterizing.file') !== false) {
 
87
                        echo "
 
88
                        <h4>Debug steps</h4>
 
89
                        <ol>
 
90
                        <li>Copy the SVG:<br/><textarea rows=5>" . htmlentities(str_replace('>', ">\n", $svg)) . "</textarea></li>
 
91
                        <li>Go to <a href='http://validator.w3.org/#validate_by_input' target='_blank'>validator.w3.org/#validate_by_input</a></li>
 
92
                        <li>Paste the SVG</li>
 
93
                        <li>Click More Options and select SVG 1.1 for Use Doctype</li>
 
94
                        <li>Click the Check button</li>
 
95
                        </ol>";
 
96
                }
 
97
        } 
 
98
        
 
99
        // stream it
 
100
        else {
 
101
                header("Content-Disposition: attachment; filename=\"$filename.$ext\"");
 
102
                header("Content-Type: $type");
 
103
                echo file_get_contents($outfile);
 
104
        }
 
105
        
 
106
        // delete it
 
107
        unlink("temp/$tempName.svg");
 
108
        unlink($outfile);
 
109
 
 
110
// SVG can be streamed directly back
 
111
} else if ($ext == 'svg') {
 
112
        header("Content-Disposition: attachment; filename=\"$filename.$ext\"");
 
113
        header("Content-Type: $type");
 
114
        echo $svg;
 
115
        
 
116
} else {
 
117
        echo "Invalid type";
 
118
}
 
119
?>