<?php

class HybridAuthIdentity extends CUserIdentity
{
    const VERSION = '2.6.0';
 
    /**
     * 
     * @var Hybrid_Auth
     */
    public $hybridAuth;
 
    /**
     * 
     * @var Hybrid_Provider_Adapter
     */
    public $adapter;
 
    /**
     * 
     * @var Hybrid_User_Profile
     */
    public $userProfile;
 
    public $allowedProviders = array('google', 'facebook', 'linkedin', 'yahoo', 'live', 'XING');
 
    protected $config;
 
    function __construct() 
    {
        $path = Yii::getPathOfAlias('ext.HybridAuth');
        require_once $path . '/hybridauth-' . self::VERSION . '/hybridauth/Hybrid/Auth.php';  //path to the Auth php file within HybridAuth folder
        
        //$socialLoginUrl = "http://localhost/xing.php";
        $socialLoginUrl = "http://www.phonalisa.de/xing.php";
        //$socialLoginUrl = "http://anton.aerea.lan/xing.php";
        
        if(isset($_GET['provider']) && $_GET['provider'] == 'XING'){
            $socialLoginUrl .= "?state=" . urlencode(base64_encode(Yii::app()->createAbsoluteUrl('phonesettingsThirdPartyCredentials/default/socialLogin')));   
        } 
        
        $this->config = array(
            "base_url" => $socialLoginUrl,
            "providers" => array(
                "Google" => array(
                    "enabled" => true,
                    "keys" => array(
                        "id" => "113039096585-tvturij8bs7o7b0aj8t935bmdurd2302.apps.googleusercontent.com", 
                        "secret" => "MgOxrsnXNeIYLw0LBRAgC9JL",
                    ),
                    "scope" => "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.google.com/m8/feeds/",
                    "access_type" => "offline",
                ),  
                "Facebook" => array (
                   "enabled" => true,
                   "keys" => array ( 
                       "id" => "facebook client id", 
                       "secret" => "facebook secret",
                   ),
                   "scope" => "email"
                ),
                "Live" => array (
                   "enabled" => true,
                   "keys" => array ( 
                       "id" => "windows client id", 
                       "secret" => "Windows Live secret",
                   ),
                   "scope" => "email"
                ),
                "Yahoo" => array(
                   "enabled" => true,
                   "keys" => array ( 
                       "key" => "yahoo client id", 
                       "secret" => "yahoo secret",
                   ),
                ),
                "LinkedIn" => array(
                   "enabled" => true,
                   "keys" => array ( 
                       "key" => "linkedin client id", 
                       "secret" => "linkedin secret",
                   ),
                ),
                "XING" => array(
                   "enabled" => true,
                    "wrapper" => array(
                        "path"=> __DIR__."/../modules/phonesettingsThirdPartyCredentials/components/plXING.php",
                        "class" => "plXING"
                    ),
                   /*
"keys" => array ( 
                       "key" => "fd11a8fd6dc863b78e2b", 
                       "secret" => "365ba998d189a4f8524664f15a56f95cd26e9a38",
                   ),
*/
                   /* PRODUCTION */
                   "keys" => array ( 
                       "key" => "4f033083f18799f1c14a", 
                       "secret" => "b89cdd1a0f07200d5dde1f162b7af405290895fe",
                   ),
                ),
            ),
 
            "debug_mode" => false, 
 
            // to enable logging, set 'debug_mode' to true, then provide here a path of a writable file 
            "debug_file" => "logfile.log"
        		         
        );
        
        $proxy = cfg('HTTPS_PROXY_IP_ADDRESS');
        $port = cfg('HTTPS_PROXY_PORT');
        if ($proxy != "" && $port != "") {
            Log::d("set proxy : $proxy","phonalisa-hybridAuth");
            Log::d("set port : $port","phonalisa-hybridAuth");
        	$this->config["proxy"] = $proxy.":".$port;
        } else {
            Log::d("no proxy defined","phonalisa-hybridAuth");
        }
        
        $this->hybridAuth = new Hybrid_Auth($this->config);
    }
 
    /**
     *
     * @param string $provider
     * @return bool 
     */
    public function validateProviderName($provider)
    {
        if (!is_string($provider))
            return false;
        if (!in_array($provider, $this->allowedProviders))
            return false;
 
        return true;
    }
 
}