<?php

class HybridAuthAdapter extends CUserIdentity
{

    const VERSION = '2.6.0';

    public $hybridAuth;
    public $adapter;
    public $userProfile;
    public $allowedProviders = array('XING', 'Google');
    protected $config;

    function __construct($provider)
    {
        $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 = "https://www.phonalisa.de/xing.php";
        $socialLoginUrl .= "?redirectLocation=" . urlencode(base64_encode(Yii::app()->createAbsoluteUrl('phonesettingsThirdPartyCredentials/default/socialLogin')));

        $this->config = array(
            "base_url" => $socialLoginUrl,
            "providers" => array(
                "XING" => array(
                    "enabled" => true,
                    "wrapper" => array(
                        "path" => __DIR__ . "/XingImport/plXING.php",
                        "class" => "plXING"
                    ),
                    /* PRODUCTION */
                    "keys" => array(
                        "key" => "4f033083f18799f1c14a",
                        "secret" => "b89cdd1a0f07200d5dde1f162b7af405290895fe",
                    ),
                ),
                "Google" => array(
                    "enabled" => true,
                    "wrapper" => array(
                        "path" => __DIR__ . "/GoogleImport/plGoogle.php",
                        "class" => "plGoogle"
                    ),
                    /* TEST */
                    "keys" => array(
                        "id" => "165102895225-fqrf1ueo7ifco647dj403jmsd33pftoe.apps.googleusercontent.com",
                        "secret" => "VfocUzTFAl2cVo4B0eDpYd3M",
                    ),
                    "approval_prompt" => "force", // optional
                )
            ),
            "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;
    }
}
