imce.admin.inc 25.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
<?php

/**
 * @file
 * Serves administration pages of IMCE.
 */

/**
 * Admin main page.
 */
function imce_admin() {

  $profiles = variable_get('imce_profiles', array());

  $header = array(t('Profile name'), array('data' => t('Operations'), 'colspan' => 2));
  $rows = array();

  foreach ($profiles as $pid => $profile) {
    $rows[] = array(
      check_plain($profile['name']),
      l(t('Edit'), 'admin/config/media/imce/profile/edit/' . $pid),
      $pid == 1 ? '' : l(t('Delete'), 'admin/config/media/imce/profile/delete/' . $pid),
    );
  }

  $rows[] = array('', array('data' => l(t('Add new profile'), 'admin/config/media/imce/profile'), 'colspan' => 2));

  $output['title'] = array(
    '#markup' => '<h2 class="title">' . t('Configuration profiles') . '</h2>',
  );
  $output['table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array('id' => 'imce-profiles-list'),
  );
  $output['form'] = drupal_get_form('imce_admin_form');

  // Display security warnings
  if (empty($_POST)) {
    $roles = variable_get('imce_roles_profiles', array());
    if (!empty($roles[DRUPAL_ANONYMOUS_RID]['public_pid']) || !empty($roles[DRUPAL_ANONYMOUS_RID]['private_pid'])) {
      drupal_set_message(t('Anonymous user role has access to IMCE.') . ' ' . t('Make sure this is not a misconfiguration.'), 'warning');
    }
    if (imce_admin_check_wildcard_upload(DRUPAL_AUTHENTICATED_RID, $roles)) {
      drupal_set_message(t('Authenticated user role is assigned a configuration profile with unrestricted file extensions.') . ' ' . t('Make sure this is not a misconfiguration.'), 'warning');
    }
  }

  return $output;
}

/**
 * Admin form.
 */
function imce_admin_form($form, &$form_state) {
  //roles profiles
  $form['roles'] = array('#tree' => TRUE);
  $roles = imce_sorted_roles();
  $form['#weighted'] = count($roles) > 3;

  foreach ($roles as $rid => $role) {
    $core = $rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID;
    $form['roles'][$rid] = imce_role_form($role, $form['#weighted'], $core);
  }

  //common settings
  $form['common'] = array(
    '#type' => 'fieldset',
    '#title' => t('Common settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['common']['textarea'] = array(
    '#type' => 'textfield',
    '#title' => t('Enable inline image/file insertion into plain textareas'),
    '#default_value' => variable_get('imce_settings_textarea', ''),
    '#maxlength' => NULL,
    '#description' => t('If you don\'t use any WYSIWYG editor, this feature will allow you to add your images or files as <strong>html code into any plain textarea</strong>. Enter <strong>comma separated textarea IDs</strong> under which you want to enable a link to IMCE. The * character is a wildcard. Hint: ID of Body fields in most node types starts with edit-body*.'),
  );
  $form['common']['absurls'] = array(
    '#type' => 'checkbox',
    '#title' => t('Absolute URLs'),
    '#default_value' => variable_get('imce_settings_absurls', 0),
    '#description' => t('Check if you want IMCE to return absolute file URLs.'),
  );
  $form['common']['replace'] = array(
    '#type' => 'radios',
    '#title' => t('Default behaviour for existing files during file uploads'),
    '#default_value' => variable_get('imce_settings_replace', FILE_EXISTS_RENAME),
    '#options' => array(
      FILE_EXISTS_RENAME => t('Keep the existing file renaming the new one'),
      FILE_EXISTS_ERROR => t('Keep the existing file rejecting the new one'),
      FILE_EXISTS_REPLACE => t('Replace the existing file with the new one')
    ),
  );
  $form['common']['thumb_method'] = array(
    '#type' => 'radios',
    '#title' => t('Default method for creating thumbnails'),
    '#default_value' => variable_get('imce_settings_thumb_method', 'scale_and_crop'),
    '#options' => array(
      'scale' => t('Scale the image with respect to the thumbnail dimensions.'),
      'scale_and_crop' => t('First scale then crop the image to fit the thumbnail dimensions.')
    ),
  );
  $form['common']['disable_private'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable serving of private files'),
    '#default_value' => variable_get('imce_settings_disable_private', 1),
    '#description' => t('IMCE serves all files under private files directory without applying any access restrictions. This allows anonymous access to any file(/system/files/filename) unless there is a module restricting access to the files. Here you can disable this feature.'),
  );

  $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  $form['#theme'] = 'imce_admin';
  $form['#submit'][] = 'imce_admin_submit';
  return $form;
}

/**
 * Admin form themed.
 */
function imce_admin_theme($variables) {
  $form = $variables['form'];
  $profile1 = imce_user1_profile();
  $header = array(t('User role'));
  $rows = array(array(t('Site maintenance account')));
  $keys = array('name');
  //add each stream wrapper as a column
  $swrappers = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
  foreach ($swrappers as $scheme => $info) {
    $header[] = l($info['name'], 'imce/' . $scheme);
    $rows[0][] = check_plain($profile1['name']);
    $keys[] = $scheme . '_pid';
  }

  //in case we need profile weights.
  $weight_info = '';
  if ($form['#weighted']) {
    $header[] = t('Weight');
    $rows[0][] = t('n/a');
    $keys[] = 'weight';
    $weight_info = t('For users who have <strong>multiple roles</strong>, the <strong>weight</strong> property will determine the assigned profile. Lighter roles that are placed upper will take the precedence. So, an administrator role should be placed over other roles by having a smaller weight, ie. -10.');
  }

  foreach (element_children($form['roles']) as $rid) {
    $cells = array();
    foreach ($keys as $key) {
      $cells[] = drupal_render($form['roles'][$rid][$key]);
    }
    $rows[] = $cells;
  }

  $output = '<h2 class="title">' . t('Role-profile assignments') . '</h2>';
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
  $output .= '<div class="form-item"><div class="description">' . t('Assign profiles to user roles for available file systems. Your default file system is %name.', array('%name' => $swrappers[variable_get('file_default_scheme', 'public')]['name'])) . ' ' . $weight_info . '</div></div>';
  $output .= drupal_render($form['common']);
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * Validate admin form.
 */
function imce_admin_form_validate($form, &$form_state) {
  $roles = $form_state['values']['roles'];
  // Check anonymous profile. Do not allow wildcard upload.
  if ($key = imce_admin_check_wildcard_upload(DRUPAL_ANONYMOUS_RID, $roles)) {
    form_error($form['roles'][DRUPAL_ANONYMOUS_RID][$key], t('Anonymous user role can not have a configuration profile with unrestricted file extensions.'));
  }
}

/**
 * Submit admin form.
 */
function imce_admin_submit($form, &$form_state) {
  $roles = $form_state['values']['roles'];
  if (count($roles) > 3) {
    uasort($roles, 'imce_rolesort');
  }
  variable_set('imce_roles_profiles', $roles);
  variable_set('imce_settings_textarea', $form_state['values']['textarea']);
  variable_set('imce_settings_absurls', $form_state['values']['absurls']);
  variable_set('imce_settings_replace', $form_state['values']['replace']);
  variable_set('imce_settings_thumb_method', $form_state['values']['thumb_method']);
  variable_set('imce_settings_disable_private', $form_state['values']['disable_private']);
  drupal_set_message(t('Changes have been saved.'));
}

/**
 * Add-Edit-Delete profiles.
 */
function imce_profile_operations($op = 'add', $pid = 0) {
  //delete
  if ($op == 'delete') {
    drupal_set_title(t('Delete configuration profile'));
    return drupal_get_form('imce_profile_delete_form', $pid);
  }
  //add-edit
  if ($op === 'add' || $op === 'edit') {
    return drupal_get_form('imce_profile_form', $pid);
  }
  drupal_access_denied();
}

/**
 * Profile form.
 */
function imce_profile_form($form, &$form_state, $pid = 0) {

  if ($pid && $profile = imce_load_profile($pid)) {
    drupal_set_title($profile['name']);
  }
  else {
    $pid = 0;
    $profile = imce_sample_profile();
    $profile['name'] = '';
  }

  //import profile
  if (isset($_GET['import']) && $imported = imce_load_profile($_GET['import'])) {
    if (empty($form_state['post'])) {
      drupal_set_message(t('Settings were imported from the profile %name', array('%name' => $imported['name'])));
    }
    $imported['name'] = $profile['name']; //preserve the original name.
    $profile = $imported;
  }

  $form_state['profile'] = $profile;//store the original profile just in case.

  $form = array('#tree' => TRUE);
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Profile name'),
    '#default_value' => $profile['name'],
    '#description' => t('Give a name to this profile.'),
    '#required' => TRUE,
  );
  $form['import'] = array(
    '#markup' => imce_profile_import_html($pid),
  );
  $form['usertab'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display file browser tab in user profile pages.'),
    '#default_value' => $profile['usertab'],
  );
  $form['filesize'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum file size per upload'),
    '#default_value' => $profile['filesize'],
    '#description' => t('Set to 0 to use the maximum value avaliable.') . ' ' . t('Your PHP settings limit the maximum file size per upload to %size.', array('%size' => format_size(file_upload_max_size()))),
    '#field_suffix' => t('MB'),
  );
  $form['quota'] = array(
    '#type' => 'textfield',
    '#title' => t('Directory quota'),
    '#default_value' => $profile['quota'],
    '#description' => t('Define the upload quota per directory.') . ' ' . t('Set to 0 to use the maximum value avaliable.'),
    '#field_suffix' => t('MB'),
  );
  $form['tuquota'] = array(
    '#type' => 'textfield',
    '#title' => t('Total user quota'),
    '#default_value' => $profile['tuquota'],
    '#description' => t('This quota measures the size of all user uploaded files in the database and does not include FTP files. You can either use both quotations together or safely ignore this by setting the value to 0.'),
    '#field_suffix' => t('MB'),
  );
  $form['extensions'] = array(
    '#type' => 'textfield',
    '#title' => t('Permitted file extensions'),
    '#default_value' => $profile['extensions'],
    '#maxlength' => 255,
    '#description' => t('Specify the allowed file extensions for uploaded files. Separate extensions with a space and do not include the leading dot.') . ' ' . t('Set to * to remove the restriction.'),
  );
  $form['dimensions'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum image dimensions'),
    '#default_value' => $profile['dimensions'],
    '#description' => t('The maximum allowed image size (e.g. 640x480). Set to 0 for no restriction. If an <a href="!image-toolkit-link">image toolkit</a> is installed, files exceeding this value will be scaled down to fit.', array('!image-toolkit-link' => url('admin/config/media/image-toolkit'))),
    '#field_suffix' => '<kbd>' . t('WIDTHxHEIGHT') . '</kbd>',
  );
  $form['filenum'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum number of files per operation'),
    '#default_value' => $profile['filenum'],
    '#description' => t('You can allow users to select multiple files for operations such as delete, resize, etc. Entire batch file operation is executed in a single drupal load, which may be good. However there will be an increase in script execution time, cpu load and memory consumption possibly exceeding the limits of your server, which is really bad. For unlimited number of file handling, set this to 0.'),
  );

  //Directories
  $form['directories']['#theme'] = 'imce_directories';
  $form['directories']['#weight'] = 1;
  for ($i = 0; $i < count($profile['directories']); $i++) {
    $form['directories'][$i] = imce_directory_form($profile['directories'][$i]);
  }
  $form['directories'][$i] = imce_directory_form();
  $form['directories'][$i+1] = imce_directory_form();

  //Thumbnails
  $form['thumbnails']['#theme'] = 'imce_thumbnails';
  $form['thumbnails']['#weight'] = 2;
  for ($i = 0; $i < count($profile['thumbnails']); $i++) {
    $form['thumbnails'][$i] = imce_thumbnail_form($profile['thumbnails'][$i]);
  }
  $form['thumbnails'][$i] = imce_thumbnail_form();
  $form['thumbnails'][$i+1] = imce_thumbnail_form();

  $form = array('profile' => $form);
  $form['pid'] = array('#type' => 'hidden', '#value' => $pid);
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  $form['#validate'][] = 'imce_profile_validate';
  $form['#submit'][] = 'imce_profile_submit';
  return $form;
}

/**
 * Profile form validate.
 */
function imce_profile_validate($form, &$form_state) {
  $profile = &$form_state['values']['profile'];
  $dim_re = '/^\d+x\d+$/';
  $dim_error = t('Dimensions must be specified in <kbd>WIDTHxHEIGHT</kbd> format.');
  // Check max image dimensions
  if ($profile['dimensions'] && !preg_match($dim_re, $profile['dimensions'])) {
    return form_set_error('profile][dimensions', $dim_error);
  }
  // Check thumbnails dimensions
  foreach ($profile['thumbnails'] as $i => $thumb) {
    if (trim($thumb['name']) != '' && !preg_match($dim_re, $thumb['dimensions'])) {
      return form_set_error("profile][thumbnails][$i][dimensions", $dim_error);
    }
  }
}

/**
 * Profile form submit.
 */
function imce_profile_submit($form, &$form_state) {
  $profile = $form_state['values']['profile'];
  $pid = $form_state['values']['pid'];
  $message = $pid > 0 ? t('The changes have been saved.') : t('Profile has been added.');

  //unset empty fields of directories and thumbnails.
  imce_clean_profile_fields($profile);

  //save profile.
  $pid = imce_update_profiles($pid, $profile);

  drupal_set_message($message);
  $form_state['redirect'] = 'admin/config/media/imce/profile/edit/' . $pid;
}

/**
 * directory settings form
 */
function imce_directory_form($directory = array()) {
  if (empty($directory)) {
    $directory = array('name' => '', 'subnav' => 0, 'browse' => 0, 'upload' => 0, 'thumb' => 0, 'delete' => 0, 'resize' => 0);
  }
  $form['name'] = array(
    '#type' => 'textfield',
    '#default_value' => $directory['name'],
    '#size' => 24,
    '#maxlength' => NULL,
  );
  $form['subnav'] = array(
    '#type' => 'checkbox',
    '#title' => t('Including subdirectories'),
    '#default_value' => $directory['subnav'],
  );
  $form['browse'] = array(
    '#type' => 'checkbox',
    '#title' => t('Browse'),
    '#default_value' => $directory['browse'],
  );
  $form['upload'] = array(
    '#type' => 'checkbox',
    '#title' => t('Upload'),
    '#default_value' => $directory['upload'],
  );
  $form['thumb'] = array(
    '#type' => 'checkbox',
    '#title' => t('Thumbnails'),
    '#default_value' => $directory['thumb'],
  );
  $form['delete'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete'),
    '#default_value' => $directory['delete'],
  );
  $form['resize'] = array(
    '#type' => 'checkbox',
    '#title' => t('Resize'),
    '#default_value' => $directory['resize'],
  );
  return $form;
}

/**
 * Directorys form themed.
 */
function imce_directories_theme($variables) {
  $form = $variables['form'];
  $rows = array();
  $root = t('root');

  foreach (element_children($form) as $key) {
    //directory path
    $row = array('<div class="container-inline">&lt;' . $root . '&gt;' . '/' . drupal_render($form[$key]['name']) . '</div>' . drupal_render($form[$key]['subnav']));
    unset($form[$key]['name'], $form[$key]['subnav']);

    //permissions
    $header = array();
    foreach (element_children($form[$key]) as $perm) {
      $header[] = $form[$key][$perm]['#title'];
      unset($form[$key][$perm]['#title']);
      $row[] = drupal_render($form[$key][$perm]);
    }

    $rows[] = $row;
  }

  array_unshift($header, t('Directory path'));

  $output = '<h3 class="title">' . t('Directories') . '</h3>';
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
  $output .= '<div class="form-item"><div class="description">' . t('Define directories that users of this profile can access.
<ul>
	<li>Use alphanumeric characters as directory paths.</li>
	<li>To specify file system root, just enter <strong>.</strong>(dot) character.</li>
	<li>Use <strong>%uid</strong> as a placeholder for user ID. Ex: <em>users/user%uid</em> creates directories such as <em>users/user1</em>, <em>users/user42</em>, etc.</li>
  <li>To remove a directory from the list, leave the directory path blank.</li>
  <li>If you want more flexibility in directory paths you can execute php to return a directory path.<br />
  For php execution your directory path must start with <strong>php:</strong> and the rest must be a valid php code that is expected to return the actual directory path. <br />Ex: <strong>php: return \'users/\'.$user->name;</strong> defines <strong>users/USER-NAME</strong> as the directory path.<br />
  A multi-level directory example <strong>php: return date(\'Y\', $user->created).\'/\'.date(\'m\', $user->created).\'/\'.$user->uid;</strong> defines <strong>MEMBERSHIP-YEAR/MONTH/USER-ID</strong> as the directory path, resulting in self-categorized user directories based on membership date.<br />
  Note that you should use the $user variable instead of $GLOBALS[\'user\'] since they are not always the same object.</li>
</ul>
<p>Note that thumbnails permission does not affect thumbnail creation on upload. See thumbnails decription below.</p>
<p>If you need more fields, just fill all and save, and you will get two more on the next page.</p>') . '</div></div>';
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * thumbnail settings form
 */
function imce_thumbnail_form($thumb = array()) {
  if (empty($thumb)) {
    $thumb = array('name' => '', 'dimensions' => '', 'prefix' => '', 'suffix' => '');
  }
  $form['name'] = array(
    '#type' => 'textfield',
    '#default_value' => $thumb['name'],
    '#size' => 20,
  );
  $form['dimensions'] = array(
    '#type' => 'textfield',
    '#default_value' => $thumb['dimensions'],
    '#size' => 20,
  );
  $form['prefix'] = array(
    '#type' => 'textfield',
    '#default_value' => $thumb['prefix'],
    '#size' => 20,
  );
  $form['suffix'] = array(
    '#type' => 'textfield',
    '#default_value' => $thumb['suffix'],
    '#size' => 20,
  );
  return $form;
}

/**
 * Thumbnails form themed.
 */
function imce_thumbnails_theme($variables) {
  $form = $variables['form'];
  $header = array(t('Name'), t('Dimensions'), t('Prefix'), t('Suffix'));
  $rows = array();

  foreach (element_children($form) as $key) {
    $rows[] = array(
      drupal_render($form[$key]['name']),
      drupal_render($form[$key]['dimensions']),
      drupal_render($form[$key]['prefix']),
      drupal_render($form[$key]['suffix']),
    );
  }

  $output = '<h3 class="title">' . t('Thumbnails') . '</h3>';
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
  $output .= '<div class="form-item"><div class="description">' . t('You may create a list of thumbnail options that users can choose from.
<ul>
  <li>Use alphanumeric characters as thumbnail names.</li>
  <li>Specify dimensions as <strong>WidthxHeight</strong>.</li>
  <li>Prefix and suffix are strings that are added to original file name to create the thumbnail name.</li>
  <li>An example thumbnail: Name = <strong>Small</strong>, Dimensions = <strong>80x80</strong>, Prefix = <strong>small_</strong></li>
</ul>
<p>Note that users will always be able to create these thumbnails on file upload no matter what the thumbnail permission is.</p>
<p>If you need more fields, just fill all and save, and you will get two more on the next page.</p>') . '</div></div>';
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * Role-profile form
 */
function imce_role_form($role, $weight = TRUE, $core = TRUE) {
  $form['name'] = array(
    '#markup' => check_plain($role['name']),
  );
  if ($weight) {
    $form['weight'] = $core ? array(
      '#type' => 'textfield',
      '#value' => $role['weight'],
      '#attributes' => array('readonly' => 'readonly', 'style' => 'border: none; width: 2em; background-color: transparent;'),
    ) : array(
      '#type' => 'weight',
      '#default_value' => $role['weight'],
    );
  }
  foreach (array_keys(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE)) as $scheme) {
    $form[$scheme . '_pid'] = array(
      '#type' => 'select',
      '#options' => imce_profile_options(),
      '#default_value' => $role[$scheme . '_pid'],
      '#empty_value' => 0,
    );
  }
  return $form;
}

/**
 * Profile delete form
 */
function imce_profile_delete_form($form, &$form_state, $pid) {
  if ($pid > 1 && $profile = imce_load_profile($pid)) {
    $form['#submit'][] = 'imce_profile_delete_submit';
    $form['pid'] = array('#type' => 'hidden', '#value' => $pid);
    return confirm_form($form,
      t('Are you sure you want to delete the profile %name?',
      array('%name' => $profile['name'])),
      'admin/config/media/imce',
      '',
      t('Delete'),
      t('Cancel')
    );
  }
  drupal_goto('admin/config/media/imce');
}

/**
 * Profile delete form submit
 */
function imce_profile_delete_submit($form, &$form_state) {
  imce_update_profiles($form_state['values']['pid'], NULL);
  drupal_set_message(t('Profile has been deleted.'));
  $form_state['redirect'] = 'admin/config/media/imce';
}

/**
 * Profile options.
 */
function imce_profile_options() {
  $options = array();
  foreach (variable_get('imce_profiles', array()) as $pid => $profile) {
    $options[$pid] = $profile['name'];
  }
  return $options;
}

/**
 * Profile import links.
 */
function imce_profile_import_html($pid = 0) {
  $output = '';
  $links = array();

  foreach (variable_get('imce_profiles', array()) as $id => $profile) {
    if ($pid != $id) {
      $links[] = l($profile['name'], $_GET['q'], array('query' => array('import' => $id)));
    }
  }

  if (!empty($links)) {
    $output = '<p><strong>' . t('Import settings from other profiles') . '</strong>: ';
    $output .= implode(', ', $links) . '</p>';
  }

  return $output;
}

/**
 * Update role-profile assignments.
 */
function imce_update_roles($pid) {
  $roles = variable_get('imce_roles_profiles', array());
  foreach ($roles as $rid => $role) {
    foreach ($role as $key => $value) {
      if (substr($key, -4) == '_pid') {
        if ($value == $pid) {
          $roles[$rid][$key] = 0;
        }
        elseif ($value > $pid) {
          $roles[$rid][$key]--;
        }
      }
    }
  }
  variable_set('imce_roles_profiles', $roles);
}

/**
 * Add, update or delete a profile.
 */
function imce_update_profiles($pid, $profile = NULL) {
  $profiles = variable_get('imce_profiles', array());

  //add or update
  if (isset($profile)) {
    $pid = isset($profiles[$pid]) ? $pid : count($profiles)+1;
    $profiles[$pid] = $profile;
  }

  //delete
  elseif (isset($profiles[$pid]) && $pid > 1) {
    unset($profiles[$pid]);
    for ($i = $pid+1; isset($profiles[$i]); $i++) {
      $profiles[$i-1] = $profiles[$i];
      unset($profiles[$i]);
    }
    imce_update_roles($pid);
  }

  variable_set('imce_profiles', $profiles);
  return $pid;
}

/**
 * Unset empty fields in thumbnails and directory paths.
 */
function imce_clean_profile_fields(&$profile) {
  $clean = array();
  foreach ($profile['thumbnails'] as $thumb) {
    if (trim($thumb['name']) != '') {
      $clean[] = $thumb;
    }
  }
  $profile['thumbnails'] = $clean;

  $clean = array();
  $names = array();
  foreach ($profile['directories'] as $dir) {
    $dir['name'] = trim($dir['name'], '/ ');
    if ($dir['name'] == '') {
      continue;
    }
    if (isset($names[$dir['name']])) {
      drupal_set_message(t('Duplicate directory paths are not allowed.'), 'error');
      continue;
    }
    if (!imce_reg_dir($dir['name'])) {
      drupal_set_message(t('%dirname is not accepted as a proper directory name.', array('%dirname' => $dir['name'])), 'error');
      continue;
    }
    $clean[] = $dir;
    $names[$dir['name']] = 1;
  }
  $profile['directories'] = $clean;
}

/**
 * Profile load.
 */
function imce_load_profile($pid) {
  $profiles = variable_get('imce_profiles', array());
  return isset($profiles[$pid]) ? $profiles[$pid] : NULL;
}

/**
 * Sort roles according to their weights.
 */
function imce_sorted_roles() {
  static $sorted;
  if (!isset($sorted)) {
    $sorted = array();
    $roles = user_roles();
    $profiles = variable_get('imce_profiles', array());
    $roles_profiles = variable_get('imce_roles_profiles', array());
    $roles_profiles[DRUPAL_ANONYMOUS_RID]['weight'] = 12;
    $roles_profiles[DRUPAL_AUTHENTICATED_RID]['weight'] = 11;
    $schemes = array_keys(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE));
    foreach ($roles as $rid => $name) {
      $sorted[$rid] = array(
        'name' => $name,
        'weight' => isset($roles_profiles[$rid]['weight']) ? $roles_profiles[$rid]['weight'] : 0
      );
      foreach ($schemes as $scheme) {
        $key = $scheme . '_pid';
        $sorted[$rid][$key] = isset($roles_profiles[$rid][$key]) && isset($profiles[$roles_profiles[$rid][$key]]) ? $roles_profiles[$rid][$key] : 0;
      }
    }
    uasort($sorted, 'imce_rolesort');
  }
  return $sorted;
}

/**
 * Sorting function for roles.
 */
function imce_rolesort($r1, $r2) {
  return $r1['weight']-$r2['weight'];
}

/**
 * Checks if the given role can upload all extensions.
 */
function imce_admin_check_wildcard_upload($rid, $conf = NULL) {
  if (!isset($conf)) {
    $conf = variable_get('imce_roles_profiles', array());
  }
  if (!empty($conf[$rid])) {
    foreach ($conf[$rid] as $key => $pid) {
      if ($pid && substr($key, -4) == '_pid') {
        if ($profile = imce_load_profile($pid)) {
          if ($profile['extensions'] === '*' && !empty($profile['directories'])) {
            foreach ($profile['directories'] as $dirconf) {
              if (!empty($dirconf['upload'])) {
                return $key;
              }
            }
          }
        }
      }
    }
  }
  return FALSE;
}

//Include core profile functions.
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'imce') . '/inc/imce.core.profiles.inc';