Exemple #1
0
 function afterWriteContent($path, \Sabre\DAV\IFile $node)
 {
     if ($node instanceof \Sabre\CardDAV\ICard) {
         $iUserId = $this->server->getUser();
         if (isset($iUserId)) {
             $iTenantId = $node instanceof \Afterlogic\DAV\CardDAV\SharedCard ? 0 : null;
             $sContactFileName = $node->getName();
             $oContactDb = $this->oApiContactsManager->getContactByStrId($iUserId, $sContactFileName, $iTenantId);
             if (!isset($oContactDb)) {
                 $oVCard = \Sabre\VObject\Reader::read($node->get(), \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES);
                 if ($oVCard && $oVCard->UID) {
                     $oContactDb = $this->oApiContactsManager->getContactByStrId($iUserId, (string) $oVCard->UID . '.vcf', $iTenantId);
                 }
             }
             $oContact = new \CContact();
             $oContact->InitFromVCardStr($iUserId, $node->get());
             $oContact->IdContactStr = $sContactFileName;
             $oContact->IdTenant = $iTenantId;
             if (isset($oContactDb)) {
                 $oContact->IdContact = $oContactDb->IdContact;
                 $oContact->IdDomain = $oContactDb->IdDomain;
                 $oContact->SharedToAll = !!$oContactDb->SharedToAll;
                 $this->oApiContactsManager->updateContact($oContact);
             } else {
                 $this->oApiContactsManager->createContact($oContact);
             }
         }
     }
 }
Exemple #2
0
 /**
  * @return array
  */
 public function AjaxContactsSaveVcf()
 {
     $oAccount = $this->getAccountFromParam();
     $mResult = false;
     if (!$this->oApiCapability->isPersonalContactsSupported($oAccount)) {
         throw new \ProjectCore\Exceptions\ClientException(\ProjectCore\Notifications::ContactsNotAllowed);
     }
     $sTempFile = (string) $this->getParamValue('File', '');
     if (empty($sTempFile)) {
         throw new \ProjectCore\Exceptions\ClientException(\ProjectCore\Notifications::InvalidInputParameter);
     }
     $oApiFileCache = \CApi::Manager('filecache');
     $sData = $oApiFileCache->get($oAccount, $sTempFile);
     if (!empty($sData)) {
         $oContactsApi = $this->ApiContacts();
         if ($oContactsApi) {
             $oContact = new \CContact();
             $oContact->InitFromVCardStr($oAccount->IdUser, $sData);
             if ($oContactsApi->createContact($oContact)) {
                 $mResult = array('Uid' => $oContact->IdContact);
             }
         }
     }
     return $this->DefaultResponse($oAccount, __FUNCTION__, $mResult);
 }
Exemple #3
0
 /**
  * @param int $iUserId
  * @param mixed $mContactId
  * @return CContact | false
  */
 public function getContactById($iUserId, $mContactId)
 {
     $oContact = false;
     if ($this->init($iUserId)) {
         $oContactItem = $this->getItem($iUserId, \afterlogic\DAV\Constants::ADDRESSBOOK_DEFAULT_NAME, $mContactId);
         if ($oContactItem) {
             $sVCardData = $oContactItem['data'];
             if ($sVCardData) {
                 $oContact = new CContact();
                 $oContact->InitFromVCardStr($iUserId, $sVCardData);
                 $oContact->IdContact = $mContactId;
                 $oContact->ETag = $oContactItem['etag'];
             }
         }
     }
     return $oContact;
 }
Exemple #4
0
 /**
  * Downloads message from IMAP and returns it.
  * 
  * @param CAccount $oAccount Account object.
  * @param string $sFolderFullNameRaw Raw full name of the Folder.
  * @param int $iUid UID of the message to download.
  * @param string $sRfc822SubMimeIndex = ''. Index at which a message is taken to parse. Index is used if the message is another message attachment.
  * @param bool $bParseICalAndVcard = false. If **true** ical and vcard attachments will be parsed.
  * @param bool $bParseAsc = false. If **true** attachments with extension .asc will be parsed.
  * @param int $iBodyTextLimit = 0. If **> 0** will be received only part of the message body. If **= 0** the message body is not limited.
  *
  * @return CApiMailMessage
  *
  * @throws CApiInvalidArgumentException
  */
 public function getMessage($oAccount, $sFolderFullNameRaw, $iUid, $sRfc822SubMimeIndex = '', $bParseICalAndVcard = false, $bParseAsc = false, $iBodyTextLimit = 0)
 {
     if (0 === strlen($sFolderFullNameRaw) || !is_numeric($iUid) || 0 >= (int) $iUid) {
         throw new CApiInvalidArgumentException();
     }
     $iUid = (int) $iUid;
     $oImapClient =& $this->_getImapClient($oAccount);
     $oImapClient->FolderExamine($sFolderFullNameRaw);
     $oMessage = false;
     $sICalMimeIndex = '';
     $sVCardMimeIndex = '';
     $aTextMimeIndexes = array();
     $aAscPartsIds = array();
     $aFetchResponse = $oImapClient->Fetch(array(\MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE), $iUid, true);
     $oBodyStructure = 0 < count($aFetchResponse) ? $aFetchResponse[0]->GetFetchBodyStructure($sRfc822SubMimeIndex) : null;
     if ($oBodyStructure) {
         $aTextParts = $oBodyStructure->SearchHtmlOrPlainParts();
         if (is_array($aTextParts) && 0 < count($aTextParts)) {
             foreach ($aTextParts as $oPart) {
                 $aTextMimeIndexes[] = array($oPart->PartID(), $oPart->Size());
             }
         }
         if ($bParseICalAndVcard) {
             $aICalPart = $oBodyStructure->SearchByContentType('text/calendar');
             $oICalPart = is_array($aICalPart) && 0 < count($aICalPart) ? $aICalPart[0] : null;
             $sICalMimeIndex = $oICalPart ? $oICalPart->PartID() : '';
             $aVCardPart = $oBodyStructure->SearchByContentType('text/vcard');
             $aVCardPart = $aVCardPart ? $aVCardPart : $oBodyStructure->SearchByContentType('text/x-vcard');
             $oVCardPart = is_array($aVCardPart) && 0 < count($aVCardPart) ? $aVCardPart[0] : null;
             $sVCardMimeIndex = $oVCardPart ? $oVCardPart->PartID() : '';
         }
         if ($bParseAsc) {
             $aAscParts = $oBodyStructure->SearchByCallback(function ($oPart) {
                 return '.asc' === \strtolower(\substr(\trim($oPart->FileName()), -4));
             });
             if (is_array($aAscParts) && 0 < count($aAscParts)) {
                 foreach ($aAscParts as $oPart) {
                     $aAscPartsIds[] = $oPart->PartID();
                 }
             }
         }
     }
     $aFetchItems = array(\MailSo\Imap\Enumerations\FetchType::INDEX, \MailSo\Imap\Enumerations\FetchType::UID, \MailSo\Imap\Enumerations\FetchType::RFC822_SIZE, \MailSo\Imap\Enumerations\FetchType::INTERNALDATE, \MailSo\Imap\Enumerations\FetchType::FLAGS, 0 < strlen($sRfc822SubMimeIndex) ? \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $sRfc822SubMimeIndex . '.HEADER]' : \MailSo\Imap\Enumerations\FetchType::BODY_HEADER_PEEK);
     if (0 < count($aTextMimeIndexes)) {
         if (0 < strlen($sRfc822SubMimeIndex) && is_numeric($sRfc822SubMimeIndex)) {
             $sLine = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $aTextMimeIndexes[0][0] . '.1]';
             if (\is_numeric($iBodyTextLimit) && 0 < $iBodyTextLimit && $iBodyTextLimit < $aTextMimeIndexes[0][1]) {
                 $sLine .= '<0.' . (int) $iBodyTextLimit . '>';
             }
             $aFetchItems[] = $sLine;
         } else {
             foreach ($aTextMimeIndexes as $aTextMimeIndex) {
                 $sLine = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $aTextMimeIndex[0] . ']';
                 if (\is_numeric($iBodyTextLimit) && 0 < $iBodyTextLimit && $iBodyTextLimit < $aTextMimeIndex[1]) {
                     $sLine .= '<0.' . (int) $iBodyTextLimit . '>';
                 }
                 $aFetchItems[] = $sLine;
             }
         }
     }
     if (0 < strlen($sICalMimeIndex)) {
         $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $sICalMimeIndex . ']';
     }
     if (0 < strlen($sVCardMimeIndex)) {
         $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $sVCardMimeIndex . ']';
     }
     if (0 < count($aAscPartsIds)) {
         foreach ($aAscPartsIds as $sPartID) {
             $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $sPartID . ']';
         }
     }
     if (!$oBodyStructure) {
         $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE;
     }
     $aFetchResponse = $oImapClient->Fetch($aFetchItems, $iUid, true);
     if (0 < count($aFetchResponse)) {
         $oMessage = CApiMailMessage::createInstance($sFolderFullNameRaw, $aFetchResponse[0], $oBodyStructure, $sRfc822SubMimeIndex, $aAscPartsIds);
     }
     if ($oMessage) {
         $sFromEmail = '';
         $oFromCollection = $oMessage->getFrom();
         if ($oFromCollection && 0 < $oFromCollection->Count()) {
             $oFrom =& $oFromCollection->GetByIndex(0);
             if ($oFrom) {
                 $sFromEmail = trim($oFrom->GetEmail());
             }
         }
         if (0 < strlen($sFromEmail)) {
             $oApiUsersManager = CApi::Manager('users');
             $oSettings =& CApi::GetSettings();
             $bAlwaysShowImagesInMessage = !!$oSettings->GetConf('WebMail/AlwaysShowImagesInMessage');
             $oMessage->setSafety($bAlwaysShowImagesInMessage ? true : $oApiUsersManager->getSafetySender($oAccount->IdUser, $sFromEmail, true));
         }
         /*if ($bParseAsc && 0 < count($aAscPartsIds))
         		{
         			
         		}*/
         if ($bParseICalAndVcard) {
             $oApiCapa = CApi::Manager('capability');
             $oApiFileCache = CApi::Manager('filecache');
             // ICAL
             $sICal = $oMessage->getExtend('ICAL_RAW');
             if (!empty($sICal) && $oApiCapa->isCalendarSupported($oAccount)) {
                 $oApiCalendarManager = CApi::Manager('calendar');
                 if ($oApiCalendarManager) {
                     $mResult = $oApiCalendarManager->processICS($oAccount, trim($sICal), $sFromEmail);
                     if (is_array($mResult) && !empty($mResult['Action']) && !empty($mResult['Body'])) {
                         $sTemptFile = md5($mResult['Body']) . '.ics';
                         if ($oApiFileCache && $oApiFileCache->put($oAccount, $sTemptFile, $mResult['Body'])) {
                             $oIcs = CApiMailIcs::createInstance();
                             $oIcs->Uid = $mResult['UID'];
                             $oIcs->Sequence = $mResult['Sequence'];
                             $oIcs->File = $sTemptFile;
                             $oIcs->Attendee = isset($mResult['Attendee']) ? $mResult['Attendee'] : null;
                             $oIcs->Type = $mResult['Action'];
                             $oIcs->Location = !empty($mResult['Location']) ? $mResult['Location'] : '';
                             $oIcs->Description = !empty($mResult['Description']) ? $mResult['Description'] : '';
                             $oIcs->When = !empty($mResult['When']) ? $mResult['When'] : '';
                             $oIcs->CalendarId = !empty($mResult['CalendarId']) ? $mResult['CalendarId'] : '';
                             if (!$oApiCapa->isCalendarAppointmentsSupported($oAccount)) {
                                 $oIcs->Type = 'SAVE';
                             }
                             // TODO
                             //								$oIcs->Calendars = array();
                             //								if (isset($mResult['Calendars']) && is_array($mResult['Calendars']) && 0 < count($mResult['Calendars']))
                             //								{
                             //									foreach ($mResult['Calendars'] as $sUid => $sName)
                             //									{
                             //										$oIcs->Calendars[$sUid] = $sName;
                             //									}
                             //								}
                             $oMessage->addExtend('ICAL', $oIcs);
                         } else {
                             CApi::Log('Can\'t save temp file "' . $sTemptFile . '"', ELogLevel::Error);
                         }
                     }
                 }
             }
             // VCARD
             $sVCard = $oMessage->getExtend('VCARD_RAW');
             if (!empty($sVCard) && $oApiCapa->isContactsSupported($oAccount)) {
                 $oApiContactsManager = CApi::Manager('contacts');
                 $oContact = new CContact();
                 $oContact->InitFromVCardStr($oAccount->IdUser, $sVCard);
                 $oContact->initBeforeChange();
                 $oContact->IdContact = 0;
                 $bContactExists = false;
                 if (0 < strlen($oContact->ViewEmail)) {
                     if ($oApiContactsManager) {
                         $oLocalContact = $oApiContactsManager->getContactByEmail($oAccount->IdUser, $oContact->ViewEmail);
                         if ($oLocalContact) {
                             $oContact->IdContact = $oLocalContact->IdContact;
                             $bContactExists = true;
                         }
                     }
                 }
                 $sTemptFile = md5($sVCard) . '.vcf';
                 if ($oApiFileCache && $oApiFileCache->put($oAccount, $sTemptFile, $sVCard)) {
                     $oVcard = CApiMailVcard::createInstance();
                     $oVcard->Uid = $oContact->IdContact;
                     $oVcard->File = $sTemptFile;
                     $oVcard->Exists = !!$bContactExists;
                     $oVcard->Name = $oContact->FullName;
                     $oVcard->Email = $oContact->ViewEmail;
                     $oMessage->addExtend('VCARD', $oVcard);
                 } else {
                     CApi::Log('Can\'t save temp file "' . $sTemptFile . '"', ELogLevel::Error);
                 }
             }
         }
     }
     return $oMessage;
 }
Exemple #5
0
 /**
  * @param int $iUserId
  * @param mixed $mContactId
  * @param string $sAddressBookName
  * @return CContact | false
  */
 public function GetContactById($iUserId, $mContactId, $sAddressBookName = \afterlogic\DAV\Constants::ADDRESSBOOK_DEFAULT_NAME)
 {
     $oContact = false;
     if ($this->Init($iUserId)) {
         $oAddressBook = $this->getAddressBook($iUserId, $sAddressBookName);
         $oContactItem = $this->geItem($iUserId, $oAddressBook, $mContactId);
         if ($oContactItem) {
             $sVCardData = $oContactItem->get();
             if ($sVCardData) {
                 $oContact = new CContact();
                 $oContact->InitFromVCardStr($iUserId, $sVCardData);
                 $oContact->IdContact = $mContactId;
                 $oContact->ETag = trim($oContactItem->getETag(), '"');
             }
         }
     }
     return $oContact;
 }
Exemple #6
0
 function afterWriteContent($path, \Sabre\DAV\IFile $node)
 {
     if ('sabredav' !== \CApi::GetManager()->GetStorageByType('contacts')) {
         if ($node instanceof \Sabre\CardDAV\ICard) {
             $oAccount = $this->server->getAccount();
             if (isset($oAccount)) {
                 $iUserId = $oAccount->IdUser;
                 $iTenantId = $node instanceof \afterlogic\DAV\CardDAV\SharedCard ? $oAccount->IdTenant : null;
                 $sFileName = $node->getName();
                 $oContactDb = $this->oApiContactsManager->GetContactByStrId($iUserId, $sFileName, $iTenantId);
                 $oContact = new \CContact();
                 $oContact->InitFromVCardStr($iUserId, $node->get());
                 $oContact->IdContact = $oContactDb->IdContact;
                 $oContact->SharedToAll = !!$oContactDb->SharedToAll;
                 $bResult = $this->oApiContactsManager->UpdateContact($oContact);
                 //					\CApi::LogObject($bResult, \ELogLevel::Full, 'contacts-');
             }
         }
     }
 }
Exemple #7
0
 function afterWriteContent($path, \Sabre\DAV\IFile $node)
 {
     if ('sabredav' !== \CApi::GetManager()->GetStorageByType('contacts')) {
         if ($node instanceof \Sabre\CardDAV\ICard) {
             $oAccount = $this->server->getAccount();
             if (isset($oAccount)) {
                 $iUserId = $oAccount->IdUser;
                 $iTenantId = $node instanceof \afterlogic\DAV\CardDAV\SharedCard ? $oAccount->IdTenant : null;
                 $sContactFileName = $node->getName();
                 $oContactDb = $this->oApiContactsManager->GetContactByStrId($iUserId, $sContactFileName, $iTenantId);
                 if (!isset($oContactDb)) {
                     $oDavManager = \CApi::Manager('dav');
                     $oVCard = $oDavManager ? $oDavManager->VObjectReaderRead($node->get()) : null;
                     if ($oVCard && $oVCard->UID) {
                         $oContactDb = $this->oApiContactsManager->GetContactByStrId($iUserId, (string) $oVCard->UID . '.vcf', $iTenantId);
                     }
                 }
                 $oContact = new \CContact();
                 $oContact->InitFromVCardStr($iUserId, $node->get());
                 $oContact->IdContactStr = $sContactFileName;
                 $oContact->IdTenant = $iTenantId;
                 if (isset($oContactDb)) {
                     $oContact->IdContact = $oContactDb->IdContact;
                     $oContact->IdDomain = $oContactDb->IdDomain;
                     $oContact->SharedToAll = !!$oContactDb->SharedToAll;
                     $this->oApiContactsManager->UpdateContact($oContact);
                 } else {
                     $this->oApiContactsManager->CreateContact($oContact);
                 }
             }
         }
     }
 }