<?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);
$cny_prefix = preg_replace ("/^\+/", "00", $config['System']['cny_prefix']);
$cty_prefix = $config['System']['cty_prefix'];
$num_days = $config['CalendarPlugin']['number_of_days'];
// OAuth 2.0 Authentication
$client_id = $config['System']['client_id'];
$client_secret = $config['System']['client_secret'];
$access_token = $config['System']['access_token'];
$refresh_token = $config['System']['refresh_token'];
// Google-php-api-master
if (isset ($config['System']['google_api_path'])) set_include_path (get_include_path () . PATH_SEPARATOR . $config['System']['google_api_path']);
require_once 'src/Google/autoload.php';
// 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']);
session_start();
if (isset($_SESSION['access_token']))
$access_token = $_SESSION['access_token'];
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=9999&alt=json&v=3.0&oauth_token=' . $access_token;
$xmlresponse = curl($url);
$response = json_decode($xmlresponse, true);
if ($response['error']['code'] == 401)
{
// access_token abgelaufen, neuen holen und in Session speichern
$url = "https://www.googleapis.com/oauth2/v3/token";
$post = "client_id=" . urlencode($client_id);
$post .= "&client_secret=" . urlencode($client_secret);
$post .= "&refresh_token=" . urlencode($refresh_token);
$post .= "&grant_type=refresh_token";
$xmlresponse = curl($url, $post);
$response = json_decode($xmlresponse, true);
$access_token = $response['access_token'];
$_SESSION['access_token'] = $response['access_token'];
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=9999&alt=json&v=3.0&oauth_token=' . $access_token;
$xmlresponse = curl($url);
$response = json_decode($xmlresponse, true);
}
$contacts = array ();
foreach ($response['feed']['entry'] as $entry)
{
$obj = new stdClass;
if (($b = $entry['gContact$birthday']['when']) != '')
// 1970 falls kein Jarh angegeben ist
$obj->birthday = preg_replace ("/^--/", "1970-", $b);
else
$obj->birthday = null;
$obj->name = $entry['gd$name']['gd$fullName']['$t'];
$obj->upd = $entry['updated']['$t'];
foreach ($entry['gd$phoneNumber'] as $p)
{
//rel suchen
$rel = $p['rel'];
//wenn rel nicht da vielleicht ein user defined label?
if ($rel == null) $rel= '#' . $p['label'];
// nur ab # wollen wir haben und in array
$obj->phoneType[] = substr ($rel, strpos ($rel, '#') + 1, strlen ($rel));
// und nummer in array
$obj->phoneNumber[] = $p['$t'];
}
// find pictures
foreach ( $entry['link'] as $link )
{
if (!empty ($link['gd$etag']))
{
$image = curl($link['href'] . "&access_token=" . $access_token);
$obj->image = "cache/google_contacts_images/" . $obj->name . ".jpg";
file_put_contents ($obj->image, $image);
}
}
// speichern
$contacts[] = $obj;
}
$sql = "DELETE FROM `if_phonebook`";
$dbconn->query ($sql) or die ('DELETE gescheitert: ' . $dbconn->errorInfo()[2]);
// 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]);
}
}
if (property_exists ($r, "phoneNumber"))
{
if (property_exists ($r, "image"))
$im = $r->image;
else
$im = "";
$up = $r->upd;
for ($i = 0; $i < count ($r->phoneNumber); $i++)
{
$ty = utf8_decode ($r->phoneType[$i]);
// Nummer ohne space, _, - () etc...
$no = utf8_decode ($r->phoneNumber[$i]);
$no = preg_replace ("/[^\+,0-9]/", "", $no); // alles was nicht Ziffer ist raus
$no = preg_replace ("/^\+/", "00", $no); // + -> 00
$no = preg_replace ("/^" . $cny_prefix . "/", "0", $no); // führende 0049 durch 0 ersetzen
$no = preg_replace ("/^00/", "+", $no); // 00 -> +
$no = preg_replace ("/^" . $cty_prefix . "/", "", $no); // city durch nix
// existiert die Nummer schon?
$sql = "SELECT * FROM `if_phonebook` WHERE `number`='" . $no . "'";
$res = $dbconn->query ($sql) or die ('SELECT * gescheitert: ' . $dbconn->errorInfo()[2]);
if ($res->rowCount () > 0)
{
// Nummer existiet schon
$row = $res->fetch(PDO::FETCH_ASSOC);
error_log("Warnung: doppelte Nummer: " . $no . " - " . $na . ": Bereits vergeben für " . $row['name'], 0);
}
else
{
// Nummer existiert noch nicht, alles einfügen
$sql = "INSERT INTO `if_phonebook`(`number`, `name`, `type`, `image`, `update`) VALUES ('" . $no . "','" . $na . "','" . $ty . "','" . $im . "','" . $up . "')";
$dbconn->query($sql) or die ('INSERT gescheitert: ' . $dbconn->errorInfo()[2]);
}
}
}
}
function curl($url, $post = "")
{
$curl = curl_init();
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
if ($post != "")
{
curl_setopt($curl, CURLOPT_POST, 5);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}
?>