You are here

imce_dir_man.module in IMCE Tools 6

Same filename and directory in other branches
  1. 7 imce_dir_man/imce_dir_man.module

imce_dir_man.module provides functions for managing configuration determining what the imce_dir_man_path() function. This function can be used as php code in the directory setting of an IMCE profile to allow for per user configuration of directory restrictions

File

imce_dir_man/imce_dir_man.module
View source
<?php

/**
 * @file
 * imce_dir_man.module provides functions for managing configuration
 * determining what the imce_dir_man_path() function. This function can
 * be used as php code in the directory setting of an IMCE profile to 
 * allow for per user configuration of directory restrictions
 */

/**
 * hook menu
 */
function imce_dir_man_menu() {
  $menu['admin/settings/imce_dir_man'] = array(
    'title' => 'IMCE User Directory Access Manager',
    'description' => 'Administer directory restrictions for IMCE',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'imce_dir_man_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $menu;
}

/**
 * generates admin form for managing user directory restrictions
 */
function imce_dir_man_form() {
  $res = db_query('SELECT idm.*, u.name FROM {imce_dir_man} idm JOIN {users} u USING(uid) ORDER BY name');
  $weight = 1;
  while ($row = db_fetch_object($res)) {
    $form[$row->uid] = array(
      '#title' => t('User @name', array(
        '@name' => $row->name,
      )),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
      '#weight' => $weight++,
    );
    $form[$row->uid]['del'] = array(
      '#title' => t('Remove restrictions'),
      '#description' => t('Deletes restrictions set for this user'),
      '#type' => 'checkbox',
      '#weight' => 10,
    );
    $form[$row->uid]['dir'] = array(
      '#title' => t('Directory'),
      '#description' => t('Directory to restrict user to relative to site file upload root, comma separate to specify multiple directories, a/b/c restricts a user to !root/a/b/c', array(
        '!root' => file_directory_path(),
      )),
      '#type' => 'textfield',
      '#size' => '100',
      '#maxsize' => '255',
      '#default_value' => $row->dir,
      '#weight' => 20,
    );
  }
  $form['new_user'] = array(
    '#title' => t('Add restriction for new user'),
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#weight' => $weight++,
  );
  $form['new_user']['name'] = array(
    '#title' => t('User'),
    '#type' => 'textfield',
    '#size' => '100',
    '#maxsize' => '255',
    '#weight' => 10,
  );
  $form['new_user']['dir'] = array(
    '#title' => t('Directory'),
    '#type' => 'textfield',
    '#description' => t('Directory to restrict user to relative to site file upload root, comma separate to specify multiple directories, a/b/c restricts a user to sites/default/files/a/b/c'),
    '#size' => '100',
    '#maxsize' => '255',
    '#weight' => 20,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save',
    '#weight' => $weight,
  );
  $form['#submit'] = array(
    'imce_dir_man_form_submit',
  );
  $form['#validate'] = array(
    'imce_dir_man_form_validate',
  );
  return $form;
}

/**
 * Validate restriction form (imce_dir_man_form) settings
 */
function imce_dir_man_form_validate($form, &$form_state) {
  foreach ($form_state['values'] as $uid => $data) {
    $data['dir'] = trim($data['dir'], ',');
    if ($data['dir'] == '' && $data['del'] != 1 && $data['name'] != '') {
      form_set_error($uid . '][dir', t('Invalid directory restriction (blank or contains only commas)'));
    }
    else {
      if ($uid == 'new_user' && $data['name'] != '') {
        $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $data['name']));
        if (!$uid) {
          form_set_error($uid . '][name', t('Invalid user'));
        }
        $uid = db_result(db_query("SELECT uid FROM {imce_dir_man} WHERE uid = %d", $uid));
        if ($uid) {
          form_set_error($uid . '][name', t('User already has restriction configured, please update their entry to make modifications'));
        }
      }
    }
  }
}

/**
 * Save configured restriction settings adding new settings, 
 * updating or deleting existing settings
 */
function imce_dir_man_form_submit($form, &$form_state) {
  $rec = new stdclass();
  foreach ($form_state['values'] as $uid => $data) {
    $rec->dir = trim($data['dir'], ',');
    $rec->uid = $uid;
    if ($uid == 'new_user' && $data['name'] != '' && $data['dir'] != '') {
      $rec->uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $data['name']));
      drupal_write_record('imce_dir_man', $rec);
    }
    else {
      if ($data['del']) {
        db_query('DELETE FROM {imce_dir_man} WHERE uid = %d', $uid);
      }
      else {
        drupal_write_record('imce_dir_man', $rec, array(
          'uid',
        ));
      }
    }
  }
}

/**
 * returns an array representing a user's currently accessible file directories
 * used by the imce uploader
 * If a user has not been restricted in the configuration, . (all directories)
 * is returned
 */
function imce_dir_man_path() {
  global $user;
  $dir = db_result(db_query('SELECT dir FROM {imce_dir_man} WHERE uid = %d', $user->uid));
  if (!$dir) {
    $dir = '.';
  }
  return preg_split('/,/', $dir);
}

Functions

Namesort descending Description
imce_dir_man_form generates admin form for managing user directory restrictions
imce_dir_man_form_submit Save configured restriction settings adding new settings, updating or deleting existing settings
imce_dir_man_form_validate Validate restriction form (imce_dir_man_form) settings
imce_dir_man_menu hook menu
imce_dir_man_path returns an array representing a user's currently accessible file directories used by the imce uploader If a user has not been restricted in the configuration, . (all directories) is returned