<commit_before><?php
/**
 * This file is part of the DreamFactory Services Platform(tm) (DSP)
 *
 * DreamFactory Services Platform(tm) <http://github.com/dreamfactorysoftware/dsp-core>
 * Copyright 2012-2013 DreamFactory Software, Inc. <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use Kisma\Core\Enums\DateTime;
use Kisma\Core\Enums\HttpResponse;
use Kisma\Core\SeedUtility;

//use Kisma\Core\Utility\Curl;
use Kisma\Core\Utility\FilterInput;
use Kisma\Core\Utility\Log;
use Platform\Utility\Curl;
use Platform\Yii\Utility\Pii;

/**
 * Fabric.php
 * The configuration file for fabric-hosted DSPs
 */
class Fabric extends SeedUtility
{
	//*************************************************************************
	//* Constants
	//*************************************************************************

	/**
	 * @var string
	 */
	const DEFAULT_AUTH_ENDPOINT = 'http://cerberus.fabric.dreamfactory.com/api/instance/credentials';
	/**
	 * @var string
	 */
	const DSP_DB_CONFIG_FILE_NAME_PATTERN = '%%INSTANCE_NAME%%.database.config.php';
	/**
	 * @var string
	 */
	const DSP_DEFAULT_SUBDOMAIN = '.cloud.dreamfactory.com';
	/**
	 * @var string My favorite cookie
	 */
	const FigNewton = 'dsp.blob';
	/**
	 * @var string My favorite cookie
	 */
	const PrivateFigNewton = 'dsp.private';
	/**
	 * @var string
	 */
	const BaseStorage = '/data/storage';
	/**
	 * @var string
	 */
	const FABRIC_MARKER = '/var/www/.fabric_hosted';
	/**
	 * @var int
	 */
	const EXPIRATION_THRESHOLD = 30;
	/**
	 * @var string
	 */
	const DEFAULT_DOC_ROOT = '/var/www/launchpad/web';

	//*************************************************************************
	//* Methods
	//*************************************************************************

	/**
	 * @return string
	 */
	public static function getHostName()
	{
		return FilterInput::server( 'HTTP_HOST', gethostname() );
	}

	/**
	 * @return bool True if this DSP is fabric-hosted
	 */
	public static function fabricHosted()
	{
		return static::DEFAULT_DOC_ROOT == FilterInput::server( 'DOCUMENT_ROOT' ) && file_exists( static::FABRIC_MARKER );
	}

	/**
	 * @param bool $returnHost If true and the host is private, the host name is returned instead of TRUE. FALSE is still returned if false
	 *
	 * @return bool
	 */
	public static function hostedPrivatePlatform( $returnHost = false )
	{
		/**
		 * Add host names to this list to white-list...
		 */
		static $_allowedHosts
		= array(
			'launchpad-dev.dreamfactory.com',
			'launchpad-demo.dreamfactory.com',
		);

		$_host = static::getHostName();

		return in_array( $_host, $_allowedHosts ) ? ( $returnHost ? $_host : true ) : false;
	}

	/**
	 * @return array|mixed
	 * @throws RuntimeException
	 * @throws CHttpException
	 */
	public static function initialize()
	{
		global $_dbName, $_instance, $_dspName;

		//	If this isn't a cloud request, bail
		$_host = static::getHostName();

		if ( false !== static::hostedPrivatePlatform() && false === strpos( $_host, static::DSP_DEFAULT_SUBDOMAIN ) )
		{
			Log::error( 'Attempt to access system from non-provisioned host: ' . $_host );
			throw new \CHttpException( HttpResponse::Forbidden, 'You are not authorized to access this system you cheeky devil you. (' . $_host . ').' );
		}

		//	What has it gots in its pocketses? Cookies first, then session
		$_privateKey
			= FilterInput::cookie( static::PrivateFigNewton, FilterInput::session( static::PrivateFigNewton ), \Kisma::get( 'platform.user_key' ) );
		$_dspName = str_ireplace( static::DSP_DEFAULT_SUBDOMAIN, null, $_host );

		$_dbConfigFileName = str_ireplace(
			'%%INSTANCE_NAME%%',
			$_dspName,
			static::DSP_DB_CONFIG_FILE_NAME_PATTERN
		);

		//	Try and get them from server...
		if ( false === ( list( $_settings, $_instance ) = static::_checkCache( $_host ) ) )
		{
			//	Otherwise we need to build it.
			$_parts = explode( '.', $_host );
			$_dbName = str_replace( '-', '_', $_dspName = $_parts[0] );

			//	Otherwise, get the credentials from the auth server...
//			Log::info( 'Credentials pull' );
			$_response = Curl::get( static::DEFAULT_AUTH_ENDPOINT . '/' . $_dspName . '/database' );

			if ( HttpResponse::NotFound == Curl::getLastHttpCode() )
			{
				Log::error( 'DB Credential pull failure. Redirecting to df.com' );
				header( 'Location: http://dreamfactory.com/' );
				exit();
			}

			if ( is_object( $_response ) && isset( $_response->details, $_response->details->code ) && HttpResponse::NotFound == $_response->details->code )
			{
//				Log::error( 'Instance "' . $_dspName . '" not found during web initialize.' );
				throw new \CHttpException( HttpResponse::NotFound, 'Instance not available.' );
			}

			if ( !$_response || !is_object( $_response ) || false == $_response->success )
			{
				error_log( 'Error connecting to Cerberus Authentication System: ' . print_r( $_response, true ) );
				throw new \CHttpException( HttpResponse::InternalServerError, 'Cannot connect to authentication service' );
			}

			$_instance = $_cache = $_response->details;
			$_dbName = $_instance->db_name;
			$_dspName = $_instance->instance->instance_name_text;
			\Kisma::set( 'dsp.credentials', $_cache );
			\Kisma::set( 'platform.private_path', $_privatePath = $_cache->private_path );
			$_privateKey = basename( dirname( $_privatePath ) );<commit_msg>Changed redirect URL to new DSP Not Found page<commit_after><?php
/**
 * This file is part of the DreamFactory Services Platform(tm) (DSP)
 *
 * DreamFactory Services Platform(tm) <http://github.com/dreamfactorysoftware/dsp-core>
 * Copyright 2012-2013 DreamFactory Software, Inc. <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use Kisma\Core\Enums\DateTime;
use Kisma\Core\Enums\HttpResponse;
use Kisma\Core\SeedUtility;

//use Kisma\Core\Utility\Curl;
use Kisma\Core\Utility\FilterInput;
use Kisma\Core\Utility\Log;
use Platform\Utility\Curl;
use Platform\Yii\Utility\Pii;

/**
 * Fabric.php
 * The configuration file for fabric-hosted DSPs
 */
class Fabric extends SeedUtility
{
	//*************************************************************************
	//* Constants
	//*************************************************************************

	/**
	 * @var string
	 */
	const DEFAULT_AUTH_ENDPOINT = 'http://cerberus.fabric.dreamfactory.com/api/instance/credentials';
	/**
	 * @var string
	 */
	const DSP_DB_CONFIG_FILE_NAME_PATTERN = '%%INSTANCE_NAME%%.database.config.php';
	/**
	 * @var string
	 */
	const DSP_DEFAULT_SUBDOMAIN = '.cloud.dreamfactory.com';
	/**
	 * @var string My favorite cookie
	 */
	const FigNewton = 'dsp.blob';
	/**
	 * @var string My favorite cookie
	 */
	const PrivateFigNewton = 'dsp.private';
	/**
	 * @var string
	 */
	const BaseStorage = '/data/storage';
	/**
	 * @var string
	 */
	const FABRIC_MARKER = '/var/www/.fabric_hosted';
	/**
	 * @var int
	 */
	const EXPIRATION_THRESHOLD = 30;
	/**
	 * @var string
	 */
	const DEFAULT_DOC_ROOT = '/var/www/launchpad/web';

	//*************************************************************************
	//* Methods
	//*************************************************************************

	/**
	 * @return string
	 */
	public static function getHostName()
	{
		return FilterInput::server( 'HTTP_HOST', gethostname() );
	}

	/**
	 * @return bool True if this DSP is fabric-hosted
	 */
	public static function fabricHosted()
	{
		return static::DEFAULT_DOC_ROOT == FilterInput::server( 'DOCUMENT_ROOT' ) && file_exists( static::FABRIC_MARKER );
	}

	/**
	 * @param bool $returnHost If true and the host is private, the host name is returned instead of TRUE. FALSE is still returned if false
	 *
	 * @return bool
	 */
	public static function hostedPrivatePlatform( $returnHost = false )
	{
		/**
		 * Add host names to this list to white-list...
		 */
		static $_allowedHosts
		= array(
			'launchpad-dev.dreamfactory.com',
			'launchpad-demo.dreamfactory.com',
		);

		$_host = static::getHostName();

		return in_array( $_host, $_allowedHosts ) ? ( $returnHost ? $_host : true ) : false;
	}

	/**
	 * @return array|mixed
	 * @throws RuntimeException
	 * @throws CHttpException
	 */
	public static function initialize()
	{
		global $_dbName, $_instance, $_dspName;

		//	If this isn't a cloud request, bail
		$_host = static::getHostName();

		if ( false !== static::hostedPrivatePlatform() && false === strpos( $_host, static::DSP_DEFAULT_SUBDOMAIN ) )
		{
			Log::error( 'Attempt to access system from non-provisioned host: ' . $_host );
			throw new \CHttpException( HttpResponse::Forbidden, 'You are not authorized to access this system you cheeky devil you. (' . $_host . ').' );
		}

		//	What has it gots in its pocketses? Cookies first, then session
		$_privateKey
			= FilterInput::cookie( static::PrivateFigNewton, FilterInput::session( static::PrivateFigNewton ), \Kisma::get( 'platform.user_key' ) );
		$_dspName = str_ireplace( static::DSP_DEFAULT_SUBDOMAIN, null, $_host );

		$_dbConfigFileName = str_ireplace(
			'%%INSTANCE_NAME%%',
			$_dspName,
			static::DSP_DB_CONFIG_FILE_NAME_PATTERN
		);

		//	Try and get them from server...
		if ( false === ( list( $_settings, $_instance ) = static::_checkCache( $_host ) ) )
		{
			//	Otherwise we need to build it.
			$_parts = explode( '.', $_host );
			$_dbName = str_replace( '-', '_', $_dspName = $_parts[0] );

			//	Otherwise, get the credentials from the auth server...
//			Log::info( 'Credentials pull' );
			$_response = Curl::get( static::DEFAULT_AUTH_ENDPOINT . '/' . $_dspName . '/database' );

			if ( HttpResponse::NotFound == Curl::getLastHttpCode() )
			{
				Log::error( 'DB Credential pull failure. Redirecting to df.com' );
				header( 'Location: https://www.dreamfactory.com/dsp-not-found?dn=' . urlencode( $_dspName ) );
				exit();
			}

			if ( is_object( $_response ) && isset( $_response->details, $_response->details->code ) && HttpResponse::NotFound == $_response->details->code )
			{
//				Log::error( 'Instance "' . $_dspName . '" not found during web initialize.' );
				throw new \CHttpException( HttpResponse::NotFound, 'Instance not available.' );
			}

			if ( !$_response || !is_object( $_response ) || false == $_response->success )
			{
				error_log( 'Error connecting to Cerberus Authentication System: ' . print_r( $_response, true ) );
				throw new \CHttpException( HttpResponse::InternalServerError, 'Cannot connect to authentication service' );
			}

			$_instance = $_cache = $_response->details;
			$_dbName = $_instance->db_name;
			$_dspName = $_instance->instance->instance_name_text;
			\Kisma::set( 'dsp.credentials', $_cache );
			\Kisma::set( 'platform.private_path', $_privatePath = $_cache->private_path );
			$_privateKey = basename( dirname( $_privatePath ) );
{'fraction_non_alphanumeric': 0.18530870531896698, 'fraction_numerical': 0.002565418163160595, 'mean_word_length': 3.9492170969107065, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 1, 'lorem ipsum': 0, 'www.': 7, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3501118', 'n_tokens_mistral': 3894, 'n_tokens_neox': 3491, 'n_words': 950}