Projet

Général

Profil

Paste
Télécharger (24,1 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / imce / inc / imce.admin.inc @ a5572547

1 85ad3d82 Assos Assos
<?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
  return $output;
39
}
40
41
/**
42
 * Admin form.
43
 */
44
function imce_admin_form($form, &$form_state) {
45
  //roles profiles
46
  $form['roles'] = array('#tree' => TRUE);
47
  $roles = imce_sorted_roles();
48
  $form['#weighted'] = count($roles) > 3;
49
50
  foreach ($roles as $rid => $role) {
51
    $core = $rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID;
52
    $form['roles'][$rid] = imce_role_form($role, $form['#weighted'], $core);
53
  }
54
55
  //common settings
56
  $form['common'] = array(
57
    '#type' => 'fieldset',
58
    '#title' => t('Common settings'),
59
    '#collapsible' => TRUE,
60
    '#collapsed' => TRUE,
61
  );
62
  $form['common']['textarea'] = array(
63
    '#type' => 'textfield',
64
    '#title' => t('Enable inline image/file insertion into plain textareas'),
65
    '#default_value' => variable_get('imce_settings_textarea', ''),
66
    '#maxlength' => NULL,
67
    '#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*.'),
68
  );
69
  $form['common']['absurls'] = array(
70
    '#type' => 'checkbox',
71
    '#title' => t('Absolute URLs'),
72
    '#default_value' => variable_get('imce_settings_absurls', 0),
73
    '#description' => t('Check if you want IMCE to return absolute file URLs.'),
74
  );
75
  $form['common']['replace'] = array(
76
    '#type' => 'radios',
77
    '#title' => t('Default behaviour for existing files during file uploads'),
78
    '#default_value' => variable_get('imce_settings_replace', FILE_EXISTS_RENAME),
79
    '#options' => array(
80
      FILE_EXISTS_RENAME => t('Keep the existing file renaming the new one'),
81
      FILE_EXISTS_ERROR => t('Keep the existing file rejecting the new one'),
82
      FILE_EXISTS_REPLACE => t('Replace the existing file with the new one')
83
    ),
84
  );
85
  $form['common']['thumb_method'] = array(
86
    '#type' => 'radios',
87
    '#title' => t('Default method for creating thumbnails'),
88
    '#default_value' => variable_get('imce_settings_thumb_method', 'scale_and_crop'),
89
    '#options' => array(
90
      'scale' => t('Scale the image with respect to the thumbnail dimensions.'),
91
      'scale_and_crop' => t('First scale then crop the image to fit the thumbnail dimensions.')
92
    ),
93
  );
94
  $form['common']['disable_private'] = array(
95
    '#type' => 'checkbox',
96
    '#title' => t('Disable serving of private files'),
97
    '#default_value' => variable_get('imce_settings_disable_private', 1),
98
    '#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.'),
99
  );
100
101
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
102
  $form['#theme'] = 'imce_admin';
103
  $form['#submit'][] = 'imce_admin_submit';
104
  return $form;
105
}
106
107
/**
108
 * Admin form themed.
109
 */
110
function imce_admin_theme($variables) {
111
  $form = $variables['form'];
112
  $profile1 = imce_user1_profile();
113
  $header = array(t('User role'));
114
  $rows = array(array(t('Site maintenance account')));
115
  $keys = array('name');
116
  //add each stream wrapper as a column
117
  $swrappers = file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE);
118
  foreach ($swrappers as $scheme => $info) {
119
    $header[] = l($info['name'], 'imce/' . $scheme);
120
    $rows[0][] = check_plain($profile1['name']);
121
    $keys[] = $scheme . '_pid';
122
  }
123
124
  //in case we need profile weights.
125
  $weight_info = '';
126
  if ($form['#weighted']) {
127
    $header[] = t('Weight');
128
    $rows[0][] = t('n/a');
129
    $keys[] = 'weight';
130
    $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.');
131
  }
132
133
  foreach (element_children($form['roles']) as $rid) {
134
    $cells = array();
135
    foreach ($keys as $key) {
136
      $cells[] = drupal_render($form['roles'][$rid][$key]);
137
    }
138
    $rows[] = $cells;
139
  }
140
141
  $output = '<h2 class="title">' . t('Role-profile assignments') . '</h2>';
142
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
143
  $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>';
144
  $output .= drupal_render($form['common']);
145
  $output .= drupal_render_children($form);
146
  return $output;
147
}
148
149
/**
150
 * Submit admin form.
151
 */
152
function imce_admin_submit($form, &$form_state) {
153
  $roles = $form_state['values']['roles'];
154
  if (count($roles) > 3) {
155
    uasort($roles, 'imce_rolesort');
156
  }
157
  variable_set('imce_roles_profiles', $roles);
158
  variable_set('imce_settings_textarea', $form_state['values']['textarea']);
159
  variable_set('imce_settings_absurls', $form_state['values']['absurls']);
160
  variable_set('imce_settings_replace', $form_state['values']['replace']);
161
  variable_set('imce_settings_thumb_method', $form_state['values']['thumb_method']);
162
  variable_set('imce_settings_disable_private', $form_state['values']['disable_private']);
163
  drupal_set_message(t('Changes have been saved.'));
164
}
165
166
/**
167
 * Add-Edit-Delete profiles.
168
 */
169
function imce_profile_operations($op = 'add', $pid = 0) {
170
  //delete
171
  if ($op == 'delete') {
172
    drupal_set_title(t('Delete configuration profile'));
173
    return drupal_get_form('imce_profile_delete_form', $pid);
174
  }
175
  //add-edit
176
  if ($pid != 1 || $GLOBALS['user']->uid == 1) {
177
    return drupal_get_form('imce_profile_form', $pid);
178
  }
179
  drupal_access_denied();
180
}
181
182
/**
183
 * Profile form.
184
 */
185
function imce_profile_form($form, &$form_state, $pid = 0) {
186
187
  if ($pid && $profile = imce_load_profile($pid)) {
188
    drupal_set_title($profile['name']);
189
  }
190
  else {
191
    $pid = 0;
192
    $profile = imce_sample_profile();
193
    $profile['name'] = '';
194
  }
195
196
  //import profile
197
  if (isset($_GET['import']) && $imported = imce_load_profile($_GET['import'])) {
198
    if (empty($form_state['post'])) {
199
      drupal_set_message(t('Settings were imported from the profile %name', array('%name' => $imported['name'])));
200
    }
201
    $imported['name'] = $profile['name']; //preserve the original name.
202
    $profile = $imported;
203
  }
204
205
  $form_state['profile'] = $profile;//store the original profile just in case.
206
207
  $form = array('#tree' => TRUE);
208
  $form['name'] = array(
209
    '#type' => 'textfield',
210
    '#title' => t('Profile name'),
211
    '#default_value' => $profile['name'],
212
    '#description' => t('Give a name to this profile.'),
213
    '#required' => TRUE,
214
  );
215
  $form['import'] = array(
216
    '#markup' => imce_profile_import_html($pid),
217
  );
218
  $form['usertab'] = array(
219
    '#type' => 'checkbox',
220
    '#title' => t('Display file browser tab in user profile pages.'),
221
    '#default_value' => $profile['usertab'],
222
  );
223
  $form['filesize'] = array(
224
    '#type' => 'textfield',
225
    '#title' => t('Maximum file size per upload'),
226
    '#default_value' => $profile['filesize'],
227
    '#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()))),
228
    '#field_suffix' => t('MB'),
229
  );
230
  $form['quota'] = array(
231
    '#type' => 'textfield',
232
    '#title' => t('Directory quota'),
233
    '#default_value' => $profile['quota'],
234
    '#description' => t('Define the upload quota per directory.') . ' ' . t('Set to 0 to use the maximum value avaliable.'),
235
    '#field_suffix' => t('MB'),
236
  );
237
  $form['tuquota'] = array(
238
    '#type' => 'textfield',
239
    '#title' => t('Total user quota'),
240
    '#default_value' => $profile['tuquota'],
241
    '#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.'),
242
    '#field_suffix' => t('MB'),
243
  );
244
  $form['extensions'] = array(
245
    '#type' => 'textfield',
246
    '#title' => t('Permitted file extensions'),
247
    '#default_value' => $profile['extensions'],
248
    '#maxlength' => 255,
249
    '#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.'),
250
  );
251
  $form['dimensions'] = array(
252
    '#type' => 'textfield',
253
    '#title' => t('Maximum image dimensions'),
254
    '#default_value' => $profile['dimensions'],
255
    '#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'))),
256
    '#field_suffix' => '<kbd>' . t('WIDTHxHEIGHT') . '</kbd>',
257
  );
258
  $form['filenum'] = array(
259
    '#type' => 'textfield',
260
    '#title' => t('Maximum number of files per operation'),
261
    '#default_value' => $profile['filenum'],
262
    '#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.'),
263
  );
264
265
  //Directories
266
  $form['directories']['#theme'] = 'imce_directories';
267
  $form['directories']['#weight'] = 1;
268
  for ($i = 0; $i < count($profile['directories']); $i++) {
269
    $form['directories'][$i] = imce_directory_form($profile['directories'][$i]);
270
  }
271
  $form['directories'][$i] = imce_directory_form();
272
  $form['directories'][$i+1] = imce_directory_form();
273
274
  //Thumbnails
275
  $form['thumbnails']['#theme'] = 'imce_thumbnails';
276
  $form['thumbnails']['#weight'] = 2;
277
  for ($i = 0; $i < count($profile['thumbnails']); $i++) {
278
    $form['thumbnails'][$i] = imce_thumbnail_form($profile['thumbnails'][$i]);
279
  }
280
  $form['thumbnails'][$i] = imce_thumbnail_form();
281
  $form['thumbnails'][$i+1] = imce_thumbnail_form();
282
283
  $form = array('profile' => $form);
284
  $form['pid'] = array('#type' => 'hidden', '#value' => $pid);
285
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
286
  $form['#validate'][] = 'imce_profile_validate';
287
  $form['#submit'][] = 'imce_profile_submit';
288
  return $form;
289
}
290
291
/**
292
 * Profile form validate.
293
 */
294
function imce_profile_validate($form, &$form_state) {
295
  $profile = &$form_state['values']['profile'];
296
  $dim_re = '/^\d+x\d+$/';
297
  $dim_error = t('Dimensions must be specified in <kbd>WIDTHxHEIGHT</kbd> format.');
298
  // Check max image dimensions
299
  if ($profile['dimensions'] && !preg_match($dim_re, $profile['dimensions'])) {
300
    return form_set_error('profile][dimensions', $dim_error);
301
  }
302
  // Check thumbnails dimensions
303
  foreach ($profile['thumbnails'] as $i => $thumb) {
304
    if (trim($thumb['name']) != '' && !preg_match($dim_re, $thumb['dimensions'])) {
305
      return form_set_error("profile][thumbnails][$i][dimensions", $dim_error);
306
    }
307
  }
308
}
309
310
/**
311
 * Profile form submit.
312
 */
313
function imce_profile_submit($form, &$form_state) {
314
  $profile = $form_state['values']['profile'];
315
  $pid = $form_state['values']['pid'];
316
  $message = $pid > 0 ? t('The changes have been saved.') : t('Profile has been added.');
317
318
  //unset empty fields of directories and thumbnails.
319
  imce_clean_profile_fields($profile);
320
321
  //save profile.
322
  $pid = imce_update_profiles($pid, $profile);
323
324
  drupal_set_message($message);
325
  $form_state['redirect'] = 'admin/config/media/imce/profile/edit/' . $pid;
326
}
327
328
/**
329
 * directory settings form
330
 */
331
function imce_directory_form($directory = array()) {
332
  if (empty($directory)) {
333
    $directory = array('name' => '', 'subnav' => 0, 'browse' => 0, 'upload' => 0, 'thumb' => 0, 'delete' => 0, 'resize' => 0);
334
  }
335
  $form['name'] = array(
336
    '#type' => 'textfield',
337
    '#default_value' => $directory['name'],
338
    '#size' => 24,
339
    '#maxlength' => NULL,
340
  );
341
  $form['subnav'] = array(
342
    '#type' => 'checkbox',
343
    '#title' => t('Including subdirectories'),
344
    '#default_value' => $directory['subnav'],
345
  );
346
  $form['browse'] = array(
347
    '#type' => 'checkbox',
348
    '#title' => t('Browse'),
349
    '#default_value' => $directory['browse'],
350
  );
351
  $form['upload'] = array(
352
    '#type' => 'checkbox',
353
    '#title' => t('Upload'),
354
    '#default_value' => $directory['upload'],
355
  );
356
  $form['thumb'] = array(
357
    '#type' => 'checkbox',
358
    '#title' => t('Thumbnails'),
359
    '#default_value' => $directory['thumb'],
360
  );
361
  $form['delete'] = array(
362
    '#type' => 'checkbox',
363
    '#title' => t('Delete'),
364
    '#default_value' => $directory['delete'],
365
  );
366
  $form['resize'] = array(
367
    '#type' => 'checkbox',
368
    '#title' => t('Resize'),
369
    '#default_value' => $directory['resize'],
370
  );
371
  return $form;
372
}
373
374
/**
375
 * Directorys form themed.
376
 */
377
function imce_directories_theme($variables) {
378
  $form = $variables['form'];
379
  $rows = array();
380
  $root = t('root');
381
382
  foreach (element_children($form) as $key) {
383
    //directory path
384
    $row = array('<div class="container-inline">&lt;' . $root . '&gt;' . '/' . drupal_render($form[$key]['name']) . '</div>' . drupal_render($form[$key]['subnav']));
385
    unset($form[$key]['name'], $form[$key]['subnav']);
386
387
    //permissions
388
    $header = array();
389
    foreach (element_children($form[$key]) as $perm) {
390
      $header[] = $form[$key][$perm]['#title'];
391
      unset($form[$key][$perm]['#title']);
392
      $row[] = drupal_render($form[$key][$perm]);
393
    }
394
395
    $rows[] = $row;
396
  }
397
398
  array_unshift($header, t('Directory path'));
399
400
  $output = '<h3 class="title">' . t('Directories') . '</h3>';
401
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
402
  $output .= '<div class="form-item"><div class="description">' . t('Define directories that users of this profile can access.
403
<ul>
404
	<li>Use alphanumeric characters as directory paths.</li>
405
	<li>To specify file system root, just enter <strong>.</strong>(dot) character.</li>
406
	<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>
407
  <li>To remove a directory from the list, leave the directory path blank.</li>
408
  <li>If you want more flexibility in directory paths you can execute php to return a directory path.<br />
409
  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 />
410
  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 />
411
  Note that you should use the $user variable instead of $GLOBALS[\'user\'] since they are not always the same object.</li>
412
</ul>
413
<p>Note that thumbnails permission does not affect thumbnail creation on upload. See thumbnails decription below.</p>
414
<p>If you need more fields, just fill all and save, and you will get two more on the next page.</p>') . '</div></div>';
415
  $output .= drupal_render_children($form);
416
  return $output;
417
}
418
419
/**
420
 * thumbnail settings form
421
 */
422
function imce_thumbnail_form($thumb = array()) {
423
  if (empty($thumb)) {
424
    $thumb = array('name' => '', 'dimensions' => '', 'prefix' => '', 'suffix' => '');
425
  }
426
  $form['name'] = array(
427
    '#type' => 'textfield',
428
    '#default_value' => $thumb['name'],
429
    '#size' => 20,
430
  );
431
  $form['dimensions'] = array(
432
    '#type' => 'textfield',
433
    '#default_value' => $thumb['dimensions'],
434
    '#size' => 20,
435
  );
436
  $form['prefix'] = array(
437
    '#type' => 'textfield',
438
    '#default_value' => $thumb['prefix'],
439
    '#size' => 20,
440
  );
441
  $form['suffix'] = array(
442
    '#type' => 'textfield',
443
    '#default_value' => $thumb['suffix'],
444
    '#size' => 20,
445
  );
446
  return $form;
447
}
448
449
/**
450
 * Thumbnails form themed.
451
 */
452
function imce_thumbnails_theme($variables) {
453
  $form = $variables['form'];
454
  $header = array(t('Name'), t('Dimensions'), t('Prefix'), t('Suffix'));
455
  $rows = array();
456
457
  foreach (element_children($form) as $key) {
458
    $rows[] = array(
459
      drupal_render($form[$key]['name']),
460
      drupal_render($form[$key]['dimensions']),
461
      drupal_render($form[$key]['prefix']),
462
      drupal_render($form[$key]['suffix']),
463
    );
464
  }
465
466
  $output = '<h3 class="title">' . t('Thumbnails') . '</h3>';
467
  $output .= theme('table', array('header' => $header, 'rows' => $rows));
468
  $output .= '<div class="form-item"><div class="description">' . t('You may create a list of thumbnail options that users can choose from.
469
<ul>
470
  <li>Use alphanumeric characters as thumbnail names.</li>
471
  <li>Specify dimensions as <strong>WidthxHeight</strong>.</li>
472
  <li>Prefix and suffix are strings that are added to original file name to create the thumbnail name.</li>
473
  <li>An example thumbnail: Name = <strong>Small</strong>, Dimensions = <strong>80x80</strong>, Prefix = <strong>small_</strong></li>
474
</ul>
475
<p>Note that users will always be able to create these thumbnails on file upload no matter what the thumbnail permission is.</p>
476
<p>If you need more fields, just fill all and save, and you will get two more on the next page.</p>') . '</div></div>';
477
  $output .= drupal_render_children($form);
478
  return $output;
479
}
480
481
/**
482
 * Role-profile form
483
 */
484
function imce_role_form($role, $weight = TRUE, $core = TRUE) {
485
  $form['name'] = array(
486
    '#markup' => check_plain($role['name']),
487
  );
488
  if ($weight) {
489
    $form['weight'] = $core ? array(
490
      '#type' => 'textfield',
491
      '#value' => $role['weight'],
492
      '#attributes' => array('readonly' => 'readonly', 'style' => 'border: none; width: 2em; background-color: transparent;'),
493
    ) : array(
494
      '#type' => 'weight',
495
      '#default_value' => $role['weight'],
496
    );
497
  }
498
  foreach (array_keys(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE)) as $scheme) {
499
    $form[$scheme . '_pid'] = array(
500
      '#type' => 'select',
501
      '#options' => imce_profile_options(),
502
      '#default_value' => $role[$scheme . '_pid'],
503
      '#empty_value' => 0,
504
    );
505
  }
506
  return $form;
507
}
508
509
/**
510
 * Profile delete form
511
 */
512
function imce_profile_delete_form($form, &$form_state, $pid) {
513
  if ($pid > 1 && $profile = imce_load_profile($pid)) {
514
    $form['#submit'][] = 'imce_profile_delete_submit';
515
    $form['pid'] = array('#type' => 'hidden', '#value' => $pid);
516
    return confirm_form($form,
517
      t('Are you sure you want to delete the profile %name?',
518
      array('%name' => $profile['name'])),
519
      'admin/config/media/imce',
520
      '',
521
      t('Delete'),
522
      t('Cancel')
523
    );
524
  }
525
  drupal_goto('admin/config/media/imce');
526
}
527
528
/**
529
 * Profile delete form submit
530
 */
531
function imce_profile_delete_submit($form, &$form_state) {
532
  imce_update_profiles($form_state['values']['pid'], NULL);
533
  drupal_set_message(t('Profile has been deleted.'));
534
  $form_state['redirect'] = 'admin/config/media/imce';
535
}
536
537
/**
538
 * Profile options.
539
 */
540
function imce_profile_options() {
541
  $options = array();
542
  foreach (variable_get('imce_profiles', array()) as $pid => $profile) {
543
    $options[$pid] = $profile['name'];
544
  }
545
  return $options;
546
}
547
548
/**
549
 * Profile import links.
550
 */
551
function imce_profile_import_html($pid = 0) {
552
  $output = '';
553
  $links = array();
554
555
  foreach (variable_get('imce_profiles', array()) as $id => $profile) {
556
    if ($pid != $id) {
557
      $links[] = l($profile['name'], $_GET['q'], array('query' => array('import' => $id)));
558
    }
559
  }
560
561
  if (!empty($links)) {
562
    $output = '<p><strong>' . t('Import settings from other profiles') . '</strong>: ';
563
    $output .= implode(', ', $links) . '</p>';
564
  }
565
566
  return $output;
567
}
568
569
/**
570
 * Update role-profile assignments.
571
 */
572
function imce_update_roles($pid) {
573
  $roles = variable_get('imce_roles_profiles', array());
574
  foreach ($roles as $rid => $role) {
575
    foreach ($role as $key => $value) {
576
      if (substr($key, -4) == '_pid') {
577
        if ($value == $pid) {
578
          $roles[$rid][$key] = 0;
579
        }
580
        elseif ($value > $pid) {
581
          $roles[$rid][$key]--;
582
        }
583
      }
584
    }
585
  }
586
  variable_set('imce_roles_profiles', $roles);
587
}
588
589
/**
590
 * Add, update or delete a profile.
591
 */
592
function imce_update_profiles($pid, $profile = NULL) {
593
  $profiles = variable_get('imce_profiles', array());
594
595
  //add or update
596
  if (isset($profile)) {
597
    $pid = isset($profiles[$pid]) ? $pid : count($profiles)+1;
598
    $profiles[$pid] = $profile;
599
  }
600
601
  //delete
602
  elseif (isset($profiles[$pid]) && $pid > 1) {
603
    unset($profiles[$pid]);
604
    for ($i = $pid+1; isset($profiles[$i]); $i++) {
605
      $profiles[$i-1] = $profiles[$i];
606
      unset($profiles[$i]);
607
    }
608
    imce_update_roles($pid);
609
  }
610
611
  variable_set('imce_profiles', $profiles);
612
  return $pid;
613
}
614
615
/**
616
 * Unset empty fields in thumbnails and directory paths.
617
 */
618
function imce_clean_profile_fields(&$profile) {
619
  $clean = array();
620
  foreach ($profile['thumbnails'] as $thumb) {
621
    if (trim($thumb['name']) != '') {
622
      $clean[] = $thumb;
623
    }
624
  }
625
  $profile['thumbnails'] = $clean;
626
627
  $clean = array();
628
  $names = array();
629
  foreach ($profile['directories'] as $dir) {
630
    $dir['name'] = trim($dir['name'], '/ ');
631
    if ($dir['name'] == '') {
632
      continue;
633
    }
634
    if (isset($names[$dir['name']])) {
635
      drupal_set_message(t('Duplicate directory paths are not allowed.'), 'error');
636
      continue;
637
    }
638
    if (!imce_reg_dir($dir['name'])) {
639
      drupal_set_message(t('%dirname is not accepted as a proper directory name.', array('%dirname' => $dir['name'])), 'error');
640
      continue;
641
    }
642
    $clean[] = $dir;
643
    $names[$dir['name']] = 1;
644
  }
645
  $profile['directories'] = $clean;
646
}
647
648
/**
649
 * Profile load.
650
 */
651
function imce_load_profile($pid) {
652
  $profiles = variable_get('imce_profiles', array());
653
  return isset($profiles[$pid]) ? $profiles[$pid] : NULL;
654
}
655
656
/**
657
 * Sort roles according to their weights.
658
 */
659
function imce_sorted_roles() {
660
  static $sorted;
661
  if (!isset($sorted)) {
662
    $sorted = array();
663
    $roles = user_roles();
664
    $profiles = variable_get('imce_profiles', array());
665
    $roles_profiles = variable_get('imce_roles_profiles', array());
666
    $roles_profiles[DRUPAL_ANONYMOUS_RID]['weight'] = 12;
667
    $roles_profiles[DRUPAL_AUTHENTICATED_RID]['weight'] = 11;
668
    $schemes = array_keys(file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE));
669
    foreach ($roles as $rid => $name) {
670
      $sorted[$rid] = array(
671
        'name' => $name,
672
        'weight' => isset($roles_profiles[$rid]['weight']) ? $roles_profiles[$rid]['weight'] : 0
673
      );
674
      foreach ($schemes as $scheme) {
675
        $key = $scheme . '_pid';
676
        $sorted[$rid][$key] = isset($roles_profiles[$rid][$key]) && isset($profiles[$roles_profiles[$rid][$key]]) ? $roles_profiles[$rid][$key] : 0;
677
      }
678
    }
679
    uasort($sorted, 'imce_rolesort');
680
  }
681
  return $sorted;
682
}
683
684
/**
685
 * Sorting function for roles.
686
 */
687
function imce_rolesort($r1, $r2) {
688
  return $r1['weight']-$r2['weight'];
689
}
690
691
//Include core profile functions.
692
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'imce') . '/inc/imce.core.profiles.inc';