You are here

function imageeditor_inline_save in Image Editor 7

1 string reference to 'imageeditor_inline_save'
imageeditor_inline_menu in imageeditor_inline/imageeditor_inline.module
Implements hook_menu().

File

imageeditor_inline/imageeditor_inline.pages.inc, line 2

Code

function imageeditor_inline_save() {
  $alert = '';
  if (!_imageeditor_inline_access_check($_POST['fullurl'])) {
    $alert .= 'You don\'t have permission to edit this image.' . "\n";
    drupal_json_output($alert);
  }
  else {
    $ds = file_default_scheme();

    // Save backup to default stream wrapper imageeditor/backup.
    $directory = $ds . '://imageeditor/backup';

    // Original image was from this site.
    if ($fullurl = imageeditor_inline_urltouri($_POST['fullurl'])) {
      $local = TRUE;
      $alert .= 'Original image was from this website.' . "\n";
      $directory .= '/' . dirname(file_uri_target($fullurl));
      if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
        watchdog('imageeditor_inline', 'Directory %directory for image backups could not be created.', array(
          '%directory' => $directory,
        ), WATCHDOG_WARNING);
      }
      elseif ($backup = file_unmanaged_copy($fullurl, $directory)) {
        watchdog('imageeditor_inline', 'Backed up original image %fullurl to %backup.', array(
          '%fullurl' => $fullurl,
          '%backup' => $backup,
        ), WATCHDOG_INFO);
        $alert .= 'Original image was backed up.' . "\n";
      }
      else {
        watchdog('imageeditor_inline', 'Could not back up original image %fullurl to %directory.', array(
          '%fullurl' => $fullurl,
          '%directory' => $directory,
        ), WATCHDOG_WARNING);
        $alert .= 'Original image backup couldn\'t be created.' . "\n";
      }
    }
    else {
      $local = FALSE;
      $alert .= 'Original image was from external website.' . "\n";
      $url_info = parse_url($_POST['fullurl']);
      $directory .= '/' . $url_info['host'] . '/' . dirname($url_info['path']);
      if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
        watchdog('imageeditor_inline', 'Directory %directory for image backups could not be created.', array(
          '%directory' => $directory,
        ), WATCHDOG_WARNING);
      }
      elseif ($data = imageeditor_inline_save_file_get_contents($_POST['fullurl'])) {
        if ($backup = file_unmanaged_save_data($data, $directory . '/' . basename($_POST['fullurl']))) {
          watchdog('imageeditor_inline', 'Backed up original image %fullurl to %backup.', array(
            '%fullurl' => $_POST['fullurl'],
            '%backup' => $backup,
          ), WATCHDOG_INFO);
          $alert .= 'Original image was backed up.' . "\n";
        }
        else {
          watchdog('imageeditor_inline', 'Could not back up original image %fullurl to %directory.', array(
            '%fullurl' => $_POST['fullurl'],
            '%directory' => $directory,
          ), WATCHDOG_WARNING);
          $alert .= 'Original image backup couldn\'t be created.' . "\n";
        }
      }
      else {
        watchdog('imageeditor_inline', 'Could not get image from %url.', array(
          '%url' => $_POST['fullurl'],
        ), WATCHDOG_WARNING);
        $alert .= 'Original image backup couldn\'t be created.' . "\n";
      }
    }

    // Replace original image with edited image.
    if ($image = imageeditor_inline_urltouri($_POST['image'])) {

      //edited image is from this site
      $alert .= 'Edited image comes from this website.' . "\n";
      if ($local) {

        //original image was from local site - rewrite it using the edited image
        if ($file = file_unmanaged_move($image, $fullurl, FILE_EXISTS_REPLACE)) {
          watchdog('imageeditor_inline', 'Saved edited image %image to %file.', array(
            '%image' => $image,
            '%file' => $file,
          ), WATCHDOG_INFO);
          $alert .= 'Original image was successfully replaced by edited image.' . "\n";
        }
        else {
          watchdog('imageeditor_inline', 'Could not save edited image %image to %fullurl.', array(
            '%image' => $image,
            '%fullurl' => $fullurl,
          ), WATCHDOG_WARNING);
          $alert .= 'Original image couldn\'t be replaced by edited image.' . "\n";
        }
      }
      else {

        //original image was from external site - save edited image to imageeditor/external
        $directory = $ds . '://imageeditor/external';
        if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
          watchdog('imageeditor_inline', 'Directory %directory could not be created.', array(
            '%directory' => $directory,
          ), WATCHDOG_WARNING);
        }
        elseif ($file = file_unmanaged_move($image, $directory . '/' . basename($image), FILE_EXISTS_RENAME)) {
          watchdog('imageeditor_inline', 'Saved edited image %image to %file.', array(
            '%image' => $image,
            '%file' => $file,
          ), WATCHDOG_INFO);
          $alert .= 'Edited image was successfully saved.' . "\n";
        }
        else {
          watchdog('imageeditor_inline', 'Could not save edited image %image to %directory.', array(
            '%image' => $image,
            '%directory' => $directory . '/' . basename($image),
          ), WATCHDOG_WARNING);
          $alert .= 'Edited image couldn\'t be saved.' . "\n";
        }
      }
    }
    else {

      //edited image is from external site
      $alert .= 'Edited image comes from external website.' . "\n";
      if ($data = imageeditor_inline_save_file_get_contents($_POST['image'])) {
        if ($local) {

          //original image was from local site - rewrite it using the edited image
          if ($file = file_unmanaged_save_data($data, $fullurl, FILE_EXISTS_REPLACE)) {
            watchdog('imageeditor_inline', 'Saved edited image %image to %file.', array(
              '%image' => $_POST['image'],
              '%file' => $file,
            ), WATCHDOG_INFO);
            $alert .= 'Original image was successfully replaced by edited image.' . "\n";
          }
          else {
            watchdog('imageeditor_inline', 'Could not save edited image %image to %fullurl.', array(
              '%image' => $_POST['image'],
              '%fullurl' => $fullurl,
            ), WATCHDOG_WARNING);
            $alert .= 'Original image couldn\'t be replaced by edited image.' . "\n";
          }
        }
        else {

          //original image was from external site - save edited image to imageeditor/external
          $directory = $ds . '://imageeditor/external';
          if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
            watchdog('imageeditor_inline', 'Directory %directory could not be created.', array(
              '%directory' => $directory,
            ), WATCHDOG_WARNING);
          }
          elseif ($file = file_unmanaged_save_data($data, $directory . '/' . basename($_POST['image']), FILE_EXISTS_RENAME)) {
            watchdog('imageeditor_inline', 'Saved edited image %image to %file.', array(
              '%image' => $_POST['image'],
              '%file' => $file,
            ), WATCHDOG_INFO);
            $alert .= 'Edited image was successfully saved.' . "\n";
          }
          else {
            watchdog('imageeditor_inline', 'Could not save edited image %image to %directory.', array(
              '%image' => $_POST['image'],
              '%directory' => $directory . '/' . basename($_POST['image']),
            ), WATCHDOG_WARNING);
            $alert .= 'Edited image couldn\'t be saved.' . "\n";
          }
        }
      }
      else {
        watchdog('imageeditor_inline', 'Could not get image from %url.', array(
          '%url' => $_POST['image'],
        ), WATCHDOG_WARNING);
      }
    }

    // Clear styles cache and update files_managed DB table if original image was local one.
    if ($local) {

      // Clear styles cache.
      // TODO - check if it is symbolic link (collageformatter).
      image_path_flush($fullurl);
      $alert .= 'Image styles of original image were flushed.' . "\n";

      // Check if this uri is registered in DB and update its record.
      $existing_files = file_load_multiple(array(), array(
        'uri' => $fullurl,
      ));
      global $user;
      foreach ($existing_files as $file) {
        if (variable_get('imageeditor_inline_file_ownership', 0)) {
          $file->uid = $user->uid;
          $alert .= 'Image file ownership has been changed to you.' . "\n";
        }
        file_save($file);

        // TODO: update image dimensions in image fields - see file_entity module.
      }
    }
    drupal_json_output($alert);
  }
}