/**
  * Calling this will stream the file to the client.
  * The parameter is a bitstream dao.
  * Optional second parameter is the download offset in bytes.
  *
  * @param BitstreamDao $bitstream
  * @param int $offset
  * @param bool $incrementDownload
  * @throws Zend_Exception
  */
 public function download($bitstream, $offset = 0, $incrementDownload = false)
 {
     // Disable gzip output on apache servers (otherwise no progress in browser)
     if (function_exists('apache_setenv')) {
         apache_setenv('no-gzip', '1');
     }
     $mimetype = $bitstream->getMimetype();
     $path = $bitstream->getAssetstore()->getPath() . '/' . $bitstream->getPath();
     $name = $bitstream->getName();
     if (!file_exists($path)) {
         throw new Zend_Exception('Unable to find file on the disk');
     }
     $chunkSize = 1024 * 64;
     $fileSize = UtilityComponent::fileSize($path);
     $handle = fopen($path, 'rb');
     if ($handle === false) {
         throw new Zend_Exception('Unable to open the file');
     }
     if (!$this->testingmode) {
         // don't send any headers in testing mode since it will break it
         $modified = gmdate('D, d M Y H:i:s') . ' GMT';
         $contentType = $mimetype;
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Last-Modified: ' . $modified);
         // if pdf set the content-type accordingly
         if (!isset($contentType) && pathinfo($name, PATHINFO_EXTENSION) == 'pdf') {
             $contentType = 'application/pdf';
             $enableContentDisposition = false;
         }
         if (!isset($contentType)) {
             $contentType = 'application/octet-stream';
         }
         // Hack for .vsp files (for OSA)
         if (!isset($contentType) && strlen($name) > 4 && substr($name, strlen($name) - 4, 4) == '.vsp') {
             $contentType = 'application/isp';
         }
         $agent = env('HTTP_USER_AGENT');
         if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
             header('Content-Type: ' . $contentType);
             header('Content-Disposition: attachment; filename="' . $name . '"');
             header('Expires: 0');
             header('Accept-Ranges: bytes');
             header('Cache-Control: private', false);
             header('Pragma: private');
             $httpRange = env('HTTP_RANGE');
             if (isset($httpRange)) {
                 // HTTP range is of the form "bytes=n-" where n is the offset
                 list(, $range) = explode('=', $httpRange);
                 $firstByte = strstr($range, '-', true);
                 $lastByte = $fileSize - 1;
                 $length = $fileSize - $firstByte;
                 header('HTTP/1.1 206 Partial Content');
                 header('Content-Length: ' . $length);
                 header('Content-Range: bytes ' . $firstByte . '-' . $lastByte . '/' . $fileSize);
                 fseek($handle, $firstByte);
             } else {
                 header('Content-Length: ' . $fileSize);
             }
         } else {
             header('Accept-Ranges: bytes');
             header('Expires: 0');
             header('Content-Type: ' . $contentType);
             header('Content-Length: ' . $fileSize);
             if (!isset($enableContentDisposition) || $enableContentDisposition == true) {
                 header('Content-Disposition: attachment; filename="' . $name . '"');
             }
             if (isset($httpRange)) {
                 list(, $range) = explode('=', $httpRange);
                 $firstByte = strstr($range, '-', true);
                 $lastByte = $fileSize - 1;
                 $length = $fileSize - $firstByte;
                 header('HTTP/1.1 206 Partial Content');
                 header('Content-Length: ' . $length);
                 header('Content-Range: bytes ' . $firstByte . '-' . $lastByte . '/' . $fileSize);
                 fseek($handle, $firstByte);
             }
         }
     }
     ignore_user_abort(true);
     // must call this so the script doesn't end as soon as connection closed
     // close the database connection so we don't get too many connections problems
     Zend_Registry::get('dbAdapter')->closeConnection();
     session_write_close();
     // unlock session writing for concurrent access
     // kill the whole ob stack (Zend uses double nested output buffers)
     while (!$this->testingmode && ob_get_level() > 0) {
         ob_end_clean();
     }
     if (is_numeric($offset) && $offset > 0 && $offset <= $fileSize) {
         fseek($handle, $offset);
     }
     while (!feof($handle) && connection_status() == 0) {
         echo fread($handle, $chunkSize);
     }
     if ($incrementDownload && feof($handle)) {
         // Only record downloads that actually complete
         /** @var ItemModel $itemModel */
         $itemModel = MidasLoader::loadModel('Item');
         $itemModel->incrementDownloadCount($bitstream->getItemrevision()->getItem());
     }
     fclose($handle);
     if (!$this->testingmode) {
         // don't exit if we are in testing mode
         exit;
     }
 }
Пример #2
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());
 }
Пример #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;
 }
Пример #4
0
 /** Import a directory recursively */
 private function _recursiveParseDirectory($path, $currentdir)
 {
     $it = new DirectoryIterator($path);
     foreach ($it as $fileInfo) {
         if ($fileInfo->isDot()) {
             continue;
         }
         // If the file/dir is not readable (permission issue)
         if (!$fileInfo->isReadable()) {
             $this->getLogger()->crit($fileInfo->getPathName() . ' cannot be imported. Not readable.');
             continue;
         }
         // If this is too slow we'll figure something out
         if ($this->_checkStopImport()) {
             return false;
         }
         if ($fileInfo->isDir()) {
             // we have a directory
             // If the the directory actually doesn't exist at this point,
             // skip it.
             if (!file_exists($fileInfo->getPathName())) {
                 continue;
             }
             // Get the files in the directory and skip the folder if it does not
             // contain any files and we aren't set to import empty directories. The
             // count($files) <= 2 is there to account for our our friends . and ..
             $files = scandir($fileInfo->getPathName());
             if (!$this->importemptydirectories && $files && count($files) <= 2) {
                 continue;
             }
             // Find if the child exists
             $child = $this->Folder->getFolderByName($currentdir, $fileInfo->getFilename());
             // If the folder does not exist, create one.
             if (!$child) {
                 $child = new FolderDao();
                 $child->setName($fileInfo->getFilename());
                 $child->setParentId($currentdir->getFolderId());
                 $child->setDateCreation(date('Y-m-d H:i:s'));
                 $child->setDescription('');
                 $this->Folder->save($child);
                 $this->Folderpolicyuser->createPolicy($this->userSession->Dao, $child, MIDAS_POLICY_ADMIN);
             }
             // Keep descending
             $this->_recursiveParseDirectory($fileInfo->getPathName(), $child);
         } else {
             // We have a file
             $this->_incrementFileProcessed();
             $newrevision = true;
             $item = $this->Folder->getItemByName($currentdir, $fileInfo->getFilename());
             if (!$item) {
                 // Create an item
                 $item = new ItemDao();
                 $item->setName($fileInfo->getFilename());
                 $item->setDescription('');
                 $item->setPrivacyStatus(MIDAS_PRIVACY_PRIVATE);
                 // Must set this flag private initially
                 $this->Item->save($item, true);
                 // Set the policy of the item
                 $this->Itempolicyuser->createPolicy($this->userSession->Dao, $item, MIDAS_POLICY_ADMIN);
                 // Add the item to the current directory
                 $this->Folder->addItem($currentdir, $item);
             }
             // Check if the bistream has been updated based on the local date
             $revision = $this->ItemRevision->getLatestRevision($item);
             if ($revision) {
                 $newrevision = false;
                 $bitstream = $this->ItemRevision->getBitstreamByName($revision, $fileInfo->getFilename());
                 $curMD5 = UtilityComponent::md5file($fileInfo->getPathName());
                 $diskFileIsNewer = strtotime($bitstream->getDate()) < filemtime($fileInfo->getPathName());
                 $md5IsDifferent = $bitstream->getChecksum() != $curMD5;
                 if (!$bitstream || $diskFileIsNewer && $md5IsDifferent) {
                     $newrevision = true;
                 }
             }
             if ($newrevision) {
                 // Create a revision for the item
                 $itemRevisionDao = new ItemRevisionDao();
                 $itemRevisionDao->setChanges('Initial revision');
                 $itemRevisionDao->setUser_id($this->userSession->Dao->getUserId());
                 $this->Item->addRevision($item, $itemRevisionDao);
                 // Add bitstreams to the revision
                 $this->getLogger()->debug('create New Bitstream');
                 $bitstreamDao = new BitstreamDao();
                 $bitstreamDao->setName($fileInfo->getFilename());
                 $bitstreamDao->setPath($fileInfo->getPathName());
                 $bitstreamDao->fillPropertiesFromPath();
                 // Set the Assetstore
                 $bitstreamDao->setAssetstoreId($this->assetstoreid);
                 // Upload the bitstream
                 $assetstoreDao = $this->Assetstore->load($this->assetstoreid);
                 $this->Component->Upload->uploadBitstream($bitstreamDao, $assetstoreDao, true);
                 $this->ItemRevision->addBitstream($itemRevisionDao, $bitstreamDao);
             }
         }
     }
     unset($it);
     return true;
 }
 /**
  * Function to create the items.
  *
  * @param int $collectionId
  * @param int $parentFolderid
  */
 private function _createFolderForItem($collectionId, $parentFolderid)
 {
     /** @var FolderModel $Folder */
     $Folder = MidasLoader::loadModel('Folder');
     /** @var ItemModel $Item */
     $Item = MidasLoader::loadModel('Item');
     /** @var ItemRevisionModel $ItemRevision */
     $ItemRevision = MidasLoader::loadModel('ItemRevision');
     /** @var GroupModel $Group */
     $Group = MidasLoader::loadModel('Group');
     /** @var AssetstoreModel $Assetstore */
     $Assetstore = MidasLoader::loadModel('Assetstore');
     /** @var FolderpolicygroupModel $Folderpolicygroup */
     $Folderpolicygroup = MidasLoader::loadModel('Folderpolicygroup');
     /** @var FolderpolicyuserModel $Folderpolicyuser */
     $Folderpolicyuser = MidasLoader::loadModel('Folderpolicyuser');
     /** @var ItempolicygroupModel $Itempolicygroup */
     $Itempolicygroup = MidasLoader::loadModel('Itempolicygroup');
     /** @var ItempolicyuserModel $Itempolicyuser */
     $Itempolicyuser = MidasLoader::loadModel('Itempolicyuser');
     /** @var UserModel $User */
     $User = MidasLoader::loadModel('User');
     $colquery = pg_query('SELECT i.item_id, mtitle.text_value AS title, mabstract.text_value AS abstract ' . 'FROM item AS i ' . 'LEFT JOIN metadatavalue AS mtitle ON (i.item_id = mtitle.item_id AND mtitle.metadata_field_id = 64) ' . 'LEFT JOIN metadatavalue AS mabstract ON (i.item_id = mabstract.item_id AND mabstract.metadata_field_id = 27) ' . 'WHERE i.owning_collection=' . $collectionId);
     while ($colquery_array = pg_fetch_array($colquery)) {
         $item_id = $colquery_array['item_id'];
         $title = $colquery_array['title'];
         // If title is empty we skip this item
         if (empty($title)) {
             continue;
         }
         $abstract = $colquery_array['abstract'];
         $folderDao = false;
         try {
             // Create the folder for the item
             $folderDao = $Folder->createFolder($title, $abstract, $parentFolderid);
             // Assign the policies to the folder as the same as the parent folder
             $folder = $Folder->load($parentFolderid);
             $policyGroup = $folder->getFolderpolicygroup();
             $policyUser = $folder->getFolderpolicyuser();
             foreach ($policyGroup as $policy) {
                 $group = $policy->getGroup();
                 $policyValue = $policy->getPolicy();
                 $Folderpolicygroup->createPolicy($group, $folderDao, $policyValue);
             }
             foreach ($policyUser as $policy) {
                 $user = $policy->getUser();
                 $policyValue = $policy->getPolicy();
                 $Folderpolicyuser->createPolicy($user, $folderDao, $policyValue);
             }
             // Add specific policies for users (not dealing with groups)
             $policyquery = pg_query('SELECT max(action_id) AS actionid, eperson.eperson_id, eperson.email
                             FROM resourcepolicy
                             LEFT JOIN eperson ON (eperson.eperson_id=resourcepolicy.eperson_id)
                              WHERE epersongroup_id IS NULL AND resource_type_id=' . MIDAS2_RESOURCE_ITEM . ' AND resource_id=' . $item_id . ' GROUP BY eperson.eperson_id, email');
             while ($policyquery_array = pg_fetch_array($policyquery)) {
                 $actionid = $policyquery_array['actionid'];
                 $email = $policyquery_array['email'];
                 if ($actionid > 1) {
                     $policyValue = MIDAS_POLICY_ADMIN;
                 } elseif ($actionid == 1) {
                     $policyValue = MIDAS_POLICY_WRITE;
                 } else {
                     $policyValue = MIDAS_POLICY_READ;
                 }
                 $userDao = $User->getByEmail($email);
                 $Folderpolicyuser->createPolicy($userDao, $folderDao, $policyValue);
             }
         } catch (Zend_Exception $e) {
             $this->getLogger()->info($e->getMessage());
             Zend_Debug::dump($e);
             // we continue
         }
         if ($folderDao) {
             // Create the item from the bitstreams
             $bitquery = pg_query('SELECT   b.bitstream_id, b.name, b.description, b.internal_id FROM bitstream AS b, item2bitstream AS i2b ' . 'WHERE i2b.bitstream_id = b.bitstream_id AND i2b.item_id=' . $item_id);
             while ($bitquery_array = pg_fetch_array($bitquery)) {
                 $filename = $bitquery_array['name'];
                 $itemdao = new ItemDao();
                 $itemdao->setName($filename);
                 // Get the number of downloads and set it
                 $itemstatsquery = pg_query('SELECT downloads from midas_resourcelog WHERE
                                   resource_id_type=' . MIDAS2_RESOURCE_ITEM . ' AND resource_id=' . $item_id);
                 $itemstats_array = pg_fetch_array($itemstatsquery);
                 if ($itemstats_array) {
                     $itemdao->setView($itemstats_array['downloads']);
                     $itemdao->setDownload($itemstats_array['downloads']);
                 }
                 $Item->save($itemdao);
                 // Just check if the group anonymous can access the item
                 $policyquery = pg_query('SELECT policy_id FROM resourcepolicy WHERE resource_type_id=' . MIDAS2_RESOURCE_ITEM . ' AND resource_id=' . $item_id . ' AND epersongroup_id=0');
                 if (pg_num_rows($policyquery) > 0) {
                     $anonymousGroup = $Group->load(MIDAS_GROUP_ANONYMOUS_KEY);
                     $Itempolicygroup->createPolicy($anonymousGroup, $itemdao, MIDAS_POLICY_READ);
                 }
                 // Add specific policies for users
                 $policyquery = pg_query('SELECT max(action_id) AS actionid, eperson.eperson_id, eperson.email
                               FROM resourcepolicy
                               LEFT JOIN eperson ON (eperson.eperson_id=resourcepolicy.eperson_id)
                                WHERE epersongroup_id IS NULL AND resource_type_id=' . MIDAS2_RESOURCE_ITEM . ' AND resource_id=' . $item_id . ' GROUP BY eperson.eperson_id, email');
                 while ($policyquery_array = pg_fetch_array($policyquery)) {
                     $actionid = $policyquery_array['actionid'];
                     $email = $policyquery_array['email'];
                     if ($actionid > 1) {
                         $policyValue = MIDAS_POLICY_ADMIN;
                     } elseif ($actionid == 1) {
                         $policyValue = MIDAS_POLICY_WRITE;
                     } else {
                         $policyValue = MIDAS_POLICY_READ;
                     }
                     $userDao = $User->getByEmail($email);
                     // Set the policy of the item
                     $Itempolicyuser->createPolicy($userDao, $itemdao, $policyValue);
                 }
                 // Add the item to the current directory
                 $Folder->addItem($folderDao, $itemdao);
                 // Create a revision for the item
                 $itemRevisionDao = new ItemRevisionDao();
                 $itemRevisionDao->setChanges('Initial revision');
                 $itemRevisionDao->setUser_id($this->userId);
                 $Item->addRevision($itemdao, $itemRevisionDao);
                 // Add the metadata
                 /** @var MetadataModel $MetadataModel */
                 $MetadataModel = MidasLoader::loadModel('Metadata');
                 //
                 $metadataquery = pg_query('SELECT metadata_field_id, text_value FROM metadatavalue WHERE item_id=' . $item_id);
                 while ($metadata_array = pg_fetch_array($metadataquery)) {
                     $text_value = $metadata_array['text_value'];
                     $metadata_field_id = $metadata_array['metadata_field_id'];
                     // Do not check 64 and 27 because they are stored as field and not metadata
                     // in MIDAS3
                     switch ($metadata_field_id) {
                         case 3:
                             $element = 'contributor';
                             $qualifier = 'author';
                             break;
                         case 11:
                             $element = 'date';
                             $qualifier = 'uploaded';
                             break;
                         case 14:
                             $element = 'date';
                             $qualifier = 'created';
                             break;
                         case 15:
                             $element = 'date';
                             $qualifier = 'issued';
                             break;
                         case 18:
                             $element = 'identifier';
                             $qualifier = 'citation';
                             break;
                         case 25:
                             $element = 'identifier';
                             $qualifier = 'uri';
                             break;
                         case 26:
                             $element = 'description';
                             $qualifier = 'general';
                             break;
                         case 28:
                             $element = 'description';
                             $qualifier = 'provenance';
                             break;
                         case 29:
                             $element = 'description';
                             $qualifier = 'sponsorship';
                             break;
                         case 39:
                             $element = 'description';
                             $qualifier = 'publisher';
                             break;
                         case 57:
                             $element = 'subject';
                             $qualifier = 'keyword';
                             break;
                         case 68:
                             $element = 'subject';
                             $qualifier = 'ocis';
                             break;
                         case 75:
                             $element = 'identifier';
                             $qualifier = 'pubmed';
                             break;
                         case 74:
                             $element = 'identifier';
                             $qualifier = 'doi';
                             break;
                         default:
                             $element = '';
                             $qualifier = '';
                     }
                     if ($element != '') {
                         $MetadataModel->addMetadataValue($itemRevisionDao, MIDAS_METADATA_TEXT, $element, $qualifier, $text_value);
                     }
                 }
                 // Add bitstreams to the revision
                 $bitstreamDao = new BitstreamDao();
                 $bitstreamDao->setName($filename);
                 // Compute the path from the internalid
                 // We are assuming only one assetstore
                 $internal_id = $bitquery_array['internal_id'];
                 $filepath = $this->midas2Assetstore . '/';
                 $filepath .= substr($internal_id, 0, 2) . '/';
                 $filepath .= substr($internal_id, 2, 2) . '/';
                 $filepath .= substr($internal_id, 4, 2) . '/';
                 $filepath .= $internal_id;
                 // Check that the file exists
                 if (file_exists($filepath)) {
                     // Upload the bitstream
                     $assetstoreDao = $Assetstore->load($this->assetstoreId);
                     $bitstreamDao->setPath($filepath);
                     $bitstreamDao->fillPropertiesFromPath();
                     $bitstreamDao->setAssetstoreId($this->assetstoreId);
                     $UploadComponent = new UploadComponent();
                     $UploadComponent->uploadBitstream($bitstreamDao, $assetstoreDao);
                     // Upload the bitstream if necessary (based on the assetstore type)
                     $ItemRevision->addBitstream($itemRevisionDao, $bitstreamDao);
                     unset($UploadComponent);
                 }
             }
         } else {
             echo 'Cannot create Folder for item: ' . $title . '<br>';
         }
     }
 }
Пример #6
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;
 }
Пример #7
0
 /** convert to threejs */
 public function convertToThreejs($revision)
 {
     /** @var SettingModel $settingModel */
     $settingModel = MidasLoader::loadModel('Setting');
     $useWebGL = $settingModel->getValueByName(VISUALIZE_USE_WEB_GL_KEY, $this->moduleName);
     if (!isset($useWebGL) || !$useWebGL) {
         return false;
     }
     /** @var ItemRevisionModel $itemRevisionModel */
     $itemRevisionModel = MidasLoader::loadModel('ItemRevision');
     /** @var ItemModel $itemModel */
     $itemModel = MidasLoader::loadModel('Item');
     if (is_array($revision)) {
         $revision = $itemRevisionModel->load($revision['itemrevision_id']);
     }
     $bitstreams = $revision->getBitstreams();
     $item = $revision->getItem();
     $parents = $item->getFolders();
     $userDao = $revision->getUser();
     if (count($parents) == 0) {
         return;
     }
     $parent = $parents[0];
     if (count($bitstreams) != 1) {
         return;
     }
     $bitstream = $bitstreams[0];
     $filenameArray = explode('.', $bitstream->getName());
     $ext = end($filenameArray);
     if ($ext != 'obj') {
         return;
     }
     if (file_exists(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js')) {
         unlink(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js');
     }
     exec('python ' . dirname(__FILE__) . '/scripts/convert_obj_three.py -i ' . $bitstream->GetFullPath() . ' -o ' . UtilityComponent::getTempDirectory() . '/tmpThreeJs.js -t binary', $output);
     if (file_exists(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js') && file_exists(UtilityComponent::getTempDirectory() . '/tmpThreeJs.bin')) {
         /** @var AssetstoreModel $assetstoreModel */
         $assetstoreModel = MidasLoader::loadModel('Assetstore');
         $assetstoreDao = $assetstoreModel->getDefault();
         /** @var UploadComponent $uploadComponent */
         $uploadComponent = MidasLoader::loadComponent('Upload');
         $newItem = $uploadComponent->createUploadedItem($userDao, $item->getName() . '.threejs.bin', UtilityComponent::getTempDirectory() . '/tmpThreeJs.bin', $parent);
         $itemModel->copyParentPolicies($newItem, $parent);
         $newRevision = $itemModel->getLastRevision($newItem);
         if ($newRevision === false) {
             throw new Zend_Exception('The item has no revisions', MIDAS_INVALID_POLICY);
         }
         $bitstreams = $newRevision->getBitstreams();
         if (count($bitstreams) === 0) {
             throw new Zend_Exception('The item has no bitstreams', MIDAS_INVALID_POLICY);
         }
         $bitstreamDao = $bitstreams[0];
         Zend_Loader::loadClass('BitstreamDao', BASE_PATH . '/core/models/dao');
         $content = file_get_contents(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js');
         $fc = Zend_Controller_Front::getInstance();
         $content = str_replace('tmpThreeJs.bin', $fc->getBaseUrl() . '/download/?bitstream=' . $bitstreamDao->getKey(), $content);
         file_put_contents(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js', $content);
         $bitstreamDao = new BitstreamDao();
         $bitstreamDao->setName($item->getName() . '.threejs.js');
         $bitstreamDao->setPath(UtilityComponent::getTempDirectory() . '/tmpThreeJs.js');
         $bitstreamDao->setChecksum('');
         $bitstreamDao->fillPropertiesFromPath();
         $bitstreamDao->setAssetstoreId($assetstoreDao->getKey());
         // Upload the bitstream if necessary (based on the assetstore type)
         $uploadComponent->uploadBitstream($bitstreamDao, $assetstoreDao);
         $itemRevisionModel->addBitstream($newRevision, $bitstreamDao);
     }
 }
 /**
  * Function for grabbing bitstream id (used in itemrevisionGet).
  *
  * @param BitstreamDao $bitstream
  * @return int
  */
 public function getBitstreamId($bitstream)
 {
     return $bitstream->getBitstreamId();
 }
Пример #9
0
 /** 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);
         }
     }
 }