loader.php 10.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
<?php

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

/**
 * Class NF_FU_External_Loader
 */
class NF_FU_External_Loader {

	protected $class_prefix = 'NF_FU_External_Services_';

	public $wpoauth;

	static $min_php_version = '5.6.20';

	const NF_FU_VENDOR_NS_PREFIX = 'NF_FU_VENDOR';

	const OAUTH_PROXY_URL = 'https://oauth.ninjaforms.com/';

	protected static $services;

	/**
	 * @var array
	 */
	protected static $service_names;

	/**
	 * NF_FU_External_Loader constructor.
	 */
	public function __construct() {
		add_filter( 'ninja_forms_uploads_tabs', array( $this, 'add_menu_tab' ) );

		if ( ! self::is_compatible() ) {
			return;
		}

		if ( file_exists( NF_FU_BASE_DIR . '/vendor-dist/scoper-autoload.php' ) ) {
			require_once NF_FU_BASE_DIR . '/vendor-dist/scoper-autoload.php';
		} else if ( file_exists( NF_FU_BASE_DIR . '/vendor/autoload.php' ) ) {
			require_once NF_FU_BASE_DIR . '/vendor/autoload.php';
			\spl_autoload_register( array( $this, 'ensure_class_alias' ), true, true );
		}

		add_filter( 'ninja_forms_register_actions', array( $this, 'register_actions' ) );
		add_filter( 'ninja_forms_file_uploads_settings_whitelist', array( $this, 'add_settings_to_whitelist' ) );
		add_action( 'ninja_forms_uploads_external_template', array( $this, 'render_services_settings' ) );
		add_filter( 'ninja_forms_uploads_file_url', array( $this, 'generate_url' ), 10, 2);
		add_filter( 'ninja_forms_uploads_file_location', array( $this, 'get_service_display_name' ) );
		add_action( 'ninja_forms_loaded', array( $this, 'init' ) );

		add_action( 'admin_init', array( $this, 'handle_external_url_admin' ) );
		add_action( 'template_redirect', array( $this, 'handle_external_url_public' ) );
	}

	public function init() {
		// Initialize the WP OAuth library to setup the admin handler for tokens
		$redirect = NF_File_Uploads()->page->get_url( 'external', array(), false );
		$this->wpoauth()->register_admin_handler( $redirect );
	}

	/**
	 * PHP 5.2 safe code to get the WP OAuth instance.
	 *
	 * @return mixed
	 */
	public function wpoauth() {
		if ( $this->wpoauth ) {
			return $this->wpoauth;
		}

		$class         = self::NF_FU_VENDOR_NS_PREFIX . '\\Polevaultweb\\WPOAuth2\\WPOAuth2';
		$this->wpoauth = call_user_func( array( $class, 'instance' ), self::OAUTH_PROXY_URL );

		return $this->wpoauth;
	}

	/**
	 * Makes sure a class alias is created when a base class exists.
	 *
	 * @param string $class Class to create alias for.
	 *
	 * @return void
	 */
	public function ensure_class_alias( $class ) {
		// If the namespace beings with the dependency class prefix, make an alias for regular class.
		if ( strpos( $class, self::NF_FU_VENDOR_NS_PREFIX ) !== 0) {
			return;
		}
		$base = substr( $class, ( strlen( self::NF_FU_VENDOR_NS_PREFIX ) + 1 ) );
		if ( ! class_exists( $base ) ) {
			return;
		}

		class_alias( $base, $class );
	}

	/**
	 * Is the site compatible for external uploads
	 *
	 * @return bool
	 */
	public static function is_compatible() {
		return version_compare( PHP_VERSION, self::$min_php_version, '>=' );
	}

	/**
	 * Get the external services we can upload files to
	 *
	 * @return array
	 */
	public function get_services() {
		if ( ! empty( self::$services ) ) {
			return self::$services;
		}

		$default_services = glob( dirname( NF_File_Uploads()->plugin_file_path ) . '/includes/external/services/*', GLOB_ONLYDIR );
		$default_services = array_map( 'basename', $default_services );

		$services = array();
		foreach ( $default_services as $service ) {
			$file = dirname( NF_File_Uploads()->plugin_file_path ) . '/includes/external/services/' . $service . '/service.php';

			$services[ $file ] = $service;
		}

		/**
		 * External services
		 *
		 * Array of external services, key is the path to service file, value is service name slug
		 *
		 * @return array $services
		 */
		self::$services = apply_filters( 'ninja_forms_uploads_external_service', $services );

		return self::$services;
	}

	/**
	 * Get external service
	 *
	 * @param string     $service
	 * @param null|array $services
	 *
	 * @return bool|NF_FU_External_Service
	 */
    public function get( $service, $services = null ) {
        $service = $this->get_upload_location( $service );

	    if ( is_null( $services ) ) {
		    $services = $this->get_services();
	    }

	    if ( ! in_array( $service, $services ) ) {
		    return false;
	    }

	    $file = array_search( $service, $services );
	    if ( ! file_exists( $file ) ) {
		    return false;
	    }

	    $service = str_replace( ' ', '_', ucwords( str_replace( '-', ' ', $service ) ) );
		$class   = $this->class_prefix . $service . '_Service';

	    if ( ! method_exists( $class, 'instance') ) {
	    	return false;
	    }

		$external = call_user_func( array( $class, 'instance' ) );

		return $external;
	}

	/**
	 * Are there any services that are ready to use?
	 *
	 * @return bool
	 */
	protected function is_connected_services() {
		$services = $this->get_services();
		foreach ( $services as $service ) {
			if ( ! ( $instance = $this->get( $service, $services ) ) ) {
				continue;
			}

			if ( $instance->is_compatible() && $instance->is_connected() ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Register actions for external services
	 *
	 * @param array $actions
	 *
	 * @return array
	 */
	public function register_actions( $actions ) {
		if ( $this->is_connected_services() ) {
			$actions['file-upload-external'] = new NF_FU_External_Action();
		}

		return $actions;
	}

	/**
	 * Add the external tab to the settings page
	 *
	 * @param $tabs
	 *
	 * @return mixed
	 */
	public function add_menu_tab( $tabs ) {
		$tabs['external'] = __( 'External Settings', 'ninja-forms-uploads' );

		return $tabs;
	}

	/**
	 * Add external settings to the whitelist for saving from settings page
	 *
	 * @param array $whitelist
	 *
	 * @return array
	 */
	public function add_settings_to_whitelist( $whitelist ) {
		$external_config = NF_File_Uploads()->config( 'settings-external' );
		$whitelist = array_merge( $whitelist, $external_config );

		$services = $this->get_services();
		foreach ( $services as $service ) {
			if ( ! ( $instance = $this->get( $service, $services ) ) ) {
				continue;
			}
			$settings  = $instance->get_settings();
			$whitelist = array_merge( $whitelist, $settings );
		}

		return $whitelist;
	}

	/**
	 * Render the settings for the services on the External settings page
	 */
	public function render_services_settings() {
		$services = $this->get_services();

		foreach ( $services as $service ) {
			if ( ! ( $instance = $this->get( $service, $services ) ) ) {
				continue;
			}

			$requirements = $instance->get_missing_requirements();
			if ( $requirements ) {
				$message = sprintf( __( '%s requires %s', 'ninja-forms-uploads' ), $instance->get_name(), implode( ', ', $requirements ) );
				printf( '<div class="error"><p>%s</p></div>', $message );

				continue;
			}

			$notices = $instance->get_notices();
			if ( ! empty( $notices ) ) {
				foreach ( $notices as $notice ) {
					printf( '<div class="%s"><p>%s</p></div>', $notice['type'], $notice['message'] );
				}
			}

			$args = array(
				'group'       => $service,
				'group_label' => sprintf( __( '%s Settings', 'ninja-forms-uploads' ), $instance->get_name() ),
				'settings'    => $instance->get_settings(),
			);

			NF_File_Uploads()->template( 'admin-menu-meta-box', $args );
		}
	}

	/**
	 * Use a public URL for external file upload URLs
	 *
	 * @return bool
	 */
	protected function use_public_url() {
    	if ( defined( 'NINJA_FORMS_UPLOADS_USE_PUBLIC_URL' ) && NINJA_FORMS_UPLOADS_USE_PUBLIC_URL ) {
			return true;
		}

		if ( (bool) NF_File_Uploads()->controllers->settings->get_setting( 'external_public_url' ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Get the external URL for the file upload
	 *
	 * @param string $file_url
	 * @param array  $data
	 *
	 * @return string
	 */
	public function generate_url( $file_url, $data ) {
		if ( ! isset( $data['upload_id'] ) ) {
			return $file_url;
		}

		if ( ! isset( $data['external_locations'] ) && isset( $data['upload_location'] ) && 'server' === $data['upload_location'] ) {
			// Not uploaded, or uploading to an external service
			return $file_url;
		}

		$url_path = '?nf-upload=' . $data['upload_id'];
		if ( $this->use_public_url() ) {
			$file_url = home_url( $url_path );
		} else {
			$file_url = admin_url( $url_path );
		}

		return $file_url;
	}

	/**
	 *  Listen for external service file URLs and redirect to the service URL
	 *
	 */
	public function handle_external_url_admin() {
		$upload_id = $this->is_external_file_id();
		if ( ! $upload_id ) {
			return;
		}

		$this->get_upload_external_url( $upload_id ) ;
	}

	/**
	 * Listen for external service file URLs and redirect to the service URL with a public URL
	 */
	public function handle_external_url_public() {
		$upload_id = $this->is_external_file_id();
		if ( ! $upload_id ) {
			return;
		}

		if ( ! $this->use_public_url() ) {
			return;
		}

		$this->get_upload_external_url( $upload_id ) ;
	}

	/**
	 * @return bool|int
	 */
	protected function is_external_file_id() {
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
			return false;
		}

		$upload_id = filter_input( INPUT_GET, 'nf-upload', FILTER_VALIDATE_INT );

		if ( ! isset( $upload_id ) ) {
			return false;
		}

		return $upload_id;
	}

	/**
	 * @param int $upload_id
	 */
	public function get_upload_external_url( $upload_id ) {
		$upload  = NF_File_Uploads()->controllers->uploads->get( $upload_id );
		$service = filter_input( INPUT_GET, 'service' );
		if ( empty( $service ) ) {
			$service = $upload->upload_location;
		}

		if ( isset( $upload->external_locations ) && count( $upload->external_locations ) > 0 && 'server' === $service ) {
			// File not uploaded to service yet, redirect to the local file
			wp_redirect( $upload->file_url );
			die();
		}

		if ( ! ( $instance = $this->get( $service ) ) ) {
			// Service no longer configured
			wp_redirect( $upload->file_url );
			die();
		}

		$path     = ( isset( $upload->external_path ) ) ? $upload->external_path : '';
		$filename = ( isset( $upload->external_filename ) ) ? $upload->external_filename : $upload->file_name;
		$file_url = $instance->get_url( $filename, $path, $upload->data );

		wp_redirect( $file_url );
		die();
	}

	/**
	 * Backwards compatible upload location
	 *
	 * @param string $location
	 *
	 * @return string
	 */
	protected function get_upload_location( $location ) {
		if ( 'amazon' === $location ) {
			return 's3';
		}

		return $location;
	}

	/**
	 * Get the display name for a service by slug
	 *
	 * @param string $slug
	 *
	 * @return string
	 */
	public function get_service_display_name( $slug ) {
		$slug = $this->get_upload_location( $slug );

		if ( isset( self::$service_names[ $slug ] ) ) {
			return self::$service_names[ $slug ];
		}

		$service_name = ucwords( $slug );

		$service = $this->get( $slug );

		if ( $service ) {
			$service_name = $service->name;
		}

		self::$service_names[ $slug ] = $service_name;

		return $service_name;
	}

}