fix #8131 - chinese translations
[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                 header('Content-Disposition: attachment; filename="' .addslashes($fname). '.gnumeric"');
62             } else {
63                 header('Content-type: text/xml');
64             }
65             echo $xml; 
66             exit;
67         }
68         
69         $ext = 'xls';
70         $outfmt = 'Gnumeric_Excel:excel_biff8';
71         $mime = 'application/vnd.ms-excel';
72         
73         /*
74          // ssconvert results in bad images 
75          if (!empty($_POST['format']) && $_POST['format']=='xlsx') {
76             $outfmt = 'Gnumeric_Excel:xlsx';
77             $ext = 'xlsx';
78             $mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
79         }
80         */
81         
82         
83         
84         $srcTmp = $this->tempName('gnumeric');
85         $targetTmp = $this->tempName($ext);
86         // write the gnumeric file...
87         $fh = fopen($srcTmp,'w');
88         fwrite($fh, $xml);
89         fclose($fh);
90         
91         
92         require_once 'System.php';
93         $xvfb = System::which('xvfb-run');
94         
95         $ss = System::which('ssconvert');
96         $cmd = $xvfb . " -a " . $ss. 
97                 " --import-encoding=Gnumeric_XmlIO:sax" .
98                 " --export-type={$outfmt} " . 
99                 $srcTmp . ' ' . $targetTmp . ' 2>&1';
100         // echo $cmd;
101         //passthru($cmd);exit; 
102         //exit;
103         $out = `$cmd`;
104         clearstatcache(); 
105         
106         if (!file_exists($targetTmp) || !filesize($targetTmp)) {
107             header("HTTP/1.0 400 Internal Server Error - Convert error");
108             die("ERROR CONVERTING?:" . $cmd ."\n<BR><BR> OUTPUT:". htmlspecialchars($out));
109         }
110         
111         if (!empty($_POST['format']) && $_POST['format']=='xlsx') {
112             require_once 'File/Convert.php';
113             $cc = new File_Convert($targetTmp,'application/vnd.ms-excel');
114             $targetTmp = $cc->convert('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
115             if (empty($targetTmp)) {
116                 $this->jerr("convert to xlsx failed");
117             }
118             
119             $mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
120             $ext = "xlsx";
121          }
122         if (empty($fname)) {
123            $fname = basename($targetTmp);
124         }
125         $fname .= preg_match('/\.' . $ext . '/i', $fname) ? '' :  ('.' . $ext); // make sure it ends in xls..
126        
127         
128         DB_DataObject::factory('Events')->addFile(array(
129             'tmp_name' => $targetTmp,
130             'name' => $fname,
131             'type' => $mime,
132             'size' => filesize($targetTmp)
133         ));
134         
135         $this->addEvent("DOWNLOAD",  false, $fname  );
136         
137        // unlink($srcTmp);
138         
139         header('Content-type: ' . $mime);
140         header('Content-Disposition: attachment; filename="' .addslashes($fname). '"');
141         header('Content-length: '. filesize($targetTmp));   
142         header("Content-Transfer-Encoding: binary");
143         if ($file = fopen($targetTmp, 'rb')) {
144             while(!feof($file) and (connection_status()==0)) {
145                 print(fread($file, 1024*8));
146                 flush();
147             }
148             fclose($file);
149         }
150        
151         unlink($targetTmp);
152         exit;
153         
154     }
155     
156      
157 }