diff -urp uploadpath_6x/uploadpath.info uploadpath_6x-per-node-type/uploadpath.info --- uploadpath_6x/uploadpath.info 2008-06-24 21:07:34.000000000 +0200 +++ uploadpath_6x-per-node-type/uploadpath.info 2008-06-24 17:36:43.000000000 +0200 @@ -1,6 +1,6 @@ ; $Id: $ -name = Upload path -description = Organize uploaded files according to admin-specified rules. +name = Upload path per node-type +description = Organize uploaded files according to admin-specified rules. Per node-type patch applied. core = 6.x dependencies[] = upload dependencies[] = token diff -urp uploadpath_6x/uploadpath.module uploadpath_6x-per-node-type/uploadpath.module --- uploadpath_6x/uploadpath.module 2008-06-24 21:08:06.000000000 +0200 +++ uploadpath_6x-per-node-type/uploadpath.module 2008-06-24 20:30:19.000000000 +0200 @@ -3,7 +3,7 @@ /** * @file - * Upload Path module; allows admins to specify directory for uploaded files. + * Upload Path module; allows admins to specify directory for uploaded files. Per node-type patch applied. */ /** @@ -32,9 +32,29 @@ function uploadpath_admin_settings() { $form['uploadpath_prefix'] = array( '#type' => 'textfield', - '#title' => t('Pattern for the file prefix'), + '#title' => t('Default pattern for the file path prefix'), '#description' => t('Specify the pattern to prefix to file names uploaded with the upload module. It will be appended after the site files directory (e.g., files) but before the file name itself. Do not include a leading or trailing slash. Spaces will be converted to underscores to avoid file system issues.'), '#default_value' => variable_get('uploadpath_prefix', ''), + '#weight' => -9 + ); + + $form['node_types_exclude'] = array( + '#title' => t('Excluded node types'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('Select the node types to exclude from upload path processing'), + '#weight' => -8 + ); + + $form['node_types'] = array( + '#title' => t('Patterns for each node type'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('Patterns for node types. If empty, the default pattern will be used.'), + '#weight' => -7 + ); $form['token_help'] = array( @@ -43,11 +63,40 @@ function uploadpath_admin_settings() { '#collapsible' => TRUE, '#collapsed' => TRUE, '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'), + '#weight' => -6 + ); $form['token_help']['help'] = array( '#value' => theme('token_help', 'node'), ); + //node type settings + $node_types = node_get_types(); + foreach($node_types as $type){ + $types[$type->type] = $type->name; + } + //set excluded node types + $form['node_types_exclude']['uploadpath_excluded_node_types'] = array( + '#type' => 'select', + '#multiple' => true, + '#title' => t('Excluded Node Types'), + '#default_value' => variable_get('uploadpath_excluded_node_types', array()), + '#description' => t('Select the node types to exclude from uploadpath processing.'), + '#options' => $types, + ); + + foreach($node_types as $type){ + //only show settings if not excluded + if(!in_array($type->type, variable_get('uploadpath_excluded_node_types',array()))){ + $form['node_types']['uploadpath_prefix_'.$type->type] = array( + '#type' => 'textfield', + '#title' => t('Path pattern for '.$type->name), + '#description' => t('Specify the path pattern to prefix for '.$type->name.' type nodes'), + '#default_value' => variable_get('uploadpath_prefix_'.$type->type, ''), + ); + } + } + return system_settings_form($form); } @@ -58,26 +107,30 @@ function uploadpath_nodeapi(&$node, $op, switch ($op) { case 'insert': case 'update': - if (user_access('upload files')) { - if (isset($node->files)) { - foreach ($node->files as $key => $file) { - if($file['remove']){ + if(!in_array($node->type, variable_get('uploadpath_excluded_node_types',array()))){ + if (user_access('upload files')) { + if (isset($node->files)) { + foreach ($node->files as $key => $file) { + if($file['remove']){ // file is slated to be deleted continue; } + } + //get the token path pattern + $pattern = variable_get('uploadpath_prefix_'.$node->type,false); + if(!$pattern){ + //default pattern + $pattern = variable_get('uploadpath_prefix', ''); + } + // Get the new, prefixed file name - $tokenized = token_replace(variable_get('uploadpath_prefix', '') . '/', 'node', $node); + $tokenized = token_replace($pattern .'/', 'node', $node); $tokenized = str_replace(array(' ', "\n", "\t"), '_', $tokenized); $dest = $tokenized . $file['filename']; // see if we need to move it, or if it's already ok if($file['filepath'] != file_directory_path() . '/' . $dest){ - // SECURITY NOTE: - // Tokens include user supplied information and could provide an attack vector. - // The current method of creating directories prevents the use of .. or other malicious - // paths, but future developers should keep this in mind when modifying the following code - // Create the directory if it doesn't exist yet. $dirs = explode('/', dirname($dest)); $directory = file_directory_path();