<?php

include('server.php');


include(__DIR__.'/vendor/autoload.php');
include(__DIR__.'/GoogleClient.php');


try{

$response = GoogleClient::getClient();

if($response['success'] == 1)
{
        $client = $response['client'];

        $service = new \Google_Service_Drive($client);


        $toplevel = $service->teamdrives->listTeamdrives(['pageSize' => 100]); //->getTeamDrives();

        //$sql = "truncate table rootfolders";
        //mysqli_query($db, $sql);

        $fileid = array();
        $filename = array();
        foreach($toplevel as $x=>$y)
        {
                $id = $toplevel[$x]->id;
                $name = $toplevel[$x]->name;
		$sql = 'select count(*) count from rootfolders where foldername = "' . $name . '" and folderid ="' . $id . '"';
		$results = mysqli_query($db, $sql);
	        $r = mysqli_fetch_assoc($results);
        	if ($r['count'] == 0)
		{
                	$sql = 'insert into rootfolders(foldername, folderid) values("' . $name . '","' . $id . '")';
                	mysqli_query($db, $sql);
		}
        }
}
} catch(\Exception $e){
        echo '<p>Exception occured:'.$e->getMessage().'</p>';
        echo '<p>If issue is in Google api, remove data in token.json and refresh this page and try again.-</p>';
        die;
}

