Uname: Linux premium294.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
Software: LiteSpeed
PHP version: 8.1.32 [ PHP INFO ] PHP os: Linux
Server Ip: 104.21.16.1
Your Ip: 216.73.216.223
User: mjbynoyq (1574) | Group: mjbynoyq (1570)
Safe Mode: OFF
Disable Function:
NONE

name : attachment-proxy.php
<?php

class Brizy_Public_AttachmentProxy extends Brizy_Public_AbstractProxy {

	use Brizy_Editor_Trait_Sanitize;

	const ENDPOINT = '_attachment';

	/**
	 * @return array
	 */
	protected function get_endpoint_keys() {
		return array( Brizy_Editor::prefix( self::ENDPOINT ) );
	}

	/**
	 * @return void
	 * @throws Exception
	 */
	public function process_query() {
		global $wp_query;

		$vars     = $wp_query->query_vars;
		$ENDPOINT = Brizy_Editor::prefix( self::ENDPOINT );

		if ( isset( $vars[ $ENDPOINT ] ) && is_string( $vars[ $ENDPOINT ] ) && ! empty( $vars[ $ENDPOINT ] ) ) {

			session_write_close();

			try {
				$uid        = $this->sanitizeUid($vars[ $ENDPOINT ]);
				$attachment = $this->getAttachment( $uid );

				if ( ! $attachment ) {
					if ( substr( $uid, 0, 3 ) !== 'wp-' ) {
						$mediaCache = new Brizy_Editor_CropCacheMedia( Brizy_Editor_Project::get() );
						$attachment = get_post( $mediaCache->download_original_image( $uid ) );
					} else {
						throw new Exception( 'File can not be found by uid: ' . $uid );
					}
				}

				$url = wp_get_attachment_url( $attachment->ID );
				wp_redirect( $url );
				exit;
			} catch ( Exception $e ) {
				Brizy_Logger::instance()->exception( $e );
				status_header( 404 );
				global $wp_query;
				$wp_query->set_404();

				return;
			}
		}
	}

	private function getAttachment( $hash ) {

		if ( is_numeric( $hash ) ) {
			$attachment = get_post( (int) $hash );
		} else {
			$attachment = $this->getAttachmentByPostId( $hash );

			if ( ! $attachment ) {
				$attachment = $this->getAttachmentByAttachmentUId( $hash );
			}
		}

		return $attachment;
	}


	/**
	 * @param $hash
	 *
	 * @return int|WP_Post|null
	 */
	private function getAttachmentByPostId( $hash ) {
		$attachments = get_posts( array(
			'meta_key'   => 'brizy_post_uid',
			'meta_value' => $hash,
			'post_type'  => 'attachment',
		) );

		if ( isset( $attachments[0] ) ) {
			return $attachments[0];
		}

		return null;
	}

	/**
	 * @param $hash
	 *
	 * @return int|WP_Post|null
	 */
	private function getAttachmentByAttachmentUId( $hash ) {
		$attachments = get_posts( array(
			'meta_key'   => 'brizy_attachment_uid',
			'meta_value' => $hash,
			'post_type'  => 'attachment',
		) );

		if ( isset( $attachments[0] ) ) {
			return $attachments[0];
		}

		return null;
	}
}
© 2025 XylotrechusZ