--- /tmp/Ivica/og_files.module	2008-01-06 03:56:18.000000000 +0200
+++ og_files.module	2008-01-21 17:02:25.000000000 +0200
@@ -37,7 +37,7 @@ function og_files($gid) {
       if (in_array(-1, $headers)) {
         return drupal_access_denied();
       }
-      
+
       if (count($headers)) {
         file_transfer($path, $headers);
       }
@@ -54,7 +54,15 @@ function og_files($gid) {
 
 function og_files_file_download($file) {
   global $user;
-  if(($user->uid != 1) && !db_result(db_query("SELECT o.uid FROM {og_uid} o, {files} f WHERE f.filepath = '%d' AND f.nid = o.nid AND o.uid = '%d' LIMIT 1",$file,$user->uid))) {
+
+  // pivica Patch - Should only check files from 'og_files' dir. This is needed when drupal file system is
+  // set to private - then accessing files from /files dir is not possible because this function
+  // returns -1.
+  if (strpos($file, '/og_files/') === FALSE)
+    return;
+
+  // pivica Patch - sql is not created correctly - f.filepath = '%d' should be f.filepath = '%s'
+  if(($user->uid != 1) && !db_result(db_query("SELECT o.uid FROM {og_uid} o, {files} f WHERE f.filepath = '%s' AND f.nid = o.nid AND o.uid = '%d' LIMIT 1",$file,$user->uid))) {
     return -1;
   }
 }
@@ -62,7 +70,7 @@ function og_files_file_download($file) {
 function og_file_list_form($gid, $path){
   $form = array();
   $form['#attributes']['enctype'] = 'multipart/form-data';
-  
+
   $form['gid'] = array(
     '#type' => 'value',
     '#value' => $gid,
@@ -71,7 +79,7 @@ function og_file_list_form($gid, $path){
     '#type' => 'value',
     '#value' => $path,
   );
-  
+
   $form['upload'] = array(
     '#type' => 'fieldset',
     '#title' => t('Upload a new file'),
@@ -86,7 +94,7 @@ function og_file_list_form($gid, $path){
     '#type' => 'submit',
     '#value' => t('Upload'),
   );
-  
+
   $form['mkdir'] = array(
     '#type' => 'fieldset',
     '#title' => t('Create a new directory'),
@@ -102,7 +110,7 @@ function og_file_list_form($gid, $path){
     '#type' => 'submit',
     '#value' => t('Create directory'),
   );
-  
+
   $file_list = array();
   $file_count = 0;
   $dir_count = 0;
@@ -111,8 +119,8 @@ function og_file_list_form($gid, $path){
   if($virtualpath[strlen($path)-1] == '/')
     $virtualpath[strlen($path)-1] = 0;
   $aux = explode('/',$virtualpath);
-  
-  if($aux[0] == '') 
+
+  if($aux[0] == '')
     array_shift($aux);
 
   $virtualpath_count = count($aux);
@@ -121,19 +129,16 @@ function og_file_list_form($gid, $path){
     array_pop($aux);
     $dir_list['..'] = l('..', og_files_virtual_base_path($gid).implode('/',$aux));
   }
-  
 
-    
-    
   while($file = db_fetch_object($files)) {
     $array = explode('/',og_files_relative_path($gid,$file->filepath));
     array_pop($array);
     if((count($array) - $virtualpath_count) > 0) { //in a subdir
-
-      
-      $dir_list[$array[0]] = l($array[0], og_files_virtual_base_path($gid).implode('/',$array));
-      $dir_count++;
-
+      // pivica Patch - we just want to show first level of subfolders in current folder
+      if (count($array) - $virtualpath_count < 2) {
+        $dir_list[$array[count($array)-1]] = l($array[count($array)-1], og_files_virtual_base_path($gid).implode('/',$array));
+        $dir_count++;
+      }
     }
     else {
       $file_list[$file->fid] = l($file->filename,og_files_virtual_base_path($gid).og_files_relative_path($gid,$file->filepath));
@@ -147,27 +152,27 @@ function og_file_list_form($gid, $path){
     '#collapsible' => true,
     '#collapsed' => false
   );
-  
+
   //if($dir_count) {
-    $dir_html = theme_item_list($dir_list);
-    $form['filelist']['folders'] = array(
+  $dir_html = theme_item_list($dir_list);
+  $form['filelist']['folders'] = array(
       '#prefix' => $dir_html,
       '#type' => 'hidden'
-    );
-  //}
-  
-  if($file_count) {
-    $form['filelist']['files'] = array(
+      );
+      //}
+
+      if($file_count) {
+        $form['filelist']['files'] = array(
       '#type' => 'checkboxes',
-     // '#title' => $path, //t('File list'),
+        // '#title' => $path, //t('File list'),
       '#options' => $file_list,
-    );
-    $form['filelist']['delete'] = array(
+        );
+        $form['filelist']['delete'] = array(
       '#type' => 'submit',
       '#value' => t('Delete selected files'),
-    );
-  }
-  return $form;
+        );
+      }
+      return $form;
 }
 
 function og_file_list_form_submit($form_id, $form_values) {
@@ -188,20 +193,28 @@ function og_file_list_form_submit($form_
       db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s','%s','%s',%d)", $fid, $gid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
       drupal_set_message(t('The file %file has been sucessful uploaded.', array('%file' => $file->filename)));
       break;
-    
+
     case t('Create directory'):
       $destination = 'node/'.$gid.'/files';
-      if($form_values['dir'][0] != '/')
-        $destination .= '/'.og_files_relative_path($form_values['gid'],$form_values['path']).'/';
+      if($form_values['dir'][0] != '/') {
+        // pivica - patching '//' situation. The last '/' should be added only if og_files_relative_path return nonempty string
+        // in other situation adding of '/' should be skiped.
+        $destination .= '/';
+        $relpath = og_files_relative_path($form_values['gid'],$form_values['path']);
+        if ($relpath)
+          $destination .= $relpath . '/';
+      }
       $destination .= $form_values['dir'];
+      drupal_set_message(t('Directory will be created when you upload first file in it.'));
       drupal_goto($destination);
       break;
-    
+
     case t('Delete selected files'):
       $string = implode(', ',$form_values['files']);
       $files = db_query('SELECT * FROM {files} WHERE nid = %d AND fid IN (%s)',$gid, $string);
       while($file = db_fetch_object($files)) {
         file_delete($file->filepath);
+        drupal_set_message(t('The file %file has been sucessful deleted.', array('%file' => $file->filename)));
       }
       db_query('DELETE FROM {files} WHERE nid = %d AND fid IN (%s)',$gid, $string);
   }
@@ -228,34 +241,34 @@ function og_files_format_path($gid, $fil
   return implode('/',$patharray);
 }
 
-function og_files_install() {
-  //seems to be not working
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      db_query("CREATE TABLE {og_files} (
-                  gid INT NOT NULL,
-                  data TEXT NOT NULL
-                );"
-      );
-      break;
-  }
-}
+/*function og_files_install() {
+ //seems to be not working
+ switch ($GLOBALS['db_type']) {
+ case 'mysql':
+ case 'mysqli':
+ db_query("CREATE TABLE {og_files} (
+ gid INT NOT NULL,
+ data TEXT NOT NULL
+ );"
+ );
+ break;
+ }
+ }*/
 
 function og_files_form_alter($form_id,&$form){
   if (!isset($form['#node']) || ($form_id != $form['#node']->type .'_node_form') || (!og_is_group_type($form['#node']->type)))
     return;
-  
+
   $node = $form['#node'];
   $enabled = isset($node->og_files_enabled);
-  
+
   if(!isset($node->nid))
     $enabled = true;
-    
+
   $form['og_files_enabled'] = array(
     '#type' => 'checkbox',
     '#title' => t('Enable OG Files to this group?'),
-    //'#default_value' => ,
+  //'#default_value' => ,
     '#value' => $enabled,
     '#weight' => 30
   );
@@ -265,7 +278,7 @@ function og_files_form_alter($form_id,&$
 function og_files_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
   if(!og_is_group_type($node->type))
     return;
-  
+
   switch($op) {
     case 'update':
       db_query('DELETE FROM {og_files} WHERE gid = %d',$node->nid);
