archive.action.inc 6.7 KB

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