/home/desid573/cf.desidrivers.com.au/site/google/api
Edit: /home/desid573/cf.desidrivers.com.au/site/google/api/calendarGet.php (8080B)
setApplicationName('Chapel Farm Calendar');
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfig('client_secret.json');
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory('credentials.json');
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = "4/1AX4XfWjCLkysGmPD2SQkcr3lK_O3aA1_Eal85TRCRQB4xkyJxPpRSXt8gEo";
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
error_log(json_encode($accessToken, true));
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path)
{
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);
date_default_timezone_set("Australia/Perth");
$calendarId = "54ihu71fo4adsvh6avt8s2lrnk@group.calendar.google.com";
$optParams = array('timeMin' => date("c", strtotime("now")), 'singleEvents' => true, 'orderBy' => "startTime");
error_log($calendarId." ".$ref);
$events = $service->events->listEvents($calendarId, $optParams);
//print_r($events);
$dateStart = null;
$dateEnd = null;
$nextDay = null;
$dateArray = array();
$length = count($events);
//print_r($events->getItems());
foreach ($events->getItems() as $event) {
$date = new DateTime($event->getStart()->getDate());
$length = $length - 1;
error_log($length);
if($dateStart == null) {
$dateStart = $date;
$nextDay = $date->format("Y-m-d");
error_log("start date ".$nextDay);
// echo "found start date
";
} else {
//error_log($date->format("Y-m-d")."!=".$nextDay);
error_log(strtotime($date->format("Y-m-d")) .">". strtotime($nextDay));
if($date->format("Y-m-d") != $nextDay) {
$lastDay = $nextDay;
$nextDay = date("Y-m-d", strtotime($nextDay.' 00:01 +1 day'));
error_log($date->format("Y-m-d")." > ".$nextDay);
if(strtotime($date->format("Y-m-d")) > strtotime($nextDay)) {
$dateEnd = $date;
error_log("found not next day");
// if($dateStart->format("Y-m-d") != $lastDay) {
array_push($dateArray, ["start" => $dateStart->format("Y-m-d"), "end" => $lastDay, "type" => "CF"]);
// } else {
// array_push($dateArray, ["start" => $dateStart->format("Y-m-d"), "end" => ""]);
// }
$dateStart = $date;
$nextDay = $date->format("Y-m-d");
} else {
error_log("found next day");
}
} else {
error_log("found same date");
}
if($length == 0) {
// if($dateStart->format("Y-m-d") != $date->format("Y-m-d")) {
array_push($dateArray, ["start" => $dateStart->format("Y-m-d"), "end" => $date->format("Y-m-d"), "type" => "CF"]);
// } else {
// array_push($dateArray, ["start" => $dateStart->format("Y-m-d"), "end" => ""]);
// }
// echo "found end";
}
}
}
$calendarId = "s1rio4cc0b8ru7nckidvv9or1c@group.calendar.google.com";
$optParams = array('timeMin' => date("c", strtotime("now")), 'singleEvents' => true, 'orderBy' => "startTime");
error_log($calendarId." ".$ref);
$events = $service->events->listEvents($calendarId, $optParams);
//print_r($events);
$dateStart = null;
$dateEnd = null;
$nextDay = null;
$length = count($events);
foreach ($events->getItems() as $event) {
$date = new DateTime($event->getStart()->getDate());
$length = $length - 1;
error_log($length);
if($dateStart == null) {
$dateStart = $date;
$nextDay = $date->format("Y-m-d");
error_log("start date ".$nextDay);
// echo "found start date
";
} else {
//error_log($date->format("Y-m-d")."!=".$nextDay);
error_log(strtotime($date->format("Y-m-d")) .">". strtotime($nextDay));
if($date->format("Y-m-d") != $nextDay) {
$lastDay = $nextDay;
$nextDay = date("Y-m-d", strtotime($nextDay.' 00:01 +1 day'));
error_log($date->format("Y-m-d")." > ".$nextDay);
if(strtotime($date->format("Y-m-d")) > strtotime($nextDay)) {
$dateEnd = $date;
error_log("found not next day");
// if($dateStart->format("Y-m-d") != $lastDay) {
array_push($dateArray, ["start" => $dateStart->format("Y-m-d"), "end" => $lastDay, "type" => "JB"]);
// } else {
// array_push($dateArray, ["start" => $dateStart->format("Y-m-d"), "end" => ""]);
// }
$dateStart = $date;
$nextDay = $date->format("Y-m-d");
} else {
error_log("found next day");
}
} else {
error_log("found same date");
}
if($length == 0) {
// if($dateStart->format("Y-m-d") != $date->format("Y-m-d")) {
array_push($dateArray, ["start" => $dateStart->format("Y-m-d"), "end" => $date->format("Y-m-d"), "type" => "JB"]);
// } else {
// array_push($dateArray, ["start" => $dateStart->format("Y-m-d"), "end" => ""]);
// }
// echo "found end";
}
}
}
echo json_encode($dateArray, true);
//echo $events->items->id;
?>