Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     $file = $row->getSourceProperty($this->configuration['source_path_property']);
     $destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
     $source = $this->configuration['source_base_path'] . $file;
     // Ensure the source file exists, if it's a local URI or path.
     if ($this->isLocalUri($source) && !file_exists($source)) {
         throw new MigrateException(SafeMarkup::format('File @source does not exist.', ['@source' => $source]));
     }
     // If the start and end file is exactly the same, there is nothing to do.
     if ($this->isLocationUnchanged($source, $destination)) {
         return parent::import($row, $old_destination_id_values);
     }
     $replace = $this->getOverwriteMode($row);
     $success = $this->writeFile($source, $destination, $replace);
     if (!$success) {
         $dir = $this->getDirectory($destination);
         if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
             $success = $this->writeFile($source, $destination, $replace);
         } else {
             throw new MigrateException(SafeMarkup::format('Could not create directory @dir', ['@dir' => $dir]));
         }
     }
     if ($success) {
         return parent::import($row, $old_destination_id_values);
     } else {
         throw new MigrateException(SafeMarkup::format('File %source could not be copied to %destination.', ['%source' => $source, '%destination' => $destination]));
     }
 }
 /**
  * Check that the directory exists and is writable, creating it if needed.
  *
  * @throws
  *  Exception
  */
 function prepareDirectory($directory)
 {
     $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
     if (!$status) {
         throw new \Exception();
     }
 }
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     $source = $this->configuration['source_base_path'] . $row->getSourceProperty($this->configuration['source_path_property']);
     $destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
     $replace = FILE_EXISTS_REPLACE;
     if (!empty($this->configuration['rename'])) {
         $entity_id = $row->getDestinationProperty($this->getKey('id'));
         if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
             $replace = FILE_EXISTS_RENAME;
         }
     }
     $dirname = drupal_dirname($destination);
     if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) {
         throw new MigrateException(t('Could not create directory %dirname', array('%dirname' => $dirname)));
     }
     if ($this->configuration['move']) {
         $copied = file_unmanaged_move($source, $destination, $replace);
     } else {
         // Determine whether we can perform this operation based on overwrite rules.
         $original_destination = $destination;
         $destination = file_destination($destination, $replace);
         if ($destination === FALSE) {
             throw new MigrateException(t('File %file could not be copied because a file by that name already exists in the destination directory (%destination)', array('%file' => $source, '%destination' => $original_destination)));
         }
         $source = $this->urlencode($source);
         $copied = copy($source, $destination);
     }
     if ($copied) {
         return parent::import($row, $old_destination_id_values);
     } else {
         throw new MigrateException(t('File %source could not be copied to %destination.', array('%source' => $source, '%destination' => $destination)));
     }
 }
Exemple #4
0
 /**
  * {@inheritdoc}
  */
 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property)
 {
     // If we're stubbing a file entity, return a uri of NULL so it will get
     // stubbed by the general process.
     if ($row->isStub()) {
         return NULL;
     }
     list($source, $destination) = $value;
     // Modify the destination filename if necessary.
     $replace = !empty($this->configuration['rename']) ? FILE_EXISTS_RENAME : FILE_EXISTS_REPLACE;
     $final_destination = file_destination($destination, $replace);
     // Try opening the file first, to avoid calling file_prepare_directory()
     // unnecessarily. We're suppressing fopen() errors because we want to try
     // to prepare the directory before we give up and fail.
     $destination_stream = @fopen($final_destination, 'w');
     if (!$destination_stream) {
         // If fopen didn't work, make sure there's a writable directory in place.
         $dir = $this->fileSystem->dirname($final_destination);
         if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
             throw new MigrateException("Could not create or write to directory '{$dir}'");
         }
         // Let's try that fopen again.
         $destination_stream = @fopen($final_destination, 'w');
         if (!$destination_stream) {
             throw new MigrateException("Could not write to file '{$final_destination}'");
         }
     }
     // Stream the request body directly to the final destination stream.
     $this->configuration['guzzle_options']['sink'] = $destination_stream;
     // Make the request. Guzzle throws an exception for anything other than 200.
     $this->httpClient->get($source, $this->configuration['guzzle_options']);
     return $final_destination;
 }
Exemple #5
0
 /**
  * Test directory handling functions.
  */
 function testFileCheckDirectoryHandling()
 {
     // A directory to operate on.
     $directory = file_default_scheme() . '://' . $this->randomMachineName() . '/' . $this->randomMachineName();
     $this->assertFalse(is_dir($directory), 'Directory does not exist prior to testing.');
     // Non-existent directory.
     $this->assertFalse(file_prepare_directory($directory, 0), 'Error reported for non-existing directory.', 'File');
     // Make a directory.
     $this->assertTrue(file_prepare_directory($directory, FILE_CREATE_DIRECTORY), 'No error reported when creating a new directory.', 'File');
     // Make sure directory actually exists.
     $this->assertTrue(is_dir($directory), 'Directory actually exists.', 'File');
     if (substr(PHP_OS, 0, 3) != 'WIN') {
         // PHP on Windows doesn't support any kind of useful read-only mode for
         // directories. When executing a chmod() on a directory, PHP only sets the
         // read-only flag, which doesn't prevent files to actually be written
         // in the directory on any recent version of Windows.
         // Make directory read only.
         @drupal_chmod($directory, 0444);
         $this->assertFalse(file_prepare_directory($directory, 0), 'Error reported for a non-writeable directory.', 'File');
         // Test directory permission modification.
         $this->setSetting('file_chmod_directory', 0777);
         $this->assertTrue(file_prepare_directory($directory, FILE_MODIFY_PERMISSIONS), 'No error reported when making directory writeable.', 'File');
     }
     // Test that the directory has the correct permissions.
     $this->assertDirectoryPermissions($directory, 0777, 'file_chmod_directory setting is respected.');
     // Remove .htaccess file to then test that it gets re-created.
     @drupal_unlink(file_default_scheme() . '://.htaccess');
     $this->assertFalse(is_file(file_default_scheme() . '://.htaccess'), 'Successfully removed the .htaccess file in the files directory.', 'File');
     file_ensure_htaccess();
     $this->assertTrue(is_file(file_default_scheme() . '://.htaccess'), 'Successfully re-created the .htaccess file in the files directory.', 'File');
     // Verify contents of .htaccess file.
     $file = file_get_contents(file_default_scheme() . '://.htaccess');
     $this->assertEqual($file, FileStorage::htaccessLines(FALSE), 'The .htaccess file contains the proper content.', 'File');
 }
 /**
  * Tests if the module cleans up the disk on uninstall.
  */
 public function testPiwikUninstall()
 {
     $cache_path = 'public://piwik';
     $site_id = '1';
     $this->config('piwik.settings')->set('site_id', $site_id)->save();
     $this->config('piwik.settings')->set('url_http', 'http://www.example.com/piwik/')->save();
     $this->config('piwik.settings')->set('url_https', 'https://www.example.com/piwik/')->save();
     // Enable local caching of piwik.js
     $this->config('piwik.settings')->set('cache', 1)->save();
     // Load front page to get the piwik.js downloaded into local cache. But
     // loading the piwik.js is not possible as "url_http" is a test dummy only.
     // Create a dummy file to complete the rest of the tests.
     file_prepare_directory($cache_path, FILE_CREATE_DIRECTORY);
     file_unmanaged_save_data($this->randomMachineName(16), $cache_path . '/piwik.js');
     // Test if the directory and piwik.js exists.
     $this->assertTrue(file_prepare_directory($cache_path), 'Cache directory "public://piwik" has been found.');
     $this->assertTrue(file_exists($cache_path . '/piwik.js'), 'Cached piwik.js tracking file has been found.');
     // Uninstall the module.
     $edit = [];
     $edit['uninstall[piwik]'] = TRUE;
     $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
     $this->assertNoText(\Drupal::translation()->translate('Configuration deletions'), 'No configuration deletions listed on the module install confirmation page.');
     $this->drupalPostForm(NULL, NULL, t('Uninstall'));
     $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
     // Test if the directory and all files have been removed.
     $this->assertFalse(file_scan_directory($cache_path, '/.*/'), 'Cached JavaScript files have been removed.');
     $this->assertFalse(file_prepare_directory($cache_path), 'Cache directory "public://piwik" has been removed.');
 }
 public function generateValues($object, $instance, $plugin_definition, $form_display_options)
 {
     static $file;
     $settings = $instance->getFieldSettings();
     if (empty($file)) {
         if ($path = $this->generateTextFile()) {
             $source = new stdClass();
             $source->uri = $path;
             $source->uid = 1;
             // TODO: randomize? use case specific.
             $source->filemime = 'text/plain';
             $source->filename = drupal_basename($path);
             $destination_dir = $settings['uri_scheme'] . '://' . $settings['file_directory'];
             file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
             $destination = $destination_dir . '/' . basename($path);
             $file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
         } else {
             return FALSE;
         }
     }
     if (!$file) {
         // In case a previous file operation failed or no file is set, return FALSE
         return FALSE;
     } else {
         $object_field['target_id'] = $file->id();
         $object_field['display'] = $settings['display_default'];
         $object_field['description'] = DevelGenerateBase::createGreeking(10);
         return $object_field;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     $file_upload = $this->getRequest()->files->get('files[import_tarball]', NULL, TRUE);
     $has_upload = FALSE;
     if ($file_upload && $file_upload->isValid()) {
         // The sync directory must be empty if we are doing an upload.
         $form_state->setValue('import_tarball', $file_upload->getRealPath());
         $has_upload = TRUE;
     }
     $sync_directory = $form_state->getValue('sync_directory');
     // If we've customised the sync directory ensure its good to go.
     if ($sync_directory != config_get_config_directory(CONFIG_SYNC_DIRECTORY)) {
         // Ensure it exists and is writeable.
         if (!file_prepare_directory($sync_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
             $form_state->setErrorByName('sync_directory', t('The directory %directory could not be created or could not be made writable. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see the <a href="@handbook_url">online handbook</a>.', array('%directory' => $sync_directory, '@handbook_url' => 'http://drupal.org/server-permissions')));
         }
     }
     // If no tarball ensure we have files.
     if (!$form_state->hasAnyErrors() && !$has_upload) {
         $sync = new FileStorage($sync_directory);
         if (count($sync->listAll()) === 0) {
             $form_state->setErrorByName('sync_directory', t('No file upload provided and the sync directory is empty'));
         }
     }
 }
 /**
  * Check that the directory exists and is writable, creating it if needed.
  *
  * @throws
  *  \ModuleBuilder\Exception
  */
 function prepareDirectory($directory)
 {
     $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
     if (!$status) {
         throw new \ModuleBuilder\Exception("The hooks directory cannot be created or is not writable.");
     }
 }
 /**
  * Check that the directory can be used for backup.
  *
  * @throws \BackupMigrate\Core\Exception\BackupMigrateException
  */
 protected function checkDirectory()
 {
     // @TODO: Figure out if the file is or might be accessible via the web.
     $dir = $this->confGet('directory');
     $is_private = strpos($dir, 'private://') === 0;
     // Attempt to create/prepare the directory if it is in the private directory
     if ($is_private) {
         if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY && FILE_MODIFY_PERMISSIONS)) {
             throw new BackupMigrateException("The backup file could not be saved to '%dir' because the directory could not be created or cannot be written to. Please make sure your private files directory is writable by the web server.", ['%dir' => $dir]);
         }
     } else {
         // If the file is local to the server.
         $real = \Drupal::service('file_system')->realpath($dir);
         if ($real) {
             // If the file is within the docroot.
             $in_root = strpos($real, DRUPAL_ROOT) === 0;
             if ($in_root && !$is_private) {
                 throw new BackupMigrateException("The backup file could not be saved to '%dir' because that directory may be publicly accessible via the web. Please save your backups to the private file directory or a directory outside of the web root.", ['%dir' => $dir]);
             }
         }
     }
     // Do the regular exists/writable checks
     parent::checkDirectory();
     // @TODO: Warn if the realpath cannot be resolved (because we cannot determine if the file is publicly accessible)
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     // Process the upload and perform validation. Note: we're using the
     // form value for the $replace parameter.
     if (!$form_state->isValueEmpty('file_subdir')) {
         $destination = 'temporary://' . $form_state->getValue('file_subdir');
         file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
     } else {
         $destination = FALSE;
     }
     // Setup validators.
     $validators = array();
     if ($form_state->getValue('is_image_file')) {
         $validators['file_validate_is_image'] = array();
     }
     if ($form_state->getValue('allow_all_extensions')) {
         $validators['file_validate_extensions'] = array();
     } elseif (!$form_state->isValueEmpty('extensions')) {
         $validators['file_validate_extensions'] = array($form_state->getValue('extensions'));
     }
     $file = file_save_upload('file_test_upload', $validators, $destination, 0, $form_state->getValue('file_test_replace'));
     if ($file) {
         $form_state->setValue('file_test_upload', $file);
         drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->getFileUri())));
         drupal_set_message(t('File name is @filename.', array('@filename' => $file->getFilename())));
         drupal_set_message(t('File MIME type is @mimetype.', array('@mimetype' => $file->getMimeType())));
         drupal_set_message(t('You WIN!'));
     } elseif ($file === FALSE) {
         drupal_set_message(t('Epic upload FAIL!'), 'error');
     }
 }
Exemple #12
0
 public static function fetch_url($url, $cache = true, $cache_fn = false, $reset = false)
 {
     if ($cache) {
         if ($cache === true && $cache_fn === false) {
             $cache_fn = 'public://cache/';
         } else {
             $cache_fn = $cache;
         }
         if (DT::endsWith($cache_fn, '/')) {
             $cache_path = $cache_fn;
             $cache_fn = $cache_path . DT::sanitize($url);
         } else {
             $cache_path = dirname($cache_fn) . '/';
         }
         file_prepare_directory($cache_path, FILE_CREATE_DIRECTORY);
         if (!file_exists($cache_fn) || $reset) {
             $data = DT::_fetch_url_data($url);
             file_put_contents($cache_fn, $data);
         } else {
             $data = file_get_contents($cache_fn);
         }
     } else {
         $data = DT::_fetch_url_data($url);
     }
     return $data;
 }
Exemple #13
0
 /**
  * {@inheritdoc}
  *
  * The file name for the CSS or JS cache file is generated from the hash of
  * the aggregated contents of the files in $data. This forces proxies and
  * browsers to download new CSS when the CSS changes.
  */
 public function dump($data, $file_extension)
 {
     // Prefix filename to prevent blocking by firewalls which reject files
     // starting with "ad*".
     $filename = $file_extension . '_' . Crypt::hashBase64($data) . '.' . $file_extension;
     // Create the css/ or js/ path within the files folder.
     $path = 'public://' . $file_extension;
     $uri = $path . '/' . $filename;
     // Create the CSS or JS file.
     file_prepare_directory($path, FILE_CREATE_DIRECTORY);
     if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
         return FALSE;
     }
     // If CSS/JS gzip compression is enabled and the zlib extension is available
     // then create a gzipped version of this file. This file is served
     // conditionally to browsers that accept gzip using .htaccess rules.
     // It's possible that the rewrite rules in .htaccess aren't working on this
     // server, but there's no harm (other than the time spent generating the
     // file) in generating the file anyway. Sites on servers where rewrite rules
     // aren't working can set css.gzip to FALSE in order to skip
     // generating a file that won't be used.
     if (extension_loaded('zlib') && \Drupal::config('system.performance')->get($file_extension . '.gzip')) {
         if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
             return FALSE;
         }
     }
     return $uri;
 }
 /**
  * {@inheritdoc}
  */
 public function directoryPrepare($file_path)
 {
     $directory_path = implode('/', $file_path);
     if (!file_exists($directory_path)) {
         file_prepare_directory($directory_path, FILE_CREATE_DIRECTORY);
     }
     return $directory_path;
 }
 public function generateImage($object, $field, $instance, $bundle)
 {
     $object_field = array();
     static $available_images = array();
     if (empty($available_images)) {
         $available_images = $this->getImages();
     }
     if (empty($available_images)) {
         $args = func_get_args();
         return call_user_func_array('_image_devel_generate', $args);
     }
     $extension = array_rand(array('jpg' => 'jpg', 'png' => 'png'));
     $min_resolution = empty($instance['settings']['min_resolution']) ? '100x100' : $instance['settings']['min_resolution'];
     $max_resolution = empty($instance['settings']['max_resolution']) ? '600x600' : $instance['settings']['max_resolution'];
     if (FALSE === ($tmp_file = drupal_tempnam('temporary://', 'imagefield_'))) {
         return FALSE;
     }
     $destination = $tmp_file . '.' . $extension;
     file_unmanaged_move($tmp_file, $destination, FILE_EXISTS_REPLACE);
     $rand_file = array_rand($available_images);
     if (!empty($instance['settings']['file_directory'])) {
         $instance['settings']['file_directory'] = $instance['settings']['file_directory'] . '/';
     }
     $destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
     file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
     if ($this->settings['devel_image_no_alter']) {
         $file = $available_images[$rand_file];
         $file = file_copy($file, $destination_dir);
     } else {
         $image = image_load($rand_file);
         $min = explode('x', $min_resolution);
         $max = explode('x', $max_resolution);
         $max[0] = $max[0] < $min[0] ? $min[0] : $max[0];
         $max[1] = $max[1] < $min[1] ? $min[1] : $max[1];
         $width = rand((int) $min[0], (int) $max[0]);
         $height = rand((int) $min[1], (int) $max[1]);
         if (!image_scale_and_crop($image, $width, $height)) {
             return FALSE;
         }
         // Use destination image type.
         $image->info['extension'] = $extension;
         if (!image_save($image, $destination)) {
             return FALSE;
         }
         $source = new stdClass();
         $source->uri = $destination;
         $source->uid = 1;
         // TODO: randomize? Use case specific.
         $source->filemime = $image->info['mime_type'];
         $source->filename = drupal_basename($image->source);
         $destination = $destination_dir . basename($destination);
         $file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
     }
     $object_field['fid'] = $file->fid;
     $object_field['alt'] = devel_create_greeking(4);
     $object_field['title'] = devel_create_greeking(4);
     return $object_field;
 }
Exemple #16
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $this->config('expo.settings')->set('ns.public318', $form_state->getValue(['ns', 'public318']))->save();
     $schema_base = $form_state->getValue(['schema', 'base']);
     $schema_json = $schema_base . $form_state->getValue(['schema', 'json']);
     $schema_media = $schema_base . $form_state->getValue(['schema', 'media']);
     file_prepare_directory($schema_json, FILE_CREATE_DIRECTORY);
     file_prepare_directory($schema_media, FILE_CREATE_DIRECTORY);
 }
 /**
  * Prepares temporary destination folder for uploaded files.
  *
  * @return bool
  *   TRUE if destination folder looks OK and FALSE otherwise.
  *
  * @throws \Drupal\plupload\UploadException
  */
 protected function prepareTemporaryUploadDestination()
 {
     $writable = file_prepare_directory($this->temporaryUploadLocation, FILE_CREATE_DIRECTORY);
     if (!$writable) {
         throw new UploadException(UploadException::DESTINATION_FOLDER_ERROR);
     }
     // Try to make sure this is private via htaccess.
     file_save_htaccess($this->temporaryUploadLocation, TRUE);
 }
Exemple #18
0
 /**
  * Helper method for saving requests coming to POETRY mock.
  *
  * @param string $message
  *    XML request.
  */
 public static function saveTranslationRequest($message, $reference)
 {
     $path = TMGMT_POETRY_MOCK_REQUESTS_PATH . $reference . '.xml';
     $dirname = dirname($path);
     if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
         file_save_data($message, $path);
     } else {
         watchdog('poetry_mock', 'Unable to prepare requests directory', array(), WATCHDOG_ERROR);
     }
 }
 public function generateImage($object, $field, $instance, $bundle)
 {
     static $images = array();
     $min_resolution = empty($instance['settings']['min_resolution']) ? '100x100' : $instance['settings']['min_resolution'];
     $max_resolution = empty($instance['settings']['max_resolution']) ? '600x600' : $instance['settings']['max_resolution'];
     $extension = 'jpg';
     if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= DEVEL_GENERATE_IMAGE_MAX) {
         if ($tmp_file = drupal_tempnam('temporary://', 'imagefield_')) {
             $destination = $tmp_file . '.' . $extension;
             file_unmanaged_move($tmp_file, $destination, FILE_EXISTS_REPLACE);
             $min = explode('x', $min_resolution);
             $max = explode('x', $max_resolution);
             $max[0] = $max[0] < $min[0] ? $min[0] : $max[0];
             $max[1] = $max[1] < $min[1] ? $min[1] : $max[1];
             $width = rand((int) $min[0], (int) $max[0]);
             $height = rand((int) $min[1], (int) $max[1]);
             $gray = isset($this->settings['devel_image_provider_gray']) ? $this->settings['devel_image_provider_gray'] : NULL;
             $tags = isset($this->settings['devel_image_provider_tags']) ? $this->settings['devel_image_provider_tags'] : NULL;
             $url = "{$this->provider_base_url}/{$width}/{$height}";
             if (!empty($tags)) {
                 $url .= '/' . $tags;
             }
             $url = $gray ? $url . '/bw' : $url;
             // Generate seed value.
             $seed = isset($this->settings['devel_image_provider_seed']) ? $this->settings['devel_image_provider_seed'] : NULL;
             $rand_value = rand(0, $seed);
             $url .= '/' . $rand_value;
             $method = isset($this->settings['devel_image_provider_get_method']) ? $this->settings['devel_image_provider_get_method'] : 'file_get_contents';
             $path = devel_image_provider_get_file($url, $destination, $method);
             $source = new stdClass();
             $source->uri = $path;
             $source->uid = 1;
             // TODO: randomize? Use case specific.
             $source->filemime = 'image/jpg';
             if (!empty($instance['settings']['file_directory'])) {
                 $instance['settings']['file_directory'] = $instance['settings']['file_directory'] . '/';
             }
             $destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
             file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
             $destination = $destination_dir . basename($path);
             $file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
         } else {
             return FALSE;
         }
     } else {
         // Select one of the images we've already generated for this field.
         $file = new stdClass();
         $file->fid = array_rand($images[$extension][$min_resolution][$max_resolution]);
     }
     $object_field['fid'] = $file->fid;
     $object_field['alt'] = devel_create_greeking(4);
     $object_field['title'] = devel_create_greeking(4);
     return $object_field;
 }
Exemple #20
0
 /**
  * {@inheritdoc}
  */
 public function requestTranslation(JobInterface $job)
 {
     $name = "JobID" . $job->id() . '_' . $job->getSourceLangcode() . '_' . $job->getTargetLangcode();
     $export = \Drupal::service('plugin.manager.tmgmt_file.format')->createInstance($job->getSetting('export_format'));
     $path = $job->getSetting('scheme') . '://tmgmt_file/' . $name . '.' . $job->getSetting('export_format');
     $dirname = dirname($path);
     if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) {
         $file = file_save_data($export->export($job), $path);
         \Drupal::service('file.usage')->add($file, 'tmgmt_file', 'tmgmt_job', $job->id());
         $job->submitted('Exported file can be downloaded <a href="@link">here</a>.', array('@link' => file_create_url($path)));
     }
 }
 /**
  * {@inheritdoc}
  */
 public static function migrateDumpAlter(TestBase $test)
 {
     // Creates a random filename and updates the source database.
     $random = new Random();
     $temp_directory = $test->getTempFilesDirectory();
     file_prepare_directory($temp_directory, FILE_CREATE_DIRECTORY);
     static::$tempFilename = $test->getDatabasePrefix() . $random->name() . '.jpg';
     $file_path = $temp_directory . '/' . static::$tempFilename;
     file_put_contents($file_path, '');
     Database::getConnection('default', 'migrate')->update('files')->condition('fid', 6)->fields(array('filename' => static::$tempFilename, 'filepath' => $file_path))->execute();
     return static::$tempFilename;
 }
Exemple #22
0
 /**
  * Check if the directory exists and create it if not.
  */
 protected function ensureStorage()
 {
     $dir = $this->getCollectionDirectory();
     $success = file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
     // Only create .htaccess file in root directory.
     if ($dir == $this->directory) {
         $success = $success && file_save_htaccess($this->directory, TRUE, TRUE);
     }
     if (!$success) {
         throw new StorageException('Failed to create config directory ' . $dir);
     }
     return $this;
 }
Exemple #23
0
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     // Enable user pictures.
     \Drupal::state()->set('user_pictures', 1);
     \Drupal::state()->set('user_picture_file_size', '');
     // Set up the pictures directory.
     $picture_path = file_default_scheme() . '://' . \Drupal::state()->get('user_picture_path', 'pictures');
     if (!file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY)) {
         $this->fail('Could not create directory ' . $picture_path . '.');
     }
     $this->account = $this->drupalCreateUser(array('administer users'));
     $this->drupalLogin($this->account);
 }
 /**
  * {@inheritdoc}
  */
 public static function value(array &$element, &$input, FormStateInterface $form_state)
 {
     if (!empty($input['filefield_flysystem']['filename'])) {
         $instance = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
         $filepath = $input['filefield_flysystem']['filename'];
         $directory = $element['#upload_location'];
         $mode = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
         if (!drupal_chmod($directory, $mode) && !file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
             \Drupal::logger('filefield_sources')->log(E_NOTICE, 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' => $filepath, '%destination' => drupal_realpath($directory)));
             drupal_set_message(t('The specified file %file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array('%file' => $filepath)), 'error');
             return;
         }
         // Clean up the file name extensions and transliterate.
         $original_filepath = $filepath;
         $new_filepath = filefield_sources_clean_filename($filepath, $instance->getSetting('file_extensions'));
         rename($filepath, $new_filepath);
         $filepath = $new_filepath;
         // Run all the normal validations, minus file size restrictions.
         $validators = $element['#upload_validators'];
         if (isset($validators['file_validate_size'])) {
             unset($validators['file_validate_size']);
         }
         // Serve files from source folder directly.
         if ($element['#filefield_sources_settings']['flysystem']['attach_mode'] == FILEFIELD_SOURCE_FLYSYSTEM_ATTACH_MODE_SERVEFROMFOLDER) {
             $directory = $filepath;
             if ($file = filefield_sources_flysystem_save_file_servefromattach($filepath, $validators, $directory)) {
                 if (!in_array($file->id(), $input['fids'])) {
                     $input['fids'][] = $file->id();
                 }
             }
         } else {
             if ($file = filefield_sources_save_file($filepath, $validators, $directory)) {
                 if (!in_array($file->id(), $input['fids'])) {
                     $input['fids'][] = $file->id();
                 }
                 // Delete the original file if "moving" the file instead of copying.
                 if ($element['#filefield_sources_settings']['flysystem']['attach_mode'] !== FILEFIELD_SOURCE_FLYSYSTEM_ATTACH_MODE_COPY) {
                     @unlink($filepath);
                 }
             }
         }
         // Restore the original file name if the file still exists.
         if (file_exists($filepath) && $filepath != $original_filepath) {
             rename($filepath, $original_filepath);
         }
         $input['filefield_flysystem']['filename'] = '';
     }
 }
  protected function validatePdfUpload(array &$form, FormStateInterface &$form_state, UploadedFile $file_upload, $file_field_name) {
    /**
     * @var $file_upload \Symfony\Component\HttpFoundation\File\UploadedFile
     */
    if ($file_upload && $file_upload->isValid()) {
      // Move it to somewhere we know.
      $uploaded_filename = $file_upload->getClientOriginalName();

      // Ensure the destination is unique; we deliberately use managed files,
      // but they are keyed on file URI, so we can't save the same one twice.
      $scheme = $this->config('fillpdf.settings')->get('scheme');
      $destination = file_destination(FillPdf::buildFileUri($scheme, 'fillpdf/' . $uploaded_filename), FILE_EXISTS_RENAME);

      // Ensure our directory exists.
      $fillpdf_directory = FillPdf::buildFileUri($scheme, 'fillpdf');
      $directory_exists = file_prepare_directory($fillpdf_directory, FILE_CREATE_DIRECTORY + FILE_MODIFY_PERMISSIONS);

      if ($directory_exists) {
        $file_moved = $this->fileSystem->moveUploadedFile($file_upload->getRealPath(), $destination);

        if ($file_moved) {
          // Create a File object from the uploaded file.
          $new_file = File::create([
            'uri' => $destination,
            'uid' => $this->currentUser()->id(),
          ]);

          $errors = file_validate_extensions($new_file, 'pdf');

          if (count($errors)) {
            $form_state->setErrorByName('upload_pdf', $this->t('Only PDF files are supported, and they must end in .pdf.'));
          }
          else {
            $form_state->setValue('upload_pdf', $new_file);
          }
        }
        else {
          $form_state->setErrorByName('upload_pdf', $this->t("Could not move your uploaded file from PHP's temporary location to Drupal file storage."));
        }
      }
      else {
        $form_state->setErrorByName('upload_pdf', $this->t('Could not automatically create the <em>fillpdf</em> subdirectory. Please create this manually before uploading your PDF form.'));
      }
    }
    else {
      $form_state->setErrorByName('upload_pdf', $this->t('Your PDF could not be uploaded. Did you select one?'));
    }
  }
 /**
  * {@inheritdoc}
  */
 public static function value(array &$element, &$input, FormStateInterface $form_state)
 {
     if (isset($input['filefield_clipboard']['contents']) && strlen($input['filefield_clipboard']['contents']) > 0) {
         // Check that the destination is writable.
         $temporary_directory = 'temporary://';
         if (!file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS)) {
             \Drupal::logger('filefield_sources')->log(E_NOTICE, 'The directory %directory is not writable, because it does not have the correct permissions set.', array('%directory' => drupal_realpath($temporary_directory)));
             drupal_set_message(t('The file could not be transferred because the temporary directory is not writable.'), 'error');
             return;
         }
         // Check that the destination is writable.
         $directory = $element['#upload_location'];
         $mode = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
         // This first chmod check is for other systems such as S3, which don't
         // work with file_prepare_directory().
         if (!drupal_chmod($directory, $mode) && !file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
             $url = $input['filefield_clipboard']['filename'];
             \Drupal::logger('filefield_sources')->log(E_NOTICE, 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' => $url, '%destination' => drupal_realpath($directory)));
             drupal_set_message(t('The specified file %file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array('%file' => $url)), 'error');
             return;
         }
         // Split the file information in mimetype and base64 encoded binary.
         $base64_data = $input['filefield_clipboard']['contents'];
         $comma_position = strpos($base64_data, ',');
         $semicolon_position = strpos($base64_data, ';');
         $file_contents = base64_decode(substr($base64_data, $comma_position + 1));
         $mimetype = substr($base64_data, 5, $semicolon_position - 5);
         $extension = \Drupal::service('file.mime_type.guesser.extension')->convertMimeTypeToExtension($mimetype);
         $filename = trim($input['filefield_clipboard']['filename']);
         $filename = preg_replace('/\\.[a-z0-9]{3,4}$/', '', $filename);
         $filename = (empty($filename) ? 'paste_' . REQUEST_TIME : $filename) . '.' . $extension;
         $filepath = file_create_filename($filename, $temporary_directory);
         $copy_success = FALSE;
         if ($fp = @fopen($filepath, 'w')) {
             fwrite($fp, $file_contents);
             fclose($fp);
             $copy_success = TRUE;
         }
         if ($copy_success && ($file = filefield_sources_save_file($filepath, $element['#upload_validators'], $element['#upload_location']))) {
             if (!in_array($file->id(), $input['fids'])) {
                 $input['fids'][] = $file->id();
             }
         }
         // Remove the temporary file generated from paste.
         @unlink($filepath);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     // Create target directory if necessary
     $destination = \Drupal::config('system.file')->get('default_scheme') . '://plupload-test';
     file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
     $saved_files = array();
     foreach ($form_state->getValue('plupload') as $uploaded_file) {
         $file_uri = file_stream_wrapper_uri_normalize($destination . '/' . $uploaded_file['name']);
         // Move file without creating a new 'file' entity.
         file_unmanaged_move($uploaded_file['tmppath'], $file_uri);
         // @todo: When https://www.drupal.org/node/2245927 is resolved,
         // use a helper to save file to file_managed table
         $saved_files[] = $file_uri;
     }
     if (!empty($saved_files)) {
         drupal_set_message('Files uploaded correctly: ' . implode(', ', $saved_files) . '.', 'status');
     }
 }
Exemple #28
0
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     $source = $this->configuration['source_base_path'] . $row->getSourceProperty($this->configuration['source_path_property']);
     $destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
     $replace = FILE_EXISTS_REPLACE;
     if (!empty($this->configuration['rename'])) {
         $entity_id = $row->getDestinationProperty($this->getKey('id'));
         if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
             $replace = FILE_EXISTS_RENAME;
         }
     }
     $dirname = drupal_dirname($destination);
     file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);
     if ($this->configuration['move']) {
         file_unmanaged_move($source, $destination, $replace);
     } else {
         file_unmanaged_copy($source, $destination, $replace);
     }
     return parent::import($row, $old_destination_id_values);
 }
 public function testUserTokens()
 {
     // Enable user pictures.
     \Drupal::state()->set('user_pictures', 1);
     \Drupal::state()->set('user_picture_file_size', '');
     // Set up the pictures directory.
     $picture_path = file_default_scheme() . '://' . \Drupal::state()->get('user_picture_path', 'pictures');
     if (!file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY)) {
         $this->fail('Could not create directory ' . $picture_path . '.');
     }
     // Add a user picture to the account.
     $image = current($this->drupalGetTestFiles('image'));
     $edit = array('files[user_picture_0]' => drupal_realpath($image->uri));
     $this->drupalPostForm('user/' . $this->account->id() . '/edit', $edit, t('Save'));
     $storage = \Drupal::entityTypeManager()->getStorage('user');
     // Load actual user data from database.
     $storage->resetCache();
     $this->account = $storage->load($this->account->id());
     $this->assertTrue(!empty($this->account->user_picture->target_id), 'User picture uploaded.');
     $picture = ['#theme' => 'user_picture', '#account' => $this->account];
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     $user_tokens = array('picture' => $renderer->renderPlain($picture), 'picture:fid' => $this->account->user_picture->target_id, 'picture:size-raw' => 125, 'ip-address' => NULL, 'roles' => implode(', ', $this->account->getRoles()));
     $this->assertTokens('user', array('user' => $this->account), $user_tokens);
     // Remove the simpletest-created user role.
     $roles = $this->account->getRoles();
     $this->account->removeRole(end($roles));
     $this->account->save();
     // Remove the user picture field and reload the user.
     FieldStorageConfig::loadByName('user', 'user_picture')->delete();
     $storage->resetCache();
     $this->account = $storage->load($this->account->id());
     $user_tokens = array('picture' => NULL, 'picture:fid' => NULL, 'ip-address' => NULL, 'roles' => 'authenticated', 'roles:keys' => (string) DRUPAL_AUTHENTICATED_RID);
     $this->assertTokens('user', array('user' => $this->account), $user_tokens);
     // The ip address token should work for the current user token type.
     $tokens = array('ip-address' => \Drupal::request()->getClientIp());
     $this->assertTokens('current-user', array(), $tokens);
     $anonymous = new AnonymousUserSession();
     $tokens = array('roles' => 'anonymous', 'roles:keys' => (string) DRUPAL_ANONYMOUS_RID);
     $this->assertTokens('user', array('user' => $anonymous), $tokens);
 }
Exemple #30
0
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     $file = $row->getSourceProperty($this->configuration['source_path_property']);
     $destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
     // We check the destination to see if this is a temporary file. If it is
     // then we do not prepend the source_base_path because temporary files are
     // already absolute.
     $source = $this->isTempFile($destination) ? $file : $this->configuration['source_base_path'] . $file;
     $dirname = drupal_dirname($destination);
     if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) {
         throw new MigrateException(t('Could not create directory %dirname', array('%dirname' => $dirname)));
     }
     // If the start and end file is exactly the same, there is nothing to do.
     if (drupal_realpath($source) === drupal_realpath($destination)) {
         return parent::import($row, $old_destination_id_values);
     }
     $replace = FILE_EXISTS_REPLACE;
     if (!empty($this->configuration['rename'])) {
         $entity_id = $row->getDestinationProperty($this->getKey('id'));
         if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
             $replace = FILE_EXISTS_RENAME;
         }
     }
     if ($this->configuration['move']) {
         $copied = file_unmanaged_move($source, $destination, $replace);
     } else {
         // Determine whether we can perform this operation based on overwrite rules.
         $original_destination = $destination;
         $destination = file_destination($destination, $replace);
         if ($destination === FALSE) {
             throw new MigrateException(t('File %file could not be copied because a file by that name already exists in the destination directory (%destination)', array('%file' => $source, '%destination' => $original_destination)));
         }
         $source = $this->urlencode($source);
         $copied = @copy($source, $destination);
     }
     if ($copied) {
         return parent::import($row, $old_destination_id_values);
     } else {
         throw new MigrateException(t('File %source could not be copied to %destination.', array('%source' => $source, '%destination' => $destination)));
     }
 }