Example #1
0
/**
 * 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
            $elementPdoManager = new ElementPdoManager();
            $element = $elementPdoManager->findById($idElement);
            if ($element instanceof Element) {
                if ($element->getState() == 1) {
                    if ($path != $element->getServerPath()) {
                        $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 = $elementPdoManager->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->getName(), $idUser);
                    if (is_string($elementNameInDestination)) {
                        //File Server 14/06/2014
                        $refElementPdoManager = new RefElementPdoManager();
                        $refElementFieldsToReturn = array('code' => TRUE, 'extension' => TRUE);
                        $refElement = $refElementPdoManager->findById($element->getRefElement(), $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->getName();
                            $completeDestinationName = $elementNameInDestination;
                        } else {
                            $completeSourceName = $element->getName() . $refElement['extension'];
                            $completeDestinationName = $elementNameInDestination . $refElement['extension'];
                        }
                        $FSMoveResult = moveFSElement($idUser, $element->getServerPath(), $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->getServerPath() . $element->getName() . '/';
                            //récupération des éléments contenus dans le dossier
                            $seekElementsInFolder = array('state' => (int) 1, 'serverPath' => new MongoRegex("/^{$serverPath}/i"), 'idOwner' => $idUser);
                            $elementsInFolder = $elementPdoManager->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->getId(), 'state' => (int) 1);
                            //préparation de la copie
                            $elementCopy = clone $impactedElement;
                            if (count($impactedElements) != $key + 1) {
                                $explode = explode($serverPath, $elementCopy->getServerPath());
                                if (isset($explode[1]) && $explode[1] != '') {
                                    $elementPath = $path . $elementNameInDestination . '/' . $explode[1];
                                    $elementCopy->setServerPath($elementPath);
                                } else {
                                    $elementCopy->setServerPath($path . $elementNameInDestination . '/');
                                }
                            } else {
                                $elementCopy->setName($elementNameInDestination);
                                $elementCopy->setServerPath($path);
                            }
                            if (array_key_exists('keepDownloadLinks', $options) && $options['keepDownloadLinks'] == FALSE) {
                                $elementCopy->setDownloadLink('');
                            }
                            //mise à jour
                            $updateResult = $elementPdoManager->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->getServerPath(), $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);
    }
}
Example #2
0
 $elementManager = new ElementPdoManager();
 $refElementManager = new RefElementPdoManager();
 $userManager = new UserPdoManager();
 $refElementEmptyDirectory = $refElementManager->findOne(array('code' => '4002', 'state' => 1));
 if ($refElementEmptyDirectory instanceof RefElement) {
     $idRefElementEmptyDirectory = $refElementEmptyDirectory->getId();
 } else {
     return $refElementEmptyDirectory;
 }
 $refElementNotEmptyDirectory = $refElementManager->findOne(array('code' => '4003', 'state' => 1));
 if ($refElementNotEmptyDirectory instanceof RefElement) {
     $idRefElementNotEmptyDirectory = $refElementNotEmptyDirectory->getId();
 } else {
     return $refElementNotEmptyDirectory;
 }
 $element = $elementManager->findById($_GET['id']);
 $refElement = $refElementManager->findById($element->getRefElement());
 $user = $userManager->findById($element->getOwner());
 echo '<div id="elementInformations">
         <label name="validationMove">Are you sure you want to move this element ?</label>
             <ul>
                 <li>Element name : ' . $element->getName() . '</li>
                 <li>Current directory : ' . $element->getServerPath() . '</li>
                 <li>Type : ' . $refElement->getDescription() . '</li>
                 <li>Size : ' . $element->getSize() . ' KB</li>
                 <li>Owner : ' . $user->getFirstName() . ' ' . $user->getLastName() . '</li>
             </ul>
       </div>';
 function cmp($a, $b)
 {
     return strcmp($a, $b);
Example #3
0
/**
 * Permet de désactiver les droits d'un élément pour un user, gestion récursive pour les dossiers.
 * @author Harry Bellod
 * @param $idElement | id de l'élément qu'on veut désactiver
 * @param $idUser | id de l'utilisateur concerné
 * @param $idOwner | id du propriétaire de l'élément
 * @since 14/06/2014
 * @return bool | array contenant un message d'erreur
 */
function disableShareRights($idElement, $idUser, $idOwner)
{
    $elementManager = new ElementPdoManager();
    $refElementManager = new RefElementPdoManager();
    $rightPdoManager = new RightPdoManager();
    $element = $elementManager->findById($idElement);
    $refElement = $refElementManager->findById($element->getRefElement());
    $idRefElement = $refElement->getId();
    /** @var  $isFolder => bool, true si l'élément est bien un dossier, sinon false */
    $isFolder = isFolder($idRefElement);
    if (is_bool($isFolder) && $isFolder == TRUE) {
        $serverPath = $element->getServerPath() . $element->getName() . '/';
        //récupération des éléments contenus dans le dossier
        $seekElementsInFolder = array('state' => (int) 1, 'serverPath' => new MongoRegex("/^{$serverPath}/i"), 'idOwner' => new MongoId($idOwner));
        //liste des éléments contenus dans le dossier
        $elementsInFolder = $elementManager->find($seekElementsInFolder);
        foreach ($elementsInFolder as $subElement) {
            $rightCriteria = array('state' => (int) 1, 'idElement' => new MongoId($subElement->getId()), 'idUser' => new MongoId($idUser));
            $rightUpdate = array('$set' => array('state' => (int) 0));
            //pour chaque élément on désactive le droit qui lui était affecté
            $disableElementsInFolder = $rightPdoManager->update($rightCriteria, $rightUpdate);
            if (is_bool($disableElementsInFolder) && $disableElementsInFolder != TRUE) {
                return array('error' => 'No match found.');
            }
        }
    }
    $rightCriteria = array('state' => (int) 1, 'idElement' => new MongoId($idElement), 'idUser' => new MongoId($idUser));
    $rightUpdate = array('$set' => array('state' => (int) 0));
    //désactivation de l'élément parent
    $disableParent = $rightPdoManager->update($rightCriteria, $rightUpdate);
    if (is_bool($disableParent) && $disableParent != TRUE) {
        return array('error' => 'No match found.');
    }
}