/** * Authentifier un utilisateur: * - Récupère l'utilisateur inscrit avec l'e-mail indiquée. S'il y en a un: * - Vérifie le mot de passe. S'il correspond: * - Récupère son compte * @author Alban Truc * @param string $email * @param string $password * @since 02/2014 * @return User|array contenant le message d'erreur */ public function authenticate($email, $password) { //Récupère l'utilisateur inscrit avec l'e-mail indiquée. $query = array('state' => (int) 1, 'email' => $email); $user = self::findOne($query); if ($user instanceof User) { $password = self::encrypt($password); if ($user->getPassword() == $password) { //On récupère le compte correspondant à l'utilisateur $accountCriteria = array('_id' => new MongoId($user->getCurrentAccount()), 'state' => (int) 1); $account = $this->accountPdoManager->findOne($accountCriteria); var_dump($account); if ($account instanceof Account) { $refPlan = $this->refPlanPdoManager->findById($account->getRefPlan()); if ($refPlan instanceof RefPlan) { $account->setRefPlan($refPlan); $user->setCurrentAccount($account); return $user; } else { $errorInfo = 'RefPlan with ID ' . $account->getRefPlan() . ' not found'; return array('error' => $errorInfo); } } else { $errorInfo = 'No active account with ID ' . $user->getCurrentAccount() . ' for user ' . $user->getId(); return array('error' => $errorInfo); } } else { $errorInfo = 'Password given (' . $password . ') does not match with password in database.'; return array('error' => $errorInfo); } } else { $errorInfo = 'No ACTIVE user found for the following e-mail: ' . $email . ' Maybe you didn\'t activate your account?'; return array('error' => $errorInfo); } }
/** * Recharger une session avec les nouvelles données en bdd */ function refreshUserSession() { //Initialise nos objets $userPdoManager = new UserPdoManager(); $accountPdoManager = new AccountPdoManager(); $refPlanPdoManager = new RefPlanPdoManager(); //Récupère l'utilisateur inscrit avec l'id indiquée. $id = array('state' => (int) 1, '_id' => unserialize($_SESSION['user'])->getId()); $user = $userPdoManager->findOne($id); if ($user instanceof User) { //On récupère le compte correspondant à l'utilisateur $accountCriteria = array('_id' => new MongoId($user->getCurrentAccount()), 'state' => (int) 1); $account = $accountPdoManager->findOne($accountCriteria); if ($account instanceof Account) { $refPlan = $refPlanPdoManager->findById($account->getRefPlan()); if ($refPlan instanceof RefPlan) { $account->setRefPlan($refPlan); $user->setCurrentAccount($account); $u = $_SESSION['user'] = serialize($user); //met les infos user en session return $u; } else { $errorInfo = 'RefPlan with ID ' . $account->getRefPlan() . ' not found'; return array('error' => $errorInfo); } } else { $errorInfo = 'No active account with ID ' . $user->getCurrentAccount() . ' for user ' . $user->getId(); return array('error' => $errorInfo); } } else { $errorInfo = 'No ACTIVE user found for the following e-mail: ' . $id . ' Maybe you didn\'t activate your account?'; return array('error' => $errorInfo); } }
/** * @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); } } }
*/ $projectRoot = $_SERVER['DOCUMENT_ROOT'] . '/Cubbyhole'; require $projectRoot . '/required.php'; $accountPdoManager = new AccountPdoManager(); echo 'Utilisation du find<br />'; echo '____Retourne tous les champs sauf le champ state'; $accountFind = $accountPdoManager->find(array('state' => 1), array('state' => 0)); var_dump($accountFind); echo '____Retourne en objet'; $accountFind = $accountPdoManager->find(array('state' => 1)); var_dump($accountFind); echo '----------------------------------------<br />'; echo 'Utilisation du findOne'; $array = array('_id' => new MongoId('52eb602d3263d8b6a4395df3'), 'state' => 1, 'idUser' => null, 'idRefPlan' => new MongoId('52eb5e783263d8b6a4395df1'), 'storage' => 2, 'ratio' => 1, 'startDate' => '01-31-2014', 'endDate' => 'none'); $manualAccount = new Account($array); $accountFindOne = $accountPdoManager->findOne($manualAccount, array('_id')); var_dump($accountFindOne); echo '____equivalent du findById'; $accountFindOne = $accountPdoManager->findOne(array('_id' => $accountFind[0]->getId())); var_dump($accountFindOne); echo '----------------------------------------<br />'; echo 'Utilisation du findById avec un MongoId en parametre'; $accountFoundById = $accountPdoManager->findById($accountFind[0]->getId()); var_dump($accountFoundById); echo 'Utilisation du findById avec une string en parametre'; $accountFoundById = $accountPdoManager->findById((string) $accountFind[0]->getId()); var_dump($accountFoundById); echo '----------------------------------------<br />'; echo 'Recuperer tous les comptes'; $allAccounts = $accountPdoManager->findAll(); var_dump($allAccounts);