Projet

Général

Profil

Paste
Télécharger (25,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / imce / inc / imce.admin.inc @ 6eb8d15f

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

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

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

    
136
  //in case we need profile weights.
137
  $weight_info = '';
138
  if ($form['#weighted']) {
139
    $header[] = t('Weight');
140
    $rows[0][] = t('n/a');
141
    $keys[] = 'weight';
142
    $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.');
143
  }
144

    
145
  foreach (element_children($form['roles']) as $rid) {
146
    $cells = array();
147
    foreach ($keys as $key) {
148
      $cells[] = drupal_render($form['roles'][$rid][$key]);
149
    }
150
    $rows[] = $cells;
151
  }
152

    
153
  $output = '<h2 class="title">' . t('Role-profile assignments') . '</h2>';
154
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
155
  $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>';
156
  $output .= drupal_render($form['common']);
157
  $output .= drupal_render_children($form);
158
  return $output;
159
}
160

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

    
172
/**
173
 * Submit admin form.
174
 */
175
function imce_admin_submit($form, &$form_state) {
176
  $roles = $form_state['values']['roles'];
177
  if (count($roles) > 3) {
178
    uasort($roles, 'imce_rolesort');
179
  }
180
  variable_set('imce_roles_profiles', $roles);
181
  variable_set('imce_settings_textarea', $form_state['values']['textarea']);
182
  variable_set('imce_settings_absurls', $form_state['values']['absurls']);
183
  variable_set('imce_settings_replace', $form_state['values']['replace']);
184
  variable_set('imce_settings_thumb_method', $form_state['values']['thumb_method']);
185
  variable_set('imce_settings_disable_private', $form_state['values']['disable_private']);
186
  drupal_set_message(t('Changes have been saved.'));
187
}
188

    
189
/**
190
 * Add-Edit-Delete profiles.
191
 */
192
function imce_profile_operations($op = 'add', $pid = 0) {
193
  //delete
194
  if ($op == 'delete') {
195
    drupal_set_title(t('Delete configuration profile'));
196
    return drupal_get_form('imce_profile_delete_form', $pid);
197
  }
198
  //add-edit
199
  if ($op === 'add' || $op === 'edit') {
200
    return drupal_get_form('imce_profile_form', $pid);
201
  }
202
  drupal_access_denied();
203
}
204

    
205
/**
206
 * Profile form.
207
 */
208
function imce_profile_form($form, &$form_state, $pid = 0) {
209

    
210
  if ($pid && $profile = imce_load_profile($pid)) {
211
    drupal_set_title($profile['name']);
212
  }
213
  else {
214
    $pid = 0;
215
    $profile = imce_sample_profile();
216
    $profile['name'] = '';
217
  }
218

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

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

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

    
288
  //Directories
289
  $form['directories']['#theme'] = 'imce_directories';
290
  $form['directories']['#weight'] = 1;
291
  for ($i = 0; $i < count($profile['directories']); $i++) {
292
    $form['directories'][$i] = imce_directory_form($profile['directories'][$i]);
293
  }
294
  $form['directories'][$i] = imce_directory_form();
295
  $form['directories'][$i+1] = imce_directory_form();
296

    
297
  //Thumbnails
298
  $form['thumbnails']['#theme'] = 'imce_thumbnails';
299
  $form['thumbnails']['#weight'] = 2;
300
  for ($i = 0; $i < count($profile['thumbnails']); $i++) {
301
    $form['thumbnails'][$i] = imce_thumbnail_form($profile['thumbnails'][$i]);
302
  }
303
  $form['thumbnails'][$i] = imce_thumbnail_form();
304
  $form['thumbnails'][$i+1] = imce_thumbnail_form();
305

    
306
  $form = array('profile' => $form);
307
  $form['pid'] = array('#type' => 'hidden', '#value' => $pid);
308
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
309
  $form['#validate'][] = 'imce_profile_validate';
310
  $form['#submit'][] = 'imce_profile_submit';
311
  return $form;
312
}
313

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

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

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

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

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

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

    
397
/**
398
 * Directorys form themed.
399
 */
400
function imce_directories_theme($variables) {
401
  $form = $variables['form'];
402
  $rows = array();
403
  $root = t('root');
404

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

    
410
    //permissions
411
    $header = array();
412
    foreach (element_children($form[$key]) as $perm) {
413
      $header[] = $form[$key][$perm]['#title'];
414
      unset($form[$key][$perm]['#title']);
415
      $row[] = drupal_render($form[$key][$perm]);
416
    }
417

    
418
    $rows[] = $row;
419
  }
420

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

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

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

    
472
/**
473
 * Thumbnails form themed.
474
 */
475
function imce_thumbnails_theme($variables) {
476
  $form = $variables['form'];
477
  $header = array(t('Name'), t('Dimensions'), t('Prefix'), t('Suffix'));
478
  $rows = array();
479

    
480
  foreach (element_children($form) as $key) {
481
    $rows[] = array(
482
      drupal_render($form[$key]['name']),
483
      drupal_render($form[$key]['dimensions']),
484
      drupal_render($form[$key]['prefix']),
485
      drupal_render($form[$key]['suffix']),
486
    );
487
  }
488

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

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

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

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

    
560
/**
561
 * Profile options.
562
 */
563
function imce_profile_options() {
564
  $options = array();
565
  foreach (variable_get('imce_profiles', array()) as $pid => $profile) {
566
    $options[$pid] = $profile['name'];
567
  }
568
  return $options;
569
}
570

    
571
/**
572
 * Profile import links.
573
 */
574
function imce_profile_import_html($pid = 0) {
575
  $output = '';
576
  $links = array();
577

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

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

    
589
  return $output;
590
}
591

    
592
/**
593
 * Update role-profile assignments.
594
 */
595
function imce_update_roles($pid) {
596
  $roles = variable_get('imce_roles_profiles', array());
597
  foreach ($roles as $rid => $role) {
598
    foreach ($role as $key => $value) {
599
      if (substr($key, -4) == '_pid') {
600
        if ($value == $pid) {
601
          $roles[$rid][$key] = 0;
602
        }
603
        elseif ($value > $pid) {
604
          $roles[$rid][$key]--;
605
        }
606
      }
607
    }
608
  }
609
  variable_set('imce_roles_profiles', $roles);
610
}
611

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

    
618
  //add or update
619
  if (isset($profile)) {
620
    $pid = isset($profiles[$pid]) ? $pid : count($profiles)+1;
621
    $profiles[$pid] = $profile;
622
  }
623

    
624
  //delete
625
  elseif (isset($profiles[$pid]) && $pid > 1) {
626
    unset($profiles[$pid]);
627
    for ($i = $pid+1; isset($profiles[$i]); $i++) {
628
      $profiles[$i-1] = $profiles[$i];
629
      unset($profiles[$i]);
630
    }
631
    imce_update_roles($pid);
632
  }
633

    
634
  variable_set('imce_profiles', $profiles);
635
  return $pid;
636
}
637

    
638
/**
639
 * Unset empty fields in thumbnails and directory paths.
640
 */
641
function imce_clean_profile_fields(&$profile) {
642
  $clean = array();
643
  foreach ($profile['thumbnails'] as $thumb) {
644
    if (trim($thumb['name']) != '') {
645
      $clean[] = $thumb;
646
    }
647
  }
648
  $profile['thumbnails'] = $clean;
649

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

    
671
/**
672
 * Profile load.
673
 */
674
function imce_load_profile($pid) {
675
  $profiles = variable_get('imce_profiles', array());
676
  return isset($profiles[$pid]) ? $profiles[$pid] : NULL;
677
}
678

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

    
707
/**
708
 * Sorting function for roles.
709
 */
710
function imce_rolesort($r1, $r2) {
711
  return $r1['weight']-$r2['weight'];
712
}
713

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

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