Пример #1
0
 /**
  * Shows all possible files in selectable list.
  */
 protected function buildJsFileDisplay($file_ids)
 {
     // Gather the files if recursion IS selected.
     // Get 'em all ready to be punched into the file list.
     $recursion_file_ids = _uc_file_sort_names(_uc_file_get_dir_file_ids($file_ids, TRUE));
     foreach ($recursion_file_ids as $file_id) {
         $file = uc_file_get_by_id($file_id);
         $recursion[] = '<li>' . $file->filename . '</li>';
     }
     // Gather the files if recursion ISN'T selected.
     // Get 'em all ready to be punched into the file list.
     $no_recursion_file_ids = $file_ids;
     foreach ($no_recursion_file_ids as $file_id) {
         $file = uc_file_get_by_id($file_id);
         $no_recursion[] = '<li>' . $file->filename . '</li>';
     }
     // We'll disable the recursion checkbox if they're equal.
     $equivalent = $this->displayArraysEquivalent($recursion_file_ids, $no_recursion_file_ids);
     // The list to show if the recursion checkbox is $key.
     $result[TRUE] = $equivalent ? FALSE : $recursion;
     $result[FALSE] = $no_recursion;
     // Set up some JS to dynamically update the list based on the
     // recursion checkbox state.
     drupal_add_js('uc_file_list[false] = ' . Json::encode('<li>' . implode('</li><li>', $no_recursion) . '</li>') . ';', 'inline');
     drupal_add_js('uc_file_list[true] = ' . Json::encode('<li>' . implode('</li><li>', $recursion) . '</li>') . ';', 'inline');
     return $result;
 }
 /**
  * Handles file downloading and error states.
  *
  * @param $fid
  *   The fid of the file specified to download.
  * @param $key
  *   The hash key of a user's download.
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The request of the page.
  */
 public function download($fid, Request $request)
 {
     $user = \Drupal::currentUser();
     // Error messages for various failed download states.
     $admin_message = $this->t('Please contact the site administrator if this message has been received in error.');
     $error_messages = array(UC_FILE_ERROR_NOT_A_FILE => $this->t('The file you requested does not exist.'), UC_FILE_ERROR_TOO_MANY_BOGUS_REQUESTS => $this->t('You have attempted to download an incorrect file URL too many times.'), UC_FILE_ERROR_INVALID_DOWNLOAD => $this->t('The following URL is not a valid download link.') . ' ', UC_FILE_ERROR_TOO_MANY_LOCATIONS => $this->t('You have downloaded this file from too many different locations.'), UC_FILE_ERROR_TOO_MANY_DOWNLOADS => $this->t('You have reached the download limit for this file.'), UC_FILE_ERROR_EXPIRED => $this->t('This file download has expired.') . ' ', UC_FILE_ERROR_HOOK_ERROR => $this->t('A hook denied your access to this file.') . ' ');
     $ip = $request->getClientIp();
     if ($user->hasPermission('view all downloads')) {
         $file_download = uc_file_get_by_id($fid);
     } else {
         $file_download = uc_file_get_by_uid($user->id(), $fid);
     }
     if (isset($file_download->filename)) {
         $file_download->full_path = uc_file_qualify_file($file_download->filename);
     } else {
         throw new AccessDeniedHttpException();
     }
     // If it's ok, we push the file to the user.
     $status = UC_FILE_ERROR_OK;
     if (!$user->hasPermission('view all downloads')) {
         $status = $this->validateDownload($file_download, $user, $ip);
     }
     if ($status == UC_FILE_ERROR_OK) {
         $this->transferDownload($file_download, $ip);
     } else {
         drupal_set_message($error_messages[$status] . $admin_message, 'error');
         // Kick 'em to the curb. >:)
         $this->redirectDownload($user->id());
     }
     drupal_exit();
 }