<commit_before><?php

class Cii {
	
    public static function getVersion()
    {
        $data = json_decode(file_get_contents(Yii::getPathOfAlias('ext.cii').'/ciims.json'),true);
        return $data['version'];
    }
	/**
	 * Checks for the existance of an item at a given array index and returns that object if it exists
	 * @param array $array 	 The array to check
	 * @param mixed $item	 The indicie to check against
	 * @param mixed $default The default return value
	 * @return mixed array index or default
	 */
	public static function get($array=NULL, $item=NULL, $default=NULL)
	{
		if ($array === NULL)
			return isset($default) ? $default : $item;
		
		if (is_object($array) && isset($array->$item))
            return $array->$item; 
		
        if (is_array($array))
            return isset($array[$item]) && !empty($array) ? $array[$item] : $default;
        
		if (!is_array($array))
			return isset($array) ? $array : $item;
		
		if (isset($array[$item]) && !empty($array))
			return $array[$item];
        
		return $default;	
	}
	
    /**
     * Gets a configuration value from CiiMS::Configuration
     * @param  string $key     The key we want to retrieve from Configuration
     * @param  mixed  $default The default value to return if key is not found
     * @return mixed           The value from Config, or default
     */
    public static function getConfig($key, $default=NULL, $prefix='settings_')
    {
        $cache = Yii::app()->cache->get($prefix.$key);

        if ($cache === false)
        {
            $data = Yii::app()->db->createCommand('SELECT value FROM configuration AS t WHERE t.key = :key')
                                   ->bindParam(':key', $key)
                                   ->queryAll();

            if (!isset($data[0]['value']))
                $cache = $default;
            else
                $cache = $data[0]['value'];

            Yii::app()->cache->set($prefix.$key, $cache);
        }

        return $cache;
    }

    /**
     * Gets a configuration value from user_metadata
     * @param  string $key     The key we want to retrieve from Configuration
     * @param  mixed  $default The default value to return if key is not found
     * @return mixed           The value from Config, or default
     */
    public static function getUserConfig($key, $default=NULL)
    {
        $uid = Yii::app()->user->id;
        $data = Yii::app()->db->createCommand('SELECT value FROM user_metadata AS t WHERE t.key = :key AND t.user_id = :id')
                              ->bindParam(':id', $uid)
                              ->bindParam(':key', $key)
                              ->queryAll();

        if (!isset($data[0]['value']))
            return NULL;
        
        return $data[0]['value'];
    }

    /**
     * Consolodates the finding of retrievinv the bcrypt_Cost
     * @param  integer $default The default bcrypt cost
     * @return int              The bcrypt cost
     */
    public static function getBcryptCost($default = 13)
    {
        return Cii::getConfig('bcrypt_cost', $default);
    }

	/**
	 * Provides methods to format a date throughout a model
     * By forcing all components through this method, we can provide a comprehensive
     * and consistent date format for the entire site
     * @param  mxied  $date   Likely a string in date format (of some kind)
     * @param  string $format The format we want to FORCE the dts to be formatted to
     *                        If this isn't supplied, we'll pull it from Cii::getConfig()
     * @return Date
     */
	public static function formatDate($date, $format = NULL)
	{
        if ($format == NULL)
            $format = Cii::getConfig('dateFormat') . ' @ ' . Cii::getConfig('timeFormat');

        if ($format == ' @ ')
            $format = 'F jS, Y @ H:i';

		return date($format, strtotime($date));
	}
	
    /**
     * Retrieves Analytics.js Providers
     * @return array $providors
     */
    public static function getAnalyticsProviders()
    {
        $providers = Yii::app()->cache->get('analyticsjs_providers');

        if ($providers === false)
        {
            // Init the array
            $providers = array();

            // Retrieve all the providors that are enabled
            $response = Yii::app()->db->createCommand('SELECT REPLACE(`key`, "analyticsjs_", "") AS `key`, value FROM `configuration` WHERE `key` LIKE "analyticsjs_%_enabled" AND value = 1')->queryAll();
            foreach ($response as $element)
            {
                $k = $element['key'];
                $provider = explode('_', str_replace("__", " " ,str_replace("___", ".", $k)));
                $provider = reset($provider);

                $sqlProvider = str_replace(" ", "__" ,str_replace(".", "___", $provider));
                $data = Yii::app()->db->createCommand('SELECT REPLACE(`key`, "analyticsjs_", "") AS `key`, value FROM `configuration` WHERE `key` LIKE "analyticsjs_' . $sqlProvider .'%" AND `key` != "analyticsjs_' . $sqlProvider .'_enabled"')->queryAll();
                
                $provider = str_replace('pwk', 'Piwik', $provider);
                foreach ($data as $el)
                {
                    $k = $el['key'];
                    $k = str_replace("pwk_", "Piwik_", $k);

                    $v = $el['value'];
                    $p = explode('_', str_replace("__", " " ,str_replace("___", ".", $k)));
                    if ($v !== "")
                        $providers[$provider][$p[1]] = $v;
                }
            }
        
            Yii::app()->cache->set('analyticsjs_providers', $providers);
        }
        
        return $providers;
    }

    /**
     * Returns an array of HybridAuth providers to be used by HybridAuth and other parts of CiiMS
     * @return array of HA Providers prettily formatted
     */
    public static function getHybridAuthProviders()
    {
        $providers = Yii::app()->cache->get('hybridauth_providers');

        if ($providers === false)
        {
            // Init the array
            $providers = array();
<commit_msg>Temporary fix for Google Analytics. This won't work in the long run because we're just convering ints to booleans. We should do better type validation via Reflection<commit_after><?php

class Cii {
	
    public static function getVersion()
    {
        $data = json_decode(file_get_contents(Yii::getPathOfAlias('ext.cii').'/ciims.json'),true);
        return $data['version'];
    }
	/**
	 * Checks for the existance of an item at a given array index and returns that object if it exists
	 * @param array $array 	 The array to check
	 * @param mixed $item	 The indicie to check against
	 * @param mixed $default The default return value
	 * @return mixed array index or default
	 */
	public static function get($array=NULL, $item=NULL, $default=NULL)
	{
		if ($array === NULL)
			return isset($default) ? $default : $item;
		
		if (is_object($array) && isset($array->$item))
            return $array->$item; 
		
        if (is_array($array))
            return isset($array[$item]) && !empty($array) ? $array[$item] : $default;
        
		if (!is_array($array))
			return isset($array) ? $array : $item;
		
		if (isset($array[$item]) && !empty($array))
			return $array[$item];
        
		return $default;	
	}
	
    /**
     * Gets a configuration value from CiiMS::Configuration
     * @param  string $key     The key we want to retrieve from Configuration
     * @param  mixed  $default The default value to return if key is not found
     * @return mixed           The value from Config, or default
     */
    public static function getConfig($key, $default=NULL, $prefix='settings_')
    {
        $cache = Yii::app()->cache->get($prefix.$key);

        if ($cache === false)
        {
            $data = Yii::app()->db->createCommand('SELECT value FROM configuration AS t WHERE t.key = :key')
                                   ->bindParam(':key', $key)
                                   ->queryAll();

            if (!isset($data[0]['value']))
                $cache = $default;
            else
                $cache = $data[0]['value'];

            Yii::app()->cache->set($prefix.$key, $cache);
        }

        return $cache;
    }

    /**
     * Gets a configuration value from user_metadata
     * @param  string $key     The key we want to retrieve from Configuration
     * @param  mixed  $default The default value to return if key is not found
     * @return mixed           The value from Config, or default
     */
    public static function getUserConfig($key, $default=NULL)
    {
        $uid = Yii::app()->user->id;
        $data = Yii::app()->db->createCommand('SELECT value FROM user_metadata AS t WHERE t.key = :key AND t.user_id = :id')
                              ->bindParam(':id', $uid)
                              ->bindParam(':key', $key)
                              ->queryAll();

        if (!isset($data[0]['value']))
            return NULL;
        
        return $data[0]['value'];
    }

    /**
     * Consolodates the finding of retrievinv the bcrypt_Cost
     * @param  integer $default The default bcrypt cost
     * @return int              The bcrypt cost
     */
    public static function getBcryptCost($default = 13)
    {
        return Cii::getConfig('bcrypt_cost', $default);
    }

	/**
	 * Provides methods to format a date throughout a model
     * By forcing all components through this method, we can provide a comprehensive
     * and consistent date format for the entire site
     * @param  mxied  $date   Likely a string in date format (of some kind)
     * @param  string $format The format we want to FORCE the dts to be formatted to
     *                        If this isn't supplied, we'll pull it from Cii::getConfig()
     * @return Date
     */
	public static function formatDate($date, $format = NULL)
	{
        if ($format == NULL)
            $format = Cii::getConfig('dateFormat') . ' @ ' . Cii::getConfig('timeFormat');

        if ($format == ' @ ')
            $format = 'F jS, Y @ H:i';

		return date($format, strtotime($date));
	}
	
    /**
     * Retrieves Analytics.js Providers
     * @return array $providors
     */
    public static function getAnalyticsProviders()
    {
        $providers = Yii::app()->cache->get('analyticsjs_providers');

        if ($providers === false)
        {
            // Init the array
            $providers = array();

            // Retrieve all the providors that are enabled
            $response = Yii::app()->db->createCommand('SELECT REPLACE(`key`, "analyticsjs_", "") AS `key`, value FROM `configuration` WHERE `key` LIKE "analyticsjs_%_enabled" AND value = 1')->queryAll();
            foreach ($response as $element)
            {
                $k = $element['key'];
                $provider = explode('_', str_replace("__", " " ,str_replace("___", ".", $k)));
                $provider = reset($provider);

                $sqlProvider = str_replace(" ", "__" ,str_replace(".", "___", $provider));
                $data = Yii::app()->db->createCommand('SELECT REPLACE(`key`, "analyticsjs_", "") AS `key`, value FROM `configuration` WHERE `key` LIKE "analyticsjs_' . $sqlProvider .'%" AND `key` != "analyticsjs_' . $sqlProvider .'_enabled"')->queryAll();

                $provider = str_replace('pwk', 'Piwik', $provider);
                foreach ($data as $el)
                {
                    $k = $el['key'];
                    $k = str_replace("pwk_", "Piwik_", $k);

                    $v = $el['value'];
                    $p = explode('_', str_replace("__", " " ,str_replace("___", ".", $k)));
                    if ($v !== "")
                    {
                        if ($v === "0")
                            $providers[$provider][$p[1]] = 'false';
                        else if ($v === "1")          
                            $providers[$provider][$p[1]] = 'true';
                        else
                            $providers[$provider][$p[1]] = $v;

                    }
                }
            }

            Yii::app()->cache->set('analyticsjs_providers', $providers);
        }

        return $providers;
    }

    /**
     * Returns an array of HybridAuth providers to be used by HybridAuth and other parts of CiiMS
     * @return array of HA Providers prettily formatted
     */
    public static function getHybridAuthProviders()
    {
        $providers = Yii::app()->cache->get('hybridauth_providers');

        if ($providers === false)
        {
            // Init the array
            $providers = array();

{'fraction_non_alphanumeric': 0.13884481117105682, 'fraction_numerical': 0.0015867978419549348, 'mean_word_length': 1.9728773584905661, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3214011', 'n_tokens_mistral': 3620, 'n_tokens_neox': 3444, 'n_words': 1036}