'; $container_file_contents .= ''; $container_file_contents .= ''; $container_file_contents .= ''; $container_file_contents .= ''; $container_file_contents .= ''; fwrite($fp, $container_file_contents); fclose($fp); // Load intermediate TEI file $tei_data = new TeiDom($_POST); $teiDom = $tei_data->getTeiDom(); // echo $tei_data->getTeiString(); die(); // Get all images referenced in tei & copy over to image directory // DOM Query using xpath: http://www.exforsys.com/tutorials/php-oracle/querying-a-dom-document-with-xpath.html $xpath = new DOMXPath($teiDom); $xpath->registerNamespace('tei', TEI); $xpath->registerNamespace('html', HTML); $xpath->registerNamespace('anth', ANTH); $query = '//img/@src'; $image_url_nodes = $xpath->query($query); foreach ($image_url_nodes as $image_url_node) // Iterate through images { // Get image url & open file $image_url = $image_url_node->nodeValue; $image_filename = preg_replace('/^.*\//', '', $image_url); // Erase all but filename from URL $ch = curl_init($image_url); $fp = fopen($temp_epub_images_dir . '/' . $image_filename, "w"); // Fetch image from url & put into file curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); } $xsl_html_file = $epub_dir . DIRECTORY_SEPARATOR . 'tei2html.xsl'; $xsl_ncx_file = $epub_dir . DIRECTORY_SEPARATOR . 'tei2ncx.xsl'; $xsl_opf_file = $epub_dir . DIRECTORY_SEPARATOR . 'tei2opf.xsl'; // Load stylesheets $tei2html_xsl = new DOMDocument(); $tei2html_xsl->load($xsl_html_file); $tei2ncx_xsl = new DOMDocument(); $tei2ncx_xsl->load($xsl_ncx_file); $tei2opf_xsl = new DOMDocument(); $tei2opf_xsl->load($xsl_opf_file); // Create XSLT processor $proc = new XSLTProcessor(); // Import stylesheets & transform & save $html_filename = $temp_epub_oebps_dir . DIRECTORY_SEPARATOR . "main_content.html"; $ncx_filename = $temp_epub_oebps_dir . DIRECTORY_SEPARATOR . "toc.ncx"; $opf_filename = $temp_epub_oebps_dir . DIRECTORY_SEPARATOR . "book.opf"; // XHTML $proc->importStylesheet($tei2html_xsl); $fp = fopen($html_filename, "w") or die("Couldn't open temporary file for epub archive (main_content.html)"); $html = $proc->transformToXML($teiDom); $empty_namespace_pattern = '/\sxmlns=""\s/i'; //$html_no_empty_namespaces = preg_replace('/x/', 'xyz', $html); $html_no_empty_namespaces = preg_replace('/xmlns=""/u', '', $html); //$html_no_empty_namespaces .= ""; fwrite($fp, $html_no_empty_namespaces); // fwrite($fp, $proc->transformToXML($teiDom)); fclose($fp); // (clean out empty namespaces using a regex) /* $string = 'April 15, 2003'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1}1,$3'; echo preg_replace($pattern, $replacement, $string); */ // NCX $proc->importStylesheet($tei2ncx_xsl); $fp = fopen($ncx_filename, "w") or die("Couldn't open temporary file for epub archive (toc.ncx)"); fwrite($fp, $proc->transformToXML($teiDom)); fclose($fp); // OPF $proc->importStylesheet($tei2opf_xsl); $fp = fopen($opf_filename, "w") or die("Couldn't open temporary file for epub archive (book.opf)"); fwrite($fp, $proc->transformToXML($teiDom)); fclose($fp); // zip up contents of temp directory into a ZIP file zip_it($temp_epub_dir_name, $temp_zip_filename) or die("Couldn't create ZIP archive file: '" . $temp_zip_filename . "'"); // Serve up zip file header("Content-type: application/epub+zip"); header("Content-Disposition: attachment; filename=" . $zip_download_filename); header("Pragma: no-cache"); header("Expires: 0"); readfile($temp_zip_filename); // Delete all contents in temp dir // Code derived from http://www.php.net/manual/en/class.recursiveiteratoriterator.php // deleteDirectoryWithContents($temp_epub_dir_name); // unlink($temp_zip_filename); die(); // END // Function to take a source directory and zip it up into a destination archive // Code derived from http://stackoverflow.com/questions/1334613/how-to-recursively-zip-a-directory-in-php function zip_it($source, $destination) { if (extension_loaded('zip') === true) { if (file_exists($source) === true) { $zip = new ZipArchive(); if ($zip->open($destination, ZIPARCHIVE::CREATE) === true) { $source = realpath($source); $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); // Iterate through files & directories and add to archive object foreach ($files as $file) { $file = realpath($file); if (is_dir($file) === true) // Create directories as they are found { $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); } else if (is_file($file) === true) // Add files as they are found { $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); } } } else { echo "Couldn't create zip file
"; } return $zip->close(); } } else { echo "Zip library not installed
"; return false; } return false; } // Delete a directory and its contents // Derived from code at http://nashruddin.com/Remove_Directories_Recursively_with_PHP function deleteDirectoryWithContents ($dir) { // DISABLED UNTIL TESTED $files = scandir($dir); array_shift($files); // remove '.' from array array_shift($files); // remove '..' from array foreach ($files as $file) { $file = $dir . '/' . $file; if (is_dir($file)) { deleteDirectoryWithContents($file); } else { unlink($file); } } rmdir($dir); } ?>