class-qdr-tss-download.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 fileid from shortcode attribute, URL parameter, or query var
  67. $fileid = $atts['id'];
  68. if (!$fileid && isset($_GET['id'])) {
  69. $fileid = sanitize_text_field($_GET['id']);
  70. }
  71. if (!$fileid) {
  72. $fileid = get_query_var('qdr_file_id', '');
  73. }
  74. // If no fileid, show form to enter fileid, password and reference
  75. if (!$fileid) {
  76. return $this->render_download_form();
  77. }
  78. // Get file information
  79. $item = $this->db_manager->get_item_by_fileid($fileid);
  80. if (!$item) {
  81. return '<div class="qdr-tss-error">' . __('File not found.', 'qdr-temporary-shared-storage') . '</div>';
  82. }
  83. // Check if file is active
  84. $current_time = current_time('mysql');
  85. if ($current_time < $item->active_from) {
  86. return '<div class="qdr-tss-error">' . __('This file is not yet available for download.', 'qdr-temporary-shared-storage') . '</div>';
  87. }
  88. if ($current_time > $item->active_to) {
  89. return '<div class="qdr-tss-error">' . __('This file has expired and is no longer available for download.', 'qdr-temporary-shared-storage') . '</div>';
  90. }
  91. // Check if form is submitted
  92. if (isset($_POST['qdr_tss_download_submit'])) {
  93. // Verify nonce
  94. if (!isset($_POST['qdr_tss_nonce']) || !wp_verify_nonce($_POST['qdr_tss_nonce'], 'qdr_tss_download')) {
  95. return '<div class="qdr-tss-error">' . __('Security check failed.', 'qdr-temporary-shared-storage') . '</div>';
  96. }
  97. // Verify password
  98. $password = isset($_POST['qdr_tss_password']) ? $_POST['qdr_tss_password'] : '';
  99. if (!wp_check_password($password, $item->password)) {
  100. return '<div class="qdr-tss-error">' . __('Invalid password.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($fileid);
  101. }
  102. // Verify reference if set
  103. if (!empty($item->reference)) {
  104. $reference = isset($_POST['qdr_tss_reference']) ? $_POST['qdr_tss_reference'] : '';
  105. if ($reference !== $item->reference) {
  106. return '<div class="qdr-tss-error">' . __('Invalid reference.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($fileid);
  107. }
  108. }
  109. // Update download count and last download time
  110. $this->db_manager->increment_download_count($item->id);
  111. // Initiate download
  112. $file_path = QDR_TSS_UPLOADS_DIR . $item->file_path;
  113. if (file_exists($file_path)) {
  114. // Set headers for download
  115. header('Content-Description: File Transfer');
  116. header('Content-Type: application/octet-stream');
  117. header('Content-Disposition: attachment; filename="' . $item->original_file_name . '"');
  118. header('Expires: 0');
  119. header('Cache-Control: must-revalidate');
  120. header('Pragma: public');
  121. header('Content-Length: ' . filesize($file_path));
  122. ob_clean();
  123. flush();
  124. readfile($file_path);
  125. exit;
  126. } else {
  127. return '<div class="qdr-tss-error">' . __('File not found on server.', 'qdr-temporary-shared-storage') . '</div>';
  128. }
  129. }
  130. // Show download form with file information
  131. $output = '<div class="qdr-tss-download-info">';
  132. $output .= '<h2>' . __('File Information', 'qdr-temporary-shared-storage') . '</h2>';
  133. $output .= '<p><strong>' . __('File Name:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->original_file_name) . '</p>';
  134. if (!empty($item->description)) {
  135. $output .= '<p><strong>' . __('Description:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->description) . '</p>';
  136. }
  137. if (!empty($item->message)) {
  138. $output .= '<p><strong>' . __('Message:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->message) . '</p>';
  139. }
  140. $output .= '</div>';
  141. $output .= $this->render_download_form($fileid);
  142. return $output;
  143. }
  144. /**
  145. * Render download form.
  146. *
  147. * @since 1.0.0
  148. * @param string $fileid Optional. File ID.
  149. * @return string Download form HTML.
  150. */
  151. private function render_download_form($fileid = null) {
  152. $form = '<div class="qdr-tss-download-form">';
  153. $form .= '<form method="post" action="">';
  154. // Add nonce for security
  155. $form .= wp_nonce_field('qdr_tss_download', 'qdr_tss_nonce', true, false);
  156. if (!$fileid) {
  157. $form .= '<div class="qdr-tss-form-group">';
  158. $form .= '<label for="qdr_tss_fileid">' . __('File ID', 'qdr-temporary-shared-storage') . '</label>';
  159. $form .= '<input type="text" name="id" id="qdr_tss_fileid" required>';
  160. $form .= '</div>';
  161. } else {
  162. $form .= '<input type="hidden" name="id" value="' . esc_attr($fileid) . '">';
  163. }
  164. $form .= '<div class="qdr-tss-form-group">';
  165. $form .= '<label for="qdr_tss_password">' . __('Password', 'qdr-temporary-shared-storage') . '</label>';
  166. $form .= '<input type="password" name="qdr_tss_password" id="qdr_tss_password" required>';
  167. $form .= '</div>';
  168. $form .= '<div class="qdr-tss-form-group">';
  169. $form .= '<label for="qdr_tss_reference">' . __('Reference (if required)', 'qdr-temporary-shared-storage') . '</label>';
  170. $form .= '<input type="text" name="qdr_tss_reference" id="qdr_tss_reference">';
  171. $form .= '</div>';
  172. $form .= '<div class="qdr-tss-form-group">';
  173. $form .= '<button type="submit" name="qdr_tss_download_submit" class="qdr-tss-button">' . __('Download File', 'qdr-temporary-shared-storage') . '</button>';
  174. $form .= '</div>';
  175. $form .= '</form>';
  176. $form .= '</div>';
  177. return $form;
  178. }
  179. }