/**
  * @param string $sEmail
  * @param string $sUrl
  * @param string $sUser
  * @param string $sPassword
  * @param string $sProxy = ''
  *
  * @return bool
  */
 public function Sync($sEmail, $sUrl, $sUser, $sPassword, $sProxy = '')
 {
     $this->SyncDatabase();
     $iUserID = $this->getUserId($sEmail);
     if (0 >= $iUserID) {
         return false;
     }
     $aUrl = \parse_url($sUrl);
     if (!\is_array($aUrl)) {
         $aUrl = array();
     }
     $aUrl['scheme'] = isset($aUrl['scheme']) ? $aUrl['scheme'] : 'http';
     $aUrl['host'] = isset($aUrl['host']) ? $aUrl['host'] : 'localhost';
     $aUrl['port'] = isset($aUrl['port']) ? $aUrl['port'] : 80;
     $aUrl['path'] = isset($aUrl['path']) ? \rtrim($aUrl['path'], '\\/') . '/' : '/';
     $aSettings = array('baseUri' => $aUrl['scheme'] . '://' . $aUrl['host'] . ('80' === (string) $aUrl['port'] ? '' : ':' . $aUrl['port']), 'userName' => $sUser, 'password' => $sPassword);
     $this->oLogger->AddSecret($sPassword);
     if (!empty($sProxy)) {
         $aSettings['proxy'] = $sProxy;
     }
     $sPath = $aUrl['path'];
     if (!\class_exists('Sabre\\DAV\\Client')) {
         return false;
     }
     $oClient = new \Sabre\DAV\Client($aSettings);
     $oClient->setVerifyPeer(false);
     $this->oLogger->Write('User: '******'userName'] . ', Url: ' . $sUrl, \MailSo\Log\Enumerations\Type::INFO, 'DAV');
     $aRemoteSyncData = $this->prepearRemoteSyncData($oClient, $sPath);
     if (false === $aRemoteSyncData) {
         return false;
     }
     $aDatabaseSyncData = $this->prepearDatabaseSyncData($iUserID);
     //		$this->oLogger->WriteDump($aDatabaseSyncData);
     //		$this->oLogger->WriteDump($aRemoteSyncData);
     //+++del (from carddav)
     foreach ($aDatabaseSyncData as $sKey => $aData) {
         if ($aData['deleted'] && isset($aRemoteSyncData[$sKey], $aRemoteSyncData[$sKey]['vcf'])) {
             $this->davClientRequest($oClient, 'DELETE', $sPath . $aRemoteSyncData[$sKey]['vcf']);
         }
     }
     //---del
     //+++del (from db)
     $aIdsForDeletedion = array();
     foreach ($aDatabaseSyncData as $sKey => $aData) {
         if (!$aData['deleted'] && !empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) {
             $aIdsForDeletedion[] = $aData['id_contact'];
         }
     }
     if (0 < \count($aIdsForDeletedion)) {
         $this->DeleteContacts($sEmail, $aIdsForDeletedion, false);
     }
     //---del
     $this->flushDeletedContacts($iUserID);
     //+++new or newer (from db)
     foreach ($aDatabaseSyncData as $sKey => $aData) {
         if (!$aData['deleted'] && (empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) || !empty($aData['etag']) && isset($aRemoteSyncData[$sKey]) && $aRemoteSyncData[$sKey]['etag'] !== $aData['etag'] && $aRemoteSyncData[$sKey]['changed'] < $aData['changed']) {
             $mID = $aData['id_contact'];
             $oContact = $this->GetContactByID($sEmail, $mID, false);
             if ($oContact) {
                 $sExsistensBody = '';
                 $mExsistenRemoteID = isset($aRemoteSyncData[$sKey]['vcf']) && !empty($aData['etag']) ? $aRemoteSyncData[$sKey]['vcf'] : '';
                 if (0 < \strlen($mExsistenRemoteID)) {
                     $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $mExsistenRemoteID);
                     if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) {
                         $sExsistensBody = \trim($oResponse['body']);
                     }
                 }
                 $oResponse = $this->davClientRequest($oClient, 'PUT', $sPath . $oContact->CardDavNameUri(), $oContact->ToVCard($sExsistensBody));
                 if ($oResponse && isset($oResponse['headers'], $oResponse['headers']['etag'])) {
                     $sEtag = \trim(\trim($oResponse['headers']['etag']), '"\'');
                     $sDate = !empty($oResponse['headers']['date']) ? \trim($oResponse['headers']['date']) : '';
                     if (!empty($sEtag)) {
                         $iChanged = empty($sDate) ? \time() : \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDate);
                         $this->updateContactEtagAndTime($iUserID, $mID, $sEtag, $iChanged);
                     }
                 }
             }
             unset($oContact);
         }
     }
     //---new
     //+++new or newer (from carddav)
     foreach ($aRemoteSyncData as $sKey => $aData) {
         if (!isset($aDatabaseSyncData[$sKey]) || $aDatabaseSyncData[$sKey]['etag'] !== $aData['etag'] && $aDatabaseSyncData[$sKey]['changed'] < $aData['changed']) {
             $mExsistenContactID = isset($aDatabaseSyncData[$sKey]['id_contact']) ? $aDatabaseSyncData[$sKey]['id_contact'] : '';
             $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $aData['vcf']);
             if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) {
                 $sBody = \trim($oResponse['body']);
                 if (!empty($sBody)) {
                     $oContact = null;
                     if ($mExsistenContactID) {
                         $oContact = $this->GetContactByID($sEmail, $mExsistenContactID);
                     }
                     if (!$oContact) {
                         $oContact = new \RainLoop\Providers\AddressBook\Classes\Contact();
                     }
                     $oContact->PopulateByVCard($sBody, !empty($oResponse['headers']['etag']) ? \trim(\trim($oResponse['headers']['etag']), '"\'') : '');
                     $this->ContactSave($sEmail, $oContact);
                     unset($oContact);
                 }
             }
         }
     }
     return true;
 }
Exemple #2
0
 public function initWithResource($resource)
 {
     $config = $resource->config;
     $this->importFly();
     $adapterClass = '\\League\\Flysystem\\Adapter\\' . $resource->adapter;
     if (property_exists($config, 'pathPrefix')) {
         $this->pathPrefix = $config->pathPrefix;
     }
     if ($resource->adapter == 'Ftp' || $resource->adapter == 'Sftp') {
         $adapterInstance = new $adapterClass((array) $config);
     } else {
         if ($resource->adapter == 'WebDav') {
             //require_once realpath(dirname(__FILE__)) .'/vendor/sabre/dav/lib/Sabre/autoload.php';
             require_once realpath(dirname(__FILE__)) . '/vendor/autoload.php';
             $client = new Sabre\DAV\Client((array) $config);
             $client->setVerifyPeer(false);
             //$client->
             /*CURLOPT_SSL_VERIFYPEER => 0,
             CURLOPT_SSL_VERIFYHOST => 0,*/
             //xapp_dump($client);
             $adapterInstance = new $adapterClass($client);
         } else {
             if ($resource->adapter == 'Dropbox') {
                 require_once realpath(dirname(__FILE__)) . '/vendor/dropbox/dropbox-sdk/lib/Dropbox/autoload.php';
                 $client = new \Dropbox\Client($config->token, $config->appname);
                 $adapterInstance = new $adapterClass($client);
             } else {
                 if ($resource->adapter == 'GoogleDrive') {
                     //$client_id = '914720938366-1b9t1n0d87g7r429j37kh29474n301la.apps.googleusercontent.com';
                     //$client_secret = '-FqmFBTynCy6VBIYwDLeIvPm';
                     $client_id = '34179804656-v7ckk0id5h1dcrpoj07c2343vbf2qto2.apps.googleusercontent.com';
                     $client_secret = 'ZnWLJFaJh_c1AO8wk_d4Y7fk';
                     //$auth_token = '{"access_token":"ya29.IQD9DFJHv7U1kBgAAAAJSdBBwvJq8lmEj8f9RBsXbK5uX82vXlvlQAbn_pL2Rg","token_type":"Bearer","expires_in":3600,"refresh_token":"1\/_RAFGmxs0bQBSCCI3hJYnntuGiyXq28UGCdsW8E1cb4","created":1401030654}';
                     $auth_token = null;
                     // This URL should be the landing page after authorising the site access to your Google Drive.
                     // It should store your $auth_token or display it for manual entry.
                     //$auth_token = '{"access_token":"4/rBfx8PY5GOGkLPdeHnJLieoQBur_.gq0ftA4x5ZATOl05ti8ZT3YPlhuxjAI","token_type":"Bearer","expires_in":3600,"refresh_token":"1\/_RAFGmxs0bQBSCCI3hJYnntuGiyXq28UGCdsW8E1cb4","created":1401030654}';
                     $auth_token = '{"access_token":"ya29.KAACmqCTqVx9lBoAAABKWlyiPU-ZJiN-MOWl57hPFu1-uNjvC2vC5cQgO1IzJA","token_type":"Bearer","expires_in":3600,"created":1401645493}';
                     //$redirect_url = 'http://localhost/flysystem/GoogleDriveSetup.php';
                     $redirect_url = 'http://mc007ibi.dyndns.org:81/x4mm/Code/trunk/xide-php/xapp/xcf/index.php?debug=true';
                     //$redirect_url = 'http://www.mc007ibi.dyndns.org:81/xapp-commander-standalone/docroot/index.php?option=com_xappcommander&service=XCOM_Directory_Service.get&path=./Determining Food Preferences of Hagfish.docx&callback=asdf&mount=google&raw=html&attachment=true&user=e741198e1842408aa660459240d430a6&sig=025d0a5326e1bb4608b7624c99c3cff49db7664f';
                     //http://www.mc007ibi.dyndns.org:81/xapp-commander-standalone/docroot/index.php?option=com_xappcommander&service=XCOM_Directory_Service.get&path=./Determining Food Preferences of Hagfish.docx&callback=asdf&mount=google&raw=html&attachment=true&user=e741198e1842408aa660459240d430a6&sig=025d0a5326e1bb4608b7624c99c3cff49db7664f
                     //http://mc007ibi.dyndns.org:81/x4mm/Code/trunk/xide-php/xapp/xcf/index.php?debug=true&code=4/rBfx8PY5GOGkLPdeHnJLieoQBur_.gq0ftA4x5ZATOl05ti8ZT3YPlhuxjAI
                     //http://192.168.1.37:81/xapp-commander-standalone/docroot/index.php?option=com_xappcommander&service=XCOM_Directory_Service.get&path=./Cleanup.doc&callback=asdf&mount=/google&raw=html&attachment=true&user=e741198e1842408aa660459240d430a6&sig=5faf21f72a91fa70890c75b7f7f579b3fd5e03a8
                     //error_log('########0');
                     $client = new Google_Client();
                     if ($auth_token) {
                         $client->setAccessToken($auth_token);
                     }
                     $client->setClientId($client_id);
                     $client->setClientSecret($client_secret);
                     $client->setRedirectUri($redirect_url);
                     $token = $client->getAccessToken();
                     $code = null;
                     //$code = '4/lnZn4gks2d2pO1ddiO488ZxPKX_o.8uYEey1YDAkROl05ti8ZT3bHlSmxjAI';
                     //{"access_token":"ya29.KAACmqCTqVx9lBoAAABKWlyiPU-ZJiN-MOWl57hPFu1-uNjvC2vC5cQgO1IzJA","token_type":"Bearer","expires_in":3600,"created":1401645493}
                     //error_log('######## : ' . $token);//
                     //www.mc007ibi.dyndns.org:81/xapp-commander-standalone/docroot/index.php?option=com_xappcommander&service=XCOM_Directory_Service.get&path=./Determining Food Preferences of Hagfish.docx&callback=asdf&mount=google&raw=html&attachment=true&user=e741198e1842408aa660459240d430a6&sig=025d0a5326e1bb4608b7624c99c3cff49db7664f
                     if ($auth_token) {
                         error_log('have auth');
                         $refreshToken = json_decode($token);
                         /*xapp_dump($refreshToken);*/
                         /*
                         $refreshToken = $refreshToken->refresh_token;
                         if($client->getAuth()->isAccessTokenExpired()) {
                         			        $client->getAuth()->refreshToken($refreshToken);
                         }
                         */
                     } else {
                         if ($code) {
                             error_log('########1 : have code ');
                             $client->authenticate($code);
                             echo "Your access token for Google Drive is:<br /><br />\n\n";
                             echo $client->getAccessToken();
                             echo "\n\n<br /><br />This is your \$auth_token value. Set it in the configuration file.";
                             exit;
                         } else {
                             error_log('######## getting auth url : begin');
                             $client->setScopes(array('https://www.googleapis.com/auth/drive'));
                             $authUrl = $client->createAuthUrl();
                             error_log('######## getting auth url : ' . $authUrl);
                             /*echo($authUrl);*/
                             die("You must first authorise the plugin. Make sure your client ID and secret are set then <a href='{$authUrl}'>click here</a> to do so.");
                         }
                     }
                     $adapterInstance = new $adapterClass($client);
                 }
             }
         }
     }
     $fs = "League\\Flysystem\\Filesystem";
     // Add them in the constructor
     $manager = new League\Flysystem\MountManager(array($resource->name => new $fs($adapterInstance)));
     $this->mountManager = $manager;
     $this->adapter = $adapterInstance;
     //xapp_dump($resource);
     //$fs = $this->getFilesystem($resource->{'name'});
     // Or mount them later
     //$manager->mountFilesystem('local', $local);
     //xapp_dump($manager);
     //$filesystem = new Filesystem($adapterInstance);
     //$ls = $fs->listWith(array('mimetype', 'size', 'timestamp'),'httpdocs');
     //$ls = $fs->listWith(null,'httpdocs');
     //xapp_dump($fs->listContents('/httpdocs'));
 }