--- fileshare.module	2007-02-12 11:29:32.000000000 -0800
+++ fileshare.module.new	2007-06-22 15:23:12.000000000 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: fileshare.module,v 1.21.2.4 2007/02/12 19:29:32 jamier Exp $
+// $Id: fileshare.module,v 1.20 2007/01/16 23:06:06 jamier Exp $
 
 /**
  * @file
@@ -13,11 +13,47 @@
  *  ini_set('upload_max_filesize', '60M');
  *  ini_set('max_input_time', '300');
  *  ini_set('max_execution_time', '300'); // 5 minuites
- *
+ *  
  *  Also be sure to check that your temp directory has enough space.
  */
 
 /**
+ * Implementation of hook_help().
+ *
+ * Throughout Drupal, hook_help() is used to display help text at the top of
+ * pages. Some other parts of Drupal pages get explanatory text from these hooks
+ * as well. We use it here to provide a description of the module on the
+ * module administration page.
+ */
+function fileshare_help($section) {
+  switch ($section) {
+    case 'admin/help#fileshare':
+      $output = '<p>'. t('The Fileshare module is used to create 
+       pages that allow browsing, uploading, 
+       downloading and deleting files from the server.').'</p>';
+      $output .= t('<p>You can:</p>
+    <ul>
+    <li>Post a Fileshare at <a href="%node-add-fileshare">
+     create content &gt;&gt; fileshare</a>.</li>
+    <li>Configure Fileshares at <a href="%admin-node-configure-types"> 
+     administer &gt;&gt; content &gt;&gt; configure types &gt;&gt; 
+    fileshare configure</a>.</li>
+    </ul>
+    ', array('%node-add-fileshare' => url('node/add/fileshare'), 
+     '%admin-node-configure-types' => url('admin/node/configure/types')));
+      $output .= '<p>'. t('This module was created by Jamie Ruderman for Marmot Mountain LLC, 
+       and was based on directions posted at drupal.org <a href="%tutorial">
+       How to create your own simple node type (Drupal 4.7)</a>', 
+       array('%tutorial' => 'http://www.drupal.org/node/40684')) .'</p>';
+      return $output;
+    case 'admin/modules#description':
+      return t('Enables the creation of pages to display, browse, upload and delete files.');
+    case 'node/add#fileshare':
+      return t('Create a Fileshare page.');
+  }
+}
+
+/**
  * Implementation of hook_node_info().
  *
  * This is a required node hook. This function describes the nodes provided by
@@ -27,7 +63,7 @@
  * when inserting the node).
  */
 function fileshare_node_info() {
-  return array('fileshare' => array('name' => t('fileshare'), 'module' => 'fileshare', 'description' => 'Create a Fileshare page.'));
+  return array('fileshare' => array('name' => t('fileshare'), 'base' => 'fileshare'));
 }
 
 /**
@@ -280,16 +316,18 @@ function fileshare_update($node) {
     fileshare_insert($node);
   } else {
     // convert and sanitize node title as directory name
-    $rename = '/'._sanitize_filename($node->title).$node->nid;
-    if ($node->_filepath != $rename) {
-      if (!rename($node->_basepath.$node->_filepath, $node->_basepath.$rename)) {
-        die('RENAMING ERROR - from "'.$node->_basepath.$node->_filepath.'" to "'.$node->_basepath.$rename.'".');
+    $orig = node_load($node->nid);
+
+    if ($node->_filepath != $orig->_filepath) {
+      if (!rename($orig->_basepath.$orig->_filepath, $node->_basepath.$node->_filepath)) {
+        drupal_set_message('RENAMING ERROR - from "'.$orig->_basepath.$orig->_filepath.'" to "'.$node->_basepath.$node->_filepath.'".', 'error');
+        return false;
       }
     }
-  $node->_filepath = $rename;
   db_query("UPDATE {node_fileshare} SET _filepath = '%s',  _basepath = '%s', _whitelist = '%s', _method = %b, _modify = %b, _thumbs = %b, _private = %b WHERE vid = %d", 
     $node->_filepath, $node->_basepath, $node->_whitelist, $node->_method, $node->_modify, $node->_thumbs, $node->_private, $node->vid);
   }
+  return true;
 }
 
 /**
@@ -318,7 +356,7 @@ function fileshare_delete($node) {
   db_query('DELETE FROM {node_fileshare} WHERE nid = %d', $node->nid);
   if (_recursive_rmdir($node->_basepath.$node->_filepath)) {
     drupal_set_message('The directory: <strong>"'.$node->_basepath.$node->_filepath.'"</strong> has been deleted.');
-    watchdog('fileshare', check_plain($node->title." and the contents of ".$node->_basepath.$node->_filepath." were deleted"), WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
+    watchdog('fileshare', $node->title." and the contents of ".$node->_basepath.$node->_filepath." were deleted", WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
   } else {
     drupal_set_message('ERROR: "'.$node->_basepath.$node->_filepath.'" could not be deleted.','error');
   }
@@ -346,12 +384,8 @@ function fileshare_load($node) {
 function fileshare_view(&$node, $teaser = FALSE, $page = FALSE) {
   $node = node_prepare($node, $teaser);
   $order_info = theme('fileshare_order_info', $node);
-  if ($teaser) {
-    $node->content['teaser']['#value'] .= $order_info; 
-  } elseif ($page) {
-    $node->content['body']['#value'] .= $order_info;
-  }
-  return $node;
+  $node->body .= $order_info;
+  $node->teaser .= $order_info; 
 }
 
 /**
@@ -367,24 +401,23 @@ function theme_fileshare_order_info($nod
   $output .= '<noscript><div class="messages error"><strong>You have no Javascript!</strong>
     You must use a Javascript enabled browser.</div></noscript>';
   $output .= '<div id="fs">';
-  $output .= drupal_get_form('_fsform', $node);
+  $output .= _fileshare_fsform($node);
   $output .= '</div>';
   if (_tf($node->_modify) && (user_access('modify files') || (user_access('manage own fileshares') && ($user->uid == $node->uid)))) {
-    $output .= drupal_get_form('_uploadform',$node);
+    $output .= _fileshare_uploadform($node);
   } else {
-    $output .= '<div id="-uploadform"></div>'; // so we don't get .js errors on load
+    $output .= '<div id="_uploadform"></div>'; // so we don't get .js errors on load
   }
   $modulepath = drupal_get_path('module', 'fileshare');
   drupal_add_js($modulepath.'/fileshare.js');
-  // drupal_add_js('fileshare.js','module', 'header', FALSE, FALSE);
-  drupal_add_css($modulepath.'/fileshare.css');
+  theme_add_style($modulepath.'/fileshare.css');
   return $output;
 }
 
 /**
  * File upload form on view node
  */
-function _uploadform($node) {
+function _fileshare_uploadform($node) { 
   $form['#attributes'] = array(
     'enctype' => 'multipart/form-data', 
     'class' => 'node-form',
@@ -421,19 +454,39 @@ function _uploadform($node) {
   $form['file']['create'] = array('#type' => 'submit', '#value' => t('New folder'));
   $form['file']['root'] = array('#type' => 'hidden', '#value' => $node->_basepath.$node->_filepath);
   $form['file']['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
-  return $form;
+  
+  if(user_access('set file path')) {
+     $form['node_modify'] = array(
+      '#type'   => 'fieldset',
+      '#title'  => t('Node base path'),
+      '#collapsible' => true,
+      '#collapsed'   => true,
+     );  
+ 
+     $form['node_modify']['_filepath'] = array(
+       '#type'          => 'textfield',
+       '#title'         => t('Node file path'),
+       '#default_value' => $node->_filepath,
+       '#maxlength'     => 255,
+     );
+     
+     $form['node_modify']['change_path'] = array('#type' => 'submit', '#value' => t('Change node base path'));
+   }
+  
+  $output = drupal_get_form('_uploadform', $form);
+  return $output;
 }
 
 /**
  * File upload form validation
  * Checks to ensure file was selected for upload
- * Called by drupal_get_form in _uploadform()
+ * Called by drupal_get_form in _fileshare_uploadform()
  */
 function _uploadform_validate($form_id, $form_values) {
   switch ($_POST['op']) {
     case t('Upload'):
-      $upload = $_FILES['files']['name']['filename'];
-      $error = $_FILES['files']['error']['filename'];
+      $upload = $_FILES['edit']['name']['filename'];
+      $error = $_FILES['edit']['error']['filename'];
       if ($error > 0) {
         $errors = array(
           1 => "The uploaded file exceeds the ".ini_get('upload_max_filesize')." maximum file size.",
@@ -464,12 +517,18 @@ function _uploadform_validate($form_id, 
         form_set_error('dirname',t('You must first enter a folder name.'));
       }
       break;
+      
+      case t('Change node base path'):
+       if(empty($form_values['_filepath']) && user_access('set file path')) {
+         form_set_error('_filepath', t('Base path is required'));
+       }
+       break;
   }
 }
 
 /**
- * Saves the file submitted in _uploadform()
- * Called by drupal_get_form in _uploadform()
+ * Saves the file submitted in _fileshare_uploadform()
+ * Called by drupal_get_form in _fileshare_uploadform()
  */
 function _uploadform_submit($form_id, $form_values) { 
   switch ($_POST['op']) {
@@ -483,7 +542,7 @@ function _uploadform_submit($form_id, $f
         chmod($form_values['targetdir'].'/'.$file->filename, 0664);
         drupal_set_message('The file: <strong>"'.$file->filename.'"</strong> uploaded successfully!');
         $node = node_load($form_values['nid']);
-        watchdog('fileshare', check_plain($file->filename.' uploaded from '.$node->title), WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
+        watchdog('fileshare', $file->filename.' uploaded from '.$node->title, WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
       } else {
         drupal_set_message('Upload failed. Please check your filename.','error');
       }
@@ -494,6 +553,18 @@ function _uploadform_submit($form_id, $f
         drupal_set_message('There was a problem creating: <strong>"'.$targetdir.'"</strong>.','error');
       }
       break;
+      
+      case t('Change node base path'):
+       $node = node_load($form_values['nid']);
+       $node->_filepath = $form_values['_filepath'];
+       if (fileshare_update(&$node)) {
+         watchdog('fileshare', check_plain('node '.$node->nid.' files moved to '.$form_values['_filepath']), WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
+         drupal_set_message('Directory move completed successfully.');
+       } else {
+         drupal_set_message('Directory move failed.', 'error');
+         watchdog('fileshare', check_plain('node '.$node->nid.' failed to move to '.$form_values['_filepath']), WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
+       }
+       break;
   }
 } 
 
@@ -501,14 +572,15 @@ function _uploadform_submit($form_id, $f
  * Sets up the form for the JAH handler 
  * to handle file and folder deletes.
  */
-function _fsform($node) {
+function _fileshare_fsform($node) {
   $form['deletefile'] = array('#type' => 'hidden');
   $form['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
   // Javascript handler start settings
   $form['div'] = array('#value' => 
     '<div id="startFileShare" name="'.url('fileshare/handler/'.$node->nid).'"></div>'
     );
-  return $form;
+  $output = drupal_get_form('_fsform', $form);
+  return $output;
 }
 
 /**
@@ -524,7 +596,7 @@ function _fsform_validate($form_id, $for
     if (file_check_directory($deletefile)) {
       if (_recursive_rmdir($deletefile)) {
         drupal_set_message('The folder and it\'s contents: <strong>"'.basename($deletefile).'"</strong> has been deleted.');
-        watchdog('fileshare', check_plain($deletefile." and it's contents were deleted from ".$node->title), WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
+        watchdog('fileshare', $deletefile." and it's contents were deleted from ".$node->title, WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
       } else {
         drupal_set_message('The folder: <strong>"'.basename($deletefile).'"</strong> could not be deleted.','error'); 
         return FALSE;
@@ -532,7 +604,7 @@ function _fsform_validate($form_id, $for
     } else {
       if (file_delete($deletefile)) {
         drupal_set_message('The file: <strong>"'.basename($deletefile).'"</strong> has been deleted.');
-        watchdog('fileshare', check_plain($deletefile.' was deleted from '.$node->title), WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
+        watchdog('fileshare', $deletefile.' was deleted from '.$node->title, WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
       } else {
         drupal_set_message('The file: <strong>"'.$deletefile.'"</strong> could not be deleted.','error'); 
         return FALSE;
@@ -561,8 +633,8 @@ function _recursive_rmdir($filepath) {
       }
     }
   }
-  closedir($handle);
   rmdir($filepath); // delete directory
+  closedir($handle);
   return TRUE;
 }
 
@@ -607,13 +679,12 @@ function _tf($tf) {
 /**
  * RECURSIVE READ DIRECTORY
  * for AJAX file browsing
- * Called by _fsform()
+ * Called by _fileshare_fsform()
  */
 function _fileshare_handler($node) {
   global $user;
-  // ensure that users are not able to ascend the file tree with relitive paths
-  isset($_GET['dir']) ? $dir = trim(str_replace('../', '', $_GET['dir']),".") : $dir = '';
-  isset($_GET['pre']) ? $pre = check_plain($_GET['pre']) : $pre = '';
+  isset($_GET['dir']) ? $dir = $_GET['dir'] : $dir = '';
+  isset($_GET['pre']) ? $pre = $_GET['pre'] : $pre = '';
   $modify     = _tf($node->_modify) && (user_access('modify files') || ( user_access('manage own fileshares') && ($user->uid == $node->uid)));
   $download   = (user_access('download files') || ( user_access('manage own fileshares') && ($user->uid == $node->uid)));
   $tag        = 'a';
@@ -627,24 +698,24 @@ function _fileshare_handler($node) {
   for ($x=0; $x <= (count($file) -1); $x++) { // loop through rows of array
     if (substr($file[$x], 0, 1) != "." && $file[$x] != '_previews') {
       if (is_dir($node->_basepath.$node->_filepath.$dir.'/'.$file[$x])) {
-        $output .= '<div class="fs_row">'."\n";
-        $output .= '<div class="fs_data">'."\n";
+        $outdir .= '<div class="fs_row">'."\n";
+        $outdir .= '<div class="fs_data">'."\n";
         if ($modify) { 
-          $output .= '<a href="javascript:submitDelete(\''
+          $outdir .= '<a href="javascript:submitDelete(\''
             .$dir.'/'.$file[$x].'\');" alt="delete file" class="fs_delete">'
             ."&nbsp;</a>\n";
         }
-        $output .= "</div>\n";
-        $output .= '<a href="javascript:;" onclick="fileshare_folder(this,\''.$pre.$tag.'\');" ';
-        $output .= 'alt="'.$node->_basepath.$node->_filepath.$dir.'/'.$file[$x].'" ';
-        $output .= 'name="'.url('fileshare/handler/'.$node->nid
+        $outdir .= "</div>\n";
+        $outdir .= '<a href="javascript:;" onclick="fileshare_folder(this,\''.$pre.$tag.'\');" ';
+        $outdir .= 'alt="'.$node->_basepath.$node->_filepath.$dir.'/'.$file[$x].'" ';
+        $outdir .= 'name="'.url('fileshare/handler/'.$node->nid
           ,'dir='.$dir.'/'.$file[$x]
           .'&pre='.$pre.$tag).'" ';
-        $output .= 'class="fs_closed">'."\n";
-        $output .= $file[$x]; 
-        $output .= "</a>\n";
-        $output .= "</div>\n";
-        $output .= '<div class="fs_nest" id="'.$pre.$tag.'"></div>'."\n"; 
+        $outdir .= 'class="fs_closed">'."\n";
+        $outdir .= $file[$x]; 
+        $outdir .= "</a>\n";
+        $outdir .= "</div>\n";
+        $outdir .= '<div class="fs_nest" id="'.$pre.$tag.'"></div>'."\n"; 
         ++$tag;
       } else {
         $output .= '<div class="fs_row">'."\n";
@@ -683,10 +754,10 @@ function _fileshare_handler($node) {
     }
   }
   closedir($handle);
-  if ($output == '') { // Safari needs some output returned to the jahHandler
+  if ($output == '' && $outdir == '') { // Safari needs some output returned to the jahHandler
     echo "&nbsp;";
   } else {
-    echo $output;
+    echo $output.$outdir;
   }
 }
 
@@ -723,7 +794,7 @@ function _fileshare_download($node) {
       $name = mime_header_encode($filename);
       $type = mime_header_encode($filetype);
 
-      watchdog('fileshare', check_plain($filename.' downloaded from '.$node->title), WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
+      watchdog('fileshare', $filename.' downloaded from '.$node->title, WATCHDOG_NOTICE, l(t('view'), 'node/'.$node->nid));
       $headers = array (
         'Content-Type: '. $type .'; name='. $name,
         'Content-Length: '. $filesize,
@@ -781,4 +852,4 @@ if (!function_exists ("mime_content_type
     if ($mime) return $mime;
     return "application/octet-stream";
   }
-}
+}
\ No newline at end of file
