Guest User

Untitled

a guest
Sep 13th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.71 KB | None | 0 0
  1. function admin_oauthCallBack($website_id = '') {
  2.         $this->autoRender = false;
  3.  
  4.         $this->loadModel('Website');
  5.         // Code begins
  6.         $authorizationCode = '';
  7.         $error = '';
  8.  
  9.         $oauthInfo = array();
  10.  
  11.         if (!empty($_REQUEST['code'])) {
  12.             $authorizationCode = trim($_REQUEST['code']);
  13.         }
  14.  
  15.         if (!empty($_REQUEST['error'])) {
  16.             $error = trim($_REQUEST['error']);
  17.         }
  18.  
  19.         if (!empty($_REQUEST['state'])) {
  20.             $website_id = $_REQUEST['state'];
  21.         }
  22.  
  23.         if (!empty($authorizationCode)) {
  24.  
  25.             $data = $this->getAccessToken($authorizationCode);
  26.             //pr($data); die;
  27.  
  28.             if (!empty($data) && empty($data['error'])) {
  29.  
  30.                 $accessToken = $data['access_token'];
  31.                 $refreshToken = $data['refresh_token'];
  32.                 // Customer Service (Single Adwords Account)
  33.                 $clientInfo = $this->getClientInfo($accessToken, $refreshToken);
  34.  
  35.  
  36.                 // Managed Customer Service (Manage Multiple Adwords Account)
  37.                 if (is_string($clientInfo)) {
  38.                     $this->redirect(array('controller' => 'websites', 'action' => 'listClients', $clientInfo, base64_encode($accessToken), base64_encode($refreshToken), base64_encode($website_id)));
  39.                 }
  40.  
  41.                 if (!empty($clientInfo) && is_array($clientInfo)) {
  42.                     $oauth_register_email = $this->getOauthRegisterEmail($accessToken);
  43.                     $update_data = array(
  44.                         'aw_oauth_refresh_token' => "'" . $clientInfo['oauth_refresh_token'] . "'",
  45.                         'aw_oauth_register_email' => "'" . $oauth_register_email . "'",
  46.                         'aw_client_id' => $clientInfo['client_id']
  47.                     );
  48.  
  49.                     $condition = array('id' => $website_id);
  50.  
  51.                     $this->Website->updateAll($update_data, $condition);
  52.                     $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true)); // clients
  53.                 } else {
  54.                     $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true));
  55.                 }
  56.             } else {
  57.                 $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true));
  58.             }
  59.         } else if (!empty($error)) {
  60.             $this->Session->setFlash('Access Denied by User.', 'default', array('class' => 'error'));
  61.             $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true));
  62.         } else {
  63.             header("Location:https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=" . Configure::read('AW_CLIENT_ID') . "&redirect_uri=" . Configure::read('REDIRECT_URI') . "&access_type=offline&approval_prompt=force&state=" . $website_id . "&scope=https://adwords.google.com/api/adwords/email");
  64.             die;
  65.         }
  66.     }
  67.  
  68.     function admin_linkAccount($client_id, $website_id, $access_token, $refresh_token) {
  69.         $clientId = base64_decode($client_id);
  70.         $websiteId = base64_decode($website_id);
  71.         $accessToken = base64_decode($access_token);
  72.         $refreshToken = base64_decode($refresh_token);
  73.         $this->loadModel('Website');
  74.         $oauth_register_email = $this->getOauthRegisterEmail($accessToken);
  75.  
  76.         $update_data = array(
  77.             'aw_oauth_refresh_token' => "'" . $refreshToken . "'",
  78.             'aw_oauth_register_email' => "'" . $oauth_register_email . "'",
  79.             'aw_client_id' => $clientId
  80.         );
  81.  
  82.         $condition = array('id' => $websiteId);
  83.         $this->Website->updateAll($update_data, $condition);
  84.         $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true));
  85.     }
  86.  
  87.     function getClientInfo($access_token, $refresh_token) {
  88.  
  89.         $clients = array();
  90.         try {
  91.  
  92.             $oauth2_info = array('client_id' => Configure::read('AW_CLIENT_ID'),
  93.                 'client_secret' => Configure::read('AW_CLIENT_SECRET'),
  94.                 'refresh_token' => $refresh_token
  95.             );
  96.  
  97.             $user = new AdWordsUser();
  98.             $user->SetOAuth2Info($oauth2_info);
  99.             $user->LogAll();
  100.  
  101.             $customerService = $user->GetService('CustomerService', ADWORDS_VERSION);
  102.  
  103.  
  104.             // Make the get request.
  105.             $result = $customerService->get();
  106.  
  107.             if (!empty($result)) {
  108.  
  109.                 $i = 0;
  110.  
  111.                 $isMCC = $result->canManageClients;
  112.  
  113.                 if (!$isMCC) {
  114.                     $clients['client_id'] = $result->customerId;
  115.                     $clients['oauth_refresh_token'] = $refresh_token;
  116.                 } else {
  117.                     return $result->customerId;
  118.                 }
  119.             }
  120.         } catch (Exception $e) {
  121.            
  122.         }
  123.  
  124.         return $clients;
  125.     }
  126.  
  127.     function getOauthRegisterEmail($access_token) {
  128.         $email = '';
  129.  
  130.         if (!empty($access_token)) {
  131.             $url = "https://www.googleapis.com/oauth2/v2/userinfo?access_token=" . $access_token;
  132.  
  133.             $ch = curl_init();
  134.             curl_setopt($ch, CURLOPT_URL, $url);
  135.  
  136.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  137.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  138.  
  139.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  140.  
  141.             $Rec_Data = curl_exec($ch);
  142.  
  143.             if (curl_exec($ch) === false) {
  144.                 return $email;
  145.             }
  146.  
  147.             $Rec_Data = json_decode($Rec_Data, true);
  148.  
  149.             if (!empty($Rec_Data['email'])) {
  150.                 $email = trim($Rec_Data['email']);
  151.             }
  152.         }
  153.  
  154.         return $email;
  155.     }
  156.  
  157.     function getAccessToken($code) {
  158.  
  159.         $Rec_Data = array();
  160.  
  161.         if (!empty($code)) {
  162.  
  163.             $postFields = 'client_id=' . Configure::read('AW_CLIENT_ID') . '&client_secret=' . Configure::read('AW_CLIENT_SECRET') . '&code=' . $code . '&grant_type=authorization_code&redirect_uri=' . Configure::read('REDIRECT_URI');
  164.  
  165.             $ch = curl_init();
  166.             curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
  167.             curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  168.  
  169.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  170.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  171.  
  172.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  173.  
  174.             $Rec_Data = curl_exec($ch);
  175.  
  176.  
  177.             if (curl_exec($ch) === false) {
  178.                 return $Rec_Data;
  179.                 //echo 'Curl error: ' . curl_error($ch);
  180.             }
  181.  
  182.             $Rec_Data = json_decode($Rec_Data, true);
  183.         }
  184.         return $Rec_Data;
  185.     }
  186. }
Add Comment
Please, Sign In to add comment