Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.70.2.30.2.91.2.25
diff -u -p -r1.70.2.30.2.91.2.25 privatemsg.module
--- privatemsg.module	22 Feb 2009 21:21:08 -0000	1.70.2.30.2.91.2.25
+++ privatemsg.module	4 Mar 2009 12:36:32 -0000
@@ -642,14 +642,7 @@ function privatemsg_new(&$form_state, $a
     '#weight'             => 20,
   );
   $form['#validate'][]    = 'pm_send_validate';
-
-  //modules can store data here, everything stored here will be available
-  //in $message, similar to #node
-
-
-
-
-
+  
   return $form;
 }
 
@@ -658,6 +651,10 @@ function pm_send_validate($form, &$form_
   // The actual message that is being sent, we create this during validation and pass to submit to send out.
   $message = array();
 
+  // Store content of $form_state['storage'] in message
+  if (is_array($form_state['storage'])) {
+    $message += $form_state['storage'];
+  }
   $message['body']      = $form_state['values']['body'];
   $message['subject']   = $form_state['values']['subject'];
   $message['author']    = $form_state['values']['author'];
Index: privatemsg_attachments/privatemsg_attachments.info
===================================================================
RCS file: privatemsg_attachments/privatemsg_attachments.info
diff -N privatemsg_attachments/privatemsg_attachments.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ privatemsg_attachments/privatemsg_attachments.info	4 Mar 2009 12:36:32 -0000
@@ -0,0 +1,9 @@
+; $Id$
+name = Privatemsg attachments
+description = Allow users to add attachments to messages
+package = Mail
+core = 6.x
+dependencies[] = privatemsg
+dependencies[] = upload
+
+
Index: privatemsg_attachments/privatemsg_attachments.install
===================================================================
RCS file: privatemsg_attachments/privatemsg_attachments.install
diff -N privatemsg_attachments/privatemsg_attachments.install
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ privatemsg_attachments/privatemsg_attachments.install	4 Mar 2009 12:36:32 -0000
@@ -0,0 +1,77 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Installation hooks for privatemsg_attachments
+ */
+
+/**
+ * Implementation of hook_install().
+ */
+function privatemsg_attachments_install() {
+  drupal_install_schema('privatemsg_attachments');
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function privatemsg_attachments_uninstall() {
+  // Remove tables.
+  drupal_uninstall_schema('privatemsg_attachments');
+}
+
+/**
+ * Implementation of hook_schema().
+ */
+function privatemsg_attachments_schema() {
+  $schema['pm_attachments'] = array(
+    'description' => t('Stores uploaded file information and table associations for private messages.'),
+    'fields' => array(
+      'fid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        'description' => t('Primary Key: The {files}.fid.'),
+      ),
+      'mid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        'description' => t('The {pm_message}.mid associated with the uploaded file.'),
+      ),
+      'description' => array(
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+        'description' => t('Description of the uploaded file.'),
+      ),
+      'list' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        'size' => 'tiny',
+        'description' => t('Whether the file should be visibly listed on the node: yes(1) or no(0).'),
+      ),
+      'weight' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+        'size' => 'tiny',
+        'description' => t('Weight of this upload in relation to other uploads in this node.'),
+      ),
+    ),
+    'primary key' => array('mid', 'fid'),
+    'indexes' => array(
+      'fid' => array('fid'),
+    ),
+  );
+
+  return $schema;
+}
+
+
Index: privatemsg_attachments/privatemsg_attachments.module
===================================================================
RCS file: privatemsg_attachments/privatemsg_attachments.module
diff -N privatemsg_attachments/privatemsg_attachments.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ privatemsg_attachments/privatemsg_attachments.module	4 Mar 2009 12:36:33 -0000
@@ -0,0 +1,276 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Allows users to add attachments to messages
+ */
+
+function privatemsg_attachments_form_alter(&$form, $form_state, $form_id) {
+  if ($form_id == 'privatemsg_new' && user_access('upload files')) {
+    $form['privatemsg']['attachments'] = array(
+        '#type' => 'fieldset',
+        '#access' => user_access('upload files'),
+        '#title' => t('File attachments'),
+        '#collapsible' => TRUE,
+        '#collapsed' => empty($form['#privatemsg_message']['files']),
+        '#description' => t('Changes made to the attachments are not permanent until you send this message.'),
+        '#prefix' => '<div class="attachments">',
+        '#suffix' => '</div>',
+        '#weight' => 1,
+    );
+
+
+    // Wrapper for fieldset contents (used by ahah.js).
+    $form['privatemsg']['attachments']['wrapper'] = array(
+        '#prefix' => '<div id="attach-wrapper">',
+        '#suffix' => '</div>',
+    );
+
+    // Make sure necessary directories for upload.module exist and are
+    // writable before displaying the attachment form.
+    $path = file_directory_path();
+    $temp = file_directory_temp();
+    // Note: pass by reference
+    if (!file_check_directory($path, FILE_CREATE_DIRECTORY) || !file_check_directory($temp, FILE_CREATE_DIRECTORY)) {
+      $form['attachments']['#description'] =  t('File attachments are disabled. The file directories have not been properly configured.');
+      if (user_access('administer site configuration')) {
+        $form['attachments']['#description'] .= ' '. t('Please visit the <a href="@admin-file-system">file system configuration page</a>.', array('@admin-file-system' => url('admin/settings/file-system')));
+      }
+      else {
+        $form['attachments']['#description'] .= ' '. t('Please contact the site administrator.');
+      }
+    }
+    else {
+      $files = array();
+      if (!empty($form['#privatemsg_message']['files'])) {
+        $files = $form['#privatemsg_message']['files'];
+      }
+
+      $form['privatemsg']['attachments']['wrapper'] += _privatemsg_upload_form($files);
+      $form['#attributes']['enctype'] = 'multipart/form-data';
+    }
+  }
+}
+
+function _privatemsg_upload_form($files = array()) {
+  global $user;
+
+  $form = array(
+    '#theme' => 'upload_form_new',
+    '#cache' => TRUE,
+  );
+
+  if (!empty($files)) {
+    $form['files']['#theme'] = 'upload_form_current';
+    $form['files']['#tree'] = TRUE;
+    foreach ($files as $key => $file) {
+      $file = (object)$file;
+      $description = file_create_url($file->filepath);
+      $description = "<small>". check_plain($description) ."</small>";
+      $form['files'][$key]['description'] = array('#type' => 'textfield', '#default_value' => !empty($file->description) ? $file->description : $file->filename, '#maxlength' => 256, '#description' => $description );
+      $form['files'][$key]['size'] = array('#value' => format_size($file->filesize));
+      $form['files'][$key]['remove'] = array('#type' => 'checkbox', '#default_value' => !empty($file->remove));
+      $form['files'][$key]['list'] = array('#type' => 'checkbox',  '#default_value' => $file->list);
+      $form['files'][$key]['weight'] = array('#type' => 'weight', '#delta' => count($files), '#default_value' => $file->weight);
+      $form['files'][$key]['filename'] = array('#type' => 'value',  '#value' => $file->filename);
+      $form['files'][$key]['filepath'] = array('#type' => 'value',  '#value' => $file->filepath);
+      $form['files'][$key]['filemime'] = array('#type' => 'value',  '#value' => $file->filemime);
+      $form['files'][$key]['filesize'] = array('#type' => 'value',  '#value' => $file->filesize);
+      $form['files'][$key]['fid'] = array('#type' => 'value',  '#value' => $file->fid);
+      $form['files'][$key]['new'] = array('#type' => 'value', '#value' => FALSE);
+    }
+  }
+
+  $limits = _upload_file_limits($user);
+  $form['new']['#weight'] = 10;
+  $form['new']['upload'] = array(
+      '#type' => 'file',
+      '#title' => t('Attach new file'),
+      '#size' => 40,
+      '#description' => ($limits['resolution'] ? t('Images are larger than %resolution will be resized.', array('%resolution' => $limits['resolution'])) : '') .' '. t('The maximum upload size is %filesize. Only files with the following extensions may be uploaded: %extensions.', array('%extensions' => $limits['extensions'], '%filesize' => format_size($limits['file_size']))),
+  );
+  $form['new']['attach'] = array(
+      '#type' => 'submit',
+      '#value' => t('Attach'),
+      '#name' => 'attach',
+      '#ahah' => array(
+        'path' => 'messages/upload/js',
+        'wrapper' => 'attach-wrapper',
+        'progress' => array('type' => 'bar', 'message' => t('Please wait...')),
+    ),
+  );
+  return $form;
+}
+
+function privatemsg_attachments_menu() {
+  $items['messages/upload/js'] = array(
+    'page callback' => 'privatemsg_attachments_upload_js',
+    'access arguments' => array('upload files'),
+    'type' => MENU_CALLBACK,
+  );
+  return $items;
+}
+
+/**
+ * Menu-callback for JavaScript-based uploads.
+ */
+function privatemsg_attachments_upload_js() {
+  $files = array();
+
+  $form_state = array('values' => $_POST, 'storage' => NULL, 'submitted' => FALSE);
+  // Load the form from the Form API cache.
+  if (!($cached_form = form_get_cache($_POST['form_build_id'], $form_state)) || !isset($cached_form['privatemsg']['attachments'])) {
+    form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
+    $output = theme('status_messages');
+
+    print drupal_to_js(array('status' => TRUE, 'data' => $output));
+    exit();
+  }
+
+  // Handle new uploads, and merge tmp files into node-files.
+  _privatemsg_attachments_upload_submit($cached_form, $form_state);
+
+  if (!empty($form_state['values']['files'])) {
+    foreach ($form_state['values']['files'] as $fid => $file) {
+      if (isset($form_state['storage']['files'][$fid]))
+        $files[$fid] = $form_state['storage']['files'][$fid];
+    }
+  }
+
+  $form = _privatemsg_upload_form($files);
+
+  unset($cached_form['privatemsg']['attachments']['wrapper']['new']);
+  $cached_form['privatemsg']['attachments']['wrapper'] = array_merge($cached_form['privatemsg']['attachments']['wrapper'], $form);
+
+  $cached_form['privatemsg']['attachments']['#collapsed'] = FALSE;
+
+  form_set_cache($_POST['form_build_id'], $cached_form, $form_state);
+
+  foreach ($files as $fid => $file) {
+    if (is_numeric($fid)) {
+      $form['files'][$fid]['description']['#default_value'] = $form_state['values']['files'][$fid]['description'];
+      $form['files'][$fid]['list']['#default_value'] = !empty($form_state['values']['files'][$fid]['list']);
+      $form['files'][$fid]['remove']['#default_value'] = !empty($form_state['values']['files'][$fid]['remove']);
+      $form['files'][$fid]['weight']['#default_value'] = $form_state['values']['files'][$fid]['weight'];
+    }
+  }
+
+  // Render the form for output.
+  $form += array(
+    '#post' => $_POST,
+    '#programmed' => FALSE,
+    '#tree' => FALSE,
+    '#parents' => array(),
+  );
+  drupal_alter('form', $form, array(), '_privatemsg_upload_form');
+  $form_state = array('submitted' => FALSE);
+  $form = form_builder('_privatemsg_upload_form', $form, $form_state);
+
+  $output = theme('status_messages') . drupal_render($form);
+
+  // We send the updated file attachments form.
+  // Don't call drupal_json(). ahah.js uses an iframe and
+  // the header output by drupal_json() causes problems in some browsers.
+  print drupal_to_js(array('status' => TRUE, 'data' => $output));
+  exit;
+}
+
+function _privatemsg_attachments_upload_submit(&$form, &$form_state) {
+  global $user;
+
+  $limits = _upload_file_limits($user);
+  $validators = array(
+    'file_validate_extensions' => array($limits['extensions']),
+    'file_validate_image_resolution' => array($limits['resolution']),
+    'file_validate_size' => array($limits['file_size'], $limits['user_size']),
+  );
+
+  // Save new file uploads.
+  if (user_access('upload files') && ($file = file_save_upload('upload', $validators, file_directory_path()))) {
+    $file->list = variable_get('upload_list_default', 1);
+    $file->description = $file->filename;
+    $file->weight = 0;
+    $file->new = TRUE;
+    $form_state['values']['files'][$file->fid] = (array)$file;
+    $form_state['storage']['files'][$file->fid] = (array)$file;
+  }
+
+  // Order the form according to the set file weight values.
+  if (!empty($form_state['values']['files'])) {
+    $microweight = 0.001;
+    foreach ($form_state['values']['files'] as $fid => $file) {
+      if (is_numeric($fid)) {
+        $form_state['values']['files'][$fid]['#weight'] = $file['weight'] + $microweight;
+        $microweight += 0.001;
+      }
+    }
+    uasort($form_state['values']['files'], 'element_sort');
+  }
+}
+
+function privatemsg_attachments_privatemsg_message_view_alter(&$vars) {
+  if (!empty($vars['message']['files'])) {
+    $vars['message_body'] .= theme('upload_attachments', $vars['message']['files']);
+  }
+}
+
+function privatemsg_attachments_privatemsg_message_insert($message) {
+
+  if (empty($message['files']) || !is_array($message['files'])) {
+    return;
+  }
+
+  foreach ($message['files'] as $fid => $file) {
+    // Convert file to object for compatibility
+    $file = (object)$file;
+
+    // Remove file. Process removals first since no further processing
+    // will be required.
+    if (!empty($file->remove)) {
+      db_query('DELETE FROM {pm_attachments} WHERE fid = %d AND mid = %d', $fid, $message['mid']);
+
+      file_delete($file->filepath);
+      db_query('DELETE FROM {files} WHERE fid = %d', $fid);
+
+      // Remove it from the session in the case of new uploads,
+      // that you want to disassociate before node submission.
+      unset($message['files'][$fid]);
+      // Move on, so the removed file won't be added to new revisions.
+      continue;
+    }
+
+    // Create a new revision, or associate a new file needed.
+    if ($file->new) {
+      db_query("INSERT INTO {pm_attachments} (fid, mid, list, description, weight) VALUES (%d, %d, %d, '%s', %d)", $file->fid, $message['mid'], $file->list, $file->description, $file->weight);
+      file_set_status($file, FILE_STATUS_PERMANENT);
+    }
+  }
+}
+
+function privatemsg_attachments_privatemsg_message_load($message) {
+  $files = array();
+
+  $result = db_query('SELECT * FROM {files} f INNER JOIN {pm_attachments} pma ON f.fid = pma.fid WHERE pma.mid = %d ORDER BY pma.weight, f.fid', $message['mid']);
+  while ($file = db_fetch_object($result)) {
+    $files[$file->fid] = $file;
+  }
+
+  return array('files' => $files);
+}
+
+function privatemsg_attachments_privatemsg_message_flush($message) {
+
+  if (!isset($message['files'])) {
+    return;
+  }
+
+  foreach ($message['files'] as $fid => $file) {
+    // Delete all files associated with the node
+    db_query('DELETE FROM {files} WHERE fid = %d', $fid);
+    file_delete($file->filepath);
+  }
+
+  // Delete all file revision information associated with the node
+  db_query('DELETE FROM {pm_attachments} WHERE mid = %d', $message['mid']);
+}
