[Gag] * @version 0.08beta */ // must be run within Dokuwiki if(!defined('DOKU_INC')) die(); if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); class action_plugin_odt2dw extends DokuWiki_Action_Plugin { /** * Registers a callback function for a given event */ function register(Doku_Event_Handler $controller) { // OdtFile Parser hook $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_parser', array()); // Display form hook before the wiki page (on top); Maybe create a param to display the form after the page $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, '_render', array()); $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array()); } /** * Add 'import odt'-button to pagetools * * @param Doku_Event $event * @param mixed $param not defined */ public function addbutton(&$event, $param) { global $ID, $REV, $conf; if($this->getConf('showimportbutton') && $event->data['view'] == 'main') { $params = array('do' => 'odt2dw'); if($REV) $params['rev'] = $REV; switch($conf['template']) { case 'dokuwiki': case 'arago': $event->data['items']['import_odt'] = '
message : '.$message.' |output : '.$output.' |val : '.$val.' |err : '.$err.'
'; // Debug = 0 => No message if ( !$force && $this->debug == 0 ) return ( $err == -1 ? false : true ); // Debug < 3 => Only error message; If it s not an error message, message return true; if ( !$force && $err != -1 && $this->debug < 3 ) return true; // Otherwise display the message $content = $output.' : '.$this->getLang( $output ).( is_array( $message ) ? ' : '.$message[1] : '' ); msg( 'odt2dw : '.$content, $err ); if ( isset( $this->logFileHandle ) ) fwrite( $this->logFileHandle, date(DATE_ATOM).':'.$_SERVER['REMOTE_USER'].':'.$content.' ' ); // If error message, return false if ( $err == -1 ) return false; // Otherwise return true; return true; } function _checkUploadFile() { ### _checkUploadFile : group all process about the uploadFile, like uploadStatus, file format, move it in a working directory, etc. ### # OUTPUT : # * true -> process successfully # * false -> something wrong; using _msg to display what's wrong // Check a file will be upload if ( ! $_FILES['odtFile'] ) return $this->_msg('er_odtFile_miss'); // Check the file status if ( $_FILES['odtFile']['error'] > 0 ) return $this->_msg( array( 'er_odtFile_upload', $_FILES['odtFile']['error'] ) ); // Check the file has an authorized mimetype if ( $this->getConf( 'parserMimeTypeAuthorized' ) != "" && strpos( $this->getConf( 'parserMimeTypeAuthorized' ), $_FILES['odtFile']['type'] ) === false ) return $this->_msg( array( 'er_odtFile_format', $_FILES['odtFile']['type'] ) ); // Create an unique temp work dir name while ( file_exists( $this->uploadDir = $this->getConf( 'parserUploadDir' ).rand( 10000, 100000 ) ) ) {}; // Create the directory if ( ! mkdir( $this->uploadDir, 0777, true ) ) return $this->_msg( 'er_odtFile_tmpDir' ); // Move the upload file into the work directory $this->odtFileName = $_FILES['odtFile']['name']; $this->odtFile = $this->uploadDir.'/'.$this->odtFileName; if ( ! move_uploaded_file( $_FILES['odtFile']['tmp_name'], $this->odtFile ) ) return $this->_msg('er_odtFile_getFromDownload'); // All upload file checking are OK return true; } function _purge_env() { ### _purge_env : clean the system from temporary file ### # OUTPUT : # void # Display some error message if something wrong in the delete process (might delete the file manually) // Perhaps this would not be needed if use temp dir. // No timeOut : the cleanning process wont be interrupted. set_time_limit(0); // use @ to catch the system error message // If exists, delete the download file if ( file_exists( $this->odtFile ) ) if ( ! @unlink( $this->odtFile ) ) $this->_msg( array( 'er_pg_file', $this->odtFile ) ); // Delete each file extracted for the uploaded file if ( $this->file_extract ) foreach ($this->file_extract as $file) if ( file_exists( $file ) ) if ( ! @unlink( $file ) ) $this->_msg( array( 'er_pg_file', $file ) ); // Delete each image would be rename and not move to the wiki if ( $this->file_import ) foreach ( $this->file_import as $file ) if ( file_exists( $this->uploadDir.'/'.$this->pictpath.'/'.$file ) ) if ( ! @unlink( $this->uploadDir.'/'.$this->pictpath.'/'.$file ) ) $this->_msg( array( 'er_pg_file', $this->uploadDir.'/'.$this->pictpath.'/'.$file ) ); // Delete the Pictures directory if ( file_exists( $this->uploadDir.'/'.$this->pictpath) ) if ( ! @rmdir( $this->uploadDir.'/'.$this->pictpath ) ) $this->_msg( array( 'er_pg_dir', $this->uploadDir.'/'.$this->pictpath ) ); // Than delete the temporary directory if ( file_exists( $this->uploadDir ) ) if ( ! @rmdir( $this->uploadDir ) ) $this->_msg( array( 'er_pg_dir', $this->uploadDir ) ); // Set back default timeOut set_time_limit(30); } function _set_xsltProcessor(){ ### _set_xsltProcessor : set all xslt param regarding the dokuwiki plugin installed ### # OUTPUT : # * true -> process successfully # * false -> something wrong; using _msg to display what's wrong # _msg info report ( debugLvl >= 2 ) display message about active plugin // Gag : I think it s a Nasty way to check plugin - must be rewrite but i don t know how $tmp_plugin_lst = plugin_list(); if ( ! $this->XSLT->importStylesheet( $this->XSL ) ) return $this->_msg('er_xslt_invalid'); foreach ( array('numberedheadings') as $param ) if ( array_search( $param, $tmp_plugin_lst ) !== false ) { if ( ! $this->XSLT->setParameter( '', $param, '1' ) ) return $this->_msg( array( 'inf_xslt_param', $param ), -1 ); // _msg info report $this->_msg( array( 'ok_infoPlugin', $param ), 1 ); } // foreach ( array('subtable_message') as $lang_elt ) if ( ! $this->XSLT->setParameter( '', $lang_elt, $this->getLang('xsl_'.$lang_elt ) ) ) $this->_msg( array( 'inf_xslt_lang', $param ), 0 ); return true; } function _apply_result() { ### _apply_result : store the content in dokuwiki page and the attache file (img) in dokuwiki media # OUTPUT : # * true -> process successfully # * false -> something wrong; using _msg to display what's wrong global $INFO; // Save the content in data/page saveWikiText( $this->pageName, $this->result, $this->getLang( 'parserSummary' ).$this->odtFileName ); if ( ! page_exists($this->pageName) ) return $this->_msg('er_apply_content'); // Check if the user could upload file (ACL : permission lvl 8) if ( auth_quickaclcheck($ID) >= AUTH_UPLOAD ) { // Import the image file in the mediaManager (data/media) $destDir = mediaFN( $this->nsName ); if ( ! ( file_exists( $destDir ) || mkdir( $destDir, 0777, true ) ) ) return $this->_msg( array( 'er_apply_dirCreate' ) ); if ( $this->file_import ) foreach ( $this->file_import as $pict ) { $destFile = mediaFN( $this->nsName.':'.$pict ); list( $ext, $mime ) = mimetype( $this->uploadDir.'/'.$this->pictpath.'/'.$pict ); if ( media_upload_finish($this->uploadDir.'/'.$this->pictpath.'/'.$pict, $destFile, $this->nsName, $mime, @file_exists($destFile), 'rename' ) != $this->nsName ) return $this->_msg( array( 'er_apply_img', $this->uploadDir.'/'.$this->pictpath.'/'.$pict ) ); } // Keep the original file (import the upload file in the mediaManager) $destFile = mediaFN( $this->nsName.':'.$this->odtFileName ); list( $ext, $mime ) = mimetype( $this->uploadDir.'/'.$this->odtFileName ); if ( media_upload_finish($this->uploadDir.'/'.$this->odtFileName, $destFile, $this->nsName, $mime, @file_exists($destFile), 'rename' ) != $this->nsName ) return $this->_msg( array( 'er_apply_odtFile' ) ); } else { // If not allowed to upload, display a message. $this->_msg( 'inf_acl_upload', 0, true ); } # Refresh info about the current page (see doku.php where $INFO is initiate) - Needed for edit or preview "parserPostDisplay" option $INFO = pageinfo(); return true; } function _parse_image() { ### _parse_image : search dokuwiki img markup in $this->result than extract the img file and rename it to easier name ### # OUTPUT : # void # using _msg to display each img file wont be process successfully global $ID; $imgs = array(); if ( preg_match_all( '|{{((?:[^/}]+/)*[^/}]+)/([0-9a-zA-Z]+)(\.[a-z]+)(\?[0-9]+(?:x[0-9]+)?)?}}|', $this->result, $imgs, PREG_SET_ORDER ) ) { if ( auth_quickaclcheck( $ID ) < AUTH_UPLOAD ) return $this->_msg( 'er_acl_upload' ); $this->err['ok'] = array(); foreach ( $imgs as $key => $value ) { set_time_limit(20); $this->pictpath = $value[1]; $pict = $value[2].$value[3]; $ext = $value[3]; $other = $value[4]; if ( $this->_unzip($this->pictpath.'/'.$pict) ) { $newname = noNS($this->pageName).'_Image_'.$key.$ext; if ( rename( $this->uploadDir.'/'.$this->pictpath.'/'.$pict, $this->uploadDir.'/'.$this->pictpath.'/'.$newname ) ) { $this->result = str_replace( '{{'.$this->pictpath.'/'.$pict.$other.'}}' , '{{'.$newname.$other.'}}' , $this->result ); $this->file_import[] = $newname; if ( $this->debug ) $this->err['ok'][] = $pict.' : '.$newname; } else $this->err[$pict] = 'rename'; } else $this->err[$pict] = 'unzip'; } } if ( $this->err ) foreach ( $this->err as $key => $value ) { switch ( $key ) { case 'ok': foreach ( $value as $msg ) $this->_msg( array( 'ok_img', $msg ) ); break; default : // $value E ( rename, unzip) => er_img_rename, er_img_unzip $this->_msg( array( 'er_img_'.$value, $key ) ); } } } function _unzip( $entrie ) { ### _unzip : extract $entrie file from $this->odtFile to $this->uploadDir using $this->ZIP object instance of ZipArchive Class ### # $entrie : string : fullFileName (with the internal path in the archive) # OUTPUT : # * true -> extraction ok # * false -> something wrong; using _msg to display what's wrong if ( ! $this->ZIP ) return $this->_msg('er_unzip_object'); if ( ! file_exists( $this->odtFile ) ) return $this->_msg('er_unzip_nofile'); if ( ! ( $this->ZIP->open( $this->odtFile ) === true ) ) return $this->_msg( 'er_unzip_open' ); $res = $this->ZIP->extractTo( $this->uploadDir, $entrie ); $this->ZIP->close(); if ( ! $res ) return $this->_msg( array( 'er_unzip_error', $entrie ) ); $this->file_extract[] = $this->uploadDir.'/'.$entrie; return $this->_msg( array( 'ok_unzip', $entrie ) ); } }