diff options
| author | Max Kremmel <xing@synapse.plus.com> | 2009-01-15 20:34:41 +0000 |
|---|---|---|
| committer | Max Kremmel <xing@synapse.plus.com> | 2009-01-15 20:34:41 +0000 |
| commit | 7e096f23e30dd91190ffebfb5aa98f3326090938 (patch) | |
| tree | fd395f776bf049e6ca3d48ed038207954bd0743b | |
| parent | fe3cfe3b4a48693f36286b335802937319eb1a6d (diff) | |
| download | liberty-7e096f23e30dd91190ffebfb5aa98f3326090938.tar.gz liberty-7e096f23e30dd91190ffebfb5aa98f3326090938.tar.bz2 liberty-7e096f23e30dd91190ffebfb5aa98f3326090938.zip | |
add option to use swftools to convert pdfs to swf and display the PDF inline
| -rw-r--r-- | admin/plugins/mime_pdf.php | 47 | ||||
| -rw-r--r-- | plugins/mime.pdf.php | 181 | ||||
| -rw-r--r-- | templates/mime/pdf/admin.tpl | 40 | ||||
| -rw-r--r-- | templates/mime/pdf/view.tpl | 17 |
4 files changed, 285 insertions, 0 deletions
diff --git a/admin/plugins/mime_pdf.php b/admin/plugins/mime_pdf.php new file mode 100644 index 0000000..514c6b8 --- /dev/null +++ b/admin/plugins/mime_pdf.php @@ -0,0 +1,47 @@ +<?php +require_once( '../../../bit_setup_inc.php' ); +include_once( KERNEL_PKG_PATH.'simple_form_functions_lib.php' ); + +$gBitSystem->verifyPermission( 'p_admin' ); + +$feedback = array(); + +$pdfSettings = array( + 'pdf2swf_path' => array( + 'label' => 'Path to pdf2swf', + 'note' => 'Path to the pdf2swf executable.', + 'type' => 'text', + ), + 'swfcombine_path' => array( + 'label' => 'Path to swfcombine', + 'note' => 'Path to the swfcombine executable.', + 'type' => 'text', + ), +); + +if( function_exists( 'shell_exec' )) { + $pdfSettings['pdf2swf_path']['default'] = shell_exec( 'which pdf2swf' ); + $pdfSettings['swfcombine_path']['default'] = shell_exec( 'which swfcombine' ); +} else { + $feedback['error'] = "You can not execute binaries on your server. You can not make use of this plugin."; +} + +$gBitSmarty->assign( 'pdfSettings', $pdfSettings ); + +if( !empty( $_REQUEST['plugin_settings'] )) { + foreach( $pdfSettings as $item => $data ) { + if( $data['type'] == 'checkbox' ) { + simple_set_toggle( $item, LIBERTY_PKG_NAME ); + } elseif( $data['type'] == 'numeric' ) { + simple_set_int( $item, LIBERTY_PKG_NAME ); + } else { + simple_set_value( $item, LIBERTY_PKG_NAME ); + } + } + + $feedback['success'] = tra( 'The plugin was successfully updated' ); +} + +$gBitSmarty->assign( 'feedback', $feedback ); +$gBitSystem->display( 'bitpackage:liberty/mime/pdf/admin.tpl', tra( 'PDF Plugin Settings' ), array( 'display_mode' => 'admin' )); +?> diff --git a/plugins/mime.pdf.php b/plugins/mime.pdf.php new file mode 100644 index 0000000..d41eb63 --- /dev/null +++ b/plugins/mime.pdf.php @@ -0,0 +1,181 @@ +<?php +/** + * @version $Header: /cvsroot/bitweaver/_bit_liberty/plugins/mime.pdf.php,v 1.1 2009/01/15 20:34:41 squareing Exp $ + * + * @author xing <xing@synapse.plus.com> + * @version $Revision: 1.1 $ + * created Thursday May 08, 2008 + * @package liberty + * @subpackage liberty_mime_handler + **/ + +/** + * setup + */ +global $gLibertySystem; + +/** + * This is the name of the plugin - max char length is 16 + * As a naming convention, the treasury mime handler definition should start with: + * PLUGIN_MIME_GUID_ + */ +define( 'PLUGIN_MIME_GUID_PDF', 'mimepdf' ); + +$pluginParams = array ( + // Set of functions and what they are called in this paricular plugin + // Use the GUID as your namespace + 'verify_function' => 'mime_default_verify', + 'store_function' => 'mime_pdf_store', + 'update_function' => 'mime_pdf_update', + 'load_function' => 'mime_pdf_load', + 'download_function' => 'mime_default_download', + 'expunge_function' => 'mime_default_expunge', + //'help_function' => 'mime_pdf_help', + // Brief description of what the plugin does + 'title' => 'Browsable PDFs', + 'description' => 'Convert PDFs to flash files that can be browsed online.', + // Templates to display the files + 'view_tpl' => 'bitpackage:liberty/mime/pdf/view.tpl', + //'attachment_tpl' => 'bitpackage:liberty/mime/image/attachment.tpl', + // url to page with options for this plugin + 'plugin_settings_url' => LIBERTY_PKG_URL.'admin/plugins/mime_pdf.php', + // This should be the same for all mime plugins + 'plugin_type' => MIME_PLUGIN, + // Set this to TRUE if you want the plugin active right after installation + 'auto_activate' => FALSE, + // Help page on bitweaver.org + //'help_page' => 'LibertyMime+Image+Plugin', + // this should pick up all image + 'mimetypes' => array( + '#.*/pdf#i', + ), +); +$gLibertySystem->registerPlugin( PLUGIN_MIME_GUID_PDF, $pluginParams ); + +/** + * Store the data in the database + * + * @param array $pStoreRow File data needed to store details in the database - sanitised and generated in the verify function + * @access public + * @return TRUE on success, FALSE on failure - $pStoreRow[errors] will contain reason + */ +function mime_pdf_store( &$pStoreRow ) { + // this will set the correct pluign guid, even if we let default handle the store process + $pStoreRow['attachment_plugin_guid'] = PLUGIN_MIME_GUID_PDF; + $pStoreRow['log'] = array(); + + // if storing works, we process the image + if( $ret = mime_default_store( $pStoreRow )) { + if( !mime_pdf_convert_pdf2swf( $pStoreRow )) { + // if it all goes tits up, we'll know why + $pStoreRow['errors'] = $pStoreRow['log']; + $ret = FALSE; + } + } + return $ret; +} + +/** + * mime_pdf_update update file information in the database if there were changes. + * + * @param array $pStoreRow File data needed to update details in the database + * @access public + * @return TRUE on success, FALSE on failure - $pStoreRow[errors] will contain reason + */ +function mime_pdf_update( &$pStoreRow, $pParams = NULL ) { + global $gThumbSizes, $gBitSystem; + + $ret = TRUE; + + // this will set the correct pluign guid, even if we let default handle the store process + $pStoreRow['attachment_plugin_guid'] = PLUGIN_MIME_GUID_PDF; + + // if storing works, we process the image + if( !empty( $pStoreRow['upload'] ) && $ret = mime_default_update( $pStoreRow )) { + } + + return $ret; +} + +/** + * Load file data from the database + * + * @param array $pFileHash Contains all file information + * @param array $pPrefs Attachment preferences taken liberty_attachment_prefs + * @param array $pParams Parameters for loading the plugin - e.g.: might contain values such as thumbnail size from the view page + * @access public + * @return TRUE on success, FALSE on failure - $pStoreRow[errors] will contain reason + */ +function mime_pdf_load( &$pFileHash, &$pPrefs, $pParams = NULL ) { + global $gBitSystem; + // don't load a mime image if we don't have an image for this file + if( $ret = mime_default_load( $pFileHash, $pPrefs, $pParams )) { + if( !empty( $ret['storage_path'] )) { + $source_path = BIT_ROOT_PATH.dirname( $ret['storage_path'] ).'/'; + // if the swf file exists, we pass it back that it can be viewed. + if( is_file( $source_path.'pdf.swf' )) { + $ret['media_url'] = storage_path_to_url( dirname( $ret['storage_path'] ).'/pdf.swf' ); + } + } + } + return $ret; +} + +/** + * mime_pdf_convert_pdf2swf Convert a PDF to a SWF video + * + * @param array $pFileHash file details. + * @param array $pFileHash[upload] should contain a complete hash from $_FILES + * @access public + * @return TRUE on success, FALSE on failure + */ +function mime_pdf_convert_pdf2swf( $pFileHash ) { + global $gBitSystem; + if( !empty( $pFileHash['upload'] ) && @BitBase::verifyId( $pFileHash['attachment_id'] )) { + // get file paths + $pdf2swf = trim( $gBitSystem->getConfig( 'swf2pdf_path', shell_exec( 'which pdf2swf' ))); + $swfcombine = trim( $gBitSystem->getConfig( 'swfcombine_path', shell_exec( 'which swfcombine' ))); + + if( is_executable( $pdf2swf ) && is_executable( $swfcombine )) { + $source = BIT_ROOT_PATH.$pFileHash['upload']['dest_path'].$pFileHash['upload']['name']; + $dest_path = dirname( $source ); + + $tmp_file = "$dest_path/tmp.swf"; + $swf_file = "$dest_path/pdf.swf"; + + $pdfviewer = UTIL_PKG_PATH."javascript/pdfviewer/fdviewer.swf"; + $swfloader = UTIL_PKG_PATH."javascript/pdfviewer/loader.swf"; + + $pdf2swfcommand = "$pdf2swf -s insertstop -s jpegquality=".$gBitSystem->getConfig( 'liberty_thumbnail_quality', 85 )." '$source' -o '$tmp_file'"; + $combinecommand = "$swfcombine '$pdfviewer' loader='$swfloader' '#1'='$tmp_file' -o '$swf_file'"; + + shell_exec( $pdf2swfcommand ); + if( is_file( $tmp_file ) && filesize( $tmp_file ) > 0 ) { + shell_exec( $combinecommand ); + if( !is_file( $swf_file ) || filesize( $swf_file ) == 0 ) { + // combination went wrong. remove swf file + $pFileHash['log']['swfcombine'] = "There was a problem combining the PDF SWF with the viewer."; + @unlink( $swf_file ); + } + } else { + $pFileHash['log']['pdf2swf'] = "There was a problem converting the PDF to SWF."; + } + + // remove temp file + @unlink( $tmp_file ); + } + } + + return( empty( $pFileHash['log'] )); +} + +/** + * mime_pdf_help + * + * @access public + * @return string + */ +function mime_pdf_help() { + return ''; +} +?> diff --git a/templates/mime/pdf/admin.tpl b/templates/mime/pdf/admin.tpl new file mode 100644 index 0000000..6ef53f6 --- /dev/null +++ b/templates/mime/pdf/admin.tpl @@ -0,0 +1,40 @@ +{* $Header: /cvsroot/bitweaver/_bit_liberty/templates/mime/pdf/admin.tpl,v 1.1 2009/01/15 20:34:41 squareing Exp $ *} +{strip} +<div class="admin liberty"> + <div class="header"> + <h1>{tr}PDF Plugin Settings{/tr}</h1> + </div> + + <div class="body"> + {form legend="PDF Plugin settings"} + <p class="warning"> + {biticon iname="dialog-warning" iexplain="Warning"} {tr}To make use of this plugin, you need to install <a class="external" href="http://www.swftools.org/">SWF Tools</a>. This will provide all necessary tools to convert uploaded PDF files to shockwave flash files that can be viewed in your browser.{/tr} + </p> + + {if !$gLibertySystem->isPluginActive( 'mimepdf' )} + {formfeedback error="This plugins has not been enabled. You need to enable it for these settings to take effect."} + {/if} + + {formfeedback hash=$feedback} + + {foreach from=$pdfSettings key=feature item=output} + <div class="row"> + {formlabel label=`$output.label` for=$feature} + {forminput} + {if $output.type == 'checkbox'} + {html_checkboxes name="$feature" values="y" checked=$gBitSystem->getConfig($feature) labels=false id=$feature} + {else} + <input type='text' name="{$feature}" id="{$feature}" size="{if $output.type == 'text'}40{else}5{/if}" value="{$gBitSystem->getConfig($feature)|escape|default:$output.default}" /> + {/if} + {formhelp note=`$output.note` page=`$output.page`} + {/forminput} + </div> + {/foreach} + + <div class="row submit"> + <input type="submit" name="settings_store" value="{tr}Change preferences{/tr}" /> + </div> + {/form} + </div><!-- end .body --> +</div><!-- end .liberty --> +{/strip} diff --git a/templates/mime/pdf/view.tpl b/templates/mime/pdf/view.tpl new file mode 100644 index 0000000..a5920e0 --- /dev/null +++ b/templates/mime/pdf/view.tpl @@ -0,0 +1,17 @@ +{strip} +{if $attachment.media_url} + <div class="row aligncenter"> + <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="800" height="600" id="viewer" align="middle"> + <param name="allowScriptAccess" value="sameDomain" /> + <param name="movie" value="{$attachment.media_url}" /> + <param name="quality" value="high" /> + <param name="bgcolor" value="#ffffff" /> + <embed src="{$attachment.media_url}" quality="high" bgcolor="#ffffff" width="800" height="600" name="myviewer" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> + </object> + </div> + + {include file=bitpackage:liberty/mime_meta_inc.tpl} +{else} + {include file=$gLibertySystem->getMimeTemplate('view', $smarty.const.LIBERTY_DEFAULT_MIME_HANDLER)} +{/if} +{/strip} |
