function send($p_result) { switch ($this->b_type) { case 'rtf': // A rtf file is generated header('Content-type: application/rtf'); header('Content-Disposition: attachment; filename="' . $this->b_name . '.rtf"'); echo $p_result; break; case 'txt': // A txt file is generated header('Content-type: application/txt'); header('Content-Disposition: attachment; filename="' . $this->b_name . '.txt"'); echo $p_result; break; case 'html': // A txt file is generated header('Content-type: application/html'); header('Content-Disposition: attachment; filename="' . $this->b_name . '.html"'); echo $p_result; break; case 'odt': case 'ods': header("Pragma: public"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: must-revalidate"); if ($this->b_type == 'odt') { header('Content-type: application/vnd.oasis.opendocument.text'); header('Content-Disposition: attachment;filename="' . $this->b_name . '.odt"', FALSE); } if ($this->b_type == 'ods') { header('Content-type: application/vnd.oasis.opendocument.spreadsheet'); header('Content-Disposition: attachment;filename="' . $this->b_name . '.ods"', FALSE); } header("Accept-Ranges: bytes"); ob_start(); // save the file in a temp folder // create a temp directory in /tmp to unpack file and to parse it $dirname = tempnam($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'tmp', 'bilan_'); unlink($dirname); mkdir($dirname); chdir($dirname); // create a temp directory in /tmp to unpack file and to parse it $file_base = dirname($_SERVER['SCRIPT_FILENAME']) . DIRECTORY_SEPARATOR . $this->b_file_template; $work_file = basename($file_base); if (copy($file_base, $work_file) == false) { throw new Exception(_("Ouverture fichier impossible")); } /* * unzip the document */ ob_start(); $zip = new Zip_Extended(); if ($zip->open($work_file) === TRUE) { $zip->extractTo($dirname . DIRECTORY_SEPARATOR); $zip->close(); } else { echo __FILE__ . ":" . __LINE__ . "cannot unzip model " . $filename; } // Remove the file we do not need anymore unlink($work_file); // replace the file $p_file = fopen($dirname . DIRECTORY_SEPARATOR . 'content.xml', 'wb'); if ($p_file == false) { throw new Exception(_("erreur Ouverture fichier") . ' content.xml'); } $a = fwrite($p_file, $p_result); if ($a == false) { throw new Exception(_("erreur écriture fichier") . ' content.xml'); } // repack $zip = new Zip_Extended(); $res = $zip->open($this->b_name . "." . $this->b_type, ZipArchive::CREATE); if ($res !== TRUE) { throw new Exception(__FILE__ . ":" . __LINE__ . "cannot recreate zip"); } $zip->add_recurse_folder($dirname . DIRECTORY_SEPARATOR); $zip->close(); ob_end_clean(); fclose($p_file); $fdoc = fopen($dirname . DIRECTORY_SEPARATOR . $this->b_name . '.' . $this->b_type, 'r'); if ($fdoc == false) { throw new Exception(_("erreur Ouverture fichier")); } $buffer = fread($fdoc, filesize($dirname . DIRECTORY_SEPARATOR . $this->b_name . '.' . $this->b_type)); echo $buffer; break; // and send } }
function Generate($p_array, $p_filename = "") { // create a temp directory in /tmp to unpack file and to parse it $dirname = tempnam($_ENV['TMP'], 'doc_'); unlink($dirname); mkdir($dirname); // Retrieve the lob and save it into $dirname $this->db->start(); $dm_info = "select md_name,md_type,md_lob,md_filename,md_mimetype\n from document_modele where md_id=" . $this->md_id; $Res = $this->db->exec_sql($dm_info); $row = Database::fetch_array($Res, 0); $this->d_lob = $row['md_lob']; $this->d_filename = $row['md_filename']; $this->d_mimetype = $row['md_mimetype']; $this->d_name = $row['md_name']; chdir($dirname); $filename = $row['md_filename']; $exp = $this->db->lo_export($row['md_lob'], $dirname . DIRECTORY_SEPARATOR . $filename); if ($exp === false) { echo_warning(__FILE__ . ":" . __LINE__ . "Export NOK {$filename}"); } $type = "n"; // if the doc is a OOo, we need to unzip it first // and the name of the file to change is always content.xml if (strpos($row['md_mimetype'], 'vnd.oasis') != 0) { ob_start(); $zip = new Zip_Extended(); if ($zip->open($filename) === TRUE) { $zip->extractTo($dirname . DIRECTORY_SEPARATOR); $zip->close(); } else { echo __FILE__ . ":" . __LINE__ . "cannot unzip model " . $filename; } // Remove the file we do not need anymore unlink($filename); ob_end_clean(); $file_to_parse = "content.xml"; $type = "OOo"; } else { $file_to_parse = $filename; } // affect a number $this->d_number = $this->db->get_next_seq("seq_doc_type_" . $row['md_type']); // parse the document - return the doc number ? $this->ParseDocument($dirname, $file_to_parse, $type, $p_array); $this->db->commit(); // if the doc is a OOo, we need to re-zip it if (strpos($row['md_mimetype'], 'vnd.oasis') != 0) { ob_start(); $zip = new Zip_Extended(); $res = $zip->open($filename, ZipArchive::CREATE); if ($res !== TRUE) { throw new Exception(__FILE__ . ":" . __LINE__ . "cannot recreate zip"); } $zip->add_recurse_folder($dirname . DIRECTORY_SEPARATOR); $zip->close(); ob_end_clean(); $file_to_parse = $filename; } if ($p_filename != "") { $this->d_filename = $this->compute_filename($p_filename, $this->d_filename); } $this->SaveGenerated($dirname . DIRECTORY_SEPARATOR . $file_to_parse); // Invoice $ret = '<A class="mtitle" HREF="show_document.php?d_id=' . $this->d_id . '&' . dossier::get() . '">Document généré</A>'; @rmdir($dirname); return $ret; }