Пример #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);
    }
}
Пример #2
0
function shareWithAnonymous($idElement, $idOwner, $recipientEmail = '')
{
    $idElement = new MongoId($idElement);
    $idOwner = new MongoId($idOwner);
    $elementPdoManager = new ElementPdoManager();
    $elementCriteria = array('state' => (int) 1, '_id' => $idElement);
    $element = $elementPdoManager->findOne($elementCriteria);
    if ($element->getDownloadLink() == '') {
        /*
         * vérification que l'idOwner en param de la fonction est le même que celui de l'element, la gestion des partages
         * n'étant dans cette version qu'accessible au propriétaire de l'élément
         */
        if ($idOwner == $element->getOwner()) {
            //vérification que l'email indiquée appartient bien à un utilisateur inscrit
            $userCriteria = array('state' => (int) 1, 'email' => $recipientEmail);
            $userPdoManager = new UserPdoManager();
            $recipientUser = $userPdoManager->findOne($userCriteria);
            /*
             * Tentative de génération de lien de téléchargement anonyme pour un utilsateur existant.
             * L'interdire ici ne résoudra cependant que partiellement cet éventuel problème,
             * mais au moins on limite la permissivité.
             */
            if ($recipientUser instanceof User) {
                return array('error' => 'The email you entered belongs to one of our users, please use the \'share with a user\' functionality.');
            }
            $downloadLink = $elementPdoManager->generateGUID();
            $updateDownloadLink = array('$set' => array('downloadLink' => $downloadLink));
            $updateStatus = $elementPdoManager->update($elementCriteria, $updateDownloadLink);
            if (is_bool($updateStatus) && $updateStatus == TRUE) {
                return array('downloadLink' => $downloadLink);
            } else {
                return $updateStatus;
            }
        } else {
            return array('error' => 'You are not the owner of this element, you cannot share it.');
        }
    } else {
        return array('error', 'There is already a download link for this element.');
    }
}