<?php
/**
 * All-in-One WP Migration 
 * Copyright (C) 2014-2018 ServMask Inc.
 *
 * Modified Copyright (C) 2025 Lead Connector
 * 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

if ( ! defined( 'ABSPATH' ) ) {
	die( 'Well Sir You Cannot Jump Here...haha' );
}


class Lcm_GCloud_Storage_Client {
	/**
	 * OAuth refresh token
	 *
	 * @var string
	 */
	protected $session_url = null;

	/**
	 * Upload URL
	 *
	 * @var string
	 */
	protected $upload_url = null;

	/**
	 * SSL mode
	 *
	 * @var boolean
	 */
	protected $ssl = null;

	public function __construct( $session_url, $ssl = true ) {
		$this->session_url = $session_url;
		$this->ssl           = $ssl;
	}

	/**
	 * Load upload URL
	 *
	 * @param  string $url Upload URL
	 * @return void
	 */
	public function load_upload_url( $url ) {
		$this->upload_url = $url;
	}

	
	/**
	 * Upload file chunk using signed URL
	 *
	 * @param  string  $file_chunk_data  File chunk data
	 * @param  integer $file_size        File size
	 * @param  integer $file_range_start File range start
	 * @param  integer $file_range_end   File range end
	 * @param  string  $signed_url       Signed URL for upload
	 * @return array
	 */
	public function upload_file_chunk( $file_chunk_data, $file_size, $file_range_start = 0, $file_range_end = 0, $session_url = null ) {
		$api = new Lcm_GCloud_Storage_Curl;
		$api->set_ssl( $this->ssl );
		if ($session_url) {
			$api->set_base_url($session_url);
		} else {
			throw new Lcm_Error_Exception( __( 'Session URL is not specified.', LC_MIGRATOR_PLUGIN_NAME ) );
		}

		$api->set_option( CURLOPT_CUSTOMREQUEST, 'PUT' );
		$api->set_option( CURLOPT_POSTFIELDS, $file_chunk_data );
		
		// Performance headers
		$api->set_header( 'Content-Length', strlen( $file_chunk_data ) );
		$api->set_header( 'Content-Range', sprintf( 'bytes %d-%d/%d', $file_range_start, $file_range_end, $file_size ) );
		// $api->set_header( 'Connection', 'keep-alive' ); // Reuse connections
		// $api->set_header( 'Expect', '' ); // Disable 100-Continue delay
		
		try {
			$response = $api->make_request( true );
		} catch ( Lcm_Error_Exception $e ) {
			throw $e;
		}
		return $response;
	}
	
	/**
	 * Left trim URL path forward slash
	 *
	 * @param  string $path Base path
	 * @return string
	 */
	public function left_trim_forward_slash( $path ) {
		return ltrim( $path, '/' );
	}

	/**
	 * Sanitize URL path
	 *
	 * @param  string $path Base path
	 * @return string
	 */
	public function sanitize_path( $path ) {
		return preg_replace( '#[\\\/]+#', '/', $path );
	}

	/**
	 * Encode URL object name
	 *
	 * @param  string $object Object name
	 * @return string
	 */
	public function rawurlencode_object( $object ) {
		return str_replace( '%7E', '~', rawurlencode( $object ) );
	}

	/**
	 * Encode URL query
	 *
	 * @param  array $query Base query
	 * @return array
	 */
	public function rawurlencode_query( $query ) {
		return array_map( array( $this, 'rawurlencode_object' ), array_filter( $query, 'is_scalar' ) );
	}
}
