<?php
// Includes & Settings
error_reporting (E_ERROR);
$loc = setlocale (LC_ALL, 'de_DE.UTF8', 'de_DE', 'de', 'ge');
// Config lesen
$config = parse_ini_file ("config.ini", true);
$num_days = $config['CalendarPlugin']['number_of_days'];
// Passwort und Login für ClientLogin Authentication
$user = $config['System']['google_user'];
$pass = $config['System']['google_pass'];
if ($config['System']['zendfw_path'] != "") set_include_path (get_include_path() . PATH_SEPARATOR . $config['System']['zendfw_path']);
// teste Datenbank Verbindung
$dbconn = new PDO('mysql:host=' . $config['System']['db_host'] . ';dbname=' . $config['System']['db_name'], $config['System']['db_user'], $config['System']['db_password']);
// Zend Gdata libs
require_once 'Zend/Loader.php';
Zend_Loader::loadClass ('Zend_Gdata');
Zend_Loader::loadClass ('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass ('Zend_Http_Client');
Zend_Loader::loadClass ('Zend_Gdata_Query');
Zend_Loader::loadClass ('Zend_Gdata_Feed');
try
{
// login und setze Protokol auf Version 3.0
$client = Zend_Gdata_ClientLogin::getHttpClient ($user, $pass, 'cp');
$gdata = new Zend_Gdata ($client);
$gdata->setMajorProtocolVersion (3);
// query Ausführen und Ergebnis nach feed
$query = new Zend_Gdata_Query ('http://www.google.com/m8/feeds/contacts/default/full');
$query->maxResults = 99999;
// "My Contacts" only
$query->setParam( 'group', 'http://www.google.com/m8/feeds/groups/' . urlencode($user) . '/base/6' );
$feed = $gdata->getFeed ($query);
// parse feed und extraiere Kontakt Informationen
$contacts = array ();
foreach ($feed as $entry)
{
$xml = simplexml_load_string ($entry->getXML ());
$obj = new stdClass;
if (($b = (string) $xml->birthday['when']) != '')
// 1970 falls kein Jarh angegeben ist
$obj->birthday = preg_replace ("/^--/", "1970-", $b);
else
$obj->birthday = null;
$obj->name = (string) $xml->name->fullName;
$contacts[] = $obj;
}
}
catch (Exception $e)
{
die ('ERROR:' . $e->getMessage ());
}
// nur Geburtstage aus contacts löschen
$sql = "DELETE FROM `if_calendar` WHERE `color`='#000000'";
$dbconn->query ($sql) or die ('DELETE gescheitert: ' . $dbconn->errorInfo()[2]);
// alle Daten in results, los gehts
foreach ($contacts as $r)
{
$na = utf8_decode ($r->name);
if ($r->birthday)
{
// Geburtstag in unixtime, Termin ist Monat, Tag mit aktuellem Jahr
$n1 = strtotime (date('o') . '-' . substr ($r->birthday, 5, 5));
// dauert einen Tag
$n2 = $n1 + 60 * 60 * 24;
// liegt Geburtstag in der Vergangenheit? dann Termin plus ein Jahr
if ($n2 < strtotime('now'))
{
$n1 = strtotime ('+ 1 year', $n1);
$n2 = strtotime ('+ 1 year', $n2);
}
// liegt Geburtstag ausserhalb der Anbzeigezeitspanne, dann ignorieren.
if ($n1 < strtotime('+ ' . $num_days . ' days'))
{
$sql = "INSERT INTO `if_calendar` (`color`, `begin`, `end`, `title`, `location`) VALUES ('#000000', FROM_UNIXTIME($n1) , FROM_UNIXTIME($n2) ,'" . $na . " hat Geburtstag','')";
$dbconn->query($sql) or die ('INSERT gescheitert: ' . $dbconn->errorInfo()[2]);
}
}
}
?>