nextcloud/apps/calendar/import.php

120 lines
3.0 KiB
PHP
Raw Normal View History

2011-10-04 00:50:10 +04:00
<?php
/**
2012-01-14 17:22:30 +04:00
* Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de>
2011-10-04 00:50:10 +04:00
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
2012-01-14 17:22:30 +04:00
//check for calendar rights or create new one
ob_start();
OC_JSON::checkLoggedIn();
2011-10-04 00:50:10 +04:00
OC_Util::checkAppEnabled('calendar');
$nl = "\n";
2012-01-14 17:22:30 +04:00
$progressfile = 'import_tmp/' . md5(session_id()) . '.txt';
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '10');
fclose($progressfopen);
}
$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
if($_POST['method'] == 'new'){
2011-10-08 15:28:01 +04:00
$id = OC_Calendar_Calendar::addCalendar(OC_User::getUser(), $_POST['calname']);
2011-10-04 00:50:10 +04:00
OC_Calendar_Calendar::setCalendarActive($id, 1);
2012-01-14 17:22:30 +04:00
}else{
$calendar = OC_Calendar_App::getCalendar($_POST['id']);
if($calendar['userid'] != OC_USER::getUser()){
OC_JSON::error();
exit();
}
$id = $_POST['id'];
}
//analyse the calendar file
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '20');
fclose($progressfopen);
}
$searchfor = array('VEVENT', 'VTODO', 'VJOURNAL');
$parts = $searchfor;
$filearr = explode($nl, $file);
2012-01-14 17:22:30 +04:00
$inelement = false;
$parts = array();
$i = 0;
foreach($filearr as $line){
foreach($searchfor as $search){
if(substr_count($line, $search) == 1){
list($attr, $val) = explode(':', $line);
if($attr == 'BEGIN'){
$parts[]['begin'] = $i;
$inelement = true;
}
if($attr == 'END'){
$parts[count($parts) - 1]['end'] = $i;
$inelement = false;
}
}
}
$i++;
}
//import the calendar
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '40');
fclose($progressfopen);
}
$start = '';
for ($i = 0; $i < $parts[0]['begin']; $i++) {
if($i == 0){
$start = $filearr[0];
2011-10-04 00:50:10 +04:00
}else{
2012-01-14 17:22:30 +04:00
$start .= $nl . $filearr[$i];
2011-10-04 00:50:10 +04:00
}
}
2012-01-14 17:22:30 +04:00
$end = '';
for($i = $parts[count($parts) - 1]['end'] + 1;$i <= count($filearr) - 1; $i++){
if($i == $parts[count($parts) - 1]['end'] + 1){
$end = $filearr[$parts[count($parts) - 1]['end'] + 1];
}else{
$end .= $nl . $filearr[$i];
}
2011-10-04 00:50:10 +04:00
}
2012-01-14 17:22:30 +04:00
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
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);
}
2011-10-04 00:50:10 +04:00
}
2012-01-14 17:22:30 +04:00
//done the import
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '100');
fclose($progressfopen);
2011-10-04 00:50:10 +04:00
}
2012-01-14 17:22:30 +04:00
sleep(3);
if(is_writable('import_tmp/')){
unlink($progressfile);
2011-10-04 00:50:10 +04:00
}
2012-01-14 17:22:30 +04:00
OC_JSON::success();