/** Index action */ public function indexAction() { $this->requireAdminPrivileges(); $this->view->pageTitle = 'Cleanup Module Configuration'; $form = new Cleanup_Form_Admin(); if ($this->getRequest()->isPost()) { $data = $this->getRequest()->getPost(); if ($form->isValid($data)) { $values = $form->getValues(); foreach ($values as $key => $value) { if ($key !== 'csrf' && !is_null($value)) { $this->Setting->setConfig($key, $value, $this->moduleName); } } $this->ModuleComponent->Admin->schedulePerformCleanupJob($values[CLEANUP_DAYS_TO_KEEP_PARTIAL_FILES_KEY], UtilityComponent::getTempDirectory(), $this->userSession->Dao); } $form->populate($data); } else { $elements = $form->getElements(); foreach ($elements as $element) { $name = $element->getName(); if ($name !== 'csrf' && $name !== 'submit') { $value = $this->Setting->getValueByName($name, $this->moduleName); if (!is_null($value)) { $form->setDefault($name, $value); } } } } $this->view->form = $form; session_start(); }
/** * will create default paths in the temporary directory * for any properties not already set, except for the * condor bin dir; imposing a firmer hand on the user. * * @param array $currentConfig current configuration * @return array */ protected function createDefaultConfig($currentConfig) { $defaultConfigDirs = array(MIDAS_BATCHMAKE_TMP_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_TMP_DIR, MIDAS_BATCHMAKE_BIN_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_BIN_DIR, MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_SCRIPT_DIR, MIDAS_BATCHMAKE_APP_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_APP_DIR, MIDAS_BATCHMAKE_DATA_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_DATA_DIR); $returnedConfig = array(); foreach ($currentConfig as $configProp => $configDir) { if ((!isset($configProp) || !isset($configDir) || empty($configDir)) && array_key_exists($configProp, $defaultConfigDirs)) { $returnedConfig[$configProp] = UtilityComponent::getTempDirectory($defaultConfigDirs[$configProp]); } else { $returnedConfig[$configProp] = $configDir; } } return $returnedConfig; }
/** * Read in the streamed uploaded file and write it to a temporary file. * Returns the name of the temporary file. */ private function _readUploadedFile($prefix) { UtilityComponent::setTimeLimit(0); $inputfile = 'php://input'; $tmpfile = tempnam(UtilityComponent::getTempDirectory('misc'), $prefix); $in = fopen($inputfile, 'rb'); $out = fopen($tmpfile, 'wb'); $bufSize = 1024 * 1024; $size = 0; // read from input and write into file while (connection_status() == CONNECTION_NORMAL && ($buf = fread($in, $bufSize))) { $size += strlen($buf); fwrite($out, $buf); } fclose($in); fclose($out); return $tmpfile; }
/** * Upload a file to the server. PUT or POST is required. * Will add the file as a bitstream to the item that was specified when * generating the upload token in a new revision to that item, unless * <b>revision</b> param is set. * * @path /system/upload * @http POST * @param uploadtoken The upload token (see <b>midas.upload.generatetoken</b>). * @param filename The name of the bitstream that will be added to the item. * @param length The length in bytes of the file being uploaded. * @param mode (Optional) Stream or multipart. Default is stream. * @param revision (Optional) * If set, will add a new file into the existing passed in revision number. * If set to "head", will add a new file into the most recent revision, * and will create a new revision in this case if none exists. * @param changes (Optional) * The changes field on the affected item revision, * e.g. for recording what has changed since the previous revision. * @param license (Optional) The license for the revision. * @return The item information of the item created or changed. * * @param array $args parameters * @throws Exception */ public function uploadPerform($args) { /** @var ApihelperComponent $apihelperComponent */ $apihelperComponent = MidasLoader::loadComponent('Apihelper'); $apihelperComponent->validateParams($args, array('uploadtoken', 'filename', 'length')); $request = Zend_Controller_Front::getInstance()->getRequest(); if (!$request->isPost() && !$request->isPut()) { throw new Exception('POST or PUT method required', MIDAS_HTTP_ERROR); } list($userid, $itemid) = explode('/', $args['uploadtoken']); /** @var ItemModel $itemModel */ $itemModel = MidasLoader::loadModel('Item'); /** @var UserModel $userModel */ $userModel = MidasLoader::loadModel('User'); $userDao = $userModel->load($userid); if (!$userDao) { throw new Exception('Invalid user id from upload token', MIDAS_INVALID_PARAMETER); } $item = $itemModel->load($itemid); if ($item == false) { throw new Exception('Unable to find item', MIDAS_INVALID_PARAMETER); } if (!$itemModel->policyCheck($item, $userDao, MIDAS_POLICY_WRITE)) { throw new Exception('Permission error', MIDAS_INVALID_POLICY); } if (array_key_exists('revision', $args)) { if (strtolower($args['revision']) == 'head') { $revision = $itemModel->getLastRevision($item); // if no revision exists, it will be created later } else { $revision = $itemModel->getRevision($item, $args['revision']); if ($revision == false) { throw new Exception('Unable to find revision', MIDAS_INVALID_PARAMETER); } } } $mode = array_key_exists('mode', $args) ? $args['mode'] : 'stream'; /** @var HttpuploadComponent $httpUploadComponent */ $httpUploadComponent = MidasLoader::loadComponent('Httpupload'); $apiSetup = $apihelperComponent->getApiSetup(); $httpUploadComponent->setTestingMode(Zend_Registry::get('configGlobal')->environment === 'testing'); if (array_key_exists('testingmode', $args)) { $httpUploadComponent->setTestingMode(true); if (!array_key_exists('localinput', $args)) { $args['localinput'] = UtilityComponent::getTempDirectory() . '/' . $args['filename']; } } // Use the Httpupload component to handle the actual file upload if ($mode == 'stream') { $result = $httpUploadComponent->process($args); $filename = $result['filename']; $filePath = $result['path']; $fileSize = (int) $result['size']; $fileChecksum = $result['md5']; } elseif ($mode == 'multipart') { if (!array_key_exists('file', $args) || !array_key_exists('file', $_FILES)) { throw new Exception('Parameter file is not defined', MIDAS_INVALID_PARAMETER); } $file = $_FILES['file']; $filename = $file['name']; $filePath = $file['tmp_name']; $fileSize = (int) $file['size']; $fileChecksum = ''; } else { throw new Exception('Invalid upload mode', MIDAS_INVALID_PARAMETER); } // get the parent folder of this item and notify the callback // this is made more difficult by the fact the items can have many parents, // just use the first in the list. $parentFolders = $item->getFolders(); if (!isset($parentFolders) || !$parentFolders || count($parentFolders) === 0) { // this shouldn't happen with any self-respecting item throw new Exception('Item does not have a parent folder', MIDAS_INVALID_PARAMETER); } $firstParent = $parentFolders[0]; $validations = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_VALIDATE_UPLOAD', array('filename' => $filename, 'size' => $fileSize, 'path' => $filePath, 'folderId' => $firstParent->getFolderId())); foreach ($validations as $validation) { if (!$validation['status']) { unlink($filePath); throw new Exception($validation['message'], MIDAS_INVALID_POLICY); } } $license = array_key_exists('license', $args) ? $args['license'] : null; $changes = array_key_exists('changes', $args) ? $args['changes'] : ''; $revisionNumber = null; if (isset($revision) && $revision !== false) { $revisionNumber = $revision->getRevision(); } /** @var UploadComponent $uploadComponent */ $uploadComponent = MidasLoader::loadComponent('Upload'); $item = $uploadComponent->createNewRevision($userDao, $filename, $filePath, $changes, $item->getKey(), $revisionNumber, $license, $fileChecksum, false, $fileSize); if (!$item) { throw new Exception('Upload failed', MIDAS_INTERNAL_ERROR); } return $item->toArray(); }
/** * The client sends the results to Midas Server (put request). * * @param token * * @param array $args parameters * @throws Exception */ public function resultsserver($args) { $testingmode = false; if (isset($_GET['testingmode']) && $_GET['testingmode'] == 1) { $testingmode = true; } if (!$testingmode && $_SERVER['REQUEST_METHOD'] != 'POST') { throw new Exception('Should be a put request.', MIDAS_INVALID_PARAMETER); } $authComponent = MidasLoader::loadComponent('Authentication'); $userDao = $authComponent->getUser($args, Zend_Registry::get('userSession')->Dao); if ($userDao == false) { throw new Exception('Unable to authenticate as a server. Please check credentials.', MIDAS_INVALID_PARAMETER); } /** @var GroupModel $groupModel */ $groupModel = MidasLoader::loadModel('Group'); $groupServer = $groupModel->load(MIDAS_GROUP_SERVER_KEY); $users = $groupServer->getUsers(); $isServer = false; foreach ($users as $user) { if ($user->getKey() == $userDao->getKey()) { $isServer = true; } } if ($isServer == false) { throw new Exception('Unable to authenticate as a server. Please check credentials.', MIDAS_INVALID_PARAMETER); } if (!file_exists(UtilityComponent::getTempDirectory() . '/remoteprocessing')) { mkdir(UtilityComponent::getTempDirectory() . '/remoteprocessing'); } /** @var RandomComponent $randomComponent */ $randomComponent = MidasLoader::loadComponent('Random'); $destination = UtilityComponent::getTempDirectory() . '/remoteprocessing/' . $randomComponent->generateInt(); while (file_exists($destination)) { $destination = UtilityComponent::getTempDirectory() . '/remoteprocessing/' . $randomComponent->generateInt(); } mkdir($destination); if (!$testingmode) { move_uploaded_file($_FILES['file']['tmp_name'], $destination . '/results.zip'); } if ($testingmode) { return array(); } if (file_exists($destination . '/results.zip')) { mkdir($destination . '/content'); $target_directory = $destination . '/content'; $filter = new Zend_Filter_Decompress(array('adapter' => 'Zip', 'options' => array('target' => $target_directory))); $compressed = $filter->filter($destination . '/results.zip'); if ($compressed && file_exists($target_directory . '/parameters.txt')) { $info = file_get_contents($target_directory . '/parameters.txt'); $info = JsonComponent::decode($info); $job_id = $info['job_id']; /** @var Remoteprocessing_JobModel $jobModel */ $jobModel = MidasLoader::loadModel('Job', 'remoteprocessing'); $jobDao = $jobModel->load($job_id); $jobDao->setStatus(MIDAS_REMOTEPROCESSING_STATUS_DONE); $jobModel->save($jobDao); $info['pathResults'] = $destination . '/content'; $info['log'] = file_get_contents($target_directory . '/log.txt'); $info['userKey'] = $userDao->getKey(); Zend_Registry::get('notifier')->callback($info['resultCallback'], $info); } else { throw new Exception('Error, unable to unzip results.', MIDAS_INVALID_PARAMETER); } } else { throw new Exception('Error, unable to find results.', MIDAS_INVALID_PARAMETER); } $this->_rrmdir($destination); return array(); }
/** * Return a temporary directory. * * @param string $subDirectory * @param bool $createDirectory * @return string */ protected function getTempDirectory($subDirectory = 'misc', $createDirectory = true) { return UtilityComponent::getTempDirectory($subDirectory, $createDirectory); }
/** * Get the temporary directory. * * @return string */ protected function getTempDirectory() { return UtilityComponent::getTempDirectory(); }
/** * Google Cloud Storage upload callback. * * @param array $args * @return array * @throws Exception */ public function callback($args) { /** @var ApihelperComponent $apihelperComponent */ $apihelperComponent = MidasLoader::loadComponent('Apihelper'); $apihelperComponent->validateParams($args, array('uploadtoken')); if (!Zend_Controller_Front::getInstance()->getRequest()->isPost()) { throw new Exception('POST method required', MIDAS_HTTP_ERROR); } $uploadToken = $args['uploadtoken']; $path = UtilityComponent::getTempDirectory() . '/' . $uploadToken; if (!file_exists($path)) { throw new Exception('Invalid upload token ' . $uploadToken, MIDAS_INVALID_UPLOAD_TOKEN); } @rmdir($path); list($userId, $itemId) = explode('/', $uploadToken); /** @var UserModel $userModel */ $userModel = MidasLoader::loadModel('User'); $userDao = $userModel->load($userId); if ($userDao === false) { throw new Exception('Invalid user id from upload token', MIDAS_INVALID_PARAMETER); } /** @var ItemModel $itemModel */ $itemModel = MidasLoader::loadModel('Item'); $itemDao = $itemModel->load($itemId); if ($itemDao === false) { throw new Exception('Unable to find item', MIDAS_INVALID_PARAMETER); } if (!$itemModel->policyCheck($itemDao, $userDao, MIDAS_POLICY_WRITE)) { throw new Exception('Permission error', MIDAS_INVALID_POLICY); } if (array_key_exists('revision', $args)) { if (strtolower($args['revision']) == 'head') { $revision = $itemModel->getLastRevision($itemDao); } else { $revision = $itemModel->getRevision($itemDao, $args['revision']); if ($revision == false) { throw new Exception('Unable to find revision', MIDAS_INVALID_PARAMETER); } } } if (!array_key_exists('file', $args) || !array_key_exists('file', $_FILES)) { throw new Exception('Parameter file is not defined', MIDAS_INVALID_PARAMETER); } $file = $_FILES['file']; $name = $file['name']; $tmpName = $file['tmp_name']; $size = filesize($tmpName); $folderDaos = $itemDao->getFolders(); /** @var FolderDao $folderDao */ $folderDao = $folderDaos[0]; $validations = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_VALIDATE_UPLOAD', array('filename' => $name, 'size' => $size, 'path' => $tmpName, 'folderId' => $folderDao->getFolderId())); foreach ($validations as $validation) { if (!$validation['status']) { throw new Exception($validation['message'], MIDAS_INVALID_POLICY); } } $license = isset($args['license']) ? $args['license'] : null; $checksum = md5_file($tmpName); $revisionNumber = null; if (isset($revision) && $revision !== false) { $revisionNumber = $revision->getRevision(); } if (isset($args['changes'])) { $changes = $args['changes']; } elseif ($revisionNumber === 1) { $changes = 'Initial revision'; } else { $changes = ''; } move_uploaded_file($tmpName, $path); /** @var UploadComponent $uploadComponent */ $uploadComponent = MidasLoader::loadComponent('Upload'); $itemDao = $uploadComponent->createNewRevision($userDao, $name, $path, $changes, $itemDao->getKey(), $revisionNumber, $license, $checksum, false, $size); if ($itemDao === false) { throw new Exception('Upload failed', MIDAS_INTERNAL_ERROR); } return $itemDao->toArray(); }
/** * Extract a zip and returns the path where it was extracted. */ protected function _extractZip($bitstreamDao, $progressDao) { $dir = UtilityComponent::getTempDirectory() . '/archive_' . $bitstreamDao->getKey() . '_' . time(); if (!mkdir($dir)) { throw new Zend_Exception('Could not write into temp directory'); } $nativeCommand = $this->Setting->getValueByName(ARCHIVE_UNZIP_COMMAND_KEY, $this->moduleName); if ($nativeCommand && $bitstreamDao->getSizebytes() > 1024 * 1024 * 1024) { // Only use native exe on zips over 1GB $this->_extractZipNative($bitstreamDao, $nativeCommand, $dir, $progressDao); } else { $this->_extractZipPhp($bitstreamDao, $dir, $progressDao); } return $dir; }
/** * Use thumbnailer to pre-process a bitstream to generate a jpeg file. * Echoes an error message if a problem occurs (for the scheduler log). * * @param string $name name of the image to be pre-processed * @param string $fullPath absolute path to the image to be pre-processed * @return string * @throws Zend_Exception */ public function preprocessByThumbnailer($name, $fullPath) { $tmpPath = UtilityComponent::getTempDirectory('thumbnailcreator'); if (!file_exists($tmpPath)) { throw new Zend_Exception('Temporary thumbnail dir does not exist: ' . $tmpPath); } $copyDestination = $tmpPath . '/' . $name; copy($fullPath, $copyDestination); $jpegDestination = $tmpPath . '/' . $name . '.jpg'; /** @var RandomComponent $randomComponent */ $randomComponent = MidasLoader::loadComponent('Random'); while (file_exists($jpegDestination)) { $jpegDestination = $tmpPath . '/' . $name . $randomComponent->generateInt() . '.jpg'; } /** @var SettingModel $settingModel */ $settingModel = MidasLoader::loadModel('Setting'); $thumbnailerPath = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_THUMBNAILER_KEY, $this->moduleName); $thumbnailerParams = array($copyDestination, $jpegDestination); $thumbnailerCmd = KWUtils::prepareExeccommand($thumbnailerPath, $thumbnailerParams); if (KWUtils::isExecutable($thumbnailerPath)) { KWUtils::exec($thumbnailerCmd); } else { unlink($copyDestination); throw new Zend_Exception('Thumbnailer does not exist or you do not have execute permission. Please check the configuration of thumbnailcreator module.'); } if (!file_exists($jpegDestination)) { unlink($copyDestination); throw new Zend_Exception('Problem executing thumbnailer on your system'); } else { unlink($copyDestination); return $jpegDestination; } }
/** * Get the amount of data already uploaded. * * @param array $args * @return array * @throws Exception */ public function getOffset($args) { // check parameters if (!array_key_exists($this->tokenParamName, $args)) { throw new Exception('Parameter ' . $this->tokenParamName . ' is not defined', MIDAS_HTTPUPLOAD_PARAM_UNDEFINED); } $uploadToken = $args[$this->tokenParamName]; $offset = UtilityComponent::fileSize(UtilityComponent::getTempDirectory() . '/' . $uploadToken); return array('offset' => $offset); }
} } $items = $publicItems; if (empty($items)) { $errors .= oai_error('noRecordsMatch'); } } // break and clean up on error if ($errors != '') { oai_exit(); } $output .= " <ListIdentifiers>\n"; // Will we need a ResumptionToken? if (count($items) - $deliveredrecords > $MAXIDS) { $token = get_token(); $tokensDir = UtilityComponent::getTempDirectory() . '/tokens'; if (!is_dir($tokensDir)) { mkdir($tokensDir); } $tokensPath = $tokensDir . '/id-' . $token; $fp = fopen($tokensPath, 'w'); $thendeliveredrecords = (int) $deliveredrecords + $MAXIDS; fwrite($fp, "{$thendeliveredrecords}#"); fwrite($fp, "{$extquery}#"); fclose($fp); $num_rows = count($items); $restoken = ' <resumptionToken expirationDate="' . $expirationdatetime . '" completeListSize="' . $num_rows . '" cursor="' . $deliveredrecords . '">' . $token . "</resumptionToken>\n"; } elseif (isset($set_resumptionToken)) { $num_rows = count($items);
/** Create default asset store. */ function createDefaultAssetstore() { Zend_Registry::set('models', array()); /** @var AssetstoreModel $assetStoreModel */ $assetStoreModel = MidasLoader::loadModel('Assetstore'); // path munging require_once BASE_PATH . '/core/controllers/components/UtilityComponent.php'; $testAssetstoreBase = UtilityComponent::getTempDirectory() . '/test/'; $testAssetstoreBase = str_replace('tests/../', '', $testAssetstoreBase); $testAssetstoreBase = str_replace('//', '/', $testAssetstoreBase); // create assetstore directory if (!is_dir($testAssetstoreBase)) { mkdir($testAssetstoreBase); } $testAssetstore = $testAssetstoreBase . '/assetstore'; if (!is_dir($testAssetstore)) { mkdir($testAssetstore); } // create default assetstore in db require_once BASE_PATH . '/core/models/dao/AssetstoreDao.php'; $assetstoreDao = new AssetstoreDao(); $assetstoreDao->setName('Default'); $assetstoreDao->setPath($testAssetstore); $assetstoreDao->setType(MIDAS_ASSETSTORE_LOCAL); $assetStoreModel->save($assetstoreDao); }
/** createParaviewPath */ public function createParaviewPath() { /** @var SettingModel $settingModel */ $settingModel = MidasLoader::loadModel('Setting'); $customTemporaryDirectory = $settingModel->getValueByName(VISUALIZE_TEMPORARY_DIRECTORY_KEY, $this->moduleName); if (isset($customTemporaryDirectory) && !empty($customTemporaryDirectory)) { $tmp_dir = $customTemporaryDirectory; if (!file_exists($tmp_dir) || !is_writable($tmp_dir)) { throw new Zend_Exception('Unable to access temp dir'); } } else { if (!file_exists(UtilityComponent::getTempDirectory() . '/visualize')) { mkdir(UtilityComponent::getTempDirectory() . '/visualize'); } $tmp_dir = UtilityComponent::getTempDirectory() . '/visualize'; } $dir = opendir($tmp_dir); while ($entry = readdir($dir)) { if (is_dir($tmp_dir . '/' . $entry) && filemtime($tmp_dir . '/' . $entry) < strtotime('-1 hours') && !in_array($entry, array('.', '..'))) { if (strpos($entry, 'Paraview') !== false) { $this->_rrmdir($tmp_dir . '/' . $entry); } } } /** @var RandomComponent $randomComponent */ $randomComponent = MidasLoader::loadComponent('Random'); $tmpFolderName = 'ParaviewWeb_' . $randomComponent->generateInt(); $path = $tmp_dir . '/' . $tmpFolderName; while (!mkdir($path)) { $tmpFolderName = 'ParaviewWeb_' . $randomComponent->generateInt(); $path = $tmp_dir . '/' . $tmpFolderName; } return array('path' => $path, 'folderName' => $tmpFolderName); }
/** tests recursiveRemoveDirectory function */ public function testRecursiveRemoveDirectory() { // test some basic exception handling $this->assertFalse(KWUtils::recursiveRemoveDirectory('')); $this->assertFalse(KWUtils::recursiveRemoveDirectory('thisstringisunlikelytobeadirectory')); // create a two-level directory $testParentDir = UtilityComponent::getTempDirectory() . '/KWUtilsParentDir'; mkdir($testParentDir); $testChildDir = UtilityComponent::getTempDirectory() . '/KWUtilsParentDir/ChildDir'; mkdir($testChildDir); copy(BASE_PATH . '/tests/testfiles/search.png', $testChildDir . '/testContent.png'); $this->assertTrue(file_exists($testChildDir . '/testContent.png')); // recursively remove the directory KWUtils::recursiveRemoveDirectory($testParentDir); $this->assertFalse(file_exists($testParentDir)); }
/** * Handle the get dashboard callback. * * @return array map of dashboard fields to their values */ public function getDasboard() { return array('Data Folder Writable' => array(is_writable(UtilityComponent::getDataDirectory())), 'Temporary Folder Writable' => array(is_writable(UtilityComponent::getTempDirectory('')))); }