/** * @brief parses the file in vcard format * @param $file the input file to import * @param $limit the number of elements to return (-1 = no limit) * @return array() */ private function getSourceElementsFromFile($file, $limit=-1) { $file = StringUtil::convertToUTF8(file_get_contents($file)); $nl = "\n"; $replaceFrom = array("\r","\n\n"); $replaceTo = array("\n","\n"); foreach ($this->configContent->import_core->replace as $replace) { if (isset($replace['from']) && isset($replace['to'])) { $replaceFrom[] = $replace['from']; $replaceTo[] = $replace['to']; } } $file = str_replace($replaceFrom, $replaceTo, $file); $lines = explode($nl, $file); $inelement = false; $parts = array(); $card = array(); $numParts = 0; foreach($lines as $line) { if(strtoupper(trim($line)) == (string)$this->configContent->import_core->card_begin) { $inelement = true; } elseif (strtoupper(trim($line)) == (string)$this->configContent->import_core->card_end) { $card[] = $line; $parts[] = implode($nl, $card); $card = array(); $inelement = false; $numParts++; if ($numParts == $limit) { break; } } if ($inelement === true && trim($line) != '') { $card[] = $line; } } return $parts; }
/** * Decode properties for upgrading from v. 2.1 * * @param Sabre_VObject_Property $property Reference to a \Sabre\VObject\Property. * The only encoding allowed in version 3.0 is 'b' for binary. All encoded strings * must therefore be decoded and the parameters removed. */ protected function decodeProperty(&$property) { foreach ($property->parameters as $key => &$parameter) { // Check for values without names which Sabre interprets // as names without values. if (trim($parameter->value) === '') { $parameter->value = $parameter->name; $parameter->name = $this->paramName($parameter->name); } // Check out for encoded string and decode them :-[ if (strtoupper($parameter->name) == 'ENCODING') { if (strtoupper($parameter->value) == 'QUOTED-PRINTABLE') { $property->value = str_replace("\r\n", "\n", VObject\StringUtil::convertToUTF8(quoted_printable_decode($property->value))); unset($property->parameters[$key]); } elseif (strtoupper($parameter->value) == 'BASE64') { $parameter->value = 'b'; } } elseif (strtoupper($parameter->name) == 'CHARSET') { unset($property->parameters[$key]); } } }
/** * @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; } }
/** * @brief returns the vcard property corresponding to the parameter * creates the property if it doesn't exists yet * @param $vcard the vcard to get or create the properties with * @param $importEntry the parameter to find * @return the property|false */ protected function getOrCreateVCardProperty(&$vcard, $importEntry) { if (isset($vcard) && isset($importEntry)) { // looking for a property with the same name $properties = $vcard->select($importEntry['property']); foreach ($properties as $property) { if ($importEntry['type'] == null && !isset($importEntry->additional_property)) { return $property; } foreach ($property->parameters as $parameter) { // Filtering types if ($parameter->name == 'TYPE' && !strcmp($parameter->value, $importEntry['type'])) { $found = 0; if (isset($importEntry->additional_property)) { // Filtering additional properties if necessary (I know, there are a lot of inner loops, sorry) foreach ($importEntry->additional_property as $additional_property) { if ((string) $parameter->name == $additional_property['name']) { $found++; } } if ($found == count($importEntry->additional_property)) { return $property; } } return $property; } } if (isset($importEntry['group']) && $property->group == $importEntry['group']) { return $property; } } // Property not found, creating one $property = \Sabre\VObject\Property::create($importEntry['property']); $vcard->add($property); if ($importEntry['type'] != null) { $property->parameters[] = new \Sabre\VObject\Parameter('TYPE', '' . StringUtil::convertToUTF8($importEntry['type'])); switch ($importEntry['property']) { case "ADR": $property->setValue(";;;;;;"); break; case "FN": $property->setValue(";;;;"); break; } } if ($importEntry['group'] != null) { $property->group = $importEntry['group']; } return $property; } else { return false; } }
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 */ public function importVcards() { $pProgresskey = $this->params('progresskey'); $pGetprogress = $this->params('getprogress'); $pPath = $this->params('path'); $pFile = $this->params('file'); $pMethod = $this->params('method'); $pAddressbook = $this->params('addressbookname'); $pIsDragged = $this->params('isDragged'); $pId = $this->params('id'); $pOverwrite = $this->params('overwrite'); \OC::$server->getSession()->close(); if (isset($pProgresskey) && isset($pGetprogress)) { $aCurrent = \OC::$server->getCache()->get($pProgresskey); $aCurrent = json_decode($aCurrent); $numVC = isset($aCurrent->{'all'}) ? $aCurrent->{'all'} : 0; $currentVCCount = isset($aCurrent->{'current'}) ? $aCurrent->{'current'} : 0; $currentFn = isset($aCurrent->{'currentFn'}) ? $aCurrent->{'currentFn'} : ''; $percent = isset($aCurrent->{'percent'}) ? $aCurrent->{'percent'} : ''; if ($percent == '') { $percent = 0; } $params = ['status' => 'success', 'percent' => $percent, 'currentmsg' => $currentFn . ' ' . $percent . '% (' . $currentVCCount . '/' . $numVC . ')']; $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); } 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->setProgresskey($pProgresskey); $import->enableProgressCache(); \OCP\Util::writeLog($this->appName, ' PROG: ' . $pProgresskey, \OCP\Util::DEBUG); if (!$import->isValid()) { $params = ['status' => 'error', 'error' => 'notvalid']; $response = new JSONResponse($params); return $response; } $newAddressbook = false; if ($pMethod == 'new') { $id = Addressbook::add($this->userId, $pAddressbook); if ($id) { Addressbook::setActive($id, 1); $newAddressbook = true; } } else { $id = $pId; Addressbook::find($id); $import->setOverwrite($pOverwrite); } //\OCP\Util::writeLog($this->appName,' METHOD: '.$pMethod.'ID:'.$id, \OCP\Util::DEBUG); $import->setAddressbookId($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(); $errorCount = $import->getErrorCount(); if ($count == 0) { if ($newAddressbook) { Addressbook::delete($id); } $params = ['status' => 'error', 'message' => $this->l10n->t('The file contained either no vcard or all vcards are already saved in your addessbook.')]; $response = new JSONResponse($params); return $response; } else { if ($newAddressbook) { $params = ['status' => 'success', 'message' => $count . ' ' . $this->l10n->t('vcards has been saved in the new addressbook') . ' ' . strip_tags($pAddressbook)]; $response = new JSONResponse($params); return $response; } else { $params = ['status' => 'success', 'message' => $errorCount . ' Error - ' . $count . ' ' . $this->l10n->t('vcards has been saved in your addressbook')]; $response = new JSONResponse($params); return $response; } } }
/** * Validates the node for correctness. * * The following options are supported: * - Node::REPAIR - If something is broken, and automatic repair may * be attempted. * * An array is returned with warnings. * * Every item in the array has the following properties: * * level - (number between 1 and 3 with severity information) * * message - (human readable message) * * node - (reference to the offending node) * * @param int $options * * @return array */ function validate($options = 0) { $warnings = []; // Checking if our value is UTF-8 if (!StringUtil::isUTF8($this->getRawMimeDirValue())) { $oldValue = $this->getRawMimeDirValue(); $level = 3; if ($options & self::REPAIR) { $newValue = StringUtil::convertToUTF8($oldValue); if (true || StringUtil::isUTF8($newValue)) { $this->setRawMimeDirValue($newValue); $level = 1; } } if (preg_match('%([\\x00-\\x08\\x0B-\\x0C\\x0E-\\x1F\\x7F])%', $oldValue, $matches)) { $message = 'Property contained a control character (0x' . bin2hex($matches[1]) . ')'; } else { $message = 'Property is not valid UTF-8! ' . $oldValue; } $warnings[] = ['level' => $level, 'message' => $message, 'node' => $this]; } // Checking if the propertyname does not contain any invalid bytes. if (!preg_match('/^([A-Z0-9-]+)$/', $this->name)) { $warnings[] = ['level' => 1, 'message' => 'The propertyname: ' . $this->name . ' contains invalid characters. Only A-Z, 0-9 and - are allowed', 'node' => $this]; if ($options & self::REPAIR) { // Uppercasing and converting underscores to dashes. $this->name = strtoupper(str_replace('_', '-', $this->name)); // Removing every other invalid character $this->name = preg_replace('/([^A-Z0-9-])/u', '', $this->name); } } if ($encoding = $this->offsetGet('ENCODING')) { if ($this->root->getDocumentType() === Document::VCARD40) { $warnings[] = ['level' => 1, 'message' => 'ENCODING parameter is not valid in vCard 4.', 'node' => $this]; } else { $encoding = (string) $encoding; $allowedEncoding = []; switch ($this->root->getDocumentType()) { case Document::ICALENDAR20: $allowedEncoding = ['8BIT', 'BASE64']; break; case Document::VCARD21: $allowedEncoding = ['QUOTED-PRINTABLE', 'BASE64', '8BIT']; break; case Document::VCARD30: $allowedEncoding = ['B']; break; } if ($allowedEncoding && !in_array(strtoupper($encoding), $allowedEncoding)) { $warnings[] = ['level' => 1, 'message' => 'ENCODING=' . strtoupper($encoding) . ' is not valid for this document type.', 'node' => $this]; } } } // Validating inner parameters foreach ($this->parameters as $param) { $warnings = array_merge($warnings, $param->validate($options)); } return $warnings; }
/** * @brief converts a ldif into a owncloud VCard * @param $element the VCard element to convert * @return VCard */ public function convertElementToVCard($element) { $dest = \Sabre\VObject\Component::create('VCARD'); foreach ($element as $ldifProperty) { $importEntry = $this->getImportEntry($ldifProperty[0]); if ($importEntry) { $value = $ldifProperty[1]; if (isset($importEntry['remove'])) { $value = str_replace($importEntry['remove'], '', $ldifProperty[1]); } $values = array($value); if (isset($importEntry['separator'])) { $values = explode($importEntry['separator'], $value); } foreach ($values as $oneValue) { $this->convertElementToProperty($oneValue, $importEntry, $dest); } } else { $property = \Sabre\VObject\Property::create("X-Unknown-Element", ''.StringUtil::convertToUTF8($ldifProperty[1])); $property->parameters[] = new \Sabre\VObject\Parameter('TYPE', ''.StringUtil::convertToUTF8($ldifProperty[0])); $dest->add($property); } } $dest->validate(\Sabre\VObject\Component\VCard::REPAIR); return $dest; }
/** * @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; } } }
/** * @brief gets the import entry corresponding to the name given in parameter * @param $name the parameter name to look for in the connector * @return string|false */ private function getImportEntryFromName($name) { $nbElt = $this->configContent->import_entry->count(); for ($i = 0; $i < $nbElt; $i++) { if ($this->configContent->import_entry[$i]['name'] == StringUtil::convertToUTF8($name) && $this->configContent->import_entry[$i]['enabled'] == 'true') { return $this->configContent->import_entry[$i]; } if (isset($this->configContent->import_entry[$i]->altname)) { foreach ($this->configContent->import_entry[$i]->altname as $altname) { if ($altname == StringUtil::convertToUTF8($name) && $this->configContent->import_entry[$i]['enabled'] == 'true') { return $this->configContent->import_entry[$i]; } } } } return false; }