1<?php 2 3namespace RainLoop\Providers\Suggestions; 4 5class OwnCloudSuggestions implements \RainLoop\Providers\Suggestions\ISuggestions 6{ 7 /** 8 * @var \MailSo\Log\Logger 9 */ 10 protected $oLogger; 11 12 /** 13 * @param \RainLoop\Model\Account $oAccount 14 * @param string $sQuery 15 * @param int $iLimit = 20 16 * 17 * @return array 18 */ 19 public function Process($oAccount, $sQuery, $iLimit = 20) 20 { 21 $iInputLimit = $iLimit; 22 $aResult = array(); 23 $sQuery = \trim($sQuery); 24 25 try 26 { 27 if ('' === $sQuery || !$oAccount || !\RainLoop\Utils::IsOwnCloudLoggedIn()) 28 { 29 return $aResult; 30 } 31 32 $aParams = array('FN', 'NICKNAME', 'TITLE', 'EMAIL'); 33 if (\class_exists('OC') && isset(\OC::$server) && \method_exists(\OC::$server, 'getContactsManager')) 34 { 35 $cm = \OC::$server->getContactsManager(); 36 if (!$cm && !$cm->isEnabled()) 37 { 38 return $aResult; 39 } 40 41 $aSearchResult = $cm->search($sQuery, $aParams); 42 } 43 else if (\class_exists('OCP\Contacts') && \OCP\Contacts::isEnabled()) 44 { 45 $aSearchResult = \OCP\Contacts::search($sQuery, $aParams); 46 } 47 else 48 { 49 return $aResult; 50 } 51 52 //$this->oLogger->WriteDump($aSearchResult); 53 54 $aHashes = array(); 55 if (\is_array($aSearchResult) && 0 < \count($aSearchResult)) 56 { 57 foreach ($aSearchResult as $aContact) 58 { 59 if (0 >= $iLimit) 60 { 61 break; 62 } 63 64 $sUid = empty($aContact['UID']) ? '' : $aContact['UID']; 65 if (!empty($sUid)) 66 { 67 $mEmails = isset($aContact['EMAIL']) ? $aContact['EMAIL'] : ''; 68 69 $sFullName = isset($aContact['FN']) ? \trim($aContact['FN']) : ''; 70 if (empty($sFullName)) 71 { 72 $sFullName = isset($aContact['NICKNAME']) ? \trim($aContact['NICKNAME']) : ''; 73 } 74 75 if (!\is_array($mEmails)) 76 { 77 $mEmails = array($mEmails); 78 } 79 80 foreach ($mEmails as $sEmail) 81 { 82 $sHash = '"'.$sFullName.'" <'.$sEmail.'>'; 83 if (!isset($aHashes[$sHash])) 84 { 85 $aHashes[$sHash] = true; 86 $aResult[] = array($sEmail, $sFullName); 87 $iLimit--; 88 } 89 } 90 } 91 } 92 93 $aResult = \array_slice($aResult, 0, $iInputLimit); 94 } 95 96 unset($aSearchResult, $aHashes); 97 } 98 catch (\Exception $oException) 99 { 100 if ($this->oLogger) 101 { 102 $this->oLogger->WriteException($oException); 103 } 104 } 105 106 return $aResult; 107 } 108 109 /** 110 * @param \MailSo\Log\Logger $oLogger 111 */ 112 public function SetLogger($oLogger) 113 { 114 $this->oLogger = $oLogger instanceof \MailSo\Log\Logger ? $oLogger : null; 115 } 116} 117