| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- /**
- * Download functionality of the plugin.
- *
- * @package QDR_Temporary_Shared_Storage
- * @since 1.0.0
- */
- class QDR_TSS_Download {
- /**
- * The ID of this plugin.
- *
- * @since 1.0.0
- * @access private
- * @var string $plugin_name The ID of this plugin.
- */
- private $plugin_name;
- /**
- * The version of this plugin.
- *
- * @since 1.0.0
- * @access private
- * @var string $version The current version of this plugin.
- */
- private $version;
- /**
- * The database manager instance.
- *
- * @since 1.0.0
- * @access private
- * @var QDR_TSS_DB_Manager $db_manager The database manager instance.
- */
- private $db_manager;
- /**
- * The file manager instance.
- *
- * @since 1.0.0
- * @access private
- * @var QDR_TSS_File_Manager $file_manager The file manager instance.
- */
- private $file_manager;
- /**
- * Initialize the class and set its properties.
- *
- * @since 1.0.0
- * @param string $plugin_name The name of this plugin.
- * @param string $version The version of this plugin.
- */
- public function __construct($plugin_name, $version) {
- $this->plugin_name = $plugin_name;
- $this->version = $version;
- $this->db_manager = QDR_TSS_DB_Manager::instance();
- $this->file_manager = QDR_TSS_File_Manager::instance();
- }
- /**
- * Process download request.
- *
- * @since 1.0.0
- * @param array $atts Shortcode attributes.
- * @return string Download form or file download.
- */
- public function process_download_request($atts) {
- // Extract attributes
- $atts = shortcode_atts(array(
- 'id' => null,
- ), $atts, 'qdr_tss_download');
-
- // Get fileid from shortcode attribute, URL parameter, or query var
- $fileid = $atts['id'];
- if (!$fileid && isset($_GET['id'])) {
- $fileid = sanitize_text_field($_GET['id']);
- }
- if (!$fileid) {
- $fileid = get_query_var('qdr_file_id', '');
- }
-
- // If no fileid, show form to enter fileid, password and reference
- if (!$fileid) {
- return $this->render_download_form();
- }
-
- // Get file information
- $item = $this->db_manager->get_item_by_fileid($fileid);
-
- if (!$item) {
- return '<div class="qdr-tss-error">' . __('File not found.', 'qdr-temporary-shared-storage') . '</div>';
- }
-
- // Check if file is active
- $current_time = current_time('mysql');
- if ($current_time < $item->active_from) {
- return '<div class="qdr-tss-error">' . __('This file is not yet available for download.', 'qdr-temporary-shared-storage') . '</div>';
- }
-
- if ($current_time > $item->active_to) {
- return '<div class="qdr-tss-error">' . __('This file has expired and is no longer available for download.', 'qdr-temporary-shared-storage') . '</div>';
- }
-
- // Check if form is submitted
- if (isset($_POST['qdr_tss_download_submit'])) {
- // Verify nonce
- if (!isset($_POST['qdr_tss_nonce']) || !wp_verify_nonce($_POST['qdr_tss_nonce'], 'qdr_tss_download')) {
- return '<div class="qdr-tss-error">' . __('Security check failed.', 'qdr-temporary-shared-storage') . '</div>';
- }
-
- // Verify password
- $password = isset($_POST['qdr_tss_password']) ? $_POST['qdr_tss_password'] : '';
- if (!wp_check_password($password, $item->password)) {
- return '<div class="qdr-tss-error">' . __('Invalid password.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($fileid);
- }
-
- // Verify reference if set
- if (!empty($item->reference)) {
- $reference = isset($_POST['qdr_tss_reference']) ? $_POST['qdr_tss_reference'] : '';
- if ($reference !== $item->reference) {
- return '<div class="qdr-tss-error">' . __('Invalid reference.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($fileid);
- }
- }
-
- // Update download count and last download time
- $this->db_manager->increment_download_count($item->id);
-
- // Initiate download
- $file_path = QDR_TSS_UPLOADS_DIR . $item->file_path;
-
- if (file_exists($file_path)) {
- // Set headers for download
- header('Content-Description: File Transfer');
- header('Content-Type: application/octet-stream');
- header('Content-Disposition: attachment; filename="' . $item->original_file_name . '"');
- header('Expires: 0');
- header('Cache-Control: must-revalidate');
- header('Pragma: public');
- header('Content-Length: ' . filesize($file_path));
- ob_clean();
- flush();
- readfile($file_path);
- exit;
- } else {
- return '<div class="qdr-tss-error">' . __('File not found on server.', 'qdr-temporary-shared-storage') . '</div>';
- }
- }
-
- // Show download form with file information
- $output = '<div class="qdr-tss-download-info">';
- $output .= '<h2>' . __('File Information', 'qdr-temporary-shared-storage') . '</h2>';
- $output .= '<p><strong>' . __('File Name:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->original_file_name) . '</p>';
-
- if (!empty($item->description)) {
- $output .= '<p><strong>' . __('Description:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->description) . '</p>';
- }
-
- if (!empty($item->message)) {
- $output .= '<p><strong>' . __('Message:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->message) . '</p>';
- }
-
- $output .= '</div>';
- $output .= $this->render_download_form($fileid);
-
- return $output;
- }
- /**
- * Render download form.
- *
- * @since 1.0.0
- * @param string $fileid Optional. File ID.
- * @return string Download form HTML.
- */
- private function render_download_form($fileid = null) {
- $form = '<div class="qdr-tss-download-form">';
- $form .= '<form method="post" action="">';
-
- // Add nonce for security
- $form .= wp_nonce_field('qdr_tss_download', 'qdr_tss_nonce', true, false);
-
- if (!$fileid) {
- $form .= '<div class="qdr-tss-form-group">';
- $form .= '<label for="qdr_tss_fileid">' . __('File ID', 'qdr-temporary-shared-storage') . '</label>';
- $form .= '<input type="text" name="id" id="qdr_tss_fileid" required>';
- $form .= '</div>';
- } else {
- $form .= '<input type="hidden" name="id" value="' . esc_attr($fileid) . '">';
- }
-
- $form .= '<div class="qdr-tss-form-group">';
- $form .= '<label for="qdr_tss_password">' . __('Password', 'qdr-temporary-shared-storage') . '</label>';
- $form .= '<input type="password" name="qdr_tss_password" id="qdr_tss_password" required>';
- $form .= '</div>';
-
- $form .= '<div class="qdr-tss-form-group">';
- $form .= '<label for="qdr_tss_reference">' . __('Reference (if required)', 'qdr-temporary-shared-storage') . '</label>';
- $form .= '<input type="text" name="qdr_tss_reference" id="qdr_tss_reference">';
- $form .= '</div>';
-
- $form .= '<div class="qdr-tss-form-group">';
- $form .= '<button type="submit" name="qdr_tss_download_submit" class="qdr-tss-button">' . __('Download File', 'qdr-temporary-shared-storage') . '</button>';
- $form .= '</div>';
-
- $form .= '</form>';
- $form .= '</div>';
-
- return $form;
- }
- }
|