/** * {@inheritdoc} */ public function alterEditorJS(array &$js, BUEditorEditor $bueditor_editor, Editor $editor = NULL) { // Check selected file browser. if (isset($js['settings']['fileBrowser']) && $js['settings']['fileBrowser'] === 'imce') { // Check access if (ImceMain::access()) { $js['libraries'][] = 'imce/drupal.imce.bueditor'; } else { unset($js['settings']['fileBrowser']); } } }
/** * Operation handler: newfolder. */ public function opNewfolder(ImceFM $fm) { $folder = $fm->activeFolder; if (!$folder || !$folder->getPermission('create_subfolders')) { return; } // Create folder $name = $fm->getPost('newfolder'); if (is_string($name) && $fm->validateFileName($name)) { // Check existence $uri = Imce::joinPaths($folder->getUri(), $name); if (file_exists($uri)) { $fm->setMessage(t('%filename already exists.', array('%filename' => $name))); } elseif (mkdir($uri, $fm->getConf('chmod_directory', 0775))) { $folder->addSubfolder($name)->addToJs(); } } }
/** * Sets widget file id values by validating and processing the submitted data. * Runs before processor callbacks. */ public static function setWidgetValue($element, &$input, FormStateInterface $form_state) { if (empty($input['imce_paths'])) { return; } $paths = $input['imce_paths']; $input['imce_paths'] = ''; // Remove excess data. $paths = array_unique(array_filter(explode(':', $paths))); if (isset($element['#cardinality']) && $element['#cardinality'] > -1) { $paths = array_slice($paths, 0, $element['#cardinality']); } // Check if paths are accessible by the current user with Imce. if (!($paths = Imce::accessFilePaths($paths, \Drupal::currentUser(), $element['#scheme']))) { return; } // Validate paths as file entities. $file_usage = \Drupal::service('file.usage'); $errors = array(); foreach ($paths as $path) { // Get entity by uri $file = Imce::getFileEntity($element['#scheme'] . '://' . $path, TRUE); if ($new_errors = file_validate($file, $element['#upload_validators'])) { $errors = array_merge($errors, $new_errors); } else { // Save the file record. if ($file->isNew()) { $file->save(); } if ($fid = $file->id()) { // Make sure the file has usage otherwise it will be denied. if (!$file_usage->listUsage($file)) { $file_usage->add($file, 'imce', 'file', $fid); } $input['fids'][] = $fid; } } } // Set error messages. if ($errors) { $errors = array_unique($errors); if (count($errors) > 1) { $errors = array('#theme' => 'item_list', '#items' => $errors); $message = \Drupal::service('renderer')->render($errors); } else { $message = array_pop($errors); } // May break the widget flow if set as a form error. drupal_set_message($message, 'error'); } }
/** * Resizes a list of imce items and returns succeeded ones. */ public function resizeItems(ImceFM $fm, array $items, $width, $height, $copy = FALSE) { $factory = \Drupal::service('image.factory'); $fs = \Drupal::service('file_system'); $success = array(); foreach ($items as $item) { $uri = $item->getUri(); $image = $factory->get($uri); // Check vallidity if (!$image->isValid()) { continue; } // Check if resizing is needed. $resize = $image->getWidth() != $width || $image->getHeight() != $height; if (!$resize && !$copy) { continue; } if ($resize && !$image->resize($width, $height)) { continue; } // Save $destination = $copy ? file_create_filename($fs->basename($uri), $fs->dirname($uri)) : $uri; if (!$image->save($destination)) { continue; } // Create a new file record. if ($copy) { $filename = $fs->basename($destination); $values = array('uid' => $fm->user->id(), 'status' => 1, 'filename' => $filename, 'uri' => $destination, 'filesize' => $image->getFileSize(), 'filemime' => $image->getMimeType()); $file = \Drupal::entityTypeManager()->getStorage('file')->create($values); // Check quota if ($errors = file_validate_size($file, 0, $fm->getConf('quota'))) { file_unmanaged_delete($destination); $fm->setMessage($errors[0]); } else { $file->save(); // Add imce item $item->parent->addFile($filename)->addToJs(); } } else { if ($file = Imce::getFileEntity($uri)) { $file->setSize($image->getFileSize()); $file->save(); } // Add to js $item->addToJs(); } $success[] = $item; } return $success; }
/** * Returns the item path relative to the root. */ public function getPath() { if (isset($this->path)) { return $this->path; } if ($this->parent) { $path = $this->parent->getPath(); if (isset($path)) { return Imce::joinPaths($path, $this->name); } } }
/** * Checks access to /imce/{scheme} path. */ public function checkAccess($scheme) { return AccessResult::allowedIf(Imce::access($this->currentUser(), $scheme)); }
/** * Returns name filtering regexp. */ public function getNameFilter() { return Imce::nameFilterInConf($this->conf); }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { // Check folders $folders = array(); foreach ($form_state->getValue(array('conf', 'folders')) as $i => $folder) { $path = trim($folder['path']); // Empty path if ($path === '') { continue; } // Validate path if (!Imce::regularPath($path)) { return $form_state->setError($form['conf']['folders'][$i]['path'], $this->t('Invalid folder path.')); } // Remove empty permissions $folder['permissions'] = array_filter($folder['permissions']); $folder['path'] = $path; $folders[$path] = $folder; } // No valid folders if (!$folders) { return $form_state->setError($form['conf']['folders'][0]['path'], $this->t('You must define a folder.')); } $form_state->setValue(array('conf', 'folders'), array_values($folders)); // Call plugin validators \Drupal::service('plugin.manager.imce.plugin')->validateProfileForm($form, $form_state, $this->getEntity()); return parent::validateForm($form, $form_state); }
/** * Checks if a file uri is accessible by a user with Imce. */ public static function accessFileUri($uri, AccountProxyInterface $user = NULL) { list($scheme, $path) = explode('://', $uri, 2); return $scheme && $path && Imce::accessFilePaths(array($path), $user, $scheme); }
/** * Deletes a folder by uri. */ public static function deleteFolderUri($uri, $ignore_usage = FALSE) { // Get folder content without any filtering. $content = Imce::scanDir($uri); if (!empty($content['error'])) { return FALSE; } // Delete subfolders first. foreach ($content['subfolders'] as $path) { if (!static::deleteFolderUri($path, $ignore_usage)) { return FALSE; } } // Delete files. foreach ($content['files'] as $path) { if (!static::deleteFileUri($path, $ignore_usage)) { return FALSE; } } // Recently emptied folders need some refreshing before the removal on windows. if (strncasecmp(PHP_OS, 'WIN', 3) == 0) { @closedir(@opendir($uri)); } // Remove the folder return rmdir($uri); }
/** * Sets the folder path. */ public function setPath($path) { $oldpath = $this->path; if ($path !== $oldpath) { // Remove oldpath references if (isset($oldpath)) { unset($this->fm()->tree[$oldpath]); foreach ($this->subfolders as $name => $item) { $item->setPath(NULL); } } // Add new path references $this->path = $path; if (isset($path)) { $this->fm()->tree[$path] = $this; foreach ($this->subfolders as $name => $item) { $item->setPath(Imce::joinPaths($path, $name)); } } } }
/** * Deletes a folder by uri. */ public static function deleteFolderUri($uri, $ignore_usage = FALSE, $check_files = FALSE) { // Get folder content without any filtering. $content = Imce::scanDir($uri); if (!empty($content['error'])) { return FALSE; } if ($check_files && !empty($content['files'])) { drupal_set_message(t('%folder contains files and can not be deleted.', array('%folder' => \Drupal::service('file_system')->basename($uri))), 'error'); return FALSE; } // Delete subfolders first. foreach ($content['subfolders'] as $path) { if (!static::deleteFolderUri($path, $ignore_usage, $check_files)) { return FALSE; } } // Delete files. foreach ($content['files'] as $path) { if (!static::deleteFileUri($path, $ignore_usage)) { return FALSE; } } // Recently emptied folders need some refreshing before the removal on windows. if (strncasecmp(PHP_OS, 'WIN', 3) == 0) { @closedir(@opendir($uri)); } // Remove the folder return rmdir($uri); }