liberty
[ class tree: liberty ] [ index: liberty ] [ all elements ]

Source for file mime.default.php

Documentation is available at mime.default.php

  1. <?php
  2. /**
  3.  * @version     $Header$
  4.  *
  5.  * @author      xing  <xing@synapse.plus.com>
  6.  * @version     $Revision$
  7.  *  created      Thursday May 08, 2008
  8.  * @package     liberty
  9.  * @subpackage  liberty_mime_handler
  10.  *
  11.  * @TODO since plugins can do just about anything here, we might need the<br>
  12.  *  option to create specific tables during install. if required we can scan for<br>
  13.  *  files called:<br>
  14.  *  table.plugin_guid.php<br>
  15.  *  where plugins can insert their own tables<br>
  16.  ***/
  17.  
  18.  
  19. /**
  20.  * setup
  21.  */
  22. global $gLibertySystem;
  23.  
  24. /**
  25.  *  This is the name of the plugin - max char length is 16
  26.  * As a naming convention, the liberty mime handler definition should start with:
  27.  * PLUGIN_MIME_GUID_
  28.  */
  29. define'PLUGIN_MIME_GUID_DEFAULT''mimedefault' );
  30.  
  31. $pluginParams array (
  32.     // Set of functions and what they are called in this paricular plugin
  33.     // Use the GUID as your namespace
  34.     'verify_function'    => 'mime_default_verify',
  35.     'store_function'     => 'mime_default_store',
  36.     'update_function'    => 'mime_default_update',
  37.     'load_function'      => 'mime_default_load',
  38.     'download_function'  => 'mime_default_download',
  39.     'expunge_function'   => 'mime_default_expunge',
  40.     // Brief description of what the plugin does
  41.     'title'              => 'Default File Handler',
  42.     'description'        => 'This mime handler can handle any file type, creates thumbnails when possible and will make the file available as an attachment.',
  43.     // Templates to display the files
  44.     'upload_tpl'         => 'bitpackage:liberty/mime/default/upload.tpl',
  45.     'view_tpl'           => 'bitpackage:liberty/mime/default/view.tpl',
  46.     'inline_tpl'         => 'bitpackage:liberty/mime/default/inline.tpl',
  47.     'storage_tpl'        => 'bitpackage:liberty/mime/default/storage.tpl',
  48.     'attachment_tpl'     => 'bitpackage:liberty/mime/default/attachment.tpl',
  49.     // This should be the same for all mime plugins
  50.     'plugin_type'        => MIME_PLUGIN,
  51.     // This needs to be specified by plugins that are included by other plugins
  52.     'file_name'          => 'mime.default.php',
  53.     // Set this to TRUE if you want the plugin active right after installation
  54.     'auto_activate'      => TRUE,
  55.     // Help page on bitweaver.org
  56.     //'help_page'          => 'MimeHelpPage',
  57.  
  58.     // Here you can use a perl regular expression to pick out file extensions you want to handle
  59.     // e.g.: Some image types: '#^image/(jpe?g|gif|png)#i'
  60.     // This plugin will be picked if nothing matches.
  61.     //'mimetypes'          => array( '/.*/' ),
  62. );
  63. $gLibertySystem->registerPluginPLUGIN_MIME_GUID_DEFAULT$pluginParams );
  64.  
  65. /**
  66.  * Sanitise and validate data before it's stored
  67.  *
  68.  * @param array $pStoreRow Hash of data that needs to be stored
  69.  * @param array $pStoreRow['upload'] Hash passed in by $_FILES upload
  70.  * @access public
  71.  * @return TRUE on success, FALSE on failure - $pStoreRow['errors'] will contain reason
  72.  */
  73. if!function_exists'mime_default_verify' )) {
  74.     function mime_default_verify&$pStoreRow {
  75.         global $gBitSystem$gBitUser;
  76.         $ret FALSE;
  77.  
  78.         // if we have a user_id set, we use that.
  79.         if!empty$pStoreRow['upload']['user_id')) {
  80.             $pStoreRow['user_id'$pStoreRow['upload']['user_id'];
  81.         else {
  82.             // storage is always owned by the user that uploaded it!
  83.             // er... or at least admin if somehow we have a NULL mUserId
  84.             $pStoreRow['user_id'@BitBase::verifyId$gBitUser->mUserId $gBitUser->mUserId ROOT_USER_ID;
  85.             if$pStoreRow['user_id'{
  86.                 bit_error_log'The user_id for the upload was not set. Defaulted to user_id = '.$pStoreRow['user_id'].' where 1 = ROOT_USER_ID, -1 = ANONYMOUS_USER_ID, other values = big problem.' );
  87.             }
  88.         }
  89.  
  90.         if!empty$pStoreRow['upload']['tmp_name'&& is_file$pStoreRow['upload']['tmp_name')) {
  91.             // attachment_id is only set when we are updating the file
  92.             if@BitBase::verifyId$pStoreRow['upload']['attachment_id')) {
  93.                 // if a new file has been uploaded, we need to get some information from the database for the file update
  94.                 $fileInfo $gBitSystem->mDb->getRow"
  95.                     SELECT la.`attachment_id`, lf.`file_id`, lf.`file_name`
  96.                     FROM `".BIT_DB_PREFIX."liberty_attachments` la
  97.                     INNER JOIN `".BIT_DB_PREFIX."liberty_files` lf ON ( lf.`file_id` = la.`foreign_id` )
  98.                     WHERE la.`attachment_id` = ?"array$pStoreRow['upload']['attachment_id'));
  99.                 $pStoreRow array_merge$pStoreRow$fileInfo );
  100.             else {
  101.                 $pStoreRow['attachment_id'$gBitSystem->mDb->GenID'liberty_attachments_id_seq' );
  102.             }
  103.             // try to generate thumbnails for the upload
  104.             ifisset$pStoreRow['upload']['thumbnail') ) {
  105.                 $pStoreRow['upload']['thumbnail'$pStoreRow['upload']['thumbnail'];
  106.             else {
  107.                 $pStoreRow['upload']['thumbnail'TRUE;
  108.             }
  109.  
  110.             // Generic values needed by the storing mechanism
  111.             $pStoreRow['upload']['source_file'$pStoreRow['upload']['tmp_name'];
  112.  
  113.             // Store all uploaded files in the users storage area
  114.             ifempty$pStoreRow['upload']['dest_branch')) {
  115.                 $pStoreRow['upload']['dest_branch'liberty_mime_get_storage_brancharray'sub_dir'=>$pStoreRow['attachment_id']'user_id'=>$pStoreRow['user_id']'package'=>liberty_mime_get_storage_sub_dir_name$pStoreRow['upload') ) );
  116.             }
  117.  
  118.             $ret TRUE;
  119.         else {
  120.             $pStoreRow['errors']['upload'tra'There was a problem verifying the uploaded file.' );
  121.         }
  122.  
  123.         return $ret;
  124.     }
  125. }
  126.  
  127. /**
  128.  * When a file is edited
  129.  *
  130.  * @param array $pStoreRow File data needed to store details in the database - sanitised and generated in the verify function
  131.  * @access public
  132.  * @return TRUE on success, FALSE on failure - $pStoreRow['errors'] will contain reason
  133.  */
  134. if!function_exists'mime_default_update' )) {
  135.     function mime_default_update&$pStoreRow {
  136.         global $gBitSystem;
  137.  
  138.         // this will reset the uploaded file
  139.         ifBitBase::verifyId$pStoreRow['attachment_id'&& !empty$pStoreRow['upload')) {
  140.             // Store all uploaded files in the users storage area
  141.             ifempty$pStoreRow['dest_branch')) {
  142.                 $pStoreRow['dest_branch'liberty_mime_get_storage_brancharray'sub_dir'=>$pStoreRow['attachment_id']'user_id'=>$pStoreRow['user_id']'package'=>liberty_mime_get_storage_sub_dir_name$pStoreRow['upload') ) );
  143.             }
  144.  
  145.             if!empty$pStoreRow['dest_branch'&& !empty$pStoreRow['file_name') ) {
  146.                 // First we remove the old file
  147.                 $path STORAGE_PKG_PATH.$pStoreRow['dest_branch'];
  148.                 $file $path.liberty_mime_get_default_file_name$pStoreRow['file_name']$pStoreRow['mime_type');
  149.                 if(( $nuke LibertyMime::validateStoragePath$path )) && is_dir$nuke )) {
  150.                     if!empty$pStoreRow['unlink_dir')) {
  151.                         @unlink_r$path );
  152.                         mkdir$path );
  153.                     else {
  154.                         @unlink$file );
  155.                     }
  156.                 }
  157.  
  158.                 // make sure we store the new file in the same place as before
  159.                 $pStoreRow['upload']['dest_branch'$pStoreRow['dest_branch'];
  160.  
  161.                 // if we can create new thumbnails for this file, we remove the old ones first
  162.                 $canThumbFunc liberty_get_function'can_thumbnail' );
  163.                 if!empty$canThumbFunc && $canThumbFunc$pStoreRow['upload']['type')) {
  164.                     liberty_clear_thumbnails$pStoreRow['upload');
  165.                 }
  166.  
  167.                 // Now we process the uploaded file
  168.                 if$storagePath liberty_process_upload$pStoreRow['upload')) {
  169.                     $sql "UPDATE `".BIT_DB_PREFIX."liberty_files` SET `file_name` = ?, `mime_type` = ?, `file_size` = ?, `user_id` = ? WHERE `file_id` = ?";
  170.                     $gBitSystem->mDb->query$sqlarray$pStoreRow['upload']['name']$pStoreRow['upload']['type']$pStoreRow['upload']['size']$pStoreRow['user_id']$pStoreRow['file_id'));
  171.                 }
  172.  
  173.                 // ensure we have the correct guid in the db
  174.                 ifempty$pStoreRow['attachment_plugin_guid')) {
  175.                     $pStoreRow['attachment_plugin_guid'LIBERTY_DEFAULT_MIME_HANDLER;
  176.                 }
  177.  
  178.                 $gBitSystem->mDb->associateUpdate(
  179.                     BIT_DB_PREFIX."liberty_attachments",
  180.                     array'attachment_plugin_guid' => $pStoreRow['attachment_plugin_guid'),
  181.                     array'attachment_id' => $pStoreRow['attachment_id')
  182.                 );
  183.             }
  184.         }
  185.         return TRUE;
  186.     }
  187. }
  188.  
  189. /**
  190.  * Store the data in the database
  191.  *
  192.  * @param array $pStoreRow File data needed to store details in the database - sanitised and generated in the verify function
  193.  * @access public
  194.  * @return TRUE on success, FALSE on failure - $pStoreRow['errors'] will contain reason
  195.  */
  196. if!function_exists'mime_default_store' )) {
  197.     function mime_default_store&$pStoreRow {
  198.         global $gBitSystem$gLibertySystem;
  199.         $ret FALSE;
  200.  
  201.         // take care of the uploaded file and insert it into the liberty_files and liberty_attachments tables
  202.         if$storagePath liberty_process_upload$pStoreRow['upload']empty$pStoreRow['upload']['copy_file'))) {
  203.             // add row to liberty_files
  204.             $storeHash array(
  205.                 "file_name"    => $pStoreRow['upload']['name'],
  206.                 "file_id"      => $gBitSystem->mDb->GenID'liberty_files_id_seq' ),
  207.                 "mime_type"    => $pStoreRow['upload']['type'],
  208.                 "file_size"    => $pStoreRow['upload']['size'],
  209.                 "user_id"      => $pStoreRow['user_id'],
  210.             );
  211.             $gBitSystem->mDb->associateInsertBIT_DB_PREFIX."liberty_files"$storeHash );
  212.  
  213.             // add the data into liberty_attachments to make this file available as attachment
  214.             $storeHash array(
  215.                 "attachment_plugin_guid" => !empty$pStoreRow['attachment_plugin_guid'$pStoreRow['attachment_plugin_guid'PLUGIN_MIME_GUID_DEFAULT,
  216.                 "attachment_id"          => $pStoreRow['attachment_id'],
  217.                 "content_id"             => $pStoreRow['content_id'],
  218.                 "foreign_id"             => $storeHash['file_id'],
  219.                 "user_id"                => $pStoreRow['user_id'],
  220.             );
  221.             $gBitSystem->mDb->associateInsertBIT_DB_PREFIX."liberty_attachments"$storeHash );
  222.  
  223.             $ret TRUE;
  224.         else {
  225.             $pStoreRow['errors']['liberty_process'"There was a problem processing the file.";
  226.         }
  227.         return $ret;
  228.     }
  229. }
  230.  
  231. /**
  232.  * Load file data from the database
  233.  *
  234.  * @param array $pFileHash contains all file information
  235.  * @access public
  236.  * @return TRUE on success, FALSE on failure - ['errors'] will contain reason for failure
  237.  */
  238. if!function_exists'mime_default_load' )) {
  239.     function mime_default_load$pFileHash&$pPrefs {
  240.         global $gBitSystem$gLibertySystem;
  241.         $ret FALSE;
  242.         if@BitBase::verifyId$pFileHash['attachment_id')) {
  243.             $query "
  244.                 SELECT la.`attachment_id`, la.`content_id`, la.`attachment_plugin_guid`, la.`foreign_id`, la.`user_id`, la.`is_primary`, la.`pos`, la.`error_code`, la.`caption`, la.`hits` AS `downloads`,
  245.                     lf.`file_id`, lf.`user_id`, lf.`file_name`, lf.`file_size`, lf.`mime_type`
  246.                 FROM `".BIT_DB_PREFIX."liberty_attachments` la
  247.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files` lf ON( la.`foreign_id` = lf.`file_id` )
  248.                 WHERE la.`attachment_id`=?";
  249.             if$row $gBitSystem->mDb->getRow$queryarray$pFileHash['attachment_id'))) {
  250.                 $ret array_merge$pFileHash$row );
  251.                 $storageName basename$row['file_name');
  252.                 // compatibility with _FILES hash
  253.                 $row['name'$storageName;
  254.                 $defaultFileName liberty_mime_get_default_file_name$row['file_name']$row['mime_type');
  255.                 $storageBranchPath liberty_mime_get_storage_brancharray'sub_dir' => $row['attachment_id']'user_id' =>$row['user_id']'package' => liberty_mime_get_storage_sub_dir_name$row ) ) );
  256.                 $storageBranch $storageBranchPath.$defaultFileName;
  257.                 if!file_existsSTORAGE_PKG_PATH.$storageBranch ) ) {
  258.                     $storageBranch liberty_mime_get_storage_brancharray'sub_dir' => $row['attachment_id']'user_id' =>$row['user_id']'package' => liberty_mime_get_storage_sub_dir_name$row ) ) ).$storageName;
  259.                 }
  260.  
  261.                 // this will fetch the correct thumbnails
  262.                 $thumbHash['source_file'STORAGE_PKG_PATH.$storageBranch;
  263.                 $row['source_file'STORAGE_PKG_PATH.$storageBranch;
  264.                 $canThumbFunc liberty_get_function'can_thumbnail' );
  265.                 if$canThumbFunc && $canThumbFunc$row['mime_type')) {
  266.                     $thumbHash['default_image'LIBERTY_PKG_URL.'icons/generating_thumbnails.png';
  267.                 }
  268.                 $ret['thumbnail_url'liberty_fetch_thumbnails$thumbHash );
  269.                 // indicate that this is a mime thumbnail
  270.                 if!empty$ret['thumbnail_url']['medium'&& strpos$ret['thumbnail_url']['medium']'/mime/' )) {
  271.                     $ret['thumbnail_is_mime'TRUE;
  272.                 }
  273.  
  274.                 // pretty URLs
  275.                 if$gBitSystem->isFeatureActive"pretty_urls" || $gBitSystem->isFeatureActive"pretty_urls_extended" )) {
  276.                     $ret['display_url'LIBERTY_PKG_URL."view/file/".$row['attachment_id'];
  277.                 else {
  278.                     $ret['display_url'LIBERTY_PKG_URL."view_file.php?attachment_id=".$row['attachment_id'];
  279.                 }
  280.  
  281.                 // legacy table data was named storage path and included a partial path. strip out any path just in case
  282.                 $ret['file_name']    $storageName;
  283.                 $ret['preferences'$pPrefs;
  284.  
  285.                 // some stuff is only available if we have a source file
  286.                 //    make sure to check for these when you use them. frequently the original might not be available
  287.                 //    e.g.: video files are large and the original might be deleted after conversion
  288.                 ifis_fileSTORAGE_PKG_PATH.$storageBranch )) {
  289.                     $ret['mime_type'$row['mime_type'];
  290.                     $ret['source_file']   STORAGE_PKG_PATH.$storageBranch;
  291.                     $ret['source_url']    STORAGE_PKG_URL.$storageBranch;
  292.                     $ret['last_modified'filemtime$ret['source_file');
  293.                     $ret['download_url'LibertyMime::getAttachmentDownloadUrl$row['attachment_id');
  294.                 }
  295.  
  296.                 // add a description of how to insert this file into a wiki page
  297.                 if$gLibertySystem->isPluginActive'dataattachment' )) {
  298.                     $ret['wiki_plugin_link'"{attachment id=".$row['attachment_id']."}";
  299.                 }
  300.             }
  301.         }
  302.  
  303.         return $ret;
  304.     }
  305. }
  306.  
  307. /**
  308.  * Takes care of the entire download process. Make sure it doesn't die at the end.
  309.  * in this functioin it would be possible to add download resume possibilites and the like
  310.  *
  311.  * @param array $pFileHash Basically the same has as returned by the load function
  312.  * @access public
  313.  * @return TRUE on success, FALSE on failure - $pParamHash['errors'] will contain reason for failure
  314.  */
  315. if!function_exists'mime_default_download' )) {
  316.     function mime_default_download&$pFileHash {
  317.         global $gBitSystem;
  318.         $ret FALSE;
  319.  
  320.         // Check to see if the file actually exists
  321.         if!empty$pFileHash['source_file'&& is_readable$pFileHash['source_file')) {
  322.             // make sure we close off obzip compression if it's on
  323.             if$gBitSystem->isFeatureActive'site_output_obzip' )) {
  324.                 @ob_end_clean();
  325.             }
  326.  
  327.             // this will get the browser to open the download dialogue - even when the
  328.             // browser could deal with the content type - not perfect, but works
  329.             if$gBitSystem->isFeatureActive'mime_force_download' )) {
  330.                 $pFileHash['mime_type'"application/force-download";
  331.             }
  332.  
  333.             // set up header
  334.             header"Cache Control:  no-cache, must-revalidate" );
  335.             header"Expires: 0" );
  336.             header"Accept-Ranges: bytes" );
  337.             header"Pragma: public" );
  338.             header"Last-Modified: ".gmdate"D, d M Y H:i:s T"$pFileHash['last_modified')TRUE200 );
  339.             header'Content-Disposition: attachment; filename="'.$pFileHash['file_name'].'"' );
  340.             header"Content-type: ".$pFileHash['mime_type');
  341.             header"Content-Description: File Transfer" );
  342.             header"Content-Length: ".filesize$pFileHash['source_file'));
  343.             header"Content-Transfer-Encoding: binary" );
  344.             //header( "Connection: close" );
  345.  
  346.             @ob_clean();
  347.             flush();
  348.             readfile$pFileHash['source_file');
  349.             $ret TRUE;
  350.         else {
  351.             $pFileHash['errors']['no_file'tra'No matching file found.' );
  352.         }
  353.         return $ret;
  354.     }
  355. }
  356.  
  357. /**
  358.  * Nuke data in tables when content is removed
  359.  *
  360.  * @param array $pParamHash The contents of LibertyMime->mInfo
  361.  * @access public
  362.  * @return TRUE on success, FALSE on failure - $pParamHash['errors'] will contain reason for failure
  363.  */
  364. if!function_exists'mime_default_expunge' )) {
  365.     function mime_default_expunge$pAttachmentId {
  366.         global $gBitSystem$gBitUser;
  367.         $ret FALSE;
  368.         if@BitBase::verifyId$pAttachmentId )) {
  369.             if$fileHash LibertyMime::loadAttachment$pAttachmentId )) {
  370.                 if$gBitUser->isAdmin(|| $gBitUser->mUserId == $fileHash['user_id'{
  371.                     // make sure this is a valid storage directory before removing it
  372.                     if!empty$fileHash['source_file'&& ($nuke LibertyMime::validateStoragePath$fileHash['source_file')) && is_file$nuke )) {
  373.                         unlink_rdirname$nuke ));
  374.                     }
  375.                     $query "DELETE FROM `".BIT_DB_PREFIX."liberty_files` WHERE `file_id` = ?";
  376.                     $gBitSystem->mDb->query$queryarray$fileHash['foreign_id'));
  377.                     $ret TRUE;
  378.                 }
  379.             }
  380.         }
  381.         return $ret;
  382.     }
  383. }
  384. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:06 +0000 by phpDocumentor 1.5.0-lsces