<commit_before><?

defined('C5_EXECUTE') or die("Access Denied.");

class Marketplace {
	
	const E_INVALID_BASE_URL = 20;
	const E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED = 21;
	const E_UNRECOGNIZED_SITE_TOKEN = 22;
	const E_GENERAL_CONNECTION_ERROR = 99;

	protected $isConnected = false;
	protected $connectionError = false;
	
	public static function getInstance() {
		static $instance;
		if (!isset($instance)) {
			$m = __CLASS__;
			$instance = new $m;
		}
		return $instance;
	}
	
	public function __construct() {
		if (defined('ENABLE_MARKETPLACE_SUPPORT') && ENABLE_MARKETPLACE_SUPPORT == false) {
			$this->connectionError = Marketplace::E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED;
			return;
		}

		$csToken = Config::get('MARKETPLACE_SITE_TOKEN');
		if ($csToken != '') {

			$fh = Loader::helper('file');
			$csiURL = urlencode(BASE_URL . DIR_REL);
			$url = MARKETPLACE_URL_CONNECT_VALIDATE."?csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION;
			$vn = Loader::helper('validation/numbers');
			$r = $fh->getContents($url);
			if ($r == false) {
				$this->isConnected = true;
			} else if ($vn->integer($r)) {
				$this->isConnected = false;
				$this->connectionError = $r;
			} else {
				$this->isConnected = false;
				$this->connectionError = self::E_GENERAL_CONNECTION_ERROR;
			}
		}		
	}
	
	public function isConnected() {
		return $this->isConnected;
	}
	
	public function hasConnectionError() {
		return $this->connectionError != false;
	}
	
	public function getConnectionError() {
		return $this->connectionError;
	}
	
	public function generateSiteToken() {
		$fh = Loader::helper('file');
		$token = $fh->getContents(MARKETPLACE_URL_CONNECT_TOKEN_NEW);
		return $token;	
	}

	public function getSiteToken() {
		$token = Config::get('MARKETPLACE_SITE_TOKEN');
		return $token;
	}
	
	public function getSitePageURL() {
		$token = Config::get('MARKETPLACE_SITE_URL_TOKEN');
		return MARKETPLACE_BASE_URL_SITE_PAGE . '/' . $token;
	}

	public static function downloadRemoteFile($file) {
		$fh = Loader::helper('file');
		$file .= '?csiURL=' . urlencode(BASE_URL . DIR_REL);
		$pkg = $fh->getContents($file);
		if (empty($pkg)) {
			return Package::E_PACKAGE_DOWNLOAD;
		}

		$file = time();
		// Use the same method as the Archive library to build a temporary file name.
		$tmpFile = $fh->getTemporaryDirectory() . '/' . $file . '.zip';
		$fp = fopen($tmpFile, "wb");
		if ($fp) {
			fwrite($fp, $pkg);
			fclose($fp);
		} else {
			return Package::E_PACKAGE_SAVE;
		}
		
		return $file;
	}
	
	public function getMarketplaceFrame($width = '100%', $height = '300', $completeURL = false) {
		// if $mpID is passed, we are going to either
		// a. go to its purchase page
		// b. pass you through to the page AFTER connecting.
		$tp = new TaskPermission();
		if ($tp->canInstallPackages()) {
			if (!$this->isConnected()) {
				$url = MARKETPLACE_URL_CONNECT;
				if (!$completeURL) {
					$completeURL = BASE_URL . View::url('/dashboard/extend/connect', 'connect_complete');
				}
				$csReferrer = urlencode($completeURL);
				$csiURL = urlencode(BASE_URL . DIR_REL);
				$csiBaseURL = urlencode(BASE_URL);
				if ($this->hasConnectionError()) {
					$csToken = $this->getSiteToken();
				} else {
					// new connection 
					$csToken = Marketplace::generateSiteToken();
				}
				$url = $url . '?ts=' . time() . '&csiBaseURL=' . $csiBaseURL . '&csiURL=' . $csiURL . '&csToken=' . $csToken . '&csReferrer=' . $csReferrer . '&csName=' . htmlspecialchars(SITE, ENT_QUOTES, APP_CHARSET);
			} else {
				$csiBaseURL = urlencode(BASE_URL);
				$url = MARKETPLACE_URL_CONNECT_SUCCESS . '?csToken=' . $this->getSiteToken() . '&csiBaseURL=' . $csiBaseURL;
			}
			if ($csToken == false && !$this->isConnected()) {
				return '<div class="ccm-error">' . t('Unable to generate a marketplace token. Please ensure that allow_url_fopen is turned on, or that cURL is enabled on your server. If these are both true, It\'s possible your site\'s IP address may be blacklisted for some reason on our server. Please ask your webhost what your site\'s outgoing cURL request IP address is, and email it to us at <a href="mailto:<EMAIL>"><EMAIL></a>.') . '</div>';
			} else {
				$time = time();
				$ifr = '<script type="text/javascript">$(function() { $.receiveMessage(function(e) { 
					var eh = e.data;
					eh = parseInt(eh) + 20;
					$("#ccm-marketplace-frame-' . $time . '").attr("height", eh); 
					}, \'' . CONCRETE5_ORG_URL . '\');	
				});	
				</script>';
				$ifr .= '<iframe id="ccm-marketplace-frame-' . $time . '" frameborder="0" width="' . $width . '" height="' . $height . '" src="' . $url . '"></iframe>';
				return $ifr;
			}
		} else {
			return '<div class="ccm-error">' . t('You do not have permission to connect this site to the marketplace.') . '</div>';
		}
	}

	public function getMarketplacePurchaseFrame($mp, $width = '100%', $height = '530') {
		$tp = new TaskPermission();
		if ($tp->canInstallPackages()) {
			if ($this->isConnected()) {
				$url = MARKETPLACE_URL_CHECKOUT;
				$csiURL = urlencode(BASE_URL . DIR_REL);
				$csiBaseURL = urlencode(BASE_URL);
				$csToken = $this->getSiteToken();
				$url = $url . '/' . $mp->getProductBlockID() . '?ts=' . time() . '&csiBaseURL=' . $csiBaseURL . '&csiURL=' . $csiURL . '&csToken=' . $csToken;
			}<commit_msg>better loading for 5.5 marketplace<commit_after><?

defined('C5_EXECUTE') or die("Access Denied.");

class Marketplace {
	
	const E_INVALID_BASE_URL = 20;
	const E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED = 21;
	const E_UNRECOGNIZED_SITE_TOKEN = 22;
	const E_GENERAL_CONNECTION_ERROR = 99;

	protected $isConnected = false;
	protected $connectionError = false;
	
	public static function getInstance() {
		static $instance;
		if (!isset($instance)) {
			$m = __CLASS__;
			$instance = new $m;
		}
		return $instance;
	}
	
	public function __construct() {
		if (defined('ENABLE_MARKETPLACE_SUPPORT') && ENABLE_MARKETPLACE_SUPPORT == false) {
			$this->connectionError = Marketplace::E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED;
			return;
		}

		$csToken = Config::get('MARKETPLACE_SITE_TOKEN');
		if ($csToken != '') {

			$fh = Loader::helper('file');
			$csiURL = urlencode(BASE_URL . DIR_REL);
			$url = MARKETPLACE_URL_CONNECT_VALIDATE."?csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION;
			$vn = Loader::helper('validation/numbers');
			$r = $fh->getContents($url);
			if ($r == false) {
				$this->isConnected = true;
			} else if ($vn->integer($r)) {
				$this->isConnected = false;
				$this->connectionError = $r;
			} else {
				$this->isConnected = false;
				$this->connectionError = self::E_GENERAL_CONNECTION_ERROR;
			}
		}		
	}
	
	public function isConnected() {
		return $this->isConnected;
	}
	
	public function hasConnectionError() {
		return $this->connectionError != false;
	}
	
	public function getConnectionError() {
		return $this->connectionError;
	}
	
	public function generateSiteToken() {
		$fh = Loader::helper('file');
		$token = $fh->getContents(MARKETPLACE_URL_CONNECT_TOKEN_NEW);
		return $token;	
	}

	public function getSiteToken() {
		$token = Config::get('MARKETPLACE_SITE_TOKEN');
		return $token;
	}
	
	public function getSitePageURL() {
		$token = Config::get('MARKETPLACE_SITE_URL_TOKEN');
		return MARKETPLACE_BASE_URL_SITE_PAGE . '/' . $token;
	}

	public static function downloadRemoteFile($file) {
		$fh = Loader::helper('file');
		$file .= '?csiURL=' . urlencode(BASE_URL . DIR_REL);
		$pkg = $fh->getContents($file);
		if (empty($pkg)) {
			return Package::E_PACKAGE_DOWNLOAD;
		}

		$file = time();
		// Use the same method as the Archive library to build a temporary file name.
		$tmpFile = $fh->getTemporaryDirectory() . '/' . $file . '.zip';
		$fp = fopen($tmpFile, "wb");
		if ($fp) {
			fwrite($fp, $pkg);
			fclose($fp);
		} else {
			return Package::E_PACKAGE_SAVE;
		}
		
		return $file;
	}
	
	public function getMarketplaceFrame($width = '100%', $height = '300', $completeURL = false) {
		// if $mpID is passed, we are going to either
		// a. go to its purchase page
		// b. pass you through to the page AFTER connecting.
		$tp = new TaskPermission();
		if ($tp->canInstallPackages()) {
			if (!$this->isConnected()) {
				$url = MARKETPLACE_URL_CONNECT;
				if (!$completeURL) {
					$completeURL = BASE_URL . View::url('/dashboard/extend/connect', 'connect_complete');
				}
				$csReferrer = urlencode($completeURL);
				$csiURL = urlencode(BASE_URL . DIR_REL);
				$csiBaseURL = urlencode(BASE_URL);
				if ($this->hasConnectionError()) {
					$csToken = $this->getSiteToken();
				} else {
					// new connection 
					$csToken = Marketplace::generateSiteToken();
				}
				$url = $url . '?ts=' . time() . '&csiBaseURL=' . $csiBaseURL . '&csiURL=' . $csiURL . '&csToken=' . $csToken . '&csReferrer=' . $csReferrer . '&csName=' . htmlspecialchars(SITE, ENT_QUOTES, APP_CHARSET);
			} else {
				$csiBaseURL = urlencode(BASE_URL);
				$url = MARKETPLACE_URL_CONNECT_SUCCESS . '?csToken=' . $this->getSiteToken() . '&csiBaseURL=' . $csiBaseURL;
			}
			if ($csToken == false && !$this->isConnected()) {
				return '<div class="ccm-error">' . t('Unable to generate a marketplace token. Please ensure that allow_url_fopen is turned on, or that cURL is enabled on your server. If these are both true, It\'s possible your site\'s IP address may be blacklisted for some reason on our server. Please ask your webhost what your site\'s outgoing cURL request IP address is, and email it to us at <a href="mailto:<EMAIL>.org"><EMAIL></a>.') . '</div>';
			} else {
				$time = time();
				$ifr = '<script type="text/javascript">$(function() { $.receiveMessage(function(e) { 
					jQuery.fn.dialog.hideLoader();

					if (e.data == "loading") {
						jQuery.fn.dialog.showLoader();
					} else { 
						var eh = e.data;
						eh = parseInt(eh) + 20;
						$("#ccm-marketplace-frame-' . $time . '").attr("height", eh); 
					}
					
					}, \'' . CONCRETE5_ORG_URL . '\');	
				});	
				</script>';
				$ifr .= '<iframe id="ccm-marketplace-frame-' . $time . '" frameborder="0" width="' . $width . '" height="' . $height . '" src="' . $url . '"></iframe>';
				return $ifr;
			}
		} else {
			return '<div class="ccm-error">' . t('You do not have permission to connect this site to the marketplace.') . '</div>';
		}
	}

	public function getMarketplacePurchaseFrame($mp, $width = '100%', $height = '530') {
		$tp = new TaskPermission();
		if ($tp->canInstallPackages()) {
			if ($this->isConnected()) {
				$url = MARKETPLACE_URL_CHECKOUT;
				$csiURL = urlencode(BASE_URL . DIR_REL);
				$csiBaseURL = urlencode(BASE_URL);
				$csToken = $this->getSiteToken();
				$url = $url . '/' . $mp->getProductBlockID() . '?ts=' . time() . '&csiBaseURL=' . $csiBaseURL . '&csiURL=' . $csiURL . '&csToken=' . $csToken;
			}
{'fraction_non_alphanumeric': 0.16852161009089223, 'fraction_numerical': 0.004822852902986459, 'mean_word_length': 3.6478448275862068, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3091006', 'n_tokens_mistral': 4361, 'n_tokens_neox': 3786, 'n_words': 842}