You are here

function angular_media_callback_file in CKEditor Widgets 7

Callback to retrieve (GET) or save (POST) updates.

This will respond to the URL on which plupoad library will upload files.

1 string reference to 'angular_media_callback_file'
angular_media_menu in ./angular_media.module
Implements hook_menu().

File

./angular_media.module, line 227
Implementation of angular_media.module.

Code

function angular_media_callback_file($fid = NULL) {
  watchdog('angular_media', 'file $_GET <pre>%f</pre>', array(
    '%f' => print_r($_GET, 1),
  ));

  // Check the token to avoid CSRF attacks
  $token = $_REQUEST['token'];

  //empty($_GET['token']) ? $_GET['token'] : $_POST['token'];
  if (empty($token) || !drupal_valid_token($token, ANGULAR_MEDIA_TOKEN_KEY)) {
    watchdog('angular_media', 'CSRF ATTACK! <pre>%f</pre>');
    return FALSE;
  }
  if (!empty($_GET['fid'])) {
    $file = file_load($_GET['fid']);
    watchdog('angular_media', 'file loaded <pre>%f</pre>', array(
      '%f' => print_r($file, 1),
    ));
  }
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    watchdog('angular_media', 'file post <pre>%f</pre>', array(
      '%f' => print_r($file, 1),
    ));

    // Copy file over from url (flickr)
    if (!empty($_GET['url']) && empty($_GET['fid'])) {
      if (!file_entity_access('create')) {
        return FALSE;
      }
      $file = angular_media_file_url_to_object($_GET['url'], $_GET['filename']);
      watchdog('angular_media', 'file copy from url <pre>%f</pre>', array(
        '%f' => print_r($file, 1),
      ));
    }

    // Copy file over from external url (fetch)
    if (!empty($_REQUEST['external'])) {
      if (!file_entity_access('create')) {
        return FALSE;
      }

      // Save the remote file
      $provider = media_internet_get_provider($_REQUEST['external']);

      // Providers decide if they need to save locally or somewhere else.
      // This method returns a file object
      $file = $provider
        ->save();
    }

    // Process crop
    if (!empty($_GET['crop'])) {
      if (!file_entity_access('update', $file)) {
        return FALSE;
      }
      $crop = json_decode($_GET['crop']);
      $crop = (array) $crop;
      $file = angular_media_process_crop($file, $crop);
      watchdog('angular_media', 'file crop <pre>%f</pre>', array(
        '%f' => print_r($crop, 1),
      ));
    }

    // Update file fields
    if (isset($file)) {
      if (!empty($_GET['name'])) {
        $file->name = $_GET['name'];
      }
      if (!empty($_GET['alt'])) {
        $file->alt = $_GET['alt'];
        $file->field_file_image_alt_text[LANGUAGE_NONE][0]['value'] = $file->alt;
      }
      $file->title = !empty($_GET['title']) ? $_GET['title'] : '';
      $file->field_file_image_title_text[LANGUAGE_NONE][0]['value'] = $file->title;
      $file->field_media_source[LANGUAGE_NONE][0]['title'] = !empty($_GET['attribution']) ? $_GET['attribution'] : '';
      $file->field_media_source[LANGUAGE_NONE][0]['url'] = !empty($_GET['source']) ? $_GET['source'] : '';
      if (!empty($_GET['licence'])) {
        $file->field_media_license[LANGUAGE_NONE][0]['licence'] = $_GET['licence'];
      }
      $file = file_save($file);
      watchdog('angular_media', 'file saved <pre>%f</pre>', array(
        '%f' => print_r($file, 1),
      ));
    }
    else {

      // @todo: err
    }
  }
  drupal_json_output(angular_media_simplify_file($file));
  exit;
}