imce.install 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @file
  4. * Installs, updates, and uninstalls IMCE.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function imce_install() {
  10. module_load_include('inc', 'imce', 'inc/imce.core.profiles');
  11. imce_install_profiles();
  12. }
  13. /**
  14. * Implements hook_uninstall().
  15. */
  16. function imce_uninstall() {
  17. db_delete('file_usage')->condition('module', 'imce')->execute();
  18. variable_del('imce_profiles');
  19. variable_del('imce_roles_profiles');
  20. variable_del('imce_settings_textarea');
  21. variable_del('imce_settings_absurls');
  22. variable_del('imce_settings_replace');
  23. variable_del('imce_settings_thumb_method');
  24. variable_del('imce_settings_disable_private');
  25. variable_del('imce_settings_admin_theme');
  26. variable_del('imce_custom_content');
  27. variable_del('imce_custom_process');
  28. variable_del('imce_custom_init');
  29. variable_del('imce_custom_scan');
  30. variable_del('imce_custom_response');
  31. }
  32. /**
  33. * Updates from 6.x to 7.x.
  34. */
  35. function imce_update_7000() {
  36. // Update role-profile assignments
  37. $roles_profiles = variable_get('imce_roles_profiles', array());
  38. if (!empty($roles_profiles)) {
  39. $scheme = variable_get('file_default_scheme', 'public');
  40. foreach ($roles_profiles as $rid => &$role) {
  41. $role[$scheme . '_pid'] = $role['pid'];
  42. unset($role['pid']);
  43. }
  44. variable_set('imce_roles_profiles', $roles_profiles);
  45. }
  46. // Update textarea ids
  47. $ids = str_replace(' ', '', variable_get('imce_settings_textarea', ''));
  48. if ($ids != '') {
  49. $ids = explode(',', $ids);
  50. foreach ($ids as &$id) {
  51. $id .= '*';
  52. }
  53. variable_set('imce_settings_textarea', implode(', ', $ids));
  54. }
  55. }
  56. /**
  57. * Migrates imce files from {files} to {file_managed}.
  58. * Removes {imce_files} in favor of {file_usage}.
  59. */
  60. function imce_update_7001(&$sandbox) {
  61. if (!db_table_exists('imce_files') || !db_table_exists('files')) {
  62. return;
  63. }
  64. // Initiate progress
  65. if (!isset($sandbox['progress'])) {
  66. $sandbox['progress'] = 0;
  67. $sandbox['last_fid_processed'] = 0;
  68. $sandbox['max'] = db_query("SELECT COUNT(*) FROM {imce_files} i INNER JOIN {files} f ON i.fid = f.fid")->fetchField();
  69. }
  70. // Prepare variables
  71. $limit = 250;
  72. $basedir = variable_get('file_directory_path', conf_path() . '/files') . '/';
  73. $baselen = strlen($basedir);
  74. $scheme = file_default_scheme() . '://';
  75. $result = db_query_range('SELECT f.* FROM {imce_files} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.fid > :fid ORDER BY i.fid', 0, $limit, array(':fid' => $sandbox['last_fid_processed']))->fetchAll();
  76. // Migrate imce files from {files} to {file_managed}
  77. foreach ($result as $file) {
  78. $relpath = substr($file->filepath, 0, $baselen) == $basedir ? substr($file->filepath, $baselen) : $file->filepath;
  79. $file->uri = file_stream_wrapper_uri_normalize($scheme . $relpath);
  80. unset($file->filepath);
  81. if (!db_query("SELECT 1 FROM {file_managed} WHERE fid = :fid", array(':fid' => $file->fid))->fetchField()) {
  82. // Check duplicate uri
  83. if ($fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :uri", array(':uri' => $file->uri))->fetchField()) {
  84. $file->fid = $fid;
  85. }
  86. else {
  87. drupal_write_record('file_managed', $file);
  88. }
  89. }
  90. file_usage_add($file, 'imce', 'file', $file->fid);
  91. $sandbox['progress']++;
  92. $sandbox['last_fid_processed'] = $file->fid;
  93. }
  94. // Drop {imce_files} if the progress is complete.
  95. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  96. if ($sandbox['#finished'] >= 1) {
  97. db_drop_table('imce_files');
  98. return t('Migrated IMCE files.');
  99. }
  100. }
  101. /**
  102. * Fixes misconfigurations where anonymous user is given User-1 profile
  103. */
  104. function imce_update_7002() {
  105. $roles = variable_get('imce_roles_profiles', array());
  106. $rid = DRUPAL_ANONYMOUS_RID;
  107. if (!empty($roles[$rid])) {
  108. $update = FALSE;
  109. foreach ($roles[$rid] as $key => $value) {
  110. if ($value == 1 && substr($key, -4) == '_pid') {
  111. $roles[$rid][$key] = '0';
  112. $update = TRUE;
  113. }
  114. }
  115. if ($update) {
  116. variable_set('imce_roles_profiles', $roles);
  117. }
  118. }
  119. }