type) && $node->og_files_enabled && in_array($node->nid, array_keys($user->og_groups))) { $items[] = array( 'path' => 'node/'.arg(1).'/files', 'title' => t('Files'), 'callback' => 'og_files', 'callback arguments' => array(arg(1)), 'type' => MENU_LOCAL_TASK, ); } } } return $items; } function og_files($gid) { $group = node_load($gid); $params = func_get_args(); array_shift($params); $path = og_files_base_path($gid); if(is_array($params)) $path .= implode('/',$params); if(db_result(db_query("SELECT fid FROM {files} WHERE nid = %d AND filepath = '%s' LIMIT 1", $gid, $path))) { //it's a file, download it. if (file_exists(file_create_path($path))) { $headers = module_invoke_all('file_download', $path); if (in_array(-1, $headers)) { return drupal_access_denied(); } if (count($headers)) { file_transfer($path, $headers); } } return drupal_not_found(); } else { if(is_array($params)) $params .= '/'; //listing files return drupal_get_form('og_file_list_form',$gid,$path); } } function og_files_file_download($file) { global $user; // 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; } } function og_file_list_form($gid, $path){ $form = array(); $form['#attributes']['enctype'] = 'multipart/form-data'; $form['gid'] = array( '#type' => 'value', '#value' => $gid, ); $form['path'] = array( '#type' => 'value', '#value' => $path, ); $form['upload'] = array( '#type' => 'fieldset', '#title' => t('Upload a new file'), '#collapsible' => true, '#collapsed' => true ); $form['upload']['file'] = array( '#type' => 'file', '#title' => t('File'), ); $form['upload']['upload'] = array( '#type' => 'submit', '#value' => t('Upload'), ); $form['mkdir'] = array( '#type' => 'fieldset', '#title' => t('Create a new directory'), '#collapsible' => true, '#collapsed' => true ); $form['mkdir']['dir'] = array( '#type' => 'textfield', '#title' => t('Directory'), '#description' => t('Type the directory name, multiple levels allowed. For example "foo/bar" creates the directories foo and foo/bar.'), ); $form['mkdir']['createdir'] = array( '#type' => 'submit', '#value' => t('Create directory'), ); $file_list = array(); $file_count = 0; $dir_count = 0; $files = db_query("SELECT * FROM {files} WHERE nid = %d AND filepath LIKE '%s%%' ORDER BY filename ASC", $gid, $path); $virtualpath = og_files_relative_path($gid,$path); if($virtualpath[strlen($path)-1] == '/') $virtualpath[strlen($path)-1] = 0; $aux = explode('/',$virtualpath); if($aux[0] == '') array_shift($aux); $virtualpath_count = count($aux); if($virtualpath_count && $aux[0] != '') { 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 // 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)); $file_count++; } } $form['filelist'] = array( '#type' => 'fieldset', '#title' => '/'.og_files_relative_path($gid,$path), '#collapsible' => true, '#collapsed' => false ); //if($dir_count) { $dir_html = theme_item_list($dir_list); $form['filelist']['folders'] = array( '#prefix' => $dir_html, '#type' => 'hidden' ); //} if($file_count) { $form['filelist']['files'] = array( '#type' => 'checkboxes', // '#title' => $path, //t('File list'), '#options' => $file_list, ); $form['filelist']['delete'] = array( '#type' => 'submit', '#value' => t('Delete selected files'), ); } return $form; } function og_file_list_form_submit($form_id, $form_values) { $gid = $form_values['gid']; switch($form_values['op']) { case t('Upload'): //print('Upload a new file'); die; $path = $form_values['path']; //$return = file_check_directory($path,FILE_CREATE_DIRECTORY); if (!is_dir($path) && mkdir($path, 0777, true)) { @chmod($path, 0775); drupal_set_message(t('The directory %directory has been created.', array('%directory' => $directory))); } $file = file_save_upload('file',$path); $fid = db_next_id('{files}_fid'); 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] != '/') { // 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); } } function og_files_base_path($gid) { return 'files/og_files/'.$gid.'/'; } function og_files_virtual_base_path($gid) { return 'node/'.$gid.'/files/'; } function og_files_relative_path($gid,$filepath) { return substr($filepath, strlen(og_files_base_path($gid))); } function og_files_format_path($gid, $filepath) { $patharray = explode('/',$filepath); array_shift($patharray); $patharray = array('node', $gid) + $patharray; $patharray[2] = 'files'; 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_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' => , '#value' => $enabled, '#weight' => 30 ); } 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); case 'insert': if(isset($node->og_files_enabled)) { db_query("INSERT INTO {og_files} VALUES (%d,'%s')",$node->nid, ''); } break; case 'load': if(db_result(db_query('SELECT count(gid) FROM {og_files} WHERE gid = %d',$node->nid))) { $node->og_files_enabled = true; } break; case 'delete': db_query('DELETE FROM {og_files} WHERE gid = %d',$node->nid); break; } }