/**
  * @NoAdminRequired
  * @param string $importurl
  */
 public function checkImportUrl($importurl)
 {
     $externUriFile = trim(urldecode($importurl));
     $newUrl = '';
     $bExistUri = false;
     $getProtocol = explode('://', $externUriFile);
     if (strtolower($getProtocol[0]) === 'webcal') {
         $newUrl = 'https://' . $getProtocol[1];
         $aMetaHttps = $this->stream_last_modified($newUrl);
         if ($aMetaHttps['fileaccess'] !== true) {
             $newUrl = 'http://' . $getProtocol[1];
             $aMetaHttp = $this->stream_last_modified($newUrl);
             if ($aMetaHttp['fileaccess'] !== true) {
                 $bExistUri = false;
             } else {
                 $bExistUri = true;
             }
         } else {
             $bExistUri = true;
         }
     } else {
         $protocol = $getProtocol[0];
         if (preg_match('%index.php/apps/calendarplus/s/(/.*)?%', $externUriFile)) {
             $temp = explode('/s/', $externUriFile);
             $externUriFile = $temp[0] . '/exporteventscalendar?t=' . $temp[1];
         }
         $newUrl = $externUriFile;
         $aMeta = $this->stream_last_modified($newUrl);
         if ($aMeta['fileaccess'] === true) {
             $bExistUri = true;
         }
     }
     $opts = array($protocol => array('method' => 'GET', 'header' => "Content-Type: text/calendar\r\n", 'timeout' => 60));
     $bError = false;
     if ($bExistUri === true) {
         $context = stream_context_create($opts);
         try {
             $file = file_get_contents($newUrl, false, $context);
             //\OCP\Util::writeLog('calendarplus','FILE: '.$newUrl, \OCP\Util::DEBUG);
             $import = new \OCA\CalendarPlus\Import($file);
             $import->setUserID($this->userId);
             $guessedcalendarname = \OCP\Util::sanitizeHTML($import->guessCalendarName());
             $testColor = $import->guessCalendarColor();
             $guessedcalendarcolor = $testColor !== null ? $testColor : '006DCC';
             $params = ['status' => 'success', 'file' => $file, 'externUriFile' => $externUriFile, 'guessedcalendarname' => $guessedcalendarname, 'guessedcalendarcolor' => $guessedcalendarcolor];
             $response = new JSONResponse($params);
             return $response;
         } catch (Exception $e) {
             $params = ['status' => 'error', 'message' => (string) $this->l10n->t('Subscribed url is not valid')];
             $response = new JSONResponse($params);
             return $response;
         }
     } else {
         $params = ['status' => 'error', 'message' => (string) $this->l10n->t('Subscribed url is not valid')];
         $response = new JSONResponse($params);
         return $response;
     }
 }
 private function addEventsFromSubscribedCalendar($externUriFile, $calName, $calColor)
 {
     $externUriFile = trim($externUriFile);
     $newUrl = '';
     $bExistUri = false;
     $getProtocol = explode('://', $externUriFile);
     if (strtolower($getProtocol[0]) === 'webcal') {
         $newUrl = 'https://' . $getProtocol[1];
         $last_modified = $this->stream_last_modified($newUrl);
         if (is_null($last_modified)) {
             $newUrl = 'http://' . $getProtocol[1];
             $last_modified = $this->stream_last_modified($newUrl);
             if (is_null($last_modified)) {
                 $bExistUri = false;
             } else {
                 $bExistUri = true;
             }
         } else {
             $bExistUri = true;
         }
     } else {
         $protocol = $getProtocol[0];
         $newUrl = $externUriFile;
         $last_modified = $this->stream_last_modified($newUrl);
         if (!is_null($last_modified)) {
             $bExistUri = true;
         }
     }
     $opts = array($protocol => array('method' => 'POST', 'header' => "Content-Type: text/calendar\r\n", 'timeout' => 60));
     $bError = false;
     if ($bExistUri === true) {
         $context = stream_context_create($opts);
         try {
             $file = file_get_contents($newUrl, false, $context);
         } catch (Exception $e) {
             $params = ['status' => 'error', 'message' => $this->l10n->t('Import failed')];
             $response = new JSONResponse($params);
             return $response;
         }
         //\OCP\Util::writeLog('calendar', 'FILE IMPORT-> '.$file, \OCP\Util::DEBUG);
         $file = \Sabre\VObject\StringUtil::convertToUTF8($file);
         $import = new Import($file);
         $import->setUserID($this->userId);
         $import->setTimeZone(CalendarApp::$tz);
         $calendarid = CalendarCalendar::addCalendar($this->userId, $calName, 'VEVENT,VTODO,VJOURNAL', null, 0, strip_tags($calColor), 1, $newUrl, $last_modified);
         CalendarCalendar::setCalendarActive($calendarid, 1);
         $import->setCalendarID($calendarid);
         try {
             $import->import();
         } catch (Exception $e) {
             $params = ['status' => 'error', 'message' => $this->l10n->t('Import failed')];
             $response = new JSONResponse($params);
             return $response;
         }
         $count = $import->getCount();
     } else {
         $bError = true;
     }
     return ['isError' => $bError, 'countEvents' => $count, 'calendarid' => $calendarid];
 }
 /**
  * @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 importEventsPerDrop()
 {
     $pCalid = $this->params('calid');
     $pAddCal = $this->params('addCal');
     $pAddCalCol = $this->params('addCalCol');
     $data = $this->params('data');
     $data = explode(',', $data);
     $data = end($data);
     $data = base64_decode($data);
     $import = new Import($data);
     $import->setUserID($this->userId);
     $import->setTimeZone(CalendarApp::$tz);
     $import->disableProgressCache();
     if (!$import->isValid()) {
         $params = ['status' => 'error', 'error' => 'notvalid'];
         $response = new JSONResponse($params);
         return $response;
     }
     if ($pCalid == 'newCal' && $pAddCal != '') {
         $calendars = CalendarCalendar::allCalendars($this->userId);
         foreach ($calendars as $calendar) {
             if ($calendar['displayname'] == $pAddCal) {
                 $id = $calendar['id'];
                 $newcal = false;
                 break;
             }
             $newcal = true;
         }
         if ($newcal) {
             $id = CalendarCalendar::addCalendar($this->userId, strip_tags($pAddCal), 'VEVENT,VTODO,VJOURNAL', null, 0, strip_tags($pAddCalCol));
             CalendarCalendar::setCalendarActive($id, 1);
         }
     } else {
         $id = $pCalid;
         $calendar = CalendarApp::getCalendar($id);
         if ($calendar['userid'] != $this->userId) {
             $params = ['status' => 'error', 'error' => 'missingcalendarrights'];
             $response = new JSONResponse($params);
             return $response;
         }
     }
     $import->setOverwrite(false);
     $import->setCalendarID($id);
     $import->import();
     $count = $import->getCount();
     if ($count == 0) {
         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 {
         $newcalendarname = strip_tags($pAddCal);
         if ($pAddCal != '') {
             $params = ['status' => 'success', 'message' => $count . ' ' . $this->l10n->t('events has been saved in the new calendar') . ' ' . $newcalendarname, '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 the calendar') . ' ' . $calendar['displayname'], 'eventSource' => ''];
             $response = new JSONResponse($params);
             return $response;
         }
     }
 }