Example #1
0
 /**
  * Validate if the expiration date fits the system settings
  *
  * @param \OCP\Share\IShare $share The share to validate the expiration date of
  * @return \OCP\Share\IShare The expiration date or null if $expireDate was null and it is not required
  * @throws GenericShareException
  * @throws \InvalidArgumentException
  * @throws \Exception
  */
 protected function validateExpirationDate(\OCP\Share\IShare $share)
 {
     $expirationDate = $share->getExpirationDate();
     if ($expirationDate !== null) {
         //Make sure the expiration date is a date
         $expirationDate->setTime(0, 0, 0);
         $date = new \DateTime();
         $date->setTime(0, 0, 0);
         if ($date >= $expirationDate) {
             $message = $this->l->t('Expiration date is in the past');
             throw new GenericShareException($message, $message, 404);
         }
     }
     // If we enforce the expiration date check that is does not exceed
     if ($this->shareApiLinkDefaultExpireDateEnforced()) {
         if ($expirationDate === null) {
             throw new \InvalidArgumentException('Expiration date is enforced');
         }
         $date = new \DateTime();
         $date->setTime(0, 0, 0);
         $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
         if ($date < $expirationDate) {
             $message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
             throw new GenericShareException($message, $message, 404);
         }
     }
     // If expiredate is empty set a default one if there is a default
     if ($expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
         $expirationDate = new \DateTime();
         $expirationDate->setTime(0, 0, 0);
         $expirationDate->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
     }
     $accepted = true;
     $message = '';
     \OCP\Util::emitHook('\\OC\\Share', 'verifyExpirationDate', ['expirationDate' => &$expirationDate, 'accepted' => &$accepted, 'message' => &$message, 'passwordSet' => $share->getPassword() === null]);
     if (!$accepted) {
         throw new \Exception($message);
     }
     $share->setExpirationDate($expirationDate);
     return $expirationDate;
 }