PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/ojs/ojs-2.3.1-2/pages/manager/FilesHandler.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 172 lines | 110 code | 33 blank | 29 comment | 19 complexity | a2f79edb4182b4990e8b9731d17b2b49 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file FilesHandler.inc.php
  4. *
  5. * Copyright (c) 2003-2009 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class FilesHandler
  9. * @ingroup pages_manager
  10. *
  11. * @brief Handle requests for files browser functions.
  12. */
  13. // $Id: FilesHandler.inc.php,v 1.21 2009/05/12 15:59:30 asmecher Exp $
  14. import('pages.manager.ManagerHandler');
  15. class FilesHandler extends ManagerHandler {
  16. /**
  17. * Constructor
  18. **/
  19. function FilesHandler() {
  20. parent::ManagerHandler();
  21. }
  22. /**
  23. * Display the files associated with a journal.
  24. */
  25. function files($args) {
  26. $this->validate();
  27. $this->setupTemplate(true);
  28. import('file.FileManager');
  29. $templateMgr =& TemplateManager::getManager();
  30. $templateMgr->assign('pageHierarchy', array(array(Request::url(null, 'manager'), 'manager.journalManagement')));
  31. FilesHandler::parseDirArg($args, $currentDir, $parentDir);
  32. $currentPath = FilesHandler::getRealFilesDir($currentDir);
  33. if (@is_file($currentPath)) {
  34. $fileMgr = new FileManager();
  35. if (Request::getUserVar('download')) {
  36. $fileMgr->downloadFile($currentPath);
  37. } else {
  38. $fileMgr->viewFile($currentPath, FilesHandler::fileMimeType($currentPath));
  39. }
  40. } else {
  41. $files = array();
  42. if ($dh = @opendir($currentPath)) {
  43. while (($file = readdir($dh)) !== false) {
  44. if ($file != '.' && $file != '..') {
  45. $filePath = $currentPath . '/'. $file;
  46. $isDir = is_dir($filePath);
  47. $info = array(
  48. 'name' => $file,
  49. 'isDir' => $isDir,
  50. 'mimetype' => $isDir ? '' : FilesHandler::fileMimeType($filePath),
  51. 'mtime' => filemtime($filePath),
  52. 'size' => $isDir ? '' : FileManager::getNiceFileSize(filesize($filePath)),
  53. );
  54. $files[$file] = $info;
  55. }
  56. }
  57. closedir($dh);
  58. }
  59. ksort($files);
  60. $templateMgr->assign_by_ref('files', $files);
  61. $templateMgr->assign('currentDir', $currentDir);
  62. $templateMgr->assign('parentDir', $parentDir);
  63. $templateMgr->assign('helpTopicId','journal.managementPages.fileBrowser');
  64. $templateMgr->display('manager/files/index.tpl');
  65. }
  66. }
  67. /**
  68. * Upload a new file.
  69. */
  70. function fileUpload($args) {
  71. $this->validate();
  72. FilesHandler::parseDirArg($args, $currentDir, $parentDir);
  73. $currentPath = FilesHandler::getRealFilesDir($currentDir);
  74. import('file.FileManager');
  75. $fileMgr = new FileManager();
  76. if ($fileMgr->uploadedFileExists('file')) {
  77. $destPath = $currentPath . '/' . FilesHandler::cleanFileName($fileMgr->getUploadedFileName('file'));
  78. @$fileMgr->uploadFile('file', $destPath);
  79. }
  80. Request::redirect(null, null, 'files', explode('/', $currentDir));
  81. }
  82. /**
  83. * Create a new directory
  84. */
  85. function fileMakeDir($args) {
  86. $this->validate();
  87. FilesHandler::parseDirArg($args, $currentDir, $parentDir);
  88. if ($dirName = Request::getUserVar('dirName')) {
  89. $currentPath = FilesHandler::getRealFilesDir($currentDir);
  90. $newDir = $currentPath . '/' . FilesHandler::cleanFileName($dirName);
  91. import('file.FileManager');
  92. $fileMgr = new FileManager();
  93. @$fileMgr->mkdir($newDir);
  94. }
  95. Request::redirect(null, null, 'files', explode('/', $currentDir));
  96. }
  97. function fileDelete($args) {
  98. $this->validate();
  99. FilesHandler::parseDirArg($args, $currentDir, $parentDir);
  100. $currentPath = FilesHandler::getRealFilesDir($currentDir);
  101. import('file.FileManager');
  102. $fileMgr = new FileManager();
  103. if (@is_file($currentPath)) {
  104. $fileMgr->deleteFile($currentPath);
  105. } else {
  106. // TODO Use recursive delete (rmtree) instead?
  107. @$fileMgr->rmdir($currentPath);
  108. }
  109. Request::redirect(null, null, 'files', explode('/', $parentDir));
  110. }
  111. //
  112. // Helper functions
  113. // FIXME Move some of these functions into common class (FileManager?)
  114. //
  115. function parseDirArg($args, &$currentDir, &$parentDir) {
  116. $pathArray = array_filter($args, array('FilesHandler', 'fileNameFilter'));
  117. $currentDir = join($pathArray, '/');
  118. array_pop($pathArray);
  119. $parentDir = join($pathArray, '/');
  120. }
  121. function getRealFilesDir($currentDir) {
  122. $journal =& Request::getJournal();
  123. return Config::getVar('files', 'files_dir') . '/journals/' . $journal->getJournalId() .'/' . $currentDir;
  124. }
  125. function fileNameFilter($var) {
  126. return (!empty($var) && $var != '..' && $var != '.');
  127. }
  128. function cleanFileName($var) {
  129. $var = String::regexp_replace('/[^\w\-\.]/', '', $var);
  130. if (!FilesHandler::fileNameFilter($var)) {
  131. $var = time() . '';
  132. }
  133. return $var;
  134. }
  135. function fileMimeType($filePath) {
  136. return String::mime_content_type($filePath);
  137. }
  138. }
  139. ?>