2012-02-20 14:26:22 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
//check for calendar rights or create new one
|
|
|
|
ob_start();
|
2012-05-30 20:23:23 +04:00
|
|
|
|
2012-05-03 14:23:29 +04:00
|
|
|
OCP\JSON::checkLoggedIn();
|
2012-05-02 21:08:37 +04:00
|
|
|
OCP\App::checkAppEnabled('calendar');
|
2012-06-04 19:42:59 +04:00
|
|
|
session_write_close();
|
2012-05-30 20:23:23 +04:00
|
|
|
|
2012-04-22 00:12:31 +04:00
|
|
|
$nl="\r\n";
|
|
|
|
$comps = array('VEVENT'=>true, 'VTODO'=>true, 'VJOURNAL'=>true);
|
2012-05-30 20:23:23 +04:00
|
|
|
|
2012-06-04 19:42:59 +04:00
|
|
|
global $progresskey;
|
2012-06-09 16:40:15 +04:00
|
|
|
$progresskey = 'calendar.import-' . $_POST['progresskey'];
|
2012-06-04 19:42:59 +04:00
|
|
|
|
2012-06-09 16:40:15 +04:00
|
|
|
if (isset($_POST['progress']) && $_POST['progress']) {
|
2012-06-04 19:42:59 +04:00
|
|
|
echo OC_Cache::get($progresskey);
|
|
|
|
die;
|
|
|
|
}
|
2012-05-30 20:23:23 +04:00
|
|
|
|
|
|
|
function writeProgress($pct) {
|
2012-06-04 19:42:59 +04:00
|
|
|
global $progresskey;
|
|
|
|
OC_Cache::set($progresskey, $pct, 300);
|
2012-02-20 14:26:22 +04:00
|
|
|
}
|
2012-05-30 20:23:23 +04:00
|
|
|
writeProgress('10');
|
2012-02-20 14:26:22 +04:00
|
|
|
$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
|
|
|
|
if($_POST['method'] == 'new'){
|
2012-05-01 20:50:31 +04:00
|
|
|
$id = OC_Calendar_Calendar::addCalendar(OCP\USER::getUser(), $_POST['calname']);
|
2012-02-20 14:26:22 +04:00
|
|
|
OC_Calendar_Calendar::setCalendarActive($id, 1);
|
|
|
|
}else{
|
|
|
|
$calendar = OC_Calendar_App::getCalendar($_POST['id']);
|
2012-05-01 20:50:31 +04:00
|
|
|
if($calendar['userid'] != OCP\USER::getUser()){
|
2012-05-03 14:23:29 +04:00
|
|
|
OCP\JSON::error();
|
2012-02-20 14:26:22 +04:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
$id = $_POST['id'];
|
|
|
|
}
|
2012-05-30 20:23:23 +04:00
|
|
|
writeProgress('20');
|
2012-04-22 00:12:31 +04:00
|
|
|
// normalize the newlines
|
|
|
|
$file = str_replace(array("\r","\n\n"), array("\n","\n"), $file);
|
|
|
|
$lines = explode("\n", $file);
|
|
|
|
unset($file);
|
2012-05-30 20:23:23 +04:00
|
|
|
writeProgress('30');
|
2012-04-22 00:12:31 +04:00
|
|
|
// analyze the file, group components by uid, and keep refs to originating calendar object
|
|
|
|
// $cals is array calendar objects, keys are 1st line# $cal, ie array( $cal => $caldata )
|
|
|
|
// $caldata is array( 'first' => 1st component line#, 'last' => last comp line#, 'end' => end line# )
|
|
|
|
// $caldata is used to create prefix/suffix strings when building import text
|
|
|
|
// $uids is array of component arrays, keys are $uid, ie array( $uid => array( $beginlineno => $component ) )
|
|
|
|
// $component is array( 'end' => end line#, 'cal'=> $cal )
|
|
|
|
$comp=$uid=$cal=false;
|
|
|
|
$cals=$uids=array();
|
2012-02-20 14:26:22 +04:00
|
|
|
$i = 0;
|
2012-04-22 00:12:31 +04:00
|
|
|
foreach($lines as $line) {
|
|
|
|
|
|
|
|
if(strpos($line, ':')!==false) {
|
|
|
|
list($attr, $val) = explode(':', strtoupper($line));
|
|
|
|
if ($attr == 'BEGIN' && $val == 'VCALENDAR') {
|
|
|
|
$cal = $i;
|
|
|
|
$cals[$cal] = array('first'=>$i,'last'=>$i,'end'=>$i);
|
|
|
|
} elseif ($attr =='BEGIN' && $cal!==false && isset($comps[$val])) {
|
|
|
|
$comp = $val;
|
|
|
|
$beginNo = $i;
|
|
|
|
} elseif ($attr == 'END' && $cal!==false && $val == 'VCALENDAR') {
|
|
|
|
if($comp!==false) {
|
|
|
|
unset($cals[$cal]); // corrupt calendar, unset it
|
|
|
|
} else {
|
|
|
|
$cals[$cal]['end'] = $i;
|
|
|
|
}
|
|
|
|
$comp=$uid=$cal=false; // reset calendar
|
|
|
|
} elseif ($attr == 'END' && $comp!==false && $val == $comp) {
|
|
|
|
if(! $uid) {
|
|
|
|
$uid = OC_Calendar_Object::createUID();
|
2012-02-20 14:26:22 +04:00
|
|
|
}
|
2012-04-22 00:12:31 +04:00
|
|
|
$uids[$uid][$beginNo] = array('end'=>$i, 'cal'=>$cal);
|
|
|
|
if ($cals[$cal]['first'] == $cal) {
|
|
|
|
$cals[$cal]['first'] = $beginNo;
|
2012-02-20 14:26:22 +04:00
|
|
|
}
|
2012-04-22 00:12:31 +04:00
|
|
|
$cals[$cal]['last'] = $i;
|
|
|
|
$comp=$uid=false; // reset component
|
|
|
|
} elseif ($attr =="UID" && $comp!==false) {
|
|
|
|
list($attr, $uid) = explode(':', $line);
|
2012-02-20 14:26:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
2012-04-22 00:12:31 +04:00
|
|
|
// import the calendar
|
2012-05-30 20:23:23 +04:00
|
|
|
writeProgress('60');
|
2012-04-22 00:12:31 +04:00
|
|
|
foreach($uids as $uid) {
|
|
|
|
|
|
|
|
$prefix=$suffix=$content=array();
|
|
|
|
foreach($uid as $begin=>$details) {
|
|
|
|
|
|
|
|
$cal = $details['cal'];
|
|
|
|
if(!isset($cals[$cal])) {
|
|
|
|
continue; // from corrupt/incomplete calendar
|
|
|
|
}
|
|
|
|
$cdata = $cals[$cal];
|
|
|
|
// if we have multiple components from different calendar objects,
|
|
|
|
// we should really merge their elements (enhancement?) -- 1st one wins for now.
|
|
|
|
if(! count($prefix)) {
|
|
|
|
$prefix = array_slice($lines, $cal, $cdata['first'] - $cal);
|
2012-02-20 14:26:22 +04:00
|
|
|
}
|
2012-04-22 00:12:31 +04:00
|
|
|
if(! count($suffix)) {
|
|
|
|
$suffix = array_slice($lines, $cdata['last']+1, $cdata['end'] - $cdata['last']);
|
|
|
|
}
|
|
|
|
$content = array_merge($content, array_slice($lines, $begin, $details['end'] - $begin + 1));
|
2012-02-20 14:26:22 +04:00
|
|
|
}
|
2012-04-22 00:12:31 +04:00
|
|
|
if(count($content)) {
|
|
|
|
$import = join($nl, array_merge($prefix, $content, $suffix)) . $nl;
|
2012-02-20 14:26:22 +04:00
|
|
|
OC_Calendar_Object::add($id, $import);
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 00:12:31 +04:00
|
|
|
// finished import
|
2012-05-30 20:23:23 +04:00
|
|
|
writeProgress('100');
|
2012-02-20 14:26:22 +04:00
|
|
|
sleep(3);
|
2012-06-04 19:42:59 +04:00
|
|
|
OC_Cache::remove($progresskey);
|
2012-06-16 11:45:17 +04:00
|
|
|
OCP\JSON::success();
|