Pman.Gnumeric.js
[Pman.Core] / GnumericToExcel.php
1 <?php
2
3 /**
4  * 
5  * MOVE To Core ---
6  * 
7  * Usage:
8  * 
9  * POST Values: 
10  *  xml = data
11  *  format = [empty=xls] , 'gnumeric'  (download format)
12  *  debug = true => download xml.
13  * 
14  * 
15  * 
16  */
17
18 require_once 'Pman.php';
19 class Pman_Core_GnumericToExcel extends Pman
20 {
21     function getAuth()
22     {
23         $au = $this->getAuthUser();
24         if (!$au) {
25             die("NOT authenticated");
26         }
27         $this->authUser = $au;
28         return true;
29     }
30
31     function get($v, $opts=array())
32     {
33         
34     }
35     function post($fname) {
36         
37           $ml = (int) ini_get('suhosin.post.max_value_length');
38         if (empty($_POST['xml'])) {
39             header("HTTP/1.0 400 Internal Server Error");
40             die(  $ml ? "Suhosin Patch enabled - try and disable it!!!" : 'no XML sent');
41         }
42         
43         if (empty($_POST['xml'])) {
44             header("HTTP/1.0 400 Internal Server Error");
45             die("Missing XML attribute");
46         }
47         $xml = $_POST['xml'];
48         
49         $xml = iconv("UTF-8", "UTF-8//IGNORE",  $xml);
50         
51         //$xml = str_replace('�', 'e', $xml);
52         //$xml = str_replace("\xA0", ' ', $xml);
53         //$xml = str_replace("�", 'dia.',$xml);
54         
55         //$this->addEvent("DOWNLOAD", false, isset($_REQUEST['title']) ? $_REQUEST['title'] : '???');
56         
57         
58         if (!empty($_POST['format']) && $_POST['format']=='gnumeric') {
59             if (empty($_POST['debug'])) {
60                 header('Content-type: application/x-gnumeric');
61             } else {
62                 header('Content-type: text/xml');
63             }
64             echo $xml; 
65             exit;
66         }
67         $srcTmp = ini_get('session.save_path') . '/' .uniqid('gnumeric_').'.gnumeric';
68         $targetTmp = ini_get('session.save_path') . '/' .uniqid('gnumeric_').'.xls';
69         // write the gnumeric file...
70         $fh = fopen($srcTmp,'w');
71         fwrite($fh, $xml);
72         fclose($fh);
73         
74         
75         require_once 'System.php';
76         $ss = System::which('ssconvert');
77         $cmd =  $ss. 
78                 " --import-encoding=Gnumeric_XmlIO:sax" .
79                 " --export-type=Gnumeric_Excel:excel_biff8 " . 
80                 $srcTmp . ' ' . $targetTmp . ' 2>&1';
81         // echo $cmd;
82         //passthru($cmd);exit;
83         //exit;
84         $out = `$cmd`;
85         clearstatcache(); 
86         
87         if (!file_exists($targetTmp) || !filesize($targetTmp)) {
88             header("HTTP/1.0 400 Internal Server Error - Convert error");
89             die("ERROR CONVERTING?:" . $cmd ."\n<BR><BR> OUTPUT:". htmlspecialchars($out));
90         }
91        // unlink($srcTmp);
92         if (empty($fname)) {
93            $fname = basename($targetTmp);
94         }
95         $fname .= preg_match('/\.xls/i', $fname) ? '' :  '.xls'; // make sure it ends in xls..
96        
97         header('Content-type: application/vnd.ms-excel');
98         header('Content-Disposition: attachment; filename="' .addslashes($fname). '"');
99         header('Content-length: '. filesize($targetTmp));   
100         header("Content-Transfer-Encoding: binary");
101         if ($file = fopen($targetTmp, 'rb')) {
102             while(!feof($file) and (connection_status()==0)) {
103                 print(fread($file, 1024*8));
104                 flush();
105             }
106             fclose($file);
107         }
108        
109         unlink($targetTmp);
110         exit;
111         
112     }
113     
114     
115 }