You are here

imagepicker.module in Image Picker 5.2

Same filename and directory in other branches
  1. 5 imagepicker.module
  2. 6.2 imagepicker.module
  3. 7 imagepicker.module

Enables permitted roles to upload images for insertion into configured nodes.

File

imagepicker.module
View source
<?php

// $Name$

/**
 * @file
 * Enables permitted roles to upload images for insertion into configured nodes.
 */
define('IMAGEPICKER_FILES_DIR', 'imagepicker');
define('IMAGEPICKER_THUMBS_DIR', 'thumbs');
define('IMAGEPICKER_BROWSER_DIR', 'browser');

// If someone is using PHP version < 5.xx, this will solve the problem, where
// function htmlspecialchars_decode() doesn't exist
if (!function_exists('htmlspecialchars_decode')) {
  function htmlspecialchars_decode($text) {
    return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
  }
}

/**
 * Implementation of hook_help().
 */
function imagepicker_help($section) {
  switch ($section) {
    case 'admin/help#imagepicker':
      $content = '<p>' . t('Adds an advanced image upload form under the node body part.') . '</p>';
      return $content;
  }
}

/**
 * Implementation of hook_perm().
 */
function imagepicker_perm() {
  return array(
    'administer imagepicker',
    'use imagepicker',
    'access own imagepicker',
    'use public imagepicker',
    'create public groups',
  );
}

/**
 * Implementation of hook_menu().
 */
function imagepicker_menu($may_cache) {
  global $user;
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'imagepicker',
      'title' => t('Image picker'),
      'callback' => 'imagepicker_upload',
      'access' => user_access('use imagepicker'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'imagepicker/upload',
      'title' => t('Upload'),
      'access' => user_access('use imagepicker'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 1,
    );
    $items[] = array(
      'path' => 'imagepicker/browse',
      'title' => t('Browse'),
      'callback' => 'imagepicker_browse',
      'access' => user_access('use imagepicker'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 2,
    );
    if (variable_get('imagepicker_public_enabled', 1)) {
      $items[] = array(
        'path' => 'imagepicker/browse_public',
        'title' => t('Browse Public'),
        'callback' => 'imagepicker_browse_public',
        'access' => user_access('use public imagepicker'),
        'type' => MENU_LOCAL_TASK,
        'weight' => 3,
      );
    }
    $items[] = array(
      'path' => 'imagepicker/groups',
      'title' => t('Groups'),
      'callback' => 'imagepicker_groups',
      'access' => user_access('use imagepicker'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 4,
    );
    $items[] = array(
      'path' => 'imagepicker/edit',
      'title' => t('Edit image'),
      'callback' => 'imagepicker_image_edit',
      'access' => user_access('use imagepicker'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'imagepicker/image',
      'title' => t('Imagepicker'),
      'callback' => 'imagepicker_image_page',
      'access' => TRUE,
      'type' => MENU_CALLBACK,
    );

    // admin settings
    $items[] = array(
      'path' => 'admin/settings/imagepicker',
      'title' => t('Imagepicker'),
      'callback' => 'imagepicker_settings_page',
      'access' => user_access('administer imagepicker'),
      'description' => t('Imagepicker settings.'),
    );
    $items[] = array(
      'path' => 'admin/settings/imagepicker/general',
      'title' => t('General'),
      'description' => t('Imagepicker settings.'),
      'access' => user_access('administer imagepicker'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'imagepicker_settings_form',
      ),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );

    // admin groups and images
    $items[] = array(
      'path' => 'admin/settings/imagepicker/groups',
      'title' => t('Groups'),
      'description' => t('Show user groups.'),
      'access' => user_access('administer imagepicker'),
      'callback' => 'imagepicker_admin_groups',
      'type' => MENU_LOCAL_TASK,
      'weight' => -8,
    );
    $items[] = array(
      'path' => 'admin/settings/imagepicker/groups/autocomplete',
      'title' => t(''),
      'description' => t(''),
      'access' => user_access('administer imagepicker'),
      'callback' => 'imagepicker_group_search_autocomplete',
      'type' => MENU_LOCAL_TASK,
      'weight' => -9,
    );
    $items[] = array(
      'path' => 'admin/settings/imagepicker/images',
      'title' => t('Images'),
      'description' => t('Show user images.'),
      'access' => user_access('administer imagepicker'),
      'callback' => 'imagepicker_admin_images',
      'type' => MENU_LOCAL_TASK,
      'weight' => -9,
    );
    $items[] = array(
      'path' => 'admin/settings/imagepicker/images/autocomplete',
      'title' => t(''),
      'description' => t(''),
      'access' => user_access('administer imagepicker'),
      'callback' => 'imagepicker_user_autocomplete',
      'type' => MENU_LOCAL_TASK,
      'weight' => -9,
    );

    // admin validation
    $items[] = array(
      'path' => 'admin/settings/imagepicker/convert',
      'title' => t('Validate Files'),
      'description' => t('Validate and convert Imagepicker Files.'),
      'access' => user_access('administer imagepicker'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'imagepicker_convert_form',
      ),
      'type' => MENU_LOCAL_TASK,
      'weight' => -7,
    );
  }

  // in my account
  if (variable_get('imagepicker_account_enabled', 1)) {

    // public
    if (user_access('use public imagepicker') && variable_get('imagepicker_public_enabled', 1) && arg(3) == 'browse_public' && is_numeric(arg(4))) {
      $items[] = array(
        'path' => 'user/' . arg(1) . '/imagepicker',
        'title' => t('My imagepicker'),
        'description' => t('Manage your imagepicker files.'),
        'callback' => 'imagepicker_user_page',
        'access' => user_access('access own imagepicker'),
        'type' => MENU_LOCAL_TASK,
      );
    }
    else {
      $items[] = array(
        'path' => 'user/' . $user->uid . '/imagepicker',
        'title' => t('My imagepicker'),
        'description' => t('Manage your imagepicker files.'),
        'callback' => 'imagepicker_user_page',
        'access' => user_access('access own imagepicker'),
        'type' => MENU_LOCAL_TASK,
      );
    }
  }
  return $items;
}

/**
 * Implementation of hook_form_alter().
 */
function imagepicker_form_alter($form_id, &$form) {
  $node = $form['#node'];
  $node_types = node_get_types('names');
  $node_type = in_array($node->type, variable_get('imagepicker_node_types_enabled', $node_types), TRUE);

  // comment
  $comment = FALSE;
  $weight = 1;
  if (module_exists('comment') && variable_get('imagepicker_comment_enabled', 0) && preg_match('/comment_form$/i', $form_id)) {
    $comment = TRUE;
    $weight = 1;
  }

  // blocks
  $block = FALSE;
  if (preg_match('/block_box_form$/i', $form_id) && variable_get('imagepicker_blocks_enabled', 0)) {
    $block = TRUE;
    $weight = 1;
  }
  if (user_access('use imagepicker')) {
    if ($node_type && preg_match('/node_form$/i', $form_id) || $comment) {
      $form['body_filter']['file_upload'] = array(
        '#type' => 'fieldset',
        '#title' => t('Image picker'),
        '#collapsible' => 1,
        '#collapsed' => variable_get('imagepicker_advanced_collapsed', 0),
        '#weight' => $weight,
        '#validate' => array(),
        '#theme' => 'imagepicker_iframe',
        1 => array(),
      );
      $form['body_filter']['#prefix'] = '<a name="body_hash"></a>' . $form['body_filter']['#prefix'];
    }
    elseif ($block) {
      $form['block_settings']['file_upload'] = array(
        '#type' => 'fieldset',
        '#title' => t('Image picker'),
        '#collapsible' => 1,
        '#collapsed' => variable_get('imagepicker_advanced_collapsed', 0),
        '#weight' => $weight,
        '#validate' => array(),
        '#theme' => 'imagepicker_iframe',
        1 => array(),
      );
      $form['block_settings']['#prefix'] = '<a name="body_hash"></a>' . $form['block_settings']['#prefix'];
    }
  }
}

/**
 * Menu callback; presents the upload form for imagepicker
 */
function imagepicker_upload() {
  global $user;
  variable_del('imagepicker_advanced_browser_pagestart');
  if (variable_get('imagepicker_upload_progress_enabled', 1)) {
    $content = theme('imagepicker_upload_progress_get_script', variable_get('imagepicker_upload_progress_delay', 0));
  }
  $content .= imagepicker_quota_ok('iframe');
  theme('imagepicker', $content);
}
function imagepicker_upload_form($account = FALSE, $admin = FALSE) {
  $form['#attributes']['enctype'] = 'multipart/form-data';
  $form['file_upload'] = array(
    '#type' => 'file',
    '#title' => t('Image file'),
    '#description' => t('Browse your computer for image file'),
    '#required' => TRUE,
    '#value' => 1,
  );
  $form['thumb'] = array(
    '#type' => 'textfield',
    '#title' => t('Thumbnail size'),
    '#size' => 10,
    '#default_value' => variable_get('imagepicker_default_thumbnail_size', 100),
    '#description' => t('Size in pixels of thumbnail\'s bigger side'),
    '#required' => TRUE,
  );
  $form['scale'] = array(
    '#type' => 'textfield',
    '#title' => t('Scale image'),
    '#size' => 10,
    '#default_value' => variable_get('imagepicker_default_scale', ''),
    '#description' => t('Scale image to this size in pixels if not left empty'),
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('Add title for your image'),
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#rows' => 2,
    '#cols' => 80,
    '#description' => t('Add description for your image'),
  );

  // groups
  if (variable_get('imagepicker_groups_in_upload_enabled', 1)) {
    $grouplist = imagepicker_get_groups($admin ? $account : FALSE);
    if ($grouplist) {
      $form['grouplist'] = array(
        '#type' => 'checkboxes',
        '#default_value' => '',
        '#options' => $grouplist,
        '#title' => t('Your Groups'),
        '#description' => t('Select a group to add your image to.'),
      );
    }
  }
  if ($account) {
    $form['account'] = array(
      '#type' => 'hidden',
      '#value' => $account->uid,
    );
  }
  if ($admin) {
    $form['admin'] = array(
      '#type' => 'hidden',
      '#value' => 1,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Upload'),
  );
  if (variable_get('imagepicker_upload_progress_enabled', 1)) {
    $form['#suffix'] = theme('imagepicker_upload_progress_busy');
  }
  return $form;
}

/**
 * Validate upload form
 */
function imagepicker_upload_form_validate($form_id, $form_values) {
  foreach ($form_values as $name => $value) {
    $value = trim($value);
    switch ($name) {
      case 'file_upload':
        if (empty($_FILES['files']['name']['file_upload'])) {
          form_set_error($name, t('Image file field is required.'));
        }
        elseif (!isset($_FILES['files']['tmp_name']['file_upload']) || !file_exists($_FILES['files']['tmp_name']['file_upload'])) {
          form_set_error($name, t('Error while uploading file.'));
        }
        elseif (!image_get_info($_FILES['files']['tmp_name']['file_upload'])) {
          form_set_error($name, t('Uploaded file is not an image.'));
        }
        elseif (!imagepicker_get_uploaded_file_extension('file_upload')) {
          form_set_error($name, t('Only .jpg, .gif and .png image files are accepted.'));
        }
        break;
      case 'thumb':
        if (!preg_match('/^[0-9]{1,3}$/', $value) || $value <= 0) {
          form_set_error($name, t('Thumbnail size should be an integer between 1 and 999.'));
        }
        break;
      case 'scale':
        if (!preg_match('/^[0-9]{0,3}$/', $value)) {
          form_set_error($name, t('Scale value should be an integer between 1 and 999 or leave it empty if you don\'t want to scale your image.'));
        }
        break;
    }
  }
}

/**
 * Submit upload form
 */
function imagepicker_upload_form_submit($form_id, $form_values) {
  if ($form_values['op'] == t('Upload')) {
    if ($form_values['account'] && $form_values['admin']) {
      $user = user_load(array(
        'uid' => $form_values['account'],
      ));
    }
    else {
      global $user;
    }
    $destination = imagepicker_get_path(FALSE, $form_values['admin'] ? array(
      'name' => $user->name,
      'uid' => $user->uid,
    ) : TRUE);
    $thumbsdir = $destination . IMAGEPICKER_THUMBS_DIR;
    $browserdir = $destination . IMAGEPICKER_BROWSER_DIR;
    if (file_check_directory($destination, TRUE) && file_check_directory($thumbsdir, TRUE) && file_check_directory($browserdir, TRUE)) {

      // Add DIRECTORY_SEPARATORS here because drupals' functions remove trailing slashes
      $destination .= DIRECTORY_SEPARATOR;
      $thumbsdir = $thumbsdir . DIRECTORY_SEPARATOR;
      $browserdir = $browserdir . DIRECTORY_SEPARATOR;
      $maxthumbsize = $form_values['thumb'] ? $form_values['thumb'] : 100;
      $scaleto = $form_values['scale'] ? $form_values['scale'] : FALSE;
      if (!$scaleto) {

        // Use $path instead of original $destination variable cause this
        // variable's value will be changed during copying file, so we won't
        // loose it.
        $path = $destination;
        $imagemoved = imagepicker_copy_uploaded_file($path, 'file_upload');
        $file = basename($path);
      }
      else {
        $source = $_FILES['files']['tmp_name']['file_upload'];
        $file = imagepicker_get_uploaded_file_name($destination, 'file_upload');
        $imagescaled = imagepicker_scale_image($source, $destination . $file, $scaleto);
      }
      if (!$scaleto && $imagemoved || $scaleto && $imagescaled) {

        // Source file should still be an uploaded one, as scaled image
        // might have some watermarks etc. from drupal's filters/hooks.
        $source = $_FILES['files']['tmp_name']['file_upload'];
        if (imagepicker_scale_image($source, $thumbsdir . $file, $maxthumbsize)) {
          imagepicker_scale_image($source, $browserdir . $file, variable_get('imagepicker_default_browser_thumbnail_size', 100));
          $nextimgid = db_next_id('{imagepicker}_img_id');
          $title = htmlspecialchars($form_values['title']);
          $description = htmlspecialchars($form_values['description']);
          if (db_query("INSERT INTO {imagepicker} (uid, img_name, img_title, img_description) VALUES ('%d', '%s', '%s', '%s')", array(
            $user->uid,
            $file,
            $title,
            $description,
          ))) {

            // need to get the img_id if there are groups to add to
            if (is_array($form_values['grouplist']) && variable_get('imagepicker_groups_in_upload_enabled', 1)) {
              $result = db_query("SELECT img_id FROM {imagepicker} WHERE uid = '%d' AND img_name = '%s'", array(
                $user->uid,
                $file,
              ));
              $row = db_fetch_array($result);
              $record['img_id'] = $row['img_id'];
              foreach ($form_values['grouplist'] as $gid) {
                $record['gid'] = $gid;
                imagepicker_insert_group_image($record);
              }
            }
            drupal_set_message(t('Image was successfully uploaded.'));
            if ($form_values['admin']) {
              drupal_goto('admin/settings/imagepicker/images/user/' . $user->uid . '/browse');
            }
            elseif ($form_values['account']) {
              drupal_goto('user/' . $user->uid . '/imagepicker/browse');
            }
            else {
              drupal_goto('imagepicker/browse/' . $nextimgid);
            }
          }
          else {
            file_delete($thumbsdir . $file);
            file_delete($browserdir . $file);
            drupal_set_message(t('Error while saving information to database for uploaded image.'), 'error');
          }
        }
        else {
          drupal_set_message(t('Error while creating a thumbnail for uploaded image.'), 'error');
        }
      }
      else {
        if (!$scaleto && !$imagemoved) {
          drupal_set_message(t('Error while moving uploaded file to its destination.'), 'error');
        }
        else {
          drupal_set_message(t('Error while scaling uploaded file.'), 'error');
        }
      }
      file_delete($destination . $file);
    }
    else {
      drupal_set_message(t('Unable to create a directory structure for your images.'), 'error');
    }
  }
  if ($form_values['admin']) {
    drupal_goto('admin/settings/imagepicker/images');
  }
  elseif ($form_values['account']) {
    drupal_goto('user/' . $user->uid . '/imagepicker');
  }
  else {
    drupal_goto('imagepicker');
  }
}
function imagepicker_get_path($url = FALSE, $userdir = FALSE) {
  global $user, $base_url;
  $dirsep = !$url ? DIRECTORY_SEPARATOR : '/';
  if (!$url) {
    $path = str_replace('/', DIRECTORY_SEPARATOR, getcwd());
  }
  else {
    if (variable_get('imagepicker_use_full_url', 1)) {
      $path = $base_url;
    }
    else {
      $path = base_path();
      $path = preg_replace("/\\/\$/", "", $path);
    }
  }
  $path .= $dirsep . file_directory_path() . $dirsep . IMAGEPICKER_FILES_DIR . $dirsep;

  // old or new
  $fstype = variable_get('imagepicker_fstype', '');
  if ($userdir) {
    if ($fstype == 'alpha') {
      $username = !is_array($userdir) ? $user->name : $userdir['name'];
      $firstletter = drupal_strtolower(drupal_substr($username, 0, 1));
      $firstletter = preg_match('/^[a-z]$/', $firstletter) ? $firstletter : 'others';
      $path .= $firstletter . $dirsep . $username . $dirsep;
    }
    else {
      $useruid = !is_array($userdir) ? $user->uid : $userdir['uid'];
      $path .= $useruid . $dirsep;
    }
  }
  return $path;
}
function imagepicker_copy_uploaded_file(&$destination, $name) {
  $source = $_FILES['files']['tmp_name'][$name];
  if (file_copy($source, $destination, FILE_EXISTS_RENAME)) {

    // Fix bug in drupal's file_copy function which uses '/' instead of
    // DIRECTORY_SEPARATOR for making directories. This causes problems on
    // Windows mashines.
    $source = str_replace('/', DIRECTORY_SEPARATOR, $source);
    $file = imagepicker_get_uploaded_file_name($destination, $name);
    $destination = $destination . $file;
    return @rename($source, $destination);
  }
  return FALSE;
}
function imagepicker_get_uploaded_file_extension($name) {
  switch ($_FILES['files']['type'][$name]) {

    // For some reason Internet Explorer 7 and later decides to give us
    // non-standard MIME types for jpg and png files, this is supposedly
    // for 'backwards compatibility'. For more details see:
    // http://msdn.microsoft.com/en-us/library/ms775147(VS.85).aspx#_replace
    // Non-standard MIME type:
    case 'image/pjpeg':
    case 'image/jpeg':
      $fileext = '.jpg';
      break;
    case 'image/gif':
      $fileext = '.gif';
      break;

    // Non-standard MIME type:
    case 'image/x-png':
    case 'image/png':
      $fileext = '.png';
      break;
    default:
      $fileext = '';
  }
  return $fileext;
}
function imagepicker_get_uploaded_file_name($destination, $name) {
  $fileext = imagepicker_get_uploaded_file_extension($name);
  if (FALSE !== strpos($_FILES['files']['name'][$name], '.')) {
    $filename = drupal_substr($_FILES['files']['name'][$name], 0, strrpos($_FILES['files']['name'][$name], '.'));
  }
  else {
    $filename = $_FILES['files']['name'][$name];
  }
  $file = $filename . $fileext;
  $i = 0;
  while (file_exists($destination . $file)) {
    $i++;
    $file = $filename . '_' . $i . $fileext;
  }
  return $file;
}
function imagepicker_get_image_path($img, $type = 'browser', $public = FALSE) {
  $imgbasedir = imagepicker_get_path(FALSE, $public ? $public : TRUE);
  switch ($type) {
    case 'browser':
      if (file_exists($imgbasedir . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR . $img['img_name'])) {
        $imgpath = imagepicker_get_path(TRUE, $public ? $public : TRUE) . IMAGEPICKER_BROWSER_DIR . '/' . $img['img_name'];
      }
      elseif (file_exists($imgbasedir . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR . $img['img_name'])) {
        $imgpath = imagepicker_get_path(TRUE, $public ? $public : TRUE) . IMAGEPICKER_THUMBS_DIR . '/' . $img['img_name'];
      }
      break;
    case 'full':
      if (file_exists($imgbasedir . $img['img_name'])) {
        $imgpath = imagepicker_get_path(TRUE, $public ? $public : TRUE) . $img['img_name'];
      }
      break;
    case 'thumb':
    default:
      if (file_exists($imgbasedir . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR . $img['img_name'])) {
        $imgpath = imagepicker_get_path(TRUE, $public ? $public : TRUE) . IMAGEPICKER_THUMBS_DIR . '/' . $img['img_name'];
      }
      elseif (file_exists($imgbasedir . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR . $img['img_name'])) {
        $imgpath = imagepicker_get_path(TRUE, $public ? $public : TRUE) . IMAGEPICKER_BROWSER_DIR . '/' . $img['img_name'];
      }
      break;
  }
  return $imgpath ? $imgpath : '';
}
function imagepicker_scale_image($source, $destination, $maxsize) {
  $info = image_get_info($source);
  $width = $maxsize >= $info['width'] ? $info['width'] : $maxsize;
  $height = $maxsize >= $info['height'] ? $info['height'] : $maxsize;
  $aspect = $info['height'] / $info['width'];
  if ($aspect < $height / $width) {
    $width = (int) min($width, $info['width']);
    $height = (int) round($width * $aspect);
  }
  else {
    $height = (int) min($height, $info['height']);
    $width = (int) round($height / $aspect);
  }
  return image_toolkit_invoke('resize', array(
    $source,
    $destination,
    $width,
    $height,
  ));
}
function imagepicker_browse() {
  if (arg(2)) {
    imagepicker_image_select(arg(2));
    exit;
  }
  $content = _imagepicker_browse();
  theme('imagepicker', $content);
}
function imagepicker_browse_public() {
  if (arg(2)) {
    imagepicker_image_select(arg(2), FALSE, TRUE);
    exit;
  }
  $content = _imagepicker_browse_public();
  theme('imagepicker', $content);
}
function imagepicker_image_select($img_id, $showgroup = TRUE, $public = FALSE, $account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $img = _imagepicker_get_img($img_id, $public ? FALSE : TRUE);
  if ($img) {
    drupal_add_js(imagepicker_js($img, $public), 'inline');
    $imgpath = imagepicker_get_image_path($img, 'browser', $public ? array(
      'name' => $img['name'],
      'uid' => $img['uid'],
    ) : FALSE);
    $content = '<div class="imgp_help">' . t('Choose the settings you want, place the cursor in the Body box above and Insert image.') . '</div>';
    if ($img['img_title']) {
      $content .= '<div id="imgp_img_holder_title">' . check_plain($img['img_title']) . '</div>';
    }
    if ($img['img_description']) {
      $content .= '<div id="imgp_img_holder_description">' . $img['img_description'] . '</div>';
    }
    $content .= '<div id="imgp_img_holder">';
    $content .= '<img id="imgp_img" src="' . $imgpath . '" alt="' . check_plain($img['img_title']) . '" title="' . $img['img_name'] . '" />';
    $content .= '</div>';
    if ($user->uid == $img['uid'] || $public) {
      $content .= drupal_get_form('imagepicker_image_form', $img['img_id'], $public);
    }

    // groups
    if (_imagepicker_has_groups() && $showgroup) {
      $content .= drupal_get_form('imagepicker_group_images_form', $img['img_id']);
    }
  }
  else {
    drupal_set_message(t('Image not found.'), 'error');
    $content = '';
  }
  theme('imagepicker', $content);
}
function imagepicker_image_form($img_id, $public = FALSE) {
  $showoptions = array(
    'full' => t('Full size'),
    'thumb' => t('Thumbnail'),
    'title' => t('Title'),
  );
  $linkoptions = array(
    'none' => t('None'),
    'file' => t('File'),
    'page' => t('Page'),
  );
  if (module_exists('lightbox2') && variable_get('imagepicker_lightbox2_enable', 1)) {
    $linkoptions['lightbox'] = t('Lightbox');
  }
  if (variable_get('imagepicker_default_align_show', 1)) {
    $alignoptions = array(
      'none' => t('None'),
      'fleft' => t('Float Left'),
      'fright' => t('Float right'),
    );
    $form['align'] = array(
      '#type' => 'radios',
      '#title' => t('Align'),
      '#default_value' => variable_get('imagepicker_insert_defaults_align', 'none'),
      '#options' => $alignoptions,
      '#description' => '',
      '#prefix' => '<div class="imgp_img_options">',
      '#suffix' => '</div>',
    );
  }
  $form['show'] = array(
    '#type' => 'radios',
    '#id' => 'show',
    '#title' => t('Show'),
    '#default_value' => variable_get('imagepicker_insert_defaults_show', 'full'),
    '#options' => $showoptions,
    '#description' => '',
    '#prefix' => '<div class="imgp_img_options">',
    '#suffix' => '</div>',
  );
  $form['link'] = array(
    '#type' => 'radios',
    '#title' => t('Link'),
    '#default_value' => variable_get('imagepicker_insert_defaults_link', 'none'),
    '#options' => $linkoptions,
    '#description' => '',
    '#prefix' => '<div class="imgp_img_options">',
    '#suffix' => '</div>',
  );
  $form['desc'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Description'),
    '#description' => t('Insert title and description'),
    '#prefix' => '<div id="imgp_desc_control">',
    '#suffix' => '</div>',
  );
  $form['insert'] = array(
    '#type' => 'button',
    '#value' => t('Insert image'),
    '#prefix' => '<div id="imgp_controls">',
    '#attributes' => array(
      'onclick' => 'imagepickerInsert(this); return false;',
    ),
  );
  if (!$public) {
    $form['edit'] = array(
      '#type' => 'submit',
      '#value' => t('Edit image'),
    );
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete image'),
      '#suffix' => '</div>',
    );
  }
  $form['img_id'] = array(
    '#type' => 'hidden',
    '#value' => $img_id,
  );
  return $form;
}

/**
 * Submit image form
 */
function imagepicker_image_form_submit($form_id, $form_values) {
  if ($form_values['op'] == t('Delete image')) {
    imagepicker_image_delete($form_values['img_id'], FALSE, 'iframe');
  }
  elseif ($form_values['op'] == t('Edit image')) {
    drupal_goto('imagepicker/edit/' . $form_values['img_id']);
  }
}
function imagepicker_image_delete($img_id, $account = FALSE, $src = 'iframe') {
  if ($account) {
    $user = $account;
    $userdir = array(
      'name' => $user->name,
      'uid' => $user->uid,
    );
  }
  else {
    global $user;
    $userdir = TRUE;
  }
  if ($src == 'account') {
    $outpath = 'user/' . $user->uid . '/imagepicker/images/browse';
  }
  elseif ($src == 'admin') {
    $outpath = 'admin/settings/imagepicker/images/user/' . $user->uid . '/browse';
  }
  else {
    $outpath = 'imagepicker/browse';
  }
  $img = _imagepicker_get_img($img_id, $src == 'admin' ? FALSE : TRUE, $account ? $user : FALSE);
  if ($img) {
    $destination = imagepicker_get_path(FALSE, $userdir);
    $thumbsdir = $destination . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR;
    $browserdir = $destination . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR;
    file_delete($destination . $img['img_name']);
    file_delete($thumbsdir . $img['img_name']);
    file_delete($browserdir . $img['img_name']);
    if (db_query("DELETE FROM {imagepicker} WHERE uid = %d AND img_id = %d", array(
      $user->uid,
      $img_id,
    ))) {

      // groups entries
      db_query("DELETE FROM {imagepicker_group_images} WHERE img_id = %d", array(
        $img_id,
      ));
      drupal_set_message(t('Image was successfully deleted'));
    }
    else {
      drupal_set_message(t('Error while trying to delete your image from database.'), 'error');
    }
  }
  else {
    drupal_set_message(t('Image not found.'), 'error');
  }
  drupal_goto($outpath);
}

/**
 * Menu callback; fetches the image edit form for imagepicker
 */
function imagepicker_image_edit() {
  $content = _imagepicker_edit_img(arg(2));
  theme('imagepicker', $content);
}

#function imagepicker_edit_form($img, $account=FALSE, $admin = FALSE) {
function imagepicker_edit_form($img, $src = 'iframe', $account = FALSE) {
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('Edit title of your image'),
    '#default_value' => htmlspecialchars_decode($img['img_title']),
    '#prefix' => '<div id="imgp_edit_form">',
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#rows' => 2,
    '#cols' => 80,
    '#description' => t('Edit description of your image'),
    '#default_value' => htmlspecialchars_decode($img['img_description']),
    '#suffix' => '</div>',
  );
  if ($account) {
    $form['account'] = array(
      '#type' => 'hidden',
      '#value' => $account->uid,
    );
  }
  $form['img_id'] = array(
    '#type' => 'hidden',
    '#value' => $img['img_id'],
  );
  $form['src'] = array(
    '#type' => 'hidden',
    '#value' => $src,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#prefix' => '<div id="imgp_controls">',
  );
  $form['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#suffix' => '</div>',
  );
  return $form;
}

/**
 * Submit edit form
 */
function imagepicker_edit_form_submit($form_id, $form_values) {
  if ($form_values['op'] == t('Submit')) {
    $img_id = $form_values['img_id'];
    $src = $form_values['src'];
    if ($form_values['account'] && $src == 'admin') {
      $user = user_load(array(
        'uid' => $form_values['account'],
      ));
    }
    else {
      global $user;
    }
    if ($src == 'admin' && $form_values['account']) {
      $img = _imagepicker_get_img($img_id, FALSE, $user);
    }
    elseif ($src == 'account') {
      $img = _imagepicker_get_img($img_id);
    }
    else {
      $img = _imagepicker_get_img($img_id);
    }
    if ($img) {
      $title = htmlspecialchars($form_values['title']);
      $description = htmlspecialchars($form_values['description']);
      if (db_query("UPDATE {imagepicker} SET img_title = '%s', img_description = '%s' WHERE img_id = '%d'", array(
        $title,
        $description,
        $img_id,
      ))) {
        drupal_set_message(t('Image was successfully updated.'));
        if ($src == 'account') {
          $outpath = 'user/' . $user->uid . '/imagepicker/images/browse/' . $img_id;
        }
        elseif ($src == 'admin') {
          $outpath = 'admin/settings/imagepicker/images/user/' . $user->uid . '/browse/' . $img_id;
        }
        else {
          $outpath = 'imagepicker/browse/' . $img_id;
        }
        drupal_goto($outpath);
      }
      else {
        drupal_set_message(t('Error while updating image.'), 'error');
      }
    }
    else {
      drupal_set_message(t('Image not found.'), 'error');
    }
  }
  if ($src == 'account') {
    $outpath = 'user/' . $user->uid . '/imagepicker/images';
  }
  elseif ($src == 'admin') {
    $outpath = 'admin/settings/imagepicker/images';
  }
  else {
    $outpath = 'imagepicker/browse';
  }
  drupal_goto($outpath);
}

/**
 * Menu callback; presents the image page for imagepicker
 */
function imagepicker_image_page() {
  global $base_url;
  $content = '';
  $result = db_query_range("SELECT i.*, u.name FROM {imagepicker} AS i JOIN {users} AS u USING (uid) WHERE img_id = '%d'", arg(2), 0, 1);
  $img = db_fetch_array($result);
  if (!is_array($img) || !count($img)) {
    drupal_set_message(t('Image not found.'), 'error');
    $content = '';
  }
  else {
    $path = drupal_get_path('module', 'imagepicker');
    drupal_add_css($path . '/imagepicker.css');
    drupal_set_title(check_plain($img['img_title']));
    $imgsrc = imagepicker_get_path(TRUE, $img) . $img['img_name'];
    $content = '<div id="imgp_page"><div id="imgp_page_img">';
    $content .= '<a href="' . $imgsrc . '" alt="' . check_plain($img['img_title']) . '" target="_blank">';
    $content .= '<img src="' . $imgsrc . '" alt="' . $img['img_title'] . '" />';
    $content .= '</a></div><div>' . nl2br($img['img_description']) . '</div>';
    if (variable_get('imagepicker_default_pagelink', 1)) {
      $content .= '<div><a href="#" onclick="history.back()">' . t('Return to page') . '</a></div>';
    }
    $content .= '</div>';
  }
  return $content;
}

/**
 * There is no need to inform users that directory structure has been created
 * and show them all paths... So lets strip these messages if there are any.
 */
function imagepicker_strip_messages($msg) {
  if ($msg) {
    $dirsep = DIRECTORY_SEPARATOR == '\\' ? '\\\\' : '\\/';
    $pattern = '/<li>.*' . $dirsep . IMAGEPICKER_FILES_DIR . $dirsep . '.*<\\/li>/i';
    $msg = preg_replace($pattern, '', $msg);
  }
  return $msg;
}

/**
 * javascript insertion routine
 */
function imagepicker_js($img, $public = FALSE) {
  $ret = "\n  function imagepickerInsert(button) {\n    // Get the form element\n    var imgpForm = document.getElementById('imagepicker-image-form');\n    if (imgpForm) {\n      var imgpShow = 'thumb';\n      var imgpLink = 'file';\n      var imgpAlign = 'none';\n      var imgpImagePath;\n      var imgpImageTitle = '" . ($img['img_title'] ? addslashes(check_plain($img['img_title'])) : t('Image')) . "';\n      var imgpImageDesc = '" . $img['img_description'] . "';\n      var imgpFileLink = '" . imagepicker_get_image_path($img, 'full', $public ? array(
    'name' => $img['name'],
    'uid' => $img['uid'],
  ) : FALSE) . "';\n      var imgpThumbLink = '" . imagepicker_get_image_path($img, 'thumb', $public ? array(
    'name' => $img['name'],
    'uid' => $img['uid'],
  ) : FALSE) . "';\n      var imgpPageLink = '" . url('imagepicker/image/' . $img['img_id']) . "';\n      var imgpImageElement;\n      var imgpLinkElement;\n      var imgpImageStyle;\n      var isFCKeditor = '" . (module_exists('fckeditor') ? 'yes' : 'no') . "'\n      var imgpInsertion;\n      var i;\n\n      // Get show value\n      for (i = 0; i < imgpForm.show.length; i++) {\n        if(imgpForm.show[i].checked) {\n          var imgpShow = imgpForm.show[i].value\n        }\n      }\n      // Get link value\n      for (i = 0; i < imgpForm.link.length; i++) {\n        if(imgpForm.link[i].checked) {\n          var imgpLink = imgpForm.link[i].value\n        }\n      }\n";
  if (variable_get('imagepicker_default_align_show', 1)) {
    $ret .= "\n      // Get align value\n      for (i = 0; i < imgpForm.align.length; i++) {\n        if(imgpForm.align[i].checked) {\n          var imgpAlign = imgpForm.align[i].value\n        }\n      }\n\n      // Create a style for image holder\n      switch (imgpAlign) {\n        case 'fleft':\n          imgpImageStyle = '" . variable_get('imagepicker_default_fleft', 'style="float: left"') . "';\n          break;\n\n        case 'fright':\n          imgpImageStyle = '" . variable_get('imagepicker_default_fright', 'style="float: right"') . "';\n          break;\n\n        case 'none':\n        default:\n          imgpImageStyle = '';\n          break;\n      }\n";
  }
  else {
    $ret .= "\n      imgpImageStyle = '';\n";
  }
  $ret .= "\n      switch (imgpShow) {\n        case 'full': imgpImagePath = imgpFileLink; break;\n        case 'title': imgpImagePath = ''; break;\n        case 'thumb':\n        default: imgpImagePath = imgpThumbLink; break;\n      }\n\n      // Create an image or span (containing title) HTML string\n      if (imgpImagePath) {\n        imgpImageElement = '<img src=\"'+imgpImagePath+'\" alt=\"'+imgpImageTitle+'\" ' + imgpImageStyle + ' \\/>';\n      }\n      else {\n        imgpImageElement = '<span>'+imgpImageTitle+'<\\/span>'\n      }\n\n      // Create a link HTML string\n      switch (imgpLink) {\n        case 'none': imgpLinkElement = '%imgpImageElement%'; break;\n        case 'page': imgpLinkElement = '<a href=\"'+imgpPageLink+'\" title=\"'+imgpImageTitle+'\" >%imgpImageElement%<\\/a>'; break;\n        case 'file': imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" target=\"_blank\" >%imgpImageElement%<\\/a>'; break;\n";
  if (module_exists('lightbox2') && variable_get('imagepicker_lightbox2_enable', 1)) {
    $ret .= "\n        case 'lightbox': imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" rel=\"" . variable_get('imagepicker_lightbox2_insert', 'lightbox') . "\" >%imgpImageElement%<\\/a>'; break;\n";
  }
  $ret .= "\n        default: imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" target=\"_blank\" >%imgpImageElement%<\\/a>'; break;\n      }\n      // add title and description if requested\n      if (imgpForm.desc.checked) {\n        imgpLinkElement = '<div class=\"imgp_title\">' + imgpImageTitle + '</div>' + imgpLinkElement + '<div class=\"imgp_desc\">' + imgpImageDesc + '</div>';\n      }\n      // Create a HTML string which should be inserted in the node body\n      imgpInsertion = imgpLinkElement.replace('%imgpImageElement%', imgpImageElement);\n\n      // Get the parent window of imagepicker iframe\n      var win = window.opener ? window.opener : window.dialogArguments;\n      if (!win) {\n        if (window.parent) {\n          win = window.parent;\n        }\n        else {\n          win = top;\n        }\n      }\n      //var isTinyMCE = win.document.getElementById('mce_editor_0'); // buggy\n      var isTinyMCE = win.tinyMCE; // Will be undefined if tinyMCE isn't loaded. This isn't a sure-proof way of knowing if tinyMCE is loaded into a field, but it works.\n\n      if (isFCKeditor == 'yes') {\n        myFCKeditor = win.FCKeditorAPI.GetInstance(win.fckLaunchedJsId);\n      }\n\n      if (isTinyMCE) {\n        win.tinyMCE.execCommand('mceInsertContent', false, imgpInsertion);\n      }\n      else if (isFCKeditor == 'yes' && myFCKeditor && win.fckIsRunning[win.fckLaunchedJsId] && win.document.getElementById(win.fckLaunchedTextareaId[0]).style.display=='none' ){\n        myFCKeditor.InsertHtml(imgpInsertion);\n      }\n      else {\n        var nodeBody = win.document.getElementById('edit-body');\n        var commentBody = win.document.getElementById('edit-comment');\n        if (nodeBody) {\n          insertAtCursor(nodeBody, imgpInsertion);\n        }\n        if (commentBody) {\n          insertAtCursor(commentBody, imgpInsertion);\n        }\n      }\n      win.location.hash='body_hash';\n    }\n  }\n\n  // Copy pasted from internet...\n  function insertAtCursor(myField, myValue) {\n    //IE support\n    if (document.selection) {\n      myField.focus();\n\n      //in effect we are creating a text range with zero\n      //length at the cursor location and replacing it\n      //with myValue\n      sel = document.selection.createRange();\n      sel.text = myValue;\n    }\n\n    //Mozilla/Firefox/Netscape 7+ support\n    else if (myField.selectionStart || myField.selectionStart == '0') {\n\n      //Here we get the start and end points of the\n      //selection. Then we create substrings up to the\n      //start of the selection and from the end point\n      //of the selection to the end of the field value.\n      //Then we concatenate the first substring, myValue,\n      //and the second substring to get the new value.\n      var startPos = myField.selectionStart;\n      var endPos = myField.selectionEnd;\n      myField.value = myField.value.substring(0, startPos)+ myValue + myField.value.substring(endPos, myField.value.length);\n\n    }\n    else {\n      myField.value += myValue;\n    }\n  }\n";
  return $ret;
}

/**
 * Menu callback; presents the settings form for imagepicker
 */
function imagepicker_settings_page($op = NULL) {
  $content = drupal_get_form('imagepicker_settings_form');
  return $content;
}
function imagepicker_settings_form() {

  // default settings
  $form['imagepicker_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Set imagepicker settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['imagepicker_settings']['imagepicker_default_browser_thumbnail_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Browser Thumbnail Size'),
    '#size' => 10,
    '#required' => TRUE,
    '#default_value' => variable_get('imagepicker_default_browser_thumbnail_size', 100),
    '#description' => t('Configure the default browser thumbnail size'),
  );
  $node_types = node_get_types('names');
  $form['imagepicker_settings']['imagepicker_node_types_enabled'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Node Types'),
    '#description' => t('Set the node types you want to enable Imagepicker for.'),
    '#default_value' => variable_get('imagepicker_node_types_enabled', array_keys($node_types)),
    '#options' => $node_types,
  );
  if (module_exists('comment')) {
    $form['imagepicker_settings']['imagepicker_comment_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Comments'),
      '#return_value' => 1,
      '#default_value' => variable_get('imagepicker_comment_enabled', 0),
      '#description' => t('Setting this option enables Imagepicker in comments.'),
    );
  }
  $form['imagepicker_settings']['imagepicker_blocks_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Blocks'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_blocks_enabled', 0),
    '#description' => t('Setting this option enables Imagepicker in blocks.'),
  );
  $form['imagepicker_settings']['imagepicker_default_pagelink'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show return link in page'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_default_pagelink', 1),
    '#description' => t('Setting this option will add a link back to the thumbnail when using the page option. Uses javascript history(back).'),
  );
  $form['imagepicker_settings']['imagepicker_account_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Imagepicker in My Account'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_account_enabled', 1),
    '#description' => t('Setting this option enables Imagepicker in My Account.'),
  );

  // default options
  $form['imagepicker_defaults'] = array(
    '#type' => 'fieldset',
    '#title' => t('Set imagepicker defaults'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['imagepicker_defaults']['imagepicker_default_thumbnail_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Thumbnail Size'),
    '#size' => 10,
    '#required' => TRUE,
    '#default_value' => variable_get('imagepicker_default_thumbnail_size', 100),
    '#description' => t('Configure the default thumbnail size'),
  );
  $form['imagepicker_defaults']['imagepicker_default_scale'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Scale'),
    '#size' => 10,
    '#required' => FALSE,
    '#default_value' => variable_get('imagepicker_default_scale', ''),
    '#description' => t('Configure the default scale. leave empty for no default scale'),
  );

  // insert settings
  $form['imagepicker_insert_defaults'] = array(
    '#type' => 'fieldset',
    '#title' => t('Set imagepicker insert defaults'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#description' => '',
  );
  $showoptions = array(
    'full' => t('Full size'),
    'thumb' => t('Thumbnail'),
    'title' => t('Title'),
  );
  $linkoptions = array(
    'none' => t('None'),
    'file' => t('File'),
    'page' => t('Page'),
  );
  if (module_exists('lightbox2') && variable_get('imagepicker_lightbox2_enable', 1)) {
    $linkoptions['lightbox'] = t('Lightbox');
  }
  $alignoptions = array(
    'none' => t('None'),
    'fleft' => t('Float Left'),
    'fright' => t('Float right'),
  );
  $form['imagepicker_insert_defaults']['imagepicker_insert_defaults_align'] = array(
    '#type' => 'radios',
    '#title' => t('Align'),
    '#default_value' => 'none',
    '#options' => $alignoptions,
    '#description' => '',
    '#default_value' => variable_get('imagepicker_insert_defaults_align', 'none'),
  );
  $form['imagepicker_insert_defaults']['imagepicker_insert_defaults_show'] = array(
    '#type' => 'radios',
    '#id' => 'show',
    '#title' => t('Show'),
    '#default_value' => 'full',
    '#options' => $showoptions,
    '#description' => '',
    '#default_value' => variable_get('imagepicker_insert_defaults_show', 'full'),
  );
  $form['imagepicker_insert_defaults']['imagepicker_insert_defaults_link'] = array(
    '#type' => 'radios',
    '#title' => t('Link'),
    '#default_value' => 'none',
    '#options' => $linkoptions,
    '#description' => '',
    '#default_value' => variable_get('imagepicker_insert_defaults_link', 'none'),
  );

  // alignment settings
  $form['imagepicker_align'] = array(
    '#type' => 'fieldset',
    '#title' => t('Image alignment settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['imagepicker_align']['imagepicker_default_align_show'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Alignment options'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_default_align_show', 1),
    '#description' => t('Unsetting this option will remove the alignment options from the insert page.'),
  );
  $form['imagepicker_align']['imagepicker_default_fleft'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Float left code'),
    '#size' => 25,
    '#maxlength' => 50,
    '#required' => TRUE,
    '#default_value' => variable_get('imagepicker_default_fleft', 'style="float: left"'),
    '#description' => t('Configure the default code used for float left styling.'),
  );
  $form['imagepicker_align']['imagepicker_default_fright'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Float right code'),
    '#size' => 25,
    '#maxlength' => 50,
    '#required' => TRUE,
    '#default_value' => variable_get('imagepicker_default_fright', 'style="float: right"'),
    '#description' => t('Configure the default code used for float right styling.'),
  );
  if (module_exists('lightbox2')) {

    // lightbox2 integration
    $form['imagepicker_lightbox2'] = array(
      '#type' => 'fieldset',
      '#title' => t('Lightbox integration'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['imagepicker_lightbox2']['imagepicker_lightbox2_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable Lightbox2 in Imagepicker'),
      '#return_value' => 1,
      '#default_value' => variable_get('imagepicker_lightbox2_enable', 1),
      '#description' => t('Unsetting this option will disable Lightbox2 in Imagepicker.'),
    );
    $form['imagepicker_lightbox2']['imagepicker_lightbox2_insert'] = array(
      '#type' => 'textfield',
      '#title' => t('Default Lightbox insert'),
      '#size' => 15,
      '#maxlength' => 30,
      '#required' => TRUE,
      '#default_value' => variable_get('imagepicker_lightbox2_insert', 'lightbox'),
      '#description' => t('Configure the default code inserted into the "rel" attribute.'),
    );
  }

  // quota
  $form['imagepicker_quotas'] = array(
    '#type' => 'fieldset',
    '#title' => t('User quotas'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['imagepicker_quotas']['imagepicker_quota_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Quotas in Imagepicker'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_quota_enable', 1),
    '#description' => t('Unsetting this option will disable Quotas in Imagepicker.'),
  );
  $form['imagepicker_quotas']['imagepicker_quota_default'] = array(
    '#type' => 'select',
    '#default_value' => variable_get('imagepicker_quota_default', 0),
    '#options' => imagepicker_get_quota_list(),
    '#title' => t('Set default quota'),
  );

  // advanced settings
  $form['imagepicker_advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced imagepicker settings'),
    '#description' => t('You can alter the default styling of the iframe here,<br />useful if the iframe is not fitting in with your theme.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['imagepicker_advanced']['imagepicker_advanced_iframe_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Iframe width'),
    '#description' => t('Configure the iframe width. This can be a number or a percentage, eg 400 or 100%'),
    '#size' => 10,
    '#required' => TRUE,
    '#default_value' => variable_get('imagepicker_advanced_iframe_width', "100%"),
  );
  $form['imagepicker_advanced']['imagepicker_advanced_iframe_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Iframe height'),
    '#description' => t('Configure the iframe height. This must be a number, eg 500'),
    '#size' => 10,
    '#required' => TRUE,
    '#default_value' => variable_get('imagepicker_advanced_iframe_height', "500"),
  );
  $form['imagepicker_advanced']['imagepicker_advanced_iframe_border'] = array(
    '#type' => 'textfield',
    '#title' => t('Iframe border'),
    '#description' => t('Configure the iframe border. This can be a number, eg 0 or 1, or a css entry such as 1px solid #808080'),
    '#size' => 25,
    '#required' => TRUE,
    '#default_value' => variable_get('imagepicker_advanced_iframe_border', "0"),
  );
  $form['imagepicker_advanced']['imagepicker_advanced_browser_columns'] = array(
    '#type' => 'textfield',
    '#title' => t('Browser columns'),
    '#description' => t('Configure the number of columns in the image browser. This must be a number, 0 for normal wraparound'),
    '#size' => 10,
    '#required' => TRUE,
    '#default_value' => variable_get('imagepicker_advanced_browser_columns', 0),
  );
  $form['imagepicker_advanced']['imagepicker_advanced_browser_page'] = array(
    '#type' => 'textfield',
    '#title' => t('Images per page'),
    '#description' => t('Configure the number of images shown per page in the image browser. This must be a number, 0 for no paging'),
    '#size' => 10,
    '#required' => TRUE,
    '#default_value' => variable_get('imagepicker_advanced_browser_page', 20),
  );
  $form['imagepicker_advanced']['imagepicker_advanced_collapsed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Fieldset state'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_advanced_collapsed', 0),
    '#description' => t('Setting this option will collapse the fieldset the iframe is in by default.'),
  );
  $form['imagepicker_advanced']['imagepicker_use_full_url'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use full url'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_use_full_url', 1),
    '#description' => t('Setting this option will make imagepicker use a full url to the image being inserted, unsetting it will use an absolute path.<br />This is useful if you are developing a site with a different url than the production site will be on.'),
  );
  $form['imagepicker_advanced']['imagepicker_show_browse_order_form'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show the order select box in the browser'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_show_browse_order_form', 1),
    '#description' => t(''),
  );
  $orderlist = array(
    'img_id DESC' => t('Newest first'),
    'img_id ASC' => t('Newest last'),
    'img_date DESC' => t('Edited first'),
    'img_date ASC' => t('Edited last'),
    'img_name' => t('By name'),
  );
  $form['imagepicker_advanced']['imagepicker_default_browser_order'] = array(
    '#type' => 'select',
    '#default_value' => variable_get('imagepicker_default_browser_order', 'img_id DESC'),
    '#options' => $orderlist,
    '#title' => t('Default order'),
    '#description' => t('The default order used to sort the browser. This will be applied wether or not the select box is visible'),
  );
  $form['imagepicker_advanced']['imagepicker_public_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Public option'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_public_enabled', 1),
    '#description' => t('Allow users to make image groups publicly available.'),
  );
  $form['imagepicker_advanced']['imagepicker_publicroles_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Public Role Control option'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_publicroles_enabled', 1),
    '#description' => t('Allow users to make publicly available image groups restricted by role.'),
  );
  $form['imagepicker_advanced']['imagepicker_groups_in_upload_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Groups in upload form '),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_groups_in_upload_enabled', 1),
    '#description' => t('Allow users to select a group in the upload form.'),
  );
  $form['imagepicker_advanced']['imagepicker_upload_progress_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Progress bar in upload'),
    '#return_value' => 1,
    '#default_value' => variable_get('imagepicker_upload_progress_enabled', 1),
    '#description' => t(''),
  );
  $form['imagepicker_advanced']['imagepicker_upload_progress_delay'] = array(
    '#type' => 'textfield',
    '#title' => t('Progress bar Delay'),
    '#size' => 3,
    '#default_value' => variable_get('imagepicker_upload_progress_delay', 3),
    '#field_suffix' => t('seconds'),
    '#description' => t('Show the progress bar if processing is longer than this value.'),
  );
  $form['imagepicker_advanced']['imagepicker_upload_progress_message'] = array(
    '#type' => 'textfield',
    '#title' => t('Progress bar message'),
    '#size' => 60,
    '#default_value' => variable_get('imagepicker_upload_progress_message', t('Processing form... please wait.')),
    '#description' => t('Message to display while uploading.'),
  );
  return system_settings_form($form);
}

/**
 * Validate settings form
 */
function imagepicker_settings_form_validate($form_id, $form_values) {
  global $user;
  if (!drupal_strlen($form_values['imagepicker_default_thumbnail_size'])) {
    form_set_error('imagepicker_default_thumbnail_size', t('You must fill in the Thumbnail field'));
  }
  if (!is_numeric($form_values['imagepicker_default_thumbnail_size'])) {
    form_set_error('imagepicker_default_thumbnail_size', t('The Thumbnail field must be a number'));
  }
  if (!drupal_strlen($form_values['imagepicker_default_browser_thumbnail_size'])) {
    form_set_error('imagepicker_default_browser_thumbnail_size', t('You must fill in the Browser Thumbnail field'));
  }
  if (!is_numeric($form_values['imagepicker_default_browser_thumbnail_size'])) {
    form_set_error('imagepicker_default_browser_thumbnail_size', t('The Browser Thumbnail field must be a number'));
  }
  if (drupal_strlen($form_values['imagepicker_default_scale']) && !is_numeric($form_values['imagepicker_default_scale'])) {
    form_set_error('imagepicker_default_scale', t('The Scale field must be a number'));
  }
  if (!drupal_strlen($form_values['imagepicker_default_fleft'])) {
    form_set_error('imagepicker_default_fleft', t('You must fill in the Left alignment field'));
  }
  if (!drupal_strlen($form_values['imagepicker_default_fright'])) {
    form_set_error('imagepicker_default_fright', t('You must fill in the Right alignment field'));
  }
  if (!is_numeric($form_values['imagepicker_advanced_iframe_height'])) {
    form_set_error('imagepicker_advanced_iframe_height', t('The Iframe height must be a number'));
  }
  if (!drupal_strlen($form_values['imagepicker_advanced_iframe_width'])) {
    form_set_error('imagepicker_advanced_iframe_width', t('You must fill in the Iframe width'));
  }
  if (!drupal_strlen($form_values['imagepicker_advanced_iframe_border'])) {
    form_set_error('imagepicker_advanced_iframe_border', t('You must fill in the Iframe border'));
  }
  if (!drupal_strlen($form_values['imagepicker_insert_defaults_align'])) {
    form_set_error('imagepicker_insert_defaults_align', t('You must fill in the default align field'));
  }
  if (!drupal_strlen($form_values['imagepicker_insert_defaults_show'])) {
    form_set_error('imagepicker_insert_defaults_show', t('You must fill in the default show field'));
  }
  if (!drupal_strlen($form_values['imagepicker_insert_defaults_link'])) {
    form_set_error('imagepicker_insert_defaults_link', t('You must fill in the default link field'));
  }
  if ($form_values['imagepicker_show_browse_order_form'] != 1) {
    user_save($user, array(
      'imagepicker_browser_order' => NULL,
    ));
  }
}

/**
 * Administration functions
 */
function imagepicker_admin_users_list($dir) {
  $how_many = variable_get('imagepicker_rows_per_page', 25);
  $header = array(
    array(
      'data' => t('Name'),
      'field' => 'u.name',
    ),
    t('Images'),
    t('Groups'),
    variable_get('imagepicker_quota_enable', 1) ? t('Quotas') : '',
  );
  $rowcount = 0;
  $result = db_query("\n    SELECT DISTINCT u.uid, u.name\n    FROM {imagepicker} i, {users} u\n    WHERE i.uid=u.uid AND status=1" . tablesort_sql($header));
  while ($row = db_fetch_array($result)) {
    $user = user_load(array(
      'uid' => $row['uid'],
    ));
    $allcount = _imagepicker_user_has_img($user);
    $groupcount = _imagepicker_user_has_groups('all', $user);
    $quota = isset($user->imagepicker_quota) ? $user->imagepicker_quota : variable_get('imagepicker_quota_default', 0);
    $row_data = array(
      check_plain($row['name']),
      $allcount ? $allcount . ' ' . l(t('images'), 'admin/settings/imagepicker/images/user/' . $row['uid'] . '/browse') : t('No images'),
      $groupcount ? $groupcount . ' ' . l(t('groups'), 'admin/settings/imagepicker/groups/user/' . $row['uid'] . '/browse') : t('No groups'),
      variable_get('imagepicker_quota_enable', 1) ? l(imagepicker_get_quota_list($quota + 1), 'admin/settings/imagepicker/images/user/' . $row['uid'] . '/quota') : '',
    );
    $rows[] = $row_data;
    $rowcount++;
  }
  if (count($rows)) {
    $content .= '<div>' . theme('table', $header, $rows) . theme('pager', NULL, $how_many) . "</div>";
  }
  else {
    $content = '<div class="messages">' . t('No users found.') . '</div>';
  }
  return $content;
}
function imagepicker_admin_groups_list() {
  $how_many = variable_get('imagepicker_rows_per_page', 25);
  $header = array(
    array(
      'data' => t('Group Name'),
      'field' => 'g.group_name',
    ),
    array(
      'data' => t('User Name'),
      'field' => 'u.name',
    ),
    array(
      'data' => t('Status'),
      'field' => 'g.public',
    ),
    array(
      'data' => t('Images'),
      'field' => 'ct',
    ),
    array(
      'data' => t('Actions'),
      'colspan' => 2,
    ),
  );
  $sql = "SELECT g.gid, g.uid, g.group_name, g.group_description, g.state, g.public, COUNT(i.img_id) AS ct, u.name\n    FROM {imagepicker_user_groups} g LEFT JOIN {imagepicker_group_images} i USING(gid), {users} u\n    WHERE g.uid=u.uid\n    GROUP BY g.gid" . tablesort_sql($header);
  $result = db_query($sql);
  $rowcount = 0;
  while ($row = db_fetch_array($result)) {
    $row_data = array(
      $row['group_name'],
      l($row['name'], 'admin/settings/imagepicker/groups/user/' . $row['uid']),
      $row['public'] ? t('Public') : t('Private'),
      $row['ct'] ? l(format_plural($row['ct'], '1 image', '@count images'), 'admin/settings/imagepicker/groups/user/' . $row['uid'] . '/browse/' . $row['gid']) : t('No images'),
      l(t('Edit'), 'admin/settings/imagepicker/groups/user/' . $row['uid'] . '/edit/' . $row['gid']),
      l(t('Delete'), 'admin/settings/imagepicker/groups/user/' . $row['uid'] . '/delete/' . $row['gid']),
    );
    $rows[] = $row_data;
    $rowcount++;
  }
  if (count($rows)) {
    $content .= '<div>' . theme('table', $header, $rows) . theme('pager', NULL, $how_many) . "</div>";
  }
  else {
    $content = '<div class="messages">' . t('No groups found.') . '</div>';
  }
  return $content;
}
function imagepicker_admin_view($img_id, $account = FALSE) {
  if ($account) {
    $user = $account;
    $userdir = array(
      'name' => $user->name,
      'uid' => $user->uid,
    );
  }
  else {
    global $user;
    $userdir = FALSE;
  }
  $img = _imagepicker_get_img($img_id, FALSE, $user);
  if ($img) {
    $imgpath = imagepicker_get_image_path($img, 'full', $userdir);
    $info = image_get_info(imagepicker_get_path(FALSE, $userdir) . $img['img_name']);
    $content = _imagepicker_view_content($img, $imgpath, $info) . drupal_get_form('imagepicker_admin_image_form', $img['img_id'], $user) . '<br />' . (_imagepicker_has_groups($user) ? drupal_get_form('imagepicker_group_images_form', $img['img_id'], $user) : '');
  }
  else {
    drupal_set_message(t('Image not found.'), 'error');
    $content = '';
  }
  return $content;
}
function imagepicker_admin_image_form($img_id, $account = FALSE) {
  $form['edit'] = array(
    '#type' => 'submit',
    '#value' => t('Edit image'),
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete image'),
  );
  $form['img_id'] = array(
    '#type' => 'hidden',
    '#value' => $img_id,
  );
  if ($account) {
    $form['account'] = array(
      '#type' => 'hidden',
      '#value' => $account->uid,
    );
  }
  return $form;
}
function imagepicker_admin_image_form_submit($form_id, $form_values) {
  if ($form_values['account']) {
    if ($form_values['op'] == t('Delete image')) {
      $user = user_load(array(
        'uid' => $form_values['account'],
      ));
      imagepicker_image_delete($form_values['img_id'], $user, 'admin');
    }
    elseif ($form_values['op'] == t('Edit image')) {
      drupal_goto('admin/settings/imagepicker/images/user/' . arg(5) . '/edit/' . $form_values['img_id'] . '');
    }
  }
}
function imagepicker_admin_groups() {
  drupal_add_css(drupal_get_path('module', 'imagepicker') . '/imagepicker.css');
  if (!arg(4)) {
    variable_del('imagepicker_currentuser');
    variable_set('imagepicker_currentgroup', 0);
    $action = 'users';
  }
  $uid = variable_get('imagepicker_currentuser', '');
  if (arg(4) == 'user' && is_numeric(arg(5))) {
    $uid = arg(5);
  }
  elseif (arg(4) == 'stats' || arg(4) == 'users') {
    $action = arg(4);
  }
  if ((arg(6) == 'browse' || arg(6) == 'edit' || arg(6) == 'delete') && is_numeric(arg(7))) {
    $gidaction = arg(6);
    $gid = arg(7);
    if (arg(6) == 'browse') {
      variable_set('imagepicker_currentgroup', $gid);
      drupal_goto('admin/settings/imagepicker/images/user/' . $uid . '/browse');
    }
  }
  $name = "";
  if ($uid) {
    $user = user_load(array(
      'uid' => $uid,
    ));
    $result = db_query("SELECT u.name FROM {users} u, {imagepicker} i WHERE u.uid = %d AND u.uid=i.uid", $uid);
    $row = db_fetch_array($result);
    $name = $row['name'];
    $menu = array(
      l(t('All groups'), 'admin/settings/imagepicker/groups'),
      l(t('Groups'), 'admin/settings/imagepicker/groups/user/' . $uid),
      l(t('Images'), 'admin/settings/imagepicker/images/user/' . $uid . '/browse'),
      l(t('Stats'), 'admin/settings/imagepicker/groups/user/' . $uid . '/stats'),
    );
  }
  elseif ($action) {
    $menu = array(
      l(t('Groups'), 'admin/settings/imagepicker/groups/users'),
      l(t('Stats'), 'admin/settings/imagepicker/groups/stats'),
    );
  }
  $content .= drupal_get_form('imagepicker_group_search_form');
  $content .= '<p>' . t('Managing groups for ') . ($name ? l($name, "user/{$uid}") : 'All') . '</p>';
  if (is_array($menu)) {
    $content .= theme_item_list($menu, NULL, 'ul', $attributes = array(
      'class' => 'tabs secondary',
    ));
  }
  if ($uid) {

    // show groups for $uid
    if ($gid) {
      if ($gidaction == 'edit') {
        $record = imagepicker_get_user_group($gid);
        $content .= drupal_get_form('imagepicker_groups_form', $record);
      }
      elseif ($gidaction == 'delete') {
        $content .= drupal_get_form('imagepicker_group_delete_form', $gid);
      }
    }
    elseif (arg(6) == 'stats') {
      $content .= "<fieldset><legend>" . t('Statistics for %name', array(
        '%name' => $name,
      )) . "</legend>";
      $content .= imagepicker_group_stats($user);
      $content .= '</fieldset>';
    }
    else {
      $content .= "<fieldset><legend>" . t('Groups for %name', array(
        '%name' => $name,
      )) . "</legend>";
      $content .= imagepicker_groups_list('admin', $user);
      $content .= '</fieldset>';
      $content .= drupal_get_form('imagepicker_groups_form', 0, $user);
    }
  }
  else {
    if ($action == 'stats') {
      $content .= '<fieldset><legend>' . t('Statistics') . '</legend>';
      $content .= imagepicker_group_stats();
      $content .= '</fieldset>';
    }
    else {
      $content .= '<fieldset><legend>' . t('Groups') . '</legend>';
      $content .= imagepicker_admin_groups_list('groups');
      $content .= '</fieldset>';
    }
  }
  return $content;
}
function imagepicker_admin_images() {
  drupal_add_css(drupal_get_path('module', 'imagepicker') . '/imagepicker.css');
  if (!arg(4)) {
    variable_del('imagepicker_currentuser');
    variable_set('imagepicker_currentgroup', 0);
    $action = 'users';
  }
  $uid = variable_get('imagepicker_currentuser', '');
  if (arg(4) == 'user' && is_numeric(arg(5))) {
    $uid = arg(5);
  }
  elseif (arg(4) == 'browse_public' || arg(4) == 'stats' || arg(4) == 'users' || arg(4) == 'list_public') {
    $action = arg(4);
    variable_del('imagepicker_currentuser');
    variable_set('imagepicker_currentgroup', 0);
    $uid = FALSE;
  }
  if (arg(6) == 'browse_public' || arg(6) == 'browse' || arg(6) == 'edit' || arg(6) == 'delete' || arg(6) == 'browseadmin' || arg(6) == 'upload' || arg(6) == 'stats' || arg(6) == 'quota') {
    $imgaction = arg(6);
  }
  if (is_numeric(arg(7))) {
    $img_id = arg(7);
  }
  if (arg(6) == 'upload') {
    if (variable_get('imagepicker_upload_progress_enabled', 1)) {
      $content .= theme('imagepicker_upload_progress_get_script', variable_get('imagepicker_upload_progress_delay', 0));
    }
  }
  $name = "";
  if ($uid) {
    $user = user_load(array(
      'uid' => $uid,
    ));
    $name = $user->name;
    $menu = array(
      l(t('Users'), 'admin/settings/imagepicker/images'),
      l(t('Browse All'), 'admin/settings/imagepicker/images/browse_public'),
      l(t('List All'), 'admin/settings/imagepicker/images/list_public'),
      l(t('Browse'), 'admin/settings/imagepicker/images/user/' . $uid . '/browse'),
      l(t('List'), 'admin/settings/imagepicker/images/user/' . $uid . '/browseadmin'),
      l(t('Upload'), 'admin/settings/imagepicker/images/user/' . $uid . '/upload'),
      l(t('Groups'), 'admin/settings/imagepicker/groups/user/' . $uid),
      l(t('Stats'), 'admin/settings/imagepicker/images/user/' . $uid . '/stats'),
      variable_get('imagepicker_quota_enable', 1) ? l(t('Quota'), 'admin/settings/imagepicker/images/user/' . $uid . '/quota') : '',
    );
  }
  elseif ($action) {
    $menu = array(
      l(t('Users'), 'admin/settings/imagepicker/images/users'),
      l(t('Browse All'), 'admin/settings/imagepicker/images/browse_public'),
      l(t('List All'), 'admin/settings/imagepicker/images/list_public'),
      l(t('Stats'), 'admin/settings/imagepicker/images/stats'),
    );
  }
  $content .= drupal_get_form('imagepicker_user_search_form', 'images');
  $content .= '<p>' . t('Managing images for ') . ($name ? l($name, "user/{$uid}") : 'All') . '</p>';
  if (is_array($menu)) {
    $content .= theme_item_list($menu, NULL, 'ul', $attributes = array(
      'class' => 'tabs secondary',
    ));
  }
  if ($uid) {
    if ($img_id) {
      if ($imgaction == 'edit') {
        $img = _imagepicker_get_img($img_id, FALSE, $user);
        $imgsrc = imagepicker_get_image_path($img, 'browser', array(
          'uid' => $user->uid,
          'name' => $user->name,
        ));
        $content .= "<div class='imgp_help'>" . t('Edit image details') . "</div>";
        $content .= '<div id="imgp_img_holder"><img src="' . $imgsrc . '" alt="' . check_plain($img['img_title']) . '" /></div>';
        $content .= drupal_get_form('imagepicker_edit_form', $img, 'admin', $user);
      }
      elseif ($imgaction == 'delete') {
        imagepicker_image_delete($img_id, $user, 'admin');
      }
      else {
        $content .= imagepicker_admin_view($img_id, $user);
      }
    }
    elseif ($imgaction == 'upload') {
      $content .= '<fieldset><legend>' . t('Upload file for %name', array(
        '%name' => $name,
      )) . '</legend>';
      if (variable_get('imagepicker_upload_progress_enabled', 1)) {
        $content .= theme('imagepicker_upload_progress_get_script', variable_get('imagepicker_upload_progress_delay', 0));
      }
      $content .= imagepicker_quota_ok('admin', $user);
      $content .= '</fieldset>';
    }
    elseif ($imgaction == 'stats') {
      $content .= '<fieldset><legend>' . t('Statistics for %name', array(
        '%name' => $name,
      )) . '</legend>';
      $content .= imagepicker_group_stats($user);
      $content .= '</fieldset>';
    }
    elseif ($imgaction == 'browseadmin') {
      $content .= '<fieldset><legend>' . t('List images for %name', array(
        '%name' => $name,
      )) . '</legend>';
      $content .= _imagepicker_browse_admin('admin', $user);
      $content .= '</fieldset>';
    }
    elseif ($imgaction == 'quota') {
      $usedbytes = imagepicker_get_all_bytes($user) + 1;
      $usedbytesprint = _imagepicker_bkmg($usedbytes);
      $quota = 0;
      if (variable_get('imagepicker_quota_enable', 1)) {
        $quota = $user->imagepicker_quota ? $user->imagepicker_quota : variable_get('imagepicker_quota_default', 1);
      }
      $content .= '<fieldset><legend>' . t('Quota for %name', array(
        '%name' => $name,
      )) . '</legend>';
      $imgtot = _imagepicker_user_has_img($user);
      if ($quota) {
        $pused = round($usedbytes / ($quota * 1000000) * 100, 2);
        $content .= '<p>' . t('User %name has used %pused percent or %usedbytesprint of quota and has %imgtot images.', array(
          '%name' => $name,
          '%pused' => $pused,
          '%usedbytesprint' => $usedbytesprint,
          '%imgtot' => $imgtot,
        )) . '</p>';
      }
      else {
        $content .= '<p>' . t('User %name has used %usedbytesprint and has %imgtot images.', array(
          '%name' => $name,
          '%usedbytesprint' => $usedbytesprint,
          '%imgtot' => $imgtot,
        )) . '</p>';
      }
      $content .= drupal_get_form('imagepicker_quota_form', $user);
      $content .= '</fieldset>';
    }
    else {
      $content .= '<fieldset><legend>' . t('Browse images for %name', array(
        '%name' => $name,
      )) . '</legend>';
      $content .= _imagepicker_browse('admin', $user);
      $content .= '</fieldset>';
    }
  }
  else {
    if ($action == 'stats') {
      $content .= '<fieldset><legend>' . t('Statistics') . '</legend>';
      $content .= imagepicker_group_stats();
      $content .= '</fieldset>';
    }
    elseif ($action == 'browse_public') {
      $content .= '<fieldset><legend>' . t('Images') . '</legend>';
      $content .= _imagepicker_browse_public('admin', variable_get('imagepicker_browse_public', 1));
      $content .= '</fieldset>';
    }
    elseif ($action == 'list_public') {
      $content .= '<fieldset><legend>' . t('List All') . '</legend>';
      $content .= _imagepicker_browse_admin('admin', FALSE, TRUE, variable_get('imagepicker_browse_public', 1));
      $content .= '</fieldset>';
    }
    else {
      $content .= '<fieldset><legend>' . t('Users') . '</legend>';
      $content .= imagepicker_admin_users_list('images');
      $content .= '</fieldset>';
    }
  }
  return $content;
}

/**
 * Helper function to check if a user has a specific role
 */
function imagepicker_user_has_role($role, $user = NULL) {
  if ($user == NULL) {
    global $user;
  }
  if (is_array($user->roles) && in_array($role, array_values($user->roles))) {
    return TRUE;
  }
  return FALSE;
}
function imagepicker_role_has_permission($rid) {
  $result = db_query("SELECT count(rid) AS ct FROM {permission} WHERE rid = %d AND perm LIKE '%%use public imagepicker%%'", array(
    $rid,
  ));
  $row = db_fetch_array($result);
  return $row['ct'];
}

/**
 * Account functions
 */
function imagepicker_user_page($path = '', $func = '', $id = 0) {
  drupal_add_css(drupal_get_path('module', 'imagepicker') . '/imagepicker.css');
  $content .= theme_imagepicker_user_menu($func == 'browse' ? 'browse' : 'browseadmin');
  if ($path == 'images') {
    if (($func == 'browse' || $func == 'browseadmin') && is_numeric($id) && $id > 0) {
      $content .= imagepicker_user_view($id);
    }
    elseif ($func == 'edit' && is_numeric($id) && $id > 0) {
      $content .= imagepicker_user_image_edit($id);
    }
    elseif ($func == 'browse') {
      $content .= imagepicker_user_browse();
    }
    elseif ($func == 'browse_public') {
      if (is_numeric($id) && $id > 0) {
        $content .= imagepicker_user_view_public($id);
      }
      else {
        $content .= imagepicker_user_browse_public();
      }
    }
    elseif ($func == 'browseadmin') {
      $content .= imagepicker_user_browse_admin();
    }
    elseif ($func == 'delete') {
      imagepicker_image_delete($id, FALSE, 'account');
    }
  }
  elseif ($path == 'groups') {
    if ($func == 'edit' && is_numeric($id) && $id > 0) {
      $content .= imagepicker_user_groups($func, $id);
    }
    elseif ($func == 'browse' && is_numeric($id) && $id > 0) {
      imagepicker_set_user_group_state(1, $id);
      $content .= imagepicker_user_browse();
    }
    elseif ($func == 'browse') {
      $content .= imagepicker_user_groups();
    }
  }
  elseif ($path == 'stats') {
    $content .= imagepicker_group_stats(-1);
  }
  else {
    $content .= imagepicker_user_upload();
  }
  return $content;
}
function imagepicker_user_upload() {
  global $user;
  variable_del('imagepicker_advanced_browser_pagestart');
  if (variable_get('imagepicker_upload_progress_enabled', 1)) {
    $content = theme('imagepicker_upload_progress_get_script', variable_get('imagepicker_upload_progress_delay', 0));
  }
  $content .= "<div class='imgp_help'>" . t('Upload images. You can give them a title and description') . "</div>";
  $content .= imagepicker_quota_ok('user', $user);
  return $content;
}

/**
 * main thumbnails page in my imagepicker
 */
function imagepicker_user_browse() {
  $content = _imagepicker_browse("account");
  return $content;
}
function imagepicker_user_browse_public() {
  $content = _imagepicker_browse_public("account");
  return $content;
}
function imagepicker_user_browse_admin() {
  $content = _imagepicker_browse_admin("account");
  return $content;
}
function imagepicker_user_view($img_id) {
  $img = _imagepicker_get_img($img_id);
  if ($img) {
    $imgpath = imagepicker_get_image_path($img, 'full');
    $info = image_get_info(imagepicker_get_path(FALSE, TRUE) . $img['img_name']);
    $content = _imagepicker_view_content($img, $imgpath, $info) . drupal_get_form('imagepicker_user_image_form', $img_id) . '<br />' . (_imagepicker_has_groups() ? drupal_get_form('imagepicker_group_images_form', $img['img_id']) : '');
  }
  else {
    drupal_set_message(t('Image not found.'), 'error');
    $content = '';
  }
  return $content;
}
function imagepicker_user_view_public($img_id) {
  $img = _imagepicker_get_img($img_id, FALSE);
  if ($img) {
    $result = db_query("SELECT name FROM {users} WHERE uid=%d AND status=1", array(
      $img['uid'],
    ));
    $row = db_fetch_array($result);
    $img['name'] = $row['name'] ? $row['name'] : FALSE;
    $imgpath = imagepicker_get_image_path($img, 'full', array(
      'name' => $img['name'],
      'uid' => $img['uid'],
    ));
    $info = image_get_info(imagepicker_get_path(FALSE, array(
      'name' => $img['name'],
      'uid' => $img['uid'],
    )) . $img['img_name']);
    $content = _imagepicker_view_content($img, $imgpath, $info);
  }
  else {
    drupal_set_message(t('Image not found.'), 'error');
    $content = '';
  }
  return $content;
}
function imagepicker_user_image_form($img_id) {
  $form['edit'] = array(
    '#type' => 'submit',
    '#value' => t('Edit image'),
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete image'),
  );
  $form['img_id'] = array(
    '#type' => 'hidden',
    '#value' => $img_id,
  );
  return $form;
}

/**
 * Submit form
 */
function imagepicker_user_image_form_submit($form_id, $form_values) {
  global $user;
  if ($form_values['op'] == t('Delete image')) {

    #imagepicker_image_delete($form_values['img_id'], $user, 'account');
    imagepicker_image_delete($form_values['img_id'], FALSE, 'account');
  }
  elseif ($form_values['op'] == t('Edit image')) {
    drupal_goto('user/' . $user->uid . '/imagepicker/images/edit/' . $form_values['img_id']);
  }
}
function imagepicker_user_image_edit($img_id) {
  $content = _imagepicker_edit_img($img_id, 'account');
  return $content;
}
function _imagepicker_browse($src = "iframe", $account = FALSE) {
  if ($account) {
    $user = $account;
    $userdir = array(
      'uid' => $user->uid,
      'name' => $user->name,
    );
  }
  else {
    global $user;
    $userdir = FALSE;
  }

  // if there are groups
  $gid = 0;
  if (_imagepicker_has_groups($user)) {
    $gids = imagepicker_get_user_group_state(1, $user);
    $gid = $gids[0];
    if ($account) {
      $gid = variable_get('imagepicker_currentgroup', 0);
    }
  }
  $how_many = variable_get('imagepicker_advanced_browser_page', 25);
  $default_order = variable_get('imagepicker_default_browser_order', 'img_id DESC');
  if ($account && $src == 'admin') {
    $order = variable_get('imagepicker_browser_order', $default_order);
  }
  else {
    $order = $user->imagepicker_browser_order ? $user->imagepicker_browser_order : $default_order;
  }

  // filter by selected group
  if ($gid) {
    $sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date\n      FROM {imagepicker} i, {imagepicker_group_images} g\n      WHERE i.uid = %d AND i.img_id = g.img_id AND g.gid = %d\n      ORDER BY i.{$order}";
    $result = pager_query($sql, $how_many, 0, NULL, array(
      $user->uid,
      $gid,
    ));
  }
  else {
    $sql = "SELECT * FROM {imagepicker} WHERE uid=%d ORDER BY {$order}";
    $result = pager_query($sql, $how_many, 0, NULL, array(
      $user->uid,
    ));
  }
  $content_main = _imagepicker_thumbs_display($result, FALSE, $src);
  if (empty($content_main)) {
    return '<div class="messages">' . t('You do not have any uploaded images') . '</div>';
  }
  $content_head = "";
  if (variable_get('imagepicker_show_browse_order_form', 1)) {
    $content_head .= drupal_get_form('imagepicker_browse_order_form', $user, $src == 'admin' ? TRUE : FALSE);
  }
  if (_imagepicker_has_groups($user) && imagepicker_has_grouplist($user)) {

    // add groups select here
    $content_head .= drupal_get_form('imagepicker_browse_groups_form', $account ? $user : FALSE);
  }
  if ($src == "account" || $src == "admin") {
    $content_head .= '<div class="imgp_help">' . t('Hold the mouse over an image to view Name, Title and Description, Click on it to view.') . '</div>';
  }
  else {
    $content_head .= '<div class="imgp_help">' . t('Hold the mouse over an image to view Name, Title and Description, Click on it to use.') . '</div>';
  }
  $content = $content_head . $content_main;
  return $content;
}
function _imagepicker_browse_public($src = "iframe", $range = 1) {
  global $user;
  $how_many = variable_get('imagepicker_advanced_browser_page', 25);
  $default_order = variable_get('imagepicker_default_browser_order', 'img_id DESC');
  $order = $user->imagepicker_browser_order ? $user->imagepicker_browser_order : $default_order;
  if ($range == 1) {

    // Build sql for role restriction matching
    if (variable_get('imagepicker_publicroles_enabled', 1) && $src != 'admin') {
      $x = 1;
      foreach ($user->roles as $role) {
        if ($x == 1) {
          $rolesql = "AND (u.avail_roles = 'all' OR u.avail_roles LIKE '%%" . $role . "%%'";
        }
        else {
          $rolesql .= " OR u.avail_roles LIKE '%%" . $role . "%%'";
        }
        if ($x == count($user->roles)) {
          $rolesql .= ")";
        }
        $x++;
      }
    }
    $sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date, n.name\n      FROM {imagepicker} i, {users} n, {imagepicker_user_groups} u, {imagepicker_group_images} g\n      WHERE i.uid = n.uid\n      AND u.public = 1\n      {$rolesql}\n      AND g.img_id = i.img_id\n      AND u.gid = g.gid\n      ORDER BY i.{$order}";
  }
  elseif ($range == 2) {
    $sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date, n.name\n      FROM {imagepicker} i, {users} n, {imagepicker_user_groups} u, {imagepicker_group_images} g\n      WHERE i.uid = n.uid\n      AND u.public = 0\n      AND g.img_id = i.img_id\n      AND u.gid = g.gid\n      ORDER BY i.{$order}";
  }
  else {
    $sql = "SELECT DISTINCT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date, n.name\n      FROM {imagepicker} i, {users} n\n      WHERE i.uid = n.uid\n      ORDER BY i.{$order}";
  }
  $result = pager_query($sql, $how_many, 0, NULL);
  $content_main = _imagepicker_thumbs_display($result, TRUE, $src);
  if (empty($content_main)) {
    return '<div class="messages">' . t('There are no public images') . '</div>';
  }
  $content_head = "";
  if (variable_get('imagepicker_show_browse_order_form', 1)) {
    $content_head .= drupal_get_form('imagepicker_browse_order_form');
  }
  if ($src == 'admin') {
    $content_head .= drupal_get_form('imagepicker_browse_public_form');
  }
  if ($src == "account" || $src == 'admin') {
    $content_head .= '<div class="imgp_help">' . t('Hold the mouse over an image to view Name, Title and Description, Click on it to view.') . '</div>';
  }
  else {
    $content_head .= '<div class="imgp_help">' . t('Hold the mouse over an image to view Name, Title and Description, Click on it to use.') . '</div>';
  }
  $content = $content_head . $content_main;
  return $content;
}
function _imagepicker_thumbs_display($result, $public = FALSE, $src = 'account') {
  global $user;
  $how_many = variable_get('imagepicker_advanced_browser_page', 25);
  $browsercols = variable_get('imagepicker_advanced_browser_columns', 0);
  $ct = 0;
  $imgct = 0;
  if ($result) {
    $content = '<div class="clear-block">';
    while ($img = db_fetch_array($result)) {
      if ($public || $src == 'admin') {
        $userdir = array(
          'name' => $img['name'],
          'uid' => $img['uid'],
        );
      }
      else {
        $userdir = array(
          'uid' => $user->uid,
          'name' => $user->name,
        );
      }

      // img_id img_name 	img_title 	img_description
      $imgpath = imagepicker_get_image_path($img, 'browser', $userdir);
      if ($imgpath) {
        if ($public) {

          // paths
          if ($src == 'account') {
            $imgurl = 'user/' . $user->uid . '/imagepicker/images/browse_public/';
          }
          elseif ($src == 'admin') {
            $imgurl = 'admin/settings/imagepicker/images/user/' . $img['uid'] . '/browse/';
          }
          else {
            $imgurl = 'imagepicker/browse_public/';
          }
        }
        else {

          // paths
          if ($src == 'account') {
            $imgurl = 'user/' . $user->uid . '/imagepicker/images/browse/';
          }
          elseif ($src == 'admin') {
            $imgurl = 'admin/settings/imagepicker/images/user/' . $img['uid'] . '/browse/';
          }
          else {
            $imgurl = 'imagepicker/browse/';
          }
        }
        $tooltip = $img['img_name'] . ': ' . $img['img_title'] . ' ' . $img['img_description'];
        $imglink = '<img src="' . $imgpath . '" alt="' . $img['img_title'] . '" title="' . $tooltip . '" />';
        $content .= '<div class="imgp_holder">';
        $content .= l($imglink, $imgurl . $img['img_id'], array(), NULL, NULL, FALSE, TRUE);
        $content .= '</div>';
        $ct++;
        if ($browsercols > 0 && $ct >= $browsercols) {
          $content .= '</div><div class="clear-block">';
          $ct = 0;
        }
        $imgct++;
      }
    }
  }
  if (!$imgct) {
    return;
  }
  $content .= '</div>';
  $content .= theme('pager', NULL, $how_many);
  return $content;
}
function _imagepicker_browse_admin($src = "iframe", $account = FALSE, $public = FALSE, $range = 1) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $content = "";

  // paths
  if ($src == 'iframe') {
    $editpath = "imagepicker/edit/";
    $deletepath = "imagepicker/delete/";
    $imgpath = 'imagepicker/browse/';
  }
  elseif ($src == 'admin') {
    $editpath = 'admin/settings/imagepicker/images/user/' . $user->uid . '/edit/';
    $deletepath = 'admin/settings/imagepicker/images/user/' . $user->uid . '/delete/';
    $imgpath = 'admin/settings/imagepicker/images/user/' . $user->uid . '/browse/';
  }
  else {
    $editpath = 'user/' . $user->uid . '/imagepicker/images/edit/';
    $deletepath = 'user/' . $user->uid . '/imagepicker/images/delete/';
    $imgpath = 'user/' . $user->uid . '/imagepicker/images/browse/';
  }

  // if there are groups
  $gid = 0;
  if (!$public) {
    if (_imagepicker_has_groups($src == 'admin' ? $user : FALSE) && imagepicker_has_grouplist($src == 'admin' ? $user : FALSE)) {

      // add groups select here
      $content .= drupal_get_form('imagepicker_browse_groups_form', $src == 'admin' ? $user : FALSE);
      $gids = imagepicker_get_user_group_state(1, $src == 'admin' ? $user : FALSE);
      $gid = $gids[0];
      if ($account) {
        $gid = variable_get('imagepicker_currentgroup', 0);
      }
    }
  }
  $how_many = variable_get('imagepicker_advanced_browser_page', 25);

  // tablesort
  if ($public && $src == 'admin') {
    $header = array(
      array(
        'data' => t('Name'),
        'field' => 'i.img_name',
      ),
      array(
        'data' => t('Title'),
        'field' => 'i.img_title',
      ),
      t('Description'),
      array(
        'data' => t('User'),
        'field' => 'n.name',
      ),
      array(
        'data' => t('Date'),
        'field' => 'i.img_date',
      ),
      array(
        'data' => t('Actions'),
        'colspan' => 2,
      ),
    );
  }
  else {
    $header = array(
      array(
        'data' => t('Name'),
        'field' => 'i.img_name',
      ),
      array(
        'data' => t('Title'),
        'field' => 'i.img_title',
      ),
      t('Description'),
      array(
        'data' => t('Date'),
        'field' => 'i.img_date',
      ),
      array(
        'data' => t('Actions'),
        'colspan' => 2,
      ),
    );
  }
  if ($public && $src == 'admin') {
    if ($range == 1) {
      $sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date, n.name\n        FROM {imagepicker} i, {users} n, {imagepicker_user_groups} u, {imagepicker_group_images} g\n        WHERE i.uid = n.uid\n        AND u.public = 1\n        AND g.img_id = i.img_id\n        AND u.gid = g.gid" . tablesort_sql($header);
    }
    elseif ($range == 2) {
      $sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date, n.name\n        FROM {imagepicker} i, {users} n, {imagepicker_user_groups} u, {imagepicker_group_images} g\n        WHERE i.uid = n.uid\n        AND u.public = 0\n        AND g.img_id = i.img_id\n        AND u.gid = g.gid" . tablesort_sql($header);
    }
    else {
      $sql = "SELECT DISTINCT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date, n.name\n        FROM {imagepicker} i, {users} n\n        WHERE i.uid = n.uid" . tablesort_sql($header);
    }
    $result = pager_query($sql, $how_many, 0, NULL);
  }
  else {

    // filter by selected group
    if ($gid) {
      $sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date\n        FROM {imagepicker} i, {imagepicker_group_images} g\n        WHERE i.uid = %d AND i.img_id = g.img_id AND g.gid = %d" . tablesort_sql($header);
      $result = pager_query($sql, $how_many, 0, NULL, array(
        $user->uid,
        $gid,
      ));
    }
    else {
      $sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date\n        FROM {imagepicker} i\n        WHERE i.uid=%d" . tablesort_sql($header);
      $result = pager_query($sql, $how_many, 0, NULL, array(
        $user->uid,
      ));
    }
  }
  $rows = array();
  while ($row = db_fetch_array($result)) {

    // img_id img_name 	img_title 	img_description
    $img_name = check_plain($row['img_name']);
    $description = check_plain($row['img_description']);
    if (drupal_strlen($description) > 30) {
      $description = drupal_substr($description, 0, 30) . '...';
    }
    if ($public && $src == 'admin') {
      $editpath = 'admin/settings/imagepicker/images/user/' . $row['uid'] . '/edit/';
      $deletepath = 'admin/settings/imagepicker/images/user/' . $row['uid'] . '/delete/';
      $imgpath = 'admin/settings/imagepicker/images/user/' . $row['uid'] . '/browse/';
      $listpath = 'admin/settings/imagepicker/images/user/' . $row['uid'] . '/browseadmin';
      $row_data = array(
        l($img_name, $imgpath . $row['img_id']),
        check_plain($row['img_title']),
        $description,
        l($row['name'], $listpath),
        check_plain($row['img_date']),
        l(t('Edit'), $editpath . $row['img_id']),
        l(t('Delete'), $deletepath . $row['img_id']),
      );
    }
    else {
      $row_data = array(
        l($img_name, $imgpath . $row['img_id']),
        check_plain($row['img_title']),
        $description,
        check_plain($row['img_date']),
        l(t('Edit'), $editpath . $row['img_id']),
        l(t('Delete'), $deletepath . $row['img_id']),
      );
    }
    $rows[] = $row_data;
  }
  if (count($rows)) {
    if ($public && $src == 'admin') {
      $content .= drupal_get_form('imagepicker_browse_public_form');
    }
    $content .= '<div class="imgp_imgs_list">';
    $content .= theme('table', $header, $rows) . theme('pager', NULL, $how_many);
    $content .= '</div>';
  }
  else {
    $content = '<div class="messages">' . t('You do not have any uploaded images') . '</div>';
  }
  return $content;
}
function _imagepicker_get_img($img_id, $checkuser = TRUE, $account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $result = db_query_range("SELECT * FROM {imagepicker} WHERE img_id = '%d'", $img_id, 0, 1);
  $img = db_fetch_array($result);
  if (count($img)) {
    if ($img['uid'] != $user->uid && $checkuser) {
      drupal_set_message(t('This image does not belong to you.'), 'error');
      watchdog('imagepicker', t('User uid !uid1 attempted to edit image belonging to user uid !uid2', array(
        '!uid1' => $user->uid,
        '!uid2' => $img['uid'],
      )), WATCHDOG_WARNING);
      return FALSE;
    }
    return $img;
  }
  return FALSE;
}
function _imagepicker_edit_img($img_id, $src = 'iframe', $account = FALSE) {
  $userdir = FALSE;
  if (is_object($account)) {
    $userdir = array(
      'uid' => $account->uid,
      'name' => $account->name,
    );
  }
  $content = '';
  $img = _imagepicker_get_img($img_id, $src == 'admin' ? FALSE : TRUE, $account);
  if ($img) {
    $imgsrc = imagepicker_get_image_path($img, 'browser', $userdir);
    $content .= "<div class='imgp_help'>" . t('Edit image details') . "</div>";
    $content .= '<div id="imgp_img_holder"><img src="' . $imgsrc . '" alt="' . check_plain($img['img_title']) . '" /></div>';
    $content .= drupal_get_form('imagepicker_edit_form', $img, $src, $account);
  }
  else {
    drupal_set_message(t('Image not found.'), 'error');
    $content = '';
  }
  return $content;
}
function _imagepicker_user_has_img($account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $num = 0;
  $result = db_query("SELECT count(img_id) AS ct FROM {imagepicker} WHERE uid = '%d'", array(
    $user->uid,
  ));
  $row = db_fetch_array($result);
  return $row['ct'];
}

/**
 * groups
 */
function _imagepicker_user_has_groups($public = 'all', $account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  if ($public == 'yes') {
    $subsql = "AND public = 1";
  }
  elseif ($public == 'no') {
    $subsql = "AND public = 0";
  }
  else {
    $subsql = "";
  }
  $result = db_query("SELECT count(gid) AS ct FROM {imagepicker_user_groups}\n    WHERE  uid = %d {$subsql} ", array(
    $user->uid,
  ));
  $row = db_fetch_array($result);
  return $row['ct'];
}
function _imagepicker_user_has_grouped_img($public = 'all', $account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  if ($public == 'yes') {
    $subsql = "AND public = 1";
  }
  elseif ($public == 'no') {
    $subsql = "AND public = 0";
  }
  else {
    $subsql = "";
  }
  $result = db_query("SELECT count(DISTINCT i.img_id) AS ct FROM {imagepicker_group_images} i, {imagepicker_user_groups} g\n    WHERE g.uid = %d AND g.gid = i.gid {$subsql} ", array(
    $user->uid,
  ));
  $row = db_fetch_array($result);
  return $row['ct'];
}
function _imagepicker_has_groups($account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $result = db_query("SELECT COUNT(gid) AS ct FROM {imagepicker_user_groups} WHERE uid = %d", array(
    $user->uid,
  ));
  $row = db_fetch_array($result);
  return $row['ct'];
}
function imagepicker_groups() {

  // from iframe
  if (!arg(2)) {
    $content = imagepicker_groups_list();
    $content .= drupal_get_form('imagepicker_groups_form');
  }
  elseif (arg(2) == 'edit') {
    $record = imagepicker_get_user_group(arg(3));
    $content .= drupal_get_form('imagepicker_groups_form', $record);
  }
  elseif (arg(2) == 'delete') {
    $content .= drupal_get_form('imagepicker_group_delete_form', arg(3));
  }
  elseif (is_numeric(arg(2)) && arg(2) > 0) {
    imagepicker_set_user_group_state(1, arg(2));
    drupal_goto('imagepicker/browse');
  }
  theme('imagepicker', $content);
}

/**
 * groups form
 */
function imagepicker_groups_form($record = 0, $account = FALSE) {
  $form['groupsave'] = array(
    '#type' => 'fieldset',
    '#title' => $record->gid ? t('Edit group') : t('Add group'),
    '#description' => t('Give your group a brief name and optionally put any additional information in the group description box'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['groupsave']['group_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Group name'),
    '#size' => 20,
    '#default_value' => $record->group_name ? $record->group_name : '',
    '#description' => t(''),
    '#required' => TRUE,
  );
  $form['groupsave']['group_description'] = array(
    '#type' => 'textfield',
    '#title' => t('group description'),
    '#size' => 50,
    '#maxlength' => 50,
    '#default_value' => $record->group_description ? $record->group_description : '',
    '#description' => t('Maximum 50 characters'),
    '#required' => FALSE,
  );
  if (user_access('use public imagepicker') && user_access('create public groups') && variable_get('imagepicker_public_enabled', 1)) {
    $form['groupsave']['group_public'] = array(
      '#type' => 'checkbox',
      '#title' => t('Public'),
      '#return_value' => 1,
      '#default_value' => $record->public ? $record->public : '',
      '#description' => t('Make this group publicly available'),
    );

    // Create role restrictions
    if (variable_get('imagepicker_publicroles_enabled', 1)) {
      $form['groupsave']['group_public_roles'] = array(
        '#type' => 'fieldset',
        '#title' => $record->gid ? t('Edit group roles') : t('Add public group roles'),
        '#description' => t('Add roles for public access to your group. (Don\'t check anything to make it available to all groups.)'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );

      // Output a list of available roles as checkboxes
      $form['groupsave']['group_public_roles']['roles']['#tree'] = TRUE;
      if ($record->avail_roles) {
        if ($record->avail_roles != 'all') {
          $availroles = explode(':', $record->avail_roles);
        }
      }
      foreach (user_roles(TRUE) as $rid => $role) {
        if (imagepicker_role_has_permission($rid)) {
          if (is_array($availroles) && in_array($role, array_values($availroles))) {
            $checked = TRUE;
          }
          else {
            $checked = FALSE;
          }
          $form['groupsave']['group_public_roles']['roles'][$rid] = array(
            '#type' => 'checkbox',
            '#title' => check_plain($role),
            '#return_value' => $role,
            '#default_value' => $checked ? 1 : '',
          );
        }
      }
    }
  }
  if ($account) {
    $form['groupsave']['account'] = array(
      '#type' => 'hidden',
      '#value' => $account->uid,
    );
  }
  if ($record->gid) {
    $form['groupsave']['gid'] = array(
      '#type' => 'hidden',
      '#value' => $record->gid,
    );
  }
  $form['groupsave']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save group'),
  );
  return $form;
}

/**
 * Submit groups form
 */
function imagepicker_groups_form_submit($form_id, $form_values) {
  global $user;
  $record['group_name'] = $form_values['group_name'];
  $record['group_description'] = $form_values['group_description'];
  $record['public'] = $form_values['group_public'];

  // Check role restrictions submitted
  if ($form_values['roles']) {
    $found = 0;
    foreach ($form_values['roles'] as $rid => $role) {
      if (!$role) {
        unset($form_values['roles'][$rid]);
      }
      else {
        $found++;
      }
    }
    if ($found) {
      $record['avail_roles'] = implode(':', $form_values['roles']);
    }
    else {
      $record['avail_roles'] = 'all';
    }
  }
  if ($form_values['account']) {
    $record['uid'] = $form_values['account'];
  }
  else {
    $record['uid'] = $user->uid;
  }
  if ($form_values['gid']) {
    $record['gid'] = $form_values['gid'];
    imagepicker_update_user_group($record);
  }
  else {
    imagepicker_insert_user_group($record);
  }
}

/**
 * imagepicker_user_groups functions
 */
function imagepicker_insert_user_group($record) {
  if (db_query("INSERT INTO {imagepicker_user_groups} (uid, group_name, group_description, public, avail_roles) VALUES (%d, '%s', '%s', %d, '%s')", array(
    $record['uid'],
    $record['group_name'],
    $record['group_description'],
    $record['public'],
    $record['avail_roles'],
  ))) {
    drupal_set_message(t('Group was successfully inserted'));
  }
  else {
    drupal_set_message(t('Error while trying to insert your group.'));
  }
}
function imagepicker_update_user_group($record) {
  if (db_query("UPDATE {imagepicker_user_groups} SET group_name='%s', group_description='%s', public=%d, avail_roles='%s' WHERE gid = %d", array(
    $record['group_name'],
    $record['group_description'],
    $record['public'],
    $record['avail_roles'],
    $record['gid'],
  ))) {
    drupal_set_message(t('Group was successfully updated'));
  }
  else {
    drupal_set_message(t('Error while trying to update your group.'));
  }
}
function imagepicker_delete_user_group($gid) {
  if (db_query("DELETE FROM {imagepicker_user_groups} WHERE gid = %d", array(
    $gid,
  )) && db_query("DELETE FROM {imagepicker_group_images} WHERE gid=%d", array(
    $gid,
  ))) {
    drupal_set_message(t('Group was successfully deleted'));
  }
  else {
    drupal_set_message(t('Error while trying to delete group.'));
  }
}
function imagepicker_get_user_group($gid, $obj = TRUE) {
  $result = db_query("SELECT * FROM {imagepicker_user_groups} WHERE gid = %d", array(
    $gid,
  ));
  if ($obj) {
    return db_fetch_object($result);
  }
  return db_fetch_array($result);
}
function imagepicker_get_user_groups_by_user($uid, $obj = TRUE) {
  $result = db_query("SELECT * FROM {imagepicker_user_groups} WHERE uid = %d", array(
    $uid,
  ));
  if ($obj) {
    return db_fetch_object($result);
  }
  return db_fetch_array($result);
}
function imagepicker_insert_group_image($record) {
  if ($record['gid'] && $record['img_id']) {
    if (!db_query("INSERT INTO {imagepicker_group_images} (gid, img_id) VALUES (%d, %d)", array(
      $record['gid'],
      $record['img_id'],
    ))) {
      drupal_set_message(t('Error while trying to insert your group.'), 'error');
    }
  }
}
function imagepicker_delete_group_image($img_id) {
  if (!db_query("DELETE FROM {imagepicker_group_images} WHERE img_id=%d", array(
    $img_id,
  ))) {
    drupal_set_message(t('Error while trying to delete your group.'), 'error');
  }
}
function imagepicker_groups_list($src = FALSE, $account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  if ($src) {
    if ($src == 'admin') {
      $editpath = 'admin/settings/imagepicker/groups/user/' . $user->uid . '/edit/';
      $deletepath = 'admin/settings/imagepicker/groups/user/' . $user->uid . '/delete/';
      $browsepath = 'admin/settings/imagepicker/groups/user/' . $user->uid . '/browse/';
    }
    else {
      $editpath = 'user/' . $user->uid . '/imagepicker/groups/edit/';
      $deletepath = 'user/' . $user->uid . '/imagepicker/groups/delete/';
      $browsepath = 'user/' . $user->uid . '/imagepicker/groups/browse/';
    }
  }
  else {
    $editpath = 'imagepicker/groups/edit/';
    $deletepath = 'imagepicker/groups/delete/';
    $browsepath = 'imagepicker/groups/';
  }
  $content = "";
  $how_many = variable_get('imagepicker_rows_per_page', 25);
  if (user_access('use public imagepicker') && variable_get('imagepicker_public_enabled', 1)) {
    $header = array(
      array(
        'data' => t('Group name'),
        'field' => 'g.group_name',
      ),
      array(
        'data' => t('Description'),
        'field' => 'g.group_description',
      ),
      array(
        'data' => t('No. images'),
        'field' => 'ct',
      ),
      t('State'),
      array(
        'data' => t('Public'),
        'field' => 'g.public',
      ),
      array(
        'data' => t('Actions'),
        'colspan' => 2,
      ),
    );
  }
  else {
    $header = array(
      array(
        'data' => t('Group name'),
        'field' => 'g.group_name',
      ),
      array(
        'data' => t('Description'),
        'field' => 'g.group_description',
      ),
      array(
        'data' => t('No. images'),
        'field' => 'ct',
      ),
      t('State'),
      array(
        'data' => t('Actions'),
        'colspan' => 2,
      ),
    );
  }
  $sql = "SELECT g.gid, g.group_name, g.group_description, g.public, COUNT(i.img_id) AS ct\n    FROM {imagepicker_user_groups} g LEFT JOIN {imagepicker_group_images} i USING(gid)\n    WHERE  uid=%d\n    GROUP BY g.gid" . tablesort_sql($header);
  $result = pager_query($sql, $how_many, 0, NULL, array(
    $user->uid,
  ));
  $rows = array();
  $totcount = 0;
  $rowcount = 0;
  $enabledlist = imagepicker_get_enabled_group($account);
  while ($row = db_fetch_array($result)) {
    if (user_access('use public imagepicker') && variable_get('imagepicker_public_enabled', 1)) {
      $row_data = array(
        check_plain($row['group_name']),
        check_plain($row['group_description']),
        $row['ct'] && $browsepath ? l($row['ct'], $browsepath . $row['gid']) : $row['ct'],
        $enabledlist && in_array($row['gid'], $enabledlist) ? t('selected') : '',
        $row['public'] ? t('Yes') : t('No'),
        l(t('Edit'), $editpath . $row['gid']),
        l(t('Delete'), $deletepath . $row['gid']),
      );
    }
    else {
      $row_data = array(
        check_plain($row['group_name']),
        check_plain($row['group_description']),
        $row['ct'] && $browsepath ? l($row['ct'], $browsepath . $row['gid']) : $row['ct'],
        $enabledlist && in_array($row['gid'], $enabledlist) ? t('selected') : '',
        l(t('Edit'), $editpath . $row['gid']),
        l(t('Delete'), $deletepath . $row['gid']),
      );
    }
    $rows[] = $row_data;
    $rowcount++;
  }
  if (count($rows)) {
    $content .= '<div class="imgp_groups_list">' . theme('table', $header, $rows) . theme('pager', NULL, $how_many) . "</div>";
    return $content;
  }
  else {
    return '<div class="messages">' . t('No groups found.') . '</div>';
  }
}
function imagepicker_user_groups($mode = "", $gid = 0) {
  if (!$mode) {
    $content = imagepicker_groups_list('account');
    $content .= drupal_get_form('imagepicker_groups_form');
  }
  elseif ($mode == 'edit') {
    $record = imagepicker_get_user_group($gid);
    $content .= drupal_get_form('imagepicker_groups_form', $record);
  }
  elseif ($mode == 'delete') {
    $content .= drupal_get_form('imagepicker_group_delete_form', $gid);
  }
  return $content;
}
function imagepicker_get_group_images_count($gid) {
  $result = db_query("SELECT count(gid) as gidct FROM {imagepicker_group_images} WHERE gid = %d", array(
    $gid,
  ));
  $row = db_fetch_array($result);
  return $row['gidct'];
}
function imagepicker_group_edit($gid) {
  $record = imagepicker_get_user_group($gid);
  $content .= drupal_get_form('imagepicker_groups_form', $record);
  return $content;
}

/**
 * group delete form
 */
function imagepicker_group_delete_form($gid) {
  $record = imagepicker_get_user_group($gid);
  if ($record) {
    $form['groupname'] = array(
      '#value' => '<p>' . $record->group_name . '</p>',
    );
    $form['mode'] = array(
      '#type' => 'hidden',
      '#value' => 'reallydelete',
    );
    $form['gid'] = array(
      '#type' => 'hidden',
      '#value' => $gid,
    );
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Really Delete record'),
    );
    return $form;
  }
}

/**
 * Submit group delete form
 */
function imagepicker_group_delete_form_submit($form_id, $form_values) {
  imagepicker_delete_user_group($form_values['gid']);
}

/**
 * insert a form into the edit image page to allow the image to be associated with a group
 */
function imagepicker_group_images_form($img_id, $account = FALSE) {
  $grouplist = imagepicker_get_groups($account);
  $enabledlist = imagepicker_get_image_groups($img_id);
  $form['group_images'] = array(
    '#type' => 'fieldset',
    '#title' => t('Groups'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['group_images']['grouplist'] = array(
    '#type' => 'checkboxes',
    '#default_value' => $enabledlist,
    '#options' => $grouplist,
    '#title' => t('Your Groups'),
  );
  $form['group_images']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save group settings'),
  );
  $form['img_id'] = array(
    '#type' => 'hidden',
    '#value' => $img_id,
  );
  return $form;
}

/**
 * Submit group images form
 */
function imagepicker_group_images_form_submit($form_id, $form_values) {

  // have to delete all the entries for this image and rebuild with the new ones;
  $img_id = $form_values['img_id'];
  imagepicker_delete_group_image($img_id);
  $grouplist = $form_values['grouplist'];
  $inserted = FALSE;
  foreach ($grouplist as $gid) {
    if ($gid > 0) {
      $record = array(
        'gid' => $gid,
        'img_id' => $img_id,
      );
      imagepicker_insert_group_image($record);
      $inserted = TRUE;
    }
  }
  if (!$inserted) {
    $gid = imagepicker_get_user_group_state();
    if ($gid) {
      $ids = imagepicker_get_images_by_group($gid);
      if (!$ids) {
        global $user;
        db_query("UPDATE {imagepicker_user_groups} SET state=0 WHERE uid=%d AND state=1", array(
          $user->uid,
        ));
      }
    }
  }
}
function imagepicker_get_groups($account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $result = db_query("SELECT * FROM {imagepicker_user_groups} WHERE uid = %d", array(
    $user->uid,
  ));
  $count = 0;
  while ($row = db_fetch_array($result)) {
    $data[$row['gid']] = $row['group_name'] . ($row['public'] ? ' - ' . t('Public') : ' - ' . t('Private'));
    $count++;
  }
  if ($count) {
    return $data;
  }
  return FALSE;
}
function imagepicker_get_image_groups($img_id) {
  $result = db_query("SELECT gid FROM {imagepicker_group_images} WHERE img_id = %d", array(
    $img_id,
  ));
  while ($row = db_fetch_array($result)) {
    $data[] = $row['gid'];
  }
  return $data;
}
function imagepicker_get_images_by_group($gid) {
  $result = db_query("SELECT img_id FROM {imagepicker_group_images} WHERE gid = %d", array(
    $gid,
  ));
  $ct = 0;
  while ($row = db_fetch_array($result)) {
    $data[] = $row['img_id'];
    $ct++;
  }
  if ($ct) {
    return $data;
  }
  return FALSE;
}
function imagepicker_get_user_group_state($state = 1, $account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $result = db_query("SELECT gid FROM {imagepicker_user_groups} WHERE state=%d AND uid=%d", array(
    $state,
    $user->uid,
  ));
  while ($row = db_fetch_array($result)) {
    $data[] = $row['gid'];
  }
  return $data;
}
function imagepicker_set_user_group_state($state, $gid, $account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  db_query("UPDATE {imagepicker_user_groups} SET state=0 WHERE uid=%d AND state=1", array(
    $user->uid,
  ));
  db_query("UPDATE {imagepicker_user_groups} SET state=%d WHERE gid=%d", array(
    $state,
    $gid,
  ));
}
function imagepicker_get_grouped_images($account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $groupedlist = array();

  // now the enabled ones
  $result = db_query("\n  SELECT i.img_id\n  FROM {imagepicker_user_groups} g, {imagepicker_group_images} i\n  WHERE g.uid=%d AND g.gid = i.gid AND g.state=1", array(
    $user->uid,
  ));
  while ($row = db_fetch_array($result)) {
    $groupedlist[] = $row['img_id'];
  }
  return $groupedlist;
}
function imagepicker_get_grouplist($account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $grouplist = array(
    '0' => 'All',
  );
  $result = db_query("\n  SELECT DISTINCT g.gid, g.group_name, g.public\n  FROM {imagepicker_user_groups} g, {imagepicker_group_images} i\n  WHERE g.uid=%d AND g.gid = i.gid", array(
    $user->uid,
  ));
  while ($row = db_fetch_array($result)) {
    $grouplist[$row['gid']] = $row['group_name'] . (user_access('use public imagepicker') && variable_get('imagepicker_public_enabled', 1) ? $row['public'] ? ' - ' . t('Public') : ' - ' . t('Private') : '');
  }
  return $grouplist;
}
function imagepicker_has_grouplist($account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $result = db_query("\n  SELECT DISTINCT g.gid, g.group_name\n  FROM {imagepicker_user_groups} g, {imagepicker_group_images} i\n  WHERE g.uid=%d AND g.gid = i.gid", array(
    $user->uid,
  ));
  $ct = 0;
  while ($row = db_fetch_array($result)) {
    $ct++;
  }
  return $ct;
}
function imagepicker_get_enabled_group($account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $result = db_query("\n  SELECT DISTINCT g.gid, g.group_name\n  FROM {imagepicker_user_groups} g, {imagepicker_group_images} i\n  WHERE g.uid=%d AND g.gid = i.gid AND g.state=1", array(
    $user->uid,
  ));
  while ($row = db_fetch_array($result)) {
    $enabledlist[] = $row['gid'];
  }
  return $enabledlist;
}

/**
 * browse groups form
 */
function imagepicker_browse_groups_form($account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }

  // all the groups for the current user which have images attached
  $grouplist = imagepicker_get_grouplist($user);
  if ($account) {
    $enabledlist = variable_get('imagepicker_currentgroup', 0);
  }
  else {
    $enabledlist = imagepicker_get_enabled_group($user);
  }
  $form['gid'] = array(
    '#type' => 'select',
    '#default_value' => $enabledlist,
    '#options' => $grouplist,
    '#title' => t('Group'),
    '#prefix' => '<div id="imgp_groups_form" class="container-inline">',
  );
  if ($account) {
    $form['uid'] = array(
      '#type' => 'hidden',
      '#value' => $user->uid,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
    '#suffix' => '</div>',
  );
  return $form;
}

/**
 * Submit browse groups form
 */
function imagepicker_browse_groups_form_submit($form_id, $form_values) {

  // need to get the users gids
  $user = FALSE;
  if ($form_values['uid']) {
    $user = user_load(array(
      'uid' => $form_values['uid'],
    ));
    if ($form_values['gid']) {
      variable_set('imagepicker_currentgroup', $form_values['gid']);
    }
    else {
      variable_set('imagepicker_currentgroup', 0);
    }
  }
  else {
    $gids = imagepicker_get_groups($user);
    $gids = array_keys($gids);
    foreach ($gids as $gid) {
      $state = 0;
      if ($gid == $form_values['gid']) {
        $state = 1;
      }
      db_query("UPDATE {imagepicker_user_groups} SET state=%d WHERE gid=%d", array(
        $state,
        $gid,
      ));
    }
  }
}

/**
 * browse order form
 */
function imagepicker_browse_order_form($account = FALSE, $admin = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $default_order = variable_get('imagepicker_default_browser_order', 'img_id DESC');
  if ($admin) {
    $order = variable_get('imagepicker_browser_order', $default_order);
    $form['admin'] = array(
      '#type' => 'hidden',
      '#value' => TRUE,
    );
  }
  else {
    $order = $user->imagepicker_browser_order ? $user->imagepicker_browser_order : $default_order;
  }
  $orderlist = array(
    'img_id DESC' => t('Newest first'),
    'img_id ASC' => t('Newest last'),
    'img_date DESC' => t('Edited first'),
    'img_date ASC' => t('Edited last'),
    'img_name' => t('By name'),
  );
  $form['imagepicker_browser_order'] = array(
    '#type' => 'select',
    '#default_value' => $order,
    '#options' => $orderlist,
    '#title' => t('Order'),
    '#prefix' => '<div id="imgp_order_form" class="container-inline">',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
    '#suffix' => '</div>',
  );
  return $form;
}

/**
 * Submit browse order form
 */
function imagepicker_browse_order_form_submit($form_id, $form_values) {
  global $user;
  if ($form_values['admin']) {
    variable_set('imagepicker_browser_order', $form_values['imagepicker_browser_order']);
  }
  else {
    user_save($user, array(
      'imagepicker_browser_order' => $form_values['imagepicker_browser_order'],
    ));
  }
}
function imagepicker_browse_public_form() {
  $list = array(
    0 => t('All'),
    1 => t('Public'),
    2 => t('Private'),
  );
  $form['imagepicker_browse_public'] = array(
    '#type' => 'select',
    '#default_value' => variable_get('imagepicker_browse_public', 1),
    '#options' => $list,
    '#title' => t('Show'),
    '#prefix' => '<div id="imgp_browse_form" class="container-inline">',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
    '#suffix' => '</div>',
  );
  return $form;
}
function imagepicker_browse_public_form_submit($form_id, $form_values) {
  variable_set('imagepicker_browse_public', $form_values['imagepicker_browse_public']);
}

/**
 * files and conversion
 */
function imagepicker_convert_form($form_values = NULL) {
  $dir = imagepicker_get_files_directory();
  imagepicker_fstype();
  $fstype = variable_get('imagepicker_fstype', '');
  if (!isset($form_values)) {
    $step = 1;
    variable_del('imagepicker_files_validate_failed');
    variable_del('imagepicker_convert_to_numeric_failed');
    variable_del('imagepicker_convert_to_alpha_failed');
  }
  else {
    $validate_failed = variable_get('imagepicker_files_validate_failed', FALSE);
    $numeric_failed = variable_get('imagepicker_convert_to_numeric_failed', FALSE);
    $alpha_failed = variable_get('imagepicker_convert_to_alpha_failed', FALSE);
    if ($validate_failed || $numeric_failed || $alpha_failed) {
      $step = 1;
    }
    else {
      $step = $form_values['step'] + 1;
      if ($step > 2) {
        $step = 1;
      }
    }
  }
  $form['imagepicker_files'] = array(
    '#type' => 'fieldset',
    '#title' => t('Imagepicker files validation'),
    '#description' => t('The current filesystem type is %fstype and the current path is %dir', array(
      '%fstype' => $fstype,
      '%dir' => $dir,
    )),
    '#collapsible' => TRUE,
    '#collapsed' => $fstype == 'numeric' ? TRUE : FALSE,
    '#weight' => $fstype == 'numeric' ? 10 : -10,
  );
  $form['imagepicker_files']['step'] = array(
    '#type' => 'hidden',
    '#value' => $step,
  );
  switch ($step) {
    case 1:
      $form['imagepicker_files']['imagepicker_files_validate_label'] = array(
        '#prefix' => '<p>',
        '#value' => t('Validate'),
        '#suffix' => '</p>',
      );
      $form['imagepicker_files']['imagepicker_files_validate_label1'] = array(
        '#prefix' => '<div class="description">',
        '#value' => t('check the files directory for anomalies'),
        '#suffix' => '</div>',
      );
      $form['imagepicker_files']['imagepicker_files_validate'] = array(
        '#type' => 'hidden',
        '#value' => 1,
      );
      break;
    case 2:
      $form['imagepicker_files']['imagepicker_files_validate_label'] = array(
        '#prefix' => '<p>',
        '#value' => t('Validation done'),
        '#suffix' => '</p>',
      );
      $form['imagepicker_files']['imagepicker_files_validate'] = array(
        '#type' => 'hidden',
        '#value' => 1,
      );
      if ($fstype == 'alpha') {
        $form['imagepicker_files']['imagepicker_files_convert_to_numeric_label'] = array(
          '#prefix' => '<p>',
          '#value' => t('Next: Convert to numeric. Recommended'),
          '#suffix' => '</p>',
        );
        $form['imagepicker_files']['imagepicker_files_convert_to_numeric'] = array(
          '#type' => 'hidden',
          '#value' => 1,
        );
      }
      else {
        $form['imagepicker_files']['imagepicker_files_convert_to_alpha'] = array(
          '#type' => 'checkbox',
          '#title' => t('Convert to alpha'),
          '#description' => t('NOT recommended'),
          '#return_value' => 1,
          '#default_value' => 0,
        );
      }
      break;
  }
  $form['#multistep'] = TRUE;
  $form['#redirect'] = FALSE;
  $form['imagepicker_files']['imagepicker_submit_label'] = array(
    '#prefix' => '<p>',
    '#value' => '&nbsp;',
    '#suffix' => '</p>',
  );
  $form['imagepicker_files']['imagepicker_files_submit'] = array(
    '#type' => 'submit',
    '#value' => $step == 1 ? t('Validate') : t('Convert'),
  );
  return $form;
}
function imagepicker_convert_form_submit($form_id, $form_values) {
  $dir = imagepicker_get_files_directory();
  if ($form_values['step'] == 1 && $form_values['imagepicker_files_validate'] == 1) {

    // run validator
    $result = imagepicker_files_validate($dir);
    if (!$result) {

      // failed
      variable_set('imagepicker_files_validate_failed', TRUE);
    }
  }
  elseif ($form_values['step'] == 2 && $form_values['imagepicker_files_convert_to_numeric'] == 1) {

    // convert to numeric
    $result = imagepicker_convert_to_numeric($dir);
    if (!$result) {

      // failed
      variable_set('imagepicker_convert_to_numeric_failed', TRUE);
    }
  }
  elseif ($form_values['step'] == 2 && $form_values['imagepicker_files_convert_to_alpha'] == 1) {

    // convert to alpha
    $result = imagepicker_convert_to_alpha($dir);
    if (!$result) {

      // failed
      variable_set('imagepicker_convert_to_alpha_failed', TRUE);
    }
  }
}
function imagepicker_convert_to_numeric($dir) {
  $result = TRUE;
  $errlist = array();

  // work over the oldstyle dirs
  $dirlist = range('a', 'z');
  array_push($dirlist, 'others');
  foreach ($dirlist as $alphadir) {
    $newdir = $dir . DIRECTORY_SEPARATOR . $alphadir;
    if (is_dir($newdir)) {

      // old style dir exists, now look to see if it has any users
      $newdirlist = file_scan_directory($newdir, ".*", array(
        '.',
        '..',
        'CVS',
      ), 0, FALSE);
      if (count($newdirlist)) {
        foreach ($newdirlist as $k => $v) {
          if ($newdirlist[$k]->basename) {
            $username = $newdirlist[$k]->basename;

            // get the user uid
            $uid = FALSE;
            $result = db_query("SELECT uid FROM {users} WHERE name='%s' AND status=1", array(
              $username,
            ));
            $row = db_fetch_array($result);
            if ($row['uid']) {
              $uid = $row['uid'];
            }
            if ($uid) {
              $newpath = $dir . DIRECTORY_SEPARATOR . $uid;
              $thumbstarget = $newpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR;
              $browsertarget = $newpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR;

              // create dirs
              if (!file_check_directory($newpath, TRUE)) {
                $errlist[$newpath] = "Could not create dir";
              }
              if (!file_check_directory($thumbstarget, TRUE)) {
                $errlist[$thumbstarget] = "Could not create thumbs dir";
              }
              if (!file_check_directory($browsertarget, TRUE)) {
                $errlist[$browsertarget] = "Could not create browser dir";
              }
              if (!count($errlist)) {

                // clear
                drupal_get_messages();
                $oldpath = $newdirlist[$k]->filename;

                // do here: move oldpath/* to newpath and delete oldpath
                // needs to be done per-file and dir
                $oldfilelist = file_scan_directory($oldpath, ".*", array(
                  '.',
                  '..',
                  'CVS',
                ), 0, FALSE);
                if (count($oldfilelist)) {
                  foreach ($oldfilelist as $k1 => $v1) {
                    if (!is_dir($oldfilelist[$k1]->filename) && file_exists($oldfilelist[$k1]->filename)) {
                      $file = $oldfilelist[$k1]->filename;
                      $thumbsfile = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR . $oldfilelist[$k1]->basename;
                      $browserfile = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR . $oldfilelist[$k1]->basename;

                      # file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME)  FILE_EXISTS_ERROR
                      if (!file_move($file, $newpath, FILE_EXISTS_ERROR)) {
                        $errlist[$file] = "Could not move to {$newpath}";
                      }
                      if (!file_move($thumbsfile, $thumbstarget, FILE_EXISTS_ERROR)) {
                        $errlist[$thumbsfile] = "Could not move to {$thumbstarget}";
                      }
                      if (!file_move($browserfile, $browsertarget, FILE_EXISTS_ERROR)) {
                        $errlist[$browserfile] = "Could not move to {$browsertarget}";
                      }
                    }
                  }
                }
                if (!count($errlist)) {
                  $delpath[] = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR;
                  $delpath[] = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR;
                  $delpath[] = $oldpath;
                  $delpath[] = $newdir;
                }
              }
            }
            else {
              $errlist[$k] = "NO uid for user {$username}";
            }
          }
        }
      }
      else {

        // newdir exists but is empty so delete it
        $delpath[] = $newdir;
      }
    }
  }
  if (!count($errlist)) {

    // tidy up
    if (count($delpath)) {
      foreach ($delpath as $p) {
        if (is_dir($p)) {
          if (!rmdir($p)) {
            $errlist[$p] = 'deletion failed';
          }
        }
        else {
          $errlist[$p] = 'not a dir';
        }
      }
    }
  }
  if (count($errlist)) {
    foreach ($errlist as $key => $val) {
      drupal_set_message("ERRORS: {$key}: {$val} ", 'error');
    }
    $result = FALSE;
  }
  return $result;
}
function imagepicker_convert_to_alpha($dir) {
  $result = TRUE;
  $errlist = array();
  $uiddirlist = file_scan_directory($dir, ".*", array(
    '.',
    '..',
    'CVS',
  ), 0, FALSE);
  if (!imagepicker_make_alphadirs($dir)) {
    drupal_set_message(t('Directories could not be created'), 'error');
    return FALSE;
  }
  if (count($uiddirlist)) {
    foreach ($uiddirlist as $uiddir => $uid) {
      if ($uiddirlist[$uiddir]->basename) {
        $useruid = $uiddirlist[$uiddir]->basename;

        // get the user name
        $result = db_query("SELECT uid, name FROM {users} WHERE uid=%d AND status=1", array(
          $useruid,
        ));
        $row = db_fetch_array($result);
        if ($row['name']) {
          $username = $row['name'];
        }
        if ($username) {
          $firstletter = drupal_strtolower(drupal_substr($username, 0, 1));
          $firstletter = preg_match('/^[a-z]$/', $firstletter) ? $firstletter : 'others';
          $newpath = $dir . DIRECTORY_SEPARATOR . $firstletter . DIRECTORY_SEPARATOR . $username;
          $thumbstarget = $newpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR;
          $browsertarget = $newpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR;

          // create dirs
          if (!file_check_directory($newpath, TRUE)) {
            $errlist[$newpath] = "Could not create dir";
          }
          if (!file_check_directory($thumbstarget, TRUE)) {
            $errlist[$thumbstarget] = "Could not create thumbs dir";
          }
          if (!file_check_directory($browsertarget, TRUE)) {
            $errlist[$browsertarget] = "Could not create browser dir";
          }
          if (!count($errlist)) {

            // clear
            drupal_get_messages();
            $oldpath = $uiddir;
            $newfilelist = file_scan_directory($oldpath, ".*", array(
              '.',
              '..',
              'CVS',
            ), 0, FALSE);
            if (count($newfilelist)) {
              foreach ($newfilelist as $k => $v) {
                if (!is_dir($k)) {
                  $file = $newfilelist[$k]->filename;
                  $thumbsfile = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR . $newfilelist[$k]->basename;
                  $browserfile = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR . $newfilelist[$k]->basename;
                  if (!file_move($file, $newpath, FILE_EXISTS_ERROR)) {
                    $errlist[$file] = "Could not move to {$newpath}";
                  }
                  if (!file_move($thumbsfile, $thumbstarget, FILE_EXISTS_ERROR)) {
                    $errlist[$thumbsfile] = "Could not move to {$thumbstarget}";
                  }
                  if (!file_move($browserfile, $browsertarget, FILE_EXISTS_ERROR)) {
                    $errlist[$browserfile] = "Could not move to {$browsertarget}";
                  }
                }
              }
            }
            if (!count($errlist)) {
              $delpath[] = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR;
              $delpath[] = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR;
              $delpath[] = $oldpath;
            }
          }
        }
        else {
          $errlist[$k] = "NO uid for user {$u}";
        }
      }
    }
  }
  if (!count($errlist)) {

    // tidy up
    if (count($delpath)) {
      foreach ($delpath as $p) {
        if (is_dir($p)) {
          if (!rmdir($p)) {
            $errlist[$p] = 'deletion failed';
          }
        }
        else {
          $errlist[$p] = 'not a dir';
        }
      }
    }
  }
  if (count($errlist)) {
    foreach ($errlist as $key => $val) {
      drupal_set_message("ERRORS: {$key}: {$val} ", 'error');
    }
    $result = FALSE;
  }
  return $result;
}
function imagepicker_make_alphadirs($dir) {
  if (file_check_directory($dir, TRUE)) {
    $dirlist = range('a', 'z');
    array_push($dirlist, 'others');
    foreach ($dirlist as $v) {
      $newdir = $dir . DIRECTORY_SEPARATOR . $v;
      if (!file_check_directory($newdir, TRUE)) {
        drupal_set_message(t('Directory creation for the Imagepicker module was unsuccessful.'), 'error');
      }
    }
    $errors = drupal_get_messages('error');
    if (!count($errors)) {

      // clear
      drupal_get_messages();
      drupal_set_message(t('Directory creation for the Imagepicker module was successful.'));
      return $dir;
    }
    else {
      foreach ($errors['error'] as $error) {
        drupal_set_message($error, 'error');
      }
    }
  }
  else {
    drupal_set_message(t('Directory creation for the Imagepicker module was unsuccessful.'), 'error');
  }
  return FALSE;
}
function imagepicker_files_validate($dir) {
  $fstype = variable_get('imagepicker_fstype', '');
  $result = TRUE;
  $errlist = array();
  $uidlist = array();
  if ($fstype == 'numeric') {
    $baddirlist = array();
    $numericdirlist = file_scan_directory($dir, ".*", array(
      '.',
      '..',
      'CVS',
    ), 0, FALSE);
    foreach ($numericdirlist as $k => $v) {
      if (is_numeric($numericdirlist[$k]->basename)) {
        $numericdir = $numericdirlist[$k]->basename;
        $result = db_query("SELECT uid FROM {users} WHERE uid=%d AND status=1", array(
          $numericdir,
        ));
        $row = db_fetch_array($result);
        if ($row['uid']) {

          // build list of valid paths to images, use uid as key
          $uidlist[$row['uid']] = $numericdirlist[$k]->filename;
        }
        else {
          array_push($baduseridlist, $numericdir);
        }
      }
    }
    if (count($baduseridlist)) {
      $errlist['user ids do not exist'] = implode(',', $baduseridlist);
    }
  }
  elseif ($fstype == 'alpha') {

    // check that a-z and other exist, log errors
    $dirlist = range('a', 'z');
    array_push($dirlist, 'others');
    $baddirlist = array();
    $gooddirlist = array();
    foreach ($dirlist as $alphadir) {
      if (!is_dir($dir . DIRECTORY_SEPARATOR . $alphadir)) {
        array_push($baddirlist, $alphadir);
      }
      else {
        array_push($gooddirlist, $alphadir);
      }
    }
    if (count($baddirlist)) {
      $errlist['missing dirs'] = implode(',', $baddirlist);
    }

    // get the first level dirs actually present and check for unknowns
    $baddirlist = array();
    $alphadirlist = file_scan_directory($dir, ".*", array(
      '.',
      '..',
      'CVS',
    ), 0, FALSE);
    foreach ($alphadirlist as $k => $v) {
      $alphadir = $alphadirlist[$k]->basename;
      if (!in_array($alphadir, $dirlist)) {
        array_push($baddirlist, $alphadir);
      }
    }
    if (count($baddirlist)) {
      $errlist['Anomalous directories'] = implode(',', $baddirlist);
    }

    // search for usernames and validate them
    $userdirlist = array();
    $userdirlistpath = array();
    foreach ($gooddirlist as $alphadir) {
      $gooddir = $dir . DIRECTORY_SEPARATOR . $alphadir;
      $userdirlisttmp = file_scan_directory($gooddir, ".*", array(
        '.',
        '..',
        'CVS',
      ), 0, FALSE);
      if (count($userdirlisttmp)) {
        foreach ($userdirlisttmp as $k => $v) {
          array_push($userdirlist, $userdirlisttmp[$k]->basename);
          array_push($userdirlistpath, $userdirlisttmp[$k]->filename);
        }
      }
    }

    // we have usernames, now look them up
    $badusernamelist = array();
    foreach ($userdirlist as $k => $username) {
      $result = db_query("SELECT uid FROM {users} WHERE name='%s' AND status=1", array(
        $username,
      ));
      $row = db_fetch_array($result);
      if ($row['uid']) {

        // build list of valid paths to images, use uid as key
        $uidlist[$row['uid']] = $userdirlistpath[$k];
      }
      else {
        array_push($badusernamelist, $username);
      }
    }
    if (count($badusernamelist)) {
      $errlist['bad usernames'] = implode(',', $badusernamelist);
    }
  }

  // check valid uids against the imagepicker table, get the file list for each
  $badfiledirs = array();
  $badfilelist = array();
  $badthumbsfilelist = array();
  $badbrowserfilelist = array();
  $badfilelist2 = array();
  $badwritefiles = array();
  $files = array();
  $thumbsfiles = array();
  $browserfiles = array();
  foreach ($uidlist as $uid => $filepath) {
    $imgnames = array();
    $result = db_query("SELECT img_name FROM {imagepicker} WHERE uid=%d", array(
      $uid,
    ));
    while ($row = db_fetch_array($result)) {
      array_push($imgnames, $row['img_name']);
    }
    $filestmp = file_scan_directory($filepath, ".*", array(
      '.',
      '..',
      'CVS',
      IMAGEPICKER_THUMBS_DIR,
      IMAGEPICKER_BROWSER_DIR,
    ), 0, FALSE);
    if (count($filestmp)) {

      // compare files with db
      foreach ($filestmp as $k => $v) {
        $file = $filestmp[$k]->basename;
        array_push($files, $file);
        if (!is_writable($filestmp[$k]->filename)) {
          array_push($badwritefiles, $filestmp[$k]->filename);
        }
        $thumbsfile = $filepath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR . $file;
        array_push($thumbsfiles, $thumbsfile);
        $browserfile = $filepath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR . $file;
        array_push($browserfiles, $browserfile);

        // test for presence in db
        if (!in_array($file, $imgnames)) {
          array_push($badfilelist, $file);
        }
        if (!file_exists($thumbsfile)) {
          array_push($badthumbsfilelist, $thumbsfile);
        }
        elseif (!is_writable($thumbsfile)) {
          array_push($badwritefiles, $thumbsfile);
        }
        if (!file_exists($browserfile)) {
          array_push($badbrowserfilelist, $browserfile);
        }
        elseif (!is_writable($browserfile)) {
          array_push($badwritefiles, $browserfile);
        }
      }
    }
    else {
      array_push($badfiledirs, $uid);
    }

    // compare db to files
    if (count($imgnames)) {
      foreach ($imgnames as $imgname) {
        if (!in_array($imgname, $files)) {
          array_push($badfilelist2, $filepath . DIRECTORY_SEPARATOR . $imgname);
          $thumbsfile = $filepath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR . $imgname;
          if (!file_exists($thumbsfile)) {
            array_push($badthumbsfilelist, $thumbsfile);
          }
          $browserfile = $filepath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR . $imgname;
          if (!file_exists($browserfile)) {
            array_push($badbrowserfilelist, $browserfile);
          }
        }
      }
    }
  }
  if (count($badfilelist)) {
    $errlist[t('file not found in database')] = implode(',', $badfilelist);
  }
  if (count($badfilelist2)) {
    $errlist[t('database image name not found in files')] = implode(',', $badfilelist2);
  }
  if (count($badthumbsfilelist)) {
    $errlist[t('thumbs file does not exist')] = implode(',', $badthumbsfilelist);
  }
  if (count($badbrowserfilelist)) {
    $errlist[t('browser file does not exist')] = implode(',', $badbrowserfilelist);
  }
  if (count($badfiledirs)) {
    $errlist[t('uid has no files')] = implode(',', $badfiledirs);
  }
  if (count($badwritefiles)) {
    $errlist[t('file not writable')] = implode(',', $badwritefiles);
  }
  if (count($errlist)) {
    foreach ($errlist as $k => $v) {
      drupal_set_message("{$k}: {$v}", 'error');
    }
    drupal_set_message(t('Validation of directory structure failed, please correct the above errors before continuing.'), 'error');
    $result = FALSE;
  }
  else {
    drupal_set_message(t('Directory structure validated, %uidlist users found', array(
      '%uidlist' => count($uidlist),
    )));
  }
  return $result;
}
function imagepicker_fstype() {
  $dir = imagepicker_get_files_directory();
  $alphadir = 'a';
  if (is_dir($dir . DIRECTORY_SEPARATOR . $alphadir)) {
    variable_set('imagepicker_fstype', 'alpha');
  }
  else {
    variable_set('imagepicker_fstype', 'numeric');
  }
}
function imagepicker_get_files_directory() {
  $drupaldir = str_replace('/', DIRECTORY_SEPARATOR, getcwd()) . DIRECTORY_SEPARATOR;
  $dir = $drupaldir . file_directory_path() . DIRECTORY_SEPARATOR . IMAGEPICKER_FILES_DIR;
  return $dir;
}

/**
 * Retrieve a pipe delimited string of autocomplete suggestions for existing users
 */
function imagepicker_user_autocomplete($string = '') {
  $matches = array();
  if ($string) {
    $result = db_query_range("SELECT u.name FROM {users} u, {imagepicker} i WHERE LOWER(u.name) LIKE LOWER('%s%%') AND u.uid=i.uid", $string, 0, 10);
    while ($user = db_fetch_object($result)) {
      $matches[$user->name] = check_plain($user->name);
    }
  }
  print drupal_to_js($matches);
  exit;
}
function imagepicker_user_search_form($dir) {
  $form['imagepicker_currentuser'] = array(
    '#type' => 'textfield',
    '#title' => t('Users'),
    '#description' => '',
    '#autocomplete_path' => 'admin/settings/imagepicker/' . $dir . '/autocomplete',
    '#default_value' => '',
    '#maxlength' => 30,
    '#size' => 25,
    '#prefix' => '<div class="container-inline">',
  );
  $form['dir'] = array(
    '#type' => 'hidden',
    '#value' => $dir,
  );
  $form['imagepicker_stats_submit'] = array(
    '#type' => 'submit',
    '#value' => 'Go',
    '#suffix' => '</div>',
  );
  return $form;
}
function imagepicker_user_search_form_submit($form_id, $form_values) {
  if ($form_values['imagepicker_currentuser']) {

    #
    $result = db_query("SELECT u.uid FROM {users} u, {imagepicker} i WHERE u.name = '%s' AND u.uid=i.uid", $form_values['imagepicker_currentuser']);
    $row = db_fetch_array($result);
    if ($row['uid']) {
      variable_set('imagepicker_currentuser', $row['uid']);
      drupal_goto('admin/settings/imagepicker/' . $form_values['dir'] . '/user/' . $row['uid']);
    }
    else {
      variable_del('imagepicker_currentuser');
    }
  }
  else {
    variable_del('imagepicker_currentuser');
  }
}

/**
 * statistics
 */
function imagepicker_group_stats($account = FALSE) {

  // stats
  $usercount = FALSE;
  $bytecount = 0;
  if ($account) {
    if ($account == -1) {
      $allcount = _imagepicker_user_has_img();
      $totcount = _imagepicker_user_has_grouped_img('all');
      $publiccount = _imagepicker_user_has_grouped_img('yes');
      $nopubliccount = _imagepicker_user_has_grouped_img('no');
      $groupcount = _imagepicker_user_has_groups('all');
      $publicgroupcount = _imagepicker_user_has_groups('yes');
      $nopublicgroupcount = _imagepicker_user_has_groups('no');
      $bytecount = imagepicker_get_all_bytes(-1);
    }
    else {
      $allcount = _imagepicker_user_has_img($account);
      $totcount = _imagepicker_user_has_grouped_img('all', $account);
      $publiccount = _imagepicker_user_has_grouped_img('yes', $account);
      $nopubliccount = _imagepicker_user_has_grouped_img('no', $account);
      $groupcount = _imagepicker_user_has_groups('all', $account);
      $publicgroupcount = _imagepicker_user_has_groups('yes', $account);
      $nopublicgroupcount = _imagepicker_user_has_groups('no', $account);
      $bytecount = imagepicker_get_all_bytes($account);
    }
  }
  else {

    # imagepicker users
    $result = db_query("SELECT count(DISTINCT uid) AS ct FROM {imagepicker_user_groups}");
    $row = db_fetch_array($result);
    $usercount = $row['ct'];

    # all groups
    $result = db_query("SELECT count(gid) AS ct FROM {imagepicker_user_groups}");
    $row = db_fetch_array($result);
    $groupcount = $row['ct'];

    # public groups
    $result = db_query("SELECT count(gid) AS ct FROM {imagepicker_user_groups} WHERE public = 1");
    $row = db_fetch_array($result);
    $publicgroupcount = $row['ct'];

    # private groups
    $result = db_query("SELECT count(gid) AS ct FROM {imagepicker_user_groups} WHERE public = 0");
    $row = db_fetch_array($result);
    $nopublicgroupcount = $row['ct'];

    # all images
    $result = db_query("SELECT count(img_id) AS ct FROM {imagepicker}");
    $row = db_fetch_array($result);
    $allcount = $row['ct'];

    # grouped images
    $result = db_query("SELECT count(DISTINCT gi.img_id) AS ct FROM {imagepicker_group_images} gi, {imagepicker_user_groups} g WHERE g.gid = gi.gid ");
    $row = db_fetch_array($result);
    $totcount = $row['ct'];

    # public images
    $result = db_query("SELECT COUNT(i.img_id) AS ct FROM {imagepicker} i, {imagepicker_user_groups} g, {imagepicker_group_images} gi WHERE i.img_id = gi.img_id AND g.gid = gi.gid AND g.public = 1");
    $row = db_fetch_array($result);
    $publiccount = $row['ct'];

    # private images
    $result = db_query("SELECT COUNT(i.img_id) AS ct FROM {imagepicker} i, {imagepicker_user_groups} g, {imagepicker_group_images} gi WHERE i.img_id = gi.img_id AND g.gid = gi.gid AND g.public = 0");
    $row = db_fetch_array($result);
    $nopubliccount = $row['ct'];
    $bytecount = imagepicker_get_all_bytes();
  }
  $bytecount = _imagepicker_bkmg($bytecount);
  $headers = array(
    array(
      'data' => t('Group statistics'),
      'colspan' => 2,
    ),
    array(
      'data' => t('Image statistics'),
      'colspan' => 2,
    ),
  );
  if (user_access('use public imagepicker') && variable_get('imagepicker_public_enabled', 1)) {
    $rows = array(
      array(
        t('Groups'),
        $groupcount,
        t('Images'),
        $allcount,
      ),
      array(
        t('Public groups'),
        $publicgroupcount,
        t('Public images:'),
        $publiccount,
      ),
      array(
        t('Private groups'),
        $nopublicgroupcount,
        t('Private images'),
        $nopubliccount,
      ),
      array(
        '',
        '',
        t('Grouped images'),
        $totcount,
      ),
      array(
        '',
        '',
        t('Ungrouped images'),
        $allcount - $totcount,
      ),
      array(
        $usercount ? t('Users') : '',
        $usercount ? $usercount : '',
        t('Space used'),
        $bytecount,
      ),
    );
  }
  else {
    $rows = array(
      array(
        t('Groups'),
        $groupcount,
        t('Images'),
        $allcount,
      ),
      array(
        '',
        '',
        t('Grouped images'),
        $totcount,
      ),
      array(
        '',
        '',
        t('Ungrouped images'),
        $allcount - $totcount,
      ),
      array(
        $usercount ? t('Users') : '',
        $usercount ? $usercount : '',
        t('Space used'),
        $bytecount,
      ),
    );
  }
  $content .= '<div class="imgp_groups_info">' . theme('table', $headers, $rows) . '</div>';
  return $content;
}

/**
 * theming
 *
 */
function theme_imagepicker(&$content) {
  $head_title = drupal_get_title() ? strip_tags(drupal_get_title()) : variable_get('site_name', 'Drupal');
  $path = drupal_get_path('module', 'imagepicker');
  drupal_add_css($path . '/imagepicker.css');
  $styles = drupal_get_css();
  $scripts = drupal_get_js();
  $tabs = theme('menu_local_tasks');
  $messages = imagepicker_strip_messages(theme('status_messages'));
  $template = 'imagepicker.tpl.php';
  $defaulttemplate = variable_get('theme_default', '');
  if ($defaulttemplate) {
    $templatepath = drupal_get_path('theme', $defaulttemplate);
    if (file_exists($templatepath . '/' . $template)) {
      $template = $templatepath . '/' . $template;
    }
  }
  include $template;
  drupal_page_footer();
  exit;
}
function theme_imagepicker_iframe() {
  return '<iframe id="imagepicker" style="width: ' . variable_get('imagepicker_advanced_iframe_width', "100%") . '; height: ' . variable_get('imagepicker_advanced_iframe_height', 500) . 'px; border: ' . variable_get('imagepicker_advanced_iframe_border', "0") . ';" src="' . url('imagepicker') . '">Imagepicker requires iframe support.</iframe>';
}
function theme_imagepicker_user_menu($mode = 'browse') {
  global $user;
  $path = "user/" . $user->uid . "/imagepicker";
  $items = array(
    l(t('Upload'), $path),
    $mode == 'browse' ? l(t('List'), "{$path}/images/browseadmin") : l(t('Browse'), "{$path}/images/browse"),
    l(t('Groups'), "{$path}/groups/browse"),
    l(t('Stats'), "{$path}/stats"),
  );
  if (user_access('use public imagepicker') && variable_get('imagepicker_public_enabled', 1)) {
    $items[] = l(t('Browse Public'), "{$path}/images/browse_public");
  }
  $content .= theme_item_list($items, NULL, 'ul', $attributes = array(
    'class' => 'tabs secondary',
  ));
  return $content;
}
function theme_imagepicker_upload_progress_get_script($delay) {
  drupal_add_js(drupal_get_path('module', 'imagepicker') . '/imagepicker.js');
  drupal_add_js(array(
    'imagepicker_upload_progress' => array(
      'delay' => $delay[0],
    ),
  ), 'setting');
}
function theme_imagepicker_upload_progress_busy() {
  $message = variable_get('imagepicker_upload_progress_message', t('Processing form... please wait.'));
  return <<<CONTENT
  <div id="sending" class="progress" style="display: none;">
    <div class="status">{<span class="php-variable">$message</span>}</div>
    <div class="bar"></div>
  </div>
CONTENT;
}

/**
 * Retrieve a pipe delimited string of autocomplete suggestions for existing groups
 */
function imagepicker_group_search_autocomplete($string = '') {
  $matches = array();
  if ($string) {
    $result = db_query_range("SELECT group_name FROM {imagepicker_user_groups} WHERE LOWER(group_name) LIKE LOWER('%s%%')", $string, 0, 10);
    while ($group = db_fetch_object($result)) {
      $matches[$group->group_name] = check_plain($group->group_name);
    }
  }
  print drupal_to_js($matches);
  exit;
}
function imagepicker_group_search_form($dir = 'groups') {
  $form['imagepicker_currentgroup'] = array(
    '#type' => 'textfield',
    '#title' => t('Groups'),
    '#description' => '',
    '#autocomplete_path' => 'admin/settings/imagepicker/' . $dir . '/autocomplete',
    '#default_value' => '',
    '#maxlength' => 30,
    '#size' => 25,
    '#prefix' => '<div class="container-inline">',
  );
  $form['dir'] = array(
    '#type' => 'hidden',
    '#value' => $dir,
  );
  $form['imagepicker_group_search_submit'] = array(
    '#type' => 'submit',
    '#value' => 'Go',
    '#suffix' => '</div>',
  );
  return $form;
}
function imagepicker_group_search_form_submit($form_id, $form_values) {
  if ($form_values['imagepicker_currentgroup']) {
    $result = db_query("SELECT gid, uid FROM {imagepicker_user_groups} WHERE group_name = '%s' ", $form_values['imagepicker_currentgroup']);
    $row = db_fetch_array($result);
    if ($row['gid']) {
      variable_set('imagepicker_currentgroup', $row['gid']);
      drupal_goto('admin/settings/imagepicker/' . $form_values['dir'] . '/user/' . $row['uid'] . '/edit/' . $row['gid']);
    }
    else {
      variable_set('imagepicker_currentgroup', 0);
    }
  }
  else {
    variable_set('imagepicker_currentgroup', 0);
  }
}
function _imagepicker_view_content($img, $imgpath, $info) {
  $content .= '<div id="imgp_img_view">' . ($img['img_title'] ? '<div id="imgp_img_view_title">' . $img['img_title'] . '</div>' : '') . '<img id="imgp_img_view_img" src="' . $imgpath . '" alt="' . $img['img_title'] . '" title="' . $img['img_name'] . '" />' . ($img['img_description'] ? '<div id="imgp_img_view_desc">' . nl2br($img['img_description']) . '</div>' : '') . '<div>' . t('Width') . ": " . $info['width'] . "&nbsp;&nbsp;&nbsp;" . t('Height') . ": " . $info['height'] . "&nbsp;&nbsp;&nbsp;" . t('Type') . ": " . $info['extension'] . "&nbsp;&nbsp;&nbsp;" . t('Size') . ": " . $info['file_size'] . '</div></div>';
  return $content;
}
function _imagepicker_get_bytes($img_id, $account = FALSE) {
  if ($account) {
    $user = $account;
    $userdir = array(
      'name' => $user->name,
      'uid' => $user->uid,
    );
  }
  else {
    global $user;
    $userdir = FALSE;
  }
  $tot = 0;
  $img = _imagepicker_get_img($img_id, FALSE, $user);
  if ($img) {
    $path = imagepicker_get_path(FALSE, $userdir);
    $fullinfo = image_get_info($path . $img['img_name']);
    $thumbsinfo = image_get_info($path . IMAGEPICKER_THUMBS_DIR . '/' . $img['img_name']);
    $browserinfo = image_get_info($path . IMAGEPICKER_BROWSER_DIR . '/' . $img['img_name']);
    $tot = $fullinfo['file_size'] + $thumbsinfo['file_size'] + $browserinfo['file_size'];
  }
  else {
    drupal_set_message(t('Image not found.'), 'error');
  }
  return $tot;
}
function imagepicker_get_all_bytes($account = FALSE) {
  $tot = 0;
  $olduid = 0;
  $sql = 'SELECT img_id, uid FROM {imagepicker} ';
  if (is_object($account)) {
    $sql .= 'WHERE uid = ' . $account->uid;
    $user = $account;
  }
  elseif ($account == -1) {
    global $user;
    $sql .= 'WHERE uid = ' . $user->uid;
  }
  $result = db_query($sql);
  while ($row = db_fetch_array($result)) {
    if (!$account && $olduid != $row['uid']) {
      $user = user_load(array(
        'uid' => $row['uid'],
      ));
    }
    $tot += _imagepicker_get_bytes($row['img_id'], $user);
    $olduid = $row['uid'];
  }
  return $tot;
}
function _imagepicker_bkmg($number) {
  $inc = 1000;
  $count = 1000;
  $symarr = array(
    'K',
    'M',
    'G',
    'T',
  );
  $sym = 'B';
  while ($number > $count) {
    $count = $count * $inc;
    $sym = array_shift($symarr);
  }
  if ($number < $inc) {
    return $number . ' ' . $sym;
  }
  return round($number / $count * $inc, 2) . ' ' . $sym;
}

/**
 * quotas
 *
 */
function imagepicker_quota_form($account = FALSE) {
  $list = imagepicker_get_quota_list();
  $default = variable_get('imagepicker_quota_default', 0);
  $quota = $default;
  $title = t('Set default quota');
  if ($account) {
    $title = t('Set user quota');
    $quota = $account->imagepicker_quota;
    if (!$quota) {
      $quota = $default;
    }
  }
  $form['imagepicker_quota'] = array(
    '#type' => 'select',
    '#default_value' => $quota,
    '#options' => $list,
    '#title' => $title,
  );
  $form['account'] = array(
    '#type' => 'hidden',
    '#value' => $account ? $account->uid : 0,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
  );
  return $form;
}
function imagepicker_quota_form_submit($form_id, $form_values) {
  if ($form_values['account'] > 0) {
    $user = user_load(array(
      'uid' => $form_values['account'],
    ));
    user_save($user, array(
      'imagepicker_quota' => $form_values['imagepicker_quota'],
    ));
  }
  else {
    variable_set('imagepicker_quota_default', $form_values['imagepicker_quota']);
  }
}
function imagepicker_get_quota_list($key = 'x') {
  $list = array(
    0 => t('Unlimited'),
    1 => t('1 Meg'),
    5 => t('5 Meg'),
    10 => t('10 Meg'),
    25 => t('25 Meg'),
    50 => t('50 Meg'),
    100 => t('100 Meg'),
    250 => t('250 Meg'),
    500 => t('500 Meg'),
    750 => t('750 Meg'),
    1000 => t('1 Gig'),
    2000 => t('2 Gig'),
    5000 => t('5 Gig'),
  );
  if ($key == 'x') {
    return $list;
  }
  $value = $list[$key - 1];
  return $value;
}
function imagepicker_quota_ok($src, $account = FALSE) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $usedbytes = imagepicker_get_all_bytes($user) + 1;
  $usedbytesprint = _imagepicker_bkmg($usedbytes);
  $quota = 0;
  $quota_enabled = variable_get('imagepicker_quota_enable', 1);
  if ($quota_enabled) {
    $quota = isset($user->imagepicker_quota) ? $user->imagepicker_quota : variable_get('imagepicker_quota_default', 1);
    if ($quota > 0) {
      $quota_ok = $quota * 1000000 > $usedbytes ? TRUE : FALSE;
    }
    else {
      $quota_ok = TRUE;
    }
  }
  else {
    $quota_ok = TRUE;
  }
  $imgtot = _imagepicker_user_has_img($user);
  if ($quota_enabled) {
    if ($quota > 0) {
      $pused = round($usedbytes / ($quota * 1000000) * 100, 2);
      $quotaprint = imagepicker_get_quota_list($quota + 1);
      if ($src == 'admin') {
        $content .= '<p class="messages">' . t('The quota for %name is %quotaprint and has used %pused percent, or %usedbytesprint in %imgtot images', array(
          '%name' => $user->name,
          '%quotaprint' => $quotaprint,
          '%pused' => $pused,
          '%usedbytesprint' => $usedbytesprint,
          '%imgtot' => $imgtot,
        )) . '</p>';
      }
      else {
        $content .= '<p class="messages">' . t('Your quota is %quotaprint and you have used %pused percent, or %usedbytesprint in %imgtot images', array(
          '%quotaprint' => $quotaprint,
          '%pused' => $pused,
          '%usedbytesprint' => $usedbytesprint,
          '%imgtot' => $imgtot,
        )) . '</p>';
      }
    }
    else {
      $quotaprint = imagepicker_get_quota_list($quota + 1);
      if ($src == 'admin') {
        $content .= '<p class="messages">' . t('The quota for %name is %quotaprint and has used %usedbytesprint in %imgtot images', array(
          '%name' => $user->name,
          '%quotaprint' => $quotaprint,
          '%usedbytesprint' => $usedbytesprint,
          '%imgtot' => $imgtot,
        )) . '</p>';
      }
      else {
        $content .= '<p class="messages">' . t('Your quota is %quotaprint and you have used %usedbytesprint in %imgtot images', array(
          '%quotaprint' => $quotaprint,
          '%usedbytesprint' => $usedbytesprint,
          '%imgtot' => $imgtot,
        )) . '</p>';
      }
    }
  }
  else {
    if ($src == 'admin') {
      $content .= '<p class="messages">' . t('%name has used %usedbytesprint in %imgtot images', array(
        '%name' => $user->name,
        '%usedbytesprint' => $usedbytesprint,
        '%imgtot' => $imgtot,
      )) . '</p>';
    }
    else {
      $content .= '<p class="messages">' . t('You have used %usedbytesprint in %imgtot images', array(
        '%usedbytesprint' => $usedbytesprint,
        '%imgtot' => $imgtot,
      )) . '</p>';
    }
  }
  if ($quota_ok) {
    if ($account) {
      if ($src == 'admin') {
        $content .= drupal_get_form('imagepicker_upload_form', $user, TRUE);
      }
      elseif ($src == 'user') {
        $content .= drupal_get_form('imagepicker_upload_form', $user);
      }
    }
    else {
      $content .= drupal_get_form('imagepicker_upload_form');
    }
  }
  else {
    if ($account) {
      $content .= '<p class="messages">' . t('%name has used all of quota, please delete some files to make some room.', array(
        '%name' => $user->name,
      )) . '</p>';
    }
    else {
      $content .= '<p class="messages">' . t('You have used all of your quota, please delete some files to make some room.') . '</p>';
    }
  }
  return $content;
}

Functions

Namesort descending Description
imagepicker_admin_groups
imagepicker_admin_groups_list
imagepicker_admin_images
imagepicker_admin_image_form
imagepicker_admin_image_form_submit
imagepicker_admin_users_list Administration functions
imagepicker_admin_view
imagepicker_browse
imagepicker_browse_groups_form browse groups form
imagepicker_browse_groups_form_submit Submit browse groups form
imagepicker_browse_order_form browse order form
imagepicker_browse_order_form_submit Submit browse order form
imagepicker_browse_public
imagepicker_browse_public_form
imagepicker_browse_public_form_submit
imagepicker_convert_form files and conversion
imagepicker_convert_form_submit
imagepicker_convert_to_alpha
imagepicker_convert_to_numeric
imagepicker_copy_uploaded_file
imagepicker_delete_group_image
imagepicker_delete_user_group
imagepicker_edit_form
imagepicker_edit_form_submit Submit edit form
imagepicker_files_validate
imagepicker_form_alter Implementation of hook_form_alter().
imagepicker_fstype
imagepicker_get_all_bytes
imagepicker_get_enabled_group
imagepicker_get_files_directory
imagepicker_get_grouped_images
imagepicker_get_grouplist
imagepicker_get_groups
imagepicker_get_group_images_count
imagepicker_get_images_by_group
imagepicker_get_image_groups
imagepicker_get_image_path
imagepicker_get_path
imagepicker_get_quota_list
imagepicker_get_uploaded_file_extension
imagepicker_get_uploaded_file_name
imagepicker_get_user_group
imagepicker_get_user_groups_by_user
imagepicker_get_user_group_state
imagepicker_groups
imagepicker_groups_form groups form
imagepicker_groups_form_submit Submit groups form
imagepicker_groups_list
imagepicker_group_delete_form group delete form
imagepicker_group_delete_form_submit Submit group delete form
imagepicker_group_edit
imagepicker_group_images_form insert a form into the edit image page to allow the image to be associated with a group
imagepicker_group_images_form_submit Submit group images form
imagepicker_group_search_autocomplete Retrieve a pipe delimited string of autocomplete suggestions for existing groups
imagepicker_group_search_form
imagepicker_group_search_form_submit
imagepicker_group_stats statistics
imagepicker_has_grouplist
imagepicker_help Implementation of hook_help().
imagepicker_image_delete
imagepicker_image_edit Menu callback; fetches the image edit form for imagepicker
imagepicker_image_form
imagepicker_image_form_submit Submit image form
imagepicker_image_page Menu callback; presents the image page for imagepicker
imagepicker_image_select
imagepicker_insert_group_image
imagepicker_insert_user_group imagepicker_user_groups functions
imagepicker_js javascript insertion routine
imagepicker_make_alphadirs
imagepicker_menu Implementation of hook_menu().
imagepicker_perm Implementation of hook_perm().
imagepicker_quota_form quotas
imagepicker_quota_form_submit
imagepicker_quota_ok
imagepicker_role_has_permission
imagepicker_scale_image
imagepicker_settings_form
imagepicker_settings_form_validate Validate settings form
imagepicker_settings_page Menu callback; presents the settings form for imagepicker
imagepicker_set_user_group_state
imagepicker_strip_messages There is no need to inform users that directory structure has been created and show them all paths... So lets strip these messages if there are any.
imagepicker_update_user_group
imagepicker_upload Menu callback; presents the upload form for imagepicker
imagepicker_upload_form
imagepicker_upload_form_submit Submit upload form
imagepicker_upload_form_validate Validate upload form
imagepicker_user_autocomplete Retrieve a pipe delimited string of autocomplete suggestions for existing users
imagepicker_user_browse main thumbnails page in my imagepicker
imagepicker_user_browse_admin
imagepicker_user_browse_public
imagepicker_user_groups
imagepicker_user_has_role Helper function to check if a user has a specific role
imagepicker_user_image_edit
imagepicker_user_image_form
imagepicker_user_image_form_submit Submit form
imagepicker_user_page Account functions
imagepicker_user_search_form
imagepicker_user_search_form_submit
imagepicker_user_upload
imagepicker_user_view
imagepicker_user_view_public
theme_imagepicker theming
theme_imagepicker_iframe
theme_imagepicker_upload_progress_busy
theme_imagepicker_upload_progress_get_script
theme_imagepicker_user_menu
_imagepicker_bkmg
_imagepicker_browse
_imagepicker_browse_admin
_imagepicker_browse_public
_imagepicker_edit_img
_imagepicker_get_bytes
_imagepicker_get_img
_imagepicker_has_groups
_imagepicker_thumbs_display
_imagepicker_user_has_grouped_img
_imagepicker_user_has_groups groups
_imagepicker_user_has_img
_imagepicker_view_content

Constants

Namesort descending Description
IMAGEPICKER_BROWSER_DIR
IMAGEPICKER_FILES_DIR @file Enables permitted roles to upload images for insertion into configured nodes.
IMAGEPICKER_THUMBS_DIR