GnumericToExcel.php
[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 post($fname) {
32         
33           $ml = (int) ini_get('suhosin.post.max_value_length');
34         if (empty($_POST['xml'])) {
35             header("HTTP/1.0 400 Internal Server Error");
36             die(  $ml ? "Suhosin Patch enabled - try and disable it!!!" : 'no XML sent');
37         }
38         
39         if (empty($_POST['xml'])) {
40             header("HTTP/1.0 400 Internal Server Error");
41             die("Missing XML attribute");
42         }
43         $xml = $_POST['xml'];
44         
45         $xml = iconv("UTF-8", "UTF-8//IGNORE",  $xml);
46         
47         //$xml = str_replace('é', 'e', $xml);
48         //$xml = str_replace("\xA0", ' ', $xml);
49         //$xml = str_replace("Ø", 'dia.',$xml);
50         
51         //$this->addEvent("DOWNLOAD", false, isset($_REQUEST['title']) ? $_REQUEST['title'] : '???');
52         
53         
54         if (!empty($_POST['format']) && $_POST['format']=='gnumeric') {
55             if (empty($_POST['debug'])) {
56                 header('Content-type: application/x-gnumeric');
57             } else {
58                 header('Content-type: text/xml');
59             }
60             echo $xml; 
61             exit;
62         }
63         $srcTmp = ini_get('session.save_path') . '/' .uniqid('gnumeric_').'.gnumeric';
64         $targetTmp = ini_get('session.save_path') . '/' .uniqid('gnumeric_').'.xls';
65         // write the gnumeric file...
66         $fh = fopen($srcTmp,'w');
67         fwrite($fh, $xml);
68         fclose($fh);
69         
70         
71         require_once 'System.php';
72         $ss = System::which('ssconvert');
73         $cmd =  $ss. 
74                 " --import-encoding=Gnumeric_XmlIO:sax" .
75                 " --export-type=Gnumeric_Excel:excel_biff8 " . 
76                 $srcTmp . ' ' . $targetTmp . ' 2>&1';
77         // echo $cmd;
78         //passthru($cmd);exit;
79         //exit;
80         $out = `$cmd`;
81         clearstatcache(); 
82         
83         if (!file_exists($targetTmp) || !filesize($targetTmp)) {
84             header("HTTP/1.0 400 Internal Server Error");
85             die("ERROR CONVERTING?:" . $cmd ."\n<BR><BR> OUTPUT:". htmlspecialchars($out));
86         }
87         unlink($srcTmp);
88         
89         $fh = fopen($targetTmp, 'r');
90         header('Content-type: application/vnd.ms-excel');
91         header('Content-Disposition: attachment; filename="' .addslashes($fname). '"');
92          
93         // will not work on IE... - needs while/fget..
94         fpassthru($fh);
95         unlink($targetTmp);
96         exit;
97         
98     }
99     
100     
101 }