Projet

Général

Profil

Paste
Télécharger (26,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / imce / inc / imce.admin.inc @ 05237dd8

1
<?php
2

    
3
/**
4
 * @file
5
 * Serves administration pages of IMCE.
6
 */
7

    
8
/**
9
 * Admin main page.
10
 */
11
function imce_admin() {
12

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

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

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

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

    
28
  $output['title'] = array(
29
    '#markup' => '<h2 class="title">' . t('Configuration profiles') . '</h2>',
30
  );
31
  $output['table'] = array(
32
    '#theme' => 'table',
33
    '#header' => $header,
34
    '#rows' => $rows,
35
    '#attributes' => array('id' => 'imce-profiles-list'),
36
  );
37
  $output['form'] = drupal_get_form('imce_admin_form');
38

    
39
  // Display security warnings
40
  if (empty($_POST)) {
41
    $roles = variable_get('imce_roles_profiles', array());
42
    if (!empty($roles[DRUPAL_ANONYMOUS_RID]['public_pid']) || !empty($roles[DRUPAL_ANONYMOUS_RID]['private_pid'])) {
43
      drupal_set_message(t('Anonymous user role has access to IMCE.') . ' ' . t('Make sure this is not a misconfiguration.'), 'warning');
44
    }
45
    if (imce_admin_check_wildcard_upload(DRUPAL_AUTHENTICATED_RID, $roles)) {
46
      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');
47
    }
48
  }
49

    
50
  return $output;
51
}
52

    
53
/**
54
 * Admin form.
55
 */
56
function imce_admin_form($form, &$form_state) {
57
  //roles profiles
58
  $form['roles'] = array('#tree' => TRUE);
59
  $roles = imce_sorted_roles();
60
  $form['#weighted'] = count($roles) > 3;
61

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

    
67
  //common settings
68
  $form['common'] = array(
69
    '#type' => 'fieldset',
70
    '#title' => t('Common settings'),
71
    '#collapsible' => TRUE,
72
    '#collapsed' => TRUE,
73
  );
74
  $form['common']['textarea'] = array(
75
    '#type' => 'textfield',
76
    '#title' => t('Enable inline image/file insertion into plain textareas'),
77
    '#default_value' => variable_get('imce_settings_textarea', ''),
78
    '#maxlength' => NULL,
79
    '#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*.'),
80
  );
81
  $form['common']['absurls'] = array(
82
    '#type' => 'checkbox',
83
    '#title' => t('Absolute URLs'),
84
    '#default_value' => variable_get('imce_settings_absurls', 0),
85
    '#description' => t('Check if you want IMCE to return absolute file URLs.'),
86
  );
87
  $form['common']['replace'] = array(
88
    '#type' => 'radios',
89
    '#title' => t('Default behaviour for existing files during file uploads'),
90
    '#default_value' => variable_get('imce_settings_replace', FILE_EXISTS_RENAME),
91
    '#options' => array(
92
      FILE_EXISTS_RENAME => t('Keep the existing file renaming the new one'),
93
      FILE_EXISTS_ERROR => t('Keep the existing file rejecting the new one'),
94
      FILE_EXISTS_REPLACE => t('Replace the existing file with the new one')
95
    ),
96
  );
97
  $form['common']['thumb_method'] = array(
98
    '#type' => 'radios',
99
    '#title' => t('Default method for creating thumbnails'),
100
    '#default_value' => variable_get('imce_settings_thumb_method', 'scale_and_crop'),
101
    '#options' => array(
102
      'scale' => t('Scale the image with respect to the thumbnail dimensions.'),
103
      'scale_and_crop' => t('First scale then crop the image to fit the thumbnail dimensions.')
104
    ),
105
  );
106
  $form['common']['disable_private'] = array(
107
    '#type' => 'checkbox',
108
    '#title' => t('Disable serving of private files'),
109
    '#default_value' => variable_get('imce_settings_disable_private', 1),
110
    '#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.'),
111
  );
112
  $form['common']['admin_theme'] = array(
113
    '#type' => 'checkbox',
114
    '#title' => t('Use admin theme for IMCE paths'),
115
    '#default_value' => variable_get('imce_settings_admin_theme', FALSE),
116
    '#description' => t('If you have user interface issues with the active theme you may consider switching to admin theme.'),
117
  );
118

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

    
125
/**
126
 * Admin form themed.
127
 */
128
function imce_admin_theme($variables) {
129
  $form = $variables['form'];
130
  $profile1 = imce_user1_profile();
131
  $header = array(t('User role'));
132
  $rows = array(array(t('Site maintenance account')));
133
  $keys = array('name');
134
  //add each stream wrapper as a column
135
  $swrappers = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
136
  foreach ($swrappers as $scheme => $info) {
137
    $header[] = l($info['name'], 'imce/' . $scheme);
138
    $rows[0][] = check_plain($profile1['name']);
139
    $keys[] = $scheme . '_pid';
140
  }
141

    
142
  //in case we need profile weights.
143
  $weight_info = '';
144
  if ($form['#weighted']) {
145
    $header[] = t('Weight');
146
    $rows[0][] = t('n/a');
147
    $keys[] = 'weight';
148
    $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.');
149
  }
150

    
151
  foreach (element_children($form['roles']) as $rid) {
152
    $cells = array();
153
    foreach ($keys as $key) {
154
      $cells[] = drupal_render($form['roles'][$rid][$key]);
155
    }
156
    $rows[] = $cells;
157
  }
158

    
159
  $output = '<h2 class="title">' . t('Role-profile assignments') . '</h2>';
160
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
161
  $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>';
162
  $output .= drupal_render($form['common']);
163
  $output .= drupal_render_children($form);
164
  return $output;
165
}
166

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

    
178
/**
179
 * Submit admin form.
180
 */
181
function imce_admin_submit($form, &$form_state) {
182
  $roles = $form_state['values']['roles'];
183
  if (count($roles) > 3) {
184
    uasort($roles, 'imce_rolesort');
185
  }
186
  variable_set('imce_roles_profiles', $roles);
187
  variable_set('imce_settings_textarea', $form_state['values']['textarea']);
188
  variable_set('imce_settings_absurls', $form_state['values']['absurls']);
189
  variable_set('imce_settings_replace', $form_state['values']['replace']);
190
  variable_set('imce_settings_thumb_method', $form_state['values']['thumb_method']);
191
  variable_set('imce_settings_disable_private', $form_state['values']['disable_private']);
192
  variable_set('imce_settings_admin_theme', $form_state['values']['admin_theme']);
193
  drupal_set_message(t('Changes have been saved.'));
194
}
195

    
196
/**
197
 * Add-Edit-Delete profiles.
198
 */
199
function imce_profile_operations($op = 'add', $pid = 0) {
200
  //delete
201
  if ($op == 'delete') {
202
    drupal_set_title(t('Delete configuration profile'));
203
    return drupal_get_form('imce_profile_delete_form', $pid);
204
  }
205
  //add-edit
206
  if ($op === 'add' || $op === 'edit') {
207
    return drupal_get_form('imce_profile_form', $pid);
208
  }
209
  drupal_access_denied();
210
}
211

    
212
/**
213
 * Profile form.
214
 */
215
function imce_profile_form($form, &$form_state, $pid = 0) {
216

    
217
  if ($pid && $profile = imce_load_profile($pid)) {
218
    drupal_set_title($profile['name']);
219
  }
220
  else {
221
    $pid = 0;
222
    $profile = imce_sample_profile();
223
    $profile['name'] = '';
224
  }
225

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

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

    
237
  $form = array('#tree' => TRUE);
238
  $form['name'] = array(
239
    '#type' => 'textfield',
240
    '#title' => t('Profile name'),
241
    '#default_value' => $profile['name'],
242
    '#description' => t('Give a name to this profile.'),
243
    '#required' => TRUE,
244
  );
245
  $form['import'] = array(
246
    '#markup' => imce_profile_import_html($pid),
247
  );
248
  $form['usertab'] = array(
249
    '#type' => 'checkbox',
250
    '#title' => t('Display file browser tab in user profile pages.'),
251
    '#default_value' => $profile['usertab'],
252
  );
253
  $form['filesize'] = array(
254
    '#type' => 'textfield',
255
    '#title' => t('Maximum file size per upload'),
256
    '#default_value' => $profile['filesize'],
257
    '#description' => t('Set to 0 to use the maximum value available.') . ' ' . t('Your PHP settings limit the maximum file size per upload to %size.', array('%size' => format_size(file_upload_max_size()))),
258
    '#field_suffix' => t('MB'),
259
  );
260
  $form['quota'] = array(
261
    '#type' => 'textfield',
262
    '#title' => t('Directory quota'),
263
    '#default_value' => $profile['quota'],
264
    '#description' => t('Define the upload quota per directory.') . ' ' . t('Set to 0 to use the maximum value available.'),
265
    '#field_suffix' => t('MB'),
266
  );
267
  $form['tuquota'] = array(
268
    '#type' => 'textfield',
269
    '#title' => t('Total user quota'),
270
    '#default_value' => $profile['tuquota'],
271
    '#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.'),
272
    '#field_suffix' => t('MB'),
273
  );
274
  $form['extensions'] = array(
275
    '#type' => 'textfield',
276
    '#title' => t('Permitted file extensions'),
277
    '#default_value' => $profile['extensions'],
278
    '#maxlength' => 255,
279
    '#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.'),
280
  );
281
  $form['dimensions'] = array(
282
    '#type' => 'textfield',
283
    '#title' => t('Maximum image dimensions'),
284
    '#default_value' => $profile['dimensions'],
285
    '#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'))),
286
    '#field_suffix' => '<kbd>' . t('WIDTHxHEIGHT') . '</kbd>',
287
  );
288
  $form['filenum'] = array(
289
    '#type' => 'textfield',
290
    '#title' => t('Maximum number of files per operation'),
291
    '#default_value' => $profile['filenum'],
292
    '#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.'),
293
  );
294

    
295
  //Directories
296
  $form['directories']['#theme'] = 'imce_directories';
297
  $form['directories']['#weight'] = 1;
298
  for ($i = 0; $i < count($profile['directories']); $i++) {
299
    $form['directories'][$i] = imce_directory_form($profile['directories'][$i]);
300
  }
301
  $form['directories'][$i] = imce_directory_form();
302
  $form['directories'][$i+1] = imce_directory_form();
303

    
304
  //Thumbnails
305
  $form['thumbnails']['#theme'] = 'imce_thumbnails';
306
  $form['thumbnails']['#weight'] = 2;
307
  for ($i = 0; $i < count($profile['thumbnails']); $i++) {
308
    $form['thumbnails'][$i] = imce_thumbnail_form($profile['thumbnails'][$i]);
309
  }
310
  $form['thumbnails'][$i] = imce_thumbnail_form();
311
  $form['thumbnails'][$i+1] = imce_thumbnail_form();
312

    
313
  $form = array('profile' => $form);
314
  $form['pid'] = array('#type' => 'hidden', '#value' => $pid);
315
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
316
  $form['#validate'][] = 'imce_profile_validate';
317
  $form['#submit'][] = 'imce_profile_submit';
318
  return $form;
319
}
320

    
321
/**
322
 * Profile form validate.
323
 */
324
function imce_profile_validate($form, &$form_state) {
325
  $profile = &$form_state['values']['profile'];
326
  $dim_re = '/^\d+x\d+$/';
327
  $dim_error = t('Dimensions must be specified in <kbd>WIDTHxHEIGHT</kbd> format.');
328
  // Check max image dimensions
329
  if ($profile['dimensions'] && !preg_match($dim_re, $profile['dimensions'])) {
330
    return form_set_error('profile][dimensions', $dim_error);
331
  }
332
  // Check thumbnails dimensions
333
  foreach ($profile['thumbnails'] as $i => $thumb) {
334
    if (trim($thumb['name']) != '' && !preg_match($dim_re, $thumb['dimensions'])) {
335
      return form_set_error("profile][thumbnails][$i][dimensions", $dim_error);
336
    }
337
  }
338
}
339

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

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

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

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

    
358
/**
359
 * directory settings form
360
 */
361
function imce_directory_form($directory = array()) {
362
  if (empty($directory)) {
363
    $directory = array('name' => '', 'subnav' => 0, 'browse' => 0, 'upload' => 0, 'thumb' => 0, 'delete' => 0, 'resize' => 0);
364
  }
365
  $form['name'] = array(
366
    '#type' => 'textfield',
367
    '#default_value' => $directory['name'],
368
    '#size' => 24,
369
    '#maxlength' => NULL,
370
  );
371
  $form['subnav'] = array(
372
    '#type' => 'checkbox',
373
    '#title' => t('Including subdirectories'),
374
    '#default_value' => $directory['subnav'],
375
  );
376
  $form['browse'] = array(
377
    '#type' => 'checkbox',
378
    '#title' => t('Browse'),
379
    '#default_value' => $directory['browse'],
380
  );
381
  $form['upload'] = array(
382
    '#type' => 'checkbox',
383
    '#title' => t('Upload'),
384
    '#default_value' => $directory['upload'],
385
  );
386
  $form['thumb'] = array(
387
    '#type' => 'checkbox',
388
    '#title' => t('Thumbnails'),
389
    '#default_value' => $directory['thumb'],
390
  );
391
  $form['delete'] = array(
392
    '#type' => 'checkbox',
393
    '#title' => t('Delete'),
394
    '#default_value' => $directory['delete'],
395
  );
396
  $form['resize'] = array(
397
    '#type' => 'checkbox',
398
    '#title' => t('Resize'),
399
    '#default_value' => $directory['resize'],
400
  );
401
  return $form;
402
}
403

    
404
/**
405
 * Directorys form themed.
406
 */
407
function imce_directories_theme($variables) {
408
  $form = $variables['form'];
409
  $rows = array();
410
  $root = t('root');
411

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

    
417
    //permissions
418
    $header = array();
419
    foreach (element_children($form[$key]) as $perm) {
420
      $header[] = $form[$key][$perm]['#title'];
421
      unset($form[$key][$perm]['#title']);
422
      $row[] = drupal_render($form[$key][$perm]);
423
    }
424

    
425
    $rows[] = $row;
426
  }
427

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

    
430
  $output = '<h3 class="title">' . t('Directories') . '</h3>';
431
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
432
  $output .= '<div class="form-item"><div class="description">' . t('Define directories that users of this profile can access.
433
<ul>
434
	<li>Use alphanumeric characters as directory paths.</li>
435
	<li>To specify file system root, just enter <strong>.</strong>(dot) character.</li>
436
	<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>
437
  <li>To remove a directory from the list, leave the directory path blank.</li>
438
  <li>If you want more flexibility in directory paths you can execute php to return a directory path.<br />
439
  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 />
440
  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 />
441
  Note that you should use the $user variable instead of $GLOBALS[\'user\'] since they are not always the same object.</li>
442
</ul>
443
<p>Note that thumbnails permission does not affect thumbnail creation on upload. See thumbnails decription below.</p>
444
<p>If you need more fields, just fill all and save, and you will get two more on the next page.</p>') . '</div></div>';
445
  $output .= drupal_render_children($form);
446
  return $output;
447
}
448

    
449
/**
450
 * thumbnail settings form
451
 */
452
function imce_thumbnail_form($thumb = array()) {
453
  if (empty($thumb)) {
454
    $thumb = array('name' => '', 'dimensions' => '', 'prefix' => '', 'suffix' => '');
455
  }
456
  $form['name'] = array(
457
    '#type' => 'textfield',
458
    '#default_value' => $thumb['name'],
459
    '#size' => 20,
460
  );
461
  $form['dimensions'] = array(
462
    '#type' => 'textfield',
463
    '#default_value' => $thumb['dimensions'],
464
    '#size' => 20,
465
  );
466
  $form['prefix'] = array(
467
    '#type' => 'textfield',
468
    '#default_value' => $thumb['prefix'],
469
    '#size' => 20,
470
  );
471
  $form['suffix'] = array(
472
    '#type' => 'textfield',
473
    '#default_value' => $thumb['suffix'],
474
    '#size' => 20,
475
  );
476
  return $form;
477
}
478

    
479
/**
480
 * Thumbnails form themed.
481
 */
482
function imce_thumbnails_theme($variables) {
483
  $form = $variables['form'];
484
  $header = array(t('Name'), t('Dimensions'), t('Prefix'), t('Suffix'));
485
  $rows = array();
486

    
487
  foreach (element_children($form) as $key) {
488
    $rows[] = array(
489
      drupal_render($form[$key]['name']),
490
      drupal_render($form[$key]['dimensions']),
491
      drupal_render($form[$key]['prefix']),
492
      drupal_render($form[$key]['suffix']),
493
    );
494
  }
495

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

    
511
/**
512
 * Role-profile form
513
 */
514
function imce_role_form($role, $weight = TRUE, $core = TRUE) {
515
  $form['name'] = array(
516
    '#markup' => check_plain($role['name']),
517
  );
518
  if ($weight) {
519
    $form['weight'] = $core ? array(
520
      '#type' => 'textfield',
521
      '#value' => $role['weight'],
522
      '#attributes' => array('readonly' => 'readonly', 'style' => 'border: none; width: 2em; background-color: transparent;'),
523
    ) : array(
524
      '#type' => 'weight',
525
      '#default_value' => $role['weight'],
526
    );
527
  }
528
  foreach (array_keys(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE)) as $scheme) {
529
    $form[$scheme . '_pid'] = array(
530
      '#type' => 'select',
531
      '#options' => imce_profile_options(),
532
      '#default_value' => $role[$scheme . '_pid'],
533
      '#empty_value' => 0,
534
    );
535
  }
536
  return $form;
537
}
538

    
539
/**
540
 * Profile delete form
541
 */
542
function imce_profile_delete_form($form, &$form_state, $pid) {
543
  if ($pid > 1 && $profile = imce_load_profile($pid)) {
544
    $form['#submit'][] = 'imce_profile_delete_submit';
545
    $form['pid'] = array('#type' => 'hidden', '#value' => $pid);
546
    return confirm_form($form,
547
      t('Are you sure you want to delete the profile %name?',
548
      array('%name' => $profile['name'])),
549
      'admin/config/media/imce',
550
      '',
551
      t('Delete'),
552
      t('Cancel')
553
    );
554
  }
555
  drupal_goto('admin/config/media/imce');
556
}
557

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

    
567
/**
568
 * Profile options.
569
 */
570
function imce_profile_options() {
571
  $options = array();
572
  foreach (variable_get('imce_profiles', array()) as $pid => $profile) {
573
    $options[$pid] = $profile['name'];
574
  }
575
  return $options;
576
}
577

    
578
/**
579
 * Profile import links.
580
 */
581
function imce_profile_import_html($pid = 0) {
582
  $output = '';
583
  $links = array();
584

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

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

    
596
  return $output;
597
}
598

    
599
/**
600
 * Update role-profile assignments.
601
 */
602
function imce_update_roles($pid) {
603
  $roles = variable_get('imce_roles_profiles', array());
604
  foreach ($roles as $rid => $role) {
605
    foreach ($role as $key => $value) {
606
      if (substr($key, -4) == '_pid') {
607
        if ($value == $pid) {
608
          $roles[$rid][$key] = 0;
609
        }
610
        elseif ($value > $pid) {
611
          $roles[$rid][$key]--;
612
        }
613
      }
614
    }
615
  }
616
  variable_set('imce_roles_profiles', $roles);
617
}
618

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

    
625
  //add or update
626
  if (isset($profile)) {
627
    $pid = isset($profiles[$pid]) ? $pid : count($profiles)+1;
628
    $profiles[$pid] = $profile;
629
  }
630

    
631
  //delete
632
  elseif (isset($profiles[$pid]) && $pid > 1) {
633
    unset($profiles[$pid]);
634
    for ($i = $pid+1; isset($profiles[$i]); $i++) {
635
      $profiles[$i-1] = $profiles[$i];
636
      unset($profiles[$i]);
637
    }
638
    imce_update_roles($pid);
639
  }
640

    
641
  variable_set('imce_profiles', $profiles);
642
  return $pid;
643
}
644

    
645
/**
646
 * Unset empty fields in thumbnails and directory paths.
647
 */
648
function imce_clean_profile_fields(&$profile) {
649
  $clean = array();
650
  foreach ($profile['thumbnails'] as $thumb) {
651
    if (trim($thumb['name']) != '') {
652
      $clean[] = $thumb;
653
    }
654
  }
655
  $profile['thumbnails'] = $clean;
656

    
657
  $clean = array();
658
  $names = array();
659
  foreach ($profile['directories'] as $dir) {
660
    $dir['name'] = trim($dir['name'], '/ ');
661
    if ($dir['name'] == '') {
662
      continue;
663
    }
664
    if (isset($names[$dir['name']])) {
665
      drupal_set_message(t('Duplicate directory paths are not allowed.'), 'error');
666
      continue;
667
    }
668
    if (!imce_reg_dir($dir['name'])) {
669
      drupal_set_message(t('%dirname is not accepted as a proper directory name.', array('%dirname' => $dir['name'])), 'error');
670
      continue;
671
    }
672
    $clean[] = $dir;
673
    $names[$dir['name']] = 1;
674
  }
675
  $profile['directories'] = $clean;
676
}
677

    
678
/**
679
 * Profile load.
680
 */
681
function imce_load_profile($pid) {
682
  $profiles = variable_get('imce_profiles', array());
683
  return isset($profiles[$pid]) ? $profiles[$pid] : NULL;
684
}
685

    
686
/**
687
 * Sort roles according to their weights.
688
 */
689
function imce_sorted_roles() {
690
  static $sorted;
691
  if (!isset($sorted)) {
692
    $sorted = array();
693
    $roles = user_roles();
694
    $profiles = variable_get('imce_profiles', array());
695
    $roles_profiles = variable_get('imce_roles_profiles', array());
696
    $roles_profiles[DRUPAL_ANONYMOUS_RID]['weight'] = 12;
697
    $roles_profiles[DRUPAL_AUTHENTICATED_RID]['weight'] = 11;
698
    $schemes = array_keys(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE));
699
    foreach ($roles as $rid => $name) {
700
      $sorted[$rid] = array(
701
        'name' => $name,
702
        'weight' => isset($roles_profiles[$rid]['weight']) ? $roles_profiles[$rid]['weight'] : 0
703
      );
704
      foreach ($schemes as $scheme) {
705
        $key = $scheme . '_pid';
706
        $sorted[$rid][$key] = isset($roles_profiles[$rid][$key]) && isset($profiles[$roles_profiles[$rid][$key]]) ? $roles_profiles[$rid][$key] : 0;
707
      }
708
    }
709
    uasort($sorted, 'imce_rolesort');
710
  }
711
  return $sorted;
712
}
713

    
714
/**
715
 * Sorting function for roles.
716
 */
717
function imce_rolesort($r1, $r2) {
718
  return $r1['weight']-$r2['weight'];
719
}
720

    
721
/**
722
 * Checks if the given role can upload all extensions.
723
 */
724
function imce_admin_check_wildcard_upload($rid, $conf = NULL) {
725
  if (!isset($conf)) {
726
    $conf = variable_get('imce_roles_profiles', array());
727
  }
728
  if (!empty($conf[$rid])) {
729
    foreach ($conf[$rid] as $key => $pid) {
730
      if ($pid && substr($key, -4) == '_pid') {
731
        if ($profile = imce_load_profile($pid)) {
732
          if ($profile['extensions'] === '*' && !empty($profile['directories'])) {
733
            foreach ($profile['directories'] as $dirconf) {
734
              if (!empty($dirconf['upload'])) {
735
                return $key;
736
              }
737
            }
738
          }
739
        }
740
      }
741
    }
742
  }
743
  return FALSE;
744
}
745

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