You are here

function plupload_pernode in Plupload integration 6

1 string reference to 'plupload_pernode'
plupload_menu in ./plupload.module
Implementation of hook_menu().

File

./plupload.module, line 141

Code

function plupload_pernode() {
  $temp_directory = file_directory_temp();

  // Chunk it?
  $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0;
  $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0;

  // Get and clean the filename.
  $file_name = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';

  // Clean the fileName for security reasons
  if (module_exists('transliteration')) {
    $file_name = transliteration_clean_filename($file_name);
  }
  else {
    $file_name = preg_replace('/[^\\w\\._]+/', '', $file_name);
  }

  // Look for the content type header
  if (isset($_SERVER["HTTP_CONTENT_TYPE"])) {
    $content_type = $_SERVER["HTTP_CONTENT_TYPE"];
  }
  if (isset($_SERVER["CONTENT_TYPE"])) {
    $content_type = $_SERVER["CONTENT_TYPE"];
  }

  // Is this a multipart upload?
  if (strpos($content_type, "multipart") !== FALSE) {
    if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {

      // Open temp file
      $out = fopen($temp_directory . DIRECTORY_SEPARATOR . $file_name, $chunk == 0 ? "wb" : "ab");
      if ($out) {

        // Read binary input stream and append it to temp file
        $in = fopen($_FILES['file']['tmp_name'], "rb");
        if ($in) {
          while ($buff = fread($in, 4096)) {
            fwrite($out, $buff);
          }
        }
        else {
          die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
        }
        fclose($in);
        fclose($out);
        unlink($_FILES['file']['tmp_name']);
      }
      else {
        die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
      }
    }
    else {
      die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
    }
  }
  else {

    // Open temp file
    $out = fopen($temp_directory . DIRECTORY_SEPARATOR . $file_name, $chunk == 0 ? "wb" : "ab");
    if ($out) {

      // Read binary input stream and append it to temp file
      $in = fopen("php://input", "rb");
      if ($in) {
        while ($buff = fread($in, 4096)) {
          fwrite($out, $buff);
        }
      }
      else {
        die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
      }
      fclose($in);
      fclose($out);
    }
    else {
      die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
    }
  }
  if ($chunks > 1 && $chunk < $chunks - 1) {

    // Don't move the file and add the node yet, we have more chunks coming
    die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
  }

  // Move it to it's final home.
  $path = file_directory_path();

  // Pull off all the options from the query string for later attachment to the node.
  $options = array();
  foreach (array_keys($_GET) as $k) {
    $pos = strpos($k, PLUPLOAD_URL_PARAM_PREFIX);
    if ($pos === 0) {
      $options[drupal_substr($k, drupal_strlen(PLUPLOAD_URL_PARAM_PREFIX))] = $_GET[$k];
    }
  }
  $image_node = plupload_imagefield_create_node_from($temp_directory . DIRECTORY_SEPARATOR . $file_name, $file_name, $options);

  // @todo check the $image_node and do some error handling.
  // Return JSON-RPC response
  die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
}