<?php
date_default_timezone_set('Europe/Warsaw');
mb_internal_encoding("UTF-8");
include_once("config.php");
include_once("import/class.dbf.php");
include_once("import/helper.php");
global $sugar_config;
$sql=mysql_connect($sugar_config['dbconfig']['db_host_name'],$sugar_config['dbconfig']['db_user_name'],$sugar_config['dbconfig']['db_password']);
mysql_select_db($sugar_config['dbconfig']['db_name']);
mysql_query ('SET NAMES utf-8');
	//die('test');
		require_once("Zend/Loader.php");
		Zend_Loader::loadClass('Zend_Gdata');
		Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
		Zend_Loader::loadClass('Zend_Gdata_Calendar');
		Zend_Loader::loadClass('Zend_Http_Client');
		// connect to service
		$c=mysql_query("SELECT google_login,id,CAST(AES_DECRYPT(google_password, 'jakistamhash123') as CHAR) AS google_password,google_calendar_name FROM users");
		while($p = mysql_fetch_array($c)){
		if($p['google_login']!='' && $p['google_password']!=''){
		$gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
		$user = $p['google_login'];
		
		$pass = $p['google_password'];
		$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
		$service = new Zend_Gdata_Calendar($client);
$query = $service->newEventQuery();
$query->setUser('default');
// Set to $query->setVisibility('private-magicCookieValue') if using
// MagicCookie auth
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
$query->setFutureevents('false');
 
// Retrieve the event list from the calendar server
try {
    $eventFeed = $service->getCalendarEventFeed($query);
} catch (Zend_Gdata_App_Exception $e) {
    echo "Error: " . $e->getMessage(); 
}
 
// Iterate through the list of events, outputting them as an HTML list
echo "<ul>";
foreach ($eventFeed as $event) {
	$g=mysql_num_rows(mysql_query("select id from tasks where google_calendar_task='".$event->id."' and assigned_user_id='2e72f487-d92b-954e-f50c-528b10ce81c9'"));
	//echo $event->when[0];
	$a=str_replace(" ",",",$event->when[0]);
	$a=explode(",",$a);
	 list($month, $day0,$day1,$year1,$year2, $g1,$g2,$m1,$m2,$s1,$s2,$st,$st2,) = sscanf($a[1], "%d-%c%c-%c%cT%c%c:%c%c:%c%c+%d:%d Ends: %d-%c%c-%c%cT%c%c:%c%c:%c%c+%d:%d");
	 $date_start=$month.'-'.$day0.''.$day1.'-'.$year1.''.$year2.' '.$g1.''.$g2.':'.$m1.''.$m2.':'.$s1.''.$s2;
	 list($month, $day0,$day1,$year1,$year2, $g1,$g2,$m1,$m2,$s1,$s2,$st,$st2) = sscanf($a[3], "%d-%c%c-%c%cT%c%c:%c%c:%c%c+%d:%d");
	  $date_duo=$month.'-'.$day0.''.$day1.'-'.$year1.''.$year2.' '.$g1.''.$g2.':'.$m1.''.$m2.':'.$s1.''.$s2;
	//echo $date_duo;
	$date1 = new DateTime($date_duo);
$date2 = new DateTime(date("Y-m-d H:i:s"));

if($date1 > $date2){
$status='planned';
} else {
$status='made';
}
	if($g==0){
	echo "INSERT INTO `crm`.`tasks`
(`id`,
`name`,
`date_entered`,
`date_modified`,
`modified_user_id`,
`created_by`,
`description`,
`deleted`,
`assigned_user_id`,
`status`,
`date_due_flag`,
`date_due`,
`date_start_flag`,
`date_start`,
`parent_type`,
`parent_id`,
`contact_id`,
`priority`,
`project_id`,
`project_name`,
`project_task_id`,
`project_task_name`,
`google_calendar_task`,
`assigned_user_id2`)
VALUES
('".create_guid()."',
'".$event->title."',
'".date("Y-m-d H:i:s")."',
'".date("Y-m-d H:i:s")."',
'".$p['id']."',
'".$p['id']."',
'".$event->content."',
'0',
'".$p['id']."',
'".$status."',
'0',
'".$date_duo."',
'0',
'".$date_start."',
'null',
null,
null,
'Medium',
null,
null,
null,
null,
'".$event->id,"',
null);
<br><br>";
	}
 // echo "<li>" . $event->title . " (Event ID: " . $event->id . ")Gdzie: ".implode($event->where)." Kiedy : ".implode($event->when)."</li>";
}
echo "</ul>";
	}
}