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 '
' . __('File not found.', 'qdr-temporary-shared-storage') . '
';
}
// Check if file is active
$current_time = current_time('mysql');
if ($current_time < $item->active_from) {
return '' . __('This file is not yet available for download.', 'qdr-temporary-shared-storage') . '
';
}
if ($current_time > $item->active_to) {
return '' . __('This file has expired and is no longer available for download.', 'qdr-temporary-shared-storage') . '
';
}
// 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 '' . __('Security check failed.', 'qdr-temporary-shared-storage') . '
';
}
// Verify password
$password = isset($_POST['qdr_tss_password']) ? $_POST['qdr_tss_password'] : '';
if (!wp_check_password($password, $item->password)) {
return '' . __('Invalid password.', 'qdr-temporary-shared-storage') . '
' . $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 '' . __('Invalid reference.', 'qdr-temporary-shared-storage') . '
' . $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 '' . __('File not found on server.', 'qdr-temporary-shared-storage') . '
';
}
}
// Show download form with file information
$output = '';
$output .= '
' . __('File Information', 'qdr-temporary-shared-storage') . '
';
$output .= '
' . __('File Name:', 'qdr-temporary-shared-storage') . ' ' . esc_html($item->original_file_name) . '
';
if (!empty($item->description)) {
$output .= '
' . __('Description:', 'qdr-temporary-shared-storage') . ' ' . esc_html($item->description) . '
';
}
if (!empty($item->message)) {
$output .= '
' . __('Message:', 'qdr-temporary-shared-storage') . ' ' . esc_html($item->message) . '
';
}
$output .= '
';
$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 = '';
return $form;
}
}