Example #1
0
 /**
  * Move all bitstreams from one asset store to another.
  *
  * @param AssetstoreDao $srcAssetstore The source asset store
  * @param AssetstoreDao $dstAssetstore The destination asset store
  * @param null|ProgressDao $progressDao Progress dao for asynchronous updating
  * @throws Zend_Exception
  */
 public function moveBitstreams($srcAssetstore, $dstAssetstore, $progressDao = null)
 {
     $current = 0;
     /** @var ProgressModel $progressModel */
     $progressModel = MidasLoader::loadModel('Progress');
     /** @var BitstreamModel $bitstreamModel */
     $bitstreamModel = MidasLoader::loadModel('Bitstream');
     $sql = $this->database->select()->setIntegrityCheck(false)->from('bitstream')->where('assetstore_id = ?', $srcAssetstore->getKey());
     $rows = $this->database->fetchAll($sql);
     $srcPath = $srcAssetstore->getPath();
     $dstPath = $dstAssetstore->getPath();
     foreach ($rows as $row) {
         $bitstream = $this->initDao('Bitstream', $row);
         if ($progressDao) {
             ++$current;
             $message = $current . ' / ' . $progressDao->getMaximum() . ': Moving ' . $bitstream->getName() . ' (' . UtilityComponent::formatSize($bitstream->getSizebytes()) . ')';
             $progressModel->updateProgress($progressDao, $current, $message);
         }
         // Move the file on disk to its new location
         $dir1 = substr($bitstream->getChecksum(), 0, 2);
         $dir2 = substr($bitstream->getChecksum(), 2, 2);
         if (!is_dir($dstPath . '/' . $dir1)) {
             if (!mkdir($dstPath . '/' . $dir1)) {
                 throw new Zend_Exception('Failed to mkdir ' . $dstPath . '/' . $dir1);
             }
         }
         if (!is_dir($dstPath . '/' . $dir1 . '/' . $dir2)) {
             if (!mkdir($dstPath . '/' . $dir1 . '/' . $dir2)) {
                 throw new Zend_Exception('Failed to mkdir ' . $dstPath . '/' . $dir1 . '/' . $dir2);
             }
         }
         if (is_file($dstPath . '/' . $bitstream->getPath())) {
             if (is_file($srcPath . '/' . $bitstream->getPath())) {
                 unlink($srcPath . '/' . $bitstream->getPath());
             }
         } else {
             if (!rename($srcPath . '/' . $bitstream->getPath(), $dstPath . '/' . $bitstream->getPath())) {
                 throw new Zend_Exception('Error moving ' . $bitstream->getPath());
             }
         }
         // Update the asset store id on the bitstream record once it has been moved
         $bitstream->setAssetstoreId($dstAssetstore->getKey());
         $bitstreamModel->save($bitstream);
     }
 }
 /**
  * Ajax action for determining what action to take based on the size of the requested download.
  *
  * @throws Zend_Exception
  */
 public function checksizeAction()
 {
     $this->disableView();
     $this->disableLayout();
     $itemIds = $this->getParam('itemIds');
     $folderIds = $this->getParam('folderIds');
     if (isset($itemIds)) {
         $itemIdArray = explode(',', $itemIds);
     } else {
         $itemIdArray = array();
     }
     if (isset($folderIds)) {
         $folderIdArray = explode(',', $folderIds);
     } else {
         $folderIdArray = array();
     }
     $items = array();
     $folders = array();
     foreach ($itemIdArray as $itemId) {
         if ($itemId) {
             $item = $this->Item->load($itemId);
             if (!$item || !$this->Item->policyCheck($item, $this->userSession->Dao, MIDAS_POLICY_READ)) {
                 throw new Zend_Exception('Permission denied on item ' . $itemId, 403);
             }
             $items[] = $item;
         }
     }
     foreach ($folderIdArray as $folderId) {
         if ($folderId) {
             $folder = $this->Folder->load($folderId);
             if (!$folder || !$this->Folder->policyCheck($folder, $this->userSession->Dao, MIDAS_POLICY_READ)) {
                 throw new Zend_Exception('Permission denied on folder ' . $folderId, 403);
             }
             $folders[] = $folder;
         }
     }
     $totalSize = 0;
     $folders = $this->Folder->getSizeFiltered($folders, $this->userSession->Dao, MIDAS_POLICY_READ);
     foreach ($folders as $folder) {
         $totalSize += $folder->size;
     }
     foreach ($items as $item) {
         $totalSize += $item->getSizebytes();
     }
     if ($totalSize > 2000000000.0 && in_array('javauploaddownload', Zend_Registry::get('modulesEnable'))) {
         echo JsonComponent::encode(array('action' => 'promptApplet', 'sizeStr' => UtilityComponent::formatSize($totalSize)));
     } else {
         echo JsonComponent::encode(array('action' => 'download'));
     }
 }
Example #3
0
 /**
  * Prompt the user to confirm deletion of a folder.
  *
  * @param folderId The id of the folder to be deleted
  */
 public function deletedialogAction()
 {
     $this->disableLayout();
     $folderId = $this->getParam('folderId');
     if (!isset($folderId)) {
         throw new Zend_Exception('Must pass folderId parameter');
     }
     $folder = $this->Folder->load($folderId);
     if ($folder === false) {
         throw new Zend_Exception('Invalid folderId', 404);
     } elseif (!$this->Folder->policyCheck($folder, $this->userSession->Dao, MIDAS_POLICY_ADMIN)) {
         throw new Zend_Exception('Admin permission required', 403);
     }
     $this->view->folder = $folder;
     $sizes = $this->Folder->getSizeFiltered(array($folder), $this->userSession->Dao);
     $this->view->sizeStr = UtilityComponent::formatSize($sizes[0]->size);
 }
Example #4
0
 /**
  * Ajax action for getting the latest package of each package type for the given os and arch.
  *
  * @param os The os to match on
  * @param arch The arch to match on
  * @param applicationId The application id
  * @return (json) - The latest uploaded package of each installer type for the given os, arch, and application
  */
 public function latestAction()
 {
     $this->disableLayout();
     $this->disableView();
     $os = $this->getParam('os');
     $arch = $this->getParam('arch');
     $applicationId = $this->getParam('applicationId');
     if (!isset($applicationId)) {
         throw new Zend_Exception('Must specify an applicationId parameter');
     }
     $application = $this->Packages_Application->load($applicationId);
     if (!$application) {
         throw new Zend_Exception('Invalid applicationId', 404);
     }
     $comm = $application->getProject()->getCommunity();
     if (!$this->Community->policyCheck($comm, $this->userSession->Dao, MIDAS_POLICY_READ)) {
         throw new Zend_Exception('You do not have read permissions on the project');
     }
     $latest = $this->Packages_Package->getLatestOfEachPackageType($application, $os, $arch);
     $filtered = array();
     foreach ($latest as $package) {
         if ($this->Item->policyCheck($package->getItem(), $this->userSession->Dao, MIDAS_POLICY_READ)) {
             $sizestr = UtilityComponent::formatSize($package->getItem()->getSizebytes());
             $filtered[] = array_merge($package->toArray(), array('size_formatted' => $sizestr));
         }
     }
     echo JsonComponent::encode($filtered);
 }
Example #5
0
 /**
  * View a Item.
  *
  * @throws Zend_Exception on invalid itemId and incorrect access permission
  */
 public function viewAction()
 {
     $this->view->Date = $this->Component->Date;
     $itemId = $this->getParam('itemId');
     if (!isset($itemId) || !is_numeric($itemId)) {
         throw new Zend_Exception('itemId should be a number');
     }
     $itemDao = $this->Item->load($itemId);
     if ($itemDao === false) {
         throw new Zend_Exception("This item doesn't exist.", 404);
     }
     if (!$this->Item->policyCheck($itemDao, $this->userSession->Dao, MIDAS_POLICY_READ)) {
         throw new Zend_Exception('Read permission required', 403);
     }
     $this->view->isAdmin = $this->Item->policyCheck($itemDao, $this->userSession->Dao, MIDAS_POLICY_ADMIN);
     $this->view->isModerator = $this->Item->policyCheck($itemDao, $this->userSession->Dao, MIDAS_POLICY_WRITE);
     $itemRevision = $this->Item->getLastRevision($itemDao);
     if ($this->_request->isPost()) {
         $itemRevisionNumber = $this->getParam('itemrevision');
         if (isset($itemRevisionNumber)) {
             $metadataItemRevision = $this->Item->getRevision($itemDao, $itemRevisionNumber);
         } else {
             $metadataItemRevision = $itemRevision;
         }
         if ($metadataItemRevision === false) {
             throw new Zend_Exception('The item must have at least one revision to have metadata', MIDAS_INVALID_POLICY);
         }
         $deleteMetadata = $this->getParam('deleteMetadata');
         $editMetadata = $this->getParam('editMetadata');
         if (isset($deleteMetadata) && !empty($deleteMetadata) && $this->view->isModerator) {
             // delete metadata field
             $this->disableView();
             $this->disableLayout();
             $metadataId = $this->getParam('element');
             $this->ItemRevision->deleteMetadata($metadataItemRevision, $metadataId);
             echo JsonComponent::encode(array(true, $this->t('Changes saved')));
             return;
         }
         if (isset($editMetadata) && !empty($editMetadata) && $this->view->isModerator) {
             // add metadata field
             $metadatatype = $this->getParam('metadatatype');
             $element = $this->getParam('element');
             $qualifier = $this->getParam('qualifier');
             $value = $this->getParam('value');
             $updateMetadata = $this->getParam('updateMetadata');
             $metadataDao = $this->Metadata->getMetadata($metadatatype, $element, $qualifier);
             if ($metadataDao == false) {
                 $metadataDao = $this->Metadata->addMetadata($metadatatype, $element, $qualifier, '');
             }
             $metadataDao->setItemrevisionId($metadataItemRevision->getKey());
             $metadataValueExists = $this->Metadata->getMetadataValueExists($metadataDao);
             if ($updateMetadata || !$metadataValueExists) {
                 // if we are updating or no metadatavalue exists, then save it
                 // otherwise we are attempting to add a new value where one already
                 // exists, and we won't save in this case
                 $this->Metadata->addMetadataValue($metadataItemRevision, $metadatatype, $element, $qualifier, $value);
             }
         }
     }
     if ($this->logged && !$this->isTestingEnv()) {
         $cookieName = hash('sha1', MIDAS_ITEM_COOKIE_NAME . $this->userSession->Dao->getKey());
         /** @var Zend_Controller_Request_Http $request */
         $request = $this->getRequest();
         $cookieData = $request->getCookie($cookieName);
         $recentItems = array();
         if (isset($cookieData)) {
             $recentItems = unserialize($cookieData);
         }
         $tmp = array_reverse($recentItems);
         $i = 0;
         foreach ($tmp as $key => $t) {
             if ($t == $itemDao->getKey() || !is_numeric($t)) {
                 unset($tmp[$key]);
                 continue;
             }
             ++$i;
             if ($i > 4) {
                 unset($tmp[$key]);
             }
         }
         $recentItems = array_reverse($tmp);
         $recentItems[] = $itemDao->getKey();
         $date = new DateTime();
         $interval = new DateInterval('P1M');
         setcookie($cookieName, serialize($recentItems), $date->add($interval)->getTimestamp(), '/', $request->getHttpHost(), (int) Zend_Registry::get('configGlobal')->get('cookie_secure', 1) === 1, true);
     }
     $this->Item->incrementViewCount($itemDao);
     $itemDao->lastrevision = $itemRevision;
     $itemDao->revisions = $itemDao->getRevisions();
     // Display the good link if the item is pointing to a website
     $this->view->itemIsLink = false;
     if (isset($itemRevision) && $itemRevision !== false) {
         $bitstreams = $itemRevision->getBitstreams();
         if (count($bitstreams) == 1) {
             $bitstream = $bitstreams[0];
             if (preg_match('/^https?:\\/\\//', $bitstream->getPath())) {
                 $this->view->itemIsLink = true;
             }
         }
         $itemDao->creation = $this->Component->Date->formatDate(strtotime($itemRevision->getDate()));
     }
     // Add the metadata for each revision
     foreach ($itemDao->getRevisions() as $revision) {
         $revision->metadatavalues = $this->ItemRevision->getMetadata($revision);
     }
     $this->Component->Sortdao->field = 'revision';
     $this->Component->Sortdao->order = 'desc';
     usort($itemDao->revisions, array($this->Component->Sortdao, 'sortByNumber'));
     $this->view->itemDao = $itemDao;
     $this->view->itemSize = UtilityComponent::formatSize($itemDao->getSizebytes());
     $this->view->title .= ' - ' . $itemDao->getName();
     $this->view->metaDescription = substr($itemDao->getDescription(), 0, 160);
     $tmp = Zend_Registry::get('notifier')->callback('CALLBACK_VISUALIZE_CAN_VISUALIZE', array('item' => $itemDao));
     if (isset($tmp['visualize']) && $tmp['visualize'] == true) {
         $this->view->preview = true;
     } else {
         $this->view->preview = false;
     }
     $currentFolder = false;
     $parents = $itemDao->getFolders();
     if (count($parents) == 1) {
         $currentFolder = $parents[0];
     } elseif (isset($this->userSession->recentFolders)) {
         foreach ($this->userSession->recentFolders as $recent) {
             foreach ($parents as $parent) {
                 if ($parent->getKey() == $recent) {
                     $currentFolder = $parent;
                     break;
                 }
             }
         }
         if ($currentFolder === false && count($parents) > 0) {
             $currentFolder = $parents[0];
         }
     } elseif (count($parents) > 0) {
         $currentFolder = $parents[0];
     }
     $this->view->currentFolder = $currentFolder;
     $parent = $currentFolder;
     $breadcrumbs = array();
     while ($parent !== false) {
         if (strpos($parent->getName(), 'community') !== false && $this->Folder->getCommunity($parent) !== false) {
             $breadcrumbs[] = array('type' => 'community', 'object' => $this->Folder->getCommunity($parent));
         } elseif (strpos($parent->getName(), 'user') !== false && $this->Folder->getUser($parent) !== false) {
             $breadcrumbs[] = array('type' => 'user', 'object' => $this->Folder->getUser($parent));
         } else {
             $breadcrumbs[] = array('type' => 'folder', 'object' => $parent);
         }
         $parent = $parent->getParent();
     }
     $this->Component->Breadcrumb->setBreadcrumbHeader(array_reverse($breadcrumbs), $this->view);
     $folders = array();
     $parents = $itemDao->getFolders();
     foreach ($parents as $parent) {
         if ($this->Folder->policyCheck($parent, $this->userSession->Dao, MIDAS_POLICY_READ)) {
             $folders[] = $parent;
         }
     }
     $this->view->folders = $folders;
     $this->view->json['item'] = $itemDao->toArray();
     $this->view->json['item']['isAdmin'] = $this->view->isAdmin;
     $this->view->json['item']['isModerator'] = $this->view->isModerator;
     $this->view->json['item']['message']['delete'] = $this->t('Delete');
     $this->view->json['item']['message']['sharedItem'] = $this->t('This item is currrently shared by other folders and/or communities. Deletion will make it disappear in all these folders and/or communitites. ');
     $this->view->json['item']['message']['deleteMessage'] = $this->t('Do you really want to delete this item? It cannot be undone.');
     $this->view->json['item']['message']['deleteMetadataMessage'] = $this->t('Do you really want to delete this metadata? It cannot be undone.');
     $this->view->json['item']['message']['deleteItemrevisionMessage'] = $this->t('Do you really want to delete this revision? It cannot be undone.');
     $this->view->json['item']['message']['deleteBitstreamMessage'] = $this->t('Do you really want to delete this bitstream? It cannot be undone.');
     $this->view->json['item']['message']['duplicate'] = $this->t('Duplicate Item');
     $this->view->json['modules'] = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_ITEM_VIEW_JSON', array('item' => $itemDao));
 }
Example #6
0
 /**
  * Add free space information to the DOM on the revision uploader page.
  *
  * @param array $args parameters, including "item", the item DAO that you are uploading a new revision into
  * @return string
  * @throws Zend_Exception
  */
 public function getExtraHtmlRevision($args)
 {
     /** @var FolderModel $folderModel */
     $folderModel = MidasLoader::loadModel('Folder');
     /** @var Sizequota_FolderQuotaModel $folderQuotaModel */
     $folderQuotaModel = MidasLoader::loadModel('FolderQuota', $this->moduleName);
     $item = $args['item'];
     if (!$item) {
         return '<div id="sizequotaFreeSpace" style="display:none;">0</div>' . '<div id="sizequotaHFreeSpace" style="display:none;">--</div>';
     }
     $folders = $item->getFolders();
     if (count($folders) == 0) {
         // don't allow any more uploading into an orphaned item
         return '<div id="sizequotaFreeSpace" style="display:none;">0</div>' . '<div id="sizequotaHFreeSpace" style="display:none;">--</div>';
     } else {
         $rootFolder = $folderModel->getRoot($folders[0]);
         $quota = $folderQuotaModel->getFolderQuota($rootFolder);
         $assetstoreFree = UtilityComponent::diskFreeSpace($this->Assetstore->getDefault()->getPath());
         if ($quota == '') {
             $free = $assetstoreFree;
         } else {
             $used = $folderModel->getSize($rootFolder);
             $free = min($assetstoreFree, $quota - $used);
         }
         $free = number_format($free, 0, '.', '');
         $hFree = UtilityComponent::formatSize($free);
         return '<div id="sizequotaFreeSpace" style="display:none;">' . htmlspecialchars($free, ENT_QUOTES, 'UTF-8') . '</div>' . '<div id="sizequotaHFreeSpace" style="display:none;">' . htmlspecialchars($hFree, ENT_QUOTES, 'UTF-8') . '</div>';
     }
 }