You are here

node.inc in Filebrowser 6.2

File

includes/node.inc
View source
<?php

/* This file is part of "filebrowser".
 *    Copyright 2009-2011, arNuméral
 *    Author : Yoran Brault
 *    eMail  : yoran.brault@bad_arnumeral.fr (remove bad_ before sending an email)
 *    Site   : http://www.arnumeral.fr
 *
 * "filebrowser" is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * "filebrowser" is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with "filebrowser"; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

/**
 * Check if user can download ZIP archives.
 */
function _filebrowser_can_download_archive(&$node) {
  return node_access('view', $node) && $node->folder_rights->download_archive && user_access(FILEBROWSER_DOWNLOAD_ARCHIVE);
}

/**
 * Check if user can download files.
 */
function _filebrowser_can_download_file(&$node) {
  return node_access('view', $node) && user_access(FILEBROWSER_DOWNLOAD);
}

/**
 * Check if user can view a specific file.
 */
function _filebrowser_can_view_file(&$node, $file) {
  return trim($node->folder_rights->filtered_files) == '' || _filebrowser_match_path($file, $node->folder_rights->filtered_files);
}

/**
 * Check if user should not view a specific file.
 */
function _filebrowser_cant_view_file(&$node, $file) {
  return trim($node->folder_rights->forbidden_files) != '' && _filebrowser_match_path($file, $node->folder_rights->forbidden_files);
}

/**
 * Check if user can explore sub-folders.
 */
function _filebrowser_can_explore_subfolders(&$node) {
  return $node->folder_rights->explore_subdirs;
}

/**
 * Prepare node record to be used. This is mainly about default stuff.
 * @param $node node to prepage
 * @param $load is this for loading (TRUE) or inserting/updating (FALSE)
 */
function _filebrowser_prepare_record(&$node, $load = TRUE) {

  // Fix trailing slashes
  $node->folder_path = rtrim($node->folder_path, '/');

  // Process uploads
  $node->folder_uploads = (object) $node->folder_uploads;

  // Process handlers
  $node->file_handlers = (object) $node->file_handlers;
  foreach (module_implements("filebrowser_handler_info") as $module) {
    if (isset($node->{$module})) {
      $node->file_handlers->{$module} = $node->{$module};
      unset($node->{$module});
    }
    $node->file_handlers = (object) $node->file_handlers;
    $node->file_handlers->{$module} = (object) $node->file_handlers->{$module};
  }

  // Fix rights
  $node->folder_rights = (object) $node->folder_rights;

  // Fix presentation
  $node->folder_presentation = (object) $node->folder_presentation;
  $columns = _filebrowser_externals('metadata_info');
  $node->folder_presentation->visible_columns = _filebrowser_filter_checkboxes_result($node->folder_presentation->visible_columns);
  foreach ($node->folder_presentation->visible_columns as $name => $value) {
    if (!isset($columns[$name])) {
      unset($node->folder_presentation->visible_columns[$name]);
    }
  }
  if (count($node->folder_presentation->visible_columns) == 0) {
    $node->folder_presentation->visible_columns = array(
      FILEBROWSER_DATA_NAME_ICON => 1,
      FILEBROWSER_DATA_NAME_DISPLAY_NAME => 1,
    );
  }
  if (!isset($node->folder_presentation->default_sort)) {
    foreach ($node->folder_presentation->visible_columns as $name => $foo) {
      if (isset($columns[$name]['sortable']) && $columns[$name]['sortable']) {
        $node->folder_presentation->default_sort = $name;
        break;
      }
    }
  }

  // Create serialized properties
  $data = new stdClass();
  $data->folder_rights = $node->folder_rights;
  $data->folder_presentation = $node->folder_presentation;
  $data->folder_uploads = $node->folder_uploads;
  $data->file_handlers = isset($node->file_handlers) ? $node->file_handlers : (object) array();
  $node->properties = serialize($data);
}
function _filebrowser_form_presentation_part($settings = NULL, $weight = 0) {
  $presentations = _filebrowser_options(_filebrowser_externals('presentations'));
  if (is_null($settings)) {
    $globale_settings = variable_get('filebrowser', NULL);
    if (!is_null($globale_settings) && isset($globale_settings['folder_presentation'])) {
      $settings = (object) $globale_settings['folder_presentation'];
    }
    if (is_null($settings)) {
      $settings = (object) array(
        'default_view' => current(array_keys($presentations)),
        'encoding' => NULL,
        'hide_extension' => FALSE,
        'visible_columns' => array(
          FILEBROWSER_DATA_NAME_ICON => 1,
          FILEBROWSER_DATA_NAME_DISPLAY_NAME => 1,
        ),
        'default_sort_order' => 'asc',
        'default_sort' => 0,
      );
    }
  }

  /** Folder presentation
   * *************************************************************** */
  $form['folder_presentation'] = array(
    '#type' => 'fieldset',
    '#title' => t('Folder presentation'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => $weight,
  );
  $form['folder_presentation']['default_view'] = array(
    '#type' => 'select',
    '#title' => t("Default view"),
    '#default_value' => $settings->default_view,
    '#options' => $presentations,
    '#prefix' => "<div class=\"filebrowser-container-inline\">",
  );
  $form['folder_presentation']['hide_extension'] = array(
    '#type' => 'checkbox',
    '#title' => t('Hide file extensions'),
    '#default_value' => $settings->hide_extension,
    '#suffix' => "</div>",
  );
  $form['folder_presentation']['encoding'] = array(
    '#type' => 'textfield',
    '#title' => t('FileSystem encoding'),
    '#description' => t('Set here your file system encoding (UTF-8, ISO-8859-15, etc.).'),
    '#default_value' => $settings->encoding,
    '#size' => 10,
  );
  $columns = _filebrowser_externals('metadata_info');
  $form['folder_presentation']['visible_columns'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#title' => t("Visible columns"),
    '#default_value' => _filebrowser_properties_to_checkboxes($settings->visible_columns),
    '#options' => _filebrowser_options($columns),
    '#prefix' => "<div class=\"filebrowser-container-inline\">",
  );
  $sortable = array();
  foreach ($columns as $name => $data) {
    if (isset($data['sortable']) && $data['sortable']) {
      $sortable[$name] = $data['title'];
    }
  }
  $form['folder_presentation']['default_sort'] = array(
    '#type' => 'select',
    '#title' => t("Default sort"),
    '#default_value' => $settings->default_sort,
    '#options' => $sortable,
  );
  $form['folder_presentation']['default_sort_order'] = array(
    '#type' => 'select',
    '#title' => t("Default sort order"),
    '#default_value' => $settings->default_sort_order,
    '#options' => array(
      'asc' => t('Ascendant'),
      'desc' => t('Descendant'),
    ),
    '#suffix' => "</div>",
  );
  return $form;
}
function _filebrowser_form_rights_part($settings = NULL, $weight = 0) {
  if (is_null($settings)) {
    $globale_settings = variable_get('filebrowser', NULL);
    if (!is_null($globale_settings) && isset($globale_settings['folder_rights'])) {
      $settings = (object) $globale_settings['folder_rights'];
    }
    if (is_null($settings)) {
      $settings = (object) array(
        'explore_subdirs' => FALSE,
        'create_folders' => FALSE,
        'download_archive' => FALSE,
        'forbidden_files' => "descript.ion\nfile.bbs\n.git\nCSV\n.svn\n",
        'filtered_files' => "",
        'download_manager' => variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC ? 'public' : 'private',
      );
    }
  }

  /** Folder rights
   * *************************************************************** */
  $form['folder_rights'] = array(
    '#type' => 'fieldset',
    '#title' => t('Folder rights'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => $weight,
  );
  $form['folder_rights']['explore_subdirs'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow subdirectory listings.'),
    '#default_value' => $settings->explore_subdirs,
  );
  $form['folder_rights']['download_archive'] = array(
    '#type' => 'checkbox',
    '#title' => t("Allow folder's files to be downloaded as an archive"),
    '#description' => t("Check this if you allow users to download all folder files as an archive."),
    '#default_value' => $settings->download_archive,
  );
  $form['folder_rights']['create_folders'] = array(
    '#type' => 'checkbox',
    '#title' => t("Allow folder to be created"),
    '#description' => t("Check this if you allow users to create new folders."),
    '#default_value' => $settings->create_folders,
  );
  $managers = _filebrowser_options(_filebrowser_externals('download_manager_info'));
  $form['folder_rights']['download_manager'] = array(
    '#type' => 'select',
    '#title' => t("Download manager"),
    '#description' => t("A download manager will handle the way of download folder files."),
    '#default_value' => $settings->download_manager,
    '#options' => $managers,
    '#weight' => -7,
  );
  $form['folder_rights']['forbidden_files'] = array(
    '#type' => 'textarea',
    '#title' => t('Blacklist'),
    '#description' => t('List of patterns of forbidden files, you can use wildcards (ex. .*).'),
    '#default_value' => $settings->forbidden_files,
  );
  $form['folder_rights']['filtered_files'] = array(
    '#type' => 'textarea',
    '#title' => t('Whitelist'),
    '#description' => t('List of patterns to filter, one per line, you can use wildcards (ex. *.pdf).'),
    '#default_value' => $settings->filtered_files,
  );
  return $form;
}
function _filebrowser_form_uploads_part($settings = NULL, $weight = 0) {
  if (is_null($settings)) {
    $globale_settings = variable_get('filebrowser', NULL);
    if (!is_null($globale_settings) && isset($globale_settings['folder_uploads'])) {
      $settings = (object) $globale_settings['folder_uploads'];
    }
    if (is_null($settings)) {
      $settings = (object) array(
        'enabled' => FALSE,
        'allow_overwrite' => FALSE,
        'accepted_uploaded_files' => '',
      );
    }
  }

  /** Folder uploads
   * *************************************************************** */
  $form['folder_uploads'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => t('Folder Upload'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => $weight,
  );
  $form['folder_uploads']['enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow uploads'),
    '#description' => t('Allow users to upload files.'),
    '#default_value' => $settings->enabled,
  );
  $form['folder_uploads']['allow_overwrite'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow overwrites'),
    '#description' => t('Allow files to be overwritten.'),
    '#default_value' => $settings->allow_overwrite,
  );
  $form['folder_uploads']['accepted_uploaded_files'] = array(
    '#type' => 'textarea',
    '#title' => t('Accepted files for uploading'),
    '#description' => t('List of file patterns accepted for upload. Empty means anything.'),
    '#default_value' => $settings->accepted_uploaded_files,
  );
  return $form;
}
function _filebrowser_form_file_handlers_part($settings = NULL, $weight = 0) {
  $form = array();
  $handlers = module_implements("filebrowser_handler_info");
  if (count($handlers)) {
    if (is_null($settings)) {
      $globale_settings = variable_get('filebrowser', NULL);
      if (!is_null($globale_settings) && isset($globale_settings['file_handlers'])) {
        $settings = (object) $globale_settings['file_handlers'];
      }
      foreach ($handlers as $module) {
        if (!isset($settings->{$module})) {
          $defaults = array(
            'enabled_thumbnailer' => FALSE,
            'enabled_metadata' => FALSE,
          );
          $defaults += module_invoke($module, "filebrowser_handler_defaults");
          $settings->{$module} = (object) $defaults;
        }
        else {
          $settings->{$module} = (object) $settings->{$module};
        }
      }
    }
    $form['file_handlers'] = array(
      '#type' => 'fieldset',
      '#tree' => TRUE,
      '#title' => 'File handlers',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => $weight,
    );
    foreach ($handlers as $module) {
      $info = module_invoke($module, "filebrowser_handler_info");
      $form['file_handlers']["{$module}"] = array(
        '#type' => 'fieldset',
        '#tree' => TRUE,
        '#title' => $info['description'],
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => -6,
      );
      $form['file_handlers']["{$module}"]['enabled_thumbnailer'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enabled as thumbnailer'),
        '#description' => t('Enable this file handler as thumbnailer.'),
        '#default_value' => $settings->{$module}->enabled_thumbnailer,
      );
      $form['file_handlers']["{$module}"]['enabled_metadata'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enabled as metadata provider'),
        '#description' => t('Enable this file handler as metadata provider.'),
        '#default_value' => $settings->{$module}->enabled_metadata,
      );
      $form['file_handlers']["{$module}"] += module_invoke($module, "filebrowser_handler_settings", $settings->{$module});
    }
  }
  return $form;
}

Functions

Namesort descending Description
_filebrowser_cant_view_file Check if user should not view a specific file.
_filebrowser_can_download_archive Check if user can download ZIP archives.
_filebrowser_can_download_file Check if user can download files.
_filebrowser_can_explore_subfolders Check if user can explore sub-folders.
_filebrowser_can_view_file Check if user can view a specific file.
_filebrowser_form_file_handlers_part
_filebrowser_form_presentation_part
_filebrowser_form_rights_part
_filebrowser_form_uploads_part
_filebrowser_prepare_record Prepare node record to be used. This is mainly about default stuff.