Esempio n. 1
0
 public function storageadmin()
 {
     if (!$this->permissions->isSuper()) {
         $this->e404();
     }
     $this->allowMethods(array('GET', 'POST'));
     Zotero_DB::beginTransaction();
     if ($this->method == 'POST') {
         if (!isset($_POST['quota'])) {
             $this->e400("Quota not provided");
         }
         // Accept 'unlimited' via API
         if ($_POST['quota'] == 'unlimited') {
             $_POST['quota'] = self::UNLIMITED;
         }
         if (!isset($_POST['expiration'])) {
             $this->e400("Expiration not provided");
         }
         if (!is_numeric($_POST['quota']) || $_POST['quota'] < 0) {
             $this->e400("Invalid quota");
         }
         if (!is_numeric($_POST['expiration'])) {
             $this->e400("Invalid expiration");
         }
         $halfHourAgo = strtotime("-30 minutes");
         if ($_POST['expiration'] != 0 && $_POST['expiration'] < $halfHourAgo) {
             $this->e400("Expiration is in the past");
         }
         try {
             Zotero_Storage::setUserValues($this->objectUserID, $_POST['quota'], $_POST['expiration']);
         } catch (Exception $e) {
             if ($e->getCode() == Z_ERROR_GROUP_QUOTA_SET_BELOW_USAGE) {
                 $this->e409("Cannot set quota below current usage");
             }
             $this->handleException($e);
         }
     }
     // GET request
     $xml = new SimpleXMLElement('<storage/>');
     $quota = Zotero_Storage::getEffectiveUserQuota($this->objectUserID);
     $xml->quota = $quota;
     $instQuota = Zotero_Storage::getInstitutionalUserQuota($this->objectUserID);
     // If personal quota is in effect
     if (!$instQuota || $quota > $instQuota) {
         $values = Zotero_Storage::getUserValues($this->objectUserID);
         if ($values) {
             $xml->expiration = (int) $values['expiration'];
         }
     }
     // Return 'unlimited' via API
     if ($quota == self::UNLIMITED) {
         $xml->quota = 'unlimited';
     }
     $usage = Zotero_Storage::getUserUsage($this->objectUserID);
     $xml->usage->total = $usage['total'];
     $xml->usage->library = $usage['library'];
     foreach ($usage['groups'] as $group) {
         if (!isset($group['id'])) {
             throw new Exception("Group id isn't set");
         }
         if (!isset($group['usage'])) {
             throw new Exception("Group usage isn't set");
         }
         $xmlGroup = $xml->usage->addChild('group', $group['usage']);
         $xmlGroup['id'] = $group['id'];
     }
     Zotero_DB::commit();
     header('application/xml');
     echo $xml->asXML();
     exit;
 }