Example #1
0
 /**
  * The code that needs to be called when the cron is running
  * 
  * If $this->enableUserAndGroupSupport() returns TRUE then the run function 
  * will be called for each $user. (The $user parameter will be given)
  * 
  * If $this->enableUserAndGroupSupport() returns FALSE then the 
  * $user parameter is null and the run function will be called only once.
  * 
  * @param CronJob $cronJob
  * @param User $user [OPTIONAL]
  */
 public function run(CronJob $cronJob, User $user = null)
 {
     $stmt = \GO\Dropbox\Model\User::model()->find(FindParams::newInstance()->select());
     $sync = new Model\Sync();
     foreach ($stmt as $dbxUser) {
         try {
             if (!empty($dbxUser->access_token)) {
                 $sync->start($dbxUser->user);
             } else {
                 $sync::log("Skipped user " . $dbxUser->user->username . " because there's no access token");
             }
         } catch (DropboxException $e) {
             $sync::log($e->getMessage());
             $this->_sendMessage($dbxUser, $e, GO::t('syncFailedBody', 'dropbox'));
             //Remove user that does not work anymore so he can reconnect
             //$dbxUser->delete();
         } catch (\Exception $e) {
             $sync::log($e->getMessage());
             //				$this->_sendMessage($dbxUser, $e,"");
         }
     }
 }
Example #2
0
 public function start(\GO\Base\Model\User $user = null, $reset = false)
 {
     if ($user === null) {
         $user = GO::user();
     }
     GO::session()->runAsRoot();
     GO::setMaxExecutionTime(0);
     self::log("Dropbox sync for user " . $user->username);
     $dbxUser = User::model()->findByPk($user->id);
     if ($reset) {
         $dbxUser->delta_cursor = "";
         $folder = self::getHomeFolder($user);
         $folder->removeChildren();
     }
     $dbxClient = self::getClient($user);
     self::log("Getting Dropbox Changes");
     $hasMore = true;
     while ($hasMore) {
         $delta = $dbxClient->Delta(empty($dbxUser->delta_cursor) ? null : $dbxUser->delta_cursor);
         //			var_dump($delta);
         //			exit();
         if (!isset($delta)) {
             throw new \Exception("Could not get delta from Dropbox!");
         }
         $hasMore = $delta->has_more;
         foreach ($delta->entries as $entry) {
             flush();
             //$entry[1]['path'] = with case. Otherwise we just have a string to lowered path for deleting
             $dbxPath = isset($entry[1]->path) ? $entry[1]->path : $entry[0];
             $goPath = self::dbxToGoPath($dbxPath, $user);
             if (!isset($entry[1])) {
                 //should be deleted
                 $file = File::model()->findByPath($goPath, false);
                 if ($file) {
                     self::log("Deleting file on Group-Office " . $goPath);
                     $file->delete();
                 } else {
                     $folder = Folder::model()->findByPath($goPath, false, array(), false);
                     if ($folder) {
                         self::log("Deleting folder on Group-Office " . $goPath);
                         $folder->delete();
                     } else {
                         self::log("Could not find path for delete file on Group-Office " . $goPath);
                     }
                 }
             } else {
                 if ($entry[1]->is_dir) {
                     self::log("Create folder on Group-Office " . $entry[1]->path . " -> " . $goPath);
                     $folder = Folder::model()->findByPath($goPath, true);
                 } else {
                     self::log("Download from Dropbox " . $entry[1]->path . " -> " . $goPath);
                     $folder = Folder::model()->findByPath(dirname($goPath), true, array(), false);
                     $name = \GO\Base\Fs\File::utf8Basename($goPath);
                     $path = $folder->fsFolder->createChild($name)->path();
                     //$f = fopen($path, "w+b");
                     $fileMetadata = $dbxClient->DownloadFile($entry[0], $path);
                     //fclose($f);
                     touch($path, strtotime($fileMetadata->modified));
                     //todo needs optimize
                     $folder->syncFilesystem();
                 }
             }
         }
     }
     $dbxUser->delta_cursor = $delta->cursor;
     $dbxUser->save();
     self::log("Applying Group-Office changes to Dropbox");
     $folder = self::getHomeFolder($user);
     $goSnapshot = self::getGroupOfficeSnapShot($folder);
     $dbxSnapshot = self::getDropboxSnapshot($user);
     foreach ($goSnapshot as $path => $props) {
         $dbxPath = self::goToDbxPath($props['path'], $user);
         $dbxPathToLower = strtolower($dbxPath);
         if (!isset($dbxSnapshot[$dbxPathToLower]) || $dbxSnapshot[$dbxPathToLower]['mtime'] < $props['mtime']) {
             if (is_file(GO::config()->file_storage_path . $props['path'])) {
                 self::log("Upload to Dropbox " . $path . " -> " . $dbxPath);
                 $localPath = GO::config()->file_storage_path . $props['path'];
                 $meta = $dbxClient->UploadFile($localPath, $dbxPath, true);
                 if (!isset($meta)) {
                     throw new \Exception("Failed to create file '" . $dbxPath . "' on Dropbox");
                 }
             } elseif (!isset($dbxSnapshot[$dbxPathToLower])) {
                 self::log("Create folder on Dropbox " . $path . " -> " . $dbxPath);
                 $folderMetaData = $dbxClient->CreateFolder($dbxPath);
                 if (!isset($folderMetaData)) {
                     throw new \Exception("Failed to create folder '" . $dbxPath . "' on Dropbox");
                 }
             }
         }
     }
     //reverse sort for deleting so that deeper files are deleted first.
     krsort($dbxSnapshot);
     foreach ($dbxSnapshot as $path => $props) {
         $goPath = strtolower(self::dbxToGoPath($path, $user));
         if (!isset($goSnapshot[$goPath])) {
             self::log("Deleting on dropbox " . $path);
             if (!$dbxClient->Delete($path)) {
                 throw new \Exception("Failed to delete '" . $path . "'");
             }
         }
     }
     //get delta again so we won't process our own changes next sync
     $delta = $dbxClient->Delta($dbxUser->delta_cursor);
     $dbxUser->delta_cursor = $delta->cursor;
     $dbxUser->save();
     self::log("Done!");
 }
Example #3
0
 protected function actionDisconnect()
 {
     $dbUser = User::model()->findByPk(GO::user()->id);
     $response['success'] = $dbUser ? $dbUser->delete() : true;
     return $response;
 }