Merge branch 'merge-requests/105'

This commit is contained in:
Georg Ehrke 2012-04-19 20:31:24 +02:00
commit 3aeea0c196
2 changed files with 70 additions and 60 deletions

View File

@ -11,7 +11,7 @@ OC_Util::checkLoggedIn();
OC_Util::checkAppEnabled('calendar'); OC_Util::checkAppEnabled('calendar');
$cal = isset($_GET['calid']) ? $_GET['calid'] : NULL; $cal = isset($_GET['calid']) ? $_GET['calid'] : NULL;
$event = isset($_GET['eventid']) ? $_GET['eventid'] : NULL; $event = isset($_GET['eventid']) ? $_GET['eventid'] : NULL;
$nl = "\n\r"; $nl = "\r\n";
if(isset($cal)){ if(isset($cal)){
$calendar = OC_Calendar_App::getCalendar($cal); $calendar = OC_Calendar_App::getCalendar($cal);
$calobjects = OC_Calendar_Object::all($cal); $calobjects = OC_Calendar_Object::all($cal);

View File

@ -10,7 +10,8 @@ ob_start();
require_once ('../../lib/base.php'); require_once ('../../lib/base.php');
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_Util::checkAppEnabled('calendar'); OC_Util::checkAppEnabled('calendar');
$nl = "\n"; $nl="\r\n";
$comps = array('VEVENT'=>true, 'VTODO'=>true, 'VJOURNAL'=>true);
$progressfile = 'import_tmp/' . md5(session_id()) . '.txt'; $progressfile = 'import_tmp/' . md5(session_id()) . '.txt';
if(is_writable('import_tmp/')){ if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w'); $progressfopen = fopen($progressfile, 'w');
@ -29,30 +30,58 @@ if($_POST['method'] == 'new'){
} }
$id = $_POST['id']; $id = $_POST['id'];
} }
//analyse the calendar file
if(is_writable('import_tmp/')){ if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w'); $progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '20'); fwrite($progressfopen, '20');
fclose($progressfopen); fclose($progressfopen);
} }
$searchfor = array('VEVENT', 'VTODO', 'VJOURNAL'); // normalize the newlines
$parts = $searchfor; $file = str_replace(array("\r","\n\n"), array("\n","\n"), $file);
$filearr = explode($nl, $file); $lines = explode("\n", $file);
$inelement = false; unset($file);
$parts = array(); if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '30');
fclose($progressfopen);
}
// 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();
$i = 0; $i = 0;
foreach($filearr as $line){ foreach($lines as $line) {
foreach($searchfor as $search){
if(substr_count($line, $search) == 1){ if(strpos($line, ':')!==false) {
list($attr, $val) = explode(':', $line); list($attr, $val) = explode(':', strtoupper($line));
if($attr == 'BEGIN'){ if ($attr == 'BEGIN' && $val == 'VCALENDAR') {
$parts[]['begin'] = $i; $cal = $i;
$inelement = true; $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;
} }
if($attr == 'END'){ $comp=$uid=$cal=false; // reset calendar
$parts[count($parts) - 1]['end'] = $i; } elseif ($attr == 'END' && $comp!==false && $val == $comp) {
$inelement = false; if(! $uid) {
$uid = OC_Calendar_Object::createUID();
} }
$uids[$uid][$beginNo] = array('end'=>$i, 'cal'=>$cal);
if ($cals[$cal]['first'] == $cal) {
$cals[$cal]['first'] = $beginNo;
}
$cals[$cal]['last'] = $i;
$comp=$uid=false; // reset component
} elseif ($attr =="UID" && $comp!==false) {
list($attr, $uid) = explode(':', $line);
} }
} }
$i++; $i++;
@ -60,54 +89,35 @@ foreach($filearr as $line){
// import the calendar // import the calendar
if(is_writable('import_tmp/')){ if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w'); $progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '40'); fwrite($progressfopen, '60');
fclose($progressfopen); fclose($progressfopen);
} }
$start = ''; foreach($uids as $uid) {
for ($i = 0; $i < $parts[0]['begin']; $i++) {
if($i == 0){ $prefix=$suffix=$content=array();
$start = $filearr[0]; foreach($uid as $begin=>$details) {
}else{
$start .= $nl . $filearr[$i]; $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);
} }
$end = ''; if(! count($suffix)) {
for($i = $parts[count($parts) - 1]['end'] + 1;$i <= count($filearr) - 1; $i++){ $suffix = array_slice($lines, $cdata['last']+1, $cdata['end'] - $cdata['last']);
if($i == $parts[count($parts) - 1]['end'] + 1){
$end = $filearr[$parts[count($parts) - 1]['end'] + 1];
}else{
$end .= $nl . $filearr[$i];
} }
$content = array_merge($content, array_slice($lines, $begin, $details['end'] - $begin + 1));
} }
if(is_writable('import_tmp/')){ if(count($content)) {
$progressfopen = fopen($progressfile, 'w'); $import = join($nl, array_merge($prefix, $content, $suffix)) . $nl;
fwrite($progressfopen, '50');
fclose($progressfopen);
}
$importready = array();
foreach($parts as $part){
for($i = $part['begin']; $i <= $part['end'];$i++){
if($i == $part['begin']){
$content = $filearr[$i];
}else{
$content .= $nl . $filearr[$i];
}
}
$importready[] = $start . $nl . $content . $nl . $end;
}
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '70');
fclose($progressfopen);
}
if(count($parts) == 1){
OC_Calendar_Object::add($id, $file);
}else{
foreach($importready as $import){
OC_Calendar_Object::add($id, $import); OC_Calendar_Object::add($id, $import);
} }
} }
//done the import // finished import
if(is_writable('import_tmp/')){ if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w'); $progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '100'); fwrite($progressfopen, '100');