You are here

public static function RESTServer::fileRecieve in Services 6.3

File

servers/rest_server/includes/RESTServer.inc, line 331
Class for handling REST calls.

Class

RESTServer
@file Class for handling REST calls.

Code

public static function fileRecieve($handle, $validators = array()) {
  $validators['file_validate_name_length'] = array();
  $type = RESTServer::parseContentHeader($_SERVER['CONTENT_TYPE']);
  $disposition = RESTServer::parseContentHeader($_SERVER['HTTP_CONTENT_DISPOSITION']);
  $filename = file_munge_filename(trim(basename($disposition['param']['filename'])));

  // Rename potentially executable files, to help prevent exploits.
  if (preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $filename) && drupal_substr($filename, -4) != '.txt') {
    $type['value'] = 'text/plain';
    $filename .= '.txt';
  }
  $filepath = file_destination(file_create_path(file_directory_temp() . '/' . $filename), FILE_EXISTS_RENAME);
  $file = (object) array(
    'uid' => 0,
    'filename' => $filename,
    'filepath' => $filepath,
    'filemime' => $type['value'],
    'status' => FILE_STATUS_TEMPORARY,
    'timestamp' => time(),
  );
  RESTServer::streamToFile($handle, $filepath);
  $file->filesize = filesize($filepath);

  // Call the validation functions.
  $errors = array();
  foreach ($validators as $function => $args) {
    array_unshift($args, $file);
    $errors = array_merge($errors, call_user_func_array($function, $args));
  }
  if (!empty($errors)) {
    return services_error(t('Errors while validating the file - @errors', array(
      '@errors' => implode(" ", $errors),
    )), 406);
  }
  drupal_write_record('files', $file);
  return $file;
}