You are here

function fillpdf_file_download in FillPDF 7

Implements hook_file_download().

@todo Rewrite this to not nest so deeply, if possible. It would be better to return early.

1 call to fillpdf_file_download()
fillpdf_file_download_access_alter in ./fillpdf.module
Implements hook_file_download_access_alter().

File

./fillpdf.module, line 165

Code

function fillpdf_file_download($uri) {

  // Do we handle this kind of file? Check using string functions for
  // performance.
  $uri_parts = explode('/', $uri);
  if ($uri_parts[2] === 'fillpdf') {

    // Does this file actually exist?
    $fid = db_query('SELECT fid FROM {file_managed} WHERE uri = :uri', array(
      ':uri' => $uri,
    ))
      ->fetchField();
    if ($fid) {
      $file = file_load($fid);

      // If no other modules have re-used the FillPDF file and increased the
      // count, there should only be one usage. In any case, we only handle
      // the fillpdf_file type. As long as one of the matching file contexts
      // matches, we permit access. If you're a module author that wants to
      // use the generated FillPDF files for other purposes, please use a
      // unique type in {file_usage} and implement hook_file_download() in
      // your own module for more control.
      $usage = file_usage_list($file);
      if (!isset($usage['fillpdf'])) {

        // File isn't registered with FillPDF, so we don't have any say.
        return;
      }
      foreach ($usage['fillpdf'] as $type => $per_id) {
        switch ($type) {
          case 'fillpdf_form':

            // Only people who can manage forms can download the source forms.
            if (user_access('administer pdfs')) {
              return file_get_content_headers($file);
            }
            break;
          case 'fillpdf_file':
            foreach ($per_id as $id => $count) {
              $raw_file_context = fillpdf_file_context_load($id);
              if ($raw_file_context) {

                // Expand the stored link into a stub context (entities not
                // loaded).
                $stub_context = fillpdf_link_to_stub_context($raw_file_context);
                if ($stub_context['fid']) {

                  // Expand the stub context (load the entities).
                  $fillpdf_info = fillpdf_load($stub_context['fid']);
                  $file_context = fillpdf_load_entities($fillpdf_info, $stub_context['nids'], $stub_context['webforms'], $stub_context['uc_order_ids'], $stub_context['uc_order_product_ids'], $GLOBALS['user'], $stub_context['entity_ids']);

                  // Check access as if they were filling in the PDF from
                  // scratch.
                  if (fillpdf_merge_pdf_access($file_context['nodes'], $file_context['webforms'], $file_context['uc_orders'], $file_context['uc_order_products'])) {

                    // We don't need to add any special headers.
                    return file_get_content_headers($file);
                  }
                }
              }
            }
            break;
        }
      }

      // The file is registered with fillpdf, but didn't reach the success
      // condition, so they aren't allowed to view this file.
      return -1;
    }
  }
}