Exemple #1
0
 public static function copyFile($params)
 {
     $apiManager = new ApiManager();
     $user = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser();
     $cloudspace = false;
     $cloudOrig = isset($params['cloud']['origin']) && strlen($params['cloud']['origin']) > 0 ? $params['cloud']['origin'] : null;
     $cloudDestination = isset($params['cloud']['destination']) && strlen($params['cloud']['destination']) > 0 ? $params['cloud']['destination'] : null;
     $pathCloud = "home://~" . $user->getName() . "/Cloudspaces/";
     $pathOrig = null;
     if ($cloudOrig) {
         if ($pathCloud . $cloudOrig == $params['orig']) {
             $pathOrig = "/";
         } else {
             $pathOrig = substr($params['orig'], strlen($pathCloud . $cloudOrig)) . "/";
         }
     }
     if ($cloudDestination) {
         $cloudspace = true;
     }
     $file = null;
     $isFolder = true;
     $tmpFile = null;
     $filename = null;
     $pathinfo = null;
     $pathAbsolute = null;
     if (!$params['file']['is_folder']) {
         $isFolder = false;
         $tmpFile = new LocalFile('/var/tmp/' . date('Y_m_d_H_i_s') . '_' . $user->getId());
         $pathAbsolute = AdvancedPathLib::getPhpLocalHackPath($tmpFile->getAbsolutePath());
         if (array_key_exists('id', $params['file'])) {
             $token = $_SESSION['access_token_' . $cloudOrig . '_v2'];
             $resourceUrl = null;
             if (isset($params['file']['resource_url'])) {
                 $token = new stdClass();
                 $token->key = $params['access_token_key'];
                 $token->secret = $params['access_token_secret'];
                 $resourceUrl = $params['resource_url'];
             }
             $metadata = $apiManager->downloadMetadata($token, $params['file']['id'], $pathAbsolute, $user->getId(), true, $cloudOrig, $resourceUrl);
             if ($metadata['status'] == 'KO') {
                 if ($metadata['error'] == 403) {
                     $denied = self::permissionDeniedCloud($cloudOrig);
                     $metadata['path'] = $denied['path'];
                 }
                 return $metadata;
             } else {
                 if (isset($metadata['local'])) {
                     $file = FSI::getFile($params['file']['pathEyeos']);
                     $tmpFile->putContents($file->getContents());
                 }
             }
         } else {
             $file = FSI::getFile($params['file']['path']);
             $tmpFile->putContents($file->getContents());
         }
     }
     if ($pathOrig) {
         if ($params['file']['path'] == $pathOrig) {
             $pathinfo = pathinfo($params['file']['filename']);
         }
     } else {
         if ($params['file']['parent'] == null) {
             $pathinfo = pathinfo($params['file']['filename']);
         }
     }
     if ($pathinfo) {
         $nameForCheck = $pathinfo['filename'];
         $extension = null;
         if (isset($pathinfo['extension'])) {
             $extension = $pathinfo['extension'];
             $nameForCheck .= '.' . $extension;
         }
         $number = 1;
         $newFile = FSI::getFile($params['dest'] . "/" . $nameForCheck);
         while ($newFile->exists()) {
             $futureName = array($pathinfo['filename'], $number);
             $nameForCheck = implode(' ', $futureName);
             if ($extension) {
                 $nameForCheck .= '.' . $extension;
             }
             $number++;
             $newFile = FSI::getFile($params['dest'] . "/" . $nameForCheck);
             $params['filenameChange'] = $nameForCheck;
             if (!array_key_exists('parent', $params['file'])) {
                 $params['pathChange'] = substr($params['orig'], strlen($pathCloud . $cloudOrig));
             }
         }
         $filename = $newFile->getName();
     } else {
         $filename = $params['file']['filename'];
     }
     if ($cloudspace) {
         $pathParent = substr($params['dest'], strlen($pathCloud . $cloudDestination));
         if (array_key_exists('parent', $params['file'])) {
             if (strlen($pathParent) == 0 && !$params['file']['parent']) {
                 $pathParent = '/';
             }
             if ($params['file']['parent']) {
                 $pathParent .= $params['file']['parent'];
             }
         } else {
             $pathParent .= '/' . substr($params['file']['path'], strlen($pathOrig));
             if (strlen($pathParent) > 1) {
                 $pathParent = substr($pathParent, 0, -1);
             }
         }
         $folder = NULL;
         if ($pathParent !== '/') {
             $pos = strrpos($pathParent, '/');
             $folder = substr($pathParent, $pos + 1);
             $pathParent = substr($pathParent, 0, $pos + 1);
         }
         $parentId = false;
         if ($folder) {
             $path = $pathParent . $folder . '/';
             $lista = new stdClass();
             $lista->cloud = $cloudDestination;
             $lista->path = $pathParent;
             $lista->filename = $folder;
             $lista->user_eyeos = $user->getId();
             $u1db = json_decode($apiManager->callProcessU1db('parent', $lista));
             if ($u1db !== NULL && count($u1db) > 0) {
                 $parentId = $u1db[0]->id;
                 if ($parentId === 'null') {
                     $parentId = '0';
                 }
             }
         } else {
             $parentId = '0';
             $path = $pathParent;
         }
         if ($parentId !== false) {
             $token = $_SESSION['access_token_' . $cloudDestination . '_v2'];
             $resourceUrl = null;
             if (isset($params['resource_url'])) {
                 $token = new stdClass();
                 $token->key = $params['access_token_key'];
                 $token->secret = $params['access_token_secret'];
                 $resourceUrl = $params['resource_url'];
             }
             $metadata = $apiManager->createMetadata($cloudDestination, $token, $user->getId(), !$isFolder, $filename, $parentId, $path, $pathAbsolute, $resourceUrl);
             if ($metadata['status'] == 'KO') {
                 if ($metadata['error'] == 403) {
                     $denied = self::permissionDeniedCloud($cloudDestination);
                     $metadata['path'] = $denied['path'];
                 }
                 return $metadata;
             }
         }
     }
     $pathDest = null;
     if (array_key_exists('parent', $params['file'])) {
         if ($params['file']['parent']) {
             $pathDest = $params['dest'] . $params['file']['parent'] . '/';
         } else {
             $pathDest = $params['dest'] . '/';
         }
     } else {
         if ($pathOrig == $params['file']['path']) {
             $pathDest = $params['dest'] . '/';
         } else {
             $pathDest = $params['dest'] . '/' . substr($params['file']['path'], strlen($pathOrig));
         }
     }
     $pathDest .= $filename;
     $newFile = FSI::getFile($pathDest);
     if ($isFolder) {
         $newFile->mkdir();
     } else {
         $tmpFile->copyTo($newFile);
     }
     if ($tmpFile) {
         $tmpFile->delete();
     }
     return $params;
 }
 public function fileWritten(FileEvent $e)
 {
     //Logger::getLogger('sebas')->error('MetadataWritten:' . $e->getSource()->getPath());
     $apiManager = new ApiManager();
     $path = $e->getSource()->getPath();
     $user = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser();
     $userName = $user->getName();
     $cloud = $this->isCloud($path, $userName);
     $resourceUrl = null;
     if ($cloud->isCloud) {
         $pathU1db = substr($path, strlen($cloud->path));
         $lenfinal = strrpos($pathU1db, $e->getSource()->getName());
         $posfinal = $lenfinal > 1 ? $lenfinal - strlen($pathU1db) - 1 : $lenfinal - strlen($pathU1db);
         $pathParent = substr($pathU1db, 0, $posfinal);
         $folder = NULL;
         if ($pathParent !== '/') {
             $pos = strrpos($pathParent, '/');
             $folder = substr($pathParent, $pos + 1);
             $pathParent = substr($pathParent, 0, $pos + 1);
         }
         $parentId = false;
         if ($folder !== NULL) {
             $path = $pathParent . $folder . '/';
             $lista = new stdClass();
             $lista->cloud = $cloud->name;
             $lista->path = $pathParent;
             $lista->filename = $folder;
             $lista->user_eyeos = $user->getId();
             $u1db = json_decode($apiManager->callProcessU1db('parent', $lista));
             if ($u1db !== NULL && count($u1db) > 0) {
                 $parentId = $u1db[0]->id;
                 if (isset($u1db[0]->resource_url)) {
                     $resourceUrl = new stdClass();
                     $resourceUrl->resource_url = $u1db[0]->resource_url;
                     $resourceUrl->token = new stdClass();
                     $resourceUrl->token->key = $u1db[0]->access_token_key;
                     $resourceUrl->token->secret = $u1db[0]->access_token_secret;
                     if ($parentId === 'null') {
                         $parentId = 0;
                     }
                 }
             }
         } else {
             $parentId = '0';
             $path = $pathParent;
         }
         if ($parentId !== false) {
             $pathAbsolute = AdvancedPathLib::getPhpLocalHackPath($e->getSource()->getRealFile()->getAbsolutePath());
             $token = $_SESSION['access_token_' . $cloud->name . '_v2'];
             if ($resourceUrl) {
                 $token = $resourceUrl->token;
                 $resourceUrl = $resourceUrl->resource_url;
             }
             $result = $apiManager->createMetadata($cloud->name, $token, $user->getId(), true, $e->getSource()->getName(), $parentId, $path, $pathAbsolute, $resourceUrl);
             if ($result['status'] == 'OK') {
                 $params = array($e->getSource()->getParentPath(), $e->getSource()->getPath());
                 $message = new ClientBusMessage('file', 'refreshStackSync', $params);
                 ClientMessageBusController::getInstance()->queueMessage($message);
             } else {
                 if ($result['error'] == 403) {
                     $path = $this->cleanCloud($cloud->name, $user);
                     $params = array($path, $cloud->name);
                     $message = new ClientBusMessage('file', 'permissionDenied', $params);
                     ClientMessageBusController::getInstance()->queueMessage($message);
                 }
             }
         }
     }
 }