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