class-qdr-tss-download.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Download functionality of the plugin.
  4. *
  5. * @package QDR_Temporary_Shared_Storage
  6. * @since 1.0.0
  7. */
  8. class QDR_TSS_Download {
  9. /**
  10. * The ID of this plugin.
  11. *
  12. * @since 1.0.0
  13. * @access private
  14. * @var string $plugin_name The ID of this plugin.
  15. */
  16. private $plugin_name;
  17. /**
  18. * The version of this plugin.
  19. *
  20. * @since 1.0.0
  21. * @access private
  22. * @var string $version The current version of this plugin.
  23. */
  24. private $version;
  25. /**
  26. * The database manager instance.
  27. *
  28. * @since 1.0.0
  29. * @access private
  30. * @var QDR_TSS_DB_Manager $db_manager The database manager instance.
  31. */
  32. private $db_manager;
  33. /**
  34. * The file manager instance.
  35. *
  36. * @since 1.0.0
  37. * @access private
  38. * @var QDR_TSS_File_Manager $file_manager The file manager instance.
  39. */
  40. private $file_manager;
  41. /**
  42. * Initialize the class and set its properties.
  43. *
  44. * @since 1.0.0
  45. * @param string $plugin_name The name of this plugin.
  46. * @param string $version The version of this plugin.
  47. */
  48. public function __construct($plugin_name, $version) {
  49. $this->plugin_name = $plugin_name;
  50. $this->version = $version;
  51. $this->db_manager = QDR_TSS_DB_Manager::instance();
  52. $this->file_manager = QDR_TSS_File_Manager::instance();
  53. }
  54. /**
  55. * Process download request.
  56. *
  57. * @since 1.0.0
  58. * @param array $atts Shortcode attributes.
  59. * @return string Download form or file download.
  60. */
  61. public function process_download_request($atts) {
  62. // Extract attributes
  63. $atts = shortcode_atts(array(
  64. 'id' => null,
  65. ), $atts, 'qdr_tss_download');
  66. // Get media ID from shortcode attribute or URL parameter
  67. $media_id = $atts['id'];
  68. if (!$media_id && isset($_GET['id'])) {
  69. $media_id = intval($_GET['id']);
  70. }
  71. // If no media ID, show form to enter media ID, password and reference
  72. if (!$media_id) {
  73. return $this->render_download_form();
  74. }
  75. // Get media information
  76. $item = $this->db_manager->get_item_by_media_id($media_id);
  77. if (!$item) {
  78. return '<div class="qdr-tss-error">' . __('File not found.', 'qdr-temporary-shared-storage') . '</div>';
  79. }
  80. // Check if file is active
  81. $current_time = current_time('mysql');
  82. if ($current_time < $item->active_from) {
  83. return '<div class="qdr-tss-error">' . __('This file is not yet available for download.', 'qdr-temporary-shared-storage') . '</div>';
  84. }
  85. if ($current_time > $item->active_to) {
  86. return '<div class="qdr-tss-error">' . __('This file has expired and is no longer available for download.', 'qdr-temporary-shared-storage') . '</div>';
  87. }
  88. // Check if form is submitted
  89. if (isset($_POST['qdr_tss_download_submit'])) {
  90. // Verify nonce
  91. if (!isset($_POST['qdr_tss_nonce']) || !wp_verify_nonce($_POST['qdr_tss_nonce'], 'qdr_tss_download')) {
  92. return '<div class="qdr-tss-error">' . __('Security check failed.', 'qdr-temporary-shared-storage') . '</div>';
  93. }
  94. // Verify password
  95. $password = isset($_POST['qdr_tss_password']) ? $_POST['qdr_tss_password'] : '';
  96. if (!wp_check_password($password, $item->password)) {
  97. return '<div class="qdr-tss-error">' . __('Invalid password.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($media_id);
  98. }
  99. // Verify reference if set
  100. if (!empty($item->reference)) {
  101. $reference = isset($_POST['qdr_tss_reference']) ? $_POST['qdr_tss_reference'] : '';
  102. if ($reference !== $item->reference) {
  103. return '<div class="qdr-tss-error">' . __('Invalid reference.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($media_id);
  104. }
  105. }
  106. // Update download count and last download time
  107. $this->db_manager->increment_download_count($item->id);
  108. // Initiate download
  109. $file_path = QDR_TSS_UPLOADS_DIR . $item->file_path;
  110. if (file_exists($file_path)) {
  111. // Set headers for download
  112. header('Content-Description: File Transfer');
  113. header('Content-Type: application/octet-stream');
  114. header('Content-Disposition: attachment; filename="' . $item->original_file_name . '"');
  115. header('Expires: 0');
  116. header('Cache-Control: must-revalidate');
  117. header('Pragma: public');
  118. header('Content-Length: ' . filesize($file_path));
  119. ob_clean();
  120. flush();
  121. readfile($file_path);
  122. exit;
  123. } else {
  124. return '<div class="qdr-tss-error">' . __('File not found on server.', 'qdr-temporary-shared-storage') . '</div>';
  125. }
  126. }
  127. // Show download form with media information
  128. $output = '<div class="qdr-tss-download-info">';
  129. $output .= '<h2>' . __('File Information', 'qdr-temporary-shared-storage') . '</h2>';
  130. $output .= '<p><strong>' . __('File Name:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->original_file_name) . '</p>';
  131. if (!empty($item->description)) {
  132. $output .= '<p><strong>' . __('Description:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->description) . '</p>';
  133. }
  134. if (!empty($item->message)) {
  135. $output .= '<p><strong>' . __('Message:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->message) . '</p>';
  136. }
  137. $output .= '</div>';
  138. $output .= $this->render_download_form($media_id);
  139. return $output;
  140. }
  141. /**
  142. * Render download form.
  143. *
  144. * @since 1.0.0
  145. * @param int $media_id Optional. Media ID.
  146. * @return string Download form HTML.
  147. */
  148. private function render_download_form($media_id = null) {
  149. $form = '<div class="qdr-tss-download-form">';
  150. $form .= '<form method="post" action="">';
  151. // Add nonce for security
  152. $form .= wp_nonce_field('qdr_tss_download', 'qdr_tss_nonce', true, false);
  153. if (!$media_id) {
  154. $form .= '<div class="qdr-tss-form-group">';
  155. $form .= '<label for="qdr_tss_media_id">' . __('Media ID', 'qdr-temporary-shared-storage') . '</label>';
  156. $form .= '<input type="text" name="id" id="qdr_tss_media_id" required>';
  157. $form .= '</div>';
  158. } else {
  159. $form .= '<input type="hidden" name="id" value="' . esc_attr($media_id) . '">';
  160. }
  161. $form .= '<div class="qdr-tss-form-group">';
  162. $form .= '<label for="qdr_tss_password">' . __('Password', 'qdr-temporary-shared-storage') . '</label>';
  163. $form .= '<input type="password" name="qdr_tss_password" id="qdr_tss_password" required>';
  164. $form .= '</div>';
  165. $form .= '<div class="qdr-tss-form-group">';
  166. $form .= '<label for="qdr_tss_reference">' . __('Reference (if required)', 'qdr-temporary-shared-storage') . '</label>';
  167. $form .= '<input type="text" name="qdr_tss_reference" id="qdr_tss_reference">';
  168. $form .= '</div>';
  169. $form .= '<div class="qdr-tss-form-group">';
  170. $form .= '<button type="submit" name="qdr_tss_download_submit" class="qdr-tss-button">' . __('Download File', 'qdr-temporary-shared-storage') . '</button>';
  171. $form .= '</div>';
  172. $form .= '</form>';
  173. $form .= '</div>';
  174. return $form;
  175. }
  176. }