способ обмена с облачными календарями
Применимо к Я.календарю
Есть такой проект simpleCalDAV
Я его скачал и подключил к МДМ в папку сервер\htdocs\3rdparty\simpleCalDAV
Потом получил пароль приложения в Яндексе, сохранил его в свойстве gg('Sergey.yacalendar')
Теперь можно посмотреть на свои календари в Яндексе
require_once '3rdparty/simpleCalDAV/SimpleCalDAVClient.php';
$client = new SimpleCalDAVClient();
try {
$client->connect(
'https://caldav.yandex.ru:443/',
'мой_логин@yandex.ru', // login with @yandex.ru
gg('Sergey.yacalendar') // password
);
$arrayOfCalendars = $client->findCalendars(); // Returns an array of all accessible calendars on the server.
} catch (Exception $e) {
echo $e->__toString();
}
echo '<pre>';
print_r($arrayOfCalendars);
Ищем запись своего календаря events - xxxxxxx
и дальше работаем с ним
экспорт
require_once '3rdparty/simpleCalDAV/SimpleCalDAVClient.php';
//header("Content-type: text/text; charset=UTF-8"); //вывод в браузер
//отберём нужное
$events = SQLSelect("SELECT clnd_events.*,
clnd_categories.ICON, clnd_categories.title CAT_NAME
FROM clnd_events left join clnd_categories
on clnd_events.calendar_category_id=clnd_categories.id
WHERE clnd_categories.title in ('Люди','Праздники','Рабочие','Заказы', 'Дела') and clnd_categories.AT_CALENDAR=1
and `IS_DONE`!=1 and `DUE`< DATE_ADD(NOW(), INTERVAL 3 MONTH ) ");//AND EX_ID is null");
$NewEventH ="BEGIN:VCALENDAR".PHP_EOL.
"VERSION:2.0".PHP_EOL.
"PRODID:-//mdm todoer".PHP_EOL.
"X-WR-CALNAME:MDM todoer".PHP_EOL.
"CALSCALE:GREGORIAN".PHP_EOL.
"BEGIN:VTIMEZONE".PHP_EOL.
"TZID:Europe/Moscow".PHP_EOL.
"TZURL:http://tzurl.org/zoneinfo/Europe/Moscow".PHP_EOL.
"X-LIC-LOCATION:Europe/Moscow".PHP_EOL.
"BEGIN:STANDARD".PHP_EOL.
"TZOFFSETFROM:+0300".PHP_EOL.
"TZOFFSETTO:+0300".PHP_EOL.
"TZNAME:MSK".PHP_EOL.
"DTSTART:19700101T000000".PHP_EOL.
"END:STANDARD".PHP_EOL.
"END:VTIMEZONE".PHP_EOL;
$client = new SimpleCalDAVClient();
try {
$client->connect(
'https://caldav.yandex.ru:443/',
'мой_логин@yandex.ru', // login with @yandex.ru
gg('Sergey.yacalendar') // password
);
$arrayOfCalendars = $client->findCalendars(); // Returns an array of all accessible calendars on the server.
} catch (Exception $e) {
echo $e->__toString();
}
$client->setCalendar($arrayOfCalendars["events-710952"]); // вот он - наш календарь
if($events){
//для каждой задачи
foreach ($events as $ev )
{
$newToDo = "BEGIN:VEVENT".PHP_EOL;
$newToDo .= "DTSTAMP:".gmdate('Ymd/THis').'Z'.PHP_EOL;
if($ev['DUE']){
$dt = date_create($ev['DUE']);
if($ev['ALL_DAY']){
$newToDo .= "DTSTART;VALUE=DATE:".date_format($dt,'Ymd').PHP_EOL;
}else{
$newToDo .= "DTSTART;TZID=Europe/Moscow:".date_format($dt,'Ymd\THis').PHP_EOL;
}
}
if($ev['END_TIME']){
$dt = date_create($ev['END_TIME']);
if($ev['ALL_DAY']){
$dt->modify('+1 day');
$newToDo .= "DTEND;VALUE=DATE:".date_format($dt,'Ymd').PHP_EOL;
}else{
$newToDo .= "DTEND;TZID=Europe/Moscow:".date_format($dt,'Ymd\THis').PHP_EOL;
}
}
$newToDo .= "SUMMARY:".$ev['CAT_NAME']."/".$ev['TITLE'].PHP_EOL;
$newToDo .= "DESCRIPTION:".str_replace(array("\r\n", "\r", "\n"),'\\n', $ev['NOTES']).PHP_EOL;
//$newToDo .= "DESCRIPTION:".$ev['NOTES'].PHP_EOL;
//$newToDo .= "UID:todoerEvent".$ev['CAT_NAME']."/".$ev['TITLE']."/".$ev['DUE'].'@mdm.clnd'.PHP_EOL;
if($ev['IS_NODATE']){
$dueId = "IS_NODATE";
}else{
$dueId = date_format(date_create($ev['DUE']),'YmdHis');
}
$uid = 'todoerEvent'.$ev['ID'].'-'.$dueId.'@mdm.clnd';
$newToDo .= "UID:".$uid.PHP_EOL;
$newToDo .= "END:VEVENT".PHP_EOL;
//echo($NewEventH.$newToDo."END:VCALENDAR".PHP_EOL);
try {
$client->create($NewEventH.$newToDo."END:VCALENDAR".PHP_EOL);
echo('export '."UID:todoerEvent".$ev['ID'].'@mdm.clnd '."SUMMARY:".$ev['CAT_NAME']."/".$ev['TITLE']."<br>");
SQLExec("UPDATE `clnd_events` SET LAST_SYNCHRO='".date('Y-m-d H:i:s')."',EX_ID='$uid' WHERE `ID`=".$ev['ID']);
} catch (Exception $e) {
echo $e->__toString();
}
}
}
echo("ok");
импорт в категорию Yandex
require_once '3rdparty/simpleCalDAV/SimpleCalDAVClient.php';
$client = new SimpleCalDAVClient();
try {
$client->connect(
'https://caldav.yandex.ru:443/',
'мой_логин@yandex.ru', // login with @yandex.ru
gg('Sergey.yacalendar') // app password from yandex
);
$arrayOfCalendars = $client->findCalendars(); // Returns an array of all accessible calendars on the server.
echo("<pre>");
print_r($arrayOfCalendars);
} catch (Exception $e) {
echo $e->__toString();
}
$client->setCalendar($arrayOfCalendars["events-710952"]);
$dt_scan = gmdate('Ymd',time() - 3 * 24 * 60 * 60).'T000000Z';
$my = $client->getEvents($dt_scan);
echo("<pre>");
print_r($my);
foreach($my as $ev){
$all = $ev->getData();
$pos = stripos($all, "BEGIN:VEVENT");
$pos1 = stripos($all, "END:VEVENT");
if($pos && $pos1){
$event = explode("\n",substr($all,$pos + 13,$pos1 - $pos - 14));//between BEGIN:VEVENT & END:VEVENT
//print_r( $event);
$todoerId = 0;
$todoerCat = '';
$todoerTitle = '';
$description = ''; $categories = '';
$summary = ''; $start = 0; $end = 0; $lastMod = 0;
$todoer = false;
foreach($event as $line){
echo($line."<br>");
//парсим по-строчно
if(preg_match('/^DTSTART;VALUE=DATE:(\d\d\d\d\d\d\d\d)/su', $line, $m)){
$start0 = date_create_from_format('Ymd',$m[1]);
$start = date_format($start0, 'Y-m-d 00:00:00');
}elseif(preg_match('/^DTSTART;TZID=(.*):(\d\d\d\d\d\d\d\dT\d\d\d\d\d\d)/su', $line, $m)){
$start0 = date_create_from_format('Ymd\THis',$m[2],new DateTimeZone($m[1]));
date_timezone_set($start0, timezone_open('Europe/Moscow'));
$start = date_format($start0, 'Y-m-d H:i:00');
}elseif(preg_match('/^DTEND;VALUE=DATE:(\d\d\d\d\d\d\d\d)/su', $line, $m)){
$end0 = date_create_from_format('Ymd',$m[1]);
$end = date_format($end0, 'Y-m-d 00:00:00');
}elseif(preg_match('/^DTEND;TZID=(.*):(\d\d\d\d\d\d\d\dT\d\d\d\d\d\d)/su', $line, $m)){
$end0 = date_create_from_format('Ymd\THis',$m[2],new DateTimeZone($m[1]));
date_timezone_set($end0, timezone_open('Europe/Moscow'));
$end = date_format($end0, 'Y-m-d H:i:00');
}elseif(str_starts_with($line,"SUMMARY:")){
$summary = str_replace("SUMMARY:",'',$line);
}elseif(str_starts_with($line,"LAST-MODIFIED:")){
$lm = str_replace("LAST-MODIFIED:",'',$line);
}elseif(str_starts_with($line,"UID:")){
$uid = str_replace("UID:",'',$line);
if(str_starts_with($uid,"todoerEvent")){
$todoer = true;
$todoerId = (int)str_replace(array("todoerEvent","@mdm.clnd"),'',$uid);
$td = explode("/",$summary);
if($td[0]) $todoerCat = $td[0];
if($td[1]) $todoerTitle = $td[1];
}else{
$todoer = false;
}
}elseif(str_starts_with($line,"DESCRIPTION:")){
$description = str_replace("DESCRIPTION:",'',$line);
$description = str_replace('\\n',PHP_EOL,$description);
$description = str_replace('\\,',',',$description);
$description = str_replace('\\;',';',$description);
}elseif(preg_match('/^LAST-MODIFIED:(\d\d\d\d\d\d\d\dT\d\d\d\d\d\d)Z/su', $line, $m)){
$lastMod0 = date_create_from_format("Ymd\THis",$m[1],new DateTimeZone('UTC'));
$lastMod = date_format($lastMod0, 'Y-m-d H:i:s');
}elseif(str_starts_with($line,"CATEGORIES:")){
$categories = str_replace("CATEGORIES:",'',$line);
}
}
}
echo "start:$start end:$end summary:$summary <br>";
echo " uid:$uid todoerId:$todoerId todoerCat:$todoerCat todoerTitle:$todoerTitle<br>";
if($description>'')echo " description:".mb_substr($description,0,20)."<br>";
echo " categories:$categories<br>";
echo " lastMod gmt:$lastMod<br>";
echo('--------------------------------<br>');
if(!$todoer){ //search in db
$interval = date_diff($start0,$end0);
echo("diff ".$interval->d." d ".$interval->days." days ".$interval->h." h ".$interval->i." m <br>");
if(($interval->d > 0 && $interval->h == 0 && $interval->i == 0) ){ //|| ($interval->h == 23 && $interval->i == 59)){
$all_day = 1;
echo('all_day<br>');
$end2 = date_create_from_format('Y-m-d H:i:s',$end);
date_modify($end2, '-1 minute');
echo 'new end:'.date_format($end2, 'Y-m-d 23:59:00').'<br>';
$end = date_format($end2, 'Y-m-d 23:59:00');
}else{
$all_day =0;
}
$rec = SQLSelectOne("SELECT c.ID FROM `clnd_events` c join clnd_categories cc on c.CALENDAR_CATEGORY_ID=cc.ID WHERE cc.TITLE='Yandex' AND c.TITLE='".DBSafe($summary)."' AND DUE='$start'");
if(!$rec['ID']) {
if(strtotime($end)>= time()){//не берём прошлое
echo('Добавим в todoer в категорию Yandex?<br>');
//add new
include_once(DIR_MODULES . 'todoer/todoer.class.php');
$todo = new todoer();
$tsk = array(
'TITLE' => $summary,
'DUE' => $start,
'END_TIME' => $end,
'NOTES' => $description,
'CATEGORY' => "Yandex",
'ALL_DAY' => $all_day,
'EX_ID' => $uid,
'LAST_SYNCHRO' => date('Y-m-d H:i:s')
// ... and so on
);
//echoes id of new task
$todoerid = $todo->create_new_task($tsk);
//SQLExec("UPDATE `clnd_events` SET LAST_SYNCHRO='".date('Y-m-d H:i:s')."' WHERE `ID`=".$todoerid);
echo " Да, добавили!<br>";
}
}else{echo "Нет, уже есть!<br>";}
}
}
echo("ok");
Всё настроено на мою временную зону Мск
// based on original work from the PHP Laravel framework
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
// source: Laravel Framework
// https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Str.php
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) {
return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
}
}
Домодедово, Россия
На форуме: xor