GoogledriveuploadpdfController.php 10.3 KB
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers;
use App\pdfModel;
use URL;
use Storage;
use Response;
use File;
Use PDF;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Facade;
use League\Flysystem\Filesystem;
use Curl\Curl;
use Google_Client;
use Google_Service_Drive;
use Imagick;




class GoogledriveuploadpdfController extends Controller
{

	public function google_drive_upload($folder, $filename,$PdfId,$cust_id)
	{
		//error_log($cust_id);
		$save_folder = $this->isGoogleFolderCreated($cust_id);
		$group_ids = preg_split( '/-/', $cust_id);
		$group_id = ltrim(rtrim($group_ids[1]));
		$cust_id = ltrim(rtrim($group_ids[0]));
		
		
	
		
		 
		session_start();
		$pdffile = new  FileEntryController();
		$pdfFile = $pdffile->getPDF($folder, $filename);

		$user = new pdfModel();
		$user = $user->getGoogleUserOath($cust_id)->get();;



		$client = new \Google_Client();
		$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
		$client->setClientId($user[0]->google_client_id);
		$client->setClientSecret($user[0]->google_client_secret);
		$client->setRedirectUri($user[0]->redirect_uri);
		$client->setAccessType('offline');

		$_SESSION['access_token'] = $client->getAccessToken();


		$client->refreshToken($user[0]->refresh_token);

		$_SESSION['access_token']= $client->getAccessToken();

		$dr_service = new \Google_Service_Drive($client);
		//error_log(print_r($dr_service ,true));



		$file = new \Google_Service_Drive_DriveFile();

		$parent = new \Google_Service_Drive_ParentReference();
		$parent->setId($save_folder[0]->subfolder_id);
		$file->setParents(array($parent));


		$mimetype = 'application/pdf';
		$uploadType = 'media';
		$filetitle = $filename;
		//$filedescription = 'Taskew ' . $model['discussion_title'];

		$datetime = date('d-m-y-h:s');

		$file->setTitle($datetime . $filetitle);
		$file->setDescription('');
		$file->setMimeType($mimetype);

		$data = $pdfFile;
        //error_log(print_r($file ,true));
		$createdFile = $dr_service->files->insert($file, array(
			'data' => $data,
			'mimeType' => $mimetype, //text/plain',
			'uploadType' => $uploadType
		));
		
		$GoogleId = $createdFile->getId();
		$pdfId = new pdfModel();
		$pdfId = $pdfId->setPdfGoogleId($GoogleId,$PdfId);


	}

public function isGoogleFolderCreated($cust_id)
    {
		$cust_ids = $cust_id;
		$group_ids = preg_split( '/-/', $cust_id);
		$group_id = ltrim(rtrim($group_ids[1]));
		$cust_id = ltrim(rtrim($group_ids[0]));
		
		$saveFolderId = new pdfModel();
		$saveFolderId = $saveFolderId->checkIsGoogleFolderCreated($group_id);
		
		if(count($saveFolderId) < 1){
		
			$newgroup_id = $this->createFolderGoogle($cust_ids);	
			
			$saveFolderId = new pdfModel();
			 $saveFolderId = $saveFolderId->checkIsGoogleFolderCreated($group_id);
			 error_log(print_r($saveFolderId, true));
			return $saveFolderId;
			}else{
			
			return $saveFolderId;
			}
	}

 public function createFolderGoogle($cust_id)
    {   	 
		$user = new pdfModel();
		$user = $user->getGoogleUserOath($cust_id)->get();;

		
		$client = new \Google_Client();
		$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
		$client->setClientId($user[0]->google_client_id);
		$client->setClientSecret($user[0]->google_client_secret);
		$client->setRedirectUri($user[0]->redirect_uri);
		$client->setAccessType('offline');

		$_SESSION['access_token'] = $client->getAccessToken();


		$client->refreshToken($user[0]->refresh_token);

		$_SESSION['access_token']= $client->getAccessToken();

		$dr_service = new \Google_Service_Drive($client);
		
		$service = new \Google_Service_Drive($client);
		
		$group_ids = preg_split( '/-/', $cust_id);
		$group_id = ltrim(rtrim($group_ids[1]));
		$cust_id = ltrim(rtrim($group_ids[0]));
		
        $files = $service->files->listFiles();
         foreach ($files['items'] as $item) {
         if ($item['title'] == $group_id) {
         $folderId = $item['id'];
		 
		  
          break;
         }
         }
		
		if ($client->getAccessToken()) {

	$file = new \Google_Service_Drive_DriveFile();
	
	//Setup the Folder to Create
	$file->setTitle('User Made');
	$file->setDescription('Made by user');
	$file->setMimeType('application/vnd.google-apps.folder');
	
	//Set the ProjectsFolder Parent
	$parent = new \Google_Service_Drive_ParentReference();
	$parent->setId($folderId);
	$file->setParents(array($parent));
	
	//create the ProjectFolder in the Parent
	$createdFile = $service->files->insert($file, array(
		'mimeType' => 'application/vnd.google-apps.folder',
	));
	  
	  $saveFolderId  = new pdfModel();
	  $saveFolderId->saveFolderId($group_id,$folderId, $createdFile->id);
	
	}
		
		
		return $createdFile->id;
   

      

    }






	public function GoogleToken()
	{
session_start();
        require_once realpath(dirname(__FILE__) . '/../autoload.php');


		$client = new \Google_Client();
		$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
		$client->setClientId('206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com');
		$client->setClientSecret('ZzEE02Dqz7AKJLSklmL30LNd');
		$client->setRedirectUri('https://pdf-customizer.synapsus.co/auth/google/tokenCallback');

		$client->setAccessType('offline');
	
        $client->setApprovalPrompt('force');

		if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
  $client->revokeToken();
}

/************************************************
  If we have a code back from the OAuth 2.0 flow,
  we need to exchange that with the authenticate()
  function. We store the resultant access token
  bundle in the session, and redirect to ourself.
 ************************************************/
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

/************************************************
  If we have an access token, we can make
  requests, else we generate an authentication URL.
 ************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

/************************************************
  If we're signed in we can go ahead and retrieve
  the ID token, which is part of the bundle of
  data that is exchange in the authenticate step
  - we only need to do a network call if we have
  to retrieve the Google certificate to verify it,
  and that can be cached.
 ************************************************/
if ($client->getAccessToken()) {
  $_SESSION['access_token'] = $client->getAccessToken();
  $token_data = $client->verifyIdToken()->getAttributes();
}

echo pageHeader("User Query - Retrieving An Id Token");
if (
    $client_id == '206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com'
    || $client_secret == 'ZzEE02Dqz7AKJLSklmL30LNd'
    || $redirect_uri == 'http://localhost:8888/pdf-customizer/public/auth/google/tokenCallback') {
  echo missingClientSecretsWarning();
}
?>
<div class="box">
  <div class="request">
<?php
if (isset($authUrl)) {
  echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
} else {
  echo "<a class='logout' href='?logout'>Logout</a>";
}
?>
  </div>

  <div class="data">
<?php 
if (isset($token_data)) {
  var_dump($token_data);
}
?>
  </div>
</div>
<?php
echo pageFooter(__FILE__);

	
	}


	public function GoogleTokenCode()
	{
		session_start();
        require_once realpath(dirname(__FILE__) . '/../autoload.php');


		$client = new \Google_Client();
		$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
		$client->setClientId('206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com');
		$client->setClientSecret('ZzEE02Dqz7AKJLSklmL30LNd');
		$client->setRedirectUri('https://pdf-customizer.synapsus.co/auth/google/tokenCallback');

		$client->setAccessType('offline');
	
        $client->setApprovalPrompt('force');

		if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
  $client->revokeToken();
}

/************************************************
  If we have a code back from the OAuth 2.0 flow,
  we need to exchange that with the authenticate()
  function. We store the resultant access token
  bundle in the session, and redirect to ourself.
 ************************************************/
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

/************************************************
  If we have an access token, we can make
  requests, else we generate an authentication URL.
 ************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

/************************************************
  If we're signed in we can go ahead and retrieve
  the ID token, which is part of the bundle of
  data that is exchange in the authenticate step
  - we only need to do a network call if we have
  to retrieve the Google certificate to verify it,
  and that can be cached.
 ************************************************/
if ($client->getAccessToken()) {
  $_SESSION['access_token'] = $client->getAccessToken();
  $token_data = $client->verifyIdToken()->getAttributes();
}

echo pageHeader("User Query - Retrieving An Id Token");
if (
    $client_id == '206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com'
    || $client_secret == 'ZzEE02Dqz7AKJLSklmL30LNd'
    || $redirect_uri == 'http://localhost:8888/pdf-customizer/public/auth/google/tokenCallback') {
  echo missingClientSecretsWarning();
}
?>
<div class="box">
  <div class="request">
<?php
if (isset($authUrl)) {
  echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
} else {
  echo "<a class='logout' href='?logout'>Logout</a>";
}
?>
  </div>

  <div class="data">
<?php 
if (isset($token_data)) {
  var_dump($token_data);
}
?>
  </div>
</div>
<?php
echo pageFooter(__FILE__);

	}

}