示例#1
0
 /**
  * Generate a unique upload token.  Either <b>itemid</b> or <b>folderid</b> is required,
  * but both are not allowed.
  *
  * @path /system/uploadtoken
  * @http GET
  * @param useSession (Optional) Authenticate using the current Midas session
  * @param token (Optional) Authentication token
  * @param itemid (Optional)
  * The id of the item to upload into.
  * @param folderid (Optional)
  * The id of the folder to create a new item in and then upload to.
  * The new item will have the same name as <b>filename</b> unless <b>itemname</b>
  * is supplied.
  * @param filename The filename of the file you will upload, will be used as the
  * bitstream's name and the item's name (unless <b>itemname</b> is supplied).
  * @param itemdescription (Optional)
  * When passing the <b>folderid</b> param, the description of the item,
  * if not supplied the item's description will be blank.
  * @param itemname (Optional)
  * When passing the <b>folderid</b> param, the name of the newly created item,
  * if not supplied, the item will have the same name as <b>filename</b>.
  * @param checksum (Optional) The md5 checksum of the file to be uploaded.
  * @return An upload token that can be used to upload a file.
  *            If <b>folderid</b> is passed instead of <b>itemid</b>, a new item will be created
  *            in that folder, but the id of the newly created item will not be
  *            returned.  If the id of the newly created item is needed,
  *            then call the <b>/item (POST)</b> api instead.
  *            If <b>checksum</b> is passed and the token returned is blank, the
  *            server already has this file and there is no need to follow this
  *            call with a call to <b>/system/upload</b>, as the passed in
  *            file will have been added as a bitstream to the item's latest
  *            revision, creating a new revision if one doesn't exist.
  *
  * @param array $args parameters
  * @throws Exception
  */
 public function uploadGeneratetoken($args)
 {
     /** @var ApihelperComponent $apihelperComponent */
     $apihelperComponent = MidasLoader::loadComponent('Apihelper');
     $apihelperComponent->validateParams($args, array('filename'));
     if (!array_key_exists('itemid', $args) && !array_key_exists('folderid', $args)) {
         throw new Exception('Parameter itemid or folderid must be defined', MIDAS_INVALID_PARAMETER);
     }
     if (array_key_exists('itemid', $args) && array_key_exists('folderid', $args)) {
         throw new Exception('Parameter itemid or folderid must be defined, but not both', MIDAS_INVALID_PARAMETER);
     }
     $apihelperComponent->requirePolicyScopes(array(MIDAS_API_PERMISSION_SCOPE_WRITE_DATA));
     $userDao = $apihelperComponent->getUser($args);
     if (!$userDao) {
         throw new Exception('Anonymous users may not upload', MIDAS_INVALID_POLICY);
     }
     /** @var ItemModel $itemModel */
     $itemModel = MidasLoader::loadModel('Item');
     if (array_key_exists('itemid', $args)) {
         $item = $itemModel->load($args['itemid']);
         if (!$itemModel->policyCheck($item, $userDao, MIDAS_POLICY_WRITE)) {
             throw new Exception('Invalid policy or itemid', MIDAS_INVALID_POLICY);
         }
     } elseif (array_key_exists('folderid', $args)) {
         /** @var FolderModel $folderModel */
         $folderModel = MidasLoader::loadModel('Folder');
         $folder = $folderModel->load($args['folderid']);
         if ($folder == false) {
             throw new Exception('Parent folder corresponding to folderid doesn\'t exist', MIDAS_INVALID_PARAMETER);
         }
         if (!$folderModel->policyCheck($folder, $userDao, MIDAS_POLICY_WRITE)) {
             throw new Exception('Invalid policy or folderid', MIDAS_INVALID_POLICY);
         }
         // create a new item in this folder
         $itemname = isset($args['itemname']) ? $args['itemname'] : $args['filename'];
         $description = isset($args['itemdescription']) ? $args['itemdescription'] : '';
         $item = $itemModel->createItem($itemname, $description, $folder);
         if ($item === false) {
             throw new Exception('Create new item failed', MIDAS_INTERNAL_ERROR);
         }
         $itemModel->copyParentPolicies($item, $folder);
         /** @var ItempolicyuserModel $itempolicyuserModel */
         $itempolicyuserModel = MidasLoader::loadModel('Itempolicyuser');
         $itempolicyuserModel->createPolicy($userDao, $item, MIDAS_POLICY_ADMIN);
     }
     if (array_key_exists('checksum', $args)) {
         // If we already have a bitstream with this checksum, create a reference and return blank token
         /** @var BitstreamModel $bitstreamModel */
         $bitstreamModel = MidasLoader::loadModel('Bitstream');
         $existingBitstream = $bitstreamModel->getByChecksum($args['checksum']);
         if ($existingBitstream) {
             // User must have read access to the existing bitstream if they are circumventing the upload.
             // Otherwise an attacker could spoof the checksum and read a private bitstream with a known checksum.
             if ($itemModel->policyCheck($existingBitstream->getItemrevision()->getItem(), $userDao, MIDAS_POLICY_READ)) {
                 $revision = $itemModel->getLastRevision($item);
                 if ($revision === false) {
                     // Create new revision if none exists yet
                     Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH . '/core/models/dao');
                     $revision = new ItemRevisionDao();
                     $revision->setChanges('Initial revision');
                     $revision->setUser_id($userDao->getKey());
                     $revision->setDate(date('Y-m-d H:i:s'));
                     $revision->setLicenseId(null);
                     $itemModel->addRevision($item, $revision);
                 }
                 $siblings = $revision->getBitstreams();
                 foreach ($siblings as $sibling) {
                     if ($sibling->getName() == $args['filename']) {
                         // already have a file with this name. don't add new record.
                         return array('token' => '');
                     }
                 }
                 Zend_Loader::loadClass('BitstreamDao', BASE_PATH . '/core/models/dao');
                 $bitstream = new BitstreamDao();
                 $bitstream->setChecksum($args['checksum']);
                 $bitstream->setName($args['filename']);
                 $bitstream->setSizebytes($existingBitstream->getSizebytes());
                 $bitstream->setPath($existingBitstream->getPath());
                 $bitstream->setAssetstoreId($existingBitstream->getAssetstoreId());
                 $bitstream->setMimetype($existingBitstream->getMimetype());
                 /** @var ItemRevisionModel $revisionModel */
                 $revisionModel = MidasLoader::loadModel('ItemRevision');
                 $revisionModel->addBitstream($revision, $bitstream);
                 return array('token' => '');
             }
         }
     }
     // we don't already have this content, so create the token
     /** @var HttpuploadComponent $uploadComponent */
     $uploadComponent = MidasLoader::loadComponent('Httpupload');
     $apiSetup = $apihelperComponent->getApiSetup();
     $uploadComponent->setTestingMode(Zend_Registry::get('configGlobal')->environment === 'testing');
     return $uploadComponent->generateToken($args, $userDao->getKey() . '/' . $item->getKey());
 }
示例#2
0
 /**
  * Duplicate an item in destination folder/community.
  *
  * Create a new item (same as old one) in destination folder/community. The new item
  * have the same metadata and revisions with the old one, but its owner is set as the
  * input userDao parameter (who run this operation) and access policy is based on
  * the input folderDao parameter (destination folder)
  *
  * @param ItemDao $itemDao the item to be duplicated
  * @param UserDao $userDao the user who run this operation
  * @param FolderDao $folderDao destination folder
  * @return ItemDao
  * @throws Zend_Exception on invalid input parameters (itemDao, userDao and folderDao)
  */
 public function duplicateItem($itemDao, $userDao, $folderDao)
 {
     if (!$itemDao instanceof ItemDao || !$folderDao instanceof FolderDao) {
         throw new Zend_Exception('Error in ItemDao or FolderDao when duplicating item');
     }
     if (!$userDao instanceof UserDao) {
         throw new Zend_Exception('Should be an user.');
     }
     /** @var BitstreamModel $BitstreamModel */
     $BitstreamModel = MidasLoader::loadModel('Bitstream');
     $name = $itemDao->getName();
     $description = $itemDao->getDescription();
     $newItem = $this->createItem($name, $description, $folderDao);
     $newItem->setType($itemDao->getType());
     $newItem->setSizebytes($itemDao->getSizebytes());
     $newItem->setDateCreation(date('Y-m-d H:i:s'));
     $newItem->setDateUpdate(date('Y-m-d H:i:s'));
     $thumbnailId = $itemDao->getThumbnailId();
     if ($thumbnailId !== null) {
         $oldThumb = $BitstreamModel->load($thumbnailId);
         $newThumb = new BitstreamDao();
         $newThumb->setItemrevisionId(-1);
         $newThumb->setName($oldThumb->getName());
         $newThumb->setMimetype($oldThumb->getMimetype());
         $newThumb->setSizebytes($oldThumb->getSizebytes());
         $newThumb->setChecksum($oldThumb->getChecksum());
         $newThumb->setPath($oldThumb->getPath());
         $newThumb->setAssetstoreId($oldThumb->getAssetstoreId());
         $newThumb->setDate($oldThumb->getDate());
         $BitstreamModel->save($newThumb);
         $newItem->setThumbnailId($newThumb->getKey());
     }
     /** @var ItemRevisionModel $ItemRevisionModel */
     $ItemRevisionModel = MidasLoader::loadModel('ItemRevision');
     /** @var BitstreamModel $BitstreamModel */
     $BitstreamModel = MidasLoader::loadModel('Bitstream');
     /** @var MetadataModel $MetadataModel */
     $MetadataModel = MidasLoader::loadModel('Metadata');
     /** @var ItempolicygroupModel $ItemPolicyGroupModel */
     $ItemPolicyGroupModel = MidasLoader::loadModel('Itempolicygroup');
     $ItemPolicyGroupModel->computePolicyStatus($newItem);
     foreach ($itemDao->getRevisions() as $revision) {
         $dupItemRevision = new ItemRevisionDao();
         $dupItemRevision->setItemId($newItem->getItemId());
         $dupItemRevision->setRevision($revision->getRevision());
         $dupItemRevision->setDate($revision->getDate());
         $dupItemRevision->setChanges($revision->getChanges());
         $dupItemRevision->setUserId($userDao->getUserId());
         $dupItemRevision->setLicenseId($revision->getLicenseId());
         $ItemRevisionModel->save($dupItemRevision);
         // duplicate metadata value
         $metadatavalues = $ItemRevisionModel->getMetadata($revision);
         foreach ($metadatavalues as $metadata) {
             $MetadataModel->addMetadataValue($dupItemRevision, $metadata->getMetadatatype(), $metadata->getElement(), $metadata->getQualifier(), $metadata->getValue(), false);
         }
         // duplicate bitstream
         foreach ($revision->getBitstreams() as $bitstream) {
             $dupBitstream = new BitstreamDao();
             $dupBitstream->setItemrevisionId($dupItemRevision->getItemrevisionId());
             $dupBitstream->setName($bitstream->getName());
             $dupBitstream->setMimetype($bitstream->getMimetype());
             $dupBitstream->setSizebytes($bitstream->getSizebytes());
             $dupBitstream->setChecksum($bitstream->getChecksum());
             $dupBitstream->setPath($bitstream->getPath());
             $dupBitstream->setAssetstoreId($bitstream->getAssetstoreId());
             $dupBitstream->setDate($bitstream->getDate());
             $BitstreamModel->save($dupBitstream);
         }
     }
     $this->save($newItem, true);
     // call save with metadata changed flag
     return $newItem;
 }
示例#3
0
 /**
  * Save upload item in the database.
  *
  * @param UserDao $userDao
  * @param string $name
  * @param string $url
  * @param null|int $parent
  * @param int $sizebytes
  * @param string $checksum
  * @return ItemDao
  * @throws Zend_Exception
  */
 public function createLinkItem($userDao, $name, $url, $parent = null, $sizebytes = 0, $checksum = ' ')
 {
     /** @var ItemModel $itemModel */
     $itemModel = MidasLoader::loadModel('Item');
     /** @var FolderModel $folderModel */
     $folderModel = MidasLoader::loadModel('Folder');
     /** @var AssetstoreModel $assetstoreModel */
     $assetstoreModel = MidasLoader::loadModel('Assetstore');
     /** @var ItemRevisionModel $itemRevisionModel */
     $itemRevisionModel = MidasLoader::loadModel('ItemRevision');
     if ($userDao == null) {
         throw new Zend_Exception('Please log in');
     }
     if (is_numeric($parent)) {
         $parent = $folderModel->load($parent);
     }
     if ($parent == false || !$folderModel->policyCheck($parent, $userDao, MIDAS_POLICY_WRITE)) {
         throw new Zend_Exception('Parent permissions errors');
     }
     Zend_Loader::loadClass('ItemDao', BASE_PATH . '/core/models/dao');
     $item = new ItemDao();
     $item->setName($name);
     $item->setDescription('');
     $item->setSizebytes($sizebytes);
     $item->setType(0);
     $item->setPrivacyStatus(MIDAS_PRIVACY_PRIVATE);
     // Must set this flag private initially
     $itemModel->save($item, false);
     $folderModel->addItem($parent, $item);
     $itemModel->copyParentPolicies($item, $parent);
     Zend_Loader::loadClass('ItemRevisionDao', BASE_PATH . '/core/models/dao');
     $itemRevisionDao = new ItemRevisionDao();
     $itemRevisionDao->setChanges('Initial revision');
     $itemRevisionDao->setUser_id($userDao->getKey());
     $itemRevisionDao->setDate(date('Y-m-d H:i:s'));
     $itemRevisionDao->setLicenseId(null);
     $itemModel->addRevision($item, $itemRevisionDao);
     // Add bitstreams to the revision
     Zend_Loader::loadClass('BitstreamDao', BASE_PATH . '/core/models/dao');
     $bitstreamDao = new BitstreamDao();
     $bitstreamDao->setName($url);
     $bitstreamDao->setPath($url);
     $bitstreamDao->setMimetype('url');
     $bitstreamDao->setSizebytes($sizebytes);
     $bitstreamDao->setChecksum($checksum);
     $assetstoreDao = $assetstoreModel->getDefault();
     $bitstreamDao->setAssetstoreId($assetstoreDao->getKey());
     $itemRevisionModel->addBitstream($itemRevisionDao, $bitstreamDao);
     $this->getLogger()->debug('Link item created (' . $item->getName() . ', id=' . $item->getKey() . ')');
     return $item;
 }
 /** define an executable */
 public function defineAction()
 {
     $itemId = $this->getParam('itemId');
     if (!isset($itemId) || !is_numeric($itemId)) {
         throw new Zend_Exception('itemId  should be a number');
     }
     $isAjax = $this->getRequest()->isXmlHttpRequest();
     $this->view->isAjax = $isAjax;
     if ($isAjax) {
         $this->disableLayout();
     }
     $itemDao = $this->Item->load($itemId);
     if ($itemDao === false) {
         throw new Zend_Exception("This item doesn't exist.");
     }
     if (!$this->Item->policyCheck($itemDao, $this->userSession->Dao, MIDAS_POLICY_WRITE)) {
         throw new Zend_Exception('Problem policies.');
     }
     $this->view->header = $this->t('Manage Configuration: ' . $itemDao->getName());
     $metaFile = $this->ModuleComponent->Executable->getMetaIoFile($itemDao);
     if (isset($_GET['init'])) {
         $this->showNotificationMessage('Please set the option of the executable first.');
     }
     $jsonContents = JsonComponent::encode(array());
     if ($metaFile !== false) {
         $jsonContents = Zend_Json::fromXml(file_get_contents($metaFile->getFullPath()), true);
     }
     $this->view->itemDao = $itemDao;
     $this->view->jsonMetadata = $jsonContents;
     $this->view->json['item'] = $itemDao->toArray();
     if ($this->_request->isPost()) {
         $this->disableLayout();
         $this->disableView();
         $results = $_POST['results'];
         $xmlContent = $this->ModuleComponent->Executable->createDefinitionFile($results);
         /** @var RandomComponent $randomComponent */
         $randomComponent = MidasLoader::loadComponent('Random');
         $pathFile = $this->getTempDirectory() . '/' . $randomComponent->generateString(32);
         file_put_contents($pathFile, $xmlContent);
         $revision = $this->Item->getLastRevision($itemDao);
         if ($revision === false) {
             throw new Zend_Exception('The item has no revisions', MIDAS_INVALID_POLICY);
         }
         $bitstreams = $revision->getBitstreams();
         $itemRevisionDao = new ItemRevisionDao();
         $itemRevisionDao->setChanges('Modification Definition File');
         $itemRevisionDao->setUser_id($this->userSession->Dao->getKey());
         $itemRevisionDao->setDate(date('Y-m-d H:i:s'));
         $itemRevisionDao->setLicenseId(null);
         $this->Item->addRevision($itemDao, $itemRevisionDao);
         foreach ($bitstreams as $b) {
             if ($b->getName() != 'MetaIO.vxml') {
                 $b->saved = false;
                 $b->setBitstreamId(null);
                 $this->Bitstream->save($b);
                 $this->ItemRevision->addBitstream($itemRevisionDao, $b);
             }
         }
         $bitstreamDao = new BitstreamDao();
         $bitstreamDao->setName('MetaIO.vxml');
         $bitstreamDao->setPath($pathFile);
         $bitstreamDao->fillPropertiesFromPath();
         $assetstoreDao = $this->Assetstore->getDefault();
         $bitstreamDao->setAssetstoreId($assetstoreDao->getKey());
         // Upload the bitstream if necessary (based on the assetstore type)
         $this->Component->Upload->uploadBitstream($bitstreamDao, $assetstoreDao);
         $this->ItemRevision->addBitstream($itemRevisionDao, $bitstreamDao);
         if (file_exists($pathFile)) {
             unlink($pathFile);
         }
     }
 }