imce.install 4.0 KB

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