/** * @todo vérification du ratio (suffisant ou non pour autoriser le téléchargement) * @todo support de lourds fichiers * @author Alban Truc * @param string|MongoId $idUser * @param string|MongoId $idElement * @since 15/06/2014 * @return array */ function userDownload($idUser, $idElement) { $idUser = new MongoId($idUser); $idElement = new MongoId($idElement); $elementPdoManager = new ElementPdoManager(); $elementCriteria = array('state' => (int) 1, '_id' => $idElement); $element = $elementPdoManager->findOne($elementCriteria); if (!$element instanceof Element) { return $element; } //récupération de la vitesse de téléchargement de l'utilisateur $accountPdoManager = new AccountPdoManager(); $accountCriteria = array('state' => 1, 'idUser' => $idUser); $account = $accountPdoManager->findOne($accountCriteria); if (!$account instanceof Account) { return $account; } $refPlanPdoManager = new RefPlanPdoManager(); $refPlan = $refPlanPdoManager->findById($account->getRefPlan()); if (!$refPlan instanceof RefPlan) { return $refPlan; } $downloadSpeed = $refPlan->getDownloadSpeed(); //return $downloadSpeed; //récupère le code et l'extension de notre élément $refElementPdoManager = new RefElementPdoManager(); $fieldsToReturn = array('code' => TRUE, 'extension' => TRUE); $refElement = $refElementPdoManager->findById($element->getRefElement(), $fieldsToReturn); if (!array_key_exists('error', $refElement)) { if (preg_match('/^4/', $refElement['code']) || preg_match('/^9/', $refElement['code'])) { // dossier ou non reconnu, pas d'extension à rajouter return array('error' => 'Donwload not available on folder or unrecognized element'); } } else { return $refElement; } // 01 correspond au droit de lecture. $hasRight = actionAllowed($idElement, $idUser, array('01')); if (is_bool($hasRight) && $hasRight == FALSE) { return array('error' => 'You are not allowed to download this file.'); } elseif (is_array($hasRight)) { return $hasRight; } $filePath = PATH . $idUser . $element->getServerPath(); $fileName = $element->getName() . $refElement['extension']; $fullFilePath = $filePath . $fileName; $fileSize = round($element->getSize() * 1024); set_time_limit(0); if ($fd = fopen($fullFilePath, 'r')) { header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=\"{$fileName}\""); header("Content-Transfer-Encoding: binary"); header("Content-length: {$fileSize}"); $fileExtension = pathinfo($fullFilePath, PATHINFO_EXTENSION); //déterminer le Content-Type $ctype = getContentType($fileExtension); //nécessite http://pecl.php.net/package/pecl_http /* http_send_content_disposition($fileName); http_send_content_type($ctype); http_throttle(0.1, $downloadSpeed * 1024); http_send_file($fullFilePath); */ header("Content-Type: {$ctype}"); $file = @fopen($fullFilePath, 'rb'); if ($file) { while (!feof($file)) { print fread($file, 1024 * $downloadSpeed); flush(); usleep(500); if (connection_status() != 0) { @fclose($file); die; } } @fclose($file); } } }
/** * Crée un nouveau dossier vierge à l'emplacement voulu. * @author Alban Truc * @param string|MongoId $idUser * @param string $path * @param string $folderName * @param bool $inheritRightsFromParent * @since 09/06/2014 * @return array|bool|Element|Element[]|TRUE -- à vérifier */ function createNewFolder($idUser, $path, $folderName, $inheritRightsFromParent) { $idUser = new MongoId($idUser); $operationSuccess = FALSE; $elementManager = new ElementManager(); if ($path != '/') { //récupération du dossier parent $explode = explode('/', $path); $parentDirectoryName = $explode[sizeof($explode) - 2]; $parentDirectoryPath = array_slice($explode, 0, sizeof($explode) - 2); $parentElementCriteria = array('state' => (int) 1, 'name' => $parentDirectoryName, 'serverPath' => $parentDirectoryPath, 'idOwner' => $idUser); $parentElement = $elementManager->findOne($parentElementCriteria); if (!array_key_exists('error', $parentElement)) { /* * 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($parentElement['_id'], $idUser, array('11')); if (is_bool($hasRight) && $hasRight == FALSE) { return array('error' => 'Creation not allowed.'); } elseif (is_array($hasRight)) { return $hasRight; } } else { return $parentElement; } } //vérification qu'il n'existe pas déjà un dossier avec le même nom $elementCriteria = array('state' => (int) 1, 'name' => $folderName, 'serverPath' => $path, 'idOwner' => $idUser); $elementsWithSameName = $elementManager->find($elementCriteria); if (is_array($elementsWithSameName) && array_key_exists('error', $elementsWithSameName)) { if ($elementsWithSameName['error'] != 'No match found.') { return $elementsWithSameName; } } else { foreach ($elementsWithSameName as $key => $elementWithSameName) { $isFolder = isFolder($elementWithSameName['idRefElement']); if (is_bool($isFolder)) { if ($isFolder == FALSE) { unset($elementsWithSameName[$key]); } } else { return $isFolder; } } if (!empty($elementsWithSameName)) { return array('error' => 'Folder name not available.'); } } //File Server - 13/06/2014 $mkdirResult = createFSDirectory($idUser, $path, $folderName); if (!is_bool($mkdirResult) || !($mkdirResult == TRUE)) { return $mkdirResult; } //Fin File Server //Récupération de l'id de RefElement dossier vide $refElementManager = new RefElementManager(); $emptyFolder = $refElementManager->findOne(array('state' => 1, 'code' => '4002'), array('_id' => TRUE)); $newFolder = array('state' => 1, 'name' => $folderName, 'idOwner' => $idUser, 'idRefElement' => $emptyFolder['_id'], 'serverPath' => $path); $insertResult = $elementManager->create($newFolder); if (is_bool($insertResult)) { if ($insertResult == TRUE) { //Le dossier parent était vide if (isset($parentElement)) { if ($parentElement['idRefElement'] == $emptyFolder['_id']) { $parentElementCriteria = array('_id' => $parentElement->getId()); //on change l'id du dossier parent pour dossier non vide $notEmptyFolder = $refElementManager->findOne(array('state' => 1, 'code' => '4003'), array('_id' => TRUE)); $update = array('$set' => array('idRefElement' => $notEmptyFolder['_id'])); //dans le cas où on voudrait récupérer le dossier parent mis à jour, on peut utiliser $updatedFolder $updatedFolder = $elementManager->findAndModify($parentElementCriteria, $update, array('new' => TRUE)); if (!array_key_exists('error', $updatedFolder)) { $operationSuccess = TRUE; } } if ($inheritRightsFromParent == 'TRUE') { //récupération des droits appliqués sur le dossier parent $rightManager = new RightManager(); $rightCriteria = array('state' => 1, 'idElement' => $parentElement['_id']); $rights = $rightManager->find($rightCriteria); if (!array_key_exists('error', $rights)) { //récupération du dossier précédemment inséré $newElement = $elementManager->findOne($newFolder); if (!array_key_exists('error', $newElement)) { $insertRightCopy = array(); foreach ($rights as $right) { $rightCopy = $right; $rightCopy['_id'] = new MongoId(); $rightCopy['idElement'] = $newElement['_id']; $insertRightCopy[] = $elementManager->create($rightCopy); //on pourrait se servir de $insertRightCopy pour identifier les erreurs éventuelles } //@todo vérifier que tous les insertRightCopy sont OK et si c'est le cas operationSuccess = TRUE $operationSuccess = TRUE; } else { return $newElement; } } } } $operationSuccess = TRUE; return $operationSuccess; } else { return array('error' => 'Could not create folder in database.'); } } else { return $insertResult; } }
<?php /** * LinkMgr * ------- * $Id: index.php,v 1.6 2003/09/09 16:38:24 richardn Exp $ * Usage: main program * Author: Richard Neish <*****@*****.**> * Borrowed from W-AGORA code by Stefan Schreyjak <*****@*****.**> */ require 'include/functions.php'; require 'globals.inc'; # Setup the session ini_set('session.use_cookies', '0'); ini_set('session.use_trans_sid', '1'); session_start(); refreshLinks($bookmarkFile); # set script to be invoked in the main frame/page if (!isset($_REQUEST['action'])) { $action = "list"; } else { $action = $_REQUEST['action']; } if (actionAllowed($action)) { include "{$action}.{$ext}"; } else { include "accessDenied.php"; }