} //soumission du formulaire de download if (isset($_POST['downloadElem']) && isset($_POST['idElement'])) { userDownload($userId, $_POST['idElement']); } // soumission du formulaire de download anonyme if (isset($_POST['downloadAnonymousElem']) && isset($_POST['idElement'])) { userDownload($_POST['owner'], $_POST['idElement']); } // soumission du formulaire d'upload basique if (isset($_POST['uploadBasicElem']) && isset($_FILES['fileExplorer'])) { $path = 'C:/wamp/www/Nestbox/' . $userId . '/Tmp-' . $userId . ''; $folder = $path . '/'; $file = $_FILES['fileExplorer']['name']; move_uploaded_file($_FILES['fileExplorer']['tmp_name'], $folder . $file); $returnMoveFS = moveFSElement($userId, '/Tmp-' . $userId . '/', $_FILES['fileExplorer']['name'], $_POST['destination'], $_FILES['fileExplorer']['name']); if ($returnMoveFS == TRUE) { $newPath = $projectRoot . '/' . $userId . $_POST['destination']; $elementManager = new ElementPdoManager(); $refElementManager = new RefElementPdoManager(); $hash = sha1_file($newPath . $_FILES['fileExplorer']['name']); $size = fileSize64($newPath . $_FILES['fileExplorer']['name']); $pathInfo = pathinfo($newPath . $_FILES['fileExplorer']['name']); $refElement = $refElementManager->findOne(array('extension' => '.' . $pathInfo['extension'])); if (is_array($refElement) && array_key_exists('error', $refElement)) { echo "Extension not found"; } else { $idRefElement = $refElement->getId(); $criteria = array('downloadLink' => '', 'idOwner' => $userId, 'idRefElement' => $idRefElement, 'name' => $pathInfo['filename'], 'state' => 1, 'hash' => $hash, 'serverPath' => $_POST['destination'], 'size' => $size); $createElement = $elementManager->create($criteria); updateFolderStatus($_POST['destination'], $userId);
/** * Déplace l'élément (et ce qu'il contient dans le cas d'un dossier) dans la destination indiquée. * $options est un tableau de booléens avec comme indexes possibles: * - returnImpactedElements à true pour retourner les éléments à déplacer * - returnMovedElements à true pour retourner les éléments déplacés * - keepRights à false pour ne pas conserver les droits sur les éléments * - keepDownloadLinks à false pour ne pas conserver les liens de téléchargement * On peut se retrouver avec la structure de retour suivante: * array( * 'operationSuccess' => true ou false, * 'error' => 'message d'erreur', * 'impactedElements' => array(), * 'movedElements' => array(), * 'failedToMove' => array() * ) * @author Alban Truc * @param string|MongoId $idElement * @param string|MongoId $idUser * @param string $path * @param array $options * @since 08/06/2014 * @return array * @todo mêmes améliorations que pour la fonction copyHandler */ function moveHandler($idElement, $idUser, $path, $options = array()) { $idElement = new MongoId($idElement); $idUser = new MongoId($idUser); $impactedElements = array(); $movedElements = array(); $failedToMove = array(); $operationSuccess = FALSE; /* * 11 correspond au droit de lecture et écriture. * Si on souhaite accepter la copie avec des droits de plus bas niveau, il suffit d'ajouter les codes correspondant * au tableau en 3e paramètre ci-dessous. */ $hasRight = actionAllowed($idElement, $idUser, array('11')); if (!is_array($hasRight)) { if ($hasRight === TRUE) { //récupère l'élément $elementManager = new ElementManager(); $element = $elementManager->findById($idElement); if (!array_key_exists('error', $element)) { if ($element['state'] == 1) { if ($path != $element['serverPath']) { $elementCriteria = array('state' => (int) 1, 'idOwner' => $idUser); /* * extraction de l'emplacement du dossier de destination à partir de $path * @see http://www.php.net/manual/en/function.implode.php * @see http://www.php.net/manual/en/function.explode.php */ $destinationFolderPath = implode('/', explode('/', $path, -2)) . '/'; $elementCriteria['serverPath'] = $destinationFolderPath; /** * la racine n'ayant pas d'enregistrement pour elle même, on a un serverPath "/" mais de nom. * il faut donc distinguer les cas de copies d'un élément dans la racine des autres cas. */ if ($path != "/") { /* * extraction du nom du dossier de destination à partir du $path * @see http://www.php.net/manual/en/function.array-slice.php */ $destinationFolderName = implode(array_slice(explode('/', $path), -2, 1)); $elementCriteria['name'] = $destinationFolderName; //récupération de l'id de l'élément en base correspondant au dossier de destination $idDestinationFolder = $elementManager->findOne($elementCriteria, array('_id' => TRUE)); if (array_key_exists('error', $idDestinationFolder)) { return prepareMoveReturn($options, $operationSuccess, $idDestinationFolder, $impactedElements, $movedElements, $failedToMove); } else { //vérification des droits dans la destination $hasRightOnDestination = actionAllowed($idDestinationFolder['_id'], $idUser, array('11')); if (is_array($hasRightOnDestination) && array_key_exists('error', $hasRightOnDestination)) { return prepareMoveReturn($options, $operationSuccess, $hasRightOnDestination, $impactedElements, $movedElements, $failedToMove); } elseif ($hasRightOnDestination == FALSE) { return prepareMoveReturn($options, $operationSuccess, array('error' => 'Access denied in destination'), $impactedElements, $movedElements, $failedToMove); } } } } $elementNameInDestination = avoidNameCollision($path, $element['name'], $idUser); if (is_string($elementNameInDestination)) { //File Server 14/06/2014 $refElementManager = new RefElementManager(); $refElementFieldsToReturn = array('code' => TRUE, 'extension' => TRUE); $refElement = $refElementManager->findById($element['idRefElement'], $refElementFieldsToReturn); if (array_key_exists('error', $refElement)) { return $refElement; } //dossier ou non reconnu, pas d'extension à rajouter if (preg_match('/^4/', $refElement['code']) || preg_match('/^9/', $refElement['code'])) { $completeSourceName = $element['name']; $completeDestinationName = $elementNameInDestination; } else { $completeSourceName = $element['name'] . $refElement['extension']; $completeDestinationName = $elementNameInDestination . $refElement['extension']; } $FSMoveResult = moveFSElement($idUser, $element['serverPath'], $completeSourceName, $path, $completeDestinationName); if (!is_bool($FSMoveResult) || $FSMoveResult != TRUE) { return $FSMoveResult; } //Fin File Server if ($refElement['code'] != '4002' && preg_match('/^4/', $refElement['code'])) { $serverPath = $element['serverPath'] . $element['name'] . '/'; //récupération des éléments contenus dans le dossier $seekElementsInFolder = array('state' => (int) 1, 'serverPath' => new MongoRegex("/^{$serverPath}/i"), 'idOwner' => $idUser); $elementsInFolder = $elementManager->find($seekElementsInFolder); } if (isset($elementsInFolder) && !array_key_exists('error', $elementsInFolder)) { $impactedElements = $elementsInFolder; } $impactedElements[] = $element; $count = 0; foreach ($impactedElements as $key => $impactedElement) { $updateCriteria = array('_id' => $impactedElement['_id'], 'state' => (int) 1); //préparation de la copie $elementCopy = $impactedElement; if (count($impactedElements) != $key + 1) { $explode = explode($serverPath, $elementCopy['serverPath']); if (isset($explode[1]) && $explode[1] != '') { $elementPath = $path . $elementNameInDestination . '/' . $explode[1]; $elementCopy['serverPath'] = $elementPath; } else { $elementCopy['serverPath'] = $path . $elementNameInDestination . '/'; } } else { $elementCopy['name'] = $elementNameInDestination; $elementCopy['serverPath'] = $path; } if (array_key_exists('keepDownloadLinks', $options) && $options['keepDownloadLinks'] == 'FALSE') { $elementCopy['downloadLink'] = ''; } //mise à jour $updateResult = $elementManager->update($updateCriteria, $elementCopy); //gestion des erreurs if (!is_bool($updateResult)) { $failedToPaste[$count]['elementToMove'] = $impactedElement; $failedToPaste[$count]['elementMoved'] = $elementCopy; $failedToPaste[$count]['error'] = $updateResult['error']; $count++; } elseif ($updateResult == TRUE) { $movedElements[] = $elementCopy; } } /* * Si le déplacement vide un dossier ou rempli un dossier qui était vide, * on met à jour son refElement */ updateFolderStatus($path, $idUser); updateFolderStatus($element['serverPath'], $idUser); if (array_key_exists('keepRights', $options) && $options['keepRights'] == 'FALSE') { disableRights($impactedElements); } $operationSuccess = TRUE; return prepareMoveReturn($options, $operationSuccess, array(), $impactedElements, $movedElements, $failedToMove); } else { return prepareMoveReturn($options, $operationSuccess, $elementNameInDestination, $impactedElements, $movedElements, $failedToMove); } } else { return prepareMoveReturn($options, $operationSuccess, array('error' => 'Element inactivated, nothing to do'), $impactedElements, $movedElements, $failedToMove); } } else { return prepareMoveReturn($options, $operationSuccess, $element, $impactedElements, $movedElements, $failedToMove); } } else { return prepareMoveReturn($options, $operationSuccess, array('error' => 'Access denied'), $impactedElements, $movedElements, $failedToMove); } } else { return prepareMoveReturn($options, $operationSuccess, $hasRight, $impactedElements, $movedElements, $failedToMove); } }
<?php $projectRoot = $_SERVER['DOCUMENT_ROOT'] . '/Nestbox'; require_once $projectRoot . '/required.php'; /** * Created by PhpStorm. * User: Harry * Date: 12/06/14 * Time: 19:42 */ $path = 'C:/wamp/www/Nestbox/' . $userId . '/Tmp-' . $userId . ''; /* Si l'utilisateur décide d'uploader un element */ if (isset($_POST['destination']) && isset($_SESSION['file'])) { $returnMoveFS = moveFSElement($userId, '/Tmp-' . $userId . '/', $_SESSION['file']['name'], $_POST['destination'], $_SESSION['file']['name']); if ($returnMoveFS == TRUE) { $newPath = $projectRoot . '/' . $userId . $_POST['destination']; $elementManager = new ElementPdoManager(); $refElementManager = new RefElementPdoManager(); $hash = sha1_file($newPath . $_SESSION['file']['name']); $size = fileSize64($newPath . $_SESSION['file']['name']); $pathInfo = pathinfo($newPath . $_SESSION['file']['name']); $refElement = $refElementManager->findOne(array('extension' => '.' . $pathInfo['extension'])); if (is_array($refElement) && array_key_exists('error', $refElement)) { echo "Extension not found"; } else { $idRefElement = $refElement->getId(); $criteria = array('downloadLink' => '', 'idOwner' => $userId, 'idRefElement' => $idRefElement, 'name' => $pathInfo['filename'], 'state' => 1, 'hash' => $hash, 'serverPath' => $_POST['destination'], 'size' => $size); $createElement = $elementManager->create($criteria); updateFolderStatus($_POST['destination'], $userId); echo "Your element has been successfully uploaded."; }