/**
  * @NoAdminRequired
  *@param integer $calendarid
  */
 public function refreshSubscribedCalendar($calendarid)
 {
     $calendar = CalendarApp::getCalendar($calendarid, false, false);
     if (!$calendar) {
         $params = ['status' => 'error', 'message' => 'permission denied'];
         $response = new JSONResponse($params);
         return $response;
     }
     if ($calendar['uri'] !== 'bdaycpltocal_' . $calendar['userid']) {
         $getProtocol = explode('://', $calendar['externuri']);
         $protocol = $getProtocol[0];
         $opts = array($protocol => array('method' => 'POST', 'header' => "Content-Type: text/calendar\r\n", 'timeout' => 60));
         $aMeta = $this->stream_last_modified(trim($calendar['externuri']));
         if ($aMeta['fileaccess'] === true) {
             $context = stream_context_create($opts);
             $file = file_get_contents($calendar['externuri'], false, $context);
             $file = \Sabre\VObject\StringUtil::convertToUTF8($file);
             $import = new Import($file);
             $import->setUserID($this->userId);
             $import->setTimeZone(CalendarApp::$tz);
             $import->setOverwrite(true);
             //$import -> setCheckModifiedDate(true);
             $import->setImportFromUri(true);
             $import->setCalendarID($calendarid);
             try {
                 $import->import();
                 $importCount = $import->getCountImport();
                 $params = ['status' => 'success', 'refresh' => $calendarid, 'count' => $importCount];
                 $response = new JSONResponse($params);
                 return $response;
             } catch (Exception $e) {
                 $params = ['status' => 'error', 'message' => (string) $this->l10n->t('Import failed')];
                 $response = new JSONResponse($params);
                 return $response;
             }
         } else {
             $params = ['status' => 'error', 'message' => (string) $this->l10n->t('Import failed')];
             $response = new JSONResponse($params);
             return $response;
         }
     } else {
         $this->addBirthdays($this->userId, (int) $calendarid);
         $params = ['status' => 'success', 'refresh' => $calendarid];
         $response = new JSONResponse($params);
         return $response;
     }
 }
 /**
  * @NoAdminRequired
  */
 public function importEvents()
 {
     $pProgresskey = $this->params('progresskey');
     $pGetprogress = $this->params('getprogress');
     $pPath = $this->params('path');
     $pFile = $this->params('file');
     $pMethod = $this->params('method');
     $pCalname = $this->params('calname');
     $pCalcolor = $this->params('calcolor');
     $pId = $this->params('id');
     $pOverwrite = $this->params('overwrite');
     $pIsDragged = $this->params('isDragged');
     $pIsSub = $this->params('isSub');
     $pSubUri = $this->params('subUri');
     $pSubUri = isset($pSubUri) ? $pSubUri : '';
     \OC::$server->getSession()->close();
     if (isset($pProgresskey) && isset($pGetprogress)) {
         $aCurrent = \OC::$server->getCache()->get($pProgresskey);
         $aCurrent = json_decode($aCurrent);
         $numVO = isset($aCurrent->{'all'}) ? $aCurrent->{'all'} : 0;
         $currentVOCount = isset($aCurrent->{'current'}) ? $aCurrent->{'current'} : 0;
         $currentSummary = isset($aCurrent->{'currentSummary'}) ? $aCurrent->{'currentSummary'} : '';
         $percent = isset($aCurrent->{'percent'}) ? $aCurrent->{'percent'} : '';
         if ($percent == '') {
             $percent = 0;
         }
         $params = ['status' => 'success', 'percent' => $percent, 'currentmsg' => $currentSummary . ' ' . $percent . '% (' . $currentVOCount . '/' . $numVO . ')'];
         $response = new JSONResponse($params);
         return $response;
     }
     if ($pIsDragged === 'true') {
         //OCP\JSON::error(array('error'=>'404'));
         $file = explode(',', $pFile);
         $file = end($file);
         $file = base64_decode($file);
     } elseif ($pIsSub === 'true') {
         $file = $pFile;
     } else {
         $file = \OC\Files\Filesystem::file_get_contents($pPath . '/' . $pFile);
     }
     if (!$file) {
         $params = ['status' => 'error', 'error' => '404'];
         $response = new JSONResponse($params);
         return $response;
     }
     $file = \Sabre\VObject\StringUtil::convertToUTF8($file);
     $import = new Import($file);
     $import->setUserID($this->userId);
     $import->setTimeZone(CalendarApp::$tz);
     $import->enableProgressCache();
     $import->setProgresskey($pProgresskey);
     $bUriImport = $pIsSub === 'true' ? true : false;
     $import->setImportFromUri($bUriImport);
     if (!$import->isValid()) {
         $params = ['status' => 'error', 'error' => 'notvalid'];
         $response = new JSONResponse($params);
         return $response;
     }
     $newcal = false;
     if ($pMethod == 'new') {
         $calendars = CalendarCalendar::allCalendars($this->userId);
         foreach ($calendars as $calendar) {
             if ($calendar['displayname'] == $pCalname) {
                 $id = $calendar['id'];
                 $newcal = false;
                 break;
             }
             $newcal = true;
         }
         if ($newcal) {
             $isSubscribed = 0;
             if ($pIsSub === 'true') {
                 $isSubscribed = 1;
             }
             $id = CalendarCalendar::addCalendar($this->userId, strip_tags($pCalname), 'VEVENT,VTODO,VJOURNAL', null, 0, strip_tags($pCalcolor), $isSubscribed, $pSubUri);
             CalendarCalendar::setCalendarActive($id, 1);
         }
     } else {
         $id = $pId;
         $calendar = CalendarApp::getCalendar($id);
         if ($calendar['userid'] != $this->userId) {
             $params = ['status' => 'error', 'error' => 'missingcalendarrights'];
             $response = new JSONResponse($params);
             return $response;
         }
         $import->setOverwrite($pOverwrite);
     }
     $import->setCalendarID($id);
     try {
         $import->import();
     } catch (\Exception $e) {
         $params = ['status' => 'error', 'message' => $this->l10n->t('Import failed')];
         $response = new JSONResponse($params);
         return $response;
     }
     $count = $import->getCount();
     if ($count == 0) {
         if ($newcal) {
             CalendarCalendar::deleteCalendar($id);
         }
         $params = ['status' => 'error', 'message' => $this->l10n->t('The file contained either no events or all events are already saved in your calendar.')];
         $response = new JSONResponse($params);
         return $response;
     } else {
         if ($newcal) {
             $params = ['status' => 'success', 'message' => $count . ' ' . $this->l10n->t('events has been saved in the new calendar') . ' ' . strip_tags($pCalname), 'eventSource' => CalendarCalendar::getEventSourceInfo(CalendarCalendar::find($id))];
             $response = new JSONResponse($params);
             return $response;
         } else {
             $params = ['status' => 'success', 'message' => $count . ' ' . $this->l10n->t('events has been saved in your calendar'), 'eventSource' => ''];
             $response = new JSONResponse($params);
             return $response;
         }
     }
 }