function download_open_export_file( $filename ) { $filepath = \PressBooks\Export\Export::getExportFolder() . $filename; if ( ! is_readable( $filepath ) ) { // Cannot read file wp_die( __( 'File not found', 'pressbooks-textbook' ) . ": $filename", '', array( 'response' => 404 ) ); } // Force download set_time_limit( 0 ); header( 'Content-Description: File Transfer' ); header( 'Content-Type: ' . \Pressbooks\Export\Export::mimeType( $filepath ) ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); header( 'Content-Transfer-Encoding: binary' ); header( 'Expires: 0' ); header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); header( 'Pragma: public' ); header( 'Content-Length: ' . filesize( $filepath ) ); @ob_clean(); flush(); while ( @ob_end_flush() ); // Fix out-of-memory problem readfile( $filepath ); exit; }
/** * Truncate the exports directory, delete old files. * * @param int $max */ function truncate_exports($max) { $max = absint($max); $dir = \PressBooks\Export\Export::getExportFolder(); $files = group_exports(); $i = 1; foreach ($files as $date => $exports) { if ($i > $max) { foreach ($exports as $export) { $export = realpath($dir . $export); unlink($export); } } ++$i; } }
/** * Scan the export directory, return latest of each file type * * @return array */ function latest_exports() { $suffix = array('._3.epub', '.epub', '.pdf', '.mobi', '.hpub', '.icml', '.html', '.xml'); $dir = \PressBooks\Export\Export::getExportFolder(); $files = array(); // group by extension, sort by date newest first foreach (\PressBooks\Utility\scandir_by_date($dir) as $file) { $ext = strstr($file, '.'); $files[$ext][] = $file; } // get only one of the latest of each type $latest = array(); foreach ($suffix as $value) { if (array_key_exists($value, $files)) { $latest[$value] = $files[$value][0]; } } // @TODO filter these results against user prefs return $latest; }
/** * Scan the export directory, return latest of each file type * * @return array */ function latest_exports() { $suffix = array( '._3.epub', '.epub', '.pdf', '.mobi', '.hpub', '.icml', '.html', '.xml', '._vanilla.xml', '._oss.pdf', ); $dir = \PressBooks\Export\Export::getExportFolder(); $files = array(); // group by extension, sort by date newest first foreach ( \PressBooks\Utility\scandir_by_date( $dir ) as $file ) { // only interested in the part of filename starting with the timestamp preg_match( '/-\d{10,11}(.*)/', $file, $matches ); // grab the first captured parenthisized subpattern $ext = $matches[1]; $files[$ext][] = $file; } // get only one of the latest of each type $latest = array(); foreach ( $suffix as $value ) { if ( array_key_exists( $value, $files ) ) { $latest[$value] = $files[$value][0]; } } // @TODO filter these results against user prefs return $latest; }
<a class="btn black" href="<?php echo get_option('home'); ?>/buy"><span class="buy-icon"></span><?php _e('Buy', 'pressbooks'); ?></a> <?php endif; ?> </div> <!-- end .call-to-action --> </div><!-- end .call-to-action-wrap --> <!-- display links to files --> <?php $files = \PBT\Utility\latest_exports(); $options = get_option( 'pbt_redistribute_settings' ); if ( ! empty( $files ) && ( true == $options['latest_files_public'] ) ) { echo '<div class="alt-formats">' . '<h4>Download in the following formats:</h4>'; $dir = \PressBooks\Export\Export::getExportFolder(); foreach ( $files as $ext => $filename ) { $file_extension = substr( strrchr( $ext, '.' ), 1 ); $pre_suffix = (false == strstr( $ext, '._3.epub' )) ? strstr( $ext, '._vanilla.xml' ) : strstr( $ext, '._3.epub' ); switch ( $file_extension ) { case 'html': $file_class = 'xhtml'; break; case 'xml': $file_class = ( false == $pre_suffix) ? 'wxr' : 'vanillawxr'; break; case 'epub': $file_class = ( false == $pre_suffix ) ? 'epub' : 'epub3'; break; default: