HEX
Server: Apache/2.4.63 (Unix)
System: Linux TOMS-220NAS 4.4.302+ #86009 SMP Wed Nov 26 18:19:17 CST 2025 x86_64
User: flavio87 (1026)
PHP: 8.3.27
Disabled: NONE
Upload Files
File: /volume1/web/qrcodesforeveryone/wp-content/plugins/dk-pdf-storage/dk-pdf-storage - Copy.php
<?php
/**
 * Plugin Name: DK PDF Storage
 * Description: Automatically saves generated PDFs to the server.
 * Version: 1.0.0
 * Requires at least: 5.9
 * Requires PHP: 8.0
 * Author: Emili Castells
 * Author URI: https://dinamiko.dev
 */

declare( strict_types=1 );

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

add_action( 'dkpdf_before_output', 'dkpdf_storage_save_pdf', 10, 2 );

/**
 * Save PDF to the server before it's output to the browser.
 *
 * @param object $mpdf The populated mPDF instance.
 * @param string $title The PDF title/filename.
 */
function dkpdf_storage_save_pdf( object $mpdf, string $title ): void {
	$storage_dir = dkpdf_storage_get_directory();

	if ( ! dkpdf_storage_ensure_directory( $storage_dir ) ) {
		return;
	}

	$format   = apply_filters( 'dkpdf_pdf_format', 'A4' );
	$filename = sanitize_file_name( $title ) . '-' . $format . '.pdf';
	$filepath = trailingslashit( $storage_dir ) . $filename;

	try {
		$mpdf->Output( $filepath, 'F' );
	} catch ( \Exception $e ) {
		error_log( 'DK PDF Storage: Failed to save PDF - ' . $e->getMessage() );
	}
}

/**
 * Get the storage directory path.
 *
 * @return string
 */
function dkpdf_storage_get_directory(): string {
	$upload_dir = wp_upload_dir();
	return trailingslashit( $upload_dir['basedir'] ) . 'dkpdf-storage';
}

/**
 * Ensure storage directory exists with security files.
 *
 * @param string $path Directory path.
 * @return bool True if directory exists or was created.
 */
function dkpdf_storage_ensure_directory( string $path ): bool {
	if ( is_dir( $path ) ) {
		return true;
	}

	if ( ! wp_mkdir_p( $path ) ) {
		return false;
	}

	$htaccess = trailingslashit( $path ) . '.htaccess';
	if ( ! file_exists( $htaccess ) ) {
		file_put_contents( $htaccess, 'deny from all' );
	}

	$index = trailingslashit( $path ) . 'index.php';
	if ( ! file_exists( $index ) ) {
		file_put_contents( $index, '<?php // Silence is golden' );
	}

	return true;
}