archive.action.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @file
  4. * Provides an action for creating a zip archive of selected files.
  5. * An entry in the {file_managed} table is created for the newly created archive,
  6. * and it is marked as permanent or temporary based on the operation settings.
  7. */
  8. function views_bulk_operations_archive_action_info() {
  9. $actions = array();
  10. if (function_exists('zip_open')) {
  11. $actions['views_bulk_operations_archive_action'] = array(
  12. 'type' => 'file',
  13. 'label' => t('Create an archive of selected files'),
  14. // This action only works when invoked through VBO. That's why it's
  15. // declared as non-configurable to prevent it from being shown in the
  16. // "Create an advanced action" dropdown on admin/config/system/actions.
  17. 'configurable' => FALSE,
  18. 'vbo_configurable' => TRUE,
  19. 'triggers' => array('any'),
  20. );
  21. }
  22. return $actions;
  23. }
  24. /**
  25. * Since Drupal's Archiver doesn't abstract properly the archivers it implements
  26. * (Archive_Tar and ZipArchive), it can't be used here.
  27. */
  28. function views_bulk_operations_archive_action($file, $context) {
  29. global $user;
  30. static $archive_contents = array();
  31. // Adding a non-existent file to the archive crashes ZipArchive on close().
  32. if (file_exists($file->uri)) {
  33. $destination = $context['destination'];
  34. $zip = new ZipArchive();
  35. // If the archive already exists, open it. If not, create it.
  36. if (file_exists($destination)) {
  37. $opened = $zip->open(drupal_realpath($destination));
  38. }
  39. else {
  40. $opened = $zip->open(drupal_realpath($destination), ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
  41. }
  42. if ($opened) {
  43. // Create a list of all files in the archive. Used for duplicate checking.
  44. if (empty($archive_contents)) {
  45. for ($i = 0; $i < $zip->numFiles; $i++) {
  46. $archive_contents[] = $zip->getNameIndex($i);
  47. }
  48. }
  49. // Make sure that the target filename is unique.
  50. $filename = _views_bulk_operations_archive_action_create_filename(basename($file->uri), $archive_contents);
  51. // Note that the actual addition happens on close(), hence the need
  52. // to open / close the archive each time the action runs.
  53. $zip->addFile(drupal_realpath($file->uri), $filename);
  54. $zip->close();
  55. $archive_contents[] = $filename;
  56. }
  57. }
  58. // The operation is complete, create a file entity and provide a download
  59. // link to the user.
  60. if ($context['progress']['current'] == $context['progress']['total']) {
  61. $archive_file = new stdClass();
  62. $archive_file->uri = $destination;
  63. $archive_file->filename = basename($destination);
  64. $archive_file->filemime = file_get_mimetype($destination);
  65. $archive_file->uid = $user->uid;
  66. $archive_file->status = $context['settings']['temporary'] ? FALSE : FILE_STATUS_PERMANENT;
  67. file_save($archive_file);
  68. $url = file_create_url($archive_file->uri);
  69. $url = l($url, $url, array('absolute' => TRUE));
  70. _views_bulk_operations_log(t('An archive has been created and can be downloaded from: !url', array('!url' => $url)));
  71. }
  72. }
  73. /**
  74. * Configuration form shown to the user before the action gets executed.
  75. */
  76. function views_bulk_operations_archive_action_form($context) {
  77. // Pass the scheme as a value, so that the submit callback can access it.
  78. $form['scheme'] = array(
  79. '#type' => 'value',
  80. '#value' => $context['settings']['scheme'],
  81. );
  82. $form['filename'] = array(
  83. '#type' => 'textfield',
  84. '#title' => t('Filename'),
  85. '#default_value' => 'vbo_archive_' . date('Ymd'),
  86. '#field_suffix' => '.zip',
  87. '#description' => t('The name of the archive file.'),
  88. );
  89. return $form;
  90. }
  91. /**
  92. * Assembles a sanitized and unique URI for the archive, and returns it for
  93. * usage by the action callback (views_bulk_operations_archive_action).
  94. */
  95. function views_bulk_operations_archive_action_submit($form, $form_state) {
  96. // Validate the scheme, fallback to public if it's somehow invalid.
  97. $scheme = $form_state['values']['scheme'];
  98. if (!file_stream_wrapper_valid_scheme($scheme)) {
  99. $scheme = 'public';
  100. }
  101. $destination = $scheme . '://' . basename($form_state['values']['filename']) . '.zip';
  102. // If the chosen filename already exists, file_destination() will append
  103. // an integer to it in order to make it unique.
  104. $destination = file_destination($destination, FILE_EXISTS_RENAME);
  105. return array(
  106. 'destination' => $destination,
  107. );
  108. }
  109. /**
  110. * Settings form (embedded into the VBO field settings in the Views UI).
  111. */
  112. function views_bulk_operations_archive_action_views_bulk_operations_form($options) {
  113. $scheme_options = array();
  114. foreach (file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL_NORMAL) as $scheme => $stream_wrapper) {
  115. $scheme_options[$scheme] = $stream_wrapper['name'];
  116. }
  117. if (count($scheme_options) > 1) {
  118. $form['scheme'] = array(
  119. '#type' => 'radios',
  120. '#title' => t('Storage'),
  121. '#options' => $scheme_options,
  122. '#default_value' => !empty($options['scheme']) ? $options['scheme'] : variable_get('file_default_scheme', 'public'),
  123. '#description' => t('Select where the archive should be stored. Private file storage has significantly more overhead than public files, but allows restricted access.'),
  124. );
  125. }
  126. else {
  127. $scheme_option_keys = array_keys($scheme_options);
  128. $form['scheme'] = array(
  129. '#type' => 'value',
  130. '#value' => reset($scheme_option_keys),
  131. );
  132. }
  133. $form['temporary'] = array(
  134. '#type' => 'checkbox',
  135. '#title' => t('Temporary'),
  136. '#default_value' => isset($options['temporary']) ? $options['temporary'] : TRUE,
  137. '#description' => t('Temporary files older than 6 hours are removed when cron runs.'),
  138. );
  139. return $form;
  140. }
  141. /**
  142. * Create a sanitized and unique version of the provided filename.
  143. *
  144. * @param $filename
  145. * String filename
  146. *
  147. * @return
  148. * The new filename.
  149. */
  150. function _views_bulk_operations_archive_action_create_filename($filename, $archive_list) {
  151. // Strip control characters (ASCII value < 32). Though these are allowed in
  152. // some filesystems, not many applications handle them well.
  153. $filename = preg_replace('/[\x00-\x1F]/u', '_', $filename);
  154. if (substr(PHP_OS, 0, 3) == 'WIN') {
  155. // These characters are not allowed in Windows filenames
  156. $filename = str_replace(array(':', '*', '?', '"', '<', '>', '|'), '_', $filename);
  157. }
  158. if (in_array($filename, $archive_list)) {
  159. // Destination file already exists, generate an alternative.
  160. $pos = strrpos($filename, '.');
  161. if ($pos !== FALSE) {
  162. $name = substr($filename, 0, $pos);
  163. $ext = substr($filename, $pos);
  164. }
  165. else {
  166. $name = $filename;
  167. $ext = '';
  168. }
  169. $counter = 0;
  170. do {
  171. $filename = $name . '_' . $counter++ . $ext;
  172. } while (in_array($filename, $archive_list));
  173. }
  174. return $filename;
  175. }