コード例 #1
0
 /**
  * check user use package or use freepackage
  * @param  string $package_id The pacakge sku to identify package
  * @param  object $ad Current purchase post
  * @return array 
  *         'url' => string process-payment-url base on type free/usePackage,
  *         'success' => bool
  * @author Dakachi
  */
 public static function package_or_free($package_id, $ad)
 {
     $instance = self::get_instance();
     $response = array('success' => false);
     $use_package = AE_Package::check_use_package($package_id);
     $package = $instance->get($package_id);
     if ($use_package) {
         et_write_session('ad_id', $ad->ID);
         $response['success'] = true;
         $response['url'] = et_get_page_link('process-payment', array('paymentType' => 'usePackage'));
         return $response;
     }
     if ($package->et_price == 0) {
         et_write_session('ad_id', $ad->ID);
         $response['success'] = true;
         $response['url'] = et_get_page_link('process-payment', array('paymentType' => 'free'));
         return $response;
     }
     return $response;
 }
コード例 #2
0
ファイル: paypal.php プロジェクト: linniepinski/perssistant
/**
 * ajax callback process bid escrow and send redirect url to client
 *
 * @author Dakachi
 */
function fre_escrow_bid()
{
    global $user_ID;
    $error = array('success' => false, 'msg' => __('Invalid bid', ET_DOMAIN));
    if (!isset($_REQUEST['bid_id'])) {
        wp_send_json($error);
    }
    $bid_id = $_REQUEST['bid_id'];
    $bid = get_post($bid_id);
    // check bid is valid
    if (!$bid || is_wp_error($bid) || $bid->post_type != BID) {
        wp_send_json($error);
    }
    // currency settings
    $currency = ae_get_option('content_currency');
    $currency = $currency['code'];
    $bid_budget = get_post_meta($bid_id, 'bid_budget', true);
    // get commission settings
    $commission = ae_get_option('commission');
    $commission_fee = $commission;
    // caculate commission fee by percent
    $commission_type = ae_get_option('commission_type');
    if ($commission_type == 'percent') {
        $commission_fee = $bid_budget * $commission / 100;
    }
    $payer_of_commission = ae_get_option('payer_of_commission', 'project_owner');
    if ($payer_of_commission == 'project_owner') {
        $total = (double) $bid_budget + (double) $commission_fee;
    } else {
        $total = $bid_budget;
        $bid_budget = (double) $total - (double) $commission_fee;
    }
    $receiver = get_user_meta($bid->post_author, 'paypal', true);
    // paypal adaptive process payment and send reponse to client
    $ppadaptive = AE_PPAdaptive::get_instance();
    // get paypal adaptive settings
    $ppadaptive_settings = ae_get_option('escrow_paypal');
    // the admin's paypal business account
    $primary = $ppadaptive_settings['business_mail'];
    // get from setting
    $feesPayer = $ppadaptive_settings['paypal_fee'];
    /**
     * paypal adaptive order data
     */
    $order_data = array('actionType' => 'PAY_PRIMARY', 'returnUrl' => et_get_page_link('process-accept-bid', array('paymentType' => 'paypaladaptive')), 'cancelUrl' => et_get_page_link('process-accept-bid', array('paymentType' => 'paypaladaptive')), 'currencyCode' => $currency, 'feesPayer' => $feesPayer, 'receiverList.receiver(0).amount' => $total, 'receiverList.receiver(0).email' => $primary, 'receiverList.receiver(0).primary' => true, 'receiverList.receiver(1).amount' => $bid_budget, 'receiverList.receiver(1).email' => $receiver, 'receiverList.receiver(1).primary' => false, 'requestEnvelope.errorLanguage' => 'en_US');
    //dinhle1987-pers@yahoo.com
    // dinhle1987-pers2@yahoo.com
    $response = $ppadaptive->Pay($order_data);
    if (is_array($response) && isset($response['success']) && !$response['success']) {
        wp_send_json(array('success' => false, 'msg' => $response['msg']));
    }
    // create order
    $order_post = array('post_type' => 'fre_order', 'post_status' => 'pending', 'post_parent' => $bid_id, 'post_author' => $user_ID, 'post_title' => 'Pay for accept bid', 'post_content' => 'Pay for accept bid ' . $bid_id);
    if (strtoupper($response->responseEnvelope->ack) == 'SUCCESS') {
        $order_id = wp_insert_post($order_post);
        update_post_meta($order_id, 'fre_paykey', $response->payKey);
        update_post_meta($order_id, 'gateway', 'PPadaptive');
        update_post_meta($bid_id, 'fre_bid_order', $order_id);
        update_post_meta($bid_id, 'fre_paykey', $response->payKey);
        et_write_session('payKey', $response->payKey);
        et_write_session('order_id', $order_id);
        et_write_session('bid_id', $bid_id);
        et_write_session('ad_id', $bid->post_parent);
        $response->redirect_url = $ppadaptive->paypal_url . $response->payKey;
        wp_send_json($response);
    } else {
        wp_send_json(array('success' => false, 'msg' => $response->error[0]->message));
    }
}
コード例 #3
0
 function setup_checkout(ET_Order $order)
 {
     //$order				=	clone	$this->_order;
     $order = $order->generate_data_to_pay();
     $settings = $this->_settings;
     $payment = new ET_Paypal($settings, 1);
     $url = $settings['return'];
     $cancel_url = $settings['cancel'];
     $currencyCodeType = isset($order['currencyCodeType']) ? $order['currencyCodeType'] : '';
     $pro = isset($order['products']) ? $order['products'] : array();
     $products = "";
     $itemamt = 0.0;
     $i = 0;
     // general product string add to paypal url
     if (!empty($pro)) {
         $length = count($pro);
         if ($length > 1) {
             foreach ($pro as $key => $value) {
                 $products .= "&item_name_{$i}=" . $value['NAME'];
                 $products .= "&amount_{$i}=" . $value['AMT'];
                 $products .= "&item_number_{$i}=" . $value['QTY'];
                 $itemamt += doubleval($value['AMT'] * $value['QTY']);
                 $i++;
             }
         } else {
             foreach ($pro as $key => $value) {
                 $products .= "&item_name=" . $value['NAME'];
                 $products .= "&amount=" . $value['AMT'];
                 $products .= "&item_number=" . $value['QTY'];
                 $itemamt += doubleval($value['AMT'] * $value['QTY']);
                 $i++;
             }
         }
         $products .= "&invoice=" . $order['ID'];
         $products .= "&custom=" . $order['ID'];
     }
     $total = '&upload=1&amount=' . $order['total'];
     $returnURL = '&return=' . urlencode($url);
     $notifyURL = '&notify_url=' . urlencode(add_query_arg('paypalListener', 'paypal_appengine_IPN', trailingslashit(home_url())));
     $cancelURL = '&cancel_return=' . urlencode("{$cancel_url}");
     $currency = '&currency_code=' . $currencyCodeType;
     $nvpstr = $notifyURL . $returnURL . $cancelURL . $products . $total . $currency;
     #set session
     et_write_session('ad_id', $order['ID']);
     return array('url' => $payment->set_checkout($nvpstr, 'SIMPLEPAYPAL'), 'ACK' => true, 'extend' => false);
 }
コード例 #4
0
ファイル: linkedin.php プロジェクト: maratdev/alllancer
 /**
  * function handle after linkedin callback
  */
 public function linked_auth()
 {
     if (isset($_REQUEST['code']) && !empty($_REQUEST['code']) && (isset($_REQUEST['state']) || $_REQUEST['state'] == $this->state)) {
         try {
             /**
              * Step2: Exchange Authorization Code for a Request Token
              */
             $request = $_REQUEST;
             $redirect_uri = home_url('?action=linked_auth_callback');
             $args = array('method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => array('grant_type' => 'authorization_code', 'code' => $request['code'], 'redirect_uri' => $redirect_uri, 'client_id' => $this->linkedin_api_key, 'client_secret' => $this->linkedin_secret_key), 'cookies' => array());
             $remote_post = wp_remote_post($this->linkedin_token_url, $args);
             if (isset($remote_post['body']) && !empty($remote_post['body'])) {
                 $data = json_decode($remote_post['body']);
             } else {
                 _e('Error to connect to Linkedin server!', ET_DOMAIN);
                 exit;
             }
             if (!isset($data->access_token) || empty($data->access_token)) {
                 _e('Can not get the access token from Linkedin server!', ET_DOMAIN);
                 exit;
             }
             /**
              * Step3: Make authenticated requests and get user's informations
              */
             $args1 = array('timeout' => 120, 'httpversion' => '1.1', 'headers' => array('Authorization' => 'Bearer ' . $data->access_token));
             $remote_get = wp_remote_get($this->linkedin_people_url, $args1);
             if (isset($remote_get['body']) && !empty($remote_get['body'])) {
                 $data_user = json_decode($remote_get['body']);
             } else {
                 _e('Error to connect to Linkedin server2!', ET_DOMAIN);
                 exit;
             }
             if (!isset($data_user->id) || empty($data_user->id)) {
                 _e('Can not get user information from Linkedin server!', ET_DOMAIN);
                 exit;
             }
             // if user is already authenticated before
             if ($this->get_user($data_user->id)) {
                 $user = $this->get_user($data_user->id);
                 $result = $this->logged_user_in($data_user->id);
                 $ae_user = AE_Users::get_instance();
                 $userdata = $ae_user->convert($user);
                 $nonce = array('reply_thread' => wp_create_nonce('insert_reply'), 'upload_img' => wp_create_nonce('et_upload_images'));
             } else {
                 // avatar
                 $ava_response = isset($data_user->pictureUrl) ? $data_user->pictureUrl : '';
                 $sizes = get_intermediate_image_sizes();
                 $avatars = array();
                 if ($ava_response) {
                     foreach ($sizes as $size) {
                         $avatars[$size] = array($ava_response);
                     }
                 } else {
                     $avatars = false;
                 }
                 $data_user->formattedName = str_replace(' ', '', sanitize_user($data_user->formattedName));
                 $username = $data_user->formattedName;
                 $params = array('user_login' => $username, 'user_email' => isset($data_user->emailAddress) ? $data_user->emailAddress : false, 'et_avatar' => $avatars);
                 //remove avatar if cant fetch avatar
                 foreach ($params as $key => $param) {
                     if ($param == false) {
                         unset($params[$key]);
                     }
                 }
                 // turn on session
                 if (!isset($_SESSION)) {
                     ob_start();
                     @session_start();
                 }
                 /**
                  * set value into session for save later
                  *
                  */
                 $_SESSION['et_auth'] = serialize($params);
                 $_SESSION['et_social_id'] = $data_user->id;
                 $_SESSION['et_auth_type'] = 'linkedin';
                 et_write_session('et_auth', serialize($params));
                 et_write_session('et_social_id', $data_user->id);
                 et_write_session('et_auth_type', 'linkedin');
             }
             header('Location: ' . $this->auth_url);
             exit;
         } catch (Exception $e) {
             _e('Error to connect to Linkedin server', ET_DOMAIN);
             exit;
         }
     }
 }
コード例 #5
0
ファイル: google.php プロジェクト: maratdev/alllancer
 public function auth_google()
 {
     if (isset($_REQUEST['code']) && !empty($_REQUEST['code']) && (isset($_REQUEST['state']) || $_REQUEST['state'] == $this->state)) {
         try {
             // turn on session
             if (!isset($_SESSION)) {
                 ob_start();
                 @session_start();
             }
             /**
              * Exchange authorization code for tokens
              */
             $redirect_uri = home_url('?action=gplus_auth_callback');
             $args = array('method' => 'POST', 'body' => array('grant_type' => 'authorization_code', 'code' => $_REQUEST['code'], 'redirect_uri' => $redirect_uri, 'client_id' => $this->gplus_client_id, 'client_secret' => $this->gplus_secret_key));
             $remote_post = wp_remote_post($this->gplus_exchange_url, $args);
             if (isset($remote_post['body'])) {
                 $data = json_decode($remote_post['body']);
                 if (isset($data->refresh_token)) {
                     $secure = 'https' === parse_url(site_url(), PHP_URL_SCHEME) && 'https' === parse_url(home_url(), PHP_URL_SCHEME);
                     setcookie('refresh_token', $data->refresh_token, time() + 3600 * 24 * 7, SITECOOKIEPATH, COOKIE_DOMAIN, $secure);
                 }
                 if (isset($data->error) && $data->error == 'invalid_grant') {
                     $args = array('method' => 'POST', 'body' => array('grant_type' => 'refresh_token', 'code' => $_REQUEST['code'], 'redirect_uri' => $redirect_uri, 'client_id' => $this->gplus_client_id, 'client_secret' => $this->gplus_secret_key, 'refresh_token' => $_COOKIE['refresh_token']));
                     $remote_post = wp_remote_post($this->gplus_exchange_url, $args);
                     $data = json_decode($remote_post['body']);
                 }
             } else {
                 _e('Error to connect to Google Server!', ET_DOMAIN);
                 exit;
             }
             /**
              * Get user information
              */
             if (isset($data->access_token)) {
                 $userinfor = wp_remote_get($this->gplus_token_info_url . '?access_token=' . $data->access_token);
                 $userinfor = json_decode($userinfor['body']);
             } else {
                 _e('Error to connect to Google', ET_DOMAIN);
                 exit;
             }
             if (!isset($userinfor->id) || empty($userinfor->id)) {
                 _e('Error to connect to Google Server!', ET_DOMAIN);
                 exit;
             }
             // if user is already authenticated before
             if ($this->get_user($userinfor->id)) {
                 $user = $this->get_user($userinfor->id);
                 $result = $this->logged_user_in($userinfor->id);
                 $ae_user = AE_Users::get_instance();
                 $userdata = $ae_user->convert($user);
                 $nonce = array('reply_thread' => wp_create_nonce('insert_reply'), 'upload_img' => wp_create_nonce('et_upload_images'));
             } else {
                 // avatar
                 $ava_response = isset($userinfor->picture) ? $userinfor->picture : '';
                 $sizes = get_intermediate_image_sizes();
                 $avatars = array();
                 if ($ava_response) {
                     foreach ($sizes as $size) {
                         $avatars[$size] = array($ava_response);
                     }
                 } else {
                     $avatars = false;
                 }
                 $userinfor->name = str_replace(' ', '', sanitize_user($userinfor->name));
                 $username = $userinfor->name;
                 $params = array('user_login' => $username, 'user_email' => isset($userinfor->email) ? $userinfor->email : false, 'et_avatar' => $avatars);
                 //remove avatar if cant fetch avatar
                 foreach ($params as $key => $param) {
                     if ($param == false) {
                         unset($params[$key]);
                     }
                 }
                 $_SESSION['et_auth'] = serialize($params);
                 $_SESSION['et_social_id'] = $userinfor->id;
                 $_SESSION['et_auth_type'] = 'google';
                 et_write_session('et_auth', serialize($params));
                 et_write_session('et_social_id', $userinfor->id);
                 et_write_session('et_auth_type', 'google');
             }
             header('Location: ' . $this->auth_url);
             exit;
         } catch (Exception $e) {
             _e('Error to connect to Google Server', ET_DOMAIN);
             exit;
         }
     }
 }
コード例 #6
0
ファイル: facebook.php プロジェクト: rinodung/wp-question
 public function auth_facebook()
 {
     try {
         // turn on session
         if (!isset($_SESSION)) {
             ob_start();
             @session_start();
         }
         $fb_appID = ae_get_option('et_facebook_key', false);
         $fb_secret_key = ae_get_option('et_facebook_secret_key', false);
         if (!$this->fb_app_id || !$this->fb_secret_key) {
             $resp = array('success' => false, 'msg' => __('Social login is invalid. Please contact administrator for help.', ET_DOMAIN));
             wp_send_json($resp);
             return;
         }
         if (!isset($_POST['fb_token']) || $_POST['fb_token'] == '') {
             $resp = array('success' => false, 'msg' => __('Social login is invalid. Please contact administrator for help.', ET_DOMAIN));
             wp_send_json($resp);
             return;
         }
         /**
          * check user id with a access token
          */
         $token_url = $this->fb_token_url;
         $token_url .= '?fields=id&access_token=' . $_POST['fb_token'];
         $check_userid = wp_remote_get($token_url);
         $check_userid = json_decode($check_userid['body']);
         if (!isset($check_userid->id) || $check_userid->id == '') {
             $resp = array('success' => false, 'msg' => __('Social login is invalid. Please contact administrator for help.', ET_DOMAIN));
             wp_send_json($resp);
             return;
         }
         $check_userid = $check_userid->id;
         /**
          * check user vefified app
          *
          */
         $fb_exchange_token = $this->fb_exchange_token;
         $fb_exchange_token .= '?grant_type=fb_exchange_token&';
         $fb_exchange_token .= 'client_id=' . $this->fb_app_id . '&';
         $fb_exchange_token .= 'client_secret=' . $this->fb_secret_key . '&';
         $fb_exchange_token .= 'fb_exchange_token=' . $_POST['fb_token'];
         // $fb_app_token = wp_remote_get('https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id='.$this->fb_app_id.'&client_secret='.$this->fb_secret_key.'&fb_exchange_token=' . $_POST['fb_token']);
         $fb_app_token = wp_remote_get($fb_exchange_token);
         if (!isset($_POST['content']) || empty($_POST['content'])) {
             $resp = array('success' => false, 'msg' => __('Social login is invalid. Please contact administrator for help.', ET_DOMAIN));
             wp_send_json($resp);
             return;
         }
         $data = $_POST['content'];
         if (!isset($data['id']) || $data['id'] == '') {
             $resp = array('success' => false, 'msg' => __('Social login is invalid. Please contact administrator for help.', ET_DOMAIN));
             wp_send_json($resp);
             return;
         }
         if (isset($fb_app_token['body']) && $fb_app_token['body'] != '') {
             $fb_app_token = explode('&', $fb_app_token['body']);
             $fb_token = explode('=', $fb_app_token['0']);
             if ($check_userid != $data['id'] || !isset($fb_token[1]) || $fb_token[1] == '') {
                 $fb_token = $fb_token['1'];
                 $resp = array('success' => false, 'msg' => __('Please login by using your Facebook account again!'));
                 wp_send_json($resp);
                 return;
             }
         } else {
             $resp = array('success' => false, 'msg' => __('Please login by using your Facebook account again!'));
             wp_send_json($resp);
             return;
         }
         // find usser
         $return = array('redirect_url' => home_url());
         $user = $this->get_user($data['id']);
         // if user is already authenticated before
         if ($user) {
             $result = $this->logged_user_in($data['id']);
             $ae_user = AE_Users::get_instance();
             $userdata = $ae_user->convert($user);
             $nonce = array('reply_thread' => wp_create_nonce('insert_reply'), 'upload_img' => wp_create_nonce('et_upload_images'));
             $return = array('user' => $userdata, 'nonce' => $nonce);
         } else {
             // avatar
             $ava_response = wp_remote_get('http://graph.facebook.com/' . $data['id'] . '/picture?type=large&redirect=false');
             if (!is_wp_error($ava_response)) {
                 $ava_response = json_decode($ava_response['body']);
             } else {
                 $ava_response = false;
             }
             $sizes = get_intermediate_image_sizes();
             $avatars = array();
             if ($ava_response) {
                 foreach ($sizes as $size) {
                     $avatars[$size] = array($ava_response->data->url);
                 }
             } else {
                 $avatars = false;
             }
             $data['name'] = str_replace(' ', '', sanitize_user($data['name']));
             $username = $data['name'];
             $params = array('user_login' => $username, 'user_email' => isset($data['email']) ? $data['email'] : false, 'description' => isset($data['bio']) ? $data['bio'] : false, 'user_location' => isset($data['location']) ? $data['location']['name'] : false, 'et_avatar' => $avatars);
             //remove avatar if cant fetch avatar
             foreach ($params as $key => $param) {
                 if ($param == false) {
                     unset($params[$key]);
                 }
             }
             $_SESSION['et_auth'] = serialize($params);
             $_SESSION['et_social_id'] = $data['id'];
             $_SESSION['et_auth_type'] = 'facebook';
             // try to use et session
             et_write_session('et_auth', serialize($params));
             et_write_session('et_social_id', $data['id']);
             et_write_session('et_auth_type', 'facebook');
             $return['params'] = $params;
             $return['redirect_url'] = $this->auth_url;
         }
         $resp = array('success' => true, 'msg' => __('You have logged in successfully', ET_DOMAIN), 'redirect' => home_url(), 'data' => $return);
     } catch (Exception $e) {
         $resp = array('success' => false, 'msg' => $e->getMessage());
     }
     wp_send_json($resp);
 }
コード例 #7
0
 /**
  * catch ajax et-setup-payment and process order generate json send back to clien
  * json data: array
  *             - 'success' => $nvp['ACK']
  *             - 'data' => array('data' , 'url'  => 'the payment gateway url')
  *             - 'paymentType' => $paymentType
  *
  * @package AE Payment
  * @category payment
  *
  * @since  1.0
  * @author  Dakachi
  */
 function setup_payment()
 {
     global $user_ID;
     $order_data = $this->setup_orderdata($_POST);
     $plans = $this->get_plans();
     if (empty($plans)) {
         wp_send_json(array('success' => false, 'msg' => __("There is no payment plan.", ET_DOMAIN)));
     }
     $adID = isset($_POST['ID']) ? $_POST['ID'] : '';
     $author = isset($_POST['author']) ? $_POST['author'] : $user_ID;
     $packageID = isset($_POST['packageID']) ? $_POST['packageID'] : '';
     $paymentType = isset($_POST['paymentType']) ? $_POST['paymentType'] : '';
     foreach ($plans as $key => $value) {
         if ($value->sku == $packageID) {
             $plan = $value;
             break;
         }
     }
     $plan->ID = $plan->sku;
     // if($adID) $plan->post_id = $adID;
     // $ship    =   array( 'street_address' => isset($company_location['full_location']) ? $company_location['full_location'] : __("No location", ET_DOMAIN));
     // filter shipping
     $ship = apply_filters('ae_payment_ship', array(), $order_data, $_POST);
     /**
      * filter order data
      *
      * @param Array $order_data
      * @param Array $_POST Client submitted data
      *
      * @since  1.0
      * @author  Dakachi
      */
     $order_data = apply_filters('ae_payment_order_data', $order_data, $_POST);
     // insert order into database
     $order = new AE_Order($order_data, $ship);
     $order->add_product((array) $plan);
     $order_data = $order->generate_data_to_pay();
     // write session
     et_write_session('order_id', $order_data['ID']);
     et_write_session('ad_id', $adID);
     $arg = apply_filters('ae_payment_links', array('return' => et_get_page_link('process-payment'), 'cancel' => et_get_page_link('process-payment')));
     /**
      * process payment
      */
     $paymentType_raw = $paymentType;
     $paymentType = strtoupper($paymentType);
     /**
      * factory create payment visitor
      */
     $visitor = AE_Payment_Factory::createPaymentVisitor($paymentType, $order, $paymentType_raw);
     // setup visitor setting
     $visitor->set_settings($arg);
     // accept visitor process payment
     $nvp = $order->accept($visitor);
     if ($nvp['ACK']) {
         $response = array('success' => $nvp['ACK'], 'data' => $nvp, 'paymentType' => $paymentType);
     } else {
         $response = array('success' => false, 'paymentType' => $paymentType, 'msg' => __("Invalid payment gateway", ET_DOMAIN));
     }
     /**
      * filter $response send to client after process payment
      *
      * @param Array $response
      * @param String $paymentType  The payment gateway user select
      * @param Array $order The order data
      *
      * @package  AE Payment
      * @category payment
      *
      * @since  1.0
      * @author  Dakachi
      */
     $response = apply_filters('ae_setup_payment', $response, $paymentType, $order);
     wp_send_json($response);
 }
コード例 #8
0
ファイル: twitter.php プロジェクト: rinodung/wp-question
 /**
  * Redirect and auth twitter account
  */
 public function redirect()
 {
     if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'twitterauth') {
         // request token
         if (!isset($_SESSION)) {
             ob_start();
             @session_start();
         }
         require_once dirname(__FILE__) . '/twitteroauth/twitteroauth.php';
         // create connection
         $connection = new TwitterOAuth($this->consumer_key, $this->consumer_secret);
         // request token
         $request_token = $connection->getRequestToken($this->oath_callback);
         //
         if ($request_token) {
             // var_dump($request_token);
             // exit;
             if (isset($request_token['oauth_token']) && $request_token['oauth_token_secret']) {
                 $token = $request_token['oauth_token'];
                 $_SESSION['oauth_token'] = $token;
                 $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
                 // try et session
                 et_write_session('oauth_token', $token);
                 et_write_session('oauth_token_secret', $request_token['oauth_token_secret']);
             }
             // redirect to twitter
             switch ($connection->http_code) {
                 case 200:
                     $url = $connection->getAuthorizeURL($request_token);
                     //redirect to Twitter .
                     header('Location: ' . $url);
                     exit;
                     break;
                 default:
                     _e("Conection with twitter Failed", ET_DOMAIN);
                     exit;
                     break;
             }
         } else {
             echo __("Error Receiving Request Token", ET_DOMAIN);
             exit;
         }
     } else {
         if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'twitterauth_callback' && $_GET['oauth_token']) {
             // request access token and
             // create account here
             if (!isset($_SESSION)) {
                 ob_start();
                 @session_start();
             }
             require_once dirname(__FILE__) . '/twitteroauth/twitteroauth.php';
             $et_session = et_read_session();
             if (isset($et_session['oauth_token'])) {
                 $oauth_token = $et_session['oauth_token'];
                 $oauth_token_secret = $et_session['oauth_token_secret'];
             } else {
                 $oauth_token = $_SESSION['oauth_token'];
                 $oauth_token_secret = $_SESSION['oauth_token_secret'];
             }
             // create connection
             $connection = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $oauth_token, $oauth_token_secret);
             // request access token
             $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
             //
             if ($access_token && isset($access_token['oauth_token'])) {
                 // recreate connection
                 $connection = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $access_token['oauth_token'], $access_token['oauth_token_secret']);
                 $account = $connection->get('account/verify_credentials');
                 // create account
                 if ($account && isset($account->screen_name) && isset($account->name)) {
                     // find
                     $users = get_users(array('meta_key' => 'et_twitter_id', 'meta_value' => $account->id));
                     if (!empty($users) && is_array($users)) {
                         $ae_user = $users[0];
                         wp_set_auth_cookie($ae_user->ID, 1);
                         wp_redirect(home_url());
                         exit;
                     }
                     $avatars = array();
                     $sizes = get_intermediate_image_sizes();
                     foreach ($sizes as $size) {
                         $avatars[$size] = array($account->profile_image_url);
                     }
                     $params = array('user_login' => $account->screen_name, 'display_name' => $account->name, 'user_location' => $account->location, 'description' => $account->description, 'et_avatar' => $avatars);
                     // save user info for saving later
                     $_SESSION['user_login'] = $account->screen_name;
                     $_SESSION['display_name'] = $account->name;
                     $_SESSION['et_twitter_id'] = $account->id;
                     $_SESSION['user_location'] = $account->location;
                     $_SESSION['description'] = $account->description;
                     $_SESSION['profile_image_url'] = $account->profile_image_url;
                     $_SESSION['et_auth'] = serialize(array('user_login' => $account->screen_name, 'display_name' => $account->name, 'user_location' => $account->location, 'description' => $account->description, 'et_avatar' => $avatars));
                     $_SESSION['et_social_id'] = $account->id;
                     $_SESSION['et_auth_type'] = 'twitter';
                     // try to user et session
                     et_write_session('et_auth', serialize($params));
                     et_write_session('et_social_id', $account->id);
                     et_write_session('et_auth_type', 'twitter');
                     wp_redirect($this->auth_url);
                     exit;
                 }
             }
             exit;
         } else {
             if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'twitterauth_login') {
                 if (!isset($_SESSION)) {
                     ob_start();
                     @session_start();
                 }
                 if (!empty($_POST['user_email'])) {
                     $password = wp_generate_password();
                     $new_account = array('user_login' => $_SESSION['user_login'], 'display_name' => $_SESSION['display_name'], 'et_twitter_id' => $_SESSION['et_twitter_id'], 'user_location' => $_SESSION['user_location'], 'description' => $_SESSION['description'], 'user_email' => $_POST['user_email'], 'user_pass' => $password, 'et_avatar' => array('thumbnail' => array($_SESSION['profile_image_url'])));
                     $ae_user = get_user_by('login', $new_account['user_login']);
                     if ($ae_user != false) {
                         $new_account['user_login'] = str_replace('@', '', $_POST['user_email']);
                     }
                     $ae_user = AE_Users::get_instance();
                     $result = $ae_user->insert($new_account);
                     if (!is_wp_error($result)) {
                         // send email here
                         //
                         do_action('et_after_register', $result);
                         // wp_mail( $_POST['user_email'],
                         //  __("You have been logged in via Twitter", ET_DOMAIN),
                         //  "Hi, <br/> your pasword on our site is {$password}");
                         // login
                         $ae_user = wp_signon(array('user_login' => $new_account['user_login'], 'user_password' => $new_account['user_pass']));
                         if (is_wp_error($ae_user)) {
                             global $et_error;
                             $et_error = $ae_user->get_error_message();
                             //echo $user->get_error_message();
                         } else {
                             wp_redirect(home_url());
                             exit;
                         }
                     } else {
                         global $et_error;
                         $et_error = $result->get_error_message();
                     }
                 }
                 // ask people for password
                 include TEMPLATEPATH . '/page-twitter-auth.php';
                 exit;
             }
         }
     }
 }